jevents.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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" },
  387. { "inst_retired.any_p", "event=0xc0" },
  388. { "cpu_clk_unhalted.ref", "event=0x0,umask=0x03" },
  389. { "cpu_clk_unhalted.thread", "event=0x3c" },
  390. { "cpu_clk_unhalted.thread_any", "event=0x3c,any=1" },
  391. { NULL, NULL},
  392. };
  393. /*
  394. * Handle different fixed counter encodings between JSON and perf.
  395. */
  396. static char *real_event(const char *name, char *event)
  397. {
  398. int i;
  399. if (!name)
  400. return NULL;
  401. for (i = 0; fixed[i].name; i++)
  402. if (!strcasecmp(name, fixed[i].name))
  403. return (char *)fixed[i].event;
  404. return event;
  405. }
  406. static int
  407. try_fixup(const char *fn, char *arch_std, char **event, char **desc,
  408. char **name, char **long_desc, char **pmu, char **filter,
  409. char **perpkg, char **unit, char **metric_expr, char **metric_name,
  410. char **metric_group, unsigned long long eventcode)
  411. {
  412. /* try to find matching event from arch standard values */
  413. struct event_struct *es;
  414. list_for_each_entry(es, &arch_std_events, list) {
  415. if (!strcmp(arch_std, es->name)) {
  416. if (!eventcode && es->event) {
  417. /* allow EventCode to be overridden */
  418. free(*event);
  419. *event = NULL;
  420. }
  421. FOR_ALL_EVENT_STRUCT_FIELDS(TRY_FIXUP_FIELD);
  422. return 0;
  423. }
  424. }
  425. pr_err("%s: could not find matching %s for %s\n",
  426. prog, arch_std, fn);
  427. return -1;
  428. }
  429. /* Call func with each event in the json file */
  430. int json_events(const char *fn,
  431. int (*func)(void *data, char *name, char *event, char *desc,
  432. char *long_desc,
  433. char *pmu, char *unit, char *perpkg,
  434. char *metric_expr,
  435. char *metric_name, char *metric_group),
  436. void *data)
  437. {
  438. int err;
  439. size_t size;
  440. jsmntok_t *tokens, *tok;
  441. int i, j, len;
  442. char *map;
  443. char buf[128];
  444. if (!fn)
  445. return -ENOENT;
  446. tokens = parse_json(fn, &map, &size, &len);
  447. if (!tokens)
  448. return -EIO;
  449. EXPECT(tokens->type == JSMN_ARRAY, tokens, "expected top level array");
  450. tok = tokens + 1;
  451. for (i = 0; i < tokens->size; i++) {
  452. char *event = NULL, *desc = NULL, *name = NULL;
  453. char *long_desc = NULL;
  454. char *extra_desc = NULL;
  455. char *pmu = NULL;
  456. char *filter = NULL;
  457. char *perpkg = NULL;
  458. char *unit = NULL;
  459. char *metric_expr = NULL;
  460. char *metric_name = NULL;
  461. char *metric_group = NULL;
  462. char *arch_std = NULL;
  463. unsigned long long eventcode = 0;
  464. struct msrmap *msr = NULL;
  465. jsmntok_t *msrval = NULL;
  466. jsmntok_t *precise = NULL;
  467. jsmntok_t *obj = tok++;
  468. EXPECT(obj->type == JSMN_OBJECT, obj, "expected object");
  469. for (j = 0; j < obj->size; j += 2) {
  470. jsmntok_t *field, *val;
  471. int nz;
  472. char *s;
  473. field = tok + j;
  474. EXPECT(field->type == JSMN_STRING, tok + j,
  475. "Expected field name");
  476. val = tok + j + 1;
  477. EXPECT(val->type == JSMN_STRING, tok + j + 1,
  478. "Expected string value");
  479. nz = !json_streq(map, val, "0");
  480. if (match_field(map, field, nz, &event, val)) {
  481. /* ok */
  482. } else if (json_streq(map, field, "EventCode")) {
  483. char *code = NULL;
  484. addfield(map, &code, "", "", val);
  485. eventcode |= strtoul(code, NULL, 0);
  486. free(code);
  487. } else if (json_streq(map, field, "ExtSel")) {
  488. char *code = NULL;
  489. addfield(map, &code, "", "", val);
  490. eventcode |= strtoul(code, NULL, 0) << 21;
  491. free(code);
  492. } else if (json_streq(map, field, "EventName")) {
  493. addfield(map, &name, "", "", val);
  494. } else if (json_streq(map, field, "BriefDescription")) {
  495. addfield(map, &desc, "", "", val);
  496. fixdesc(desc);
  497. } else if (json_streq(map, field,
  498. "PublicDescription")) {
  499. addfield(map, &long_desc, "", "", val);
  500. fixdesc(long_desc);
  501. } else if (json_streq(map, field, "PEBS") && nz) {
  502. precise = val;
  503. } else if (json_streq(map, field, "MSRIndex") && nz) {
  504. msr = lookup_msr(map, val);
  505. } else if (json_streq(map, field, "MSRValue")) {
  506. msrval = val;
  507. } else if (json_streq(map, field, "Errata") &&
  508. !json_streq(map, val, "null")) {
  509. addfield(map, &extra_desc, ". ",
  510. " Spec update: ", val);
  511. } else if (json_streq(map, field, "Data_LA") && nz) {
  512. addfield(map, &extra_desc, ". ",
  513. " Supports address when precise",
  514. NULL);
  515. } else if (json_streq(map, field, "Unit")) {
  516. const char *ppmu;
  517. ppmu = field_to_perf(unit_to_pmu, map, val);
  518. if (ppmu) {
  519. pmu = strdup(ppmu);
  520. } else {
  521. if (!pmu)
  522. pmu = strdup("uncore_");
  523. addfield(map, &pmu, "", "", val);
  524. for (s = pmu; *s; s++)
  525. *s = tolower(*s);
  526. }
  527. addfield(map, &desc, ". ", "Unit: ", NULL);
  528. addfield(map, &desc, "", pmu, NULL);
  529. addfield(map, &desc, "", " ", NULL);
  530. } else if (json_streq(map, field, "Filter")) {
  531. addfield(map, &filter, "", "", val);
  532. } else if (json_streq(map, field, "ScaleUnit")) {
  533. addfield(map, &unit, "", "", val);
  534. } else if (json_streq(map, field, "PerPkg")) {
  535. addfield(map, &perpkg, "", "", val);
  536. } else if (json_streq(map, field, "MetricName")) {
  537. addfield(map, &metric_name, "", "", val);
  538. } else if (json_streq(map, field, "MetricGroup")) {
  539. addfield(map, &metric_group, "", "", val);
  540. } else if (json_streq(map, field, "MetricExpr")) {
  541. addfield(map, &metric_expr, "", "", val);
  542. for (s = metric_expr; *s; s++)
  543. *s = tolower(*s);
  544. } else if (json_streq(map, field, "ArchStdEvent")) {
  545. addfield(map, &arch_std, "", "", val);
  546. for (s = arch_std; *s; s++)
  547. *s = tolower(*s);
  548. }
  549. /* ignore unknown fields */
  550. }
  551. if (precise && desc && !strstr(desc, "(Precise Event)")) {
  552. if (json_streq(map, precise, "2"))
  553. addfield(map, &extra_desc, " ",
  554. "(Must be precise)", NULL);
  555. else
  556. addfield(map, &extra_desc, " ",
  557. "(Precise event)", NULL);
  558. }
  559. snprintf(buf, sizeof buf, "event=%#llx", eventcode);
  560. addfield(map, &event, ",", buf, NULL);
  561. if (desc && extra_desc)
  562. addfield(map, &desc, " ", extra_desc, NULL);
  563. if (long_desc && extra_desc)
  564. addfield(map, &long_desc, " ", extra_desc, NULL);
  565. if (filter)
  566. addfield(map, &event, ",", filter, NULL);
  567. if (msr != NULL)
  568. addfield(map, &event, ",", msr->pname, msrval);
  569. if (name)
  570. fixname(name);
  571. if (arch_std) {
  572. /*
  573. * An arch standard event is referenced, so try to
  574. * fixup any unassigned values.
  575. */
  576. err = try_fixup(fn, arch_std, &event, &desc, &name,
  577. &long_desc, &pmu, &filter, &perpkg,
  578. &unit, &metric_expr, &metric_name,
  579. &metric_group, eventcode);
  580. if (err)
  581. goto free_strings;
  582. }
  583. err = func(data, name, real_event(name, event), desc, long_desc,
  584. pmu, unit, perpkg, metric_expr, metric_name, metric_group);
  585. free_strings:
  586. free(event);
  587. free(desc);
  588. free(name);
  589. free(long_desc);
  590. free(extra_desc);
  591. free(pmu);
  592. free(filter);
  593. free(perpkg);
  594. free(unit);
  595. free(metric_expr);
  596. free(metric_name);
  597. free(metric_group);
  598. free(arch_std);
  599. if (err)
  600. break;
  601. tok += j;
  602. }
  603. EXPECT(tok - tokens == len, tok, "unexpected objects at end");
  604. err = 0;
  605. out_free:
  606. free_json(map, size, tokens);
  607. return err;
  608. }
  609. static char *file_name_to_table_name(char *fname)
  610. {
  611. unsigned int i;
  612. int n;
  613. int c;
  614. char *tblname;
  615. /*
  616. * Ensure tablename starts with alphabetic character.
  617. * Derive rest of table name from basename of the JSON file,
  618. * replacing hyphens and stripping out .json suffix.
  619. */
  620. n = asprintf(&tblname, "pme_%s", fname);
  621. if (n < 0) {
  622. pr_info("%s: asprintf() error %s for file %s\n", prog,
  623. strerror(errno), fname);
  624. return NULL;
  625. }
  626. for (i = 0; i < strlen(tblname); i++) {
  627. c = tblname[i];
  628. if (c == '-' || c == '/')
  629. tblname[i] = '_';
  630. else if (c == '.') {
  631. tblname[i] = '\0';
  632. break;
  633. } else if (!isalnum(c) && c != '_') {
  634. pr_err("%s: Invalid character '%c' in file name %s\n",
  635. prog, c, basename(fname));
  636. free(tblname);
  637. tblname = NULL;
  638. break;
  639. }
  640. }
  641. return tblname;
  642. }
  643. static void print_mapping_table_prefix(FILE *outfp)
  644. {
  645. fprintf(outfp, "struct pmu_events_map pmu_events_map[] = {\n");
  646. }
  647. static void print_mapping_table_suffix(FILE *outfp)
  648. {
  649. /*
  650. * Print the terminating, NULL entry.
  651. */
  652. fprintf(outfp, "{\n");
  653. fprintf(outfp, "\t.cpuid = 0,\n");
  654. fprintf(outfp, "\t.version = 0,\n");
  655. fprintf(outfp, "\t.type = 0,\n");
  656. fprintf(outfp, "\t.table = 0,\n");
  657. fprintf(outfp, "},\n");
  658. /* and finally, the closing curly bracket for the struct */
  659. fprintf(outfp, "};\n");
  660. }
  661. static int process_mapfile(FILE *outfp, char *fpath)
  662. {
  663. int n = 16384;
  664. FILE *mapfp;
  665. char *save = NULL;
  666. char *line, *p;
  667. int line_num;
  668. char *tblname;
  669. pr_info("%s: Processing mapfile %s\n", prog, fpath);
  670. line = malloc(n);
  671. if (!line)
  672. return -1;
  673. mapfp = fopen(fpath, "r");
  674. if (!mapfp) {
  675. pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
  676. fpath);
  677. return -1;
  678. }
  679. print_mapping_table_prefix(outfp);
  680. /* Skip first line (header) */
  681. p = fgets(line, n, mapfp);
  682. if (!p)
  683. goto out;
  684. line_num = 1;
  685. while (1) {
  686. char *cpuid, *version, *type, *fname;
  687. line_num++;
  688. p = fgets(line, n, mapfp);
  689. if (!p)
  690. break;
  691. if (line[0] == '#' || line[0] == '\n')
  692. continue;
  693. if (line[strlen(line)-1] != '\n') {
  694. /* TODO Deal with lines longer than 16K */
  695. pr_info("%s: Mapfile %s: line %d too long, aborting\n",
  696. prog, fpath, line_num);
  697. return -1;
  698. }
  699. line[strlen(line)-1] = '\0';
  700. cpuid = fixregex(strtok_r(p, ",", &save));
  701. version = strtok_r(NULL, ",", &save);
  702. fname = strtok_r(NULL, ",", &save);
  703. type = strtok_r(NULL, ",", &save);
  704. tblname = file_name_to_table_name(fname);
  705. fprintf(outfp, "{\n");
  706. fprintf(outfp, "\t.cpuid = \"%s\",\n", cpuid);
  707. fprintf(outfp, "\t.version = \"%s\",\n", version);
  708. fprintf(outfp, "\t.type = \"%s\",\n", type);
  709. /*
  710. * CHECK: We can't use the type (eg "core") field in the
  711. * table name. For us to do that, we need to somehow tweak
  712. * the other caller of file_name_to_table(), process_json()
  713. * to determine the type. process_json() file has no way
  714. * of knowing these are "core" events unless file name has
  715. * core in it. If filename has core in it, we can safely
  716. * ignore the type field here also.
  717. */
  718. fprintf(outfp, "\t.table = %s\n", tblname);
  719. fprintf(outfp, "},\n");
  720. }
  721. out:
  722. print_mapping_table_suffix(outfp);
  723. return 0;
  724. }
  725. /*
  726. * If we fail to locate/process JSON and map files, create a NULL mapping
  727. * table. This would at least allow perf to build even if we can't find/use
  728. * the aliases.
  729. */
  730. static void create_empty_mapping(const char *output_file)
  731. {
  732. FILE *outfp;
  733. pr_info("%s: Creating empty pmu_events_map[] table\n", prog);
  734. /* Truncate file to clear any partial writes to it */
  735. outfp = fopen(output_file, "w");
  736. if (!outfp) {
  737. perror("fopen()");
  738. _Exit(1);
  739. }
  740. fprintf(outfp, "#include \"../../pmu-events/pmu-events.h\"\n");
  741. print_mapping_table_prefix(outfp);
  742. print_mapping_table_suffix(outfp);
  743. fclose(outfp);
  744. }
  745. static int get_maxfds(void)
  746. {
  747. struct rlimit rlim;
  748. if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
  749. return min((int)rlim.rlim_max / 2, 512);
  750. return 512;
  751. }
  752. /*
  753. * nftw() doesn't let us pass an argument to the processing function,
  754. * so use a global variables.
  755. */
  756. static FILE *eventsfp;
  757. static char *mapfile;
  758. static int is_leaf_dir(const char *fpath)
  759. {
  760. DIR *d;
  761. struct dirent *dir;
  762. int res = 1;
  763. d = opendir(fpath);
  764. if (!d)
  765. return 0;
  766. while ((dir = readdir(d)) != NULL) {
  767. if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
  768. continue;
  769. if (dir->d_type == DT_DIR) {
  770. res = 0;
  771. break;
  772. } else if (dir->d_type == DT_UNKNOWN) {
  773. char path[PATH_MAX];
  774. struct stat st;
  775. sprintf(path, "%s/%s", fpath, dir->d_name);
  776. if (stat(path, &st))
  777. break;
  778. if (S_ISDIR(st.st_mode)) {
  779. res = 0;
  780. break;
  781. }
  782. }
  783. }
  784. closedir(d);
  785. return res;
  786. }
  787. static int is_json_file(const char *name)
  788. {
  789. const char *suffix;
  790. if (strlen(name) < 5)
  791. return 0;
  792. suffix = name + strlen(name) - 5;
  793. if (strncmp(suffix, ".json", 5) == 0)
  794. return 1;
  795. return 0;
  796. }
  797. static int preprocess_arch_std_files(const char *fpath, const struct stat *sb,
  798. int typeflag, struct FTW *ftwbuf)
  799. {
  800. int level = ftwbuf->level;
  801. int is_file = typeflag == FTW_F;
  802. if (level == 1 && is_file && is_json_file(fpath))
  803. return json_events(fpath, save_arch_std_events, (void *)sb);
  804. return 0;
  805. }
  806. static int process_one_file(const char *fpath, const struct stat *sb,
  807. int typeflag, struct FTW *ftwbuf)
  808. {
  809. char *tblname, *bname;
  810. int is_dir = typeflag == FTW_D;
  811. int is_file = typeflag == FTW_F;
  812. int level = ftwbuf->level;
  813. int err = 0;
  814. if (level == 2 && is_dir) {
  815. /*
  816. * For level 2 directory, bname will include parent name,
  817. * like vendor/platform. So search back from platform dir
  818. * to find this.
  819. */
  820. bname = (char *) fpath + ftwbuf->base - 2;
  821. for (;;) {
  822. if (*bname == '/')
  823. break;
  824. bname--;
  825. }
  826. bname++;
  827. } else
  828. bname = (char *) fpath + ftwbuf->base;
  829. pr_debug("%s %d %7jd %-20s %s\n",
  830. is_file ? "f" : is_dir ? "d" : "x",
  831. level, sb->st_size, bname, fpath);
  832. /* base dir or too deep */
  833. if (level == 0 || level > 3)
  834. return 0;
  835. /* model directory, reset topic */
  836. if ((level == 1 && is_dir && is_leaf_dir(fpath)) ||
  837. (level == 2 && is_dir)) {
  838. if (close_table)
  839. print_events_table_suffix(eventsfp);
  840. /*
  841. * Drop file name suffix. Replace hyphens with underscores.
  842. * Fail if file name contains any alphanum characters besides
  843. * underscores.
  844. */
  845. tblname = file_name_to_table_name(bname);
  846. if (!tblname) {
  847. pr_info("%s: Error determining table name for %s\n", prog,
  848. bname);
  849. return -1;
  850. }
  851. print_events_table_prefix(eventsfp, tblname);
  852. return 0;
  853. }
  854. /*
  855. * Save the mapfile name for now. We will process mapfile
  856. * after processing all JSON files (so we can write out the
  857. * mapping table after all PMU events tables).
  858. *
  859. */
  860. if (level == 1 && is_file) {
  861. if (!strcmp(bname, "mapfile.csv")) {
  862. mapfile = strdup(fpath);
  863. return 0;
  864. }
  865. pr_info("%s: Ignoring file %s\n", prog, fpath);
  866. return 0;
  867. }
  868. /*
  869. * If the file name does not have a .json extension,
  870. * ignore it. It could be a readme.txt for instance.
  871. */
  872. if (is_file) {
  873. if (!is_json_file(bname)) {
  874. pr_info("%s: Ignoring file without .json suffix %s\n", prog,
  875. fpath);
  876. return 0;
  877. }
  878. }
  879. if (level > 1 && add_topic(bname))
  880. return -ENOMEM;
  881. /*
  882. * Assume all other files are JSON files.
  883. *
  884. * If mapfile refers to 'power7_core.json', we create a table
  885. * named 'power7_core'. Any inconsistencies between the mapfile
  886. * and directory tree could result in build failure due to table
  887. * names not being found.
  888. *
  889. * Atleast for now, be strict with processing JSON file names.
  890. * i.e. if JSON file name cannot be mapped to C-style table name,
  891. * fail.
  892. */
  893. if (is_file) {
  894. struct perf_entry_data data = {
  895. .topic = get_topic(),
  896. .outfp = eventsfp,
  897. };
  898. err = json_events(fpath, print_events_table_entry, &data);
  899. free(data.topic);
  900. }
  901. return err;
  902. }
  903. #ifndef PATH_MAX
  904. #define PATH_MAX 4096
  905. #endif
  906. /*
  907. * Starting in directory 'start_dirname', find the "mapfile.csv" and
  908. * the set of JSON files for the architecture 'arch'.
  909. *
  910. * From each JSON file, create a C-style "PMU events table" from the
  911. * JSON file (see struct pmu_event).
  912. *
  913. * From the mapfile, create a mapping between the CPU revisions and
  914. * PMU event tables (see struct pmu_events_map).
  915. *
  916. * Write out the PMU events tables and the mapping table to pmu-event.c.
  917. */
  918. int main(int argc, char *argv[])
  919. {
  920. int rc;
  921. int maxfds;
  922. char ldirname[PATH_MAX];
  923. const char *arch;
  924. const char *output_file;
  925. const char *start_dirname;
  926. struct stat stbuf;
  927. prog = basename(argv[0]);
  928. if (argc < 4) {
  929. pr_err("Usage: %s <arch> <starting_dir> <output_file>\n", prog);
  930. return 1;
  931. }
  932. arch = argv[1];
  933. start_dirname = argv[2];
  934. output_file = argv[3];
  935. if (argc > 4)
  936. verbose = atoi(argv[4]);
  937. eventsfp = fopen(output_file, "w");
  938. if (!eventsfp) {
  939. pr_err("%s Unable to create required file %s (%s)\n",
  940. prog, output_file, strerror(errno));
  941. return 2;
  942. }
  943. sprintf(ldirname, "%s/%s", start_dirname, arch);
  944. /* If architecture does not have any event lists, bail out */
  945. if (stat(ldirname, &stbuf) < 0) {
  946. pr_info("%s: Arch %s has no PMU event lists\n", prog, arch);
  947. goto empty_map;
  948. }
  949. /* Include pmu-events.h first */
  950. fprintf(eventsfp, "#include \"../../pmu-events/pmu-events.h\"\n");
  951. /*
  952. * The mapfile allows multiple CPUids to point to the same JSON file,
  953. * so, not sure if there is a need for symlinks within the pmu-events
  954. * directory.
  955. *
  956. * For now, treat symlinks of JSON files as regular files and create
  957. * separate tables for each symlink (presumably, each symlink refers
  958. * to specific version of the CPU).
  959. */
  960. maxfds = get_maxfds();
  961. mapfile = NULL;
  962. rc = nftw(ldirname, preprocess_arch_std_files, maxfds, 0);
  963. if (rc && verbose) {
  964. pr_info("%s: Error preprocessing arch standard files %s\n",
  965. prog, ldirname);
  966. goto empty_map;
  967. } else if (rc < 0) {
  968. /* Make build fail */
  969. free_arch_std_events();
  970. return 1;
  971. } else if (rc) {
  972. goto empty_map;
  973. }
  974. rc = nftw(ldirname, process_one_file, maxfds, 0);
  975. if (rc && verbose) {
  976. pr_info("%s: Error walking file tree %s\n", prog, ldirname);
  977. goto empty_map;
  978. } else if (rc < 0) {
  979. /* Make build fail */
  980. free_arch_std_events();
  981. return 1;
  982. } else if (rc) {
  983. goto empty_map;
  984. }
  985. if (close_table)
  986. print_events_table_suffix(eventsfp);
  987. if (!mapfile) {
  988. pr_info("%s: No CPU->JSON mapping?\n", prog);
  989. goto empty_map;
  990. }
  991. if (process_mapfile(eventsfp, mapfile)) {
  992. pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
  993. /* Make build fail */
  994. return 1;
  995. }
  996. return 0;
  997. empty_map:
  998. fclose(eventsfp);
  999. create_empty_mapping(output_file);
  1000. free_arch_std_events();
  1001. return 0;
  1002. }