blob: 06af2fadcd870bc4b7d085cea880df7b0498f581 [file] [log] [blame]
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001
2#include "../perf.h"
3#include "util.h"
4#include "parse-options.h"
5#include "parse-events.h"
6#include "exec_cmd.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -03007#include "string.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +02008
Ingo Molnar8326f442009-06-05 20:22:46 +02009extern char *strcasestr(const char *haystack, const char *needle);
10
Ingo Molnara21ca2c2009-06-06 09:58:57 +020011int nr_counters;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020012
Ingo Molnara21ca2c2009-06-06 09:58:57 +020013struct perf_counter_attr attrs[MAX_COUNTERS];
Ingo Molnar8ad8db32009-05-26 11:10:09 +020014
15struct event_symbol {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100016 u8 type;
17 u64 config;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020018 char *symbol;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053019 char *alias;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020020};
21
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053022#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
Ingo Molnar8ad8db32009-05-26 11:10:09 +020024
Ingo Molnara21ca2c2009-06-06 09:58:57 +020025static struct event_symbol event_symbols[] = {
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053026 { 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", "" },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020033
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053034 { CSW(CPU_CLOCK), "cpu-clock", "" },
35 { CSW(TASK_CLOCK), "task-clock", "" },
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053036 { CSW(PAGE_FAULTS), "page-faults", "faults" },
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053037 { 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" },
Ingo Molnar8ad8db32009-05-26 11:10:09 +020041};
42
Ingo Molnar52425192009-05-26 09:17:18 +020043#define __PERF_COUNTER_FIELD(config, name) \
44 ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
45
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)
50
51static char *hw_event_names[] = {
Ingo Molnar8faf3b52009-06-06 13:58:12 +020052 "cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020053 "instructions",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020054 "cache-references",
55 "cache-misses",
Ingo Molnar52425192009-05-26 09:17:18 +020056 "branches",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020057 "branch-misses",
58 "bus-cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020059};
60
61static char *sw_event_names[] = {
Ingo Molnar44175b62009-06-13 13:35:00 +020062 "cpu-clock-msecs",
63 "task-clock-msecs",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020064 "page-faults",
65 "context-switches",
66 "CPU-migrations",
67 "minor-faults",
68 "major-faults",
Ingo Molnar52425192009-05-26 09:17:18 +020069};
70
Ingo Molnar8326f442009-06-05 20:22:46 +020071#define MAX_ALIASES 8
72
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053073static char *hw_cache[][MAX_ALIASES] = {
74 { "L1-data", "l1-d", "l1d" },
75 { "L1-instruction", "l1-i", "l1i" },
76 { "L2", "l2" },
77 { "Data-TLB", "dtlb", "d-tlb" },
78 { "Instruction-TLB", "itlb", "i-tlb" },
79 { "Branch", "bpu" , "btb", "bpc" },
Ingo Molnar8326f442009-06-05 20:22:46 +020080};
81
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053082static char *hw_cache_op[][MAX_ALIASES] = {
83 { "Load", "read" },
84 { "Store", "write" },
85 { "Prefetch", "speculative-read", "speculative-load" },
Ingo Molnar8326f442009-06-05 20:22:46 +020086};
87
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053088static char *hw_cache_result[][MAX_ALIASES] = {
89 { "Reference", "ops", "access" },
90 { "Miss" },
Ingo Molnar8326f442009-06-05 20:22:46 +020091};
92
Ingo Molnara21ca2c2009-06-06 09:58:57 +020093char *event_name(int counter)
Ingo Molnar52425192009-05-26 09:17:18 +020094{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100095 u64 config = attrs[counter].config;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020096 int type = attrs[counter].type;
Ingo Molnar52425192009-05-26 09:17:18 +020097 static char buf[32];
98
Ingo Molnara21ca2c2009-06-06 09:58:57 +020099 if (attrs[counter].type == PERF_TYPE_RAW) {
100 sprintf(buf, "raw 0x%llx", config);
Ingo Molnar52425192009-05-26 09:17:18 +0200101 return buf;
102 }
103
104 switch (type) {
105 case PERF_TYPE_HARDWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200106 if (config < PERF_COUNT_HW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200107 return hw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200108 return "unknown-hardware";
109
Ingo Molnar8326f442009-06-05 20:22:46 +0200110 case PERF_TYPE_HW_CACHE: {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000111 u8 cache_type, cache_op, cache_result;
Ingo Molnar8326f442009-06-05 20:22:46 +0200112 static char name[100];
113
114 cache_type = (config >> 0) & 0xff;
115 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
116 return "unknown-ext-hardware-cache-type";
117
118 cache_op = (config >> 8) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200119 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
120 return "unknown-ext-hardware-cache-op";
Ingo Molnar8326f442009-06-05 20:22:46 +0200121
122 cache_result = (config >> 16) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200123 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
124 return "unknown-ext-hardware-cache-result";
Ingo Molnar8326f442009-06-05 20:22:46 +0200125
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200126 sprintf(name, "%s-Cache-%s-%ses",
Ingo Molnar8326f442009-06-05 20:22:46 +0200127 hw_cache[cache_type][0],
128 hw_cache_op[cache_op][0],
129 hw_cache_result[cache_result][0]);
130
131 return name;
132 }
133
Ingo Molnar52425192009-05-26 09:17:18 +0200134 case PERF_TYPE_SOFTWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200135 if (config < PERF_COUNT_SW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200136 return sw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200137 return "unknown-software";
138
139 default:
140 break;
141 }
142
143 return "unknown";
144}
145
Ingo Molnar8326f442009-06-05 20:22:46 +0200146static int parse_aliases(const char *str, char *names[][MAX_ALIASES], int size)
147{
148 int i, j;
149
150 for (i = 0; i < size; i++) {
151 for (j = 0; j < MAX_ALIASES; j++) {
152 if (!names[i][j])
153 break;
154 if (strcasestr(str, names[i][j]))
155 return i;
156 }
157 }
158
Ingo Molnar89536452009-06-06 21:04:17 +0200159 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200160}
161
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +0530162static int
163parse_generic_hw_symbols(const char *str, struct perf_counter_attr *attr)
Ingo Molnar8326f442009-06-05 20:22:46 +0200164{
Ingo Molnar89536452009-06-06 21:04:17 +0200165 int cache_type = -1, cache_op = 0, cache_result = 0;
Ingo Molnar8326f442009-06-05 20:22:46 +0200166
167 cache_type = parse_aliases(str, hw_cache, PERF_COUNT_HW_CACHE_MAX);
168 /*
169 * No fallback - if we cannot get a clear cache type
170 * then bail out:
171 */
172 if (cache_type == -1)
173 return -EINVAL;
174
175 cache_op = parse_aliases(str, hw_cache_op, PERF_COUNT_HW_CACHE_OP_MAX);
176 /*
177 * Fall back to reads:
178 */
Ingo Molnar89536452009-06-06 21:04:17 +0200179 if (cache_op == -1)
180 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200181
182 cache_result = parse_aliases(str, hw_cache_result,
183 PERF_COUNT_HW_CACHE_RESULT_MAX);
184 /*
185 * Fall back to accesses:
186 */
187 if (cache_result == -1)
188 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
189
190 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
191 attr->type = PERF_TYPE_HW_CACHE;
192
193 return 0;
194}
195
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530196static int check_events(const char *str, unsigned int i)
197{
198 if (!strncmp(str, event_symbols[i].symbol,
199 strlen(event_symbols[i].symbol)))
200 return 1;
201
202 if (strlen(event_symbols[i].alias))
203 if (!strncmp(str, event_symbols[i].alias,
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +0530204 strlen(event_symbols[i].alias)))
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530205 return 1;
206 return 0;
207}
208
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200209/*
210 * Each event can have multiple symbolic names.
211 * Symbolic names are (almost) exactly matched.
212 */
Ingo Molnar8326f442009-06-05 20:22:46 +0200213static int parse_event_symbols(const char *str, struct perf_counter_attr *attr)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200214{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000215 u64 config, id;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200216 int type;
217 unsigned int i;
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300218 const char *sep, *pstr;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200219
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200220 if (str[0] == 'r' && hex2u64(str + 1, &config) > 0) {
221 attr->type = PERF_TYPE_RAW;
222 attr->config = config;
223
224 return 0;
225 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200226
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300227 pstr = str;
228 sep = strchr(pstr, ':');
229 if (sep) {
230 type = atoi(pstr);
231 pstr = sep + 1;
232 id = atoi(pstr);
233 sep = strchr(pstr, ':');
234 if (sep) {
235 pstr = sep + 1;
236 if (strchr(pstr, 'k'))
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200237 attr->exclude_user = 1;
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300238 if (strchr(pstr, 'u'))
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200239 attr->exclude_kernel = 1;
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300240 }
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200241 attr->type = type;
242 attr->config = id;
243
244 return 0;
Ingo Molnar52425192009-05-26 09:17:18 +0200245 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200246
247 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530248 if (check_events(str, i)) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200249 attr->type = event_symbols[i].type;
250 attr->config = event_symbols[i].config;
251
252 return 0;
253 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200254 }
255
Ingo Molnar8326f442009-06-05 20:22:46 +0200256 return parse_generic_hw_symbols(str, attr);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200257}
258
259int parse_events(const struct option *opt, const char *str, int unset)
260{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200261 struct perf_counter_attr attr;
262 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200263
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200264 memset(&attr, 0, sizeof(attr));
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200265again:
266 if (nr_counters == MAX_COUNTERS)
267 return -1;
268
Ingo Molnar8326f442009-06-05 20:22:46 +0200269 ret = parse_event_symbols(str, &attr);
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200270 if (ret < 0)
271 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200272
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200273 attrs[nr_counters] = attr;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200274 nr_counters++;
275
276 str = strstr(str, ",");
277 if (str) {
278 str++;
279 goto again;
280 }
281
282 return 0;
283}
284
Thomas Gleixner86847b62009-06-06 12:24:17 +0200285static const char * const event_type_descriptors[] = {
286 "",
287 "Hardware event",
288 "Software event",
289 "Tracepoint event",
290 "Hardware cache event",
291};
292
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200293/*
Thomas Gleixner86847b62009-06-06 12:24:17 +0200294 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200295 */
Thomas Gleixner86847b62009-06-06 12:24:17 +0200296void print_events(void)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200297{
Thomas Gleixner86847b62009-06-06 12:24:17 +0200298 struct event_symbol *syms = event_symbols;
299 unsigned int i, type, prev_type = -1;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530300 char name[40];
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200301
Thomas Gleixner86847b62009-06-06 12:24:17 +0200302 fprintf(stderr, "\n");
303 fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200304
Thomas Gleixner86847b62009-06-06 12:24:17 +0200305 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
306 type = syms->type + 1;
307 if (type > ARRAY_SIZE(event_type_descriptors))
308 type = 0;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200309
Thomas Gleixner86847b62009-06-06 12:24:17 +0200310 if (type != prev_type)
311 fprintf(stderr, "\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200312
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530313 if (strlen(syms->alias))
314 sprintf(name, "%s OR %s", syms->symbol, syms->alias);
315 else
316 strcpy(name, syms->symbol);
317 fprintf(stderr, " %-40s [%s]\n", name,
Thomas Gleixner86847b62009-06-06 12:24:17 +0200318 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200319
Thomas Gleixner86847b62009-06-06 12:24:17 +0200320 prev_type = type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200321 }
322
Thomas Gleixner86847b62009-06-06 12:24:17 +0200323 fprintf(stderr, "\n");
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530324 fprintf(stderr, " %-40s [raw hardware event descriptor]\n",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200325 "rNNN");
326 fprintf(stderr, "\n");
327
328 exit(129);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200329}