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