]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - tools/perf/pmu-events/jevents.c
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 static struct msrmap
{
123 { "0x3F6", "ldlat=" },
124 { "0x1A6", "offcore_rsp=" },
125 { "0x1A7", "offcore_rsp=" },
126 { "0x3F7", "frontend=" },
130 static struct field
{
134 { "UMask", "umask=" },
135 { "CounterMask", "cmask=" },
136 { "Invert", "inv=" },
137 { "AnyThread", "any=" },
138 { "EdgeDetect", "edge=" },
139 { "SampleAfterValue", "period=" },
143 static void cut_comma(char *map
, jsmntok_t
*newval
)
147 /* Cut off everything after comma */
148 for (i
= newval
->start
; i
< newval
->end
; i
++) {
154 static int match_field(char *map
, jsmntok_t
*field
, int nz
,
155 char **event
, jsmntok_t
*val
)
158 jsmntok_t newval
= *val
;
160 for (f
= fields
; f
->field
; f
++)
161 if (json_streq(map
, field
, f
->field
) && nz
) {
162 cut_comma(map
, &newval
);
163 addfield(map
, event
, ",", f
->kernel
, &newval
);
169 static struct msrmap
*lookup_msr(char *map
, jsmntok_t
*val
)
171 jsmntok_t newval
= *val
;
175 cut_comma(map
, &newval
);
176 for (i
= 0; msrmap
[i
].num
; i
++)
177 if (json_streq(map
, &newval
, msrmap
[i
].num
))
181 pr_err("%s: Unknown MSR in event file %.*s\n", prog
,
182 json_len(val
), map
+ val
->start
);
191 { "CBO", "uncore_cbox" },
192 { "QPI LL", "uncore_qpi" },
193 { "SBO", "uncore_sbox" },
194 { "iMPH-U", "uncore_arb" },
198 static const char *field_to_perf(struct map
*table
, char *map
, jsmntok_t
*val
)
202 for (i
= 0; table
[i
].json
; i
++) {
203 if (json_streq(map
, val
, table
[i
].json
))
204 return table
[i
].perf
;
209 #define EXPECT(e, t, m) do { if (!(e)) { \
210 jsmntok_t *loc = (t); \
211 if (!(t)->start && (t) > tokens) \
213 pr_err("%s:%d: " m ", got %s\n", fn, \
214 json_line(map, loc), \
219 #define TOPIC_DEPTH 256
220 static char *topic_array
[TOPIC_DEPTH
];
221 static int topic_level
;
223 static char *get_topic(void)
225 char *tp_old
, *tp
= NULL
;
228 for (i
= 0; i
< topic_level
+ 1; i
++) {
232 n
= asprintf(&tp
, "%s%s", tp
?: "", topic_array
[i
]);
234 pr_info("%s: asprintf() error %s\n", prog
);
240 for (i
= 0; i
< (int) strlen(tp
); i
++) {
254 static int add_topic(int level
, char *bname
)
260 if (level
>= TOPIC_DEPTH
)
263 topic
= strdup(bname
);
265 pr_info("%s: strdup() error %s for file %s\n", prog
,
266 strerror(errno
), bname
);
270 free(topic_array
[topic_level
]);
271 topic_array
[topic_level
] = topic
;
276 struct perf_entry_data
{
281 static int close_table
;
283 static void print_events_table_prefix(FILE *fp
, const char *tblname
)
285 fprintf(fp
, "struct pmu_event %s[] = {\n", tblname
);
289 static int print_events_table_entry(void *data
, char *name
, char *event
,
290 char *desc
, char *long_desc
,
291 char *pmu
, char *unit
, char *perpkg
,
295 struct perf_entry_data
*pd
= data
;
296 FILE *outfp
= pd
->outfp
;
297 char *topic
= pd
->topic
;
300 * TODO: Remove formatting chars after debugging to reduce
303 fprintf(outfp
, "{\n");
305 fprintf(outfp
, "\t.name = \"%s\",\n", name
);
306 fprintf(outfp
, "\t.event = \"%s\",\n", event
);
307 fprintf(outfp
, "\t.desc = \"%s\",\n", desc
);
308 fprintf(outfp
, "\t.topic = \"%s\",\n", topic
);
309 if (long_desc
&& long_desc
[0])
310 fprintf(outfp
, "\t.long_desc = \"%s\",\n", long_desc
);
312 fprintf(outfp
, "\t.pmu = \"%s\",\n", pmu
);
314 fprintf(outfp
, "\t.unit = \"%s\",\n", unit
);
316 fprintf(outfp
, "\t.perpkg = \"%s\",\n", perpkg
);
318 fprintf(outfp
, "\t.metric_expr = \"%s\",\n", metric_expr
);
320 fprintf(outfp
, "\t.metric_name = \"%s\",\n", metric_name
);
321 fprintf(outfp
, "},\n");
326 static void print_events_table_suffix(FILE *outfp
)
328 fprintf(outfp
, "{\n");
330 fprintf(outfp
, "\t.name = 0,\n");
331 fprintf(outfp
, "\t.event = 0,\n");
332 fprintf(outfp
, "\t.desc = 0,\n");
334 fprintf(outfp
, "},\n");
335 fprintf(outfp
, "};\n");
339 static struct fixed
{
343 { "inst_retired.any", "event=0xc0" },
344 { "inst_retired.any_p", "event=0xc0" },
345 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03" },
346 { "cpu_clk_unhalted.thread", "event=0x3c" },
347 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1" },
352 * Handle different fixed counter encodings between JSON and perf.
354 static char *real_event(const char *name
, char *event
)
358 for (i
= 0; fixed
[i
].name
; i
++)
359 if (!strcasecmp(name
, fixed
[i
].name
))
360 return (char *)fixed
[i
].event
;
364 /* Call func with each event in the json file */
365 int json_events(const char *fn
,
366 int (*func
)(void *data
, char *name
, char *event
, char *desc
,
368 char *pmu
, char *unit
, char *perpkg
,
375 jsmntok_t
*tokens
, *tok
;
383 tokens
= parse_json(fn
, &map
, &size
, &len
);
386 EXPECT(tokens
->type
== JSMN_ARRAY
, tokens
, "expected top level array");
388 for (i
= 0; i
< tokens
->size
; i
++) {
389 char *event
= NULL
, *desc
= NULL
, *name
= NULL
;
390 char *long_desc
= NULL
;
391 char *extra_desc
= NULL
;
396 char *metric_expr
= NULL
;
397 char *metric_name
= NULL
;
398 unsigned long long eventcode
= 0;
399 struct msrmap
*msr
= NULL
;
400 jsmntok_t
*msrval
= NULL
;
401 jsmntok_t
*precise
= NULL
;
402 jsmntok_t
*obj
= tok
++;
404 EXPECT(obj
->type
== JSMN_OBJECT
, obj
, "expected object");
405 for (j
= 0; j
< obj
->size
; j
+= 2) {
406 jsmntok_t
*field
, *val
;
411 EXPECT(field
->type
== JSMN_STRING
, tok
+ j
,
412 "Expected field name");
414 EXPECT(val
->type
== JSMN_STRING
, tok
+ j
+ 1,
415 "Expected string value");
417 nz
= !json_streq(map
, val
, "0");
418 if (match_field(map
, field
, nz
, &event
, val
)) {
420 } else if (json_streq(map
, field
, "EventCode")) {
422 addfield(map
, &code
, "", "", val
);
423 eventcode
|= strtoul(code
, NULL
, 0);
425 } else if (json_streq(map
, field
, "ExtSel")) {
427 addfield(map
, &code
, "", "", val
);
428 eventcode
|= strtoul(code
, NULL
, 0) << 21;
430 } else if (json_streq(map
, field
, "EventName")) {
431 addfield(map
, &name
, "", "", val
);
432 } else if (json_streq(map
, field
, "BriefDescription")) {
433 addfield(map
, &desc
, "", "", val
);
435 } else if (json_streq(map
, field
,
436 "PublicDescription")) {
437 addfield(map
, &long_desc
, "", "", val
);
439 } else if (json_streq(map
, field
, "PEBS") && nz
) {
441 } else if (json_streq(map
, field
, "MSRIndex") && nz
) {
442 msr
= lookup_msr(map
, val
);
443 } else if (json_streq(map
, field
, "MSRValue")) {
445 } else if (json_streq(map
, field
, "Errata") &&
446 !json_streq(map
, val
, "null")) {
447 addfield(map
, &extra_desc
, ". ",
448 " Spec update: ", val
);
449 } else if (json_streq(map
, field
, "Data_LA") && nz
) {
450 addfield(map
, &extra_desc
, ". ",
451 " Supports address when precise",
453 } else if (json_streq(map
, field
, "Unit")) {
456 ppmu
= field_to_perf(unit_to_pmu
, map
, val
);
461 pmu
= strdup("uncore_");
462 addfield(map
, &pmu
, "", "", val
);
463 for (s
= pmu
; *s
; s
++)
466 addfield(map
, &desc
, ". ", "Unit: ", NULL
);
467 addfield(map
, &desc
, "", pmu
, NULL
);
468 addfield(map
, &desc
, "", " ", NULL
);
469 } else if (json_streq(map
, field
, "Filter")) {
470 addfield(map
, &filter
, "", "", val
);
471 } else if (json_streq(map
, field
, "ScaleUnit")) {
472 addfield(map
, &unit
, "", "", val
);
473 } else if (json_streq(map
, field
, "PerPkg")) {
474 addfield(map
, &perpkg
, "", "", val
);
475 } else if (json_streq(map
, field
, "MetricName")) {
476 addfield(map
, &metric_name
, "", "", val
);
477 } else if (json_streq(map
, field
, "MetricExpr")) {
478 addfield(map
, &metric_expr
, "", "", val
);
479 for (s
= metric_expr
; *s
; s
++)
482 /* ignore unknown fields */
484 if (precise
&& desc
&& !strstr(desc
, "(Precise Event)")) {
485 if (json_streq(map
, precise
, "2"))
486 addfield(map
, &extra_desc
, " ",
487 "(Must be precise)", NULL
);
489 addfield(map
, &extra_desc
, " ",
490 "(Precise event)", NULL
);
492 snprintf(buf
, sizeof buf
, "event=%#llx", eventcode
);
493 addfield(map
, &event
, ",", buf
, NULL
);
494 if (desc
&& extra_desc
)
495 addfield(map
, &desc
, " ", extra_desc
, NULL
);
496 if (long_desc
&& extra_desc
)
497 addfield(map
, &long_desc
, " ", extra_desc
, NULL
);
499 addfield(map
, &event
, ",", filter
, NULL
);
501 addfield(map
, &event
, ",", msr
->pname
, msrval
);
504 err
= func(data
, name
, real_event(name
, event
), desc
, long_desc
,
505 pmu
, unit
, perpkg
, metric_expr
, metric_name
);
521 EXPECT(tok
- tokens
== len
, tok
, "unexpected objects at end");
524 free_json(map
, size
, tokens
);
528 static char *file_name_to_table_name(char *fname
)
536 * Ensure tablename starts with alphabetic character.
537 * Derive rest of table name from basename of the JSON file,
538 * replacing hyphens and stripping out .json suffix.
540 n
= asprintf(&tblname
, "pme_%s", basename(fname
));
542 pr_info("%s: asprintf() error %s for file %s\n", prog
,
543 strerror(errno
), fname
);
547 for (i
= 0; i
< strlen(tblname
); i
++) {
555 } else if (!isalnum(c
) && c
!= '_') {
556 pr_err("%s: Invalid character '%c' in file name %s\n",
557 prog
, c
, basename(fname
));
567 static void print_mapping_table_prefix(FILE *outfp
)
569 fprintf(outfp
, "struct pmu_events_map pmu_events_map[] = {\n");
572 static void print_mapping_table_suffix(FILE *outfp
)
575 * Print the terminating, NULL entry.
577 fprintf(outfp
, "{\n");
578 fprintf(outfp
, "\t.cpuid = 0,\n");
579 fprintf(outfp
, "\t.version = 0,\n");
580 fprintf(outfp
, "\t.type = 0,\n");
581 fprintf(outfp
, "\t.table = 0,\n");
582 fprintf(outfp
, "},\n");
584 /* and finally, the closing curly bracket for the struct */
585 fprintf(outfp
, "};\n");
588 static int process_mapfile(FILE *outfp
, char *fpath
)
597 pr_info("%s: Processing mapfile %s\n", prog
, fpath
);
603 mapfp
= fopen(fpath
, "r");
605 pr_info("%s: Error %s opening %s\n", prog
, strerror(errno
),
610 print_mapping_table_prefix(outfp
);
612 /* Skip first line (header) */
613 p
= fgets(line
, n
, mapfp
);
619 char *cpuid
, *version
, *type
, *fname
;
622 p
= fgets(line
, n
, mapfp
);
626 if (line
[0] == '#' || line
[0] == '\n')
629 if (line
[strlen(line
)-1] != '\n') {
630 /* TODO Deal with lines longer than 16K */
631 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
632 prog
, fpath
, line_num
);
635 line
[strlen(line
)-1] = '\0';
637 cpuid
= strtok_r(p
, ",", &save
);
638 version
= strtok_r(NULL
, ",", &save
);
639 fname
= strtok_r(NULL
, ",", &save
);
640 type
= strtok_r(NULL
, ",", &save
);
642 tblname
= file_name_to_table_name(fname
);
643 fprintf(outfp
, "{\n");
644 fprintf(outfp
, "\t.cpuid = \"%s\",\n", cpuid
);
645 fprintf(outfp
, "\t.version = \"%s\",\n", version
);
646 fprintf(outfp
, "\t.type = \"%s\",\n", type
);
649 * CHECK: We can't use the type (eg "core") field in the
650 * table name. For us to do that, we need to somehow tweak
651 * the other caller of file_name_to_table(), process_json()
652 * to determine the type. process_json() file has no way
653 * of knowing these are "core" events unless file name has
654 * core in it. If filename has core in it, we can safely
655 * ignore the type field here also.
657 fprintf(outfp
, "\t.table = %s\n", tblname
);
658 fprintf(outfp
, "},\n");
662 print_mapping_table_suffix(outfp
);
667 * If we fail to locate/process JSON and map files, create a NULL mapping
668 * table. This would at least allow perf to build even if we can't find/use
671 static void create_empty_mapping(const char *output_file
)
675 pr_info("%s: Creating empty pmu_events_map[] table\n", prog
);
677 /* Truncate file to clear any partial writes to it */
678 outfp
= fopen(output_file
, "w");
684 fprintf(outfp
, "#include \"../../pmu-events/pmu-events.h\"\n");
685 print_mapping_table_prefix(outfp
);
686 print_mapping_table_suffix(outfp
);
690 static int get_maxfds(void)
694 if (getrlimit(RLIMIT_NOFILE
, &rlim
) == 0)
695 return min((int)rlim
.rlim_max
/ 2, 512);
701 * nftw() doesn't let us pass an argument to the processing function,
702 * so use a global variables.
704 static FILE *eventsfp
;
705 static char *mapfile
;
707 static int process_one_file(const char *fpath
, const struct stat
*sb
,
708 int typeflag
, struct FTW
*ftwbuf
)
710 char *tblname
, *bname
= (char *) fpath
+ ftwbuf
->base
;
711 int is_dir
= typeflag
== FTW_D
;
712 int is_file
= typeflag
== FTW_F
;
713 int level
= ftwbuf
->level
;
716 pr_debug("%s %d %7jd %-20s %s\n",
717 is_file
? "f" : is_dir
? "d" : "x",
718 level
, sb
->st_size
, bname
, fpath
);
724 /* model directory, reset topic */
725 if (level
== 1 && is_dir
) {
727 print_events_table_suffix(eventsfp
);
730 * Drop file name suffix. Replace hyphens with underscores.
731 * Fail if file name contains any alphanum characters besides
734 tblname
= file_name_to_table_name(bname
);
736 pr_info("%s: Error determining table name for %s\n", prog
,
741 print_events_table_prefix(eventsfp
, tblname
);
746 * Save the mapfile name for now. We will process mapfile
747 * after processing all JSON files (so we can write out the
748 * mapping table after all PMU events tables).
750 * TODO: Allow for multiple mapfiles? Punt for now.
752 if (level
== 1 && is_file
) {
753 if (!strncmp(bname
, "mapfile.csv", 11)) {
755 pr_info("%s: Many mapfiles? Using %s, ignoring %s\n",
756 prog
, mapfile
, fpath
);
758 mapfile
= strdup(fpath
);
763 pr_info("%s: Ignoring file %s\n", prog
, fpath
);
768 * If the file name does not have a .json extension,
769 * ignore it. It could be a readme.txt for instance.
772 char *suffix
= bname
+ strlen(bname
) - 5;
774 if (strncmp(suffix
, ".json", 5)) {
775 pr_info("%s: Ignoring file without .json suffix %s\n", prog
,
781 if (level
> 1 && add_topic(level
, bname
))
785 * Assume all other files are JSON files.
787 * If mapfile refers to 'power7_core.json', we create a table
788 * named 'power7_core'. Any inconsistencies between the mapfile
789 * and directory tree could result in build failure due to table
790 * names not being found.
792 * Atleast for now, be strict with processing JSON file names.
793 * i.e. if JSON file name cannot be mapped to C-style table name,
797 struct perf_entry_data data
= {
798 .topic
= get_topic(),
802 err
= json_events(fpath
, print_events_table_entry
, &data
);
811 #define PATH_MAX 4096
815 * Starting in directory 'start_dirname', find the "mapfile.csv" and
816 * the set of JSON files for the architecture 'arch'.
818 * From each JSON file, create a C-style "PMU events table" from the
819 * JSON file (see struct pmu_event).
821 * From the mapfile, create a mapping between the CPU revisions and
822 * PMU event tables (see struct pmu_events_map).
824 * Write out the PMU events tables and the mapping table to pmu-event.c.
826 * If unable to process the JSON or arch files, create an empty mapping
827 * table so we can continue to build/use perf even if we cannot use the
830 int main(int argc
, char *argv
[])
834 char ldirname
[PATH_MAX
];
837 const char *output_file
;
838 const char *start_dirname
;
840 prog
= basename(argv
[0]);
842 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog
);
847 start_dirname
= argv
[2];
848 output_file
= argv
[3];
851 verbose
= atoi(argv
[4]);
853 eventsfp
= fopen(output_file
, "w");
855 pr_err("%s Unable to create required file %s (%s)\n",
856 prog
, output_file
, strerror(errno
));
860 /* Include pmu-events.h first */
861 fprintf(eventsfp
, "#include \"../../pmu-events/pmu-events.h\"\n");
863 sprintf(ldirname
, "%s/%s", start_dirname
, arch
);
866 * The mapfile allows multiple CPUids to point to the same JSON file,
867 * so, not sure if there is a need for symlinks within the pmu-events
870 * For now, treat symlinks of JSON files as regular files and create
871 * separate tables for each symlink (presumably, each symlink refers
872 * to specific version of the CPU).
875 maxfds
= get_maxfds();
877 rc
= nftw(ldirname
, process_one_file
, maxfds
, 0);
879 pr_info("%s: Error walking file tree %s\n", prog
, ldirname
);
886 print_events_table_suffix(eventsfp
);
889 pr_info("%s: No CPU->JSON mapping?\n", prog
);
893 if (process_mapfile(eventsfp
, mapfile
)) {
894 pr_info("%s: Error processing mapfile %s\n", prog
, mapfile
);
902 create_empty_mapping(output_file
);