4 #include "parse-options.h"
5 #include "parse-events.h"
9 extern char *strcasestr(const char *haystack
, const char *needle
);
13 struct perf_counter_attr attrs
[MAX_COUNTERS
];
22 #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
23 #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
25 static struct event_symbol event_symbols
[] = {
26 { CHW(CPU_CYCLES
), "cpu-cycles", "cycles" },
27 { CHW(INSTRUCTIONS
), "instructions", "" },
28 { CHW(CACHE_REFERENCES
), "cache-references", "" },
29 { CHW(CACHE_MISSES
), "cache-misses", "" },
30 { CHW(BRANCH_INSTRUCTIONS
), "branch-instructions", "branches" },
31 { CHW(BRANCH_MISSES
), "branch-misses", "" },
32 { CHW(BUS_CYCLES
), "bus-cycles", "" },
34 { CSW(CPU_CLOCK
), "cpu-clock", "" },
35 { CSW(TASK_CLOCK
), "task-clock", "" },
36 { CSW(PAGE_FAULTS
), "page-faults", "faults" },
37 { CSW(PAGE_FAULTS_MIN
), "minor-faults", "" },
38 { CSW(PAGE_FAULTS_MAJ
), "major-faults", "" },
39 { CSW(CONTEXT_SWITCHES
), "context-switches", "cs" },
40 { CSW(CPU_MIGRATIONS
), "cpu-migrations", "migrations" },
43 #define __PERF_COUNTER_FIELD(config, name) \
44 ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
46 #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
47 #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
48 #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
49 #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
51 static char *hw_event_names
[] = {
61 static char *sw_event_names
[] = {
73 static char *hw_cache
[][MAX_ALIASES
] = {
74 { "L1-dcache", "l1-d", "l1d", "L1-data", },
75 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
77 { "dTLB", "d-tlb", "Data-TLB", },
78 { "iTLB", "i-tlb", "Instruction-TLB", },
79 { "branch", "branches", "bpu", "btb", "bpc", },
82 static char *hw_cache_op
[][MAX_ALIASES
] = {
83 { "load", "loads", "read", },
84 { "store", "stores", "write", },
85 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
88 static char *hw_cache_result
[][MAX_ALIASES
] = {
89 { "refs", "Reference", "ops", "access", },
90 { "misses", "miss", },
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)
100 * cache operartion stat
101 * L1I : Read and prefetch only
102 * ITLB and BPU : Read-only
104 static 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
),
113 static int is_cache_op_valid(u8 cache_type
, u8 cache_op
)
115 if (hw_cache_stat
[cache_type
] & COP(cache_op
))
116 return 1; /* valid */
118 return 0; /* invalid */
121 static char *event_cache_name(u8 cache_type
, u8 cache_op
, u8 cache_result
)
123 static char name
[50];
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]);
130 sprintf(name
, "%s-%s", hw_cache
[cache_type
][0],
131 hw_cache_op
[cache_op
][1]);
137 char *event_name(int counter
)
139 u64 config
= attrs
[counter
].config
;
140 int type
= attrs
[counter
].type
;
143 if (attrs
[counter
].type
== PERF_TYPE_RAW
) {
144 sprintf(buf
, "raw 0x%llx", config
);
149 case PERF_TYPE_HARDWARE
:
150 if (config
< PERF_COUNT_HW_MAX
)
151 return hw_event_names
[config
];
152 return "unknown-hardware";
154 case PERF_TYPE_HW_CACHE
: {
155 u8 cache_type
, cache_op
, cache_result
;
157 cache_type
= (config
>> 0) & 0xff;
158 if (cache_type
> PERF_COUNT_HW_CACHE_MAX
)
159 return "unknown-ext-hardware-cache-type";
161 cache_op
= (config
>> 8) & 0xff;
162 if (cache_op
> PERF_COUNT_HW_CACHE_OP_MAX
)
163 return "unknown-ext-hardware-cache-op";
165 cache_result
= (config
>> 16) & 0xff;
166 if (cache_result
> PERF_COUNT_HW_CACHE_RESULT_MAX
)
167 return "unknown-ext-hardware-cache-result";
169 if (!is_cache_op_valid(cache_type
, cache_op
))
170 return "invalid-cache";
172 return event_cache_name(cache_type
, cache_op
, cache_result
);
175 case PERF_TYPE_SOFTWARE
:
176 if (config
< PERF_COUNT_SW_MAX
)
177 return sw_event_names
[config
];
178 return "unknown-software";
187 static int parse_aliases(const char **str
, char *names
[][MAX_ALIASES
], int size
)
192 for (i
= 0; i
< size
; i
++) {
193 for (j
= 0; j
< MAX_ALIASES
&& names
[i
][j
]; j
++) {
194 n
= strlen(names
[i
][j
]);
195 if (n
> longest
&& !strncasecmp(*str
, names
[i
][j
], n
))
208 parse_generic_hw_event(const char **str
, struct perf_counter_attr
*attr
)
210 const char *s
= *str
;
211 int cache_type
= -1, cache_op
= -1, cache_result
= -1;
213 cache_type
= parse_aliases(&s
, hw_cache
, PERF_COUNT_HW_CACHE_MAX
);
215 * No fallback - if we cannot get a clear cache type
218 if (cache_type
== -1)
221 while ((cache_op
== -1 || cache_result
== -1) && *s
== '-') {
224 if (cache_op
== -1) {
225 cache_op
= parse_aliases(&s
, hw_cache_op
,
226 PERF_COUNT_HW_CACHE_OP_MAX
);
228 if (!is_cache_op_valid(cache_type
, cache_op
))
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)
242 * Can't parse this as a cache op or result, so back up
250 * Fall back to reads:
253 cache_op
= PERF_COUNT_HW_CACHE_OP_READ
;
256 * Fall back to accesses:
258 if (cache_result
== -1)
259 cache_result
= PERF_COUNT_HW_CACHE_RESULT_ACCESS
;
261 attr
->config
= cache_type
| (cache_op
<< 8) | (cache_result
<< 16);
262 attr
->type
= PERF_TYPE_HW_CACHE
;
268 static int check_events(const char *str
, unsigned int i
)
272 n
= strlen(event_symbols
[i
].symbol
);
273 if (!strncmp(str
, event_symbols
[i
].symbol
, n
))
276 n
= strlen(event_symbols
[i
].alias
);
278 if (!strncmp(str
, event_symbols
[i
].alias
, n
))
284 parse_symbolic_event(const char **strp
, struct perf_counter_attr
*attr
)
286 const char *str
= *strp
;
290 for (i
= 0; i
< ARRAY_SIZE(event_symbols
); i
++) {
291 n
= check_events(str
, i
);
293 attr
->type
= event_symbols
[i
].type
;
294 attr
->config
= event_symbols
[i
].config
;
302 static int parse_raw_event(const char **strp
, struct perf_counter_attr
*attr
)
304 const char *str
= *strp
;
310 n
= hex2u64(str
+ 1, &config
);
313 attr
->type
= PERF_TYPE_RAW
;
314 attr
->config
= config
;
321 parse_numeric_event(const char **strp
, struct perf_counter_attr
*attr
)
323 const char *str
= *strp
;
328 type
= strtoul(str
, &endp
, 0);
329 if (endp
> str
&& type
< PERF_TYPE_MAX
&& *endp
== ':') {
331 config
= strtoul(str
, &endp
, 0);
334 attr
->config
= config
;
343 parse_event_modifier(const char **strp
, struct perf_counter_attr
*attr
)
345 const char *str
= *strp
;
346 int eu
= 1, ek
= 1, eh
= 1;
353 else if (*str
== 'k')
355 else if (*str
== 'h')
361 if (str
>= *strp
+ 2) {
363 attr
->exclude_user
= eu
;
364 attr
->exclude_kernel
= ek
;
365 attr
->exclude_hv
= eh
;
372 * Each event can have multiple symbolic names.
373 * Symbolic names are (almost) exactly matched.
375 static int parse_event_symbols(const char **str
, struct perf_counter_attr
*attr
)
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
)))
383 parse_event_modifier(str
, attr
);
388 int parse_events(const struct option
*opt __used
, const char *str
, int unset __used
)
390 struct perf_counter_attr attr
;
393 if (nr_counters
== MAX_COUNTERS
)
396 memset(&attr
, 0, sizeof(attr
));
397 if (!parse_event_symbols(&str
, &attr
))
400 if (!(*str
== 0 || *str
== ',' || isspace(*str
)))
403 attrs
[nr_counters
] = attr
;
410 while (isspace(*str
))
417 static const char * const event_type_descriptors
[] = {
422 "Hardware cache event",
426 * Print the help text for the event symbols:
428 void print_events(void)
430 struct event_symbol
*syms
= event_symbols
;
431 unsigned int i
, type
, op
, prev_type
= -1;
434 fprintf(stderr
, "\n");
435 fprintf(stderr
, "List of pre-defined events (to be used in -e):\n");
437 for (i
= 0; i
< ARRAY_SIZE(event_symbols
); i
++, syms
++) {
438 type
= syms
->type
+ 1;
439 if (type
>= ARRAY_SIZE(event_type_descriptors
))
442 if (type
!= prev_type
)
443 fprintf(stderr
, "\n");
445 if (strlen(syms
->alias
))
446 sprintf(name
, "%s OR %s", syms
->symbol
, syms
->alias
);
448 strcpy(name
, syms
->symbol
);
449 fprintf(stderr
, " %-40s [%s]\n", name
,
450 event_type_descriptors
[type
]);
455 fprintf(stderr
, "\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
))
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]);
470 fprintf(stderr
, "\n");
471 fprintf(stderr
, " %-40s [raw hardware event descriptor]\n",
473 fprintf(stderr
, "\n");