jevents.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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 <limits.h>
  40. #include <dirent.h>
  41. #include <sys/time.h> /* getrlimit */
  42. #include <sys/resource.h> /* getrlimit */
  43. #include <ftw.h>
  44. #include <sys/stat.h>
  45. #include <linux/list.h>
  46. #include "jsmn.h"
  47. #include "json.h"
  48. #include "jevents.h"
  49. int verbose;
  50. char *prog;
  51. int eprintf(int level, int var, const char *fmt, ...)
  52. {
  53. int ret;
  54. va_list args;
  55. if (var < level)
  56. return 0;
  57. va_start(args, fmt);
  58. ret = vfprintf(stderr, fmt, args);
  59. va_end(args);
  60. return ret;
  61. }
  62. __attribute__((weak)) char *get_cpu_str(void)
  63. {
  64. return NULL;
  65. }
  66. static void addfield(char *map, char **dst, const char *sep,
  67. const char *a, jsmntok_t *bt)
  68. {
  69. unsigned int len = strlen(a) + 1 + strlen(sep);
  70. int olen = *dst ? strlen(*dst) : 0;
  71. int blen = bt ? json_len(bt) : 0;
  72. char *out;
  73. out = realloc(*dst, len + olen + blen);
  74. if (!out) {
  75. /* Don't add field in this case */
  76. return;
  77. }
  78. *dst = out;
  79. if (!olen)
  80. *(*dst) = 0;
  81. else
  82. strcat(*dst, sep);
  83. strcat(*dst, a);
  84. if (bt)
  85. strncat(*dst, map + bt->start, blen);
  86. }
  87. static void fixname(char *s)
  88. {
  89. for (; *s; s++)
  90. *s = tolower(*s);
  91. }
  92. static void fixdesc(char *s)
  93. {
  94. char *e = s + strlen(s);
  95. /* Remove trailing dots that look ugly in perf list */
  96. --e;
  97. while (e >= s && isspace(*e))
  98. --e;
  99. if (*e == '.')
  100. *e = 0;
  101. }
  102. /* Add escapes for '\' so they are proper C strings. */
  103. static char *fixregex(char *s)
  104. {
  105. int len = 0;
  106. int esc_count = 0;
  107. char *fixed = NULL;
  108. char *p, *q;
  109. /* Count the number of '\' in string */
  110. for (p = s; *p; p++) {
  111. ++len;
  112. if (*p == '\\')
  113. ++esc_count;
  114. }
  115. if (esc_count == 0)
  116. return s;
  117. /* allocate space for a new string */
  118. fixed = (char *) malloc(len + 1);
  119. if (!fixed)
  120. return NULL;
  121. /* copy over the characters */
  122. q = fixed;
  123. for (p = s; *p; p++) {
  124. if (*p == '\\') {
  125. *q = '\\';
  126. ++q;
  127. }
  128. *q = *p;
  129. ++q;
  130. }
  131. *q = '\0';
  132. return fixed;
  133. }
  134. static struct msrmap {
  135. const char *num;
  136. const char *pname;
  137. } msrmap[] = {
  138. { "0x3F6", "ldlat=" },
  139. { "0x1A6", "offcore_rsp=" },
  140. { "0x1A7", "offcore_rsp=" },
  141. { "0x3F7", "frontend=" },
  142. { NULL, NULL }
  143. };
  144. static struct field {
  145. const char *field;
  146. const char *kernel;
  147. } fields[] = {
  148. { "UMask", "umask=" },
  149. { "CounterMask", "cmask=" },
  150. { "Invert", "inv=" },
  151. { "AnyThread", "any=" },
  152. { "EdgeDetect", "edge=" },
  153. { "SampleAfterValue", "period=" },
  154. { "FCMask", "fc_mask=" },
  155. { "PortMask", "ch_mask=" },
  156. { NULL, NULL }
  157. };
  158. static void cut_comma(char *map, jsmntok_t *newval)
  159. {
  160. int i;
  161. /* Cut off everything after comma */
  162. for (i = newval->start; i < newval->end; i++) {
  163. if (map[i] == ',')
  164. newval->end = i;
  165. }
  166. }
  167. static int match_field(char *map, jsmntok_t *field, int nz,
  168. char **event, jsmntok_t *val)
  169. {
  170. struct field *f;
  171. jsmntok_t newval = *val;
  172. for (f = fields; f->field; f++)
  173. if (json_streq(map, field, f->field) && nz) {
  174. cut_comma(map, &newval);
  175. addfield(map, event, ",", f->kernel, &newval);
  176. return 1;
  177. }
  178. return 0;
  179. }
  180. static struct msrmap *lookup_msr(char *map, jsmntok_t *val)
  181. {
  182. jsmntok_t newval = *val;
  183. static bool warned;
  184. int i;
  185. cut_comma(map, &newval);
  186. for (i = 0; msrmap[i].num; i++)
  187. if (json_streq(map, &newval, msrmap[i].num))
  188. return &msrmap[i];
  189. if (!warned) {
  190. warned = true;
  191. pr_err("%s: Unknown MSR in event file %.*s\n", prog,
  192. json_len(val), map + val->start);
  193. }
  194. return NULL;
  195. }
  196. static struct map {
  197. const char *json;
  198. const char *perf;
  199. } unit_to_pmu[] = {
  200. { "CBO", "uncore_cbox" },
  201. { "QPI LL", "uncore_qpi" },
  202. { "SBO", "uncore_sbox" },
  203. { "iMPH-U", "uncore_arb" },
  204. { "CPU-M-CF", "cpum_cf" },
  205. { "CPU-M-SF", "cpum_sf" },
  206. {}
  207. };
  208. static const char *field_to_perf(struct map *table, char *map, jsmntok_t *val)
  209. {
  210. int i;
  211. for (i = 0; table[i].json; i++) {
  212. if (json_streq(map, val, table[i].json))
  213. return table[i].perf;
  214. }
  215. return NULL;
  216. }
  217. #define EXPECT(e, t, m) do { if (!(e)) { \
  218. jsmntok_t *loc = (t); \
  219. if (!(t)->start && (t) > tokens) \
  220. loc = (t) - 1; \
  221. pr_err("%s:%d: " m ", got %s\n", fn, \
  222. json_line(map, loc), \
  223. json_name(t)); \
  224. err = -EIO; \
  225. goto out_free; \
  226. } } while (0)
  227. static char *topic;
  228. static char *get_topic(void)
  229. {
  230. char *tp;
  231. int i;
  232. /* tp is free'd in process_one_file() */
  233. i = asprintf(&tp, "%s", topic);
  234. if (i < 0) {
  235. pr_info("%s: asprintf() error %s\n", prog);
  236. return NULL;
  237. }
  238. for (i = 0; i < (int) strlen(tp); i++) {
  239. char c = tp[i];
  240. if (c == '-')
  241. tp[i] = ' ';
  242. else if (c == '.') {
  243. tp[i] = '\0';
  244. break;
  245. }
  246. }
  247. return tp;
  248. }
  249. static int add_topic(char *bname)
  250. {
  251. free(topic);
  252. topic = strdup(bname);
  253. if (!topic) {
  254. pr_info("%s: strdup() error %s for file %s\n", prog,
  255. strerror(errno), bname);
  256. return -ENOMEM;
  257. }
  258. return 0;
  259. }
  260. struct perf_entry_data {
  261. FILE *outfp;
  262. char *topic;
  263. };
  264. static int close_table;
  265. static void print_events_table_prefix(FILE *fp, const char *tblname)
  266. {
  267. fprintf(fp, "struct pmu_event %s[] = {\n", tblname);
  268. close_table = 1;
  269. }
  270. static int print_events_table_entry(void *data, char *name, char *event,
  271. char *desc, char *long_desc,
  272. char *pmu, char *unit, char *perpkg,
  273. char *metric_expr,
  274. char *metric_name, char *metric_group)
  275. {
  276. struct perf_entry_data *pd = data;
  277. FILE *outfp = pd->outfp;
  278. char *topic = pd->topic;
  279. /*
  280. * TODO: Remove formatting chars after debugging to reduce
  281. * string lengths.
  282. */
  283. fprintf(outfp, "{\n");
  284. if (name)
  285. fprintf(outfp, "\t.name = \"%s\",\n", name);
  286. if (event)
  287. fprintf(outfp, "\t.event = \"%s\",\n", event);
  288. fprintf(outfp, "\t.desc = \"%s\",\n", desc);
  289. fprintf(outfp, "\t.topic = \"%s\",\n", topic);
  290. if (long_desc && long_desc[0])
  291. fprintf(outfp, "\t.long_desc = \"%s\",\n", long_desc);
  292. if (pmu)
  293. fprintf(outfp, "\t.pmu = \"%s\",\n", pmu);
  294. if (unit)
  295. fprintf(outfp, "\t.unit = \"%s\",\n", unit);
  296. if (perpkg)
  297. fprintf(outfp, "\t.perpkg = \"%s\",\n", perpkg);
  298. if (metric_expr)
  299. fprintf(outfp, "\t.metric_expr = \"%s\",\n", metric_expr);
  300. if (metric_name)
  301. fprintf(outfp, "\t.metric_name = \"%s\",\n", metric_name);
  302. if (metric_group)
  303. fprintf(outfp, "\t.metric_group = \"%s\",\n", metric_group);
  304. fprintf(outfp, "},\n");
  305. return 0;
  306. }
  307. struct event_struct {
  308. struct list_head list;
  309. char *name;
  310. char *event;
  311. char *desc;
  312. char *long_desc;
  313. char *pmu;
  314. char *unit;
  315. char *perpkg;
  316. char *metric_expr;
  317. char *metric_name;
  318. char *metric_group;
  319. };
  320. #define ADD_EVENT_FIELD(field) do { if (field) { \
  321. es->field = strdup(field); \
  322. if (!es->field) \
  323. goto out_free; \
  324. } } while (0)
  325. #define FREE_EVENT_FIELD(field) free(es->field)
  326. #define TRY_FIXUP_FIELD(field) do { if (es->field && !*field) {\
  327. *field = strdup(es->field); \
  328. if (!*field) \
  329. return -ENOMEM; \
  330. } } while (0)
  331. #define FOR_ALL_EVENT_STRUCT_FIELDS(op) do { \
  332. op(name); \
  333. op(event); \
  334. op(desc); \
  335. op(long_desc); \
  336. op(pmu); \
  337. op(unit); \
  338. op(perpkg); \
  339. op(metric_expr); \
  340. op(metric_name); \
  341. op(metric_group); \
  342. } while (0)
  343. static LIST_HEAD(arch_std_events);
  344. static void free_arch_std_events(void)
  345. {
  346. struct event_struct *es, *next;
  347. list_for_each_entry_safe(es, next, &arch_std_events, list) {
  348. FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
  349. list_del(&es->list);
  350. free(es);
  351. }
  352. }
  353. static int save_arch_std_events(void *data, char *name, char *event,
  354. char *desc, char *long_desc, char *pmu,
  355. char *unit, char *perpkg, char *metric_expr,
  356. char *metric_name, char *metric_group)
  357. {
  358. struct event_struct *es;
  359. struct stat *sb = data;
  360. es = malloc(sizeof(*es));
  361. if (!es)
  362. return -ENOMEM;
  363. memset(es, 0, sizeof(*es));
  364. FOR_ALL_EVENT_STRUCT_FIELDS(ADD_EVENT_FIELD);
  365. list_add_tail(&es->list, &arch_std_events);
  366. return 0;
  367. out_free:
  368. FOR_ALL_EVENT_STRUCT_FIELDS(FREE_EVENT_FIELD);
  369. free(es);
  370. return -ENOMEM;
  371. }
  372. static void print_events_table_suffix(FILE *outfp)
  373. {
  374. fprintf(outfp, "{\n");
  375. fprintf(outfp, "\t.name = 0,\n");
  376. fprintf(outfp, "\t.event = 0,\n");
  377. fprintf(outfp, "\t.desc = 0,\n");
  378. fprintf(outfp, "},\n");
  379. fprintf(outfp, "};\n");
  380. close_table = 0;
  381. }
  382. static struct fixed {
  383. const char *name;
  384. const char *event;
  385. } fixed[] = {
  386. { "inst_retired.any", "event=0xc0,period=2000003" },
  387. { "inst_retired.any_p", "event=0xc0,period=2000003" },
  388. { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03,period=2000003" },
  389. { "cpu_clk_unhalted.thread", "event=0x3c,period=2000003" },
  390. { "cpu_clk_unhalted.core", "event=0x3c,period=2000003" },
  391. { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1,period=2000003" },
  392. { NULL, NULL},
  393. };
  394. /*
  395. * Handle different fixed counter encodings between JSON and perf.
  396. */
  397. static char *real_event(const char *name, char *event)
  398. {
  399. int i;
  400. if (!name)
  401. return NULL;
  402. for (i = 0; fixed[i].name; i++)
  403. if (!strcasecmp(name, fixed[i].name))
  404. return (char *)fixed[i].event;
  405. return event;
  406. }
  407. static int
  408. try_fixup(const char *fn, char *arch_std, char **event, char **desc,
  409. char **name, char **long_desc, char **pmu, char **filter,
  410. char **perpkg, char **unit, char **metric_expr, char **metric_name,
  411. char **metric_group, unsigned long long eventcode)
  412. {
  413. /* try to find matching event from arch standard values */
  414. struct event_struct *es;
  415. list_for_each_entry(es, &arch_std_events, list) {
  416. if (!strcmp(arch_std, es->name)) {
  417. if (!eventcode && es->event) {
  418. /* allow EventCode to be overridden */
  419. free(*event);
  420. *event = NULL;
  421. }
  422. FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
  423. return 0;
  424. }
  425. }
  426. pr_err("%s: could not find matching %s for %s\n",
  427. prog, arch_std, fn);
  428. return -1;
  429. }
  430. /* Call func with each event in the json file */
  431. int json_events(const char *fn,
  432. int (*func)(void *data, char *name, char *event, char *desc,
  433. char *long_desc,
  434. char *pmu, char *unit, char *perpkg,
  435. char *metric_expr,
  436. char *metric_name, char *metric_group),
  437. void *data)
  438. {
  439. int err;
  440. size_t size;
  441. jsmntok_t *tokens, *tok;
  442. int i, j, len;
  443. char *map;
  444. char buf[128];
  445. if (!fn)
  446. return -ENOENT;
  447. tokens = parse_json(fn, &map, &size, &len);
  448. if (!tokens)
  449. return -EIO;
  450. EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
  451. tok = tokens + 1;
  452. for (i = 0; i < tokens->size; i++) {
  453. char *event = NULL, *desc = NULL, *name = NULL;
  454. char *long_desc = NULL;
  455. char *extra_desc = NULL;
  456. char *pmu = NULL;
  457. char *filter = NULL;
  458. char *perpkg = NULL;
  459. char *unit = NULL;
  460. char *metric_expr = NULL;
  461. char *metric_name = NULL;
  462. char *metric_group = NULL;
  463. char *arch_std = NULL;
  464. unsigned long long eventcode = 0;
  465. struct msrmap *msr = NULL;
  466. jsmntok_t *msrval = NULL;
  467. jsmntok_t *precise = NULL;
  468. jsmntok_t *obj = tok++;
  469. EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
  470. for (j = 0; j < obj->size; j += 2) {
  471. jsmntok_t *field, *val;
  472. int nz;
  473. char *s;
  474. field = tok + j;
  475. EXPECT(field->type == JSMN_STRING, tok + j,
  476. "Expected field name");
  477. val = tok + j + 1;
  478. EXPECT(val->type == JSMN_STRING, tok + j + 1,
  479. "Expected string value");
  480. nz = !json_streq(map, val, "0");
  481. if (match_field(map, field, nz, &event, val)) {
  482. /* ok */
  483. } else if (json_streq(map, field, "EventCode")) {
  484. char *code = NULL;
  485. addfield(map, &code, "", "", val);
  486. eventcode |= strtoul(code, NULL, 0);
  487. free(code);
  488. } else if (json_streq(map, field, "ExtSel")) {
  489. char *code = NULL;
  490. addfield(map, &code, "", "", val);
  491. eventcode |= strtoul(code, NULL, 0) << 21;
  492. free(code);
  493. } else if (json_streq(map, field, "EventName")) {
  494. addfield(map, &name, "", "", val);
  495. } else if (json_streq(map, field, "BriefDescription")) {
  496. addfield(map, &desc, "", "", val);
  497. fixdesc(desc);
  498. } else if (json_streq(map, field,
  499. "PublicDescription")) {
  500. addfield(map, &long_desc, "", "", val);
  501. fixdesc(long_desc);
  502. } else if (json_streq(map, field, "PEBS") && nz) {
  503. precise = val;
  504. } else if (json_streq(map, field, "MSRIndex") && nz) {
  505. msr = lookup_msr(map, val);
  506. } else if (json_streq(map, field, "MSRValue")) {
  507. msrval = val;
  508. } else if (json_streq(map, field, "Errata") &&
  509. !json_streq(map, val, "null")) {
  510. addfield(map, &extra_desc, ". ",
  511. " Spec update: ", val);
  512. } else if (json_streq(map, field, "Data_LA") && nz) {
  513. addfield(map, &extra_desc, ". ",
  514. " Supports address when precise",
  515. NULL);
  516. } else if (json_streq(map, field, "Unit")) {
  517. const char *ppmu;
  518. ppmu = field_to_perf(unit_to_pmu, map, val);
  519. if (ppmu) {
  520. pmu = strdup(ppmu);
  521. } else {
  522. if (!pmu)
  523. pmu = strdup("uncore_");
  524. addfield(map, &pmu, "", "", val);
  525. for (s = pmu; *s; s++)
  526. *s = tolower(*s);
  527. }
  528. addfield(map, &desc, ". ", "Unit: ", NULL);
  529. addfield(map, &desc, "", pmu, NULL);
  530. addfield(map, &desc, "", " ", NULL);
  531. } else if (json_streq(map, field, "Filter")) {
  532. addfield(map, &filter, "", "", val);
  533. } else if (json_streq(map, field, "ScaleUnit")) {
  534. addfield(map, &unit, "", "", val);
  535. } else if (json_streq(map, field, "PerPkg")) {
  536. addfield(map, &perpkg, "", "", val);
  537. } else if (json_streq(map, field, "MetricName")) {
  538. addfield(map, &metric_name, "", "", val);
  539. } else if (json_streq(map, field, "MetricGroup")) {
  540. addfield(map, &metric_group, "", "", val);
  541. } else if (json_streq(map, field, "MetricExpr")) {
  542. addfield(map, &metric_expr, "", "", val);
  543. for (s = metric_expr; *s; s++)
  544. *s = tolower(*s);
  545. } else if (json_streq(map, field, "ArchStdEvent")) {
  546. addfield(map, &arch_std, "", "", val);
  547. for (s = arch_std; *s; s++)
  548. *s = tolower(*s);
  549. }
  550. /* ignore unknown fields */
  551. }
  552. if (precise && desc && !strstr(desc, "(Precise Event)")) {
  553. if (json_streq(map, precise, "2"))
  554. addfield(map, &extra_desc, " ",
  555. "(Must be precise)", NULL);
  556. else
  557. addfield(map, &extra_desc, " ",
  558. "(Precise event)", NULL);
  559. }
  560. snprintf(buf, sizeof buf, "event=%#llx", eventcode);
  561. addfield(map, &event, ",", buf, NULL);
  562. if (desc && extra_desc)
  563. addfield(map, &desc, " ", extra_desc, NULL);
  564. if (long_desc && extra_desc)
  565. addfield(map, &long_desc, " ", extra_desc, NULL);
  566. if (filter)
  567. addfield(map, &event, ",", filter, NULL);
  568. if (msr != NULL)
  569. addfield(map, &event, ",", msr->pname, msrval);
  570. if (name)
  571. fixname(name);
  572. if (arch_std) {
  573. /*
  574. * An arch standard event is referenced, so try to
  575. * fixup any unassigned values.
  576. */
  577. err = try_fixup(fn, arch_std, &event, &desc, &name,
  578. &long_desc, &pmu, &filter, &perpkg,
  579. &unit, &metric_expr, &metric_name,
  580. &metric_group, eventcode);
  581. if (err)
  582. goto free_strings;
  583. }
  584. err = func(data, name, real_event(name, event), desc, long_desc,
  585. pmu, unit, perpkg, metric_expr, metric_name, metric_group);
  586. free_strings:
  587. free(event);
  588. free(desc);
  589. free(name);
  590. free(long_desc);
  591. free(extra_desc);
  592. free(pmu);
  593. free(filter);
  594. free(perpkg);
  595. free(unit);
  596. free(metric_expr);
  597. free(metric_name);
  598. free(metric_group);
  599. free(arch_std);
  600. if (err)
  601. break;
  602. tok += j;
  603. }
  604. EXPECT(tok - tokens == len, tok, "unexpected objects at end");
  605. err = 0;
  606. out_free:
  607. free_json(map, size, tokens);
  608. return err;
  609. }
  610. static char *file_name_to_table_name(char *fname)
  611. {
  612. unsigned int i;
  613. int n;
  614. int c;
  615. char *tblname;
  616. /*
  617. * Ensure tablename starts with alphabetic character.
  618. * Derive rest of table name from basename of the JSON file,
  619. * replacing hyphens and stripping out .json suffix.
  620. */
  621. n = asprintf(&tblname, "pme_%s", fname);
  622. if (n < 0) {
  623. pr_info("%s: asprintf() error %s for file %s\n", prog,
  624. strerror(errno), fname);
  625. return NULL;
  626. }
  627. for (i = 0; i < strlen(tblname); i++) {
  628. c = tblname[i];
  629. if (c == '-' || c == '/')
  630. tblname[i] = '_';
  631. else if (c == '.') {
  632. tblname[i] = '\0';
  633. break;
  634. } else if (!isalnum(c) && c != '_') {
  635. pr_err("%s: Invalid character '%c' in file name %s\n",
  636. prog, c, basename(fname));
  637. free(tblname);
  638. tblname = NULL;
  639. break;
  640. }
  641. }
  642. return tblname;
  643. }
  644. static void print_mapping_table_prefix(FILE *outfp)
  645. {
  646. fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n");
  647. }
  648. static void print_mapping_table_suffix(FILE *outfp)
  649. {
  650. /*
  651. * Print the terminating, NULL entry.
  652. */
  653. fprintf(outfp, "{\n");
  654. fprintf(outfp, "\t.cpuid = 0,\n");
  655. fprintf(outfp, "\t.version = 0,\n");
  656. fprintf(outfp, "\t.type = 0,\n");
  657. fprintf(outfp, "\t.table = 0,\n");
  658. fprintf(outfp, "},\n");
  659. /* and finally, the closing curly bracket for the struct */
  660. fprintf(outfp, "};\n");
  661. }
  662. static int process_mapfile(FILE *outfp, char *fpath)
  663. {
  664. int n = 16384;
  665. FILE *mapfp;
  666. char *save = NULL;
  667. char *line, *p;
  668. int line_num;
  669. char *tblname;
  670. int ret = 0;
  671. pr_info("%s: Processing mapfile %s\n", prog, fpath);
  672. line = malloc(n);
  673. if (!line)
  674. return -1;
  675. mapfp = fopen(fpath, "r");
  676. if (!mapfp) {
  677. pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
  678. fpath);
  679. free(line);
  680. return -1;
  681. }
  682. print_mapping_table_prefix(outfp);
  683. /* Skip first line (header) */
  684. p = fgets(line, n, mapfp);
  685. if (!p)
  686. goto out;
  687. line_num = 1;
  688. while (1) {
  689. char *cpuid, *version, *type, *fname;
  690. line_num++;
  691. p = fgets(line, n, mapfp);
  692. if (!p)
  693. break;
  694. if (line[0] == '#' || line[0] == '\n')
  695. continue;
  696. if (line[strlen(line)-1] != '\n') {
  697. /* TODO Deal with lines longer than 16K */
  698. pr_info("%s: Mapfile %s: line %d too long, aborting\n",
  699. prog, fpath, line_num);
  700. ret = -1;
  701. goto out;
  702. }
  703. line[strlen(line)-1] = '\0';
  704. cpuid = fixregex(strtok_r(p, ",", &save));
  705. version = strtok_r(NULL, ",", &save);
  706. fname = strtok_r(NULL, ",", &save);
  707. type = strtok_r(NULL, ",", &save);
  708. tblname = file_name_to_table_name(fname);
  709. fprintf(outfp, "{\n");
  710. fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
  711. fprintf(outfp, "\t.version = \"%s\",\n", version);
  712. fprintf(outfp, "\t.type = \"%s\",\n", type);
  713. /*
  714. * CHECK: We can't use the type (eg "core") field in the
  715. * table name. For us to do that, we need to somehow tweak
  716. * the other caller of file_name_to_table(), process_json()
  717. * to determine the type. process_json() file has no way
  718. * of knowing these are "core" events unless file name has
  719. * core in it. If filename has core in it, we can safely
  720. * ignore the type field here also.
  721. */
  722. fprintf(outfp, "\t.table = %s\n", tblname);
  723. fprintf(outfp, "},\n");
  724. }
  725. out:
  726. print_mapping_table_suffix(outfp);
  727. fclose(mapfp);
  728. free(line);
  729. return ret;
  730. }
  731. /*
  732. * If we fail to locate/process JSON and map files, create a NULL mapping
  733. * table. This would at least allow perf to build even if we can't find/use
  734. * the aliases.
  735. */
  736. static void create_empty_mapping(const char *output_file)
  737. {
  738. FILE *outfp;
  739. pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
  740. /* Truncate file to clear any partial writes to it */
  741. outfp = fopen(output_file, "w");
  742. if (!outfp) {
  743. perror("fopen()");
  744. _Exit(1);
  745. }
  746. fprintf(outfp, "#include \"../../pmu-events/pmu-events.h\"\n");
  747. print_mapping_table_prefix(outfp);
  748. print_mapping_table_suffix(outfp);
  749. fclose(outfp);
  750. }
  751. static int get_maxfds(void)
  752. {
  753. struct rlimit rlim;
  754. if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
  755. return min((int)rlim.rlim_max / 2, 512);
  756. return 512;
  757. }
  758. /*
  759. * nftw() doesn't let us pass an argument to the processing function,
  760. * so use a global variables.
  761. */
  762. static FILE *eventsfp;
  763. static char *mapfile;
  764. static int is_leaf_dir(const char *fpath)
  765. {
  766. DIR *d;
  767. struct dirent *dir;
  768. int res = 1;
  769. d = opendir(fpath);
  770. if (!d)
  771. return 0;
  772. while ((dir = readdir(d)) != NULL) {
  773. if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
  774. continue;
  775. if (dir->d_type == DT_DIR) {
  776. res = 0;
  777. break;
  778. } else if (dir->d_type == DT_UNKNOWN) {
  779. char path[PATH_MAX];
  780. struct stat st;
  781. sprintf(path, "%s/%s", fpath, dir->d_name);
  782. if (stat(path, &st))
  783. break;
  784. if (S_ISDIR(st.st_mode)) {
  785. res = 0;
  786. break;
  787. }
  788. }
  789. }
  790. closedir(d);
  791. return res;
  792. }
  793. static int is_json_file(const char *name)
  794. {
  795. const char *suffix;
  796. if (strlen(name) < 5)
  797. return 0;
  798. suffix = name + strlen(name) - 5;
  799. if (strncmp(suffix, ".json", 5) == 0)
  800. return 1;
  801. return 0;
  802. }
  803. static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
  804. int typeflag, struct FTW *ftwbuf)
  805. {
  806. int level = ftwbuf->level;
  807. int is_file = typeflag == FTW_F;
  808. if (level == 1 && is_file && is_json_file(fpath))
  809. return json_events(fpath, save_arch_std_events, (void *)sb);
  810. return 0;
  811. }
  812. static int process_one_file(const char *fpath, const struct stat *sb,
  813. int typeflag, struct FTW *ftwbuf)
  814. {
  815. char *tblname, *bname;
  816. int is_dir = typeflag == FTW_D;
  817. int is_file = typeflag == FTW_F;
  818. int level = ftwbuf->level;
  819. int err = 0;
  820. if (level == 2 && is_dir) {
  821. /*
  822. * For level 2 directory, bname will include parent name,
  823. * like vendor/platform. So search back from platform dir
  824. * to find this.
  825. */
  826. bname = (char *) fpath + ftwbuf->base - 2;
  827. for (;;) {
  828. if (*bname == '/')
  829. break;
  830. bname--;
  831. }
  832. bname++;
  833. } else
  834. bname = (char *) fpath + ftwbuf->base;
  835. pr_debug("%s %d %7jd %-20s %s\n",
  836. is_file ? "f" : is_dir ? "d" : "x",
  837. level, sb->st_size, bname, fpath);
  838. /* base dir or too deep */
  839. if (level == 0 || level > 3)
  840. return 0;
  841. /* model directory, reset topic */
  842. if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
  843. (level == 2 && is_dir)) {
  844. if (close_table)
  845. print_events_table_suffix(eventsfp);
  846. /*
  847. * Drop file name suffix. Replace hyphens with underscores.
  848. * Fail if file name contains any alphanum characters besides
  849. * underscores.
  850. */
  851. tblname = file_name_to_table_name(bname);
  852. if (!tblname) {
  853. pr_info("%s: Error determining table name for %s\n", prog,
  854. bname);
  855. return -1;
  856. }
  857. print_events_table_prefix(eventsfp, tblname);
  858. return 0;
  859. }
  860. /*
  861. * Save the mapfile name for now. We will process mapfile
  862. * after processing all JSON files (so we can write out the
  863. * mapping table after all PMU events tables).
  864. *
  865. */
  866. if (level == 1 && is_file) {
  867. if (!strcmp(bname, "mapfile.csv")) {
  868. mapfile = strdup(fpath);
  869. return 0;
  870. }
  871. pr_info("%s: Ignoring file %s\n", prog, fpath);
  872. return 0;
  873. }
  874. /*
  875. * If the file name does not have a .json extension,
  876. * ignore it. It could be a readme.txt for instance.
  877. */
  878. if (is_file) {
  879. if (!is_json_file(bname)) {
  880. pr_info("%s: Ignoring file without .json suffix %s\n", prog,
  881. fpath);
  882. return 0;
  883. }
  884. }
  885. if (level > 1 && add_topic(bname))
  886. return -ENOMEM;
  887. /*
  888. * Assume all other files are JSON files.
  889. *
  890. * If mapfile refers to 'power7_core.json', we create a table
  891. * named 'power7_core'. Any inconsistencies between the mapfile
  892. * and directory tree could result in build failure due to table
  893. * names not being found.
  894. *
  895. * Atleast for now, be strict with processing JSON file names.
  896. * i.e. if JSON file name cannot be mapped to C-style table name,
  897. * fail.
  898. */
  899. if (is_file) {
  900. struct perf_entry_data data = {
  901. .topic = get_topic(),
  902. .outfp = eventsfp,
  903. };
  904. err = json_events(fpath, print_events_table_entry, &data);
  905. free(data.topic);
  906. }
  907. return err;
  908. }
  909. #ifndef PATH_MAX
  910. #define PATH_MAX 4096
  911. #endif
  912. /*
  913. * Starting in directory 'start_dirname', find the "mapfile.csv" and
  914. * the set of JSON files for the architecture 'arch'.
  915. *
  916. * From each JSON file, create a C-style "PMU events table" from the
  917. * JSON file (see struct pmu_event).
  918. *
  919. * From the mapfile, create a mapping between the CPU revisions and
  920. * PMU event tables (see struct pmu_events_map).
  921. *
  922. * Write out the PMU events tables and the mapping table to pmu-event.c.
  923. */
  924. int main(int argc, char *argv[])
  925. {
  926. int rc;
  927. int maxfds;
  928. char ldirname[PATH_MAX];
  929. const char *arch;
  930. const char *output_file;
  931. const char *start_dirname;
  932. struct stat stbuf;
  933. prog = basename(argv[0]);
  934. if (argc < 4) {
  935. pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
  936. return 1;
  937. }
  938. arch = argv[1];
  939. start_dirname = argv[2];
  940. output_file = argv[3];
  941. if (argc > 4)
  942. verbose = atoi(argv[4]);
  943. eventsfp = fopen(output_file, "w");
  944. if (!eventsfp) {
  945. pr_err("%s Unable to create required file %s (%s)\n",
  946. prog, output_file, strerror(errno));
  947. return 2;
  948. }
  949. sprintf(ldirname, "%s/%s", start_dirname, arch);
  950. /* If architecture does not have any event lists, bail out */
  951. if (stat(ldirname, &stbuf) < 0) {
  952. pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
  953. goto empty_map;
  954. }
  955. /* Include pmu-events.h first */
  956. fprintf(eventsfp, "#include \"../../pmu-events/pmu-events.h\"\n");
  957. /*
  958. * The mapfile allows multiple CPUids to point to the same JSON file,
  959. * so, not sure if there is a need for symlinks within the pmu-events
  960. * directory.
  961. *
  962. * For now, treat symlinks of JSON files as regular files and create
  963. * separate tables for each symlink (presumably, each symlink refers
  964. * to specific version of the CPU).
  965. */
  966. maxfds = get_maxfds();
  967. mapfile = NULL;
  968. rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
  969. if (rc && verbose) {
  970. pr_info("%s: Error preprocessing arch standard files %s\n",
  971. prog, ldirname);
  972. goto empty_map;
  973. } else if (rc < 0) {
  974. /* Make build fail */
  975. fclose(eventsfp);
  976. free_arch_std_events();
  977. return 1;
  978. } else if (rc) {
  979. goto empty_map;
  980. }
  981. rc = nftw(ldirname, process_one_file, maxfds, 0);
  982. if (rc && verbose) {
  983. pr_info("%s: Error walking file tree %s\n", prog, ldirname);
  984. goto empty_map;
  985. } else if (rc < 0) {
  986. /* Make build fail */
  987. fclose(eventsfp);
  988. free_arch_std_events();
  989. return 1;
  990. } else if (rc) {
  991. goto empty_map;
  992. }
  993. if (close_table)
  994. print_events_table_suffix(eventsfp);
  995. if (!mapfile) {
  996. pr_info("%s: No CPU->JSON mapping?\n", prog);
  997. goto empty_map;
  998. }
  999. if (process_mapfile(eventsfp, mapfile)) {
  1000. pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
  1001. /* Make build fail */
  1002. fclose(eventsfp);
  1003. free_arch_std_events();
  1004. return 1;
  1005. }
  1006. return 0;
  1007. empty_map:
  1008. fclose(eventsfp);
  1009. create_empty_mapping(output_file);
  1010. free_arch_std_events();
  1011. return 0;
  1012. }