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