Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 1 | /* |
| 2 | * builtin-test.c |
| 3 | * |
| 4 | * Builtin regression testing command: ever growing number of sanity tests |
| 5 | */ |
| 6 | #include "builtin.h" |
| 7 | |
| 8 | #include "util/cache.h" |
| 9 | #include "util/debug.h" |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 10 | #include "util/debugfs.h" |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 11 | #include "util/evlist.h" |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 12 | #include "util/parse-options.h" |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 13 | #include "util/parse-events.h" |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 14 | #include "util/symbol.h" |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 15 | #include "util/thread_map.h" |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 16 | #include "util/pmu.h" |
Jiri Olsa | 13b6256 | 2011-07-14 11:25:33 +0200 | [diff] [blame] | 17 | #include "../../include/linux/hw_breakpoint.h" |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 18 | |
Peter Zijlstra | 08aa0d1 | 2011-11-21 14:42:47 +0100 | [diff] [blame] | 19 | #include <sys/mman.h> |
| 20 | |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 21 | static int vmlinux_matches_kallsyms_filter(struct map *map __used, struct symbol *sym) |
| 22 | { |
| 23 | bool *visited = symbol__priv(sym); |
| 24 | *visited = true; |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | static int test__vmlinux_matches_kallsyms(void) |
| 29 | { |
| 30 | int err = -1; |
| 31 | struct rb_node *nd; |
| 32 | struct symbol *sym; |
| 33 | struct map *kallsyms_map, *vmlinux_map; |
| 34 | struct machine kallsyms, vmlinux; |
| 35 | enum map_type type = MAP__FUNCTION; |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 36 | long page_size = sysconf(_SC_PAGE_SIZE); |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 37 | struct ref_reloc_sym ref_reloc_sym = { .name = "_stext", }; |
| 38 | |
| 39 | /* |
| 40 | * Step 1: |
| 41 | * |
| 42 | * Init the machines that will hold kernel, modules obtained from |
| 43 | * both vmlinux + .ko files and from /proc/kallsyms split by modules. |
| 44 | */ |
| 45 | machine__init(&kallsyms, "", HOST_KERNEL_ID); |
| 46 | machine__init(&vmlinux, "", HOST_KERNEL_ID); |
| 47 | |
| 48 | /* |
| 49 | * Step 2: |
| 50 | * |
| 51 | * Create the kernel maps for kallsyms and the DSO where we will then |
| 52 | * load /proc/kallsyms. Also create the modules maps from /proc/modules |
| 53 | * and find the .ko files that match them in /lib/modules/`uname -r`/. |
| 54 | */ |
| 55 | if (machine__create_kernel_maps(&kallsyms) < 0) { |
| 56 | pr_debug("machine__create_kernel_maps "); |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Step 3: |
| 62 | * |
| 63 | * Load and split /proc/kallsyms into multiple maps, one per module. |
| 64 | */ |
| 65 | if (machine__load_kallsyms(&kallsyms, "/proc/kallsyms", type, NULL) <= 0) { |
| 66 | pr_debug("dso__load_kallsyms "); |
| 67 | goto out; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Step 4: |
| 72 | * |
| 73 | * kallsyms will be internally on demand sorted by name so that we can |
| 74 | * find the reference relocation * symbol, i.e. the symbol we will use |
| 75 | * to see if the running kernel was relocated by checking if it has the |
| 76 | * same value in the vmlinux file we load. |
| 77 | */ |
| 78 | kallsyms_map = machine__kernel_map(&kallsyms, type); |
| 79 | |
| 80 | sym = map__find_symbol_by_name(kallsyms_map, ref_reloc_sym.name, NULL); |
| 81 | if (sym == NULL) { |
| 82 | pr_debug("dso__find_symbol_by_name "); |
| 83 | goto out; |
| 84 | } |
| 85 | |
| 86 | ref_reloc_sym.addr = sym->start; |
| 87 | |
| 88 | /* |
| 89 | * Step 5: |
| 90 | * |
| 91 | * Now repeat step 2, this time for the vmlinux file we'll auto-locate. |
| 92 | */ |
| 93 | if (machine__create_kernel_maps(&vmlinux) < 0) { |
| 94 | pr_debug("machine__create_kernel_maps "); |
| 95 | goto out; |
| 96 | } |
| 97 | |
| 98 | vmlinux_map = machine__kernel_map(&vmlinux, type); |
| 99 | map__kmap(vmlinux_map)->ref_reloc_sym = &ref_reloc_sym; |
| 100 | |
| 101 | /* |
| 102 | * Step 6: |
| 103 | * |
| 104 | * Locate a vmlinux file in the vmlinux path that has a buildid that |
| 105 | * matches the one of the running kernel. |
| 106 | * |
| 107 | * While doing that look if we find the ref reloc symbol, if we find it |
| 108 | * we'll have its ref_reloc_symbol.unrelocated_addr and then |
| 109 | * maps__reloc_vmlinux will notice and set proper ->[un]map_ip routines |
| 110 | * to fixup the symbols. |
| 111 | */ |
| 112 | if (machine__load_vmlinux_path(&vmlinux, type, |
| 113 | vmlinux_matches_kallsyms_filter) <= 0) { |
| 114 | pr_debug("machine__load_vmlinux_path "); |
| 115 | goto out; |
| 116 | } |
| 117 | |
| 118 | err = 0; |
| 119 | /* |
| 120 | * Step 7: |
| 121 | * |
| 122 | * Now look at the symbols in the vmlinux DSO and check if we find all of them |
| 123 | * in the kallsyms dso. For the ones that are in both, check its names and |
| 124 | * end addresses too. |
| 125 | */ |
| 126 | for (nd = rb_first(&vmlinux_map->dso->symbols[type]); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | d367875 | 2010-12-21 23:38:37 -0200 | [diff] [blame] | 127 | struct symbol *pair, *first_pair; |
| 128 | bool backwards = true; |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 129 | |
| 130 | sym = rb_entry(nd, struct symbol, rb_node); |
Arnaldo Carvalho de Melo | d367875 | 2010-12-21 23:38:37 -0200 | [diff] [blame] | 131 | |
| 132 | if (sym->start == sym->end) |
| 133 | continue; |
| 134 | |
| 135 | first_pair = machine__find_kernel_symbol(&kallsyms, type, sym->start, NULL, NULL); |
| 136 | pair = first_pair; |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 137 | |
| 138 | if (pair && pair->start == sym->start) { |
| 139 | next_pair: |
| 140 | if (strcmp(sym->name, pair->name) == 0) { |
| 141 | /* |
| 142 | * kallsyms don't have the symbol end, so we |
| 143 | * set that by using the next symbol start - 1, |
| 144 | * in some cases we get this up to a page |
| 145 | * wrong, trace_kmalloc when I was developing |
| 146 | * this code was one such example, 2106 bytes |
| 147 | * off the real size. More than that and we |
| 148 | * _really_ have a problem. |
| 149 | */ |
| 150 | s64 skew = sym->end - pair->end; |
| 151 | if (llabs(skew) < page_size) |
| 152 | continue; |
| 153 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 154 | pr_debug("%#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n", |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 155 | sym->start, sym->name, sym->end, pair->end); |
| 156 | } else { |
Arnaldo Carvalho de Melo | d367875 | 2010-12-21 23:38:37 -0200 | [diff] [blame] | 157 | struct rb_node *nnd; |
| 158 | detour: |
| 159 | nnd = backwards ? rb_prev(&pair->rb_node) : |
| 160 | rb_next(&pair->rb_node); |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 161 | if (nnd) { |
| 162 | struct symbol *next = rb_entry(nnd, struct symbol, rb_node); |
| 163 | |
| 164 | if (next->start == sym->start) { |
| 165 | pair = next; |
| 166 | goto next_pair; |
| 167 | } |
| 168 | } |
Arnaldo Carvalho de Melo | d367875 | 2010-12-21 23:38:37 -0200 | [diff] [blame] | 169 | |
| 170 | if (backwards) { |
| 171 | backwards = false; |
| 172 | pair = first_pair; |
| 173 | goto detour; |
| 174 | } |
| 175 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 176 | pr_debug("%#" PRIx64 ": diff name v: %s k: %s\n", |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 177 | sym->start, sym->name, pair->name); |
| 178 | } |
| 179 | } else |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 180 | pr_debug("%#" PRIx64 ": %s not on kallsyms\n", sym->start, sym->name); |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 181 | |
| 182 | err = -1; |
| 183 | } |
| 184 | |
| 185 | if (!verbose) |
| 186 | goto out; |
| 187 | |
| 188 | pr_info("Maps only in vmlinux:\n"); |
| 189 | |
| 190 | for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) { |
| 191 | struct map *pos = rb_entry(nd, struct map, rb_node), *pair; |
| 192 | /* |
| 193 | * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while |
| 194 | * the kernel will have the path for the vmlinux file being used, |
| 195 | * so use the short name, less descriptive but the same ("[kernel]" in |
| 196 | * both cases. |
| 197 | */ |
| 198 | pair = map_groups__find_by_name(&kallsyms.kmaps, type, |
| 199 | (pos->dso->kernel ? |
| 200 | pos->dso->short_name : |
| 201 | pos->dso->name)); |
| 202 | if (pair) |
| 203 | pair->priv = 1; |
| 204 | else |
| 205 | map__fprintf(pos, stderr); |
| 206 | } |
| 207 | |
| 208 | pr_info("Maps in vmlinux with a different name in kallsyms:\n"); |
| 209 | |
| 210 | for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) { |
| 211 | struct map *pos = rb_entry(nd, struct map, rb_node), *pair; |
| 212 | |
| 213 | pair = map_groups__find(&kallsyms.kmaps, type, pos->start); |
| 214 | if (pair == NULL || pair->priv) |
| 215 | continue; |
| 216 | |
| 217 | if (pair->start == pos->start) { |
| 218 | pair->priv = 1; |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 219 | pr_info(" %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as", |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 220 | pos->start, pos->end, pos->pgoff, pos->dso->name); |
| 221 | if (pos->pgoff != pair->pgoff || pos->end != pair->end) |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 222 | pr_info(": \n*%" PRIx64 "-%" PRIx64 " %" PRIx64 "", |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 223 | pair->start, pair->end, pair->pgoff); |
| 224 | pr_info(" %s\n", pair->dso->name); |
| 225 | pair->priv = 1; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | pr_info("Maps only in kallsyms:\n"); |
| 230 | |
| 231 | for (nd = rb_first(&kallsyms.kmaps.maps[type]); |
| 232 | nd; nd = rb_next(nd)) { |
| 233 | struct map *pos = rb_entry(nd, struct map, rb_node); |
| 234 | |
| 235 | if (!pos->priv) |
| 236 | map__fprintf(pos, stderr); |
| 237 | } |
| 238 | out: |
| 239 | return err; |
| 240 | } |
| 241 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 242 | #include "util/cpumap.h" |
Arnaldo Carvalho de Melo | d854861 | 2011-01-04 00:16:20 -0200 | [diff] [blame] | 243 | #include "util/evsel.h" |
| 244 | #include <sys/types.h> |
| 245 | |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 246 | static int trace_event__id(const char *evname) |
Arnaldo Carvalho de Melo | d854861 | 2011-01-04 00:16:20 -0200 | [diff] [blame] | 247 | { |
| 248 | char *filename; |
| 249 | int err = -1, fd; |
| 250 | |
| 251 | if (asprintf(&filename, |
Jiri Olsa | baf040a | 2011-07-14 11:25:34 +0200 | [diff] [blame] | 252 | "%s/syscalls/%s/id", |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 253 | tracing_events_path, evname) < 0) |
Arnaldo Carvalho de Melo | d854861 | 2011-01-04 00:16:20 -0200 | [diff] [blame] | 254 | return -1; |
| 255 | |
| 256 | fd = open(filename, O_RDONLY); |
| 257 | if (fd >= 0) { |
| 258 | char id[16]; |
| 259 | if (read(fd, id, sizeof(id)) > 0) |
| 260 | err = atoi(id); |
| 261 | close(fd); |
| 262 | } |
| 263 | |
| 264 | free(filename); |
| 265 | return err; |
| 266 | } |
| 267 | |
| 268 | static int test__open_syscall_event(void) |
| 269 | { |
| 270 | int err = -1, fd; |
| 271 | struct thread_map *threads; |
| 272 | struct perf_evsel *evsel; |
Lin Ming | 23a2f3a | 2011-01-07 11:11:09 +0800 | [diff] [blame] | 273 | struct perf_event_attr attr; |
Arnaldo Carvalho de Melo | d854861 | 2011-01-04 00:16:20 -0200 | [diff] [blame] | 274 | unsigned int nr_open_calls = 111, i; |
| 275 | int id = trace_event__id("sys_enter_open"); |
| 276 | |
| 277 | if (id < 0) { |
Arnaldo Carvalho de Melo | 454a3bb | 2011-01-04 10:40:08 -0200 | [diff] [blame] | 278 | pr_debug("is debugfs mounted on /sys/kernel/debug?\n"); |
Arnaldo Carvalho de Melo | d854861 | 2011-01-04 00:16:20 -0200 | [diff] [blame] | 279 | return -1; |
| 280 | } |
| 281 | |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 282 | threads = thread_map__new(-1, getpid(), UINT_MAX); |
Arnaldo Carvalho de Melo | d854861 | 2011-01-04 00:16:20 -0200 | [diff] [blame] | 283 | if (threads == NULL) { |
Arnaldo Carvalho de Melo | 454a3bb | 2011-01-04 10:40:08 -0200 | [diff] [blame] | 284 | pr_debug("thread_map__new\n"); |
Arnaldo Carvalho de Melo | d854861 | 2011-01-04 00:16:20 -0200 | [diff] [blame] | 285 | return -1; |
| 286 | } |
| 287 | |
Lin Ming | 23a2f3a | 2011-01-07 11:11:09 +0800 | [diff] [blame] | 288 | memset(&attr, 0, sizeof(attr)); |
| 289 | attr.type = PERF_TYPE_TRACEPOINT; |
| 290 | attr.config = id; |
| 291 | evsel = perf_evsel__new(&attr, 0); |
Arnaldo Carvalho de Melo | d854861 | 2011-01-04 00:16:20 -0200 | [diff] [blame] | 292 | if (evsel == NULL) { |
Arnaldo Carvalho de Melo | 454a3bb | 2011-01-04 10:40:08 -0200 | [diff] [blame] | 293 | pr_debug("perf_evsel__new\n"); |
Arnaldo Carvalho de Melo | d854861 | 2011-01-04 00:16:20 -0200 | [diff] [blame] | 294 | goto out_thread_map_delete; |
| 295 | } |
| 296 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 297 | if (perf_evsel__open_per_thread(evsel, threads) < 0) { |
Arnaldo Carvalho de Melo | 454a3bb | 2011-01-04 10:40:08 -0200 | [diff] [blame] | 298 | pr_debug("failed to open counter: %s, " |
| 299 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", |
| 300 | strerror(errno)); |
Arnaldo Carvalho de Melo | d854861 | 2011-01-04 00:16:20 -0200 | [diff] [blame] | 301 | goto out_evsel_delete; |
| 302 | } |
| 303 | |
| 304 | for (i = 0; i < nr_open_calls; ++i) { |
| 305 | fd = open("/etc/passwd", O_RDONLY); |
| 306 | close(fd); |
| 307 | } |
| 308 | |
| 309 | if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) { |
Arnaldo Carvalho de Melo | 5d2cd90 | 2011-04-14 11:20:14 -0300 | [diff] [blame] | 310 | pr_debug("perf_evsel__read_on_cpu\n"); |
Arnaldo Carvalho de Melo | d854861 | 2011-01-04 00:16:20 -0200 | [diff] [blame] | 311 | goto out_close_fd; |
| 312 | } |
| 313 | |
Arnaldo Carvalho de Melo | 454a3bb | 2011-01-04 10:40:08 -0200 | [diff] [blame] | 314 | if (evsel->counts->cpu[0].val != nr_open_calls) { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 315 | pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n", |
Arnaldo Carvalho de Melo | d854861 | 2011-01-04 00:16:20 -0200 | [diff] [blame] | 316 | nr_open_calls, evsel->counts->cpu[0].val); |
Arnaldo Carvalho de Melo | 454a3bb | 2011-01-04 10:40:08 -0200 | [diff] [blame] | 317 | goto out_close_fd; |
| 318 | } |
Arnaldo Carvalho de Melo | d854861 | 2011-01-04 00:16:20 -0200 | [diff] [blame] | 319 | |
| 320 | err = 0; |
| 321 | out_close_fd: |
| 322 | perf_evsel__close_fd(evsel, 1, threads->nr); |
| 323 | out_evsel_delete: |
| 324 | perf_evsel__delete(evsel); |
| 325 | out_thread_map_delete: |
| 326 | thread_map__delete(threads); |
| 327 | return err; |
| 328 | } |
| 329 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 330 | #include <sched.h> |
| 331 | |
| 332 | static int test__open_syscall_event_on_all_cpus(void) |
| 333 | { |
| 334 | int err = -1, fd, cpu; |
| 335 | struct thread_map *threads; |
| 336 | struct cpu_map *cpus; |
| 337 | struct perf_evsel *evsel; |
| 338 | struct perf_event_attr attr; |
| 339 | unsigned int nr_open_calls = 111, i; |
Arnaldo Carvalho de Melo | 57b84e5 | 2011-01-22 23:14:20 -0200 | [diff] [blame] | 340 | cpu_set_t cpu_set; |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 341 | int id = trace_event__id("sys_enter_open"); |
| 342 | |
| 343 | if (id < 0) { |
| 344 | pr_debug("is debugfs mounted on /sys/kernel/debug?\n"); |
| 345 | return -1; |
| 346 | } |
| 347 | |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 348 | threads = thread_map__new(-1, getpid(), UINT_MAX); |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 349 | if (threads == NULL) { |
| 350 | pr_debug("thread_map__new\n"); |
| 351 | return -1; |
| 352 | } |
| 353 | |
| 354 | cpus = cpu_map__new(NULL); |
Han Pingtian | 98d77b7 | 2011-01-15 07:00:50 +0800 | [diff] [blame] | 355 | if (cpus == NULL) { |
| 356 | pr_debug("cpu_map__new\n"); |
| 357 | goto out_thread_map_delete; |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 358 | } |
| 359 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 360 | |
Arnaldo Carvalho de Melo | 57b84e5 | 2011-01-22 23:14:20 -0200 | [diff] [blame] | 361 | CPU_ZERO(&cpu_set); |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 362 | |
| 363 | memset(&attr, 0, sizeof(attr)); |
| 364 | attr.type = PERF_TYPE_TRACEPOINT; |
| 365 | attr.config = id; |
| 366 | evsel = perf_evsel__new(&attr, 0); |
| 367 | if (evsel == NULL) { |
| 368 | pr_debug("perf_evsel__new\n"); |
Arnaldo Carvalho de Melo | 57b84e5 | 2011-01-22 23:14:20 -0200 | [diff] [blame] | 369 | goto out_thread_map_delete; |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 370 | } |
| 371 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 372 | if (perf_evsel__open(evsel, cpus, threads) < 0) { |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 373 | pr_debug("failed to open counter: %s, " |
| 374 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", |
| 375 | strerror(errno)); |
| 376 | goto out_evsel_delete; |
| 377 | } |
| 378 | |
| 379 | for (cpu = 0; cpu < cpus->nr; ++cpu) { |
| 380 | unsigned int ncalls = nr_open_calls + cpu; |
Arnaldo Carvalho de Melo | 57b84e5 | 2011-01-22 23:14:20 -0200 | [diff] [blame] | 381 | /* |
| 382 | * XXX eventually lift this restriction in a way that |
| 383 | * keeps perf building on older glibc installations |
| 384 | * without CPU_ALLOC. 1024 cpus in 2010 still seems |
| 385 | * a reasonable upper limit tho :-) |
| 386 | */ |
| 387 | if (cpus->map[cpu] >= CPU_SETSIZE) { |
| 388 | pr_debug("Ignoring CPU %d\n", cpus->map[cpu]); |
| 389 | continue; |
| 390 | } |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 391 | |
Arnaldo Carvalho de Melo | 57b84e5 | 2011-01-22 23:14:20 -0200 | [diff] [blame] | 392 | CPU_SET(cpus->map[cpu], &cpu_set); |
| 393 | if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) { |
Han Pingtian | ffb5e0f | 2011-01-20 19:47:07 +0800 | [diff] [blame] | 394 | pr_debug("sched_setaffinity() failed on CPU %d: %s ", |
| 395 | cpus->map[cpu], |
| 396 | strerror(errno)); |
| 397 | goto out_close_fd; |
| 398 | } |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 399 | for (i = 0; i < ncalls; ++i) { |
| 400 | fd = open("/etc/passwd", O_RDONLY); |
| 401 | close(fd); |
| 402 | } |
Arnaldo Carvalho de Melo | 57b84e5 | 2011-01-22 23:14:20 -0200 | [diff] [blame] | 403 | CPU_CLR(cpus->map[cpu], &cpu_set); |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Here we need to explicitely preallocate the counts, as if |
| 408 | * we use the auto allocation it will allocate just for 1 cpu, |
| 409 | * as we start by cpu 0. |
| 410 | */ |
| 411 | if (perf_evsel__alloc_counts(evsel, cpus->nr) < 0) { |
| 412 | pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr); |
| 413 | goto out_close_fd; |
| 414 | } |
| 415 | |
Arnaldo Carvalho de Melo | d2af968 | 2011-01-14 16:24:49 -0200 | [diff] [blame] | 416 | err = 0; |
| 417 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 418 | for (cpu = 0; cpu < cpus->nr; ++cpu) { |
| 419 | unsigned int expected; |
| 420 | |
Arnaldo Carvalho de Melo | 57b84e5 | 2011-01-22 23:14:20 -0200 | [diff] [blame] | 421 | if (cpus->map[cpu] >= CPU_SETSIZE) |
| 422 | continue; |
| 423 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 424 | if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) { |
Arnaldo Carvalho de Melo | 5d2cd90 | 2011-04-14 11:20:14 -0300 | [diff] [blame] | 425 | pr_debug("perf_evsel__read_on_cpu\n"); |
Arnaldo Carvalho de Melo | d2af968 | 2011-01-14 16:24:49 -0200 | [diff] [blame] | 426 | err = -1; |
| 427 | break; |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | expected = nr_open_calls + cpu; |
| 431 | if (evsel->counts->cpu[cpu].val != expected) { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 432 | pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n", |
Han Pingtian | ffb5e0f | 2011-01-20 19:47:07 +0800 | [diff] [blame] | 433 | expected, cpus->map[cpu], evsel->counts->cpu[cpu].val); |
Arnaldo Carvalho de Melo | d2af968 | 2011-01-14 16:24:49 -0200 | [diff] [blame] | 434 | err = -1; |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 438 | out_close_fd: |
| 439 | perf_evsel__close_fd(evsel, 1, threads->nr); |
| 440 | out_evsel_delete: |
| 441 | perf_evsel__delete(evsel); |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 442 | out_thread_map_delete: |
| 443 | thread_map__delete(threads); |
| 444 | return err; |
| 445 | } |
| 446 | |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 447 | /* |
| 448 | * This test will generate random numbers of calls to some getpid syscalls, |
| 449 | * then establish an mmap for a group of events that are created to monitor |
| 450 | * the syscalls. |
| 451 | * |
| 452 | * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated |
| 453 | * sample.id field to map back to its respective perf_evsel instance. |
| 454 | * |
| 455 | * Then it checks if the number of syscalls reported as perf events by |
| 456 | * the kernel corresponds to the number of syscalls made. |
| 457 | */ |
| 458 | static int test__basic_mmap(void) |
| 459 | { |
| 460 | int err = -1; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 461 | union perf_event *event; |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 462 | struct thread_map *threads; |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 463 | struct cpu_map *cpus; |
| 464 | struct perf_evlist *evlist; |
| 465 | struct perf_event_attr attr = { |
| 466 | .type = PERF_TYPE_TRACEPOINT, |
| 467 | .read_format = PERF_FORMAT_ID, |
| 468 | .sample_type = PERF_SAMPLE_ID, |
| 469 | .watermark = 0, |
| 470 | }; |
| 471 | cpu_set_t cpu_set; |
| 472 | const char *syscall_names[] = { "getsid", "getppid", "getpgrp", |
| 473 | "getpgid", }; |
| 474 | pid_t (*syscalls[])(void) = { (void *)getsid, getppid, getpgrp, |
| 475 | (void*)getpgid }; |
| 476 | #define nsyscalls ARRAY_SIZE(syscall_names) |
| 477 | int ids[nsyscalls]; |
| 478 | unsigned int nr_events[nsyscalls], |
| 479 | expected_nr_events[nsyscalls], i, j; |
| 480 | struct perf_evsel *evsels[nsyscalls], *evsel; |
| 481 | |
| 482 | for (i = 0; i < nsyscalls; ++i) { |
| 483 | char name[64]; |
| 484 | |
| 485 | snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]); |
| 486 | ids[i] = trace_event__id(name); |
| 487 | if (ids[i] < 0) { |
| 488 | pr_debug("Is debugfs mounted on /sys/kernel/debug?\n"); |
| 489 | return -1; |
| 490 | } |
| 491 | nr_events[i] = 0; |
| 492 | expected_nr_events[i] = random() % 257; |
| 493 | } |
| 494 | |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 495 | threads = thread_map__new(-1, getpid(), UINT_MAX); |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 496 | if (threads == NULL) { |
| 497 | pr_debug("thread_map__new\n"); |
| 498 | return -1; |
| 499 | } |
| 500 | |
| 501 | cpus = cpu_map__new(NULL); |
Han Pingtian | 54489c18 | 2011-01-25 07:39:00 +0800 | [diff] [blame] | 502 | if (cpus == NULL) { |
| 503 | pr_debug("cpu_map__new\n"); |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 504 | goto out_free_threads; |
| 505 | } |
| 506 | |
| 507 | CPU_ZERO(&cpu_set); |
| 508 | CPU_SET(cpus->map[0], &cpu_set); |
| 509 | sched_setaffinity(0, sizeof(cpu_set), &cpu_set); |
| 510 | if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) { |
| 511 | pr_debug("sched_setaffinity() failed on CPU %d: %s ", |
| 512 | cpus->map[0], strerror(errno)); |
| 513 | goto out_free_cpus; |
| 514 | } |
| 515 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 516 | evlist = perf_evlist__new(cpus, threads); |
Han Pingtian | 54489c18 | 2011-01-25 07:39:00 +0800 | [diff] [blame] | 517 | if (evlist == NULL) { |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 518 | pr_debug("perf_evlist__new\n"); |
| 519 | goto out_free_cpus; |
| 520 | } |
| 521 | |
| 522 | /* anonymous union fields, can't be initialized above */ |
| 523 | attr.wakeup_events = 1; |
| 524 | attr.sample_period = 1; |
| 525 | |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 526 | for (i = 0; i < nsyscalls; ++i) { |
| 527 | attr.config = ids[i]; |
| 528 | evsels[i] = perf_evsel__new(&attr, i); |
| 529 | if (evsels[i] == NULL) { |
| 530 | pr_debug("perf_evsel__new\n"); |
| 531 | goto out_free_evlist; |
| 532 | } |
| 533 | |
| 534 | perf_evlist__add(evlist, evsels[i]); |
| 535 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 536 | if (perf_evsel__open(evsels[i], cpus, threads) < 0) { |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 537 | pr_debug("failed to open counter: %s, " |
| 538 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", |
| 539 | strerror(errno)); |
| 540 | goto out_close_fd; |
| 541 | } |
| 542 | } |
| 543 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 544 | if (perf_evlist__mmap(evlist, 128, true) < 0) { |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 545 | pr_debug("failed to mmap events: %d (%s)\n", errno, |
| 546 | strerror(errno)); |
| 547 | goto out_close_fd; |
| 548 | } |
| 549 | |
| 550 | for (i = 0; i < nsyscalls; ++i) |
| 551 | for (j = 0; j < expected_nr_events[i]; ++j) { |
| 552 | int foo = syscalls[i](); |
| 553 | ++foo; |
| 554 | } |
| 555 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 556 | while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) { |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 557 | struct perf_sample sample; |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 558 | |
| 559 | if (event->header.type != PERF_RECORD_SAMPLE) { |
| 560 | pr_debug("unexpected %s event\n", |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 561 | perf_event__name(event->header.type)); |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 562 | goto out_munmap; |
| 563 | } |
| 564 | |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 565 | err = perf_evlist__parse_sample(evlist, event, &sample, false); |
Frederic Weisbecker | 5538bec | 2011-05-22 02:17:22 +0200 | [diff] [blame] | 566 | if (err) { |
| 567 | pr_err("Can't parse sample, err = %d\n", err); |
| 568 | goto out_munmap; |
| 569 | } |
| 570 | |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 571 | evsel = perf_evlist__id2evsel(evlist, sample.id); |
| 572 | if (evsel == NULL) { |
| 573 | pr_debug("event with id %" PRIu64 |
| 574 | " doesn't map to an evsel\n", sample.id); |
| 575 | goto out_munmap; |
| 576 | } |
| 577 | nr_events[evsel->idx]++; |
| 578 | } |
| 579 | |
| 580 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 581 | if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) { |
| 582 | pr_debug("expected %d %s events, got %d\n", |
| 583 | expected_nr_events[evsel->idx], |
Arnaldo Carvalho de Melo | 7289f83 | 2012-06-12 12:34:58 -0300 | [diff] [blame] | 584 | perf_evsel__name(evsel), nr_events[evsel->idx]); |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 585 | goto out_munmap; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | err = 0; |
| 590 | out_munmap: |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 591 | perf_evlist__munmap(evlist); |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 592 | out_close_fd: |
| 593 | for (i = 0; i < nsyscalls; ++i) |
| 594 | perf_evsel__close_fd(evsels[i], 1, threads->nr); |
| 595 | out_free_evlist: |
| 596 | perf_evlist__delete(evlist); |
| 597 | out_free_cpus: |
| 598 | cpu_map__delete(cpus); |
| 599 | out_free_threads: |
| 600 | thread_map__delete(threads); |
| 601 | return err; |
| 602 | #undef nsyscalls |
| 603 | } |
| 604 | |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 605 | static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t **maskp, |
| 606 | size_t *sizep) |
| 607 | { |
| 608 | cpu_set_t *mask; |
| 609 | size_t size; |
| 610 | int i, cpu = -1, nrcpus = 1024; |
| 611 | realloc: |
| 612 | mask = CPU_ALLOC(nrcpus); |
| 613 | size = CPU_ALLOC_SIZE(nrcpus); |
| 614 | CPU_ZERO_S(size, mask); |
| 615 | |
| 616 | if (sched_getaffinity(pid, size, mask) == -1) { |
| 617 | CPU_FREE(mask); |
| 618 | if (errno == EINVAL && nrcpus < (1024 << 8)) { |
| 619 | nrcpus = nrcpus << 2; |
| 620 | goto realloc; |
| 621 | } |
| 622 | perror("sched_getaffinity"); |
| 623 | return -1; |
| 624 | } |
| 625 | |
| 626 | for (i = 0; i < nrcpus; i++) { |
| 627 | if (CPU_ISSET_S(i, size, mask)) { |
| 628 | if (cpu == -1) { |
| 629 | cpu = i; |
| 630 | *maskp = mask; |
| 631 | *sizep = size; |
| 632 | } else |
| 633 | CPU_CLR_S(i, size, mask); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | if (cpu == -1) |
| 638 | CPU_FREE(mask); |
| 639 | |
| 640 | return cpu; |
| 641 | } |
| 642 | |
| 643 | static int test__PERF_RECORD(void) |
| 644 | { |
| 645 | struct perf_record_opts opts = { |
Namhyung Kim | b809ac1 | 2012-04-26 14:15:19 +0900 | [diff] [blame] | 646 | .target = { |
| 647 | .uid = UINT_MAX, |
Namhyung Kim | d1cb9fc | 2012-05-16 18:45:49 +0900 | [diff] [blame] | 648 | .uses_mmap = true, |
Namhyung Kim | b809ac1 | 2012-04-26 14:15:19 +0900 | [diff] [blame] | 649 | }, |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 650 | .no_delay = true, |
| 651 | .freq = 10, |
| 652 | .mmap_pages = 256, |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 653 | }; |
| 654 | cpu_set_t *cpu_mask = NULL; |
| 655 | size_t cpu_mask_size = 0; |
| 656 | struct perf_evlist *evlist = perf_evlist__new(NULL, NULL); |
| 657 | struct perf_evsel *evsel; |
| 658 | struct perf_sample sample; |
| 659 | const char *cmd = "sleep"; |
| 660 | const char *argv[] = { cmd, "1", NULL, }; |
| 661 | char *bname; |
Arnaldo Carvalho de Melo | 7f3be65 | 2012-08-01 19:15:52 -0300 | [diff] [blame] | 662 | u64 prev_time = 0; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 663 | bool found_cmd_mmap = false, |
| 664 | found_libc_mmap = false, |
| 665 | found_vdso_mmap = false, |
| 666 | found_ld_mmap = false; |
Arnaldo Carvalho de Melo | bde0946 | 2012-08-01 18:53:11 -0300 | [diff] [blame] | 667 | int err = -1, errs = 0, i, wakeups = 0; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 668 | u32 cpu; |
| 669 | int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, }; |
| 670 | |
| 671 | if (evlist == NULL || argv == NULL) { |
| 672 | pr_debug("Not enough memory to create evlist\n"); |
| 673 | goto out; |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * We need at least one evsel in the evlist, use the default |
| 678 | * one: "cycles". |
| 679 | */ |
| 680 | err = perf_evlist__add_default(evlist); |
| 681 | if (err < 0) { |
| 682 | pr_debug("Not enough memory to create evsel\n"); |
| 683 | goto out_delete_evlist; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Create maps of threads and cpus to monitor. In this case |
| 688 | * we start with all threads and cpus (-1, -1) but then in |
| 689 | * perf_evlist__prepare_workload we'll fill in the only thread |
| 690 | * we're monitoring, the one forked there. |
| 691 | */ |
Namhyung Kim | b809ac1 | 2012-04-26 14:15:19 +0900 | [diff] [blame] | 692 | err = perf_evlist__create_maps(evlist, &opts.target); |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 693 | if (err < 0) { |
| 694 | pr_debug("Not enough memory to create thread/cpu maps\n"); |
| 695 | goto out_delete_evlist; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Prepare the workload in argv[] to run, it'll fork it, and then wait |
| 700 | * for perf_evlist__start_workload() to exec it. This is done this way |
| 701 | * so that we have time to open the evlist (calling sys_perf_event_open |
| 702 | * on all the fds) and then mmap them. |
| 703 | */ |
| 704 | err = perf_evlist__prepare_workload(evlist, &opts, argv); |
| 705 | if (err < 0) { |
| 706 | pr_debug("Couldn't run the workload!\n"); |
| 707 | goto out_delete_evlist; |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * Config the evsels, setting attr->comm on the first one, etc. |
| 712 | */ |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 713 | evsel = perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 714 | evsel->attr.sample_type |= PERF_SAMPLE_CPU; |
| 715 | evsel->attr.sample_type |= PERF_SAMPLE_TID; |
| 716 | evsel->attr.sample_type |= PERF_SAMPLE_TIME; |
| 717 | perf_evlist__config_attrs(evlist, &opts); |
| 718 | |
| 719 | err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask, |
| 720 | &cpu_mask_size); |
| 721 | if (err < 0) { |
| 722 | pr_debug("sched__get_first_possible_cpu: %s\n", strerror(errno)); |
| 723 | goto out_delete_evlist; |
| 724 | } |
| 725 | |
| 726 | cpu = err; |
| 727 | |
| 728 | /* |
| 729 | * So that we can check perf_sample.cpu on all the samples. |
| 730 | */ |
| 731 | if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) { |
| 732 | pr_debug("sched_setaffinity: %s\n", strerror(errno)); |
| 733 | goto out_free_cpu_mask; |
| 734 | } |
| 735 | |
| 736 | /* |
| 737 | * Call sys_perf_event_open on all the fds on all the evsels, |
| 738 | * grouping them if asked to. |
| 739 | */ |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 740 | err = perf_evlist__open(evlist); |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 741 | if (err < 0) { |
| 742 | pr_debug("perf_evlist__open: %s\n", strerror(errno)); |
| 743 | goto out_delete_evlist; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * mmap the first fd on a given CPU and ask for events for the other |
| 748 | * fds in the same CPU to be injected in the same mmap ring buffer |
| 749 | * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)). |
| 750 | */ |
| 751 | err = perf_evlist__mmap(evlist, opts.mmap_pages, false); |
| 752 | if (err < 0) { |
| 753 | pr_debug("perf_evlist__mmap: %s\n", strerror(errno)); |
| 754 | goto out_delete_evlist; |
| 755 | } |
| 756 | |
| 757 | /* |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 758 | * Now that all is properly set up, enable the events, they will |
| 759 | * count just on workload.pid, which will start... |
| 760 | */ |
| 761 | perf_evlist__enable(evlist); |
| 762 | |
| 763 | /* |
| 764 | * Now! |
| 765 | */ |
| 766 | perf_evlist__start_workload(evlist); |
| 767 | |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 768 | while (1) { |
| 769 | int before = total_events; |
| 770 | |
| 771 | for (i = 0; i < evlist->nr_mmaps; i++) { |
| 772 | union perf_event *event; |
| 773 | |
| 774 | while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { |
| 775 | const u32 type = event->header.type; |
| 776 | const char *name = perf_event__name(type); |
| 777 | |
| 778 | ++total_events; |
| 779 | if (type < PERF_RECORD_MAX) |
| 780 | nr_events[type]++; |
| 781 | |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 782 | err = perf_evlist__parse_sample(evlist, event, &sample, false); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 783 | if (err < 0) { |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 784 | if (verbose) |
| 785 | perf_event__fprintf(event, stderr); |
| 786 | pr_debug("Couldn't parse sample\n"); |
| 787 | goto out_err; |
| 788 | } |
| 789 | |
| 790 | if (verbose) { |
| 791 | pr_info("%" PRIu64" %d ", sample.time, sample.cpu); |
| 792 | perf_event__fprintf(event, stderr); |
| 793 | } |
| 794 | |
| 795 | if (prev_time > sample.time) { |
| 796 | pr_debug("%s going backwards in time, prev=%" PRIu64 ", curr=%" PRIu64 "\n", |
| 797 | name, prev_time, sample.time); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 798 | ++errs; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | prev_time = sample.time; |
| 802 | |
| 803 | if (sample.cpu != cpu) { |
| 804 | pr_debug("%s with unexpected cpu, expected %d, got %d\n", |
| 805 | name, cpu, sample.cpu); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 806 | ++errs; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | if ((pid_t)sample.pid != evlist->workload.pid) { |
| 810 | pr_debug("%s with unexpected pid, expected %d, got %d\n", |
| 811 | name, evlist->workload.pid, sample.pid); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 812 | ++errs; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | if ((pid_t)sample.tid != evlist->workload.pid) { |
| 816 | pr_debug("%s with unexpected tid, expected %d, got %d\n", |
| 817 | name, evlist->workload.pid, sample.tid); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 818 | ++errs; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | if ((type == PERF_RECORD_COMM || |
| 822 | type == PERF_RECORD_MMAP || |
| 823 | type == PERF_RECORD_FORK || |
| 824 | type == PERF_RECORD_EXIT) && |
| 825 | (pid_t)event->comm.pid != evlist->workload.pid) { |
| 826 | pr_debug("%s with unexpected pid/tid\n", name); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 827 | ++errs; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | if ((type == PERF_RECORD_COMM || |
| 831 | type == PERF_RECORD_MMAP) && |
| 832 | event->comm.pid != event->comm.tid) { |
| 833 | pr_debug("%s with different pid/tid!\n", name); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 834 | ++errs; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | switch (type) { |
| 838 | case PERF_RECORD_COMM: |
| 839 | if (strcmp(event->comm.comm, cmd)) { |
| 840 | pr_debug("%s with unexpected comm!\n", name); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 841 | ++errs; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 842 | } |
| 843 | break; |
| 844 | case PERF_RECORD_EXIT: |
| 845 | goto found_exit; |
| 846 | case PERF_RECORD_MMAP: |
| 847 | bname = strrchr(event->mmap.filename, '/'); |
| 848 | if (bname != NULL) { |
| 849 | if (!found_cmd_mmap) |
| 850 | found_cmd_mmap = !strcmp(bname + 1, cmd); |
| 851 | if (!found_libc_mmap) |
| 852 | found_libc_mmap = !strncmp(bname + 1, "libc", 4); |
| 853 | if (!found_ld_mmap) |
| 854 | found_ld_mmap = !strncmp(bname + 1, "ld", 2); |
| 855 | } else if (!found_vdso_mmap) |
| 856 | found_vdso_mmap = !strcmp(event->mmap.filename, "[vdso]"); |
| 857 | break; |
| 858 | |
| 859 | case PERF_RECORD_SAMPLE: |
| 860 | /* Just ignore samples for now */ |
| 861 | break; |
| 862 | default: |
| 863 | pr_debug("Unexpected perf_event->header.type %d!\n", |
| 864 | type); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 865 | ++errs; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | /* |
| 871 | * We don't use poll here because at least at 3.1 times the |
| 872 | * PERF_RECORD_{!SAMPLE} events don't honour |
| 873 | * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does. |
| 874 | */ |
| 875 | if (total_events == before && false) |
| 876 | poll(evlist->pollfd, evlist->nr_fds, -1); |
| 877 | |
| 878 | sleep(1); |
| 879 | if (++wakeups > 5) { |
| 880 | pr_debug("No PERF_RECORD_EXIT event!\n"); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 881 | break; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | |
| 885 | found_exit: |
| 886 | if (nr_events[PERF_RECORD_COMM] > 1) { |
| 887 | pr_debug("Excessive number of PERF_RECORD_COMM events!\n"); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 888 | ++errs; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | if (nr_events[PERF_RECORD_COMM] == 0) { |
| 892 | pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 893 | ++errs; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | if (!found_cmd_mmap) { |
| 897 | pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 898 | ++errs; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | if (!found_libc_mmap) { |
| 902 | pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc"); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 903 | ++errs; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | if (!found_ld_mmap) { |
| 907 | pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld"); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 908 | ++errs; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | if (!found_vdso_mmap) { |
| 912 | pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]"); |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 913 | ++errs; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 914 | } |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 915 | out_err: |
| 916 | perf_evlist__munmap(evlist); |
| 917 | out_free_cpu_mask: |
| 918 | CPU_FREE(cpu_mask); |
| 919 | out_delete_evlist: |
| 920 | perf_evlist__delete(evlist); |
| 921 | out: |
Arnaldo Carvalho de Melo | f71c49e | 2011-12-02 13:53:04 -0200 | [diff] [blame] | 922 | return (err < 0 || errs > 0) ? -1 : 0; |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 923 | } |
| 924 | |
Peter Zijlstra | 08aa0d1 | 2011-11-21 14:42:47 +0100 | [diff] [blame] | 925 | |
| 926 | #if defined(__x86_64__) || defined(__i386__) |
| 927 | |
| 928 | #define barrier() asm volatile("" ::: "memory") |
| 929 | |
| 930 | static u64 rdpmc(unsigned int counter) |
| 931 | { |
| 932 | unsigned int low, high; |
| 933 | |
| 934 | asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter)); |
| 935 | |
| 936 | return low | ((u64)high) << 32; |
| 937 | } |
| 938 | |
| 939 | static u64 rdtsc(void) |
| 940 | { |
| 941 | unsigned int low, high; |
| 942 | |
| 943 | asm volatile("rdtsc" : "=a" (low), "=d" (high)); |
| 944 | |
| 945 | return low | ((u64)high) << 32; |
| 946 | } |
| 947 | |
| 948 | static u64 mmap_read_self(void *addr) |
| 949 | { |
| 950 | struct perf_event_mmap_page *pc = addr; |
| 951 | u32 seq, idx, time_mult = 0, time_shift = 0; |
| 952 | u64 count, cyc = 0, time_offset = 0, enabled, running, delta; |
| 953 | |
| 954 | do { |
| 955 | seq = pc->lock; |
| 956 | barrier(); |
| 957 | |
| 958 | enabled = pc->time_enabled; |
| 959 | running = pc->time_running; |
| 960 | |
| 961 | if (enabled != running) { |
| 962 | cyc = rdtsc(); |
| 963 | time_mult = pc->time_mult; |
| 964 | time_shift = pc->time_shift; |
| 965 | time_offset = pc->time_offset; |
| 966 | } |
| 967 | |
| 968 | idx = pc->index; |
| 969 | count = pc->offset; |
| 970 | if (idx) |
| 971 | count += rdpmc(idx - 1); |
| 972 | |
| 973 | barrier(); |
| 974 | } while (pc->lock != seq); |
| 975 | |
| 976 | if (enabled != running) { |
| 977 | u64 quot, rem; |
| 978 | |
| 979 | quot = (cyc >> time_shift); |
| 980 | rem = cyc & ((1 << time_shift) - 1); |
| 981 | delta = time_offset + quot * time_mult + |
| 982 | ((rem * time_mult) >> time_shift); |
| 983 | |
| 984 | enabled += delta; |
| 985 | if (idx) |
| 986 | running += delta; |
| 987 | |
| 988 | quot = count / running; |
| 989 | rem = count % running; |
| 990 | count = quot * enabled + (rem * enabled) / running; |
| 991 | } |
| 992 | |
| 993 | return count; |
| 994 | } |
| 995 | |
| 996 | /* |
| 997 | * If the RDPMC instruction faults then signal this back to the test parent task: |
| 998 | */ |
| 999 | static void segfault_handler(int sig __used, siginfo_t *info __used, void *uc __used) |
| 1000 | { |
| 1001 | exit(-1); |
| 1002 | } |
| 1003 | |
| 1004 | static int __test__rdpmc(void) |
| 1005 | { |
| 1006 | long page_size = sysconf(_SC_PAGE_SIZE); |
| 1007 | volatile int tmp = 0; |
| 1008 | u64 i, loops = 1000; |
| 1009 | int n; |
| 1010 | int fd; |
| 1011 | void *addr; |
| 1012 | struct perf_event_attr attr = { |
| 1013 | .type = PERF_TYPE_HARDWARE, |
| 1014 | .config = PERF_COUNT_HW_INSTRUCTIONS, |
| 1015 | .exclude_kernel = 1, |
| 1016 | }; |
| 1017 | u64 delta_sum = 0; |
| 1018 | struct sigaction sa; |
| 1019 | |
| 1020 | sigfillset(&sa.sa_mask); |
| 1021 | sa.sa_sigaction = segfault_handler; |
| 1022 | sigaction(SIGSEGV, &sa, NULL); |
| 1023 | |
Peter Zijlstra | 08aa0d1 | 2011-11-21 14:42:47 +0100 | [diff] [blame] | 1024 | fd = sys_perf_event_open(&attr, 0, -1, -1, 0); |
| 1025 | if (fd < 0) { |
Arnaldo Carvalho de Melo | 32c7f73 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1026 | pr_debug("Error: sys_perf_event_open() syscall returned " |
| 1027 | "with %d (%s)\n", fd, strerror(errno)); |
| 1028 | return -1; |
Peter Zijlstra | 08aa0d1 | 2011-11-21 14:42:47 +0100 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0); |
| 1032 | if (addr == (void *)(-1)) { |
Arnaldo Carvalho de Melo | 32c7f73 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1033 | pr_debug("Error: mmap() syscall returned with (%s)\n", |
| 1034 | strerror(errno)); |
| 1035 | goto out_close; |
Peter Zijlstra | 08aa0d1 | 2011-11-21 14:42:47 +0100 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | for (n = 0; n < 6; n++) { |
| 1039 | u64 stamp, now, delta; |
| 1040 | |
| 1041 | stamp = mmap_read_self(addr); |
| 1042 | |
| 1043 | for (i = 0; i < loops; i++) |
| 1044 | tmp++; |
| 1045 | |
| 1046 | now = mmap_read_self(addr); |
| 1047 | loops *= 10; |
| 1048 | |
| 1049 | delta = now - stamp; |
Arnaldo Carvalho de Melo | 23080e4 | 2012-04-26 16:22:09 -0300 | [diff] [blame] | 1050 | pr_debug("%14d: %14Lu\n", n, (long long)delta); |
Peter Zijlstra | 08aa0d1 | 2011-11-21 14:42:47 +0100 | [diff] [blame] | 1051 | |
| 1052 | delta_sum += delta; |
| 1053 | } |
| 1054 | |
| 1055 | munmap(addr, page_size); |
Arnaldo Carvalho de Melo | 23080e4 | 2012-04-26 16:22:09 -0300 | [diff] [blame] | 1056 | pr_debug(" "); |
Arnaldo Carvalho de Melo | 32c7f73 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1057 | out_close: |
| 1058 | close(fd); |
Peter Zijlstra | 08aa0d1 | 2011-11-21 14:42:47 +0100 | [diff] [blame] | 1059 | |
| 1060 | if (!delta_sum) |
| 1061 | return -1; |
| 1062 | |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
| 1066 | static int test__rdpmc(void) |
| 1067 | { |
| 1068 | int status = 0; |
| 1069 | int wret = 0; |
| 1070 | int ret; |
| 1071 | int pid; |
| 1072 | |
| 1073 | pid = fork(); |
| 1074 | if (pid < 0) |
| 1075 | return -1; |
| 1076 | |
| 1077 | if (!pid) { |
| 1078 | ret = __test__rdpmc(); |
| 1079 | |
| 1080 | exit(ret); |
| 1081 | } |
| 1082 | |
| 1083 | wret = waitpid(pid, &status, 0); |
| 1084 | if (wret < 0 || status) |
| 1085 | return -1; |
| 1086 | |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
| 1090 | #endif |
| 1091 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1092 | static int test__perf_pmu(void) |
| 1093 | { |
| 1094 | return perf_pmu__test(); |
| 1095 | } |
| 1096 | |
Arnaldo Carvalho de Melo | 49f20d7 | 2012-09-06 14:55:44 -0300 | [diff] [blame] | 1097 | static int perf_evsel__roundtrip_cache_name_test(void) |
| 1098 | { |
| 1099 | char name[128]; |
| 1100 | int type, op, err = 0, ret = 0, i, idx; |
| 1101 | struct perf_evsel *evsel; |
| 1102 | struct perf_evlist *evlist = perf_evlist__new(NULL, NULL); |
| 1103 | |
| 1104 | if (evlist == NULL) |
| 1105 | return -ENOMEM; |
| 1106 | |
| 1107 | for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { |
| 1108 | for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { |
| 1109 | /* skip invalid cache type */ |
| 1110 | if (!perf_evsel__is_cache_op_valid(type, op)) |
| 1111 | continue; |
| 1112 | |
| 1113 | for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { |
| 1114 | __perf_evsel__hw_cache_type_op_res_name(type, op, i, |
| 1115 | name, sizeof(name)); |
| 1116 | err = parse_events(evlist, name, 0); |
| 1117 | if (err) |
| 1118 | ret = err; |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | idx = 0; |
| 1124 | evsel = perf_evlist__first(evlist); |
| 1125 | |
| 1126 | for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { |
| 1127 | for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { |
| 1128 | /* skip invalid cache type */ |
| 1129 | if (!perf_evsel__is_cache_op_valid(type, op)) |
| 1130 | continue; |
| 1131 | |
| 1132 | for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { |
| 1133 | __perf_evsel__hw_cache_type_op_res_name(type, op, i, |
| 1134 | name, sizeof(name)); |
| 1135 | if (evsel->idx != idx) |
| 1136 | continue; |
| 1137 | |
| 1138 | ++idx; |
| 1139 | |
| 1140 | if (strcmp(perf_evsel__name(evsel), name)) { |
| 1141 | pr_debug("%s != %s\n", perf_evsel__name(evsel), name); |
| 1142 | ret = -1; |
| 1143 | } |
| 1144 | |
| 1145 | evsel = perf_evsel__next(evsel); |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | perf_evlist__delete(evlist); |
| 1151 | return ret; |
| 1152 | } |
| 1153 | |
Arnaldo Carvalho de Melo | 8ad7013 | 2012-09-06 13:11:18 -0300 | [diff] [blame] | 1154 | static int __perf_evsel__name_array_test(const char *names[], int nr_names) |
| 1155 | { |
| 1156 | int i, err; |
| 1157 | struct perf_evsel *evsel; |
| 1158 | struct perf_evlist *evlist = perf_evlist__new(NULL, NULL); |
| 1159 | |
| 1160 | if (evlist == NULL) |
| 1161 | return -ENOMEM; |
| 1162 | |
| 1163 | for (i = 0; i < nr_names; ++i) { |
| 1164 | err = parse_events(evlist, names[i], 0); |
| 1165 | if (err) { |
| 1166 | pr_debug("failed to parse event '%s', err %d\n", |
| 1167 | names[i], err); |
| 1168 | goto out_delete_evlist; |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | err = 0; |
| 1173 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 1174 | if (strcmp(perf_evsel__name(evsel), names[evsel->idx])) { |
| 1175 | --err; |
| 1176 | pr_debug("%s != %s\n", perf_evsel__name(evsel), names[evsel->idx]); |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | out_delete_evlist: |
| 1181 | perf_evlist__delete(evlist); |
| 1182 | return err; |
| 1183 | } |
| 1184 | |
| 1185 | #define perf_evsel__name_array_test(names) \ |
| 1186 | __perf_evsel__name_array_test(names, ARRAY_SIZE(names)) |
| 1187 | |
| 1188 | static int perf_evsel__roundtrip_name_test(void) |
| 1189 | { |
| 1190 | int err = 0, ret = 0; |
| 1191 | |
| 1192 | err = perf_evsel__name_array_test(perf_evsel__hw_names); |
| 1193 | if (err) |
| 1194 | ret = err; |
| 1195 | |
| 1196 | err = perf_evsel__name_array_test(perf_evsel__sw_names); |
| 1197 | if (err) |
| 1198 | ret = err; |
| 1199 | |
Arnaldo Carvalho de Melo | 49f20d7 | 2012-09-06 14:55:44 -0300 | [diff] [blame] | 1200 | err = perf_evsel__roundtrip_cache_name_test(); |
| 1201 | if (err) |
| 1202 | ret = err; |
| 1203 | |
Arnaldo Carvalho de Melo | 8ad7013 | 2012-09-06 13:11:18 -0300 | [diff] [blame] | 1204 | return ret; |
| 1205 | } |
| 1206 | |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 1207 | static struct test { |
| 1208 | const char *desc; |
| 1209 | int (*func)(void); |
| 1210 | } tests[] = { |
| 1211 | { |
| 1212 | .desc = "vmlinux symtab matches kallsyms", |
| 1213 | .func = test__vmlinux_matches_kallsyms, |
| 1214 | }, |
| 1215 | { |
Arnaldo Carvalho de Melo | d854861 | 2011-01-04 00:16:20 -0200 | [diff] [blame] | 1216 | .desc = "detect open syscall event", |
| 1217 | .func = test__open_syscall_event, |
| 1218 | }, |
| 1219 | { |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 1220 | .desc = "detect open syscall event on all cpus", |
| 1221 | .func = test__open_syscall_event_on_all_cpus, |
| 1222 | }, |
| 1223 | { |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 1224 | .desc = "read samples using the mmap interface", |
| 1225 | .func = test__basic_mmap, |
| 1226 | }, |
| 1227 | { |
Jiri Olsa | 13b6256 | 2011-07-14 11:25:33 +0200 | [diff] [blame] | 1228 | .desc = "parse events tests", |
Jiri Olsa | f50246e | 2012-05-21 09:12:49 +0200 | [diff] [blame] | 1229 | .func = parse_events__test, |
Jiri Olsa | 13b6256 | 2011-07-14 11:25:33 +0200 | [diff] [blame] | 1230 | }, |
Peter Zijlstra | 08aa0d1 | 2011-11-21 14:42:47 +0100 | [diff] [blame] | 1231 | #if defined(__x86_64__) || defined(__i386__) |
| 1232 | { |
| 1233 | .desc = "x86 rdpmc test", |
| 1234 | .func = test__rdpmc, |
| 1235 | }, |
| 1236 | #endif |
Jiri Olsa | 13b6256 | 2011-07-14 11:25:33 +0200 | [diff] [blame] | 1237 | { |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 1238 | .desc = "Validate PERF_RECORD_* events & perf_sample fields", |
| 1239 | .func = test__PERF_RECORD, |
| 1240 | }, |
| 1241 | { |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1242 | .desc = "Test perf pmu format parsing", |
| 1243 | .func = test__perf_pmu, |
| 1244 | }, |
| 1245 | { |
Jiri Olsa | f7add55 | 2012-07-22 14:14:40 +0200 | [diff] [blame] | 1246 | .desc = "Test dso data interface", |
| 1247 | .func = dso__test_data, |
| 1248 | }, |
| 1249 | { |
Arnaldo Carvalho de Melo | 8ad7013 | 2012-09-06 13:11:18 -0300 | [diff] [blame] | 1250 | .desc = "roundtrip evsel->name check", |
| 1251 | .func = perf_evsel__roundtrip_name_test, |
| 1252 | }, |
| 1253 | { |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 1254 | .func = NULL, |
| 1255 | }, |
| 1256 | }; |
| 1257 | |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 1258 | static bool perf_test__matches(int curr, int argc, const char *argv[]) |
| 1259 | { |
| 1260 | int i; |
| 1261 | |
| 1262 | if (argc == 0) |
| 1263 | return true; |
| 1264 | |
| 1265 | for (i = 0; i < argc; ++i) { |
| 1266 | char *end; |
| 1267 | long nr = strtoul(argv[i], &end, 10); |
| 1268 | |
| 1269 | if (*end == '\0') { |
| 1270 | if (nr == curr + 1) |
| 1271 | return true; |
| 1272 | continue; |
| 1273 | } |
| 1274 | |
| 1275 | if (strstr(tests[curr].desc, argv[i])) |
| 1276 | return true; |
| 1277 | } |
| 1278 | |
| 1279 | return false; |
| 1280 | } |
| 1281 | |
| 1282 | static int __cmd_test(int argc, const char *argv[]) |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 1283 | { |
| 1284 | int i = 0; |
| 1285 | |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 1286 | while (tests[i].func) { |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 1287 | int curr = i++, err; |
| 1288 | |
| 1289 | if (!perf_test__matches(curr, argc, argv)) |
| 1290 | continue; |
| 1291 | |
| 1292 | pr_info("%2d: %s:", i, tests[curr].desc); |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 1293 | pr_debug("\n--- start ---\n"); |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 1294 | err = tests[curr].func(); |
| 1295 | pr_debug("---- end ----\n%s:", tests[curr].desc); |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 1296 | pr_info(" %s\n", err ? "FAILED!\n" : "Ok"); |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 1302 | static int perf_test__list(int argc, const char **argv) |
| 1303 | { |
| 1304 | int i = 0; |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 1305 | |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 1306 | while (tests[i].func) { |
| 1307 | int curr = i++; |
| 1308 | |
| 1309 | if (argc > 1 && !strstr(tests[curr].desc, argv[1])) |
| 1310 | continue; |
| 1311 | |
| 1312 | pr_info("%2d: %s\n", i, tests[curr].desc); |
| 1313 | } |
| 1314 | |
| 1315 | return 0; |
| 1316 | } |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 1317 | |
| 1318 | int cmd_test(int argc, const char **argv, const char *prefix __used) |
| 1319 | { |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 1320 | const char * const test_usage[] = { |
| 1321 | "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]", |
| 1322 | NULL, |
| 1323 | }; |
| 1324 | const struct option test_options[] = { |
Namhyung Kim | c30ab8a | 2012-01-08 02:25:26 +0900 | [diff] [blame] | 1325 | OPT_INCR('v', "verbose", &verbose, |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 1326 | "be more verbose (show symbol address, etc)"), |
| 1327 | OPT_END() |
| 1328 | }; |
| 1329 | |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 1330 | argc = parse_options(argc, argv, test_options, test_usage, 0); |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 1331 | if (argc >= 1 && !strcmp(argv[0], "list")) |
| 1332 | return perf_test__list(argc, argv); |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 1333 | |
| 1334 | symbol_conf.priv_size = sizeof(int); |
| 1335 | symbol_conf.sort_by_name = true; |
| 1336 | symbol_conf.try_vmlinux_path = true; |
| 1337 | |
| 1338 | if (symbol__init() < 0) |
| 1339 | return -1; |
| 1340 | |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 1341 | return __cmd_test(argc, argv); |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 1342 | } |