parse-events.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. __u64 event_id[MAX_COUNTERS] = { };
  9. int event_mask[MAX_COUNTERS];
  10. struct event_symbol {
  11. __u64 event;
  12. char *symbol;
  13. };
  14. static struct event_symbol event_symbols[] = {
  15. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES), "cpu-cycles", },
  16. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES), "cycles", },
  17. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS), "instructions", },
  18. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES), "cache-references", },
  19. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES), "cache-misses", },
  20. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_INSTRUCTIONS), "branch-instructions", },
  21. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_INSTRUCTIONS), "branches", },
  22. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_MISSES), "branch-misses", },
  23. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BUS_CYCLES), "bus-cycles", },
  24. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_CLOCK), "cpu-clock", },
  25. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK), "task-clock", },
  26. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS), "page-faults", },
  27. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS), "faults", },
  28. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS_MIN), "minor-faults", },
  29. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS_MAJ), "major-faults", },
  30. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES), "context-switches", },
  31. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES), "cs", },
  32. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS), "cpu-migrations", },
  33. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_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. "CPU 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 ticks",
  52. "task clock ticks",
  53. "pagefaults",
  54. "context switches",
  55. "CPU migrations",
  56. "minor faults",
  57. "major faults",
  58. };
  59. char *event_name(int ctr)
  60. {
  61. __u64 config = event_id[ctr];
  62. int type = PERF_COUNTER_TYPE(config);
  63. int id = PERF_COUNTER_ID(config);
  64. static char buf[32];
  65. if (PERF_COUNTER_RAW(config)) {
  66. sprintf(buf, "raw 0x%llx", PERF_COUNTER_CONFIG(config));
  67. return buf;
  68. }
  69. switch (type) {
  70. case PERF_TYPE_HARDWARE:
  71. if (id < PERF_HW_EVENTS_MAX)
  72. return hw_event_names[id];
  73. return "unknown-hardware";
  74. case PERF_TYPE_SOFTWARE:
  75. if (id < PERF_SW_EVENTS_MAX)
  76. return sw_event_names[id];
  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 __u64 match_event_symbols(const char *str)
  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. return config | PERF_COUNTER_RAW_MASK;
  95. pstr = str;
  96. sep = strchr(pstr, ':');
  97. if (sep) {
  98. type = atoi(pstr);
  99. pstr = sep + 1;
  100. id = atoi(pstr);
  101. sep = strchr(pstr, ':');
  102. if (sep) {
  103. pstr = sep + 1;
  104. if (strchr(pstr, 'k'))
  105. event_mask[nr_counters] |= EVENT_MASK_USER;
  106. if (strchr(pstr, 'u'))
  107. event_mask[nr_counters] |= EVENT_MASK_KERNEL;
  108. }
  109. return EID(type, id);
  110. }
  111. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  112. if (!strncmp(str, event_symbols[i].symbol,
  113. strlen(event_symbols[i].symbol)))
  114. return event_symbols[i].event;
  115. }
  116. return ~0ULL;
  117. }
  118. int parse_events(const struct option *opt, const char *str, int unset)
  119. {
  120. __u64 config;
  121. again:
  122. if (nr_counters == MAX_COUNTERS)
  123. return -1;
  124. config = match_event_symbols(str);
  125. if (config == ~0ULL)
  126. return -1;
  127. event_id[nr_counters] = config;
  128. nr_counters++;
  129. str = strstr(str, ",");
  130. if (str) {
  131. str++;
  132. goto again;
  133. }
  134. return 0;
  135. }
  136. /*
  137. * Create the help text for the event symbols:
  138. */
  139. void create_events_help(char *events_help_msg)
  140. {
  141. unsigned int i;
  142. char *str;
  143. __u64 e;
  144. str = events_help_msg;
  145. str += sprintf(str,
  146. "event name: [");
  147. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  148. int type, id;
  149. e = event_symbols[i].event;
  150. type = PERF_COUNTER_TYPE(e);
  151. id = PERF_COUNTER_ID(e);
  152. if (i)
  153. str += sprintf(str, "|");
  154. str += sprintf(str, "%s",
  155. event_symbols[i].symbol);
  156. }
  157. str += sprintf(str, "|rNNN]");
  158. }