Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / tools / perf / pmu-events / jevents.c
blobb578aa26e375dbdd0aeb12b3fea3f2b9c870dee2
1 #define _XOPEN_SOURCE 500 /* needed for nftw() */
2 #define _GNU_SOURCE /* needed for asprintf() */
4 /* Parse event JSON files */
6 /*
7 * Copyright (c) 2014, Intel Corporation
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
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.
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.
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"
51 int verbose;
52 char *prog;
54 int eprintf(int level, int var, const char *fmt, ...)
57 int ret;
58 va_list args;
60 if (var < level)
61 return 0;
63 va_start(args, fmt);
65 ret = vfprintf(stderr, fmt, args);
67 va_end(args);
69 return ret;
72 __attribute__((weak)) char *get_cpu_str(void)
74 return NULL;
77 static void addfield(char *map, char **dst, const char *sep,
78 const char *a, jsmntok_t *bt)
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;
85 out = realloc(*dst, len + olen + blen);
86 if (!out) {
87 /* Don't add field in this case */
88 return;
90 *dst = out;
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);
101 static void fixname(char *s)
103 for (; *s; s++)
104 *s = tolower(*s);
107 static void fixdesc(char *s)
109 char *e = s + strlen(s);
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;
119 /* Add escapes for '\' so they are proper C strings. */
120 static char *fixregex(char *s)
122 int len = 0;
123 int esc_count = 0;
124 char *fixed = NULL;
125 char *p, *q;
127 /* Count the number of '\' in string */
128 for (p = s; *p; p++) {
129 ++len;
130 if (*p == '\\')
131 ++esc_count;
134 if (esc_count == 0)
135 return s;
137 /* allocate space for a new string */
138 fixed = (char *) malloc(len + 1);
139 if (!fixed)
140 return NULL;
142 /* copy over the characters */
143 q = fixed;
144 for (p = s; *p; p++) {
145 if (*p == '\\') {
146 *q = '\\';
147 ++q;
149 *q = *p;
150 ++q;
152 *q = '\0';
153 return fixed;
156 static struct msrmap {
157 const char *num;
158 const char *pname;
159 } msrmap[] = {
160 { "0x3F6", "ldlat=" },
161 { "0x1A6", "offcore_rsp=" },
162 { "0x1A7", "offcore_rsp=" },
163 { "0x3F7", "frontend=" },
164 { NULL, NULL }
167 static struct field {
168 const char *field;
169 const char *kernel;
170 } fields[] = {
171 { "UMask", "umask=" },
172 { "CounterMask", "cmask=" },
173 { "Invert", "inv=" },
174 { "AnyThread", "any=" },
175 { "EdgeDetect", "edge=" },
176 { "SampleAfterValue", "period=" },
177 { "FCMask", "fc_mask=" },
178 { "PortMask", "ch_mask=" },
179 { NULL, NULL }
182 static void cut_comma(char *map, jsmntok_t *newval)
184 int i;
186 /* Cut off everything after comma */
187 for (i = newval->start; i < newval->end; i++) {
188 if (map[i] == ',')
189 newval->end = i;
193 static int match_field(char *map, jsmntok_t *field, int nz,
194 char **event, jsmntok_t *val)
196 struct field *f;
197 jsmntok_t newval = *val;
199 for (f = fields; f->field; f++)
200 if (json_streq(map, field, f->field) && nz) {
201 cut_comma(map, &newval);
202 addfield(map, event, ",", f->kernel, &newval);
203 return 1;
205 return 0;
208 static struct msrmap *lookup_msr(char *map, jsmntok_t *val)
210 jsmntok_t newval = *val;
211 static bool warned;
212 int i;
214 cut_comma(map, &newval);
215 for (i = 0; msrmap[i].num; i++)
216 if (json_streq(map, &newval, msrmap[i].num))
217 return &msrmap[i];
218 if (!warned) {
219 warned = true;
220 pr_err("%s: Unknown MSR in event file %.*s\n", prog,
221 json_len(val), map + val->start);
223 return NULL;
226 static struct map {
227 const char *json;
228 const char *perf;
229 } unit_to_pmu[] = {
230 { "CBO", "uncore_cbox" },
231 { "QPI LL", "uncore_qpi" },
232 { "SBO", "uncore_sbox" },
233 { "iMPH-U", "uncore_arb" },
237 static const char *field_to_perf(struct map *table, char *map, jsmntok_t *val)
239 int i;
241 for (i = 0; table[i].json; i++) {
242 if (json_streq(map, val, table[i].json))
243 return table[i].perf;
245 return NULL;
248 #define EXPECT(e, t, m) do { if (!(e)) { \
249 jsmntok_t *loc = (t); \
250 if (!(t)->start && (t) > tokens) \
251 loc = (t) - 1; \
252 pr_err("%s:%d: " m ", got %s\n", fn, \
253 json_line(map, loc), \
254 json_name(t)); \
255 goto out_free; \
256 } } while (0)
258 #define TOPIC_DEPTH 256
259 static char *topic_array[TOPIC_DEPTH];
260 static int topic_level;
262 static char *get_topic(void)
264 char *tp_old, *tp = NULL;
265 int i;
267 for (i = 0; i < topic_level + 1; i++) {
268 int n;
270 tp_old = tp;
271 n = asprintf(&tp, "%s%s", tp ?: "", topic_array[i]);
272 if (n < 0) {
273 pr_info("%s: asprintf() error %s\n", prog);
274 return NULL;
276 free(tp_old);
279 for (i = 0; i < (int) strlen(tp); i++) {
280 char c = tp[i];
282 if (c == '-')
283 tp[i] = ' ';
284 else if (c == '.') {
285 tp[i] = '\0';
286 break;
290 return tp;
293 static int add_topic(int level, char *bname)
295 char *topic;
297 level -= 2;
299 if (level >= TOPIC_DEPTH)
300 return -EINVAL;
302 topic = strdup(bname);
303 if (!topic) {
304 pr_info("%s: strdup() error %s for file %s\n", prog,
305 strerror(errno), bname);
306 return -ENOMEM;
309 free(topic_array[topic_level]);
310 topic_array[topic_level] = topic;
311 topic_level = level;
312 return 0;
315 struct perf_entry_data {
316 FILE *outfp;
317 char *topic;
320 static int close_table;
322 static void print_events_table_prefix(FILE *fp, const char *tblname)
324 fprintf(fp, "struct pmu_event %s[] = {\n", tblname);
325 close_table = 1;
328 static int print_events_table_entry(void *data, char *name, char *event,
329 char *desc, char *long_desc,
330 char *pmu, char *unit, char *perpkg,
331 char *metric_expr,
332 char *metric_name, char *metric_group)
334 struct perf_entry_data *pd = data;
335 FILE *outfp = pd->outfp;
336 char *topic = pd->topic;
339 * TODO: Remove formatting chars after debugging to reduce
340 * string lengths.
342 fprintf(outfp, "{\n");
344 if (name)
345 fprintf(outfp, "\t.name = \"%s\",\n", name);
346 if (event)
347 fprintf(outfp, "\t.event = \"%s\",\n", event);
348 fprintf(outfp, "\t.desc = \"%s\",\n", desc);
349 fprintf(outfp, "\t.topic = \"%s\",\n", topic);
350 if (long_desc && long_desc[0])
351 fprintf(outfp, "\t.long_desc = \"%s\",\n", long_desc);
352 if (pmu)
353 fprintf(outfp, "\t.pmu = \"%s\",\n", pmu);
354 if (unit)
355 fprintf(outfp, "\t.unit = \"%s\",\n", unit);
356 if (perpkg)
357 fprintf(outfp, "\t.perpkg = \"%s\",\n", perpkg);
358 if (metric_expr)
359 fprintf(outfp, "\t.metric_expr = \"%s\",\n", metric_expr);
360 if (metric_name)
361 fprintf(outfp, "\t.metric_name = \"%s\",\n", metric_name);
362 if (metric_group)
363 fprintf(outfp, "\t.metric_group = \"%s\",\n", metric_group);
364 fprintf(outfp, "},\n");
366 return 0;
369 static void print_events_table_suffix(FILE *outfp)
371 fprintf(outfp, "{\n");
373 fprintf(outfp, "\t.name = 0,\n");
374 fprintf(outfp, "\t.event = 0,\n");
375 fprintf(outfp, "\t.desc = 0,\n");
377 fprintf(outfp, "},\n");
378 fprintf(outfp, "};\n");
379 close_table = 0;
382 static struct fixed {
383 const char *name;
384 const char *event;
385 } fixed[] = {
386 { "inst_retired.any", "event=0xc0" },
387 { "inst_retired.any_p", "event=0xc0" },
388 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03" },
389 { "cpu_clk_unhalted.thread", "event=0x3c" },
390 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1" },
391 { NULL, NULL},
395 * Handle different fixed counter encodings between JSON and perf.
397 static char *real_event(const char *name, char *event)
399 int i;
401 if (!name)
402 return NULL;
404 for (i = 0; fixed[i].name; i++)
405 if (!strcasecmp(name, fixed[i].name))
406 return (char *)fixed[i].event;
407 return event;
410 /* Call func with each event in the json file */
411 int json_events(const char *fn,
412 int (*func)(void *data, char *name, char *event, char *desc,
413 char *long_desc,
414 char *pmu, char *unit, char *perpkg,
415 char *metric_expr,
416 char *metric_name, char *metric_group),
417 void *data)
419 int err = -EIO;
420 size_t size;
421 jsmntok_t *tokens, *tok;
422 int i, j, len;
423 char *map;
424 char buf[128];
426 if (!fn)
427 return -ENOENT;
429 tokens = parse_json(fn, &map, &size, &len);
430 if (!tokens)
431 return -EIO;
432 EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
433 tok = tokens + 1;
434 for (i = 0; i < tokens->size; i++) {
435 char *event = NULL, *desc = NULL, *name = NULL;
436 char *long_desc = NULL;
437 char *extra_desc = NULL;
438 char *pmu = NULL;
439 char *filter = NULL;
440 char *perpkg = NULL;
441 char *unit = NULL;
442 char *metric_expr = NULL;
443 char *metric_name = NULL;
444 char *metric_group = NULL;
445 unsigned long long eventcode = 0;
446 struct msrmap *msr = NULL;
447 jsmntok_t *msrval = NULL;
448 jsmntok_t *precise = NULL;
449 jsmntok_t *obj = tok++;
451 EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
452 for (j = 0; j < obj->size; j += 2) {
453 jsmntok_t *field, *val;
454 int nz;
455 char *s;
457 field = tok + j;
458 EXPECT(field->type == JSMN_STRING, tok + j,
459 "Expected field name");
460 val = tok + j + 1;
461 EXPECT(val->type == JSMN_STRING, tok + j + 1,
462 "Expected string value");
464 nz = !json_streq(map, val, "0");
465 if (match_field(map, field, nz, &event, val)) {
466 /* ok */
467 } else if (json_streq(map, field, "EventCode")) {
468 char *code = NULL;
469 addfield(map, &code, "", "", val);
470 eventcode |= strtoul(code, NULL, 0);
471 free(code);
472 } else if (json_streq(map, field, "ExtSel")) {
473 char *code = NULL;
474 addfield(map, &code, "", "", val);
475 eventcode |= strtoul(code, NULL, 0) << 21;
476 free(code);
477 } else if (json_streq(map, field, "EventName")) {
478 addfield(map, &name, "", "", val);
479 } else if (json_streq(map, field, "BriefDescription")) {
480 addfield(map, &desc, "", "", val);
481 fixdesc(desc);
482 } else if (json_streq(map, field,
483 "PublicDescription")) {
484 addfield(map, &long_desc, "", "", val);
485 fixdesc(long_desc);
486 } else if (json_streq(map, field, "PEBS") && nz) {
487 precise = val;
488 } else if (json_streq(map, field, "MSRIndex") && nz) {
489 msr = lookup_msr(map, val);
490 } else if (json_streq(map, field, "MSRValue")) {
491 msrval = val;
492 } else if (json_streq(map, field, "Errata") &&
493 !json_streq(map, val, "null")) {
494 addfield(map, &extra_desc, ". ",
495 " Spec update: ", val);
496 } else if (json_streq(map, field, "Data_LA") && nz) {
497 addfield(map, &extra_desc, ". ",
498 " Supports address when precise",
499 NULL);
500 } else if (json_streq(map, field, "Unit")) {
501 const char *ppmu;
503 ppmu = field_to_perf(unit_to_pmu, map, val);
504 if (ppmu) {
505 pmu = strdup(ppmu);
506 } else {
507 if (!pmu)
508 pmu = strdup("uncore_");
509 addfield(map, &pmu, "", "", val);
510 for (s = pmu; *s; s++)
511 *s = tolower(*s);
513 addfield(map, &desc, ". ", "Unit: ", NULL);
514 addfield(map, &desc, "", pmu, NULL);
515 addfield(map, &desc, "", " ", NULL);
516 } else if (json_streq(map, field, "Filter")) {
517 addfield(map, &filter, "", "", val);
518 } else if (json_streq(map, field, "ScaleUnit")) {
519 addfield(map, &unit, "", "", val);
520 } else if (json_streq(map, field, "PerPkg")) {
521 addfield(map, &perpkg, "", "", val);
522 } else if (json_streq(map, field, "MetricName")) {
523 addfield(map, &metric_name, "", "", val);
524 } else if (json_streq(map, field, "MetricGroup")) {
525 addfield(map, &metric_group, "", "", val);
526 } else if (json_streq(map, field, "MetricExpr")) {
527 addfield(map, &metric_expr, "", "", val);
528 for (s = metric_expr; *s; s++)
529 *s = tolower(*s);
531 /* ignore unknown fields */
533 if (precise && desc && !strstr(desc, "(Precise Event)")) {
534 if (json_streq(map, precise, "2"))
535 addfield(map, &extra_desc, " ",
536 "(Must be precise)", NULL);
537 else
538 addfield(map, &extra_desc, " ",
539 "(Precise event)", NULL);
541 snprintf(buf, sizeof buf, "event=%#llx", eventcode);
542 addfield(map, &event, ",", buf, NULL);
543 if (desc && extra_desc)
544 addfield(map, &desc, " ", extra_desc, NULL);
545 if (long_desc && extra_desc)
546 addfield(map, &long_desc, " ", extra_desc, NULL);
547 if (filter)
548 addfield(map, &event, ",", filter, NULL);
549 if (msr != NULL)
550 addfield(map, &event, ",", msr->pname, msrval);
551 if (name)
552 fixname(name);
554 err = func(data, name, real_event(name, event), desc, long_desc,
555 pmu, unit, perpkg, metric_expr, metric_name, metric_group);
556 free(event);
557 free(desc);
558 free(name);
559 free(long_desc);
560 free(extra_desc);
561 free(pmu);
562 free(filter);
563 free(perpkg);
564 free(unit);
565 free(metric_expr);
566 free(metric_name);
567 free(metric_group);
568 if (err)
569 break;
570 tok += j;
572 EXPECT(tok - tokens == len, tok, "unexpected objects at end");
573 err = 0;
574 out_free:
575 free_json(map, size, tokens);
576 return err;
579 static char *file_name_to_table_name(char *fname)
581 unsigned int i;
582 int n;
583 int c;
584 char *tblname;
587 * Ensure tablename starts with alphabetic character.
588 * Derive rest of table name from basename of the JSON file,
589 * replacing hyphens and stripping out .json suffix.
591 n = asprintf(&tblname, "pme_%s", basename(fname));
592 if (n < 0) {
593 pr_info("%s: asprintf() error %s for file %s\n", prog,
594 strerror(errno), fname);
595 return NULL;
598 for (i = 0; i < strlen(tblname); i++) {
599 c = tblname[i];
601 if (c == '-')
602 tblname[i] = '_';
603 else if (c == '.') {
604 tblname[i] = '\0';
605 break;
606 } else if (!isalnum(c) && c != '_') {
607 pr_err("%s: Invalid character '%c' in file name %s\n",
608 prog, c, basename(fname));
609 free(tblname);
610 tblname = NULL;
611 break;
615 return tblname;
618 static void print_mapping_table_prefix(FILE *outfp)
620 fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n");
623 static void print_mapping_table_suffix(FILE *outfp)
626 * Print the terminating, NULL entry.
628 fprintf(outfp, "{\n");
629 fprintf(outfp, "\t.cpuid = 0,\n");
630 fprintf(outfp, "\t.version = 0,\n");
631 fprintf(outfp, "\t.type = 0,\n");
632 fprintf(outfp, "\t.table = 0,\n");
633 fprintf(outfp, "},\n");
635 /* and finally, the closing curly bracket for the struct */
636 fprintf(outfp, "};\n");
639 static int process_mapfile(FILE *outfp, char *fpath)
641 int n = 16384;
642 FILE *mapfp;
643 char *save = NULL;
644 char *line, *p;
645 int line_num;
646 char *tblname;
648 pr_info("%s: Processing mapfile %s\n", prog, fpath);
650 line = malloc(n);
651 if (!line)
652 return -1;
654 mapfp = fopen(fpath, "r");
655 if (!mapfp) {
656 pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
657 fpath);
658 return -1;
661 print_mapping_table_prefix(outfp);
663 /* Skip first line (header) */
664 p = fgets(line, n, mapfp);
665 if (!p)
666 goto out;
668 line_num = 1;
669 while (1) {
670 char *cpuid, *version, *type, *fname;
672 line_num++;
673 p = fgets(line, n, mapfp);
674 if (!p)
675 break;
677 if (line[0] == '#' || line[0] == '\n')
678 continue;
680 if (line[strlen(line)-1] != '\n') {
681 /* TODO Deal with lines longer than 16K */
682 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
683 prog, fpath, line_num);
684 return -1;
686 line[strlen(line)-1] = '\0';
688 cpuid = fixregex(strtok_r(p, ",", &save));
689 version = strtok_r(NULL, ",", &save);
690 fname = strtok_r(NULL, ",", &save);
691 type = strtok_r(NULL, ",", &save);
693 tblname = file_name_to_table_name(fname);
694 fprintf(outfp, "{\n");
695 fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
696 fprintf(outfp, "\t.version = \"%s\",\n", version);
697 fprintf(outfp, "\t.type = \"%s\",\n", type);
700 * CHECK: We can't use the type (eg "core") field in the
701 * table name. For us to do that, we need to somehow tweak
702 * the other caller of file_name_to_table(), process_json()
703 * to determine the type. process_json() file has no way
704 * of knowing these are "core" events unless file name has
705 * core in it. If filename has core in it, we can safely
706 * ignore the type field here also.
708 fprintf(outfp, "\t.table = %s\n", tblname);
709 fprintf(outfp, "},\n");
712 out:
713 print_mapping_table_suffix(outfp);
714 return 0;
718 * If we fail to locate/process JSON and map files, create a NULL mapping
719 * table. This would at least allow perf to build even if we can't find/use
720 * the aliases.
722 static void create_empty_mapping(const char *output_file)
724 FILE *outfp;
726 pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
728 /* Truncate file to clear any partial writes to it */
729 outfp = fopen(output_file, "w");
730 if (!outfp) {
731 perror("fopen()");
732 _Exit(1);
735 fprintf(outfp, "#include \"../../pmu-events/pmu-events.h\"\n");
736 print_mapping_table_prefix(outfp);
737 print_mapping_table_suffix(outfp);
738 fclose(outfp);
741 static int get_maxfds(void)
743 struct rlimit rlim;
745 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
746 return min((int)rlim.rlim_max / 2, 512);
748 return 512;
752 * nftw() doesn't let us pass an argument to the processing function,
753 * so use a global variables.
755 static FILE *eventsfp;
756 static char *mapfile;
758 static int process_one_file(const char *fpath, const struct stat *sb,
759 int typeflag, struct FTW *ftwbuf)
761 char *tblname, *bname = (char *) fpath + ftwbuf->base;
762 int is_dir = typeflag == FTW_D;
763 int is_file = typeflag == FTW_F;
764 int level = ftwbuf->level;
765 int err = 0;
767 pr_debug("%s %d %7jd %-20s %s\n",
768 is_file ? "f" : is_dir ? "d" : "x",
769 level, sb->st_size, bname, fpath);
771 /* base dir */
772 if (level == 0)
773 return 0;
775 /* model directory, reset topic */
776 if (level == 1 && is_dir) {
777 if (close_table)
778 print_events_table_suffix(eventsfp);
781 * Drop file name suffix. Replace hyphens with underscores.
782 * Fail if file name contains any alphanum characters besides
783 * underscores.
785 tblname = file_name_to_table_name(bname);
786 if (!tblname) {
787 pr_info("%s: Error determining table name for %s\n", prog,
788 bname);
789 return -1;
792 print_events_table_prefix(eventsfp, tblname);
793 return 0;
797 * Save the mapfile name for now. We will process mapfile
798 * after processing all JSON files (so we can write out the
799 * mapping table after all PMU events tables).
801 * TODO: Allow for multiple mapfiles? Punt for now.
803 if (level == 1 && is_file) {
804 if (!strncmp(bname, "mapfile.csv", 11)) {
805 if (mapfile) {
806 pr_info("%s: Many mapfiles? Using %s, ignoring %s\n",
807 prog, mapfile, fpath);
808 } else {
809 mapfile = strdup(fpath);
811 return 0;
814 pr_info("%s: Ignoring file %s\n", prog, fpath);
815 return 0;
819 * If the file name does not have a .json extension,
820 * ignore it. It could be a readme.txt for instance.
822 if (is_file) {
823 char *suffix = bname + strlen(bname) - 5;
825 if (strncmp(suffix, ".json", 5)) {
826 pr_info("%s: Ignoring file without .json suffix %s\n", prog,
827 fpath);
828 return 0;
832 if (level > 1 && add_topic(level, bname))
833 return -ENOMEM;
836 * Assume all other files are JSON files.
838 * If mapfile refers to 'power7_core.json', we create a table
839 * named 'power7_core'. Any inconsistencies between the mapfile
840 * and directory tree could result in build failure due to table
841 * names not being found.
843 * Atleast for now, be strict with processing JSON file names.
844 * i.e. if JSON file name cannot be mapped to C-style table name,
845 * fail.
847 if (is_file) {
848 struct perf_entry_data data = {
849 .topic = get_topic(),
850 .outfp = eventsfp,
853 err = json_events(fpath, print_events_table_entry, &data);
855 free(data.topic);
858 return err;
861 #ifndef PATH_MAX
862 #define PATH_MAX 4096
863 #endif
866 * Starting in directory 'start_dirname', find the "mapfile.csv" and
867 * the set of JSON files for the architecture 'arch'.
869 * From each JSON file, create a C-style "PMU events table" from the
870 * JSON file (see struct pmu_event).
872 * From the mapfile, create a mapping between the CPU revisions and
873 * PMU event tables (see struct pmu_events_map).
875 * Write out the PMU events tables and the mapping table to pmu-event.c.
877 int main(int argc, char *argv[])
879 int rc;
880 int maxfds;
881 char ldirname[PATH_MAX];
883 const char *arch;
884 const char *output_file;
885 const char *start_dirname;
886 struct stat stbuf;
888 prog = basename(argv[0]);
889 if (argc < 4) {
890 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
891 return 1;
894 arch = argv[1];
895 start_dirname = argv[2];
896 output_file = argv[3];
898 if (argc > 4)
899 verbose = atoi(argv[4]);
901 eventsfp = fopen(output_file, "w");
902 if (!eventsfp) {
903 pr_err("%s Unable to create required file %s (%s)\n",
904 prog, output_file, strerror(errno));
905 return 2;
908 sprintf(ldirname, "%s/%s", start_dirname, arch);
910 /* If architecture does not have any event lists, bail out */
911 if (stat(ldirname, &stbuf) < 0) {
912 pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
913 goto empty_map;
916 /* Include pmu-events.h first */
917 fprintf(eventsfp, "#include \"../../pmu-events/pmu-events.h\"\n");
920 * The mapfile allows multiple CPUids to point to the same JSON file,
921 * so, not sure if there is a need for symlinks within the pmu-events
922 * directory.
924 * For now, treat symlinks of JSON files as regular files and create
925 * separate tables for each symlink (presumably, each symlink refers
926 * to specific version of the CPU).
929 maxfds = get_maxfds();
930 mapfile = NULL;
931 rc = nftw(ldirname, process_one_file, maxfds, 0);
932 if (rc && verbose) {
933 pr_info("%s: Error walking file tree %s\n", prog, ldirname);
934 goto empty_map;
935 } else if (rc < 0) {
936 /* Make build fail */
937 return 1;
938 } else if (rc) {
939 goto empty_map;
942 if (close_table)
943 print_events_table_suffix(eventsfp);
945 if (!mapfile) {
946 pr_info("%s: No CPU->JSON mapping?\n", prog);
947 goto empty_map;
950 if (process_mapfile(eventsfp, mapfile)) {
951 pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
952 /* Make build fail */
953 return 1;
956 return 0;
958 empty_map:
959 fclose(eventsfp);
960 create_empty_mapping(output_file);
961 return 0;