xref: /aosp_15_r20/external/ltp/lib/tst_timer_test.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2017 Cyril Hrubis <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker #include <sys/prctl.h>
7*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
8*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
9*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker #define TST_NO_DEFAULT_MAIN
13*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_clocks.h"
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_timer_test.h"
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #define MAX_SAMPLES 500
18*49cdfc7eSAndroid Build Coastguard Worker #if defined (__arm__) || defined(__aarch64__)
19*49cdfc7eSAndroid Build Coastguard Worker #define BASE_THRESHOLD 500
20*49cdfc7eSAndroid Build Coastguard Worker #else
21*49cdfc7eSAndroid Build Coastguard Worker #define BASE_THRESHOLD 400
22*49cdfc7eSAndroid Build Coastguard Worker #endif
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker static const char *scall;
25*49cdfc7eSAndroid Build Coastguard Worker static void (*setup)(void);
26*49cdfc7eSAndroid Build Coastguard Worker static void (*cleanup)(void);
27*49cdfc7eSAndroid Build Coastguard Worker static int (*sample)(int clk_id, long long usec);
28*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test *test;
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker static long long *samples;
31*49cdfc7eSAndroid Build Coastguard Worker static unsigned int cur_sample;
32*49cdfc7eSAndroid Build Coastguard Worker static unsigned int monotonic_resolution;
33*49cdfc7eSAndroid Build Coastguard Worker static unsigned int timerslack;
34*49cdfc7eSAndroid Build Coastguard Worker static int virt_env;
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker static char *print_frequency_plot;
37*49cdfc7eSAndroid Build Coastguard Worker static char *file_name;
38*49cdfc7eSAndroid Build Coastguard Worker static char *str_sleep_time;
39*49cdfc7eSAndroid Build Coastguard Worker static char *str_sample_cnt;
40*49cdfc7eSAndroid Build Coastguard Worker static int sleep_time = -1;
41*49cdfc7eSAndroid Build Coastguard Worker static int sample_cnt;
42*49cdfc7eSAndroid Build Coastguard Worker 
print_line(char c,int len)43*49cdfc7eSAndroid Build Coastguard Worker static void print_line(char c, int len)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker 	while (len-- > 0)
46*49cdfc7eSAndroid Build Coastguard Worker 		fputc(c, stderr);
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker 
ceilu(float f)49*49cdfc7eSAndroid Build Coastguard Worker static unsigned int ceilu(float f)
50*49cdfc7eSAndroid Build Coastguard Worker {
51*49cdfc7eSAndroid Build Coastguard Worker 	if (f - (int)f > 0)
52*49cdfc7eSAndroid Build Coastguard Worker 		return (unsigned int)f + 1;
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	return (unsigned int)f;
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker 
flooru(float f)57*49cdfc7eSAndroid Build Coastguard Worker static unsigned int flooru(float f)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker 	return (unsigned int)f;
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker 
bucket_len(unsigned int bucket,unsigned int max_bucket,unsigned int cols)62*49cdfc7eSAndroid Build Coastguard Worker static float bucket_len(unsigned int bucket, unsigned int max_bucket,
63*49cdfc7eSAndroid Build Coastguard Worker 		        unsigned int cols)
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker 	return 1.00 * bucket * cols / max_bucket;
66*49cdfc7eSAndroid Build Coastguard Worker }
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker static const char *table_heading = " Time: us ";
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker /*
71*49cdfc7eSAndroid Build Coastguard Worker  * Line Header: '10023 | '
72*49cdfc7eSAndroid Build Coastguard Worker  */
header_len(long long max_sample)73*49cdfc7eSAndroid Build Coastguard Worker static unsigned int header_len(long long max_sample)
74*49cdfc7eSAndroid Build Coastguard Worker {
75*49cdfc7eSAndroid Build Coastguard Worker 	size_t l = 1;
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	while (max_sample/=10)
78*49cdfc7eSAndroid Build Coastguard Worker 		l++;
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	return MAX(strlen(table_heading) + 2, l + 3);
81*49cdfc7eSAndroid Build Coastguard Worker }
82*49cdfc7eSAndroid Build Coastguard Worker 
frequency_plot(void)83*49cdfc7eSAndroid Build Coastguard Worker static void frequency_plot(void)
84*49cdfc7eSAndroid Build Coastguard Worker {
85*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int cols = 80;
86*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int rows = 20;
87*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i, buckets[rows];
88*49cdfc7eSAndroid Build Coastguard Worker 	long long max_sample = samples[0];
89*49cdfc7eSAndroid Build Coastguard Worker 	long long min_sample = samples[cur_sample-1];
90*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int line_header_len = header_len(max_sample);
91*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int plot_line_len = cols - line_header_len;
92*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int bucket_size;
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker 	memset(buckets, 0, sizeof(buckets));
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	/*
97*49cdfc7eSAndroid Build Coastguard Worker 	 * We work with discrete data buckets smaller than 1 does not make
98*49cdfc7eSAndroid Build Coastguard Worker 	 * sense as well as it's a good idea to keep buckets integer sized
99*49cdfc7eSAndroid Build Coastguard Worker 	 * to avoid scaling artifacts.
100*49cdfc7eSAndroid Build Coastguard Worker 	 */
101*49cdfc7eSAndroid Build Coastguard Worker 	bucket_size = MAX(1u, ceilu(1.00 * (max_sample - min_sample)/(rows-1)));
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < cur_sample; i++) {
104*49cdfc7eSAndroid Build Coastguard Worker 		unsigned int bucket;
105*49cdfc7eSAndroid Build Coastguard Worker 		bucket = flooru(1.00 * (samples[i] - min_sample)/bucket_size);
106*49cdfc7eSAndroid Build Coastguard Worker 		buckets[bucket]++;
107*49cdfc7eSAndroid Build Coastguard Worker 	}
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int max_bucket = buckets[0];
110*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 1; i < rows; i++)
111*49cdfc7eSAndroid Build Coastguard Worker 		max_bucket = MAX(max_bucket, buckets[i]);
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "\n%*s| Frequency\n", line_header_len - 2, table_heading);
114*49cdfc7eSAndroid Build Coastguard Worker 
115*49cdfc7eSAndroid Build Coastguard Worker 	print_line('-', cols);
116*49cdfc7eSAndroid Build Coastguard Worker 	fputc('\n', stderr);
117*49cdfc7eSAndroid Build Coastguard Worker 
118*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int l, r;
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 	for (l = 0; l < rows; l++) {
121*49cdfc7eSAndroid Build Coastguard Worker 		if (buckets[l])
122*49cdfc7eSAndroid Build Coastguard Worker 			break;
123*49cdfc7eSAndroid Build Coastguard Worker 	}
124*49cdfc7eSAndroid Build Coastguard Worker 
125*49cdfc7eSAndroid Build Coastguard Worker 	for (r = rows-1; r > l; r--) {
126*49cdfc7eSAndroid Build Coastguard Worker 		if (buckets[r])
127*49cdfc7eSAndroid Build Coastguard Worker 			break;
128*49cdfc7eSAndroid Build Coastguard Worker 	}
129*49cdfc7eSAndroid Build Coastguard Worker 
130*49cdfc7eSAndroid Build Coastguard Worker 	for (i = l; i <= r; i++) {
131*49cdfc7eSAndroid Build Coastguard Worker 		float len = bucket_len(buckets[i], max_bucket, plot_line_len);
132*49cdfc7eSAndroid Build Coastguard Worker 
133*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "%*lli | ",
134*49cdfc7eSAndroid Build Coastguard Worker 			line_header_len - 3, min_sample + bucket_size*i);
135*49cdfc7eSAndroid Build Coastguard Worker 		print_line('*', len);
136*49cdfc7eSAndroid Build Coastguard Worker 
137*49cdfc7eSAndroid Build Coastguard Worker 		if ((len - (int)len) >= 0.5)
138*49cdfc7eSAndroid Build Coastguard Worker 			fputc('+', stderr);
139*49cdfc7eSAndroid Build Coastguard Worker 		else if ((len - (int)len) >= 0.25)
140*49cdfc7eSAndroid Build Coastguard Worker 			fputc('-', stderr);
141*49cdfc7eSAndroid Build Coastguard Worker 		else if (len < 0.25 && buckets[i])
142*49cdfc7eSAndroid Build Coastguard Worker 			fputc('.', stderr);
143*49cdfc7eSAndroid Build Coastguard Worker 
144*49cdfc7eSAndroid Build Coastguard Worker 		fputc('\n', stderr);
145*49cdfc7eSAndroid Build Coastguard Worker 	}
146*49cdfc7eSAndroid Build Coastguard Worker 
147*49cdfc7eSAndroid Build Coastguard Worker 	print_line('-', cols);
148*49cdfc7eSAndroid Build Coastguard Worker 	fputc('\n', stderr);
149*49cdfc7eSAndroid Build Coastguard Worker 
150*49cdfc7eSAndroid Build Coastguard Worker 	float scale = 1.00 * plot_line_len / max_bucket;
151*49cdfc7eSAndroid Build Coastguard Worker 
152*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr,
153*49cdfc7eSAndroid Build Coastguard Worker 		"%*uus | 1 sample = %.5f '*', %.5f '+', %.5f '-', non-zero '.'\n",
154*49cdfc7eSAndroid Build Coastguard Worker 		line_header_len - 5, bucket_size, scale, scale * 2, scale * 4);
155*49cdfc7eSAndroid Build Coastguard Worker 
156*49cdfc7eSAndroid Build Coastguard Worker 	fputc('\n', stderr);
157*49cdfc7eSAndroid Build Coastguard Worker }
158*49cdfc7eSAndroid Build Coastguard Worker 
tst_timer_sample(void)159*49cdfc7eSAndroid Build Coastguard Worker void tst_timer_sample(void)
160*49cdfc7eSAndroid Build Coastguard Worker {
161*49cdfc7eSAndroid Build Coastguard Worker 	samples[cur_sample++] = tst_timer_elapsed_us();
162*49cdfc7eSAndroid Build Coastguard Worker }
163*49cdfc7eSAndroid Build Coastguard Worker 
cmp(const void * a,const void * b)164*49cdfc7eSAndroid Build Coastguard Worker static int cmp(const void *a, const void *b)
165*49cdfc7eSAndroid Build Coastguard Worker {
166*49cdfc7eSAndroid Build Coastguard Worker 	const long long *aa = a, *bb = b;
167*49cdfc7eSAndroid Build Coastguard Worker 
168*49cdfc7eSAndroid Build Coastguard Worker 	return (*bb - *aa);
169*49cdfc7eSAndroid Build Coastguard Worker }
170*49cdfc7eSAndroid Build Coastguard Worker 
171*49cdfc7eSAndroid Build Coastguard Worker /*
172*49cdfc7eSAndroid Build Coastguard Worker  * The threshold per one syscall is computed as a sum of:
173*49cdfc7eSAndroid Build Coastguard Worker  *
174*49cdfc7eSAndroid Build Coastguard Worker  *  400 or 500 us          - accomodates for context switches, process
175*49cdfc7eSAndroid Build Coastguard Worker  *                           migrations between CPUs on SMP, etc.  Increase to
176*49cdfc7eSAndroid Build Coastguard Worker  *                           500 on arm/arm64 to allow for increased little CPU
177*49cdfc7eSAndroid Build Coastguard Worker  *                           scheduling
178*49cdfc7eSAndroid Build Coastguard Worker  *  2*monotonic_resolution - accomodates for granurality of the CLOCK_MONOTONIC
179*49cdfc7eSAndroid Build Coastguard Worker  *  slack_per_scall        - max of 0.1% of the sleep capped on 100ms or
180*49cdfc7eSAndroid Build Coastguard Worker  *                           current->timer_slack_ns, which is slack allowed
181*49cdfc7eSAndroid Build Coastguard Worker  *                           in kernel
182*49cdfc7eSAndroid Build Coastguard Worker  *
183*49cdfc7eSAndroid Build Coastguard Worker  *  The formula	for slack_per_scall applies to select() and *poll*() syscalls,
184*49cdfc7eSAndroid Build Coastguard Worker  *  the futex and *nanosleep() use only the timer_slack_ns, so we are a bit
185*49cdfc7eSAndroid Build Coastguard Worker  *  less strict here that we could be for these two for longer sleep times...
186*49cdfc7eSAndroid Build Coastguard Worker  *
187*49cdfc7eSAndroid Build Coastguard Worker  * We also allow for outliners, i.e. add some number to the threshold in case
188*49cdfc7eSAndroid Build Coastguard Worker  * that the number of iteration is small. For large enoung number of iterations
189*49cdfc7eSAndroid Build Coastguard Worker  * outliners are discarded and averaged out.
190*49cdfc7eSAndroid Build Coastguard Worker  */
compute_threshold(long long requested_us,unsigned int nsamples)191*49cdfc7eSAndroid Build Coastguard Worker static long long compute_threshold(long long requested_us,
192*49cdfc7eSAndroid Build Coastguard Worker 				   unsigned int nsamples)
193*49cdfc7eSAndroid Build Coastguard Worker {
194*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int slack_per_scall = MIN(100000LL, requested_us / 1000);
195*49cdfc7eSAndroid Build Coastguard Worker 
196*49cdfc7eSAndroid Build Coastguard Worker 	slack_per_scall = MAX(slack_per_scall, timerslack);
197*49cdfc7eSAndroid Build Coastguard Worker 
198*49cdfc7eSAndroid Build Coastguard Worker 	return (BASE_THRESHOLD + 2 * monotonic_resolution + slack_per_scall) * nsamples
199*49cdfc7eSAndroid Build Coastguard Worker 		+ 3000/nsamples;
200*49cdfc7eSAndroid Build Coastguard Worker }
201*49cdfc7eSAndroid Build Coastguard Worker 
202*49cdfc7eSAndroid Build Coastguard Worker /*
203*49cdfc7eSAndroid Build Coastguard Worker  * Returns number of samples to discard.
204*49cdfc7eSAndroid Build Coastguard Worker  *
205*49cdfc7eSAndroid Build Coastguard Worker  * We set it to either at least 1 if number of samples > 1 or 5%.
206*49cdfc7eSAndroid Build Coastguard Worker  */
compute_discard(unsigned int nsamples)207*49cdfc7eSAndroid Build Coastguard Worker static unsigned int compute_discard(unsigned int nsamples)
208*49cdfc7eSAndroid Build Coastguard Worker {
209*49cdfc7eSAndroid Build Coastguard Worker 	if (nsamples == 1)
210*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
211*49cdfc7eSAndroid Build Coastguard Worker 
212*49cdfc7eSAndroid Build Coastguard Worker 	return MAX(1u, nsamples / 20);
213*49cdfc7eSAndroid Build Coastguard Worker }
214*49cdfc7eSAndroid Build Coastguard Worker 
write_to_file(void)215*49cdfc7eSAndroid Build Coastguard Worker static void write_to_file(void)
216*49cdfc7eSAndroid Build Coastguard Worker {
217*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
218*49cdfc7eSAndroid Build Coastguard Worker 	FILE *f;
219*49cdfc7eSAndroid Build Coastguard Worker 
220*49cdfc7eSAndroid Build Coastguard Worker 	if (!file_name)
221*49cdfc7eSAndroid Build Coastguard Worker 		return;
222*49cdfc7eSAndroid Build Coastguard Worker 
223*49cdfc7eSAndroid Build Coastguard Worker 	f = fopen(file_name, "w");
224*49cdfc7eSAndroid Build Coastguard Worker 
225*49cdfc7eSAndroid Build Coastguard Worker 	if (!f) {
226*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN | TERRNO,
227*49cdfc7eSAndroid Build Coastguard Worker 			"Failed to open '%s'", file_name);
228*49cdfc7eSAndroid Build Coastguard Worker 		return;
229*49cdfc7eSAndroid Build Coastguard Worker 	}
230*49cdfc7eSAndroid Build Coastguard Worker 
231*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < cur_sample; i++)
232*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(f, "%lli\n", samples[i]);
233*49cdfc7eSAndroid Build Coastguard Worker 
234*49cdfc7eSAndroid Build Coastguard Worker 	if (fclose(f)) {
235*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN | TERRNO,
236*49cdfc7eSAndroid Build Coastguard Worker 			"Failed to close file '%s'", file_name);
237*49cdfc7eSAndroid Build Coastguard Worker 	}
238*49cdfc7eSAndroid Build Coastguard Worker }
239*49cdfc7eSAndroid Build Coastguard Worker 
240*49cdfc7eSAndroid Build Coastguard Worker 
241*49cdfc7eSAndroid Build Coastguard Worker /*
242*49cdfc7eSAndroid Build Coastguard Worker  * Timer testing function.
243*49cdfc7eSAndroid Build Coastguard Worker  *
244*49cdfc7eSAndroid Build Coastguard Worker  * What we do here is:
245*49cdfc7eSAndroid Build Coastguard Worker  *
246*49cdfc7eSAndroid Build Coastguard Worker  * * Take nsamples measurements of the timer function, the function
247*49cdfc7eSAndroid Build Coastguard Worker  *   to be sampled is defined in the actual test.
248*49cdfc7eSAndroid Build Coastguard Worker  *
249*49cdfc7eSAndroid Build Coastguard Worker  * * We sort the array of samples, then:
250*49cdfc7eSAndroid Build Coastguard Worker  *
251*49cdfc7eSAndroid Build Coastguard Worker  *   - look for outliners which are samples where the sleep time has exceeded
252*49cdfc7eSAndroid Build Coastguard Worker  *     requested sleep time by an order of magnitude and, at the same time, are
253*49cdfc7eSAndroid Build Coastguard Worker  *     greater than clock resolution multiplied by three.
254*49cdfc7eSAndroid Build Coastguard Worker  *
255*49cdfc7eSAndroid Build Coastguard Worker  *   - check for samples where the call has woken up too early which is a plain
256*49cdfc7eSAndroid Build Coastguard Worker  *     old bug
257*49cdfc7eSAndroid Build Coastguard Worker  *
258*49cdfc7eSAndroid Build Coastguard Worker  *   - then we compute truncated mean and compare that with the requested sleep
259*49cdfc7eSAndroid Build Coastguard Worker  *     time increased by a threshold
260*49cdfc7eSAndroid Build Coastguard Worker  */
do_timer_test(long long usec,unsigned int nsamples)261*49cdfc7eSAndroid Build Coastguard Worker void do_timer_test(long long usec, unsigned int nsamples)
262*49cdfc7eSAndroid Build Coastguard Worker {
263*49cdfc7eSAndroid Build Coastguard Worker 	long long trunc_mean, median;
264*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int discard = compute_discard(nsamples);
265*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int keep_samples = nsamples - discard;
266*49cdfc7eSAndroid Build Coastguard Worker 	long long threshold = compute_threshold(usec, keep_samples);
267*49cdfc7eSAndroid Build Coastguard Worker 	int i;
268*49cdfc7eSAndroid Build Coastguard Worker 	int failed = 0;
269*49cdfc7eSAndroid Build Coastguard Worker 
270*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO,
271*49cdfc7eSAndroid Build Coastguard Worker 		"%s sleeping for %llius %u iterations, threshold %.2fus",
272*49cdfc7eSAndroid Build Coastguard Worker 		scall, usec, nsamples, 1.00 * threshold / (keep_samples));
273*49cdfc7eSAndroid Build Coastguard Worker 
274*49cdfc7eSAndroid Build Coastguard Worker 	cur_sample = 0;
275*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < (int)nsamples; i++) {
276*49cdfc7eSAndroid Build Coastguard Worker 		if (sample(CLOCK_MONOTONIC, usec)) {
277*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TINFO, "sampling function failed, exiting");
278*49cdfc7eSAndroid Build Coastguard Worker 			return;
279*49cdfc7eSAndroid Build Coastguard Worker 		}
280*49cdfc7eSAndroid Build Coastguard Worker 	}
281*49cdfc7eSAndroid Build Coastguard Worker 
282*49cdfc7eSAndroid Build Coastguard Worker 	qsort(samples, nsamples, sizeof(samples[0]), cmp);
283*49cdfc7eSAndroid Build Coastguard Worker 
284*49cdfc7eSAndroid Build Coastguard Worker 	write_to_file();
285*49cdfc7eSAndroid Build Coastguard Worker 
286*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; samples[i] > 10 * usec && i < (int)nsamples; i++) {
287*49cdfc7eSAndroid Build Coastguard Worker 		if (samples[i] <= 3 * monotonic_resolution)
288*49cdfc7eSAndroid Build Coastguard Worker 			break;
289*49cdfc7eSAndroid Build Coastguard Worker 	}
290*49cdfc7eSAndroid Build Coastguard Worker 
291*49cdfc7eSAndroid Build Coastguard Worker 	if (i > 0) {
292*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Found %i outliners in [%lli,%lli] range",
293*49cdfc7eSAndroid Build Coastguard Worker 			i, samples[0], samples[i-1]);
294*49cdfc7eSAndroid Build Coastguard Worker 	}
295*49cdfc7eSAndroid Build Coastguard Worker 
296*49cdfc7eSAndroid Build Coastguard Worker 	for (i = nsamples - 1; samples[i] < usec && i > -1; i--);
297*49cdfc7eSAndroid Build Coastguard Worker 
298*49cdfc7eSAndroid Build Coastguard Worker 	if (i < (int)nsamples - 1) {
299*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "%s woken up early %u times range: [%lli,%lli]",
300*49cdfc7eSAndroid Build Coastguard Worker 			scall, nsamples - 1 - i,
301*49cdfc7eSAndroid Build Coastguard Worker 			samples[i+1], samples[nsamples-1]);
302*49cdfc7eSAndroid Build Coastguard Worker 		failed = 1;
303*49cdfc7eSAndroid Build Coastguard Worker 	}
304*49cdfc7eSAndroid Build Coastguard Worker 
305*49cdfc7eSAndroid Build Coastguard Worker 	median = samples[nsamples/2];
306*49cdfc7eSAndroid Build Coastguard Worker 
307*49cdfc7eSAndroid Build Coastguard Worker 	trunc_mean = 0;
308*49cdfc7eSAndroid Build Coastguard Worker 
309*49cdfc7eSAndroid Build Coastguard Worker 	for (i = discard; i < (int)nsamples; i++)
310*49cdfc7eSAndroid Build Coastguard Worker 		trunc_mean += samples[i];
311*49cdfc7eSAndroid Build Coastguard Worker 
312*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO,
313*49cdfc7eSAndroid Build Coastguard Worker 		"min %llius, max %llius, median %llius, trunc mean %.2fus (discarded %u)",
314*49cdfc7eSAndroid Build Coastguard Worker 		samples[nsamples-1], samples[0], median,
315*49cdfc7eSAndroid Build Coastguard Worker 		1.00 * trunc_mean / keep_samples, discard);
316*49cdfc7eSAndroid Build Coastguard Worker 
317*49cdfc7eSAndroid Build Coastguard Worker 	if (virt_env) {
318*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO,
319*49cdfc7eSAndroid Build Coastguard Worker 			"Virtualisation detected, skipping oversleep checks");
320*49cdfc7eSAndroid Build Coastguard Worker 	} else if (trunc_mean > (nsamples - discard) * usec + threshold) {
321*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "%s slept for too long", scall);
322*49cdfc7eSAndroid Build Coastguard Worker 
323*49cdfc7eSAndroid Build Coastguard Worker 		if (!print_frequency_plot)
324*49cdfc7eSAndroid Build Coastguard Worker 			frequency_plot();
325*49cdfc7eSAndroid Build Coastguard Worker 
326*49cdfc7eSAndroid Build Coastguard Worker 		failed = 1;
327*49cdfc7eSAndroid Build Coastguard Worker 	}
328*49cdfc7eSAndroid Build Coastguard Worker 
329*49cdfc7eSAndroid Build Coastguard Worker 	if (print_frequency_plot)
330*49cdfc7eSAndroid Build Coastguard Worker 		frequency_plot();
331*49cdfc7eSAndroid Build Coastguard Worker 
332*49cdfc7eSAndroid Build Coastguard Worker 	if (!failed)
333*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Measured times are within thresholds");
334*49cdfc7eSAndroid Build Coastguard Worker }
335*49cdfc7eSAndroid Build Coastguard Worker 
336*49cdfc7eSAndroid Build Coastguard Worker static void parse_timer_opts(void);
337*49cdfc7eSAndroid Build Coastguard Worker 
set_latency(void)338*49cdfc7eSAndroid Build Coastguard Worker static int set_latency(void)
339*49cdfc7eSAndroid Build Coastguard Worker {
340*49cdfc7eSAndroid Build Coastguard Worker         int fd, latency = 0;
341*49cdfc7eSAndroid Build Coastguard Worker 
342*49cdfc7eSAndroid Build Coastguard Worker         fd = open("/dev/cpu_dma_latency", O_WRONLY);
343*49cdfc7eSAndroid Build Coastguard Worker         if (fd < 0)
344*49cdfc7eSAndroid Build Coastguard Worker                 return fd;
345*49cdfc7eSAndroid Build Coastguard Worker 
346*49cdfc7eSAndroid Build Coastguard Worker         return write(fd, &latency, sizeof(latency));
347*49cdfc7eSAndroid Build Coastguard Worker }
348*49cdfc7eSAndroid Build Coastguard Worker 
timer_setup(void)349*49cdfc7eSAndroid Build Coastguard Worker static void timer_setup(void)
350*49cdfc7eSAndroid Build Coastguard Worker {
351*49cdfc7eSAndroid Build Coastguard Worker 	struct timespec t;
352*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
353*49cdfc7eSAndroid Build Coastguard Worker 
354*49cdfc7eSAndroid Build Coastguard Worker 	if (setup)
355*49cdfc7eSAndroid Build Coastguard Worker 		setup();
356*49cdfc7eSAndroid Build Coastguard Worker 
357*49cdfc7eSAndroid Build Coastguard Worker 	/*
358*49cdfc7eSAndroid Build Coastguard Worker 	 * Running tests in VM may cause timing issues, disable upper bound
359*49cdfc7eSAndroid Build Coastguard Worker 	 * checks if any hypervisor is detected.
360*49cdfc7eSAndroid Build Coastguard Worker 	 */
361*49cdfc7eSAndroid Build Coastguard Worker 	virt_env = tst_is_virt(VIRT_ANY);
362*49cdfc7eSAndroid Build Coastguard Worker 	tst_clock_getres(CLOCK_MONOTONIC, &t);
363*49cdfc7eSAndroid Build Coastguard Worker 
364*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "CLOCK_MONOTONIC resolution %lins", (long)t.tv_nsec);
365*49cdfc7eSAndroid Build Coastguard Worker 
366*49cdfc7eSAndroid Build Coastguard Worker 	monotonic_resolution = t.tv_nsec / 1000;
367*49cdfc7eSAndroid Build Coastguard Worker 	timerslack = 50;
368*49cdfc7eSAndroid Build Coastguard Worker 
369*49cdfc7eSAndroid Build Coastguard Worker #ifdef PR_GET_TIMERSLACK
370*49cdfc7eSAndroid Build Coastguard Worker 	ret = prctl(PR_GET_TIMERSLACK);
371*49cdfc7eSAndroid Build Coastguard Worker 	if (ret < 0) {
372*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "prctl(PR_GET_TIMERSLACK) = -1, using %uus",
373*49cdfc7eSAndroid Build Coastguard Worker 			timerslack);
374*49cdfc7eSAndroid Build Coastguard Worker 	} else {
375*49cdfc7eSAndroid Build Coastguard Worker 		timerslack = ret / 1000;
376*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "prctl(PR_GET_TIMERSLACK) = %ius", timerslack);
377*49cdfc7eSAndroid Build Coastguard Worker 	}
378*49cdfc7eSAndroid Build Coastguard Worker #else
379*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "PR_GET_TIMERSLACK not defined, using %uus",
380*49cdfc7eSAndroid Build Coastguard Worker 		timerslack);
381*49cdfc7eSAndroid Build Coastguard Worker #endif /* PR_GET_TIMERSLACK */
382*49cdfc7eSAndroid Build Coastguard Worker 	parse_timer_opts();
383*49cdfc7eSAndroid Build Coastguard Worker 
384*49cdfc7eSAndroid Build Coastguard Worker 	samples = SAFE_MALLOC(sizeof(long long) * MAX(MAX_SAMPLES, sample_cnt));
385*49cdfc7eSAndroid Build Coastguard Worker 	if (set_latency() < 0)
386*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Failed to set zero latency constraint: %m");
387*49cdfc7eSAndroid Build Coastguard Worker }
388*49cdfc7eSAndroid Build Coastguard Worker 
timer_cleanup(void)389*49cdfc7eSAndroid Build Coastguard Worker static void timer_cleanup(void)
390*49cdfc7eSAndroid Build Coastguard Worker {
391*49cdfc7eSAndroid Build Coastguard Worker 	free(samples);
392*49cdfc7eSAndroid Build Coastguard Worker 
393*49cdfc7eSAndroid Build Coastguard Worker 	if (cleanup)
394*49cdfc7eSAndroid Build Coastguard Worker 		cleanup();
395*49cdfc7eSAndroid Build Coastguard Worker }
396*49cdfc7eSAndroid Build Coastguard Worker 
397*49cdfc7eSAndroid Build Coastguard Worker static struct tst_timer_tcase {
398*49cdfc7eSAndroid Build Coastguard Worker 	long long usec;
399*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int samples;
400*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
401*49cdfc7eSAndroid Build Coastguard Worker 	{1000,  500},
402*49cdfc7eSAndroid Build Coastguard Worker 	{2000,  500},
403*49cdfc7eSAndroid Build Coastguard Worker 	{5000,  300},
404*49cdfc7eSAndroid Build Coastguard Worker 	{10000, 100},
405*49cdfc7eSAndroid Build Coastguard Worker 	{25000,  50},
406*49cdfc7eSAndroid Build Coastguard Worker 	{100000, 10},
407*49cdfc7eSAndroid Build Coastguard Worker 	{1000000, 2},
408*49cdfc7eSAndroid Build Coastguard Worker };
409*49cdfc7eSAndroid Build Coastguard Worker 
timer_test_fn(unsigned int n)410*49cdfc7eSAndroid Build Coastguard Worker static void timer_test_fn(unsigned int n)
411*49cdfc7eSAndroid Build Coastguard Worker {
412*49cdfc7eSAndroid Build Coastguard Worker 	do_timer_test(tcases[n].usec, tcases[n].samples);
413*49cdfc7eSAndroid Build Coastguard Worker }
414*49cdfc7eSAndroid Build Coastguard Worker 
single_timer_test(void)415*49cdfc7eSAndroid Build Coastguard Worker static void single_timer_test(void)
416*49cdfc7eSAndroid Build Coastguard Worker {
417*49cdfc7eSAndroid Build Coastguard Worker 	do_timer_test(sleep_time, sample_cnt);
418*49cdfc7eSAndroid Build Coastguard Worker }
419*49cdfc7eSAndroid Build Coastguard Worker 
420*49cdfc7eSAndroid Build Coastguard Worker static struct tst_option options[] = {
421*49cdfc7eSAndroid Build Coastguard Worker 	{"p",  &print_frequency_plot, "-p       Print frequency plot"},
422*49cdfc7eSAndroid Build Coastguard Worker 	{"s:", &str_sleep_time, "-s us    Sleep time"},
423*49cdfc7eSAndroid Build Coastguard Worker 	{"n:", &str_sample_cnt, "-n uint  Number of samples to take"},
424*49cdfc7eSAndroid Build Coastguard Worker 	{"f:", &file_name, "-f fname Write measured samples into a file"},
425*49cdfc7eSAndroid Build Coastguard Worker 	{NULL, NULL, NULL}
426*49cdfc7eSAndroid Build Coastguard Worker };
427*49cdfc7eSAndroid Build Coastguard Worker 
parse_timer_opts(void)428*49cdfc7eSAndroid Build Coastguard Worker static void parse_timer_opts(void)
429*49cdfc7eSAndroid Build Coastguard Worker {
430*49cdfc7eSAndroid Build Coastguard Worker 	size_t i;
431*49cdfc7eSAndroid Build Coastguard Worker 	long long runtime_us = 0;
432*49cdfc7eSAndroid Build Coastguard Worker 
433*49cdfc7eSAndroid Build Coastguard Worker 	if (str_sleep_time) {
434*49cdfc7eSAndroid Build Coastguard Worker 		if (tst_parse_int(str_sleep_time, &sleep_time, 0, INT_MAX)) {
435*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TBROK,
436*49cdfc7eSAndroid Build Coastguard Worker 				"Invalid sleep time '%s'", str_sleep_time);
437*49cdfc7eSAndroid Build Coastguard Worker 		}
438*49cdfc7eSAndroid Build Coastguard Worker 	}
439*49cdfc7eSAndroid Build Coastguard Worker 
440*49cdfc7eSAndroid Build Coastguard Worker 	if (str_sample_cnt) {
441*49cdfc7eSAndroid Build Coastguard Worker 		if (tst_parse_int(str_sample_cnt, &sample_cnt, 1, INT_MAX)) {
442*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TBROK,
443*49cdfc7eSAndroid Build Coastguard Worker 				"Invalid sample count '%s'", str_sample_cnt);
444*49cdfc7eSAndroid Build Coastguard Worker 		}
445*49cdfc7eSAndroid Build Coastguard Worker 	}
446*49cdfc7eSAndroid Build Coastguard Worker 
447*49cdfc7eSAndroid Build Coastguard Worker 	if (str_sleep_time || str_sample_cnt) {
448*49cdfc7eSAndroid Build Coastguard Worker 		if (sleep_time < 0)
449*49cdfc7eSAndroid Build Coastguard Worker 			sleep_time = 10000;
450*49cdfc7eSAndroid Build Coastguard Worker 
451*49cdfc7eSAndroid Build Coastguard Worker 		if (!sample_cnt)
452*49cdfc7eSAndroid Build Coastguard Worker 			sample_cnt = 500;
453*49cdfc7eSAndroid Build Coastguard Worker 
454*49cdfc7eSAndroid Build Coastguard Worker 		runtime_us = sleep_time * sample_cnt;
455*49cdfc7eSAndroid Build Coastguard Worker 
456*49cdfc7eSAndroid Build Coastguard Worker 		test->test_all = single_timer_test;
457*49cdfc7eSAndroid Build Coastguard Worker 		test->test = NULL;
458*49cdfc7eSAndroid Build Coastguard Worker 		test->tcnt = 0;
459*49cdfc7eSAndroid Build Coastguard Worker 	} else {
460*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < ARRAY_SIZE(tcases); i++)
461*49cdfc7eSAndroid Build Coastguard Worker 			runtime_us += tcases[i].usec * tcases[i].samples;
462*49cdfc7eSAndroid Build Coastguard Worker 	}
463*49cdfc7eSAndroid Build Coastguard Worker 
464*49cdfc7eSAndroid Build Coastguard Worker 	tst_set_max_runtime((runtime_us + runtime_us/10)/1000000);
465*49cdfc7eSAndroid Build Coastguard Worker }
466*49cdfc7eSAndroid Build Coastguard Worker 
tst_timer_test_setup(struct tst_test * timer_test)467*49cdfc7eSAndroid Build Coastguard Worker struct tst_test *tst_timer_test_setup(struct tst_test *timer_test)
468*49cdfc7eSAndroid Build Coastguard Worker {
469*49cdfc7eSAndroid Build Coastguard Worker 	setup = timer_test->setup;
470*49cdfc7eSAndroid Build Coastguard Worker 	cleanup = timer_test->cleanup;
471*49cdfc7eSAndroid Build Coastguard Worker 	scall = timer_test->scall;
472*49cdfc7eSAndroid Build Coastguard Worker 	sample = timer_test->sample;
473*49cdfc7eSAndroid Build Coastguard Worker 
474*49cdfc7eSAndroid Build Coastguard Worker 	timer_test->scall = NULL;
475*49cdfc7eSAndroid Build Coastguard Worker 	timer_test->setup = timer_setup;
476*49cdfc7eSAndroid Build Coastguard Worker 	timer_test->cleanup = timer_cleanup;
477*49cdfc7eSAndroid Build Coastguard Worker 	timer_test->test = timer_test_fn;
478*49cdfc7eSAndroid Build Coastguard Worker 	timer_test->tcnt = ARRAY_SIZE(tcases);
479*49cdfc7eSAndroid Build Coastguard Worker 	timer_test->sample = NULL;
480*49cdfc7eSAndroid Build Coastguard Worker 	timer_test->options = options;
481*49cdfc7eSAndroid Build Coastguard Worker 
482*49cdfc7eSAndroid Build Coastguard Worker 	test = timer_test;
483*49cdfc7eSAndroid Build Coastguard Worker 
484*49cdfc7eSAndroid Build Coastguard Worker 	return timer_test;
485*49cdfc7eSAndroid Build Coastguard Worker }
486