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"
69 char *metric_constraint;
72 enum aggr_mode_class convert(const char *aggr_mode)
74 if (!strcmp(aggr_mode, "PerCore"))
76 else if (!strcmp(aggr_mode, "PerChip"))
79 pr_err("%s: Wrong AggregationMode value '%s'\n", prog, aggr_mode);
83 typedef int (*func)(void *data, struct json_event *je);
85 int eprintf(int level, int var, const char *fmt, ...)
96 ret = vfprintf(stderr, fmt, args);
103 static void addfield(char *map, char **dst, const char *sep,
104 const char *a, jsmntok_t *bt)
106 unsigned int len = strlen(a) + 1 + strlen(sep);
107 int olen = *dst ? strlen(*dst) : 0;
108 int blen = bt ? json_len(bt) : 0;
111 out = realloc(*dst, len + olen + blen);
113 /* Don't add field in this case */
124 strncat(*dst, map + bt->start, blen);
127 static void fixname(char *s)
133 static void fixdesc(char *s)
135 char *e = s + strlen(s);
137 /* Remove trailing dots that look ugly in perf list */
139 while (e >= s && isspace(*e))
145 /* Add escapes for '\' so they are proper C strings. */
146 static char *fixregex(char *s)
153 /* Count the number of '\' in string */
154 for (p = s; *p; p++) {
163 /* allocate space for a new string */
164 fixed = (char *) malloc(len + esc_count + 1);
168 /* copy over the characters */
170 for (p = s; *p; p++) {
182 static struct msrmap {
186 { "0x3F6", "ldlat=" },
187 { "0x1A6", "offcore_rsp=" },
188 { "0x1A7", "offcore_rsp=" },
189 { "0x3F7", "frontend=" },
193 static struct field {
197 { "UMask", "umask=" },
198 { "CounterMask", "cmask=" },
199 { "Invert", "inv=" },
200 { "AnyThread", "any=" },
201 { "EdgeDetect", "edge=" },
202 { "SampleAfterValue", "period=" },
203 { "FCMask", "fc_mask=" },
204 { "PortMask", "ch_mask=" },
208 static void cut_comma(char *map, jsmntok_t *newval)
212 /* Cut off everything after comma */
213 for (i = newval->start; i < newval->end; i++) {
219 static int match_field(char *map, jsmntok_t *field, int nz,
220 char **event, jsmntok_t *val)
223 jsmntok_t newval = *val;
225 for (f = fields; f->field; f++)
226 if (json_streq(map, field, f->field) && nz) {
227 cut_comma(map, &newval);
228 addfield(map, event, ",", f->kernel, &newval);
234 static struct msrmap *lookup_msr(char *map, jsmntok_t *val)
236 jsmntok_t newval = *val;
240 cut_comma(map, &newval);
241 for (i = 0; msrmap[i].num; i++)
242 if (json_streq(map, &newval, msrmap[i].num))
246 pr_err("%s: Unknown MSR in event file %.*s\n", prog,
247 json_len(val), map + val->start);
256 { "CBO", "uncore_cbox" },
257 { "QPI LL", "uncore_qpi" },
258 { "SBO", "uncore_sbox" },
259 { "iMPH-U", "uncore_arb" },
260 { "CPU-M-CF", "cpum_cf" },
261 { "CPU-M-SF", "cpum_sf" },
262 { "UPI LL", "uncore_upi" },
263 { "hisi_sccl,ddrc", "hisi_sccl,ddrc" },
264 { "hisi_sccl,hha", "hisi_sccl,hha" },
265 { "hisi_sccl,l3c", "hisi_sccl,l3c" },
266 { "L3PMC", "amd_l3" },
267 { "DFPMC", "amd_df" },
271 static const char *field_to_perf(struct map *table, char *map, jsmntok_t *val)
275 for (i = 0; table[i].json; i++) {
276 if (json_streq(map, val, table[i].json))
277 return table[i].perf;
282 #define EXPECT(e, t, m) do { if (!(e)) { \
283 jsmntok_t *loc = (t); \
284 if (!(t)->start && (t) > tokens) \
286 pr_err("%s:%d: " m ", got %s\n", fn, \
287 json_line(map, loc), \
295 static char *get_topic(void)
300 /* tp is free'd in process_one_file() */
301 i = asprintf(&tp, "%s", topic);
303 pr_info("%s: asprintf() error %s\n", prog);
307 for (i = 0; i < (int) strlen(tp); i++) {
321 static int add_topic(char *bname)
324 topic = strdup(bname);
326 pr_info("%s: strdup() error %s for file %s\n", prog,
327 strerror(errno), bname);
333 struct perf_entry_data {
338 static int close_table;
340 static void print_events_table_prefix(FILE *fp, const char *tblname)
342 fprintf(fp, "struct pmu_event %s[] = {\n", tblname);
346 static int print_events_table_entry(void *data, struct json_event *je)
348 struct perf_entry_data *pd = data;
349 FILE *outfp = pd->outfp;
350 char *topic = pd->topic;
353 * TODO: Remove formatting chars after debugging to reduce
356 fprintf(outfp, "{\n");
359 fprintf(outfp, "\t.name = \"%s\",\n", je->name);
361 fprintf(outfp, "\t.event = \"%s\",\n", je->event);
362 fprintf(outfp, "\t.desc = \"%s\",\n", je->desc);
363 fprintf(outfp, "\t.topic = \"%s\",\n", topic);
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;
403 char *metric_constraint;
406 #define ADD_EVENT_FIELD(field) do { if (je->field) { \
407 es->field = strdup(je->field); \
412 #define FREE_EVENT_FIELD(field) free(es->field)
414 #define TRY_FIXUP_FIELD(field) do { if (es->field && !je->field) {\
415 je->field = strdup(es->field); \
420 #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
435 static LIST_HEAD(arch_std_events);
437 static void free_arch_std_events(void)
439 struct event_struct *es, *next;
441 list_for_each_entry_safe(es, next, &arch_std_events, list) {
442 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
443 list_del_init(&es->list);
448 static int save_arch_std_events(void *data, struct json_event *je)
450 struct event_struct *es;
452 es = malloc(sizeof(*es));
455 memset(es, 0, sizeof(*es));
456 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD);
457 list_add_tail(&es->list, &arch_std_events);
460 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
465 static void print_events_table_suffix(FILE *outfp)
467 fprintf(outfp, "{\n");
469 fprintf(outfp, "\t.name = 0,\n");
470 fprintf(outfp, "\t.event = 0,\n");
471 fprintf(outfp, "\t.desc = 0,\n");
473 fprintf(outfp, "},\n");
474 fprintf(outfp, "};\n");
478 static struct fixed {
482 { "inst_retired.any", "event=0xc0,period=2000003" },
483 { "inst_retired.any_p", "event=0xc0,period=2000003" },
484 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03,period=2000003" },
485 { "cpu_clk_unhalted.thread", "event=0x3c,period=2000003" },
486 { "cpu_clk_unhalted.core", "event=0x3c,period=2000003" },
487 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1,period=2000003" },
492 * Handle different fixed counter encodings between JSON and perf.
494 static char *real_event(const char *name, char *event)
501 for (i = 0; fixed[i].name; i++)
502 if (!strcasecmp(name, fixed[i].name))
503 return (char *)fixed[i].event;
508 try_fixup(const char *fn, char *arch_std, struct json_event *je, char **event)
510 /* try to find matching event from arch standard values */
511 struct event_struct *es;
513 list_for_each_entry(es, &arch_std_events, list) {
514 if (!strcmp(arch_std, es->name)) {
515 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
521 pr_err("%s: could not find matching %s for %s\n",
526 /* Call func with each event in the json file */
527 static int json_events(const char *fn,
528 int (*func)(void *data, struct json_event *je),
533 jsmntok_t *tokens, *tok;
541 tokens = parse_json(fn, &map, &size, &len);
544 EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
546 for (i = 0; i < tokens->size; i++) {
548 char *extra_desc = NULL;
550 struct json_event je = {};
551 char *arch_std = NULL;
552 unsigned long long eventcode = 0;
553 struct msrmap *msr = NULL;
554 jsmntok_t *msrval = NULL;
555 jsmntok_t *precise = NULL;
556 jsmntok_t *obj = tok++;
558 EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
559 for (j = 0; j < obj->size; j += 2) {
560 jsmntok_t *field, *val;
565 EXPECT(field->type == JSMN_STRING, tok + j,
566 "Expected field name");
568 EXPECT(val->type == JSMN_STRING, tok + j + 1,
569 "Expected string value");
571 nz = !json_streq(map, val, "0");
572 if (match_field(map, field, nz, &event, val)) {
574 } else if (json_streq(map, field, "EventCode")) {
576 addfield(map, &code, "", "", val);
577 eventcode |= strtoul(code, NULL, 0);
579 } else if (json_streq(map, field, "ExtSel")) {
581 addfield(map, &code, "", "", val);
582 eventcode |= strtoul(code, NULL, 0) << 8;
584 } else if (json_streq(map, field, "EventName")) {
585 addfield(map, &je.name, "", "", val);
586 } else if (json_streq(map, field, "BriefDescription")) {
587 addfield(map, &je.desc, "", "", val);
589 } else if (json_streq(map, field,
590 "PublicDescription")) {
591 addfield(map, &je.long_desc, "", "", val);
592 fixdesc(je.long_desc);
593 } else if (json_streq(map, field, "PEBS") && nz) {
595 } else if (json_streq(map, field, "MSRIndex") && nz) {
596 msr = lookup_msr(map, val);
597 } else if (json_streq(map, field, "MSRValue")) {
599 } else if (json_streq(map, field, "Errata") &&
600 !json_streq(map, val, "null")) {
601 addfield(map, &extra_desc, ". ",
602 " Spec update: ", val);
603 } else if (json_streq(map, field, "Data_LA") && nz) {
604 addfield(map, &extra_desc, ". ",
605 " Supports address when precise",
607 } else if (json_streq(map, field, "Unit")) {
610 ppmu = field_to_perf(unit_to_pmu, map, val);
612 je.pmu = strdup(ppmu);
615 je.pmu = strdup("uncore_");
616 addfield(map, &je.pmu, "", "", val);
617 for (s = je.pmu; *s; s++)
620 addfield(map, &je.desc, ". ", "Unit: ", NULL);
621 addfield(map, &je.desc, "", je.pmu, NULL);
622 addfield(map, &je.desc, "", " ", NULL);
623 } else if (json_streq(map, field, "Filter")) {
624 addfield(map, &filter, "", "", val);
625 } else if (json_streq(map, field, "ScaleUnit")) {
626 addfield(map, &je.unit, "", "", val);
627 } else if (json_streq(map, field, "PerPkg")) {
628 addfield(map, &je.perpkg, "", "", val);
629 } else if (json_streq(map, field, "AggregationMode")) {
630 addfield(map, &je.aggr_mode, "", "", val);
631 } else if (json_streq(map, field, "Deprecated")) {
632 addfield(map, &je.deprecated, "", "", val);
633 } else if (json_streq(map, field, "MetricName")) {
634 addfield(map, &je.metric_name, "", "", val);
635 } else if (json_streq(map, field, "MetricGroup")) {
636 addfield(map, &je.metric_group, "", "", val);
637 } else if (json_streq(map, field, "MetricConstraint")) {
638 addfield(map, &je.metric_constraint, "", "", val);
639 } else if (json_streq(map, field, "MetricExpr")) {
640 addfield(map, &je.metric_expr, "", "", val);
641 for (s = je.metric_expr; *s; s++)
643 } else if (json_streq(map, field, "ArchStdEvent")) {
644 addfield(map, &arch_std, "", "", val);
645 for (s = arch_std; *s; s++)
648 /* ignore unknown fields */
650 if (precise && je.desc && !strstr(je.desc, "(Precise Event)")) {
651 if (json_streq(map, precise, "2"))
652 addfield(map, &extra_desc, " ",
653 "(Must be precise)", NULL);
655 addfield(map, &extra_desc, " ",
656 "(Precise event)", NULL);
658 snprintf(buf, sizeof buf, "event=%#llx", eventcode);
659 addfield(map, &event, ",", buf, NULL);
660 if (je.desc && extra_desc)
661 addfield(map, &je.desc, " ", extra_desc, NULL);
662 if (je.long_desc && extra_desc)
663 addfield(map, &je.long_desc, " ", extra_desc, NULL);
665 addfield(map, &event, ",", filter, NULL);
667 addfield(map, &event, ",", msr->pname, msrval);
673 * An arch standard event is referenced, so try to
674 * fixup any unassigned values.
676 err = try_fixup(fn, arch_std, &je, &event);
680 je.event = real_event(je.name, event);
681 err = func(data, &je);
694 free(je.metric_expr);
695 free(je.metric_name);
696 free(je.metric_group);
697 free(je.metric_constraint);
704 EXPECT(tok - tokens == len, tok, "unexpected objects at end");
707 free_json(map, size, tokens);
711 static char *file_name_to_table_name(char *fname)
719 * Ensure tablename starts with alphabetic character.
720 * Derive rest of table name from basename of the JSON file,
721 * replacing hyphens and stripping out .json suffix.
723 n = asprintf(&tblname, "pme_%s", fname);
725 pr_info("%s: asprintf() error %s for file %s\n", prog,
726 strerror(errno), fname);
730 for (i = 0; i < strlen(tblname); i++) {
733 if (c == '-' || c == '/')
738 } else if (!isalnum(c) && c != '_') {
739 pr_err("%s: Invalid character '%c' in file name %s\n",
740 prog, c, basename(fname));
750 static void print_mapping_table_prefix(FILE *outfp)
752 fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n");
755 static void print_mapping_table_suffix(FILE *outfp)
758 * Print the terminating, NULL entry.
760 fprintf(outfp, "{\n");
761 fprintf(outfp, "\t.cpuid = 0,\n");
762 fprintf(outfp, "\t.version = 0,\n");
763 fprintf(outfp, "\t.type = 0,\n");
764 fprintf(outfp, "\t.table = 0,\n");
765 fprintf(outfp, "},\n");
767 /* and finally, the closing curly bracket for the struct */
768 fprintf(outfp, "};\n");
771 static void print_mapping_test_table(FILE *outfp)
774 * Print the terminating, NULL entry.
776 fprintf(outfp, "{\n");
777 fprintf(outfp, "\t.cpuid = \"testcpu\",\n");
778 fprintf(outfp, "\t.version = \"v1\",\n");
779 fprintf(outfp, "\t.type = \"core\",\n");
780 fprintf(outfp, "\t.table = pme_test_cpu,\n");
781 fprintf(outfp, "},\n");
784 static int process_mapfile(FILE *outfp, char *fpath)
794 pr_info("%s: Processing mapfile %s\n", prog, fpath);
800 mapfp = fopen(fpath, "r");
802 pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
808 print_mapping_table_prefix(outfp);
810 /* Skip first line (header) */
811 p = fgets(line, n, mapfp);
817 char *cpuid, *version, *type, *fname;
820 p = fgets(line, n, mapfp);
824 if (line[0] == '#' || line[0] == '\n')
827 if (line[strlen(line)-1] != '\n') {
828 /* TODO Deal with lines longer than 16K */
829 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
830 prog, fpath, line_num);
834 line[strlen(line)-1] = '\0';
836 cpuid = fixregex(strtok_r(p, ",", &save));
837 version = strtok_r(NULL, ",", &save);
838 fname = strtok_r(NULL, ",", &save);
839 type = strtok_r(NULL, ",", &save);
841 tblname = file_name_to_table_name(fname);
842 fprintf(outfp, "{\n");
843 fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
844 fprintf(outfp, "\t.version = \"%s\",\n", version);
845 fprintf(outfp, "\t.type = \"%s\",\n", type);
848 * CHECK: We can't use the type (eg "core") field in the
849 * table name. For us to do that, we need to somehow tweak
850 * the other caller of file_name_to_table(), process_json()
851 * to determine the type. process_json() file has no way
852 * of knowing these are "core" events unless file name has
853 * core in it. If filename has core in it, we can safely
854 * ignore the type field here also.
856 fprintf(outfp, "\t.table = %s\n", tblname);
857 fprintf(outfp, "},\n");
861 print_mapping_test_table(outfp);
862 print_mapping_table_suffix(outfp);
869 * If we fail to locate/process JSON and map files, create a NULL mapping
870 * table. This would at least allow perf to build even if we can't find/use
873 static void create_empty_mapping(const char *output_file)
877 pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
879 /* Truncate file to clear any partial writes to it */
880 outfp = fopen(output_file, "w");
886 fprintf(outfp, "#include \"pmu-events/pmu-events.h\"\n");
887 print_mapping_table_prefix(outfp);
888 print_mapping_table_suffix(outfp);
892 static int get_maxfds(void)
896 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
897 return min(rlim.rlim_max / 2, (rlim_t)512);
903 * nftw() doesn't let us pass an argument to the processing function,
904 * so use a global variables.
906 static FILE *eventsfp;
907 static char *mapfile;
909 static int is_leaf_dir(const char *fpath)
919 while ((dir = readdir(d)) != NULL) {
920 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
923 if (dir->d_type == DT_DIR) {
926 } else if (dir->d_type == DT_UNKNOWN) {
930 sprintf(path, "%s/%s", fpath, dir->d_name);
934 if (S_ISDIR(st.st_mode)) {
946 static int is_json_file(const char *name)
950 if (strlen(name) < 5)
953 suffix = name + strlen(name) - 5;
955 if (strncmp(suffix, ".json", 5) == 0)
960 static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
961 int typeflag, struct FTW *ftwbuf)
963 int level = ftwbuf->level;
964 int is_file = typeflag == FTW_F;
966 if (level == 1 && is_file && is_json_file(fpath))
967 return json_events(fpath, save_arch_std_events, (void *)sb);
972 static int process_one_file(const char *fpath, const struct stat *sb,
973 int typeflag, struct FTW *ftwbuf)
975 char *tblname, *bname;
976 int is_dir = typeflag == FTW_D;
977 int is_file = typeflag == FTW_F;
978 int level = ftwbuf->level;
981 if (level == 2 && is_dir) {
983 * For level 2 directory, bname will include parent name,
984 * like vendor/platform. So search back from platform dir
987 bname = (char *) fpath + ftwbuf->base - 2;
995 bname = (char *) fpath + ftwbuf->base;
997 pr_debug("%s %d %7jd %-20s %s\n",
998 is_file ? "f" : is_dir ? "d" : "x",
999 level, sb->st_size, bname, fpath);
1001 /* base dir or too deep */
1002 if (level == 0 || level > 3)
1006 /* model directory, reset topic */
1007 if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
1008 (level == 2 && is_dir)) {
1010 print_events_table_suffix(eventsfp);
1013 * Drop file name suffix. Replace hyphens with underscores.
1014 * Fail if file name contains any alphanum characters besides
1017 tblname = file_name_to_table_name(bname);
1019 pr_info("%s: Error determining table name for %s\n", prog,
1024 print_events_table_prefix(eventsfp, tblname);
1029 * Save the mapfile name for now. We will process mapfile
1030 * after processing all JSON files (so we can write out the
1031 * mapping table after all PMU events tables).
1034 if (level == 1 && is_file) {
1035 if (!strcmp(bname, "mapfile.csv")) {
1036 mapfile = strdup(fpath);
1040 pr_info("%s: Ignoring file %s\n", prog, fpath);
1045 * If the file name does not have a .json extension,
1046 * ignore it. It could be a readme.txt for instance.
1049 if (!is_json_file(bname)) {
1050 pr_info("%s: Ignoring file without .json suffix %s\n", prog,
1056 if (level > 1 && add_topic(bname))
1060 * Assume all other files are JSON files.
1062 * If mapfile refers to 'power7_core.json', we create a table
1063 * named 'power7_core'. Any inconsistencies between the mapfile
1064 * and directory tree could result in build failure due to table
1065 * names not being found.
1067 * Atleast for now, be strict with processing JSON file names.
1068 * i.e. if JSON file name cannot be mapped to C-style table name,
1072 struct perf_entry_data data = {
1073 .topic = get_topic(),
1077 err = json_events(fpath, print_events_table_entry, &data);
1086 #define PATH_MAX 4096
1090 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1091 * the set of JSON files for the architecture 'arch'.
1093 * From each JSON file, create a C-style "PMU events table" from the
1094 * JSON file (see struct pmu_event).
1096 * From the mapfile, create a mapping between the CPU revisions and
1097 * PMU event tables (see struct pmu_events_map).
1099 * Write out the PMU events tables and the mapping table to pmu-event.c.
1101 int main(int argc, char *argv[])
1103 int rc, ret = 0, empty_map = 0;
1105 char ldirname[PATH_MAX];
1107 const char *output_file;
1108 const char *start_dirname;
1109 char *err_string_ext = "";
1112 prog = basename(argv[0]);
1114 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
1119 start_dirname = argv[2];
1120 output_file = argv[3];
1123 verbose = atoi(argv[4]);
1125 eventsfp = fopen(output_file, "w");
1127 pr_err("%s Unable to create required file %s (%s)\n",
1128 prog, output_file, strerror(errno));
1132 sprintf(ldirname, "%s/%s", start_dirname, arch);
1134 /* If architecture does not have any event lists, bail out */
1135 if (stat(ldirname, &stbuf) < 0) {
1136 pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
1138 goto err_close_eventsfp;
1141 /* Include pmu-events.h first */
1142 fprintf(eventsfp, "#include \"pmu-events/pmu-events.h\"\n");
1145 * The mapfile allows multiple CPUids to point to the same JSON file,
1146 * so, not sure if there is a need for symlinks within the pmu-events
1149 * For now, treat symlinks of JSON files as regular files and create
1150 * separate tables for each symlink (presumably, each symlink refers
1151 * to specific version of the CPU).
1154 maxfds = get_maxfds();
1155 rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1157 goto err_processing_std_arch_event_dir;
1159 rc = nftw(ldirname, process_one_file, maxfds, 0);
1161 goto err_processing_dir;
1163 sprintf(ldirname, "%s/test", start_dirname);
1165 rc = nftw(ldirname, process_one_file, maxfds, 0);
1167 goto err_processing_dir;
1170 print_events_table_suffix(eventsfp);
1173 pr_info("%s: No CPU->JSON mapping?\n", prog);
1175 goto err_close_eventsfp;
1178 rc = process_mapfile(eventsfp, mapfile);
1181 pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
1182 /* Make build fail */
1187 free_arch_std_events();
1191 err_processing_std_arch_event_dir:
1192 err_string_ext = " for std arch event";
1195 pr_info("%s: Error walking file tree %s%s\n", prog, ldirname,
1198 } else if (rc < 0) {
1206 create_empty_mapping(output_file);
1208 free_arch_std_events();