Skip to content

Commit 6c8f442

Browse files
committed
tools/filetop: Add directory filter
Add support to filtop to filter by directory. Signed-off-by: Srivathsa Dara <[email protected]>
1 parent 6e76832 commit 6c8f442

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

tools/filetop.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from bcc import BPF
1919
from time import sleep, strftime
2020
import argparse
21+
import os
22+
import stat
2123
from subprocess import call
2224

2325
# arguments
@@ -55,6 +57,9 @@
5557
help="number of outputs")
5658
parser.add_argument("--ebpf", action="store_true",
5759
help=argparse.SUPPRESS)
60+
parser.add_argument("-d", "--directory", type=str,
61+
help="trace this directory only")
62+
5863
args = parser.parse_args()
5964
interval = int(args.interval)
6065
countdown = int(args.count)
@@ -109,6 +114,10 @@
109114
if (d_name.len == 0 || TYPE_FILTER)
110115
return 0;
111116
117+
// skip if not in the specified directory
118+
if (DIRECTORY_FILTER)
119+
return 0;
120+
112121
// store counts and sizes by pid & file
113122
struct info_t info = {
114123
.pid = pid,
@@ -163,6 +172,16 @@
163172
bpf_text = bpf_text.replace('TYPE_FILTER', '0')
164173
else:
165174
bpf_text = bpf_text.replace('TYPE_FILTER', '!S_ISREG(mode)')
175+
if args.directory:
176+
try:
177+
directory_inode = os.lstat(args.directory)[stat.ST_INO]
178+
print(f'Tracing directory: {args.directory} (Inode: {directory_inode})')
179+
bpf_text = bpf_text.replace('DIRECTORY_FILTER', 'file->f_path.dentry->d_parent->d_inode->i_ino != %d' % directory_inode)
180+
except (FileNotFoundError, PermissionError) as e:
181+
print(f'Error accessing directory {args.directory}: {e}')
182+
exit(1)
183+
else:
184+
bpf_text = bpf_text.replace('DIRECTORY_FILTER', '0')
166185

167186
if debug or args.ebpf:
168187
print(bpf_text)

0 commit comments

Comments
 (0)