summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/timers/nanosleep.c
blob: b45e4c85525997c121322dd59bad28c63a19475f (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
/* Make sure timers don't return early
 *              by: john stultz (johnstul@us.ibm.com)
 *		    John Stultz (john.stultz@linaro.org)
 *              (C) Copyright IBM 2012
 *              (C) Copyright Linaro 2013 2015
 *              Licensed under the GPLv2
 *
 *  To build:
 *	$ gcc nanosleep.c -o nanosleep -lrt
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/timex.h>
#include <string.h>
#include <signal.h>
#include "clock-helpers.h"
#include "kselftest.h"

/* returns 1 if a <= b, 0 otherwise */
static inline int in_order(struct timespec a, struct timespec b)
{
	if (a.tv_sec < b.tv_sec)
		return 1;
	if (a.tv_sec > b.tv_sec)
		return 0;
	if (a.tv_nsec > b.tv_nsec)
		return 0;
	return 1;
}

struct timespec timespec_add(struct timespec ts, unsigned long long ns)
{
	ts.tv_nsec += ns;
	while (ts.tv_nsec >= NSEC_PER_SEC) {
		ts.tv_nsec -= NSEC_PER_SEC;
		ts.tv_sec++;
	}
	return ts;
}

int nanosleep_test(int clockid, long long ns)
{
	struct timespec now, target, rel;

	/* First check abs time */
	if (clock_gettime(clockid, &now))
		return KSFT_SKIP;
	target = timespec_add(now, ns);

	if (clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL))
		return KSFT_SKIP;
	clock_gettime(clockid, &now);

	if (!in_order(target, now))
		return KSFT_FAIL;

	/* Second check reltime */
	clock_gettime(clockid, &now);
	rel.tv_sec = 0;
	rel.tv_nsec = 0;
	rel = timespec_add(rel, ns);
	target = timespec_add(now, ns);
	clock_nanosleep(clockid, 0, &rel, NULL);
	clock_gettime(clockid, &now);

	if (!in_order(target, now))
		return KSFT_FAIL;
	return KSFT_PASS;
}

static void dummy_event_handler(int val)
{
	/* No action needed */
}

static int nanosleep_test_remaining(int clockid)
{
	struct timespec rqtp = {}, rmtp = {};
	struct itimerspec itimer = {};
	struct sigaction sa = {};
	timer_t timer;
	int ret;

	sa.sa_handler = dummy_event_handler;
	ret = sigaction(SIGALRM, &sa, NULL);
	if (ret)
		return KSFT_FAIL;

	ret = timer_create(clockid, NULL, &timer);
	if (ret)
		return KSFT_FAIL;

	itimer.it_value.tv_nsec = NSEC_PER_SEC / 4;
	ret = timer_settime(timer, 0, &itimer, NULL);
	if (ret)
		return KSFT_FAIL;

	rqtp.tv_nsec = NSEC_PER_SEC / 2;
	ret = clock_nanosleep(clockid, 0, &rqtp, &rmtp);

	if (timer_delete(timer)) {
		ksft_exit_fail_msg("Unable to delete the timeout timer for %s. "
				   "This might interfere with following testcases.\n",
				   clock_name(clockid));
	}

	if (ret != EINTR)
		return KSFT_FAIL;

	sa.sa_handler = SIG_DFL;
	ret = sigaction(SIGALRM, &sa, NULL);
	if (ret)
		return KSFT_FAIL;

	if (!in_order((struct timespec) {}, rmtp))
		return KSFT_FAIL;

	if (!in_order(rmtp, rqtp))
		return KSFT_FAIL;

	return KSFT_PASS;
}

static void nanosleep_test_clock(clockid_t clockid)
{
	long long length = 10;
	int ret;

	while (length <= (NSEC_PER_SEC * 10)) {
		ret = nanosleep_test(clockid, length);
		if (ret != KSFT_PASS) {
			ksft_test_result_report(ret, "%s\n", clock_name(clockid));
			ksft_test_result_skip("%s (remaining)\n", clock_name(clockid));
			return;
		}

		length *= 100;
	}
	ksft_test_result_pass("%s\n", clock_name(clockid));

	ret = nanosleep_test_remaining(clockid);
	ksft_test_result_report(ret, "%s (remaining)\n", clock_name(clockid));
}

int main(int argc, char **argv)
{
	int clockid;

	static const clockid_t tested_clocks[] = {
		CLOCK_REALTIME,
		CLOCK_MONOTONIC,
		CLOCK_BOOTTIME,
		CLOCK_BOOTTIME_ALARM,
		CLOCK_REALTIME_ALARM,
		CLOCK_TAI,
	};

	ksft_print_header();
	ksft_set_plan(ARRAY_SIZE(tested_clocks) * 2);

	for (size_t clock_index = 0; clock_index < ARRAY_SIZE(tested_clocks); clock_index++) {
		clockid = tested_clocks[clock_index];

		fflush(stdout);

		nanosleep_test_clock(clockid);
	}
	ksft_finished();
}