Skip to content

Commit 4802138

Browse files
Jin Yaoacmel
authored andcommitted
perf diff: Support --time filter option
To improve 'perf diff', implement a --time filter option to diff the samples within given time window. It supports time percent with multiple time ranges. The time string format is 'a%/n,b%/m,...' or 'a%-b%,c%-%d,...'. For example: Select the second 10% time slice to diff: perf diff --time 10%/2 Select from 0% to 10% time slice to diff: perf diff --time 0%-10% Select the first and the second 10% time slices to diff: perf diff --time 10%/1,10%/2 Select from 0% to 10% and 30% to 40% slices to diff: perf diff --time 0%-10%,30%-40% It also supports analysing samples within a given time window <start>,<stop>. Times have the format seconds.microseconds. If 'start' is not given (i.e., time string is ',x.y') then analysis starts at the beginning of the file. If the stop time is not given (i.e, time string is 'x.y,') then analysis goes to end of file. Time string is 'a1.b1,c1.d1:a2.b2,c2.d2'. Use ':' to separate timestamps for different perf.data files. For example, we get the timestamp information from perf script. perf script -i perf.data.old mgen 13940 [000] 3946.361400: ... perf script -i perf.data mgen 13940 [000] 3971.150589 ... perf diff --time 3946.361400,:3971.150589, It analyzes the perf.data.old from the timestamp 3946.361400 to the end of perf.data.old and analyzes the perf.data from the timestamp 3971.150589 to the end of perf.data. v4: --- Update abstime_str_dup(), let it return error if strdup is failed, and update __cmd_diff() accordingly. Signed-off-by: Jin Yao <yao.jin@linux.intel.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Jin Yao <yao.jin@intel.com> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/1551791143-10334-2-git-send-email-yao.jin@linux.intel.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 1532593 commit 4802138

2 files changed

Lines changed: 179 additions & 14 deletions

File tree

‎tools/perf/Documentation/perf-diff.txt‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,51 @@ OPTIONS
118118
sum of shown entries will be always 100%. "absolute" means it retains
119119
the original value before and after the filter is applied.
120120

121+
--time::
122+
Analyze samples within given time window. It supports time
123+
percent with multiple time ranges. Time string is 'a%/n,b%/m,...'
124+
or 'a%-b%,c%-%d,...'.
125+
126+
For example:
127+
128+
Select the second 10% time slice to diff:
129+
130+
perf diff --time 10%/2
131+
132+
Select from 0% to 10% time slice to diff:
133+
134+
perf diff --time 0%-10%
135+
136+
Select the first and the second 10% time slices to diff:
137+
138+
perf diff --time 10%/1,10%/2
139+
140+
Select from 0% to 10% and 30% to 40% slices to diff:
141+
142+
perf diff --time 0%-10%,30%-40%
143+
144+
It also supports analyzing samples within a given time window
145+
<start>,<stop>. Times have the format seconds.microseconds. If 'start'
146+
is not given (i.e., time string is ',x.y') then analysis starts at
147+
the beginning of the file. If stop time is not given (i.e, time
148+
string is 'x.y,') then analysis goes to the end of the file. Time string is
149+
'a1.b1,c1.d1:a2.b2,c2.d2'. Use ':' to separate timestamps for different
150+
perf.data files.
151+
152+
For example, we get the timestamp information from 'perf script'.
153+
154+
perf script -i perf.data.old
155+
mgen 13940 [000] 3946.361400: ...
156+
157+
perf script -i perf.data
158+
mgen 13940 [000] 3971.150589 ...
159+
160+
perf diff --time 3946.361400,:3971.150589,
161+
162+
It analyzes the perf.data.old from the timestamp 3946.361400 to
163+
the end of perf.data.old and analyzes the perf.data from the
164+
timestamp 3971.150589 to the end of perf.data.
165+
121166
COMPARISON
122167
----------
123168
The comparison is governed by the baseline file. The baseline perf.data

‎tools/perf/builtin-diff.c‎

Lines changed: 134 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,21 @@
1919
#include "util/util.h"
2020
#include "util/data.h"
2121
#include "util/config.h"
22+
#include "util/time-utils.h"
2223

2324
#include <errno.h>
2425
#include <inttypes.h>
2526
#include <stdlib.h>
2627
#include <math.h>
2728

29+
struct perf_diff {
30+
struct perf_tool tool;
31+
const char *time_str;
32+
struct perf_time_interval *ptime_range;
33+
int range_size;
34+
int range_num;
35+
};
36+
2837
/* Diff command specific HPP columns. */
2938
enum {
3039
PERF_HPP_DIFF__BASELINE,
@@ -323,16 +332,22 @@ static int formula_fprintf(struct hist_entry *he, struct hist_entry *pair,
323332
return -1;
324333
}
325334

326-
static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
335+
static int diff__process_sample_event(struct perf_tool *tool,
327336
union perf_event *event,
328337
struct perf_sample *sample,
329338
struct perf_evsel *evsel,
330339
struct machine *machine)
331340
{
341+
struct perf_diff *pdiff = container_of(tool, struct perf_diff, tool);
332342
struct addr_location al;
333343
struct hists *hists = evsel__hists(evsel);
334344
int ret = -1;
335345

346+
if (perf_time__ranges_skip_sample(pdiff->ptime_range, pdiff->range_num,
347+
sample->time)) {
348+
return 0;
349+
}
350+
336351
if (machine__resolve(machine, &al, sample) < 0) {
337352
pr_warning("problem processing %d event, skipping it.\n",
338353
event->header.type);
@@ -359,17 +374,19 @@ static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
359374
return ret;
360375
}
361376

362-
static struct perf_tool tool = {
363-
.sample = diff__process_sample_event,
364-
.mmap = perf_event__process_mmap,
365-
.mmap2 = perf_event__process_mmap2,
366-
.comm = perf_event__process_comm,
367-
.exit = perf_event__process_exit,
368-
.fork = perf_event__process_fork,
369-
.lost = perf_event__process_lost,
370-
.namespaces = perf_event__process_namespaces,
371-
.ordered_events = true,
372-
.ordering_requires_timestamps = true,
377+
static struct perf_diff pdiff = {
378+
.tool = {
379+
.sample = diff__process_sample_event,
380+
.mmap = perf_event__process_mmap,
381+
.mmap2 = perf_event__process_mmap2,
382+
.comm = perf_event__process_comm,
383+
.exit = perf_event__process_exit,
384+
.fork = perf_event__process_fork,
385+
.lost = perf_event__process_lost,
386+
.namespaces = perf_event__process_namespaces,
387+
.ordered_events = true,
388+
.ordering_requires_timestamps = true,
389+
},
373390
};
374391

375392
static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
@@ -771,26 +788,120 @@ static void data__free(struct data__file *d)
771788
}
772789
}
773790

791+
static int abstime_str_dup(char **pstr)
792+
{
793+
char *str = NULL;
794+
795+
if (pdiff.time_str && strchr(pdiff.time_str, ':')) {
796+
str = strdup(pdiff.time_str);
797+
if (!str)
798+
return -ENOMEM;
799+
}
800+
801+
*pstr = str;
802+
return 0;
803+
}
804+
805+
static int parse_absolute_time(struct data__file *d, char **pstr)
806+
{
807+
char *p = *pstr;
808+
int ret;
809+
810+
/*
811+
* Absolute timestamp for one file has the format: a.b,c.d
812+
* For multiple files, the format is: a.b,c.d:a.b,c.d
813+
*/
814+
p = strchr(*pstr, ':');
815+
if (p) {
816+
if (p == *pstr) {
817+
pr_err("Invalid time string\n");
818+
return -EINVAL;
819+
}
820+
821+
*p = 0;
822+
p++;
823+
if (*p == 0) {
824+
pr_err("Invalid time string\n");
825+
return -EINVAL;
826+
}
827+
}
828+
829+
ret = perf_time__parse_for_ranges(*pstr, d->session,
830+
&pdiff.ptime_range,
831+
&pdiff.range_size,
832+
&pdiff.range_num);
833+
if (ret < 0)
834+
return ret;
835+
836+
if (!p || *p == 0)
837+
*pstr = NULL;
838+
else
839+
*pstr = p;
840+
841+
return ret;
842+
}
843+
844+
static int parse_percent_time(struct data__file *d)
845+
{
846+
int ret;
847+
848+
ret = perf_time__parse_for_ranges(pdiff.time_str, d->session,
849+
&pdiff.ptime_range,
850+
&pdiff.range_size,
851+
&pdiff.range_num);
852+
return ret;
853+
}
854+
855+
static int parse_time_str(struct data__file *d, char *abstime_ostr,
856+
char **pabstime_tmp)
857+
{
858+
int ret = 0;
859+
860+
if (abstime_ostr)
861+
ret = parse_absolute_time(d, pabstime_tmp);
862+
else if (pdiff.time_str)
863+
ret = parse_percent_time(d);
864+
865+
return ret;
866+
}
867+
774868
static int __cmd_diff(void)
775869
{
776870
struct data__file *d;
777-
int ret = -EINVAL, i;
871+
int ret, i;
872+
char *abstime_ostr, *abstime_tmp;
873+
874+
ret = abstime_str_dup(&abstime_ostr);
875+
if (ret)
876+
return ret;
877+
878+
abstime_tmp = abstime_ostr;
879+
ret = -EINVAL;
778880

779881
data__for_each_file(i, d) {
780-
d->session = perf_session__new(&d->data, false, &tool);
882+
d->session = perf_session__new(&d->data, false, &pdiff.tool);
781883
if (!d->session) {
782884
pr_err("Failed to open %s\n", d->data.path);
783885
ret = -1;
784886
goto out_delete;
785887
}
786888

889+
if (pdiff.time_str) {
890+
ret = parse_time_str(d, abstime_ostr, &abstime_tmp);
891+
if (ret < 0)
892+
goto out_delete;
893+
}
894+
787895
ret = perf_session__process_events(d->session);
788896
if (ret) {
789897
pr_err("Failed to process %s\n", d->data.path);
790898
goto out_delete;
791899
}
792900

793901
perf_evlist__collapse_resort(d->session->evlist);
902+
903+
if (pdiff.ptime_range)
904+
zfree(&pdiff.ptime_range);
794905
}
795906

796907
data_process();
@@ -802,6 +913,13 @@ static int __cmd_diff(void)
802913
}
803914

804915
free(data__files);
916+
917+
if (pdiff.ptime_range)
918+
zfree(&pdiff.ptime_range);
919+
920+
if (abstime_ostr)
921+
free(abstime_ostr);
922+
805923
return ret;
806924
}
807925

@@ -849,6 +967,8 @@ static const struct option options[] = {
849967
OPT_UINTEGER('o', "order", &sort_compute, "Specify compute sorting."),
850968
OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
851969
"How to display percentage of filtered entries", parse_filter_percentage),
970+
OPT_STRING(0, "time", &pdiff.time_str, "str",
971+
"Time span (time percent or absolute timestamp)"),
852972
OPT_END()
853973
};
854974

0 commit comments

Comments
 (0)