blob: 12abab3a0d63196a50a37a88f0df791c33c397af [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;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020019};
20
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053021#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
22#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
Ingo Molnar8ad8db32009-05-26 11:10:09 +020023
Ingo Molnara21ca2c2009-06-06 09:58:57 +020024static struct event_symbol event_symbols[] = {
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053025 { CHW(CPU_CYCLES), "cpu-cycles", },
26 { CHW(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", },
31 { CHW(BRANCH_INSTRUCTIONS), "branches", },
32 { CHW(BRANCH_MISSES), "branch-misses", },
33 { CHW(BUS_CYCLES), "bus-cycles", },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020034
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053035 { CSW(CPU_CLOCK), "cpu-clock", },
36 { CSW(TASK_CLOCK), "task-clock", },
37 { CSW(PAGE_FAULTS), "page-faults", },
38 { CSW(PAGE_FAULTS), "faults", },
39 { CSW(PAGE_FAULTS_MIN), "minor-faults", },
40 { CSW(PAGE_FAULTS_MAJ), "major-faults", },
41 { CSW(CONTEXT_SWITCHES), "context-switches", },
42 { CSW(CONTEXT_SWITCHES), "cs", },
43 { CSW(CPU_MIGRATIONS), "cpu-migrations", },
44 { CSW(CPU_MIGRATIONS), "migrations", },
Ingo Molnar8ad8db32009-05-26 11:10:09 +020045};
46
Ingo Molnar52425192009-05-26 09:17:18 +020047#define __PERF_COUNTER_FIELD(config, name) \
48 ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
49
50#define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
51#define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
52#define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
53#define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
54
55static char *hw_event_names[] = {
Ingo Molnar8faf3b52009-06-06 13:58:12 +020056 "cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020057 "instructions",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020058 "cache-references",
59 "cache-misses",
Ingo Molnar52425192009-05-26 09:17:18 +020060 "branches",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020061 "branch-misses",
62 "bus-cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020063};
64
65static char *sw_event_names[] = {
Ingo Molnar44175b62009-06-13 13:35:00 +020066 "cpu-clock-msecs",
67 "task-clock-msecs",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020068 "page-faults",
69 "context-switches",
70 "CPU-migrations",
71 "minor-faults",
72 "major-faults",
Ingo Molnar52425192009-05-26 09:17:18 +020073};
74
Ingo Molnar8326f442009-06-05 20:22:46 +020075#define MAX_ALIASES 8
76
77static char *hw_cache [][MAX_ALIASES] = {
Yong Wangfaafec12009-06-12 11:17:06 +080078 { "L1-data" , "l1-d", "l1d" },
Ingo Molnar8faf3b52009-06-06 13:58:12 +020079 { "L1-instruction" , "l1-i", "l1i" },
80 { "L2" , "l2" },
81 { "Data-TLB" , "dtlb", "d-tlb" },
82 { "Instruction-TLB" , "itlb", "i-tlb" },
83 { "Branch" , "bpu" , "btb", "bpc" },
Ingo Molnar8326f442009-06-05 20:22:46 +020084};
85
86static char *hw_cache_op [][MAX_ALIASES] = {
Ingo Molnar8faf3b52009-06-06 13:58:12 +020087 { "Load" , "read" },
88 { "Store" , "write" },
89 { "Prefetch" , "speculative-read", "speculative-load" },
Ingo Molnar8326f442009-06-05 20:22:46 +020090};
91
92static char *hw_cache_result [][MAX_ALIASES] = {
Ingo Molnar8faf3b52009-06-06 13:58:12 +020093 { "Reference" , "ops", "access" },
94 { "Miss" },
Ingo Molnar8326f442009-06-05 20:22:46 +020095};
96
Ingo Molnara21ca2c2009-06-06 09:58:57 +020097char *event_name(int counter)
Ingo Molnar52425192009-05-26 09:17:18 +020098{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100099 u64 config = attrs[counter].config;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200100 int type = attrs[counter].type;
Ingo Molnar52425192009-05-26 09:17:18 +0200101 static char buf[32];
102
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200103 if (attrs[counter].type == PERF_TYPE_RAW) {
104 sprintf(buf, "raw 0x%llx", config);
Ingo Molnar52425192009-05-26 09:17:18 +0200105 return buf;
106 }
107
108 switch (type) {
109 case PERF_TYPE_HARDWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200110 if (config < PERF_COUNT_HW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200111 return hw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200112 return "unknown-hardware";
113
Ingo Molnar8326f442009-06-05 20:22:46 +0200114 case PERF_TYPE_HW_CACHE: {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000115 u8 cache_type, cache_op, cache_result;
Ingo Molnar8326f442009-06-05 20:22:46 +0200116 static char name[100];
117
118 cache_type = (config >> 0) & 0xff;
119 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
120 return "unknown-ext-hardware-cache-type";
121
122 cache_op = (config >> 8) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200123 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
124 return "unknown-ext-hardware-cache-op";
Ingo Molnar8326f442009-06-05 20:22:46 +0200125
126 cache_result = (config >> 16) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200127 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
128 return "unknown-ext-hardware-cache-result";
Ingo Molnar8326f442009-06-05 20:22:46 +0200129
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200130 sprintf(name, "%s-Cache-%s-%ses",
Ingo Molnar8326f442009-06-05 20:22:46 +0200131 hw_cache[cache_type][0],
132 hw_cache_op[cache_op][0],
133 hw_cache_result[cache_result][0]);
134
135 return name;
136 }
137
Ingo Molnar52425192009-05-26 09:17:18 +0200138 case PERF_TYPE_SOFTWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200139 if (config < PERF_COUNT_SW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200140 return sw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200141 return "unknown-software";
142
143 default:
144 break;
145 }
146
147 return "unknown";
148}
149
Ingo Molnar8326f442009-06-05 20:22:46 +0200150static int parse_aliases(const char *str, char *names[][MAX_ALIASES], int size)
151{
152 int i, j;
153
154 for (i = 0; i < size; i++) {
155 for (j = 0; j < MAX_ALIASES; j++) {
156 if (!names[i][j])
157 break;
158 if (strcasestr(str, names[i][j]))
159 return i;
160 }
161 }
162
Ingo Molnar89536452009-06-06 21:04:17 +0200163 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200164}
165
166static int parse_generic_hw_symbols(const char *str, struct perf_counter_attr *attr)
167{
Ingo Molnar89536452009-06-06 21:04:17 +0200168 int cache_type = -1, cache_op = 0, cache_result = 0;
Ingo Molnar8326f442009-06-05 20:22:46 +0200169
170 cache_type = parse_aliases(str, hw_cache, PERF_COUNT_HW_CACHE_MAX);
171 /*
172 * No fallback - if we cannot get a clear cache type
173 * then bail out:
174 */
175 if (cache_type == -1)
176 return -EINVAL;
177
178 cache_op = parse_aliases(str, hw_cache_op, PERF_COUNT_HW_CACHE_OP_MAX);
179 /*
180 * Fall back to reads:
181 */
Ingo Molnar89536452009-06-06 21:04:17 +0200182 if (cache_op == -1)
183 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200184
185 cache_result = parse_aliases(str, hw_cache_result,
186 PERF_COUNT_HW_CACHE_RESULT_MAX);
187 /*
188 * Fall back to accesses:
189 */
190 if (cache_result == -1)
191 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
192
193 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
194 attr->type = PERF_TYPE_HW_CACHE;
195
196 return 0;
197}
198
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200199/*
200 * Each event can have multiple symbolic names.
201 * Symbolic names are (almost) exactly matched.
202 */
Ingo Molnar8326f442009-06-05 20:22:46 +0200203static int parse_event_symbols(const char *str, struct perf_counter_attr *attr)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200204{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000205 u64 config, id;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200206 int type;
207 unsigned int i;
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300208 const char *sep, *pstr;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200209
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200210 if (str[0] == 'r' && hex2u64(str + 1, &config) > 0) {
211 attr->type = PERF_TYPE_RAW;
212 attr->config = config;
213
214 return 0;
215 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200216
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300217 pstr = str;
218 sep = strchr(pstr, ':');
219 if (sep) {
220 type = atoi(pstr);
221 pstr = sep + 1;
222 id = atoi(pstr);
223 sep = strchr(pstr, ':');
224 if (sep) {
225 pstr = sep + 1;
226 if (strchr(pstr, 'k'))
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200227 attr->exclude_user = 1;
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300228 if (strchr(pstr, 'u'))
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200229 attr->exclude_kernel = 1;
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300230 }
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200231 attr->type = type;
232 attr->config = id;
233
234 return 0;
Ingo Molnar52425192009-05-26 09:17:18 +0200235 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200236
237 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
238 if (!strncmp(str, event_symbols[i].symbol,
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200239 strlen(event_symbols[i].symbol))) {
240
241 attr->type = event_symbols[i].type;
242 attr->config = event_symbols[i].config;
243
244 return 0;
245 }
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200246 }
247
Ingo Molnar8326f442009-06-05 20:22:46 +0200248 return parse_generic_hw_symbols(str, attr);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200249}
250
251int parse_events(const struct option *opt, const char *str, int unset)
252{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200253 struct perf_counter_attr attr;
254 int ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200255
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200256 memset(&attr, 0, sizeof(attr));
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200257again:
258 if (nr_counters == MAX_COUNTERS)
259 return -1;
260
Ingo Molnar8326f442009-06-05 20:22:46 +0200261 ret = parse_event_symbols(str, &attr);
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200262 if (ret < 0)
263 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200264
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200265 attrs[nr_counters] = attr;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200266 nr_counters++;
267
268 str = strstr(str, ",");
269 if (str) {
270 str++;
271 goto again;
272 }
273
274 return 0;
275}
276
Thomas Gleixner86847b62009-06-06 12:24:17 +0200277static const char * const event_type_descriptors[] = {
278 "",
279 "Hardware event",
280 "Software event",
281 "Tracepoint event",
282 "Hardware cache event",
283};
284
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200285/*
Thomas Gleixner86847b62009-06-06 12:24:17 +0200286 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200287 */
Thomas Gleixner86847b62009-06-06 12:24:17 +0200288void print_events(void)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200289{
Thomas Gleixner86847b62009-06-06 12:24:17 +0200290 struct event_symbol *syms = event_symbols;
291 unsigned int i, type, prev_type = -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200292
Thomas Gleixner86847b62009-06-06 12:24:17 +0200293 fprintf(stderr, "\n");
294 fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200295
Thomas Gleixner86847b62009-06-06 12:24:17 +0200296 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
297 type = syms->type + 1;
298 if (type > ARRAY_SIZE(event_type_descriptors))
299 type = 0;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200300
Thomas Gleixner86847b62009-06-06 12:24:17 +0200301 if (type != prev_type)
302 fprintf(stderr, "\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200303
Thomas Gleixner86847b62009-06-06 12:24:17 +0200304 fprintf(stderr, " %-30s [%s]\n", syms->symbol,
305 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200306
Thomas Gleixner86847b62009-06-06 12:24:17 +0200307 prev_type = type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200308 }
309
Thomas Gleixner86847b62009-06-06 12:24:17 +0200310 fprintf(stderr, "\n");
311 fprintf(stderr, " %-30s [raw hardware event descriptor]\n",
312 "rNNN");
313 fprintf(stderr, "\n");
314
315 exit(129);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200316}