1 #define _XOPEN_SOURCE 500 /* needed for nftw() */
2 #define _GNU_SOURCE /* needed for asprintf() */
4 /* Parse event JSON files */
7 * Copyright (c) 2014, Intel Corporation
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.
44 #include <sys/time.h> /* getrlimit */
45 #include <sys/resource.h> /* getrlimit */
48 #include <linux/list.h>
56 int eprintf(int level, int var, const char *fmt, ...)
67 ret = vfprintf(stderr, fmt, args);
74 __attribute__((weak)) char *get_cpu_str(void)
79 static void addfield(char *map, char **dst, const char *sep,
80 const char *a, jsmntok_t *bt)
82 unsigned int len = strlen(a) + 1 + strlen(sep);
83 int olen = *dst ? strlen(*dst) : 0;
84 int blen = bt ? json_len(bt) : 0;
87 out = realloc(*dst, len + olen + blen);
89 /* Don't add field in this case */
100 strncat(*dst, map + bt->start, blen);
103 static void fixname(char *s)
109 static void fixdesc(char *s)
111 char *e = s + strlen(s);
113 /* Remove trailing dots that look ugly in perf list */
115 while (e >= s && isspace(*e))
121 /* Add escapes for '\' so they are proper C strings. */
122 static char *fixregex(char *s)
129 /* Count the number of '\' in string */
130 for (p = s; *p; p++) {
139 /* allocate space for a new string */
140 fixed = (char *) malloc(len + esc_count + 1);
144 /* copy over the characters */
146 for (p = s; *p; p++) {
158 static struct msrmap {
162 { "0x3F6", "ldlat=" },
163 { "0x1A6", "offcore_rsp=" },
164 { "0x1A7", "offcore_rsp=" },
165 { "0x3F7", "frontend=" },
169 static struct field {
173 { "UMask", "umask=" },
174 { "CounterMask", "cmask=" },
175 { "Invert", "inv=" },
176 { "AnyThread", "any=" },
177 { "EdgeDetect", "edge=" },
178 { "SampleAfterValue", "period=" },
179 { "FCMask", "fc_mask=" },
180 { "PortMask", "ch_mask=" },
184 static void cut_comma(char *map, jsmntok_t *newval)
188 /* Cut off everything after comma */
189 for (i = newval->start; i < newval->end; i++) {
195 static int match_field(char *map, jsmntok_t *field, int nz,
196 char **event, jsmntok_t *val)
199 jsmntok_t newval = *val;
201 for (f = fields; f->field; f++)
202 if (json_streq(map, field, f->field) && nz) {
203 cut_comma(map, &newval);
204 addfield(map, event, ",", f->kernel, &newval);
210 static struct msrmap *lookup_msr(char *map, jsmntok_t *val)
212 jsmntok_t newval = *val;
216 cut_comma(map, &newval);
217 for (i = 0; msrmap[i].num; i++)
218 if (json_streq(map, &newval, msrmap[i].num))
222 pr_err("%s: Unknown MSR in event file %.*s\n", prog,
223 json_len(val), map + val->start);
232 { "CBO", "uncore_cbox" },
233 { "QPI LL", "uncore_qpi" },
234 { "SBO", "uncore_sbox" },
235 { "iMPH-U", "uncore_arb" },
236 { "CPU-M-CF", "cpum_cf" },
237 { "CPU-M-SF", "cpum_sf" },
238 { "UPI LL", "uncore_upi" },
239 { "hisi_sccl,ddrc", "hisi_sccl,ddrc" },
240 { "hisi_sccl,hha", "hisi_sccl,hha" },
241 { "hisi_sccl,l3c", "hisi_sccl,l3c" },
242 { "L3PMC", "amd_l3" },
246 static const char *field_to_perf(struct map *table, char *map, jsmntok_t *val)
250 for (i = 0; table[i].json; i++) {
251 if (json_streq(map, val, table[i].json))
252 return table[i].perf;
257 #define EXPECT(e, t, m) do { if (!(e)) { \
258 jsmntok_t *loc = (t); \
259 if (!(t)->start && (t) > tokens) \
261 pr_err("%s:%d: " m ", got %s\n", fn, \
262 json_line(map, loc), \
270 static char *get_topic(void)
275 /* tp is free'd in process_one_file() */
276 i = asprintf(&tp, "%s", topic);
278 pr_info("%s: asprintf() error %s\n", prog);
282 for (i = 0; i < (int) strlen(tp); i++) {
296 static int add_topic(char *bname)
299 topic = strdup(bname);
301 pr_info("%s: strdup() error %s for file %s\n", prog,
302 strerror(errno), bname);
308 struct perf_entry_data {
313 static int close_table;
315 static void print_events_table_prefix(FILE *fp, const char *tblname)
317 fprintf(fp, "struct pmu_event %s[] = {\n", tblname);
321 static int print_events_table_entry(void *data, char *name, char *event,
322 char *desc, char *long_desc,
323 char *pmu, char *unit, char *perpkg,
325 char *metric_name, char *metric_group,
326 char *deprecated, char *metric_constraint)
328 struct perf_entry_data *pd = data;
329 FILE *outfp = pd->outfp;
330 char *topic = pd->topic;
333 * TODO: Remove formatting chars after debugging to reduce
336 fprintf(outfp, "{\n");
339 fprintf(outfp, "\t.name = \"%s\",\n", name);
341 fprintf(outfp, "\t.event = \"%s\",\n", event);
342 fprintf(outfp, "\t.desc = \"%s\",\n", desc);
343 fprintf(outfp, "\t.topic = \"%s\",\n", topic);
344 if (long_desc && long_desc[0])
345 fprintf(outfp, "\t.long_desc = \"%s\",\n", long_desc);
347 fprintf(outfp, "\t.pmu = \"%s\",\n", pmu);
349 fprintf(outfp, "\t.unit = \"%s\",\n", unit);
351 fprintf(outfp, "\t.perpkg = \"%s\",\n", perpkg);
353 fprintf(outfp, "\t.metric_expr = \"%s\",\n", metric_expr);
355 fprintf(outfp, "\t.metric_name = \"%s\",\n", metric_name);
357 fprintf(outfp, "\t.metric_group = \"%s\",\n", metric_group);
359 fprintf(outfp, "\t.deprecated = \"%s\",\n", deprecated);
360 if (metric_constraint)
361 fprintf(outfp, "\t.metric_constraint = \"%s\",\n", metric_constraint);
362 fprintf(outfp, "},\n");
367 struct event_struct {
368 struct list_head list;
380 char *metric_constraint;
383 #define ADD_EVENT_FIELD(field) do { if (field) { \
384 es->field = strdup(field); \
389 #define FREE_EVENT_FIELD(field) free(es->field)
391 #define TRY_FIXUP_FIELD(field) do { if (es->field && !*field) {\
392 *field = strdup(es->field); \
397 #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
411 static LIST_HEAD(arch_std_events);
413 static void free_arch_std_events(void)
415 struct event_struct *es, *next;
417 list_for_each_entry_safe(es, next, &arch_std_events, list) {
418 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
419 list_del_init(&es->list);
424 static int save_arch_std_events(void *data, char *name, char *event,
425 char *desc, char *long_desc, char *pmu,
426 char *unit, char *perpkg, char *metric_expr,
427 char *metric_name, char *metric_group,
428 char *deprecated, char *metric_constraint)
430 struct event_struct *es;
432 es = malloc(sizeof(*es));
435 memset(es, 0, sizeof(*es));
436 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD);
437 list_add_tail(&es->list, &arch_std_events);
440 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
445 static void print_events_table_suffix(FILE *outfp)
447 fprintf(outfp, "{\n");
449 fprintf(outfp, "\t.name = 0,\n");
450 fprintf(outfp, "\t.event = 0,\n");
451 fprintf(outfp, "\t.desc = 0,\n");
453 fprintf(outfp, "},\n");
454 fprintf(outfp, "};\n");
458 static struct fixed {
462 { "inst_retired.any", "event=0xc0,period=2000003" },
463 { "inst_retired.any_p", "event=0xc0,period=2000003" },
464 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03,period=2000003" },
465 { "cpu_clk_unhalted.thread", "event=0x3c,period=2000003" },
466 { "cpu_clk_unhalted.core", "event=0x3c,period=2000003" },
467 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1,period=2000003" },
472 * Handle different fixed counter encodings between JSON and perf.
474 static char *real_event(const char *name, char *event)
481 for (i = 0; fixed[i].name; i++)
482 if (!strcasecmp(name, fixed[i].name))
483 return (char *)fixed[i].event;
488 try_fixup(const char *fn, char *arch_std, char **event, char **desc,
489 char **name, char **long_desc, char **pmu, char **filter,
490 char **perpkg, char **unit, char **metric_expr, char **metric_name,
491 char **metric_group, unsigned long long eventcode,
492 char **deprecated, char **metric_constraint)
494 /* try to find matching event from arch standard values */
495 struct event_struct *es;
497 list_for_each_entry(es, &arch_std_events, list) {
498 if (!strcmp(arch_std, es->name)) {
499 if (!eventcode && es->event) {
500 /* allow EventCode to be overridden */
504 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
509 pr_err("%s: could not find matching %s for %s\n",
514 /* Call func with each event in the json file */
515 int json_events(const char *fn,
516 int (*func)(void *data, char *name, char *event, char *desc,
518 char *pmu, char *unit, char *perpkg,
520 char *metric_name, char *metric_group,
521 char *deprecated, char *metric_constraint),
526 jsmntok_t *tokens, *tok;
534 tokens = parse_json(fn, &map, &size, &len);
537 EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
539 for (i = 0; i < tokens->size; i++) {
540 char *event = NULL, *desc = NULL, *name = NULL;
541 char *long_desc = NULL;
542 char *extra_desc = NULL;
547 char *metric_expr = NULL;
548 char *metric_name = NULL;
549 char *metric_group = NULL;
550 char *deprecated = NULL;
551 char *metric_constraint = NULL;
552 char *arch_std = NULL;
553 unsigned long long eventcode = 0;
554 struct msrmap *msr = NULL;
555 jsmntok_t *msrval = NULL;
556 jsmntok_t *precise = NULL;
557 jsmntok_t *obj = tok++;
559 EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
560 for (j = 0; j < obj->size; j += 2) {
561 jsmntok_t *field, *val;
566 EXPECT(field->type == JSMN_STRING, tok + j,
567 "Expected field name");
569 EXPECT(val->type == JSMN_STRING, tok + j + 1,
570 "Expected string value");
572 nz = !json_streq(map, val, "0");
573 if (match_field(map, field, nz, &event, val)) {
575 } else if (json_streq(map, field, "EventCode")) {
577 addfield(map, &code, "", "", val);
578 eventcode |= strtoul(code, NULL, 0);
580 } else if (json_streq(map, field, "ExtSel")) {
582 addfield(map, &code, "", "", val);
583 eventcode |= strtoul(code, NULL, 0) << 21;
585 } else if (json_streq(map, field, "EventName")) {
586 addfield(map, &name, "", "", val);
587 } else if (json_streq(map, field, "BriefDescription")) {
588 addfield(map, &desc, "", "", val);
590 } else if (json_streq(map, field,
591 "PublicDescription")) {
592 addfield(map, &long_desc, "", "", val);
594 } else if (json_streq(map, field, "PEBS") && nz) {
596 } else if (json_streq(map, field, "MSRIndex") && nz) {
597 msr = lookup_msr(map, val);
598 } else if (json_streq(map, field, "MSRValue")) {
600 } else if (json_streq(map, field, "Errata") &&
601 !json_streq(map, val, "null")) {
602 addfield(map, &extra_desc, ". ",
603 " Spec update: ", val);
604 } else if (json_streq(map, field, "Data_LA") && nz) {
605 addfield(map, &extra_desc, ". ",
606 " Supports address when precise",
608 } else if (json_streq(map, field, "Unit")) {
611 ppmu = field_to_perf(unit_to_pmu, map, val);
616 pmu = strdup("uncore_");
617 addfield(map, &pmu, "", "", val);
618 for (s = pmu; *s; s++)
621 addfield(map, &desc, ". ", "Unit: ", NULL);
622 addfield(map, &desc, "", pmu, NULL);
623 addfield(map, &desc, "", " ", NULL);
624 } else if (json_streq(map, field, "Filter")) {
625 addfield(map, &filter, "", "", val);
626 } else if (json_streq(map, field, "ScaleUnit")) {
627 addfield(map, &unit, "", "", val);
628 } else if (json_streq(map, field, "PerPkg")) {
629 addfield(map, &perpkg, "", "", val);
630 } else if (json_streq(map, field, "Deprecated")) {
631 addfield(map, &deprecated, "", "", val);
632 } else if (json_streq(map, field, "MetricName")) {
633 addfield(map, &metric_name, "", "", val);
634 } else if (json_streq(map, field, "MetricGroup")) {
635 addfield(map, &metric_group, "", "", val);
636 } else if (json_streq(map, field, "MetricConstraint")) {
637 addfield(map, &metric_constraint, "", "", val);
638 } else if (json_streq(map, field, "MetricExpr")) {
639 addfield(map, &metric_expr, "", "", val);
640 for (s = metric_expr; *s; s++)
642 } else if (json_streq(map, field, "ArchStdEvent")) {
643 addfield(map, &arch_std, "", "", val);
644 for (s = arch_std; *s; s++)
647 /* ignore unknown fields */
649 if (precise && desc && !strstr(desc, "(Precise Event)")) {
650 if (json_streq(map, precise, "2"))
651 addfield(map, &extra_desc, " ",
652 "(Must be precise)", NULL);
654 addfield(map, &extra_desc, " ",
655 "(Precise event)", NULL);
657 snprintf(buf, sizeof buf, "event=%#llx", eventcode);
658 addfield(map, &event, ",", buf, NULL);
659 if (desc && extra_desc)
660 addfield(map, &desc, " ", extra_desc, NULL);
661 if (long_desc && extra_desc)
662 addfield(map, &long_desc, " ", extra_desc, NULL);
664 addfield(map, &event, ",", filter, NULL);
666 addfield(map, &event, ",", msr->pname, msrval);
672 * An arch standard event is referenced, so try to
673 * fixup any unassigned values.
675 err = try_fixup(fn, arch_std, &event, &desc, &name,
676 &long_desc, &pmu, &filter, &perpkg,
677 &unit, &metric_expr, &metric_name,
678 &metric_group, eventcode,
679 &deprecated, &metric_constraint);
683 err = func(data, name, real_event(name, event), desc, long_desc,
684 pmu, unit, perpkg, metric_expr, metric_name,
685 metric_group, deprecated, metric_constraint);
700 free(metric_constraint);
707 EXPECT(tok - tokens == len, tok, "unexpected objects at end");
710 free_json(map, size, tokens);
714 static char *file_name_to_table_name(char *fname)
722 * Ensure tablename starts with alphabetic character.
723 * Derive rest of table name from basename of the JSON file,
724 * replacing hyphens and stripping out .json suffix.
726 n = asprintf(&tblname, "pme_%s", fname);
728 pr_info("%s: asprintf() error %s for file %s\n", prog,
729 strerror(errno), fname);
733 for (i = 0; i < strlen(tblname); i++) {
736 if (c == '-' || c == '/')
741 } else if (!isalnum(c) && c != '_') {
742 pr_err("%s: Invalid character '%c' in file name %s\n",
743 prog, c, basename(fname));
753 static void print_mapping_table_prefix(FILE *outfp)
755 fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n");
758 static void print_mapping_table_suffix(FILE *outfp)
761 * Print the terminating, NULL entry.
763 fprintf(outfp, "{\n");
764 fprintf(outfp, "\t.cpuid = 0,\n");
765 fprintf(outfp, "\t.version = 0,\n");
766 fprintf(outfp, "\t.type = 0,\n");
767 fprintf(outfp, "\t.table = 0,\n");
768 fprintf(outfp, "},\n");
770 /* and finally, the closing curly bracket for the struct */
771 fprintf(outfp, "};\n");
774 static void print_mapping_test_table(FILE *outfp)
777 * Print the terminating, NULL entry.
779 fprintf(outfp, "{\n");
780 fprintf(outfp, "\t.cpuid = \"testcpu\",\n");
781 fprintf(outfp, "\t.version = \"v1\",\n");
782 fprintf(outfp, "\t.type = \"core\",\n");
783 fprintf(outfp, "\t.table = pme_test_cpu,\n");
784 fprintf(outfp, "},\n");
787 static int process_mapfile(FILE *outfp, char *fpath)
797 pr_info("%s: Processing mapfile %s\n", prog, fpath);
803 mapfp = fopen(fpath, "r");
805 pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
811 print_mapping_table_prefix(outfp);
813 /* Skip first line (header) */
814 p = fgets(line, n, mapfp);
820 char *cpuid, *version, *type, *fname;
823 p = fgets(line, n, mapfp);
827 if (line[0] == '#' || line[0] == '\n')
830 if (line[strlen(line)-1] != '\n') {
831 /* TODO Deal with lines longer than 16K */
832 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
833 prog, fpath, line_num);
837 line[strlen(line)-1] = '\0';
839 cpuid = fixregex(strtok_r(p, ",", &save));
840 version = strtok_r(NULL, ",", &save);
841 fname = strtok_r(NULL, ",", &save);
842 type = strtok_r(NULL, ",", &save);
844 tblname = file_name_to_table_name(fname);
845 fprintf(outfp, "{\n");
846 fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
847 fprintf(outfp, "\t.version = \"%s\",\n", version);
848 fprintf(outfp, "\t.type = \"%s\",\n", type);
851 * CHECK: We can't use the type (eg "core") field in the
852 * table name. For us to do that, we need to somehow tweak
853 * the other caller of file_name_to_table(), process_json()
854 * to determine the type. process_json() file has no way
855 * of knowing these are "core" events unless file name has
856 * core in it. If filename has core in it, we can safely
857 * ignore the type field here also.
859 fprintf(outfp, "\t.table = %s\n", tblname);
860 fprintf(outfp, "},\n");
864 print_mapping_test_table(outfp);
865 print_mapping_table_suffix(outfp);
872 * If we fail to locate/process JSON and map files, create a NULL mapping
873 * table. This would at least allow perf to build even if we can't find/use
876 static void create_empty_mapping(const char *output_file)
880 pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
882 /* Truncate file to clear any partial writes to it */
883 outfp = fopen(output_file, "w");
889 fprintf(outfp, "#include \"pmu-events/pmu-events.h\"\n");
890 print_mapping_table_prefix(outfp);
891 print_mapping_table_suffix(outfp);
895 static int get_maxfds(void)
899 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
900 return min((int)rlim.rlim_max / 2, 512);
906 * nftw() doesn't let us pass an argument to the processing function,
907 * so use a global variables.
909 static FILE *eventsfp;
910 static char *mapfile;
912 static int is_leaf_dir(const char *fpath)
922 while ((dir = readdir(d)) != NULL) {
923 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
926 if (dir->d_type == DT_DIR) {
929 } else if (dir->d_type == DT_UNKNOWN) {
933 sprintf(path, "%s/%s", fpath, dir->d_name);
937 if (S_ISDIR(st.st_mode)) {
949 static int is_json_file(const char *name)
953 if (strlen(name) < 5)
956 suffix = name + strlen(name) - 5;
958 if (strncmp(suffix, ".json", 5) == 0)
963 static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
964 int typeflag, struct FTW *ftwbuf)
966 int level = ftwbuf->level;
967 int is_file = typeflag == FTW_F;
969 if (level == 1 && is_file && is_json_file(fpath))
970 return json_events(fpath, save_arch_std_events, (void *)sb);
975 static int process_one_file(const char *fpath, const struct stat *sb,
976 int typeflag, struct FTW *ftwbuf)
978 char *tblname, *bname;
979 int is_dir = typeflag == FTW_D;
980 int is_file = typeflag == FTW_F;
981 int level = ftwbuf->level;
984 if (level == 2 && is_dir) {
986 * For level 2 directory, bname will include parent name,
987 * like vendor/platform. So search back from platform dir
990 bname = (char *) fpath + ftwbuf->base - 2;
998 bname = (char *) fpath + ftwbuf->base;
1000 pr_debug("%s %d %7jd %-20s %s\n",
1001 is_file ? "f" : is_dir ? "d" : "x",
1002 level, sb->st_size, bname, fpath);
1004 /* base dir or too deep */
1005 if (level == 0 || level > 3)
1009 /* model directory, reset topic */
1010 if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
1011 (level == 2 && is_dir)) {
1013 print_events_table_suffix(eventsfp);
1016 * Drop file name suffix. Replace hyphens with underscores.
1017 * Fail if file name contains any alphanum characters besides
1020 tblname = file_name_to_table_name(bname);
1022 pr_info("%s: Error determining table name for %s\n", prog,
1027 print_events_table_prefix(eventsfp, tblname);
1032 * Save the mapfile name for now. We will process mapfile
1033 * after processing all JSON files (so we can write out the
1034 * mapping table after all PMU events tables).
1037 if (level == 1 && is_file) {
1038 if (!strcmp(bname, "mapfile.csv")) {
1039 mapfile = strdup(fpath);
1043 pr_info("%s: Ignoring file %s\n", prog, fpath);
1048 * If the file name does not have a .json extension,
1049 * ignore it. It could be a readme.txt for instance.
1052 if (!is_json_file(bname)) {
1053 pr_info("%s: Ignoring file without .json suffix %s\n", prog,
1059 if (level > 1 && add_topic(bname))
1063 * Assume all other files are JSON files.
1065 * If mapfile refers to 'power7_core.json', we create a table
1066 * named 'power7_core'. Any inconsistencies between the mapfile
1067 * and directory tree could result in build failure due to table
1068 * names not being found.
1070 * Atleast for now, be strict with processing JSON file names.
1071 * i.e. if JSON file name cannot be mapped to C-style table name,
1075 struct perf_entry_data data = {
1076 .topic = get_topic(),
1080 err = json_events(fpath, print_events_table_entry, &data);
1089 #define PATH_MAX 4096
1093 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1094 * the set of JSON files for the architecture 'arch'.
1096 * From each JSON file, create a C-style "PMU events table" from the
1097 * JSON file (see struct pmu_event).
1099 * From the mapfile, create a mapping between the CPU revisions and
1100 * PMU event tables (see struct pmu_events_map).
1102 * Write out the PMU events tables and the mapping table to pmu-event.c.
1104 int main(int argc, char *argv[])
1108 char ldirname[PATH_MAX];
1110 const char *output_file;
1111 const char *start_dirname;
1114 prog = basename(argv[0]);
1116 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
1121 start_dirname = argv[2];
1122 output_file = argv[3];
1125 verbose = atoi(argv[4]);
1127 eventsfp = fopen(output_file, "w");
1129 pr_err("%s Unable to create required file %s (%s)\n",
1130 prog, output_file, strerror(errno));
1134 sprintf(ldirname, "%s/%s", start_dirname, arch);
1136 /* If architecture does not have any event lists, bail out */
1137 if (stat(ldirname, &stbuf) < 0) {
1138 pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
1142 /* Include pmu-events.h first */
1143 fprintf(eventsfp, "#include \"pmu-events/pmu-events.h\"\n");
1146 * The mapfile allows multiple CPUids to point to the same JSON file,
1147 * so, not sure if there is a need for symlinks within the pmu-events
1150 * For now, treat symlinks of JSON files as regular files and create
1151 * separate tables for each symlink (presumably, each symlink refers
1152 * to specific version of the CPU).
1155 maxfds = get_maxfds();
1157 rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1158 if (rc && verbose) {
1159 pr_info("%s: Error preprocessing arch standard files %s\n",
1162 } else if (rc < 0) {
1163 /* Make build fail */
1165 free_arch_std_events();
1171 rc = nftw(ldirname, process_one_file, maxfds, 0);
1172 if (rc && verbose) {
1173 pr_info("%s: Error walking file tree %s\n", prog, ldirname);
1175 } else if (rc < 0) {
1176 /* Make build fail */
1178 free_arch_std_events();
1180 goto out_free_mapfile;
1185 sprintf(ldirname, "%s/test", start_dirname);
1187 rc = nftw(ldirname, process_one_file, maxfds, 0);
1188 if (rc && verbose) {
1189 pr_info("%s: Error walking file tree %s rc=%d for test\n",
1190 prog, ldirname, rc);
1192 } else if (rc < 0) {
1193 /* Make build fail */
1194 free_arch_std_events();
1196 goto out_free_mapfile;
1202 print_events_table_suffix(eventsfp);
1205 pr_info("%s: No CPU->JSON mapping?\n", prog);
1209 if (process_mapfile(eventsfp, mapfile)) {
1210 pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
1211 /* Make build fail */
1213 free_arch_std_events();
1218 goto out_free_mapfile;
1222 create_empty_mapping(output_file);
1223 free_arch_std_events();