1 /* Parse event JSON files */
4 * Copyright (c) 2014, Intel Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/param.h>
32 #include <sys/resource.h> /* getrlimit */
34 #include <sys/time.h> /* getrlimit */
50 #include "pmu-events.h"
53 nftw_ordered(const char *path, int (*fn)(const char *, const struct stat *, int,
54 struct FTW *), int nfds, int ftwflags);
55 #define nftw nftw_ordered
57 _Noreturn void _Exit(int);
58 char *get_cpu_str(void);
77 char *metric_constraint;
80 static enum aggr_mode_class convert(const char *aggr_mode)
82 if (!strcmp(aggr_mode, "PerCore"))
84 else if (!strcmp(aggr_mode, "PerChip"))
87 pr_err("%s: Wrong AggregationMode value '%s'\n", prog, aggr_mode);
91 static LIST_HEAD(sys_event_tables);
93 struct sys_event_table {
94 struct list_head list;
98 static void free_sys_event_tables(void)
100 struct sys_event_table *et, *next;
102 list_for_each_entry_safe(et, next, &sys_event_tables, list) {
108 int eprintf(int level, int var, const char *fmt, ...)
119 ret = vfprintf(stderr, fmt, args);
126 static void addfield(char *map, char **dst, const char *sep,
127 const char *a, jsmntok_t *bt)
129 unsigned int len = strlen(a) + 1 + strlen(sep);
130 int olen = *dst ? strlen(*dst) : 0;
131 int blen = bt ? json_len(bt) : 0;
134 out = realloc(*dst, len + olen + blen);
136 /* Don't add field in this case */
147 strncat(*dst, map + bt->start, blen);
150 static void fixname(char *s)
156 static void fixdesc(char *s)
158 char *e = s + strlen(s);
160 /* Remove trailing dots that look ugly in perf list */
162 while (e >= s && isspace(*e))
164 if (e >= s && *e == '.')
168 /* Add escapes for '\' so they are proper C strings. */
169 static char *fixregex(char *s)
176 /* Count the number of '\' in string */
177 for (p = s; *p; p++) {
186 /* allocate space for a new string */
187 fixed = (char *) malloc(len + esc_count + 1);
191 /* copy over the characters */
193 for (p = s; *p; p++) {
205 static struct msrmap {
209 { "0x3F6", "ldlat=" },
210 { "0x1A6", "offcore_rsp=" },
211 { "0x1A7", "offcore_rsp=" },
212 { "0x3F7", "frontend=" },
216 static void cut_comma(char *map, jsmntok_t *newval)
220 /* Cut off everything after comma */
221 for (i = newval->start; i < newval->end; i++) {
227 static struct msrmap *lookup_msr(char *map, jsmntok_t *val)
229 jsmntok_t newval = *val;
233 cut_comma(map, &newval);
234 for (i = 0; msrmap[i].num; i++)
235 if (json_streq(map, &newval, msrmap[i].num))
239 pr_err("%s: Unknown MSR in event file %.*s\n", prog,
240 json_len(val), map + val->start);
249 { "CBO", "uncore_cbox" },
250 { "QPI LL", "uncore_qpi" },
251 { "SBO", "uncore_sbox" },
252 { "iMPH-U", "uncore_arb" },
253 { "CPU-M-CF", "cpum_cf" },
254 { "CPU-M-SF", "cpum_sf" },
255 { "UPI LL", "uncore_upi" },
256 { "hisi_sicl,cpa", "hisi_sicl,cpa"},
257 { "hisi_sccl,ddrc", "hisi_sccl,ddrc" },
258 { "hisi_sccl,hha", "hisi_sccl,hha" },
259 { "hisi_sccl,l3c", "hisi_sccl,l3c" },
260 /* it's not realistic to keep adding these, we need something more scalable ... */
261 { "imx8_ddr", "imx8_ddr" },
262 { "L3PMC", "amd_l3" },
263 { "DFPMC", "amd_df" },
264 { "cpu_core", "cpu_core" },
265 { "cpu_atom", "cpu_atom" },
269 static const char *field_to_perf(struct map *table, char *map, jsmntok_t *val)
273 for (i = 0; table[i].json; i++) {
274 if (json_streq(map, val, table[i].json))
275 return table[i].perf;
280 #define EXPECT(e, t, m) do { if (!(e)) { \
281 jsmntok_t *loc = (t); \
282 if (!(t)->start && (t) > tokens) \
284 pr_err("%s:%d: " m ", got %s\n", fn, \
285 json_line(map, loc), \
293 static char *get_topic(void)
298 /* tp is free'd in process_one_file() */
299 i = asprintf(&tp, "%s", topic);
301 pr_info("%s: asprintf() error %s\n", prog);
305 for (i = 0; i < (int) strlen(tp); i++) {
319 static int add_topic(char *bname)
322 topic = strdup(bname);
324 pr_info("%s: strdup() error %s for file %s\n", prog,
325 strerror(errno), bname);
331 struct perf_entry_data {
336 static int close_table;
338 static void print_events_table_prefix(FILE *fp, const char *tblname)
340 fprintf(fp, "static const struct pmu_event %s[] = {\n", tblname);
344 static int print_events_table_entry(void *data, struct json_event *je)
346 struct perf_entry_data *pd = data;
347 FILE *outfp = pd->outfp;
348 char *topic_local = pd->topic;
351 * TODO: Remove formatting chars after debugging to reduce
354 fprintf(outfp, "{\n");
357 fprintf(outfp, "\t.name = \"%s\",\n", je->name);
359 fprintf(outfp, "\t.event = \"%s\",\n", je->event);
360 fprintf(outfp, "\t.desc = \"%s\",\n", je->desc);
362 fprintf(outfp, "\t.compat = \"%s\",\n", je->compat);
363 fprintf(outfp, "\t.topic = \"%s\",\n", topic_local);
364 if (je->long_desc && je->long_desc[0])
365 fprintf(outfp, "\t.long_desc = \"%s\",\n", je->long_desc);
367 fprintf(outfp, "\t.pmu = \"%s\",\n", je->pmu);
369 fprintf(outfp, "\t.unit = \"%s\",\n", je->unit);
371 fprintf(outfp, "\t.perpkg = \"%s\",\n", je->perpkg);
373 fprintf(outfp, "\t.aggr_mode = \"%d\",\n", convert(je->aggr_mode));
375 fprintf(outfp, "\t.metric_expr = \"%s\",\n", je->metric_expr);
377 fprintf(outfp, "\t.metric_name = \"%s\",\n", je->metric_name);
378 if (je->metric_group)
379 fprintf(outfp, "\t.metric_group = \"%s\",\n", je->metric_group);
381 fprintf(outfp, "\t.deprecated = \"%s\",\n", je->deprecated);
382 if (je->metric_constraint)
383 fprintf(outfp, "\t.metric_constraint = \"%s\",\n", je->metric_constraint);
384 fprintf(outfp, "},\n");
389 struct event_struct {
390 struct list_head list;
404 char *metric_constraint;
407 #define ADD_EVENT_FIELD(field) do { if (je->field) { \
408 es->field = strdup(je->field); \
413 #define FREE_EVENT_FIELD(field) free(es->field)
415 #define TRY_FIXUP_FIELD(field) do { if (es->field && !je->field) {\
416 je->field = strdup(es->field); \
421 #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
436 static LIST_HEAD(arch_std_events);
438 static void free_arch_std_events(void)
440 struct event_struct *es, *next;
442 list_for_each_entry_safe(es, next, &arch_std_events, list) {
443 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
444 list_del_init(&es->list);
449 static int save_arch_std_events(void *data __unused, struct json_event *je)
451 struct event_struct *es;
453 es = malloc(sizeof(*es));
456 memset(es, 0, sizeof(*es));
457 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD);
458 list_add_tail(&es->list, &arch_std_events);
461 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
466 static void print_events_table_suffix(FILE *outfp)
468 fprintf(outfp, "{\n");
470 fprintf(outfp, "\t.name = 0,\n");
471 fprintf(outfp, "\t.event = 0,\n");
472 fprintf(outfp, "\t.desc = 0,\n");
474 fprintf(outfp, "},\n");
475 fprintf(outfp, "};\n");
479 static struct fixed {
484 { "inst_retired.any", "event=0xc0,period=2000003" },
485 { "inst_retired.any_p", "event=0xc0,period=2000003" },
486 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03,period=2000003" },
487 { "cpu_clk_unhalted.thread", "event=0x3c,period=2000003" },
488 { "cpu_clk_unhalted.core", "event=0x3c,period=2000003" },
489 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1,period=2000003" },
495 * Handle different fixed counter encodings between JSON and perf.
497 static char *real_event(const char *name, char *event)
504 for (i = 0; fixed[i].name; i++)
505 if (!strcasecmp(name, fixed[i].name))
506 return (char *)fixed[i].event;
511 try_fixup(const char *fn, char *arch_std, struct json_event *je, char **event)
513 /* try to find matching event from arch standard values */
514 struct event_struct *es;
516 list_for_each_entry(es, &arch_std_events, list) {
517 if (!strcmp(arch_std, es->name)) {
518 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
524 pr_err("%s: could not find matching %s for %s\n",
529 /* Call func with each event in the json file */
530 static int json_events(const char *fn,
531 int (*func)(void *data, struct json_event *je),
536 jsmntok_t *tokens, *tok;
544 tokens = parse_json(fn, &map, &size, &len);
547 EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
549 for (i = 0; i < tokens->size; i++) {
551 char *extra_desc = NULL;
553 struct json_event je = {};
554 char *arch_std = NULL;
555 unsigned long long eventcode = 0;
556 unsigned long long configcode = 0;
557 struct msrmap *msr = NULL;
558 jsmntok_t *msrval = NULL;
559 jsmntok_t *precise = NULL;
560 jsmntok_t *obj = tok++;
561 bool configcode_present = false;
568 char *fc_mask = NULL;
569 char *ch_mask = NULL;
571 EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
572 for (j = 0; j < obj->size; j += 2) {
573 jsmntok_t *field, *val;
578 EXPECT(field->type == JSMN_STRING, tok + j,
579 "Expected field name");
581 EXPECT(val->type == JSMN_STRING, tok + j + 1,
582 "Expected string value");
584 nz = !json_streq(map, val, "0");
586 if (json_streq(map, field, "UMask") && nz) {
587 addfield(map, &umask, "", "umask=", val);
588 } else if (json_streq(map, field, "CounterMask") && nz) {
589 addfield(map, &cmask, "", "cmask=", val);
590 } else if (json_streq(map, field, "Invert") && nz) {
591 addfield(map, &inv, "", "inv=", val);
592 } else if (json_streq(map, field, "AnyThread") && nz) {
593 addfield(map, &any, "", "any=", val);
594 } else if (json_streq(map, field, "EdgeDetect") && nz) {
595 addfield(map, &edge, "", "edge=", val);
596 } else if (json_streq(map, field, "SampleAfterValue") && nz) {
597 addfield(map, &period, "", "period=", val);
598 } else if (json_streq(map, field, "FCMask") && nz) {
599 addfield(map, &fc_mask, "", "fc_mask=", val);
600 } else if (json_streq(map, field, "PortMask") && nz) {
601 addfield(map, &ch_mask, "", "ch_mask=", val);
602 } else if (json_streq(map, field, "EventCode")) {
604 addfield(map, &code, "", "", val);
605 eventcode |= strtoul(code, NULL, 0);
607 } else if (json_streq(map, field, "ConfigCode")) {
609 addfield(map, &code, "", "", val);
610 configcode |= strtoul(code, NULL, 0);
612 configcode_present = true;
613 } else if (json_streq(map, field, "ExtSel")) {
615 addfield(map, &code, "", "", val);
616 eventcode |= strtoul(code, NULL, 0) << 8;
618 } else if (json_streq(map, field, "EventName")) {
619 addfield(map, &je.name, "", "", val);
620 } else if (json_streq(map, field, "Compat")) {
621 addfield(map, &je.compat, "", "", val);
622 } else if (json_streq(map, field, "BriefDescription")) {
623 addfield(map, &je.desc, "", "", val);
625 } else if (json_streq(map, field,
626 "PublicDescription")) {
627 addfield(map, &je.long_desc, "", "", val);
628 fixdesc(je.long_desc);
629 } else if (json_streq(map, field, "PEBS") && nz) {
631 } else if (json_streq(map, field, "MSRIndex") && nz) {
632 msr = lookup_msr(map, val);
633 } else if (json_streq(map, field, "MSRValue")) {
635 } else if (json_streq(map, field, "Errata") &&
636 !json_streq(map, val, "null")) {
637 addfield(map, &extra_desc, ". ",
638 " Spec update: ", val);
639 } else if (json_streq(map, field, "Data_LA") && nz) {
640 addfield(map, &extra_desc, ". ",
641 " Supports address when precise",
643 } else if (json_streq(map, field, "Unit")) {
646 ppmu = field_to_perf(unit_to_pmu, map, val);
648 je.pmu = strdup(ppmu);
651 je.pmu = strdup("uncore_");
652 addfield(map, &je.pmu, "", "", val);
653 for (s = je.pmu; *s; s++)
656 } else if (json_streq(map, field, "Filter")) {
657 addfield(map, &filter, "", "", val);
658 } else if (json_streq(map, field, "ScaleUnit")) {
659 addfield(map, &je.unit, "", "", val);
660 } else if (json_streq(map, field, "PerPkg")) {
661 addfield(map, &je.perpkg, "", "", val);
662 } else if (json_streq(map, field, "AggregationMode")) {
663 addfield(map, &je.aggr_mode, "", "", val);
664 } else if (json_streq(map, field, "Deprecated")) {
665 addfield(map, &je.deprecated, "", "", val);
666 } else if (json_streq(map, field, "MetricName")) {
667 addfield(map, &je.metric_name, "", "", val);
668 } else if (json_streq(map, field, "MetricGroup")) {
669 addfield(map, &je.metric_group, "", "", val);
670 } else if (json_streq(map, field, "MetricConstraint")) {
671 addfield(map, &je.metric_constraint, "", "", val);
672 } else if (json_streq(map, field, "MetricExpr")) {
673 addfield(map, &je.metric_expr, "", "", val);
674 } else if (json_streq(map, field, "ArchStdEvent")) {
675 addfield(map, &arch_std, "", "", val);
676 for (s = arch_std; *s; s++)
679 /* ignore unknown fields */
681 if (precise && je.desc && !strstr(je.desc, "(Precise Event)")) {
682 if (json_streq(map, precise, "2"))
683 addfield(map, &extra_desc, " ",
684 "(Must be precise)", NULL);
686 addfield(map, &extra_desc, " ",
687 "(Precise event)", NULL);
689 if (configcode_present)
690 snprintf(buf, sizeof buf, "config=%#llx", configcode);
692 snprintf(buf, sizeof buf, "event=%#llx", eventcode);
693 addfield(map, &event, ",", buf, NULL);
695 addfield(map, &event, ",", any, NULL);
697 addfield(map, &event, ",", ch_mask, NULL);
699 addfield(map, &event, ",", cmask, NULL);
701 addfield(map, &event, ",", edge, NULL);
703 addfield(map, &event, ",", fc_mask, NULL);
705 addfield(map, &event, ",", inv, NULL);
707 addfield(map, &event, ",", period, NULL);
709 addfield(map, &event, ",", umask, NULL);
711 if (je.desc && extra_desc)
712 addfield(map, &je.desc, " ", extra_desc, NULL);
713 if (je.long_desc && extra_desc)
714 addfield(map, &je.long_desc, " ", extra_desc, NULL);
716 addfield(map, &je.desc, ". ", "Unit: ", NULL);
717 addfield(map, &je.desc, "", je.pmu, NULL);
718 addfield(map, &je.desc, "", " ", NULL);
721 addfield(map, &event, ",", filter, NULL);
723 addfield(map, &event, ",", msr->pname, msrval);
729 * An arch standard event is referenced, so try to
730 * fixup any unassigned values.
732 err = try_fixup(fn, arch_std, &je, &event);
736 je.event = real_event(je.name, event);
737 err = func(data, &je);
759 free(je.metric_expr);
760 free(je.metric_name);
761 free(je.metric_group);
762 free(je.metric_constraint);
769 EXPECT(tok - tokens == len, tok, "unexpected objects at end");
772 free_json(map, size, tokens);
776 static char *file_name_to_table_name(char *fname)
784 * Ensure tablename starts with alphabetic character.
785 * Derive rest of table name from basename of the JSON file,
786 * replacing hyphens and stripping out .json suffix.
788 n = asprintf(&tblname, "pme_%s", fname);
790 pr_info("%s: asprintf() error %s for file %s\n", prog,
791 strerror(errno), fname);
795 for (i = 0; i < strlen(tblname); i++) {
798 if (c == '-' || c == '/')
803 } else if (!isalnum(c) && c != '_') {
804 pr_err("%s: Invalid character '%c' in file name '%s'\n",
815 static bool is_sys_dir(char *fname)
817 size_t len = strlen(fname), len2 = strlen("/sys");
821 return !strcmp(fname+len-len2, "/sys");
824 static void print_mapping_table_prefix(FILE *outfp)
826 fprintf(outfp, "const struct pmu_events_map pmu_events_map[] = {\n");
829 static void print_mapping_table_suffix(FILE *outfp)
832 * Print the terminating, NULL entry.
834 fprintf(outfp, "{\n");
835 fprintf(outfp, "\t.cpuid = 0,\n");
836 fprintf(outfp, "\t.version = 0,\n");
837 fprintf(outfp, "\t.type = 0,\n");
838 fprintf(outfp, "\t.table = 0,\n");
839 fprintf(outfp, "},\n");
841 /* and finally, the closing curly bracket for the struct */
842 fprintf(outfp, "};\n");
845 static void print_mapping_test_table(FILE *outfp)
848 * Print the terminating, NULL entry.
850 fprintf(outfp, "{\n");
851 fprintf(outfp, "\t.cpuid = \"testcpu\",\n");
852 fprintf(outfp, "\t.version = \"v1\",\n");
853 fprintf(outfp, "\t.type = \"core\",\n");
854 fprintf(outfp, "\t.table = pme_test_soc_cpu,\n");
855 fprintf(outfp, "},\n");
858 static void print_system_event_mapping_table_prefix(FILE *outfp)
860 fprintf(outfp, "\nconst struct pmu_sys_events pmu_sys_event_tables[] = {");
863 static void print_system_event_mapping_table_suffix(FILE *outfp)
865 fprintf(outfp, "\n\t{\n\t\t.table = 0\n\t},");
866 fprintf(outfp, "\n};\n");
869 static int process_system_event_tables(FILE *outfp)
871 struct sys_event_table *sys_event_table;
873 print_system_event_mapping_table_prefix(outfp);
875 list_for_each_entry(sys_event_table, &sys_event_tables, list) {
876 fprintf(outfp, "\n\t{\n\t\t.table = %s,\n\t\t.name = \"%s\",\n\t},",
877 sys_event_table->soc_id,
878 sys_event_table->soc_id);
881 print_system_event_mapping_table_suffix(outfp);
886 static int process_mapfile(FILE *outfp, char *fpath)
896 pr_info("%s: Processing mapfile %s\n", prog, fpath);
902 mapfp = fopen(fpath, "r");
904 pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
910 print_mapping_table_prefix(outfp);
912 /* Skip first line (header) */
913 p = fgets(line, n, mapfp);
919 char *cpuid, *version, *type, *fname;
922 p = fgets(line, n, mapfp);
926 if (line[0] == '#' || line[0] == '\n')
929 if (line[strlen(line)-1] != '\n') {
930 /* TODO Deal with lines longer than 16K */
931 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
932 prog, fpath, line_num);
936 line[strlen(line)-1] = '\0';
938 cpuid = fixregex(strtok_r(p, ",", &save));
939 version = strtok_r(NULL, ",", &save);
940 fname = strtok_r(NULL, ",", &save);
941 type = strtok_r(NULL, ",", &save);
943 tblname = file_name_to_table_name(fname);
944 fprintf(outfp, "{\n");
945 fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
946 fprintf(outfp, "\t.version = \"%s\",\n", version);
947 fprintf(outfp, "\t.type = \"%s\",\n", type);
950 * CHECK: We can't use the type (eg "core") field in the
951 * table name. For us to do that, we need to somehow tweak
952 * the other caller of file_name_to_table(), process_json()
953 * to determine the type. process_json() file has no way
954 * of knowing these are "core" events unless file name has
955 * core in it. If filename has core in it, we can safely
956 * ignore the type field here also.
958 fprintf(outfp, "\t.table = %s\n", tblname);
959 fprintf(outfp, "},\n");
963 print_mapping_test_table(outfp);
964 print_mapping_table_suffix(outfp);
971 * If we fail to locate/process JSON and map files, create a NULL mapping
972 * table. This would at least allow perf to build even if we can't find/use
975 static void create_empty_mapping(const char *output_file)
979 pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
981 /* Truncate file to clear any partial writes to it */
982 outfp = fopen(output_file, "w");
988 fprintf(outfp, "#include \"pmu-events/pmu-events.h\"\n");
989 print_mapping_table_prefix(outfp);
990 print_mapping_table_suffix(outfp);
991 print_system_event_mapping_table_prefix(outfp);
992 print_system_event_mapping_table_suffix(outfp);
996 static int get_maxfds(void)
1000 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
1001 if (rlim.rlim_max == RLIM_INFINITY)
1003 return MIN(rlim.rlim_max / 2, 512);
1010 * nftw() doesn't let us pass an argument to the processing function,
1011 * so use a global variables.
1013 static FILE *eventsfp;
1014 static char *mapfile;
1016 static int is_leaf_dir(const char *fpath)
1026 while ((dir = readdir(d)) != NULL) {
1027 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1030 if (dir->d_type == DT_DIR) {
1033 } else if (dir->d_type == DT_UNKNOWN) {
1034 char path[PATH_MAX];
1037 snprintf(path, sizeof(path), "%s/%s", fpath, dir->d_name);
1038 if (stat(path, &st))
1041 if (S_ISDIR(st.st_mode)) {
1053 static int is_json_file(const char *name)
1057 if (strlen(name) < 5)
1060 suffix = name + strlen(name) - 5;
1062 if (strncmp(suffix, ".json", 5) == 0)
1067 static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
1068 int typeflag, struct FTW *ftwbuf)
1070 int level = ftwbuf->level;
1071 int is_file = typeflag == FTW_F;
1073 if (level == 1 && is_file && is_json_file(fpath))
1074 return json_events(fpath, save_arch_std_events, (void *)(uintptr_t)sb);
1079 static int process_one_file(const char *fpath, const struct stat *sb,
1080 int typeflag, struct FTW *ftwbuf)
1082 char *tblname, *bname;
1083 int is_dir = typeflag == FTW_D;
1084 int is_file = typeflag == FTW_F;
1085 int level = ftwbuf->level;
1088 if (level >= 2 && is_dir) {
1091 * For level 2 directory, bname will include parent name,
1092 * like vendor/platform. So search back from platform dir
1094 * Something similar for level 3 directory, but we're a PMU
1095 * category folder, like vendor/platform/cpu.
1097 bname = (char *) fpath + ftwbuf->base - 2;
1101 if (count == level - 1)
1107 bname = (char *) fpath + ftwbuf->base;
1109 pr_debug("%s %d %7jd %-20s %s\n",
1110 is_file ? "f" : is_dir ? "d" : "x",
1111 level, sb->st_size, bname, fpath);
1113 /* base dir or too deep */
1114 if (level == 0 || level > 4)
1118 /* model directory, reset topic */
1119 if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
1120 (level >= 2 && is_dir && is_leaf_dir(fpath))) {
1122 print_events_table_suffix(eventsfp);
1125 * Drop file name suffix. Replace hyphens with underscores.
1126 * Fail if file name contains any alphanum characters besides
1129 tblname = file_name_to_table_name(bname);
1131 pr_info("%s: Error determining table name for %s\n", prog,
1136 if (is_sys_dir(bname)) {
1137 struct sys_event_table *sys_event_table;
1139 sys_event_table = malloc(sizeof(*sys_event_table));
1140 if (!sys_event_table)
1143 sys_event_table->soc_id = strdup(tblname);
1144 if (!sys_event_table->soc_id) {
1145 free(sys_event_table);
1148 list_add_tail(&sys_event_table->list,
1152 print_events_table_prefix(eventsfp, tblname);
1157 * Save the mapfile name for now. We will process mapfile
1158 * after processing all JSON files (so we can write out the
1159 * mapping table after all PMU events tables).
1162 if (level == 1 && is_file) {
1163 if (!strcmp(bname, "mapfile.csv")) {
1164 mapfile = strdup(fpath);
1167 if (is_json_file(bname))
1168 pr_debug("%s: ArchStd json is preprocessed %s\n", prog, fpath);
1170 pr_info("%s: Ignoring file %s\n", prog, fpath);
1175 * If the file name does not have a .json extension,
1176 * ignore it. It could be a readme.txt for instance.
1179 if (!is_json_file(bname)) {
1180 pr_info("%s: Ignoring file without .json suffix %s\n", prog,
1186 if (level > 1 && add_topic(bname))
1190 * Assume all other files are JSON files.
1192 * If mapfile refers to 'power7_core.json', we create a table
1193 * named 'power7_core'. Any inconsistencies between the mapfile
1194 * and directory tree could result in build failure due to table
1195 * names not being found.
1197 * At least for now, be strict with processing JSON file names.
1198 * i.e. if JSON file name cannot be mapped to C-style table name,
1202 struct perf_entry_data data = {
1203 .topic = get_topic(),
1207 err = json_events(fpath, print_events_table_entry, &data);
1216 #define PATH_MAX 4096
1220 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1221 * the set of JSON files for the architecture 'arch'.
1223 * From each JSON file, create a C-style "PMU events table" from the
1224 * JSON file (see struct pmu_event).
1226 * From the mapfile, create a mapping between the CPU revisions and
1227 * PMU event tables (see struct pmu_events_map).
1229 * Write out the PMU events tables and the mapping table to pmu-event.c.
1231 int main(int argc, char *argv[])
1233 int rc, ret = 0, empty_map = 0;
1235 char ldirname[PATH_MAX];
1237 const char *output_file;
1238 const char *start_dirname;
1239 const char *err_string_ext = "";
1242 prog = basename(argv[0]);
1244 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
1249 start_dirname = argv[2];
1250 output_file = argv[3];
1253 verbose = atoi(argv[4]);
1255 eventsfp = fopen(output_file, "w");
1257 pr_err("%s Unable to create required file %s (%s)\n",
1258 prog, output_file, strerror(errno));
1262 snprintf(ldirname, sizeof(ldirname), "%s/%s", start_dirname, arch);
1264 /* If architecture does not have any event lists, bail out */
1265 if (stat(ldirname, &stbuf) < 0) {
1266 pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
1268 goto err_close_eventsfp;
1271 /* Include pmu-events.h first */
1272 fprintf(eventsfp, "#include \"pmu-events/pmu-events.h\"\n");
1275 * The mapfile allows multiple CPUids to point to the same JSON file,
1276 * so, not sure if there is a need for symlinks within the pmu-events
1279 * For now, treat symlinks of JSON files as regular files and create
1280 * separate tables for each symlink (presumably, each symlink refers
1281 * to specific version of the CPU).
1284 maxfds = get_maxfds();
1285 rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1287 goto err_processing_std_arch_event_dir;
1289 rc = nftw(ldirname, process_one_file, maxfds, 0);
1291 goto err_processing_dir;
1293 sprintf(ldirname, "%s/test", start_dirname);
1295 rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1297 goto err_processing_std_arch_event_dir;
1299 rc = nftw(ldirname, process_one_file, maxfds, 0);
1301 goto err_processing_dir;
1304 print_events_table_suffix(eventsfp);
1307 pr_info("%s: No CPU->JSON mapping?\n", prog);
1309 goto err_close_eventsfp;
1312 rc = process_mapfile(eventsfp, mapfile);
1314 pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
1315 /* Make build fail */
1317 goto err_close_eventsfp;
1320 rc = process_system_event_tables(eventsfp);
1327 free_arch_std_events();
1328 free_sys_event_tables();
1332 err_processing_std_arch_event_dir:
1333 err_string_ext = " for std arch event";
1336 pr_info("%s: Error walking file tree %s%s\n", prog, ldirname,
1339 } else if (rc < 0) {
1347 create_empty_mapping(output_file);
1349 free_arch_std_events();
1350 free_sys_event_tables();
1358 fts_compare(const FTSENT * const *a, const FTSENT * const *b)
1360 return (strcmp((*a)->fts_name, (*b)->fts_name));
1364 nftw_ordered(const char *path, int (*fn)(const char *, const struct stat *, int,
1365 struct FTW *), int nfds, int ftwflags)
1367 char * const paths[2] = { (char *)path, NULL };
1371 int error = 0, ftsflags, fnflag, postorder, sverrno;
1373 /* XXX - nfds is currently unused */
1379 ftsflags = FTS_COMFOLLOW;
1380 if (!(ftwflags & FTW_CHDIR))
1381 ftsflags |= FTS_NOCHDIR;
1382 if (ftwflags & FTW_MOUNT)
1383 ftsflags |= FTS_XDEV;
1384 if (ftwflags & FTW_PHYS)
1385 ftsflags |= FTS_PHYSICAL;
1387 ftsflags |= FTS_LOGICAL;
1388 postorder = (ftwflags & FTW_DEPTH) != 0;
1389 ftsp = fts_open(paths, ftsflags, fts_compare);
1392 while ((cur = fts_read(ftsp)) != NULL) {
1393 switch (cur->fts_info) {
1427 ftw.base = cur->fts_pathlen - cur->fts_namelen;
1428 ftw.level = cur->fts_level;
1429 error = fn(cur->fts_path, cur->fts_statp, fnflag, &ftw);
1435 if (fts_close(ftsp) != 0 && error == 0)