| Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 1 | #include "thread_map.h" | 
 | 2 | #include "evsel.h" | 
 | 3 | #include "debug.h" | 
 | 4 | #include "tests.h" | 
 | 5 |  | 
 | 6 | int test__open_syscall_event(void) | 
 | 7 | { | 
 | 8 | 	int err = -1, fd; | 
 | 9 | 	struct thread_map *threads; | 
 | 10 | 	struct perf_evsel *evsel; | 
 | 11 | 	struct perf_event_attr attr; | 
 | 12 | 	unsigned int nr_open_calls = 111, i; | 
 | 13 | 	int id = trace_event__id("sys_enter_open"); | 
 | 14 |  | 
 | 15 | 	if (id < 0) { | 
 | 16 | 		pr_debug("is debugfs mounted on /sys/kernel/debug?\n"); | 
 | 17 | 		return -1; | 
 | 18 | 	} | 
 | 19 |  | 
 | 20 | 	threads = thread_map__new(-1, getpid(), UINT_MAX); | 
 | 21 | 	if (threads == NULL) { | 
 | 22 | 		pr_debug("thread_map__new\n"); | 
 | 23 | 		return -1; | 
 | 24 | 	} | 
 | 25 |  | 
 | 26 | 	memset(&attr, 0, sizeof(attr)); | 
 | 27 | 	attr.type = PERF_TYPE_TRACEPOINT; | 
 | 28 | 	attr.config = id; | 
 | 29 | 	evsel = perf_evsel__new(&attr, 0); | 
 | 30 | 	if (evsel == NULL) { | 
 | 31 | 		pr_debug("perf_evsel__new\n"); | 
 | 32 | 		goto out_thread_map_delete; | 
 | 33 | 	} | 
 | 34 |  | 
 | 35 | 	if (perf_evsel__open_per_thread(evsel, threads) < 0) { | 
 | 36 | 		pr_debug("failed to open counter: %s, " | 
 | 37 | 			 "tweak /proc/sys/kernel/perf_event_paranoid?\n", | 
 | 38 | 			 strerror(errno)); | 
 | 39 | 		goto out_evsel_delete; | 
 | 40 | 	} | 
 | 41 |  | 
 | 42 | 	for (i = 0; i < nr_open_calls; ++i) { | 
 | 43 | 		fd = open("/etc/passwd", O_RDONLY); | 
 | 44 | 		close(fd); | 
 | 45 | 	} | 
 | 46 |  | 
 | 47 | 	if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) { | 
 | 48 | 		pr_debug("perf_evsel__read_on_cpu\n"); | 
 | 49 | 		goto out_close_fd; | 
 | 50 | 	} | 
 | 51 |  | 
 | 52 | 	if (evsel->counts->cpu[0].val != nr_open_calls) { | 
 | 53 | 		pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n", | 
 | 54 | 			 nr_open_calls, evsel->counts->cpu[0].val); | 
 | 55 | 		goto out_close_fd; | 
 | 56 | 	} | 
 | 57 |  | 
 | 58 | 	err = 0; | 
 | 59 | out_close_fd: | 
 | 60 | 	perf_evsel__close_fd(evsel, 1, threads->nr); | 
 | 61 | out_evsel_delete: | 
 | 62 | 	perf_evsel__delete(evsel); | 
 | 63 | out_thread_map_delete: | 
 | 64 | 	thread_map__delete(threads); | 
 | 65 | 	return err; | 
 | 66 | } |