jevents.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. #define _XOPEN_SOURCE 500 /* needed for nftw() */
  2. #define _GNU_SOURCE /* needed for asprintf() */
  3. /* Parse event JSON files */
  4. /*
  5. * Copyright (c) 2014, Intel Corporation
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  27. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  29. * OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <errno.h>
  34. #include <string.h>
  35. #include <ctype.h>
  36. #include <unistd.h>
  37. #include <stdarg.h>
  38. #include <libgen.h>
  39. #include <dirent.h>
  40. #include <sys/time.h> /* getrlimit */
  41. #include <sys/resource.h> /* getrlimit */
  42. #include <ftw.h>
  43. #include <sys/stat.h>
  44. #include "jsmn.h"
  45. #include "json.h"
  46. #include "jevents.h"
  47. #ifndef __maybe_unused
  48. #define __maybe_unused __attribute__((unused))
  49. #endif
  50. int verbose;
  51. char *prog;
  52. int eprintf(int level, int var, const char *fmt, ...)
  53. {
  54. int ret;
  55. va_list args;
  56. if (var < level)
  57. return 0;
  58. va_start(args, fmt);
  59. ret = vfprintf(stderr, fmt, args);
  60. va_end(args);
  61. return ret;
  62. }
  63. __attribute__((weak)) char *get_cpu_str(void)
  64. {
  65. return NULL;
  66. }
  67. static void addfield(char *map, char **dst, const char *sep,
  68. const char *a, jsmntok_t *bt)
  69. {
  70. unsigned int len = strlen(a) + 1 + strlen(sep);
  71. int olen = *dst ? strlen(*dst) : 0;
  72. int blen = bt ? json_len(bt) : 0;
  73. char *out;
  74. out = realloc(*dst, len + olen + blen);
  75. if (!out) {
  76. /* Don't add field in this case */
  77. return;
  78. }
  79. *dst = out;
  80. if (!olen)
  81. *(*dst) = 0;
  82. else
  83. strcat(*dst, sep);
  84. strcat(*dst, a);
  85. if (bt)
  86. strncat(*dst, map + bt->start, blen);
  87. }
  88. static void fixname(char *s)
  89. {
  90. for (; *s; s++)
  91. *s = tolower(*s);
  92. }
  93. static void fixdesc(char *s)
  94. {
  95. char *e = s + strlen(s);
  96. /* Remove trailing dots that look ugly in perf list */
  97. --e;
  98. while (e >= s && isspace(*e))
  99. --e;
  100. if (*e == '.')
  101. *e = 0;
  102. }
  103. static struct msrmap {
  104. const char *num;
  105. const char *pname;
  106. } msrmap[] = {
  107. { "0x3F6", "ldlat=" },
  108. { "0x1A6", "offcore_rsp=" },
  109. { "0x1A7", "offcore_rsp=" },
  110. { "0x3F7", "frontend=" },
  111. { NULL, NULL }
  112. };
  113. static struct field {
  114. const char *field;
  115. const char *kernel;
  116. } fields[] = {
  117. { "EventCode", "event=" },
  118. { "UMask", "umask=" },
  119. { "CounterMask", "cmask=" },
  120. { "Invert", "inv=" },
  121. { "AnyThread", "any=" },
  122. { "EdgeDetect", "edge=" },
  123. { "SampleAfterValue", "period=" },
  124. { NULL, NULL }
  125. };
  126. static void cut_comma(char *map, jsmntok_t *newval)
  127. {
  128. int i;
  129. /* Cut off everything after comma */
  130. for (i = newval->start; i < newval->end; i++) {
  131. if (map[i] == ',')
  132. newval->end = i;
  133. }
  134. }
  135. static int match_field(char *map, jsmntok_t *field, int nz,
  136. char **event, jsmntok_t *val)
  137. {
  138. struct field *f;
  139. jsmntok_t newval = *val;
  140. for (f = fields; f->field; f++)
  141. if (json_streq(map, field, f->field) && nz) {
  142. cut_comma(map, &newval);
  143. addfield(map, event, ",", f->kernel, &newval);
  144. return 1;
  145. }
  146. return 0;
  147. }
  148. static struct msrmap *lookup_msr(char *map, jsmntok_t *val)
  149. {
  150. jsmntok_t newval = *val;
  151. static bool warned;
  152. int i;
  153. cut_comma(map, &newval);
  154. for (i = 0; msrmap[i].num; i++)
  155. if (json_streq(map, &newval, msrmap[i].num))
  156. return &msrmap[i];
  157. if (!warned) {
  158. warned = true;
  159. pr_err("%s: Unknown MSR in event file %.*s\n", prog,
  160. json_len(val), map + val->start);
  161. }
  162. return NULL;
  163. }
  164. #define EXPECT(e, t, m) do { if (!(e)) { \
  165. jsmntok_t *loc = (t); \
  166. if (!(t)->start && (t) > tokens) \
  167. loc = (t) - 1; \
  168. pr_err("%s:%d: " m ", got %s\n", fn, \
  169. json_line(map, loc), \
  170. json_name(t)); \
  171. goto out_free; \
  172. } } while (0)
  173. #define TOPIC_DEPTH 256
  174. static char *topic_array[TOPIC_DEPTH];
  175. static int topic_level;
  176. static char *get_topic(void)
  177. {
  178. char *tp_old, *tp = NULL;
  179. int i;
  180. for (i = 0; i < topic_level + 1; i++) {
  181. int n;
  182. tp_old = tp;
  183. n = asprintf(&tp, "%s%s", tp ?: "", topic_array[i]);
  184. if (n < 0) {
  185. pr_info("%s: asprintf() error %s\n", prog);
  186. return NULL;
  187. }
  188. free(tp_old);
  189. }
  190. for (i = 0; i < (int) strlen(tp); i++) {
  191. char c = tp[i];
  192. if (c == '-')
  193. tp[i] = ' ';
  194. else if (c == '.') {
  195. tp[i] = '\0';
  196. break;
  197. }
  198. }
  199. return tp;
  200. }
  201. static int add_topic(int level, char *bname)
  202. {
  203. char *topic;
  204. level -= 2;
  205. if (level >= TOPIC_DEPTH)
  206. return -EINVAL;
  207. topic = strdup(bname);
  208. if (!topic) {
  209. pr_info("%s: strdup() error %s for file %s\n", prog,
  210. strerror(errno), bname);
  211. return -ENOMEM;
  212. }
  213. free(topic_array[topic_level]);
  214. topic_array[topic_level] = topic;
  215. topic_level = level;
  216. return 0;
  217. }
  218. struct perf_entry_data {
  219. FILE *outfp;
  220. char *topic;
  221. };
  222. static int close_table;
  223. static void print_events_table_prefix(FILE *fp, const char *tblname)
  224. {
  225. fprintf(fp, "struct pmu_event %s[] = {\n", tblname);
  226. close_table = 1;
  227. }
  228. static int print_events_table_entry(void *data, char *name, char *event,
  229. char *desc, char *long_desc)
  230. {
  231. struct perf_entry_data *pd = data;
  232. FILE *outfp = pd->outfp;
  233. char *topic = pd->topic;
  234. /*
  235. * TODO: Remove formatting chars after debugging to reduce
  236. * string lengths.
  237. */
  238. fprintf(outfp, "{\n");
  239. fprintf(outfp, "\t.name = \"%s\",\n", name);
  240. fprintf(outfp, "\t.event = \"%s\",\n", event);
  241. fprintf(outfp, "\t.desc = \"%s\",\n", desc);
  242. fprintf(outfp, "\t.topic = \"%s\",\n", topic);
  243. if (long_desc && long_desc[0])
  244. fprintf(outfp, "\t.long_desc = \"%s\",\n", long_desc);
  245. fprintf(outfp, "},\n");
  246. return 0;
  247. }
  248. static void print_events_table_suffix(FILE *outfp)
  249. {
  250. fprintf(outfp, "{\n");
  251. fprintf(outfp, "\t.name = 0,\n");
  252. fprintf(outfp, "\t.event = 0,\n");
  253. fprintf(outfp, "\t.desc = 0,\n");
  254. fprintf(outfp, "},\n");
  255. fprintf(outfp, "};\n");
  256. close_table = 0;
  257. }
  258. static struct fixed {
  259. const char *name;
  260. const char *event;
  261. } fixed[] = {
  262. { "inst_retired.any", "event=0xc0" },
  263. { "inst_retired.any_p", "event=0xc0" },
  264. { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03" },
  265. { "cpu_clk_unhalted.thread", "event=0x3c" },
  266. { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1" },
  267. { NULL, NULL},
  268. };
  269. /*
  270. * Handle different fixed counter encodings between JSON and perf.
  271. */
  272. static char *real_event(const char *name, char *event)
  273. {
  274. int i;
  275. for (i = 0; fixed[i].name; i++)
  276. if (!strcasecmp(name, fixed[i].name))
  277. return (char *)fixed[i].event;
  278. return event;
  279. }
  280. /* Call func with each event in the json file */
  281. int json_events(const char *fn,
  282. int (*func)(void *data, char *name, char *event, char *desc,
  283. char *long_desc),
  284. void *data)
  285. {
  286. int err = -EIO;
  287. size_t size;
  288. jsmntok_t *tokens, *tok;
  289. int i, j, len;
  290. char *map;
  291. if (!fn)
  292. return -ENOENT;
  293. tokens = parse_json(fn, &map, &size, &len);
  294. if (!tokens)
  295. return -EIO;
  296. EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
  297. tok = tokens + 1;
  298. for (i = 0; i < tokens->size; i++) {
  299. char *event = NULL, *desc = NULL, *name = NULL;
  300. char *long_desc = NULL;
  301. char *extra_desc = NULL;
  302. struct msrmap *msr = NULL;
  303. jsmntok_t *msrval = NULL;
  304. jsmntok_t *precise = NULL;
  305. jsmntok_t *obj = tok++;
  306. EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
  307. for (j = 0; j < obj->size; j += 2) {
  308. jsmntok_t *field, *val;
  309. int nz;
  310. field = tok + j;
  311. EXPECT(field->type == JSMN_STRING, tok + j,
  312. "Expected field name");
  313. val = tok + j + 1;
  314. EXPECT(val->type == JSMN_STRING, tok + j + 1,
  315. "Expected string value");
  316. nz = !json_streq(map, val, "0");
  317. if (match_field(map, field, nz, &event, val)) {
  318. /* ok */
  319. } else if (json_streq(map, field, "EventName")) {
  320. addfield(map, &name, "", "", val);
  321. } else if (json_streq(map, field, "BriefDescription")) {
  322. addfield(map, &desc, "", "", val);
  323. fixdesc(desc);
  324. } else if (json_streq(map, field,
  325. "PublicDescription")) {
  326. addfield(map, &long_desc, "", "", val);
  327. fixdesc(long_desc);
  328. } else if (json_streq(map, field, "PEBS") && nz) {
  329. precise = val;
  330. } else if (json_streq(map, field, "MSRIndex") && nz) {
  331. msr = lookup_msr(map, val);
  332. } else if (json_streq(map, field, "MSRValue")) {
  333. msrval = val;
  334. } else if (json_streq(map, field, "Errata") &&
  335. !json_streq(map, val, "null")) {
  336. addfield(map, &extra_desc, ". ",
  337. " Spec update: ", val);
  338. } else if (json_streq(map, field, "Data_LA") && nz) {
  339. addfield(map, &extra_desc, ". ",
  340. " Supports address when precise",
  341. NULL);
  342. }
  343. /* ignore unknown fields */
  344. }
  345. if (precise && desc && !strstr(desc, "(Precise Event)")) {
  346. if (json_streq(map, precise, "2"))
  347. addfield(map, &extra_desc, " ",
  348. "(Must be precise)", NULL);
  349. else
  350. addfield(map, &extra_desc, " ",
  351. "(Precise event)", NULL);
  352. }
  353. if (desc && extra_desc)
  354. addfield(map, &desc, " ", extra_desc, NULL);
  355. if (long_desc && extra_desc)
  356. addfield(map, &long_desc, " ", extra_desc, NULL);
  357. if (msr != NULL)
  358. addfield(map, &event, ",", msr->pname, msrval);
  359. fixname(name);
  360. err = func(data, name, real_event(name, event), desc, long_desc);
  361. free(event);
  362. free(desc);
  363. free(name);
  364. free(long_desc);
  365. free(extra_desc);
  366. if (err)
  367. break;
  368. tok += j;
  369. }
  370. EXPECT(tok - tokens == len, tok, "unexpected objects at end");
  371. err = 0;
  372. out_free:
  373. free_json(map, size, tokens);
  374. return err;
  375. }
  376. static char *file_name_to_table_name(char *fname)
  377. {
  378. unsigned int i;
  379. int n;
  380. int c;
  381. char *tblname;
  382. /*
  383. * Ensure tablename starts with alphabetic character.
  384. * Derive rest of table name from basename of the JSON file,
  385. * replacing hyphens and stripping out .json suffix.
  386. */
  387. n = asprintf(&tblname, "pme_%s", basename(fname));
  388. if (n < 0) {
  389. pr_info("%s: asprintf() error %s for file %s\n", prog,
  390. strerror(errno), fname);
  391. return NULL;
  392. }
  393. for (i = 0; i < strlen(tblname); i++) {
  394. c = tblname[i];
  395. if (c == '-')
  396. tblname[i] = '_';
  397. else if (c == '.') {
  398. tblname[i] = '\0';
  399. break;
  400. } else if (!isalnum(c) && c != '_') {
  401. pr_err("%s: Invalid character '%c' in file name %s\n",
  402. prog, c, basename(fname));
  403. free(tblname);
  404. tblname = NULL;
  405. break;
  406. }
  407. }
  408. return tblname;
  409. }
  410. static void print_mapping_table_prefix(FILE *outfp)
  411. {
  412. fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n");
  413. }
  414. static void print_mapping_table_suffix(FILE *outfp)
  415. {
  416. /*
  417. * Print the terminating, NULL entry.
  418. */
  419. fprintf(outfp, "{\n");
  420. fprintf(outfp, "\t.cpuid = 0,\n");
  421. fprintf(outfp, "\t.version = 0,\n");
  422. fprintf(outfp, "\t.type = 0,\n");
  423. fprintf(outfp, "\t.table = 0,\n");
  424. fprintf(outfp, "},\n");
  425. /* and finally, the closing curly bracket for the struct */
  426. fprintf(outfp, "};\n");
  427. }
  428. static int process_mapfile(FILE *outfp, char *fpath)
  429. {
  430. int n = 16384;
  431. FILE *mapfp;
  432. char *save = NULL;
  433. char *line, *p;
  434. int line_num;
  435. char *tblname;
  436. pr_info("%s: Processing mapfile %s\n", prog, fpath);
  437. line = malloc(n);
  438. if (!line)
  439. return -1;
  440. mapfp = fopen(fpath, "r");
  441. if (!mapfp) {
  442. pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
  443. fpath);
  444. return -1;
  445. }
  446. print_mapping_table_prefix(outfp);
  447. /* Skip first line (header) */
  448. p = fgets(line, n, mapfp);
  449. if (!p)
  450. goto out;
  451. line_num = 1;
  452. while (1) {
  453. char *cpuid, *version, *type, *fname;
  454. line_num++;
  455. p = fgets(line, n, mapfp);
  456. if (!p)
  457. break;
  458. if (line[0] == '#' || line[0] == '\n')
  459. continue;
  460. if (line[strlen(line)-1] != '\n') {
  461. /* TODO Deal with lines longer than 16K */
  462. pr_info("%s: Mapfile %s: line %d too long, aborting\n",
  463. prog, fpath, line_num);
  464. return -1;
  465. }
  466. line[strlen(line)-1] = '\0';
  467. cpuid = strtok_r(p, ",", &save);
  468. version = strtok_r(NULL, ",", &save);
  469. fname = strtok_r(NULL, ",", &save);
  470. type = strtok_r(NULL, ",", &save);
  471. tblname = file_name_to_table_name(fname);
  472. fprintf(outfp, "{\n");
  473. fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
  474. fprintf(outfp, "\t.version = \"%s\",\n", version);
  475. fprintf(outfp, "\t.type = \"%s\",\n", type);
  476. /*
  477. * CHECK: We can't use the type (eg "core") field in the
  478. * table name. For us to do that, we need to somehow tweak
  479. * the other caller of file_name_to_table(), process_json()
  480. * to determine the type. process_json() file has no way
  481. * of knowing these are "core" events unless file name has
  482. * core in it. If filename has core in it, we can safely
  483. * ignore the type field here also.
  484. */
  485. fprintf(outfp, "\t.table = %s\n", tblname);
  486. fprintf(outfp, "},\n");
  487. }
  488. out:
  489. print_mapping_table_suffix(outfp);
  490. return 0;
  491. }
  492. /*
  493. * If we fail to locate/process JSON and map files, create a NULL mapping
  494. * table. This would at least allow perf to build even if we can't find/use
  495. * the aliases.
  496. */
  497. static void create_empty_mapping(const char *output_file)
  498. {
  499. FILE *outfp;
  500. pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
  501. /* Truncate file to clear any partial writes to it */
  502. outfp = fopen(output_file, "w");
  503. if (!outfp) {
  504. perror("fopen()");
  505. _Exit(1);
  506. }
  507. fprintf(outfp, "#include \"../../pmu-events/pmu-events.h\"\n");
  508. print_mapping_table_prefix(outfp);
  509. print_mapping_table_suffix(outfp);
  510. fclose(outfp);
  511. }
  512. static int get_maxfds(void)
  513. {
  514. struct rlimit rlim;
  515. if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
  516. return min((int)rlim.rlim_max / 2, 512);
  517. return 512;
  518. }
  519. /*
  520. * nftw() doesn't let us pass an argument to the processing function,
  521. * so use a global variables.
  522. */
  523. static FILE *eventsfp;
  524. static char *mapfile;
  525. static int process_one_file(const char *fpath, const struct stat *sb,
  526. int typeflag, struct FTW *ftwbuf)
  527. {
  528. char *tblname, *bname = (char *) fpath + ftwbuf->base;
  529. int is_dir = typeflag == FTW_D;
  530. int is_file = typeflag == FTW_F;
  531. int level = ftwbuf->level;
  532. int err = 0;
  533. pr_debug("%s %d %7jd %-20s %s\n",
  534. is_file ? "f" : is_dir ? "d" : "x",
  535. level, sb->st_size, bname, fpath);
  536. /* base dir */
  537. if (level == 0)
  538. return 0;
  539. /* model directory, reset topic */
  540. if (level == 1 && is_dir) {
  541. if (close_table)
  542. print_events_table_suffix(eventsfp);
  543. /*
  544. * Drop file name suffix. Replace hyphens with underscores.
  545. * Fail if file name contains any alphanum characters besides
  546. * underscores.
  547. */
  548. tblname = file_name_to_table_name(bname);
  549. if (!tblname) {
  550. pr_info("%s: Error determining table name for %s\n", prog,
  551. bname);
  552. return -1;
  553. }
  554. print_events_table_prefix(eventsfp, tblname);
  555. return 0;
  556. }
  557. /*
  558. * Save the mapfile name for now. We will process mapfile
  559. * after processing all JSON files (so we can write out the
  560. * mapping table after all PMU events tables).
  561. *
  562. * TODO: Allow for multiple mapfiles? Punt for now.
  563. */
  564. if (level == 1 && is_file) {
  565. if (!strncmp(bname, "mapfile.csv", 11)) {
  566. if (mapfile) {
  567. pr_info("%s: Many mapfiles? Using %s, ignoring %s\n",
  568. prog, mapfile, fpath);
  569. } else {
  570. mapfile = strdup(fpath);
  571. }
  572. return 0;
  573. }
  574. pr_info("%s: Ignoring file %s\n", prog, fpath);
  575. return 0;
  576. }
  577. /*
  578. * If the file name does not have a .json extension,
  579. * ignore it. It could be a readme.txt for instance.
  580. */
  581. if (is_file) {
  582. char *suffix = bname + strlen(bname) - 5;
  583. if (strncmp(suffix, ".json", 5)) {
  584. pr_info("%s: Ignoring file without .json suffix %s\n", prog,
  585. fpath);
  586. return 0;
  587. }
  588. }
  589. if (level > 1 && add_topic(level, bname))
  590. return -ENOMEM;
  591. /*
  592. * Assume all other files are JSON files.
  593. *
  594. * If mapfile refers to 'power7_core.json', we create a table
  595. * named 'power7_core'. Any inconsistencies between the mapfile
  596. * and directory tree could result in build failure due to table
  597. * names not being found.
  598. *
  599. * Atleast for now, be strict with processing JSON file names.
  600. * i.e. if JSON file name cannot be mapped to C-style table name,
  601. * fail.
  602. */
  603. if (is_file) {
  604. struct perf_entry_data data = {
  605. .topic = get_topic(),
  606. .outfp = eventsfp,
  607. };
  608. err = json_events(fpath, print_events_table_entry, &data);
  609. free(data.topic);
  610. }
  611. return err;
  612. }
  613. #ifndef PATH_MAX
  614. #define PATH_MAX 4096
  615. #endif
  616. /*
  617. * Starting in directory 'start_dirname', find the "mapfile.csv" and
  618. * the set of JSON files for the architecture 'arch'.
  619. *
  620. * From each JSON file, create a C-style "PMU events table" from the
  621. * JSON file (see struct pmu_event).
  622. *
  623. * From the mapfile, create a mapping between the CPU revisions and
  624. * PMU event tables (see struct pmu_events_map).
  625. *
  626. * Write out the PMU events tables and the mapping table to pmu-event.c.
  627. *
  628. * If unable to process the JSON or arch files, create an empty mapping
  629. * table so we can continue to build/use perf even if we cannot use the
  630. * PMU event aliases.
  631. */
  632. int main(int argc, char *argv[])
  633. {
  634. int rc;
  635. int maxfds;
  636. char ldirname[PATH_MAX];
  637. const char *arch;
  638. const char *output_file;
  639. const char *start_dirname;
  640. prog = basename(argv[0]);
  641. if (argc < 4) {
  642. pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
  643. return 1;
  644. }
  645. arch = argv[1];
  646. start_dirname = argv[2];
  647. output_file = argv[3];
  648. if (argc > 4)
  649. verbose = atoi(argv[4]);
  650. eventsfp = fopen(output_file, "w");
  651. if (!eventsfp) {
  652. pr_err("%s Unable to create required file %s (%s)\n",
  653. prog, output_file, strerror(errno));
  654. return 2;
  655. }
  656. /* Include pmu-events.h first */
  657. fprintf(eventsfp, "#include \"../../pmu-events/pmu-events.h\"\n");
  658. sprintf(ldirname, "%s/%s", start_dirname, arch);
  659. /*
  660. * The mapfile allows multiple CPUids to point to the same JSON file,
  661. * so, not sure if there is a need for symlinks within the pmu-events
  662. * directory.
  663. *
  664. * For now, treat symlinks of JSON files as regular files and create
  665. * separate tables for each symlink (presumably, each symlink refers
  666. * to specific version of the CPU).
  667. */
  668. maxfds = get_maxfds();
  669. mapfile = NULL;
  670. rc = nftw(ldirname, process_one_file, maxfds, 0);
  671. if (rc && verbose) {
  672. pr_info("%s: Error walking file tree %s\n", prog, ldirname);
  673. goto empty_map;
  674. } else if (rc) {
  675. goto empty_map;
  676. }
  677. if (close_table)
  678. print_events_table_suffix(eventsfp);
  679. if (!mapfile) {
  680. pr_info("%s: No CPU->JSON mapping?\n", prog);
  681. goto empty_map;
  682. }
  683. if (process_mapfile(eventsfp, mapfile)) {
  684. pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
  685. goto empty_map;
  686. }
  687. return 0;
  688. empty_map:
  689. fclose(eventsfp);
  690. create_empty_mapping(output_file);
  691. return 0;
  692. }