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