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>
67 char *metric_constraint
;
70 typedef int (*func
)(void *data
, struct json_event
*je
);
72 int eprintf(int level
, int var
, const char *fmt
, ...)
83 ret
= vfprintf(stderr
, fmt
, args
);
90 static void addfield(char *map
, char **dst
, const char *sep
,
91 const char *a
, jsmntok_t
*bt
)
93 unsigned int len
= strlen(a
) + 1 + strlen(sep
);
94 int olen
= *dst
? strlen(*dst
) : 0;
95 int blen
= bt
? json_len(bt
) : 0;
98 out
= realloc(*dst
, len
+ olen
+ blen
);
100 /* Don't add field in this case */
111 strncat(*dst
, map
+ bt
->start
, blen
);
114 static void fixname(char *s
)
120 static void fixdesc(char *s
)
122 char *e
= s
+ strlen(s
);
124 /* Remove trailing dots that look ugly in perf list */
126 while (e
>= s
&& isspace(*e
))
132 /* Add escapes for '\' so they are proper C strings. */
133 static char *fixregex(char *s
)
140 /* Count the number of '\' in string */
141 for (p
= s
; *p
; p
++) {
150 /* allocate space for a new string */
151 fixed
= (char *) malloc(len
+ esc_count
+ 1);
155 /* copy over the characters */
157 for (p
= s
; *p
; p
++) {
169 static struct msrmap
{
173 { "0x3F6", "ldlat=" },
174 { "0x1A6", "offcore_rsp=" },
175 { "0x1A7", "offcore_rsp=" },
176 { "0x3F7", "frontend=" },
180 static struct field
{
184 { "UMask", "umask=" },
185 { "CounterMask", "cmask=" },
186 { "Invert", "inv=" },
187 { "AnyThread", "any=" },
188 { "EdgeDetect", "edge=" },
189 { "SampleAfterValue", "period=" },
190 { "FCMask", "fc_mask=" },
191 { "PortMask", "ch_mask=" },
195 static void cut_comma(char *map
, jsmntok_t
*newval
)
199 /* Cut off everything after comma */
200 for (i
= newval
->start
; i
< newval
->end
; i
++) {
206 static int match_field(char *map
, jsmntok_t
*field
, int nz
,
207 char **event
, jsmntok_t
*val
)
210 jsmntok_t newval
= *val
;
212 for (f
= fields
; f
->field
; f
++)
213 if (json_streq(map
, field
, f
->field
) && nz
) {
214 cut_comma(map
, &newval
);
215 addfield(map
, event
, ",", f
->kernel
, &newval
);
221 static struct msrmap
*lookup_msr(char *map
, jsmntok_t
*val
)
223 jsmntok_t newval
= *val
;
227 cut_comma(map
, &newval
);
228 for (i
= 0; msrmap
[i
].num
; i
++)
229 if (json_streq(map
, &newval
, msrmap
[i
].num
))
233 pr_err("%s: Unknown MSR in event file %.*s\n", prog
,
234 json_len(val
), map
+ val
->start
);
243 { "CBO", "uncore_cbox" },
244 { "QPI LL", "uncore_qpi" },
245 { "SBO", "uncore_sbox" },
246 { "iMPH-U", "uncore_arb" },
247 { "CPU-M-CF", "cpum_cf" },
248 { "CPU-M-SF", "cpum_sf" },
249 { "UPI LL", "uncore_upi" },
250 { "hisi_sccl,ddrc", "hisi_sccl,ddrc" },
251 { "hisi_sccl,hha", "hisi_sccl,hha" },
252 { "hisi_sccl,l3c", "hisi_sccl,l3c" },
253 { "L3PMC", "amd_l3" },
254 { "DFPMC", "amd_df" },
258 static const char *field_to_perf(struct map
*table
, char *map
, jsmntok_t
*val
)
262 for (i
= 0; table
[i
].json
; i
++) {
263 if (json_streq(map
, val
, table
[i
].json
))
264 return table
[i
].perf
;
269 #define EXPECT(e, t, m) do { if (!(e)) { \
270 jsmntok_t *loc = (t); \
271 if (!(t)->start && (t) > tokens) \
273 pr_err("%s:%d: " m ", got %s\n", fn, \
274 json_line(map, loc), \
282 static char *get_topic(void)
287 /* tp is free'd in process_one_file() */
288 i
= asprintf(&tp
, "%s", topic
);
290 pr_info("%s: asprintf() error %s\n", prog
);
294 for (i
= 0; i
< (int) strlen(tp
); i
++) {
308 static int add_topic(char *bname
)
311 topic
= strdup(bname
);
313 pr_info("%s: strdup() error %s for file %s\n", prog
,
314 strerror(errno
), bname
);
320 struct perf_entry_data
{
325 static int close_table
;
327 static void print_events_table_prefix(FILE *fp
, const char *tblname
)
329 fprintf(fp
, "struct pmu_event %s[] = {\n", tblname
);
333 static int print_events_table_entry(void *data
, struct json_event
*je
)
335 struct perf_entry_data
*pd
= data
;
336 FILE *outfp
= pd
->outfp
;
337 char *topic
= pd
->topic
;
340 * TODO: Remove formatting chars after debugging to reduce
343 fprintf(outfp
, "{\n");
346 fprintf(outfp
, "\t.name = \"%s\",\n", je
->name
);
348 fprintf(outfp
, "\t.event = \"%s\",\n", je
->event
);
349 fprintf(outfp
, "\t.desc = \"%s\",\n", je
->desc
);
350 fprintf(outfp
, "\t.topic = \"%s\",\n", topic
);
351 if (je
->long_desc
&& je
->long_desc
[0])
352 fprintf(outfp
, "\t.long_desc = \"%s\",\n", je
->long_desc
);
354 fprintf(outfp
, "\t.pmu = \"%s\",\n", je
->pmu
);
356 fprintf(outfp
, "\t.unit = \"%s\",\n", je
->unit
);
358 fprintf(outfp
, "\t.perpkg = \"%s\",\n", je
->perpkg
);
360 fprintf(outfp
, "\t.metric_expr = \"%s\",\n", je
->metric_expr
);
362 fprintf(outfp
, "\t.metric_name = \"%s\",\n", je
->metric_name
);
363 if (je
->metric_group
)
364 fprintf(outfp
, "\t.metric_group = \"%s\",\n", je
->metric_group
);
366 fprintf(outfp
, "\t.deprecated = \"%s\",\n", je
->deprecated
);
367 if (je
->metric_constraint
)
368 fprintf(outfp
, "\t.metric_constraint = \"%s\",\n", je
->metric_constraint
);
369 fprintf(outfp
, "},\n");
374 struct event_struct
{
375 struct list_head list
;
387 char *metric_constraint
;
390 #define ADD_EVENT_FIELD(field) do { if (je->field) { \
391 es->field = strdup(je->field); \
396 #define FREE_EVENT_FIELD(field) free(es->field)
398 #define TRY_FIXUP_FIELD(field) do { if (es->field && !je->field) {\
399 je->field = strdup(es->field); \
404 #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
418 static LIST_HEAD(arch_std_events
);
420 static void free_arch_std_events(void)
422 struct event_struct
*es
, *next
;
424 list_for_each_entry_safe(es
, next
, &arch_std_events
, list
) {
425 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD
);
426 list_del_init(&es
->list
);
431 static int save_arch_std_events(void *data
, struct json_event
*je
)
433 struct event_struct
*es
;
435 es
= malloc(sizeof(*es
));
438 memset(es
, 0, sizeof(*es
));
439 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD
);
440 list_add_tail(&es
->list
, &arch_std_events
);
443 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD
);
448 static void print_events_table_suffix(FILE *outfp
)
450 fprintf(outfp
, "{\n");
452 fprintf(outfp
, "\t.name = 0,\n");
453 fprintf(outfp
, "\t.event = 0,\n");
454 fprintf(outfp
, "\t.desc = 0,\n");
456 fprintf(outfp
, "},\n");
457 fprintf(outfp
, "};\n");
461 static struct fixed
{
465 { "inst_retired.any", "event=0xc0,period=2000003" },
466 { "inst_retired.any_p", "event=0xc0,period=2000003" },
467 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03,period=2000003" },
468 { "cpu_clk_unhalted.thread", "event=0x3c,period=2000003" },
469 { "cpu_clk_unhalted.core", "event=0x3c,period=2000003" },
470 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1,period=2000003" },
475 * Handle different fixed counter encodings between JSON and perf.
477 static char *real_event(const char *name
, char *event
)
484 for (i
= 0; fixed
[i
].name
; i
++)
485 if (!strcasecmp(name
, fixed
[i
].name
))
486 return (char *)fixed
[i
].event
;
491 try_fixup(const char *fn
, char *arch_std
, unsigned long long eventcode
,
492 struct json_event
*je
)
494 /* try to find matching event from arch standard values */
495 struct event_struct
*es
;
497 list_for_each_entry(es
, &arch_std_events
, list
) {
498 if (!strcmp(arch_std
, es
->name
)) {
499 if (!eventcode
&& es
->event
) {
500 /* allow EventCode to be overridden */
504 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD
);
509 pr_err("%s: could not find matching %s for %s\n",
514 /* Call func with each event in the json file */
515 static int json_events(const char *fn
,
516 int (*func
)(void *data
, struct json_event
*je
),
521 jsmntok_t
*tokens
, *tok
;
529 tokens
= parse_json(fn
, &map
, &size
, &len
);
532 EXPECT(tokens
->type
== JSMN_ARRAY
, tokens
, "expected top level array");
534 for (i
= 0; i
< tokens
->size
; i
++) {
536 char *extra_desc
= NULL
;
538 struct json_event je
= {};
539 char *arch_std
= NULL
;
540 unsigned long long eventcode
= 0;
541 struct msrmap
*msr
= NULL
;
542 jsmntok_t
*msrval
= NULL
;
543 jsmntok_t
*precise
= NULL
;
544 jsmntok_t
*obj
= tok
++;
546 EXPECT(obj
->type
== JSMN_OBJECT
, obj
, "expected object");
547 for (j
= 0; j
< obj
->size
; j
+= 2) {
548 jsmntok_t
*field
, *val
;
553 EXPECT(field
->type
== JSMN_STRING
, tok
+ j
,
554 "Expected field name");
556 EXPECT(val
->type
== JSMN_STRING
, tok
+ j
+ 1,
557 "Expected string value");
559 nz
= !json_streq(map
, val
, "0");
560 if (match_field(map
, field
, nz
, &event
, val
)) {
562 } else if (json_streq(map
, field
, "EventCode")) {
564 addfield(map
, &code
, "", "", val
);
565 eventcode
|= strtoul(code
, NULL
, 0);
567 } else if (json_streq(map
, field
, "ExtSel")) {
569 addfield(map
, &code
, "", "", val
);
570 eventcode
|= strtoul(code
, NULL
, 0) << 21;
572 } else if (json_streq(map
, field
, "EventName")) {
573 addfield(map
, &je
.name
, "", "", val
);
574 } else if (json_streq(map
, field
, "BriefDescription")) {
575 addfield(map
, &je
.desc
, "", "", val
);
577 } else if (json_streq(map
, field
,
578 "PublicDescription")) {
579 addfield(map
, &je
.long_desc
, "", "", val
);
580 fixdesc(je
.long_desc
);
581 } else if (json_streq(map
, field
, "PEBS") && nz
) {
583 } else if (json_streq(map
, field
, "MSRIndex") && nz
) {
584 msr
= lookup_msr(map
, val
);
585 } else if (json_streq(map
, field
, "MSRValue")) {
587 } else if (json_streq(map
, field
, "Errata") &&
588 !json_streq(map
, val
, "null")) {
589 addfield(map
, &extra_desc
, ". ",
590 " Spec update: ", val
);
591 } else if (json_streq(map
, field
, "Data_LA") && nz
) {
592 addfield(map
, &extra_desc
, ". ",
593 " Supports address when precise",
595 } else if (json_streq(map
, field
, "Unit")) {
598 ppmu
= field_to_perf(unit_to_pmu
, map
, val
);
600 je
.pmu
= strdup(ppmu
);
603 je
.pmu
= strdup("uncore_");
604 addfield(map
, &je
.pmu
, "", "", val
);
605 for (s
= je
.pmu
; *s
; s
++)
608 addfield(map
, &je
.desc
, ". ", "Unit: ", NULL
);
609 addfield(map
, &je
.desc
, "", je
.pmu
, NULL
);
610 addfield(map
, &je
.desc
, "", " ", NULL
);
611 } else if (json_streq(map
, field
, "Filter")) {
612 addfield(map
, &filter
, "", "", val
);
613 } else if (json_streq(map
, field
, "ScaleUnit")) {
614 addfield(map
, &je
.unit
, "", "", val
);
615 } else if (json_streq(map
, field
, "PerPkg")) {
616 addfield(map
, &je
.perpkg
, "", "", val
);
617 } else if (json_streq(map
, field
, "Deprecated")) {
618 addfield(map
, &je
.deprecated
, "", "", val
);
619 } else if (json_streq(map
, field
, "MetricName")) {
620 addfield(map
, &je
.metric_name
, "", "", val
);
621 } else if (json_streq(map
, field
, "MetricGroup")) {
622 addfield(map
, &je
.metric_group
, "", "", val
);
623 } else if (json_streq(map
, field
, "MetricConstraint")) {
624 addfield(map
, &je
.metric_constraint
, "", "", val
);
625 } else if (json_streq(map
, field
, "MetricExpr")) {
626 addfield(map
, &je
.metric_expr
, "", "", val
);
627 for (s
= je
.metric_expr
; *s
; s
++)
629 } else if (json_streq(map
, field
, "ArchStdEvent")) {
630 addfield(map
, &arch_std
, "", "", val
);
631 for (s
= arch_std
; *s
; s
++)
634 /* ignore unknown fields */
636 if (precise
&& je
.desc
&& !strstr(je
.desc
, "(Precise Event)")) {
637 if (json_streq(map
, precise
, "2"))
638 addfield(map
, &extra_desc
, " ",
639 "(Must be precise)", NULL
);
641 addfield(map
, &extra_desc
, " ",
642 "(Precise event)", NULL
);
644 snprintf(buf
, sizeof buf
, "event=%#llx", eventcode
);
645 addfield(map
, &event
, ",", buf
, NULL
);
646 if (je
.desc
&& extra_desc
)
647 addfield(map
, &je
.desc
, " ", extra_desc
, NULL
);
648 if (je
.long_desc
&& extra_desc
)
649 addfield(map
, &je
.long_desc
, " ", extra_desc
, NULL
);
651 addfield(map
, &event
, ",", filter
, NULL
);
653 addfield(map
, &event
, ",", msr
->pname
, msrval
);
659 * An arch standard event is referenced, so try to
660 * fixup any unassigned values.
662 err
= try_fixup(fn
, arch_std
, eventcode
, &je
);
666 je
.event
= real_event(je
.name
, event
);
667 err
= func(data
, &je
);
679 free(je
.metric_expr
);
680 free(je
.metric_name
);
681 free(je
.metric_group
);
682 free(je
.metric_constraint
);
689 EXPECT(tok
- tokens
== len
, tok
, "unexpected objects at end");
692 free_json(map
, size
, tokens
);
696 static char *file_name_to_table_name(char *fname
)
704 * Ensure tablename starts with alphabetic character.
705 * Derive rest of table name from basename of the JSON file,
706 * replacing hyphens and stripping out .json suffix.
708 n
= asprintf(&tblname
, "pme_%s", fname
);
710 pr_info("%s: asprintf() error %s for file %s\n", prog
,
711 strerror(errno
), fname
);
715 for (i
= 0; i
< strlen(tblname
); i
++) {
718 if (c
== '-' || c
== '/')
723 } else if (!isalnum(c
) && c
!= '_') {
724 pr_err("%s: Invalid character '%c' in file name %s\n",
725 prog
, c
, basename(fname
));
735 static void print_mapping_table_prefix(FILE *outfp
)
737 fprintf(outfp
, "struct pmu_events_map pmu_events_map[] = {\n");
740 static void print_mapping_table_suffix(FILE *outfp
)
743 * Print the terminating, NULL entry.
745 fprintf(outfp
, "{\n");
746 fprintf(outfp
, "\t.cpuid = 0,\n");
747 fprintf(outfp
, "\t.version = 0,\n");
748 fprintf(outfp
, "\t.type = 0,\n");
749 fprintf(outfp
, "\t.table = 0,\n");
750 fprintf(outfp
, "},\n");
752 /* and finally, the closing curly bracket for the struct */
753 fprintf(outfp
, "};\n");
756 static void print_mapping_test_table(FILE *outfp
)
759 * Print the terminating, NULL entry.
761 fprintf(outfp
, "{\n");
762 fprintf(outfp
, "\t.cpuid = \"testcpu\",\n");
763 fprintf(outfp
, "\t.version = \"v1\",\n");
764 fprintf(outfp
, "\t.type = \"core\",\n");
765 fprintf(outfp
, "\t.table = pme_test_cpu,\n");
766 fprintf(outfp
, "},\n");
769 static int process_mapfile(FILE *outfp
, char *fpath
)
779 pr_info("%s: Processing mapfile %s\n", prog
, fpath
);
785 mapfp
= fopen(fpath
, "r");
787 pr_info("%s: Error %s opening %s\n", prog
, strerror(errno
),
793 print_mapping_table_prefix(outfp
);
795 /* Skip first line (header) */
796 p
= fgets(line
, n
, mapfp
);
802 char *cpuid
, *version
, *type
, *fname
;
805 p
= fgets(line
, n
, mapfp
);
809 if (line
[0] == '#' || line
[0] == '\n')
812 if (line
[strlen(line
)-1] != '\n') {
813 /* TODO Deal with lines longer than 16K */
814 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
815 prog
, fpath
, line_num
);
819 line
[strlen(line
)-1] = '\0';
821 cpuid
= fixregex(strtok_r(p
, ",", &save
));
822 version
= strtok_r(NULL
, ",", &save
);
823 fname
= strtok_r(NULL
, ",", &save
);
824 type
= strtok_r(NULL
, ",", &save
);
826 tblname
= file_name_to_table_name(fname
);
827 fprintf(outfp
, "{\n");
828 fprintf(outfp
, "\t.cpuid = \"%s\",\n", cpuid
);
829 fprintf(outfp
, "\t.version = \"%s\",\n", version
);
830 fprintf(outfp
, "\t.type = \"%s\",\n", type
);
833 * CHECK: We can't use the type (eg "core") field in the
834 * table name. For us to do that, we need to somehow tweak
835 * the other caller of file_name_to_table(), process_json()
836 * to determine the type. process_json() file has no way
837 * of knowing these are "core" events unless file name has
838 * core in it. If filename has core in it, we can safely
839 * ignore the type field here also.
841 fprintf(outfp
, "\t.table = %s\n", tblname
);
842 fprintf(outfp
, "},\n");
846 print_mapping_test_table(outfp
);
847 print_mapping_table_suffix(outfp
);
854 * If we fail to locate/process JSON and map files, create a NULL mapping
855 * table. This would at least allow perf to build even if we can't find/use
858 static void create_empty_mapping(const char *output_file
)
862 pr_info("%s: Creating empty pmu_events_map[] table\n", prog
);
864 /* Truncate file to clear any partial writes to it */
865 outfp
= fopen(output_file
, "w");
871 fprintf(outfp
, "#include \"pmu-events/pmu-events.h\"\n");
872 print_mapping_table_prefix(outfp
);
873 print_mapping_table_suffix(outfp
);
877 static int get_maxfds(void)
881 if (getrlimit(RLIMIT_NOFILE
, &rlim
) == 0)
882 return min((int)rlim
.rlim_max
/ 2, 512);
888 * nftw() doesn't let us pass an argument to the processing function,
889 * so use a global variables.
891 static FILE *eventsfp
;
892 static char *mapfile
;
894 static int is_leaf_dir(const char *fpath
)
904 while ((dir
= readdir(d
)) != NULL
) {
905 if (!strcmp(dir
->d_name
, ".") || !strcmp(dir
->d_name
, ".."))
908 if (dir
->d_type
== DT_DIR
) {
911 } else if (dir
->d_type
== DT_UNKNOWN
) {
915 sprintf(path
, "%s/%s", fpath
, dir
->d_name
);
919 if (S_ISDIR(st
.st_mode
)) {
931 static int is_json_file(const char *name
)
935 if (strlen(name
) < 5)
938 suffix
= name
+ strlen(name
) - 5;
940 if (strncmp(suffix
, ".json", 5) == 0)
945 static int preprocess_arch_std_files(const char *fpath
, const struct stat
*sb
,
946 int typeflag
, struct FTW
*ftwbuf
)
948 int level
= ftwbuf
->level
;
949 int is_file
= typeflag
== FTW_F
;
951 if (level
== 1 && is_file
&& is_json_file(fpath
))
952 return json_events(fpath
, save_arch_std_events
, (void *)sb
);
957 static int process_one_file(const char *fpath
, const struct stat
*sb
,
958 int typeflag
, struct FTW
*ftwbuf
)
960 char *tblname
, *bname
;
961 int is_dir
= typeflag
== FTW_D
;
962 int is_file
= typeflag
== FTW_F
;
963 int level
= ftwbuf
->level
;
966 if (level
== 2 && is_dir
) {
968 * For level 2 directory, bname will include parent name,
969 * like vendor/platform. So search back from platform dir
972 bname
= (char *) fpath
+ ftwbuf
->base
- 2;
980 bname
= (char *) fpath
+ ftwbuf
->base
;
982 pr_debug("%s %d %7jd %-20s %s\n",
983 is_file
? "f" : is_dir
? "d" : "x",
984 level
, sb
->st_size
, bname
, fpath
);
986 /* base dir or too deep */
987 if (level
== 0 || level
> 3)
991 /* model directory, reset topic */
992 if ((level
== 1 && is_dir
&& is_leaf_dir(fpath
)) ||
993 (level
== 2 && is_dir
)) {
995 print_events_table_suffix(eventsfp
);
998 * Drop file name suffix. Replace hyphens with underscores.
999 * Fail if file name contains any alphanum characters besides
1002 tblname
= file_name_to_table_name(bname
);
1004 pr_info("%s: Error determining table name for %s\n", prog
,
1009 print_events_table_prefix(eventsfp
, tblname
);
1014 * Save the mapfile name for now. We will process mapfile
1015 * after processing all JSON files (so we can write out the
1016 * mapping table after all PMU events tables).
1019 if (level
== 1 && is_file
) {
1020 if (!strcmp(bname
, "mapfile.csv")) {
1021 mapfile
= strdup(fpath
);
1025 pr_info("%s: Ignoring file %s\n", prog
, fpath
);
1030 * If the file name does not have a .json extension,
1031 * ignore it. It could be a readme.txt for instance.
1034 if (!is_json_file(bname
)) {
1035 pr_info("%s: Ignoring file without .json suffix %s\n", prog
,
1041 if (level
> 1 && add_topic(bname
))
1045 * Assume all other files are JSON files.
1047 * If mapfile refers to 'power7_core.json', we create a table
1048 * named 'power7_core'. Any inconsistencies between the mapfile
1049 * and directory tree could result in build failure due to table
1050 * names not being found.
1052 * Atleast for now, be strict with processing JSON file names.
1053 * i.e. if JSON file name cannot be mapped to C-style table name,
1057 struct perf_entry_data data
= {
1058 .topic
= get_topic(),
1062 err
= json_events(fpath
, print_events_table_entry
, &data
);
1071 #define PATH_MAX 4096
1075 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1076 * the set of JSON files for the architecture 'arch'.
1078 * From each JSON file, create a C-style "PMU events table" from the
1079 * JSON file (see struct pmu_event).
1081 * From the mapfile, create a mapping between the CPU revisions and
1082 * PMU event tables (see struct pmu_events_map).
1084 * Write out the PMU events tables and the mapping table to pmu-event.c.
1086 int main(int argc
, char *argv
[])
1090 char ldirname
[PATH_MAX
];
1092 const char *output_file
;
1093 const char *start_dirname
;
1096 prog
= basename(argv
[0]);
1098 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog
);
1103 start_dirname
= argv
[2];
1104 output_file
= argv
[3];
1107 verbose
= atoi(argv
[4]);
1109 eventsfp
= fopen(output_file
, "w");
1111 pr_err("%s Unable to create required file %s (%s)\n",
1112 prog
, output_file
, strerror(errno
));
1116 sprintf(ldirname
, "%s/%s", start_dirname
, arch
);
1118 /* If architecture does not have any event lists, bail out */
1119 if (stat(ldirname
, &stbuf
) < 0) {
1120 pr_info("%s: Arch %s has no PMU event lists\n", prog
, arch
);
1124 /* Include pmu-events.h first */
1125 fprintf(eventsfp
, "#include \"pmu-events/pmu-events.h\"\n");
1128 * The mapfile allows multiple CPUids to point to the same JSON file,
1129 * so, not sure if there is a need for symlinks within the pmu-events
1132 * For now, treat symlinks of JSON files as regular files and create
1133 * separate tables for each symlink (presumably, each symlink refers
1134 * to specific version of the CPU).
1137 maxfds
= get_maxfds();
1139 rc
= nftw(ldirname
, preprocess_arch_std_files
, maxfds
, 0);
1140 if (rc
&& verbose
) {
1141 pr_info("%s: Error preprocessing arch standard files %s\n",
1144 } else if (rc
< 0) {
1145 /* Make build fail */
1147 free_arch_std_events();
1153 rc
= nftw(ldirname
, process_one_file
, maxfds
, 0);
1154 if (rc
&& verbose
) {
1155 pr_info("%s: Error walking file tree %s\n", prog
, ldirname
);
1157 } else if (rc
< 0) {
1158 /* Make build fail */
1160 free_arch_std_events();
1162 goto out_free_mapfile
;
1167 sprintf(ldirname
, "%s/test", start_dirname
);
1169 rc
= nftw(ldirname
, process_one_file
, maxfds
, 0);
1170 if (rc
&& verbose
) {
1171 pr_info("%s: Error walking file tree %s rc=%d for test\n",
1172 prog
, ldirname
, rc
);
1174 } else if (rc
< 0) {
1175 /* Make build fail */
1176 free_arch_std_events();
1178 goto out_free_mapfile
;
1184 print_events_table_suffix(eventsfp
);
1187 pr_info("%s: No CPU->JSON mapping?\n", prog
);
1191 if (process_mapfile(eventsfp
, mapfile
)) {
1192 pr_info("%s: Error processing mapfile %s\n", prog
, mapfile
);
1193 /* Make build fail */
1195 free_arch_std_events();
1200 goto out_free_mapfile
;
1204 create_empty_mapping(output_file
);
1205 free_arch_std_events();