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 */
56 nftw_ordered(const char *path, int (*fn)(const char *, const struct stat *, int,
57 struct FTW *), int nfds, int ftwflags);
59 _Noreturn void _Exit(int);
64 int eprintf(int level, int var, const char *fmt, ...)
75 ret = vfprintf(stderr, fmt, args);
82 __attribute__((weak)) char *get_cpu_str(void)
87 static void addfield(char *map, char **dst, const char *sep,
88 const char *a, jsmntok_t *bt)
90 unsigned int len = strlen(a) + 1 + strlen(sep);
91 int olen = *dst ? strlen(*dst) : 0;
92 int blen = bt ? json_len(bt) : 0;
95 out = realloc(*dst, len + olen + blen);
97 /* Don't add field in this case */
108 strncat(*dst, map + bt->start, blen);
111 static void fixname(char *s)
117 static void fixdesc(char *s)
119 char *e = s + strlen(s);
121 /* Remove trailing dots that look ugly in perf list */
123 while (e >= s && isspace(*e))
129 /* Add escapes for '\' so they are proper C strings. */
130 static char *fixregex(char *s)
137 /* Count the number of '\' in string */
138 for (p = s; *p; p++) {
147 /* allocate space for a new string */
148 fixed = (char *) malloc(len + 1);
152 /* copy over the characters */
154 for (p = s; *p; p++) {
166 static struct msrmap {
170 { "0x3F6", "ldlat=" },
171 { "0x1A6", "offcore_rsp=" },
172 { "0x1A7", "offcore_rsp=" },
173 { "0x3F7", "frontend=" },
177 static struct field {
181 { "UMask", "umask=" },
182 { "CounterMask", "cmask=" },
183 { "Invert", "inv=" },
184 { "AnyThread", "any=" },
185 { "EdgeDetect", "edge=" },
186 { "SampleAfterValue", "period=" },
187 { "FCMask", "fc_mask=" },
188 { "PortMask", "ch_mask=" },
189 { "L3ThreadMask", "l3_thread_mask=" },
190 { "L3SliceMask", "l3_slice_mask=" },
194 static void cut_comma(char *map, jsmntok_t *newval)
198 /* Cut off everything after comma */
199 for (i = newval->start; i < newval->end; i++) {
205 static int match_field(char *map, jsmntok_t *field, int nz,
206 char **event, jsmntok_t *val)
209 jsmntok_t newval = *val;
211 for (f = fields; f->field; f++)
212 if (json_streq(map, field, f->field) && nz) {
213 cut_comma(map, &newval);
214 addfield(map, event, ",", f->kernel, &newval);
220 static struct msrmap *lookup_msr(char *map, jsmntok_t *val)
222 jsmntok_t newval = *val;
226 cut_comma(map, &newval);
227 for (i = 0; msrmap[i].num; i++)
228 if (json_streq(map, &newval, msrmap[i].num))
232 pr_err("%s: Unknown MSR in event file %.*s\n", prog,
233 json_len(val), map + val->start);
242 { "CBO", "uncore_cbox" },
243 { "QPI LL", "uncore_qpi" },
244 { "SBO", "uncore_sbox" },
245 { "iMPH-U", "uncore_arb" },
249 static const char *field_to_perf(struct map *table, char *map, jsmntok_t *val)
253 for (i = 0; table[i].json; i++) {
254 if (json_streq(map, val, table[i].json))
255 return table[i].perf;
260 #define EXPECT(e, t, m) do { if (!(e)) { \
261 jsmntok_t *loc = (t); \
262 if (!(t)->start && (t) > tokens) \
264 pr_err("%s:%d: " m ", got %s\n", fn, \
265 json_line(map, loc), \
273 static char *get_topic(void)
278 /* tp is free'd in process_one_file() */
279 i = asprintf(&tp, "%s", topic);
281 pr_info("%s: asprintf() error %s\n", prog);
285 for (i = 0; i < (int) strlen(tp); i++) {
299 static int add_topic(const char *bname)
302 topic = strdup(bname);
304 pr_info("%s: strdup() error %s for file %s\n", prog,
305 strerror(errno), bname);
311 struct perf_entry_data {
316 static int close_table;
318 static void print_events_table_prefix(FILE *fp, const char *tblname)
320 fprintf(fp, "static struct pmu_event %s[] = {\n", tblname);
324 static int print_events_table_entry(void *data, char *name, const char *event,
325 char *desc, char *long_desc,
326 char *pmu, char *unit, char *perpkg,
328 char *metric_name, char *metric_group)
330 struct perf_entry_data *pd = data;
331 FILE *outfp = pd->outfp;
332 char *etopic = pd->topic;
335 * TODO: Remove formatting chars after debugging to reduce
338 fprintf(outfp, "{\n");
341 fprintf(outfp, "\t.name = \"%s\",\n", name);
343 fprintf(outfp, "\t.event = \"%s\",\n", event);
344 fprintf(outfp, "\t.desc = \"%s\",\n", desc);
345 fprintf(outfp, "\t.topic = \"%s\",\n", etopic);
346 if (long_desc && long_desc[0])
347 fprintf(outfp, "\t.long_desc = \"%s\",\n", long_desc);
349 fprintf(outfp, "\t.pmu = \"%s\",\n", pmu);
351 fprintf(outfp, "\t.unit = \"%s\",\n", unit);
353 fprintf(outfp, "\t.perpkg = \"%s\",\n", perpkg);
355 fprintf(outfp, "\t.metric_expr = \"%s\",\n", metric_expr);
357 fprintf(outfp, "\t.metric_name = \"%s\",\n", metric_name);
359 fprintf(outfp, "\t.metric_group = \"%s\",\n", metric_group);
360 fprintf(outfp, "},\n");
365 struct event_struct {
366 struct list_head list;
379 #define ADD_EVENT_FIELD(field) do { if (field) { \
380 es->field = strdup(field); \
385 #define FREE_EVENT_FIELD(field) free(es->field)
387 #define TRY_FIXUP_FIELD(field) do { if (es->field && !*field) {\
388 *field = strdup(es->field); \
393 #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
406 static LIST_HEAD(arch_std_events);
408 static void free_arch_std_events(void)
410 struct event_struct *es, *next;
412 list_for_each_entry_safe(es, next, &arch_std_events, list) {
413 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
419 static int save_arch_std_events(void *data __unused, char *name, const char *event,
420 char *desc, char *long_desc, char *pmu,
421 char *unit, char *perpkg, char *metric_expr,
422 char *metric_name, char *metric_group)
424 struct event_struct *es;
426 es = malloc(sizeof(*es));
429 memset(es, 0, sizeof(*es));
430 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD);
431 list_add_tail(&es->list, &arch_std_events);
434 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
439 static void print_events_table_suffix(FILE *outfp)
441 fprintf(outfp, "{\n");
443 fprintf(outfp, "\t.name = 0,\n");
444 fprintf(outfp, "\t.event = 0,\n");
445 fprintf(outfp, "\t.desc = 0,\n");
447 fprintf(outfp, "},\n");
448 fprintf(outfp, "};\n");
452 static struct fixed {
456 { "inst_retired.any", "event=0xc0" },
457 { "inst_retired.any_p", "event=0xc0" },
458 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03" },
459 { "cpu_clk_unhalted.thread", "event=0x3c" },
460 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1" },
465 * Handle different fixed counter encodings between JSON and perf.
467 static const char *real_event(const char *name, char *event)
474 for (i = 0; fixed[i].name; i++)
475 if (!strcasecmp(name, fixed[i].name))
476 return fixed[i].event;
481 try_fixup(const char *fn, char *arch_std, char **event, char **desc,
482 char **name, char **long_desc, char **pmu, char **filter __unused,
483 char **perpkg, char **unit, char **metric_expr, char **metric_name,
484 char **metric_group, unsigned long long eventcode)
486 /* try to find matching event from arch standard values */
487 struct event_struct *es;
489 list_for_each_entry(es, &arch_std_events, list) {
490 if (!strcmp(arch_std, es->name)) {
491 if (!eventcode && es->event) {
492 /* allow EventCode to be overridden */
496 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
501 pr_err("%s: could not find matching %s for %s\n",
506 /* Call func with each event in the json file */
507 int json_events(const char *fn,
508 int (*func)(void *data, char *name, const char *event, char *desc,
510 char *pmu, char *unit, char *perpkg,
512 char *metric_name, char *metric_group),
517 jsmntok_t *tokens, *tok;
525 tokens = parse_json(fn, &map, &size, &len);
528 EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
530 for (i = 0; i < tokens->size; i++) {
531 char *event = NULL, *desc = NULL, *name = NULL;
532 char *long_desc = NULL;
533 char *extra_desc = NULL;
538 char *metric_expr = NULL;
539 char *metric_name = NULL;
540 char *metric_group = NULL;
541 char *arch_std = NULL;
542 unsigned long long eventcode = 0;
543 struct msrmap *msr = NULL;
544 jsmntok_t *msrval = NULL;
545 jsmntok_t *precise = NULL;
546 jsmntok_t *obj = tok++;
548 EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
549 for (j = 0; j < obj->size; j += 2) {
550 jsmntok_t *field, *val;
555 EXPECT(field->type == JSMN_STRING, tok + j,
556 "Expected field name");
558 EXPECT(val->type == JSMN_STRING, tok + j + 1,
559 "Expected string value");
561 nz = !json_streq(map, val, "0");
562 if (match_field(map, field, nz, &event, val)) {
564 } else if (json_streq(map, field, "EventCode")) {
566 addfield(map, &code, "", "", val);
567 eventcode |= strtoul(code, NULL, 0);
569 } else if (json_streq(map, field, "ExtSel")) {
571 addfield(map, &code, "", "", val);
572 eventcode |= strtoul(code, NULL, 0) << 21;
574 } else if (json_streq(map, field, "EventName")) {
575 addfield(map, &name, "", "", val);
576 } else if (json_streq(map, field, "BriefDescription")) {
577 addfield(map, &desc, "", "", val);
579 } else if (json_streq(map, field,
580 "PublicDescription")) {
581 addfield(map, &long_desc, "", "", val);
583 } else if (json_streq(map, field, "PEBS") && nz) {
585 } else if (json_streq(map, field, "MSRIndex") && nz) {
586 msr = lookup_msr(map, val);
587 } else if (json_streq(map, field, "MSRValue")) {
589 } else if (json_streq(map, field, "Errata") &&
590 !json_streq(map, val, "null")) {
591 addfield(map, &extra_desc, ". ",
592 " Spec update: ", val);
593 } else if (json_streq(map, field, "Data_LA") && nz) {
594 addfield(map, &extra_desc, ". ",
595 " Supports address when precise",
597 } else if (json_streq(map, field, "Unit")) {
600 ppmu = field_to_perf(unit_to_pmu, map, val);
605 pmu = strdup("uncore_");
606 addfield(map, &pmu, "", "", val);
607 for (s = pmu; *s; s++)
610 addfield(map, &desc, ". ", "Unit: ", NULL);
611 addfield(map, &desc, "", pmu, NULL);
612 addfield(map, &desc, "", " ", NULL);
613 } else if (json_streq(map, field, "Filter")) {
614 addfield(map, &filter, "", "", val);
615 } else if (json_streq(map, field, "ScaleUnit")) {
616 addfield(map, &unit, "", "", val);
617 } else if (json_streq(map, field, "PerPkg")) {
618 addfield(map, &perpkg, "", "", val);
619 } else if (json_streq(map, field, "MetricName")) {
620 addfield(map, &metric_name, "", "", val);
621 } else if (json_streq(map, field, "MetricGroup")) {
622 addfield(map, &metric_group, "", "", val);
623 } else if (json_streq(map, field, "MetricExpr")) {
624 addfield(map, &metric_expr, "", "", val);
625 for (s = metric_expr; *s; s++)
627 } else if (json_streq(map, field, "ArchStdEvent")) {
628 addfield(map, &arch_std, "", "", val);
629 for (s = arch_std; *s; s++)
632 /* ignore unknown fields */
634 if (precise && desc && !strstr(desc, "(Precise Event)")) {
635 if (json_streq(map, precise, "2"))
636 addfield(map, &extra_desc, " ",
637 "(Must be precise)", NULL);
639 addfield(map, &extra_desc, " ",
640 "(Precise event)", NULL);
642 snprintf(buf, sizeof(buf), "event=%#llx", eventcode);
643 addfield(map, &event, ",", buf, NULL);
644 if (desc && extra_desc)
645 addfield(map, &desc, " ", extra_desc, NULL);
646 if (long_desc && extra_desc)
647 addfield(map, &long_desc, " ", extra_desc, NULL);
649 addfield(map, &event, ",", filter, NULL);
651 addfield(map, &event, ",", msr->pname, msrval);
657 * An arch standard event is referenced, so try to
658 * fixup any unassigned values.
660 err = try_fixup(fn, arch_std, &event, &desc, &name,
661 &long_desc, &pmu, &filter, &perpkg,
662 &unit, &metric_expr, &metric_name,
663 &metric_group, eventcode);
667 err = func(data, name, real_event(name, event), desc, long_desc,
668 pmu, unit, perpkg, metric_expr, metric_name, metric_group);
688 EXPECT(tok - tokens == len, tok, "unexpected objects at end");
691 free_json(map, size, tokens);
695 static char *file_name_to_table_name(const char *fname)
704 * Ensure tablename starts with alphabetic character.
705 * Derive rest of table name from basename of the JSON file,
706 * replacing hyphens and stripping out .json suffix.
708 n = asprintf(&tblname, "pme_%s", fname);
710 pr_info("%s: asprintf() error %s for file %s\n", prog,
711 strerror(errno), fname);
715 for (i = 0; i < strlen(tblname); i++) {
718 if (c == '-' || c == '/')
723 } else if (!isalnum(c) && c != '_') {
724 char *tmp = strdup(fname);
725 pr_err("%s: Invalid character '%c' in file name %s\n",
726 prog, c, basename(tmp));
737 static void print_mapping_table_prefix(FILE *outfp)
739 fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n");
742 static void print_mapping_table_suffix(FILE *outfp)
745 * Print the terminating, NULL entry.
747 fprintf(outfp, "{\n");
748 fprintf(outfp, "\t.cpuid = 0,\n");
749 fprintf(outfp, "\t.version = 0,\n");
750 fprintf(outfp, "\t.type = 0,\n");
751 fprintf(outfp, "\t.table = 0,\n");
752 fprintf(outfp, "},\n");
754 /* and finally, the closing curly bracket for the struct */
755 fprintf(outfp, "};\n");
758 static int process_mapfile(FILE *outfp, char *fpath)
767 pr_info("%s: Processing mapfile %s\n", prog, fpath);
773 mapfp = fopen(fpath, "r");
775 pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
780 print_mapping_table_prefix(outfp);
782 /* Skip first line (header) */
783 p = fgets(line, n, mapfp);
789 char *cpuid, *version, *type, *fname;
792 p = fgets(line, n, mapfp);
796 if (line[0] == '#' || line[0] == '\n')
799 if (line[strlen(line)-1] != '\n') {
800 /* TODO Deal with lines longer than 16K */
801 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
802 prog, fpath, line_num);
805 line[strlen(line)-1] = '\0';
807 cpuid = fixregex(strtok_r(p, ",", &save));
808 version = strtok_r(NULL, ",", &save);
809 fname = strtok_r(NULL, ",", &save);
810 type = strtok_r(NULL, ",", &save);
812 tblname = file_name_to_table_name(fname);
813 fprintf(outfp, "{\n");
814 fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
815 fprintf(outfp, "\t.version = \"%s\",\n", version);
816 fprintf(outfp, "\t.type = \"%s\",\n", type);
819 * CHECK: We can't use the type (eg "core") field in the
820 * table name. For us to do that, we need to somehow tweak
821 * the other caller of file_name_to_table(), process_json()
822 * to determine the type. process_json() file has no way
823 * of knowing these are "core" events unless file name has
824 * core in it. If filename has core in it, we can safely
825 * ignore the type field here also.
827 fprintf(outfp, "\t.table = %s\n", tblname);
828 fprintf(outfp, "},\n");
832 print_mapping_table_suffix(outfp);
837 * If we fail to locate/process JSON and map files, create a NULL mapping
838 * table. This would at least allow perf to build even if we can't find/use
841 static void create_empty_mapping(const char *output_file)
845 pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
847 /* Truncate file to clear any partial writes to it */
848 outfp = fopen(output_file, "w");
854 fprintf(outfp, "#include \"pmu-events/pmu-events.h\"\n");
855 print_mapping_table_prefix(outfp);
856 print_mapping_table_suffix(outfp);
860 static int get_maxfds(void)
864 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
865 if (rlim.rlim_max == RLIM_INFINITY)
867 return MIN(rlim.rlim_max / 2, 512);
874 * nftw() doesn't let us pass an argument to the processing function,
875 * so use a global variables.
877 static FILE *eventsfp;
878 static char *mapfile;
880 static int is_leaf_dir(const char *fpath)
890 while ((dir = readdir(d)) != NULL) {
891 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
894 if (dir->d_type == DT_DIR) {
897 } else if (dir->d_type == DT_UNKNOWN) {
901 sprintf(path, "%s/%s", fpath, dir->d_name);
905 if (S_ISDIR(st.st_mode)) {
917 static int is_json_file(const char *name)
921 if (strlen(name) < 5)
924 suffix = name + strlen(name) - 5;
926 if (strncmp(suffix, ".json", 5) == 0)
931 static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
932 int typeflag, struct FTW *ftwbuf)
934 int level = ftwbuf->level;
935 int is_file = typeflag == FTW_F;
937 if (level == 1 && is_file && is_json_file(fpath))
938 return json_events(fpath, save_arch_std_events, (void *)(uintptr_t)sb);
943 static int process_one_file(const char *fpath, const struct stat *sb,
944 int typeflag, struct FTW *ftwbuf)
948 int is_dir = typeflag == FTW_D;
949 int is_file = typeflag == FTW_F;
950 int level = ftwbuf->level;
953 if (level == 2 && is_dir) {
955 * For level 2 directory, bname will include parent name,
956 * like vendor/platform. So search back from platform dir
959 bname = fpath + ftwbuf->base - 2;
967 bname = fpath + ftwbuf->base;
969 pr_debug("%s %d %7jd %-20s %s\n",
970 is_file ? "f" : is_dir ? "d" : "x",
971 level, sb->st_size, bname, fpath);
973 /* base dir or too deep */
974 if (level == 0 || level > 3)
978 /* model directory, reset topic */
979 if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
980 (level == 2 && is_dir)) {
982 print_events_table_suffix(eventsfp);
985 * Drop file name suffix. Replace hyphens with underscores.
986 * Fail if file name contains any alphanum characters besides
989 tblname = file_name_to_table_name(bname);
991 pr_info("%s: Error determining table name for %s\n", prog,
996 print_events_table_prefix(eventsfp, tblname);
1001 * Save the mapfile name for now. We will process mapfile
1002 * after processing all JSON files (so we can write out the
1003 * mapping table after all PMU events tables).
1006 if (level == 1 && is_file) {
1007 if (!strcmp(bname, "mapfile.csv")) {
1008 mapfile = strdup(fpath);
1012 pr_info("%s: Ignoring file %s\n", prog, fpath);
1017 * If the file name does not have a .json extension,
1018 * ignore it. It could be a readme.txt for instance.
1021 if (!is_json_file(bname)) {
1022 pr_info("%s: Ignoring file without .json suffix %s\n", prog,
1028 if (level > 1 && add_topic(bname))
1032 * Assume all other files are JSON files.
1034 * If mapfile refers to 'power7_core.json', we create a table
1035 * named 'power7_core'. Any inconsistencies between the mapfile
1036 * and directory tree could result in build failure due to table
1037 * names not being found.
1039 * Atleast for now, be strict with processing JSON file names.
1040 * i.e. if JSON file name cannot be mapped to C-style table name,
1044 struct perf_entry_data data = {
1045 .topic = get_topic(),
1049 err = json_events(fpath, print_events_table_entry, &data);
1058 #define PATH_MAX 4096
1062 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1063 * the set of JSON files for the architecture 'arch'.
1065 * From each JSON file, create a C-style "PMU events table" from the
1066 * JSON file (see struct pmu_event).
1068 * From the mapfile, create a mapping between the CPU revisions and
1069 * PMU event tables (see struct pmu_events_map).
1071 * Write out the PMU events tables and the mapping table to pmu-event.c.
1073 int main(int argc, char *argv[])
1077 char ldirname[PATH_MAX];
1080 const char *output_file;
1081 const char *start_dirname;
1084 prog = basename(argv[0]);
1086 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
1091 start_dirname = argv[2];
1092 output_file = argv[3];
1095 verbose = atoi(argv[4]);
1097 eventsfp = fopen(output_file, "w");
1099 pr_err("%s Unable to create required file %s (%s)\n",
1100 prog, output_file, strerror(errno));
1104 sprintf(ldirname, "%s/%s", start_dirname, arch);
1106 /* If architecture does not have any event lists, bail out */
1107 if (stat(ldirname, &stbuf) < 0) {
1108 pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
1112 /* Include pmu-events.h first */
1113 fprintf(eventsfp, "#include \"pmu-events/pmu-events.h\"\n");
1116 * The mapfile allows multiple CPUids to point to the same JSON file,
1117 * so, not sure if there is a need for symlinks within the pmu-events
1120 * For now, treat symlinks of JSON files as regular files and create
1121 * separate tables for each symlink (presumably, each symlink refers
1122 * to specific version of the CPU).
1125 maxfds = get_maxfds();
1127 rc = nftw_ordered(ldirname, preprocess_arch_std_files, maxfds, 0);
1128 if (rc && verbose) {
1129 pr_info("%s: Error preprocessing arch standard files %s: %s\n",
1130 prog, ldirname, strerror(errno));
1132 } else if (rc < 0) {
1133 /* Make build fail */
1134 free_arch_std_events();
1140 rc = nftw_ordered(ldirname, process_one_file, maxfds, 0);
1141 if (rc && verbose) {
1142 pr_info("%s: Error walking file tree %s\n", prog, ldirname);
1144 } else if (rc < 0) {
1145 /* Make build fail */
1146 free_arch_std_events();
1153 print_events_table_suffix(eventsfp);
1156 pr_info("%s: No CPU->JSON mapping?\n", prog);
1160 if (process_mapfile(eventsfp, mapfile)) {
1161 pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
1162 /* Make build fail */
1170 create_empty_mapping(output_file);
1171 free_arch_std_events();
1178 fts_compare(const FTSENT * const *a, const FTSENT * const *b)
1180 return (strcmp((*a)->fts_name, (*b)->fts_name));
1184 nftw_ordered(const char *path, int (*fn)(const char *, const struct stat *, int,
1185 struct FTW *), int nfds, int ftwflags)
1187 char * const paths[2] = { (char *)path, NULL };
1191 int error = 0, ftsflags, fnflag, postorder, sverrno;
1193 /* XXX - nfds is currently unused */
1199 ftsflags = FTS_COMFOLLOW;
1200 if (!(ftwflags & FTW_CHDIR))
1201 ftsflags |= FTS_NOCHDIR;
1202 if (ftwflags & FTW_MOUNT)
1203 ftsflags |= FTS_XDEV;
1204 if (ftwflags & FTW_PHYS)
1205 ftsflags |= FTS_PHYSICAL;
1207 ftsflags |= FTS_LOGICAL;
1208 postorder = (ftwflags & FTW_DEPTH) != 0;
1209 ftsp = fts_open(paths, ftsflags, fts_compare);
1212 while ((cur = fts_read(ftsp)) != NULL) {
1213 switch (cur->fts_info) {
1247 ftw.base = cur->fts_pathlen - cur->fts_namelen;
1248 ftw.level = cur->fts_level;
1249 error = fn(cur->fts_path, cur->fts_statp, fnflag, &ftw);
1255 if (fts_close(ftsp) != 0 && error == 0)