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/compiler.h>
49 #include <linux/list.h>
52 #include "pmu-events.h"
71 char *metric_constraint;
74 static enum aggr_mode_class convert(const char *aggr_mode)
76 if (!strcmp(aggr_mode, "PerCore"))
78 else if (!strcmp(aggr_mode, "PerChip"))
81 pr_err("%s: Wrong AggregationMode value '%s'\n", prog, aggr_mode);
85 static LIST_HEAD(sys_event_tables);
87 struct sys_event_table {
88 struct list_head list;
92 static void free_sys_event_tables(void)
94 struct sys_event_table *et, *next;
96 list_for_each_entry_safe(et, next, &sys_event_tables, list) {
102 int eprintf(int level, int var, const char *fmt, ...)
113 ret = vfprintf(stderr, fmt, args);
120 static void addfield(char *map, char **dst, const char *sep,
121 const char *a, jsmntok_t *bt)
123 unsigned int len = strlen(a) + 1 + strlen(sep);
124 int olen = *dst ? strlen(*dst) : 0;
125 int blen = bt ? json_len(bt) : 0;
128 out = realloc(*dst, len + olen + blen);
130 /* Don't add field in this case */
141 strncat(*dst, map + bt->start, blen);
144 static void fixname(char *s)
150 static void fixdesc(char *s)
152 char *e = s + strlen(s);
154 /* Remove trailing dots that look ugly in perf list */
156 while (e >= s && isspace(*e))
162 /* Add escapes for '\' so they are proper C strings. */
163 static char *fixregex(char *s)
170 /* Count the number of '\' in string */
171 for (p = s; *p; p++) {
180 /* allocate space for a new string */
181 fixed = (char *) malloc(len + esc_count + 1);
185 /* copy over the characters */
187 for (p = s; *p; p++) {
199 static struct msrmap {
203 { "0x3F6", "ldlat=" },
204 { "0x1A6", "offcore_rsp=" },
205 { "0x1A7", "offcore_rsp=" },
206 { "0x3F7", "frontend=" },
210 static struct field {
214 { "UMask", "umask=" },
215 { "CounterMask", "cmask=" },
216 { "Invert", "inv=" },
217 { "AnyThread", "any=" },
218 { "EdgeDetect", "edge=" },
219 { "SampleAfterValue", "period=" },
220 { "FCMask", "fc_mask=" },
221 { "PortMask", "ch_mask=" },
225 static void cut_comma(char *map, jsmntok_t *newval)
229 /* Cut off everything after comma */
230 for (i = newval->start; i < newval->end; i++) {
236 static int match_field(char *map, jsmntok_t *field, int nz,
237 char **event, jsmntok_t *val)
240 jsmntok_t newval = *val;
242 for (f = fields; f->field; f++)
243 if (json_streq(map, field, f->field) && nz) {
244 cut_comma(map, &newval);
245 addfield(map, event, ",", f->kernel, &newval);
251 static struct msrmap *lookup_msr(char *map, jsmntok_t *val)
253 jsmntok_t newval = *val;
257 cut_comma(map, &newval);
258 for (i = 0; msrmap[i].num; i++)
259 if (json_streq(map, &newval, msrmap[i].num))
263 pr_err("%s: Unknown MSR in event file %.*s\n", prog,
264 json_len(val), map + val->start);
273 { "CBO", "uncore_cbox" },
274 { "QPI LL", "uncore_qpi" },
275 { "SBO", "uncore_sbox" },
276 { "iMPH-U", "uncore_arb" },
277 { "CPU-M-CF", "cpum_cf" },
278 { "CPU-M-SF", "cpum_sf" },
279 { "UPI LL", "uncore_upi" },
280 { "hisi_sccl,ddrc", "hisi_sccl,ddrc" },
281 { "hisi_sccl,hha", "hisi_sccl,hha" },
282 { "hisi_sccl,l3c", "hisi_sccl,l3c" },
283 /* it's not realistic to keep adding these, we need something more scalable ... */
284 { "imx8_ddr", "imx8_ddr" },
285 { "L3PMC", "amd_l3" },
286 { "DFPMC", "amd_df" },
287 { "cpu_core", "cpu_core" },
288 { "cpu_atom", "cpu_atom" },
292 static const char *field_to_perf(struct map *table, char *map, jsmntok_t *val)
296 for (i = 0; table[i].json; i++) {
297 if (json_streq(map, val, table[i].json))
298 return table[i].perf;
303 #define EXPECT(e, t, m) do { if (!(e)) { \
304 jsmntok_t *loc = (t); \
305 if (!(t)->start && (t) > tokens) \
307 pr_err("%s:%d: " m ", got %s\n", fn, \
308 json_line(map, loc), \
316 static char *get_topic(void)
321 /* tp is free'd in process_one_file() */
322 i = asprintf(&tp, "%s", topic);
324 pr_info("%s: asprintf() error %s\n", prog);
328 for (i = 0; i < (int) strlen(tp); i++) {
342 static int add_topic(char *bname)
345 topic = strdup(bname);
347 pr_info("%s: strdup() error %s for file %s\n", prog,
348 strerror(errno), bname);
354 struct perf_entry_data {
359 static int close_table;
361 static void print_events_table_prefix(FILE *fp, const char *tblname)
363 fprintf(fp, "static const struct pmu_event %s[] = {\n", tblname);
367 static int print_events_table_entry(void *data, struct json_event *je)
369 struct perf_entry_data *pd = data;
370 FILE *outfp = pd->outfp;
371 char *topic_local = pd->topic;
374 * TODO: Remove formatting chars after debugging to reduce
377 fprintf(outfp, "{\n");
380 fprintf(outfp, "\t.name = \"%s\",\n", je->name);
382 fprintf(outfp, "\t.event = \"%s\",\n", je->event);
383 fprintf(outfp, "\t.desc = \"%s\",\n", je->desc);
385 fprintf(outfp, "\t.compat = \"%s\",\n", je->compat);
386 fprintf(outfp, "\t.topic = \"%s\",\n", topic_local);
387 if (je->long_desc && je->long_desc[0])
388 fprintf(outfp, "\t.long_desc = \"%s\",\n", je->long_desc);
390 fprintf(outfp, "\t.pmu = \"%s\",\n", je->pmu);
392 fprintf(outfp, "\t.unit = \"%s\",\n", je->unit);
394 fprintf(outfp, "\t.perpkg = \"%s\",\n", je->perpkg);
396 fprintf(outfp, "\t.aggr_mode = \"%d\",\n", convert(je->aggr_mode));
398 fprintf(outfp, "\t.metric_expr = \"%s\",\n", je->metric_expr);
400 fprintf(outfp, "\t.metric_name = \"%s\",\n", je->metric_name);
401 if (je->metric_group)
402 fprintf(outfp, "\t.metric_group = \"%s\",\n", je->metric_group);
404 fprintf(outfp, "\t.deprecated = \"%s\",\n", je->deprecated);
405 if (je->metric_constraint)
406 fprintf(outfp, "\t.metric_constraint = \"%s\",\n", je->metric_constraint);
407 fprintf(outfp, "},\n");
412 struct event_struct {
413 struct list_head list;
427 char *metric_constraint;
430 #define ADD_EVENT_FIELD(field) do { if (je->field) { \
431 es->field = strdup(je->field); \
436 #define FREE_EVENT_FIELD(field) free(es->field)
438 #define TRY_FIXUP_FIELD(field) do { if (es->field && !je->field) {\
439 je->field = strdup(es->field); \
444 #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
459 static LIST_HEAD(arch_std_events);
461 static void free_arch_std_events(void)
463 struct event_struct *es, *next;
465 list_for_each_entry_safe(es, next, &arch_std_events, list) {
466 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
467 list_del_init(&es->list);
472 static int save_arch_std_events(void *data __maybe_unused, struct json_event *je)
474 struct event_struct *es;
476 es = malloc(sizeof(*es));
479 memset(es, 0, sizeof(*es));
480 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD);
481 list_add_tail(&es->list, &arch_std_events);
484 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
489 static void print_events_table_suffix(FILE *outfp)
491 fprintf(outfp, "{\n");
493 fprintf(outfp, "\t.name = 0,\n");
494 fprintf(outfp, "\t.event = 0,\n");
495 fprintf(outfp, "\t.desc = 0,\n");
497 fprintf(outfp, "},\n");
498 fprintf(outfp, "};\n");
502 static struct fixed {
506 { "inst_retired.any", "event=0xc0,period=2000003" },
507 { "inst_retired.any_p", "event=0xc0,period=2000003" },
508 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03,period=2000003" },
509 { "cpu_clk_unhalted.thread", "event=0x3c,period=2000003" },
510 { "cpu_clk_unhalted.core", "event=0x3c,period=2000003" },
511 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1,period=2000003" },
516 * Handle different fixed counter encodings between JSON and perf.
518 static char *real_event(const char *name, char *event)
525 for (i = 0; fixed[i].name; i++)
526 if (!strcasecmp(name, fixed[i].name))
527 return (char *)fixed[i].event;
532 try_fixup(const char *fn, char *arch_std, struct json_event *je, char **event)
534 /* try to find matching event from arch standard values */
535 struct event_struct *es;
537 list_for_each_entry(es, &arch_std_events, list) {
538 if (!strcmp(arch_std, es->name)) {
539 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
545 pr_err("%s: could not find matching %s for %s\n",
550 /* Call func with each event in the json file */
551 static int json_events(const char *fn,
552 int (*func)(void *data, struct json_event *je),
557 jsmntok_t *tokens, *tok;
565 tokens = parse_json(fn, &map, &size, &len);
568 EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
570 for (i = 0; i < tokens->size; i++) {
572 char *extra_desc = NULL;
574 struct json_event je = {};
575 char *arch_std = NULL;
576 unsigned long long eventcode = 0;
577 unsigned long long configcode = 0;
578 struct msrmap *msr = NULL;
579 jsmntok_t *msrval = NULL;
580 jsmntok_t *precise = NULL;
581 jsmntok_t *obj = tok++;
582 bool configcode_present = false;
584 EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
585 for (j = 0; j < obj->size; j += 2) {
586 jsmntok_t *field, *val;
591 EXPECT(field->type == JSMN_STRING, tok + j,
592 "Expected field name");
594 EXPECT(val->type == JSMN_STRING, tok + j + 1,
595 "Expected string value");
597 nz = !json_streq(map, val, "0");
598 if (match_field(map, field, nz, &event, val)) {
600 } else if (json_streq(map, field, "EventCode")) {
602 addfield(map, &code, "", "", val);
603 eventcode |= strtoul(code, NULL, 0);
605 } else if (json_streq(map, field, "ConfigCode")) {
607 addfield(map, &code, "", "", val);
608 configcode |= strtoul(code, NULL, 0);
610 configcode_present = true;
611 } else if (json_streq(map, field, "ExtSel")) {
613 addfield(map, &code, "", "", val);
614 eventcode |= strtoul(code, NULL, 0) << 21;
616 } else if (json_streq(map, field, "EventName")) {
617 addfield(map, &je.name, "", "", val);
618 } else if (json_streq(map, field, "Compat")) {
619 addfield(map, &je.compat, "", "", val);
620 } else if (json_streq(map, field, "BriefDescription")) {
621 addfield(map, &je.desc, "", "", val);
623 } else if (json_streq(map, field,
624 "PublicDescription")) {
625 addfield(map, &je.long_desc, "", "", val);
626 fixdesc(je.long_desc);
627 } else if (json_streq(map, field, "PEBS") && nz) {
629 } else if (json_streq(map, field, "MSRIndex") && nz) {
630 msr = lookup_msr(map, val);
631 } else if (json_streq(map, field, "MSRValue")) {
633 } else if (json_streq(map, field, "Errata") &&
634 !json_streq(map, val, "null")) {
635 addfield(map, &extra_desc, ". ",
636 " Spec update: ", val);
637 } else if (json_streq(map, field, "Data_LA") && nz) {
638 addfield(map, &extra_desc, ". ",
639 " Supports address when precise",
641 } else if (json_streq(map, field, "Unit")) {
644 ppmu = field_to_perf(unit_to_pmu, map, val);
646 je.pmu = strdup(ppmu);
649 je.pmu = strdup("uncore_");
650 addfield(map, &je.pmu, "", "", val);
651 for (s = je.pmu; *s; s++)
654 addfield(map, &je.desc, ". ", "Unit: ", NULL);
655 addfield(map, &je.desc, "", je.pmu, NULL);
656 addfield(map, &je.desc, "", " ", NULL);
657 } else if (json_streq(map, field, "Filter")) {
658 addfield(map, &filter, "", "", val);
659 } else if (json_streq(map, field, "ScaleUnit")) {
660 addfield(map, &je.unit, "", "", val);
661 } else if (json_streq(map, field, "PerPkg")) {
662 addfield(map, &je.perpkg, "", "", val);
663 } else if (json_streq(map, field, "AggregationMode")) {
664 addfield(map, &je.aggr_mode, "", "", val);
665 } else if (json_streq(map, field, "Deprecated")) {
666 addfield(map, &je.deprecated, "", "", val);
667 } else if (json_streq(map, field, "MetricName")) {
668 addfield(map, &je.metric_name, "", "", val);
669 } else if (json_streq(map, field, "MetricGroup")) {
670 addfield(map, &je.metric_group, "", "", val);
671 } else if (json_streq(map, field, "MetricConstraint")) {
672 addfield(map, &je.metric_constraint, "", "", val);
673 } else if (json_streq(map, field, "MetricExpr")) {
674 addfield(map, &je.metric_expr, "", "", val);
675 } else if (json_streq(map, field, "ArchStdEvent")) {
676 addfield(map, &arch_std, "", "", val);
677 for (s = arch_std; *s; s++)
680 /* ignore unknown fields */
682 if (precise && je.desc && !strstr(je.desc, "(Precise Event)")) {
683 if (json_streq(map, precise, "2"))
684 addfield(map, &extra_desc, " ",
685 "(Must be precise)", NULL);
687 addfield(map, &extra_desc, " ",
688 "(Precise event)", NULL);
690 if (configcode_present)
691 snprintf(buf, sizeof buf, "config=%#llx", configcode);
693 snprintf(buf, sizeof buf, "event=%#llx", eventcode);
694 addfield(map, &event, ",", buf, NULL);
695 if (je.desc && extra_desc)
696 addfield(map, &je.desc, " ", extra_desc, NULL);
697 if (je.long_desc && extra_desc)
698 addfield(map, &je.long_desc, " ", extra_desc, NULL);
700 addfield(map, &event, ",", filter, NULL);
702 addfield(map, &event, ",", msr->pname, msrval);
708 * An arch standard event is referenced, so try to
709 * fixup any unassigned values.
711 err = try_fixup(fn, arch_std, &je, &event);
715 je.event = real_event(je.name, event);
716 err = func(data, &je);
730 free(je.metric_expr);
731 free(je.metric_name);
732 free(je.metric_group);
733 free(je.metric_constraint);
740 EXPECT(tok - tokens == len, tok, "unexpected objects at end");
743 free_json(map, size, tokens);
747 static char *file_name_to_table_name(char *fname)
755 * Ensure tablename starts with alphabetic character.
756 * Derive rest of table name from basename of the JSON file,
757 * replacing hyphens and stripping out .json suffix.
759 n = asprintf(&tblname, "pme_%s", fname);
761 pr_info("%s: asprintf() error %s for file %s\n", prog,
762 strerror(errno), fname);
766 for (i = 0; i < strlen(tblname); i++) {
769 if (c == '-' || c == '/')
774 } else if (!isalnum(c) && c != '_') {
775 pr_err("%s: Invalid character '%c' in file name %s\n",
776 prog, c, basename(fname));
786 static bool is_sys_dir(char *fname)
788 size_t len = strlen(fname), len2 = strlen("/sys");
792 return !strcmp(fname+len-len2, "/sys");
795 static void print_mapping_table_prefix(FILE *outfp)
797 fprintf(outfp, "const struct pmu_events_map pmu_events_map[] = {\n");
800 static void print_mapping_table_suffix(FILE *outfp)
803 * Print the terminating, NULL entry.
805 fprintf(outfp, "{\n");
806 fprintf(outfp, "\t.cpuid = 0,\n");
807 fprintf(outfp, "\t.version = 0,\n");
808 fprintf(outfp, "\t.type = 0,\n");
809 fprintf(outfp, "\t.table = 0,\n");
810 fprintf(outfp, "},\n");
812 /* and finally, the closing curly bracket for the struct */
813 fprintf(outfp, "};\n");
816 static void print_mapping_test_table(FILE *outfp)
819 * Print the terminating, NULL entry.
821 fprintf(outfp, "{\n");
822 fprintf(outfp, "\t.cpuid = \"testcpu\",\n");
823 fprintf(outfp, "\t.version = \"v1\",\n");
824 fprintf(outfp, "\t.type = \"core\",\n");
825 fprintf(outfp, "\t.table = pme_test_soc_cpu,\n");
826 fprintf(outfp, "},\n");
829 static void print_system_event_mapping_table_prefix(FILE *outfp)
831 fprintf(outfp, "\nconst struct pmu_sys_events pmu_sys_event_tables[] = {");
834 static void print_system_event_mapping_table_suffix(FILE *outfp)
836 fprintf(outfp, "\n\t{\n\t\t.table = 0\n\t},");
837 fprintf(outfp, "\n};\n");
840 static int process_system_event_tables(FILE *outfp)
842 struct sys_event_table *sys_event_table;
844 print_system_event_mapping_table_prefix(outfp);
846 list_for_each_entry(sys_event_table, &sys_event_tables, list) {
847 fprintf(outfp, "\n\t{\n\t\t.table = %s,\n\t\t.name = \"%s\",\n\t},",
848 sys_event_table->soc_id,
849 sys_event_table->soc_id);
852 print_system_event_mapping_table_suffix(outfp);
857 static int process_mapfile(FILE *outfp, char *fpath)
867 pr_info("%s: Processing mapfile %s\n", prog, fpath);
873 mapfp = fopen(fpath, "r");
875 pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
881 print_mapping_table_prefix(outfp);
883 /* Skip first line (header) */
884 p = fgets(line, n, mapfp);
890 char *cpuid, *version, *type, *fname;
893 p = fgets(line, n, mapfp);
897 if (line[0] == '#' || line[0] == '\n')
900 if (line[strlen(line)-1] != '\n') {
901 /* TODO Deal with lines longer than 16K */
902 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
903 prog, fpath, line_num);
907 line[strlen(line)-1] = '\0';
909 cpuid = fixregex(strtok_r(p, ",", &save));
910 version = strtok_r(NULL, ",", &save);
911 fname = strtok_r(NULL, ",", &save);
912 type = strtok_r(NULL, ",", &save);
914 tblname = file_name_to_table_name(fname);
915 fprintf(outfp, "{\n");
916 fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
917 fprintf(outfp, "\t.version = \"%s\",\n", version);
918 fprintf(outfp, "\t.type = \"%s\",\n", type);
921 * CHECK: We can't use the type (eg "core") field in the
922 * table name. For us to do that, we need to somehow tweak
923 * the other caller of file_name_to_table(), process_json()
924 * to determine the type. process_json() file has no way
925 * of knowing these are "core" events unless file name has
926 * core in it. If filename has core in it, we can safely
927 * ignore the type field here also.
929 fprintf(outfp, "\t.table = %s\n", tblname);
930 fprintf(outfp, "},\n");
934 print_mapping_test_table(outfp);
935 print_mapping_table_suffix(outfp);
942 * If we fail to locate/process JSON and map files, create a NULL mapping
943 * table. This would at least allow perf to build even if we can't find/use
946 static void create_empty_mapping(const char *output_file)
950 pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
952 /* Truncate file to clear any partial writes to it */
953 outfp = fopen(output_file, "w");
959 fprintf(outfp, "#include \"pmu-events/pmu-events.h\"\n");
960 print_mapping_table_prefix(outfp);
961 print_mapping_table_suffix(outfp);
962 print_system_event_mapping_table_prefix(outfp);
963 print_system_event_mapping_table_suffix(outfp);
967 static int get_maxfds(void)
971 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
972 return min(rlim.rlim_max / 2, (rlim_t)512);
978 * nftw() doesn't let us pass an argument to the processing function,
979 * so use a global variables.
981 static FILE *eventsfp;
982 static char *mapfile;
984 static int is_leaf_dir(const char *fpath)
994 while ((dir = readdir(d)) != NULL) {
995 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
998 if (dir->d_type == DT_DIR) {
1001 } else if (dir->d_type == DT_UNKNOWN) {
1002 char path[PATH_MAX];
1005 sprintf(path, "%s/%s", fpath, dir->d_name);
1006 if (stat(path, &st))
1009 if (S_ISDIR(st.st_mode)) {
1021 static int is_json_file(const char *name)
1025 if (strlen(name) < 5)
1028 suffix = name + strlen(name) - 5;
1030 if (strncmp(suffix, ".json", 5) == 0)
1035 static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
1036 int typeflag, struct FTW *ftwbuf)
1038 int level = ftwbuf->level;
1039 int is_file = typeflag == FTW_F;
1041 if (level == 1 && is_file && is_json_file(fpath))
1042 return json_events(fpath, save_arch_std_events, (void *)sb);
1047 static int process_one_file(const char *fpath, const struct stat *sb,
1048 int typeflag, struct FTW *ftwbuf)
1050 char *tblname, *bname;
1051 int is_dir = typeflag == FTW_D;
1052 int is_file = typeflag == FTW_F;
1053 int level = ftwbuf->level;
1056 if (level >= 2 && is_dir) {
1059 * For level 2 directory, bname will include parent name,
1060 * like vendor/platform. So search back from platform dir
1062 * Something similar for level 3 directory, but we're a PMU
1063 * category folder, like vendor/platform/cpu.
1065 bname = (char *) fpath + ftwbuf->base - 2;
1069 if (count == level - 1)
1075 bname = (char *) fpath + ftwbuf->base;
1077 pr_debug("%s %d %7jd %-20s %s\n",
1078 is_file ? "f" : is_dir ? "d" : "x",
1079 level, sb->st_size, bname, fpath);
1081 /* base dir or too deep */
1082 if (level == 0 || level > 4)
1086 /* model directory, reset topic */
1087 if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
1088 (level >= 2 && is_dir && is_leaf_dir(fpath))) {
1090 print_events_table_suffix(eventsfp);
1093 * Drop file name suffix. Replace hyphens with underscores.
1094 * Fail if file name contains any alphanum characters besides
1097 tblname = file_name_to_table_name(bname);
1099 pr_info("%s: Error determining table name for %s\n", prog,
1104 if (is_sys_dir(bname)) {
1105 struct sys_event_table *sys_event_table;
1107 sys_event_table = malloc(sizeof(*sys_event_table));
1108 if (!sys_event_table)
1111 sys_event_table->soc_id = strdup(tblname);
1112 if (!sys_event_table->soc_id) {
1113 free(sys_event_table);
1116 list_add_tail(&sys_event_table->list,
1120 print_events_table_prefix(eventsfp, tblname);
1125 * Save the mapfile name for now. We will process mapfile
1126 * after processing all JSON files (so we can write out the
1127 * mapping table after all PMU events tables).
1130 if (level == 1 && is_file) {
1131 if (!strcmp(bname, "mapfile.csv")) {
1132 mapfile = strdup(fpath);
1135 if (is_json_file(bname))
1136 pr_debug("%s: ArchStd json is preprocessed %s\n", prog, fpath);
1138 pr_info("%s: Ignoring file %s\n", prog, fpath);
1143 * If the file name does not have a .json extension,
1144 * ignore it. It could be a readme.txt for instance.
1147 if (!is_json_file(bname)) {
1148 pr_info("%s: Ignoring file without .json suffix %s\n", prog,
1154 if (level > 1 && add_topic(bname))
1158 * Assume all other files are JSON files.
1160 * If mapfile refers to 'power7_core.json', we create a table
1161 * named 'power7_core'. Any inconsistencies between the mapfile
1162 * and directory tree could result in build failure due to table
1163 * names not being found.
1165 * At least for now, be strict with processing JSON file names.
1166 * i.e. if JSON file name cannot be mapped to C-style table name,
1170 struct perf_entry_data data = {
1171 .topic = get_topic(),
1175 err = json_events(fpath, print_events_table_entry, &data);
1184 #define PATH_MAX 4096
1188 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1189 * the set of JSON files for the architecture 'arch'.
1191 * From each JSON file, create a C-style "PMU events table" from the
1192 * JSON file (see struct pmu_event).
1194 * From the mapfile, create a mapping between the CPU revisions and
1195 * PMU event tables (see struct pmu_events_map).
1197 * Write out the PMU events tables and the mapping table to pmu-event.c.
1199 int main(int argc, char *argv[])
1201 int rc, ret = 0, empty_map = 0;
1203 char ldirname[PATH_MAX];
1205 const char *output_file;
1206 const char *start_dirname;
1207 const char *err_string_ext = "";
1210 prog = basename(argv[0]);
1212 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
1217 start_dirname = argv[2];
1218 output_file = argv[3];
1221 verbose = atoi(argv[4]);
1223 eventsfp = fopen(output_file, "w");
1225 pr_err("%s Unable to create required file %s (%s)\n",
1226 prog, output_file, strerror(errno));
1230 sprintf(ldirname, "%s/%s", start_dirname, arch);
1232 /* If architecture does not have any event lists, bail out */
1233 if (stat(ldirname, &stbuf) < 0) {
1234 pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
1236 goto err_close_eventsfp;
1239 /* Include pmu-events.h first */
1240 fprintf(eventsfp, "#include \"pmu-events/pmu-events.h\"\n");
1243 * The mapfile allows multiple CPUids to point to the same JSON file,
1244 * so, not sure if there is a need for symlinks within the pmu-events
1247 * For now, treat symlinks of JSON files as regular files and create
1248 * separate tables for each symlink (presumably, each symlink refers
1249 * to specific version of the CPU).
1252 maxfds = get_maxfds();
1253 rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1255 goto err_processing_std_arch_event_dir;
1257 rc = nftw(ldirname, process_one_file, maxfds, 0);
1259 goto err_processing_dir;
1261 sprintf(ldirname, "%s/test", start_dirname);
1263 rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1265 goto err_processing_std_arch_event_dir;
1267 rc = nftw(ldirname, process_one_file, maxfds, 0);
1269 goto err_processing_dir;
1272 print_events_table_suffix(eventsfp);
1275 pr_info("%s: No CPU->JSON mapping?\n", prog);
1277 goto err_close_eventsfp;
1280 rc = process_mapfile(eventsfp, mapfile);
1282 pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
1283 /* Make build fail */
1285 goto err_close_eventsfp;
1288 rc = process_system_event_tables(eventsfp);
1295 free_arch_std_events();
1296 free_sys_event_tables();
1300 err_processing_std_arch_event_dir:
1301 err_string_ext = " for std arch event";
1304 pr_info("%s: Error walking file tree %s%s\n", prog, ldirname,
1307 } else if (rc < 0) {
1315 create_empty_mapping(output_file);
1317 free_arch_std_events();
1318 free_sys_event_tables();