1 /* Parse event JSON files */
2
3 /*
4 * Copyright (c) 2014, Intel Corporation
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/param.h>
32 #include <sys/resource.h> /* getrlimit */
33 #include <sys/stat.h>
34 #include <sys/time.h> /* getrlimit */
35 #include <ctype.h>
36 #include <dirent.h>
37 #include <errno.h>
38 #include <libgen.h>
39 #include <limits.h>
40 #include <stdarg.h>
41 #include <stddef.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <ftw.h>
47 #include "list.h"
48 #include "jsmn.h"
49 #include "json.h"
50 #include "pmu-events.h"
51
52 static int
53 nftw_ordered(const char *path, int (*fn)(const char *, const struct stat *, int,
54 struct FTW *), int nfds, int ftwflags);
55 #define nftw nftw_ordered
56
57 _Noreturn void _Exit(int);
58 char *get_cpu_str(void);
59
60 int verbose;
61 static char *prog;
62
63 struct json_event {
64 char *name;
65 char *compat;
66 char *event;
67 char *desc;
68 char *long_desc;
69 char *pmu;
70 char *unit;
71 char *perpkg;
72 char *aggr_mode;
73 char *metric_expr;
74 char *metric_threshold;
75 char *metric_name;
76 char *metric_group;
77 char *metric_group_nogroup;
78 char *default_metric_group;
79 char *deprecated;
80 char *metric_constraint;
81 };
82
convert(const char * aggr_mode)83 static enum aggr_mode_class convert(const char *aggr_mode)
84 {
85 if (!strcmp(aggr_mode, "PerCore"))
86 return PerCore;
87 else if (!strcmp(aggr_mode, "PerChip"))
88 return PerChip;
89
90 pr_err("%s: Wrong AggregationMode value '%s'\n", prog, aggr_mode);
91 return -1;
92 }
93
94 static LIST_HEAD(sys_event_tables);
95
96 struct sys_event_table {
97 struct list_head list;
98 char *soc_id;
99 };
100
free_sys_event_tables(void)101 static void free_sys_event_tables(void)
102 {
103 struct sys_event_table *et, *next;
104
105 list_for_each_entry_safe(et, next, &sys_event_tables, list) {
106 free(et->soc_id);
107 free(et);
108 }
109 }
110
eprintf(int level,int var,const char * fmt,...)111 int eprintf(int level, int var, const char *fmt, ...)
112 {
113
114 int ret;
115 va_list args;
116
117 if (var < level)
118 return 0;
119
120 va_start(args, fmt);
121
122 ret = vfprintf(stderr, fmt, args);
123
124 va_end(args);
125
126 return ret;
127 }
128
addfield(char * map,char ** dst,const char * sep,const char * a,jsmntok_t * bt)129 static void addfield(char *map, char **dst, const char *sep,
130 const char *a, jsmntok_t *bt)
131 {
132 unsigned int len = strlen(a) + 1 + strlen(sep);
133 int olen = *dst ? strlen(*dst) : 0;
134 int blen = bt ? json_len(bt) : 0;
135 char *out;
136
137 out = realloc(*dst, len + olen + blen);
138 if (!out) {
139 /* Don't add field in this case */
140 return;
141 }
142 *dst = out;
143
144 if (!olen)
145 *(*dst) = 0;
146 else
147 strcat(*dst, sep);
148 strcat(*dst, a);
149 if (bt)
150 strncat(*dst, map + bt->start, blen);
151 }
152
fixname(char * s)153 static void fixname(char *s)
154 {
155 for (; *s; s++)
156 *s = tolower(*s);
157 }
158
fixdesc(char * s)159 static void fixdesc(char *s)
160 {
161 char *e = s + strlen(s);
162
163 /* Remove trailing dots that look ugly in perf list */
164 --e;
165 while (e >= s && isspace(*e))
166 --e;
167 if (e >= s && *e == '.')
168 *e = 0;
169 }
170
171 /* Add escapes for '\' so they are proper C strings. */
fixregex(char * s)172 static char *fixregex(char *s)
173 {
174 int len = 0;
175 int esc_count = 0;
176 char *fixed = NULL;
177 char *p, *q;
178
179 /* Count the number of '\' in string */
180 for (p = s; *p; p++) {
181 ++len;
182 if (*p == '\\')
183 ++esc_count;
184 }
185
186 if (esc_count == 0)
187 return s;
188
189 /* allocate space for a new string */
190 fixed = (char *) malloc(len + esc_count + 1);
191 if (!fixed)
192 return NULL;
193
194 /* copy over the characters */
195 q = fixed;
196 for (p = s; *p; p++) {
197 if (*p == '\\') {
198 *q = '\\';
199 ++q;
200 }
201 *q = *p;
202 ++q;
203 }
204 *q = '\0';
205 return fixed;
206 }
207
208 static struct msrmap {
209 const char *num;
210 const char *pname;
211 } msrmap[] = {
212 { "0x3F6", "ldlat=" },
213 { "0x1A6", "offcore_rsp=" },
214 { "0x1A7", "offcore_rsp=" },
215 { "0x3F7", "frontend=" },
216 { NULL, NULL }
217 };
218
cut_comma(char * map,jsmntok_t * newval)219 static void cut_comma(char *map, jsmntok_t *newval)
220 {
221 int i;
222
223 /* Cut off everything after comma */
224 for (i = newval->start; i < newval->end; i++) {
225 if (map[i] == ',')
226 newval->end = i;
227 }
228 }
229
lookup_msr(char * map,jsmntok_t * val)230 static struct msrmap *lookup_msr(char *map, jsmntok_t *val)
231 {
232 jsmntok_t newval = *val;
233 static bool warned;
234 int i;
235
236 cut_comma(map, &newval);
237 for (i = 0; msrmap[i].num; i++)
238 if (json_streq(map, &newval, msrmap[i].num))
239 return &msrmap[i];
240 if (!warned) {
241 warned = true;
242 pr_err("%s: Unknown MSR in event file %.*s\n", prog,
243 json_len(val), map + val->start);
244 }
245 return NULL;
246 }
247
248 /*
249 * Converts the unit names to add a prefix used to identify the counter class
250 * in other parts of libpmc. The fallback path for unknown units prefixes the
251 * unit with "uncore_".
252 */
253 static struct map {
254 const char *json;
255 const char *perf;
256 } unit_to_pmu[] = {
257 /* Intel */
258 { "cpu_core", "cpu_core" },
259 { "cpu_atom", "cpu_atom" },
260 { "CBO", "uncore_cbox" },
261 { "QPI LL", "uncore_qpi" },
262 { "SBO", "uncore_sbox" },
263 { "iMPH-U", "uncore_arb" },
264 { "UPI LL", "uncore_upi" },
265 /* AMD */
266 { "L3PMC", "amd_l3" },
267 { "DFPMC", "amd_df" },
268 /* ARM HiSilicon */
269 { "hisi_sicl,cpa", "hisi_sicl,cpa"},
270 { "hisi_sccl,ddrc", "hisi_sccl,ddrc" },
271 { "hisi_sccl,hha", "hisi_sccl,hha" },
272 { "hisi_sccl,l3c", "hisi_sccl,l3c" },
273 /* ARM FreeScale */
274 { "imx8_ddr", "imx8_ddr" },
275 {}
276 };
277
field_to_perf(struct map * table,char * map,jsmntok_t * val)278 static const char *field_to_perf(struct map *table, char *map, jsmntok_t *val)
279 {
280 int i;
281
282 for (i = 0; table[i].json; i++) {
283 if (json_streq(map, val, table[i].json))
284 return table[i].perf;
285 }
286 return NULL;
287 }
288
289 #define EXPECT(e, t, m) do { if (!(e)) { \
290 jsmntok_t *loc = (t); \
291 if (!(t)->start && (t) > tokens) \
292 loc = (t) - 1; \
293 pr_err("%s:%d: " m ", got %s\n", fn, \
294 json_line(map, loc), \
295 json_name(t)); \
296 err = -EIO; \
297 goto out_free; \
298 } } while (0)
299
300 static char *topic;
301
get_topic(void)302 static char *get_topic(void)
303 {
304 char *tp;
305 int i;
306
307 /* tp is free'd in process_one_file() */
308 i = asprintf(&tp, "%s", topic);
309 if (i < 0) {
310 pr_info("%s: asprintf() error %s\n", prog);
311 return NULL;
312 }
313
314 for (i = 0; i < (int) strlen(tp); i++) {
315 char c = tp[i];
316
317 if (c == '-')
318 tp[i] = ' ';
319 else if (c == '.') {
320 tp[i] = '\0';
321 break;
322 }
323 }
324
325 return tp;
326 }
327
add_topic(char * bname)328 static int add_topic(char *bname)
329 {
330 free(topic);
331 topic = strdup(bname);
332 if (!topic) {
333 pr_info("%s: strdup() error %s for file %s\n", prog,
334 strerror(errno), bname);
335 return -ENOMEM;
336 }
337 return 0;
338 }
339
340 struct perf_entry_data {
341 FILE *outfp;
342 char *topic;
343 };
344
345 static int close_table;
346
print_events_table_prefix(FILE * fp,const char * tblname)347 static void print_events_table_prefix(FILE *fp, const char *tblname)
348 {
349 fprintf(fp, "static const struct pmu_event %s[] = {\n", tblname);
350 close_table = 1;
351 }
352
print_events_table_entry(void * data,struct json_event * je)353 static int print_events_table_entry(void *data, struct json_event *je)
354 {
355 struct perf_entry_data *pd = data;
356 FILE *outfp = pd->outfp;
357 char *topic_local = pd->topic;
358
359 /*
360 * TODO: Remove formatting chars after debugging to reduce
361 * string lengths.
362 */
363 fprintf(outfp, "{\n");
364
365 if (je->name)
366 fprintf(outfp, "\t.name = \"%s\",\n", je->name);
367 if (je->event)
368 fprintf(outfp, "\t.event = \"%s\",\n", je->event);
369 fprintf(outfp, "\t.desc = \"%s\",\n", je->desc);
370 if (je->compat)
371 fprintf(outfp, "\t.compat = \"%s\",\n", je->compat);
372 fprintf(outfp, "\t.topic = \"%s\",\n", topic_local);
373 if (je->long_desc && je->long_desc[0])
374 fprintf(outfp, "\t.long_desc = \"%s\",\n", je->long_desc);
375 if (je->pmu)
376 fprintf(outfp, "\t.pmu = \"%s\",\n", je->pmu);
377 if (je->unit)
378 fprintf(outfp, "\t.unit = \"%s\",\n", je->unit);
379 if (je->perpkg)
380 fprintf(outfp, "\t.perpkg = \"%s\",\n", je->perpkg);
381 if (je->aggr_mode)
382 fprintf(outfp, "\t.aggr_mode = \"%d\",\n", convert(je->aggr_mode));
383 if (je->metric_expr)
384 fprintf(outfp, "\t.metric_expr = \"%s\",\n", je->metric_expr);
385 if (je->metric_threshold)
386 fprintf(outfp, "\t.metric_threshold = \"%s\",\n", je->metric_threshold);
387 if (je->metric_name)
388 fprintf(outfp, "\t.metric_name = \"%s\",\n", je->metric_name);
389 if (je->metric_group)
390 fprintf(outfp, "\t.metric_group = \"%s\",\n", je->metric_group);
391 if (je->metric_group_nogroup)
392 fprintf(outfp, "\t.metric_group_nogroup = \"%s\",\n", je->metric_group_nogroup);
393 if (je->default_metric_group)
394 fprintf(outfp, "\t.default_metric_group = \"%s\",\n", je->default_metric_group);
395 if (je->deprecated)
396 fprintf(outfp, "\t.deprecated = \"%s\",\n", je->deprecated);
397 if (je->metric_constraint)
398 fprintf(outfp, "\t.metric_constraint = \"%s\",\n", je->metric_constraint);
399 fprintf(outfp, "},\n");
400
401 return 0;
402 }
403
404 struct event_struct {
405 struct list_head list;
406 char *name;
407 char *event;
408 char *compat;
409 char *desc;
410 char *long_desc;
411 char *pmu;
412 char *unit;
413 char *perpkg;
414 char *aggr_mode;
415 char *metric_expr;
416 char *metric_threshold;
417 char *metric_name;
418 char *metric_group;
419 char *metric_group_nogroup;
420 char *default_metric_group;
421 char *deprecated;
422 char *metric_constraint;
423 };
424
425 #define ADD_EVENT_FIELD(field) do { if (je->field) { \
426 es->field = strdup(je->field); \
427 if (!es->field) \
428 goto out_free; \
429 } } while (0)
430
431 #define FREE_EVENT_FIELD(field) free(es->field)
432
433 #define TRY_FIXUP_FIELD(field) do { if (es->field && !je->field) {\
434 je->field = strdup(es->field); \
435 if (!je->field) \
436 return -ENOMEM; \
437 } } while (0)
438
439 #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
440 op(name); \
441 op(event); \
442 op(desc); \
443 op(long_desc); \
444 op(pmu); \
445 op(unit); \
446 op(perpkg); \
447 op(aggr_mode); \
448 op(metric_expr); \
449 op(metric_threshold); \
450 op(metric_name); \
451 op(metric_group); \
452 op(metric_group_nogroup); \
453 op(default_metric_group); \
454 op(deprecated); \
455 } while (0)
456
457 static LIST_HEAD(arch_std_events);
458
free_arch_std_events(void)459 static void free_arch_std_events(void)
460 {
461 struct event_struct *es, *next;
462
463 list_for_each_entry_safe(es, next, &arch_std_events, list) {
464 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
465 list_del_init(&es->list);
466 free(es);
467 }
468 }
469
save_arch_std_events(void * data __unused,struct json_event * je)470 static int save_arch_std_events(void *data __unused, struct json_event *je)
471 {
472 struct event_struct *es;
473
474 es = malloc(sizeof(*es));
475 if (!es)
476 return -ENOMEM;
477 memset(es, 0, sizeof(*es));
478 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD);
479 list_add_tail(&es->list, &arch_std_events);
480 return 0;
481 out_free:
482 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
483 free(es);
484 return -ENOMEM;
485 }
486
print_events_table_suffix(FILE * outfp)487 static void print_events_table_suffix(FILE *outfp)
488 {
489 fprintf(outfp, "{\n");
490
491 fprintf(outfp, "\t.name = 0,\n");
492 fprintf(outfp, "\t.event = 0,\n");
493 fprintf(outfp, "\t.desc = 0,\n");
494
495 fprintf(outfp, "},\n");
496 fprintf(outfp, "};\n");
497 close_table = 0;
498 }
499
500 static struct fixed {
501 const char *name;
502 const char *event;
503 } fixed[] = {
504 #if 0
505 { "inst_retired.any", "event=0xc0,period=2000003" },
506 { "inst_retired.any_p", "event=0xc0,period=2000003" },
507 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03,period=2000003" },
508 { "cpu_clk_unhalted.thread", "event=0x3c,period=2000003" },
509 { "cpu_clk_unhalted.core", "event=0x3c,period=2000003" },
510 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1,period=2000003" },
511 #endif
512 { NULL, NULL},
513 };
514
515 /*
516 * Handle different fixed counter encodings between JSON and perf.
517 */
real_event(const char * name,char * event)518 static char *real_event(const char *name, char *event)
519 {
520 int i;
521
522 if (!name)
523 return NULL;
524
525 for (i = 0; fixed[i].name; i++)
526 if (!strcasecmp(name, fixed[i].name))
527 return (char *)fixed[i].event;
528 return event;
529 }
530
531 static int
try_fixup(const char * fn,char * arch_std,struct json_event * je,char ** event)532 try_fixup(const char *fn, char *arch_std, struct json_event *je, char **event)
533 {
534 /* try to find matching event from arch standard values */
535 struct event_struct *es;
536
537 list_for_each_entry(es, &arch_std_events, list) {
538 if (!strcmp(arch_std, es->name)) {
539 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
540 *event = je->event;
541 return 0;
542 }
543 }
544
545 pr_err("%s: could not find matching %s for %s\n",
546 prog, arch_std, fn);
547 return -1;
548 }
549
550 /* Call func with each event in the json file */
json_events(const char * fn,int (* func)(void * data,struct json_event * je),void * data)551 static int json_events(const char *fn,
552 int (*func)(void *data, struct json_event *je),
553 void *data)
554 {
555 int err;
556 size_t size;
557 jsmntok_t *tokens, *tok;
558 int i, j, len;
559 char *map;
560 char buf[128];
561
562 if (!fn)
563 return -ENOENT;
564
565 tokens = parse_json(fn, &map, &size, &len);
566 if (!tokens)
567 return -EIO;
568 EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
569 tok = tokens + 1;
570 for (i = 0; i < tokens->size; i++) {
571 char *event = NULL;
572 char *extra_desc = NULL;
573 char *filter = NULL;
574 struct json_event je = {};
575 char *arch_std = NULL;
576 unsigned long long eventcode = 0;
577 unsigned long long configcode = 0;
578 struct msrmap *msr = NULL;
579 jsmntok_t *msrval = NULL;
580 jsmntok_t *precise = NULL;
581 jsmntok_t *obj = tok++;
582 bool configcode_present = false;
583 char *umask = NULL;
584 char *allcores = NULL;
585 char *allslices = NULL;
586 char *sliceid = NULL;
587 char *threadmask = NULL;
588 char *cmask = NULL;
589 char *inv = NULL;
590 char *any = NULL;
591 char *edge = NULL;
592 char *period = NULL;
593 char *fc_mask = NULL;
594 char *ch_mask = NULL;
595
596 EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
597 for (j = 0; j < obj->size; j += 2) {
598 jsmntok_t *field, *val;
599 int nz;
600 char *s;
601
602 field = tok + j;
603 EXPECT(field->type == JSMN_STRING, tok + j,
604 "Expected field name");
605 val = tok + j + 1;
606 EXPECT(val->type == JSMN_STRING, tok + j + 1,
607 "Expected string value");
608
609 nz = !json_streq(map, val, "0");
610 /*
611 * Match the field against known fields. This list is
612 * an explicit whitelist so that the build will break
613 * if we add new json definitions with unimplemented
614 * fields. If a field may contain a zero value that
615 * results in ignoring the field, do not check nz in
616 * the top level conditional statement as it will
617 * result in executing the else clause that reports an
618 * error.
619 */
620 if (json_streq(map, field, "UMask")) {
621 if (nz)
622 addfield(map, &umask, "", "umask=", val);
623 } else if (json_streq(map, field, "EnAllCores")) {
624 addfield(map, &allcores, "", "allcores=", val);
625 } else if (json_streq(map, field, "EnAllSlices")) {
626 addfield(map, &allslices, "", "allslices=", val);
627 } else if (json_streq(map, field, "SliceId")) {
628 /*
629 * We use sourceid because there's a
630 * descripency where the JSON from linux calls
631 * it a SliceId, which is not the name used by
632 * AMD in the PPRs. The field name from Family
633 * 19h and below that calls it slicemask see
634 * the references in hwpmc_amd.h.
635 */
636 addfield(map, &sliceid, "", "sourceid=", val);
637 } else if (json_streq(map, field, "ThreadMask")) {
638 if (nz)
639 addfield(map, &threadmask, "", "l3_thread_mask=", val);
640 } else if (json_streq(map, field, "CounterMask")) {
641 if (nz)
642 addfield(map, &cmask, "", "cmask=", val);
643 } else if (json_streq(map, field, "RdWrMask")) {
644 /* AMD UMC */
645 } else if (json_streq(map, field, "Invert")) {
646 if (nz)
647 addfield(map, &inv, "", "inv=", val);
648 } else if (json_streq(map, field, "AnyThread")) {
649 if (nz)
650 addfield(map, &any, "", "any=", val);
651 } else if (json_streq(map, field, "EdgeDetect")) {
652 if (nz)
653 addfield(map, &edge, "", "edge=", val);
654 } else if (json_streq(map, field, "SampleAfterValue")) {
655 if (nz)
656 addfield(map, &period, "", "period=", val);
657 } else if (json_streq(map, field, "FCMask") && nz) {
658 addfield(map, &fc_mask, "", "fc_mask=", val);
659 } else if (json_streq(map, field, "PortMask") && nz) {
660 addfield(map, &ch_mask, "", "ch_mask=", val);
661 } else if (json_streq(map, field, "EventCode")) {
662 char *code = NULL;
663 addfield(map, &code, "", "", val);
664 eventcode |= strtoul(code, NULL, 0);
665 free(code);
666 } else if (json_streq(map, field, "ConfigCode")) {
667 char *code = NULL;
668 addfield(map, &code, "", "", val);
669 configcode |= strtoul(code, NULL, 0);
670 free(code);
671 configcode_present = true;
672 } else if (json_streq(map, field, "ExtSel")) {
673 char *code = NULL;
674 addfield(map, &code, "", "", val);
675 eventcode |= strtoul(code, NULL, 0) << 8;
676 free(code);
677 } else if (json_streq(map, field, "EventName")) {
678 addfield(map, &je.name, "", "", val);
679 } else if (json_streq(map, field, "Compat")) {
680 addfield(map, &je.compat, "", "", val);
681 } else if (json_streq(map, field, "BriefDescription")) {
682 addfield(map, &je.desc, "", "", val);
683 fixdesc(je.desc);
684 } else if (json_streq(map, field,
685 "PublicDescription")) {
686 addfield(map, &je.long_desc, "", "", val);
687 fixdesc(je.long_desc);
688 } else if (json_streq(map, field, "PEBS") && nz) {
689 precise = val;
690 } else if (json_streq(map, field, "MSRIndex") && nz) {
691 msr = lookup_msr(map, val);
692 } else if (json_streq(map, field, "MSRValue")) {
693 msrval = val;
694 } else if (json_streq(map, field, "Errata") &&
695 !json_streq(map, val, "null")) {
696 addfield(map, &extra_desc, ". ",
697 " Spec update: ", val);
698 } else if (json_streq(map, field, "Data_LA") && nz) {
699 addfield(map, &extra_desc, ". ",
700 " Supports address when precise",
701 NULL);
702 } else if (json_streq(map, field, "Unit")) {
703 const char *ppmu;
704
705 ppmu = field_to_perf(unit_to_pmu, map, val);
706 if (ppmu) {
707 je.pmu = strdup(ppmu);
708 } else {
709 if (!je.pmu)
710 je.pmu = strdup("uncore_");
711 addfield(map, &je.pmu, "", "", val);
712 for (s = je.pmu; *s; s++)
713 *s = tolower(*s);
714 }
715 } else if (json_streq(map, field, "Filter")) {
716 addfield(map, &filter, "", "", val);
717 } else if (json_streq(map, field, "ScaleUnit")) {
718 addfield(map, &je.unit, "", "", val);
719 } else if (json_streq(map, field, "PerPkg")) {
720 addfield(map, &je.perpkg, "", "", val);
721 } else if (json_streq(map, field, "AggregationMode")) {
722 addfield(map, &je.aggr_mode, "", "", val);
723 } else if (json_streq(map, field, "Deprecated")) {
724 addfield(map, &je.deprecated, "", "", val);
725 } else if (json_streq(map, field, "MetricName")) {
726 addfield(map, &je.metric_name, "", "", val);
727 } else if (json_streq(map, field, "MetricGroup")) {
728 addfield(map, &je.metric_group, "", "", val);
729 } else if (json_streq(map, field, "MetricgroupNoGroup")) {
730 addfield(map, &je.metric_group_nogroup, "", "", val);
731 } else if (json_streq(map, field, "DefaultMetricgroupName")) {
732 addfield(map, &je.default_metric_group, "", "", val);
733 } else if (json_streq(map, field, "MetricConstraint")) {
734 addfield(map, &je.metric_constraint, "", "", val);
735 } else if (json_streq(map, field, "MetricExpr")) {
736 addfield(map, &je.metric_expr, "", "", val);
737 } else if (json_streq(map, field, "MetricThreshold")) {
738 addfield(map, &je.metric_threshold, "", "", val);
739 } else if (json_streq(map, field, "ArchStdEvent")) {
740 addfield(map, &arch_std, "", "", val);
741 for (s = arch_std; *s; s++)
742 *s = tolower(*s);
743 } else if (json_streq(map, field, "Offcore")) {
744 /* Check the relevant MSR has been set */
745 } else if (json_streq(map, field, "CounterType")) {
746 /* Unsupported Intel Offcore counters */
747 } else if (json_streq(map, field, "UMaskExt")) {
748 /* Unsupported Intel Offcore counters */
749 } else if (json_streq(map, field, "PDIR_COUNTER")) {
750 /* Intel PEBS not supported */
751 } else if (json_streq(map, field, "CollectPEBSRecord")) {
752 /* Intel PEBS not supported */
753 } else if (json_streq(map, field, "PEBScounters")) {
754 /* Intel PEBS not supported */
755 } else if (json_streq(map, field, "Counter")) {
756 /* Intel PEBS not supported */
757 } else if (json_streq(map, field, "CounterHTOff")) {
758 /* Intel PEBS not supported */
759 } else if (json_streq(map, field, "PRECISE_STORE")) {
760 /* Intel PEBS not supported */
761 } else if (json_streq(map, field, "L1_Hit_Indication")) {
762 /* Intel PEBS not supported */
763 } else if (json_streq(map, field, "RetirementLatencyMin")) {
764 /* Intel TPEBS not supported */
765 } else if (json_streq(map, field, "RetirementLatencyMax")) {
766 /* Intel TPEBS not supported */
767 } else if (json_streq(map, field, "RetirementLatencyMean")) {
768 /* Intel TPEBS not supported */
769 } else if (json_streq(map, field, "Speculative")) {
770 /* Intel informative */
771 } else if (json_streq(map, field, "Experimental")) {
772 /* Intel informative */
773 } else if (json_streq(map, field, "ELLC")) {
774 /* Intel informative */
775 } else if (json_streq(map, field, "TakenAlone")) {
776 /*
777 * Do not measure with other counters, usually
778 * this is because it uses an MSR to filter the
779 * event in a way that affects other counters.
780 */
781 if (json_streq(map, val, "1"))
782 addfield(map, &event, ",", "alone", NULL);
783 } else {
784 /*
785 * We shouldn't ignore unknown fields that may
786 * make the counter invalid!
787 *
788 * Often the JSON definitions are copied
789 * without checking if any fields require
790 * handling.
791 */
792 json_copystr(map, field, buf, sizeof(buf));
793 fprintf(stderr, "Unknown event field '%s' in %s\n", buf, fn);
794 _Exit(1);
795 }
796 }
797 if (precise && je.desc && !strstr(je.desc, "(Precise Event)")) {
798 if (json_streq(map, precise, "2")) {
799 addfield(map, &extra_desc, " ",
800 "(Must be precise)", NULL);
801 addfield(map, &event, ",", "pebs=", precise);
802 } else {
803 addfield(map, &extra_desc, " ",
804 "(Precise event)", NULL);
805 }
806 }
807 if (configcode_present)
808 snprintf(buf, sizeof buf, "config=%#llx", configcode);
809 else
810 snprintf(buf, sizeof buf, "event=%#llx", eventcode);
811 addfield(map, &event, ",", buf, NULL);
812 if (any)
813 addfield(map, &event, ",", any, NULL);
814 if (ch_mask)
815 addfield(map, &event, ",", ch_mask, NULL);
816 if (cmask)
817 addfield(map, &event, ",", cmask, NULL);
818 if (edge)
819 addfield(map, &event, ",", edge, NULL);
820 if (fc_mask)
821 addfield(map, &event, ",", fc_mask, NULL);
822 if (inv)
823 addfield(map, &event, ",", inv, NULL);
824 if (period)
825 addfield(map, &event, ",", period, NULL);
826 if (umask)
827 addfield(map, &event, ",", umask, NULL);
828 if (allcores)
829 addfield(map, &event, ",", allcores, NULL);
830 if (allslices)
831 addfield(map, &event, ",", allslices, NULL);
832 if (sliceid)
833 addfield(map, &event, ",", sliceid, NULL);
834 if (threadmask)
835 addfield(map, &event, ",", threadmask, NULL);
836
837 if (je.desc && extra_desc)
838 addfield(map, &je.desc, " ", extra_desc, NULL);
839 if (je.long_desc && extra_desc)
840 addfield(map, &je.long_desc, " ", extra_desc, NULL);
841 if (je.pmu) {
842 addfield(map, &je.desc, ". ", "Unit: ", NULL);
843 addfield(map, &je.desc, "", je.pmu, NULL);
844 addfield(map, &je.desc, "", " ", NULL);
845 }
846 if (filter)
847 addfield(map, &event, ",", filter, NULL);
848 if (msr != NULL)
849 addfield(map, &event, ",", msr->pname, msrval);
850 if (je.name)
851 fixname(je.name);
852
853 if (arch_std) {
854 /*
855 * An arch standard event is referenced, so try to
856 * fixup any unassigned values.
857 */
858 err = try_fixup(fn, arch_std, &je, &event);
859 if (err)
860 goto free_strings;
861 }
862 je.event = real_event(je.name, event);
863 err = func(data, &je);
864 free_strings:
865 free(umask);
866 free(allcores);
867 free(allslices);
868 free(sliceid);
869 free(threadmask);
870 free(cmask);
871 free(inv);
872 free(any);
873 free(edge);
874 free(period);
875 free(fc_mask);
876 free(ch_mask);
877 free(event);
878 free(je.desc);
879 free(je.name);
880 free(je.compat);
881 free(je.long_desc);
882 free(extra_desc);
883 free(je.pmu);
884 free(filter);
885 free(je.perpkg);
886 free(je.aggr_mode);
887 free(je.deprecated);
888 free(je.unit);
889 free(je.metric_expr);
890 free(je.metric_threshold);
891 free(je.metric_name);
892 free(je.metric_group);
893 free(je.metric_group_nogroup);
894 free(je.default_metric_group);
895 free(je.metric_constraint);
896 free(arch_std);
897
898 if (err)
899 break;
900 tok += j;
901 }
902 EXPECT(tok - tokens == len, tok, "unexpected objects at end");
903 err = 0;
904 out_free:
905 free_json(map, size, tokens);
906 return err;
907 }
908
file_name_to_table_name(char * fname)909 static char *file_name_to_table_name(char *fname)
910 {
911 unsigned int i;
912 int n;
913 int c;
914 char *tblname;
915
916 /*
917 * Ensure tablename starts with alphabetic character.
918 * Derive rest of table name from basename of the JSON file,
919 * replacing hyphens and stripping out .json suffix.
920 */
921 n = asprintf(&tblname, "pme_%s", fname);
922 if (n < 0) {
923 pr_info("%s: asprintf() error %s for file %s\n", prog,
924 strerror(errno), fname);
925 return NULL;
926 }
927
928 for (i = 0; i < strlen(tblname); i++) {
929 c = tblname[i];
930
931 if (c == '-' || c == '/')
932 tblname[i] = '_';
933 else if (c == '.') {
934 tblname[i] = '\0';
935 break;
936 } else if (!isalnum(c) && c != '_') {
937 pr_err("%s: Invalid character '%c' in file name '%s'\n",
938 prog, c, fname);
939 free(tblname);
940 tblname = NULL;
941 break;
942 }
943 }
944
945 return tblname;
946 }
947
is_sys_dir(char * fname)948 static bool is_sys_dir(char *fname)
949 {
950 size_t len = strlen(fname), len2 = strlen("/sys");
951
952 if (len2 > len)
953 return false;
954 return !strcmp(fname+len-len2, "/sys");
955 }
956
print_mapping_table_prefix(FILE * outfp)957 static void print_mapping_table_prefix(FILE *outfp)
958 {
959 fprintf(outfp, "const struct pmu_events_map pmu_events_map[] = {\n");
960 }
961
print_mapping_table_suffix(FILE * outfp)962 static void print_mapping_table_suffix(FILE *outfp)
963 {
964 /*
965 * Print the terminating, NULL entry.
966 */
967 fprintf(outfp, "{\n");
968 fprintf(outfp, "\t.cpuid = 0,\n");
969 fprintf(outfp, "\t.version = 0,\n");
970 fprintf(outfp, "\t.type = 0,\n");
971 fprintf(outfp, "\t.table = 0,\n");
972 fprintf(outfp, "},\n");
973
974 /* and finally, the closing curly bracket for the struct */
975 fprintf(outfp, "};\n");
976 }
977
print_mapping_test_table(FILE * outfp)978 static void print_mapping_test_table(FILE *outfp)
979 {
980 /*
981 * Print the terminating, NULL entry.
982 */
983 fprintf(outfp, "{\n");
984 fprintf(outfp, "\t.cpuid = \"testcpu\",\n");
985 fprintf(outfp, "\t.version = \"v1\",\n");
986 fprintf(outfp, "\t.type = \"core\",\n");
987 fprintf(outfp, "\t.table = pme_test_soc_cpu,\n");
988 fprintf(outfp, "},\n");
989 }
990
print_system_event_mapping_table_prefix(FILE * outfp)991 static void print_system_event_mapping_table_prefix(FILE *outfp)
992 {
993 fprintf(outfp, "\nconst struct pmu_sys_events pmu_sys_event_tables[] = {");
994 }
995
print_system_event_mapping_table_suffix(FILE * outfp)996 static void print_system_event_mapping_table_suffix(FILE *outfp)
997 {
998 fprintf(outfp, "\n\t{\n\t\t.table = 0\n\t},");
999 fprintf(outfp, "\n};\n");
1000 }
1001
process_system_event_tables(FILE * outfp)1002 static int process_system_event_tables(FILE *outfp)
1003 {
1004 struct sys_event_table *sys_event_table;
1005
1006 print_system_event_mapping_table_prefix(outfp);
1007
1008 list_for_each_entry(sys_event_table, &sys_event_tables, list) {
1009 fprintf(outfp, "\n\t{\n\t\t.table = %s,\n\t\t.name = \"%s\",\n\t},",
1010 sys_event_table->soc_id,
1011 sys_event_table->soc_id);
1012 }
1013
1014 print_system_event_mapping_table_suffix(outfp);
1015
1016 return 0;
1017 }
1018
process_mapfile(FILE * outfp,char * fpath)1019 static int process_mapfile(FILE *outfp, char *fpath)
1020 {
1021 int n = 16384;
1022 FILE *mapfp;
1023 char *save = NULL;
1024 char *line, *p;
1025 int line_num;
1026 char *tblname;
1027 int ret = 0;
1028
1029 pr_info("%s: Processing mapfile %s\n", prog, fpath);
1030
1031 line = malloc(n);
1032 if (!line)
1033 return -1;
1034
1035 mapfp = fopen(fpath, "r");
1036 if (!mapfp) {
1037 pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
1038 fpath);
1039 free(line);
1040 return -1;
1041 }
1042
1043 print_mapping_table_prefix(outfp);
1044
1045 /* Skip first line (header) */
1046 p = fgets(line, n, mapfp);
1047 if (!p)
1048 goto out;
1049
1050 line_num = 1;
1051 while (1) {
1052 char *cpuid, *version, *type, *fname;
1053
1054 line_num++;
1055 p = fgets(line, n, mapfp);
1056 if (!p)
1057 break;
1058
1059 if (line[0] == '#' || line[0] == '\n')
1060 continue;
1061
1062 if (line[strlen(line)-1] != '\n') {
1063 /* TODO Deal with lines longer than 16K */
1064 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
1065 prog, fpath, line_num);
1066 ret = -1;
1067 goto out;
1068 }
1069 line[strlen(line)-1] = '\0';
1070
1071 cpuid = fixregex(strtok_r(p, ",", &save));
1072 version = strtok_r(NULL, ",", &save);
1073 fname = strtok_r(NULL, ",", &save);
1074 type = strtok_r(NULL, ",", &save);
1075
1076 tblname = file_name_to_table_name(fname);
1077 fprintf(outfp, "{\n");
1078 fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
1079 fprintf(outfp, "\t.version = \"%s\",\n", version);
1080 fprintf(outfp, "\t.type = \"%s\",\n", type);
1081
1082 /*
1083 * CHECK: We can't use the type (eg "core") field in the
1084 * table name. For us to do that, we need to somehow tweak
1085 * the other caller of file_name_to_table(), process_json()
1086 * to determine the type. process_json() file has no way
1087 * of knowing these are "core" events unless file name has
1088 * core in it. If filename has core in it, we can safely
1089 * ignore the type field here also.
1090 */
1091 fprintf(outfp, "\t.table = %s\n", tblname);
1092 fprintf(outfp, "},\n");
1093 }
1094
1095 out:
1096 print_mapping_test_table(outfp);
1097 print_mapping_table_suffix(outfp);
1098 fclose(mapfp);
1099 free(line);
1100 return ret;
1101 }
1102
1103 /*
1104 * If we fail to locate/process JSON and map files, create a NULL mapping
1105 * table. This would at least allow perf to build even if we can't find/use
1106 * the aliases.
1107 */
create_empty_mapping(const char * output_file)1108 static void create_empty_mapping(const char *output_file)
1109 {
1110 FILE *outfp;
1111
1112 pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
1113
1114 /* Truncate file to clear any partial writes to it */
1115 outfp = fopen(output_file, "w");
1116 if (!outfp) {
1117 perror("fopen()");
1118 _Exit(1);
1119 }
1120
1121 fprintf(outfp, "#include \"pmu-events/pmu-events.h\"\n");
1122 print_mapping_table_prefix(outfp);
1123 print_mapping_table_suffix(outfp);
1124 print_system_event_mapping_table_prefix(outfp);
1125 print_system_event_mapping_table_suffix(outfp);
1126 fclose(outfp);
1127 }
1128
get_maxfds(void)1129 static int get_maxfds(void)
1130 {
1131 struct rlimit rlim;
1132
1133 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
1134 if (rlim.rlim_max == RLIM_INFINITY)
1135 return 512;
1136 return MIN(rlim.rlim_max / 2, 512);
1137 }
1138
1139 return 512;
1140 }
1141
1142 /*
1143 * nftw() doesn't let us pass an argument to the processing function,
1144 * so use a global variables.
1145 */
1146 static FILE *eventsfp;
1147 static char *mapfile;
1148
is_leaf_dir(const char * fpath)1149 static int is_leaf_dir(const char *fpath)
1150 {
1151 DIR *d;
1152 struct dirent *dir;
1153 int res = 1;
1154
1155 d = opendir(fpath);
1156 if (!d)
1157 return 0;
1158
1159 while ((dir = readdir(d)) != NULL) {
1160 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1161 continue;
1162
1163 if (dir->d_type == DT_DIR) {
1164 res = 0;
1165 break;
1166 } else if (dir->d_type == DT_UNKNOWN) {
1167 char path[PATH_MAX];
1168 struct stat st;
1169
1170 snprintf(path, sizeof(path), "%s/%s", fpath, dir->d_name);
1171 if (stat(path, &st))
1172 break;
1173
1174 if (S_ISDIR(st.st_mode)) {
1175 res = 0;
1176 break;
1177 }
1178 }
1179 }
1180
1181 closedir(d);
1182
1183 return res;
1184 }
1185
is_json_file(const char * name)1186 static int is_json_file(const char *name)
1187 {
1188 const char *suffix;
1189
1190 if (strlen(name) < 5)
1191 return 0;
1192
1193 suffix = name + strlen(name) - 5;
1194
1195 if (strncmp(suffix, ".json", 5) == 0)
1196 return 1;
1197 return 0;
1198 }
1199
preprocess_arch_std_files(const char * fpath,const struct stat * sb,int typeflag,struct FTW * ftwbuf)1200 static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
1201 int typeflag, struct FTW *ftwbuf)
1202 {
1203 int level = ftwbuf->level;
1204 int is_file = typeflag == FTW_F;
1205
1206 if (level == 1 && is_file && is_json_file(fpath))
1207 return json_events(fpath, save_arch_std_events, (void *)(uintptr_t)sb);
1208
1209 return 0;
1210 }
1211
process_one_file(const char * fpath,const struct stat * sb,int typeflag,struct FTW * ftwbuf)1212 static int process_one_file(const char *fpath, const struct stat *sb,
1213 int typeflag, struct FTW *ftwbuf)
1214 {
1215 char *tblname, *bname;
1216 int is_dir = typeflag == FTW_D;
1217 int is_file = typeflag == FTW_F;
1218 int level = ftwbuf->level;
1219 int err = 0;
1220
1221 if (level >= 2 && is_dir) {
1222 int count = 0;
1223 /*
1224 * For level 2 directory, bname will include parent name,
1225 * like vendor/platform. So search back from platform dir
1226 * to find this.
1227 * Something similar for level 3 directory, but we're a PMU
1228 * category folder, like vendor/platform/cpu.
1229 */
1230 bname = (char *) fpath + ftwbuf->base - 2;
1231 for (;;) {
1232 if (*bname == '/')
1233 count++;
1234 if (count == level - 1)
1235 break;
1236 bname--;
1237 }
1238 bname++;
1239 } else
1240 bname = (char *) fpath + ftwbuf->base;
1241
1242 pr_debug("%s %d %7jd %-20s %s\n",
1243 is_file ? "f" : is_dir ? "d" : "x",
1244 level, sb->st_size, bname, fpath);
1245
1246 /* base dir or too deep */
1247 if (level == 0 || level > 4)
1248 return 0;
1249
1250
1251 /* model directory, reset topic */
1252 if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
1253 (level >= 2 && is_dir && is_leaf_dir(fpath))) {
1254 if (close_table)
1255 print_events_table_suffix(eventsfp);
1256
1257 /*
1258 * Drop file name suffix. Replace hyphens with underscores.
1259 * Fail if file name contains any alphanum characters besides
1260 * underscores.
1261 */
1262 tblname = file_name_to_table_name(bname);
1263 if (!tblname) {
1264 pr_info("%s: Error determining table name for %s\n", prog,
1265 bname);
1266 return -1;
1267 }
1268
1269 if (is_sys_dir(bname)) {
1270 struct sys_event_table *sys_event_table;
1271
1272 sys_event_table = malloc(sizeof(*sys_event_table));
1273 if (!sys_event_table)
1274 return -1;
1275
1276 sys_event_table->soc_id = strdup(tblname);
1277 if (!sys_event_table->soc_id) {
1278 free(sys_event_table);
1279 return -1;
1280 }
1281 list_add_tail(&sys_event_table->list,
1282 &sys_event_tables);
1283 }
1284
1285 print_events_table_prefix(eventsfp, tblname);
1286 return 0;
1287 }
1288
1289 /*
1290 * Save the mapfile name for now. We will process mapfile
1291 * after processing all JSON files (so we can write out the
1292 * mapping table after all PMU events tables).
1293 *
1294 */
1295 if (level == 1 && is_file) {
1296 if (!strcmp(bname, "mapfile.csv")) {
1297 mapfile = strdup(fpath);
1298 return 0;
1299 }
1300 if (is_json_file(bname))
1301 pr_debug("%s: ArchStd json is preprocessed %s\n", prog, fpath);
1302 else
1303 pr_info("%s: Ignoring file %s\n", prog, fpath);
1304 return 0;
1305 }
1306
1307 /*
1308 * If the file name does not have a .json extension,
1309 * ignore it. It could be a readme.txt for instance.
1310 */
1311 if (is_file) {
1312 if (!is_json_file(bname)) {
1313 pr_info("%s: Ignoring file without .json suffix %s\n", prog,
1314 fpath);
1315 return 0;
1316 }
1317 }
1318
1319 if (level > 1 && add_topic(bname))
1320 return -ENOMEM;
1321
1322 /*
1323 * Assume all other files are JSON files.
1324 *
1325 * If mapfile refers to 'power7_core.json', we create a table
1326 * named 'power7_core'. Any inconsistencies between the mapfile
1327 * and directory tree could result in build failure due to table
1328 * names not being found.
1329 *
1330 * At least for now, be strict with processing JSON file names.
1331 * i.e. if JSON file name cannot be mapped to C-style table name,
1332 * fail.
1333 */
1334 if (is_file) {
1335 struct perf_entry_data data = {
1336 .topic = get_topic(),
1337 .outfp = eventsfp,
1338 };
1339
1340 err = json_events(fpath, print_events_table_entry, &data);
1341
1342 free(data.topic);
1343 }
1344
1345 return err;
1346 }
1347
1348 #ifndef PATH_MAX
1349 #define PATH_MAX 4096
1350 #endif
1351
1352 /*
1353 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1354 * the set of JSON files for the architecture 'arch'.
1355 *
1356 * From each JSON file, create a C-style "PMU events table" from the
1357 * JSON file (see struct pmu_event).
1358 *
1359 * From the mapfile, create a mapping between the CPU revisions and
1360 * PMU event tables (see struct pmu_events_map).
1361 *
1362 * Write out the PMU events tables and the mapping table to pmu-event.c.
1363 */
main(int argc,char * argv[])1364 int main(int argc, char *argv[])
1365 {
1366 int rc, ret = 0, empty_map = 0;
1367 int maxfds;
1368 char ldirname[PATH_MAX];
1369 const char *arch;
1370 const char *output_file;
1371 const char *start_dirname;
1372 const char *err_string_ext = "";
1373 struct stat stbuf;
1374
1375 prog = basename(argv[0]);
1376 if (argc < 4) {
1377 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
1378 return 1;
1379 }
1380
1381 arch = argv[1];
1382 start_dirname = argv[2];
1383 output_file = argv[3];
1384
1385 if (argc > 4)
1386 verbose = atoi(argv[4]);
1387
1388 eventsfp = fopen(output_file, "w");
1389 if (!eventsfp) {
1390 pr_err("%s Unable to create required file %s (%s)\n",
1391 prog, output_file, strerror(errno));
1392 return 2;
1393 }
1394
1395 snprintf(ldirname, sizeof(ldirname), "%s/%s", start_dirname, arch);
1396
1397 /* If architecture does not have any event lists, bail out */
1398 if (stat(ldirname, &stbuf) < 0) {
1399 pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
1400 empty_map = 1;
1401 goto err_close_eventsfp;
1402 }
1403
1404 /* Include pmu-events.h first */
1405 fprintf(eventsfp, "#include \"pmu-events/pmu-events.h\"\n");
1406
1407 /*
1408 * The mapfile allows multiple CPUids to point to the same JSON file,
1409 * so, not sure if there is a need for symlinks within the pmu-events
1410 * directory.
1411 *
1412 * For now, treat symlinks of JSON files as regular files and create
1413 * separate tables for each symlink (presumably, each symlink refers
1414 * to specific version of the CPU).
1415 */
1416
1417 maxfds = get_maxfds();
1418 rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1419 if (rc)
1420 goto err_processing_std_arch_event_dir;
1421
1422 rc = nftw(ldirname, process_one_file, maxfds, 0);
1423 if (rc)
1424 goto err_processing_dir;
1425
1426 sprintf(ldirname, "%s/test", start_dirname);
1427
1428 rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1429 if (rc)
1430 goto err_processing_std_arch_event_dir;
1431
1432 rc = nftw(ldirname, process_one_file, maxfds, 0);
1433 if (rc)
1434 goto err_processing_dir;
1435
1436 if (close_table)
1437 print_events_table_suffix(eventsfp);
1438
1439 if (!mapfile) {
1440 pr_info("%s: No CPU->JSON mapping?\n", prog);
1441 empty_map = 1;
1442 goto err_close_eventsfp;
1443 }
1444
1445 rc = process_mapfile(eventsfp, mapfile);
1446 if (rc) {
1447 pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
1448 /* Make build fail */
1449 ret = 1;
1450 goto err_close_eventsfp;
1451 }
1452
1453 rc = process_system_event_tables(eventsfp);
1454 fclose(eventsfp);
1455 if (rc) {
1456 ret = 1;
1457 goto err_out;
1458 }
1459
1460 free_arch_std_events();
1461 free_sys_event_tables();
1462 free(mapfile);
1463 return 0;
1464
1465 err_processing_std_arch_event_dir:
1466 err_string_ext = " for std arch event";
1467 err_processing_dir:
1468 if (verbose) {
1469 pr_info("%s: Error walking file tree %s%s\n", prog, ldirname,
1470 err_string_ext);
1471 empty_map = 1;
1472 } else if (rc < 0) {
1473 ret = 1;
1474 } else {
1475 empty_map = 1;
1476 }
1477 err_close_eventsfp:
1478 fclose(eventsfp);
1479 if (empty_map)
1480 create_empty_mapping(output_file);
1481 err_out:
1482 free_arch_std_events();
1483 free_sys_event_tables();
1484 free(mapfile);
1485 return ret;
1486 }
1487
1488 #include <fts.h>
1489
1490 static int
1491 #if defined(__linux__) || defined(__APPLE__)
fts_compare(const FTSENT ** a,const FTSENT ** b)1492 fts_compare(const FTSENT **a, const FTSENT **b)
1493 #else
1494 fts_compare(const FTSENT * const *a, const FTSENT * const *b)
1495 #endif
1496 {
1497 return (strcmp((*a)->fts_name, (*b)->fts_name));
1498 }
1499
1500 static int
nftw_ordered(const char * path,int (* fn)(const char *,const struct stat *,int,struct FTW *),int nfds,int ftwflags)1501 nftw_ordered(const char *path, int (*fn)(const char *, const struct stat *, int,
1502 struct FTW *), int nfds, int ftwflags)
1503 {
1504 char * const paths[2] = { (char *)path, NULL };
1505 struct FTW ftw;
1506 FTSENT *cur;
1507 FTS *ftsp;
1508 int error = 0, ftsflags, fnflag, postorder, sverrno;
1509
1510 /* XXX - nfds is currently unused */
1511 if (nfds < 1) {
1512 errno = EINVAL;
1513 return (-1);
1514 }
1515
1516 ftsflags = FTS_COMFOLLOW;
1517 if (!(ftwflags & FTW_CHDIR))
1518 ftsflags |= FTS_NOCHDIR;
1519 if (ftwflags & FTW_MOUNT)
1520 ftsflags |= FTS_XDEV;
1521 if (ftwflags & FTW_PHYS)
1522 ftsflags |= FTS_PHYSICAL;
1523 else
1524 ftsflags |= FTS_LOGICAL;
1525 postorder = (ftwflags & FTW_DEPTH) != 0;
1526 ftsp = fts_open(paths, ftsflags, fts_compare);
1527 if (ftsp == NULL)
1528 return (-1);
1529 while ((cur = fts_read(ftsp)) != NULL) {
1530 switch (cur->fts_info) {
1531 case FTS_D:
1532 if (postorder)
1533 continue;
1534 fnflag = FTW_D;
1535 break;
1536 case FTS_DC:
1537 continue;
1538 case FTS_DNR:
1539 fnflag = FTW_DNR;
1540 break;
1541 case FTS_DP:
1542 if (!postorder)
1543 continue;
1544 fnflag = FTW_DP;
1545 break;
1546 case FTS_F:
1547 case FTS_DEFAULT:
1548 fnflag = FTW_F;
1549 break;
1550 case FTS_NS:
1551 case FTS_NSOK:
1552 fnflag = FTW_NS;
1553 break;
1554 case FTS_SL:
1555 fnflag = FTW_SL;
1556 break;
1557 case FTS_SLNONE:
1558 fnflag = FTW_SLN;
1559 break;
1560 default:
1561 error = -1;
1562 goto done;
1563 }
1564 ftw.base = cur->fts_pathlen - cur->fts_namelen;
1565 ftw.level = cur->fts_level;
1566 error = fn(cur->fts_path, cur->fts_statp, fnflag, &ftw);
1567 if (error != 0)
1568 break;
1569 }
1570 done:
1571 sverrno = errno;
1572 if (fts_close(ftsp) != 0 && error == 0)
1573 error = -1;
1574 else
1575 errno = sverrno;
1576 return (error);
1577 }
1578