Skip to content

Commit 1302d88

Browse files
committed
perf trace: Use sched:sched_stat_runtime to provide a thread summary
[root@sandy ~]# perf trace --sched --duration 0.100 --pid `pidof firefox` <SNIP> 17079.847 ( 0.009 ms): 17643 poll(ufds: 140037623086496, nfds: 11, timeout_msecs: 0) = 0 Timeout 17079.892 ( 0.010 ms): 17643 read(fd: 4, buf: 140038178943092, count: 4096 ) = -1 EAGAIN Resource temporarily unavailable 17079.921 ( 0.013 ms): 17643 poll(ufds: 140037623086496, nfds: 11, timeout_msecs: 0) = 0 Timeout 17079.949 ( 0.009 ms): 17643 read(fd: 4, buf: 140038178943092, count: 4096 ) = -1 EAGAIN Resource temporarily unavailable ^C _____________________________________________________________________ __) Summary of events (__ [ task - pid ] [ events ] [ ratio ] [ runtime ] _____________________________________________________________________ firefox - 17643 : 18013 [ 72.2% ] 359.110 ms firefox - 17663 : 41 [ 0.2% ] 21.439 ms firefox - 17664 : 6840 [ 27.4% ] 133.642 ms firefox - 17667 : 46 [ 0.2% ] 0.682 ms [root@sandy ~]# This is equivalent to the 'perf trace summary' subcomand in the tmp.perf/trace2 branch. Another example, setting a huge duration filter to get just a system wide summary: [root@sandy ~]# perf trace --duration 10000.0 --sched ^C _____________________________________________________________________ __) Summary of events (__ [ task - pid ] [ events ] [ ratio ] [ runtime ] _____________________________________________________________________ scsi_eh_1 - 258 : 15 [ 0.0% ] 0.133 ms kworker/0:1H - 322 : 13 [ 0.0% ] 0.032 ms jbd2/dm-0-8 - 384 : 4 [ 0.0% ] 0.115 ms flush-253:0 - 470 : 1 [ 0.0% ] 0.027 ms firefox - 950 : 4783 [ 0.1% ] 24.863 ms firefox - 992 : 1883 [ 0.1% ] 6.808 ms firefox - 995 : 35 [ 0.0% ] 0.111 ms ksoftirqd/6 - 4362 : 2 [ 0.0% ] 0.005 ms ksoftirqd/7 - 4365 : 1 [ 0.0% ] 0.007 ms Xorg - 4671 : 148 [ 0.0% ] 0.912 ms gnome-settings- - 4846 : 14 [ 0.0% ] 0.086 ms seahorse-daemon - 4847 : 14 [ 0.0% ] 0.092 ms gnome-panel - 4875 : 46 [ 0.0% ] 0.159 ms gnome-power-man - 4918 : 16 [ 0.0% ] 0.065 ms gvfs-afc-volume - 4992 : 77 [ 0.0% ] 0.136 ms gnome-screensav - 5114 : 24 [ 0.0% ] 0.128 ms xchat - 8082 : 466 [ 0.0% ] 2.019 ms synergyc - 8369 : 941 [ 0.0% ] 3.291 ms synergyc - 8371 : 85 [ 0.0% ] 1.817 ms jbd2/dm-4-8 - 9352 : 4 [ 0.0% ] 0.109 ms rpcbind - 9786 : 3 [ 0.0% ] 0.017 ms rtkit-daemon - 12802 : 10 [ 0.0% ] 0.038 ms rtkit-daemon - 12803 : 8 [ 0.0% ] 0.000 ms udisks-daemon - 13020 : 27 [ 0.0% ] 0.240 ms kworker/7:0 - 14651 : 669 [ 0.0% ] 2.616 ms kworker/5:1 - 16220 : 2 [ 0.0% ] 0.069 ms kworker/4:0 - 19776 : 13 [ 0.0% ] 0.176 ms openvpn - 20131 : 133 [ 0.0% ] 0.762 ms plugin-containe - 20508 : 60658 [ 1.7% ] 131.153 ms npviewer.bin - 20520 : 72208 [ 2.0% ] 138.945 ms npviewer.bin - 20542 : 35 [ 0.0% ] 0.074 ms npviewer.bin - 20543 : 30 [ 0.0% ] 0.074 ms npviewer.bin - 20547 : 35 [ 0.0% ] 0.092 ms npviewer.bin - 20552 : 35 [ 0.0% ] 0.093 ms sshd - 20645 : 32 [ 0.0% ] 0.071 ms npviewer.bin - 21053 : 35 [ 0.0% ] 0.074 ms npviewer.bin - 21054 : 35 [ 0.0% ] 0.097 ms kworker/0:2 - 21169 : 149 [ 0.0% ] 1.143 ms kworker/3:0 - 22171 : 113 [ 0.0% ] 96.892 ms flush-253:4 - 22410 : 1 [ 0.0% ] 0.028 ms kworker/6:0 - 24581 : 25 [ 0.0% ] 0.275 ms kworker/1:0 - 25572 : 4 [ 0.0% ] 0.103 ms kworker/2:1 - 26299 : 138 [ 0.0% ] 1.440 ms kworker/0:0 - 26325 : 1 [ 0.0% ] 0.003 ms perf - 26330 : 3506967 [ 96.1% ] 6648.310 ms [root@sandy ~]# Cc: David Ahern <[email protected]> Cc: Frederic Weisbecker <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Mike Galbraith <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Thomas Gleixner <[email protected]> Link: http://lkml.kernel.org/n/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent efd5745 commit 1302d88

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

tools/perf/Documentation/perf-trace.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ the thread executes on the designated CPUs. Default is to monitor all CPUs.
5151
--duration:
5252
Show only events that had a duration greater than N.M ms.
5353

54+
--sched:
55+
Accrue thread runtime and provide a summary at the end of the session.
56+
5457
SEE ALSO
5558
--------
5659
linkperf:perf-record[1], linkperf:perf-script[1]

tools/perf/builtin-trace.c

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct thread_trace {
6969
bool entry_pending;
7070
unsigned long nr_events;
7171
char *entry_str;
72+
double runtime_ms;
7273
};
7374

7475
static struct thread_trace *thread_trace__new(void)
@@ -109,8 +110,10 @@ struct trace {
109110
struct machine host;
110111
u64 base_time;
111112
unsigned long nr_events;
113+
bool sched;
112114
bool multiple_threads;
113115
double duration_filter;
116+
double runtime_ms;
114117
};
115118

116119
static bool trace__filter_duration(struct trace *trace, double t)
@@ -389,6 +392,31 @@ static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
389392
return 0;
390393
}
391394

395+
static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
396+
struct perf_sample *sample)
397+
{
398+
u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
399+
double runtime_ms = (double)runtime / NSEC_PER_MSEC;
400+
struct thread *thread = machine__findnew_thread(&trace->host, sample->tid);
401+
struct thread_trace *ttrace = thread__trace(thread);
402+
403+
if (ttrace == NULL)
404+
goto out_dump;
405+
406+
ttrace->runtime_ms += runtime_ms;
407+
trace->runtime_ms += runtime_ms;
408+
return 0;
409+
410+
out_dump:
411+
printf("%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
412+
evsel->name,
413+
perf_evsel__strval(evsel, sample, "comm"),
414+
(pid_t)perf_evsel__intval(evsel, sample, "pid"),
415+
runtime,
416+
perf_evsel__intval(evsel, sample, "vruntime"));
417+
return 0;
418+
}
419+
392420
static int trace__run(struct trace *trace, int argc, const char **argv)
393421
{
394422
struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
@@ -408,6 +436,13 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
408436
goto out_delete_evlist;
409437
}
410438

439+
if (trace->sched &&
440+
perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
441+
trace__sched_stat_runtime)) {
442+
printf("Couldn't read the sched_stat_runtime tracepoint information!\n");
443+
goto out_delete_evlist;
444+
}
445+
411446
err = perf_evlist__create_maps(evlist, &trace->opts.target);
412447
if (err < 0) {
413448
printf("Problems parsing the target to trace, check your options!\n");
@@ -521,6 +556,51 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
521556
return err;
522557
}
523558

559+
static size_t trace__fprintf_threads_header(FILE *fp)
560+
{
561+
size_t printed;
562+
563+
printed = fprintf(fp, "\n _____________________________________________________________________\n");
564+
printed += fprintf(fp," __) Summary of events (__\n\n");
565+
printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n");
566+
printed += fprintf(fp," _____________________________________________________________________\n\n");
567+
568+
return printed;
569+
}
570+
571+
static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
572+
{
573+
size_t printed = trace__fprintf_threads_header(fp);
574+
struct rb_node *nd;
575+
576+
for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) {
577+
struct thread *thread = rb_entry(nd, struct thread, rb_node);
578+
struct thread_trace *ttrace = thread->priv;
579+
const char *color;
580+
double ratio;
581+
582+
if (ttrace == NULL)
583+
continue;
584+
585+
ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
586+
587+
color = PERF_COLOR_NORMAL;
588+
if (ratio > 50.0)
589+
color = PERF_COLOR_RED;
590+
else if (ratio > 25.0)
591+
color = PERF_COLOR_GREEN;
592+
else if (ratio > 5.0)
593+
color = PERF_COLOR_YELLOW;
594+
595+
printed += color_fprintf(fp, color, "%20s", thread->comm);
596+
printed += fprintf(fp, " - %-5d :%11lu [", thread->pid, ttrace->nr_events);
597+
printed += color_fprintf(fp, color, "%5.1f%%", ratio);
598+
printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms);
599+
}
600+
601+
return printed;
602+
}
603+
524604
static int trace__set_duration(const struct option *opt, const char *str,
525605
int unset __maybe_unused)
526606
{
@@ -571,6 +651,7 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
571651
OPT_CALLBACK(0, "duration", &trace, "float",
572652
"show only events with duration > N.M ms",
573653
trace__set_duration),
654+
OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
574655
OPT_END()
575656
};
576657
int err;
@@ -595,5 +676,10 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
595676
if (!argc && perf_target__none(&trace.opts.target))
596677
trace.opts.target.system_wide = true;
597678

598-
return trace__run(&trace, argc, argv);
679+
err = trace__run(&trace, argc, argv);
680+
681+
if (trace.sched && !err)
682+
trace__fprintf_thread_summary(&trace, stdout);
683+
684+
return err;
599685
}

0 commit comments

Comments
 (0)