blob: 3c95affd85a48b2c470851a2b0e59995039802b0 [file] [log] [blame]
Andi Kleen80eeb672016-09-19 17:39:33 -03001#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>
John Garry51ce1dc2018-03-08 18:58:29 +080042#include <limits.h>
Andi Kleen80eeb672016-09-19 17:39:33 -030043#include <dirent.h>
44#include <sys/time.h> /* getrlimit */
45#include <sys/resource.h> /* getrlimit */
46#include <ftw.h>
47#include <sys/stat.h>
John Garrye9d32c12018-03-08 18:58:32 +080048#include <linux/list.h>
Andi Kleen80eeb672016-09-19 17:39:33 -030049#include "jsmn.h"
50#include "json.h"
51#include "jevents.h"
52
Andi Kleen80eeb672016-09-19 17:39:33 -030053int 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
William Cohenfbc28442017-12-04 09:57:28 -0500121/* 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
Andi Kleen80eeb672016-09-19 17:39:33 -0300158static struct msrmap {
159 const char *num;
160 const char *pname;
161} msrmap[] = {
162 { "0x3F6", "ldlat=" },
163 { "0x1A6", "offcore_rsp=" },
164 { "0x1A7", "offcore_rsp=" },
Andi Kleenb42c7362016-09-15 15:24:55 -0700165 { "0x3F7", "frontend=" },
Andi Kleen80eeb672016-09-19 17:39:33 -0300166 { NULL, NULL }
167};
168
169static struct field {
170 const char *field;
171 const char *kernel;
172} fields[] = {
Andi Kleen80eeb672016-09-19 17:39:33 -0300173 { "UMask", "umask=" },
174 { "CounterMask", "cmask=" },
175 { "Invert", "inv=" },
176 { "AnyThread", "any=" },
177 { "EdgeDetect", "edge=" },
178 { "SampleAfterValue", "period=" },
Andi Kleenc73881e2017-08-16 15:02:00 -0700179 { "FCMask", "fc_mask=" },
180 { "PortMask", "ch_mask=" },
Andi Kleen80eeb672016-09-19 17:39:33 -0300181 { 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
Andi Kleenfedb2b52017-01-27 18:03:37 -0800228static 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" },
Andi Kleenaf34cb42017-03-29 17:20:28 -0700235 { "iMPH-U", "uncore_arb" },
Thomas Richter9bacbce2018-06-21 10:04:50 +0200236 { "CPU-M-CF", "cpum_cf" },
237 { "CPU-M-SF", "cpum_sf" },
Kan Liangbf6d18c2019-05-07 06:16:31 -0700238 { "UPI LL", "uncore_upi" },
John Garry57cc7322019-06-28 22:35:50 +0800239 { "hisi_sccl,ddrc", "hisi_sccl,ddrc" },
John Garry8f5b7032019-06-28 22:35:51 +0800240 { "hisi_sccl,hha", "hisi_sccl,hha" },
Andi Kleenfedb2b52017-01-27 18:03:37 -0800241 {}
242};
243
244static const char *field_to_perf(struct map *table, char *map, jsmntok_t *val)
245{
246 int i;
247
248 for (i = 0; table[i].json; i++) {
249 if (json_streq(map, val, table[i].json))
250 return table[i].perf;
251 }
252 return NULL;
253}
254
Andi Kleen80eeb672016-09-19 17:39:33 -0300255#define EXPECT(e, t, m) do { if (!(e)) { \
256 jsmntok_t *loc = (t); \
257 if (!(t)->start && (t) > tokens) \
258 loc = (t) - 1; \
John Garry931ef5d2018-03-08 18:58:27 +0800259 pr_err("%s:%d: " m ", got %s\n", fn, \
260 json_line(map, loc), \
261 json_name(t)); \
262 err = -EIO; \
Andi Kleen80eeb672016-09-19 17:39:33 -0300263 goto out_free; \
264} } while (0)
265
John Garry6f2f2ca2018-03-08 18:58:28 +0800266static char *topic;
Andi Kleen80eeb672016-09-19 17:39:33 -0300267
268static char *get_topic(void)
269{
John Garry6f2f2ca2018-03-08 18:58:28 +0800270 char *tp;
Andi Kleen80eeb672016-09-19 17:39:33 -0300271 int i;
272
John Garry6f2f2ca2018-03-08 18:58:28 +0800273 /* tp is free'd in process_one_file() */
274 i = asprintf(&tp, "%s", topic);
275 if (i < 0) {
276 pr_info("%s: asprintf() error %s\n", prog);
277 return NULL;
Andi Kleen80eeb672016-09-19 17:39:33 -0300278 }
279
280 for (i = 0; i < (int) strlen(tp); i++) {
281 char c = tp[i];
282
283 if (c == '-')
284 tp[i] = ' ';
285 else if (c == '.') {
286 tp[i] = '\0';
287 break;
288 }
289 }
290
291 return tp;
292}
293
John Garry6f2f2ca2018-03-08 18:58:28 +0800294static int add_topic(char *bname)
Andi Kleen80eeb672016-09-19 17:39:33 -0300295{
John Garry6f2f2ca2018-03-08 18:58:28 +0800296 free(topic);
Andi Kleen80eeb672016-09-19 17:39:33 -0300297 topic = strdup(bname);
298 if (!topic) {
299 pr_info("%s: strdup() error %s for file %s\n", prog,
300 strerror(errno), bname);
301 return -ENOMEM;
302 }
Andi Kleen80eeb672016-09-19 17:39:33 -0300303 return 0;
304}
305
306struct perf_entry_data {
307 FILE *outfp;
308 char *topic;
309};
310
311static int close_table;
312
313static void print_events_table_prefix(FILE *fp, const char *tblname)
314{
315 fprintf(fp, "struct pmu_event %s[] = {\n", tblname);
316 close_table = 1;
317}
318
319static int print_events_table_entry(void *data, char *name, char *event,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800320 char *desc, char *long_desc,
Andi Kleen00636c32017-03-20 13:17:07 -0700321 char *pmu, char *unit, char *perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700322 char *metric_expr,
Andi Kleen3ba36d32017-08-31 12:40:27 -0700323 char *metric_name, char *metric_group)
Andi Kleen80eeb672016-09-19 17:39:33 -0300324{
325 struct perf_entry_data *pd = data;
326 FILE *outfp = pd->outfp;
327 char *topic = pd->topic;
328
329 /*
330 * TODO: Remove formatting chars after debugging to reduce
331 * string lengths.
332 */
333 fprintf(outfp, "{\n");
334
Andi Kleen3ba36d32017-08-31 12:40:27 -0700335 if (name)
336 fprintf(outfp, "\t.name = \"%s\",\n", name);
337 if (event)
338 fprintf(outfp, "\t.event = \"%s\",\n", event);
Andi Kleen80eeb672016-09-19 17:39:33 -0300339 fprintf(outfp, "\t.desc = \"%s\",\n", desc);
340 fprintf(outfp, "\t.topic = \"%s\",\n", topic);
Sukadev Bhattiprolu794ba542016-09-15 15:24:47 -0700341 if (long_desc && long_desc[0])
342 fprintf(outfp, "\t.long_desc = \"%s\",\n", long_desc);
Andi Kleenfedb2b52017-01-27 18:03:37 -0800343 if (pmu)
344 fprintf(outfp, "\t.pmu = \"%s\",\n", pmu);
345 if (unit)
346 fprintf(outfp, "\t.unit = \"%s\",\n", unit);
347 if (perpkg)
348 fprintf(outfp, "\t.perpkg = \"%s\",\n", perpkg);
Andi Kleen00636c32017-03-20 13:17:07 -0700349 if (metric_expr)
350 fprintf(outfp, "\t.metric_expr = \"%s\",\n", metric_expr);
Andi Kleen96284812017-03-20 13:17:10 -0700351 if (metric_name)
352 fprintf(outfp, "\t.metric_name = \"%s\",\n", metric_name);
Andi Kleen3ba36d32017-08-31 12:40:27 -0700353 if (metric_group)
354 fprintf(outfp, "\t.metric_group = \"%s\",\n", metric_group);
Andi Kleen80eeb672016-09-19 17:39:33 -0300355 fprintf(outfp, "},\n");
356
357 return 0;
358}
359
John Garrye9d32c12018-03-08 18:58:32 +0800360struct event_struct {
361 struct list_head list;
362 char *name;
363 char *event;
364 char *desc;
365 char *long_desc;
366 char *pmu;
367 char *unit;
368 char *perpkg;
369 char *metric_expr;
370 char *metric_name;
371 char *metric_group;
372};
373
374#define ADD_EVENT_FIELD(field) do { if (field) { \
375 es->field = strdup(field); \
376 if (!es->field) \
377 goto out_free; \
378} } while (0)
379
380#define FREE_EVENT_FIELD(field) free(es->field)
381
382#define TRY_FIXUP_FIELD(field) do { if (es->field && !*field) {\
383 *field = strdup(es->field); \
384 if (!*field) \
385 return -ENOMEM; \
386} } while (0)
387
388#define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
389 op(name); \
390 op(event); \
391 op(desc); \
392 op(long_desc); \
393 op(pmu); \
394 op(unit); \
395 op(perpkg); \
396 op(metric_expr); \
397 op(metric_name); \
398 op(metric_group); \
399} while (0)
400
401static LIST_HEAD(arch_std_events);
402
403static void free_arch_std_events(void)
404{
405 struct event_struct *es, *next;
406
407 list_for_each_entry_safe(es, next, &arch_std_events, list) {
408 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
409 list_del(&es->list);
410 free(es);
411 }
412}
413
414static int save_arch_std_events(void *data, char *name, char *event,
415 char *desc, char *long_desc, char *pmu,
416 char *unit, char *perpkg, char *metric_expr,
417 char *metric_name, char *metric_group)
418{
419 struct event_struct *es;
John Garrye9d32c12018-03-08 18:58:32 +0800420
421 es = malloc(sizeof(*es));
422 if (!es)
423 return -ENOMEM;
424 memset(es, 0, sizeof(*es));
425 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD);
426 list_add_tail(&es->list, &arch_std_events);
427 return 0;
428out_free:
429 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
430 free(es);
431 return -ENOMEM;
432}
433
Andi Kleen80eeb672016-09-19 17:39:33 -0300434static void print_events_table_suffix(FILE *outfp)
435{
436 fprintf(outfp, "{\n");
437
438 fprintf(outfp, "\t.name = 0,\n");
439 fprintf(outfp, "\t.event = 0,\n");
440 fprintf(outfp, "\t.desc = 0,\n");
441
442 fprintf(outfp, "},\n");
443 fprintf(outfp, "};\n");
444 close_table = 0;
445}
446
Andi Kleen0b1db472016-09-15 15:24:54 -0700447static struct fixed {
448 const char *name;
449 const char *event;
450} fixed[] = {
451 { "inst_retired.any", "event=0xc0" },
Andi Kleen72c6ff22016-10-05 12:47:12 -0700452 { "inst_retired.any_p", "event=0xc0" },
453 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03" },
Andi Kleen0b1db472016-09-15 15:24:54 -0700454 { "cpu_clk_unhalted.thread", "event=0x3c" },
455 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1" },
456 { NULL, NULL},
457};
458
459/*
460 * Handle different fixed counter encodings between JSON and perf.
461 */
462static char *real_event(const char *name, char *event)
463{
464 int i;
465
Andi Kleen3ba36d32017-08-31 12:40:27 -0700466 if (!name)
467 return NULL;
468
Andi Kleen0b1db472016-09-15 15:24:54 -0700469 for (i = 0; fixed[i].name; i++)
470 if (!strcasecmp(name, fixed[i].name))
471 return (char *)fixed[i].event;
472 return event;
473}
474
John Garrye9d32c12018-03-08 18:58:32 +0800475static int
476try_fixup(const char *fn, char *arch_std, char **event, char **desc,
477 char **name, char **long_desc, char **pmu, char **filter,
478 char **perpkg, char **unit, char **metric_expr, char **metric_name,
479 char **metric_group, unsigned long long eventcode)
480{
481 /* try to find matching event from arch standard values */
482 struct event_struct *es;
483
484 list_for_each_entry(es, &arch_std_events, list) {
485 if (!strcmp(arch_std, es->name)) {
486 if (!eventcode && es->event) {
487 /* allow EventCode to be overridden */
488 free(*event);
489 *event = NULL;
490 }
491 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
492 return 0;
493 }
494 }
495
496 pr_err("%s: could not find matching %s for %s\n",
497 prog, arch_std, fn);
498 return -1;
499}
500
Andi Kleen80eeb672016-09-19 17:39:33 -0300501/* Call func with each event in the json file */
502int json_events(const char *fn,
Sukadev Bhattiprolu794ba542016-09-15 15:24:47 -0700503 int (*func)(void *data, char *name, char *event, char *desc,
Andi Kleenfedb2b52017-01-27 18:03:37 -0800504 char *long_desc,
Andi Kleen00636c32017-03-20 13:17:07 -0700505 char *pmu, char *unit, char *perpkg,
Andi Kleen96284812017-03-20 13:17:10 -0700506 char *metric_expr,
Andi Kleen3ba36d32017-08-31 12:40:27 -0700507 char *metric_name, char *metric_group),
Andi Kleen80eeb672016-09-19 17:39:33 -0300508 void *data)
509{
John Garry931ef5d2018-03-08 18:58:27 +0800510 int err;
Andi Kleen80eeb672016-09-19 17:39:33 -0300511 size_t size;
512 jsmntok_t *tokens, *tok;
513 int i, j, len;
514 char *map;
Andi Kleend5811412017-01-27 18:03:36 -0800515 char buf[128];
Andi Kleen80eeb672016-09-19 17:39:33 -0300516
517 if (!fn)
518 return -ENOENT;
519
520 tokens = parse_json(fn, &map, &size, &len);
521 if (!tokens)
522 return -EIO;
523 EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
524 tok = tokens + 1;
525 for (i = 0; i < tokens->size; i++) {
526 char *event = NULL, *desc = NULL, *name = NULL;
Sukadev Bhattiprolu794ba542016-09-15 15:24:47 -0700527 char *long_desc = NULL;
528 char *extra_desc = NULL;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800529 char *pmu = NULL;
530 char *filter = NULL;
531 char *perpkg = NULL;
532 char *unit = NULL;
Andi Kleen00636c32017-03-20 13:17:07 -0700533 char *metric_expr = NULL;
Andi Kleen96284812017-03-20 13:17:10 -0700534 char *metric_name = NULL;
Andi Kleen3ba36d32017-08-31 12:40:27 -0700535 char *metric_group = NULL;
John Garrye9d32c12018-03-08 18:58:32 +0800536 char *arch_std = NULL;
Andi Kleend5811412017-01-27 18:03:36 -0800537 unsigned long long eventcode = 0;
Andi Kleen80eeb672016-09-19 17:39:33 -0300538 struct msrmap *msr = NULL;
539 jsmntok_t *msrval = NULL;
540 jsmntok_t *precise = NULL;
541 jsmntok_t *obj = tok++;
542
543 EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
544 for (j = 0; j < obj->size; j += 2) {
545 jsmntok_t *field, *val;
546 int nz;
Andi Kleen00636c32017-03-20 13:17:07 -0700547 char *s;
Andi Kleen80eeb672016-09-19 17:39:33 -0300548
549 field = tok + j;
550 EXPECT(field->type == JSMN_STRING, tok + j,
551 "Expected field name");
552 val = tok + j + 1;
553 EXPECT(val->type == JSMN_STRING, tok + j + 1,
554 "Expected string value");
555
556 nz = !json_streq(map, val, "0");
557 if (match_field(map, field, nz, &event, val)) {
558 /* ok */
Andi Kleend5811412017-01-27 18:03:36 -0800559 } else if (json_streq(map, field, "EventCode")) {
560 char *code = NULL;
561 addfield(map, &code, "", "", val);
562 eventcode |= strtoul(code, NULL, 0);
563 free(code);
Andi Kleenfedb2b52017-01-27 18:03:37 -0800564 } else if (json_streq(map, field, "ExtSel")) {
565 char *code = NULL;
566 addfield(map, &code, "", "", val);
567 eventcode |= strtoul(code, NULL, 0) << 21;
568 free(code);
Andi Kleen80eeb672016-09-19 17:39:33 -0300569 } else if (json_streq(map, field, "EventName")) {
570 addfield(map, &name, "", "", val);
571 } else if (json_streq(map, field, "BriefDescription")) {
572 addfield(map, &desc, "", "", val);
573 fixdesc(desc);
Sukadev Bhattiprolu794ba542016-09-15 15:24:47 -0700574 } else if (json_streq(map, field,
575 "PublicDescription")) {
576 addfield(map, &long_desc, "", "", val);
577 fixdesc(long_desc);
Andi Kleen80eeb672016-09-19 17:39:33 -0300578 } else if (json_streq(map, field, "PEBS") && nz) {
579 precise = val;
580 } else if (json_streq(map, field, "MSRIndex") && nz) {
581 msr = lookup_msr(map, val);
582 } else if (json_streq(map, field, "MSRValue")) {
583 msrval = val;
584 } else if (json_streq(map, field, "Errata") &&
585 !json_streq(map, val, "null")) {
Sukadev Bhattiprolu794ba542016-09-15 15:24:47 -0700586 addfield(map, &extra_desc, ". ",
Andi Kleen80eeb672016-09-19 17:39:33 -0300587 " Spec update: ", val);
588 } else if (json_streq(map, field, "Data_LA") && nz) {
Sukadev Bhattiprolu794ba542016-09-15 15:24:47 -0700589 addfield(map, &extra_desc, ". ",
Andi Kleen80eeb672016-09-19 17:39:33 -0300590 " Supports address when precise",
591 NULL);
Andi Kleenfedb2b52017-01-27 18:03:37 -0800592 } else if (json_streq(map, field, "Unit")) {
593 const char *ppmu;
Andi Kleenfedb2b52017-01-27 18:03:37 -0800594
595 ppmu = field_to_perf(unit_to_pmu, map, val);
596 if (ppmu) {
597 pmu = strdup(ppmu);
598 } else {
599 if (!pmu)
600 pmu = strdup("uncore_");
601 addfield(map, &pmu, "", "", val);
602 for (s = pmu; *s; s++)
603 *s = tolower(*s);
604 }
605 addfield(map, &desc, ". ", "Unit: ", NULL);
606 addfield(map, &desc, "", pmu, NULL);
Andi Kleen3401e8d2017-03-29 17:22:18 -0700607 addfield(map, &desc, "", " ", NULL);
Andi Kleenfedb2b52017-01-27 18:03:37 -0800608 } else if (json_streq(map, field, "Filter")) {
609 addfield(map, &filter, "", "", val);
610 } else if (json_streq(map, field, "ScaleUnit")) {
611 addfield(map, &unit, "", "", val);
612 } else if (json_streq(map, field, "PerPkg")) {
613 addfield(map, &perpkg, "", "", val);
Andi Kleen96284812017-03-20 13:17:10 -0700614 } else if (json_streq(map, field, "MetricName")) {
615 addfield(map, &metric_name, "", "", val);
Andi Kleen3ba36d32017-08-31 12:40:27 -0700616 } else if (json_streq(map, field, "MetricGroup")) {
617 addfield(map, &metric_group, "", "", val);
Andi Kleen00636c32017-03-20 13:17:07 -0700618 } else if (json_streq(map, field, "MetricExpr")) {
619 addfield(map, &metric_expr, "", "", val);
620 for (s = metric_expr; *s; s++)
621 *s = tolower(*s);
John Garrye9d32c12018-03-08 18:58:32 +0800622 } else if (json_streq(map, field, "ArchStdEvent")) {
623 addfield(map, &arch_std, "", "", val);
624 for (s = arch_std; *s; s++)
625 *s = tolower(*s);
Andi Kleen80eeb672016-09-19 17:39:33 -0300626 }
627 /* ignore unknown fields */
628 }
629 if (precise && desc && !strstr(desc, "(Precise Event)")) {
630 if (json_streq(map, precise, "2"))
Sukadev Bhattiprolu794ba542016-09-15 15:24:47 -0700631 addfield(map, &extra_desc, " ",
632 "(Must be precise)", NULL);
Andi Kleen80eeb672016-09-19 17:39:33 -0300633 else
Sukadev Bhattiprolu794ba542016-09-15 15:24:47 -0700634 addfield(map, &extra_desc, " ",
Andi Kleen80eeb672016-09-19 17:39:33 -0300635 "(Precise event)", NULL);
636 }
Andi Kleend5811412017-01-27 18:03:36 -0800637 snprintf(buf, sizeof buf, "event=%#llx", eventcode);
638 addfield(map, &event, ",", buf, NULL);
Sukadev Bhattiprolu794ba542016-09-15 15:24:47 -0700639 if (desc && extra_desc)
640 addfield(map, &desc, " ", extra_desc, NULL);
641 if (long_desc && extra_desc)
642 addfield(map, &long_desc, " ", extra_desc, NULL);
Andi Kleenfedb2b52017-01-27 18:03:37 -0800643 if (filter)
644 addfield(map, &event, ",", filter, NULL);
Andi Kleen80eeb672016-09-19 17:39:33 -0300645 if (msr != NULL)
646 addfield(map, &event, ",", msr->pname, msrval);
Andi Kleen3ba36d32017-08-31 12:40:27 -0700647 if (name)
648 fixname(name);
Sukadev Bhattiprolu794ba542016-09-15 15:24:47 -0700649
John Garrye9d32c12018-03-08 18:58:32 +0800650 if (arch_std) {
651 /*
652 * An arch standard event is referenced, so try to
653 * fixup any unassigned values.
654 */
655 err = try_fixup(fn, arch_std, &event, &desc, &name,
656 &long_desc, &pmu, &filter, &perpkg,
657 &unit, &metric_expr, &metric_name,
658 &metric_group, eventcode);
659 if (err)
660 goto free_strings;
661 }
Andi Kleenfedb2b52017-01-27 18:03:37 -0800662 err = func(data, name, real_event(name, event), desc, long_desc,
Andi Kleen3ba36d32017-08-31 12:40:27 -0700663 pmu, unit, perpkg, metric_expr, metric_name, metric_group);
John Garrye9d32c12018-03-08 18:58:32 +0800664free_strings:
Andi Kleen80eeb672016-09-19 17:39:33 -0300665 free(event);
666 free(desc);
667 free(name);
Sukadev Bhattiprolu794ba542016-09-15 15:24:47 -0700668 free(long_desc);
669 free(extra_desc);
Andi Kleenfedb2b52017-01-27 18:03:37 -0800670 free(pmu);
671 free(filter);
672 free(perpkg);
673 free(unit);
Andi Kleen00636c32017-03-20 13:17:07 -0700674 free(metric_expr);
Andi Kleen96284812017-03-20 13:17:10 -0700675 free(metric_name);
Andi Kleen3ba36d32017-08-31 12:40:27 -0700676 free(metric_group);
John Garrye9d32c12018-03-08 18:58:32 +0800677 free(arch_std);
678
Andi Kleen80eeb672016-09-19 17:39:33 -0300679 if (err)
680 break;
681 tok += j;
682 }
683 EXPECT(tok - tokens == len, tok, "unexpected objects at end");
684 err = 0;
685out_free:
686 free_json(map, size, tokens);
687 return err;
688}
689
690static char *file_name_to_table_name(char *fname)
691{
692 unsigned int i;
693 int n;
694 int c;
695 char *tblname;
696
697 /*
698 * Ensure tablename starts with alphabetic character.
699 * Derive rest of table name from basename of the JSON file,
700 * replacing hyphens and stripping out .json suffix.
701 */
John Garry51ce1dc2018-03-08 18:58:29 +0800702 n = asprintf(&tblname, "pme_%s", fname);
Andi Kleen80eeb672016-09-19 17:39:33 -0300703 if (n < 0) {
704 pr_info("%s: asprintf() error %s for file %s\n", prog,
705 strerror(errno), fname);
706 return NULL;
707 }
708
709 for (i = 0; i < strlen(tblname); i++) {
710 c = tblname[i];
711
John Garry51ce1dc2018-03-08 18:58:29 +0800712 if (c == '-' || c == '/')
Andi Kleen80eeb672016-09-19 17:39:33 -0300713 tblname[i] = '_';
714 else if (c == '.') {
715 tblname[i] = '\0';
716 break;
717 } else if (!isalnum(c) && c != '_') {
718 pr_err("%s: Invalid character '%c' in file name %s\n",
719 prog, c, basename(fname));
720 free(tblname);
721 tblname = NULL;
722 break;
723 }
724 }
725
726 return tblname;
727}
728
729static void print_mapping_table_prefix(FILE *outfp)
730{
731 fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n");
732}
733
734static void print_mapping_table_suffix(FILE *outfp)
735{
736 /*
737 * Print the terminating, NULL entry.
738 */
739 fprintf(outfp, "{\n");
740 fprintf(outfp, "\t.cpuid = 0,\n");
741 fprintf(outfp, "\t.version = 0,\n");
742 fprintf(outfp, "\t.type = 0,\n");
743 fprintf(outfp, "\t.table = 0,\n");
744 fprintf(outfp, "},\n");
745
746 /* and finally, the closing curly bracket for the struct */
747 fprintf(outfp, "};\n");
748}
749
750static int process_mapfile(FILE *outfp, char *fpath)
751{
752 int n = 16384;
753 FILE *mapfp;
754 char *save = NULL;
755 char *line, *p;
756 int line_num;
757 char *tblname;
758
759 pr_info("%s: Processing mapfile %s\n", prog, fpath);
760
761 line = malloc(n);
762 if (!line)
763 return -1;
764
765 mapfp = fopen(fpath, "r");
766 if (!mapfp) {
767 pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
768 fpath);
769 return -1;
770 }
771
772 print_mapping_table_prefix(outfp);
773
Andi Kleendc720ff2016-09-15 15:24:51 -0700774 /* Skip first line (header) */
775 p = fgets(line, n, mapfp);
776 if (!p)
777 goto out;
778
779 line_num = 1;
Andi Kleen80eeb672016-09-19 17:39:33 -0300780 while (1) {
781 char *cpuid, *version, *type, *fname;
782
783 line_num++;
784 p = fgets(line, n, mapfp);
785 if (!p)
786 break;
787
788 if (line[0] == '#' || line[0] == '\n')
789 continue;
790
791 if (line[strlen(line)-1] != '\n') {
792 /* TODO Deal with lines longer than 16K */
793 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
794 prog, fpath, line_num);
795 return -1;
796 }
797 line[strlen(line)-1] = '\0';
798
William Cohenfbc28442017-12-04 09:57:28 -0500799 cpuid = fixregex(strtok_r(p, ",", &save));
Andi Kleen80eeb672016-09-19 17:39:33 -0300800 version = strtok_r(NULL, ",", &save);
801 fname = strtok_r(NULL, ",", &save);
802 type = strtok_r(NULL, ",", &save);
803
804 tblname = file_name_to_table_name(fname);
805 fprintf(outfp, "{\n");
806 fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
807 fprintf(outfp, "\t.version = \"%s\",\n", version);
808 fprintf(outfp, "\t.type = \"%s\",\n", type);
809
810 /*
811 * CHECK: We can't use the type (eg "core") field in the
812 * table name. For us to do that, we need to somehow tweak
813 * the other caller of file_name_to_table(), process_json()
814 * to determine the type. process_json() file has no way
815 * of knowing these are "core" events unless file name has
816 * core in it. If filename has core in it, we can safely
817 * ignore the type field here also.
818 */
819 fprintf(outfp, "\t.table = %s\n", tblname);
820 fprintf(outfp, "},\n");
821 }
822
Andi Kleendc720ff2016-09-15 15:24:51 -0700823out:
Andi Kleen80eeb672016-09-19 17:39:33 -0300824 print_mapping_table_suffix(outfp);
Andi Kleen80eeb672016-09-19 17:39:33 -0300825 return 0;
826}
827
828/*
829 * If we fail to locate/process JSON and map files, create a NULL mapping
830 * table. This would at least allow perf to build even if we can't find/use
831 * the aliases.
832 */
833static void create_empty_mapping(const char *output_file)
834{
835 FILE *outfp;
836
837 pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
838
839 /* Truncate file to clear any partial writes to it */
840 outfp = fopen(output_file, "w");
841 if (!outfp) {
842 perror("fopen()");
843 _Exit(1);
844 }
845
Luke Mujica06c642c2019-06-25 10:31:22 -0700846 fprintf(outfp, "#include \"pmu-events/pmu-events.h\"\n");
Andi Kleen80eeb672016-09-19 17:39:33 -0300847 print_mapping_table_prefix(outfp);
848 print_mapping_table_suffix(outfp);
849 fclose(outfp);
850}
851
852static int get_maxfds(void)
853{
854 struct rlimit rlim;
855
856 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
857 return min((int)rlim.rlim_max / 2, 512);
858
859 return 512;
860}
861
862/*
863 * nftw() doesn't let us pass an argument to the processing function,
864 * so use a global variables.
865 */
866static FILE *eventsfp;
867static char *mapfile;
868
John Garry51ce1dc2018-03-08 18:58:29 +0800869static int is_leaf_dir(const char *fpath)
870{
871 DIR *d;
872 struct dirent *dir;
873 int res = 1;
874
875 d = opendir(fpath);
876 if (!d)
877 return 0;
878
879 while ((dir = readdir(d)) != NULL) {
880 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
881 continue;
882
883 if (dir->d_type == DT_DIR) {
884 res = 0;
885 break;
886 } else if (dir->d_type == DT_UNKNOWN) {
887 char path[PATH_MAX];
888 struct stat st;
889
890 sprintf(path, "%s/%s", fpath, dir->d_name);
891 if (stat(path, &st))
892 break;
893
894 if (S_ISDIR(st.st_mode)) {
895 res = 0;
896 break;
897 }
898 }
899 }
900
901 closedir(d);
902
903 return res;
904}
905
John Garrye9d32c12018-03-08 18:58:32 +0800906static int is_json_file(const char *name)
907{
908 const char *suffix;
909
910 if (strlen(name) < 5)
911 return 0;
912
913 suffix = name + strlen(name) - 5;
914
915 if (strncmp(suffix, ".json", 5) == 0)
916 return 1;
917 return 0;
918}
919
920static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
921 int typeflag, struct FTW *ftwbuf)
922{
923 int level = ftwbuf->level;
924 int is_file = typeflag == FTW_F;
925
926 if (level == 1 && is_file && is_json_file(fpath))
927 return json_events(fpath, save_arch_std_events, (void *)sb);
928
929 return 0;
930}
931
Andi Kleen80eeb672016-09-19 17:39:33 -0300932static int process_one_file(const char *fpath, const struct stat *sb,
933 int typeflag, struct FTW *ftwbuf)
934{
John Garry51ce1dc2018-03-08 18:58:29 +0800935 char *tblname, *bname;
Andi Kleen80eeb672016-09-19 17:39:33 -0300936 int is_dir = typeflag == FTW_D;
937 int is_file = typeflag == FTW_F;
938 int level = ftwbuf->level;
939 int err = 0;
940
John Garry51ce1dc2018-03-08 18:58:29 +0800941 if (level == 2 && is_dir) {
942 /*
943 * For level 2 directory, bname will include parent name,
944 * like vendor/platform. So search back from platform dir
945 * to find this.
946 */
947 bname = (char *) fpath + ftwbuf->base - 2;
948 for (;;) {
949 if (*bname == '/')
950 break;
951 bname--;
952 }
953 bname++;
954 } else
955 bname = (char *) fpath + ftwbuf->base;
956
Andi Kleen80eeb672016-09-19 17:39:33 -0300957 pr_debug("%s %d %7jd %-20s %s\n",
958 is_file ? "f" : is_dir ? "d" : "x",
959 level, sb->st_size, bname, fpath);
960
John Garry51ce1dc2018-03-08 18:58:29 +0800961 /* base dir or too deep */
962 if (level == 0 || level > 3)
Andi Kleen80eeb672016-09-19 17:39:33 -0300963 return 0;
964
John Garry51ce1dc2018-03-08 18:58:29 +0800965
Andi Kleen80eeb672016-09-19 17:39:33 -0300966 /* model directory, reset topic */
John Garry51ce1dc2018-03-08 18:58:29 +0800967 if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
968 (level == 2 && is_dir)) {
Andi Kleen80eeb672016-09-19 17:39:33 -0300969 if (close_table)
970 print_events_table_suffix(eventsfp);
971
972 /*
973 * Drop file name suffix. Replace hyphens with underscores.
974 * Fail if file name contains any alphanum characters besides
975 * underscores.
976 */
977 tblname = file_name_to_table_name(bname);
978 if (!tblname) {
979 pr_info("%s: Error determining table name for %s\n", prog,
980 bname);
981 return -1;
982 }
983
984 print_events_table_prefix(eventsfp, tblname);
985 return 0;
986 }
987
988 /*
989 * Save the mapfile name for now. We will process mapfile
990 * after processing all JSON files (so we can write out the
991 * mapping table after all PMU events tables).
992 *
Andi Kleen80eeb672016-09-19 17:39:33 -0300993 */
994 if (level == 1 && is_file) {
John Garry4c0ab162018-03-08 18:58:26 +0800995 if (!strcmp(bname, "mapfile.csv")) {
996 mapfile = strdup(fpath);
Andi Kleen80eeb672016-09-19 17:39:33 -0300997 return 0;
998 }
999
1000 pr_info("%s: Ignoring file %s\n", prog, fpath);
1001 return 0;
1002 }
1003
1004 /*
1005 * If the file name does not have a .json extension,
1006 * ignore it. It could be a readme.txt for instance.
1007 */
1008 if (is_file) {
John Garrye9d32c12018-03-08 18:58:32 +08001009 if (!is_json_file(bname)) {
Andi Kleen80eeb672016-09-19 17:39:33 -03001010 pr_info("%s: Ignoring file without .json suffix %s\n", prog,
1011 fpath);
1012 return 0;
1013 }
1014 }
1015
John Garry6f2f2ca2018-03-08 18:58:28 +08001016 if (level > 1 && add_topic(bname))
Andi Kleen80eeb672016-09-19 17:39:33 -03001017 return -ENOMEM;
1018
1019 /*
1020 * Assume all other files are JSON files.
1021 *
1022 * If mapfile refers to 'power7_core.json', we create a table
1023 * named 'power7_core'. Any inconsistencies between the mapfile
1024 * and directory tree could result in build failure due to table
1025 * names not being found.
1026 *
1027 * Atleast for now, be strict with processing JSON file names.
1028 * i.e. if JSON file name cannot be mapped to C-style table name,
1029 * fail.
1030 */
1031 if (is_file) {
1032 struct perf_entry_data data = {
1033 .topic = get_topic(),
1034 .outfp = eventsfp,
1035 };
1036
1037 err = json_events(fpath, print_events_table_entry, &data);
1038
1039 free(data.topic);
1040 }
1041
1042 return err;
1043}
1044
1045#ifndef PATH_MAX
1046#define PATH_MAX 4096
1047#endif
1048
1049/*
1050 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1051 * the set of JSON files for the architecture 'arch'.
1052 *
1053 * From each JSON file, create a C-style "PMU events table" from the
1054 * JSON file (see struct pmu_event).
1055 *
1056 * From the mapfile, create a mapping between the CPU revisions and
1057 * PMU event tables (see struct pmu_events_map).
1058 *
1059 * Write out the PMU events tables and the mapping table to pmu-event.c.
Andi Kleen80eeb672016-09-19 17:39:33 -03001060 */
1061int main(int argc, char *argv[])
1062{
1063 int rc;
1064 int maxfds;
1065 char ldirname[PATH_MAX];
1066
1067 const char *arch;
1068 const char *output_file;
1069 const char *start_dirname;
Andi Kleen3f056b62017-07-24 17:16:38 -07001070 struct stat stbuf;
Andi Kleen80eeb672016-09-19 17:39:33 -03001071
1072 prog = basename(argv[0]);
1073 if (argc < 4) {
1074 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
1075 return 1;
1076 }
1077
1078 arch = argv[1];
1079 start_dirname = argv[2];
1080 output_file = argv[3];
1081
1082 if (argc > 4)
1083 verbose = atoi(argv[4]);
1084
1085 eventsfp = fopen(output_file, "w");
1086 if (!eventsfp) {
1087 pr_err("%s Unable to create required file %s (%s)\n",
1088 prog, output_file, strerror(errno));
1089 return 2;
1090 }
1091
Andi Kleen3f056b62017-07-24 17:16:38 -07001092 sprintf(ldirname, "%s/%s", start_dirname, arch);
1093
1094 /* If architecture does not have any event lists, bail out */
1095 if (stat(ldirname, &stbuf) < 0) {
1096 pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
1097 goto empty_map;
1098 }
1099
Andi Kleen80eeb672016-09-19 17:39:33 -03001100 /* Include pmu-events.h first */
Luke Mujica06c642c2019-06-25 10:31:22 -07001101 fprintf(eventsfp, "#include \"pmu-events/pmu-events.h\"\n");
Andi Kleen80eeb672016-09-19 17:39:33 -03001102
Andi Kleen80eeb672016-09-19 17:39:33 -03001103 /*
1104 * The mapfile allows multiple CPUids to point to the same JSON file,
1105 * so, not sure if there is a need for symlinks within the pmu-events
1106 * directory.
1107 *
1108 * For now, treat symlinks of JSON files as regular files and create
1109 * separate tables for each symlink (presumably, each symlink refers
1110 * to specific version of the CPU).
1111 */
1112
1113 maxfds = get_maxfds();
1114 mapfile = NULL;
John Garrye9d32c12018-03-08 18:58:32 +08001115 rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1116 if (rc && verbose) {
1117 pr_info("%s: Error preprocessing arch standard files %s\n",
1118 prog, ldirname);
1119 goto empty_map;
1120 } else if (rc < 0) {
1121 /* Make build fail */
1122 free_arch_std_events();
1123 return 1;
1124 } else if (rc) {
1125 goto empty_map;
1126 }
1127
Andi Kleen80eeb672016-09-19 17:39:33 -03001128 rc = nftw(ldirname, process_one_file, maxfds, 0);
1129 if (rc && verbose) {
1130 pr_info("%s: Error walking file tree %s\n", prog, ldirname);
1131 goto empty_map;
Andi Kleen3f056b62017-07-24 17:16:38 -07001132 } else if (rc < 0) {
1133 /* Make build fail */
John Garrye9d32c12018-03-08 18:58:32 +08001134 free_arch_std_events();
Andi Kleen3f056b62017-07-24 17:16:38 -07001135 return 1;
Andi Kleen80eeb672016-09-19 17:39:33 -03001136 } else if (rc) {
1137 goto empty_map;
1138 }
1139
1140 if (close_table)
1141 print_events_table_suffix(eventsfp);
1142
1143 if (!mapfile) {
1144 pr_info("%s: No CPU->JSON mapping?\n", prog);
1145 goto empty_map;
1146 }
1147
1148 if (process_mapfile(eventsfp, mapfile)) {
1149 pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
Andi Kleen3f056b62017-07-24 17:16:38 -07001150 /* Make build fail */
1151 return 1;
Andi Kleen80eeb672016-09-19 17:39:33 -03001152 }
1153
1154 return 0;
1155
1156empty_map:
1157 fclose(eventsfp);
1158 create_empty_mapping(output_file);
John Garrye9d32c12018-03-08 18:58:32 +08001159 free_arch_std_events();
Andi Kleen80eeb672016-09-19 17:39:33 -03001160 return 0;
1161}