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>
51 #include "pmu-events.h"
70 char *metric_constraint;
73 enum aggr_mode_class convert(const char *aggr_mode)
75 if (!strcmp(aggr_mode, "PerCore"))
77 else if (!strcmp(aggr_mode, "PerChip"))
80 pr_err("%s: Wrong AggregationMode value '%s'\n", prog, aggr_mode);
84 typedef int (*func)(void *data, struct json_event *je);
86 static LIST_HEAD(sys_event_tables);
88 struct sys_event_table {
89 struct list_head list;
93 static void free_sys_event_tables(void)
95 struct sys_event_table *et, *next;
97 list_for_each_entry_safe(et, next, &sys_event_tables, list) {
103 int eprintf(int level, int var, const char *fmt, ...)
114 ret = vfprintf(stderr, fmt, args);
121 static void addfield(char *map, char **dst, const char *sep,
122 const char *a, jsmntok_t *bt)
124 unsigned int len = strlen(a) + 1 + strlen(sep);
125 int olen = *dst ? strlen(*dst) : 0;
126 int blen = bt ? json_len(bt) : 0;
129 out = realloc(*dst, len + olen + blen);
131 /* Don't add field in this case */
142 strncat(*dst, map + bt->start, blen);
145 static void fixname(char *s)
151 static void fixdesc(char *s)
153 char *e = s + strlen(s);
155 /* Remove trailing dots that look ugly in perf list */
157 while (e >= s && isspace(*e))
163 /* Add escapes for '\' so they are proper C strings. */
164 static char *fixregex(char *s)
171 /* Count the number of '\' in string */
172 for (p = s; *p; p++) {
181 /* allocate space for a new string */
182 fixed = (char *) malloc(len + esc_count + 1);
186 /* copy over the characters */
188 for (p = s; *p; p++) {
200 static struct msrmap {
204 { "0x3F6", "ldlat=" },
205 { "0x1A6", "offcore_rsp=" },
206 { "0x1A7", "offcore_rsp=" },
207 { "0x3F7", "frontend=" },
211 static struct field {
215 { "UMask", "umask=" },
216 { "CounterMask", "cmask=" },
217 { "Invert", "inv=" },
218 { "AnyThread", "any=" },
219 { "EdgeDetect", "edge=" },
220 { "SampleAfterValue", "period=" },
221 { "FCMask", "fc_mask=" },
222 { "PortMask", "ch_mask=" },
226 static void cut_comma(char *map, jsmntok_t *newval)
230 /* Cut off everything after comma */
231 for (i = newval->start; i < newval->end; i++) {
237 static int match_field(char *map, jsmntok_t *field, int nz,
238 char **event, jsmntok_t *val)
241 jsmntok_t newval = *val;
243 for (f = fields; f->field; f++)
244 if (json_streq(map, field, f->field) && nz) {
245 cut_comma(map, &newval);
246 addfield(map, event, ",", f->kernel, &newval);
252 static struct msrmap *lookup_msr(char *map, jsmntok_t *val)
254 jsmntok_t newval = *val;
258 cut_comma(map, &newval);
259 for (i = 0; msrmap[i].num; i++)
260 if (json_streq(map, &newval, msrmap[i].num))
264 pr_err("%s: Unknown MSR in event file %.*s\n", prog,
265 json_len(val), map + val->start);
274 { "CBO", "uncore_cbox" },
275 { "QPI LL", "uncore_qpi" },
276 { "SBO", "uncore_sbox" },
277 { "iMPH-U", "uncore_arb" },
278 { "CPU-M-CF", "cpum_cf" },
279 { "CPU-M-SF", "cpum_sf" },
280 { "UPI LL", "uncore_upi" },
281 { "hisi_sccl,ddrc", "hisi_sccl,ddrc" },
282 { "hisi_sccl,hha", "hisi_sccl,hha" },
283 { "hisi_sccl,l3c", "hisi_sccl,l3c" },
284 /* it's not realistic to keep adding these, we need something more scalable ... */
285 { "imx8_ddr", "imx8_ddr" },
286 { "L3PMC", "amd_l3" },
287 { "DFPMC", "amd_df" },
288 { "cpu_core", "cpu_core" },
289 { "cpu_atom", "cpu_atom" },
293 static const char *field_to_perf(struct map *table, char *map, jsmntok_t *val)
297 for (i = 0; table[i].json; i++) {
298 if (json_streq(map, val, table[i].json))
299 return table[i].perf;
304 #define EXPECT(e, t, m) do { if (!(e)) { \
305 jsmntok_t *loc = (t); \
306 if (!(t)->start && (t) > tokens) \
308 pr_err("%s:%d: " m ", got %s\n", fn, \
309 json_line(map, loc), \
317 static char *get_topic(void)
322 /* tp is free'd in process_one_file() */
323 i = asprintf(&tp, "%s", topic);
325 pr_info("%s: asprintf() error %s\n", prog);
329 for (i = 0; i < (int) strlen(tp); i++) {
343 static int add_topic(char *bname)
346 topic = strdup(bname);
348 pr_info("%s: strdup() error %s for file %s\n", prog,
349 strerror(errno), bname);
355 struct perf_entry_data {
360 static int close_table;
362 static void print_events_table_prefix(FILE *fp, const char *tblname)
364 fprintf(fp, "struct pmu_event %s[] = {\n", tblname);
368 static int print_events_table_entry(void *data, struct json_event *je)
370 struct perf_entry_data *pd = data;
371 FILE *outfp = pd->outfp;
372 char *topic = pd->topic;
375 * TODO: Remove formatting chars after debugging to reduce
378 fprintf(outfp, "{\n");
381 fprintf(outfp, "\t.name = \"%s\",\n", je->name);
383 fprintf(outfp, "\t.event = \"%s\",\n", je->event);
384 fprintf(outfp, "\t.desc = \"%s\",\n", je->desc);
386 fprintf(outfp, "\t.compat = \"%s\",\n", je->compat);
387 fprintf(outfp, "\t.topic = \"%s\",\n", topic);
388 if (je->long_desc && je->long_desc[0])
389 fprintf(outfp, "\t.long_desc = \"%s\",\n", je->long_desc);
391 fprintf(outfp, "\t.pmu = \"%s\",\n", je->pmu);
393 fprintf(outfp, "\t.unit = \"%s\",\n", je->unit);
395 fprintf(outfp, "\t.perpkg = \"%s\",\n", je->perpkg);
397 fprintf(outfp, "\t.aggr_mode = \"%d\",\n", convert(je->aggr_mode));
399 fprintf(outfp, "\t.metric_expr = \"%s\",\n", je->metric_expr);
401 fprintf(outfp, "\t.metric_name = \"%s\",\n", je->metric_name);
402 if (je->metric_group)
403 fprintf(outfp, "\t.metric_group = \"%s\",\n", je->metric_group);
405 fprintf(outfp, "\t.deprecated = \"%s\",\n", je->deprecated);
406 if (je->metric_constraint)
407 fprintf(outfp, "\t.metric_constraint = \"%s\",\n", je->metric_constraint);
408 fprintf(outfp, "},\n");
413 struct event_struct {
414 struct list_head list;
428 char *metric_constraint;
431 #define ADD_EVENT_FIELD(field) do { if (je->field) { \
432 es->field = strdup(je->field); \
437 #define FREE_EVENT_FIELD(field) free(es->field)
439 #define TRY_FIXUP_FIELD(field) do { if (es->field && !je->field) {\
440 je->field = strdup(es->field); \
445 #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
460 static LIST_HEAD(arch_std_events);
462 static void free_arch_std_events(void)
464 struct event_struct *es, *next;
466 list_for_each_entry_safe(es, next, &arch_std_events, list) {
467 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
468 list_del_init(&es->list);
473 static int save_arch_std_events(void *data, struct json_event *je)
475 struct event_struct *es;
477 es = malloc(sizeof(*es));
480 memset(es, 0, sizeof(*es));
481 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD);
482 list_add_tail(&es->list, &arch_std_events);
485 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
490 static void print_events_table_suffix(FILE *outfp)
492 fprintf(outfp, "{\n");
494 fprintf(outfp, "\t.name = 0,\n");
495 fprintf(outfp, "\t.event = 0,\n");
496 fprintf(outfp, "\t.desc = 0,\n");
498 fprintf(outfp, "},\n");
499 fprintf(outfp, "};\n");
503 static struct fixed {
507 { "inst_retired.any", "event=0xc0,period=2000003" },
508 { "inst_retired.any_p", "event=0xc0,period=2000003" },
509 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03,period=2000003" },
510 { "cpu_clk_unhalted.thread", "event=0x3c,period=2000003" },
511 { "cpu_clk_unhalted.core", "event=0x3c,period=2000003" },
512 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1,period=2000003" },
517 * Handle different fixed counter encodings between JSON and perf.
519 static char *real_event(const char *name, char *event)
526 for (i = 0; fixed[i].name; i++)
527 if (!strcasecmp(name, fixed[i].name))
528 return (char *)fixed[i].event;
533 try_fixup(const char *fn, char *arch_std, struct json_event *je, char **event)
535 /* try to find matching event from arch standard values */
536 struct event_struct *es;
538 list_for_each_entry(es, &arch_std_events, list) {
539 if (!strcmp(arch_std, es->name)) {
540 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
546 pr_err("%s: could not find matching %s for %s\n",
551 /* Call func with each event in the json file */
552 static int json_events(const char *fn,
553 int (*func)(void *data, struct json_event *je),
558 jsmntok_t *tokens, *tok;
566 tokens = parse_json(fn, &map, &size, &len);
569 EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
571 for (i = 0; i < tokens->size; i++) {
573 char *extra_desc = NULL;
575 struct json_event je = {};
576 char *arch_std = NULL;
577 unsigned long long eventcode = 0;
578 struct msrmap *msr = NULL;
579 jsmntok_t *msrval = NULL;
580 jsmntok_t *precise = NULL;
581 jsmntok_t *obj = tok++;
583 EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
584 for (j = 0; j < obj->size; j += 2) {
585 jsmntok_t *field, *val;
590 EXPECT(field->type == JSMN_STRING, tok + j,
591 "Expected field name");
593 EXPECT(val->type == JSMN_STRING, tok + j + 1,
594 "Expected string value");
596 nz = !json_streq(map, val, "0");
597 if (match_field(map, field, nz, &event, val)) {
599 } else if (json_streq(map, field, "EventCode")) {
601 addfield(map, &code, "", "", val);
602 eventcode |= strtoul(code, NULL, 0);
604 } else if (json_streq(map, field, "ExtSel")) {
606 addfield(map, &code, "", "", val);
607 eventcode |= strtoul(code, NULL, 0) << 21;
609 } else if (json_streq(map, field, "EventName")) {
610 addfield(map, &je.name, "", "", val);
611 } else if (json_streq(map, field, "Compat")) {
612 addfield(map, &je.compat, "", "", val);
613 } else if (json_streq(map, field, "BriefDescription")) {
614 addfield(map, &je.desc, "", "", val);
616 } else if (json_streq(map, field,
617 "PublicDescription")) {
618 addfield(map, &je.long_desc, "", "", val);
619 fixdesc(je.long_desc);
620 } else if (json_streq(map, field, "PEBS") && nz) {
622 } else if (json_streq(map, field, "MSRIndex") && nz) {
623 msr = lookup_msr(map, val);
624 } else if (json_streq(map, field, "MSRValue")) {
626 } else if (json_streq(map, field, "Errata") &&
627 !json_streq(map, val, "null")) {
628 addfield(map, &extra_desc, ". ",
629 " Spec update: ", val);
630 } else if (json_streq(map, field, "Data_LA") && nz) {
631 addfield(map, &extra_desc, ". ",
632 " Supports address when precise",
634 } else if (json_streq(map, field, "Unit")) {
637 ppmu = field_to_perf(unit_to_pmu, map, val);
639 je.pmu = strdup(ppmu);
642 je.pmu = strdup("uncore_");
643 addfield(map, &je.pmu, "", "", val);
644 for (s = je.pmu; *s; s++)
647 addfield(map, &je.desc, ". ", "Unit: ", NULL);
648 addfield(map, &je.desc, "", je.pmu, NULL);
649 addfield(map, &je.desc, "", " ", NULL);
650 } else if (json_streq(map, field, "Filter")) {
651 addfield(map, &filter, "", "", val);
652 } else if (json_streq(map, field, "ScaleUnit")) {
653 addfield(map, &je.unit, "", "", val);
654 } else if (json_streq(map, field, "PerPkg")) {
655 addfield(map, &je.perpkg, "", "", val);
656 } else if (json_streq(map, field, "AggregationMode")) {
657 addfield(map, &je.aggr_mode, "", "", val);
658 } else if (json_streq(map, field, "Deprecated")) {
659 addfield(map, &je.deprecated, "", "", val);
660 } else if (json_streq(map, field, "MetricName")) {
661 addfield(map, &je.metric_name, "", "", val);
662 } else if (json_streq(map, field, "MetricGroup")) {
663 addfield(map, &je.metric_group, "", "", val);
664 } else if (json_streq(map, field, "MetricConstraint")) {
665 addfield(map, &je.metric_constraint, "", "", val);
666 } else if (json_streq(map, field, "MetricExpr")) {
667 addfield(map, &je.metric_expr, "", "", val);
668 for (s = je.metric_expr; *s; s++)
670 } else if (json_streq(map, field, "ArchStdEvent")) {
671 addfield(map, &arch_std, "", "", val);
672 for (s = arch_std; *s; s++)
675 /* ignore unknown fields */
677 if (precise && je.desc && !strstr(je.desc, "(Precise Event)")) {
678 if (json_streq(map, precise, "2"))
679 addfield(map, &extra_desc, " ",
680 "(Must be precise)", NULL);
682 addfield(map, &extra_desc, " ",
683 "(Precise event)", NULL);
685 snprintf(buf, sizeof buf, "event=%#llx", eventcode);
686 addfield(map, &event, ",", buf, NULL);
687 if (je.desc && extra_desc)
688 addfield(map, &je.desc, " ", extra_desc, NULL);
689 if (je.long_desc && extra_desc)
690 addfield(map, &je.long_desc, " ", extra_desc, NULL);
692 addfield(map, &event, ",", filter, NULL);
694 addfield(map, &event, ",", msr->pname, msrval);
700 * An arch standard event is referenced, so try to
701 * fixup any unassigned values.
703 err = try_fixup(fn, arch_std, &je, &event);
707 je.event = real_event(je.name, event);
708 err = func(data, &je);
722 free(je.metric_expr);
723 free(je.metric_name);
724 free(je.metric_group);
725 free(je.metric_constraint);
732 EXPECT(tok - tokens == len, tok, "unexpected objects at end");
735 free_json(map, size, tokens);
739 static char *file_name_to_table_name(char *fname)
747 * Ensure tablename starts with alphabetic character.
748 * Derive rest of table name from basename of the JSON file,
749 * replacing hyphens and stripping out .json suffix.
751 n = asprintf(&tblname, "pme_%s", fname);
753 pr_info("%s: asprintf() error %s for file %s\n", prog,
754 strerror(errno), fname);
758 for (i = 0; i < strlen(tblname); i++) {
761 if (c == '-' || c == '/')
766 } else if (!isalnum(c) && c != '_') {
767 pr_err("%s: Invalid character '%c' in file name %s\n",
768 prog, c, basename(fname));
778 static bool is_sys_dir(char *fname)
780 size_t len = strlen(fname), len2 = strlen("/sys");
784 return !strcmp(fname+len-len2, "/sys");
787 static void print_mapping_table_prefix(FILE *outfp)
789 fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n");
792 static void print_mapping_table_suffix(FILE *outfp)
795 * Print the terminating, NULL entry.
797 fprintf(outfp, "{\n");
798 fprintf(outfp, "\t.cpuid = 0,\n");
799 fprintf(outfp, "\t.version = 0,\n");
800 fprintf(outfp, "\t.type = 0,\n");
801 fprintf(outfp, "\t.table = 0,\n");
802 fprintf(outfp, "},\n");
804 /* and finally, the closing curly bracket for the struct */
805 fprintf(outfp, "};\n");
808 static void print_mapping_test_table(FILE *outfp)
811 * Print the terminating, NULL entry.
813 fprintf(outfp, "{\n");
814 fprintf(outfp, "\t.cpuid = \"testcpu\",\n");
815 fprintf(outfp, "\t.version = \"v1\",\n");
816 fprintf(outfp, "\t.type = \"core\",\n");
817 fprintf(outfp, "\t.table = pme_test_cpu,\n");
818 fprintf(outfp, "},\n");
821 static void print_system_event_mapping_table_prefix(FILE *outfp)
823 fprintf(outfp, "\nstruct pmu_sys_events pmu_sys_event_tables[] = {");
826 static void print_system_event_mapping_table_suffix(FILE *outfp)
828 fprintf(outfp, "\n\t{\n\t\t.table = 0\n\t},");
829 fprintf(outfp, "\n};\n");
832 static int process_system_event_tables(FILE *outfp)
834 struct sys_event_table *sys_event_table;
836 print_system_event_mapping_table_prefix(outfp);
838 list_for_each_entry(sys_event_table, &sys_event_tables, list) {
839 fprintf(outfp, "\n\t{\n\t\t.table = %s,\n\t},",
840 sys_event_table->soc_id);
843 print_system_event_mapping_table_suffix(outfp);
848 static int process_mapfile(FILE *outfp, char *fpath)
858 pr_info("%s: Processing mapfile %s\n", prog, fpath);
864 mapfp = fopen(fpath, "r");
866 pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
872 print_mapping_table_prefix(outfp);
874 /* Skip first line (header) */
875 p = fgets(line, n, mapfp);
881 char *cpuid, *version, *type, *fname;
884 p = fgets(line, n, mapfp);
888 if (line[0] == '#' || line[0] == '\n')
891 if (line[strlen(line)-1] != '\n') {
892 /* TODO Deal with lines longer than 16K */
893 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
894 prog, fpath, line_num);
898 line[strlen(line)-1] = '\0';
900 cpuid = fixregex(strtok_r(p, ",", &save));
901 version = strtok_r(NULL, ",", &save);
902 fname = strtok_r(NULL, ",", &save);
903 type = strtok_r(NULL, ",", &save);
905 tblname = file_name_to_table_name(fname);
906 fprintf(outfp, "{\n");
907 fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
908 fprintf(outfp, "\t.version = \"%s\",\n", version);
909 fprintf(outfp, "\t.type = \"%s\",\n", type);
912 * CHECK: We can't use the type (eg "core") field in the
913 * table name. For us to do that, we need to somehow tweak
914 * the other caller of file_name_to_table(), process_json()
915 * to determine the type. process_json() file has no way
916 * of knowing these are "core" events unless file name has
917 * core in it. If filename has core in it, we can safely
918 * ignore the type field here also.
920 fprintf(outfp, "\t.table = %s\n", tblname);
921 fprintf(outfp, "},\n");
925 print_mapping_test_table(outfp);
926 print_mapping_table_suffix(outfp);
933 * If we fail to locate/process JSON and map files, create a NULL mapping
934 * table. This would at least allow perf to build even if we can't find/use
937 static void create_empty_mapping(const char *output_file)
941 pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
943 /* Truncate file to clear any partial writes to it */
944 outfp = fopen(output_file, "w");
950 fprintf(outfp, "#include \"pmu-events/pmu-events.h\"\n");
951 print_mapping_table_prefix(outfp);
952 print_mapping_table_suffix(outfp);
953 print_system_event_mapping_table_prefix(outfp);
954 print_system_event_mapping_table_suffix(outfp);
958 static int get_maxfds(void)
962 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
963 return min((int)rlim.rlim_max / 2, 512);
969 * nftw() doesn't let us pass an argument to the processing function,
970 * so use a global variables.
972 static FILE *eventsfp;
973 static char *mapfile;
975 static int is_leaf_dir(const char *fpath)
985 while ((dir = readdir(d)) != NULL) {
986 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
989 if (dir->d_type == DT_DIR) {
992 } else if (dir->d_type == DT_UNKNOWN) {
996 sprintf(path, "%s/%s", fpath, dir->d_name);
1000 if (S_ISDIR(st.st_mode)) {
1012 static int is_json_file(const char *name)
1016 if (strlen(name) < 5)
1019 suffix = name + strlen(name) - 5;
1021 if (strncmp(suffix, ".json", 5) == 0)
1026 static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
1027 int typeflag, struct FTW *ftwbuf)
1029 int level = ftwbuf->level;
1030 int is_file = typeflag == FTW_F;
1032 if (level == 1 && is_file && is_json_file(fpath))
1033 return json_events(fpath, save_arch_std_events, (void *)sb);
1038 static int process_one_file(const char *fpath, const struct stat *sb,
1039 int typeflag, struct FTW *ftwbuf)
1041 char *tblname, *bname;
1042 int is_dir = typeflag == FTW_D;
1043 int is_file = typeflag == FTW_F;
1044 int level = ftwbuf->level;
1047 if (level >= 2 && is_dir) {
1050 * For level 2 directory, bname will include parent name,
1051 * like vendor/platform. So search back from platform dir
1053 * Something similar for level 3 directory, but we're a PMU
1054 * category folder, like vendor/platform/cpu.
1056 bname = (char *) fpath + ftwbuf->base - 2;
1060 if (count == level - 1)
1066 bname = (char *) fpath + ftwbuf->base;
1068 pr_debug("%s %d %7jd %-20s %s\n",
1069 is_file ? "f" : is_dir ? "d" : "x",
1070 level, sb->st_size, bname, fpath);
1072 /* base dir or too deep */
1073 if (level == 0 || level > 4)
1077 /* model directory, reset topic */
1078 if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
1079 (level >= 2 && is_dir && is_leaf_dir(fpath))) {
1081 print_events_table_suffix(eventsfp);
1084 * Drop file name suffix. Replace hyphens with underscores.
1085 * Fail if file name contains any alphanum characters besides
1088 tblname = file_name_to_table_name(bname);
1090 pr_info("%s: Error determining table name for %s\n", prog,
1095 if (is_sys_dir(bname)) {
1096 struct sys_event_table *sys_event_table;
1098 sys_event_table = malloc(sizeof(*sys_event_table));
1099 if (!sys_event_table)
1102 sys_event_table->soc_id = strdup(tblname);
1103 if (!sys_event_table->soc_id) {
1104 free(sys_event_table);
1107 list_add_tail(&sys_event_table->list,
1111 print_events_table_prefix(eventsfp, tblname);
1116 * Save the mapfile name for now. We will process mapfile
1117 * after processing all JSON files (so we can write out the
1118 * mapping table after all PMU events tables).
1121 if (level == 1 && is_file) {
1122 if (!strcmp(bname, "mapfile.csv")) {
1123 mapfile = strdup(fpath);
1127 pr_info("%s: Ignoring file %s\n", prog, fpath);
1132 * If the file name does not have a .json extension,
1133 * ignore it. It could be a readme.txt for instance.
1136 if (!is_json_file(bname)) {
1137 pr_info("%s: Ignoring file without .json suffix %s\n", prog,
1143 if (level > 1 && add_topic(bname))
1147 * Assume all other files are JSON files.
1149 * If mapfile refers to 'power7_core.json', we create a table
1150 * named 'power7_core'. Any inconsistencies between the mapfile
1151 * and directory tree could result in build failure due to table
1152 * names not being found.
1154 * At least for now, be strict with processing JSON file names.
1155 * i.e. if JSON file name cannot be mapped to C-style table name,
1159 struct perf_entry_data data = {
1160 .topic = get_topic(),
1164 err = json_events(fpath, print_events_table_entry, &data);
1173 #define PATH_MAX 4096
1177 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1178 * the set of JSON files for the architecture 'arch'.
1180 * From each JSON file, create a C-style "PMU events table" from the
1181 * JSON file (see struct pmu_event).
1183 * From the mapfile, create a mapping between the CPU revisions and
1184 * PMU event tables (see struct pmu_events_map).
1186 * Write out the PMU events tables and the mapping table to pmu-event.c.
1188 int main(int argc, char *argv[])
1190 int rc, ret = 0, empty_map = 0;
1192 char ldirname[PATH_MAX];
1194 const char *output_file;
1195 const char *start_dirname;
1196 char *err_string_ext = "";
1199 prog = basename(argv[0]);
1201 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
1206 start_dirname = argv[2];
1207 output_file = argv[3];
1210 verbose = atoi(argv[4]);
1212 eventsfp = fopen(output_file, "w");
1214 pr_err("%s Unable to create required file %s (%s)\n",
1215 prog, output_file, strerror(errno));
1219 sprintf(ldirname, "%s/%s", start_dirname, arch);
1221 /* If architecture does not have any event lists, bail out */
1222 if (stat(ldirname, &stbuf) < 0) {
1223 pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
1225 goto err_close_eventsfp;
1228 /* Include pmu-events.h first */
1229 fprintf(eventsfp, "#include \"pmu-events/pmu-events.h\"\n");
1232 * The mapfile allows multiple CPUids to point to the same JSON file,
1233 * so, not sure if there is a need for symlinks within the pmu-events
1236 * For now, treat symlinks of JSON files as regular files and create
1237 * separate tables for each symlink (presumably, each symlink refers
1238 * to specific version of the CPU).
1241 maxfds = get_maxfds();
1242 rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1244 goto err_processing_std_arch_event_dir;
1246 rc = nftw(ldirname, process_one_file, maxfds, 0);
1248 goto err_processing_dir;
1250 sprintf(ldirname, "%s/test", start_dirname);
1252 rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1254 goto err_processing_std_arch_event_dir;
1256 rc = nftw(ldirname, process_one_file, maxfds, 0);
1258 goto err_processing_dir;
1261 print_events_table_suffix(eventsfp);
1264 pr_info("%s: No CPU->JSON mapping?\n", prog);
1266 goto err_close_eventsfp;
1269 rc = process_mapfile(eventsfp, mapfile);
1271 pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
1272 /* Make build fail */
1274 goto err_close_eventsfp;
1277 rc = process_system_event_tables(eventsfp);
1284 free_arch_std_events();
1288 err_processing_std_arch_event_dir:
1289 err_string_ext = " for std arch event";
1292 pr_info("%s: Error walking file tree %s%s\n", prog, ldirname,
1295 } else if (rc < 0) {
1303 create_empty_mapping(output_file);
1305 free_arch_std_events();