perf_counter: Implement generalized cache event types
[deliverable/linux.git] / Documentation / perf_counter / util / parse-events.c
CommitLineData
8ad8db37
IM
1
2#include "../perf.h"
3#include "util.h"
4#include "parse-options.h"
5#include "parse-events.h"
6#include "exec_cmd.h"
a0055ae2 7#include "string.h"
8ad8db37 8
8326f44d
IM
9extern char *strcasestr(const char *haystack, const char *needle);
10
a21ca2ca 11int nr_counters;
8ad8db37 12
a21ca2ca 13struct perf_counter_attr attrs[MAX_COUNTERS];
8ad8db37
IM
14
15struct event_symbol {
a21ca2ca
IM
16 __u8 type;
17 __u64 config;
18 char *symbol;
8ad8db37
IM
19};
20
a21ca2ca 21#define C(x, y) .type = PERF_TYPE_##x, .config = PERF_COUNT_##y
8326f44d 22#define CR(x, y) .type = PERF_TYPE_##x, .config = y
a21ca2ca 23
8ad8db37 24static struct event_symbol event_symbols[] = {
a21ca2ca
IM
25 { C(HARDWARE, CPU_CYCLES), "cpu-cycles", },
26 { C(HARDWARE, CPU_CYCLES), "cycles", },
27 { C(HARDWARE, INSTRUCTIONS), "instructions", },
28 { C(HARDWARE, CACHE_REFERENCES), "cache-references", },
29 { C(HARDWARE, CACHE_MISSES), "cache-misses", },
30 { C(HARDWARE, BRANCH_INSTRUCTIONS), "branch-instructions", },
31 { C(HARDWARE, BRANCH_INSTRUCTIONS), "branches", },
32 { C(HARDWARE, BRANCH_MISSES), "branch-misses", },
33 { C(HARDWARE, BUS_CYCLES), "bus-cycles", },
34
35 { C(SOFTWARE, CPU_CLOCK), "cpu-clock", },
36 { C(SOFTWARE, TASK_CLOCK), "task-clock", },
37 { C(SOFTWARE, PAGE_FAULTS), "page-faults", },
38 { C(SOFTWARE, PAGE_FAULTS), "faults", },
39 { C(SOFTWARE, PAGE_FAULTS_MIN), "minor-faults", },
40 { C(SOFTWARE, PAGE_FAULTS_MAJ), "major-faults", },
41 { C(SOFTWARE, CONTEXT_SWITCHES), "context-switches", },
42 { C(SOFTWARE, CONTEXT_SWITCHES), "cs", },
43 { C(SOFTWARE, CPU_MIGRATIONS), "cpu-migrations", },
44 { C(SOFTWARE, CPU_MIGRATIONS), "migrations", },
8ad8db37
IM
45};
46
5242519b
IM
47#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[] = {
56 "CPU cycles",
57 "instructions",
58 "cache references",
59 "cache misses",
60 "branches",
61 "branch misses",
62 "bus cycles",
63};
64
65static char *sw_event_names[] = {
66 "cpu clock ticks",
67 "task clock ticks",
68 "pagefaults",
69 "context switches",
70 "CPU migrations",
71 "minor faults",
72 "major faults",
73};
74
8326f44d
IM
75#define MAX_ALIASES 8
76
77static char *hw_cache [][MAX_ALIASES] = {
78 { "l1-d" , "l1d" , "l1", "l1-data-cache" },
79 { "l1-i" , "l1i" , "l1-instruction-cache" },
80 { "l2" , },
81 { "dtlb", },
82 { "itlb", },
83 { "bpu" , "btb", "branch-cache", NULL },
84};
85
86static char *hw_cache_op [][MAX_ALIASES] = {
87 { "read" , "load" },
88 { "write" , "store" },
89 { "prefetch" , "speculative-read", "speculative-load" },
90};
91
92static char *hw_cache_result [][MAX_ALIASES] = {
93 { "access", "ops" },
94 { "miss", },
95};
96
a21ca2ca 97char *event_name(int counter)
5242519b 98{
a21ca2ca
IM
99 __u64 config = attrs[counter].config;
100 int type = attrs[counter].type;
5242519b
IM
101 static char buf[32];
102
a21ca2ca
IM
103 if (attrs[counter].type == PERF_TYPE_RAW) {
104 sprintf(buf, "raw 0x%llx", config);
5242519b
IM
105 return buf;
106 }
107
108 switch (type) {
109 case PERF_TYPE_HARDWARE:
a21ca2ca
IM
110 if (config < PERF_HW_EVENTS_MAX)
111 return hw_event_names[config];
5242519b
IM
112 return "unknown-hardware";
113
8326f44d
IM
114 case PERF_TYPE_HW_CACHE: {
115 __u8 cache_type, cache_op, cache_result;
116 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;
123 if (cache_type > PERF_COUNT_HW_CACHE_OP_MAX)
124 return "unknown-ext-hardware-cache-op-type";
125
126 cache_result = (config >> 16) & 0xff;
127 if (cache_type > PERF_COUNT_HW_CACHE_RESULT_MAX)
128 return "unknown-ext-hardware-cache-result-type";
129
130 sprintf(name, "%s:%s:%s",
131 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
5242519b 138 case PERF_TYPE_SOFTWARE:
a21ca2ca
IM
139 if (config < PERF_SW_EVENTS_MAX)
140 return sw_event_names[config];
5242519b
IM
141 return "unknown-software";
142
143 default:
144 break;
145 }
146
147 return "unknown";
148}
149
8326f44d
IM
150static 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
163 return 0;
164}
165
166static int parse_generic_hw_symbols(const char *str, struct perf_counter_attr *attr)
167{
168 __u8 cache_type = -1, cache_op = 0, cache_result = 0;
169
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 */
182 if (cache_type == -1)
183 cache_type = PERF_COUNT_HW_CACHE_OP_READ;
184
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
8ad8db37
IM
199/*
200 * Each event can have multiple symbolic names.
201 * Symbolic names are (almost) exactly matched.
202 */
8326f44d 203static int parse_event_symbols(const char *str, struct perf_counter_attr *attr)
8ad8db37
IM
204{
205 __u64 config, id;
206 int type;
207 unsigned int i;
a0055ae2 208 const char *sep, *pstr;
8ad8db37 209
a21ca2ca
IM
210 if (str[0] == 'r' && hex2u64(str + 1, &config) > 0) {
211 attr->type = PERF_TYPE_RAW;
212 attr->config = config;
213
214 return 0;
215 }
8ad8db37 216
a0055ae2
ACM
217 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'))
a21ca2ca 227 attr->exclude_user = 1;
a0055ae2 228 if (strchr(pstr, 'u'))
a21ca2ca 229 attr->exclude_kernel = 1;
a0055ae2 230 }
a21ca2ca
IM
231 attr->type = type;
232 attr->config = id;
233
234 return 0;
5242519b 235 }
8ad8db37
IM
236
237 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
238 if (!strncmp(str, event_symbols[i].symbol,
a21ca2ca
IM
239 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 }
8ad8db37
IM
246 }
247
8326f44d 248 return parse_generic_hw_symbols(str, attr);
8ad8db37
IM
249}
250
251int parse_events(const struct option *opt, const char *str, int unset)
252{
a21ca2ca
IM
253 struct perf_counter_attr attr;
254 int ret;
8ad8db37 255
a21ca2ca 256 memset(&attr, 0, sizeof(attr));
8ad8db37
IM
257again:
258 if (nr_counters == MAX_COUNTERS)
259 return -1;
260
8326f44d 261 ret = parse_event_symbols(str, &attr);
a21ca2ca
IM
262 if (ret < 0)
263 return ret;
8ad8db37 264
a21ca2ca 265 attrs[nr_counters] = attr;
8ad8db37
IM
266 nr_counters++;
267
268 str = strstr(str, ",");
269 if (str) {
270 str++;
271 goto again;
272 }
273
274 return 0;
275}
276
8ad8db37
IM
277/*
278 * Create the help text for the event symbols:
279 */
280void create_events_help(char *events_help_msg)
281{
282 unsigned int i;
283 char *str;
8ad8db37
IM
284
285 str = events_help_msg;
286
287 str += sprintf(str,
288 "event name: [");
289
290 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
291 int type, id;
292
a21ca2ca
IM
293 type = event_symbols[i].type;
294 id = event_symbols[i].config;
8ad8db37
IM
295
296 if (i)
297 str += sprintf(str, "|");
298
299 str += sprintf(str, "%s",
300 event_symbols[i].symbol);
301 }
302
303 str += sprintf(str, "|rNNN]");
304}
This page took 0.051051 seconds and 5 git commands to generate.