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"
70 char *metric_constraint
;
73 enum aggr_mode_class
convert(const char *aggr_mode
)
75 if (!strcmp(aggr_mode
, "PerCore"))
77 else if (!strcmp(aggr_mode
, "PerChip"))
80 pr_err("%s: Wrong AggregationMode value '%s'\n", prog
, aggr_mode
);
84 typedef int (*func
)(void *data
, struct json_event
*je
);
86 static LIST_HEAD(sys_event_tables
);
88 struct sys_event_table
{
89 struct list_head list
;
93 static void free_sys_event_tables(void)
95 struct sys_event_table
*et
, *next
;
97 list_for_each_entry_safe(et
, next
, &sys_event_tables
, list
) {
103 int eprintf(int level
, int var
, const char *fmt
, ...)
114 ret
= vfprintf(stderr
, fmt
, args
);
121 static void addfield(char *map
, char **dst
, const char *sep
,
122 const char *a
, jsmntok_t
*bt
)
124 unsigned int len
= strlen(a
) + 1 + strlen(sep
);
125 int olen
= *dst
? strlen(*dst
) : 0;
126 int blen
= bt
? json_len(bt
) : 0;
129 out
= realloc(*dst
, len
+ olen
+ blen
);
131 /* Don't add field in this case */
142 strncat(*dst
, map
+ bt
->start
, blen
);
145 static void fixname(char *s
)
151 static void fixdesc(char *s
)
153 char *e
= s
+ strlen(s
);
155 /* Remove trailing dots that look ugly in perf list */
157 while (e
>= s
&& isspace(*e
))
163 /* Add escapes for '\' so they are proper C strings. */
164 static char *fixregex(char *s
)
171 /* Count the number of '\' in string */
172 for (p
= s
; *p
; p
++) {
181 /* allocate space for a new string */
182 fixed
= (char *) malloc(len
+ esc_count
+ 1);
186 /* copy over the characters */
188 for (p
= s
; *p
; p
++) {
200 static struct msrmap
{
204 { "0x3F6", "ldlat=" },
205 { "0x1A6", "offcore_rsp=" },
206 { "0x1A7", "offcore_rsp=" },
207 { "0x3F7", "frontend=" },
211 static struct field
{
215 { "UMask", "umask=" },
216 { "CounterMask", "cmask=" },
217 { "Invert", "inv=" },
218 { "AnyThread", "any=" },
219 { "EdgeDetect", "edge=" },
220 { "SampleAfterValue", "period=" },
221 { "FCMask", "fc_mask=" },
222 { "PortMask", "ch_mask=" },
226 static void cut_comma(char *map
, jsmntok_t
*newval
)
230 /* Cut off everything after comma */
231 for (i
= newval
->start
; i
< newval
->end
; i
++) {
237 static int match_field(char *map
, jsmntok_t
*field
, int nz
,
238 char **event
, jsmntok_t
*val
)
241 jsmntok_t newval
= *val
;
243 for (f
= fields
; f
->field
; f
++)
244 if (json_streq(map
, field
, f
->field
) && nz
) {
245 cut_comma(map
, &newval
);
246 addfield(map
, event
, ",", f
->kernel
, &newval
);
252 static struct msrmap
*lookup_msr(char *map
, jsmntok_t
*val
)
254 jsmntok_t newval
= *val
;
258 cut_comma(map
, &newval
);
259 for (i
= 0; msrmap
[i
].num
; i
++)
260 if (json_streq(map
, &newval
, msrmap
[i
].num
))
264 pr_err("%s: Unknown MSR in event file %.*s\n", prog
,
265 json_len(val
), map
+ val
->start
);
274 { "CBO", "uncore_cbox" },
275 { "QPI LL", "uncore_qpi" },
276 { "SBO", "uncore_sbox" },
277 { "iMPH-U", "uncore_arb" },
278 { "CPU-M-CF", "cpum_cf" },
279 { "CPU-M-SF", "cpum_sf" },
280 { "UPI LL", "uncore_upi" },
281 { "hisi_sccl,ddrc", "hisi_sccl,ddrc" },
282 { "hisi_sccl,hha", "hisi_sccl,hha" },
283 { "hisi_sccl,l3c", "hisi_sccl,l3c" },
284 /* it's not realistic to keep adding these, we need something more scalable ... */
285 { "imx8_ddr", "imx8_ddr" },
286 { "L3PMC", "amd_l3" },
287 { "DFPMC", "amd_df" },
291 static const char *field_to_perf(struct map
*table
, char *map
, jsmntok_t
*val
)
295 for (i
= 0; table
[i
].json
; i
++) {
296 if (json_streq(map
, val
, table
[i
].json
))
297 return table
[i
].perf
;
302 #define EXPECT(e, t, m) do { if (!(e)) { \
303 jsmntok_t *loc = (t); \
304 if (!(t)->start && (t) > tokens) \
306 pr_err("%s:%d: " m ", got %s\n", fn, \
307 json_line(map, loc), \
315 static char *get_topic(void)
320 /* tp is free'd in process_one_file() */
321 i
= asprintf(&tp
, "%s", topic
);
323 pr_info("%s: asprintf() error %s\n", prog
);
327 for (i
= 0; i
< (int) strlen(tp
); i
++) {
341 static int add_topic(char *bname
)
344 topic
= strdup(bname
);
346 pr_info("%s: strdup() error %s for file %s\n", prog
,
347 strerror(errno
), bname
);
353 struct perf_entry_data
{
358 static int close_table
;
360 static void print_events_table_prefix(FILE *fp
, const char *tblname
)
362 fprintf(fp
, "struct pmu_event %s[] = {\n", tblname
);
366 static int print_events_table_entry(void *data
, struct json_event
*je
)
368 struct perf_entry_data
*pd
= data
;
369 FILE *outfp
= pd
->outfp
;
370 char *topic
= pd
->topic
;
373 * TODO: Remove formatting chars after debugging to reduce
376 fprintf(outfp
, "{\n");
379 fprintf(outfp
, "\t.name = \"%s\",\n", je
->name
);
381 fprintf(outfp
, "\t.event = \"%s\",\n", je
->event
);
382 fprintf(outfp
, "\t.desc = \"%s\",\n", je
->desc
);
384 fprintf(outfp
, "\t.compat = \"%s\",\n", je
->compat
);
385 fprintf(outfp
, "\t.topic = \"%s\",\n", topic
);
386 if (je
->long_desc
&& je
->long_desc
[0])
387 fprintf(outfp
, "\t.long_desc = \"%s\",\n", je
->long_desc
);
389 fprintf(outfp
, "\t.pmu = \"%s\",\n", je
->pmu
);
391 fprintf(outfp
, "\t.unit = \"%s\",\n", je
->unit
);
393 fprintf(outfp
, "\t.perpkg = \"%s\",\n", je
->perpkg
);
395 fprintf(outfp
, "\t.aggr_mode = \"%d\",\n", convert(je
->aggr_mode
));
397 fprintf(outfp
, "\t.metric_expr = \"%s\",\n", je
->metric_expr
);
399 fprintf(outfp
, "\t.metric_name = \"%s\",\n", je
->metric_name
);
400 if (je
->metric_group
)
401 fprintf(outfp
, "\t.metric_group = \"%s\",\n", je
->metric_group
);
403 fprintf(outfp
, "\t.deprecated = \"%s\",\n", je
->deprecated
);
404 if (je
->metric_constraint
)
405 fprintf(outfp
, "\t.metric_constraint = \"%s\",\n", je
->metric_constraint
);
406 fprintf(outfp
, "},\n");
411 struct event_struct
{
412 struct list_head list
;
426 char *metric_constraint
;
429 #define ADD_EVENT_FIELD(field) do { if (je->field) { \
430 es->field = strdup(je->field); \
435 #define FREE_EVENT_FIELD(field) free(es->field)
437 #define TRY_FIXUP_FIELD(field) do { if (es->field && !je->field) {\
438 je->field = strdup(es->field); \
443 #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
458 static LIST_HEAD(arch_std_events
);
460 static void free_arch_std_events(void)
462 struct event_struct
*es
, *next
;
464 list_for_each_entry_safe(es
, next
, &arch_std_events
, list
) {
465 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD
);
466 list_del_init(&es
->list
);
471 static int save_arch_std_events(void *data
, struct json_event
*je
)
473 struct event_struct
*es
;
475 es
= malloc(sizeof(*es
));
478 memset(es
, 0, sizeof(*es
));
479 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD
);
480 list_add_tail(&es
->list
, &arch_std_events
);
483 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD
);
488 static void print_events_table_suffix(FILE *outfp
)
490 fprintf(outfp
, "{\n");
492 fprintf(outfp
, "\t.name = 0,\n");
493 fprintf(outfp
, "\t.event = 0,\n");
494 fprintf(outfp
, "\t.desc = 0,\n");
496 fprintf(outfp
, "},\n");
497 fprintf(outfp
, "};\n");
501 static struct fixed
{
505 { "inst_retired.any", "event=0xc0,period=2000003" },
506 { "inst_retired.any_p", "event=0xc0,period=2000003" },
507 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03,period=2000003" },
508 { "cpu_clk_unhalted.thread", "event=0x3c,period=2000003" },
509 { "cpu_clk_unhalted.core", "event=0x3c,period=2000003" },
510 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1,period=2000003" },
515 * Handle different fixed counter encodings between JSON and perf.
517 static char *real_event(const char *name
, char *event
)
524 for (i
= 0; fixed
[i
].name
; i
++)
525 if (!strcasecmp(name
, fixed
[i
].name
))
526 return (char *)fixed
[i
].event
;
531 try_fixup(const char *fn
, char *arch_std
, struct json_event
*je
, char **event
)
533 /* try to find matching event from arch standard values */
534 struct event_struct
*es
;
536 list_for_each_entry(es
, &arch_std_events
, list
) {
537 if (!strcmp(arch_std
, es
->name
)) {
538 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD
);
544 pr_err("%s: could not find matching %s for %s\n",
549 /* Call func with each event in the json file */
550 static int json_events(const char *fn
,
551 int (*func
)(void *data
, struct json_event
*je
),
556 jsmntok_t
*tokens
, *tok
;
564 tokens
= parse_json(fn
, &map
, &size
, &len
);
567 EXPECT(tokens
->type
== JSMN_ARRAY
, tokens
, "expected top level array");
569 for (i
= 0; i
< tokens
->size
; i
++) {
571 char *extra_desc
= NULL
;
573 struct json_event je
= {};
574 char *arch_std
= NULL
;
575 unsigned long long eventcode
= 0;
576 struct msrmap
*msr
= NULL
;
577 jsmntok_t
*msrval
= NULL
;
578 jsmntok_t
*precise
= NULL
;
579 jsmntok_t
*obj
= tok
++;
581 EXPECT(obj
->type
== JSMN_OBJECT
, obj
, "expected object");
582 for (j
= 0; j
< obj
->size
; j
+= 2) {
583 jsmntok_t
*field
, *val
;
588 EXPECT(field
->type
== JSMN_STRING
, tok
+ j
,
589 "Expected field name");
591 EXPECT(val
->type
== JSMN_STRING
, tok
+ j
+ 1,
592 "Expected string value");
594 nz
= !json_streq(map
, val
, "0");
595 if (match_field(map
, field
, nz
, &event
, val
)) {
597 } else if (json_streq(map
, field
, "EventCode")) {
599 addfield(map
, &code
, "", "", val
);
600 eventcode
|= strtoul(code
, NULL
, 0);
602 } else if (json_streq(map
, field
, "ExtSel")) {
604 addfield(map
, &code
, "", "", val
);
605 eventcode
|= strtoul(code
, NULL
, 0) << 21;
607 } else if (json_streq(map
, field
, "EventName")) {
608 addfield(map
, &je
.name
, "", "", val
);
609 } else if (json_streq(map
, field
, "Compat")) {
610 addfield(map
, &je
.compat
, "", "", val
);
611 } else if (json_streq(map
, field
, "BriefDescription")) {
612 addfield(map
, &je
.desc
, "", "", val
);
614 } else if (json_streq(map
, field
,
615 "PublicDescription")) {
616 addfield(map
, &je
.long_desc
, "", "", val
);
617 fixdesc(je
.long_desc
);
618 } else if (json_streq(map
, field
, "PEBS") && nz
) {
620 } else if (json_streq(map
, field
, "MSRIndex") && nz
) {
621 msr
= lookup_msr(map
, val
);
622 } else if (json_streq(map
, field
, "MSRValue")) {
624 } else if (json_streq(map
, field
, "Errata") &&
625 !json_streq(map
, val
, "null")) {
626 addfield(map
, &extra_desc
, ". ",
627 " Spec update: ", val
);
628 } else if (json_streq(map
, field
, "Data_LA") && nz
) {
629 addfield(map
, &extra_desc
, ". ",
630 " Supports address when precise",
632 } else if (json_streq(map
, field
, "Unit")) {
635 ppmu
= field_to_perf(unit_to_pmu
, map
, val
);
637 je
.pmu
= strdup(ppmu
);
640 je
.pmu
= strdup("uncore_");
641 addfield(map
, &je
.pmu
, "", "", val
);
642 for (s
= je
.pmu
; *s
; s
++)
645 addfield(map
, &je
.desc
, ". ", "Unit: ", NULL
);
646 addfield(map
, &je
.desc
, "", je
.pmu
, NULL
);
647 addfield(map
, &je
.desc
, "", " ", NULL
);
648 } else if (json_streq(map
, field
, "Filter")) {
649 addfield(map
, &filter
, "", "", val
);
650 } else if (json_streq(map
, field
, "ScaleUnit")) {
651 addfield(map
, &je
.unit
, "", "", val
);
652 } else if (json_streq(map
, field
, "PerPkg")) {
653 addfield(map
, &je
.perpkg
, "", "", val
);
654 } else if (json_streq(map
, field
, "AggregationMode")) {
655 addfield(map
, &je
.aggr_mode
, "", "", val
);
656 } else if (json_streq(map
, field
, "Deprecated")) {
657 addfield(map
, &je
.deprecated
, "", "", val
);
658 } else if (json_streq(map
, field
, "MetricName")) {
659 addfield(map
, &je
.metric_name
, "", "", val
);
660 } else if (json_streq(map
, field
, "MetricGroup")) {
661 addfield(map
, &je
.metric_group
, "", "", val
);
662 } else if (json_streq(map
, field
, "MetricConstraint")) {
663 addfield(map
, &je
.metric_constraint
, "", "", val
);
664 } else if (json_streq(map
, field
, "MetricExpr")) {
665 addfield(map
, &je
.metric_expr
, "", "", val
);
666 for (s
= je
.metric_expr
; *s
; s
++)
668 } else if (json_streq(map
, field
, "ArchStdEvent")) {
669 addfield(map
, &arch_std
, "", "", val
);
670 for (s
= arch_std
; *s
; s
++)
673 /* ignore unknown fields */
675 if (precise
&& je
.desc
&& !strstr(je
.desc
, "(Precise Event)")) {
676 if (json_streq(map
, precise
, "2"))
677 addfield(map
, &extra_desc
, " ",
678 "(Must be precise)", NULL
);
680 addfield(map
, &extra_desc
, " ",
681 "(Precise event)", NULL
);
683 snprintf(buf
, sizeof buf
, "event=%#llx", eventcode
);
684 addfield(map
, &event
, ",", buf
, NULL
);
685 if (je
.desc
&& extra_desc
)
686 addfield(map
, &je
.desc
, " ", extra_desc
, NULL
);
687 if (je
.long_desc
&& extra_desc
)
688 addfield(map
, &je
.long_desc
, " ", extra_desc
, NULL
);
690 addfield(map
, &event
, ",", filter
, NULL
);
692 addfield(map
, &event
, ",", msr
->pname
, msrval
);
698 * An arch standard event is referenced, so try to
699 * fixup any unassigned values.
701 err
= try_fixup(fn
, arch_std
, &je
, &event
);
705 je
.event
= real_event(je
.name
, event
);
706 err
= func(data
, &je
);
720 free(je
.metric_expr
);
721 free(je
.metric_name
);
722 free(je
.metric_group
);
723 free(je
.metric_constraint
);
730 EXPECT(tok
- tokens
== len
, tok
, "unexpected objects at end");
733 free_json(map
, size
, tokens
);
737 static char *file_name_to_table_name(char *fname
)
745 * Ensure tablename starts with alphabetic character.
746 * Derive rest of table name from basename of the JSON file,
747 * replacing hyphens and stripping out .json suffix.
749 n
= asprintf(&tblname
, "pme_%s", fname
);
751 pr_info("%s: asprintf() error %s for file %s\n", prog
,
752 strerror(errno
), fname
);
756 for (i
= 0; i
< strlen(tblname
); i
++) {
759 if (c
== '-' || c
== '/')
764 } else if (!isalnum(c
) && c
!= '_') {
765 pr_err("%s: Invalid character '%c' in file name %s\n",
766 prog
, c
, basename(fname
));
776 static bool is_sys_dir(char *fname
)
778 size_t len
= strlen(fname
), len2
= strlen("/sys");
782 return !strcmp(fname
+len
-len2
, "/sys");
785 static void print_mapping_table_prefix(FILE *outfp
)
787 fprintf(outfp
, "struct pmu_events_map pmu_events_map[] = {\n");
790 static void print_mapping_table_suffix(FILE *outfp
)
793 * Print the terminating, NULL entry.
795 fprintf(outfp
, "{\n");
796 fprintf(outfp
, "\t.cpuid = 0,\n");
797 fprintf(outfp
, "\t.version = 0,\n");
798 fprintf(outfp
, "\t.type = 0,\n");
799 fprintf(outfp
, "\t.table = 0,\n");
800 fprintf(outfp
, "},\n");
802 /* and finally, the closing curly bracket for the struct */
803 fprintf(outfp
, "};\n");
806 static void print_mapping_test_table(FILE *outfp
)
809 * Print the terminating, NULL entry.
811 fprintf(outfp
, "{\n");
812 fprintf(outfp
, "\t.cpuid = \"testcpu\",\n");
813 fprintf(outfp
, "\t.version = \"v1\",\n");
814 fprintf(outfp
, "\t.type = \"core\",\n");
815 fprintf(outfp
, "\t.table = pme_test_cpu,\n");
816 fprintf(outfp
, "},\n");
819 static void print_system_event_mapping_table_prefix(FILE *outfp
)
821 fprintf(outfp
, "\nstruct pmu_sys_events pmu_sys_event_tables[] = {");
824 static void print_system_event_mapping_table_suffix(FILE *outfp
)
826 fprintf(outfp
, "\n\t{\n\t\t.table = 0\n\t},");
827 fprintf(outfp
, "\n};\n");
830 static int process_system_event_tables(FILE *outfp
)
832 struct sys_event_table
*sys_event_table
;
834 print_system_event_mapping_table_prefix(outfp
);
836 list_for_each_entry(sys_event_table
, &sys_event_tables
, list
) {
837 fprintf(outfp
, "\n\t{\n\t\t.table = %s,\n\t},",
838 sys_event_table
->soc_id
);
841 print_system_event_mapping_table_suffix(outfp
);
846 static int process_mapfile(FILE *outfp
, char *fpath
)
856 pr_info("%s: Processing mapfile %s\n", prog
, fpath
);
862 mapfp
= fopen(fpath
, "r");
864 pr_info("%s: Error %s opening %s\n", prog
, strerror(errno
),
870 print_mapping_table_prefix(outfp
);
872 /* Skip first line (header) */
873 p
= fgets(line
, n
, mapfp
);
879 char *cpuid
, *version
, *type
, *fname
;
882 p
= fgets(line
, n
, mapfp
);
886 if (line
[0] == '#' || line
[0] == '\n')
889 if (line
[strlen(line
)-1] != '\n') {
890 /* TODO Deal with lines longer than 16K */
891 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
892 prog
, fpath
, line_num
);
896 line
[strlen(line
)-1] = '\0';
898 cpuid
= fixregex(strtok_r(p
, ",", &save
));
899 version
= strtok_r(NULL
, ",", &save
);
900 fname
= strtok_r(NULL
, ",", &save
);
901 type
= strtok_r(NULL
, ",", &save
);
903 tblname
= file_name_to_table_name(fname
);
904 fprintf(outfp
, "{\n");
905 fprintf(outfp
, "\t.cpuid = \"%s\",\n", cpuid
);
906 fprintf(outfp
, "\t.version = \"%s\",\n", version
);
907 fprintf(outfp
, "\t.type = \"%s\",\n", type
);
910 * CHECK: We can't use the type (eg "core") field in the
911 * table name. For us to do that, we need to somehow tweak
912 * the other caller of file_name_to_table(), process_json()
913 * to determine the type. process_json() file has no way
914 * of knowing these are "core" events unless file name has
915 * core in it. If filename has core in it, we can safely
916 * ignore the type field here also.
918 fprintf(outfp
, "\t.table = %s\n", tblname
);
919 fprintf(outfp
, "},\n");
923 print_mapping_test_table(outfp
);
924 print_mapping_table_suffix(outfp
);
931 * If we fail to locate/process JSON and map files, create a NULL mapping
932 * table. This would at least allow perf to build even if we can't find/use
935 static void create_empty_mapping(const char *output_file
)
939 pr_info("%s: Creating empty pmu_events_map[] table\n", prog
);
941 /* Truncate file to clear any partial writes to it */
942 outfp
= fopen(output_file
, "w");
948 fprintf(outfp
, "#include \"pmu-events/pmu-events.h\"\n");
949 print_mapping_table_prefix(outfp
);
950 print_mapping_table_suffix(outfp
);
951 print_system_event_mapping_table_prefix(outfp
);
952 print_system_event_mapping_table_suffix(outfp
);
956 static int get_maxfds(void)
960 if (getrlimit(RLIMIT_NOFILE
, &rlim
) == 0)
961 return min((int)rlim
.rlim_max
/ 2, 512);
967 * nftw() doesn't let us pass an argument to the processing function,
968 * so use a global variables.
970 static FILE *eventsfp
;
971 static char *mapfile
;
973 static int is_leaf_dir(const char *fpath
)
983 while ((dir
= readdir(d
)) != NULL
) {
984 if (!strcmp(dir
->d_name
, ".") || !strcmp(dir
->d_name
, ".."))
987 if (dir
->d_type
== DT_DIR
) {
990 } else if (dir
->d_type
== DT_UNKNOWN
) {
994 sprintf(path
, "%s/%s", fpath
, dir
->d_name
);
998 if (S_ISDIR(st
.st_mode
)) {
1010 static int is_json_file(const char *name
)
1014 if (strlen(name
) < 5)
1017 suffix
= name
+ strlen(name
) - 5;
1019 if (strncmp(suffix
, ".json", 5) == 0)
1024 static int preprocess_arch_std_files(const char *fpath
, const struct stat
*sb
,
1025 int typeflag
, struct FTW
*ftwbuf
)
1027 int level
= ftwbuf
->level
;
1028 int is_file
= typeflag
== FTW_F
;
1030 if (level
== 1 && is_file
&& is_json_file(fpath
))
1031 return json_events(fpath
, save_arch_std_events
, (void *)sb
);
1036 static int process_one_file(const char *fpath
, const struct stat
*sb
,
1037 int typeflag
, struct FTW
*ftwbuf
)
1039 char *tblname
, *bname
;
1040 int is_dir
= typeflag
== FTW_D
;
1041 int is_file
= typeflag
== FTW_F
;
1042 int level
= ftwbuf
->level
;
1045 if (level
>= 2 && is_dir
) {
1048 * For level 2 directory, bname will include parent name,
1049 * like vendor/platform. So search back from platform dir
1051 * Something similar for level 3 directory, but we're a PMU
1052 * category folder, like vendor/platform/cpu.
1054 bname
= (char *) fpath
+ ftwbuf
->base
- 2;
1058 if (count
== level
- 1)
1064 bname
= (char *) fpath
+ ftwbuf
->base
;
1066 pr_debug("%s %d %7jd %-20s %s\n",
1067 is_file
? "f" : is_dir
? "d" : "x",
1068 level
, sb
->st_size
, bname
, fpath
);
1070 /* base dir or too deep */
1071 if (level
== 0 || level
> 4)
1075 /* model directory, reset topic */
1076 if ((level
== 1 && is_dir
&& is_leaf_dir(fpath
)) ||
1077 (level
>= 2 && is_dir
&& is_leaf_dir(fpath
))) {
1079 print_events_table_suffix(eventsfp
);
1082 * Drop file name suffix. Replace hyphens with underscores.
1083 * Fail if file name contains any alphanum characters besides
1086 tblname
= file_name_to_table_name(bname
);
1088 pr_info("%s: Error determining table name for %s\n", prog
,
1093 if (is_sys_dir(bname
)) {
1094 struct sys_event_table
*sys_event_table
;
1096 sys_event_table
= malloc(sizeof(*sys_event_table
));
1097 if (!sys_event_table
)
1100 sys_event_table
->soc_id
= strdup(tblname
);
1101 if (!sys_event_table
->soc_id
) {
1102 free(sys_event_table
);
1105 list_add_tail(&sys_event_table
->list
,
1109 print_events_table_prefix(eventsfp
, tblname
);
1114 * Save the mapfile name for now. We will process mapfile
1115 * after processing all JSON files (so we can write out the
1116 * mapping table after all PMU events tables).
1119 if (level
== 1 && is_file
) {
1120 if (!strcmp(bname
, "mapfile.csv")) {
1121 mapfile
= strdup(fpath
);
1125 pr_info("%s: Ignoring file %s\n", prog
, fpath
);
1130 * If the file name does not have a .json extension,
1131 * ignore it. It could be a readme.txt for instance.
1134 if (!is_json_file(bname
)) {
1135 pr_info("%s: Ignoring file without .json suffix %s\n", prog
,
1141 if (level
> 1 && add_topic(bname
))
1145 * Assume all other files are JSON files.
1147 * If mapfile refers to 'power7_core.json', we create a table
1148 * named 'power7_core'. Any inconsistencies between the mapfile
1149 * and directory tree could result in build failure due to table
1150 * names not being found.
1152 * Atleast for now, be strict with processing JSON file names.
1153 * i.e. if JSON file name cannot be mapped to C-style table name,
1157 struct perf_entry_data data
= {
1158 .topic
= get_topic(),
1162 err
= json_events(fpath
, print_events_table_entry
, &data
);
1171 #define PATH_MAX 4096
1175 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1176 * the set of JSON files for the architecture 'arch'.
1178 * From each JSON file, create a C-style "PMU events table" from the
1179 * JSON file (see struct pmu_event).
1181 * From the mapfile, create a mapping between the CPU revisions and
1182 * PMU event tables (see struct pmu_events_map).
1184 * Write out the PMU events tables and the mapping table to pmu-event.c.
1186 int main(int argc
, char *argv
[])
1188 int rc
, ret
= 0, empty_map
= 0;
1190 char ldirname
[PATH_MAX
];
1192 const char *output_file
;
1193 const char *start_dirname
;
1194 char *err_string_ext
= "";
1197 prog
= basename(argv
[0]);
1199 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog
);
1204 start_dirname
= argv
[2];
1205 output_file
= argv
[3];
1208 verbose
= atoi(argv
[4]);
1210 eventsfp
= fopen(output_file
, "w");
1212 pr_err("%s Unable to create required file %s (%s)\n",
1213 prog
, output_file
, strerror(errno
));
1217 sprintf(ldirname
, "%s/%s", start_dirname
, arch
);
1219 /* If architecture does not have any event lists, bail out */
1220 if (stat(ldirname
, &stbuf
) < 0) {
1221 pr_info("%s: Arch %s has no PMU event lists\n", prog
, arch
);
1223 goto err_close_eventsfp
;
1226 /* Include pmu-events.h first */
1227 fprintf(eventsfp
, "#include \"pmu-events/pmu-events.h\"\n");
1230 * The mapfile allows multiple CPUids to point to the same JSON file,
1231 * so, not sure if there is a need for symlinks within the pmu-events
1234 * For now, treat symlinks of JSON files as regular files and create
1235 * separate tables for each symlink (presumably, each symlink refers
1236 * to specific version of the CPU).
1239 maxfds
= get_maxfds();
1240 rc
= nftw(ldirname
, preprocess_arch_std_files
, maxfds
, 0);
1242 goto err_processing_std_arch_event_dir
;
1244 rc
= nftw(ldirname
, process_one_file
, maxfds
, 0);
1246 goto err_processing_dir
;
1248 sprintf(ldirname
, "%s/test", start_dirname
);
1250 rc
= nftw(ldirname
, preprocess_arch_std_files
, maxfds
, 0);
1252 goto err_processing_std_arch_event_dir
;
1254 rc
= nftw(ldirname
, process_one_file
, maxfds
, 0);
1256 goto err_processing_dir
;
1259 print_events_table_suffix(eventsfp
);
1262 pr_info("%s: No CPU->JSON mapping?\n", prog
);
1264 goto err_close_eventsfp
;
1267 rc
= process_mapfile(eventsfp
, mapfile
);
1269 pr_info("%s: Error processing mapfile %s\n", prog
, mapfile
);
1270 /* Make build fail */
1272 goto err_close_eventsfp
;
1275 rc
= process_system_event_tables(eventsfp
);
1282 free_arch_std_events();
1286 err_processing_std_arch_event_dir
:
1287 err_string_ext
= " for std arch event";
1290 pr_info("%s: Error walking file tree %s%s\n", prog
, ldirname
,
1293 } else if (rc
< 0) {
1301 create_empty_mapping(output_file
);
1303 free_arch_std_events();