parse-events.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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-d$", "l1-d", "l1d", "L1-data", },
  61. { "L1-i$", "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. for (i = 0; i < size; i++) {
  156. for (j = 0; j < MAX_ALIASES; j++) {
  157. if (!names[i][j])
  158. break;
  159. if (strcasestr(str, names[i][j]))
  160. return i;
  161. }
  162. }
  163. return -1;
  164. }
  165. static int
  166. parse_generic_hw_symbols(const char *str, struct perf_counter_attr *attr)
  167. {
  168. int cache_type = -1, cache_op = 0, cache_result = 0;
  169. cache_type = parse_aliases(str, hw_cache, PERF_COUNT_HW_CACHE_MAX);
  170. /*
  171. * No fallback - if we cannot get a clear cache type
  172. * then bail out:
  173. */
  174. if (cache_type == -1)
  175. return -EINVAL;
  176. cache_op = parse_aliases(str, hw_cache_op, PERF_COUNT_HW_CACHE_OP_MAX);
  177. /*
  178. * Fall back to reads:
  179. */
  180. if (cache_op == -1)
  181. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  182. if (!is_cache_op_valid(cache_type, cache_op))
  183. return -EINVAL;
  184. cache_result = parse_aliases(str, hw_cache_result,
  185. PERF_COUNT_HW_CACHE_RESULT_MAX);
  186. /*
  187. * Fall back to accesses:
  188. */
  189. if (cache_result == -1)
  190. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  191. attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
  192. attr->type = PERF_TYPE_HW_CACHE;
  193. return 0;
  194. }
  195. static int check_events(const char *str, unsigned int i)
  196. {
  197. if (!strncmp(str, event_symbols[i].symbol,
  198. strlen(event_symbols[i].symbol)))
  199. return 1;
  200. if (strlen(event_symbols[i].alias))
  201. if (!strncmp(str, event_symbols[i].alias,
  202. strlen(event_symbols[i].alias)))
  203. return 1;
  204. return 0;
  205. }
  206. /*
  207. * Each event can have multiple symbolic names.
  208. * Symbolic names are (almost) exactly matched.
  209. */
  210. static int parse_event_symbols(const char *str, struct perf_counter_attr *attr)
  211. {
  212. u64 config, id;
  213. int type;
  214. unsigned int i;
  215. const char *sep, *pstr;
  216. if (str[0] == 'r' && hex2u64(str + 1, &config) > 0) {
  217. attr->type = PERF_TYPE_RAW;
  218. attr->config = config;
  219. return 0;
  220. }
  221. pstr = str;
  222. sep = strchr(pstr, ':');
  223. if (sep) {
  224. type = atoi(pstr);
  225. pstr = sep + 1;
  226. id = atoi(pstr);
  227. sep = strchr(pstr, ':');
  228. if (sep) {
  229. pstr = sep + 1;
  230. if (strchr(pstr, 'k'))
  231. attr->exclude_user = 1;
  232. if (strchr(pstr, 'u'))
  233. attr->exclude_kernel = 1;
  234. }
  235. attr->type = type;
  236. attr->config = id;
  237. return 0;
  238. }
  239. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  240. if (check_events(str, i)) {
  241. attr->type = event_symbols[i].type;
  242. attr->config = event_symbols[i].config;
  243. return 0;
  244. }
  245. }
  246. return parse_generic_hw_symbols(str, attr);
  247. }
  248. int parse_events(const struct option *opt, const char *str, int unset)
  249. {
  250. struct perf_counter_attr attr;
  251. int ret;
  252. memset(&attr, 0, sizeof(attr));
  253. again:
  254. if (nr_counters == MAX_COUNTERS)
  255. return -1;
  256. ret = parse_event_symbols(str, &attr);
  257. if (ret < 0)
  258. return ret;
  259. attrs[nr_counters] = attr;
  260. nr_counters++;
  261. str = strstr(str, ",");
  262. if (str) {
  263. str++;
  264. goto again;
  265. }
  266. return 0;
  267. }
  268. static const char * const event_type_descriptors[] = {
  269. "",
  270. "Hardware event",
  271. "Software event",
  272. "Tracepoint event",
  273. "Hardware cache event",
  274. };
  275. /*
  276. * Print the help text for the event symbols:
  277. */
  278. void print_events(void)
  279. {
  280. struct event_symbol *syms = event_symbols;
  281. unsigned int i, type, prev_type = -1;
  282. char name[40];
  283. fprintf(stderr, "\n");
  284. fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
  285. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  286. type = syms->type + 1;
  287. if (type > ARRAY_SIZE(event_type_descriptors))
  288. type = 0;
  289. if (type != prev_type)
  290. fprintf(stderr, "\n");
  291. if (strlen(syms->alias))
  292. sprintf(name, "%s OR %s", syms->symbol, syms->alias);
  293. else
  294. strcpy(name, syms->symbol);
  295. fprintf(stderr, " %-40s [%s]\n", name,
  296. event_type_descriptors[type]);
  297. prev_type = type;
  298. }
  299. fprintf(stderr, "\n");
  300. fprintf(stderr, " %-40s [raw hardware event descriptor]\n",
  301. "rNNN");
  302. fprintf(stderr, "\n");
  303. exit(129);
  304. }