aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/parse-events.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/util/parse-events.c')
-rw-r--r--tools/perf/util/parse-events.c378
1 files changed, 269 insertions, 109 deletions
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 35d04da38d6a..5184959e0615 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -16,32 +16,28 @@ struct event_symbol {
16 u8 type; 16 u8 type;
17 u64 config; 17 u64 config;
18 char *symbol; 18 char *symbol;
19 char *alias;
19}; 20};
20 21
21#define C(x, y) .type = PERF_TYPE_##x, .config = PERF_COUNT_##y 22#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
22#define CR(x, y) .type = PERF_TYPE_##x, .config = y 23#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
23 24
24static struct event_symbol event_symbols[] = { 25static struct event_symbol event_symbols[] = {
25 { C(HARDWARE, HW_CPU_CYCLES), "cpu-cycles", }, 26 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
26 { C(HARDWARE, HW_CPU_CYCLES), "cycles", }, 27 { CHW(INSTRUCTIONS), "instructions", "" },
27 { C(HARDWARE, HW_INSTRUCTIONS), "instructions", }, 28 { CHW(CACHE_REFERENCES), "cache-references", "" },
28 { C(HARDWARE, HW_CACHE_REFERENCES), "cache-references", }, 29 { CHW(CACHE_MISSES), "cache-misses", "" },
29 { C(HARDWARE, HW_CACHE_MISSES), "cache-misses", }, 30 { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
30 { C(HARDWARE, HW_BRANCH_INSTRUCTIONS),"branch-instructions", }, 31 { CHW(BRANCH_MISSES), "branch-misses", "" },
31 { C(HARDWARE, HW_BRANCH_INSTRUCTIONS),"branches", }, 32 { CHW(BUS_CYCLES), "bus-cycles", "" },
32 { C(HARDWARE, HW_BRANCH_MISSES), "branch-misses", }, 33
33 { C(HARDWARE, HW_BUS_CYCLES), "bus-cycles", }, 34 { CSW(CPU_CLOCK), "cpu-clock", "" },
34 35 { CSW(TASK_CLOCK), "task-clock", "" },
35 { C(SOFTWARE, SW_CPU_CLOCK), "cpu-clock", }, 36 { CSW(PAGE_FAULTS), "page-faults", "faults" },
36 { C(SOFTWARE, SW_TASK_CLOCK), "task-clock", }, 37 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
37 { C(SOFTWARE, SW_PAGE_FAULTS), "page-faults", }, 38 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
38 { C(SOFTWARE, SW_PAGE_FAULTS), "faults", }, 39 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
39 { C(SOFTWARE, SW_PAGE_FAULTS_MIN), "minor-faults", }, 40 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
40 { C(SOFTWARE, SW_PAGE_FAULTS_MAJ), "major-faults", },
41 { C(SOFTWARE, SW_CONTEXT_SWITCHES), "context-switches", },
42 { C(SOFTWARE, SW_CONTEXT_SWITCHES), "cs", },
43 { C(SOFTWARE, SW_CPU_MIGRATIONS), "cpu-migrations", },
44 { C(SOFTWARE, SW_CPU_MIGRATIONS), "migrations", },
45}; 41};
46 42
47#define __PERF_COUNTER_FIELD(config, name) \ 43#define __PERF_COUNTER_FIELD(config, name) \
@@ -74,26 +70,70 @@ static char *sw_event_names[] = {
74 70
75#define MAX_ALIASES 8 71#define MAX_ALIASES 8
76 72
77static char *hw_cache [][MAX_ALIASES] = { 73static char *hw_cache[][MAX_ALIASES] = {
78 { "L1-data" , "l1-d", "l1d" }, 74 { "L1-d$", "l1-d", "l1d", "L1-data", },
79 { "L1-instruction" , "l1-i", "l1i" }, 75 { "L1-i$", "l1-i", "l1i", "L1-instruction", },
80 { "L2" , "l2" }, 76 { "LLC", "L2" },
81 { "Data-TLB" , "dtlb", "d-tlb" }, 77 { "dTLB", "d-tlb", "Data-TLB", },
82 { "Instruction-TLB" , "itlb", "i-tlb" }, 78 { "iTLB", "i-tlb", "Instruction-TLB", },
83 { "Branch" , "bpu" , "btb", "bpc" }, 79 { "branch", "branches", "bpu", "btb", "bpc", },
84}; 80};
85 81
86static char *hw_cache_op [][MAX_ALIASES] = { 82static char *hw_cache_op[][MAX_ALIASES] = {
87 { "Load" , "read" }, 83 { "load", "loads", "read", },
88 { "Store" , "write" }, 84 { "store", "stores", "write", },
89 { "Prefetch" , "speculative-read", "speculative-load" }, 85 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
90}; 86};
91 87
92static char *hw_cache_result [][MAX_ALIASES] = { 88static char *hw_cache_result[][MAX_ALIASES] = {
93 { "Reference" , "ops", "access" }, 89 { "refs", "Reference", "ops", "access", },
94 { "Miss" }, 90 { "misses", "miss", },
95}; 91};
96 92
93#define C(x) PERF_COUNT_HW_CACHE_##x
94#define CACHE_READ (1 << C(OP_READ))
95#define CACHE_WRITE (1 << C(OP_WRITE))
96#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
97#define COP(x) (1 << x)
98
99/*
100 * cache operartion stat
101 * L1I : Read and prefetch only
102 * ITLB and BPU : Read-only
103 */
104static unsigned long hw_cache_stat[C(MAX)] = {
105 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
106 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
107 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
108 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
109 [C(ITLB)] = (CACHE_READ),
110 [C(BPU)] = (CACHE_READ),
111};
112
113static int is_cache_op_valid(u8 cache_type, u8 cache_op)
114{
115 if (hw_cache_stat[cache_type] & COP(cache_op))
116 return 1; /* valid */
117 else
118 return 0; /* invalid */
119}
120
121static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
122{
123 static char name[50];
124
125 if (cache_result) {
126 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
127 hw_cache_op[cache_op][0],
128 hw_cache_result[cache_result][0]);
129 } else {
130 sprintf(name, "%s-%s", hw_cache[cache_type][0],
131 hw_cache_op[cache_op][1]);
132 }
133
134 return name;
135}
136
97char *event_name(int counter) 137char *event_name(int counter)
98{ 138{
99 u64 config = attrs[counter].config; 139 u64 config = attrs[counter].config;
@@ -113,7 +153,6 @@ char *event_name(int counter)
113 153
114 case PERF_TYPE_HW_CACHE: { 154 case PERF_TYPE_HW_CACHE: {
115 u8 cache_type, cache_op, cache_result; 155 u8 cache_type, cache_op, cache_result;
116 static char name[100];
117 156
118 cache_type = (config >> 0) & 0xff; 157 cache_type = (config >> 0) & 0xff;
119 if (cache_type > PERF_COUNT_HW_CACHE_MAX) 158 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
@@ -127,12 +166,10 @@ char *event_name(int counter)
127 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX) 166 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
128 return "unknown-ext-hardware-cache-result"; 167 return "unknown-ext-hardware-cache-result";
129 168
130 sprintf(name, "%s-Cache-%s-%ses", 169 if (!is_cache_op_valid(cache_type, cache_op))
131 hw_cache[cache_type][0], 170 return "invalid-cache";
132 hw_cache_op[cache_op][0],
133 hw_cache_result[cache_result][0]);
134 171
135 return name; 172 return event_cache_name(cache_type, cache_op, cache_result);
136 } 173 }
137 174
138 case PERF_TYPE_SOFTWARE: 175 case PERF_TYPE_SOFTWARE:
@@ -147,43 +184,74 @@ char *event_name(int counter)
147 return "unknown"; 184 return "unknown";
148} 185}
149 186
150static int parse_aliases(const char *str, char *names[][MAX_ALIASES], int size) 187static int parse_aliases(const char **str, char *names[][MAX_ALIASES], int size)
151{ 188{
152 int i, j; 189 int i, j;
190 int n, longest = -1;
153 191
154 for (i = 0; i < size; i++) { 192 for (i = 0; i < size; i++) {
155 for (j = 0; j < MAX_ALIASES; j++) { 193 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
156 if (!names[i][j]) 194 n = strlen(names[i][j]);
157 break; 195 if (n > longest && !strncasecmp(*str, names[i][j], n))
158 if (strcasestr(str, names[i][j])) 196 longest = n;
159 return i; 197 }
198 if (longest > 0) {
199 *str += longest;
200 return i;
160 } 201 }
161 } 202 }
162 203
163 return -1; 204 return -1;
164} 205}
165 206
166static int parse_generic_hw_symbols(const char *str, struct perf_counter_attr *attr) 207static int
208parse_generic_hw_event(const char **str, struct perf_counter_attr *attr)
167{ 209{
168 int cache_type = -1, cache_op = 0, cache_result = 0; 210 const char *s = *str;
211 int cache_type = -1, cache_op = -1, cache_result = -1;
169 212
170 cache_type = parse_aliases(str, hw_cache, PERF_COUNT_HW_CACHE_MAX); 213 cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
171 /* 214 /*
172 * No fallback - if we cannot get a clear cache type 215 * No fallback - if we cannot get a clear cache type
173 * then bail out: 216 * then bail out:
174 */ 217 */
175 if (cache_type == -1) 218 if (cache_type == -1)
176 return -EINVAL; 219 return 0;
220
221 while ((cache_op == -1 || cache_result == -1) && *s == '-') {
222 ++s;
223
224 if (cache_op == -1) {
225 cache_op = parse_aliases(&s, hw_cache_op,
226 PERF_COUNT_HW_CACHE_OP_MAX);
227 if (cache_op >= 0) {
228 if (!is_cache_op_valid(cache_type, cache_op))
229 return 0;
230 continue;
231 }
232 }
233
234 if (cache_result == -1) {
235 cache_result = parse_aliases(&s, hw_cache_result,
236 PERF_COUNT_HW_CACHE_RESULT_MAX);
237 if (cache_result >= 0)
238 continue;
239 }
240
241 /*
242 * Can't parse this as a cache op or result, so back up
243 * to the '-'.
244 */
245 --s;
246 break;
247 }
177 248
178 cache_op = parse_aliases(str, hw_cache_op, PERF_COUNT_HW_CACHE_OP_MAX);
179 /* 249 /*
180 * Fall back to reads: 250 * Fall back to reads:
181 */ 251 */
182 if (cache_op == -1) 252 if (cache_op == -1)
183 cache_op = PERF_COUNT_HW_CACHE_OP_READ; 253 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
184 254
185 cache_result = parse_aliases(str, hw_cache_result,
186 PERF_COUNT_HW_CACHE_RESULT_MAX);
187 /* 255 /*
188 * Fall back to accesses: 256 * Fall back to accesses:
189 */ 257 */
@@ -193,82 +261,154 @@ static int parse_generic_hw_symbols(const char *str, struct perf_counter_attr *a
193 attr->config = cache_type | (cache_op << 8) | (cache_result << 16); 261 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
194 attr->type = PERF_TYPE_HW_CACHE; 262 attr->type = PERF_TYPE_HW_CACHE;
195 263
264 *str = s;
265 return 1;
266}
267
268static int check_events(const char *str, unsigned int i)
269{
270 int n;
271
272 n = strlen(event_symbols[i].symbol);
273 if (!strncmp(str, event_symbols[i].symbol, n))
274 return n;
275
276 n = strlen(event_symbols[i].alias);
277 if (n)
278 if (!strncmp(str, event_symbols[i].alias, n))
279 return n;
196 return 0; 280 return 0;
197} 281}
198 282
199/* 283static int
200 * Each event can have multiple symbolic names. 284parse_symbolic_event(const char **strp, struct perf_counter_attr *attr)
201 * Symbolic names are (almost) exactly matched.
202 */
203static int parse_event_symbols(const char *str, struct perf_counter_attr *attr)
204{ 285{
205 u64 config, id; 286 const char *str = *strp;
206 int type;
207 unsigned int i; 287 unsigned int i;
208 const char *sep, *pstr; 288 int n;
209 289
210 if (str[0] == 'r' && hex2u64(str + 1, &config) > 0) { 290 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
211 attr->type = PERF_TYPE_RAW; 291 n = check_events(str, i);
212 attr->config = config; 292 if (n > 0) {
293 attr->type = event_symbols[i].type;
294 attr->config = event_symbols[i].config;
295 *strp = str + n;
296 return 1;
297 }
298 }
299 return 0;
300}
301
302static int parse_raw_event(const char **strp, struct perf_counter_attr *attr)
303{
304 const char *str = *strp;
305 u64 config;
306 int n;
213 307
308 if (*str != 'r')
214 return 0; 309 return 0;
310 n = hex2u64(str + 1, &config);
311 if (n > 0) {
312 *strp = str + n + 1;
313 attr->type = PERF_TYPE_RAW;
314 attr->config = config;
315 return 1;
215 } 316 }
317 return 0;
318}
216 319
217 pstr = str; 320static int
218 sep = strchr(pstr, ':'); 321parse_numeric_event(const char **strp, struct perf_counter_attr *attr)
219 if (sep) { 322{
220 type = atoi(pstr); 323 const char *str = *strp;
221 pstr = sep + 1; 324 char *endp;
222 id = atoi(pstr); 325 unsigned long type;
223 sep = strchr(pstr, ':'); 326 u64 config;
224 if (sep) { 327
225 pstr = sep + 1; 328 type = strtoul(str, &endp, 0);
226 if (strchr(pstr, 'k')) 329 if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
227 attr->exclude_user = 1; 330 str = endp + 1;
228 if (strchr(pstr, 'u')) 331 config = strtoul(str, &endp, 0);
229 attr->exclude_kernel = 1; 332 if (endp > str) {
333 attr->type = type;
334 attr->config = config;
335 *strp = endp;
336 return 1;
230 } 337 }
231 attr->type = type; 338 }
232 attr->config = id; 339 return 0;
340}
341
342static int
343parse_event_modifier(const char **strp, struct perf_counter_attr *attr)
344{
345 const char *str = *strp;
346 int eu = 1, ek = 1, eh = 1;
233 347
348 if (*str++ != ':')
234 return 0; 349 return 0;
350 while (*str) {
351 if (*str == 'u')
352 eu = 0;
353 else if (*str == 'k')
354 ek = 0;
355 else if (*str == 'h')
356 eh = 0;
357 else
358 break;
359 ++str;
235 } 360 }
361 if (str >= *strp + 2) {
362 *strp = str;
363 attr->exclude_user = eu;
364 attr->exclude_kernel = ek;
365 attr->exclude_hv = eh;
366 return 1;
367 }
368 return 0;
369}
236 370
237 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) { 371/*
238 if (!strncmp(str, event_symbols[i].symbol, 372 * Each event can have multiple symbolic names.
239 strlen(event_symbols[i].symbol))) { 373 * Symbolic names are (almost) exactly matched.
240 374 */
241 attr->type = event_symbols[i].type; 375static int parse_event_symbols(const char **str, struct perf_counter_attr *attr)
242 attr->config = event_symbols[i].config; 376{
377 if (!(parse_raw_event(str, attr) ||
378 parse_numeric_event(str, attr) ||
379 parse_symbolic_event(str, attr) ||
380 parse_generic_hw_event(str, attr)))
381 return 0;
243 382
244 return 0; 383 parse_event_modifier(str, attr);
245 }
246 }
247 384
248 return parse_generic_hw_symbols(str, attr); 385 return 1;
249} 386}
250 387
251int parse_events(const struct option *opt, const char *str, int unset) 388int parse_events(const struct option *opt __used, const char *str, int unset __used)
252{ 389{
253 struct perf_counter_attr attr; 390 struct perf_counter_attr attr;
254 int ret;
255 391
256 memset(&attr, 0, sizeof(attr)); 392 for (;;) {
257again: 393 if (nr_counters == MAX_COUNTERS)
258 if (nr_counters == MAX_COUNTERS) 394 return -1;
259 return -1; 395
396 memset(&attr, 0, sizeof(attr));
397 if (!parse_event_symbols(&str, &attr))
398 return -1;
260 399
261 ret = parse_event_symbols(str, &attr); 400 if (!(*str == 0 || *str == ',' || isspace(*str)))
262 if (ret < 0) 401 return -1;
263 return ret;
264 402
265 attrs[nr_counters] = attr; 403 attrs[nr_counters] = attr;
266 nr_counters++; 404 nr_counters++;
267 405
268 str = strstr(str, ","); 406 if (*str == 0)
269 if (str) { 407 break;
270 str++; 408 if (*str == ',')
271 goto again; 409 ++str;
410 while (isspace(*str))
411 ++str;
272 } 412 }
273 413
274 return 0; 414 return 0;
@@ -288,7 +428,8 @@ static const char * const event_type_descriptors[] = {
288void print_events(void) 428void print_events(void)
289{ 429{
290 struct event_symbol *syms = event_symbols; 430 struct event_symbol *syms = event_symbols;
291 unsigned int i, type, prev_type = -1; 431 unsigned int i, type, op, prev_type = -1;
432 char name[40];
292 433
293 fprintf(stderr, "\n"); 434 fprintf(stderr, "\n");
294 fprintf(stderr, "List of pre-defined events (to be used in -e):\n"); 435 fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
@@ -301,14 +442,33 @@ void print_events(void)
301 if (type != prev_type) 442 if (type != prev_type)
302 fprintf(stderr, "\n"); 443 fprintf(stderr, "\n");
303 444
304 fprintf(stderr, " %-30s [%s]\n", syms->symbol, 445 if (strlen(syms->alias))
446 sprintf(name, "%s OR %s", syms->symbol, syms->alias);
447 else
448 strcpy(name, syms->symbol);
449 fprintf(stderr, " %-40s [%s]\n", name,
305 event_type_descriptors[type]); 450 event_type_descriptors[type]);
306 451
307 prev_type = type; 452 prev_type = type;
308 } 453 }
309 454
310 fprintf(stderr, "\n"); 455 fprintf(stderr, "\n");
311 fprintf(stderr, " %-30s [raw hardware event descriptor]\n", 456 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
457 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
458 /* skip invalid cache type */
459 if (!is_cache_op_valid(type, op))
460 continue;
461
462 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
463 fprintf(stderr, " %-40s [%s]\n",
464 event_cache_name(type, op, i),
465 event_type_descriptors[4]);
466 }
467 }
468 }
469
470 fprintf(stderr, "\n");
471 fprintf(stderr, " %-40s [raw hardware event descriptor]\n",
312 "rNNN"); 472 "rNNN");
313 fprintf(stderr, "\n"); 473 fprintf(stderr, "\n");
314 474