1 /* Parse event JSON files */
4 * Copyright (c) 2014, Intel Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
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.
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.
34 #include <sys/param.h>
35 #include <sys/resource.h> /* getrlimit */
37 #include <sys/time.h> /* getrlimit */
60 nftw_ordered(const char *path, int (*fn)(const char *, const struct stat *, int,
61 struct FTW *), int nfds, int ftwflags);
67 eprintf(int level, int var, const char *fmt, ...)
78 ret = vfprintf(stderr, fmt, args);
85 __attribute__((weak)) char *
92 addfield(char *map, char **dst, const char *sep, const char *a, jsmntok_t *bt)
94 unsigned int len = strlen(a) + 1 + strlen(sep);
95 int olen = *dst ? strlen(*dst) : 0;
96 int blen = bt ? json_len(bt) : 0;
99 out = realloc(*dst, len + olen + blen);
101 /* Don't add field in this case */
112 strncat(*dst, map + bt->start, blen);
125 char *e = s + strlen(s);
127 /* Remove trailing dots that look ugly in perf list */
129 while (e >= s && isspace(*e))
131 if (e >= s && *e == '.')
135 /* Add escapes for '\' so they are proper C strings. */
144 /* Count the number of '\' in string */
145 for (p = s; *p; p++) {
154 /* allocate space for a new string */
155 fixed = (char *) malloc(len + 1);
159 /* copy over the characters */
161 for (p = s; *p; p++) {
173 static struct msrmap {
177 { "0x3F6", "ldlat=" },
178 { "0x1A6", "offcore_rsp=" },
179 { "0x1A7", "offcore_rsp=" },
180 { "0x3F7", "frontend=" },
184 static struct field {
188 { "UMask", "umask=" },
189 { "CounterMask", "cmask=" },
190 { "Invert", "inv=" },
191 { "AnyThread", "any=" },
192 { "EdgeDetect", "edge=" },
193 { "SampleAfterValue", "period=" },
194 { "FCMask", "fc_mask=" },
195 { "PortMask", "ch_mask=" },
196 { "L3ThreadMask", "l3_thread_mask=" },
197 { "L3SliceMask", "l3_slice_mask=" },
202 cut_comma(char *map, jsmntok_t *newval)
206 /* Cut off everything after comma */
207 for (i = newval->start; i < newval->end; i++) {
214 match_field(char *map, jsmntok_t *field, int nz, char **event, jsmntok_t *val)
217 jsmntok_t newval = *val;
219 for (f = fields; f->field; f++)
220 if (json_streq(map, field, f->field) && nz) {
221 cut_comma(map, &newval);
222 addfield(map, event, ",", f->kernel, &newval);
228 static struct msrmap *
229 lookup_msr(char *map, jsmntok_t *val)
231 jsmntok_t newval = *val;
235 cut_comma(map, &newval);
236 for (i = 0; msrmap[i].num; i++)
237 if (json_streq(map, &newval, msrmap[i].num))
241 pr_err("%s: Unknown MSR in event file %.*s\n", prog,
242 json_len(val), map + val->start);
251 { "CBO", "uncore_cbox" },
252 { "QPI LL", "uncore_qpi" },
253 { "SBO", "uncore_sbox" },
254 { "iMPH-U", "uncore_arb" },
259 field_to_perf(struct map *table, char *map, jsmntok_t *val)
263 for (i = 0; table[i].json; i++) {
264 if (json_streq(map, val, table[i].json))
265 return table[i].perf;
270 #define EXPECT(e, t, m) do { if (!(e)) { \
271 jsmntok_t *loc = (t); \
272 if (!(t)->start && (t) > tokens) \
274 pr_err("%s:%d: " m ", got %s\n", fn, \
275 json_line(map, loc), \
289 /* tp is free'd in process_one_file() */
290 i = asprintf(&tp, "%s", topic);
292 pr_info("%s: asprintf() error %s\n", prog);
296 for (i = 0; i < (int) strlen(tp); i++) {
311 add_topic(const char *bname)
314 topic = strdup(bname);
316 pr_info("%s: strdup() error %s for file %s\n", prog,
317 strerror(errno), bname);
323 struct perf_entry_data {
328 static int close_table;
331 print_events_table_prefix(FILE *fp, const char *tblname)
333 fprintf(fp, "static struct pmu_event %s[] = {\n", tblname);
338 print_events_table_entry(void *data, char *name, const char *event, char *desc,
339 char *long_desc, char *pmu, char *unit, char *perpkg, char *metric_expr,
340 char *metric_name, char *metric_group)
342 struct perf_entry_data *pd = data;
343 FILE *outfp = pd->outfp;
344 char *etopic = pd->topic;
347 * TODO: Remove formatting chars after debugging to reduce
350 fprintf(outfp, "{\n");
353 fprintf(outfp, "\t.name = \"%s\",\n", name);
355 fprintf(outfp, "\t.event = \"%s\",\n", event);
356 fprintf(outfp, "\t.desc = \"%s\",\n", desc);
357 fprintf(outfp, "\t.topic = \"%s\",\n", etopic);
358 if (long_desc && long_desc[0])
359 fprintf(outfp, "\t.long_desc = \"%s\",\n", long_desc);
361 fprintf(outfp, "\t.pmu = \"%s\",\n", pmu);
363 fprintf(outfp, "\t.unit = \"%s\",\n", unit);
365 fprintf(outfp, "\t.perpkg = \"%s\",\n", perpkg);
367 fprintf(outfp, "\t.metric_expr = \"%s\",\n", metric_expr);
369 fprintf(outfp, "\t.metric_name = \"%s\",\n", metric_name);
371 fprintf(outfp, "\t.metric_group = \"%s\",\n", metric_group);
372 fprintf(outfp, "},\n");
377 struct event_struct {
378 struct list_head list;
391 #define ADD_EVENT_FIELD(field) do { if (field) { \
392 es->field = strdup(field); \
397 #define FREE_EVENT_FIELD(field) free(es->field)
399 #define TRY_FIXUP_FIELD(field) do { if (es->field && !*field) {\
400 *field = strdup(es->field); \
405 #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
418 static LIST_HEAD(arch_std_events);
421 free_arch_std_events(void)
423 struct event_struct *es, *next;
425 list_for_each_entry_safe(es, next, &arch_std_events, list) {
426 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
433 save_arch_std_events(void *data __unused, char *name, const char *event,
434 char *desc, char *long_desc, char *pmu, char *unit, char *perpkg,
435 char *metric_expr, char *metric_name, char *metric_group)
437 struct event_struct *es;
439 es = malloc(sizeof(*es));
442 memset(es, 0, sizeof(*es));
443 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD);
444 list_add_tail(&es->list, &arch_std_events);
447 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
453 print_events_table_suffix(FILE *outfp)
455 fprintf(outfp, "{\n");
457 fprintf(outfp, "\t.name = 0,\n");
458 fprintf(outfp, "\t.event = 0,\n");
459 fprintf(outfp, "\t.desc = 0,\n");
461 fprintf(outfp, "},\n");
462 fprintf(outfp, "};\n");
466 static struct fixed {
470 { "inst_retired.any", "event=0xc0" },
471 { "inst_retired.any_p", "event=0xc0" },
472 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03" },
473 { "cpu_clk_unhalted.thread", "event=0x3c" },
474 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1" },
479 * Handle different fixed counter encodings between JSON and perf.
482 real_event(const char *name, char *event)
489 for (i = 0; fixed[i].name; i++)
490 if (!strcasecmp(name, fixed[i].name))
491 return fixed[i].event;
496 try_fixup(const char *fn, char *arch_std, char **event, char **desc,
497 char **name, char **long_desc, char **pmu, char **filter __unused,
498 char **perpkg, char **unit, char **metric_expr, char **metric_name,
499 char **metric_group, unsigned long long eventcode)
501 /* try to find matching event from arch standard values */
502 struct event_struct *es;
504 list_for_each_entry(es, &arch_std_events, list) {
505 if (!strcmp(arch_std, es->name)) {
506 if (!eventcode && es->event) {
507 /* allow EventCode to be overridden */
511 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
516 pr_err("%s: could not find matching %s for %s\n",
521 /* Call func with each event in the json file */
523 json_events(const char *fn,
524 int (*func)(void *data, char *name, const char *event, char *desc,
526 char *pmu, char *unit, char *perpkg,
528 char *metric_name, char *metric_group),
533 jsmntok_t *tokens, *tok;
541 tokens = parse_json(fn, &map, &size, &len);
544 EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
546 for (i = 0; i < tokens->size; i++) {
547 char *event = NULL, *desc = NULL, *name = NULL;
548 char *long_desc = NULL;
549 char *extra_desc = NULL;
554 char *metric_expr = NULL;
555 char *metric_name = NULL;
556 char *metric_group = NULL;
557 char *arch_std = NULL;
558 unsigned long long eventcode = 0;
559 struct msrmap *msr = NULL;
560 jsmntok_t *msrval = NULL;
561 jsmntok_t *precise = NULL;
562 jsmntok_t *obj = tok++;
564 EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
565 for (j = 0; j < obj->size; j += 2) {
566 jsmntok_t *field, *val;
571 EXPECT(field->type == JSMN_STRING, tok + j,
572 "Expected field name");
574 EXPECT(val->type == JSMN_STRING, tok + j + 1,
575 "Expected string value");
577 nz = !json_streq(map, val, "0");
578 if (match_field(map, field, nz, &event, val)) {
580 } else if (json_streq(map, field, "EventCode")) {
582 addfield(map, &code, "", "", val);
583 eventcode |= strtoul(code, NULL, 0);
585 } else if (json_streq(map, field, "ExtSel")) {
587 addfield(map, &code, "", "", val);
588 eventcode |= strtoul(code, NULL, 0) << 21;
590 } else if (json_streq(map, field, "EventName")) {
591 addfield(map, &name, "", "", val);
592 } else if (json_streq(map, field, "BriefDescription")) {
593 addfield(map, &desc, "", "", val);
595 } else if (json_streq(map, field,
596 "PublicDescription")) {
597 addfield(map, &long_desc, "", "", val);
599 } else if (json_streq(map, field, "PEBS") && nz) {
601 } else if (json_streq(map, field, "MSRIndex") && nz) {
602 msr = lookup_msr(map, val);
603 } else if (json_streq(map, field, "MSRValue")) {
605 } else if (json_streq(map, field, "Errata") &&
606 !json_streq(map, val, "null")) {
607 addfield(map, &extra_desc, ". ",
608 " Spec update: ", val);
609 } else if (json_streq(map, field, "Data_LA") && nz) {
610 addfield(map, &extra_desc, ". ",
611 " Supports address when precise",
613 } else if (json_streq(map, field, "Unit")) {
616 ppmu = field_to_perf(unit_to_pmu, map, val);
621 pmu = strdup("uncore_");
622 addfield(map, &pmu, "", "", val);
623 for (s = pmu; *s; s++)
626 addfield(map, &desc, ". ", "Unit: ", NULL);
627 addfield(map, &desc, "", pmu, NULL);
628 addfield(map, &desc, "", " ", NULL);
629 } else if (json_streq(map, field, "Filter")) {
630 addfield(map, &filter, "", "", val);
631 } else if (json_streq(map, field, "ScaleUnit")) {
632 addfield(map, &unit, "", "", val);
633 } else if (json_streq(map, field, "PerPkg")) {
634 addfield(map, &perpkg, "", "", val);
635 } else if (json_streq(map, field, "MetricName")) {
636 addfield(map, &metric_name, "", "", val);
637 } else if (json_streq(map, field, "MetricGroup")) {
638 addfield(map, &metric_group, "", "", val);
639 } else if (json_streq(map, field, "MetricExpr")) {
640 addfield(map, &metric_expr, "", "", val);
641 for (s = metric_expr; *s; s++)
643 } else if (json_streq(map, field, "ArchStdEvent")) {
644 addfield(map, &arch_std, "", "", val);
645 for (s = arch_std; *s; s++)
648 /* ignore unknown fields */
650 if (precise && desc && !strstr(desc, "(Precise Event)")) {
651 if (json_streq(map, precise, "2"))
652 addfield(map, &extra_desc, " ",
653 "(Must be precise)", NULL);
655 addfield(map, &extra_desc, " ",
656 "(Precise event)", NULL);
658 snprintf(buf, sizeof(buf), "event=%#llx", eventcode);
659 addfield(map, &event, ",", buf, NULL);
660 if (desc && extra_desc)
661 addfield(map, &desc, " ", extra_desc, NULL);
662 if (long_desc && extra_desc)
663 addfield(map, &long_desc, " ", extra_desc, NULL);
665 addfield(map, &event, ",", filter, NULL);
667 addfield(map, &event, ",", msr->pname, msrval);
673 * An arch standard event is referenced, so try to
674 * fixup any unassigned values.
676 err = try_fixup(fn, arch_std, &event, &desc, &name,
677 &long_desc, &pmu, &filter, &perpkg,
678 &unit, &metric_expr, &metric_name,
679 &metric_group, eventcode);
683 err = func(data, name, real_event(name, event), desc, long_desc,
684 pmu, unit, perpkg, metric_expr, metric_name, metric_group);
704 EXPECT(tok - tokens == len, tok, "unexpected objects at end");
707 free_json(map, size, tokens);
712 file_name_to_table_name(const char *fname)
721 * Ensure tablename starts with alphabetic character.
722 * Derive rest of table name from basename of the JSON file,
723 * replacing hyphens and stripping out .json suffix.
725 n = asprintf(&tblname, "pme_%s", fname);
727 pr_info("%s: asprintf() error %s for file %s\n", prog,
728 strerror(errno), fname);
732 for (i = 0; i < strlen(tblname); i++) {
735 if (c == '-' || c == '/')
740 } else if (!isalnum(c) && c != '_') {
741 char *tmp = strdup(fname);
742 pr_err("%s: Invalid character '%c' in file name %s\n",
743 prog, c, basename(tmp));
755 print_mapping_table_prefix(FILE *outfp)
757 fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n");
761 print_mapping_table_suffix(FILE *outfp)
764 * Print the terminating, NULL entry.
766 fprintf(outfp, "{\n");
767 fprintf(outfp, "\t.cpuid = 0,\n");
768 fprintf(outfp, "\t.version = 0,\n");
769 fprintf(outfp, "\t.type = 0,\n");
770 fprintf(outfp, "\t.table = 0,\n");
771 fprintf(outfp, "},\n");
773 /* and finally, the closing curly bracket for the struct */
774 fprintf(outfp, "};\n");
778 process_mapfile(FILE *outfp, char *fpath)
787 pr_info("%s: Processing mapfile %s\n", prog, fpath);
793 mapfp = fopen(fpath, "r");
795 pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
800 print_mapping_table_prefix(outfp);
802 /* Skip first line (header) */
803 p = fgets(line, n, mapfp);
809 char *cpuid, *version, *type, *fname;
812 p = fgets(line, n, mapfp);
816 if (line[0] == '#' || line[0] == '\n')
819 if (line[strlen(line)-1] != '\n') {
820 /* TODO Deal with lines longer than 16K */
821 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
822 prog, fpath, line_num);
825 line[strlen(line)-1] = '\0';
827 cpuid = fixregex(strtok_r(p, ",", &save));
828 version = strtok_r(NULL, ",", &save);
829 fname = strtok_r(NULL, ",", &save);
830 type = strtok_r(NULL, ",", &save);
832 tblname = file_name_to_table_name(fname);
833 fprintf(outfp, "{\n");
834 fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
835 fprintf(outfp, "\t.version = \"%s\",\n", version);
836 fprintf(outfp, "\t.type = \"%s\",\n", type);
839 * CHECK: We can't use the type (eg "core") field in the
840 * table name. For us to do that, we need to somehow tweak
841 * the other caller of file_name_to_table(), process_json()
842 * to determine the type. process_json() file has no way
843 * of knowing these are "core" events unless file name has
844 * core in it. If filename has core in it, we can safely
845 * ignore the type field here also.
847 fprintf(outfp, "\t.table = %s\n", tblname);
848 fprintf(outfp, "},\n");
852 print_mapping_table_suffix(outfp);
857 * If we fail to locate/process JSON and map files, create a NULL mapping
858 * table. This would at least allow perf to build even if we can't find/use
862 create_empty_mapping(const char *output_file)
866 pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
868 /* Truncate file to clear any partial writes to it */
869 outfp = fopen(output_file, "w");
875 fprintf(outfp, "#include \"pmu-events/pmu-events.h\"\n");
876 print_mapping_table_prefix(outfp);
877 print_mapping_table_suffix(outfp);
886 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
887 if (rlim.rlim_max == RLIM_INFINITY)
889 return MIN(rlim.rlim_max / 2, 512);
896 * nftw() doesn't let us pass an argument to the processing function,
897 * so use a global variables.
899 static FILE *eventsfp;
900 static char *mapfile;
903 is_leaf_dir(const char *fpath)
913 while ((dir = readdir(d)) != NULL) {
914 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
917 if (dir->d_type == DT_DIR) {
920 } else if (dir->d_type == DT_UNKNOWN) {
924 snprintf(path, sizeof(path), "%s/%s", fpath,
929 if (S_ISDIR(st.st_mode)) {
942 is_json_file(const char *name)
946 if (strlen(name) < 5)
949 suffix = name + strlen(name) - 5;
951 if (strncmp(suffix, ".json", 5) == 0)
957 preprocess_arch_std_files(const char *fpath, const struct stat *sb,
958 int typeflag, struct FTW *ftwbuf)
960 int level = ftwbuf->level;
961 int is_file = typeflag == FTW_F;
963 if (level == 1 && is_file && is_json_file(fpath))
964 return json_events(fpath, save_arch_std_events, (void *)(uintptr_t)sb);
970 process_one_file(const char *fpath, const struct stat *sb, int typeflag,
975 int is_dir = typeflag == FTW_D;
976 int is_file = typeflag == FTW_F;
977 int level = ftwbuf->level;
980 if (level == 2 && is_dir) {
982 * For level 2 directory, bname will include parent name,
983 * like vendor/platform. So search back from platform dir
986 bname = fpath + ftwbuf->base - 2;
994 bname = fpath + ftwbuf->base;
996 pr_debug("%s %d %7jd %-20s %s\n",
997 is_file ? "f" : is_dir ? "d" : "x",
998 level, sb->st_size, bname, fpath);
1000 /* base dir or too deep */
1001 if (level == 0 || level > 3)
1005 /* model directory, reset topic */
1006 if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
1007 (level == 2 && is_dir)) {
1009 print_events_table_suffix(eventsfp);
1012 * Drop file name suffix. Replace hyphens with underscores.
1013 * Fail if file name contains any alphanum characters besides
1016 tblname = file_name_to_table_name(bname);
1018 pr_info("%s: Error determining table name for %s\n", prog,
1023 print_events_table_prefix(eventsfp, tblname);
1028 * Save the mapfile name for now. We will process mapfile
1029 * after processing all JSON files (so we can write out the
1030 * mapping table after all PMU events tables).
1033 if (level == 1 && is_file) {
1034 if (!strcmp(bname, "mapfile.csv")) {
1035 mapfile = strdup(fpath);
1039 pr_info("%s: Ignoring file %s\n", prog, fpath);
1044 * If the file name does not have a .json extension,
1045 * ignore it. It could be a readme.txt for instance.
1048 if (!is_json_file(bname)) {
1049 pr_info("%s: Ignoring file without .json suffix %s\n", prog,
1055 if (level > 1 && add_topic(bname))
1059 * Assume all other files are JSON files.
1061 * If mapfile refers to 'power7_core.json', we create a table
1062 * named 'power7_core'. Any inconsistencies between the mapfile
1063 * and directory tree could result in build failure due to table
1064 * names not being found.
1066 * Atleast for now, be strict with processing JSON file names.
1067 * i.e. if JSON file name cannot be mapped to C-style table name,
1071 struct perf_entry_data data = {
1072 .topic = get_topic(),
1076 err = json_events(fpath, print_events_table_entry, &data);
1085 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1086 * the set of JSON files for the architecture 'arch'.
1088 * From each JSON file, create a C-style "PMU events table" from the
1089 * JSON file (see struct pmu_event).
1091 * From the mapfile, create a mapping between the CPU revisions and
1092 * PMU event tables (see struct pmu_events_map).
1094 * Write out the PMU events tables and the mapping table to pmu-event.c.
1097 main(int argc, char *argv[])
1101 char ldirname[PATH_MAX];
1104 const char *output_file;
1105 const char *start_dirname;
1108 prog = basename(argv[0]);
1110 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
1115 start_dirname = argv[2];
1116 output_file = argv[3];
1119 verbose = atoi(argv[4]);
1121 eventsfp = fopen(output_file, "w");
1123 pr_err("%s Unable to create required file %s (%s)\n",
1124 prog, output_file, strerror(errno));
1128 snprintf(ldirname, sizeof(ldirname), "%s/%s", start_dirname, arch);
1130 /* If architecture does not have any event lists, bail out */
1131 if (stat(ldirname, &stbuf) < 0) {
1132 pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
1136 /* Include pmu-events.h first */
1137 fprintf(eventsfp, "#include \"pmu-events/pmu-events.h\"\n");
1140 * The mapfile allows multiple CPUids to point to the same JSON file,
1141 * so, not sure if there is a need for symlinks within the pmu-events
1144 * For now, treat symlinks of JSON files as regular files and create
1145 * separate tables for each symlink (presumably, each symlink refers
1146 * to specific version of the CPU).
1149 maxfds = get_maxfds();
1151 rc = nftw_ordered(ldirname, preprocess_arch_std_files, maxfds, 0);
1152 if (rc && verbose) {
1153 pr_info("%s: Error preprocessing arch standard files %s: %s\n",
1154 prog, ldirname, strerror(errno));
1156 } else if (rc < 0) {
1157 /* Make build fail */
1158 free_arch_std_events();
1164 rc = nftw_ordered(ldirname, process_one_file, maxfds, 0);
1165 if (rc && verbose) {
1166 pr_info("%s: Error walking file tree %s\n", prog, ldirname);
1168 } else if (rc < 0) {
1169 /* Make build fail */
1170 free_arch_std_events();
1177 print_events_table_suffix(eventsfp);
1180 pr_info("%s: No CPU->JSON mapping?\n", prog);
1184 if (process_mapfile(eventsfp, mapfile)) {
1185 pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
1186 /* Make build fail */
1194 create_empty_mapping(output_file);
1195 free_arch_std_events();
1200 fts_compare(const FTSENT * const *a, const FTSENT * const *b)
1202 return (strcmp((*a)->fts_name, (*b)->fts_name));
1206 nftw_ordered(const char *path, int (*fn)(const char *, const struct stat *, int,
1207 struct FTW *), int nfds, int ftwflags)
1209 char * const paths[2] = { (char *)path, NULL };
1213 int error = 0, ftsflags, fnflag, postorder, sverrno;
1215 /* XXX - nfds is currently unused */
1221 ftsflags = FTS_COMFOLLOW;
1222 if (!(ftwflags & FTW_CHDIR))
1223 ftsflags |= FTS_NOCHDIR;
1224 if (ftwflags & FTW_MOUNT)
1225 ftsflags |= FTS_XDEV;
1226 if (ftwflags & FTW_PHYS)
1227 ftsflags |= FTS_PHYSICAL;
1229 ftsflags |= FTS_LOGICAL;
1230 postorder = (ftwflags & FTW_DEPTH) != 0;
1231 ftsp = fts_open(paths, ftsflags, fts_compare);
1234 while ((cur = fts_read(ftsp)) != NULL) {
1235 switch (cur->fts_info) {
1269 ftw.base = cur->fts_pathlen - cur->fts_namelen;
1270 ftw.level = cur->fts_level;
1271 error = fn(cur->fts_path, cur->fts_statp, fnflag, &ftw);
1277 if (fts_close(ftsp) != 0 && error == 0)