Linux Audio

Check our new training course

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