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 { "L3PMC", "amd_l3" },
285 { "DFPMC", "amd_df" },
289 static const char *field_to_perf(struct map *table, char *map, jsmntok_t *val)
293 for (i = 0; table[i].json; i++) {
294 if (json_streq(map, val, table[i].json))
295 return table[i].perf;
300 #define EXPECT(e, t, m) do { if (!(e)) { \
301 jsmntok_t *loc = (t); \
302 if (!(t)->start && (t) > tokens) \
304 pr_err("%s:%d: " m ", got %s\n", fn, \
305 json_line(map, loc), \
313 static char *get_topic(void)
318 /* tp is free'd in process_one_file() */
319 i = asprintf(&tp, "%s", topic);
321 pr_info("%s: asprintf() error %s\n", prog);
325 for (i = 0; i < (int) strlen(tp); i++) {
339 static int add_topic(char *bname)
342 topic = strdup(bname);
344 pr_info("%s: strdup() error %s for file %s\n", prog,
345 strerror(errno), bname);
351 struct perf_entry_data {
356 static int close_table;
358 static void print_events_table_prefix(FILE *fp, const char *tblname)
360 fprintf(fp, "struct pmu_event %s[] = {\n", tblname);
364 static int print_events_table_entry(void *data, struct json_event *je)
366 struct perf_entry_data *pd = data;
367 FILE *outfp = pd->outfp;
368 char *topic = pd->topic;
371 * TODO: Remove formatting chars after debugging to reduce
374 fprintf(outfp, "{\n");
377 fprintf(outfp, "\t.name = \"%s\",\n", je->name);
379 fprintf(outfp, "\t.event = \"%s\",\n", je->event);
380 fprintf(outfp, "\t.desc = \"%s\",\n", je->desc);
382 fprintf(outfp, "\t.compat = \"%s\",\n", je->compat);
383 fprintf(outfp, "\t.topic = \"%s\",\n", topic);
384 if (je->long_desc && je->long_desc[0])
385 fprintf(outfp, "\t.long_desc = \"%s\",\n", je->long_desc);
387 fprintf(outfp, "\t.pmu = \"%s\",\n", je->pmu);
389 fprintf(outfp, "\t.unit = \"%s\",\n", je->unit);
391 fprintf(outfp, "\t.perpkg = \"%s\",\n", je->perpkg);
393 fprintf(outfp, "\t.aggr_mode = \"%d\",\n", convert(je->aggr_mode));
395 fprintf(outfp, "\t.metric_expr = \"%s\",\n", je->metric_expr);
397 fprintf(outfp, "\t.metric_name = \"%s\",\n", je->metric_name);
398 if (je->metric_group)
399 fprintf(outfp, "\t.metric_group = \"%s\",\n", je->metric_group);
401 fprintf(outfp, "\t.deprecated = \"%s\",\n", je->deprecated);
402 if (je->metric_constraint)
403 fprintf(outfp, "\t.metric_constraint = \"%s\",\n", je->metric_constraint);
404 fprintf(outfp, "},\n");
409 struct event_struct {
410 struct list_head list;
424 char *metric_constraint;
427 #define ADD_EVENT_FIELD(field) do { if (je->field) { \
428 es->field = strdup(je->field); \
433 #define FREE_EVENT_FIELD(field) free(es->field)
435 #define TRY_FIXUP_FIELD(field) do { if (es->field && !je->field) {\
436 je->field = strdup(es->field); \
441 #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
456 static LIST_HEAD(arch_std_events);
458 static void free_arch_std_events(void)
460 struct event_struct *es, *next;
462 list_for_each_entry_safe(es, next, &arch_std_events, list) {
463 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
464 list_del_init(&es->list);
469 static int save_arch_std_events(void *data, struct json_event *je)
471 struct event_struct *es;
473 es = malloc(sizeof(*es));
476 memset(es, 0, sizeof(*es));
477 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD);
478 list_add_tail(&es->list, &arch_std_events);
481 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
486 static void print_events_table_suffix(FILE *outfp)
488 fprintf(outfp, "{\n");
490 fprintf(outfp, "\t.name = 0,\n");
491 fprintf(outfp, "\t.event = 0,\n");
492 fprintf(outfp, "\t.desc = 0,\n");
494 fprintf(outfp, "},\n");
495 fprintf(outfp, "};\n");
499 static struct fixed {
503 { "inst_retired.any", "event=0xc0,period=2000003" },
504 { "inst_retired.any_p", "event=0xc0,period=2000003" },
505 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03,period=2000003" },
506 { "cpu_clk_unhalted.thread", "event=0x3c,period=2000003" },
507 { "cpu_clk_unhalted.core", "event=0x3c,period=2000003" },
508 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1,period=2000003" },
513 * Handle different fixed counter encodings between JSON and perf.
515 static char *real_event(const char *name, char *event)
522 for (i = 0; fixed[i].name; i++)
523 if (!strcasecmp(name, fixed[i].name))
524 return (char *)fixed[i].event;
529 try_fixup(const char *fn, char *arch_std, struct json_event *je, char **event)
531 /* try to find matching event from arch standard values */
532 struct event_struct *es;
534 list_for_each_entry(es, &arch_std_events, list) {
535 if (!strcmp(arch_std, es->name)) {
536 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
542 pr_err("%s: could not find matching %s for %s\n",
547 /* Call func with each event in the json file */
548 static int json_events(const char *fn,
549 int (*func)(void *data, struct json_event *je),
554 jsmntok_t *tokens, *tok;
562 tokens = parse_json(fn, &map, &size, &len);
565 EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
567 for (i = 0; i < tokens->size; i++) {
569 char *extra_desc = NULL;
571 struct json_event je = {};
572 char *arch_std = NULL;
573 unsigned long long eventcode = 0;
574 struct msrmap *msr = NULL;
575 jsmntok_t *msrval = NULL;
576 jsmntok_t *precise = NULL;
577 jsmntok_t *obj = tok++;
579 EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
580 for (j = 0; j < obj->size; j += 2) {
581 jsmntok_t *field, *val;
586 EXPECT(field->type == JSMN_STRING, tok + j,
587 "Expected field name");
589 EXPECT(val->type == JSMN_STRING, tok + j + 1,
590 "Expected string value");
592 nz = !json_streq(map, val, "0");
593 if (match_field(map, field, nz, &event, val)) {
595 } else if (json_streq(map, field, "EventCode")) {
597 addfield(map, &code, "", "", val);
598 eventcode |= strtoul(code, NULL, 0);
600 } else if (json_streq(map, field, "ExtSel")) {
602 addfield(map, &code, "", "", val);
603 eventcode |= strtoul(code, NULL, 0) << 21;
605 } else if (json_streq(map, field, "EventName")) {
606 addfield(map, &je.name, "", "", val);
607 } else if (json_streq(map, field, "Compat")) {
608 addfield(map, &je.compat, "", "", val);
609 } else if (json_streq(map, field, "BriefDescription")) {
610 addfield(map, &je.desc, "", "", val);
612 } else if (json_streq(map, field,
613 "PublicDescription")) {
614 addfield(map, &je.long_desc, "", "", val);
615 fixdesc(je.long_desc);
616 } else if (json_streq(map, field, "PEBS") && nz) {
618 } else if (json_streq(map, field, "MSRIndex") && nz) {
619 msr = lookup_msr(map, val);
620 } else if (json_streq(map, field, "MSRValue")) {
622 } else if (json_streq(map, field, "Errata") &&
623 !json_streq(map, val, "null")) {
624 addfield(map, &extra_desc, ". ",
625 " Spec update: ", val);
626 } else if (json_streq(map, field, "Data_LA") && nz) {
627 addfield(map, &extra_desc, ". ",
628 " Supports address when precise",
630 } else if (json_streq(map, field, "Unit")) {
633 ppmu = field_to_perf(unit_to_pmu, map, val);
635 je.pmu = strdup(ppmu);
638 je.pmu = strdup("uncore_");
639 addfield(map, &je.pmu, "", "", val);
640 for (s = je.pmu; *s; s++)
643 addfield(map, &je.desc, ". ", "Unit: ", NULL);
644 addfield(map, &je.desc, "", je.pmu, NULL);
645 addfield(map, &je.desc, "", " ", NULL);
646 } else if (json_streq(map, field, "Filter")) {
647 addfield(map, &filter, "", "", val);
648 } else if (json_streq(map, field, "ScaleUnit")) {
649 addfield(map, &je.unit, "", "", val);
650 } else if (json_streq(map, field, "PerPkg")) {
651 addfield(map, &je.perpkg, "", "", val);
652 } else if (json_streq(map, field, "AggregationMode")) {
653 addfield(map, &je.aggr_mode, "", "", val);
654 } else if (json_streq(map, field, "Deprecated")) {
655 addfield(map, &je.deprecated, "", "", val);
656 } else if (json_streq(map, field, "MetricName")) {
657 addfield(map, &je.metric_name, "", "", val);
658 } else if (json_streq(map, field, "MetricGroup")) {
659 addfield(map, &je.metric_group, "", "", val);
660 } else if (json_streq(map, field, "MetricConstraint")) {
661 addfield(map, &je.metric_constraint, "", "", val);
662 } else if (json_streq(map, field, "MetricExpr")) {
663 addfield(map, &je.metric_expr, "", "", val);
664 for (s = je.metric_expr; *s; s++)
666 } else if (json_streq(map, field, "ArchStdEvent")) {
667 addfield(map, &arch_std, "", "", val);
668 for (s = arch_std; *s; s++)
671 /* ignore unknown fields */
673 if (precise && je.desc && !strstr(je.desc, "(Precise Event)")) {
674 if (json_streq(map, precise, "2"))
675 addfield(map, &extra_desc, " ",
676 "(Must be precise)", NULL);
678 addfield(map, &extra_desc, " ",
679 "(Precise event)", NULL);
681 snprintf(buf, sizeof buf, "event=%#llx", eventcode);
682 addfield(map, &event, ",", buf, NULL);
683 if (je.desc && extra_desc)
684 addfield(map, &je.desc, " ", extra_desc, NULL);
685 if (je.long_desc && extra_desc)
686 addfield(map, &je.long_desc, " ", extra_desc, NULL);
688 addfield(map, &event, ",", filter, NULL);
690 addfield(map, &event, ",", msr->pname, msrval);
696 * An arch standard event is referenced, so try to
697 * fixup any unassigned values.
699 err = try_fixup(fn, arch_std, &je, &event);
703 je.event = real_event(je.name, event);
704 err = func(data, &je);
718 free(je.metric_expr);
719 free(je.metric_name);
720 free(je.metric_group);
721 free(je.metric_constraint);
728 EXPECT(tok - tokens == len, tok, "unexpected objects at end");
731 free_json(map, size, tokens);
735 static char *file_name_to_table_name(char *fname)
743 * Ensure tablename starts with alphabetic character.
744 * Derive rest of table name from basename of the JSON file,
745 * replacing hyphens and stripping out .json suffix.
747 n = asprintf(&tblname, "pme_%s", fname);
749 pr_info("%s: asprintf() error %s for file %s\n", prog,
750 strerror(errno), fname);
754 for (i = 0; i < strlen(tblname); i++) {
757 if (c == '-' || c == '/')
762 } else if (!isalnum(c) && c != '_') {
763 pr_err("%s: Invalid character '%c' in file name %s\n",
764 prog, c, basename(fname));
774 static bool is_sys_dir(char *fname)
776 size_t len = strlen(fname), len2 = strlen("/sys");
780 return !strcmp(fname+len-len2, "/sys");
783 static void print_mapping_table_prefix(FILE *outfp)
785 fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n");
788 static void print_mapping_table_suffix(FILE *outfp)
791 * Print the terminating, NULL entry.
793 fprintf(outfp, "{\n");
794 fprintf(outfp, "\t.cpuid = 0,\n");
795 fprintf(outfp, "\t.version = 0,\n");
796 fprintf(outfp, "\t.type = 0,\n");
797 fprintf(outfp, "\t.table = 0,\n");
798 fprintf(outfp, "},\n");
800 /* and finally, the closing curly bracket for the struct */
801 fprintf(outfp, "};\n");
804 static void print_mapping_test_table(FILE *outfp)
807 * Print the terminating, NULL entry.
809 fprintf(outfp, "{\n");
810 fprintf(outfp, "\t.cpuid = \"testcpu\",\n");
811 fprintf(outfp, "\t.version = \"v1\",\n");
812 fprintf(outfp, "\t.type = \"core\",\n");
813 fprintf(outfp, "\t.table = pme_test_cpu,\n");
814 fprintf(outfp, "},\n");
817 static void print_system_event_mapping_table_prefix(FILE *outfp)
819 fprintf(outfp, "\nstruct pmu_sys_events pmu_sys_event_tables[] = {");
822 static void print_system_event_mapping_table_suffix(FILE *outfp)
824 fprintf(outfp, "\n\t{\n\t\t.table = 0\n\t},");
825 fprintf(outfp, "\n};\n");
828 static int process_system_event_tables(FILE *outfp)
830 struct sys_event_table *sys_event_table;
832 print_system_event_mapping_table_prefix(outfp);
834 list_for_each_entry(sys_event_table, &sys_event_tables, list) {
835 fprintf(outfp, "\n\t{\n\t\t.table = %s,\n\t},",
836 sys_event_table->soc_id);
839 print_system_event_mapping_table_suffix(outfp);
844 static int process_mapfile(FILE *outfp, char *fpath)
854 pr_info("%s: Processing mapfile %s\n", prog, fpath);
860 mapfp = fopen(fpath, "r");
862 pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
868 print_mapping_table_prefix(outfp);
870 /* Skip first line (header) */
871 p = fgets(line, n, mapfp);
877 char *cpuid, *version, *type, *fname;
880 p = fgets(line, n, mapfp);
884 if (line[0] == '#' || line[0] == '\n')
887 if (line[strlen(line)-1] != '\n') {
888 /* TODO Deal with lines longer than 16K */
889 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
890 prog, fpath, line_num);
894 line[strlen(line)-1] = '\0';
896 cpuid = fixregex(strtok_r(p, ",", &save));
897 version = strtok_r(NULL, ",", &save);
898 fname = strtok_r(NULL, ",", &save);
899 type = strtok_r(NULL, ",", &save);
901 tblname = file_name_to_table_name(fname);
902 fprintf(outfp, "{\n");
903 fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
904 fprintf(outfp, "\t.version = \"%s\",\n", version);
905 fprintf(outfp, "\t.type = \"%s\",\n", type);
908 * CHECK: We can't use the type (eg "core") field in the
909 * table name. For us to do that, we need to somehow tweak
910 * the other caller of file_name_to_table(), process_json()
911 * to determine the type. process_json() file has no way
912 * of knowing these are "core" events unless file name has
913 * core in it. If filename has core in it, we can safely
914 * ignore the type field here also.
916 fprintf(outfp, "\t.table = %s\n", tblname);
917 fprintf(outfp, "},\n");
921 print_mapping_test_table(outfp);
922 print_mapping_table_suffix(outfp);
929 * If we fail to locate/process JSON and map files, create a NULL mapping
930 * table. This would at least allow perf to build even if we can't find/use
933 static void create_empty_mapping(const char *output_file)
937 pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
939 /* Truncate file to clear any partial writes to it */
940 outfp = fopen(output_file, "w");
946 fprintf(outfp, "#include \"pmu-events/pmu-events.h\"\n");
947 print_mapping_table_prefix(outfp);
948 print_mapping_table_suffix(outfp);
949 print_system_event_mapping_table_prefix(outfp);
950 print_system_event_mapping_table_suffix(outfp);
954 static int get_maxfds(void)
958 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
959 return min((int)rlim.rlim_max / 2, 512);
965 * nftw() doesn't let us pass an argument to the processing function,
966 * so use a global variables.
968 static FILE *eventsfp;
969 static char *mapfile;
971 static int is_leaf_dir(const char *fpath)
981 while ((dir = readdir(d)) != NULL) {
982 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
985 if (dir->d_type == DT_DIR) {
988 } else if (dir->d_type == DT_UNKNOWN) {
992 sprintf(path, "%s/%s", fpath, dir->d_name);
996 if (S_ISDIR(st.st_mode)) {
1008 static int is_json_file(const char *name)
1012 if (strlen(name) < 5)
1015 suffix = name + strlen(name) - 5;
1017 if (strncmp(suffix, ".json", 5) == 0)
1022 static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
1023 int typeflag, struct FTW *ftwbuf)
1025 int level = ftwbuf->level;
1026 int is_file = typeflag == FTW_F;
1028 if (level == 1 && is_file && is_json_file(fpath))
1029 return json_events(fpath, save_arch_std_events, (void *)sb);
1034 static int process_one_file(const char *fpath, const struct stat *sb,
1035 int typeflag, struct FTW *ftwbuf)
1037 char *tblname, *bname;
1038 int is_dir = typeflag == FTW_D;
1039 int is_file = typeflag == FTW_F;
1040 int level = ftwbuf->level;
1043 if (level >= 2 && is_dir) {
1046 * For level 2 directory, bname will include parent name,
1047 * like vendor/platform. So search back from platform dir
1049 * Something similar for level 3 directory, but we're a PMU
1050 * category folder, like vendor/platform/cpu.
1052 bname = (char *) fpath + ftwbuf->base - 2;
1056 if (count == level - 1)
1062 bname = (char *) fpath + ftwbuf->base;
1064 pr_debug("%s %d %7jd %-20s %s\n",
1065 is_file ? "f" : is_dir ? "d" : "x",
1066 level, sb->st_size, bname, fpath);
1068 /* base dir or too deep */
1069 if (level == 0 || level > 4)
1073 /* model directory, reset topic */
1074 if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
1075 (level >= 2 && is_dir && is_leaf_dir(fpath))) {
1077 print_events_table_suffix(eventsfp);
1080 * Drop file name suffix. Replace hyphens with underscores.
1081 * Fail if file name contains any alphanum characters besides
1084 tblname = file_name_to_table_name(bname);
1086 pr_info("%s: Error determining table name for %s\n", prog,
1091 if (is_sys_dir(bname)) {
1092 struct sys_event_table *sys_event_table;
1094 sys_event_table = malloc(sizeof(*sys_event_table));
1095 if (!sys_event_table)
1098 sys_event_table->soc_id = strdup(tblname);
1099 if (!sys_event_table->soc_id) {
1100 free(sys_event_table);
1103 list_add_tail(&sys_event_table->list,
1107 print_events_table_prefix(eventsfp, tblname);
1112 * Save the mapfile name for now. We will process mapfile
1113 * after processing all JSON files (so we can write out the
1114 * mapping table after all PMU events tables).
1117 if (level == 1 && is_file) {
1118 if (!strcmp(bname, "mapfile.csv")) {
1119 mapfile = strdup(fpath);
1123 pr_info("%s: Ignoring file %s\n", prog, fpath);
1128 * If the file name does not have a .json extension,
1129 * ignore it. It could be a readme.txt for instance.
1132 if (!is_json_file(bname)) {
1133 pr_info("%s: Ignoring file without .json suffix %s\n", prog,
1139 if (level > 1 && add_topic(bname))
1143 * Assume all other files are JSON files.
1145 * If mapfile refers to 'power7_core.json', we create a table
1146 * named 'power7_core'. Any inconsistencies between the mapfile
1147 * and directory tree could result in build failure due to table
1148 * names not being found.
1150 * Atleast for now, be strict with processing JSON file names.
1151 * i.e. if JSON file name cannot be mapped to C-style table name,
1155 struct perf_entry_data data = {
1156 .topic = get_topic(),
1160 err = json_events(fpath, print_events_table_entry, &data);
1169 #define PATH_MAX 4096
1173 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1174 * the set of JSON files for the architecture 'arch'.
1176 * From each JSON file, create a C-style "PMU events table" from the
1177 * JSON file (see struct pmu_event).
1179 * From the mapfile, create a mapping between the CPU revisions and
1180 * PMU event tables (see struct pmu_events_map).
1182 * Write out the PMU events tables and the mapping table to pmu-event.c.
1184 int main(int argc, char *argv[])
1186 int rc, ret = 0, empty_map = 0;
1188 char ldirname[PATH_MAX];
1190 const char *output_file;
1191 const char *start_dirname;
1192 char *err_string_ext = "";
1195 prog = basename(argv[0]);
1197 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
1202 start_dirname = argv[2];
1203 output_file = argv[3];
1206 verbose = atoi(argv[4]);
1208 eventsfp = fopen(output_file, "w");
1210 pr_err("%s Unable to create required file %s (%s)\n",
1211 prog, output_file, strerror(errno));
1215 sprintf(ldirname, "%s/%s", start_dirname, arch);
1217 /* If architecture does not have any event lists, bail out */
1218 if (stat(ldirname, &stbuf) < 0) {
1219 pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
1221 goto err_close_eventsfp;
1224 /* Include pmu-events.h first */
1225 fprintf(eventsfp, "#include \"pmu-events/pmu-events.h\"\n");
1228 * The mapfile allows multiple CPUids to point to the same JSON file,
1229 * so, not sure if there is a need for symlinks within the pmu-events
1232 * For now, treat symlinks of JSON files as regular files and create
1233 * separate tables for each symlink (presumably, each symlink refers
1234 * to specific version of the CPU).
1237 maxfds = get_maxfds();
1238 rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1240 goto err_processing_std_arch_event_dir;
1242 rc = nftw(ldirname, process_one_file, maxfds, 0);
1244 goto err_processing_dir;
1246 sprintf(ldirname, "%s/test", start_dirname);
1248 rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1250 goto err_processing_std_arch_event_dir;
1252 rc = nftw(ldirname, process_one_file, maxfds, 0);
1254 goto err_processing_dir;
1257 print_events_table_suffix(eventsfp);
1260 pr_info("%s: No CPU->JSON mapping?\n", prog);
1262 goto err_close_eventsfp;
1265 rc = process_mapfile(eventsfp, mapfile);
1267 pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
1268 /* Make build fail */
1270 goto err_close_eventsfp;
1273 rc = process_system_event_tables(eventsfp);
1280 free_arch_std_events();
1284 err_processing_std_arch_event_dir:
1285 err_string_ext = " for std arch event";
1288 pr_info("%s: Error walking file tree %s%s\n", prog, ldirname,
1291 } else if (rc < 0) {
1299 create_empty_mapping(output_file);
1301 free_arch_std_events();