1 #include <stdio.h>
2 #include <unistd.h>
3 #include <linux/types.h>
4 #include <sys/prctl.h>
5 
6 #include "parse-events.h"
7 #include "evlist.h"
8 #include "evsel.h"
9 #include "thread_map.h"
10 #include "cpumap.h"
11 #include "tsc.h"
12 #include "tests.h"
13 
14 #define CHECK__(x) {				\
15 	while ((x) < 0) {			\
16 		pr_debug(#x " failed!\n");	\
17 		goto out_err;			\
18 	}					\
19 }
20 
21 #define CHECK_NOT_NULL__(x) {			\
22 	while ((x) == NULL) {			\
23 		pr_debug(#x " failed!\n");	\
24 		goto out_err;			\
25 	}					\
26 }
27 
28 /**
29  * test__perf_time_to_tsc - test converting perf time to TSC.
30  *
31  * This function implements a test that checks that the conversion of perf time
32  * to and from TSC is consistent with the order of events.  If the test passes
33  * %0 is returned, otherwise %-1 is returned.  If TSC conversion is not
34  * supported then then the test passes but " (not supported)" is printed.
35  */
36 int test__perf_time_to_tsc(void)
37 {
38 	struct record_opts opts = {
39 		.mmap_pages	     = UINT_MAX,
40 		.user_freq	     = UINT_MAX,
41 		.user_interval	     = ULLONG_MAX,
42 		.freq		     = 4000,
43 		.target		     = {
44 			.uses_mmap   = true,
45 		},
46 		.sample_time	     = true,
47 	};
48 	struct thread_map *threads = NULL;
49 	struct cpu_map *cpus = NULL;
50 	struct perf_evlist *evlist = NULL;
51 	struct perf_evsel *evsel = NULL;
52 	int err = -1, ret, i;
53 	const char *comm1, *comm2;
54 	struct perf_tsc_conversion tc;
55 	struct perf_event_mmap_page *pc;
56 	union perf_event *event;
57 	u64 test_tsc, comm1_tsc, comm2_tsc;
58 	u64 test_time, comm1_time = 0, comm2_time = 0;
59 
60 	threads = thread_map__new(-1, getpid(), UINT_MAX);
61 	CHECK_NOT_NULL__(threads);
62 
63 	cpus = cpu_map__new(NULL);
64 	CHECK_NOT_NULL__(cpus);
65 
66 	evlist = perf_evlist__new();
67 	CHECK_NOT_NULL__(evlist);
68 
69 	perf_evlist__set_maps(evlist, cpus, threads);
70 
71 	CHECK__(parse_events(evlist, "cycles:u", NULL));
72 
73 	perf_evlist__config(evlist, &opts);
74 
75 	evsel = perf_evlist__first(evlist);
76 
77 	evsel->attr.comm = 1;
78 	evsel->attr.disabled = 1;
79 	evsel->attr.enable_on_exec = 0;
80 
81 	CHECK__(perf_evlist__open(evlist));
82 
83 	CHECK__(perf_evlist__mmap(evlist, UINT_MAX, false));
84 
85 	pc = evlist->mmap[0].base;
86 	ret = perf_read_tsc_conversion(pc, &tc);
87 	if (ret) {
88 		if (ret == -EOPNOTSUPP) {
89 			fprintf(stderr, " (not supported)");
90 			return 0;
91 		}
92 		goto out_err;
93 	}
94 
95 	perf_evlist__enable(evlist);
96 
97 	comm1 = "Test COMM 1";
98 	CHECK__(prctl(PR_SET_NAME, (unsigned long)comm1, 0, 0, 0));
99 
100 	test_tsc = rdtsc();
101 
102 	comm2 = "Test COMM 2";
103 	CHECK__(prctl(PR_SET_NAME, (unsigned long)comm2, 0, 0, 0));
104 
105 	perf_evlist__disable(evlist);
106 
107 	for (i = 0; i < evlist->nr_mmaps; i++) {
108 		while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
109 			struct perf_sample sample;
110 
111 			if (event->header.type != PERF_RECORD_COMM ||
112 			    (pid_t)event->comm.pid != getpid() ||
113 			    (pid_t)event->comm.tid != getpid())
114 				goto next_event;
115 
116 			if (strcmp(event->comm.comm, comm1) == 0) {
117 				CHECK__(perf_evsel__parse_sample(evsel, event,
118 								 &sample));
119 				comm1_time = sample.time;
120 			}
121 			if (strcmp(event->comm.comm, comm2) == 0) {
122 				CHECK__(perf_evsel__parse_sample(evsel, event,
123 								 &sample));
124 				comm2_time = sample.time;
125 			}
126 next_event:
127 			perf_evlist__mmap_consume(evlist, i);
128 		}
129 	}
130 
131 	if (!comm1_time || !comm2_time)
132 		goto out_err;
133 
134 	test_time = tsc_to_perf_time(test_tsc, &tc);
135 	comm1_tsc = perf_time_to_tsc(comm1_time, &tc);
136 	comm2_tsc = perf_time_to_tsc(comm2_time, &tc);
137 
138 	pr_debug("1st event perf time %"PRIu64" tsc %"PRIu64"\n",
139 		 comm1_time, comm1_tsc);
140 	pr_debug("rdtsc          time %"PRIu64" tsc %"PRIu64"\n",
141 		 test_time, test_tsc);
142 	pr_debug("2nd event perf time %"PRIu64" tsc %"PRIu64"\n",
143 		 comm2_time, comm2_tsc);
144 
145 	if (test_time <= comm1_time ||
146 	    test_time >= comm2_time)
147 		goto out_err;
148 
149 	if (test_tsc <= comm1_tsc ||
150 	    test_tsc >= comm2_tsc)
151 		goto out_err;
152 
153 	err = 0;
154 
155 out_err:
156 	if (evlist) {
157 		perf_evlist__disable(evlist);
158 		perf_evlist__delete(evlist);
159 	}
160 
161 	return err;
162 }
163