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;
8384static int sort_by = ALL ;
8485static int interval = 1 ;
8586static int count = 99999999 ;
87+ static pid_t target_pid = 0 ;
8688static bool verbose = false;
8789
8890const char * argp_program_version = "biotop 0.1" ;
@@ -91,24 +93,26 @@ const char *argp_program_bug_address =
9193const 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
100103static 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
109113static 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