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" },
238 { "UPI LL", "uncore_upi" },
239 { "hisi_sccl,ddrc", "hisi_sccl,ddrc" },
240 { "hisi_sccl,hha", "hisi_sccl,hha" },
241 { "hisi_sccl,l3c", "hisi_sccl,l3c" },
245 static const char *field_to_perf(struct map
*table
, char *map
, jsmntok_t
*val
)
249 for (i
= 0; table
[i
].json
; i
++) {
250 if (json_streq(map
, val
, table
[i
].json
))
251 return table
[i
].perf
;
256 #define EXPECT(e, t, m) do { if (!(e)) { \
257 jsmntok_t *loc = (t); \
258 if (!(t)->start && (t) > tokens) \
260 pr_err("%s:%d: " m ", got %s\n", fn, \
261 json_line(map, loc), \
269 static char *get_topic(void)
274 /* tp is free'd in process_one_file() */
275 i
= asprintf(&tp
, "%s", topic
);
277 pr_info("%s: asprintf() error %s\n", prog
);
281 for (i
= 0; i
< (int) strlen(tp
); i
++) {
295 static int add_topic(char *bname
)
298 topic
= strdup(bname
);
300 pr_info("%s: strdup() error %s for file %s\n", prog
,
301 strerror(errno
), bname
);
307 struct perf_entry_data
{
312 static int close_table
;
314 static void print_events_table_prefix(FILE *fp
, const char *tblname
)
316 fprintf(fp
, "struct pmu_event %s[] = {\n", tblname
);
320 static int print_events_table_entry(void *data
, char *name
, char *event
,
321 char *desc
, char *long_desc
,
322 char *pmu
, char *unit
, char *perpkg
,
324 char *metric_name
, char *metric_group
)
326 struct perf_entry_data
*pd
= data
;
327 FILE *outfp
= pd
->outfp
;
328 char *topic
= pd
->topic
;
331 * TODO: Remove formatting chars after debugging to reduce
334 fprintf(outfp
, "{\n");
337 fprintf(outfp
, "\t.name = \"%s\",\n", name
);
339 fprintf(outfp
, "\t.event = \"%s\",\n", event
);
340 fprintf(outfp
, "\t.desc = \"%s\",\n", desc
);
341 fprintf(outfp
, "\t.topic = \"%s\",\n", topic
);
342 if (long_desc
&& long_desc
[0])
343 fprintf(outfp
, "\t.long_desc = \"%s\",\n", long_desc
);
345 fprintf(outfp
, "\t.pmu = \"%s\",\n", pmu
);
347 fprintf(outfp
, "\t.unit = \"%s\",\n", unit
);
349 fprintf(outfp
, "\t.perpkg = \"%s\",\n", perpkg
);
351 fprintf(outfp
, "\t.metric_expr = \"%s\",\n", metric_expr
);
353 fprintf(outfp
, "\t.metric_name = \"%s\",\n", metric_name
);
355 fprintf(outfp
, "\t.metric_group = \"%s\",\n", metric_group
);
356 fprintf(outfp
, "},\n");
361 struct event_struct
{
362 struct list_head list
;
375 #define ADD_EVENT_FIELD(field) do { if (field) { \
376 es->field = strdup(field); \
381 #define FREE_EVENT_FIELD(field) free(es->field)
383 #define TRY_FIXUP_FIELD(field) do { if (es->field && !*field) {\
384 *field = strdup(es->field); \
389 #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
402 static LIST_HEAD(arch_std_events
);
404 static void free_arch_std_events(void)
406 struct event_struct
*es
, *next
;
408 list_for_each_entry_safe(es
, next
, &arch_std_events
, list
) {
409 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD
);
410 list_del_init(&es
->list
);
415 static int save_arch_std_events(void *data
, char *name
, char *event
,
416 char *desc
, char *long_desc
, char *pmu
,
417 char *unit
, char *perpkg
, char *metric_expr
,
418 char *metric_name
, char *metric_group
)
420 struct event_struct
*es
;
422 es
= malloc(sizeof(*es
));
425 memset(es
, 0, sizeof(*es
));
426 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD
);
427 list_add_tail(&es
->list
, &arch_std_events
);
430 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD
);
435 static void print_events_table_suffix(FILE *outfp
)
437 fprintf(outfp
, "{\n");
439 fprintf(outfp
, "\t.name = 0,\n");
440 fprintf(outfp
, "\t.event = 0,\n");
441 fprintf(outfp
, "\t.desc = 0,\n");
443 fprintf(outfp
, "},\n");
444 fprintf(outfp
, "};\n");
448 static struct fixed
{
452 { "inst_retired.any", "event=0xc0" },
453 { "inst_retired.any_p", "event=0xc0" },
454 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03" },
455 { "cpu_clk_unhalted.thread", "event=0x3c" },
456 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1" },
461 * Handle different fixed counter encodings between JSON and perf.
463 static char *real_event(const char *name
, char *event
)
470 for (i
= 0; fixed
[i
].name
; i
++)
471 if (!strcasecmp(name
, fixed
[i
].name
))
472 return (char *)fixed
[i
].event
;
477 try_fixup(const char *fn
, char *arch_std
, char **event
, char **desc
,
478 char **name
, char **long_desc
, char **pmu
, char **filter
,
479 char **perpkg
, char **unit
, char **metric_expr
, char **metric_name
,
480 char **metric_group
, unsigned long long eventcode
)
482 /* try to find matching event from arch standard values */
483 struct event_struct
*es
;
485 list_for_each_entry(es
, &arch_std_events
, list
) {
486 if (!strcmp(arch_std
, es
->name
)) {
487 if (!eventcode
&& es
->event
) {
488 /* allow EventCode to be overridden */
492 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD
);
497 pr_err("%s: could not find matching %s for %s\n",
502 /* Call func with each event in the json file */
503 int json_events(const char *fn
,
504 int (*func
)(void *data
, char *name
, char *event
, char *desc
,
506 char *pmu
, char *unit
, char *perpkg
,
508 char *metric_name
, char *metric_group
),
513 jsmntok_t
*tokens
, *tok
;
521 tokens
= parse_json(fn
, &map
, &size
, &len
);
524 EXPECT(tokens
->type
== JSMN_ARRAY
, tokens
, "expected top level array");
526 for (i
= 0; i
< tokens
->size
; i
++) {
527 char *event
= NULL
, *desc
= NULL
, *name
= NULL
;
528 char *long_desc
= NULL
;
529 char *extra_desc
= NULL
;
534 char *metric_expr
= NULL
;
535 char *metric_name
= NULL
;
536 char *metric_group
= NULL
;
537 char *arch_std
= NULL
;
538 unsigned long long eventcode
= 0;
539 struct msrmap
*msr
= NULL
;
540 jsmntok_t
*msrval
= NULL
;
541 jsmntok_t
*precise
= NULL
;
542 jsmntok_t
*obj
= tok
++;
544 EXPECT(obj
->type
== JSMN_OBJECT
, obj
, "expected object");
545 for (j
= 0; j
< obj
->size
; j
+= 2) {
546 jsmntok_t
*field
, *val
;
551 EXPECT(field
->type
== JSMN_STRING
, tok
+ j
,
552 "Expected field name");
554 EXPECT(val
->type
== JSMN_STRING
, tok
+ j
+ 1,
555 "Expected string value");
557 nz
= !json_streq(map
, val
, "0");
558 if (match_field(map
, field
, nz
, &event
, val
)) {
560 } else if (json_streq(map
, field
, "EventCode")) {
562 addfield(map
, &code
, "", "", val
);
563 eventcode
|= strtoul(code
, NULL
, 0);
565 } else if (json_streq(map
, field
, "ExtSel")) {
567 addfield(map
, &code
, "", "", val
);
568 eventcode
|= strtoul(code
, NULL
, 0) << 21;
570 } else if (json_streq(map
, field
, "EventName")) {
571 addfield(map
, &name
, "", "", val
);
572 } else if (json_streq(map
, field
, "BriefDescription")) {
573 addfield(map
, &desc
, "", "", val
);
575 } else if (json_streq(map
, field
,
576 "PublicDescription")) {
577 addfield(map
, &long_desc
, "", "", val
);
579 } else if (json_streq(map
, field
, "PEBS") && nz
) {
581 } else if (json_streq(map
, field
, "MSRIndex") && nz
) {
582 msr
= lookup_msr(map
, val
);
583 } else if (json_streq(map
, field
, "MSRValue")) {
585 } else if (json_streq(map
, field
, "Errata") &&
586 !json_streq(map
, val
, "null")) {
587 addfield(map
, &extra_desc
, ". ",
588 " Spec update: ", val
);
589 } else if (json_streq(map
, field
, "Data_LA") && nz
) {
590 addfield(map
, &extra_desc
, ". ",
591 " Supports address when precise",
593 } else if (json_streq(map
, field
, "Unit")) {
596 ppmu
= field_to_perf(unit_to_pmu
, map
, val
);
601 pmu
= strdup("uncore_");
602 addfield(map
, &pmu
, "", "", val
);
603 for (s
= pmu
; *s
; s
++)
606 addfield(map
, &desc
, ". ", "Unit: ", NULL
);
607 addfield(map
, &desc
, "", pmu
, NULL
);
608 addfield(map
, &desc
, "", " ", NULL
);
609 } else if (json_streq(map
, field
, "Filter")) {
610 addfield(map
, &filter
, "", "", val
);
611 } else if (json_streq(map
, field
, "ScaleUnit")) {
612 addfield(map
, &unit
, "", "", val
);
613 } else if (json_streq(map
, field
, "PerPkg")) {
614 addfield(map
, &perpkg
, "", "", val
);
615 } else if (json_streq(map
, field
, "MetricName")) {
616 addfield(map
, &metric_name
, "", "", val
);
617 } else if (json_streq(map
, field
, "MetricGroup")) {
618 addfield(map
, &metric_group
, "", "", val
);
619 } else if (json_streq(map
, field
, "MetricExpr")) {
620 addfield(map
, &metric_expr
, "", "", val
);
621 for (s
= metric_expr
; *s
; s
++)
623 } else if (json_streq(map
, field
, "ArchStdEvent")) {
624 addfield(map
, &arch_std
, "", "", val
);
625 for (s
= arch_std
; *s
; s
++)
628 /* ignore unknown fields */
630 if (precise
&& desc
&& !strstr(desc
, "(Precise Event)")) {
631 if (json_streq(map
, precise
, "2"))
632 addfield(map
, &extra_desc
, " ",
633 "(Must be precise)", NULL
);
635 addfield(map
, &extra_desc
, " ",
636 "(Precise event)", NULL
);
638 snprintf(buf
, sizeof buf
, "event=%#llx", eventcode
);
639 addfield(map
, &event
, ",", buf
, NULL
);
640 if (desc
&& extra_desc
)
641 addfield(map
, &desc
, " ", extra_desc
, NULL
);
642 if (long_desc
&& extra_desc
)
643 addfield(map
, &long_desc
, " ", extra_desc
, NULL
);
645 addfield(map
, &event
, ",", filter
, NULL
);
647 addfield(map
, &event
, ",", msr
->pname
, msrval
);
653 * An arch standard event is referenced, so try to
654 * fixup any unassigned values.
656 err
= try_fixup(fn
, arch_std
, &event
, &desc
, &name
,
657 &long_desc
, &pmu
, &filter
, &perpkg
,
658 &unit
, &metric_expr
, &metric_name
,
659 &metric_group
, eventcode
);
663 err
= func(data
, name
, real_event(name
, event
), desc
, long_desc
,
664 pmu
, unit
, perpkg
, metric_expr
, metric_name
, metric_group
);
684 EXPECT(tok
- tokens
== len
, tok
, "unexpected objects at end");
687 free_json(map
, size
, tokens
);
691 static char *file_name_to_table_name(char *fname
)
699 * Ensure tablename starts with alphabetic character.
700 * Derive rest of table name from basename of the JSON file,
701 * replacing hyphens and stripping out .json suffix.
703 n
= asprintf(&tblname
, "pme_%s", fname
);
705 pr_info("%s: asprintf() error %s for file %s\n", prog
,
706 strerror(errno
), fname
);
710 for (i
= 0; i
< strlen(tblname
); i
++) {
713 if (c
== '-' || c
== '/')
718 } else if (!isalnum(c
) && c
!= '_') {
719 pr_err("%s: Invalid character '%c' in file name %s\n",
720 prog
, c
, basename(fname
));
730 static void print_mapping_table_prefix(FILE *outfp
)
732 fprintf(outfp
, "struct pmu_events_map pmu_events_map[] = {\n");
735 static void print_mapping_table_suffix(FILE *outfp
)
738 * Print the terminating, NULL entry.
740 fprintf(outfp
, "{\n");
741 fprintf(outfp
, "\t.cpuid = 0,\n");
742 fprintf(outfp
, "\t.version = 0,\n");
743 fprintf(outfp
, "\t.type = 0,\n");
744 fprintf(outfp
, "\t.table = 0,\n");
745 fprintf(outfp
, "},\n");
747 /* and finally, the closing curly bracket for the struct */
748 fprintf(outfp
, "};\n");
751 static int process_mapfile(FILE *outfp
, char *fpath
)
760 pr_info("%s: Processing mapfile %s\n", prog
, fpath
);
766 mapfp
= fopen(fpath
, "r");
768 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
);
798 line
[strlen(line
)-1] = '\0';
800 cpuid
= fixregex(strtok_r(p
, ",", &save
));
801 version
= strtok_r(NULL
, ",", &save
);
802 fname
= strtok_r(NULL
, ",", &save
);
803 type
= strtok_r(NULL
, ",", &save
);
805 tblname
= file_name_to_table_name(fname
);
806 fprintf(outfp
, "{\n");
807 fprintf(outfp
, "\t.cpuid = \"%s\",\n", cpuid
);
808 fprintf(outfp
, "\t.version = \"%s\",\n", version
);
809 fprintf(outfp
, "\t.type = \"%s\",\n", type
);
812 * CHECK: We can't use the type (eg "core") field in the
813 * table name. For us to do that, we need to somehow tweak
814 * the other caller of file_name_to_table(), process_json()
815 * to determine the type. process_json() file has no way
816 * of knowing these are "core" events unless file name has
817 * core in it. If filename has core in it, we can safely
818 * ignore the type field here also.
820 fprintf(outfp
, "\t.table = %s\n", tblname
);
821 fprintf(outfp
, "},\n");
825 print_mapping_table_suffix(outfp
);
830 * If we fail to locate/process JSON and map files, create a NULL mapping
831 * table. This would at least allow perf to build even if we can't find/use
834 static void create_empty_mapping(const char *output_file
)
838 pr_info("%s: Creating empty pmu_events_map[] table\n", prog
);
840 /* Truncate file to clear any partial writes to it */
841 outfp
= fopen(output_file
, "w");
847 fprintf(outfp
, "#include \"pmu-events/pmu-events.h\"\n");
848 print_mapping_table_prefix(outfp
);
849 print_mapping_table_suffix(outfp
);
853 static int get_maxfds(void)
857 if (getrlimit(RLIMIT_NOFILE
, &rlim
) == 0)
858 return min((int)rlim
.rlim_max
/ 2, 512);
864 * nftw() doesn't let us pass an argument to the processing function,
865 * so use a global variables.
867 static FILE *eventsfp
;
868 static char *mapfile
;
870 static int is_leaf_dir(const char *fpath
)
880 while ((dir
= readdir(d
)) != NULL
) {
881 if (!strcmp(dir
->d_name
, ".") || !strcmp(dir
->d_name
, ".."))
884 if (dir
->d_type
== DT_DIR
) {
887 } else if (dir
->d_type
== DT_UNKNOWN
) {
891 sprintf(path
, "%s/%s", fpath
, dir
->d_name
);
895 if (S_ISDIR(st
.st_mode
)) {
907 static int is_json_file(const char *name
)
911 if (strlen(name
) < 5)
914 suffix
= name
+ strlen(name
) - 5;
916 if (strncmp(suffix
, ".json", 5) == 0)
921 static int preprocess_arch_std_files(const char *fpath
, const struct stat
*sb
,
922 int typeflag
, struct FTW
*ftwbuf
)
924 int level
= ftwbuf
->level
;
925 int is_file
= typeflag
== FTW_F
;
927 if (level
== 1 && is_file
&& is_json_file(fpath
))
928 return json_events(fpath
, save_arch_std_events
, (void *)sb
);
933 static int process_one_file(const char *fpath
, const struct stat
*sb
,
934 int typeflag
, struct FTW
*ftwbuf
)
936 char *tblname
, *bname
;
937 int is_dir
= typeflag
== FTW_D
;
938 int is_file
= typeflag
== FTW_F
;
939 int level
= ftwbuf
->level
;
942 if (level
== 2 && is_dir
) {
944 * For level 2 directory, bname will include parent name,
945 * like vendor/platform. So search back from platform dir
948 bname
= (char *) fpath
+ ftwbuf
->base
- 2;
956 bname
= (char *) fpath
+ ftwbuf
->base
;
958 pr_debug("%s %d %7jd %-20s %s\n",
959 is_file
? "f" : is_dir
? "d" : "x",
960 level
, sb
->st_size
, bname
, fpath
);
962 /* base dir or too deep */
963 if (level
== 0 || level
> 3)
967 /* model directory, reset topic */
968 if ((level
== 1 && is_dir
&& is_leaf_dir(fpath
)) ||
969 (level
== 2 && is_dir
)) {
971 print_events_table_suffix(eventsfp
);
974 * Drop file name suffix. Replace hyphens with underscores.
975 * Fail if file name contains any alphanum characters besides
978 tblname
= file_name_to_table_name(bname
);
980 pr_info("%s: Error determining table name for %s\n", prog
,
985 print_events_table_prefix(eventsfp
, tblname
);
990 * Save the mapfile name for now. We will process mapfile
991 * after processing all JSON files (so we can write out the
992 * mapping table after all PMU events tables).
995 if (level
== 1 && is_file
) {
996 if (!strcmp(bname
, "mapfile.csv")) {
997 mapfile
= strdup(fpath
);
1001 pr_info("%s: Ignoring file %s\n", prog
, fpath
);
1006 * If the file name does not have a .json extension,
1007 * ignore it. It could be a readme.txt for instance.
1010 if (!is_json_file(bname
)) {
1011 pr_info("%s: Ignoring file without .json suffix %s\n", prog
,
1017 if (level
> 1 && add_topic(bname
))
1021 * Assume all other files are JSON files.
1023 * If mapfile refers to 'power7_core.json', we create a table
1024 * named 'power7_core'. Any inconsistencies between the mapfile
1025 * and directory tree could result in build failure due to table
1026 * names not being found.
1028 * Atleast for now, be strict with processing JSON file names.
1029 * i.e. if JSON file name cannot be mapped to C-style table name,
1033 struct perf_entry_data data
= {
1034 .topic
= get_topic(),
1038 err
= json_events(fpath
, print_events_table_entry
, &data
);
1047 #define PATH_MAX 4096
1051 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1052 * the set of JSON files for the architecture 'arch'.
1054 * From each JSON file, create a C-style "PMU events table" from the
1055 * JSON file (see struct pmu_event).
1057 * From the mapfile, create a mapping between the CPU revisions and
1058 * PMU event tables (see struct pmu_events_map).
1060 * Write out the PMU events tables and the mapping table to pmu-event.c.
1062 int main(int argc
, char *argv
[])
1066 char ldirname
[PATH_MAX
];
1069 const char *output_file
;
1070 const char *start_dirname
;
1073 prog
= basename(argv
[0]);
1075 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog
);
1080 start_dirname
= argv
[2];
1081 output_file
= argv
[3];
1084 verbose
= atoi(argv
[4]);
1086 eventsfp
= fopen(output_file
, "w");
1088 pr_err("%s Unable to create required file %s (%s)\n",
1089 prog
, output_file
, strerror(errno
));
1093 sprintf(ldirname
, "%s/%s", start_dirname
, arch
);
1095 /* If architecture does not have any event lists, bail out */
1096 if (stat(ldirname
, &stbuf
) < 0) {
1097 pr_info("%s: Arch %s has no PMU event lists\n", prog
, arch
);
1101 /* Include pmu-events.h first */
1102 fprintf(eventsfp
, "#include \"pmu-events/pmu-events.h\"\n");
1105 * The mapfile allows multiple CPUids to point to the same JSON file,
1106 * so, not sure if there is a need for symlinks within the pmu-events
1109 * For now, treat symlinks of JSON files as regular files and create
1110 * separate tables for each symlink (presumably, each symlink refers
1111 * to specific version of the CPU).
1114 maxfds
= get_maxfds();
1116 rc
= nftw(ldirname
, preprocess_arch_std_files
, maxfds
, 0);
1117 if (rc
&& verbose
) {
1118 pr_info("%s: Error preprocessing arch standard files %s\n",
1121 } else if (rc
< 0) {
1122 /* Make build fail */
1123 free_arch_std_events();
1129 rc
= nftw(ldirname
, process_one_file
, maxfds
, 0);
1130 if (rc
&& verbose
) {
1131 pr_info("%s: Error walking file tree %s\n", prog
, ldirname
);
1133 } else if (rc
< 0) {
1134 /* Make build fail */
1135 free_arch_std_events();
1142 print_events_table_suffix(eventsfp
);
1145 pr_info("%s: No CPU->JSON mapping?\n", prog
);
1149 if (process_mapfile(eventsfp
, mapfile
)) {
1150 pr_info("%s: Error processing mapfile %s\n", prog
, mapfile
);
1151 /* Make build fail */
1159 create_empty_mapping(output_file
);
1160 free_arch_std_events();