Skip to content

Commit c1172cb

Browse files
Ting Zhangyonghong-song
authored andcommitted
libbpf-tools/biotop:Add PID filter support
1 parent 61230b2 commit c1172cb

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

libbpf-tools/biotop.bpf.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "maps.bpf.h"
1010
#include "core_fixes.bpf.h"
1111

12+
const volatile pid_t target_pid = 0;
13+
1214
struct {
1315
__uint(type, BPF_MAP_TYPE_HASH);
1416
__uint(max_entries, 10240);
@@ -34,10 +36,18 @@ static __always_inline
3436
int trace_start(struct request *req)
3537
{
3638
struct who_t who = {};
39+
__u64 pid_tgid;
40+
__u32 pid;
3741

3842
/* cache PID and comm by-req */
43+
pid_tgid = bpf_get_current_pid_tgid();
44+
pid = pid_tgid >> 32;
45+
46+
if (target_pid && target_pid != pid)
47+
return 0;
48+
3949
bpf_get_current_comm(&who.name, sizeof(who.name));
40-
who.pid = bpf_get_current_pid_tgid() >> 32;
50+
who.pid = pid;
4151
bpf_map_update_elem(&whobyreq, &req, &who, 0);
4252

4353
return 0;
@@ -66,11 +76,16 @@ int trace_done(struct request *req)
6676
struct gendisk *disk;
6777
struct who_t *whop;
6878
u64 delta_us;
79+
u64 id = bpf_get_current_pid_tgid();
80+
u32 pid = id >> 32;
81+
82+
if (target_pid && target_pid != pid)
83+
goto cleanup;
6984

7085
/* fetch timestamp and calculate delta */
7186
startp = bpf_map_lookup_elem(&start, &req);
7287
if (!startp)
73-
return 0; /* missed tracing issue */
88+
goto cleanup; /* missed tracing issue */
7489

7590
delta_us = (bpf_ktime_get_ns() - startp->ts) / 1000;
7691

@@ -97,9 +112,9 @@ int trace_done(struct request *req)
97112
valp->io++;
98113
}
99114

115+
cleanup:
100116
bpf_map_delete_elem(&start, &req);
101117
bpf_map_delete_elem(&whobyreq, &req);
102-
103118
return 0;
104119
}
105120

libbpf-tools/biotop.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*
77
* Based on biotop(8) from BCC by Brendan Gregg.
88
* 03-Mar-2022 Francis Laniel Created this.
9+
* 23-Nov-2023 Pcheng Cui Add PID filter support.
910
*/
1011
#ifndef _GNU_SOURCE
1112
#define _GNU_SOURCE
@@ -83,6 +84,7 @@ static int output_rows = 20;
8384
static int sort_by = ALL;
8485
static int interval = 1;
8586
static int count = 99999999;
87+
static pid_t target_pid = 0;
8688
static bool verbose = false;
8789

8890
const char *argp_program_version = "biotop 0.1";
@@ -91,24 +93,26 @@ const char *argp_program_bug_address =
9193
const char argp_program_doc[] =
9294
"Trace file reads/writes by process.\n"
9395
"\n"
94-
"USAGE: biotop [-h] [interval] [count]\n"
96+
"USAGE: biotop [-h] [interval] [count] [-p PID]\n"
9597
"\n"
9698
"EXAMPLES:\n"
9799
" biotop # file I/O top, refresh every 1s\n"
98-
" biotop 5 10 # 5s summaries, 10 times\n";
100+
" biotop 5 10 # 5s summaries, 10 times\n"
101+
" biotop -p 181 # only trace PID 1216\n";
99102

100103
static const struct argp_option opts[] = {
101104
{ "noclear", 'C', NULL, 0, "Don't clear the screen" },
102105
{ "sort", 's', "SORT", 0, "Sort columns, default all [all, io, bytes, time]" },
103106
{ "rows", 'r', "ROWS", 0, "Maximum rows to print, default 20" },
107+
{ "pid", 'p', "PID", 0, "Process ID to trace" },
104108
{ "verbose", 'v', NULL, 0, "Verbose debug output" },
105109
{ NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
106110
{},
107111
};
108112

109113
static error_t parse_arg(int key, char *arg, struct argp_state *state)
110114
{
111-
long rows;
115+
long rows, pid;
112116
static int pos_args;
113117

114118
switch (key) {
@@ -140,6 +144,15 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
140144
if (output_rows > OUTPUT_ROWS_LIMIT)
141145
output_rows = OUTPUT_ROWS_LIMIT;
142146
break;
147+
case 'p':
148+
errno = 0;
149+
pid = strtol(arg, NULL, 10);
150+
if (errno || pid <= 0) {
151+
warn("Invalid PID: %s\n", arg);
152+
argp_usage(state);
153+
}
154+
target_pid = pid;
155+
break;
143156
case 'v':
144157
verbose = true;
145158
break;
@@ -409,6 +422,8 @@ int main(int argc, char **argv)
409422
return 1;
410423
}
411424

425+
obj->rodata->target_pid = target_pid;
426+
412427
parse_disk_stat();
413428

414429
ksyms = ksyms__load();

0 commit comments

Comments
 (0)