Skip to content

Commit

Permalink
libbpf-tools/biotop:Add PID filter support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ting Zhang authored and yonghong-song committed Dec 7, 2023
1 parent 61230b2 commit c1172cb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
21 changes: 18 additions & 3 deletions libbpf-tools/biotop.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "maps.bpf.h"
#include "core_fixes.bpf.h"

const volatile pid_t target_pid = 0;

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 10240);
Expand All @@ -34,10 +36,18 @@ static __always_inline
int trace_start(struct request *req)
{
struct who_t who = {};
__u64 pid_tgid;
__u32 pid;

/* cache PID and comm by-req */
pid_tgid = bpf_get_current_pid_tgid();
pid = pid_tgid >> 32;

if (target_pid && target_pid != pid)
return 0;

bpf_get_current_comm(&who.name, sizeof(who.name));
who.pid = bpf_get_current_pid_tgid() >> 32;
who.pid = pid;
bpf_map_update_elem(&whobyreq, &req, &who, 0);

return 0;
Expand Down Expand Up @@ -66,11 +76,16 @@ int trace_done(struct request *req)
struct gendisk *disk;
struct who_t *whop;
u64 delta_us;
u64 id = bpf_get_current_pid_tgid();
u32 pid = id >> 32;

if (target_pid && target_pid != pid)
goto cleanup;

/* fetch timestamp and calculate delta */
startp = bpf_map_lookup_elem(&start, &req);
if (!startp)
return 0; /* missed tracing issue */
goto cleanup; /* missed tracing issue */

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

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

cleanup:
bpf_map_delete_elem(&start, &req);
bpf_map_delete_elem(&whobyreq, &req);

return 0;
}

Expand Down
21 changes: 18 additions & 3 deletions libbpf-tools/biotop.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
* Based on biotop(8) from BCC by Brendan Gregg.
* 03-Mar-2022 Francis Laniel Created this.
* 23-Nov-2023 Pcheng Cui Add PID filter support.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
Expand Down Expand Up @@ -83,6 +84,7 @@ static int output_rows = 20;
static int sort_by = ALL;
static int interval = 1;
static int count = 99999999;
static pid_t target_pid = 0;
static bool verbose = false;

const char *argp_program_version = "biotop 0.1";
Expand All @@ -91,24 +93,26 @@ const char *argp_program_bug_address =
const char argp_program_doc[] =
"Trace file reads/writes by process.\n"
"\n"
"USAGE: biotop [-h] [interval] [count]\n"
"USAGE: biotop [-h] [interval] [count] [-p PID]\n"
"\n"
"EXAMPLES:\n"
" biotop # file I/O top, refresh every 1s\n"
" biotop 5 10 # 5s summaries, 10 times\n";
" biotop 5 10 # 5s summaries, 10 times\n"
" biotop -p 181 # only trace PID 1216\n";

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

static error_t parse_arg(int key, char *arg, struct argp_state *state)
{
long rows;
long rows, pid;
static int pos_args;

switch (key) {
Expand Down Expand Up @@ -140,6 +144,15 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
if (output_rows > OUTPUT_ROWS_LIMIT)
output_rows = OUTPUT_ROWS_LIMIT;
break;
case 'p':
errno = 0;
pid = strtol(arg, NULL, 10);
if (errno || pid <= 0) {
warn("Invalid PID: %s\n", arg);
argp_usage(state);
}
target_pid = pid;
break;
case 'v':
verbose = true;
break;
Expand Down Expand Up @@ -409,6 +422,8 @@ int main(int argc, char **argv)
return 1;
}

obj->rodata->target_pid = target_pid;

parse_disk_stat();

ksyms = ksyms__load();
Expand Down

0 comments on commit c1172cb

Please sign in to comment.