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