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=" }, | |
130 | { NULL, NULL } | |
131 | }; | |
132 | ||
133 | static struct field { | |
134 | const char *field; | |
135 | const char *kernel; | |
136 | } fields[] = { | |
137 | { "EventCode", "event=" }, | |
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 | ||
191 | #define EXPECT(e, t, m) do { if (!(e)) { \ | |
192 | jsmntok_t *loc = (t); \ | |
193 | if (!(t)->start && (t) > tokens) \ | |
194 | loc = (t) - 1; \ | |
195 | pr_err("%s:%d: " m ", got %s\n", fn, \ | |
196 | json_line(map, loc), \ | |
197 | json_name(t)); \ | |
198 | goto out_free; \ | |
199 | } } while (0) | |
200 | ||
201 | #define TOPIC_DEPTH 256 | |
202 | static char *topic_array[TOPIC_DEPTH]; | |
203 | static int topic_level; | |
204 | ||
205 | static char *get_topic(void) | |
206 | { | |
207 | char *tp_old, *tp = NULL; | |
208 | int i; | |
209 | ||
210 | for (i = 0; i < topic_level + 1; i++) { | |
211 | int n; | |
212 | ||
213 | tp_old = tp; | |
214 | n = asprintf(&tp, "%s%s", tp ?: "", topic_array[i]); | |
215 | if (n < 0) { | |
216 | pr_info("%s: asprintf() error %s\n", prog); | |
217 | return NULL; | |
218 | } | |
219 | free(tp_old); | |
220 | } | |
221 | ||
222 | for (i = 0; i < (int) strlen(tp); i++) { | |
223 | char c = tp[i]; | |
224 | ||
225 | if (c == '-') | |
226 | tp[i] = ' '; | |
227 | else if (c == '.') { | |
228 | tp[i] = '\0'; | |
229 | break; | |
230 | } | |
231 | } | |
232 | ||
233 | return tp; | |
234 | } | |
235 | ||
236 | static int add_topic(int level, char *bname) | |
237 | { | |
238 | char *topic; | |
239 | ||
240 | level -= 2; | |
241 | ||
242 | if (level >= TOPIC_DEPTH) | |
243 | return -EINVAL; | |
244 | ||
245 | topic = strdup(bname); | |
246 | if (!topic) { | |
247 | pr_info("%s: strdup() error %s for file %s\n", prog, | |
248 | strerror(errno), bname); | |
249 | return -ENOMEM; | |
250 | } | |
251 | ||
252 | free(topic_array[topic_level]); | |
253 | topic_array[topic_level] = topic; | |
254 | topic_level = level; | |
255 | return 0; | |
256 | } | |
257 | ||
258 | struct perf_entry_data { | |
259 | FILE *outfp; | |
260 | char *topic; | |
261 | }; | |
262 | ||
263 | static int close_table; | |
264 | ||
265 | static void print_events_table_prefix(FILE *fp, const char *tblname) | |
266 | { | |
267 | fprintf(fp, "struct pmu_event %s[] = {\n", tblname); | |
268 | close_table = 1; | |
269 | } | |
270 | ||
271 | static int print_events_table_entry(void *data, char *name, char *event, | |
794ba54a | 272 | char *desc, char *long_desc) |
80eeb67f AK |
273 | { |
274 | struct perf_entry_data *pd = data; | |
275 | FILE *outfp = pd->outfp; | |
276 | char *topic = pd->topic; | |
277 | ||
278 | /* | |
279 | * TODO: Remove formatting chars after debugging to reduce | |
280 | * string lengths. | |
281 | */ | |
282 | fprintf(outfp, "{\n"); | |
283 | ||
284 | fprintf(outfp, "\t.name = \"%s\",\n", name); | |
285 | fprintf(outfp, "\t.event = \"%s\",\n", event); | |
286 | fprintf(outfp, "\t.desc = \"%s\",\n", desc); | |
287 | fprintf(outfp, "\t.topic = \"%s\",\n", topic); | |
794ba54a SB |
288 | if (long_desc && long_desc[0]) |
289 | fprintf(outfp, "\t.long_desc = \"%s\",\n", long_desc); | |
80eeb67f AK |
290 | |
291 | fprintf(outfp, "},\n"); | |
292 | ||
293 | return 0; | |
294 | } | |
295 | ||
296 | static void print_events_table_suffix(FILE *outfp) | |
297 | { | |
298 | fprintf(outfp, "{\n"); | |
299 | ||
300 | fprintf(outfp, "\t.name = 0,\n"); | |
301 | fprintf(outfp, "\t.event = 0,\n"); | |
302 | fprintf(outfp, "\t.desc = 0,\n"); | |
303 | ||
304 | fprintf(outfp, "},\n"); | |
305 | fprintf(outfp, "};\n"); | |
306 | close_table = 0; | |
307 | } | |
308 | ||
0b1db474 AK |
309 | static struct fixed { |
310 | const char *name; | |
311 | const char *event; | |
312 | } fixed[] = { | |
313 | { "inst_retired.any", "event=0xc0" }, | |
314 | { "cpu_clk_unhalted.thread", "event=0x3c" }, | |
315 | { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1" }, | |
316 | { NULL, NULL}, | |
317 | }; | |
318 | ||
319 | /* | |
320 | * Handle different fixed counter encodings between JSON and perf. | |
321 | */ | |
322 | static char *real_event(const char *name, char *event) | |
323 | { | |
324 | int i; | |
325 | ||
326 | for (i = 0; fixed[i].name; i++) | |
327 | if (!strcasecmp(name, fixed[i].name)) | |
328 | return (char *)fixed[i].event; | |
329 | return event; | |
330 | } | |
331 | ||
80eeb67f AK |
332 | /* Call func with each event in the json file */ |
333 | int json_events(const char *fn, | |
794ba54a SB |
334 | int (*func)(void *data, char *name, char *event, char *desc, |
335 | char *long_desc), | |
80eeb67f AK |
336 | void *data) |
337 | { | |
338 | int err = -EIO; | |
339 | size_t size; | |
340 | jsmntok_t *tokens, *tok; | |
341 | int i, j, len; | |
342 | char *map; | |
343 | ||
344 | if (!fn) | |
345 | return -ENOENT; | |
346 | ||
347 | tokens = parse_json(fn, &map, &size, &len); | |
348 | if (!tokens) | |
349 | return -EIO; | |
350 | EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array"); | |
351 | tok = tokens + 1; | |
352 | for (i = 0; i < tokens->size; i++) { | |
353 | char *event = NULL, *desc = NULL, *name = NULL; | |
794ba54a SB |
354 | char *long_desc = NULL; |
355 | char *extra_desc = NULL; | |
80eeb67f AK |
356 | struct msrmap *msr = NULL; |
357 | jsmntok_t *msrval = NULL; | |
358 | jsmntok_t *precise = NULL; | |
359 | jsmntok_t *obj = tok++; | |
360 | ||
361 | EXPECT(obj->type == JSMN_OBJECT, obj, "expected object"); | |
362 | for (j = 0; j < obj->size; j += 2) { | |
363 | jsmntok_t *field, *val; | |
364 | int nz; | |
365 | ||
366 | field = tok + j; | |
367 | EXPECT(field->type == JSMN_STRING, tok + j, | |
368 | "Expected field name"); | |
369 | val = tok + j + 1; | |
370 | EXPECT(val->type == JSMN_STRING, tok + j + 1, | |
371 | "Expected string value"); | |
372 | ||
373 | nz = !json_streq(map, val, "0"); | |
374 | if (match_field(map, field, nz, &event, val)) { | |
375 | /* ok */ | |
376 | } else if (json_streq(map, field, "EventName")) { | |
377 | addfield(map, &name, "", "", val); | |
378 | } else if (json_streq(map, field, "BriefDescription")) { | |
379 | addfield(map, &desc, "", "", val); | |
380 | fixdesc(desc); | |
794ba54a SB |
381 | } else if (json_streq(map, field, |
382 | "PublicDescription")) { | |
383 | addfield(map, &long_desc, "", "", val); | |
384 | fixdesc(long_desc); | |
80eeb67f AK |
385 | } else if (json_streq(map, field, "PEBS") && nz) { |
386 | precise = val; | |
387 | } else if (json_streq(map, field, "MSRIndex") && nz) { | |
388 | msr = lookup_msr(map, val); | |
389 | } else if (json_streq(map, field, "MSRValue")) { | |
390 | msrval = val; | |
391 | } else if (json_streq(map, field, "Errata") && | |
392 | !json_streq(map, val, "null")) { | |
794ba54a | 393 | addfield(map, &extra_desc, ". ", |
80eeb67f AK |
394 | " Spec update: ", val); |
395 | } else if (json_streq(map, field, "Data_LA") && nz) { | |
794ba54a | 396 | addfield(map, &extra_desc, ". ", |
80eeb67f AK |
397 | " Supports address when precise", |
398 | NULL); | |
399 | } | |
400 | /* ignore unknown fields */ | |
401 | } | |
402 | if (precise && desc && !strstr(desc, "(Precise Event)")) { | |
403 | if (json_streq(map, precise, "2")) | |
794ba54a SB |
404 | addfield(map, &extra_desc, " ", |
405 | "(Must be precise)", NULL); | |
80eeb67f | 406 | else |
794ba54a | 407 | addfield(map, &extra_desc, " ", |
80eeb67f AK |
408 | "(Precise event)", NULL); |
409 | } | |
794ba54a SB |
410 | if (desc && extra_desc) |
411 | addfield(map, &desc, " ", extra_desc, NULL); | |
412 | if (long_desc && extra_desc) | |
413 | addfield(map, &long_desc, " ", extra_desc, NULL); | |
80eeb67f AK |
414 | if (msr != NULL) |
415 | addfield(map, &event, ",", msr->pname, msrval); | |
416 | fixname(name); | |
794ba54a | 417 | |
0b1db474 | 418 | err = func(data, name, real_event(name, event), desc, long_desc); |
80eeb67f AK |
419 | free(event); |
420 | free(desc); | |
421 | free(name); | |
794ba54a SB |
422 | free(long_desc); |
423 | free(extra_desc); | |
80eeb67f AK |
424 | if (err) |
425 | break; | |
426 | tok += j; | |
427 | } | |
428 | EXPECT(tok - tokens == len, tok, "unexpected objects at end"); | |
429 | err = 0; | |
430 | out_free: | |
431 | free_json(map, size, tokens); | |
432 | return err; | |
433 | } | |
434 | ||
435 | static char *file_name_to_table_name(char *fname) | |
436 | { | |
437 | unsigned int i; | |
438 | int n; | |
439 | int c; | |
440 | char *tblname; | |
441 | ||
442 | /* | |
443 | * Ensure tablename starts with alphabetic character. | |
444 | * Derive rest of table name from basename of the JSON file, | |
445 | * replacing hyphens and stripping out .json suffix. | |
446 | */ | |
447 | n = asprintf(&tblname, "pme_%s", basename(fname)); | |
448 | if (n < 0) { | |
449 | pr_info("%s: asprintf() error %s for file %s\n", prog, | |
450 | strerror(errno), fname); | |
451 | return NULL; | |
452 | } | |
453 | ||
454 | for (i = 0; i < strlen(tblname); i++) { | |
455 | c = tblname[i]; | |
456 | ||
457 | if (c == '-') | |
458 | tblname[i] = '_'; | |
459 | else if (c == '.') { | |
460 | tblname[i] = '\0'; | |
461 | break; | |
462 | } else if (!isalnum(c) && c != '_') { | |
463 | pr_err("%s: Invalid character '%c' in file name %s\n", | |
464 | prog, c, basename(fname)); | |
465 | free(tblname); | |
466 | tblname = NULL; | |
467 | break; | |
468 | } | |
469 | } | |
470 | ||
471 | return tblname; | |
472 | } | |
473 | ||
474 | static void print_mapping_table_prefix(FILE *outfp) | |
475 | { | |
476 | fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n"); | |
477 | } | |
478 | ||
479 | static void print_mapping_table_suffix(FILE *outfp) | |
480 | { | |
481 | /* | |
482 | * Print the terminating, NULL entry. | |
483 | */ | |
484 | fprintf(outfp, "{\n"); | |
485 | fprintf(outfp, "\t.cpuid = 0,\n"); | |
486 | fprintf(outfp, "\t.version = 0,\n"); | |
487 | fprintf(outfp, "\t.type = 0,\n"); | |
488 | fprintf(outfp, "\t.table = 0,\n"); | |
489 | fprintf(outfp, "},\n"); | |
490 | ||
491 | /* and finally, the closing curly bracket for the struct */ | |
492 | fprintf(outfp, "};\n"); | |
493 | } | |
494 | ||
495 | static int process_mapfile(FILE *outfp, char *fpath) | |
496 | { | |
497 | int n = 16384; | |
498 | FILE *mapfp; | |
499 | char *save = NULL; | |
500 | char *line, *p; | |
501 | int line_num; | |
502 | char *tblname; | |
503 | ||
504 | pr_info("%s: Processing mapfile %s\n", prog, fpath); | |
505 | ||
506 | line = malloc(n); | |
507 | if (!line) | |
508 | return -1; | |
509 | ||
510 | mapfp = fopen(fpath, "r"); | |
511 | if (!mapfp) { | |
512 | pr_info("%s: Error %s opening %s\n", prog, strerror(errno), | |
513 | fpath); | |
514 | return -1; | |
515 | } | |
516 | ||
517 | print_mapping_table_prefix(outfp); | |
518 | ||
dc720ffc AK |
519 | /* Skip first line (header) */ |
520 | p = fgets(line, n, mapfp); | |
521 | if (!p) | |
522 | goto out; | |
523 | ||
524 | line_num = 1; | |
80eeb67f AK |
525 | while (1) { |
526 | char *cpuid, *version, *type, *fname; | |
527 | ||
528 | line_num++; | |
529 | p = fgets(line, n, mapfp); | |
530 | if (!p) | |
531 | break; | |
532 | ||
533 | if (line[0] == '#' || line[0] == '\n') | |
534 | continue; | |
535 | ||
536 | if (line[strlen(line)-1] != '\n') { | |
537 | /* TODO Deal with lines longer than 16K */ | |
538 | pr_info("%s: Mapfile %s: line %d too long, aborting\n", | |
539 | prog, fpath, line_num); | |
540 | return -1; | |
541 | } | |
542 | line[strlen(line)-1] = '\0'; | |
543 | ||
544 | cpuid = strtok_r(p, ",", &save); | |
545 | version = strtok_r(NULL, ",", &save); | |
546 | fname = strtok_r(NULL, ",", &save); | |
547 | type = strtok_r(NULL, ",", &save); | |
548 | ||
549 | tblname = file_name_to_table_name(fname); | |
550 | fprintf(outfp, "{\n"); | |
551 | fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid); | |
552 | fprintf(outfp, "\t.version = \"%s\",\n", version); | |
553 | fprintf(outfp, "\t.type = \"%s\",\n", type); | |
554 | ||
555 | /* | |
556 | * CHECK: We can't use the type (eg "core") field in the | |
557 | * table name. For us to do that, we need to somehow tweak | |
558 | * the other caller of file_name_to_table(), process_json() | |
559 | * to determine the type. process_json() file has no way | |
560 | * of knowing these are "core" events unless file name has | |
561 | * core in it. If filename has core in it, we can safely | |
562 | * ignore the type field here also. | |
563 | */ | |
564 | fprintf(outfp, "\t.table = %s\n", tblname); | |
565 | fprintf(outfp, "},\n"); | |
566 | } | |
567 | ||
dc720ffc | 568 | out: |
80eeb67f | 569 | print_mapping_table_suffix(outfp); |
80eeb67f AK |
570 | return 0; |
571 | } | |
572 | ||
573 | /* | |
574 | * If we fail to locate/process JSON and map files, create a NULL mapping | |
575 | * table. This would at least allow perf to build even if we can't find/use | |
576 | * the aliases. | |
577 | */ | |
578 | static void create_empty_mapping(const char *output_file) | |
579 | { | |
580 | FILE *outfp; | |
581 | ||
582 | pr_info("%s: Creating empty pmu_events_map[] table\n", prog); | |
583 | ||
584 | /* Truncate file to clear any partial writes to it */ | |
585 | outfp = fopen(output_file, "w"); | |
586 | if (!outfp) { | |
587 | perror("fopen()"); | |
588 | _Exit(1); | |
589 | } | |
590 | ||
591 | fprintf(outfp, "#include \"../../pmu-events/pmu-events.h\"\n"); | |
592 | print_mapping_table_prefix(outfp); | |
593 | print_mapping_table_suffix(outfp); | |
594 | fclose(outfp); | |
595 | } | |
596 | ||
597 | static int get_maxfds(void) | |
598 | { | |
599 | struct rlimit rlim; | |
600 | ||
601 | if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) | |
602 | return min((int)rlim.rlim_max / 2, 512); | |
603 | ||
604 | return 512; | |
605 | } | |
606 | ||
607 | /* | |
608 | * nftw() doesn't let us pass an argument to the processing function, | |
609 | * so use a global variables. | |
610 | */ | |
611 | static FILE *eventsfp; | |
612 | static char *mapfile; | |
613 | ||
614 | static int process_one_file(const char *fpath, const struct stat *sb, | |
615 | int typeflag, struct FTW *ftwbuf) | |
616 | { | |
617 | char *tblname, *bname = (char *) fpath + ftwbuf->base; | |
618 | int is_dir = typeflag == FTW_D; | |
619 | int is_file = typeflag == FTW_F; | |
620 | int level = ftwbuf->level; | |
621 | int err = 0; | |
622 | ||
623 | pr_debug("%s %d %7jd %-20s %s\n", | |
624 | is_file ? "f" : is_dir ? "d" : "x", | |
625 | level, sb->st_size, bname, fpath); | |
626 | ||
627 | /* base dir */ | |
628 | if (level == 0) | |
629 | return 0; | |
630 | ||
631 | /* model directory, reset topic */ | |
632 | if (level == 1 && is_dir) { | |
633 | if (close_table) | |
634 | print_events_table_suffix(eventsfp); | |
635 | ||
636 | /* | |
637 | * Drop file name suffix. Replace hyphens with underscores. | |
638 | * Fail if file name contains any alphanum characters besides | |
639 | * underscores. | |
640 | */ | |
641 | tblname = file_name_to_table_name(bname); | |
642 | if (!tblname) { | |
643 | pr_info("%s: Error determining table name for %s\n", prog, | |
644 | bname); | |
645 | return -1; | |
646 | } | |
647 | ||
648 | print_events_table_prefix(eventsfp, tblname); | |
649 | return 0; | |
650 | } | |
651 | ||
652 | /* | |
653 | * Save the mapfile name for now. We will process mapfile | |
654 | * after processing all JSON files (so we can write out the | |
655 | * mapping table after all PMU events tables). | |
656 | * | |
657 | * TODO: Allow for multiple mapfiles? Punt for now. | |
658 | */ | |
659 | if (level == 1 && is_file) { | |
660 | if (!strncmp(bname, "mapfile.csv", 11)) { | |
661 | if (mapfile) { | |
662 | pr_info("%s: Many mapfiles? Using %s, ignoring %s\n", | |
663 | prog, mapfile, fpath); | |
664 | } else { | |
665 | mapfile = strdup(fpath); | |
666 | } | |
667 | return 0; | |
668 | } | |
669 | ||
670 | pr_info("%s: Ignoring file %s\n", prog, fpath); | |
671 | return 0; | |
672 | } | |
673 | ||
674 | /* | |
675 | * If the file name does not have a .json extension, | |
676 | * ignore it. It could be a readme.txt for instance. | |
677 | */ | |
678 | if (is_file) { | |
679 | char *suffix = bname + strlen(bname) - 5; | |
680 | ||
681 | if (strncmp(suffix, ".json", 5)) { | |
682 | pr_info("%s: Ignoring file without .json suffix %s\n", prog, | |
683 | fpath); | |
684 | return 0; | |
685 | } | |
686 | } | |
687 | ||
688 | if (level > 1 && add_topic(level, bname)) | |
689 | return -ENOMEM; | |
690 | ||
691 | /* | |
692 | * Assume all other files are JSON files. | |
693 | * | |
694 | * If mapfile refers to 'power7_core.json', we create a table | |
695 | * named 'power7_core'. Any inconsistencies between the mapfile | |
696 | * and directory tree could result in build failure due to table | |
697 | * names not being found. | |
698 | * | |
699 | * Atleast for now, be strict with processing JSON file names. | |
700 | * i.e. if JSON file name cannot be mapped to C-style table name, | |
701 | * fail. | |
702 | */ | |
703 | if (is_file) { | |
704 | struct perf_entry_data data = { | |
705 | .topic = get_topic(), | |
706 | .outfp = eventsfp, | |
707 | }; | |
708 | ||
709 | err = json_events(fpath, print_events_table_entry, &data); | |
710 | ||
711 | free(data.topic); | |
712 | } | |
713 | ||
714 | return err; | |
715 | } | |
716 | ||
717 | #ifndef PATH_MAX | |
718 | #define PATH_MAX 4096 | |
719 | #endif | |
720 | ||
721 | /* | |
722 | * Starting in directory 'start_dirname', find the "mapfile.csv" and | |
723 | * the set of JSON files for the architecture 'arch'. | |
724 | * | |
725 | * From each JSON file, create a C-style "PMU events table" from the | |
726 | * JSON file (see struct pmu_event). | |
727 | * | |
728 | * From the mapfile, create a mapping between the CPU revisions and | |
729 | * PMU event tables (see struct pmu_events_map). | |
730 | * | |
731 | * Write out the PMU events tables and the mapping table to pmu-event.c. | |
732 | * | |
733 | * If unable to process the JSON or arch files, create an empty mapping | |
734 | * table so we can continue to build/use perf even if we cannot use the | |
735 | * PMU event aliases. | |
736 | */ | |
737 | int main(int argc, char *argv[]) | |
738 | { | |
739 | int rc; | |
740 | int maxfds; | |
741 | char ldirname[PATH_MAX]; | |
742 | ||
743 | const char *arch; | |
744 | const char *output_file; | |
745 | const char *start_dirname; | |
746 | ||
747 | prog = basename(argv[0]); | |
748 | if (argc < 4) { | |
749 | pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog); | |
750 | return 1; | |
751 | } | |
752 | ||
753 | arch = argv[1]; | |
754 | start_dirname = argv[2]; | |
755 | output_file = argv[3]; | |
756 | ||
757 | if (argc > 4) | |
758 | verbose = atoi(argv[4]); | |
759 | ||
760 | eventsfp = fopen(output_file, "w"); | |
761 | if (!eventsfp) { | |
762 | pr_err("%s Unable to create required file %s (%s)\n", | |
763 | prog, output_file, strerror(errno)); | |
764 | return 2; | |
765 | } | |
766 | ||
767 | /* Include pmu-events.h first */ | |
768 | fprintf(eventsfp, "#include \"../../pmu-events/pmu-events.h\"\n"); | |
769 | ||
770 | sprintf(ldirname, "%s/%s", start_dirname, arch); | |
771 | ||
772 | /* | |
773 | * The mapfile allows multiple CPUids to point to the same JSON file, | |
774 | * so, not sure if there is a need for symlinks within the pmu-events | |
775 | * directory. | |
776 | * | |
777 | * For now, treat symlinks of JSON files as regular files and create | |
778 | * separate tables for each symlink (presumably, each symlink refers | |
779 | * to specific version of the CPU). | |
780 | */ | |
781 | ||
782 | maxfds = get_maxfds(); | |
783 | mapfile = NULL; | |
784 | rc = nftw(ldirname, process_one_file, maxfds, 0); | |
785 | if (rc && verbose) { | |
786 | pr_info("%s: Error walking file tree %s\n", prog, ldirname); | |
787 | goto empty_map; | |
788 | } else if (rc) { | |
789 | goto empty_map; | |
790 | } | |
791 | ||
792 | if (close_table) | |
793 | print_events_table_suffix(eventsfp); | |
794 | ||
795 | if (!mapfile) { | |
796 | pr_info("%s: No CPU->JSON mapping?\n", prog); | |
797 | goto empty_map; | |
798 | } | |
799 | ||
800 | if (process_mapfile(eventsfp, mapfile)) { | |
801 | pr_info("%s: Error processing mapfile %s\n", prog, mapfile); | |
802 | goto empty_map; | |
803 | } | |
804 | ||
805 | return 0; | |
806 | ||
807 | empty_map: | |
808 | fclose(eventsfp); | |
809 | create_empty_mapping(output_file); | |
810 | return 0; | |
811 | } |