1/* Make sure timers don't return early
2 *              by: john stultz (johnstul@us.ibm.com)
3 *		    John Stultz (john.stultz@linaro.org)
4 *              (C) Copyright IBM 2012
5 *              (C) Copyright Linaro 2013 2015
6 *              Licensed under the GPLv2
7 *
8 *  To build:
9 *	$ gcc nanosleep.c -o nanosleep -lrt
10 *
11 *   This program is free software: you can redistribute it and/or modify
12 *   it under the terms of the GNU General Public License as published by
13 *   the Free Software Foundation, either version 2 of the License, or
14 *   (at your option) any later version.
15 *
16 *   This program is distributed in the hope that it will be useful,
17 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 *   GNU General Public License for more details.
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <time.h>
25#include <sys/time.h>
26#include <sys/timex.h>
27#include <string.h>
28#include <signal.h>
29#ifdef KTEST
30#include "../kselftest.h"
31#else
32static inline int ksft_exit_pass(void)
33{
34	exit(0);
35}
36static inline int ksft_exit_fail(void)
37{
38	exit(1);
39}
40#endif
41
42#define NSEC_PER_SEC 1000000000ULL
43
44#define CLOCK_REALTIME			0
45#define CLOCK_MONOTONIC			1
46#define CLOCK_PROCESS_CPUTIME_ID	2
47#define CLOCK_THREAD_CPUTIME_ID		3
48#define CLOCK_MONOTONIC_RAW		4
49#define CLOCK_REALTIME_COARSE		5
50#define CLOCK_MONOTONIC_COARSE		6
51#define CLOCK_BOOTTIME			7
52#define CLOCK_REALTIME_ALARM		8
53#define CLOCK_BOOTTIME_ALARM		9
54#define CLOCK_HWSPECIFIC		10
55#define CLOCK_TAI			11
56#define NR_CLOCKIDS			12
57
58#define UNSUPPORTED 0xf00f
59
60char *clockstring(int clockid)
61{
62	switch (clockid) {
63	case CLOCK_REALTIME:
64		return "CLOCK_REALTIME";
65	case CLOCK_MONOTONIC:
66		return "CLOCK_MONOTONIC";
67	case CLOCK_PROCESS_CPUTIME_ID:
68		return "CLOCK_PROCESS_CPUTIME_ID";
69	case CLOCK_THREAD_CPUTIME_ID:
70		return "CLOCK_THREAD_CPUTIME_ID";
71	case CLOCK_MONOTONIC_RAW:
72		return "CLOCK_MONOTONIC_RAW";
73	case CLOCK_REALTIME_COARSE:
74		return "CLOCK_REALTIME_COARSE";
75	case CLOCK_MONOTONIC_COARSE:
76		return "CLOCK_MONOTONIC_COARSE";
77	case CLOCK_BOOTTIME:
78		return "CLOCK_BOOTTIME";
79	case CLOCK_REALTIME_ALARM:
80		return "CLOCK_REALTIME_ALARM";
81	case CLOCK_BOOTTIME_ALARM:
82		return "CLOCK_BOOTTIME_ALARM";
83	case CLOCK_TAI:
84		return "CLOCK_TAI";
85	};
86	return "UNKNOWN_CLOCKID";
87}
88
89/* returns 1 if a <= b, 0 otherwise */
90static inline int in_order(struct timespec a, struct timespec b)
91{
92	if (a.tv_sec < b.tv_sec)
93		return 1;
94	if (a.tv_sec > b.tv_sec)
95		return 0;
96	if (a.tv_nsec > b.tv_nsec)
97		return 0;
98	return 1;
99}
100
101struct timespec timespec_add(struct timespec ts, unsigned long long ns)
102{
103	ts.tv_nsec += ns;
104	while (ts.tv_nsec >= NSEC_PER_SEC) {
105		ts.tv_nsec -= NSEC_PER_SEC;
106		ts.tv_sec++;
107	}
108	return ts;
109}
110
111int nanosleep_test(int clockid, long long ns)
112{
113	struct timespec now, target, rel;
114
115	/* First check abs time */
116	if (clock_gettime(clockid, &now))
117		return UNSUPPORTED;
118	target = timespec_add(now, ns);
119
120	if (clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL))
121		return UNSUPPORTED;
122	clock_gettime(clockid, &now);
123
124	if (!in_order(target, now))
125		return -1;
126
127	/* Second check reltime */
128	clock_gettime(clockid, &now);
129	rel.tv_sec = 0;
130	rel.tv_nsec = 0;
131	rel = timespec_add(rel, ns);
132	target = timespec_add(now, ns);
133	clock_nanosleep(clockid, 0, &rel, NULL);
134	clock_gettime(clockid, &now);
135
136	if (!in_order(target, now))
137		return -1;
138	return 0;
139}
140
141int main(int argc, char **argv)
142{
143	long long length;
144	int clockid, ret;
145
146	for (clockid = CLOCK_REALTIME; clockid < NR_CLOCKIDS; clockid++) {
147
148		/* Skip cputime clockids since nanosleep won't increment cputime */
149		if (clockid == CLOCK_PROCESS_CPUTIME_ID ||
150				clockid == CLOCK_THREAD_CPUTIME_ID ||
151				clockid == CLOCK_HWSPECIFIC)
152			continue;
153
154		printf("Nanosleep %-31s ", clockstring(clockid));
155
156		length = 10;
157		while (length <= (NSEC_PER_SEC * 10)) {
158			ret = nanosleep_test(clockid, length);
159			if (ret == UNSUPPORTED) {
160				printf("[UNSUPPORTED]\n");
161				goto next;
162			}
163			if (ret < 0) {
164				printf("[FAILED]\n");
165				return ksft_exit_fail();
166			}
167			length *= 100;
168		}
169		printf("[OK]\n");
170next:
171		ret = 0;
172	}
173	return ksft_exit_pass();
174}
175