parse-events.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. #include "../perf.h"
  2. #include "util.h"
  3. #include "parse-options.h"
  4. #include "parse-events.h"
  5. #include "exec_cmd.h"
  6. #include "string.h"
  7. extern char *strcasestr(const char *haystack, const char *needle);
  8. int nr_counters;
  9. struct perf_counter_attr attrs[MAX_COUNTERS];
  10. struct event_symbol {
  11. u8 type;
  12. u64 config;
  13. char *symbol;
  14. char *alias;
  15. };
  16. #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
  17. #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
  18. static struct event_symbol event_symbols[] = {
  19. { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
  20. { CHW(INSTRUCTIONS), "instructions", "" },
  21. { CHW(CACHE_REFERENCES), "cache-references", "" },
  22. { CHW(CACHE_MISSES), "cache-misses", "" },
  23. { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
  24. { CHW(BRANCH_MISSES), "branch-misses", "" },
  25. { CHW(BUS_CYCLES), "bus-cycles", "" },
  26. { CSW(CPU_CLOCK), "cpu-clock", "" },
  27. { CSW(TASK_CLOCK), "task-clock", "" },
  28. { CSW(PAGE_FAULTS), "page-faults", "faults" },
  29. { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
  30. { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
  31. { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
  32. { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
  33. };
  34. #define __PERF_COUNTER_FIELD(config, name) \
  35. ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
  36. #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
  37. #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
  38. #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
  39. #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
  40. static char *hw_event_names[] = {
  41. "cycles",
  42. "instructions",
  43. "cache-references",
  44. "cache-misses",
  45. "branches",
  46. "branch-misses",
  47. "bus-cycles",
  48. };
  49. static char *sw_event_names[] = {
  50. "cpu-clock-msecs",
  51. "task-clock-msecs",
  52. "page-faults",
  53. "context-switches",
  54. "CPU-migrations",
  55. "minor-faults",
  56. "major-faults",
  57. };
  58. #define MAX_ALIASES 8
  59. static char *hw_cache[][MAX_ALIASES] = {
  60. { "L1-dcache", "l1-d", "l1d", "L1-data", },
  61. { "L1-icache", "l1-i", "l1i", "L1-instruction", },
  62. { "LLC", "L2" },
  63. { "dTLB", "d-tlb", "Data-TLB", },
  64. { "iTLB", "i-tlb", "Instruction-TLB", },
  65. { "branch", "branches", "bpu", "btb", "bpc", },
  66. };
  67. static char *hw_cache_op[][MAX_ALIASES] = {
  68. { "load", "loads", "read", },
  69. { "store", "stores", "write", },
  70. { "prefetch", "prefetches", "speculative-read", "speculative-load", },
  71. };
  72. static char *hw_cache_result[][MAX_ALIASES] = {
  73. { "refs", "Reference", "ops", "access", },
  74. { "misses", "miss", },
  75. };
  76. #define C(x) PERF_COUNT_HW_CACHE_##x
  77. #define CACHE_READ (1 << C(OP_READ))
  78. #define CACHE_WRITE (1 << C(OP_WRITE))
  79. #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
  80. #define COP(x) (1 << x)
  81. /*
  82. * cache operartion stat
  83. * L1I : Read and prefetch only
  84. * ITLB and BPU : Read-only
  85. */
  86. static unsigned long hw_cache_stat[C(MAX)] = {
  87. [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  88. [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
  89. [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  90. [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  91. [C(ITLB)] = (CACHE_READ),
  92. [C(BPU)] = (CACHE_READ),
  93. };
  94. static int is_cache_op_valid(u8 cache_type, u8 cache_op)
  95. {
  96. if (hw_cache_stat[cache_type] & COP(cache_op))
  97. return 1; /* valid */
  98. else
  99. return 0; /* invalid */
  100. }
  101. static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
  102. {
  103. static char name[50];
  104. if (cache_result) {
  105. sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
  106. hw_cache_op[cache_op][0],
  107. hw_cache_result[cache_result][0]);
  108. } else {
  109. sprintf(name, "%s-%s", hw_cache[cache_type][0],
  110. hw_cache_op[cache_op][1]);
  111. }
  112. return name;
  113. }
  114. char *event_name(int counter)
  115. {
  116. u64 config = attrs[counter].config;
  117. int type = attrs[counter].type;
  118. static char buf[32];
  119. if (attrs[counter].type == PERF_TYPE_RAW) {
  120. sprintf(buf, "raw 0x%llx", config);
  121. return buf;
  122. }
  123. switch (type) {
  124. case PERF_TYPE_HARDWARE:
  125. if (config < PERF_COUNT_HW_MAX)
  126. return hw_event_names[config];
  127. return "unknown-hardware";
  128. case PERF_TYPE_HW_CACHE: {
  129. u8 cache_type, cache_op, cache_result;
  130. cache_type = (config >> 0) & 0xff;
  131. if (cache_type > PERF_COUNT_HW_CACHE_MAX)
  132. return "unknown-ext-hardware-cache-type";
  133. cache_op = (config >> 8) & 0xff;
  134. if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
  135. return "unknown-ext-hardware-cache-op";
  136. cache_result = (config >> 16) & 0xff;
  137. if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
  138. return "unknown-ext-hardware-cache-result";
  139. if (!is_cache_op_valid(cache_type, cache_op))
  140. return "invalid-cache";
  141. return event_cache_name(cache_type, cache_op, cache_result);
  142. }
  143. case PERF_TYPE_SOFTWARE:
  144. if (config < PERF_COUNT_SW_MAX)
  145. return sw_event_names[config];
  146. return "unknown-software";
  147. default:
  148. break;
  149. }
  150. return "unknown";
  151. }
  152. static int parse_aliases(const char **str, char *names[][MAX_ALIASES], int size)
  153. {
  154. int i, j;
  155. int n, longest = -1;
  156. for (i = 0; i < size; i++) {
  157. for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
  158. n = strlen(names[i][j]);
  159. if (n > longest && !strncasecmp(*str, names[i][j], n))
  160. longest = n;
  161. }
  162. if (longest > 0) {
  163. *str += longest;
  164. return i;
  165. }
  166. }
  167. return -1;
  168. }
  169. static int
  170. parse_generic_hw_event(const char **str, struct perf_counter_attr *attr)
  171. {
  172. const char *s = *str;
  173. int cache_type = -1, cache_op = -1, cache_result = -1;
  174. cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
  175. /*
  176. * No fallback - if we cannot get a clear cache type
  177. * then bail out:
  178. */
  179. if (cache_type == -1)
  180. return 0;
  181. while ((cache_op == -1 || cache_result == -1) && *s == '-') {
  182. ++s;
  183. if (cache_op == -1) {
  184. cache_op = parse_aliases(&s, hw_cache_op,
  185. PERF_COUNT_HW_CACHE_OP_MAX);
  186. if (cache_op >= 0) {
  187. if (!is_cache_op_valid(cache_type, cache_op))
  188. return 0;
  189. continue;
  190. }
  191. }
  192. if (cache_result == -1) {
  193. cache_result = parse_aliases(&s, hw_cache_result,
  194. PERF_COUNT_HW_CACHE_RESULT_MAX);
  195. if (cache_result >= 0)
  196. continue;
  197. }
  198. /*
  199. * Can't parse this as a cache op or result, so back up
  200. * to the '-'.
  201. */
  202. --s;
  203. break;
  204. }
  205. /*
  206. * Fall back to reads:
  207. */
  208. if (cache_op == -1)
  209. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  210. /*
  211. * Fall back to accesses:
  212. */
  213. if (cache_result == -1)
  214. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  215. attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
  216. attr->type = PERF_TYPE_HW_CACHE;
  217. *str = s;
  218. return 1;
  219. }
  220. static int check_events(const char *str, unsigned int i)
  221. {
  222. int n;
  223. n = strlen(event_symbols[i].symbol);
  224. if (!strncmp(str, event_symbols[i].symbol, n))
  225. return n;
  226. n = strlen(event_symbols[i].alias);
  227. if (n)
  228. if (!strncmp(str, event_symbols[i].alias, n))
  229. return n;
  230. return 0;
  231. }
  232. static int
  233. parse_symbolic_event(const char **strp, struct perf_counter_attr *attr)
  234. {
  235. const char *str = *strp;
  236. unsigned int i;
  237. int n;
  238. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  239. n = check_events(str, i);
  240. if (n > 0) {
  241. attr->type = event_symbols[i].type;
  242. attr->config = event_symbols[i].config;
  243. *strp = str + n;
  244. return 1;
  245. }
  246. }
  247. return 0;
  248. }
  249. static int parse_raw_event(const char **strp, struct perf_counter_attr *attr)
  250. {
  251. const char *str = *strp;
  252. u64 config;
  253. int n;
  254. if (*str != 'r')
  255. return 0;
  256. n = hex2u64(str + 1, &config);
  257. if (n > 0) {
  258. *strp = str + n + 1;
  259. attr->type = PERF_TYPE_RAW;
  260. attr->config = config;
  261. return 1;
  262. }
  263. return 0;
  264. }
  265. static int
  266. parse_numeric_event(const char **strp, struct perf_counter_attr *attr)
  267. {
  268. const char *str = *strp;
  269. char *endp;
  270. unsigned long type;
  271. u64 config;
  272. type = strtoul(str, &endp, 0);
  273. if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
  274. str = endp + 1;
  275. config = strtoul(str, &endp, 0);
  276. if (endp > str) {
  277. attr->type = type;
  278. attr->config = config;
  279. *strp = endp;
  280. return 1;
  281. }
  282. }
  283. return 0;
  284. }
  285. static int
  286. parse_event_modifier(const char **strp, struct perf_counter_attr *attr)
  287. {
  288. const char *str = *strp;
  289. int eu = 1, ek = 1, eh = 1;
  290. if (*str++ != ':')
  291. return 0;
  292. while (*str) {
  293. if (*str == 'u')
  294. eu = 0;
  295. else if (*str == 'k')
  296. ek = 0;
  297. else if (*str == 'h')
  298. eh = 0;
  299. else
  300. break;
  301. ++str;
  302. }
  303. if (str >= *strp + 2) {
  304. *strp = str;
  305. attr->exclude_user = eu;
  306. attr->exclude_kernel = ek;
  307. attr->exclude_hv = eh;
  308. return 1;
  309. }
  310. return 0;
  311. }
  312. /*
  313. * Each event can have multiple symbolic names.
  314. * Symbolic names are (almost) exactly matched.
  315. */
  316. static int parse_event_symbols(const char **str, struct perf_counter_attr *attr)
  317. {
  318. if (!(parse_raw_event(str, attr) ||
  319. parse_numeric_event(str, attr) ||
  320. parse_symbolic_event(str, attr) ||
  321. parse_generic_hw_event(str, attr)))
  322. return 0;
  323. parse_event_modifier(str, attr);
  324. return 1;
  325. }
  326. int parse_events(const struct option *opt __used, const char *str, int unset __used)
  327. {
  328. struct perf_counter_attr attr;
  329. for (;;) {
  330. if (nr_counters == MAX_COUNTERS)
  331. return -1;
  332. memset(&attr, 0, sizeof(attr));
  333. if (!parse_event_symbols(&str, &attr))
  334. return -1;
  335. if (!(*str == 0 || *str == ',' || isspace(*str)))
  336. return -1;
  337. attrs[nr_counters] = attr;
  338. nr_counters++;
  339. if (*str == 0)
  340. break;
  341. if (*str == ',')
  342. ++str;
  343. while (isspace(*str))
  344. ++str;
  345. }
  346. return 0;
  347. }
  348. static const char * const event_type_descriptors[] = {
  349. "",
  350. "Hardware event",
  351. "Software event",
  352. "Tracepoint event",
  353. "Hardware cache event",
  354. };
  355. /*
  356. * Print the help text for the event symbols:
  357. */
  358. void print_events(void)
  359. {
  360. struct event_symbol *syms = event_symbols;
  361. unsigned int i, type, op, prev_type = -1;
  362. char name[40];
  363. fprintf(stderr, "\n");
  364. fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
  365. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  366. type = syms->type + 1;
  367. if (type > ARRAY_SIZE(event_type_descriptors))
  368. type = 0;
  369. if (type != prev_type)
  370. fprintf(stderr, "\n");
  371. if (strlen(syms->alias))
  372. sprintf(name, "%s OR %s", syms->symbol, syms->alias);
  373. else
  374. strcpy(name, syms->symbol);
  375. fprintf(stderr, " %-40s [%s]\n", name,
  376. event_type_descriptors[type]);
  377. prev_type = type;
  378. }
  379. fprintf(stderr, "\n");
  380. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  381. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  382. /* skip invalid cache type */
  383. if (!is_cache_op_valid(type, op))
  384. continue;
  385. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  386. fprintf(stderr, " %-40s [%s]\n",
  387. event_cache_name(type, op, i),
  388. event_type_descriptors[4]);
  389. }
  390. }
  391. }
  392. fprintf(stderr, "\n");
  393. fprintf(stderr, " %-40s [raw hardware event descriptor]\n",
  394. "rNNN");
  395. fprintf(stderr, "\n");
  396. exit(129);
  397. }