1 #define _XOPEN_SOURCE 500 /* needed for nftw() */
2 #define __BSD_VISIBLE 1 /* needed for asprintf() */
3 /* Parse event JSON files */
6 * Copyright (c) 2014, Intel Corporation
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 #include <sys/time.h> /* getrlimit */
49 #include <sys/resource.h> /* getrlimit */
58 nftw_ordered(const char *path, int (*fn)(const char *, const struct stat *, int,
59 struct FTW *), int nfds, int ftwflags);
61 _Noreturn void _Exit(int);
66 int eprintf(int level, int var, const char *fmt, ...)
77 ret = vfprintf(stderr, fmt, args);
84 __attribute__((weak)) char *get_cpu_str(void)
89 static void addfield(char *map, char **dst, const char *sep,
90 const char *a, jsmntok_t *bt)
92 unsigned int len = strlen(a) + 1 + strlen(sep);
93 int olen = *dst ? strlen(*dst) : 0;
94 int blen = bt ? json_len(bt) : 0;
97 out = realloc(*dst, len + olen + blen);
99 /* Don't add field in this case */
110 strncat(*dst, map + bt->start, blen);
113 static void fixname(char *s)
119 static void fixdesc(char *s)
121 char *e = s + strlen(s);
123 /* Remove trailing dots that look ugly in perf list */
125 while (e >= s && isspace(*e))
131 /* Add escapes for '\' so they are proper C strings. */
132 static char *fixregex(char *s)
139 /* Count the number of '\' in string */
140 for (p = s; *p; p++) {
149 /* allocate space for a new string */
150 fixed = (char *) malloc(len + 1);
154 /* copy over the characters */
156 for (p = s; *p; p++) {
168 static struct msrmap {
172 { "0x3F6", "ldlat=" },
173 { "0x1A6", "offcore_rsp=" },
174 { "0x1A7", "offcore_rsp=" },
175 { "0x3F7", "frontend=" },
179 static struct field {
183 { "UMask", "umask=" },
184 { "CounterMask", "cmask=" },
185 { "Invert", "inv=" },
186 { "AnyThread", "any=" },
187 { "EdgeDetect", "edge=" },
188 { "SampleAfterValue", "period=" },
189 { "FCMask", "fc_mask=" },
190 { "PortMask", "ch_mask=" },
191 { "L3ThreadMask", "l3_thread_mask=" },
192 { "L3SliceMask", "l3_slice_mask=" },
196 static void cut_comma(char *map, jsmntok_t *newval)
200 /* Cut off everything after comma */
201 for (i = newval->start; i < newval->end; i++) {
207 static int match_field(char *map, jsmntok_t *field, int nz,
208 char **event, jsmntok_t *val)
211 jsmntok_t newval = *val;
213 for (f = fields; f->field; f++)
214 if (json_streq(map, field, f->field) && nz) {
215 cut_comma(map, &newval);
216 addfield(map, event, ",", f->kernel, &newval);
222 static struct msrmap *lookup_msr(char *map, jsmntok_t *val)
224 jsmntok_t newval = *val;
228 cut_comma(map, &newval);
229 for (i = 0; msrmap[i].num; i++)
230 if (json_streq(map, &newval, msrmap[i].num))
234 pr_err("%s: Unknown MSR in event file %.*s\n", prog,
235 json_len(val), map + val->start);
244 { "CBO", "uncore_cbox" },
245 { "QPI LL", "uncore_qpi" },
246 { "SBO", "uncore_sbox" },
247 { "iMPH-U", "uncore_arb" },
251 static const char *field_to_perf(struct map *table, char *map, jsmntok_t *val)
255 for (i = 0; table[i].json; i++) {
256 if (json_streq(map, val, table[i].json))
257 return table[i].perf;
262 #define EXPECT(e, t, m) do { if (!(e)) { \
263 jsmntok_t *loc = (t); \
264 if (!(t)->start && (t) > tokens) \
266 pr_err("%s:%d: " m ", got %s\n", fn, \
267 json_line(map, loc), \
275 static char *get_topic(void)
280 /* tp is free'd in process_one_file() */
281 i = asprintf(&tp, "%s", topic);
283 pr_info("%s: asprintf() error %s\n", prog);
287 for (i = 0; i < (int) strlen(tp); i++) {
301 static int add_topic(const char *bname)
304 topic = strdup(bname);
306 pr_info("%s: strdup() error %s for file %s\n", prog,
307 strerror(errno), bname);
313 struct perf_entry_data {
318 static int close_table;
320 static void print_events_table_prefix(FILE *fp, const char *tblname)
322 fprintf(fp, "static struct pmu_event %s[] = {\n", tblname);
326 static int print_events_table_entry(void *data, char *name, const char *event,
327 char *desc, char *long_desc,
328 char *pmu, char *unit, char *perpkg,
330 char *metric_name, char *metric_group)
332 struct perf_entry_data *pd = data;
333 FILE *outfp = pd->outfp;
334 char *etopic = pd->topic;
337 * TODO: Remove formatting chars after debugging to reduce
340 fprintf(outfp, "{\n");
343 fprintf(outfp, "\t.name = \"%s\",\n", name);
345 fprintf(outfp, "\t.event = \"%s\",\n", event);
346 fprintf(outfp, "\t.desc = \"%s\",\n", desc);
347 fprintf(outfp, "\t.topic = \"%s\",\n", etopic);
348 if (long_desc && long_desc[0])
349 fprintf(outfp, "\t.long_desc = \"%s\",\n", long_desc);
351 fprintf(outfp, "\t.pmu = \"%s\",\n", pmu);
353 fprintf(outfp, "\t.unit = \"%s\",\n", unit);
355 fprintf(outfp, "\t.perpkg = \"%s\",\n", perpkg);
357 fprintf(outfp, "\t.metric_expr = \"%s\",\n", metric_expr);
359 fprintf(outfp, "\t.metric_name = \"%s\",\n", metric_name);
361 fprintf(outfp, "\t.metric_group = \"%s\",\n", metric_group);
362 fprintf(outfp, "},\n");
367 struct event_struct {
368 struct list_head list;
381 #define ADD_EVENT_FIELD(field) do { if (field) { \
382 es->field = strdup(field); \
387 #define FREE_EVENT_FIELD(field) free(es->field)
389 #define TRY_FIXUP_FIELD(field) do { if (es->field && !*field) {\
390 *field = strdup(es->field); \
395 #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
408 static LIST_HEAD(arch_std_events);
410 static void free_arch_std_events(void)
412 struct event_struct *es, *next;
414 list_for_each_entry_safe(es, next, &arch_std_events, list) {
415 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
421 static int save_arch_std_events(void *data __unused, char *name, const char *event,
422 char *desc, char *long_desc, char *pmu,
423 char *unit, char *perpkg, char *metric_expr,
424 char *metric_name, char *metric_group)
426 struct event_struct *es;
428 es = malloc(sizeof(*es));
431 memset(es, 0, sizeof(*es));
432 FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD);
433 list_add_tail(&es->list, &arch_std_events);
436 FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
441 static void print_events_table_suffix(FILE *outfp)
443 fprintf(outfp, "{\n");
445 fprintf(outfp, "\t.name = 0,\n");
446 fprintf(outfp, "\t.event = 0,\n");
447 fprintf(outfp, "\t.desc = 0,\n");
449 fprintf(outfp, "},\n");
450 fprintf(outfp, "};\n");
454 static struct fixed {
458 { "inst_retired.any", "event=0xc0" },
459 { "inst_retired.any_p", "event=0xc0" },
460 { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03" },
461 { "cpu_clk_unhalted.thread", "event=0x3c" },
462 { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1" },
467 * Handle different fixed counter encodings between JSON and perf.
469 static const char *real_event(const char *name, char *event)
476 for (i = 0; fixed[i].name; i++)
477 if (!strcasecmp(name, fixed[i].name))
478 return fixed[i].event;
483 try_fixup(const char *fn, char *arch_std, char **event, char **desc,
484 char **name, char **long_desc, char **pmu, char **filter __unused,
485 char **perpkg, char **unit, char **metric_expr, char **metric_name,
486 char **metric_group, unsigned long long eventcode)
488 /* try to find matching event from arch standard values */
489 struct event_struct *es;
491 list_for_each_entry(es, &arch_std_events, list) {
492 if (!strcmp(arch_std, es->name)) {
493 if (!eventcode && es->event) {
494 /* allow EventCode to be overridden */
498 FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
503 pr_err("%s: could not find matching %s for %s\n",
508 /* Call func with each event in the json file */
509 int json_events(const char *fn,
510 int (*func)(void *data, char *name, const char *event, char *desc,
512 char *pmu, char *unit, char *perpkg,
514 char *metric_name, char *metric_group),
519 jsmntok_t *tokens, *tok;
527 tokens = parse_json(fn, &map, &size, &len);
530 EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
532 for (i = 0; i < tokens->size; i++) {
533 char *event = NULL, *desc = NULL, *name = NULL;
534 char *long_desc = NULL;
535 char *extra_desc = NULL;
540 char *metric_expr = NULL;
541 char *metric_name = NULL;
542 char *metric_group = NULL;
543 char *arch_std = NULL;
544 unsigned long long eventcode = 0;
545 struct msrmap *msr = NULL;
546 jsmntok_t *msrval = NULL;
547 jsmntok_t *precise = NULL;
548 jsmntok_t *obj = tok++;
550 EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
551 for (j = 0; j < obj->size; j += 2) {
552 jsmntok_t *field, *val;
557 EXPECT(field->type == JSMN_STRING, tok + j,
558 "Expected field name");
560 EXPECT(val->type == JSMN_STRING, tok + j + 1,
561 "Expected string value");
563 nz = !json_streq(map, val, "0");
564 if (match_field(map, field, nz, &event, val)) {
566 } else if (json_streq(map, field, "EventCode")) {
568 addfield(map, &code, "", "", val);
569 eventcode |= strtoul(code, NULL, 0);
571 } else if (json_streq(map, field, "ExtSel")) {
573 addfield(map, &code, "", "", val);
574 eventcode |= strtoul(code, NULL, 0) << 21;
576 } else if (json_streq(map, field, "EventName")) {
577 addfield(map, &name, "", "", val);
578 } else if (json_streq(map, field, "BriefDescription")) {
579 addfield(map, &desc, "", "", val);
581 } else if (json_streq(map, field,
582 "PublicDescription")) {
583 addfield(map, &long_desc, "", "", val);
585 } else if (json_streq(map, field, "PEBS") && nz) {
587 } else if (json_streq(map, field, "MSRIndex") && nz) {
588 msr = lookup_msr(map, val);
589 } else if (json_streq(map, field, "MSRValue")) {
591 } else if (json_streq(map, field, "Errata") &&
592 !json_streq(map, val, "null")) {
593 addfield(map, &extra_desc, ". ",
594 " Spec update: ", val);
595 } else if (json_streq(map, field, "Data_LA") && nz) {
596 addfield(map, &extra_desc, ". ",
597 " Supports address when precise",
599 } else if (json_streq(map, field, "Unit")) {
602 ppmu = field_to_perf(unit_to_pmu, map, val);
607 pmu = strdup("uncore_");
608 addfield(map, &pmu, "", "", val);
609 for (s = pmu; *s; s++)
612 addfield(map, &desc, ". ", "Unit: ", NULL);
613 addfield(map, &desc, "", pmu, NULL);
614 addfield(map, &desc, "", " ", NULL);
615 } else if (json_streq(map, field, "Filter")) {
616 addfield(map, &filter, "", "", val);
617 } else if (json_streq(map, field, "ScaleUnit")) {
618 addfield(map, &unit, "", "", val);
619 } else if (json_streq(map, field, "PerPkg")) {
620 addfield(map, &perpkg, "", "", val);
621 } else if (json_streq(map, field, "MetricName")) {
622 addfield(map, &metric_name, "", "", val);
623 } else if (json_streq(map, field, "MetricGroup")) {
624 addfield(map, &metric_group, "", "", val);
625 } else if (json_streq(map, field, "MetricExpr")) {
626 addfield(map, &metric_expr, "", "", val);
627 for (s = metric_expr; *s; s++)
629 } else if (json_streq(map, field, "ArchStdEvent")) {
630 addfield(map, &arch_std, "", "", val);
631 for (s = arch_std; *s; s++)
634 /* ignore unknown fields */
636 if (precise && desc && !strstr(desc, "(Precise Event)")) {
637 if (json_streq(map, precise, "2"))
638 addfield(map, &extra_desc, " ",
639 "(Must be precise)", NULL);
641 addfield(map, &extra_desc, " ",
642 "(Precise event)", NULL);
644 snprintf(buf, sizeof buf, "event=%#llx", eventcode);
645 addfield(map, &event, ",", buf, NULL);
646 if (desc && extra_desc)
647 addfield(map, &desc, " ", extra_desc, NULL);
648 if (long_desc && extra_desc)
649 addfield(map, &long_desc, " ", extra_desc, NULL);
651 addfield(map, &event, ",", filter, NULL);
653 addfield(map, &event, ",", msr->pname, msrval);
659 * An arch standard event is referenced, so try to
660 * fixup any unassigned values.
662 err = try_fixup(fn, arch_std, &event, &desc, &name,
663 &long_desc, &pmu, &filter, &perpkg,
664 &unit, &metric_expr, &metric_name,
665 &metric_group, eventcode);
669 err = func(data, name, real_event(name, event), desc, long_desc,
670 pmu, unit, perpkg, metric_expr, metric_name, metric_group);
690 EXPECT(tok - tokens == len, tok, "unexpected objects at end");
693 free_json(map, size, tokens);
697 static char *file_name_to_table_name(const char *fname)
706 * Ensure tablename starts with alphabetic character.
707 * Derive rest of table name from basename of the JSON file,
708 * replacing hyphens and stripping out .json suffix.
710 n = asprintf(&tblname, "pme_%s", fname);
712 pr_info("%s: asprintf() error %s for file %s\n", prog,
713 strerror(errno), fname);
717 for (i = 0; i < strlen(tblname); i++) {
720 if (c == '-' || c == '/')
725 } else if (!isalnum(c) && c != '_') {
726 char *tmp = strdup(fname);
727 pr_err("%s: Invalid character '%c' in file name %s\n",
728 prog, c, basename(tmp));
739 static void print_mapping_table_prefix(FILE *outfp)
741 fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n");
744 static void print_mapping_table_suffix(FILE *outfp)
747 * Print the terminating, NULL entry.
749 fprintf(outfp, "{\n");
750 fprintf(outfp, "\t.cpuid = 0,\n");
751 fprintf(outfp, "\t.version = 0,\n");
752 fprintf(outfp, "\t.type = 0,\n");
753 fprintf(outfp, "\t.table = 0,\n");
754 fprintf(outfp, "},\n");
756 /* and finally, the closing curly bracket for the struct */
757 fprintf(outfp, "};\n");
760 static int process_mapfile(FILE *outfp, char *fpath)
769 pr_info("%s: Processing mapfile %s\n", prog, fpath);
775 mapfp = fopen(fpath, "r");
777 pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
782 print_mapping_table_prefix(outfp);
784 /* Skip first line (header) */
785 p = fgets(line, n, mapfp);
791 char *cpuid, *version, *type, *fname;
794 p = fgets(line, n, mapfp);
798 if (line[0] == '#' || line[0] == '\n')
801 if (line[strlen(line)-1] != '\n') {
802 /* TODO Deal with lines longer than 16K */
803 pr_info("%s: Mapfile %s: line %d too long, aborting\n",
804 prog, fpath, line_num);
807 line[strlen(line)-1] = '\0';
809 cpuid = fixregex(strtok_r(p, ",", &save));
810 version = strtok_r(NULL, ",", &save);
811 fname = strtok_r(NULL, ",", &save);
812 type = strtok_r(NULL, ",", &save);
814 tblname = file_name_to_table_name(fname);
815 fprintf(outfp, "{\n");
816 fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
817 fprintf(outfp, "\t.version = \"%s\",\n", version);
818 fprintf(outfp, "\t.type = \"%s\",\n", type);
821 * CHECK: We can't use the type (eg "core") field in the
822 * table name. For us to do that, we need to somehow tweak
823 * the other caller of file_name_to_table(), process_json()
824 * to determine the type. process_json() file has no way
825 * of knowing these are "core" events unless file name has
826 * core in it. If filename has core in it, we can safely
827 * ignore the type field here also.
829 fprintf(outfp, "\t.table = %s\n", tblname);
830 fprintf(outfp, "},\n");
834 print_mapping_table_suffix(outfp);
839 * If we fail to locate/process JSON and map files, create a NULL mapping
840 * table. This would at least allow perf to build even if we can't find/use
843 static void create_empty_mapping(const char *output_file)
847 pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
849 /* Truncate file to clear any partial writes to it */
850 outfp = fopen(output_file, "w");
856 fprintf(outfp, "#include \"pmu-events/pmu-events.h\"\n");
857 print_mapping_table_prefix(outfp);
858 print_mapping_table_suffix(outfp);
862 static int get_maxfds(void)
866 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
867 if (rlim.rlim_max == RLIM_INFINITY)
869 return min((unsigned)rlim.rlim_max / 2, 512);
876 * nftw() doesn't let us pass an argument to the processing function,
877 * so use a global variables.
879 static FILE *eventsfp;
880 static char *mapfile;
882 static int is_leaf_dir(const char *fpath)
892 while ((dir = readdir(d)) != NULL) {
893 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
896 if (dir->d_type == DT_DIR) {
899 } else if (dir->d_type == DT_UNKNOWN) {
903 sprintf(path, "%s/%s", fpath, dir->d_name);
907 if (S_ISDIR(st.st_mode)) {
919 static int is_json_file(const char *name)
923 if (strlen(name) < 5)
926 suffix = name + strlen(name) - 5;
928 if (strncmp(suffix, ".json", 5) == 0)
933 static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
934 int typeflag, struct FTW *ftwbuf)
936 int level = ftwbuf->level;
937 int is_file = typeflag == FTW_F;
939 if (level == 1 && is_file && is_json_file(fpath))
940 return json_events(fpath, save_arch_std_events, (void *)(uintptr_t)sb);
945 static int process_one_file(const char *fpath, const struct stat *sb,
946 int typeflag, struct FTW *ftwbuf)
950 int is_dir = typeflag == FTW_D;
951 int is_file = typeflag == FTW_F;
952 int level = ftwbuf->level;
955 if (level == 2 && is_dir) {
957 * For level 2 directory, bname will include parent name,
958 * like vendor/platform. So search back from platform dir
961 bname = fpath + ftwbuf->base - 2;
969 bname = fpath + ftwbuf->base;
971 pr_debug("%s %d %7jd %-20s %s\n",
972 is_file ? "f" : is_dir ? "d" : "x",
973 level, sb->st_size, bname, fpath);
975 /* base dir or too deep */
976 if (level == 0 || level > 3)
980 /* model directory, reset topic */
981 if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
982 (level == 2 && is_dir)) {
984 print_events_table_suffix(eventsfp);
987 * Drop file name suffix. Replace hyphens with underscores.
988 * Fail if file name contains any alphanum characters besides
991 tblname = file_name_to_table_name(bname);
993 pr_info("%s: Error determining table name for %s\n", prog,
998 print_events_table_prefix(eventsfp, tblname);
1003 * Save the mapfile name for now. We will process mapfile
1004 * after processing all JSON files (so we can write out the
1005 * mapping table after all PMU events tables).
1008 if (level == 1 && is_file) {
1009 if (!strcmp(bname, "mapfile.csv")) {
1010 mapfile = strdup(fpath);
1014 pr_info("%s: Ignoring file %s\n", prog, fpath);
1019 * If the file name does not have a .json extension,
1020 * ignore it. It could be a readme.txt for instance.
1023 if (!is_json_file(bname)) {
1024 pr_info("%s: Ignoring file without .json suffix %s\n", prog,
1030 if (level > 1 && add_topic(bname))
1034 * Assume all other files are JSON files.
1036 * If mapfile refers to 'power7_core.json', we create a table
1037 * named 'power7_core'. Any inconsistencies between the mapfile
1038 * and directory tree could result in build failure due to table
1039 * names not being found.
1041 * Atleast for now, be strict with processing JSON file names.
1042 * i.e. if JSON file name cannot be mapped to C-style table name,
1046 struct perf_entry_data data = {
1047 .topic = get_topic(),
1051 err = json_events(fpath, print_events_table_entry, &data);
1060 #define PATH_MAX 4096
1064 * Starting in directory 'start_dirname', find the "mapfile.csv" and
1065 * the set of JSON files for the architecture 'arch'.
1067 * From each JSON file, create a C-style "PMU events table" from the
1068 * JSON file (see struct pmu_event).
1070 * From the mapfile, create a mapping between the CPU revisions and
1071 * PMU event tables (see struct pmu_events_map).
1073 * Write out the PMU events tables and the mapping table to pmu-event.c.
1075 int main(int argc, char *argv[])
1079 char ldirname[PATH_MAX];
1082 const char *output_file;
1083 const char *start_dirname;
1086 prog = basename(argv[0]);
1088 pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
1093 start_dirname = argv[2];
1094 output_file = argv[3];
1097 verbose = atoi(argv[4]);
1099 eventsfp = fopen(output_file, "w");
1101 pr_err("%s Unable to create required file %s (%s)\n",
1102 prog, output_file, strerror(errno));
1106 sprintf(ldirname, "%s/%s", start_dirname, arch);
1108 /* If architecture does not have any event lists, bail out */
1109 if (stat(ldirname, &stbuf) < 0) {
1110 pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
1114 /* Include pmu-events.h first */
1115 fprintf(eventsfp, "#include \"pmu-events/pmu-events.h\"\n");
1118 * The mapfile allows multiple CPUids to point to the same JSON file,
1119 * so, not sure if there is a need for symlinks within the pmu-events
1122 * For now, treat symlinks of JSON files as regular files and create
1123 * separate tables for each symlink (presumably, each symlink refers
1124 * to specific version of the CPU).
1127 maxfds = get_maxfds();
1129 rc = nftw_ordered(ldirname, preprocess_arch_std_files, maxfds, 0);
1130 if (rc && verbose) {
1131 pr_info("%s: Error preprocessing arch standard files %s: %s\n",
1132 prog, ldirname, strerror(errno));
1134 } else if (rc < 0) {
1135 /* Make build fail */
1136 free_arch_std_events();
1142 rc = nftw_ordered(ldirname, process_one_file, maxfds, 0);
1143 if (rc && verbose) {
1144 pr_info("%s: Error walking file tree %s\n", prog, ldirname);
1146 } else if (rc < 0) {
1147 /* Make build fail */
1148 free_arch_std_events();
1155 print_events_table_suffix(eventsfp);
1158 pr_info("%s: No CPU->JSON mapping?\n", prog);
1162 if (process_mapfile(eventsfp, mapfile)) {
1163 pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
1164 /* Make build fail */
1172 create_empty_mapping(output_file);
1173 free_arch_std_events();
1180 fts_compare(const FTSENT * const *a, const FTSENT * const *b)
1182 return (strcmp((*a)->fts_name, (*b)->fts_name));
1186 nftw_ordered(const char *path, int (*fn)(const char *, const struct stat *, int,
1187 struct FTW *), int nfds, int ftwflags)
1189 char * const paths[2] = { (char *)path, NULL };
1193 int error = 0, ftsflags, fnflag, postorder, sverrno;
1195 /* XXX - nfds is currently unused */
1201 ftsflags = FTS_COMFOLLOW;
1202 if (!(ftwflags & FTW_CHDIR))
1203 ftsflags |= FTS_NOCHDIR;
1204 if (ftwflags & FTW_MOUNT)
1205 ftsflags |= FTS_XDEV;
1206 if (ftwflags & FTW_PHYS)
1207 ftsflags |= FTS_PHYSICAL;
1209 ftsflags |= FTS_LOGICAL;
1210 postorder = (ftwflags & FTW_DEPTH) != 0;
1211 ftsp = fts_open(paths, ftsflags, fts_compare);
1214 while ((cur = fts_read(ftsp)) != NULL) {
1215 switch (cur->fts_info) {
1249 ftw.base = cur->fts_pathlen - cur->fts_namelen;
1250 ftw.level = cur->fts_level;
1251 error = fn(cur->fts_path, cur->fts_statp, fnflag, &ftw);
1257 if (fts_close(ftsp) != 0 && error == 0)