xref: /freebsd/lib/libpmc/pmu-events/jevents.c (revision d38b3a5ead0d3507080da5321144219b13d43aa9)
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 				/*
627 				 * We use the AMD PPR Family 1Ah Model 70h
628 				 * naming scheme of allsources rather than
629 				 * slices.  The symbol EnAllSlices is not used
630 				 * anywhere except in Zen 4+ for the L3
631 				 * counters.
632 				 */
633 				addfield(map, &allslices, "", "allsources=", val);
634 			} else if (json_streq(map, field, "SliceId")) {
635 				/*
636 				 * We use sourceid because there's a
637 				 * descripency where the JSON from linux calls
638 				 * it a SliceId, which is not the name used by
639 				 * AMD in the PPRs.  The field name from Family
640 				 * 19h and below that calls it slicemask see
641 				 * the references in hwpmc_amd.h.
642 				 */
643 				addfield(map, &sliceid, "", "sourceid=", val);
644 			} else if (json_streq(map, field, "ThreadMask")) {
645 				if (nz)
646 					addfield(map, &threadmask, "", "l3_thread_mask=", val);
647 			} else if (json_streq(map, field, "CounterMask")) {
648 				if (nz)
649 					addfield(map, &cmask, "", "cmask=", val);
650 			} else if (json_streq(map, field, "RdWrMask")) {
651 				/* AMD UMC */
652 			} else if (json_streq(map, field, "Invert")) {
653 				if (nz)
654 					addfield(map, &inv, "", "inv=", val);
655 			} else if (json_streq(map, field, "AnyThread")) {
656 				if (nz)
657 					addfield(map, &any, "", "any=", val);
658 			} else if (json_streq(map, field, "EdgeDetect")) {
659 				if (nz)
660 					addfield(map, &edge, "", "edge=", val);
661 			} else if (json_streq(map, field, "SampleAfterValue")) {
662 				if (nz)
663 					addfield(map, &period, "", "period=", val);
664 			} else if (json_streq(map, field, "FCMask") && nz) {
665 				addfield(map, &fc_mask, "", "fc_mask=", val);
666 			} else if (json_streq(map, field, "PortMask") && nz) {
667 				addfield(map, &ch_mask, "", "ch_mask=", val);
668 			} else if (json_streq(map, field, "EventCode")) {
669 				char *code = NULL;
670 				addfield(map, &code, "", "", val);
671 				eventcode |= strtoul(code, NULL, 0);
672 				free(code);
673 			} else if (json_streq(map, field, "ConfigCode")) {
674 				char *code = NULL;
675 				addfield(map, &code, "", "", val);
676 				configcode |= strtoul(code, NULL, 0);
677 				free(code);
678 				configcode_present = true;
679 			} else if (json_streq(map, field, "ExtSel")) {
680 				char *code = NULL;
681 				addfield(map, &code, "", "", val);
682 				eventcode |= strtoul(code, NULL, 0) << 8;
683 				free(code);
684 			} else if (json_streq(map, field, "EventName")) {
685 				addfield(map, &je.name, "", "", val);
686 			} else if (json_streq(map, field, "Compat")) {
687 				addfield(map, &je.compat, "", "", val);
688 			} else if (json_streq(map, field, "BriefDescription")) {
689 				addfield(map, &je.desc, "", "", val);
690 				fixdesc(je.desc);
691 			} else if (json_streq(map, field,
692 					     "PublicDescription")) {
693 				addfield(map, &je.long_desc, "", "", val);
694 				fixdesc(je.long_desc);
695 			} else if (json_streq(map, field, "PEBS") && nz) {
696 				precise = val;
697 			} else if (json_streq(map, field, "MSRIndex") && nz) {
698 				msr = lookup_msr(map, val);
699 			} else if (json_streq(map, field, "MSRValue")) {
700 				msrval = val;
701 			} else if (json_streq(map, field, "Errata") &&
702 				   !json_streq(map, val, "null")) {
703 				addfield(map, &extra_desc, ". ",
704 					" Spec update: ", val);
705 			} else if (json_streq(map, field, "Data_LA") && nz) {
706 				addfield(map, &extra_desc, ". ",
707 					" Supports address when precise",
708 					NULL);
709 			} else if (json_streq(map, field, "Unit")) {
710 				const char *ppmu;
711 
712 				ppmu = field_to_perf(unit_to_pmu, map, val);
713 				if (ppmu) {
714 					je.pmu = strdup(ppmu);
715 				} else {
716 					if (!je.pmu)
717 						je.pmu = strdup("uncore_");
718 					addfield(map, &je.pmu, "", "", val);
719 					for (s = je.pmu; *s; s++)
720 						*s = tolower(*s);
721 				}
722 			} else if (json_streq(map, field, "Filter")) {
723 				addfield(map, &filter, "", "", val);
724 			} else if (json_streq(map, field, "ScaleUnit")) {
725 				addfield(map, &je.unit, "", "", val);
726 			} else if (json_streq(map, field, "PerPkg")) {
727 				addfield(map, &je.perpkg, "", "", val);
728 			} else if (json_streq(map, field, "AggregationMode")) {
729 				addfield(map, &je.aggr_mode, "", "", val);
730 			} else if (json_streq(map, field, "Deprecated")) {
731 				addfield(map, &je.deprecated, "", "", val);
732 			} else if (json_streq(map, field, "MetricName")) {
733 				addfield(map, &je.metric_name, "", "", val);
734 			} else if (json_streq(map, field, "MetricGroup")) {
735 				addfield(map, &je.metric_group, "", "", val);
736 			} else if (json_streq(map, field, "MetricgroupNoGroup")) {
737 				addfield(map, &je.metric_group_nogroup, "", "", val);
738 			} else if (json_streq(map, field, "DefaultMetricgroupName")) {
739 				addfield(map, &je.default_metric_group, "", "", val);
740 			} else if (json_streq(map, field, "MetricConstraint")) {
741 				addfield(map, &je.metric_constraint, "", "", val);
742 			} else if (json_streq(map, field, "MetricExpr")) {
743 				addfield(map, &je.metric_expr, "", "", val);
744 			} else if (json_streq(map, field, "MetricThreshold")) {
745 				addfield(map, &je.metric_threshold, "", "", val);
746 			} else if (json_streq(map, field, "ArchStdEvent")) {
747 				addfield(map, &arch_std, "", "", val);
748 				for (s = arch_std; *s; s++)
749 					*s = tolower(*s);
750 			} else if (json_streq(map, field, "Offcore")) {
751 				/* Check the relevant MSR has been set */
752 			} else if (json_streq(map, field, "CounterType")) {
753 				/* Unsupported Intel Offcore counters */
754 			} else if (json_streq(map, field, "UMaskExt")) {
755 				/* Unsupported Intel Offcore counters */
756 			} else if (json_streq(map, field, "PDIR_COUNTER")) {
757 				/* Intel PEBS not supported */
758 			} else if (json_streq(map, field, "CollectPEBSRecord")) {
759 				/* Intel PEBS not supported */
760 			} else if (json_streq(map, field, "PEBScounters")) {
761 				/* Intel PEBS not supported */
762 			} else if (json_streq(map, field, "Counter")) {
763 				/* Intel PEBS not supported */
764 			} else if (json_streq(map, field, "CounterHTOff")) {
765 				/* Intel PEBS not supported */
766 			} else if (json_streq(map, field, "PRECISE_STORE")) {
767 				/* Intel PEBS not supported */
768 			} else if (json_streq(map, field, "L1_Hit_Indication")) {
769 				/* Intel PEBS not supported */
770 			} else if (json_streq(map, field, "RetirementLatencyMin")) {
771 				/* Intel TPEBS not supported */
772 			} else if (json_streq(map, field, "RetirementLatencyMax")) {
773 				/* Intel TPEBS not supported */
774 			} else if (json_streq(map, field, "RetirementLatencyMean")) {
775 				/* Intel TPEBS not supported */
776 			} else if (json_streq(map, field, "Speculative")) {
777 				/* Intel informative */
778 			} else if (json_streq(map, field, "Experimental")) {
779 				/* Intel informative */
780 			} else if (json_streq(map, field, "ELLC")) {
781 				/* Intel informative */
782 			} else if (json_streq(map, field, "TakenAlone")) {
783 				/*
784 				 * Do not measure with other counters, usually
785 				 * this is because it uses an MSR to filter the
786 				 * event in a way that affects other counters.
787 				 */
788 				if (json_streq(map, val, "1"))
789 					addfield(map, &event, ",", "alone", NULL);
790 			} else {
791 				/*
792 				 * We shouldn't ignore unknown fields that may
793 				 * make the counter invalid!
794 				 *
795 				 * Often the JSON definitions are copied
796 				 * without checking if any fields require
797 				 * handling.
798 				 */
799 				json_copystr(map, field, buf, sizeof(buf));
800 				fprintf(stderr, "Unknown event field '%s' in %s\n", buf, fn);
801 				_Exit(1);
802 			}
803 		}
804 		if (precise && je.desc && !strstr(je.desc, "(Precise Event)")) {
805 			if (json_streq(map, precise, "2")) {
806 				addfield(map, &extra_desc, " ",
807 						"(Must be precise)", NULL);
808 				addfield(map, &event, ",", "pebs=", precise);
809 			} else {
810 				addfield(map, &extra_desc, " ",
811 						"(Precise event)", NULL);
812 			}
813 		}
814 		if (configcode_present)
815 			snprintf(buf, sizeof buf, "config=%#llx", configcode);
816 		else
817 			snprintf(buf, sizeof buf, "event=%#llx", eventcode);
818 		addfield(map, &event, ",", buf, NULL);
819 		if (any)
820 			addfield(map, &event, ",", any, NULL);
821 		if (ch_mask)
822 			addfield(map, &event, ",", ch_mask, NULL);
823 		if (cmask)
824 			addfield(map, &event, ",", cmask, NULL);
825 		if (edge)
826 			addfield(map, &event, ",", edge, NULL);
827 		if (fc_mask)
828 			addfield(map, &event, ",", fc_mask, NULL);
829 		if (inv)
830 			addfield(map, &event, ",", inv, NULL);
831 		if (period)
832 			addfield(map, &event, ",", period, NULL);
833 		if (umask)
834 			addfield(map, &event, ",", umask, NULL);
835 		if (allcores)
836 			addfield(map, &event, ",", allcores, NULL);
837 		if (allslices)
838 			addfield(map, &event, ",", allslices, NULL);
839 		if (sliceid)
840 			addfield(map, &event, ",", sliceid, NULL);
841 		if (threadmask)
842 			addfield(map, &event, ",", threadmask, NULL);
843 
844 		if (je.desc && extra_desc)
845 			addfield(map, &je.desc, " ", extra_desc, NULL);
846 		if (je.long_desc && extra_desc)
847 			addfield(map, &je.long_desc, " ", extra_desc, NULL);
848 		if (je.pmu) {
849 			addfield(map, &je.desc, ". ", "Unit: ", NULL);
850 			addfield(map, &je.desc, "", je.pmu, NULL);
851 			addfield(map, &je.desc, "", " ", NULL);
852 		}
853 		if (filter)
854 			addfield(map, &event, ",", filter, NULL);
855 		if (msr != NULL)
856 			addfield(map, &event, ",", msr->pname, msrval);
857 		if (je.name)
858 			fixname(je.name);
859 
860 		if (arch_std) {
861 			/*
862 			 * An arch standard event is referenced, so try to
863 			 * fixup any unassigned values.
864 			 */
865 			err = try_fixup(fn, arch_std, &je, &event);
866 			if (err)
867 				goto free_strings;
868 		}
869 		je.event = real_event(je.name, event);
870 		err = func(data, &je);
871 free_strings:
872 		free(umask);
873 		free(allcores);
874 		free(allslices);
875 		free(sliceid);
876 		free(threadmask);
877 		free(cmask);
878 		free(inv);
879 		free(any);
880 		free(edge);
881 		free(period);
882 		free(fc_mask);
883 		free(ch_mask);
884 		free(event);
885 		free(je.desc);
886 		free(je.name);
887 		free(je.compat);
888 		free(je.long_desc);
889 		free(extra_desc);
890 		free(je.pmu);
891 		free(filter);
892 		free(je.perpkg);
893 		free(je.aggr_mode);
894 		free(je.deprecated);
895 		free(je.unit);
896 		free(je.metric_expr);
897 		free(je.metric_threshold);
898 		free(je.metric_name);
899 		free(je.metric_group);
900 		free(je.metric_group_nogroup);
901 		free(je.default_metric_group);
902 		free(je.metric_constraint);
903 		free(arch_std);
904 
905 		if (err)
906 			break;
907 		tok += j;
908 	}
909 	EXPECT(tok - tokens == len, tok, "unexpected objects at end");
910 	err = 0;
911 out_free:
912 	free_json(map, size, tokens);
913 	return err;
914 }
915 
file_name_to_table_name(char * fname)916 static char *file_name_to_table_name(char *fname)
917 {
918 	unsigned int i;
919 	int n;
920 	int c;
921 	char *tblname;
922 
923 	/*
924 	 * Ensure tablename starts with alphabetic character.
925 	 * Derive rest of table name from basename of the JSON file,
926 	 * replacing hyphens and stripping out .json suffix.
927 	 */
928 	n = asprintf(&tblname, "pme_%s", fname);
929 	if (n < 0) {
930 		pr_info("%s: asprintf() error %s for file %s\n", prog,
931 				strerror(errno), fname);
932 		return NULL;
933 	}
934 
935 	for (i = 0; i < strlen(tblname); i++) {
936 		c = tblname[i];
937 
938 		if (c == '-' || c == '/')
939 			tblname[i] = '_';
940 		else if (c == '.') {
941 			tblname[i] = '\0';
942 			break;
943 		} else if (!isalnum(c) && c != '_') {
944 			pr_err("%s: Invalid character '%c' in file name '%s'\n",
945 					prog, c, fname);
946 			free(tblname);
947 			tblname = NULL;
948 			break;
949 		}
950 	}
951 
952 	return tblname;
953 }
954 
is_sys_dir(char * fname)955 static bool is_sys_dir(char *fname)
956 {
957 	size_t len = strlen(fname), len2 = strlen("/sys");
958 
959 	if (len2 > len)
960 		return false;
961 	return !strcmp(fname+len-len2, "/sys");
962 }
963 
print_mapping_table_prefix(FILE * outfp)964 static void print_mapping_table_prefix(FILE *outfp)
965 {
966 	fprintf(outfp, "const struct pmu_events_map pmu_events_map[] = {\n");
967 }
968 
print_mapping_table_suffix(FILE * outfp)969 static void print_mapping_table_suffix(FILE *outfp)
970 {
971 	/*
972 	 * Print the terminating, NULL entry.
973 	 */
974 	fprintf(outfp, "{\n");
975 	fprintf(outfp, "\t.cpuid = 0,\n");
976 	fprintf(outfp, "\t.version = 0,\n");
977 	fprintf(outfp, "\t.type = 0,\n");
978 	fprintf(outfp, "\t.table = 0,\n");
979 	fprintf(outfp, "},\n");
980 
981 	/* and finally, the closing curly bracket for the struct */
982 	fprintf(outfp, "};\n");
983 }
984 
print_mapping_test_table(FILE * outfp)985 static void print_mapping_test_table(FILE *outfp)
986 {
987 	/*
988 	 * Print the terminating, NULL entry.
989 	 */
990 	fprintf(outfp, "{\n");
991 	fprintf(outfp, "\t.cpuid = \"testcpu\",\n");
992 	fprintf(outfp, "\t.version = \"v1\",\n");
993 	fprintf(outfp, "\t.type = \"core\",\n");
994 	fprintf(outfp, "\t.table = pme_test_soc_cpu,\n");
995 	fprintf(outfp, "},\n");
996 }
997 
print_system_event_mapping_table_prefix(FILE * outfp)998 static void print_system_event_mapping_table_prefix(FILE *outfp)
999 {
1000 	fprintf(outfp, "\nconst struct pmu_sys_events pmu_sys_event_tables[] = {");
1001 }
1002 
print_system_event_mapping_table_suffix(FILE * outfp)1003 static void print_system_event_mapping_table_suffix(FILE *outfp)
1004 {
1005 	fprintf(outfp, "\n\t{\n\t\t.table = 0\n\t},");
1006 	fprintf(outfp, "\n};\n");
1007 }
1008 
process_system_event_tables(FILE * outfp)1009 static int process_system_event_tables(FILE *outfp)
1010 {
1011 	struct sys_event_table *sys_event_table;
1012 
1013 	print_system_event_mapping_table_prefix(outfp);
1014 
1015 	list_for_each_entry(sys_event_table, &sys_event_tables, list) {
1016 		fprintf(outfp, "\n\t{\n\t\t.table = %s,\n\t\t.name = \"%s\",\n\t},",
1017 			sys_event_table->soc_id,
1018 			sys_event_table->soc_id);
1019 	}
1020 
1021 	print_system_event_mapping_table_suffix(outfp);
1022 
1023 	return 0;
1024 }
1025 
process_mapfile(FILE * outfp,char * fpath)1026 static int process_mapfile(FILE *outfp, char *fpath)
1027 {
1028 	int n = 16384;
1029 	FILE *mapfp;
1030 	char *save = NULL;
1031 	char *line, *p;
1032 	int line_num;
1033 	char *tblname;
1034 	int ret = 0;
1035 
1036 	pr_info("%s: Processing mapfile %s\n", prog, fpath);
1037 
1038 	line = malloc(n);
1039 	if (!line)
1040 		return -1;
1041 
1042 	mapfp = fopen(fpath, "r");
1043 	if (!mapfp) {
1044 		pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
1045 				fpath);
1046 		free(line);
1047 		return -1;
1048 	}
1049 
1050 	print_mapping_table_prefix(outfp);
1051 
1052 	/* Skip first line (header) */
1053 	p = fgets(line, n, mapfp);
1054 	if (!p)
1055 		goto out;
1056 
1057 	line_num = 1;
1058 	while (1) {
1059 		char *cpuid, *version, *type, *fname;
1060 
1061 		line_num++;
1062 		p = fgets(line, n, mapfp);
1063 		if (!p)
1064 			break;
1065 
1066 		if (line[0] == '#' || line[0] == '\n')
1067 			continue;
1068 
1069 		if (line[strlen(line)-1] != '\n') {
1070 			/* TODO Deal with lines longer than 16K */
1071 			pr_info("%s: Mapfile %s: line %d too long, aborting\n",
1072 					prog, fpath, line_num);
1073 			ret = -1;
1074 			goto out;
1075 		}
1076 		line[strlen(line)-1] = '\0';
1077 
1078 		cpuid = fixregex(strtok_r(p, ",", &save));
1079 		version = strtok_r(NULL, ",", &save);
1080 		fname = strtok_r(NULL, ",", &save);
1081 		type = strtok_r(NULL, ",", &save);
1082 
1083 		tblname = file_name_to_table_name(fname);
1084 		fprintf(outfp, "{\n");
1085 		fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
1086 		fprintf(outfp, "\t.version = \"%s\",\n", version);
1087 		fprintf(outfp, "\t.type = \"%s\",\n", type);
1088 
1089 		/*
1090 		 * CHECK: We can't use the type (eg "core") field in the
1091 		 * table name. For us to do that, we need to somehow tweak
1092 		 * the other caller of file_name_to_table(), process_json()
1093 		 * to determine the type. process_json() file has no way
1094 		 * of knowing these are "core" events unless file name has
1095 		 * core in it. If filename has core in it, we can safely
1096 		 * ignore the type field here also.
1097 		 */
1098 		fprintf(outfp, "\t.table = %s\n", tblname);
1099 		fprintf(outfp, "},\n");
1100 	}
1101 
1102 out:
1103 	print_mapping_test_table(outfp);
1104 	print_mapping_table_suffix(outfp);
1105 	fclose(mapfp);
1106 	free(line);
1107 	return ret;
1108 }
1109 
1110 /*
1111  * If we fail to locate/process JSON and map files, create a NULL mapping
1112  * table. This would at least allow perf to build even if we can't find/use
1113  * the aliases.
1114  */
create_empty_mapping(const char * output_file)1115 static void create_empty_mapping(const char *output_file)
1116 {
1117 	FILE *outfp;
1118 
1119 	pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
1120 
1121 	/* Truncate file to clear any partial writes to it */
1122 	outfp = fopen(output_file, "w");
1123 	if (!outfp) {
1124 		perror("fopen()");
1125 		_Exit(1);
1126 	}
1127 
1128 	fprintf(outfp, "#include \"pmu-events/pmu-events.h\"\n");
1129 	print_mapping_table_prefix(outfp);
1130 	print_mapping_table_suffix(outfp);
1131 	print_system_event_mapping_table_prefix(outfp);
1132 	print_system_event_mapping_table_suffix(outfp);
1133 	fclose(outfp);
1134 }
1135 
get_maxfds(void)1136 static int get_maxfds(void)
1137 {
1138 	struct rlimit rlim;
1139 
1140 	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
1141 		if (rlim.rlim_max == RLIM_INFINITY)
1142 			return 512;
1143 		return MIN(rlim.rlim_max / 2, 512);
1144 	}
1145 
1146 	return 512;
1147 }
1148 
1149 /*
1150  * nftw() doesn't let us pass an argument to the processing function,
1151  * so use a global variables.
1152  */
1153 static FILE *eventsfp;
1154 static char *mapfile;
1155 
is_leaf_dir(const char * fpath)1156 static int is_leaf_dir(const char *fpath)
1157 {
1158 	DIR *d;
1159 	struct dirent *dir;
1160 	int res = 1;
1161 
1162 	d = opendir(fpath);
1163 	if (!d)
1164 		return 0;
1165 
1166 	while ((dir = readdir(d)) != NULL) {
1167 		if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1168 			continue;
1169 
1170 		if (dir->d_type == DT_DIR) {
1171 			res = 0;
1172 			break;
1173 		} else if (dir->d_type == DT_UNKNOWN) {
1174 			char path[PATH_MAX];
1175 			struct stat st;
1176 
1177 			snprintf(path, sizeof(path), "%s/%s", fpath, dir->d_name);
1178 			if (stat(path, &st))
1179 				break;
1180 
1181 			if (S_ISDIR(st.st_mode)) {
1182 				res = 0;
1183 				break;
1184 			}
1185 		}
1186 	}
1187 
1188 	closedir(d);
1189 
1190 	return res;
1191 }
1192 
is_json_file(const char * name)1193 static int is_json_file(const char *name)
1194 {
1195 	const char *suffix;
1196 
1197 	if (strlen(name) < 5)
1198 		return 0;
1199 
1200 	suffix = name + strlen(name) - 5;
1201 
1202 	if (strncmp(suffix, ".json", 5) == 0)
1203 		return 1;
1204 	return 0;
1205 }
1206 
preprocess_arch_std_files(const char * fpath,const struct stat * sb,int typeflag,struct FTW * ftwbuf)1207 static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
1208 				int typeflag, struct FTW *ftwbuf)
1209 {
1210 	int level = ftwbuf->level;
1211 	int is_file = typeflag == FTW_F;
1212 
1213 	if (level == 1 && is_file && is_json_file(fpath))
1214 		return json_events(fpath, save_arch_std_events, (void *)(uintptr_t)sb);
1215 
1216 	return 0;
1217 }
1218 
process_one_file(const char * fpath,const struct stat * sb,int typeflag,struct FTW * ftwbuf)1219 static int process_one_file(const char *fpath, const struct stat *sb,
1220 			    int typeflag, struct FTW *ftwbuf)
1221 {
1222 	char *tblname, *bname;
1223 	int is_dir  = typeflag == FTW_D;
1224 	int is_file = typeflag == FTW_F;
1225 	int level   = ftwbuf->level;
1226 	int err = 0;
1227 
1228 	if (level >= 2 && is_dir) {
1229 		int count = 0;
1230 		/*
1231 		 * For level 2 directory, bname will include parent name,
1232 		 * like vendor/platform. So search back from platform dir
1233 		 * to find this.
1234 		 * Something similar for level 3 directory, but we're a PMU
1235 		 * category folder, like vendor/platform/cpu.
1236 		 */
1237 		bname = (char *) fpath + ftwbuf->base - 2;
1238 		for (;;) {
1239 			if (*bname == '/')
1240 				count++;
1241 			if (count == level - 1)
1242 				break;
1243 			bname--;
1244 		}
1245 		bname++;
1246 	} else
1247 		bname = (char *) fpath + ftwbuf->base;
1248 
1249 	pr_debug("%s %d %7jd %-20s %s\n",
1250 		 is_file ? "f" : is_dir ? "d" : "x",
1251 		 level, sb->st_size, bname, fpath);
1252 
1253 	/* base dir or too deep */
1254 	if (level == 0 || level > 4)
1255 		return 0;
1256 
1257 
1258 	/* model directory, reset topic */
1259 	if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
1260 	    (level >= 2 && is_dir && is_leaf_dir(fpath))) {
1261 		if (close_table)
1262 			print_events_table_suffix(eventsfp);
1263 
1264 		/*
1265 		 * Drop file name suffix. Replace hyphens with underscores.
1266 		 * Fail if file name contains any alphanum characters besides
1267 		 * underscores.
1268 		 */
1269 		tblname = file_name_to_table_name(bname);
1270 		if (!tblname) {
1271 			pr_info("%s: Error determining table name for %s\n", prog,
1272 				bname);
1273 			return -1;
1274 		}
1275 
1276 		if (is_sys_dir(bname)) {
1277 			struct sys_event_table *sys_event_table;
1278 
1279 			sys_event_table = malloc(sizeof(*sys_event_table));
1280 			if (!sys_event_table)
1281 				return -1;
1282 
1283 			sys_event_table->soc_id = strdup(tblname);
1284 			if (!sys_event_table->soc_id) {
1285 				free(sys_event_table);
1286 				return -1;
1287 			}
1288 			list_add_tail(&sys_event_table->list,
1289 				      &sys_event_tables);
1290 		}
1291 
1292 		print_events_table_prefix(eventsfp, tblname);
1293 		return 0;
1294 	}
1295 
1296 	/*
1297 	 * Save the mapfile name for now. We will process mapfile
1298 	 * after processing all JSON files (so we can write out the
1299 	 * mapping table after all PMU events tables).
1300 	 *
1301 	 */
1302 	if (level == 1 && is_file) {
1303 		if (!strcmp(bname, "mapfile.csv")) {
1304 			mapfile = strdup(fpath);
1305 			return 0;
1306 		}
1307 		if (is_json_file(bname))
1308 			pr_debug("%s: ArchStd json is preprocessed %s\n", prog, fpath);
1309 		else
1310 			pr_info("%s: Ignoring file %s\n", prog, fpath);
1311 		return 0;
1312 	}
1313 
1314 	/*
1315 	 * If the file name does not have a .json extension,
1316 	 * ignore it. It could be a readme.txt for instance.
1317 	 */
1318 	if (is_file) {
1319 		if (!is_json_file(bname)) {
1320 			pr_info("%s: Ignoring file without .json suffix %s\n", prog,
1321 				fpath);
1322 			return 0;
1323 		}
1324 	}
1325 
1326 	if (level > 1 && add_topic(bname))
1327 		return -ENOMEM;
1328 
1329 	/*
1330 	 * Assume all other files are JSON files.
1331 	 *
1332 	 * If mapfile refers to 'power7_core.json', we create a table
1333 	 * named 'power7_core'. Any inconsistencies between the mapfile
1334 	 * and directory tree could result in build failure due to table
1335 	 * names not being found.
1336 	 *
1337 	 * At least for now, be strict with processing JSON file names.
1338 	 * i.e. if JSON file name cannot be mapped to C-style table name,
1339 	 * fail.
1340 	 */
1341 	if (is_file) {
1342 		struct perf_entry_data data = {
1343 			.topic = get_topic(),
1344 			.outfp = eventsfp,
1345 		};
1346 
1347 		err = json_events(fpath, print_events_table_entry, &data);
1348 
1349 		free(data.topic);
1350 	}
1351 
1352 	return err;
1353 }
1354 
1355 #ifndef PATH_MAX
1356 #define PATH_MAX	4096
1357 #endif
1358 
1359 /*
1360  * Starting in directory 'start_dirname', find the "mapfile.csv" and
1361  * the set of JSON files for the architecture 'arch'.
1362  *
1363  * From each JSON file, create a C-style "PMU events table" from the
1364  * JSON file (see struct pmu_event).
1365  *
1366  * From the mapfile, create a mapping between the CPU revisions and
1367  * PMU event tables (see struct pmu_events_map).
1368  *
1369  * Write out the PMU events tables and the mapping table to pmu-event.c.
1370  */
main(int argc,char * argv[])1371 int main(int argc, char *argv[])
1372 {
1373 	int rc, ret = 0, empty_map = 0;
1374 	int maxfds;
1375 	char ldirname[PATH_MAX];
1376 	const char *arch;
1377 	const char *output_file;
1378 	const char *start_dirname;
1379 	const char *err_string_ext = "";
1380 	struct stat stbuf;
1381 
1382 	prog = basename(argv[0]);
1383 	if (argc < 4) {
1384 		pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
1385 		return 1;
1386 	}
1387 
1388 	arch = argv[1];
1389 	start_dirname = argv[2];
1390 	output_file = argv[3];
1391 
1392 	if (argc > 4)
1393 		verbose = atoi(argv[4]);
1394 
1395 	eventsfp = fopen(output_file, "w");
1396 	if (!eventsfp) {
1397 		pr_err("%s Unable to create required file %s (%s)\n",
1398 				prog, output_file, strerror(errno));
1399 		return 2;
1400 	}
1401 
1402 	snprintf(ldirname, sizeof(ldirname), "%s/%s", start_dirname, arch);
1403 
1404 	/* If architecture does not have any event lists, bail out */
1405 	if (stat(ldirname, &stbuf) < 0) {
1406 		pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
1407 		empty_map = 1;
1408 		goto err_close_eventsfp;
1409 	}
1410 
1411 	/* Include pmu-events.h first */
1412 	fprintf(eventsfp, "#include \"pmu-events/pmu-events.h\"\n");
1413 
1414 	/*
1415 	 * The mapfile allows multiple CPUids to point to the same JSON file,
1416 	 * so, not sure if there is a need for symlinks within the pmu-events
1417 	 * directory.
1418 	 *
1419 	 * For now, treat symlinks of JSON files as regular files and create
1420 	 * separate tables for each symlink (presumably, each symlink refers
1421 	 * to specific version of the CPU).
1422 	 */
1423 
1424 	maxfds = get_maxfds();
1425 	rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1426 	if (rc)
1427 		goto err_processing_std_arch_event_dir;
1428 
1429 	rc = nftw(ldirname, process_one_file, maxfds, 0);
1430 	if (rc)
1431 		goto err_processing_dir;
1432 
1433 	sprintf(ldirname, "%s/test", start_dirname);
1434 
1435 	rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
1436 	if (rc)
1437 		goto err_processing_std_arch_event_dir;
1438 
1439 	rc = nftw(ldirname, process_one_file, maxfds, 0);
1440 	if (rc)
1441 		goto err_processing_dir;
1442 
1443 	if (close_table)
1444 		print_events_table_suffix(eventsfp);
1445 
1446 	if (!mapfile) {
1447 		pr_info("%s: No CPU->JSON mapping?\n", prog);
1448 		empty_map = 1;
1449 		goto err_close_eventsfp;
1450 	}
1451 
1452 	rc = process_mapfile(eventsfp, mapfile);
1453 	if (rc) {
1454 		pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
1455 		/* Make build fail */
1456 		ret = 1;
1457 		goto err_close_eventsfp;
1458 	}
1459 
1460 	rc = process_system_event_tables(eventsfp);
1461 	fclose(eventsfp);
1462 	if (rc) {
1463 		ret = 1;
1464 		goto err_out;
1465 	}
1466 
1467 	free_arch_std_events();
1468 	free_sys_event_tables();
1469 	free(mapfile);
1470 	return 0;
1471 
1472 err_processing_std_arch_event_dir:
1473 	err_string_ext = " for std arch event";
1474 err_processing_dir:
1475 	if (verbose) {
1476 		pr_info("%s: Error walking file tree %s%s\n", prog, ldirname,
1477 			err_string_ext);
1478 		empty_map = 1;
1479 	} else if (rc < 0) {
1480 		ret = 1;
1481 	} else {
1482 		empty_map = 1;
1483 	}
1484 err_close_eventsfp:
1485 	fclose(eventsfp);
1486 	if (empty_map)
1487 		create_empty_mapping(output_file);
1488 err_out:
1489 	free_arch_std_events();
1490 	free_sys_event_tables();
1491 	free(mapfile);
1492 	return ret;
1493 }
1494 
1495 #include <fts.h>
1496 
1497 static int
1498 #if defined(__linux__) || defined(__APPLE__)
fts_compare(const FTSENT ** a,const FTSENT ** b)1499 fts_compare(const FTSENT **a, const FTSENT **b)
1500 #else
1501 fts_compare(const FTSENT * const *a, const FTSENT * const *b)
1502 #endif
1503 {
1504 	return (strcmp((*a)->fts_name, (*b)->fts_name));
1505 }
1506 
1507 static int
nftw_ordered(const char * path,int (* fn)(const char *,const struct stat *,int,struct FTW *),int nfds,int ftwflags)1508 nftw_ordered(const char *path, int (*fn)(const char *, const struct stat *, int,
1509      struct FTW *), int nfds, int ftwflags)
1510 {
1511 	char * const paths[2] = { (char *)path, NULL };
1512 	struct FTW ftw;
1513 	FTSENT *cur;
1514 	FTS *ftsp;
1515 	int error = 0, ftsflags, fnflag, postorder, sverrno;
1516 
1517 	/* XXX - nfds is currently unused */
1518 	if (nfds < 1) {
1519 		errno = EINVAL;
1520 		return (-1);
1521 	}
1522 
1523 	ftsflags = FTS_COMFOLLOW;
1524 	if (!(ftwflags & FTW_CHDIR))
1525 		ftsflags |= FTS_NOCHDIR;
1526 	if (ftwflags & FTW_MOUNT)
1527 		ftsflags |= FTS_XDEV;
1528 	if (ftwflags & FTW_PHYS)
1529 		ftsflags |= FTS_PHYSICAL;
1530 	else
1531 		ftsflags |= FTS_LOGICAL;
1532 	postorder = (ftwflags & FTW_DEPTH) != 0;
1533 	ftsp = fts_open(paths, ftsflags, fts_compare);
1534 	if (ftsp == NULL)
1535 		return (-1);
1536 	while ((cur = fts_read(ftsp)) != NULL) {
1537 		switch (cur->fts_info) {
1538 		case FTS_D:
1539 			if (postorder)
1540 				continue;
1541 			fnflag = FTW_D;
1542 			break;
1543 		case FTS_DC:
1544 			continue;
1545 		case FTS_DNR:
1546 			fnflag = FTW_DNR;
1547 			break;
1548 		case FTS_DP:
1549 			if (!postorder)
1550 				continue;
1551 			fnflag = FTW_DP;
1552 			break;
1553 		case FTS_F:
1554 		case FTS_DEFAULT:
1555 			fnflag = FTW_F;
1556 			break;
1557 		case FTS_NS:
1558 		case FTS_NSOK:
1559 			fnflag = FTW_NS;
1560 			break;
1561 		case FTS_SL:
1562 			fnflag = FTW_SL;
1563 			break;
1564 		case FTS_SLNONE:
1565 			fnflag = FTW_SLN;
1566 			break;
1567 		default:
1568 			error = -1;
1569 			goto done;
1570 		}
1571 		ftw.base = cur->fts_pathlen - cur->fts_namelen;
1572 		ftw.level = cur->fts_level;
1573 		error = fn(cur->fts_path, cur->fts_statp, fnflag, &ftw);
1574 		if (error != 0)
1575 			break;
1576 	}
1577 done:
1578 	sverrno = errno;
1579 	if (fts_close(ftsp) != 0 && error == 0)
1580 		error = -1;
1581 	else
1582 		errno = sverrno;
1583 	return (error);
1584 }
1585