parse-events.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. };
  15. #define C(x, y) .type = PERF_TYPE_##x, .config = PERF_COUNT_##y
  16. #define CR(x, y) .type = PERF_TYPE_##x, .config = y
  17. static struct event_symbol event_symbols[] = {
  18. { C(HARDWARE, HW_CPU_CYCLES), "cpu-cycles", },
  19. { C(HARDWARE, HW_CPU_CYCLES), "cycles", },
  20. { C(HARDWARE, HW_INSTRUCTIONS), "instructions", },
  21. { C(HARDWARE, HW_CACHE_REFERENCES), "cache-references", },
  22. { C(HARDWARE, HW_CACHE_MISSES), "cache-misses", },
  23. { C(HARDWARE, HW_BRANCH_INSTRUCTIONS),"branch-instructions", },
  24. { C(HARDWARE, HW_BRANCH_INSTRUCTIONS),"branches", },
  25. { C(HARDWARE, HW_BRANCH_MISSES), "branch-misses", },
  26. { C(HARDWARE, HW_BUS_CYCLES), "bus-cycles", },
  27. { C(SOFTWARE, SW_CPU_CLOCK), "cpu-clock", },
  28. { C(SOFTWARE, SW_TASK_CLOCK), "task-clock", },
  29. { C(SOFTWARE, SW_PAGE_FAULTS), "page-faults", },
  30. { C(SOFTWARE, SW_PAGE_FAULTS), "faults", },
  31. { C(SOFTWARE, SW_PAGE_FAULTS_MIN), "minor-faults", },
  32. { C(SOFTWARE, SW_PAGE_FAULTS_MAJ), "major-faults", },
  33. { C(SOFTWARE, SW_CONTEXT_SWITCHES), "context-switches", },
  34. { C(SOFTWARE, SW_CONTEXT_SWITCHES), "cs", },
  35. { C(SOFTWARE, SW_CPU_MIGRATIONS), "cpu-migrations", },
  36. { C(SOFTWARE, SW_CPU_MIGRATIONS), "migrations", },
  37. };
  38. #define __PERF_COUNTER_FIELD(config, name) \
  39. ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
  40. #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
  41. #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
  42. #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
  43. #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
  44. static char *hw_event_names[] = {
  45. "cycles",
  46. "instructions",
  47. "cache-references",
  48. "cache-misses",
  49. "branches",
  50. "branch-misses",
  51. "bus-cycles",
  52. };
  53. static char *sw_event_names[] = {
  54. "cpu-clock-ticks",
  55. "task-clock-ticks",
  56. "page-faults",
  57. "context-switches",
  58. "CPU-migrations",
  59. "minor-faults",
  60. "major-faults",
  61. };
  62. #define MAX_ALIASES 8
  63. static char *hw_cache [][MAX_ALIASES] = {
  64. { "L1-data" , "l1-d", "l1d" },
  65. { "L1-instruction" , "l1-i", "l1i" },
  66. { "L2" , "l2" },
  67. { "Data-TLB" , "dtlb", "d-tlb" },
  68. { "Instruction-TLB" , "itlb", "i-tlb" },
  69. { "Branch" , "bpu" , "btb", "bpc" },
  70. };
  71. static char *hw_cache_op [][MAX_ALIASES] = {
  72. { "Load" , "read" },
  73. { "Store" , "write" },
  74. { "Prefetch" , "speculative-read", "speculative-load" },
  75. };
  76. static char *hw_cache_result [][MAX_ALIASES] = {
  77. { "Reference" , "ops", "access" },
  78. { "Miss" },
  79. };
  80. char *event_name(int counter)
  81. {
  82. __u64 config = attrs[counter].config;
  83. int type = attrs[counter].type;
  84. static char buf[32];
  85. if (attrs[counter].type == PERF_TYPE_RAW) {
  86. sprintf(buf, "raw 0x%llx", config);
  87. return buf;
  88. }
  89. switch (type) {
  90. case PERF_TYPE_HARDWARE:
  91. if (config < PERF_COUNT_HW_MAX)
  92. return hw_event_names[config];
  93. return "unknown-hardware";
  94. case PERF_TYPE_HW_CACHE: {
  95. __u8 cache_type, cache_op, cache_result;
  96. static char name[100];
  97. cache_type = (config >> 0) & 0xff;
  98. if (cache_type > PERF_COUNT_HW_CACHE_MAX)
  99. return "unknown-ext-hardware-cache-type";
  100. cache_op = (config >> 8) & 0xff;
  101. if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
  102. return "unknown-ext-hardware-cache-op";
  103. cache_result = (config >> 16) & 0xff;
  104. if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
  105. return "unknown-ext-hardware-cache-result";
  106. sprintf(name, "%s-Cache-%s-%ses",
  107. hw_cache[cache_type][0],
  108. hw_cache_op[cache_op][0],
  109. hw_cache_result[cache_result][0]);
  110. return name;
  111. }
  112. case PERF_TYPE_SOFTWARE:
  113. if (config < PERF_COUNT_SW_MAX)
  114. return sw_event_names[config];
  115. return "unknown-software";
  116. default:
  117. break;
  118. }
  119. return "unknown";
  120. }
  121. static int parse_aliases(const char *str, char *names[][MAX_ALIASES], int size)
  122. {
  123. int i, j;
  124. for (i = 0; i < size; i++) {
  125. for (j = 0; j < MAX_ALIASES; j++) {
  126. if (!names[i][j])
  127. break;
  128. if (strcasestr(str, names[i][j]))
  129. return i;
  130. }
  131. }
  132. return -1;
  133. }
  134. static int parse_generic_hw_symbols(const char *str, struct perf_counter_attr *attr)
  135. {
  136. int cache_type = -1, cache_op = 0, cache_result = 0;
  137. cache_type = parse_aliases(str, hw_cache, PERF_COUNT_HW_CACHE_MAX);
  138. /*
  139. * No fallback - if we cannot get a clear cache type
  140. * then bail out:
  141. */
  142. if (cache_type == -1)
  143. return -EINVAL;
  144. cache_op = parse_aliases(str, hw_cache_op, PERF_COUNT_HW_CACHE_OP_MAX);
  145. /*
  146. * Fall back to reads:
  147. */
  148. if (cache_op == -1)
  149. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  150. cache_result = parse_aliases(str, hw_cache_result,
  151. PERF_COUNT_HW_CACHE_RESULT_MAX);
  152. /*
  153. * Fall back to accesses:
  154. */
  155. if (cache_result == -1)
  156. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  157. attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
  158. attr->type = PERF_TYPE_HW_CACHE;
  159. return 0;
  160. }
  161. /*
  162. * Each event can have multiple symbolic names.
  163. * Symbolic names are (almost) exactly matched.
  164. */
  165. static int parse_event_symbols(const char *str, struct perf_counter_attr *attr)
  166. {
  167. __u64 config, id;
  168. int type;
  169. unsigned int i;
  170. const char *sep, *pstr;
  171. if (str[0] == 'r' && hex2u64(str + 1, &config) > 0) {
  172. attr->type = PERF_TYPE_RAW;
  173. attr->config = config;
  174. return 0;
  175. }
  176. pstr = str;
  177. sep = strchr(pstr, ':');
  178. if (sep) {
  179. type = atoi(pstr);
  180. pstr = sep + 1;
  181. id = atoi(pstr);
  182. sep = strchr(pstr, ':');
  183. if (sep) {
  184. pstr = sep + 1;
  185. if (strchr(pstr, 'k'))
  186. attr->exclude_user = 1;
  187. if (strchr(pstr, 'u'))
  188. attr->exclude_kernel = 1;
  189. }
  190. attr->type = type;
  191. attr->config = id;
  192. return 0;
  193. }
  194. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  195. if (!strncmp(str, event_symbols[i].symbol,
  196. strlen(event_symbols[i].symbol))) {
  197. attr->type = event_symbols[i].type;
  198. attr->config = event_symbols[i].config;
  199. return 0;
  200. }
  201. }
  202. return parse_generic_hw_symbols(str, attr);
  203. }
  204. int parse_events(const struct option *opt, const char *str, int unset)
  205. {
  206. struct perf_counter_attr attr;
  207. int ret;
  208. memset(&attr, 0, sizeof(attr));
  209. again:
  210. if (nr_counters == MAX_COUNTERS)
  211. return -1;
  212. ret = parse_event_symbols(str, &attr);
  213. if (ret < 0)
  214. return ret;
  215. attrs[nr_counters] = attr;
  216. nr_counters++;
  217. str = strstr(str, ",");
  218. if (str) {
  219. str++;
  220. goto again;
  221. }
  222. return 0;
  223. }
  224. static const char * const event_type_descriptors[] = {
  225. "",
  226. "Hardware event",
  227. "Software event",
  228. "Tracepoint event",
  229. "Hardware cache event",
  230. };
  231. /*
  232. * Print the help text for the event symbols:
  233. */
  234. void print_events(void)
  235. {
  236. struct event_symbol *syms = event_symbols;
  237. unsigned int i, type, prev_type = -1;
  238. fprintf(stderr, "\n");
  239. fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
  240. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  241. type = syms->type + 1;
  242. if (type > ARRAY_SIZE(event_type_descriptors))
  243. type = 0;
  244. if (type != prev_type)
  245. fprintf(stderr, "\n");
  246. fprintf(stderr, " %-30s [%s]\n", syms->symbol,
  247. event_type_descriptors[type]);
  248. prev_type = type;
  249. }
  250. fprintf(stderr, "\n");
  251. fprintf(stderr, " %-30s [raw hardware event descriptor]\n",
  252. "rNNN");
  253. fprintf(stderr, "\n");
  254. exit(129);
  255. }