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.
43 #include <sys/time.h> /* getrlimit */
44 #include <sys/resource.h> /* getrlimit */
54 int eprintf(int level
, int var
, const char *fmt
, ...)
65 ret
= vfprintf(stderr
, fmt
, args
);
72 __attribute__((weak
)) char *get_cpu_str(void)
77 static void addfield(char *map
, char **dst
, const char *sep
,
78 const char *a
, jsmntok_t
*bt
)
80 unsigned int len
= strlen(a
) + 1 + strlen(sep
);
81 int olen
= *dst
? strlen(*dst
) : 0;
82 int blen
= bt
? json_len(bt
) : 0;
85 out
= realloc(*dst
, len
+ olen
+ blen
);
87 /* Don't add field in this case */
98 strncat(*dst
, map
+ bt
->start
, blen
);
101 static void fixname(char *s
)
107 static void fixdesc(char *s
)
109 char *e
= s
+ strlen(s
);
111 /* Remove trailing dots that look ugly in perf list */
113 while (e
>= s
&& isspace(*e
))
119 /* Add escapes for '\' so they are proper C strings. */
120 static char *fixregex(char *s
)
127 /* Count the number of '\' in string */
128 for (p
= s
; *p
; p
++) {
137 /* allocate space for a new string */
138 fixed
= (char *) malloc(len
+ 1);
142 /* copy over the characters */
144 for (p
= s
; *p
; p
++) {
156 static struct msrmap
{
160 { "0x3F6", "ldlat=" },
161 { "0x1A6", "offcore_rsp=" },
162 { "0x1A7", "offcore_rsp=" },
163 { "0x3F7", "frontend=" },
167 static struct field
{
171 { "UMask", "umask=" },
172 { "CounterMask", "cmask=" },
173 { "Invert", "inv=" },
174 { "AnyThread", "any=" },
175 { "EdgeDetect", "edge=" },
176 { "SampleAfterValue", "period=" },
177 { "FCMask", "fc_mask=" },
178 { "PortMask", "ch_mask=" },
182 static void cut_comma(char *map
, jsmntok_t
*newval
)
186 /* Cut off everything after comma */
187 for (i
= newval
->start
; i
< newval
->end
; i
++) {
193 static int match_field(char *map
, jsmntok_t
*field
, int nz
,
194 char **event
, jsmntok_t
*val
)
197 jsmntok_t newval
= *val
;
199 for (f
= fields
; f
->field
; f
++)
200 if (json_streq(map
, field
, f
->field
) && nz
) {
201 cut_comma(map
, &newval
);
202 addfield(map
, event
, ",", f
->kernel
, &newval
);
208 static struct msrmap
*lookup_msr(char *map
, jsmntok_t
*val
)
210 jsmntok_t newval
= *val
;
214 cut_comma(map
, &newval
);
215 for (i
= 0; msrmap
[i
].num
; i
++)
216 if (json_streq(map
, &newval
, msrmap
[i
].num
))
220 pr_err("%s: Unknown MSR in event file %.*s\n", prog
,
221 json_len(val
), map
+ val
->start
);
230 { "CBO", "uncore_cbox" },
231 { "QPI LL", "uncore_qpi" },
232 { "SBO", "uncore_sbox" },
233 { "iMPH-U", "uncore_arb" },
237 static const char *field_to_perf(struct map
*table
, char *map
, jsmntok_t
*val
)
241 for (i
= 0; table
[i
].json
; i
++) {
242 if (json_streq(map
, val
, table
[i
].json
))
243 return table
[i
].perf
;
248 #define EXPECT(e, t, m) do { if (!(e)) { \
249 jsmntok_t *loc = (t); \
250 if (!(t)->start && (t) > tokens) \
252 pr_err("%s:%d: " m ", got %s\n", fn, \
253 json_line(map, loc), \
258 #define TOPIC_DEPTH 256
259 static char *topic_array
[TOPIC_DEPTH
];
260 static int topic_level
;
262 static char *get_topic(void)
264 char *tp_old
, *tp
= NULL
;
267 for (i
= 0; i
< topic_level
+ 1; i
++) {
271 n
= asprintf(&tp
, "%s%s", tp
?: "", topic_array
[i
]);
273 pr_info("%s: asprintf() error %s\n", prog
);
279 for (i
= 0; i
< (int) strlen(tp
); i
++) {
293 static int add_topic(int level
, char *bname
)
299 if (level
>= TOPIC_DEPTH
)
302 topic
= strdup(bname
);
304 pr_info("%s: strdup() error %s for file %s\n", prog
,
305 strerror(errno
), bname
);
309 free(topic_array
[topic_level
]);
310 topic_array
[topic_level
] = topic
;
315 struct perf_entry_data
{
320 static int close_table
;
322 static void print_events_table_prefix(FILE *fp
, const char *tblname
)
324 fprintf(fp
, "struct pmu_event %s[] = {\n", tblname
);
328 static int print_events_table_entry(void *data
, char *name
, char *event
,
329 char *desc
, char *long_desc
,
330 char *pmu
, char *unit
, char *perpkg
,
332 char *metric_name
, char *metric_group
)
334 struct perf_entry_data
*pd
= data
;
335 FILE *outfp
= pd
->outfp
;
336 char *topic
= pd
->topic
;
339 * TODO: Remove formatting chars after debugging to reduce
342 fprintf(outfp
, "{\n");
345 fprintf(outfp
, "\t.name = \"%s\",\n", name
);
347 fprintf(outfp
, "\t.event = \"%s\",\n", event
);
348 fprintf(outfp
, "\t.desc = \"%s\",\n", desc
);
349 fprintf(outfp
, "\t.topic = \"%s\",\n", topic
);
350 if (long_desc
&& long_desc
[0])
351 fprintf(outfp
, "\t.long_desc = \"%s\",\n", long_desc
);
353 fprintf(outfp
, "\t.pmu = \"%s\",\n", pmu
);
355 fprintf(outfp
, "\t.unit = \"%s\",\n", unit
);
357 fprintf(outfp
, "\t.perpkg = \"%s\",\n", perpkg
);
359 fprintf(outfp
, "\t.metric_expr = \"%s\",\n", metric_expr
);
361 fprintf(outfp
, "\t.metric_name = \"%s\",\n", metric_name
);
363 fprintf(outfp
, "\t.metric_group = \"%s\",\n", metric_group
);
364 fprintf(outfp
, "},\n");
369 static void print_events_table_suffix(FILE *outfp
)
371 fprintf(outfp
, "{\n");
373 fprintf(outfp
, "\t.name = 0,\n");
374 fprintf(outfp
, "\t.event = 0,\n");
375 fprintf(outfp
, "\t.desc = 0,\n");
377 fprintf(outfp
, "},\n");
378 fprintf(outfp
, "};\n");
382 static struct fixed
{
386 { "inst_retired.any", "event=0xc0" },
387 { "inst_retired.any_p", "event=0xc0" },
388 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03" },
389 { "cpu_clk_unhalted.thread", "event=0x3c" },
390 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1" },
395 * Handle different fixed counter encodings between JSON and perf.
397 static char *real_event(const char *name
, char *event
)
404 for (i
= 0; fixed
[i
].name
; i
++)
405 if (!strcasecmp(name
, fixed
[i
].name
))
406 return (char *)fixed
[i
].event
;
410 /* Call func with each event in the json file */
411 int json_events(const char *fn
,
412 int (*func
)(void *data
, char *name
, char *event
, char *desc
,
414 char *pmu
, char *unit
, char *perpkg
,
416 char *metric_name
, char *metric_group
),
421 jsmntok_t
*tokens
, *tok
;
429 tokens
= parse_json(fn
, &map
, &size
, &len
);
432 EXPECT(tokens
->type
== JSMN_ARRAY
, tokens
, "expected top level array");
434 for (i
= 0; i
< tokens
->size
; i
++) {
435 char *event
= NULL
, *desc
= NULL
, *name
= NULL
;
436 char *long_desc
= NULL
;
437 char *extra_desc
= NULL
;
442 char *metric_expr
= NULL
;
443 char *metric_name
= NULL
;
444 char *metric_group
= NULL
;
445 unsigned long long eventcode
= 0;
446 struct msrmap
*msr
= NULL
;
447 jsmntok_t
*msrval
= NULL
;
448 jsmntok_t
*precise
= NULL
;
449 jsmntok_t
*obj
= tok
++;
451 EXPECT(obj
->type
== JSMN_OBJECT
, obj
, "expected object");
452 for (j
= 0; j
< obj
->size
; j
+= 2) {
453 jsmntok_t
*field
, *val
;
458 EXPECT(field
->type
== JSMN_STRING
, tok
+ j
,
459 "Expected field name");
461 EXPECT(val
->type
== JSMN_STRING
, tok
+ j
+ 1,
462 "Expected string value");
464 nz
= !json_streq(map
, val
, "0");
465 if (match_field(map
, field
, nz
, &event
, val
)) {
467 } else if (json_streq(map
, field
, "EventCode")) {
469 addfield(map
, &code
, "", "", val
);
470 eventcode
|= strtoul(code
, NULL
, 0);
472 } else if (json_streq(map
, field
, "ExtSel")) {
474 addfield(map
, &code
, "", "", val
);
475 eventcode
|= strtoul(code
, NULL
, 0) << 21;
477 } else if (json_streq(map
, field
, "EventName")) {
478 addfield(map
, &name
, "", "", val
);
479 } else if (json_streq(map
, field
, "BriefDescription")) {
480 addfield(map
, &desc
, "", "", val
);
482 } else if (json_streq(map
, field
,
483 "PublicDescription")) {
484 addfield(map
, &long_desc
, "", "", val
);
486 } else if (json_streq(map
, field
, "PEBS") && nz
) {
488 } else if (json_streq(map
, field
, "MSRIndex") && nz
) {
489 msr
= lookup_msr(map
, val
);
490 } else if (json_streq(map
, field
, "MSRValue")) {
492 } else if (json_streq(map
, field
, "Errata") &&
493 !json_streq(map
, val
, "null")) {
494 addfield(map
, &extra_desc
, ". ",
495 " Spec update: ", val
);
496 } else if (json_streq(map
, field
, "Data_LA") && nz
) {
497 addfield(map
, &extra_desc
, ". ",
498 " Supports address when precise",
500 } else if (json_streq(map
, field
, "Unit")) {
503 ppmu
= field_to_perf(unit_to_pmu
, map
, val
);
508 pmu
= strdup("uncore_");
509 addfield(map
, &pmu
, "", "", val
);
510 for (s
= pmu
; *s
; s
++)
513 addfield(map
, &desc
, ". ", "Unit: ", NULL
);
514 addfield(map
, &desc
, "", pmu
, NULL
);
515 addfield(map
, &desc
, "", " ", NULL
);
516 } else if (json_streq(map
, field
, "Filter")) {
517 addfield(map
, &filter
, "", "", val
);
518 } else if (json_streq(map
, field
, "ScaleUnit")) {
519 addfield(map
, &unit
, "", "", val
);
520 } else if (json_streq(map
, field
, "PerPkg")) {
521 addfield(map
, &perpkg
, "", "", val
);
522 } else if (json_streq(map
, field
, "MetricName")) {
523 addfield(map
, &metric_name
, "", "", val
);
524 } else if (json_streq(map
, field
, "MetricGroup")) {
525 addfield(map
, &metric_group
, "", "", val
);
526 } else if (json_streq(map
, field
, "MetricExpr")) {
527 addfield(map
, &metric_expr
, "", "", val
);
528 for (s
= metric_expr
; *s
; s
++)
531 /* ignore unknown fields */
533 if (precise
&& desc
&& !strstr(desc
, "(Precise Event)")) {
534 if (json_streq(map
, precise
, "2"))
535 addfield(map
, &extra_desc
, " ",
536 "(Must be precise)", NULL
);
538 addfield(map
, &extra_desc
, " ",
539 "(Precise event)", NULL
);
541 snprintf(buf
, sizeof buf
, "event=%#llx", eventcode
);
542 addfield(map
, &event
, ",", buf
, NULL
);
543 if (desc
&& extra_desc
)
544 addfield(map
, &desc
, " ", extra_desc
, NULL
);
545 if (long_desc
&& extra_desc
)
546 addfield(map
, &long_desc
, " ", extra_desc
, NULL
);
548 addfield(map
, &event
, ",", filter
, NULL
);
550 addfield(map
, &event
, ",", msr
->pname
, msrval
);
554 err
= func(data
, name
, real_event(name
, event
), desc
, long_desc
,
555 pmu
, unit
, perpkg
, metric_expr
, metric_name
, metric_group
);
572 EXPECT(tok
- tokens
== len
, tok
, "unexpected objects at end");
575 free_json(map
, size
, tokens
);
579 static char *file_name_to_table_name(char *fname
)
587 * Ensure tablename starts with alphabetic character.
588 * Derive rest of table name from basename of the JSON file,
589 * replacing hyphens and stripping out .json suffix.
591 n
= asprintf(&tblname
, "pme_%s", basename(fname
));
593 pr_info("%s: asprintf() error %s for file %s\n", prog
,
594 strerror(errno
), fname
);
598 for (i
= 0; i
< strlen(tblname
); i
++) {
606 } else if (!isalnum(c
) && c
!= '_') {
607 pr_err("%s: Invalid character '%c' in file name %s\n",
608 prog
, c
, basename(fname
));
618 static void print_mapping_table_prefix(FILE *outfp
)
620 fprintf(outfp
, "struct pmu_events_map pmu_events_map[] = {\n");
623 static void print_mapping_table_suffix(FILE *outfp
)
626 * Print the terminating, NULL entry.
628 fprintf(outfp
, "{\n");
629 fprintf(outfp
, "\t.cpuid = 0,\n");
630 fprintf(outfp
, "\t.version = 0,\n");
631 fprintf(outfp
, "\t.type = 0,\n");
632 fprintf(outfp
, "\t.table = 0,\n");
633 fprintf(outfp
, "},\n");
635 /* and finally, the closing curly bracket for the struct */
636 fprintf(outfp
, "};\n");
639 static int process_mapfile(FILE *outfp
, char *fpath
)
648 pr_info("%s: Processing mapfile %s\n", prog
, fpath
);
654 mapfp
= fopen(fpath
, "r");
656 pr_info("%s: Error %s opening %s\n", prog
, strerror(errno
),
661 print_mapping_table_prefix(outfp
);
663 /* Skip first line (header) */
664 p
= fgets(line
, n
, mapfp
);
670 char *cpuid
, *version
, *type
, *fname
;
673 p
= fgets(line
, n
, mapfp
);
677 if (line
[0] == '#' || line
[0] == '\n')
680 if (line
[strlen(line
)-1] != '\n') {
681 /* TODO Deal with lines longer than 16K */
682 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
683 prog
, fpath
, line_num
);
686 line
[strlen(line
)-1] = '\0';
688 cpuid
= fixregex(strtok_r(p
, ",", &save
));
689 version
= strtok_r(NULL
, ",", &save
);
690 fname
= strtok_r(NULL
, ",", &save
);
691 type
= strtok_r(NULL
, ",", &save
);
693 tblname
= file_name_to_table_name(fname
);
694 fprintf(outfp
, "{\n");
695 fprintf(outfp
, "\t.cpuid = \"%s\",\n", cpuid
);
696 fprintf(outfp
, "\t.version = \"%s\",\n", version
);
697 fprintf(outfp
, "\t.type = \"%s\",\n", type
);
700 * CHECK: We can't use the type (eg "core") field in the
701 * table name. For us to do that, we need to somehow tweak
702 * the other caller of file_name_to_table(), process_json()
703 * to determine the type. process_json() file has no way
704 * of knowing these are "core" events unless file name has
705 * core in it. If filename has core in it, we can safely
706 * ignore the type field here also.
708 fprintf(outfp
, "\t.table = %s\n", tblname
);
709 fprintf(outfp
, "},\n");
713 print_mapping_table_suffix(outfp
);
718 * If we fail to locate/process JSON and map files, create a NULL mapping
719 * table. This would at least allow perf to build even if we can't find/use
722 static void create_empty_mapping(const char *output_file
)
726 pr_info("%s: Creating empty pmu_events_map[] table\n", prog
);
728 /* Truncate file to clear any partial writes to it */
729 outfp
= fopen(output_file
, "w");
735 fprintf(outfp
, "#include \"../../pmu-events/pmu-events.h\"\n");
736 print_mapping_table_prefix(outfp
);
737 print_mapping_table_suffix(outfp
);
741 static int get_maxfds(void)
745 if (getrlimit(RLIMIT_NOFILE
, &rlim
) == 0)
746 return min((int)rlim
.rlim_max
/ 2, 512);
752 * nftw() doesn't let us pass an argument to the processing function,
753 * so use a global variables.
755 static FILE *eventsfp
;
756 static char *mapfile
;
758 static int process_one_file(const char *fpath
, const struct stat
*sb
,
759 int typeflag
, struct FTW
*ftwbuf
)
761 char *tblname
, *bname
= (char *) fpath
+ ftwbuf
->base
;
762 int is_dir
= typeflag
== FTW_D
;
763 int is_file
= typeflag
== FTW_F
;
764 int level
= ftwbuf
->level
;
767 pr_debug("%s %d %7jd %-20s %s\n",
768 is_file
? "f" : is_dir
? "d" : "x",
769 level
, sb
->st_size
, bname
, fpath
);
775 /* model directory, reset topic */
776 if (level
== 1 && is_dir
) {
778 print_events_table_suffix(eventsfp
);
781 * Drop file name suffix. Replace hyphens with underscores.
782 * Fail if file name contains any alphanum characters besides
785 tblname
= file_name_to_table_name(bname
);
787 pr_info("%s: Error determining table name for %s\n", prog
,
792 print_events_table_prefix(eventsfp
, tblname
);
797 * Save the mapfile name for now. We will process mapfile
798 * after processing all JSON files (so we can write out the
799 * mapping table after all PMU events tables).
801 * TODO: Allow for multiple mapfiles? Punt for now.
803 if (level
== 1 && is_file
) {
804 if (!strncmp(bname
, "mapfile.csv", 11)) {
806 pr_info("%s: Many mapfiles? Using %s, ignoring %s\n",
807 prog
, mapfile
, fpath
);
809 mapfile
= strdup(fpath
);
814 pr_info("%s: Ignoring file %s\n", prog
, fpath
);
819 * If the file name does not have a .json extension,
820 * ignore it. It could be a readme.txt for instance.
823 char *suffix
= bname
+ strlen(bname
) - 5;
825 if (strncmp(suffix
, ".json", 5)) {
826 pr_info("%s: Ignoring file without .json suffix %s\n", prog
,
832 if (level
> 1 && add_topic(level
, bname
))
836 * Assume all other files are JSON files.
838 * If mapfile refers to 'power7_core.json', we create a table
839 * named 'power7_core'. Any inconsistencies between the mapfile
840 * and directory tree could result in build failure due to table
841 * names not being found.
843 * Atleast for now, be strict with processing JSON file names.
844 * i.e. if JSON file name cannot be mapped to C-style table name,
848 struct perf_entry_data data
= {
849 .topic
= get_topic(),
853 err
= json_events(fpath
, print_events_table_entry
, &data
);
862 #define PATH_MAX 4096
866 * Starting in directory 'start_dirname', find the "mapfile.csv" and
867 * the set of JSON files for the architecture 'arch'.
869 * From each JSON file, create a C-style "PMU events table" from the
870 * JSON file (see struct pmu_event).
872 * From the mapfile, create a mapping between the CPU revisions and
873 * PMU event tables (see struct pmu_events_map).
875 * Write out the PMU events tables and the mapping table to pmu-event.c.
877 int main(int argc
, char *argv
[])
881 char ldirname
[PATH_MAX
];
884 const char *output_file
;
885 const char *start_dirname
;
888 prog
= basename(argv
[0]);
890 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog
);
895 start_dirname
= argv
[2];
896 output_file
= argv
[3];
899 verbose
= atoi(argv
[4]);
901 eventsfp
= fopen(output_file
, "w");
903 pr_err("%s Unable to create required file %s (%s)\n",
904 prog
, output_file
, strerror(errno
));
908 sprintf(ldirname
, "%s/%s", start_dirname
, arch
);
910 /* If architecture does not have any event lists, bail out */
911 if (stat(ldirname
, &stbuf
) < 0) {
912 pr_info("%s: Arch %s has no PMU event lists\n", prog
, arch
);
916 /* Include pmu-events.h first */
917 fprintf(eventsfp
, "#include \"../../pmu-events/pmu-events.h\"\n");
920 * The mapfile allows multiple CPUids to point to the same JSON file,
921 * so, not sure if there is a need for symlinks within the pmu-events
924 * For now, treat symlinks of JSON files as regular files and create
925 * separate tables for each symlink (presumably, each symlink refers
926 * to specific version of the CPU).
929 maxfds
= get_maxfds();
931 rc
= nftw(ldirname
, process_one_file
, maxfds
, 0);
933 pr_info("%s: Error walking file tree %s\n", prog
, ldirname
);
936 /* Make build fail */
943 print_events_table_suffix(eventsfp
);
946 pr_info("%s: No CPU->JSON mapping?\n", prog
);
950 if (process_mapfile(eventsfp
, mapfile
)) {
951 pr_info("%s: Error processing mapfile %s\n", prog
, mapfile
);
952 /* Make build fail */
960 create_empty_mapping(output_file
);