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