Skip to content

Commit 90b10f4

Browse files
Andi Kleenacmel
authored andcommitted
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 <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Acked-by: Jiri Olsa <[email protected]> LPU-Reference: [email protected] Link: https://lkml.kernel.org/n/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent a4e7e6e commit 90b10f4

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

tools/perf/Documentation/perf-script.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@ include::itrace.txt[]
380380
Set the maximum number of program blocks to print with brstackasm for
381381
each sample.
382382

383+
--reltime::
384+
Print time stamps relative to trace start.
385+
383386
--per-event-dump::
384387
Create per event files with a "perf.data.EVENT.dump" name instead of
385388
printing to stdout, useful, for instance, for generating flamegraphs.

tools/perf/builtin-script.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353

5454
static char const *script_name;
5555
static char const *generate_script_lang;
56+
static bool reltime;
57+
static u64 initial_time;
5658
static bool debug_mode;
5759
static u64 last_timestamp;
5860
static u64 nr_unordered;
@@ -686,15 +688,21 @@ static int perf_sample__fprintf_start(struct perf_sample *sample,
686688
}
687689

688690
if (PRINT_FIELD(TIME)) {
689-
nsecs = sample->time;
691+
u64 t = sample->time;
692+
if (reltime) {
693+
if (!initial_time)
694+
initial_time = sample->time;
695+
t = sample->time - initial_time;
696+
}
697+
nsecs = t;
690698
secs = nsecs / NSEC_PER_SEC;
691699
nsecs -= secs * NSEC_PER_SEC;
692700

693701
if (symbol_conf.nanosecs)
694702
printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
695703
else {
696704
char sample_time[32];
697-
timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
705+
timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
698706
printed += fprintf(fp, "%12s: ", sample_time);
699707
}
700708
}
@@ -3413,6 +3421,7 @@ int cmd_script(int argc, const char **argv)
34133421
"Set the maximum stack depth when parsing the callchain, "
34143422
"anything beyond the specified depth will be ignored. "
34153423
"Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
3424+
OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
34163425
OPT_BOOLEAN('I', "show-info", &show_full_info,
34173426
"display extended information from perf.data file"),
34183427
OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
@@ -3487,6 +3496,11 @@ int cmd_script(int argc, const char **argv)
34873496
}
34883497
}
34893498

3499+
if (script.time_str && reltime) {
3500+
fprintf(stderr, "Don't combine --reltime with --time\n");
3501+
return -1;
3502+
}
3503+
34903504
if (itrace_synth_opts.callchain &&
34913505
itrace_synth_opts.callchain_sz > scripting_max_stack)
34923506
scripting_max_stack = itrace_synth_opts.callchain_sz;

0 commit comments

Comments
 (0)