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", "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-data", "l1-d", "l1d" },
  61. { "L1-instruction", "l1-i", "l1i" },
  62. { "L2", "l2" },
  63. { "Data-TLB", "dtlb", "d-tlb" },
  64. { "Instruction-TLB", "itlb", "i-tlb" },
  65. { "Branch", "bpu" , "btb", "bpc" },
  66. };
  67. static char *hw_cache_op[][MAX_ALIASES] = {
  68. { "Load", "read" },
  69. { "Store", "write" },
  70. { "Prefetch", "speculative-read", "speculative-load" },
  71. };
  72. static char *hw_cache_result[][MAX_ALIASES] = {
  73. { "Reference", "ops", "access" },
  74. { "Miss" },
  75. };
  76. char *event_name(int counter)
  77. {
  78. u64 config = attrs[counter].config;
  79. int type = attrs[counter].type;
  80. static char buf[32];
  81. if (attrs[counter].type == PERF_TYPE_RAW) {
  82. sprintf(buf, "raw 0x%llx", config);
  83. return buf;
  84. }
  85. switch (type) {
  86. case PERF_TYPE_HARDWARE:
  87. if (config < PERF_COUNT_HW_MAX)
  88. return hw_event_names[config];
  89. return "unknown-hardware";
  90. case PERF_TYPE_HW_CACHE: {
  91. u8 cache_type, cache_op, cache_result;
  92. static char name[100];
  93. cache_type = (config >> 0) & 0xff;
  94. if (cache_type > PERF_COUNT_HW_CACHE_MAX)
  95. return "unknown-ext-hardware-cache-type";
  96. cache_op = (config >> 8) & 0xff;
  97. if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
  98. return "unknown-ext-hardware-cache-op";
  99. cache_result = (config >> 16) & 0xff;
  100. if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
  101. return "unknown-ext-hardware-cache-result";
  102. sprintf(name, "%s-Cache-%s-%ses",
  103. hw_cache[cache_type][0],
  104. hw_cache_op[cache_op][0],
  105. hw_cache_result[cache_result][0]);
  106. return name;
  107. }
  108. case PERF_TYPE_SOFTWARE:
  109. if (config < PERF_COUNT_SW_MAX)
  110. return sw_event_names[config];
  111. return "unknown-software";
  112. default:
  113. break;
  114. }
  115. return "unknown";
  116. }
  117. static int parse_aliases(const char *str, char *names[][MAX_ALIASES], int size)
  118. {
  119. int i, j;
  120. for (i = 0; i < size; i++) {
  121. for (j = 0; j < MAX_ALIASES; j++) {
  122. if (!names[i][j])
  123. break;
  124. if (strcasestr(str, names[i][j]))
  125. return i;
  126. }
  127. }
  128. return -1;
  129. }
  130. static int
  131. 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. }