libbpf-tools: runqslow: add '-P' optional
authorzhenwei pi <pizhenwei@bytedance.com>
Tue, 17 Aug 2021 12:46:54 +0000 (20:46 +0800)
committeryonghong-song <ys114321@gmail.com>
Fri, 20 Aug 2021 06:30:03 +0000 (23:30 -0700)
Sync change 508d9694ba7ea503cce821175ffca5a7740b832b.

During a task hits schedule delay, in the high probability, the
previous task takes a long time to run. It's possible to dump the
previous task comm and TID by '-P' or '--previous' option.

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
libbpf-tools/runqslower.bpf.c
libbpf-tools/runqslower.c
libbpf-tools/runqslower.h

index 82c1c75..84e7aea 100644 (file)
@@ -88,8 +88,10 @@ int handle__sched_switch(u64 *ctx)
                return 0;
 
        event.pid = pid;
+       event.prev_pid = prev->pid;
        event.delta_us = delta_us;
        bpf_probe_read_kernel_str(&event.task, sizeof(event.task), next->comm);
+       bpf_probe_read_kernel_str(&event.prev_task, sizeof(event.prev_task), prev->comm);
 
        /* output */
        bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU,
index 7654ae9..3d518ac 100644 (file)
@@ -18,6 +18,7 @@ struct env {
        pid_t pid;
        pid_t tid;
        __u64 min_us;
+       bool previous;
        bool verbose;
 } env = {
        .min_us = 10000,
@@ -29,18 +30,20 @@ const char *argp_program_bug_address =
 const char argp_program_doc[] =
 "Trace high run queue latency.\n"
 "\n"
-"USAGE: runqslower [--help] [-p PID] [-t TID] [min_us]\n"
+"USAGE: runqslower [--help] [-p PID] [-t TID] [-P] [min_us]\n"
 "\n"
 "EXAMPLES:\n"
 "    runqslower         # trace latency higher than 10000 us (default)\n"
 "    runqslower 1000    # trace latency higher than 1000 us\n"
 "    runqslower -p 123  # trace pid 123\n"
-"    runqslower -t 123  # trace tid 123 (use for threads only)\n";
+"    runqslower -t 123  # trace tid 123 (use for threads only)\n"
+"    runqslower -P      # also show previous task name and TID\n";
 
 static const struct argp_option opts[] = {
        { "pid", 'p', "PID", 0, "Process PID to trace"},
        { "tid", 't', "TID", 0, "Thread TID to trace"},
        { "verbose", 'v', NULL, 0, "Verbose debug output" },
+       { "previous", 'P', NULL, 0, "also show previous task name and TID" },
        { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
        {},
 };
@@ -58,6 +61,9 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
        case 'v':
                env.verbose = true;
                break;
+       case 'P':
+               env.previous = true;
+               break;
        case 'p':
                errno = 0;
                pid = strtol(arg, NULL, 10);
@@ -114,7 +120,10 @@ void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
        time(&t);
        tm = localtime(&t);
        strftime(ts, sizeof(ts), "%H:%M:%S", tm);
-       printf("%-8s %-16s %-6d %14llu\n", ts, e->task, e->pid, e->delta_us);
+       if (env.previous)
+               printf("%-8s %-16s %-6d %14llu %-16s %-6d\n", ts, e->task, e->pid, e->delta_us, e->prev_task, e->prev_pid);
+       else
+               printf("%-8s %-16s %-6d %14llu\n", ts, e->task, e->pid, e->delta_us);
 }
 
 void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
@@ -170,7 +179,10 @@ int main(int argc, char **argv)
        }
 
        printf("Tracing run queue latency higher than %llu us\n", env.min_us);
-       printf("%-8s %-16s %-6s %14s\n", "TIME", "COMM", "TID", "LAT(us)");
+       if (env.previous)
+               printf("%-8s %-16s %-6s %14s %-16s %-6s\n", "TIME", "COMM", "TID", "LAT(us)", "PREV COMM", "PREV TID");
+       else
+               printf("%-8s %-16s %-6s %14s\n", "TIME", "COMM", "TID", "LAT(us)");
 
        pb_opts.sample_cb = handle_event;
        pb_opts.lost_cb = handle_lost_events;
index 9db2254..1e545fd 100644 (file)
@@ -6,8 +6,10 @@
 
 struct event {
        char task[TASK_COMM_LEN];
+       char prev_task[TASK_COMM_LEN];
        __u64 delta_us;
        pid_t pid;
+       pid_t prev_pid;
 };
 
 #endif /* __RUNQSLOWER_H */