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>
56 int eprintf(int level
, int var
, const char *fmt
, ...)
67 ret
= vfprintf(stderr
, fmt
, args
);
74 __attribute__((weak
)) char *get_cpu_str(void)
79 static void addfield(char *map
, char **dst
, const char *sep
,
80 const char *a
, jsmntok_t
*bt
)
82 unsigned int len
= strlen(a
) + 1 + strlen(sep
);
83 int olen
= *dst
? strlen(*dst
) : 0;
84 int blen
= bt
? json_len(bt
) : 0;
87 out
= realloc(*dst
, len
+ olen
+ blen
);
89 /* Don't add field in this case */
100 strncat(*dst
, map
+ bt
->start
, blen
);
103 static void fixname(char *s
)
109 static void fixdesc(char *s
)
111 char *e
= s
+ strlen(s
);
113 /* Remove trailing dots that look ugly in perf list */
115 while (e
>= s
&& isspace(*e
))
121 /* Add escapes for '\' so they are proper C strings. */
122 static char *fixregex(char *s
)
129 /* Count the number of '\' in string */
130 for (p
= s
; *p
; p
++) {
139 /* allocate space for a new string */
140 fixed
= (char *) malloc(len
+ 1);
144 /* copy over the characters */
146 for (p
= s
; *p
; p
++) {
158 static struct msrmap
{
162 { "0x3F6", "ldlat=" },
163 { "0x1A6", "offcore_rsp=" },
164 { "0x1A7", "offcore_rsp=" },
165 { "0x3F7", "frontend=" },
169 static struct field
{
173 { "UMask", "umask=" },
174 { "CounterMask", "cmask=" },
175 { "Invert", "inv=" },
176 { "AnyThread", "any=" },
177 { "EdgeDetect", "edge=" },
178 { "SampleAfterValue", "period=" },
179 { "FCMask", "fc_mask=" },
180 { "PortMask", "ch_mask=" },
184 static void cut_comma(char *map
, jsmntok_t
*newval
)
188 /* Cut off everything after comma */
189 for (i
= newval
->start
; i
< newval
->end
; i
++) {
195 static int match_field(char *map
, jsmntok_t
*field
, int nz
,
196 char **event
, jsmntok_t
*val
)
199 jsmntok_t newval
= *val
;
201 for (f
= fields
; f
->field
; f
++)
202 if (json_streq(map
, field
, f
->field
) && nz
) {
203 cut_comma(map
, &newval
);
204 addfield(map
, event
, ",", f
->kernel
, &newval
);
210 static struct msrmap
*lookup_msr(char *map
, jsmntok_t
*val
)
212 jsmntok_t newval
= *val
;
216 cut_comma(map
, &newval
);
217 for (i
= 0; msrmap
[i
].num
; i
++)
218 if (json_streq(map
, &newval
, msrmap
[i
].num
))
222 pr_err("%s: Unknown MSR in event file %.*s\n", prog
,
223 json_len(val
), map
+ val
->start
);
232 { "CBO", "uncore_cbox" },
233 { "QPI LL", "uncore_qpi" },
234 { "SBO", "uncore_sbox" },
235 { "iMPH-U", "uncore_arb" },
236 { "CPU-M-CF", "cpum_cf" },
237 { "CPU-M-SF", "cpum_sf" },
241 static const char *field_to_perf(struct map
*table
, char *map
, jsmntok_t
*val
)
245 for (i
= 0; table
[i
].json
; i
++) {
246 if (json_streq(map
, val
, table
[i
].json
))
247 return table
[i
].perf
;
252 #define EXPECT(e, t, m) do { if (!(e)) { \
253 jsmntok_t *loc = (t); \
254 if (!(t)->start && (t) > tokens) \
256 pr_err("%s:%d: " m ", got %s\n", fn, \
257 json_line(map, loc), \
265 static char *get_topic(void)
270 /* tp is free'd in process_one_file() */
271 i
= asprintf(&tp
, "%s", topic
);
273 pr_info("%s: asprintf() error %s\n", prog
);
277 for (i
= 0; i
< (int) strlen(tp
); i
++) {
291 static int add_topic(char *bname
)
294 topic
= strdup(bname
);
296 pr_info("%s: strdup() error %s for file %s\n", prog
,
297 strerror(errno
), bname
);
303 struct perf_entry_data
{
308 static int close_table
;
310 static void print_events_table_prefix(FILE *fp
, const char *tblname
)
312 fprintf(fp
, "struct pmu_event %s[] = {\n", tblname
);
316 static int print_events_table_entry(void *data
, char *name
, char *event
,
317 char *desc
, char *long_desc
,
318 char *pmu
, char *unit
, char *perpkg
,
320 char *metric_name
, char *metric_group
)
322 struct perf_entry_data
*pd
= data
;
323 FILE *outfp
= pd
->outfp
;
324 char *topic
= pd
->topic
;
327 * TODO: Remove formatting chars after debugging to reduce
330 fprintf(outfp
, "{\n");
333 fprintf(outfp
, "\t.name = \"%s\",\n", name
);
335 fprintf(outfp
, "\t.event = \"%s\",\n", event
);
336 fprintf(outfp
, "\t.desc = \"%s\",\n", desc
);
337 fprintf(outfp
, "\t.topic = \"%s\",\n", topic
);
338 if (long_desc
&& long_desc
[0])
339 fprintf(outfp
, "\t.long_desc = \"%s\",\n", long_desc
);
341 fprintf(outfp
, "\t.pmu = \"%s\",\n", pmu
);
343 fprintf(outfp
, "\t.unit = \"%s\",\n", unit
);
345 fprintf(outfp
, "\t.perpkg = \"%s\",\n", perpkg
);
347 fprintf(outfp
, "\t.metric_expr = \"%s\",\n", metric_expr
);
349 fprintf(outfp
, "\t.metric_name = \"%s\",\n", metric_name
);
351 fprintf(outfp
, "\t.metric_group = \"%s\",\n", metric_group
);
352 fprintf(outfp
, "},\n");
357 struct event_struct
{
358 struct list_head list
;
371 #define ADD_EVENT_FIELD(field) do { if (field) { \
372 es->field = strdup(field); \
377 #define FREE_EVENT_FIELD(field) free(es->field)
379 #define TRY_FIXUP_FIELD(field) do { if (es->field && !*field) {\
380 *field = strdup(es->field); \
385 #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
398 static LIST_HEAD(arch_std_events
);
400 static void free_arch_std_events(void)
402 struct event_struct
*es
, *next
;
404 list_for_each_entry_safe(es
, next
, &arch_std_events
, list
) {
405 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD
);
411 static int save_arch_std_events(void *data
, char *name
, char *event
,
412 char *desc
, char *long_desc
, char *pmu
,
413 char *unit
, char *perpkg
, char *metric_expr
,
414 char *metric_name
, char *metric_group
)
416 struct event_struct
*es
;
417 struct stat
*sb
= data
;
419 es
= malloc(sizeof(*es
));
422 memset(es
, 0, sizeof(*es
));
423 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD
);
424 list_add_tail(&es
->list
, &arch_std_events
);
427 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD
);
432 static void print_events_table_suffix(FILE *outfp
)
434 fprintf(outfp
, "{\n");
436 fprintf(outfp
, "\t.name = 0,\n");
437 fprintf(outfp
, "\t.event = 0,\n");
438 fprintf(outfp
, "\t.desc = 0,\n");
440 fprintf(outfp
, "},\n");
441 fprintf(outfp
, "};\n");
445 static struct fixed
{
449 { "inst_retired.any", "event=0xc0,period=2000003" },
450 { "inst_retired.any_p", "event=0xc0,period=2000003" },
451 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03,period=2000003" },
452 { "cpu_clk_unhalted.thread", "event=0x3c,period=2000003" },
453 { "cpu_clk_unhalted.core", "event=0x3c,period=2000003" },
454 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1,period=2000003" },
459 * Handle different fixed counter encodings between JSON and perf.
461 static char *real_event(const char *name
, char *event
)
468 for (i
= 0; fixed
[i
].name
; i
++)
469 if (!strcasecmp(name
, fixed
[i
].name
))
470 return (char *)fixed
[i
].event
;
475 try_fixup(const char *fn
, char *arch_std
, char **event
, char **desc
,
476 char **name
, char **long_desc
, char **pmu
, char **filter
,
477 char **perpkg
, char **unit
, char **metric_expr
, char **metric_name
,
478 char **metric_group
, unsigned long long eventcode
)
480 /* try to find matching event from arch standard values */
481 struct event_struct
*es
;
483 list_for_each_entry(es
, &arch_std_events
, list
) {
484 if (!strcmp(arch_std
, es
->name
)) {
485 if (!eventcode
&& es
->event
) {
486 /* allow EventCode to be overridden */
490 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD
);
495 pr_err("%s: could not find matching %s for %s\n",
500 /* Call func with each event in the json file */
501 int json_events(const char *fn
,
502 int (*func
)(void *data
, char *name
, char *event
, char *desc
,
504 char *pmu
, char *unit
, char *perpkg
,
506 char *metric_name
, char *metric_group
),
511 jsmntok_t
*tokens
, *tok
;
519 tokens
= parse_json(fn
, &map
, &size
, &len
);
522 EXPECT(tokens
->type
== JSMN_ARRAY
, tokens
, "expected top level array");
524 for (i
= 0; i
< tokens
->size
; i
++) {
525 char *event
= NULL
, *desc
= NULL
, *name
= NULL
;
526 char *long_desc
= NULL
;
527 char *extra_desc
= NULL
;
532 char *metric_expr
= NULL
;
533 char *metric_name
= NULL
;
534 char *metric_group
= NULL
;
535 char *arch_std
= NULL
;
536 unsigned long long eventcode
= 0;
537 struct msrmap
*msr
= NULL
;
538 jsmntok_t
*msrval
= NULL
;
539 jsmntok_t
*precise
= NULL
;
540 jsmntok_t
*obj
= tok
++;
542 EXPECT(obj
->type
== JSMN_OBJECT
, obj
, "expected object");
543 for (j
= 0; j
< obj
->size
; j
+= 2) {
544 jsmntok_t
*field
, *val
;
549 EXPECT(field
->type
== JSMN_STRING
, tok
+ j
,
550 "Expected field name");
552 EXPECT(val
->type
== JSMN_STRING
, tok
+ j
+ 1,
553 "Expected string value");
555 nz
= !json_streq(map
, val
, "0");
556 if (match_field(map
, field
, nz
, &event
, val
)) {
558 } else if (json_streq(map
, field
, "EventCode")) {
560 addfield(map
, &code
, "", "", val
);
561 eventcode
|= strtoul(code
, NULL
, 0);
563 } else if (json_streq(map
, field
, "ExtSel")) {
565 addfield(map
, &code
, "", "", val
);
566 eventcode
|= strtoul(code
, NULL
, 0) << 21;
568 } else if (json_streq(map
, field
, "EventName")) {
569 addfield(map
, &name
, "", "", val
);
570 } else if (json_streq(map
, field
, "BriefDescription")) {
571 addfield(map
, &desc
, "", "", val
);
573 } else if (json_streq(map
, field
,
574 "PublicDescription")) {
575 addfield(map
, &long_desc
, "", "", val
);
577 } else if (json_streq(map
, field
, "PEBS") && nz
) {
579 } else if (json_streq(map
, field
, "MSRIndex") && nz
) {
580 msr
= lookup_msr(map
, val
);
581 } else if (json_streq(map
, field
, "MSRValue")) {
583 } else if (json_streq(map
, field
, "Errata") &&
584 !json_streq(map
, val
, "null")) {
585 addfield(map
, &extra_desc
, ". ",
586 " Spec update: ", val
);
587 } else if (json_streq(map
, field
, "Data_LA") && nz
) {
588 addfield(map
, &extra_desc
, ". ",
589 " Supports address when precise",
591 } else if (json_streq(map
, field
, "Unit")) {
594 ppmu
= field_to_perf(unit_to_pmu
, map
, val
);
599 pmu
= strdup("uncore_");
600 addfield(map
, &pmu
, "", "", val
);
601 for (s
= pmu
; *s
; s
++)
604 addfield(map
, &desc
, ". ", "Unit: ", NULL
);
605 addfield(map
, &desc
, "", pmu
, NULL
);
606 addfield(map
, &desc
, "", " ", NULL
);
607 } else if (json_streq(map
, field
, "Filter")) {
608 addfield(map
, &filter
, "", "", val
);
609 } else if (json_streq(map
, field
, "ScaleUnit")) {
610 addfield(map
, &unit
, "", "", val
);
611 } else if (json_streq(map
, field
, "PerPkg")) {
612 addfield(map
, &perpkg
, "", "", val
);
613 } else if (json_streq(map
, field
, "MetricName")) {
614 addfield(map
, &metric_name
, "", "", val
);
615 } else if (json_streq(map
, field
, "MetricGroup")) {
616 addfield(map
, &metric_group
, "", "", val
);
617 } else if (json_streq(map
, field
, "MetricExpr")) {
618 addfield(map
, &metric_expr
, "", "", val
);
619 for (s
= metric_expr
; *s
; s
++)
621 } else if (json_streq(map
, field
, "ArchStdEvent")) {
622 addfield(map
, &arch_std
, "", "", val
);
623 for (s
= arch_std
; *s
; s
++)
626 /* ignore unknown fields */
628 if (precise
&& desc
&& !strstr(desc
, "(Precise Event)")) {
629 if (json_streq(map
, precise
, "2"))
630 addfield(map
, &extra_desc
, " ",
631 "(Must be precise)", NULL
);
633 addfield(map
, &extra_desc
, " ",
634 "(Precise event)", NULL
);
636 snprintf(buf
, sizeof buf
, "event=%#llx", eventcode
);
637 addfield(map
, &event
, ",", buf
, NULL
);
638 if (desc
&& extra_desc
)
639 addfield(map
, &desc
, " ", extra_desc
, NULL
);
640 if (long_desc
&& extra_desc
)
641 addfield(map
, &long_desc
, " ", extra_desc
, NULL
);
643 addfield(map
, &event
, ",", filter
, NULL
);
645 addfield(map
, &event
, ",", msr
->pname
, msrval
);
651 * An arch standard event is referenced, so try to
652 * fixup any unassigned values.
654 err
= try_fixup(fn
, arch_std
, &event
, &desc
, &name
,
655 &long_desc
, &pmu
, &filter
, &perpkg
,
656 &unit
, &metric_expr
, &metric_name
,
657 &metric_group
, eventcode
);
661 err
= func(data
, name
, real_event(name
, event
), desc
, long_desc
,
662 pmu
, unit
, perpkg
, metric_expr
, metric_name
, metric_group
);
682 EXPECT(tok
- tokens
== len
, tok
, "unexpected objects at end");
685 free_json(map
, size
, tokens
);
689 static char *file_name_to_table_name(char *fname
)
697 * Ensure tablename starts with alphabetic character.
698 * Derive rest of table name from basename of the JSON file,
699 * replacing hyphens and stripping out .json suffix.
701 n
= asprintf(&tblname
, "pme_%s", fname
);
703 pr_info("%s: asprintf() error %s for file %s\n", prog
,
704 strerror(errno
), fname
);
708 for (i
= 0; i
< strlen(tblname
); i
++) {
711 if (c
== '-' || c
== '/')
716 } else if (!isalnum(c
) && c
!= '_') {
717 pr_err("%s: Invalid character '%c' in file name %s\n",
718 prog
, c
, basename(fname
));
728 static void print_mapping_table_prefix(FILE *outfp
)
730 fprintf(outfp
, "struct pmu_events_map pmu_events_map[] = {\n");
733 static void print_mapping_table_suffix(FILE *outfp
)
736 * Print the terminating, NULL entry.
738 fprintf(outfp
, "{\n");
739 fprintf(outfp
, "\t.cpuid = 0,\n");
740 fprintf(outfp
, "\t.version = 0,\n");
741 fprintf(outfp
, "\t.type = 0,\n");
742 fprintf(outfp
, "\t.table = 0,\n");
743 fprintf(outfp
, "},\n");
745 /* and finally, the closing curly bracket for the struct */
746 fprintf(outfp
, "};\n");
749 static int process_mapfile(FILE *outfp
, char *fpath
)
759 pr_info("%s: Processing mapfile %s\n", prog
, fpath
);
765 mapfp
= fopen(fpath
, "r");
767 pr_info("%s: Error %s opening %s\n", prog
, strerror(errno
),
773 print_mapping_table_prefix(outfp
);
775 /* Skip first line (header) */
776 p
= fgets(line
, n
, mapfp
);
782 char *cpuid
, *version
, *type
, *fname
;
785 p
= fgets(line
, n
, mapfp
);
789 if (line
[0] == '#' || line
[0] == '\n')
792 if (line
[strlen(line
)-1] != '\n') {
793 /* TODO Deal with lines longer than 16K */
794 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
795 prog
, fpath
, line_num
);
799 line
[strlen(line
)-1] = '\0';
801 cpuid
= fixregex(strtok_r(p
, ",", &save
));
802 version
= strtok_r(NULL
, ",", &save
);
803 fname
= strtok_r(NULL
, ",", &save
);
804 type
= strtok_r(NULL
, ",", &save
);
806 tblname
= file_name_to_table_name(fname
);
807 fprintf(outfp
, "{\n");
808 fprintf(outfp
, "\t.cpuid = \"%s\",\n", cpuid
);
809 fprintf(outfp
, "\t.version = \"%s\",\n", version
);
810 fprintf(outfp
, "\t.type = \"%s\",\n", type
);
813 * CHECK: We can't use the type (eg "core") field in the
814 * table name. For us to do that, we need to somehow tweak
815 * the other caller of file_name_to_table(), process_json()
816 * to determine the type. process_json() file has no way
817 * of knowing these are "core" events unless file name has
818 * core in it. If filename has core in it, we can safely
819 * ignore the type field here also.
821 fprintf(outfp
, "\t.table = %s\n", tblname
);
822 fprintf(outfp
, "},\n");
826 print_mapping_table_suffix(outfp
);
833 * If we fail to locate/process JSON and map files, create a NULL mapping
834 * table. This would at least allow perf to build even if we can't find/use
837 static void create_empty_mapping(const char *output_file
)
841 pr_info("%s: Creating empty pmu_events_map[] table\n", prog
);
843 /* Truncate file to clear any partial writes to it */
844 outfp
= fopen(output_file
, "w");
850 fprintf(outfp
, "#include \"../../pmu-events/pmu-events.h\"\n");
851 print_mapping_table_prefix(outfp
);
852 print_mapping_table_suffix(outfp
);
856 static int get_maxfds(void)
860 if (getrlimit(RLIMIT_NOFILE
, &rlim
) == 0)
861 return min((int)rlim
.rlim_max
/ 2, 512);
867 * nftw() doesn't let us pass an argument to the processing function,
868 * so use a global variables.
870 static FILE *eventsfp
;
871 static char *mapfile
;
873 static int is_leaf_dir(const char *fpath
)
883 while ((dir
= readdir(d
)) != NULL
) {
884 if (!strcmp(dir
->d_name
, ".") || !strcmp(dir
->d_name
, ".."))
887 if (dir
->d_type
== DT_DIR
) {
890 } else if (dir
->d_type
== DT_UNKNOWN
) {
894 sprintf(path
, "%s/%s", fpath
, dir
->d_name
);
898 if (S_ISDIR(st
.st_mode
)) {
910 static int is_json_file(const char *name
)
914 if (strlen(name
) < 5)
917 suffix
= name
+ strlen(name
) - 5;
919 if (strncmp(suffix
, ".json", 5) == 0)
924 static int preprocess_arch_std_files(const char *fpath
, const struct stat
*sb
,
925 int typeflag
, struct FTW
*ftwbuf
)
927 int level
= ftwbuf
->level
;
928 int is_file
= typeflag
== FTW_F
;
930 if (level
== 1 && is_file
&& is_json_file(fpath
))
931 return json_events(fpath
, save_arch_std_events
, (void *)sb
);
936 static int process_one_file(const char *fpath
, const struct stat
*sb
,
937 int typeflag
, struct FTW
*ftwbuf
)
939 char *tblname
, *bname
;
940 int is_dir
= typeflag
== FTW_D
;
941 int is_file
= typeflag
== FTW_F
;
942 int level
= ftwbuf
->level
;
945 if (level
== 2 && is_dir
) {
947 * For level 2 directory, bname will include parent name,
948 * like vendor/platform. So search back from platform dir
951 bname
= (char *) fpath
+ ftwbuf
->base
- 2;
959 bname
= (char *) fpath
+ ftwbuf
->base
;
961 pr_debug("%s %d %7jd %-20s %s\n",
962 is_file
? "f" : is_dir
? "d" : "x",
963 level
, sb
->st_size
, bname
, fpath
);
965 /* base dir or too deep */
966 if (level
== 0 || level
> 3)
970 /* model directory, reset topic */
971 if ((level
== 1 && is_dir
&& is_leaf_dir(fpath
)) ||
972 (level
== 2 && is_dir
)) {
974 print_events_table_suffix(eventsfp
);
977 * Drop file name suffix. Replace hyphens with underscores.
978 * Fail if file name contains any alphanum characters besides
981 tblname
= file_name_to_table_name(bname
);
983 pr_info("%s: Error determining table name for %s\n", prog
,
988 print_events_table_prefix(eventsfp
, tblname
);
993 * Save the mapfile name for now. We will process mapfile
994 * after processing all JSON files (so we can write out the
995 * mapping table after all PMU events tables).
998 if (level
== 1 && is_file
) {
999 if (!strcmp(bname
, "mapfile.csv")) {
1000 mapfile
= strdup(fpath
);
1004 pr_info("%s: Ignoring file %s\n", prog
, fpath
);
1009 * If the file name does not have a .json extension,
1010 * ignore it. It could be a readme.txt for instance.
1013 if (!is_json_file(bname
)) {
1014 pr_info("%s: Ignoring file without .json suffix %s\n", prog
,
1020 if (level
> 1 && add_topic(bname
))
1024 * Assume all other files are JSON files.
1026 * If mapfile refers to 'power7_core.json', we create a table
1027 * named 'power7_core'. Any inconsistencies between the mapfile
1028 * and directory tree could result in build failure due to table
1029 * names not being found.
1031 * Atleast for now, be strict with processing JSON file names.
1032 * i.e. if JSON file name cannot be mapped to C-style table name,
1036 struct perf_entry_data data
= {
1037 .topic
= get_topic(),
1041 err
= json_events(fpath
, print_events_table_entry
, &data
);
1050 #define PATH_MAX 4096
1054 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1055 * the set of JSON files for the architecture 'arch'.
1057 * From each JSON file, create a C-style "PMU events table" from the
1058 * JSON file (see struct pmu_event).
1060 * From the mapfile, create a mapping between the CPU revisions and
1061 * PMU event tables (see struct pmu_events_map).
1063 * Write out the PMU events tables and the mapping table to pmu-event.c.
1065 int main(int argc
, char *argv
[])
1069 char ldirname
[PATH_MAX
];
1072 const char *output_file
;
1073 const char *start_dirname
;
1076 prog
= basename(argv
[0]);
1078 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog
);
1083 start_dirname
= argv
[2];
1084 output_file
= argv
[3];
1087 verbose
= atoi(argv
[4]);
1089 eventsfp
= fopen(output_file
, "w");
1091 pr_err("%s Unable to create required file %s (%s)\n",
1092 prog
, output_file
, strerror(errno
));
1096 sprintf(ldirname
, "%s/%s", start_dirname
, arch
);
1098 /* If architecture does not have any event lists, bail out */
1099 if (stat(ldirname
, &stbuf
) < 0) {
1100 pr_info("%s: Arch %s has no PMU event lists\n", prog
, arch
);
1104 /* Include pmu-events.h first */
1105 fprintf(eventsfp
, "#include \"../../pmu-events/pmu-events.h\"\n");
1108 * The mapfile allows multiple CPUids to point to the same JSON file,
1109 * so, not sure if there is a need for symlinks within the pmu-events
1112 * For now, treat symlinks of JSON files as regular files and create
1113 * separate tables for each symlink (presumably, each symlink refers
1114 * to specific version of the CPU).
1117 maxfds
= get_maxfds();
1119 rc
= nftw(ldirname
, preprocess_arch_std_files
, maxfds
, 0);
1120 if (rc
&& verbose
) {
1121 pr_info("%s: Error preprocessing arch standard files %s\n",
1124 } else if (rc
< 0) {
1125 /* Make build fail */
1127 free_arch_std_events();
1133 rc
= nftw(ldirname
, process_one_file
, maxfds
, 0);
1134 if (rc
&& verbose
) {
1135 pr_info("%s: Error walking file tree %s\n", prog
, ldirname
);
1137 } else if (rc
< 0) {
1138 /* Make build fail */
1140 free_arch_std_events();
1147 print_events_table_suffix(eventsfp
);
1150 pr_info("%s: No CPU->JSON mapping?\n", prog
);
1154 if (process_mapfile(eventsfp
, mapfile
)) {
1155 pr_info("%s: Error processing mapfile %s\n", prog
, mapfile
);
1156 /* Make build fail */
1158 free_arch_std_events();
1166 create_empty_mapping(output_file
);
1167 free_arch_std_events();