Skip to content

Conversation

@wswsmao
Copy link

@wswsmao wswsmao commented Sep 19, 2025

The commit 137bd5f introduced a directory filter. However, files under subdirectories were not being traced.
This PR adds a --recursive option which, while remaining backward-compatible, enables recursive filtering so that files in subdirectories are also matched.

tools/filetop.py Outdated
if args.recursive:
print(f'Tracing directory recursively: {args.directory} (Inode: {directory_inode})')
directory_filter = ("({ struct dentry *pde = de; int found = 0; "
"for (int i = 0; i < 50; i++) { "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Please replace the magic number 50 with a macro or constant for clarity and maintainability.

  2. Is there a specific reason why 50 is chosen as the max depth? Could you also include your thoughts on performance, BPF instruction count, and verifier safety?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1、done
2、I switched the bound to 32 and made it a macro FILETOP_MAX_DENTRY_DEPTH. 32 aligns with existing path-walking limits in this repo (e.g., libbpf-tools uses MAX_PATH_DEPTH=32) and is well above typical directory depths we see in practice.

tools/filetop.py Outdated
bpf_text = bpf_text.replace('DIRECTORY_FILTER', 'file->f_path.dentry->d_parent->d_inode->i_ino != %d' % directory_inode)
if args.recursive:
print(f'Tracing directory recursively: {args.directory} (Inode: {directory_inode})')
directory_filter = ("({ struct dentry *pde = de; int found = 0; "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The directory_filter code for the recursive case is hard to read.
Could you please refactor it for better readability and maintainability?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

tools/filetop.py Outdated
// Limit dentry parent walk depth to satisfy eBPF verifier loop
#ifndef FILETOP_MAX_DENTRY_DEPTH
#define FILETOP_MAX_DENTRY_DEPTH 32
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While FILETOP_MAX_DENTRY_DEPTH is below 32 in most cases, there is still a chance that it could exceed this limit in certain scenarios. If that happens, the tool's behavior might deviate from the user's expectations. Would it be worth notifying users about this limitation to avoid potential confusion?

Copy link
Author

@wswsmao wswsmao Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, add max depth log in --help and Tracing directory recursively log

{
struct dentry *pde = de;
int found = 0;
#pragma unroll
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a blank line after the variable declaration section.

exit(1)
else:
if args.recursive:
print("Error: --recursive can only be used with -d/--directory option")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about -R/--recursive?

parser.add_argument("-d", "--directory", type=str,
help="trace this directory only")
parser.add_argument("-R", "--recursive", action="store_true",
help=f"when used with -d, also trace files in subdirectories (max depth {MAX_DENTRY_DEPTH_VAL})")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to make it clearer that this is only available when using -d. The current wording makes it seem like there could be cases where it's possible to use without -d.
For example, "This feature is only supported when used with the -d option, tracing files inside subdirectories."

{
struct dentry *pde = de;
int found = 0;
#pragma unroll
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is #pragma unroll (and __always_inline) intended?
please refer to:
https://lwn.net/Articles/1017116/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants