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