Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 1 | |
| 2 | #include "../perf.h" |
| 3 | #include "util.h" |
| 4 | #include "parse-options.h" |
| 5 | #include "parse-events.h" |
| 6 | #include "exec_cmd.h" |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 7 | #include "string.h" |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 8 | #include "cache.h" |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 9 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 10 | extern char *strcasestr(const char *haystack, const char *needle); |
| 11 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 12 | int nr_counters; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 13 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 14 | struct perf_counter_attr attrs[MAX_COUNTERS]; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 15 | |
| 16 | struct event_symbol { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 17 | u8 type; |
| 18 | u64 config; |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 19 | char *symbol; |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 20 | char *alias; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 21 | }; |
| 22 | |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 23 | char debugfs_path[MAXPATHLEN]; |
| 24 | |
Jaswinder Singh Rajput | 51e2684 | 2009-06-22 16:43:14 +0530 | [diff] [blame] | 25 | #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x |
| 26 | #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 27 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 28 | static struct event_symbol event_symbols[] = { |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 29 | { CHW(CPU_CYCLES), "cpu-cycles", "cycles" }, |
| 30 | { CHW(INSTRUCTIONS), "instructions", "" }, |
| 31 | { CHW(CACHE_REFERENCES), "cache-references", "" }, |
| 32 | { CHW(CACHE_MISSES), "cache-misses", "" }, |
| 33 | { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" }, |
| 34 | { CHW(BRANCH_MISSES), "branch-misses", "" }, |
| 35 | { CHW(BUS_CYCLES), "bus-cycles", "" }, |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 36 | |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 37 | { CSW(CPU_CLOCK), "cpu-clock", "" }, |
| 38 | { CSW(TASK_CLOCK), "task-clock", "" }, |
Jaswinder Singh Rajput | c0c22db | 2009-06-22 20:47:26 +0530 | [diff] [blame] | 39 | { CSW(PAGE_FAULTS), "page-faults", "faults" }, |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 40 | { CSW(PAGE_FAULTS_MIN), "minor-faults", "" }, |
| 41 | { CSW(PAGE_FAULTS_MAJ), "major-faults", "" }, |
| 42 | { CSW(CONTEXT_SWITCHES), "context-switches", "cs" }, |
| 43 | { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" }, |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 44 | }; |
| 45 | |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 46 | #define __PERF_COUNTER_FIELD(config, name) \ |
| 47 | ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT) |
| 48 | |
| 49 | #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW) |
| 50 | #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG) |
| 51 | #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE) |
| 52 | #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT) |
| 53 | |
| 54 | static char *hw_event_names[] = { |
Ingo Molnar | 8faf3b5 | 2009-06-06 13:58:12 +0200 | [diff] [blame] | 55 | "cycles", |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 56 | "instructions", |
Ingo Molnar | 8faf3b5 | 2009-06-06 13:58:12 +0200 | [diff] [blame] | 57 | "cache-references", |
| 58 | "cache-misses", |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 59 | "branches", |
Ingo Molnar | 8faf3b5 | 2009-06-06 13:58:12 +0200 | [diff] [blame] | 60 | "branch-misses", |
| 61 | "bus-cycles", |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | static char *sw_event_names[] = { |
Ingo Molnar | 44175b6 | 2009-06-13 13:35:00 +0200 | [diff] [blame] | 65 | "cpu-clock-msecs", |
| 66 | "task-clock-msecs", |
Ingo Molnar | 8faf3b5 | 2009-06-06 13:58:12 +0200 | [diff] [blame] | 67 | "page-faults", |
| 68 | "context-switches", |
| 69 | "CPU-migrations", |
| 70 | "minor-faults", |
| 71 | "major-faults", |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 72 | }; |
| 73 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 74 | #define MAX_ALIASES 8 |
| 75 | |
Jaswinder Singh Rajput | c0c22db | 2009-06-22 20:47:26 +0530 | [diff] [blame] | 76 | static char *hw_cache[][MAX_ALIASES] = { |
Anton Blanchard | 9590b7b | 2009-07-06 22:01:31 +1000 | [diff] [blame] | 77 | { "L1-dcache", "l1-d", "l1d", "L1-data", }, |
| 78 | { "L1-icache", "l1-i", "l1i", "L1-instruction", }, |
Jaswinder Singh Rajput | e5c5954 | 2009-06-25 18:25:22 +0530 | [diff] [blame] | 79 | { "LLC", "L2" }, |
| 80 | { "dTLB", "d-tlb", "Data-TLB", }, |
| 81 | { "iTLB", "i-tlb", "Instruction-TLB", }, |
| 82 | { "branch", "branches", "bpu", "btb", "bpc", }, |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 83 | }; |
| 84 | |
Jaswinder Singh Rajput | c0c22db | 2009-06-22 20:47:26 +0530 | [diff] [blame] | 85 | static char *hw_cache_op[][MAX_ALIASES] = { |
Jaswinder Singh Rajput | e5c5954 | 2009-06-25 18:25:22 +0530 | [diff] [blame] | 86 | { "load", "loads", "read", }, |
| 87 | { "store", "stores", "write", }, |
| 88 | { "prefetch", "prefetches", "speculative-read", "speculative-load", }, |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
Jaswinder Singh Rajput | c0c22db | 2009-06-22 20:47:26 +0530 | [diff] [blame] | 91 | static char *hw_cache_result[][MAX_ALIASES] = { |
Jaswinder Singh Rajput | e5c5954 | 2009-06-25 18:25:22 +0530 | [diff] [blame] | 92 | { "refs", "Reference", "ops", "access", }, |
| 93 | { "misses", "miss", }, |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
Jaswinder Singh Rajput | 06813f6 | 2009-06-25 17:16:07 +0530 | [diff] [blame] | 96 | #define C(x) PERF_COUNT_HW_CACHE_##x |
| 97 | #define CACHE_READ (1 << C(OP_READ)) |
| 98 | #define CACHE_WRITE (1 << C(OP_WRITE)) |
| 99 | #define CACHE_PREFETCH (1 << C(OP_PREFETCH)) |
| 100 | #define COP(x) (1 << x) |
| 101 | |
| 102 | /* |
| 103 | * cache operartion stat |
| 104 | * L1I : Read and prefetch only |
| 105 | * ITLB and BPU : Read-only |
| 106 | */ |
| 107 | static unsigned long hw_cache_stat[C(MAX)] = { |
| 108 | [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), |
| 109 | [C(L1I)] = (CACHE_READ | CACHE_PREFETCH), |
| 110 | [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), |
| 111 | [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), |
| 112 | [C(ITLB)] = (CACHE_READ), |
| 113 | [C(BPU)] = (CACHE_READ), |
| 114 | }; |
| 115 | |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 116 | #define for_each_subsystem(sys_dir, sys_dirent, sys_next, file, st) \ |
| 117 | while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \ |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 118 | if (snprintf(file, MAXPATHLEN, "%s/%s", debugfs_path, \ |
| 119 | sys_dirent.d_name) && \ |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 120 | (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \ |
| 121 | (strcmp(sys_dirent.d_name, ".")) && \ |
| 122 | (strcmp(sys_dirent.d_name, ".."))) |
| 123 | |
Peter Zijlstra | ae07b63 | 2009-08-06 16:48:54 +0200 | [diff] [blame^] | 124 | static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir) |
| 125 | { |
| 126 | char evt_path[MAXPATHLEN]; |
| 127 | int fd; |
| 128 | |
| 129 | snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path, |
| 130 | sys_dir->d_name, evt_dir->d_name); |
| 131 | fd = open(evt_path, O_RDONLY); |
| 132 | if (fd < 0) |
| 133 | return -EINVAL; |
| 134 | close(fd); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 139 | #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next, file, st) \ |
| 140 | while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \ |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 141 | if (snprintf(file, MAXPATHLEN, "%s/%s/%s", debugfs_path, \ |
| 142 | sys_dirent.d_name, evt_dirent.d_name) && \ |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 143 | (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \ |
| 144 | (strcmp(evt_dirent.d_name, ".")) && \ |
Peter Zijlstra | ae07b63 | 2009-08-06 16:48:54 +0200 | [diff] [blame^] | 145 | (strcmp(evt_dirent.d_name, "..")) && \ |
| 146 | (!tp_event_has_id(&sys_dirent, &evt_dirent))) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 147 | |
| 148 | #define MAX_EVENT_LENGTH 30 |
| 149 | |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 150 | int valid_debugfs_mount(const char *debugfs) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 151 | { |
| 152 | struct statfs st_fs; |
| 153 | |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 154 | if (statfs(debugfs, &st_fs) < 0) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 155 | return -ENOENT; |
| 156 | else if (st_fs.f_type != (long) DEBUGFS_MAGIC) |
| 157 | return -ENOENT; |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static char *tracepoint_id_to_name(u64 config) |
| 162 | { |
| 163 | static char tracepoint_name[2 * MAX_EVENT_LENGTH]; |
| 164 | DIR *sys_dir, *evt_dir; |
| 165 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; |
| 166 | struct stat st; |
| 167 | char id_buf[4]; |
| 168 | int fd; |
| 169 | u64 id; |
| 170 | char evt_path[MAXPATHLEN]; |
| 171 | |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 172 | if (valid_debugfs_mount(debugfs_path)) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 173 | return "unkown"; |
| 174 | |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 175 | sys_dir = opendir(debugfs_path); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 176 | if (!sys_dir) |
| 177 | goto cleanup; |
| 178 | |
| 179 | for_each_subsystem(sys_dir, sys_dirent, sys_next, evt_path, st) { |
| 180 | evt_dir = opendir(evt_path); |
| 181 | if (!evt_dir) |
| 182 | goto cleanup; |
| 183 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next, |
| 184 | evt_path, st) { |
| 185 | snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 186 | debugfs_path, sys_dirent.d_name, |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 187 | evt_dirent.d_name); |
| 188 | fd = open(evt_path, O_RDONLY); |
| 189 | if (fd < 0) |
| 190 | continue; |
| 191 | if (read(fd, id_buf, sizeof(id_buf)) < 0) { |
| 192 | close(fd); |
| 193 | continue; |
| 194 | } |
| 195 | close(fd); |
| 196 | id = atoll(id_buf); |
| 197 | if (id == config) { |
| 198 | closedir(evt_dir); |
| 199 | closedir(sys_dir); |
| 200 | snprintf(tracepoint_name, 2 * MAX_EVENT_LENGTH, |
| 201 | "%s:%s", sys_dirent.d_name, |
| 202 | evt_dirent.d_name); |
| 203 | return tracepoint_name; |
| 204 | } |
| 205 | } |
| 206 | closedir(evt_dir); |
| 207 | } |
| 208 | |
| 209 | cleanup: |
| 210 | closedir(sys_dir); |
| 211 | return "unkown"; |
| 212 | } |
| 213 | |
Jaswinder Singh Rajput | 06813f6 | 2009-06-25 17:16:07 +0530 | [diff] [blame] | 214 | static int is_cache_op_valid(u8 cache_type, u8 cache_op) |
| 215 | { |
| 216 | if (hw_cache_stat[cache_type] & COP(cache_op)) |
| 217 | return 1; /* valid */ |
| 218 | else |
| 219 | return 0; /* invalid */ |
| 220 | } |
| 221 | |
Jaswinder Singh Rajput | e5c5954 | 2009-06-25 18:25:22 +0530 | [diff] [blame] | 222 | static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result) |
| 223 | { |
| 224 | static char name[50]; |
| 225 | |
| 226 | if (cache_result) { |
| 227 | sprintf(name, "%s-%s-%s", hw_cache[cache_type][0], |
| 228 | hw_cache_op[cache_op][0], |
| 229 | hw_cache_result[cache_result][0]); |
| 230 | } else { |
| 231 | sprintf(name, "%s-%s", hw_cache[cache_type][0], |
| 232 | hw_cache_op[cache_op][1]); |
| 233 | } |
| 234 | |
| 235 | return name; |
| 236 | } |
| 237 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 238 | char *event_name(int counter) |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 239 | { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 240 | u64 config = attrs[counter].config; |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 241 | int type = attrs[counter].type; |
Peter Zijlstra | 8f18aec | 2009-08-06 19:40:28 +0200 | [diff] [blame] | 242 | |
| 243 | return __event_name(type, config); |
| 244 | } |
| 245 | |
| 246 | char *__event_name(int type, u64 config) |
| 247 | { |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 248 | static char buf[32]; |
| 249 | |
Peter Zijlstra | 8f18aec | 2009-08-06 19:40:28 +0200 | [diff] [blame] | 250 | if (type == PERF_TYPE_RAW) { |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 251 | sprintf(buf, "raw 0x%llx", config); |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 252 | return buf; |
| 253 | } |
| 254 | |
| 255 | switch (type) { |
| 256 | case PERF_TYPE_HARDWARE: |
Peter Zijlstra | f4dbfa8 | 2009-06-11 14:06:28 +0200 | [diff] [blame] | 257 | if (config < PERF_COUNT_HW_MAX) |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 258 | return hw_event_names[config]; |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 259 | return "unknown-hardware"; |
| 260 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 261 | case PERF_TYPE_HW_CACHE: { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 262 | u8 cache_type, cache_op, cache_result; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 263 | |
| 264 | cache_type = (config >> 0) & 0xff; |
| 265 | if (cache_type > PERF_COUNT_HW_CACHE_MAX) |
| 266 | return "unknown-ext-hardware-cache-type"; |
| 267 | |
| 268 | cache_op = (config >> 8) & 0xff; |
Ingo Molnar | 8faf3b5 | 2009-06-06 13:58:12 +0200 | [diff] [blame] | 269 | if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX) |
| 270 | return "unknown-ext-hardware-cache-op"; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 271 | |
| 272 | cache_result = (config >> 16) & 0xff; |
Ingo Molnar | 8faf3b5 | 2009-06-06 13:58:12 +0200 | [diff] [blame] | 273 | if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX) |
| 274 | return "unknown-ext-hardware-cache-result"; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 275 | |
Jaswinder Singh Rajput | 06813f6 | 2009-06-25 17:16:07 +0530 | [diff] [blame] | 276 | if (!is_cache_op_valid(cache_type, cache_op)) |
| 277 | return "invalid-cache"; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 278 | |
Jaswinder Singh Rajput | e5c5954 | 2009-06-25 18:25:22 +0530 | [diff] [blame] | 279 | return event_cache_name(cache_type, cache_op, cache_result); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 282 | case PERF_TYPE_SOFTWARE: |
Peter Zijlstra | f4dbfa8 | 2009-06-11 14:06:28 +0200 | [diff] [blame] | 283 | if (config < PERF_COUNT_SW_MAX) |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 284 | return sw_event_names[config]; |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 285 | return "unknown-software"; |
| 286 | |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 287 | case PERF_TYPE_TRACEPOINT: |
| 288 | return tracepoint_id_to_name(config); |
| 289 | |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 290 | default: |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | return "unknown"; |
| 295 | } |
| 296 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 297 | static int parse_aliases(const char **str, char *names[][MAX_ALIASES], int size) |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 298 | { |
| 299 | int i, j; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 300 | int n, longest = -1; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 301 | |
| 302 | for (i = 0; i < size; i++) { |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 303 | for (j = 0; j < MAX_ALIASES && names[i][j]; j++) { |
| 304 | n = strlen(names[i][j]); |
| 305 | if (n > longest && !strncasecmp(*str, names[i][j], n)) |
| 306 | longest = n; |
| 307 | } |
| 308 | if (longest > 0) { |
| 309 | *str += longest; |
| 310 | return i; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Ingo Molnar | 8953645 | 2009-06-06 21:04:17 +0200 | [diff] [blame] | 314 | return -1; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 315 | } |
| 316 | |
Jaswinder Singh Rajput | c0c22db | 2009-06-22 20:47:26 +0530 | [diff] [blame] | 317 | static int |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 318 | parse_generic_hw_event(const char **str, struct perf_counter_attr *attr) |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 319 | { |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 320 | const char *s = *str; |
| 321 | int cache_type = -1, cache_op = -1, cache_result = -1; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 322 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 323 | cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 324 | /* |
| 325 | * No fallback - if we cannot get a clear cache type |
| 326 | * then bail out: |
| 327 | */ |
| 328 | if (cache_type == -1) |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 329 | return 0; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 330 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 331 | while ((cache_op == -1 || cache_result == -1) && *s == '-') { |
| 332 | ++s; |
| 333 | |
| 334 | if (cache_op == -1) { |
| 335 | cache_op = parse_aliases(&s, hw_cache_op, |
| 336 | PERF_COUNT_HW_CACHE_OP_MAX); |
| 337 | if (cache_op >= 0) { |
| 338 | if (!is_cache_op_valid(cache_type, cache_op)) |
| 339 | return 0; |
| 340 | continue; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if (cache_result == -1) { |
| 345 | cache_result = parse_aliases(&s, hw_cache_result, |
| 346 | PERF_COUNT_HW_CACHE_RESULT_MAX); |
| 347 | if (cache_result >= 0) |
| 348 | continue; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Can't parse this as a cache op or result, so back up |
| 353 | * to the '-'. |
| 354 | */ |
| 355 | --s; |
| 356 | break; |
| 357 | } |
| 358 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 359 | /* |
| 360 | * Fall back to reads: |
| 361 | */ |
Ingo Molnar | 8953645 | 2009-06-06 21:04:17 +0200 | [diff] [blame] | 362 | if (cache_op == -1) |
| 363 | cache_op = PERF_COUNT_HW_CACHE_OP_READ; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 364 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 365 | /* |
| 366 | * Fall back to accesses: |
| 367 | */ |
| 368 | if (cache_result == -1) |
| 369 | cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; |
| 370 | |
| 371 | attr->config = cache_type | (cache_op << 8) | (cache_result << 16); |
| 372 | attr->type = PERF_TYPE_HW_CACHE; |
| 373 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 374 | *str = s; |
| 375 | return 1; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 378 | static int parse_tracepoint_event(const char **strp, |
| 379 | struct perf_counter_attr *attr) |
| 380 | { |
| 381 | const char *evt_name; |
| 382 | char sys_name[MAX_EVENT_LENGTH]; |
| 383 | char id_buf[4]; |
| 384 | int fd; |
| 385 | unsigned int sys_length, evt_length; |
| 386 | u64 id; |
| 387 | char evt_path[MAXPATHLEN]; |
| 388 | |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 389 | if (valid_debugfs_mount(debugfs_path)) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 390 | return 0; |
| 391 | |
| 392 | evt_name = strchr(*strp, ':'); |
| 393 | if (!evt_name) |
| 394 | return 0; |
| 395 | |
| 396 | sys_length = evt_name - *strp; |
| 397 | if (sys_length >= MAX_EVENT_LENGTH) |
| 398 | return 0; |
| 399 | |
| 400 | strncpy(sys_name, *strp, sys_length); |
| 401 | sys_name[sys_length] = '\0'; |
| 402 | evt_name = evt_name + 1; |
| 403 | evt_length = strlen(evt_name); |
| 404 | if (evt_length >= MAX_EVENT_LENGTH) |
| 405 | return 0; |
| 406 | |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 407 | snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path, |
| 408 | sys_name, evt_name); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 409 | fd = open(evt_path, O_RDONLY); |
| 410 | if (fd < 0) |
| 411 | return 0; |
| 412 | |
| 413 | if (read(fd, id_buf, sizeof(id_buf)) < 0) { |
| 414 | close(fd); |
| 415 | return 0; |
| 416 | } |
| 417 | close(fd); |
| 418 | id = atoll(id_buf); |
| 419 | attr->config = id; |
| 420 | attr->type = PERF_TYPE_TRACEPOINT; |
| 421 | *strp = evt_name + evt_length; |
| 422 | return 1; |
| 423 | } |
| 424 | |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 425 | static int check_events(const char *str, unsigned int i) |
| 426 | { |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 427 | int n; |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 428 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 429 | n = strlen(event_symbols[i].symbol); |
| 430 | if (!strncmp(str, event_symbols[i].symbol, n)) |
| 431 | return n; |
| 432 | |
| 433 | n = strlen(event_symbols[i].alias); |
| 434 | if (n) |
| 435 | if (!strncmp(str, event_symbols[i].alias, n)) |
| 436 | return n; |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | static int |
| 441 | parse_symbolic_event(const char **strp, struct perf_counter_attr *attr) |
| 442 | { |
| 443 | const char *str = *strp; |
| 444 | unsigned int i; |
| 445 | int n; |
| 446 | |
| 447 | for (i = 0; i < ARRAY_SIZE(event_symbols); i++) { |
| 448 | n = check_events(str, i); |
| 449 | if (n > 0) { |
| 450 | attr->type = event_symbols[i].type; |
| 451 | attr->config = event_symbols[i].config; |
| 452 | *strp = str + n; |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 453 | return 1; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static int parse_raw_event(const char **strp, struct perf_counter_attr *attr) |
| 460 | { |
| 461 | const char *str = *strp; |
| 462 | u64 config; |
| 463 | int n; |
| 464 | |
| 465 | if (*str != 'r') |
| 466 | return 0; |
| 467 | n = hex2u64(str + 1, &config); |
| 468 | if (n > 0) { |
| 469 | *strp = str + n + 1; |
| 470 | attr->type = PERF_TYPE_RAW; |
| 471 | attr->config = config; |
| 472 | return 1; |
| 473 | } |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | static int |
| 478 | parse_numeric_event(const char **strp, struct perf_counter_attr *attr) |
| 479 | { |
| 480 | const char *str = *strp; |
| 481 | char *endp; |
| 482 | unsigned long type; |
| 483 | u64 config; |
| 484 | |
| 485 | type = strtoul(str, &endp, 0); |
| 486 | if (endp > str && type < PERF_TYPE_MAX && *endp == ':') { |
| 487 | str = endp + 1; |
| 488 | config = strtoul(str, &endp, 0); |
| 489 | if (endp > str) { |
| 490 | attr->type = type; |
| 491 | attr->config = config; |
| 492 | *strp = endp; |
| 493 | return 1; |
| 494 | } |
| 495 | } |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static int |
| 500 | parse_event_modifier(const char **strp, struct perf_counter_attr *attr) |
| 501 | { |
| 502 | const char *str = *strp; |
| 503 | int eu = 1, ek = 1, eh = 1; |
| 504 | |
| 505 | if (*str++ != ':') |
| 506 | return 0; |
| 507 | while (*str) { |
| 508 | if (*str == 'u') |
| 509 | eu = 0; |
| 510 | else if (*str == 'k') |
| 511 | ek = 0; |
| 512 | else if (*str == 'h') |
| 513 | eh = 0; |
| 514 | else |
| 515 | break; |
| 516 | ++str; |
| 517 | } |
| 518 | if (str >= *strp + 2) { |
| 519 | *strp = str; |
| 520 | attr->exclude_user = eu; |
| 521 | attr->exclude_kernel = ek; |
| 522 | attr->exclude_hv = eh; |
| 523 | return 1; |
| 524 | } |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 525 | return 0; |
| 526 | } |
| 527 | |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 528 | /* |
| 529 | * Each event can have multiple symbolic names. |
| 530 | * Symbolic names are (almost) exactly matched. |
| 531 | */ |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 532 | static int parse_event_symbols(const char **str, struct perf_counter_attr *attr) |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 533 | { |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 534 | if (!(parse_tracepoint_event(str, attr) || |
| 535 | parse_raw_event(str, attr) || |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 536 | parse_numeric_event(str, attr) || |
| 537 | parse_symbolic_event(str, attr) || |
| 538 | parse_generic_hw_event(str, attr))) |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 539 | return 0; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 540 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 541 | parse_event_modifier(str, attr); |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 542 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 543 | return 1; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 546 | int parse_events(const struct option *opt __used, const char *str, int unset __used) |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 547 | { |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 548 | struct perf_counter_attr attr; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 549 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 550 | for (;;) { |
| 551 | if (nr_counters == MAX_COUNTERS) |
| 552 | return -1; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 553 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 554 | memset(&attr, 0, sizeof(attr)); |
| 555 | if (!parse_event_symbols(&str, &attr)) |
| 556 | return -1; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 557 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 558 | if (!(*str == 0 || *str == ',' || isspace(*str))) |
| 559 | return -1; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 560 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 561 | attrs[nr_counters] = attr; |
| 562 | nr_counters++; |
| 563 | |
| 564 | if (*str == 0) |
| 565 | break; |
| 566 | if (*str == ',') |
| 567 | ++str; |
| 568 | while (isspace(*str)) |
| 569 | ++str; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 575 | static const char * const event_type_descriptors[] = { |
| 576 | "", |
| 577 | "Hardware event", |
| 578 | "Software event", |
| 579 | "Tracepoint event", |
| 580 | "Hardware cache event", |
| 581 | }; |
| 582 | |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 583 | /* |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 584 | * Print the events from <debugfs_mount_point>/tracing/events |
| 585 | */ |
| 586 | |
| 587 | static void print_tracepoint_events(void) |
| 588 | { |
| 589 | DIR *sys_dir, *evt_dir; |
| 590 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; |
| 591 | struct stat st; |
| 592 | char evt_path[MAXPATHLEN]; |
| 593 | |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 594 | if (valid_debugfs_mount(debugfs_path)) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 595 | return; |
| 596 | |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 597 | sys_dir = opendir(debugfs_path); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 598 | if (!sys_dir) |
| 599 | goto cleanup; |
| 600 | |
| 601 | for_each_subsystem(sys_dir, sys_dirent, sys_next, evt_path, st) { |
| 602 | evt_dir = opendir(evt_path); |
| 603 | if (!evt_dir) |
| 604 | goto cleanup; |
| 605 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next, |
| 606 | evt_path, st) { |
| 607 | snprintf(evt_path, MAXPATHLEN, "%s:%s", |
| 608 | sys_dirent.d_name, evt_dirent.d_name); |
| 609 | fprintf(stderr, " %-40s [%s]\n", evt_path, |
| 610 | event_type_descriptors[PERF_TYPE_TRACEPOINT+1]); |
| 611 | } |
| 612 | closedir(evt_dir); |
| 613 | } |
| 614 | |
| 615 | cleanup: |
| 616 | closedir(sys_dir); |
| 617 | } |
| 618 | |
| 619 | /* |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 620 | * Print the help text for the event symbols: |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 621 | */ |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 622 | void print_events(void) |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 623 | { |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 624 | struct event_symbol *syms = event_symbols; |
Jaswinder Singh Rajput | 73c24cb | 2009-07-01 18:36:18 +0530 | [diff] [blame] | 625 | unsigned int i, type, op, prev_type = -1; |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 626 | char name[40]; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 627 | |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 628 | fprintf(stderr, "\n"); |
| 629 | fprintf(stderr, "List of pre-defined events (to be used in -e):\n"); |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 630 | |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 631 | for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) { |
| 632 | type = syms->type + 1; |
Roel Kluin | 23cdb5d | 2009-07-13 02:25:47 +0200 | [diff] [blame] | 633 | if (type >= ARRAY_SIZE(event_type_descriptors)) |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 634 | type = 0; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 635 | |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 636 | if (type != prev_type) |
| 637 | fprintf(stderr, "\n"); |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 638 | |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 639 | if (strlen(syms->alias)) |
| 640 | sprintf(name, "%s OR %s", syms->symbol, syms->alias); |
| 641 | else |
| 642 | strcpy(name, syms->symbol); |
| 643 | fprintf(stderr, " %-40s [%s]\n", name, |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 644 | event_type_descriptors[type]); |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 645 | |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 646 | prev_type = type; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 647 | } |
| 648 | |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 649 | fprintf(stderr, "\n"); |
Jaswinder Singh Rajput | 73c24cb | 2009-07-01 18:36:18 +0530 | [diff] [blame] | 650 | for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { |
| 651 | for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { |
| 652 | /* skip invalid cache type */ |
| 653 | if (!is_cache_op_valid(type, op)) |
| 654 | continue; |
| 655 | |
| 656 | for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { |
| 657 | fprintf(stderr, " %-40s [%s]\n", |
| 658 | event_cache_name(type, op, i), |
| 659 | event_type_descriptors[4]); |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | fprintf(stderr, "\n"); |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 665 | fprintf(stderr, " %-40s [raw hardware event descriptor]\n", |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 666 | "rNNN"); |
| 667 | fprintf(stderr, "\n"); |
| 668 | |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 669 | print_tracepoint_events(); |
| 670 | |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 671 | exit(129); |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 672 | } |