parse-events.c 7.3 KB

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