Skip to content

Commit 6b9c026

Browse files
tlfalconacmel
authored andcommitted
perf record: Add ratio-to-prev term
Provide ratio-to-prev term which allows the user to set the event sample period of two events corresponding to a desired ratio. If using on an Intel x86 platform with Auto Counter Reload support, also set corresponding event's config2 attribute with a bitmask which counters to reset and which counters to sample if the desired ratio is met or exceeded. On other platforms, only the sample period is affected by the ratio-to-prev term. Reviewed-by: Ian Rogers <irogers@google.com> Signed-off-by: Thomas Falcon <thomas.falcon@intel.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Dapeng Mi <dapeng1.mi@linux.intel.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 584754c commit 6b9c026

10 files changed

Lines changed: 212 additions & 2 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Intel Auto Counter Reload Support
2+
---------------------------------
3+
Support for Intel Auto Counter Reload in perf tools
4+
5+
Auto counter reload provides a means for software to specify to hardware
6+
that certain counters, if supported, should be automatically reloaded
7+
upon overflow of chosen counters. By taking a sample only if the rate of
8+
one event exceeds some threshold relative to the rate of another event,
9+
this feature enables software to sample based on the relative rate of
10+
two or more events. To enable this, the user must provide a sample period
11+
term and a bitmask ("acr_mask") for each relevant event specifying the
12+
counters in an event group to reload if the event's specified sample
13+
period is exceeded.
14+
15+
For example, if the user desires to measure a scenario when IPC > 2,
16+
the event group might look like the one below:
17+
18+
perf record -e {cpu_atom/instructions,period=200000,acr_mask=0x2/, \
19+
cpu_atom/cycles,period=100000,acr_mask=0x3/} -- true
20+
21+
In this case, if the "instructions" counter exceeds the sample period of
22+
200000, the second counter, "cycles", will be reset and a sample will be
23+
taken. If "cycles" is exceeded first, both counters in the group will be
24+
reset. In this way, samples will only be taken for cases where IPC > 2.
25+
26+
The acr_mask term is a hexadecimal value representing a bitmask of the
27+
events in the group to be reset when the period is exceeded. In the
28+
example above, "instructions" is assigned an acr_mask of 0x2, meaning
29+
only the second event in the group is reloaded and a sample is taken
30+
for the first event. "cycles" is assigned an acr_mask of 0x3, meaning
31+
that both event counters will be reset if the sample period is exceeded
32+
first.
33+
34+
ratio-to-prev Event Term
35+
------------------------
36+
To simplify this, an event term "ratio-to-prev" is provided which is used
37+
alongside the sample period term n or the -c/--count option. This would
38+
allow users to specify the desired relative rate between events as a
39+
ratio. Note: Both events compared must belong to the same PMU.
40+
41+
The command above would then become
42+
43+
perf record -e {cpu_atom/instructions/, \
44+
cpu_atom/cycles,period=100000,ratio-to-prev=0.5/} -- true
45+
46+
ratio-to-prev is the ratio of the event using the term relative
47+
to the previous event in the group, which will always be 1,
48+
for a 1:0.5 or 2:1 ratio.
49+
50+
To sample for IPC < 2 for example, the events need to be reordered:
51+
52+
perf record -e {cpu_atom/cycles/, \
53+
cpu_atom/instructions,period=200000,ratio-to-prev=2.0/} -- true

tools/perf/Documentation/perf-list.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ Support raw format:
393393
. '--raw-dump [hw|sw|cache|tracepoint|pmu|event_glob]', shows the raw-dump of
394394
a certain kind of events.
395395

396+
include::intel-acr.txt[]
397+
396398
SEE ALSO
397399
--------
398400
linkperf:perf-stat[1], linkperf:perf-top[1],

tools/perf/arch/x86/util/evsel.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdlib.h>
55
#include "util/evlist.h"
66
#include "util/evsel.h"
7+
#include "util/evsel_config.h"
78
#include "util/env.h"
89
#include "util/pmu.h"
910
#include "util/pmus.h"
@@ -71,6 +72,57 @@ int arch_evsel__hw_name(struct evsel *evsel, char *bf, size_t size)
7172
event_name);
7273
}
7374

75+
void arch_evsel__apply_ratio_to_prev(struct evsel *evsel,
76+
struct perf_event_attr *attr)
77+
{
78+
struct perf_event_attr *prev_attr = NULL;
79+
struct evsel *evsel_prev = NULL;
80+
const char *name = "acr_mask";
81+
int evsel_idx = 0;
82+
__u64 ev_mask, pr_ev_mask;
83+
84+
if (!perf_pmu__has_format(evsel->pmu, name)) {
85+
pr_err("'%s' does not have acr_mask format support\n", evsel->pmu->name);
86+
return;
87+
}
88+
if (perf_pmu__format_type(evsel->pmu, name) !=
89+
PERF_PMU_FORMAT_VALUE_CONFIG2) {
90+
pr_err("'%s' does not have config2 format support\n", evsel->pmu->name);
91+
return;
92+
}
93+
94+
evsel_prev = evsel__prev(evsel);
95+
if (!evsel_prev) {
96+
pr_err("Previous event does not exist.\n");
97+
return;
98+
}
99+
100+
prev_attr = &evsel_prev->core.attr;
101+
102+
if (prev_attr->config2) {
103+
pr_err("'%s' has set config2 (acr_mask?) already, configuration not supported\n", evsel_prev->name);
104+
return;
105+
}
106+
107+
/*
108+
* acr_mask (config2) is calculated using the event's index in
109+
* the event group. The first event will use the index of the
110+
* second event as its mask (e.g., 0x2), indicating that the
111+
* second event counter will be reset and a sample taken for
112+
* the first event if its counter overflows. The second event
113+
* will use the mask consisting of the first and second bits
114+
* (e.g., 0x3), meaning both counters will be reset if the
115+
* second event counter overflows.
116+
*/
117+
118+
evsel_idx = evsel__group_idx(evsel);
119+
ev_mask = 1ull << evsel_idx;
120+
pr_ev_mask = 1ull << (evsel_idx - 1);
121+
122+
prev_attr->config2 = ev_mask;
123+
attr->config2 = ev_mask | pr_ev_mask;
124+
}
125+
74126
static void ibs_l3miss_warn(void)
75127
{
76128
pr_warning(

tools/perf/util/evsel.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,71 @@ static void evsel__reset_callgraph(struct evsel *evsel, struct callchain_param *
10921092
}
10931093
}
10941094

1095+
static void evsel__apply_ratio_to_prev(struct evsel *evsel,
1096+
struct perf_event_attr *attr,
1097+
struct record_opts *opts,
1098+
const char *buf)
1099+
{
1100+
struct perf_event_attr *prev_attr = NULL;
1101+
struct evsel *evsel_prev = NULL;
1102+
u64 type = evsel->core.attr.sample_type;
1103+
u64 prev_type = 0;
1104+
double rtp;
1105+
1106+
rtp = strtod(buf, NULL);
1107+
if (rtp <= 0) {
1108+
pr_err("Invalid ratio-to-prev value %lf\n", rtp);
1109+
return;
1110+
}
1111+
if (evsel == evsel__leader(evsel)) {
1112+
pr_err("Invalid use of ratio-to-prev term without preceding element in group\n");
1113+
return;
1114+
}
1115+
if (!evsel->pmu->is_core) {
1116+
pr_err("Event using ratio-to-prev term must have a core PMU\n");
1117+
return;
1118+
}
1119+
1120+
evsel_prev = evsel__prev(evsel);
1121+
if (!evsel_prev) {
1122+
pr_err("Previous event does not exist.\n");
1123+
return;
1124+
}
1125+
1126+
if (evsel_prev->pmu->type != evsel->pmu->type) {
1127+
pr_err("Compared events (\"%s\", \"%s\") must have same PMU\n",
1128+
evsel->name, evsel_prev->name);
1129+
return;
1130+
}
1131+
1132+
prev_attr = &evsel_prev->core.attr;
1133+
prev_type = evsel_prev->core.attr.sample_type;
1134+
1135+
if (!(prev_type & PERF_SAMPLE_PERIOD)) {
1136+
attr->sample_period = prev_attr->sample_period * rtp;
1137+
attr->freq = 0;
1138+
evsel__reset_sample_bit(evsel, PERIOD);
1139+
} else if (!(type & PERF_SAMPLE_PERIOD)) {
1140+
prev_attr->sample_period = attr->sample_period / rtp;
1141+
prev_attr->freq = 0;
1142+
evsel__reset_sample_bit(evsel_prev, PERIOD);
1143+
} else {
1144+
if (opts->user_interval != ULLONG_MAX) {
1145+
prev_attr->sample_period = opts->user_interval;
1146+
attr->sample_period = prev_attr->sample_period * rtp;
1147+
prev_attr->freq = 0;
1148+
attr->freq = 0;
1149+
evsel__reset_sample_bit(evsel_prev, PERIOD);
1150+
evsel__reset_sample_bit(evsel, PERIOD);
1151+
} else {
1152+
pr_err("Event period term or count (-c) must be set when using ratio-to-prev term.\n");
1153+
return;
1154+
}
1155+
}
1156+
1157+
arch_evsel__apply_ratio_to_prev(evsel, attr);
1158+
}
1159+
10951160
static void evsel__apply_config_terms(struct evsel *evsel,
10961161
struct record_opts *opts, bool track)
10971162
{
@@ -1105,6 +1170,7 @@ static void evsel__apply_config_terms(struct evsel *evsel,
11051170
u32 dump_size = 0;
11061171
int max_stack = 0;
11071172
const char *callgraph_buf = NULL;
1173+
const char *rtp_buf = NULL;
11081174

11091175
list_for_each_entry(term, config_terms, list) {
11101176
switch (term->type) {
@@ -1175,6 +1241,9 @@ static void evsel__apply_config_terms(struct evsel *evsel,
11751241
break;
11761242
case EVSEL__CONFIG_TERM_CFG_CHG:
11771243
break;
1244+
case EVSEL__CONFIG_TERM_RATIO_TO_PREV:
1245+
rtp_buf = term->val.str;
1246+
break;
11781247
default:
11791248
break;
11801249
}
@@ -1226,6 +1295,8 @@ static void evsel__apply_config_terms(struct evsel *evsel,
12261295
evsel__config_callchain(evsel, opts, &param);
12271296
}
12281297
}
1298+
if (rtp_buf)
1299+
evsel__apply_ratio_to_prev(evsel, attr, opts, rtp_buf);
12291300
}
12301301

12311302
struct evsel_config_term *__evsel__get_config_term(struct evsel *evsel, enum evsel_term_type type)
@@ -1250,6 +1321,11 @@ void __weak arch__post_evsel_config(struct evsel *evsel __maybe_unused,
12501321
{
12511322
}
12521323

1324+
void __weak arch_evsel__apply_ratio_to_prev(struct evsel *evsel __maybe_unused,
1325+
struct perf_event_attr *attr __maybe_unused)
1326+
{
1327+
}
1328+
12531329
static void evsel__set_default_freq_period(struct record_opts *opts,
12541330
struct perf_event_attr *attr)
12551331
{

tools/perf/util/evsel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ void evsel__set_sample_id(struct evsel *evsel, bool use_sample_identifier);
342342
void arch_evsel__set_sample_weight(struct evsel *evsel);
343343
void arch__post_evsel_config(struct evsel *evsel, struct perf_event_attr *attr);
344344
int arch_evsel__open_strerror(struct evsel *evsel, int err, char *msg, size_t size);
345+
void arch_evsel__apply_ratio_to_prev(struct evsel *evsel, struct perf_event_attr *attr);
345346

346347
int evsel__set_filter(struct evsel *evsel, const char *filter);
347348
int evsel__append_tp_filter(struct evsel *evsel, const char *filter);

tools/perf/util/evsel_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum evsel_term_type {
2828
EVSEL__CONFIG_TERM_AUX_ACTION,
2929
EVSEL__CONFIG_TERM_AUX_SAMPLE_SIZE,
3030
EVSEL__CONFIG_TERM_CFG_CHG,
31+
EVSEL__CONFIG_TERM_RATIO_TO_PREV,
3132
};
3233

3334
struct evsel_config_term {

tools/perf/util/parse-events.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ const char *parse_events__term_type_str(enum parse_events__term_type term_type)
842842
[PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE] = "legacy-cache",
843843
[PARSE_EVENTS__TERM_TYPE_HARDWARE] = "hardware",
844844
[PARSE_EVENTS__TERM_TYPE_CPU] = "cpu",
845+
[PARSE_EVENTS__TERM_TYPE_RATIO_TO_PREV] = "ratio-to-prev",
845846
};
846847
if ((unsigned int)term_type >= __PARSE_EVENTS__TERM_TYPE_NR)
847848
return "unknown term";
@@ -892,6 +893,7 @@ config_term_avail(enum parse_events__term_type term_type, struct parse_events_er
892893
case PARSE_EVENTS__TERM_TYPE_RAW:
893894
case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE:
894895
case PARSE_EVENTS__TERM_TYPE_HARDWARE:
896+
case PARSE_EVENTS__TERM_TYPE_RATIO_TO_PREV:
895897
default:
896898
if (!err)
897899
return false;
@@ -1045,6 +1047,21 @@ do { \
10451047
perf_cpu_map__put(map);
10461048
break;
10471049
}
1050+
case PARSE_EVENTS__TERM_TYPE_RATIO_TO_PREV:
1051+
CHECK_TYPE_VAL(STR);
1052+
if (strtod(term->val.str, NULL) <= 0) {
1053+
parse_events_error__handle(parse_state->error, term->err_val,
1054+
strdup("zero or negative"),
1055+
NULL);
1056+
return -EINVAL;
1057+
}
1058+
if (errno == ERANGE) {
1059+
parse_events_error__handle(parse_state->error, term->err_val,
1060+
strdup("too big"),
1061+
NULL);
1062+
return -EINVAL;
1063+
}
1064+
break;
10481065
case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
10491066
case PARSE_EVENTS__TERM_TYPE_USER:
10501067
case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE:
@@ -1173,6 +1190,7 @@ static int config_term_tracepoint(struct perf_event_attr *attr,
11731190
case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE:
11741191
case PARSE_EVENTS__TERM_TYPE_HARDWARE:
11751192
case PARSE_EVENTS__TERM_TYPE_CPU:
1193+
case PARSE_EVENTS__TERM_TYPE_RATIO_TO_PREV:
11761194
default:
11771195
parse_events_error__handle(parse_state->error, term->err_term,
11781196
strdup(parse_events__term_type_str(term->type_term)),
@@ -1295,6 +1313,9 @@ do { \
12951313
ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size,
12961314
term->val.num, term->weak);
12971315
break;
1316+
case PARSE_EVENTS__TERM_TYPE_RATIO_TO_PREV:
1317+
ADD_CONFIG_TERM_STR(RATIO_TO_PREV, term->val.str, term->weak);
1318+
break;
12981319
case PARSE_EVENTS__TERM_TYPE_USER:
12991320
case PARSE_EVENTS__TERM_TYPE_CONFIG:
13001321
case PARSE_EVENTS__TERM_TYPE_CONFIG1:
@@ -1361,6 +1382,7 @@ static int get_config_chgs(struct perf_pmu *pmu, struct parse_events_terms *head
13611382
case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE:
13621383
case PARSE_EVENTS__TERM_TYPE_HARDWARE:
13631384
case PARSE_EVENTS__TERM_TYPE_CPU:
1385+
case PARSE_EVENTS__TERM_TYPE_RATIO_TO_PREV:
13641386
default:
13651387
break;
13661388
}

tools/perf/util/parse-events.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ enum parse_events__term_type {
8383
PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE,
8484
PARSE_EVENTS__TERM_TYPE_HARDWARE,
8585
PARSE_EVENTS__TERM_TYPE_CPU,
86-
#define __PARSE_EVENTS__TERM_TYPE_NR (PARSE_EVENTS__TERM_TYPE_CPU + 1)
86+
PARSE_EVENTS__TERM_TYPE_RATIO_TO_PREV,
87+
#define __PARSE_EVENTS__TERM_TYPE_NR (PARSE_EVENTS__TERM_TYPE_RATIO_TO_PREV + 1)
8788
};
8889

8990
struct parse_events_term {

tools/perf/util/parse-events.l

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ aux-action { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_ACTION); }
337337
aux-sample-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE); }
338338
metric-id { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_METRIC_ID); }
339339
cpu { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CPU); }
340+
ratio-to-prev { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_RATIO_TO_PREV); }
340341
cpu-cycles|cycles { return hw_term(yyscanner, PERF_COUNT_HW_CPU_CYCLES); }
341342
stalled-cycles-frontend|idle-cycles-frontend { return hw_term(yyscanner, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
342343
stalled-cycles-backend|idle-cycles-backend { return hw_term(yyscanner, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }

tools/perf/util/pmu.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,7 @@ static int pmu_config_term(const struct perf_pmu *pmu,
15411541
break;
15421542
case PARSE_EVENTS__TERM_TYPE_USER: /* Not hardcoded. */
15431543
return -EINVAL;
1544-
case PARSE_EVENTS__TERM_TYPE_NAME ... PARSE_EVENTS__TERM_TYPE_CPU:
1544+
case PARSE_EVENTS__TERM_TYPE_NAME ... PARSE_EVENTS__TERM_TYPE_RATIO_TO_PREV:
15451545
/* Skip non-config terms. */
15461546
break;
15471547
default:
@@ -1930,6 +1930,7 @@ int perf_pmu__for_each_format(struct perf_pmu *pmu, void *state, pmu_format_call
19301930
"aux-action=(pause|resume|start-paused)",
19311931
"aux-sample-size=number",
19321932
"cpu=number",
1933+
"ratio-to-prev=string",
19331934
};
19341935
struct perf_pmu_format *format;
19351936
int ret;

0 commit comments

Comments
 (0)