Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf script: Support relative time

When comparing time stamps in 'perf script' traces it can be annoying to
work with the full perf time stamps.

Add a --reltime option that displays time stamps relative to the trace
start to make it easier to read the traces.

Note: not currently supported for --time. Report an error in this
case.

Before:

% perf script
swapper 0 [000] 245402.891216: 1 cycles:ppp: ffffffffa0068814 native_write_msr+0x4 ([kernel.kallsyms])
swapper 0 [000] 245402.891223: 1 cycles:ppp: ffffffffa0068814 native_write_msr+0x4 ([kernel.kallsyms])
swapper 0 [000] 245402.891227: 5 cycles:ppp: ffffffffa0068814 native_write_msr+0x4 ([kernel.kallsyms])
swapper 0 [000] 245402.891231: 41 cycles:ppp: ffffffffa0068816 native_write_msr+0x6 ([kernel.kallsyms])
swapper 0 [000] 245402.891235: 355 cycles:ppp: ffffffffa000dd51 intel_bts_enable_local+0x21 ([kernel.kallsyms])
swapper 0 [000] 245402.891239: 3084 cycles:ppp: ffffffffa0a0150a end_repeat_nmi+0x48 ([kernel.kallsyms])

After:

% perf script --reltime

swapper 0 [000] 0.000000: 1 cycles:ppp: ffffffffa0068814 native_write_msr+0x4 ([kernel.kallsyms])
swapper 0 [000] 0.000006: 1 cycles:ppp: ffffffffa0068814 native_write_msr+0x4 ([kernel.kallsyms])
swapper 0 [000] 0.000010: 5 cycles:ppp: ffffffffa0068814 native_write_msr+0x4 ([kernel.kallsyms])
swapper 0 [000] 0.000014: 41 cycles:ppp: ffffffffa0068816 native_write_msr+0x6 ([kernel.kallsyms])
swapper 0 [000] 0.000018: 355 cycles:ppp: ffffffffa000dd51 intel_bts_enable_local+0x21 ([kernel.kallsyms])
swapper 0 [000] 0.000022: 3084 cycles:ppp: ffffffffa0a0150a end_repeat_nmi+0x48 ([kernel.kallsyms])

Committer notes:

Do not use 'time' as the name of a variable, as this breaks the build on
older glibcs:

cc1: warnings being treated as errors
builtin-script.c: In function 'perf_sample__fprintf_start':
builtin-script.c:691: warning: declaration of 'time' shadows a global declaration
/usr/include/time.h:187: warning: shadowed declaration is here

Signed-off-by: Andi Kleen <ak@linux.intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
LPU-Reference: 20190314225002.30108-8-andi@firstfloor.org
Link: https://lkml.kernel.org/n/tip-bpahyi6pr9r399mvihu65fvc@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Andi Kleen and committed by
Arnaldo Carvalho de Melo
90b10f47 a4e7e6ef

+19 -2
+3
tools/perf/Documentation/perf-script.txt
··· 380 380 Set the maximum number of program blocks to print with brstackasm for 381 381 each sample. 382 382 383 + --reltime:: 384 + Print time stamps relative to trace start. 385 + 383 386 --per-event-dump:: 384 387 Create per event files with a "perf.data.EVENT.dump" name instead of 385 388 printing to stdout, useful, for instance, for generating flamegraphs.
+16 -2
tools/perf/builtin-script.c
··· 53 53 54 54 static char const *script_name; 55 55 static char const *generate_script_lang; 56 + static bool reltime; 57 + static u64 initial_time; 56 58 static bool debug_mode; 57 59 static u64 last_timestamp; 58 60 static u64 nr_unordered; ··· 688 686 } 689 687 690 688 if (PRINT_FIELD(TIME)) { 691 - nsecs = sample->time; 689 + u64 t = sample->time; 690 + if (reltime) { 691 + if (!initial_time) 692 + initial_time = sample->time; 693 + t = sample->time - initial_time; 694 + } 695 + nsecs = t; 692 696 secs = nsecs / NSEC_PER_SEC; 693 697 nsecs -= secs * NSEC_PER_SEC; 694 698 ··· 702 694 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs); 703 695 else { 704 696 char sample_time[32]; 705 - timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time)); 697 + timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time)); 706 698 printed += fprintf(fp, "%12s: ", sample_time); 707 699 } 708 700 } ··· 3421 3413 "Set the maximum stack depth when parsing the callchain, " 3422 3414 "anything beyond the specified depth will be ignored. " 3423 3415 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)), 3416 + OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"), 3424 3417 OPT_BOOLEAN('I', "show-info", &show_full_info, 3425 3418 "display extended information from perf.data file"), 3426 3419 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path, ··· 3494 3485 "(see 'perf script -l' for listing)\n"); 3495 3486 return -1; 3496 3487 } 3488 + } 3489 + 3490 + if (script.time_str && reltime) { 3491 + fprintf(stderr, "Don't combine --reltime with --time\n"); 3492 + return -1; 3497 3493 } 3498 3494 3499 3495 if (itrace_synth_opts.callchain &&