parse-events.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. int nr_counters;
  8. struct perf_counter_attr attrs[MAX_COUNTERS];
  9. struct event_symbol {
  10. __u8 type;
  11. __u64 config;
  12. char *symbol;
  13. };
  14. #define C(x, y) .type = PERF_TYPE_##x, .config = PERF_COUNT_##y
  15. static struct event_symbol event_symbols[] = {
  16. { C(HARDWARE, CPU_CYCLES), "cpu-cycles", },
  17. { C(HARDWARE, CPU_CYCLES), "cycles", },
  18. { C(HARDWARE, INSTRUCTIONS), "instructions", },
  19. { C(HARDWARE, CACHE_REFERENCES), "cache-references", },
  20. { C(HARDWARE, CACHE_MISSES), "cache-misses", },
  21. { C(HARDWARE, BRANCH_INSTRUCTIONS), "branch-instructions", },
  22. { C(HARDWARE, BRANCH_INSTRUCTIONS), "branches", },
  23. { C(HARDWARE, BRANCH_MISSES), "branch-misses", },
  24. { C(HARDWARE, BUS_CYCLES), "bus-cycles", },
  25. { C(SOFTWARE, CPU_CLOCK), "cpu-clock", },
  26. { C(SOFTWARE, TASK_CLOCK), "task-clock", },
  27. { C(SOFTWARE, PAGE_FAULTS), "page-faults", },
  28. { C(SOFTWARE, PAGE_FAULTS), "faults", },
  29. { C(SOFTWARE, PAGE_FAULTS_MIN), "minor-faults", },
  30. { C(SOFTWARE, PAGE_FAULTS_MAJ), "major-faults", },
  31. { C(SOFTWARE, CONTEXT_SWITCHES), "context-switches", },
  32. { C(SOFTWARE, CONTEXT_SWITCHES), "cs", },
  33. { C(SOFTWARE, CPU_MIGRATIONS), "cpu-migrations", },
  34. { C(SOFTWARE, CPU_MIGRATIONS), "migrations", },
  35. };
  36. #define __PERF_COUNTER_FIELD(config, name) \
  37. ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
  38. #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
  39. #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
  40. #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
  41. #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
  42. static char *hw_event_names[] = {
  43. "CPU cycles",
  44. "instructions",
  45. "cache references",
  46. "cache misses",
  47. "branches",
  48. "branch misses",
  49. "bus cycles",
  50. };
  51. static char *sw_event_names[] = {
  52. "cpu clock ticks",
  53. "task clock ticks",
  54. "pagefaults",
  55. "context switches",
  56. "CPU migrations",
  57. "minor faults",
  58. "major faults",
  59. };
  60. char *event_name(int counter)
  61. {
  62. __u64 config = attrs[counter].config;
  63. int type = attrs[counter].type;
  64. static char buf[32];
  65. if (attrs[counter].type == PERF_TYPE_RAW) {
  66. sprintf(buf, "raw 0x%llx", config);
  67. return buf;
  68. }
  69. switch (type) {
  70. case PERF_TYPE_HARDWARE:
  71. if (config < PERF_HW_EVENTS_MAX)
  72. return hw_event_names[config];
  73. return "unknown-hardware";
  74. case PERF_TYPE_SOFTWARE:
  75. if (config < PERF_SW_EVENTS_MAX)
  76. return sw_event_names[config];
  77. return "unknown-software";
  78. default:
  79. break;
  80. }
  81. return "unknown";
  82. }
  83. /*
  84. * Each event can have multiple symbolic names.
  85. * Symbolic names are (almost) exactly matched.
  86. */
  87. static int match_event_symbols(const char *str, struct perf_counter_attr *attr)
  88. {
  89. __u64 config, id;
  90. int type;
  91. unsigned int i;
  92. const char *sep, *pstr;
  93. if (str[0] == 'r' && hex2u64(str + 1, &config) > 0) {
  94. attr->type = PERF_TYPE_RAW;
  95. attr->config = config;
  96. return 0;
  97. }
  98. pstr = str;
  99. sep = strchr(pstr, ':');
  100. if (sep) {
  101. type = atoi(pstr);
  102. pstr = sep + 1;
  103. id = atoi(pstr);
  104. sep = strchr(pstr, ':');
  105. if (sep) {
  106. pstr = sep + 1;
  107. if (strchr(pstr, 'k'))
  108. attr->exclude_user = 1;
  109. if (strchr(pstr, 'u'))
  110. attr->exclude_kernel = 1;
  111. }
  112. attr->type = type;
  113. attr->config = id;
  114. return 0;
  115. }
  116. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  117. if (!strncmp(str, event_symbols[i].symbol,
  118. strlen(event_symbols[i].symbol))) {
  119. attr->type = event_symbols[i].type;
  120. attr->config = event_symbols[i].config;
  121. return 0;
  122. }
  123. }
  124. return -EINVAL;
  125. }
  126. int parse_events(const struct option *opt, const char *str, int unset)
  127. {
  128. struct perf_counter_attr attr;
  129. int ret;
  130. memset(&attr, 0, sizeof(attr));
  131. again:
  132. if (nr_counters == MAX_COUNTERS)
  133. return -1;
  134. ret = match_event_symbols(str, &attr);
  135. if (ret < 0)
  136. return ret;
  137. attrs[nr_counters] = attr;
  138. nr_counters++;
  139. str = strstr(str, ",");
  140. if (str) {
  141. str++;
  142. goto again;
  143. }
  144. return 0;
  145. }
  146. /*
  147. * Create the help text for the event symbols:
  148. */
  149. void create_events_help(char *events_help_msg)
  150. {
  151. unsigned int i;
  152. char *str;
  153. str = events_help_msg;
  154. str += sprintf(str,
  155. "event name: [");
  156. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  157. int type, id;
  158. type = event_symbols[i].type;
  159. id = event_symbols[i].config;
  160. if (i)
  161. str += sprintf(str, "|");
  162. str += sprintf(str, "%s",
  163. event_symbols[i].symbol);
  164. }
  165. str += sprintf(str, "|rNNN]");
  166. }