Commit | Line | Data |
---|---|---|
80eeb67f AK |
1 | #define _XOPEN_SOURCE 500 /* needed for nftw() */ |
2 | #define _GNU_SOURCE /* needed for asprintf() */ | |
3 | ||
4 | /* Parse event JSON files */ | |
5 | ||
6 | /* | |
7 | * Copyright (c) 2014, Intel Corporation | |
8 | * All rights reserved. | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions are met: | |
12 | * | |
13 | * 1. Redistributions of source code must retain the above copyright notice, | |
14 | * this list of conditions and the following disclaimer. | |
15 | * | |
16 | * 2. Redistributions in binary form must reproduce the above copyright | |
17 | * notice, this list of conditions and the following disclaimer in the | |
18 | * documentation and/or other materials provided with the distribution. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
24 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
25 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
26 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
27 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
29 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
31 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #include <stdio.h> | |
35 | #include <stdlib.h> | |
36 | #include <errno.h> | |
37 | #include <string.h> | |
38 | #include <ctype.h> | |
39 | #include <unistd.h> | |
40 | #include <stdarg.h> | |
41 | #include <libgen.h> | |
42 | #include <dirent.h> | |
43 | #include <sys/time.h> /* getrlimit */ | |
44 | #include <sys/resource.h> /* getrlimit */ | |
45 | #include <ftw.h> | |
46 | #include <sys/stat.h> | |
47 | #include "jsmn.h" | |
48 | #include "json.h" | |
49 | #include "jevents.h" | |
50 | ||
80eeb67f AK |
51 | int verbose; |
52 | char *prog; | |
53 | ||
54 | int eprintf(int level, int var, const char *fmt, ...) | |
55 | { | |
56 | ||
57 | int ret; | |
58 | va_list args; | |
59 | ||
60 | if (var < level) | |
61 | return 0; | |
62 | ||
63 | va_start(args, fmt); | |
64 | ||
65 | ret = vfprintf(stderr, fmt, args); | |
66 | ||
67 | va_end(args); | |
68 | ||
69 | return ret; | |
70 | } | |
71 | ||
72 | __attribute__((weak)) char *get_cpu_str(void) | |
73 | { | |
74 | return NULL; | |
75 | } | |
76 | ||
77 | static void addfield(char *map, char **dst, const char *sep, | |
78 | const char *a, jsmntok_t *bt) | |
79 | { | |
80 | unsigned int len = strlen(a) + 1 + strlen(sep); | |
81 | int olen = *dst ? strlen(*dst) : 0; | |
82 | int blen = bt ? json_len(bt) : 0; | |
83 | char *out; | |
84 | ||
85 | out = realloc(*dst, len + olen + blen); | |
86 | if (!out) { | |
87 | /* Don't add field in this case */ | |
88 | return; | |
89 | } | |
90 | *dst = out; | |
91 | ||
92 | if (!olen) | |
93 | *(*dst) = 0; | |
94 | else | |
95 | strcat(*dst, sep); | |
96 | strcat(*dst, a); | |
97 | if (bt) | |
98 | strncat(*dst, map + bt->start, blen); | |
99 | } | |
100 | ||
101 | static void fixname(char *s) | |
102 | { | |
103 | for (; *s; s++) | |
104 | *s = tolower(*s); | |
105 | } | |
106 | ||
107 | static void fixdesc(char *s) | |
108 | { | |
109 | char *e = s + strlen(s); | |
110 | ||
111 | /* Remove trailing dots that look ugly in perf list */ | |
112 | --e; | |
113 | while (e >= s && isspace(*e)) | |
114 | --e; | |
115 | if (*e == '.') | |
116 | *e = 0; | |
117 | } | |
118 | ||
119 | static struct msrmap { | |
120 | const char *num; | |
121 | const char *pname; | |
122 | } msrmap[] = { | |
123 | { "0x3F6", "ldlat=" }, | |
124 | { "0x1A6", "offcore_rsp=" }, | |
125 | { "0x1A7", "offcore_rsp=" }, | |
b42c7369 | 126 | { "0x3F7", "frontend=" }, |
80eeb67f AK |
127 | { NULL, NULL } |
128 | }; | |
129 | ||
130 | static struct field { | |
131 | const char *field; | |
132 | const char *kernel; | |
133 | } fields[] = { | |
80eeb67f AK |
134 | { "UMask", "umask=" }, |
135 | { "CounterMask", "cmask=" }, | |
136 | { "Invert", "inv=" }, | |
137 | { "AnyThread", "any=" }, | |
138 | { "EdgeDetect", "edge=" }, | |
139 | { "SampleAfterValue", "period=" }, | |
c73881ee AK |
140 | { "FCMask", "fc_mask=" }, |
141 | { "PortMask", "ch_mask=" }, | |
80eeb67f AK |
142 | { NULL, NULL } |
143 | }; | |
144 | ||
145 | static void cut_comma(char *map, jsmntok_t *newval) | |
146 | { | |
147 | int i; | |
148 | ||
149 | /* Cut off everything after comma */ | |
150 | for (i = newval->start; i < newval->end; i++) { | |
151 | if (map[i] == ',') | |
152 | newval->end = i; | |
153 | } | |
154 | } | |
155 | ||
156 | static int match_field(char *map, jsmntok_t *field, int nz, | |
157 | char **event, jsmntok_t *val) | |
158 | { | |
159 | struct field *f; | |
160 | jsmntok_t newval = *val; | |
161 | ||
162 | for (f = fields; f->field; f++) | |
163 | if (json_streq(map, field, f->field) && nz) { | |
164 | cut_comma(map, &newval); | |
165 | addfield(map, event, ",", f->kernel, &newval); | |
166 | return 1; | |
167 | } | |
168 | return 0; | |
169 | } | |
170 | ||
171 | static struct msrmap *lookup_msr(char *map, jsmntok_t *val) | |
172 | { | |
173 | jsmntok_t newval = *val; | |
174 | static bool warned; | |
175 | int i; | |
176 | ||
177 | cut_comma(map, &newval); | |
178 | for (i = 0; msrmap[i].num; i++) | |
179 | if (json_streq(map, &newval, msrmap[i].num)) | |
180 | return &msrmap[i]; | |
181 | if (!warned) { | |
182 | warned = true; | |
183 | pr_err("%s: Unknown MSR in event file %.*s\n", prog, | |
184 | json_len(val), map + val->start); | |
185 | } | |
186 | return NULL; | |
187 | } | |
188 | ||
fedb2b51 AK |
189 | static struct map { |
190 | const char *json; | |
191 | const char *perf; | |
192 | } unit_to_pmu[] = { | |
193 | { "CBO", "uncore_cbox" }, | |
194 | { "QPI LL", "uncore_qpi" }, | |
195 | { "SBO", "uncore_sbox" }, | |
af34cb4f | 196 | { "iMPH-U", "uncore_arb" }, |
fedb2b51 AK |
197 | {} |
198 | }; | |
199 | ||
200 | static const char *field_to_perf(struct map *table, char *map, jsmntok_t *val) | |
201 | { | |
202 | int i; | |
203 | ||
204 | for (i = 0; table[i].json; i++) { | |
205 | if (json_streq(map, val, table[i].json)) | |
206 | return table[i].perf; | |
207 | } | |
208 | return NULL; | |
209 | } | |
210 | ||
80eeb67f AK |
211 | #define EXPECT(e, t, m) do { if (!(e)) { \ |
212 | jsmntok_t *loc = (t); \ | |
213 | if (!(t)->start && (t) > tokens) \ | |
214 | loc = (t) - 1; \ | |
215 | pr_err("%s:%d: " m ", got %s\n", fn, \ | |
216 | json_line(map, loc), \ | |
217 | json_name(t)); \ | |
218 | goto out_free; \ | |
219 | } } while (0) | |
220 | ||
221 | #define TOPIC_DEPTH 256 | |
222 | static char *topic_array[TOPIC_DEPTH]; | |
223 | static int topic_level; | |
224 | ||
225 | static char *get_topic(void) | |
226 | { | |
227 | char *tp_old, *tp = NULL; | |
228 | int i; | |
229 | ||
230 | for (i = 0; i < topic_level + 1; i++) { | |
231 | int n; | |
232 | ||
233 | tp_old = tp; | |
234 | n = asprintf(&tp, "%s%s", tp ?: "", topic_array[i]); | |
235 | if (n < 0) { | |
236 | pr_info("%s: asprintf() error %s\n", prog); | |
237 | return NULL; | |
238 | } | |
239 | free(tp_old); | |
240 | } | |
241 | ||
242 | for (i = 0; i < (int) strlen(tp); i++) { | |
243 | char c = tp[i]; | |
244 | ||
245 | if (c == '-') | |
246 | tp[i] = ' '; | |
247 | else if (c == '.') { | |
248 | tp[i] = '\0'; | |
249 | break; | |
250 | } | |
251 | } | |
252 | ||
253 | return tp; | |
254 | } | |
255 | ||
256 | static int add_topic(int level, char *bname) | |
257 | { | |
258 | char *topic; | |
259 | ||
260 | level -= 2; | |
261 | ||
262 | if (level >= TOPIC_DEPTH) | |
263 | return -EINVAL; | |
264 | ||
265 | topic = strdup(bname); | |
266 | if (!topic) { | |
267 | pr_info("%s: strdup() error %s for file %s\n", prog, | |
268 | strerror(errno), bname); | |
269 | return -ENOMEM; | |
270 | } | |
271 | ||
272 | free(topic_array[topic_level]); | |
273 | topic_array[topic_level] = topic; | |
274 | topic_level = level; | |
275 | return 0; | |
276 | } | |
277 | ||
278 | struct perf_entry_data { | |
279 | FILE *outfp; | |
280 | char *topic; | |
281 | }; | |
282 | ||
283 | static int close_table; | |
284 | ||
285 | static void print_events_table_prefix(FILE *fp, const char *tblname) | |
286 | { | |
287 | fprintf(fp, "struct pmu_event %s[] = {\n", tblname); | |
288 | close_table = 1; | |
289 | } | |
290 | ||
291 | static int print_events_table_entry(void *data, char *name, char *event, | |
fedb2b51 | 292 | char *desc, char *long_desc, |
00636c3b | 293 | char *pmu, char *unit, char *perpkg, |
96284814 AK |
294 | char *metric_expr, |
295 | char *metric_name) | |
80eeb67f AK |
296 | { |
297 | struct perf_entry_data *pd = data; | |
298 | FILE *outfp = pd->outfp; | |
299 | char *topic = pd->topic; | |
300 | ||
301 | /* | |
302 | * TODO: Remove formatting chars after debugging to reduce | |
303 | * string lengths. | |
304 | */ | |
305 | fprintf(outfp, "{\n"); | |
306 | ||
307 | fprintf(outfp, "\t.name = \"%s\",\n", name); | |
308 | fprintf(outfp, "\t.event = \"%s\",\n", event); | |
309 | fprintf(outfp, "\t.desc = \"%s\",\n", desc); | |
310 | fprintf(outfp, "\t.topic = \"%s\",\n", topic); | |
794ba54a SB |
311 | if (long_desc && long_desc[0]) |
312 | fprintf(outfp, "\t.long_desc = \"%s\",\n", long_desc); | |
fedb2b51 AK |
313 | if (pmu) |
314 | fprintf(outfp, "\t.pmu = \"%s\",\n", pmu); | |
315 | if (unit) | |
316 | fprintf(outfp, "\t.unit = \"%s\",\n", unit); | |
317 | if (perpkg) | |
318 | fprintf(outfp, "\t.perpkg = \"%s\",\n", perpkg); | |
00636c3b AK |
319 | if (metric_expr) |
320 | fprintf(outfp, "\t.metric_expr = \"%s\",\n", metric_expr); | |
96284814 AK |
321 | if (metric_name) |
322 | fprintf(outfp, "\t.metric_name = \"%s\",\n", metric_name); | |
80eeb67f AK |
323 | fprintf(outfp, "},\n"); |
324 | ||
325 | return 0; | |
326 | } | |
327 | ||
328 | static void print_events_table_suffix(FILE *outfp) | |
329 | { | |
330 | fprintf(outfp, "{\n"); | |
331 | ||
332 | fprintf(outfp, "\t.name = 0,\n"); | |
333 | fprintf(outfp, "\t.event = 0,\n"); | |
334 | fprintf(outfp, "\t.desc = 0,\n"); | |
335 | ||
336 | fprintf(outfp, "},\n"); | |
337 | fprintf(outfp, "};\n"); | |
338 | close_table = 0; | |
339 | } | |
340 | ||
0b1db474 AK |
341 | static struct fixed { |
342 | const char *name; | |
343 | const char *event; | |
344 | } fixed[] = { | |
345 | { "inst_retired.any", "event=0xc0" }, | |
72c6ff25 AK |
346 | { "inst_retired.any_p", "event=0xc0" }, |
347 | { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03" }, | |
0b1db474 AK |
348 | { "cpu_clk_unhalted.thread", "event=0x3c" }, |
349 | { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1" }, | |
350 | { NULL, NULL}, | |
351 | }; | |
352 | ||
353 | /* | |
354 | * Handle different fixed counter encodings between JSON and perf. | |
355 | */ | |
356 | static char *real_event(const char *name, char *event) | |
357 | { | |
358 | int i; | |
359 | ||
360 | for (i = 0; fixed[i].name; i++) | |
361 | if (!strcasecmp(name, fixed[i].name)) | |
362 | return (char *)fixed[i].event; | |
363 | return event; | |
364 | } | |
365 | ||
80eeb67f AK |
366 | /* Call func with each event in the json file */ |
367 | int json_events(const char *fn, | |
794ba54a | 368 | int (*func)(void *data, char *name, char *event, char *desc, |
fedb2b51 | 369 | char *long_desc, |
00636c3b | 370 | char *pmu, char *unit, char *perpkg, |
96284814 AK |
371 | char *metric_expr, |
372 | char *metric_name), | |
80eeb67f AK |
373 | void *data) |
374 | { | |
375 | int err = -EIO; | |
376 | size_t size; | |
377 | jsmntok_t *tokens, *tok; | |
378 | int i, j, len; | |
379 | char *map; | |
d5811419 | 380 | char buf[128]; |
80eeb67f AK |
381 | |
382 | if (!fn) | |
383 | return -ENOENT; | |
384 | ||
385 | tokens = parse_json(fn, &map, &size, &len); | |
386 | if (!tokens) | |
387 | return -EIO; | |
388 | EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array"); | |
389 | tok = tokens + 1; | |
390 | for (i = 0; i < tokens->size; i++) { | |
391 | char *event = NULL, *desc = NULL, *name = NULL; | |
794ba54a SB |
392 | char *long_desc = NULL; |
393 | char *extra_desc = NULL; | |
fedb2b51 AK |
394 | char *pmu = NULL; |
395 | char *filter = NULL; | |
396 | char *perpkg = NULL; | |
397 | char *unit = NULL; | |
00636c3b | 398 | char *metric_expr = NULL; |
96284814 | 399 | char *metric_name = NULL; |
d5811419 | 400 | unsigned long long eventcode = 0; |
80eeb67f AK |
401 | struct msrmap *msr = NULL; |
402 | jsmntok_t *msrval = NULL; | |
403 | jsmntok_t *precise = NULL; | |
404 | jsmntok_t *obj = tok++; | |
405 | ||
406 | EXPECT(obj->type == JSMN_OBJECT, obj, "expected object"); | |
407 | for (j = 0; j < obj->size; j += 2) { | |
408 | jsmntok_t *field, *val; | |
409 | int nz; | |
00636c3b | 410 | char *s; |
80eeb67f AK |
411 | |
412 | field = tok + j; | |
413 | EXPECT(field->type == JSMN_STRING, tok + j, | |
414 | "Expected field name"); | |
415 | val = tok + j + 1; | |
416 | EXPECT(val->type == JSMN_STRING, tok + j + 1, | |
417 | "Expected string value"); | |
418 | ||
419 | nz = !json_streq(map, val, "0"); | |
420 | if (match_field(map, field, nz, &event, val)) { | |
421 | /* ok */ | |
d5811419 AK |
422 | } else if (json_streq(map, field, "EventCode")) { |
423 | char *code = NULL; | |
424 | addfield(map, &code, "", "", val); | |
425 | eventcode |= strtoul(code, NULL, 0); | |
426 | free(code); | |
fedb2b51 AK |
427 | } else if (json_streq(map, field, "ExtSel")) { |
428 | char *code = NULL; | |
429 | addfield(map, &code, "", "", val); | |
430 | eventcode |= strtoul(code, NULL, 0) << 21; | |
431 | free(code); | |
80eeb67f AK |
432 | } else if (json_streq(map, field, "EventName")) { |
433 | addfield(map, &name, "", "", val); | |
434 | } else if (json_streq(map, field, "BriefDescription")) { | |
435 | addfield(map, &desc, "", "", val); | |
436 | fixdesc(desc); | |
794ba54a SB |
437 | } else if (json_streq(map, field, |
438 | "PublicDescription")) { | |
439 | addfield(map, &long_desc, "", "", val); | |
440 | fixdesc(long_desc); | |
80eeb67f AK |
441 | } else if (json_streq(map, field, "PEBS") && nz) { |
442 | precise = val; | |
443 | } else if (json_streq(map, field, "MSRIndex") && nz) { | |
444 | msr = lookup_msr(map, val); | |
445 | } else if (json_streq(map, field, "MSRValue")) { | |
446 | msrval = val; | |
447 | } else if (json_streq(map, field, "Errata") && | |
448 | !json_streq(map, val, "null")) { | |
794ba54a | 449 | addfield(map, &extra_desc, ". ", |
80eeb67f AK |
450 | " Spec update: ", val); |
451 | } else if (json_streq(map, field, "Data_LA") && nz) { | |
794ba54a | 452 | addfield(map, &extra_desc, ". ", |
80eeb67f AK |
453 | " Supports address when precise", |
454 | NULL); | |
fedb2b51 AK |
455 | } else if (json_streq(map, field, "Unit")) { |
456 | const char *ppmu; | |
fedb2b51 AK |
457 | |
458 | ppmu = field_to_perf(unit_to_pmu, map, val); | |
459 | if (ppmu) { | |
460 | pmu = strdup(ppmu); | |
461 | } else { | |
462 | if (!pmu) | |
463 | pmu = strdup("uncore_"); | |
464 | addfield(map, &pmu, "", "", val); | |
465 | for (s = pmu; *s; s++) | |
466 | *s = tolower(*s); | |
467 | } | |
468 | addfield(map, &desc, ". ", "Unit: ", NULL); | |
469 | addfield(map, &desc, "", pmu, NULL); | |
3401e8d1 | 470 | addfield(map, &desc, "", " ", NULL); |
fedb2b51 AK |
471 | } else if (json_streq(map, field, "Filter")) { |
472 | addfield(map, &filter, "", "", val); | |
473 | } else if (json_streq(map, field, "ScaleUnit")) { | |
474 | addfield(map, &unit, "", "", val); | |
475 | } else if (json_streq(map, field, "PerPkg")) { | |
476 | addfield(map, &perpkg, "", "", val); | |
96284814 AK |
477 | } else if (json_streq(map, field, "MetricName")) { |
478 | addfield(map, &metric_name, "", "", val); | |
00636c3b AK |
479 | } else if (json_streq(map, field, "MetricExpr")) { |
480 | addfield(map, &metric_expr, "", "", val); | |
481 | for (s = metric_expr; *s; s++) | |
482 | *s = tolower(*s); | |
80eeb67f AK |
483 | } |
484 | /* ignore unknown fields */ | |
485 | } | |
486 | if (precise && desc && !strstr(desc, "(Precise Event)")) { | |
487 | if (json_streq(map, precise, "2")) | |
794ba54a SB |
488 | addfield(map, &extra_desc, " ", |
489 | "(Must be precise)", NULL); | |
80eeb67f | 490 | else |
794ba54a | 491 | addfield(map, &extra_desc, " ", |
80eeb67f AK |
492 | "(Precise event)", NULL); |
493 | } | |
d5811419 AK |
494 | snprintf(buf, sizeof buf, "event=%#llx", eventcode); |
495 | addfield(map, &event, ",", buf, NULL); | |
794ba54a SB |
496 | if (desc && extra_desc) |
497 | addfield(map, &desc, " ", extra_desc, NULL); | |
498 | if (long_desc && extra_desc) | |
499 | addfield(map, &long_desc, " ", extra_desc, NULL); | |
fedb2b51 AK |
500 | if (filter) |
501 | addfield(map, &event, ",", filter, NULL); | |
80eeb67f AK |
502 | if (msr != NULL) |
503 | addfield(map, &event, ",", msr->pname, msrval); | |
504 | fixname(name); | |
794ba54a | 505 | |
fedb2b51 | 506 | err = func(data, name, real_event(name, event), desc, long_desc, |
96284814 | 507 | pmu, unit, perpkg, metric_expr, metric_name); |
80eeb67f AK |
508 | free(event); |
509 | free(desc); | |
510 | free(name); | |
794ba54a SB |
511 | free(long_desc); |
512 | free(extra_desc); | |
fedb2b51 AK |
513 | free(pmu); |
514 | free(filter); | |
515 | free(perpkg); | |
516 | free(unit); | |
00636c3b | 517 | free(metric_expr); |
96284814 | 518 | free(metric_name); |
80eeb67f AK |
519 | if (err) |
520 | break; | |
521 | tok += j; | |
522 | } | |
523 | EXPECT(tok - tokens == len, tok, "unexpected objects at end"); | |
524 | err = 0; | |
525 | out_free: | |
526 | free_json(map, size, tokens); | |
527 | return err; | |
528 | } | |
529 | ||
530 | static char *file_name_to_table_name(char *fname) | |
531 | { | |
532 | unsigned int i; | |
533 | int n; | |
534 | int c; | |
535 | char *tblname; | |
536 | ||
537 | /* | |
538 | * Ensure tablename starts with alphabetic character. | |
539 | * Derive rest of table name from basename of the JSON file, | |
540 | * replacing hyphens and stripping out .json suffix. | |
541 | */ | |
542 | n = asprintf(&tblname, "pme_%s", basename(fname)); | |
543 | if (n < 0) { | |
544 | pr_info("%s: asprintf() error %s for file %s\n", prog, | |
545 | strerror(errno), fname); | |
546 | return NULL; | |
547 | } | |
548 | ||
549 | for (i = 0; i < strlen(tblname); i++) { | |
550 | c = tblname[i]; | |
551 | ||
552 | if (c == '-') | |
553 | tblname[i] = '_'; | |
554 | else if (c == '.') { | |
555 | tblname[i] = '\0'; | |
556 | break; | |
557 | } else if (!isalnum(c) && c != '_') { | |
558 | pr_err("%s: Invalid character '%c' in file name %s\n", | |
559 | prog, c, basename(fname)); | |
560 | free(tblname); | |
561 | tblname = NULL; | |
562 | break; | |
563 | } | |
564 | } | |
565 | ||
566 | return tblname; | |
567 | } | |
568 | ||
569 | static void print_mapping_table_prefix(FILE *outfp) | |
570 | { | |
571 | fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n"); | |
572 | } | |
573 | ||
574 | static void print_mapping_table_suffix(FILE *outfp) | |
575 | { | |
576 | /* | |
577 | * Print the terminating, NULL entry. | |
578 | */ | |
579 | fprintf(outfp, "{\n"); | |
580 | fprintf(outfp, "\t.cpuid = 0,\n"); | |
581 | fprintf(outfp, "\t.version = 0,\n"); | |
582 | fprintf(outfp, "\t.type = 0,\n"); | |
583 | fprintf(outfp, "\t.table = 0,\n"); | |
584 | fprintf(outfp, "},\n"); | |
585 | ||
586 | /* and finally, the closing curly bracket for the struct */ | |
587 | fprintf(outfp, "};\n"); | |
588 | } | |
589 | ||
590 | static int process_mapfile(FILE *outfp, char *fpath) | |
591 | { | |
592 | int n = 16384; | |
593 | FILE *mapfp; | |
594 | char *save = NULL; | |
595 | char *line, *p; | |
596 | int line_num; | |
597 | char *tblname; | |
598 | ||
599 | pr_info("%s: Processing mapfile %s\n", prog, fpath); | |
600 | ||
601 | line = malloc(n); | |
602 | if (!line) | |
603 | return -1; | |
604 | ||
605 | mapfp = fopen(fpath, "r"); | |
606 | if (!mapfp) { | |
607 | pr_info("%s: Error %s opening %s\n", prog, strerror(errno), | |
608 | fpath); | |
609 | return -1; | |
610 | } | |
611 | ||
612 | print_mapping_table_prefix(outfp); | |
613 | ||
dc720ffc AK |
614 | /* Skip first line (header) */ |
615 | p = fgets(line, n, mapfp); | |
616 | if (!p) | |
617 | goto out; | |
618 | ||
619 | line_num = 1; | |
80eeb67f AK |
620 | while (1) { |
621 | char *cpuid, *version, *type, *fname; | |
622 | ||
623 | line_num++; | |
624 | p = fgets(line, n, mapfp); | |
625 | if (!p) | |
626 | break; | |
627 | ||
628 | if (line[0] == '#' || line[0] == '\n') | |
629 | continue; | |
630 | ||
631 | if (line[strlen(line)-1] != '\n') { | |
632 | /* TODO Deal with lines longer than 16K */ | |
633 | pr_info("%s: Mapfile %s: line %d too long, aborting\n", | |
634 | prog, fpath, line_num); | |
635 | return -1; | |
636 | } | |
637 | line[strlen(line)-1] = '\0'; | |
638 | ||
639 | cpuid = strtok_r(p, ",", &save); | |
640 | version = strtok_r(NULL, ",", &save); | |
641 | fname = strtok_r(NULL, ",", &save); | |
642 | type = strtok_r(NULL, ",", &save); | |
643 | ||
644 | tblname = file_name_to_table_name(fname); | |
645 | fprintf(outfp, "{\n"); | |
646 | fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid); | |
647 | fprintf(outfp, "\t.version = \"%s\",\n", version); | |
648 | fprintf(outfp, "\t.type = \"%s\",\n", type); | |
649 | ||
650 | /* | |
651 | * CHECK: We can't use the type (eg "core") field in the | |
652 | * table name. For us to do that, we need to somehow tweak | |
653 | * the other caller of file_name_to_table(), process_json() | |
654 | * to determine the type. process_json() file has no way | |
655 | * of knowing these are "core" events unless file name has | |
656 | * core in it. If filename has core in it, we can safely | |
657 | * ignore the type field here also. | |
658 | */ | |
659 | fprintf(outfp, "\t.table = %s\n", tblname); | |
660 | fprintf(outfp, "},\n"); | |
661 | } | |
662 | ||
dc720ffc | 663 | out: |
80eeb67f | 664 | print_mapping_table_suffix(outfp); |
80eeb67f AK |
665 | return 0; |
666 | } | |
667 | ||
668 | /* | |
669 | * If we fail to locate/process JSON and map files, create a NULL mapping | |
670 | * table. This would at least allow perf to build even if we can't find/use | |
671 | * the aliases. | |
672 | */ | |
673 | static void create_empty_mapping(const char *output_file) | |
674 | { | |
675 | FILE *outfp; | |
676 | ||
677 | pr_info("%s: Creating empty pmu_events_map[] table\n", prog); | |
678 | ||
679 | /* Truncate file to clear any partial writes to it */ | |
680 | outfp = fopen(output_file, "w"); | |
681 | if (!outfp) { | |
682 | perror("fopen()"); | |
683 | _Exit(1); | |
684 | } | |
685 | ||
686 | fprintf(outfp, "#include \"../../pmu-events/pmu-events.h\"\n"); | |
687 | print_mapping_table_prefix(outfp); | |
688 | print_mapping_table_suffix(outfp); | |
689 | fclose(outfp); | |
690 | } | |
691 | ||
692 | static int get_maxfds(void) | |
693 | { | |
694 | struct rlimit rlim; | |
695 | ||
696 | if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) | |
697 | return min((int)rlim.rlim_max / 2, 512); | |
698 | ||
699 | return 512; | |
700 | } | |
701 | ||
702 | /* | |
703 | * nftw() doesn't let us pass an argument to the processing function, | |
704 | * so use a global variables. | |
705 | */ | |
706 | static FILE *eventsfp; | |
707 | static char *mapfile; | |
708 | ||
709 | static int process_one_file(const char *fpath, const struct stat *sb, | |
710 | int typeflag, struct FTW *ftwbuf) | |
711 | { | |
712 | char *tblname, *bname = (char *) fpath + ftwbuf->base; | |
713 | int is_dir = typeflag == FTW_D; | |
714 | int is_file = typeflag == FTW_F; | |
715 | int level = ftwbuf->level; | |
716 | int err = 0; | |
717 | ||
718 | pr_debug("%s %d %7jd %-20s %s\n", | |
719 | is_file ? "f" : is_dir ? "d" : "x", | |
720 | level, sb->st_size, bname, fpath); | |
721 | ||
722 | /* base dir */ | |
723 | if (level == 0) | |
724 | return 0; | |
725 | ||
726 | /* model directory, reset topic */ | |
727 | if (level == 1 && is_dir) { | |
728 | if (close_table) | |
729 | print_events_table_suffix(eventsfp); | |
730 | ||
731 | /* | |
732 | * Drop file name suffix. Replace hyphens with underscores. | |
733 | * Fail if file name contains any alphanum characters besides | |
734 | * underscores. | |
735 | */ | |
736 | tblname = file_name_to_table_name(bname); | |
737 | if (!tblname) { | |
738 | pr_info("%s: Error determining table name for %s\n", prog, | |
739 | bname); | |
740 | return -1; | |
741 | } | |
742 | ||
743 | print_events_table_prefix(eventsfp, tblname); | |
744 | return 0; | |
745 | } | |
746 | ||
747 | /* | |
748 | * Save the mapfile name for now. We will process mapfile | |
749 | * after processing all JSON files (so we can write out the | |
750 | * mapping table after all PMU events tables). | |
751 | * | |
752 | * TODO: Allow for multiple mapfiles? Punt for now. | |
753 | */ | |
754 | if (level == 1 && is_file) { | |
755 | if (!strncmp(bname, "mapfile.csv", 11)) { | |
756 | if (mapfile) { | |
757 | pr_info("%s: Many mapfiles? Using %s, ignoring %s\n", | |
758 | prog, mapfile, fpath); | |
759 | } else { | |
760 | mapfile = strdup(fpath); | |
761 | } | |
762 | return 0; | |
763 | } | |
764 | ||
765 | pr_info("%s: Ignoring file %s\n", prog, fpath); | |
766 | return 0; | |
767 | } | |
768 | ||
769 | /* | |
770 | * If the file name does not have a .json extension, | |
771 | * ignore it. It could be a readme.txt for instance. | |
772 | */ | |
773 | if (is_file) { | |
774 | char *suffix = bname + strlen(bname) - 5; | |
775 | ||
776 | if (strncmp(suffix, ".json", 5)) { | |
777 | pr_info("%s: Ignoring file without .json suffix %s\n", prog, | |
778 | fpath); | |
779 | return 0; | |
780 | } | |
781 | } | |
782 | ||
783 | if (level > 1 && add_topic(level, bname)) | |
784 | return -ENOMEM; | |
785 | ||
786 | /* | |
787 | * Assume all other files are JSON files. | |
788 | * | |
789 | * If mapfile refers to 'power7_core.json', we create a table | |
790 | * named 'power7_core'. Any inconsistencies between the mapfile | |
791 | * and directory tree could result in build failure due to table | |
792 | * names not being found. | |
793 | * | |
794 | * Atleast for now, be strict with processing JSON file names. | |
795 | * i.e. if JSON file name cannot be mapped to C-style table name, | |
796 | * fail. | |
797 | */ | |
798 | if (is_file) { | |
799 | struct perf_entry_data data = { | |
800 | .topic = get_topic(), | |
801 | .outfp = eventsfp, | |
802 | }; | |
803 | ||
804 | err = json_events(fpath, print_events_table_entry, &data); | |
805 | ||
806 | free(data.topic); | |
807 | } | |
808 | ||
809 | return err; | |
810 | } | |
811 | ||
812 | #ifndef PATH_MAX | |
813 | #define PATH_MAX 4096 | |
814 | #endif | |
815 | ||
816 | /* | |
817 | * Starting in directory 'start_dirname', find the "mapfile.csv" and | |
818 | * the set of JSON files for the architecture 'arch'. | |
819 | * | |
820 | * From each JSON file, create a C-style "PMU events table" from the | |
821 | * JSON file (see struct pmu_event). | |
822 | * | |
823 | * From the mapfile, create a mapping between the CPU revisions and | |
824 | * PMU event tables (see struct pmu_events_map). | |
825 | * | |
826 | * Write out the PMU events tables and the mapping table to pmu-event.c. | |
80eeb67f AK |
827 | */ |
828 | int main(int argc, char *argv[]) | |
829 | { | |
830 | int rc; | |
831 | int maxfds; | |
832 | char ldirname[PATH_MAX]; | |
833 | ||
834 | const char *arch; | |
835 | const char *output_file; | |
836 | const char *start_dirname; | |
3f056b66 | 837 | struct stat stbuf; |
80eeb67f AK |
838 | |
839 | prog = basename(argv[0]); | |
840 | if (argc < 4) { | |
841 | pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog); | |
842 | return 1; | |
843 | } | |
844 | ||
845 | arch = argv[1]; | |
846 | start_dirname = argv[2]; | |
847 | output_file = argv[3]; | |
848 | ||
849 | if (argc > 4) | |
850 | verbose = atoi(argv[4]); | |
851 | ||
852 | eventsfp = fopen(output_file, "w"); | |
853 | if (!eventsfp) { | |
854 | pr_err("%s Unable to create required file %s (%s)\n", | |
855 | prog, output_file, strerror(errno)); | |
856 | return 2; | |
857 | } | |
858 | ||
3f056b66 AK |
859 | sprintf(ldirname, "%s/%s", start_dirname, arch); |
860 | ||
861 | /* If architecture does not have any event lists, bail out */ | |
862 | if (stat(ldirname, &stbuf) < 0) { | |
863 | pr_info("%s: Arch %s has no PMU event lists\n", prog, arch); | |
864 | goto empty_map; | |
865 | } | |
866 | ||
80eeb67f AK |
867 | /* Include pmu-events.h first */ |
868 | fprintf(eventsfp, "#include \"../../pmu-events/pmu-events.h\"\n"); | |
869 | ||
80eeb67f AK |
870 | /* |
871 | * The mapfile allows multiple CPUids to point to the same JSON file, | |
872 | * so, not sure if there is a need for symlinks within the pmu-events | |
873 | * directory. | |
874 | * | |
875 | * For now, treat symlinks of JSON files as regular files and create | |
876 | * separate tables for each symlink (presumably, each symlink refers | |
877 | * to specific version of the CPU). | |
878 | */ | |
879 | ||
880 | maxfds = get_maxfds(); | |
881 | mapfile = NULL; | |
882 | rc = nftw(ldirname, process_one_file, maxfds, 0); | |
883 | if (rc && verbose) { | |
884 | pr_info("%s: Error walking file tree %s\n", prog, ldirname); | |
885 | goto empty_map; | |
3f056b66 AK |
886 | } else if (rc < 0) { |
887 | /* Make build fail */ | |
888 | return 1; | |
80eeb67f AK |
889 | } else if (rc) { |
890 | goto empty_map; | |
891 | } | |
892 | ||
893 | if (close_table) | |
894 | print_events_table_suffix(eventsfp); | |
895 | ||
896 | if (!mapfile) { | |
897 | pr_info("%s: No CPU->JSON mapping?\n", prog); | |
898 | goto empty_map; | |
899 | } | |
900 | ||
901 | if (process_mapfile(eventsfp, mapfile)) { | |
902 | pr_info("%s: Error processing mapfile %s\n", prog, mapfile); | |
3f056b66 AK |
903 | /* Make build fail */ |
904 | return 1; | |
80eeb67f AK |
905 | } |
906 | ||
907 | return 0; | |
908 | ||
909 | empty_map: | |
910 | fclose(eventsfp); | |
911 | create_empty_mapping(output_file); | |
912 | return 0; | |
913 | } |