Loading...
1#define _XOPEN_SOURCE 500 /* needed for nftw() */
2#define _GNU_SOURCE /* needed for asprintf() */
3
4/* Parse event JSON files */
5
6/*
7 * Copyright (c) 2014, Intel Corporation
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 *
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.
19 *
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.
32*/
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <errno.h>
37#include <string.h>
38#include <ctype.h>
39#include <unistd.h>
40#include <stdarg.h>
41#include <libgen.h>
42#include <limits.h>
43#include <dirent.h>
44#include <sys/time.h> /* getrlimit */
45#include <sys/resource.h> /* getrlimit */
46#include <ftw.h>
47#include <sys/stat.h>
48#include <linux/list.h>
49#include "jsmn.h"
50#include "json.h"
51#include "jevents.h"
52
53int verbose;
54char *prog;
55
56int eprintf(int level, int var, const char *fmt, ...)
57{
58
59 int ret;
60 va_list args;
61
62 if (var < level)
63 return 0;
64
65 va_start(args, fmt);
66
67 ret = vfprintf(stderr, fmt, args);
68
69 va_end(args);
70
71 return ret;
72}
73
74__attribute__((weak)) char *get_cpu_str(void)
75{
76 return NULL;
77}
78
79static void addfield(char *map, char **dst, const char *sep,
80 const char *a, jsmntok_t *bt)
81{
82 unsigned int len = strlen(a) + 1 + strlen(sep);
83 int olen = *dst ? strlen(*dst) : 0;
84 int blen = bt ? json_len(bt) : 0;
85 char *out;
86
87 out = realloc(*dst, len + olen + blen);
88 if (!out) {
89 /* Don't add field in this case */
90 return;
91 }
92 *dst = out;
93
94 if (!olen)
95 *(*dst) = 0;
96 else
97 strcat(*dst, sep);
98 strcat(*dst, a);
99 if (bt)
100 strncat(*dst, map + bt->start, blen);
101}
102
103static void fixname(char *s)
104{
105 for (; *s; s++)
106 *s = tolower(*s);
107}
108
109static void fixdesc(char *s)
110{
111 char *e = s + strlen(s);
112
113 /* Remove trailing dots that look ugly in perf list */
114 --e;
115 while (e >= s && isspace(*e))
116 --e;
117 if (*e == '.')
118 *e = 0;
119}
120
121/* Add escapes for '\' so they are proper C strings. */
122static char *fixregex(char *s)
123{
124 int len = 0;
125 int esc_count = 0;
126 char *fixed = NULL;
127 char *p, *q;
128
129 /* Count the number of '\' in string */
130 for (p = s; *p; p++) {
131 ++len;
132 if (*p == '\\')
133 ++esc_count;
134 }
135
136 if (esc_count == 0)
137 return s;
138
139 /* allocate space for a new string */
140 fixed = (char *) malloc(len + 1);
141 if (!fixed)
142 return NULL;
143
144 /* copy over the characters */
145 q = fixed;
146 for (p = s; *p; p++) {
147 if (*p == '\\') {
148 *q = '\\';
149 ++q;
150 }
151 *q = *p;
152 ++q;
153 }
154 *q = '\0';
155 return fixed;
156}
157
158static struct msrmap {
159 const char *num;
160 const char *pname;
161} msrmap[] = {
162 { "0x3F6", "ldlat=" },
163 { "0x1A6", "offcore_rsp=" },
164 { "0x1A7", "offcore_rsp=" },
165 { "0x3F7", "frontend=" },
166 { NULL, NULL }
167};
168
169static struct field {
170 const char *field;
171 const char *kernel;
172} fields[] = {
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=" },
181 { NULL, NULL }
182};
183
184static void cut_comma(char *map, jsmntok_t *newval)
185{
186 int i;
187
188 /* Cut off everything after comma */
189 for (i = newval->start; i < newval->end; i++) {
190 if (map[i] == ',')
191 newval->end = i;
192 }
193}
194
195static int match_field(char *map, jsmntok_t *field, int nz,
196 char **event, jsmntok_t *val)
197{
198 struct field *f;
199 jsmntok_t newval = *val;
200
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);
205 return 1;
206 }
207 return 0;
208}
209
210static struct msrmap *lookup_msr(char *map, jsmntok_t *val)
211{
212 jsmntok_t newval = *val;
213 static bool warned;
214 int i;
215
216 cut_comma(map, &newval);
217 for (i = 0; msrmap[i].num; i++)
218 if (json_streq(map, &newval, msrmap[i].num))
219 return &msrmap[i];
220 if (!warned) {
221 warned = true;
222 pr_err("%s: Unknown MSR in event file %.*s\n", prog,
223 json_len(val), map + val->start);
224 }
225 return NULL;
226}
227
228static struct map {
229 const char *json;
230 const char *perf;
231} unit_to_pmu[] = {
232 { "CBO", "uncore_cbox" },
233 { "QPI LL", "uncore_qpi" },
234 { "SBO", "uncore_sbox" },
235 { "iMPH-U", "uncore_arb" },
236 {}
237};
238
239static const char *field_to_perf(struct map *table, char *map, jsmntok_t *val)
240{
241 int i;
242
243 for (i = 0; table[i].json; i++) {
244 if (json_streq(map, val, table[i].json))
245 return table[i].perf;
246 }
247 return NULL;
248}
249
250#define EXPECT(e, t, m) do { if (!(e)) { \
251 jsmntok_t *loc = (t); \
252 if (!(t)->start && (t) > tokens) \
253 loc = (t) - 1; \
254 pr_err("%s:%d: " m ", got %s\n", fn, \
255 json_line(map, loc), \
256 json_name(t)); \
257 err = -EIO; \
258 goto out_free; \
259} } while (0)
260
261static char *topic;
262
263static char *get_topic(void)
264{
265 char *tp;
266 int i;
267
268 /* tp is free'd in process_one_file() */
269 i = asprintf(&tp, "%s", topic);
270 if (i < 0) {
271 pr_info("%s: asprintf() error %s\n", prog);
272 return NULL;
273 }
274
275 for (i = 0; i < (int) strlen(tp); i++) {
276 char c = tp[i];
277
278 if (c == '-')
279 tp[i] = ' ';
280 else if (c == '.') {
281 tp[i] = '\0';
282 break;
283 }
284 }
285
286 return tp;
287}
288
289static int add_topic(char *bname)
290{
291 free(topic);
292 topic = strdup(bname);
293 if (!topic) {
294 pr_info("%s: strdup() error %s for file %s\n", prog,
295 strerror(errno), bname);
296 return -ENOMEM;
297 }
298 return 0;
299}
300
301struct perf_entry_data {
302 FILE *outfp;
303 char *topic;
304};
305
306static int close_table;
307
308static void print_events_table_prefix(FILE *fp, const char *tblname)
309{
310 fprintf(fp, "struct pmu_event %s[] = {\n", tblname);
311 close_table = 1;
312}
313
314static int print_events_table_entry(void *data, char *name, char *event,
315 char *desc, char *long_desc,
316 char *pmu, char *unit, char *perpkg,
317 char *metric_expr,
318 char *metric_name, char *metric_group)
319{
320 struct perf_entry_data *pd = data;
321 FILE *outfp = pd->outfp;
322 char *topic = pd->topic;
323
324 /*
325 * TODO: Remove formatting chars after debugging to reduce
326 * string lengths.
327 */
328 fprintf(outfp, "{\n");
329
330 if (name)
331 fprintf(outfp, "\t.name = \"%s\",\n", name);
332 if (event)
333 fprintf(outfp, "\t.event = \"%s\",\n", event);
334 fprintf(outfp, "\t.desc = \"%s\",\n", desc);
335 fprintf(outfp, "\t.topic = \"%s\",\n", topic);
336 if (long_desc && long_desc[0])
337 fprintf(outfp, "\t.long_desc = \"%s\",\n", long_desc);
338 if (pmu)
339 fprintf(outfp, "\t.pmu = \"%s\",\n", pmu);
340 if (unit)
341 fprintf(outfp, "\t.unit = \"%s\",\n", unit);
342 if (perpkg)
343 fprintf(outfp, "\t.perpkg = \"%s\",\n", perpkg);
344 if (metric_expr)
345 fprintf(outfp, "\t.metric_expr = \"%s\",\n", metric_expr);
346 if (metric_name)
347 fprintf(outfp, "\t.metric_name = \"%s\",\n", metric_name);
348 if (metric_group)
349 fprintf(outfp, "\t.metric_group = \"%s\",\n", metric_group);
350 fprintf(outfp, "},\n");
351
352 return 0;
353}
354
355struct event_struct {
356 struct list_head list;
357 char *name;
358 char *event;
359 char *desc;
360 char *long_desc;
361 char *pmu;
362 char *unit;
363 char *perpkg;
364 char *metric_expr;
365 char *metric_name;
366 char *metric_group;
367};
368
369#define ADD_EVENT_FIELD(field) do { if (field) { \
370 es->field = strdup(field); \
371 if (!es->field) \
372 goto out_free; \
373} } while (0)
374
375#define FREE_EVENT_FIELD(field) free(es->field)
376
377#define TRY_FIXUP_FIELD(field) do { if (es->field && !*field) {\
378 *field = strdup(es->field); \
379 if (!*field) \
380 return -ENOMEM; \
381} } while (0)
382
383#define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
384 op(name); \
385 op(event); \
386 op(desc); \
387 op(long_desc); \
388 op(pmu); \
389 op(unit); \
390 op(perpkg); \
391 op(metric_expr); \
392 op(metric_name); \
393 op(metric_group); \
394} while (0)
395
396static LIST_HEAD(arch_std_events);
397
398static void free_arch_std_events(void)
399{
400 struct event_struct *es, *next;
401
402 list_for_each_entry_safe(es, next, &arch_std_events, list) {
403 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
404 list_del(&es->list);
405 free(es);
406 }
407}
408
409static int save_arch_std_events(void *data, char *name, char *event,
410 char *desc, char *long_desc, char *pmu,
411 char *unit, char *perpkg, char *metric_expr,
412 char *metric_name, char *metric_group)
413{
414 struct event_struct *es;
415 struct stat *sb = data;
416
417 es = malloc(sizeof(*es));
418 if (!es)
419 return -ENOMEM;
420 memset(es, 0, sizeof(*es));
421 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD);
422 list_add_tail(&es->list, &arch_std_events);
423 return 0;
424out_free:
425 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
426 free(es);
427 return -ENOMEM;
428}
429
430static void print_events_table_suffix(FILE *outfp)
431{
432 fprintf(outfp, "{\n");
433
434 fprintf(outfp, "\t.name = 0,\n");
435 fprintf(outfp, "\t.event = 0,\n");
436 fprintf(outfp, "\t.desc = 0,\n");
437
438 fprintf(outfp, "},\n");
439 fprintf(outfp, "};\n");
440 close_table = 0;
441}
442
443static struct fixed {
444 const char *name;
445 const char *event;
446} fixed[] = {
447 { "inst_retired.any", "event=0xc0" },
448 { "inst_retired.any_p", "event=0xc0" },
449 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03" },
450 { "cpu_clk_unhalted.thread", "event=0x3c" },
451 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1" },
452 { NULL, NULL},
453};
454
455/*
456 * Handle different fixed counter encodings between JSON and perf.
457 */
458static char *real_event(const char *name, char *event)
459{
460 int i;
461
462 if (!name)
463 return NULL;
464
465 for (i = 0; fixed[i].name; i++)
466 if (!strcasecmp(name, fixed[i].name))
467 return (char *)fixed[i].event;
468 return event;
469}
470
471static int
472try_fixup(const char *fn, char *arch_std, char **event, char **desc,
473 char **name, char **long_desc, char **pmu, char **filter,
474 char **perpkg, char **unit, char **metric_expr, char **metric_name,
475 char **metric_group, unsigned long long eventcode)
476{
477 /* try to find matching event from arch standard values */
478 struct event_struct *es;
479
480 list_for_each_entry(es, &arch_std_events, list) {
481 if (!strcmp(arch_std, es->name)) {
482 if (!eventcode && es->event) {
483 /* allow EventCode to be overridden */
484 free(*event);
485 *event = NULL;
486 }
487 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
488 return 0;
489 }
490 }
491
492 pr_err("%s: could not find matching %s for %s\n",
493 prog, arch_std, fn);
494 return -1;
495}
496
497/* Call func with each event in the json file */
498int json_events(const char *fn,
499 int (*func)(void *data, char *name, char *event, char *desc,
500 char *long_desc,
501 char *pmu, char *unit, char *perpkg,
502 char *metric_expr,
503 char *metric_name, char *metric_group),
504 void *data)
505{
506 int err;
507 size_t size;
508 jsmntok_t *tokens, *tok;
509 int i, j, len;
510 char *map;
511 char buf[128];
512
513 if (!fn)
514 return -ENOENT;
515
516 tokens = parse_json(fn, &map, &size, &len);
517 if (!tokens)
518 return -EIO;
519 EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
520 tok = tokens + 1;
521 for (i = 0; i < tokens->size; i++) {
522 char *event = NULL, *desc = NULL, *name = NULL;
523 char *long_desc = NULL;
524 char *extra_desc = NULL;
525 char *pmu = NULL;
526 char *filter = NULL;
527 char *perpkg = NULL;
528 char *unit = NULL;
529 char *metric_expr = NULL;
530 char *metric_name = NULL;
531 char *metric_group = NULL;
532 char *arch_std = NULL;
533 unsigned long long eventcode = 0;
534 struct msrmap *msr = NULL;
535 jsmntok_t *msrval = NULL;
536 jsmntok_t *precise = NULL;
537 jsmntok_t *obj = tok++;
538
539 EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
540 for (j = 0; j < obj->size; j += 2) {
541 jsmntok_t *field, *val;
542 int nz;
543 char *s;
544
545 field = tok + j;
546 EXPECT(field->type == JSMN_STRING, tok + j,
547 "Expected field name");
548 val = tok + j + 1;
549 EXPECT(val->type == JSMN_STRING, tok + j + 1,
550 "Expected string value");
551
552 nz = !json_streq(map, val, "0");
553 if (match_field(map, field, nz, &event, val)) {
554 /* ok */
555 } else if (json_streq(map, field, "EventCode")) {
556 char *code = NULL;
557 addfield(map, &code, "", "", val);
558 eventcode |= strtoul(code, NULL, 0);
559 free(code);
560 } else if (json_streq(map, field, "ExtSel")) {
561 char *code = NULL;
562 addfield(map, &code, "", "", val);
563 eventcode |= strtoul(code, NULL, 0) << 21;
564 free(code);
565 } else if (json_streq(map, field, "EventName")) {
566 addfield(map, &name, "", "", val);
567 } else if (json_streq(map, field, "BriefDescription")) {
568 addfield(map, &desc, "", "", val);
569 fixdesc(desc);
570 } else if (json_streq(map, field,
571 "PublicDescription")) {
572 addfield(map, &long_desc, "", "", val);
573 fixdesc(long_desc);
574 } else if (json_streq(map, field, "PEBS") && nz) {
575 precise = val;
576 } else if (json_streq(map, field, "MSRIndex") && nz) {
577 msr = lookup_msr(map, val);
578 } else if (json_streq(map, field, "MSRValue")) {
579 msrval = val;
580 } else if (json_streq(map, field, "Errata") &&
581 !json_streq(map, val, "null")) {
582 addfield(map, &extra_desc, ". ",
583 " Spec update: ", val);
584 } else if (json_streq(map, field, "Data_LA") && nz) {
585 addfield(map, &extra_desc, ". ",
586 " Supports address when precise",
587 NULL);
588 } else if (json_streq(map, field, "Unit")) {
589 const char *ppmu;
590
591 ppmu = field_to_perf(unit_to_pmu, map, val);
592 if (ppmu) {
593 pmu = strdup(ppmu);
594 } else {
595 if (!pmu)
596 pmu = strdup("uncore_");
597 addfield(map, &pmu, "", "", val);
598 for (s = pmu; *s; s++)
599 *s = tolower(*s);
600 }
601 addfield(map, &desc, ". ", "Unit: ", NULL);
602 addfield(map, &desc, "", pmu, NULL);
603 addfield(map, &desc, "", " ", NULL);
604 } else if (json_streq(map, field, "Filter")) {
605 addfield(map, &filter, "", "", val);
606 } else if (json_streq(map, field, "ScaleUnit")) {
607 addfield(map, &unit, "", "", val);
608 } else if (json_streq(map, field, "PerPkg")) {
609 addfield(map, &perpkg, "", "", val);
610 } else if (json_streq(map, field, "MetricName")) {
611 addfield(map, &metric_name, "", "", val);
612 } else if (json_streq(map, field, "MetricGroup")) {
613 addfield(map, &metric_group, "", "", val);
614 } else if (json_streq(map, field, "MetricExpr")) {
615 addfield(map, &metric_expr, "", "", val);
616 for (s = metric_expr; *s; s++)
617 *s = tolower(*s);
618 } else if (json_streq(map, field, "ArchStdEvent")) {
619 addfield(map, &arch_std, "", "", val);
620 for (s = arch_std; *s; s++)
621 *s = tolower(*s);
622 }
623 /* ignore unknown fields */
624 }
625 if (precise && desc && !strstr(desc, "(Precise Event)")) {
626 if (json_streq(map, precise, "2"))
627 addfield(map, &extra_desc, " ",
628 "(Must be precise)", NULL);
629 else
630 addfield(map, &extra_desc, " ",
631 "(Precise event)", NULL);
632 }
633 snprintf(buf, sizeof buf, "event=%#llx", eventcode);
634 addfield(map, &event, ",", buf, NULL);
635 if (desc && extra_desc)
636 addfield(map, &desc, " ", extra_desc, NULL);
637 if (long_desc && extra_desc)
638 addfield(map, &long_desc, " ", extra_desc, NULL);
639 if (filter)
640 addfield(map, &event, ",", filter, NULL);
641 if (msr != NULL)
642 addfield(map, &event, ",", msr->pname, msrval);
643 if (name)
644 fixname(name);
645
646 if (arch_std) {
647 /*
648 * An arch standard event is referenced, so try to
649 * fixup any unassigned values.
650 */
651 err = try_fixup(fn, arch_std, &event, &desc, &name,
652 &long_desc, &pmu, &filter, &perpkg,
653 &unit, &metric_expr, &metric_name,
654 &metric_group, eventcode);
655 if (err)
656 goto free_strings;
657 }
658 err = func(data, name, real_event(name, event), desc, long_desc,
659 pmu, unit, perpkg, metric_expr, metric_name, metric_group);
660free_strings:
661 free(event);
662 free(desc);
663 free(name);
664 free(long_desc);
665 free(extra_desc);
666 free(pmu);
667 free(filter);
668 free(perpkg);
669 free(unit);
670 free(metric_expr);
671 free(metric_name);
672 free(metric_group);
673 free(arch_std);
674
675 if (err)
676 break;
677 tok += j;
678 }
679 EXPECT(tok - tokens == len, tok, "unexpected objects at end");
680 err = 0;
681out_free:
682 free_json(map, size, tokens);
683 return err;
684}
685
686static char *file_name_to_table_name(char *fname)
687{
688 unsigned int i;
689 int n;
690 int c;
691 char *tblname;
692
693 /*
694 * Ensure tablename starts with alphabetic character.
695 * Derive rest of table name from basename of the JSON file,
696 * replacing hyphens and stripping out .json suffix.
697 */
698 n = asprintf(&tblname, "pme_%s", fname);
699 if (n < 0) {
700 pr_info("%s: asprintf() error %s for file %s\n", prog,
701 strerror(errno), fname);
702 return NULL;
703 }
704
705 for (i = 0; i < strlen(tblname); i++) {
706 c = tblname[i];
707
708 if (c == '-' || c == '/')
709 tblname[i] = '_';
710 else if (c == '.') {
711 tblname[i] = '\0';
712 break;
713 } else if (!isalnum(c) && c != '_') {
714 pr_err("%s: Invalid character '%c' in file name %s\n",
715 prog, c, basename(fname));
716 free(tblname);
717 tblname = NULL;
718 break;
719 }
720 }
721
722 return tblname;
723}
724
725static void print_mapping_table_prefix(FILE *outfp)
726{
727 fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n");
728}
729
730static void print_mapping_table_suffix(FILE *outfp)
731{
732 /*
733 * Print the terminating, NULL entry.
734 */
735 fprintf(outfp, "{\n");
736 fprintf(outfp, "\t.cpuid = 0,\n");
737 fprintf(outfp, "\t.version = 0,\n");
738 fprintf(outfp, "\t.type = 0,\n");
739 fprintf(outfp, "\t.table = 0,\n");
740 fprintf(outfp, "},\n");
741
742 /* and finally, the closing curly bracket for the struct */
743 fprintf(outfp, "};\n");
744}
745
746static int process_mapfile(FILE *outfp, char *fpath)
747{
748 int n = 16384;
749 FILE *mapfp;
750 char *save = NULL;
751 char *line, *p;
752 int line_num;
753 char *tblname;
754
755 pr_info("%s: Processing mapfile %s\n", prog, fpath);
756
757 line = malloc(n);
758 if (!line)
759 return -1;
760
761 mapfp = fopen(fpath, "r");
762 if (!mapfp) {
763 pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
764 fpath);
765 return -1;
766 }
767
768 print_mapping_table_prefix(outfp);
769
770 /* Skip first line (header) */
771 p = fgets(line, n, mapfp);
772 if (!p)
773 goto out;
774
775 line_num = 1;
776 while (1) {
777 char *cpuid, *version, *type, *fname;
778
779 line_num++;
780 p = fgets(line, n, mapfp);
781 if (!p)
782 break;
783
784 if (line[0] == '#' || line[0] == '\n')
785 continue;
786
787 if (line[strlen(line)-1] != '\n') {
788 /* TODO Deal with lines longer than 16K */
789 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
790 prog, fpath, line_num);
791 return -1;
792 }
793 line[strlen(line)-1] = '\0';
794
795 cpuid = fixregex(strtok_r(p, ",", &save));
796 version = strtok_r(NULL, ",", &save);
797 fname = strtok_r(NULL, ",", &save);
798 type = strtok_r(NULL, ",", &save);
799
800 tblname = file_name_to_table_name(fname);
801 fprintf(outfp, "{\n");
802 fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
803 fprintf(outfp, "\t.version = \"%s\",\n", version);
804 fprintf(outfp, "\t.type = \"%s\",\n", type);
805
806 /*
807 * CHECK: We can't use the type (eg "core") field in the
808 * table name. For us to do that, we need to somehow tweak
809 * the other caller of file_name_to_table(), process_json()
810 * to determine the type. process_json() file has no way
811 * of knowing these are "core" events unless file name has
812 * core in it. If filename has core in it, we can safely
813 * ignore the type field here also.
814 */
815 fprintf(outfp, "\t.table = %s\n", tblname);
816 fprintf(outfp, "},\n");
817 }
818
819out:
820 print_mapping_table_suffix(outfp);
821 return 0;
822}
823
824/*
825 * If we fail to locate/process JSON and map files, create a NULL mapping
826 * table. This would at least allow perf to build even if we can't find/use
827 * the aliases.
828 */
829static void create_empty_mapping(const char *output_file)
830{
831 FILE *outfp;
832
833 pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
834
835 /* Truncate file to clear any partial writes to it */
836 outfp = fopen(output_file, "w");
837 if (!outfp) {
838 perror("fopen()");
839 _Exit(1);
840 }
841
842 fprintf(outfp, "#include \"../../pmu-events/pmu-events.h\"\n");
843 print_mapping_table_prefix(outfp);
844 print_mapping_table_suffix(outfp);
845 fclose(outfp);
846}
847
848static int get_maxfds(void)
849{
850 struct rlimit rlim;
851
852 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
853 return min((int)rlim.rlim_max / 2, 512);
854
855 return 512;
856}
857
858/*
859 * nftw() doesn't let us pass an argument to the processing function,
860 * so use a global variables.
861 */
862static FILE *eventsfp;
863static char *mapfile;
864
865static int is_leaf_dir(const char *fpath)
866{
867 DIR *d;
868 struct dirent *dir;
869 int res = 1;
870
871 d = opendir(fpath);
872 if (!d)
873 return 0;
874
875 while ((dir = readdir(d)) != NULL) {
876 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
877 continue;
878
879 if (dir->d_type == DT_DIR) {
880 res = 0;
881 break;
882 } else if (dir->d_type == DT_UNKNOWN) {
883 char path[PATH_MAX];
884 struct stat st;
885
886 sprintf(path, "%s/%s", fpath, dir->d_name);
887 if (stat(path, &st))
888 break;
889
890 if (S_ISDIR(st.st_mode)) {
891 res = 0;
892 break;
893 }
894 }
895 }
896
897 closedir(d);
898
899 return res;
900}
901
902static int is_json_file(const char *name)
903{
904 const char *suffix;
905
906 if (strlen(name) < 5)
907 return 0;
908
909 suffix = name + strlen(name) - 5;
910
911 if (strncmp(suffix, ".json", 5) == 0)
912 return 1;
913 return 0;
914}
915
916static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
917 int typeflag, struct FTW *ftwbuf)
918{
919 int level = ftwbuf->level;
920 int is_file = typeflag == FTW_F;
921
922 if (level == 1 && is_file && is_json_file(fpath))
923 return json_events(fpath, save_arch_std_events, (void *)sb);
924
925 return 0;
926}
927
928static int process_one_file(const char *fpath, const struct stat *sb,
929 int typeflag, struct FTW *ftwbuf)
930{
931 char *tblname, *bname;
932 int is_dir = typeflag == FTW_D;
933 int is_file = typeflag == FTW_F;
934 int level = ftwbuf->level;
935 int err = 0;
936
937 if (level == 2 && is_dir) {
938 /*
939 * For level 2 directory, bname will include parent name,
940 * like vendor/platform. So search back from platform dir
941 * to find this.
942 */
943 bname = (char *) fpath + ftwbuf->base - 2;
944 for (;;) {
945 if (*bname == '/')
946 break;
947 bname--;
948 }
949 bname++;
950 } else
951 bname = (char *) fpath + ftwbuf->base;
952
953 pr_debug("%s %d %7jd %-20s %s\n",
954 is_file ? "f" : is_dir ? "d" : "x",
955 level, sb->st_size, bname, fpath);
956
957 /* base dir or too deep */
958 if (level == 0 || level > 3)
959 return 0;
960
961
962 /* model directory, reset topic */
963 if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
964 (level == 2 && is_dir)) {
965 if (close_table)
966 print_events_table_suffix(eventsfp);
967
968 /*
969 * Drop file name suffix. Replace hyphens with underscores.
970 * Fail if file name contains any alphanum characters besides
971 * underscores.
972 */
973 tblname = file_name_to_table_name(bname);
974 if (!tblname) {
975 pr_info("%s: Error determining table name for %s\n", prog,
976 bname);
977 return -1;
978 }
979
980 print_events_table_prefix(eventsfp, tblname);
981 return 0;
982 }
983
984 /*
985 * Save the mapfile name for now. We will process mapfile
986 * after processing all JSON files (so we can write out the
987 * mapping table after all PMU events tables).
988 *
989 */
990 if (level == 1 && is_file) {
991 if (!strcmp(bname, "mapfile.csv")) {
992 mapfile = strdup(fpath);
993 return 0;
994 }
995
996 pr_info("%s: Ignoring file %s\n", prog, fpath);
997 return 0;
998 }
999
1000 /*
1001 * If the file name does not have a .json extension,
1002 * ignore it. It could be a readme.txt for instance.
1003 */
1004 if (is_file) {
1005 if (!is_json_file(bname)) {
1006 pr_info("%s: Ignoring file without .json suffix %s\n", prog,
1007 fpath);
1008 return 0;
1009 }
1010 }
1011
1012 if (level > 1 && add_topic(bname))
1013 return -ENOMEM;
1014
1015 /*
1016 * Assume all other files are JSON files.
1017 *
1018 * If mapfile refers to 'power7_core.json', we create a table
1019 * named 'power7_core'. Any inconsistencies between the mapfile
1020 * and directory tree could result in build failure due to table
1021 * names not being found.
1022 *
1023 * Atleast for now, be strict with processing JSON file names.
1024 * i.e. if JSON file name cannot be mapped to C-style table name,
1025 * fail.
1026 */
1027 if (is_file) {
1028 struct perf_entry_data data = {
1029 .topic = get_topic(),
1030 .outfp = eventsfp,
1031 };
1032
1033 err = json_events(fpath, print_events_table_entry, &data);
1034
1035 free(data.topic);
1036 }
1037
1038 return err;
1039}
1040
1041#ifndef PATH_MAX
1042#define PATH_MAX 4096
1043#endif
1044
1045/*
1046 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1047 * the set of JSON files for the architecture 'arch'.
1048 *
1049 * From each JSON file, create a C-style "PMU events table" from the
1050 * JSON file (see struct pmu_event).
1051 *
1052 * From the mapfile, create a mapping between the CPU revisions and
1053 * PMU event tables (see struct pmu_events_map).
1054 *
1055 * Write out the PMU events tables and the mapping table to pmu-event.c.
1056 */
1057int main(int argc, char *argv[])
1058{
1059 int rc;
1060 int maxfds;
1061 char ldirname[PATH_MAX];
1062
1063 const char *arch;
1064 const char *output_file;
1065 const char *start_dirname;
1066 struct stat stbuf;
1067
1068 prog = basename(argv[0]);
1069 if (argc < 4) {
1070 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
1071 return 1;
1072 }
1073
1074 arch = argv[1];
1075 start_dirname = argv[2];
1076 output_file = argv[3];
1077
1078 if (argc > 4)
1079 verbose = atoi(argv[4]);
1080
1081 eventsfp = fopen(output_file, "w");
1082 if (!eventsfp) {
1083 pr_err("%s Unable to create required file %s (%s)\n",
1084 prog, output_file, strerror(errno));
1085 return 2;
1086 }
1087
1088 sprintf(ldirname, "%s/%s", start_dirname, arch);
1089
1090 /* If architecture does not have any event lists, bail out */
1091 if (stat(ldirname, &stbuf) < 0) {
1092 pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
1093 goto empty_map;
1094 }
1095
1096 /* Include pmu-events.h first */
1097 fprintf(eventsfp, "#include \"../../pmu-events/pmu-events.h\"\n");
1098
1099 /*
1100 * The mapfile allows multiple CPUids to point to the same JSON file,
1101 * so, not sure if there is a need for symlinks within the pmu-events
1102 * directory.
1103 *
1104 * For now, treat symlinks of JSON files as regular files and create
1105 * separate tables for each symlink (presumably, each symlink refers
1106 * to specific version of the CPU).
1107 */
1108
1109 maxfds = get_maxfds();
1110 mapfile = NULL;
1111 rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1112 if (rc && verbose) {
1113 pr_info("%s: Error preprocessing arch standard files %s\n",
1114 prog, ldirname);
1115 goto empty_map;
1116 } else if (rc < 0) {
1117 /* Make build fail */
1118 free_arch_std_events();
1119 return 1;
1120 } else if (rc) {
1121 goto empty_map;
1122 }
1123
1124 rc = nftw(ldirname, process_one_file, maxfds, 0);
1125 if (rc && verbose) {
1126 pr_info("%s: Error walking file tree %s\n", prog, ldirname);
1127 goto empty_map;
1128 } else if (rc < 0) {
1129 /* Make build fail */
1130 free_arch_std_events();
1131 return 1;
1132 } else if (rc) {
1133 goto empty_map;
1134 }
1135
1136 if (close_table)
1137 print_events_table_suffix(eventsfp);
1138
1139 if (!mapfile) {
1140 pr_info("%s: No CPU->JSON mapping?\n", prog);
1141 goto empty_map;
1142 }
1143
1144 if (process_mapfile(eventsfp, mapfile)) {
1145 pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
1146 /* Make build fail */
1147 return 1;
1148 }
1149
1150 return 0;
1151
1152empty_map:
1153 fclose(eventsfp);
1154 create_empty_mapping(output_file);
1155 free_arch_std_events();
1156 return 0;
1157}
1#define _XOPEN_SOURCE 500 /* needed for nftw() */
2#define _GNU_SOURCE /* needed for asprintf() */
3
4/* Parse event JSON files */
5
6/*
7 * Copyright (c) 2014, Intel Corporation
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 *
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.
19 *
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.
32*/
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <errno.h>
37#include <string.h>
38#include <ctype.h>
39#include <unistd.h>
40#include <stdarg.h>
41#include <libgen.h>
42#include <limits.h>
43#include <dirent.h>
44#include <sys/time.h> /* getrlimit */
45#include <sys/resource.h> /* getrlimit */
46#include <ftw.h>
47#include <sys/stat.h>
48#include <linux/list.h>
49#include "jsmn.h"
50#include "json.h"
51#include "jevents.h"
52
53int verbose;
54char *prog;
55
56int eprintf(int level, int var, const char *fmt, ...)
57{
58
59 int ret;
60 va_list args;
61
62 if (var < level)
63 return 0;
64
65 va_start(args, fmt);
66
67 ret = vfprintf(stderr, fmt, args);
68
69 va_end(args);
70
71 return ret;
72}
73
74__attribute__((weak)) char *get_cpu_str(void)
75{
76 return NULL;
77}
78
79static void addfield(char *map, char **dst, const char *sep,
80 const char *a, jsmntok_t *bt)
81{
82 unsigned int len = strlen(a) + 1 + strlen(sep);
83 int olen = *dst ? strlen(*dst) : 0;
84 int blen = bt ? json_len(bt) : 0;
85 char *out;
86
87 out = realloc(*dst, len + olen + blen);
88 if (!out) {
89 /* Don't add field in this case */
90 return;
91 }
92 *dst = out;
93
94 if (!olen)
95 *(*dst) = 0;
96 else
97 strcat(*dst, sep);
98 strcat(*dst, a);
99 if (bt)
100 strncat(*dst, map + bt->start, blen);
101}
102
103static void fixname(char *s)
104{
105 for (; *s; s++)
106 *s = tolower(*s);
107}
108
109static void fixdesc(char *s)
110{
111 char *e = s + strlen(s);
112
113 /* Remove trailing dots that look ugly in perf list */
114 --e;
115 while (e >= s && isspace(*e))
116 --e;
117 if (*e == '.')
118 *e = 0;
119}
120
121/* Add escapes for '\' so they are proper C strings. */
122static char *fixregex(char *s)
123{
124 int len = 0;
125 int esc_count = 0;
126 char *fixed = NULL;
127 char *p, *q;
128
129 /* Count the number of '\' in string */
130 for (p = s; *p; p++) {
131 ++len;
132 if (*p == '\\')
133 ++esc_count;
134 }
135
136 if (esc_count == 0)
137 return s;
138
139 /* allocate space for a new string */
140 fixed = (char *) malloc(len + esc_count + 1);
141 if (!fixed)
142 return NULL;
143
144 /* copy over the characters */
145 q = fixed;
146 for (p = s; *p; p++) {
147 if (*p == '\\') {
148 *q = '\\';
149 ++q;
150 }
151 *q = *p;
152 ++q;
153 }
154 *q = '\0';
155 return fixed;
156}
157
158static struct msrmap {
159 const char *num;
160 const char *pname;
161} msrmap[] = {
162 { "0x3F6", "ldlat=" },
163 { "0x1A6", "offcore_rsp=" },
164 { "0x1A7", "offcore_rsp=" },
165 { "0x3F7", "frontend=" },
166 { NULL, NULL }
167};
168
169static struct field {
170 const char *field;
171 const char *kernel;
172} fields[] = {
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=" },
181 { NULL, NULL }
182};
183
184static void cut_comma(char *map, jsmntok_t *newval)
185{
186 int i;
187
188 /* Cut off everything after comma */
189 for (i = newval->start; i < newval->end; i++) {
190 if (map[i] == ',')
191 newval->end = i;
192 }
193}
194
195static int match_field(char *map, jsmntok_t *field, int nz,
196 char **event, jsmntok_t *val)
197{
198 struct field *f;
199 jsmntok_t newval = *val;
200
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);
205 return 1;
206 }
207 return 0;
208}
209
210static struct msrmap *lookup_msr(char *map, jsmntok_t *val)
211{
212 jsmntok_t newval = *val;
213 static bool warned;
214 int i;
215
216 cut_comma(map, &newval);
217 for (i = 0; msrmap[i].num; i++)
218 if (json_streq(map, &newval, msrmap[i].num))
219 return &msrmap[i];
220 if (!warned) {
221 warned = true;
222 pr_err("%s: Unknown MSR in event file %.*s\n", prog,
223 json_len(val), map + val->start);
224 }
225 return NULL;
226}
227
228static struct map {
229 const char *json;
230 const char *perf;
231} unit_to_pmu[] = {
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" },
242 { "L3PMC", "amd_l3" },
243 {}
244};
245
246static const char *field_to_perf(struct map *table, char *map, jsmntok_t *val)
247{
248 int i;
249
250 for (i = 0; table[i].json; i++) {
251 if (json_streq(map, val, table[i].json))
252 return table[i].perf;
253 }
254 return NULL;
255}
256
257#define EXPECT(e, t, m) do { if (!(e)) { \
258 jsmntok_t *loc = (t); \
259 if (!(t)->start && (t) > tokens) \
260 loc = (t) - 1; \
261 pr_err("%s:%d: " m ", got %s\n", fn, \
262 json_line(map, loc), \
263 json_name(t)); \
264 err = -EIO; \
265 goto out_free; \
266} } while (0)
267
268static char *topic;
269
270static char *get_topic(void)
271{
272 char *tp;
273 int i;
274
275 /* tp is free'd in process_one_file() */
276 i = asprintf(&tp, "%s", topic);
277 if (i < 0) {
278 pr_info("%s: asprintf() error %s\n", prog);
279 return NULL;
280 }
281
282 for (i = 0; i < (int) strlen(tp); i++) {
283 char c = tp[i];
284
285 if (c == '-')
286 tp[i] = ' ';
287 else if (c == '.') {
288 tp[i] = '\0';
289 break;
290 }
291 }
292
293 return tp;
294}
295
296static int add_topic(char *bname)
297{
298 free(topic);
299 topic = strdup(bname);
300 if (!topic) {
301 pr_info("%s: strdup() error %s for file %s\n", prog,
302 strerror(errno), bname);
303 return -ENOMEM;
304 }
305 return 0;
306}
307
308struct perf_entry_data {
309 FILE *outfp;
310 char *topic;
311};
312
313static int close_table;
314
315static void print_events_table_prefix(FILE *fp, const char *tblname)
316{
317 fprintf(fp, "struct pmu_event %s[] = {\n", tblname);
318 close_table = 1;
319}
320
321static int print_events_table_entry(void *data, char *name, char *event,
322 char *desc, char *long_desc,
323 char *pmu, char *unit, char *perpkg,
324 char *metric_expr,
325 char *metric_name, char *metric_group,
326 char *deprecated, char *metric_constraint)
327{
328 struct perf_entry_data *pd = data;
329 FILE *outfp = pd->outfp;
330 char *topic = pd->topic;
331
332 /*
333 * TODO: Remove formatting chars after debugging to reduce
334 * string lengths.
335 */
336 fprintf(outfp, "{\n");
337
338 if (name)
339 fprintf(outfp, "\t.name = \"%s\",\n", name);
340 if (event)
341 fprintf(outfp, "\t.event = \"%s\",\n", event);
342 fprintf(outfp, "\t.desc = \"%s\",\n", desc);
343 fprintf(outfp, "\t.topic = \"%s\",\n", topic);
344 if (long_desc && long_desc[0])
345 fprintf(outfp, "\t.long_desc = \"%s\",\n", long_desc);
346 if (pmu)
347 fprintf(outfp, "\t.pmu = \"%s\",\n", pmu);
348 if (unit)
349 fprintf(outfp, "\t.unit = \"%s\",\n", unit);
350 if (perpkg)
351 fprintf(outfp, "\t.perpkg = \"%s\",\n", perpkg);
352 if (metric_expr)
353 fprintf(outfp, "\t.metric_expr = \"%s\",\n", metric_expr);
354 if (metric_name)
355 fprintf(outfp, "\t.metric_name = \"%s\",\n", metric_name);
356 if (metric_group)
357 fprintf(outfp, "\t.metric_group = \"%s\",\n", metric_group);
358 if (deprecated)
359 fprintf(outfp, "\t.deprecated = \"%s\",\n", deprecated);
360 if (metric_constraint)
361 fprintf(outfp, "\t.metric_constraint = \"%s\",\n", metric_constraint);
362 fprintf(outfp, "},\n");
363
364 return 0;
365}
366
367struct event_struct {
368 struct list_head list;
369 char *name;
370 char *event;
371 char *desc;
372 char *long_desc;
373 char *pmu;
374 char *unit;
375 char *perpkg;
376 char *metric_expr;
377 char *metric_name;
378 char *metric_group;
379 char *deprecated;
380 char *metric_constraint;
381};
382
383#define ADD_EVENT_FIELD(field) do { if (field) { \
384 es->field = strdup(field); \
385 if (!es->field) \
386 goto out_free; \
387} } while (0)
388
389#define FREE_EVENT_FIELD(field) free(es->field)
390
391#define TRY_FIXUP_FIELD(field) do { if (es->field && !*field) {\
392 *field = strdup(es->field); \
393 if (!*field) \
394 return -ENOMEM; \
395} } while (0)
396
397#define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
398 op(name); \
399 op(event); \
400 op(desc); \
401 op(long_desc); \
402 op(pmu); \
403 op(unit); \
404 op(perpkg); \
405 op(metric_expr); \
406 op(metric_name); \
407 op(metric_group); \
408 op(deprecated); \
409} while (0)
410
411static LIST_HEAD(arch_std_events);
412
413static void free_arch_std_events(void)
414{
415 struct event_struct *es, *next;
416
417 list_for_each_entry_safe(es, next, &arch_std_events, list) {
418 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
419 list_del_init(&es->list);
420 free(es);
421 }
422}
423
424static int save_arch_std_events(void *data, char *name, char *event,
425 char *desc, char *long_desc, char *pmu,
426 char *unit, char *perpkg, char *metric_expr,
427 char *metric_name, char *metric_group,
428 char *deprecated, char *metric_constraint)
429{
430 struct event_struct *es;
431
432 es = malloc(sizeof(*es));
433 if (!es)
434 return -ENOMEM;
435 memset(es, 0, sizeof(*es));
436 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD);
437 list_add_tail(&es->list, &arch_std_events);
438 return 0;
439out_free:
440 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
441 free(es);
442 return -ENOMEM;
443}
444
445static void print_events_table_suffix(FILE *outfp)
446{
447 fprintf(outfp, "{\n");
448
449 fprintf(outfp, "\t.name = 0,\n");
450 fprintf(outfp, "\t.event = 0,\n");
451 fprintf(outfp, "\t.desc = 0,\n");
452
453 fprintf(outfp, "},\n");
454 fprintf(outfp, "};\n");
455 close_table = 0;
456}
457
458static struct fixed {
459 const char *name;
460 const char *event;
461} fixed[] = {
462 { "inst_retired.any", "event=0xc0,period=2000003" },
463 { "inst_retired.any_p", "event=0xc0,period=2000003" },
464 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03,period=2000003" },
465 { "cpu_clk_unhalted.thread", "event=0x3c,period=2000003" },
466 { "cpu_clk_unhalted.core", "event=0x3c,period=2000003" },
467 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1,period=2000003" },
468 { NULL, NULL},
469};
470
471/*
472 * Handle different fixed counter encodings between JSON and perf.
473 */
474static char *real_event(const char *name, char *event)
475{
476 int i;
477
478 if (!name)
479 return NULL;
480
481 for (i = 0; fixed[i].name; i++)
482 if (!strcasecmp(name, fixed[i].name))
483 return (char *)fixed[i].event;
484 return event;
485}
486
487static int
488try_fixup(const char *fn, char *arch_std, char **event, char **desc,
489 char **name, char **long_desc, char **pmu, char **filter,
490 char **perpkg, char **unit, char **metric_expr, char **metric_name,
491 char **metric_group, unsigned long long eventcode,
492 char **deprecated, char **metric_constraint)
493{
494 /* try to find matching event from arch standard values */
495 struct event_struct *es;
496
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 */
501 free(*event);
502 *event = NULL;
503 }
504 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
505 return 0;
506 }
507 }
508
509 pr_err("%s: could not find matching %s for %s\n",
510 prog, arch_std, fn);
511 return -1;
512}
513
514/* Call func with each event in the json file */
515int json_events(const char *fn,
516 int (*func)(void *data, char *name, char *event, char *desc,
517 char *long_desc,
518 char *pmu, char *unit, char *perpkg,
519 char *metric_expr,
520 char *metric_name, char *metric_group,
521 char *deprecated, char *metric_constraint),
522 void *data)
523{
524 int err;
525 size_t size;
526 jsmntok_t *tokens, *tok;
527 int i, j, len;
528 char *map;
529 char buf[128];
530
531 if (!fn)
532 return -ENOENT;
533
534 tokens = parse_json(fn, &map, &size, &len);
535 if (!tokens)
536 return -EIO;
537 EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
538 tok = tokens + 1;
539 for (i = 0; i < tokens->size; i++) {
540 char *event = NULL, *desc = NULL, *name = NULL;
541 char *long_desc = NULL;
542 char *extra_desc = NULL;
543 char *pmu = NULL;
544 char *filter = NULL;
545 char *perpkg = NULL;
546 char *unit = NULL;
547 char *metric_expr = NULL;
548 char *metric_name = NULL;
549 char *metric_group = NULL;
550 char *deprecated = NULL;
551 char *metric_constraint = NULL;
552 char *arch_std = NULL;
553 unsigned long long eventcode = 0;
554 struct msrmap *msr = NULL;
555 jsmntok_t *msrval = NULL;
556 jsmntok_t *precise = NULL;
557 jsmntok_t *obj = tok++;
558
559 EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
560 for (j = 0; j < obj->size; j += 2) {
561 jsmntok_t *field, *val;
562 int nz;
563 char *s;
564
565 field = tok + j;
566 EXPECT(field->type == JSMN_STRING, tok + j,
567 "Expected field name");
568 val = tok + j + 1;
569 EXPECT(val->type == JSMN_STRING, tok + j + 1,
570 "Expected string value");
571
572 nz = !json_streq(map, val, "0");
573 if (match_field(map, field, nz, &event, val)) {
574 /* ok */
575 } else if (json_streq(map, field, "EventCode")) {
576 char *code = NULL;
577 addfield(map, &code, "", "", val);
578 eventcode |= strtoul(code, NULL, 0);
579 free(code);
580 } else if (json_streq(map, field, "ExtSel")) {
581 char *code = NULL;
582 addfield(map, &code, "", "", val);
583 eventcode |= strtoul(code, NULL, 0) << 21;
584 free(code);
585 } else if (json_streq(map, field, "EventName")) {
586 addfield(map, &name, "", "", val);
587 } else if (json_streq(map, field, "BriefDescription")) {
588 addfield(map, &desc, "", "", val);
589 fixdesc(desc);
590 } else if (json_streq(map, field,
591 "PublicDescription")) {
592 addfield(map, &long_desc, "", "", val);
593 fixdesc(long_desc);
594 } else if (json_streq(map, field, "PEBS") && nz) {
595 precise = val;
596 } else if (json_streq(map, field, "MSRIndex") && nz) {
597 msr = lookup_msr(map, val);
598 } else if (json_streq(map, field, "MSRValue")) {
599 msrval = val;
600 } else if (json_streq(map, field, "Errata") &&
601 !json_streq(map, val, "null")) {
602 addfield(map, &extra_desc, ". ",
603 " Spec update: ", val);
604 } else if (json_streq(map, field, "Data_LA") && nz) {
605 addfield(map, &extra_desc, ". ",
606 " Supports address when precise",
607 NULL);
608 } else if (json_streq(map, field, "Unit")) {
609 const char *ppmu;
610
611 ppmu = field_to_perf(unit_to_pmu, map, val);
612 if (ppmu) {
613 pmu = strdup(ppmu);
614 } else {
615 if (!pmu)
616 pmu = strdup("uncore_");
617 addfield(map, &pmu, "", "", val);
618 for (s = pmu; *s; s++)
619 *s = tolower(*s);
620 }
621 addfield(map, &desc, ". ", "Unit: ", NULL);
622 addfield(map, &desc, "", pmu, NULL);
623 addfield(map, &desc, "", " ", NULL);
624 } else if (json_streq(map, field, "Filter")) {
625 addfield(map, &filter, "", "", val);
626 } else if (json_streq(map, field, "ScaleUnit")) {
627 addfield(map, &unit, "", "", val);
628 } else if (json_streq(map, field, "PerPkg")) {
629 addfield(map, &perpkg, "", "", val);
630 } else if (json_streq(map, field, "Deprecated")) {
631 addfield(map, &deprecated, "", "", val);
632 } else if (json_streq(map, field, "MetricName")) {
633 addfield(map, &metric_name, "", "", val);
634 } else if (json_streq(map, field, "MetricGroup")) {
635 addfield(map, &metric_group, "", "", val);
636 } else if (json_streq(map, field, "MetricConstraint")) {
637 addfield(map, &metric_constraint, "", "", val);
638 } else if (json_streq(map, field, "MetricExpr")) {
639 addfield(map, &metric_expr, "", "", val);
640 for (s = metric_expr; *s; s++)
641 *s = tolower(*s);
642 } else if (json_streq(map, field, "ArchStdEvent")) {
643 addfield(map, &arch_std, "", "", val);
644 for (s = arch_std; *s; s++)
645 *s = tolower(*s);
646 }
647 /* ignore unknown fields */
648 }
649 if (precise && desc && !strstr(desc, "(Precise Event)")) {
650 if (json_streq(map, precise, "2"))
651 addfield(map, &extra_desc, " ",
652 "(Must be precise)", NULL);
653 else
654 addfield(map, &extra_desc, " ",
655 "(Precise event)", NULL);
656 }
657 snprintf(buf, sizeof buf, "event=%#llx", eventcode);
658 addfield(map, &event, ",", buf, NULL);
659 if (desc && extra_desc)
660 addfield(map, &desc, " ", extra_desc, NULL);
661 if (long_desc && extra_desc)
662 addfield(map, &long_desc, " ", extra_desc, NULL);
663 if (filter)
664 addfield(map, &event, ",", filter, NULL);
665 if (msr != NULL)
666 addfield(map, &event, ",", msr->pname, msrval);
667 if (name)
668 fixname(name);
669
670 if (arch_std) {
671 /*
672 * An arch standard event is referenced, so try to
673 * fixup any unassigned values.
674 */
675 err = try_fixup(fn, arch_std, &event, &desc, &name,
676 &long_desc, &pmu, &filter, &perpkg,
677 &unit, &metric_expr, &metric_name,
678 &metric_group, eventcode,
679 &deprecated, &metric_constraint);
680 if (err)
681 goto free_strings;
682 }
683 err = func(data, name, real_event(name, event), desc, long_desc,
684 pmu, unit, perpkg, metric_expr, metric_name,
685 metric_group, deprecated, metric_constraint);
686free_strings:
687 free(event);
688 free(desc);
689 free(name);
690 free(long_desc);
691 free(extra_desc);
692 free(pmu);
693 free(filter);
694 free(perpkg);
695 free(deprecated);
696 free(unit);
697 free(metric_expr);
698 free(metric_name);
699 free(metric_group);
700 free(metric_constraint);
701 free(arch_std);
702
703 if (err)
704 break;
705 tok += j;
706 }
707 EXPECT(tok - tokens == len, tok, "unexpected objects at end");
708 err = 0;
709out_free:
710 free_json(map, size, tokens);
711 return err;
712}
713
714static char *file_name_to_table_name(char *fname)
715{
716 unsigned int i;
717 int n;
718 int c;
719 char *tblname;
720
721 /*
722 * Ensure tablename starts with alphabetic character.
723 * Derive rest of table name from basename of the JSON file,
724 * replacing hyphens and stripping out .json suffix.
725 */
726 n = asprintf(&tblname, "pme_%s", fname);
727 if (n < 0) {
728 pr_info("%s: asprintf() error %s for file %s\n", prog,
729 strerror(errno), fname);
730 return NULL;
731 }
732
733 for (i = 0; i < strlen(tblname); i++) {
734 c = tblname[i];
735
736 if (c == '-' || c == '/')
737 tblname[i] = '_';
738 else if (c == '.') {
739 tblname[i] = '\0';
740 break;
741 } else if (!isalnum(c) && c != '_') {
742 pr_err("%s: Invalid character '%c' in file name %s\n",
743 prog, c, basename(fname));
744 free(tblname);
745 tblname = NULL;
746 break;
747 }
748 }
749
750 return tblname;
751}
752
753static void print_mapping_table_prefix(FILE *outfp)
754{
755 fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n");
756}
757
758static void print_mapping_table_suffix(FILE *outfp)
759{
760 /*
761 * Print the terminating, NULL entry.
762 */
763 fprintf(outfp, "{\n");
764 fprintf(outfp, "\t.cpuid = 0,\n");
765 fprintf(outfp, "\t.version = 0,\n");
766 fprintf(outfp, "\t.type = 0,\n");
767 fprintf(outfp, "\t.table = 0,\n");
768 fprintf(outfp, "},\n");
769
770 /* and finally, the closing curly bracket for the struct */
771 fprintf(outfp, "};\n");
772}
773
774static void print_mapping_test_table(FILE *outfp)
775{
776 /*
777 * Print the terminating, NULL entry.
778 */
779 fprintf(outfp, "{\n");
780 fprintf(outfp, "\t.cpuid = \"testcpu\",\n");
781 fprintf(outfp, "\t.version = \"v1\",\n");
782 fprintf(outfp, "\t.type = \"core\",\n");
783 fprintf(outfp, "\t.table = pme_test_cpu,\n");
784 fprintf(outfp, "},\n");
785}
786
787static int process_mapfile(FILE *outfp, char *fpath)
788{
789 int n = 16384;
790 FILE *mapfp;
791 char *save = NULL;
792 char *line, *p;
793 int line_num;
794 char *tblname;
795 int ret = 0;
796
797 pr_info("%s: Processing mapfile %s\n", prog, fpath);
798
799 line = malloc(n);
800 if (!line)
801 return -1;
802
803 mapfp = fopen(fpath, "r");
804 if (!mapfp) {
805 pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
806 fpath);
807 free(line);
808 return -1;
809 }
810
811 print_mapping_table_prefix(outfp);
812
813 /* Skip first line (header) */
814 p = fgets(line, n, mapfp);
815 if (!p)
816 goto out;
817
818 line_num = 1;
819 while (1) {
820 char *cpuid, *version, *type, *fname;
821
822 line_num++;
823 p = fgets(line, n, mapfp);
824 if (!p)
825 break;
826
827 if (line[0] == '#' || line[0] == '\n')
828 continue;
829
830 if (line[strlen(line)-1] != '\n') {
831 /* TODO Deal with lines longer than 16K */
832 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
833 prog, fpath, line_num);
834 ret = -1;
835 goto out;
836 }
837 line[strlen(line)-1] = '\0';
838
839 cpuid = fixregex(strtok_r(p, ",", &save));
840 version = strtok_r(NULL, ",", &save);
841 fname = strtok_r(NULL, ",", &save);
842 type = strtok_r(NULL, ",", &save);
843
844 tblname = file_name_to_table_name(fname);
845 fprintf(outfp, "{\n");
846 fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
847 fprintf(outfp, "\t.version = \"%s\",\n", version);
848 fprintf(outfp, "\t.type = \"%s\",\n", type);
849
850 /*
851 * CHECK: We can't use the type (eg "core") field in the
852 * table name. For us to do that, we need to somehow tweak
853 * the other caller of file_name_to_table(), process_json()
854 * to determine the type. process_json() file has no way
855 * of knowing these are "core" events unless file name has
856 * core in it. If filename has core in it, we can safely
857 * ignore the type field here also.
858 */
859 fprintf(outfp, "\t.table = %s\n", tblname);
860 fprintf(outfp, "},\n");
861 }
862
863out:
864 print_mapping_test_table(outfp);
865 print_mapping_table_suffix(outfp);
866 fclose(mapfp);
867 free(line);
868 return ret;
869}
870
871/*
872 * If we fail to locate/process JSON and map files, create a NULL mapping
873 * table. This would at least allow perf to build even if we can't find/use
874 * the aliases.
875 */
876static void create_empty_mapping(const char *output_file)
877{
878 FILE *outfp;
879
880 pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
881
882 /* Truncate file to clear any partial writes to it */
883 outfp = fopen(output_file, "w");
884 if (!outfp) {
885 perror("fopen()");
886 _Exit(1);
887 }
888
889 fprintf(outfp, "#include \"pmu-events/pmu-events.h\"\n");
890 print_mapping_table_prefix(outfp);
891 print_mapping_table_suffix(outfp);
892 fclose(outfp);
893}
894
895static int get_maxfds(void)
896{
897 struct rlimit rlim;
898
899 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
900 return min((int)rlim.rlim_max / 2, 512);
901
902 return 512;
903}
904
905/*
906 * nftw() doesn't let us pass an argument to the processing function,
907 * so use a global variables.
908 */
909static FILE *eventsfp;
910static char *mapfile;
911
912static int is_leaf_dir(const char *fpath)
913{
914 DIR *d;
915 struct dirent *dir;
916 int res = 1;
917
918 d = opendir(fpath);
919 if (!d)
920 return 0;
921
922 while ((dir = readdir(d)) != NULL) {
923 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
924 continue;
925
926 if (dir->d_type == DT_DIR) {
927 res = 0;
928 break;
929 } else if (dir->d_type == DT_UNKNOWN) {
930 char path[PATH_MAX];
931 struct stat st;
932
933 sprintf(path, "%s/%s", fpath, dir->d_name);
934 if (stat(path, &st))
935 break;
936
937 if (S_ISDIR(st.st_mode)) {
938 res = 0;
939 break;
940 }
941 }
942 }
943
944 closedir(d);
945
946 return res;
947}
948
949static int is_json_file(const char *name)
950{
951 const char *suffix;
952
953 if (strlen(name) < 5)
954 return 0;
955
956 suffix = name + strlen(name) - 5;
957
958 if (strncmp(suffix, ".json", 5) == 0)
959 return 1;
960 return 0;
961}
962
963static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
964 int typeflag, struct FTW *ftwbuf)
965{
966 int level = ftwbuf->level;
967 int is_file = typeflag == FTW_F;
968
969 if (level == 1 && is_file && is_json_file(fpath))
970 return json_events(fpath, save_arch_std_events, (void *)sb);
971
972 return 0;
973}
974
975static int process_one_file(const char *fpath, const struct stat *sb,
976 int typeflag, struct FTW *ftwbuf)
977{
978 char *tblname, *bname;
979 int is_dir = typeflag == FTW_D;
980 int is_file = typeflag == FTW_F;
981 int level = ftwbuf->level;
982 int err = 0;
983
984 if (level == 2 && is_dir) {
985 /*
986 * For level 2 directory, bname will include parent name,
987 * like vendor/platform. So search back from platform dir
988 * to find this.
989 */
990 bname = (char *) fpath + ftwbuf->base - 2;
991 for (;;) {
992 if (*bname == '/')
993 break;
994 bname--;
995 }
996 bname++;
997 } else
998 bname = (char *) fpath + ftwbuf->base;
999
1000 pr_debug("%s %d %7jd %-20s %s\n",
1001 is_file ? "f" : is_dir ? "d" : "x",
1002 level, sb->st_size, bname, fpath);
1003
1004 /* base dir or too deep */
1005 if (level == 0 || level > 3)
1006 return 0;
1007
1008
1009 /* model directory, reset topic */
1010 if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
1011 (level == 2 && is_dir)) {
1012 if (close_table)
1013 print_events_table_suffix(eventsfp);
1014
1015 /*
1016 * Drop file name suffix. Replace hyphens with underscores.
1017 * Fail if file name contains any alphanum characters besides
1018 * underscores.
1019 */
1020 tblname = file_name_to_table_name(bname);
1021 if (!tblname) {
1022 pr_info("%s: Error determining table name for %s\n", prog,
1023 bname);
1024 return -1;
1025 }
1026
1027 print_events_table_prefix(eventsfp, tblname);
1028 return 0;
1029 }
1030
1031 /*
1032 * Save the mapfile name for now. We will process mapfile
1033 * after processing all JSON files (so we can write out the
1034 * mapping table after all PMU events tables).
1035 *
1036 */
1037 if (level == 1 && is_file) {
1038 if (!strcmp(bname, "mapfile.csv")) {
1039 mapfile = strdup(fpath);
1040 return 0;
1041 }
1042
1043 pr_info("%s: Ignoring file %s\n", prog, fpath);
1044 return 0;
1045 }
1046
1047 /*
1048 * If the file name does not have a .json extension,
1049 * ignore it. It could be a readme.txt for instance.
1050 */
1051 if (is_file) {
1052 if (!is_json_file(bname)) {
1053 pr_info("%s: Ignoring file without .json suffix %s\n", prog,
1054 fpath);
1055 return 0;
1056 }
1057 }
1058
1059 if (level > 1 && add_topic(bname))
1060 return -ENOMEM;
1061
1062 /*
1063 * Assume all other files are JSON files.
1064 *
1065 * If mapfile refers to 'power7_core.json', we create a table
1066 * named 'power7_core'. Any inconsistencies between the mapfile
1067 * and directory tree could result in build failure due to table
1068 * names not being found.
1069 *
1070 * Atleast for now, be strict with processing JSON file names.
1071 * i.e. if JSON file name cannot be mapped to C-style table name,
1072 * fail.
1073 */
1074 if (is_file) {
1075 struct perf_entry_data data = {
1076 .topic = get_topic(),
1077 .outfp = eventsfp,
1078 };
1079
1080 err = json_events(fpath, print_events_table_entry, &data);
1081
1082 free(data.topic);
1083 }
1084
1085 return err;
1086}
1087
1088#ifndef PATH_MAX
1089#define PATH_MAX 4096
1090#endif
1091
1092/*
1093 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1094 * the set of JSON files for the architecture 'arch'.
1095 *
1096 * From each JSON file, create a C-style "PMU events table" from the
1097 * JSON file (see struct pmu_event).
1098 *
1099 * From the mapfile, create a mapping between the CPU revisions and
1100 * PMU event tables (see struct pmu_events_map).
1101 *
1102 * Write out the PMU events tables and the mapping table to pmu-event.c.
1103 */
1104int main(int argc, char *argv[])
1105{
1106 int rc, ret = 0;
1107 int maxfds;
1108 char ldirname[PATH_MAX];
1109 const char *arch;
1110 const char *output_file;
1111 const char *start_dirname;
1112 struct stat stbuf;
1113
1114 prog = basename(argv[0]);
1115 if (argc < 4) {
1116 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
1117 return 1;
1118 }
1119
1120 arch = argv[1];
1121 start_dirname = argv[2];
1122 output_file = argv[3];
1123
1124 if (argc > 4)
1125 verbose = atoi(argv[4]);
1126
1127 eventsfp = fopen(output_file, "w");
1128 if (!eventsfp) {
1129 pr_err("%s Unable to create required file %s (%s)\n",
1130 prog, output_file, strerror(errno));
1131 return 2;
1132 }
1133
1134 sprintf(ldirname, "%s/%s", start_dirname, arch);
1135
1136 /* If architecture does not have any event lists, bail out */
1137 if (stat(ldirname, &stbuf) < 0) {
1138 pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
1139 goto empty_map;
1140 }
1141
1142 /* Include pmu-events.h first */
1143 fprintf(eventsfp, "#include \"pmu-events/pmu-events.h\"\n");
1144
1145 /*
1146 * The mapfile allows multiple CPUids to point to the same JSON file,
1147 * so, not sure if there is a need for symlinks within the pmu-events
1148 * directory.
1149 *
1150 * For now, treat symlinks of JSON files as regular files and create
1151 * separate tables for each symlink (presumably, each symlink refers
1152 * to specific version of the CPU).
1153 */
1154
1155 maxfds = get_maxfds();
1156 mapfile = NULL;
1157 rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1158 if (rc && verbose) {
1159 pr_info("%s: Error preprocessing arch standard files %s\n",
1160 prog, ldirname);
1161 goto empty_map;
1162 } else if (rc < 0) {
1163 /* Make build fail */
1164 fclose(eventsfp);
1165 free_arch_std_events();
1166 return 1;
1167 } else if (rc) {
1168 goto empty_map;
1169 }
1170
1171 rc = nftw(ldirname, process_one_file, maxfds, 0);
1172 if (rc && verbose) {
1173 pr_info("%s: Error walking file tree %s\n", prog, ldirname);
1174 goto empty_map;
1175 } else if (rc < 0) {
1176 /* Make build fail */
1177 fclose(eventsfp);
1178 free_arch_std_events();
1179 ret = 1;
1180 goto out_free_mapfile;
1181 } else if (rc) {
1182 goto empty_map;
1183 }
1184
1185 sprintf(ldirname, "%s/test", start_dirname);
1186
1187 rc = nftw(ldirname, process_one_file, maxfds, 0);
1188 if (rc && verbose) {
1189 pr_info("%s: Error walking file tree %s rc=%d for test\n",
1190 prog, ldirname, rc);
1191 goto empty_map;
1192 } else if (rc < 0) {
1193 /* Make build fail */
1194 free_arch_std_events();
1195 ret = 1;
1196 goto out_free_mapfile;
1197 } else if (rc) {
1198 goto empty_map;
1199 }
1200
1201 if (close_table)
1202 print_events_table_suffix(eventsfp);
1203
1204 if (!mapfile) {
1205 pr_info("%s: No CPU->JSON mapping?\n", prog);
1206 goto empty_map;
1207 }
1208
1209 if (process_mapfile(eventsfp, mapfile)) {
1210 pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
1211 /* Make build fail */
1212 fclose(eventsfp);
1213 free_arch_std_events();
1214 ret = 1;
1215 }
1216
1217
1218 goto out_free_mapfile;
1219
1220empty_map:
1221 fclose(eventsfp);
1222 create_empty_mapping(output_file);
1223 free_arch_std_events();
1224out_free_mapfile:
1225 free(mapfile);
1226 return ret;
1227}