summaryrefslogtreecommitdiff
path: root/usr/src/test/os-tests/tests/clock_gettime.c
blob: b6bfdbe7bb3d82cf7662a7ddc3e7ef9a13319b9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2021 Oxide Comptuer Company
 */

/*
 * Test a bunch of basics around clocks.
 */

#include <time.h>
#include <err.h>
#include <stdlib.h>
#include <libproc.h>
#include <thread.h>
#include <sys/sysmacros.h>

typedef hrtime_t (*clock_alttime_f)(void);

typedef struct clock_gettime_test {
	clockid_t	cgt_clock;
	clock_alttime_f	cgt_alt;
	const char	*cgt_name;
} clock_gettime_test_t;

typedef struct clock_gettime_thr_arg {
	hrtime_t	cgta_usr;
	hrtime_t	cgta_usrsys;
} clock_gettime_thr_arg_t;

static hrtime_t
clock_ts2hrt(const timespec_t *tsp)
{
	return ((tsp->tv_sec * NANOSEC) + tsp->tv_nsec);
}

static hrtime_t
clock_gettime_proc(void)
{
	psinfo_t ps;

	if (proc_get_psinfo(getpid(), &ps) != 0) {
		warn("failed to get psinfo for process");
		return (0);
	}

	return (clock_ts2hrt(&ps.pr_time));
}

static hrtime_t
clock_gettime_thread(void)
{
	lwpsinfo_t lwpsinfo;

	if (proc_get_lwpsinfo(getpid(), thr_self(), &lwpsinfo) != 0) {
		warn("failed to get lwpsinfo for thread %u", thr_self());
		return (0);
	}

	return (clock_ts2hrt(&lwpsinfo.pr_time));
}

clock_gettime_test_t clock_tests[] = {
	{ CLOCK_HIGHRES, gethrtime, "highres" },
	{ CLOCK_VIRTUAL, gethrvtime, "virtual" },
	{ CLOCK_THREAD_CPUTIME_ID, clock_gettime_thread, "thread_cputime" },
	{ CLOCK_PROCESS_CPUTIME_ID, clock_gettime_proc, "proc_cputime" }
};

/*
 * Do a series of reads of the clock from clock_gettime and its secondary
 * source. Make sure that we always see increasing values.
 */
static boolean_t
clock_test(clock_gettime_test_t *test)
{
	hrtime_t hrt0, hrt1, hrt2, convts0, convts1;
	struct timespec ts0, ts1;
	boolean_t ret = B_TRUE;

	if (clock_gettime(test->cgt_clock, &ts0) != 0) {
		warn("failed to get clock %u", test->cgt_clock);
		return (B_FALSE);
	}

	hrt0 = test->cgt_alt();
	hrt1 = test->cgt_alt();

	if (clock_gettime(test->cgt_clock, &ts1) != 0) {
		warn("failed to get clock %u", test->cgt_clock);
		return (B_FALSE);
	}

	hrt2 = test->cgt_alt();

	convts0 = clock_ts2hrt(&ts0);
	convts1 = clock_ts2hrt(&ts1);

	if (convts0 > hrt0) {
		warnx("clock %s traveled backwards, clock_gettime ahead of "
		    "later alternate: clock_gettime %lld, alternate: %lld",
		    test->cgt_name, convts0, hrt0);
		ret = B_FALSE;
	}

	if (hrt0 > hrt1) {
		warnx("clock %s traveled backwards, alternate ahead of "
		    "later alternate: first alternate %lld, later "
		    "alternate: %lld", test->cgt_name, hrt0, hrt1);
		ret = B_FALSE;
	}

	if (convts1 > hrt2) {
		warnx("clock %s traveled backwards, clock_gettime ahead of "
		    "later alternate: clock_gettime %lld, alternate: %lld",
		    test->cgt_name, convts1, hrt2);
		ret = B_FALSE;
	}

	if (hrt1 > hrt2) {
		warnx("clock %s traveled backwards, alternate ahead of "
		    "later alternate: first alternate %lld, later "
		    "alternate: %lld", test->cgt_name, hrt1, hrt2);
		ret = B_FALSE;
	}

	if (convts0 > convts1) {
		warnx("clock %s traveled backwards, clock_gettime ahead of "
		    "later clock_gettime: first clock_gettime %lld, later "
		    "clock_gettime: %lld", test->cgt_name, convts0, convts1);
		ret = B_FALSE;
	}

	return (ret);
}

static void *
clock_test_thr(void *arg)
{
	boolean_t ret = B_TRUE;

	for (uint_t i = 0; i < ARRAY_SIZE(clock_tests); i++) {
		boolean_t rval = clock_test(&clock_tests[i]);
		if (!rval) {
			ret = B_FALSE;
		}

		(void) printf("TEST %s: basic %s usage and interleaving%s\n",
		    rval ? "PASSED" : "FAILED", clock_tests[i].cgt_name,
		    thr_self() == 1 ? "" : " (in thread)");
	}

	return ((void *)(uintptr_t)ret);
}

static void *
clock_test_cputime_thr(void *arg)
{
	struct timespec ts;
	clock_gettime_thr_arg_t *cp = arg;

	if (clock_gettime(CLOCK_VIRTUAL, &ts) != 0) {
		warn("failed to get clock CLOCK_VIRTUAL");
		cp->cgta_usr = 0;
	} else {
		cp->cgta_usr = clock_ts2hrt(&ts);
	}

	if (clock_gettime(CLOCK_VIRTUAL, &ts) != 0) {
		warn("failed to get clock CLOCK_VIRTUAL");
		cp->cgta_usrsys = 0;
	} else {
		cp->cgta_usrsys = clock_ts2hrt(&ts);
	}

	return (NULL);
}

/*
 * Compare the value of CLOCK_THREAD_CPUTIME_ID between a new thread and the
 * main thread.
 */
static boolean_t
clock_test_thread_clock(void)
{
	thread_t thr;
	clock_gettime_thr_arg_t arg;
	hrtime_t hrt;
	struct timespec ts;
	boolean_t ret = B_TRUE;

	if (thr_create(NULL, 0, clock_test_cputime_thr, &arg, 0, &thr) != 0) {
		errx(EXIT_FAILURE, "failed to create thread to run basic "
		    "tests!");
	}

	if (thr_join(thr, NULL, NULL) != 0) {
		errx(EXIT_FAILURE, "failed to join to thread that ran basic "
		    "tests");
	}

	if (clock_gettime(CLOCK_VIRTUAL, &ts) != 0) {
		warn("failed to get clock CLOCK_VIRTUAL");
		return (B_FALSE);
	}

	hrt = clock_ts2hrt(&ts);
	if (arg.cgta_usr > hrt) {
		warnx("new thread %u somehow had higher CLOCK_VIRTUAL time "
		    "than main thread: new thread: %lld, main thread: %lld",
		    thr, hrt, arg.cgta_usr);
		ret = B_FALSE;
	}

	if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) != 0) {
		warn("failed to get clock CLOCK_THREAD_CPUTIME_ID");
		return (B_FALSE);
	}

	hrt = clock_ts2hrt(&ts);
	if (arg.cgta_usr > hrt) {
		warnx("new thread %u somehow had higher "
		    "CLOCK_THREAD_CPUTIME_ID time than main thread: new "
		    "thread: %lld, main thread: %lld", thr, hrt, arg.cgta_usr);
		ret = B_FALSE;
	}

	return (ret);
}

/*
 * This test is a little circumspect. It's basically going to argue that all the
 * time we spent doing kernel actions should be larger than the additional bit
 * of user time to make a subsequent system call. That seems probably
 * reasonable given everything we've done; however, there's no way to feel like
 * it's not possibly going to lead to false positives. If so, then just delete
 * this.
 */
static boolean_t
clock_test_thread_sys(void)
{
	struct timespec usr, sys;
	hrtime_t hrtusr, hrtsys;

	if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &sys) != 0) {
		warn("failed to get clock CLOCK_THREAD_CPUTIME_ID");
		return (B_FALSE);
	}

	if (clock_gettime(CLOCK_VIRTUAL, &usr) != 0) {
		warn("failed to get clock CLOCK_VIRTUAL");
		return (B_FALSE);
	}

	hrtusr = clock_ts2hrt(&usr);
	hrtsys = clock_ts2hrt(&sys);

	if (hrtusr > hrtsys) {
		warnx("CLOCK_VIRTUAL was greater than CLOCK_THREAD_CPUTIME_ID: "
		    "usr time: %lld, usr/sys time: %lld (this may be a race)",
		    hrtusr, hrtsys);
		return (B_FALSE);
	}

	return (B_TRUE);
}

/*
 * This is similar to clock_test_thread_sys(), but using the process clock and
 * the thread clock. This is circumspect for similar reasons.
 */
static boolean_t
clock_test_thread_proc(void)
{
	struct timespec thr, proc;
	hrtime_t hrtthr, hrtproc;

	if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &proc) != 0) {
		warn("failed to get clock CLOCK_VIRTUAL");
		return (B_FALSE);
	}

	if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &thr) != 0) {
		warn("failed to get clock CLOCK_THREAD_CPUTIME_ID");
		return (B_FALSE);
	}

	hrtthr = clock_ts2hrt(&thr);
	hrtproc = clock_ts2hrt(&proc);

	if (hrtthr > hrtproc) {
		warnx("CLOCK_THRAD_CPUTIME_ID was greater than "
		    "CLOCK_PROCESS_CPUTIME_ID: thr time: %lld, proc time: %lld "
		    "(this may be a race)", hrtthr, hrtproc);
		return (B_FALSE);
	}

	return (B_TRUE);
}

int
main(void)
{
	int ret = EXIT_SUCCESS;
	void *thr_ret;
	thread_t thr;
	boolean_t bval;

	thr_ret = clock_test_thr(NULL);
	if (!(boolean_t)(uintptr_t)thr_ret) {
		ret = EXIT_FAILURE;
	}

	if (thr_create(NULL, 0, clock_test_thr, NULL, 0, &thr) != 0) {
		errx(EXIT_FAILURE, "failed to create thread to run basic "
		    "tests!");
	}

	if (thr_join(thr, NULL, &thr_ret) != 0) {
		errx(EXIT_FAILURE, "failed to join to thread that ran basic "
		    "tests");
	}

	if (!(boolean_t)(uintptr_t)thr_ret) {
		ret = EXIT_FAILURE;
	}

	bval = clock_test_thread_clock();
	(void) printf("TEST %s: comparing CLOCK_THREAD_CPUTIME_ID and "
	    "CLOCK_VIRTUAL between threads\n", bval ? "PASSED" : "FAILED");

	bval = clock_test_thread_sys();
	(void) printf("TEST %s: comparing CLOCK_THREAD_CPUTIME_ID and "
	    "CLOCK_VIRTUAL\n", bval ? "PASSED" : "FAILED");


	bval = clock_test_thread_proc();
	(void) printf("TEST %s: comparing CLOCK_THREAD_CPUTIME_ID and "
	    "CLOCK_PROCESS_CPUTIME_ID\n", bval ? "PASSED" : "FAILED");
	/*
	 * XXX CLOCK_THREAD_CPUTIME_ID > CLOCK_VIRTUAL for same thread?
	 * XXX CLOCK_PROCESS_CPUTIME_ID > CLOCK_THREAD_CPUTIME_ID
	 */

	return (ret);
}