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