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,period=2000003" },
  263. { "inst_retired.any_p", "event=0xc0,period=2000003" },
  264. { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03,period=2000003" },
  265. { "cpu_clk_unhalted.thread", "event=0x3c,period=2000003" },
  266. { "cpu_clk_unhalted.core", "event=0x3c,period=2000003" },
  267. { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1,period=2000003" },
  268. { NULL, NULL},
  269. };
  270. /*
  271. * Handle different fixed counter encodings between JSON and perf.
  272. */
  273. static char *real_event(const char *name, char *event)
  274. {
  275. int i;
  276. for (i = 0; fixed[i].name; i++)
  277. if (!strcasecmp(name, fixed[i].name))
  278. return (char *)fixed[i].event;
  279. return event;
  280. }
  281. /* Call func with each event in the json file */
  282. int json_events(const char *fn,
  283. int (*func)(void *data, char *name, char *event, char *desc,
  284. char *long_desc),
  285. void *data)
  286. {
  287. int err = -EIO;
  288. size_t size;
  289. jsmntok_t *tokens, *tok;
  290. int i, j, len;
  291. char *map;
  292. if (!fn)
  293. return -ENOENT;
  294. tokens = parse_json(fn, &map, &size, &len);
  295. if (!tokens)
  296. return -EIO;
  297. EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
  298. tok = tokens + 1;
  299. for (i = 0; i < tokens->size; i++) {
  300. char *event = NULL, *desc = NULL, *name = NULL;
  301. char *long_desc = NULL;
  302. char *extra_desc = NULL;
  303. struct msrmap *msr = NULL;
  304. jsmntok_t *msrval = NULL;
  305. jsmntok_t *precise = NULL;
  306. jsmntok_t *obj = tok++;
  307. EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
  308. for (j = 0; j < obj->size; j += 2) {
  309. jsmntok_t *field, *val;
  310. int nz;
  311. field = tok + j;
  312. EXPECT(field->type == JSMN_STRING, tok + j,
  313. "Expected field name");
  314. val = tok + j + 1;
  315. EXPECT(val->type == JSMN_STRING, tok + j + 1,
  316. "Expected string value");
  317. nz = !json_streq(map, val, "0");
  318. if (match_field(map, field, nz, &event, val)) {
  319. /* ok */
  320. } else if (json_streq(map, field, "EventName")) {
  321. addfield(map, &name, "", "", val);
  322. } else if (json_streq(map, field, "BriefDescription")) {
  323. addfield(map, &desc, "", "", val);
  324. fixdesc(desc);
  325. } else if (json_streq(map, field,
  326. "PublicDescription")) {
  327. addfield(map, &long_desc, "", "", val);
  328. fixdesc(long_desc);
  329. } else if (json_streq(map, field, "PEBS") && nz) {
  330. precise = val;
  331. } else if (json_streq(map, field, "MSRIndex") && nz) {
  332. msr = lookup_msr(map, val);
  333. } else if (json_streq(map, field, "MSRValue")) {
  334. msrval = val;
  335. } else if (json_streq(map, field, "Errata") &&
  336. !json_streq(map, val, "null")) {
  337. addfield(map, &extra_desc, ". ",
  338. " Spec update: ", val);
  339. } else if (json_streq(map, field, "Data_LA") && nz) {
  340. addfield(map, &extra_desc, ". ",
  341. " Supports address when precise",
  342. NULL);
  343. }
  344. /* ignore unknown fields */
  345. }
  346. if (precise && desc && !strstr(desc, "(Precise Event)")) {
  347. if (json_streq(map, precise, "2"))
  348. addfield(map, &extra_desc, " ",
  349. "(Must be precise)", NULL);
  350. else
  351. addfield(map, &extra_desc, " ",
  352. "(Precise event)", NULL);
  353. }
  354. if (desc && extra_desc)
  355. addfield(map, &desc, " ", extra_desc, NULL);
  356. if (long_desc && extra_desc)
  357. addfield(map, &long_desc, " ", extra_desc, NULL);
  358. if (msr != NULL)
  359. addfield(map, &event, ",", msr->pname, msrval);
  360. fixname(name);
  361. err = func(data, name, real_event(name, event), desc, long_desc);
  362. free(event);
  363. free(desc);
  364. free(name);
  365. free(long_desc);
  366. free(extra_desc);
  367. if (err)
  368. break;
  369. tok += j;
  370. }
  371. EXPECT(tok - tokens == len, tok, "unexpected objects at end");
  372. err = 0;
  373. out_free:
  374. free_json(map, size, tokens);
  375. return err;
  376. }
  377. static char *file_name_to_table_name(char *fname)
  378. {
  379. unsigned int i;
  380. int n;
  381. int c;
  382. char *tblname;
  383. /*
  384. * Ensure tablename starts with alphabetic character.
  385. * Derive rest of table name from basename of the JSON file,
  386. * replacing hyphens and stripping out .json suffix.
  387. */
  388. n = asprintf(&tblname, "pme_%s", basename(fname));
  389. if (n < 0) {
  390. pr_info("%s: asprintf() error %s for file %s\n", prog,
  391. strerror(errno), fname);
  392. return NULL;
  393. }
  394. for (i = 0; i < strlen(tblname); i++) {
  395. c = tblname[i];
  396. if (c == '-')
  397. tblname[i] = '_';
  398. else if (c == '.') {
  399. tblname[i] = '\0';
  400. break;
  401. } else if (!isalnum(c) && c != '_') {
  402. pr_err("%s: Invalid character '%c' in file name %s\n",
  403. prog, c, basename(fname));
  404. free(tblname);
  405. tblname = NULL;
  406. break;
  407. }
  408. }
  409. return tblname;
  410. }
  411. static void print_mapping_table_prefix(FILE *outfp)
  412. {
  413. fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n");
  414. }
  415. static void print_mapping_table_suffix(FILE *outfp)
  416. {
  417. /*
  418. * Print the terminating, NULL entry.
  419. */
  420. fprintf(outfp, "{\n");
  421. fprintf(outfp, "\t.cpuid = 0,\n");
  422. fprintf(outfp, "\t.version = 0,\n");
  423. fprintf(outfp, "\t.type = 0,\n");
  424. fprintf(outfp, "\t.table = 0,\n");
  425. fprintf(outfp, "},\n");
  426. /* and finally, the closing curly bracket for the struct */
  427. fprintf(outfp, "};\n");
  428. }
  429. static int process_mapfile(FILE *outfp, char *fpath)
  430. {
  431. int n = 16384;
  432. FILE *mapfp;
  433. char *save = NULL;
  434. char *line, *p;
  435. int line_num;
  436. char *tblname;
  437. pr_info("%s: Processing mapfile %s\n", prog, fpath);
  438. line = malloc(n);
  439. if (!line)
  440. return -1;
  441. mapfp = fopen(fpath, "r");
  442. if (!mapfp) {
  443. pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
  444. fpath);
  445. return -1;
  446. }
  447. print_mapping_table_prefix(outfp);
  448. /* Skip first line (header) */
  449. p = fgets(line, n, mapfp);
  450. if (!p)
  451. goto out;
  452. line_num = 1;
  453. while (1) {
  454. char *cpuid, *version, *type, *fname;
  455. line_num++;
  456. p = fgets(line, n, mapfp);
  457. if (!p)
  458. break;
  459. if (line[0] == '#' || line[0] == '\n')
  460. continue;
  461. if (line[strlen(line)-1] != '\n') {
  462. /* TODO Deal with lines longer than 16K */
  463. pr_info("%s: Mapfile %s: line %d too long, aborting\n",
  464. prog, fpath, line_num);
  465. return -1;
  466. }
  467. line[strlen(line)-1] = '\0';
  468. cpuid = strtok_r(p, ",", &save);
  469. version = strtok_r(NULL, ",", &save);
  470. fname = strtok_r(NULL, ",", &save);
  471. type = strtok_r(NULL, ",", &save);
  472. tblname = file_name_to_table_name(fname);
  473. fprintf(outfp, "{\n");
  474. fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
  475. fprintf(outfp, "\t.version = \"%s\",\n", version);
  476. fprintf(outfp, "\t.type = \"%s\",\n", type);
  477. /*
  478. * CHECK: We can't use the type (eg "core") field in the
  479. * table name. For us to do that, we need to somehow tweak
  480. * the other caller of file_name_to_table(), process_json()
  481. * to determine the type. process_json() file has no way
  482. * of knowing these are "core" events unless file name has
  483. * core in it. If filename has core in it, we can safely
  484. * ignore the type field here also.
  485. */
  486. fprintf(outfp, "\t.table = %s\n", tblname);
  487. fprintf(outfp, "},\n");
  488. }
  489. out:
  490. print_mapping_table_suffix(outfp);
  491. return 0;
  492. }
  493. /*
  494. * If we fail to locate/process JSON and map files, create a NULL mapping
  495. * table. This would at least allow perf to build even if we can't find/use
  496. * the aliases.
  497. */
  498. static void create_empty_mapping(const char *output_file)
  499. {
  500. FILE *outfp;
  501. pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
  502. /* Truncate file to clear any partial writes to it */
  503. outfp = fopen(output_file, "w");
  504. if (!outfp) {
  505. perror("fopen()");
  506. _Exit(1);
  507. }
  508. fprintf(outfp, "#include \"../../pmu-events/pmu-events.h\"\n");
  509. print_mapping_table_prefix(outfp);
  510. print_mapping_table_suffix(outfp);
  511. fclose(outfp);
  512. }
  513. static int get_maxfds(void)
  514. {
  515. struct rlimit rlim;
  516. if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
  517. return min((int)rlim.rlim_max / 2, 512);
  518. return 512;
  519. }
  520. /*
  521. * nftw() doesn't let us pass an argument to the processing function,
  522. * so use a global variables.
  523. */
  524. static FILE *eventsfp;
  525. static char *mapfile;
  526. static int process_one_file(const char *fpath, const struct stat *sb,
  527. int typeflag, struct FTW *ftwbuf)
  528. {
  529. char *tblname, *bname = (char *) fpath + ftwbuf->base;
  530. int is_dir = typeflag == FTW_D;
  531. int is_file = typeflag == FTW_F;
  532. int level = ftwbuf->level;
  533. int err = 0;
  534. pr_debug("%s %d %7jd %-20s %s\n",
  535. is_file ? "f" : is_dir ? "d" : "x",
  536. level, sb->st_size, bname, fpath);
  537. /* base dir */
  538. if (level == 0)
  539. return 0;
  540. /* model directory, reset topic */
  541. if (level == 1 && is_dir) {
  542. if (close_table)
  543. print_events_table_suffix(eventsfp);
  544. /*
  545. * Drop file name suffix. Replace hyphens with underscores.
  546. * Fail if file name contains any alphanum characters besides
  547. * underscores.
  548. */
  549. tblname = file_name_to_table_name(bname);
  550. if (!tblname) {
  551. pr_info("%s: Error determining table name for %s\n", prog,
  552. bname);
  553. return -1;
  554. }
  555. print_events_table_prefix(eventsfp, tblname);
  556. return 0;
  557. }
  558. /*
  559. * Save the mapfile name for now. We will process mapfile
  560. * after processing all JSON files (so we can write out the
  561. * mapping table after all PMU events tables).
  562. *
  563. * TODO: Allow for multiple mapfiles? Punt for now.
  564. */
  565. if (level == 1 && is_file) {
  566. if (!strncmp(bname, "mapfile.csv", 11)) {
  567. if (mapfile) {
  568. pr_info("%s: Many mapfiles? Using %s, ignoring %s\n",
  569. prog, mapfile, fpath);
  570. } else {
  571. mapfile = strdup(fpath);
  572. }
  573. return 0;
  574. }
  575. pr_info("%s: Ignoring file %s\n", prog, fpath);
  576. return 0;
  577. }
  578. /*
  579. * If the file name does not have a .json extension,
  580. * ignore it. It could be a readme.txt for instance.
  581. */
  582. if (is_file) {
  583. char *suffix = bname + strlen(bname) - 5;
  584. if (strncmp(suffix, ".json", 5)) {
  585. pr_info("%s: Ignoring file without .json suffix %s\n", prog,
  586. fpath);
  587. return 0;
  588. }
  589. }
  590. if (level > 1 && add_topic(level, bname))
  591. return -ENOMEM;
  592. /*
  593. * Assume all other files are JSON files.
  594. *
  595. * If mapfile refers to 'power7_core.json', we create a table
  596. * named 'power7_core'. Any inconsistencies between the mapfile
  597. * and directory tree could result in build failure due to table
  598. * names not being found.
  599. *
  600. * Atleast for now, be strict with processing JSON file names.
  601. * i.e. if JSON file name cannot be mapped to C-style table name,
  602. * fail.
  603. */
  604. if (is_file) {
  605. struct perf_entry_data data = {
  606. .topic = get_topic(),
  607. .outfp = eventsfp,
  608. };
  609. err = json_events(fpath, print_events_table_entry, &data);
  610. free(data.topic);
  611. }
  612. return err;
  613. }
  614. #ifndef PATH_MAX
  615. #define PATH_MAX 4096
  616. #endif
  617. /*
  618. * Starting in directory 'start_dirname', find the "mapfile.csv" and
  619. * the set of JSON files for the architecture 'arch'.
  620. *
  621. * From each JSON file, create a C-style "PMU events table" from the
  622. * JSON file (see struct pmu_event).
  623. *
  624. * From the mapfile, create a mapping between the CPU revisions and
  625. * PMU event tables (see struct pmu_events_map).
  626. *
  627. * Write out the PMU events tables and the mapping table to pmu-event.c.
  628. *
  629. * If unable to process the JSON or arch files, create an empty mapping
  630. * table so we can continue to build/use perf even if we cannot use the
  631. * PMU event aliases.
  632. */
  633. int main(int argc, char *argv[])
  634. {
  635. int rc;
  636. int maxfds;
  637. char ldirname[PATH_MAX];
  638. const char *arch;
  639. const char *output_file;
  640. const char *start_dirname;
  641. prog = basename(argv[0]);
  642. if (argc < 4) {
  643. pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
  644. return 1;
  645. }
  646. arch = argv[1];
  647. start_dirname = argv[2];
  648. output_file = argv[3];
  649. if (argc > 4)
  650. verbose = atoi(argv[4]);
  651. eventsfp = fopen(output_file, "w");
  652. if (!eventsfp) {
  653. pr_err("%s Unable to create required file %s (%s)\n",
  654. prog, output_file, strerror(errno));
  655. return 2;
  656. }
  657. /* Include pmu-events.h first */
  658. fprintf(eventsfp, "#include \"../../pmu-events/pmu-events.h\"\n");
  659. sprintf(ldirname, "%s/%s", start_dirname, arch);
  660. /*
  661. * The mapfile allows multiple CPUids to point to the same JSON file,
  662. * so, not sure if there is a need for symlinks within the pmu-events
  663. * directory.
  664. *
  665. * For now, treat symlinks of JSON files as regular files and create
  666. * separate tables for each symlink (presumably, each symlink refers
  667. * to specific version of the CPU).
  668. */
  669. maxfds = get_maxfds();
  670. mapfile = NULL;
  671. rc = nftw(ldirname, process_one_file, maxfds, 0);
  672. if (rc && verbose) {
  673. pr_info("%s: Error walking file tree %s\n", prog, ldirname);
  674. goto empty_map;
  675. } else if (rc) {
  676. goto empty_map;
  677. }
  678. if (close_table)
  679. print_events_table_suffix(eventsfp);
  680. if (!mapfile) {
  681. pr_info("%s: No CPU->JSON mapping?\n", prog);
  682. goto empty_map;
  683. }
  684. if (process_mapfile(eventsfp, mapfile)) {
  685. pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
  686. goto empty_map;
  687. }
  688. return 0;
  689. empty_map:
  690. fclose(eventsfp);
  691. create_empty_mapping(output_file);
  692. return 0;
  693. }