File Index
Files and directories
Sign in
FILE

nanosleep.c

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/timers/nanosleep.c C
  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 <errno.h>
 23#include <stdio.h>
 24#include <stdlib.h>
 25#include <time.h>
 26#include <sys/time.h>
 27#include <sys/timex.h>
 28#include <string.h>
 29#include <signal.h>
 30#include <include/vdso/time64.h>
 31#include "kselftest.h"
 32
 33/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */
 34#define CLOCK_HWSPECIFIC		10
 35
 36#define UNSUPPORTED 0xf00f
 37
 38char *clockstring(int clockid)
 39{
 40	switch (clockid) {
 41	case CLOCK_REALTIME:
 42		return "CLOCK_REALTIME";
 43	case CLOCK_MONOTONIC:
 44		return "CLOCK_MONOTONIC";
 45	case CLOCK_PROCESS_CPUTIME_ID:
 46		return "CLOCK_PROCESS_CPUTIME_ID";
 47	case CLOCK_THREAD_CPUTIME_ID:
 48		return "CLOCK_THREAD_CPUTIME_ID";
 49	case CLOCK_MONOTONIC_RAW:
 50		return "CLOCK_MONOTONIC_RAW";
 51	case CLOCK_REALTIME_COARSE:
 52		return "CLOCK_REALTIME_COARSE";
 53	case CLOCK_MONOTONIC_COARSE:
 54		return "CLOCK_MONOTONIC_COARSE";
 55	case CLOCK_BOOTTIME:
 56		return "CLOCK_BOOTTIME";
 57	case CLOCK_REALTIME_ALARM:
 58		return "CLOCK_REALTIME_ALARM";
 59	case CLOCK_BOOTTIME_ALARM:
 60		return "CLOCK_BOOTTIME_ALARM";
 61	case CLOCK_TAI:
 62		return "CLOCK_TAI";
 63	};
 64	return "UNKNOWN_CLOCKID";
 65}
 66
 67/* returns 1 if a <= b, 0 otherwise */
 68static inline int in_order(struct timespec a, struct timespec b)
 69{
 70	if (a.tv_sec < b.tv_sec)
 71		return 1;
 72	if (a.tv_sec > b.tv_sec)
 73		return 0;
 74	if (a.tv_nsec > b.tv_nsec)
 75		return 0;
 76	return 1;
 77}
 78
 79struct timespec timespec_add(struct timespec ts, unsigned long long ns)
 80{
 81	ts.tv_nsec += ns;
 82	while (ts.tv_nsec >= NSEC_PER_SEC) {
 83		ts.tv_nsec -= NSEC_PER_SEC;
 84		ts.tv_sec++;
 85	}
 86	return ts;
 87}
 88
 89int nanosleep_test(int clockid, long long ns)
 90{
 91	struct timespec now, target, rel;
 92
 93	/* First check abs time */
 94	if (clock_gettime(clockid, &now))
 95		return UNSUPPORTED;
 96	target = timespec_add(now, ns);
 97
 98	if (clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL))
 99		return UNSUPPORTED;
100	clock_gettime(clockid, &now);
101
102	if (!in_order(target, now))
103		return -1;
104
105	/* Second check reltime */
106	clock_gettime(clockid, &now);
107	rel.tv_sec = 0;
108	rel.tv_nsec = 0;
109	rel = timespec_add(rel, ns);
110	target = timespec_add(now, ns);
111	clock_nanosleep(clockid, 0, &rel, NULL);
112	clock_gettime(clockid, &now);
113
114	if (!in_order(target, now))
115		return -1;
116	return 0;
117}
118
119static void dummy_event_handler(int val)
120{
121	/* No action needed */
122}
123
124static int nanosleep_test_remaining(int clockid)
125{
126	struct timespec rqtp = {}, rmtp = {};
127	struct itimerspec itimer = {};
128	struct sigaction sa = {};
129	timer_t timer;
130	int ret;
131
132	sa.sa_handler = dummy_event_handler;
133	ret = sigaction(SIGALRM, &sa, NULL);
134	if (ret)
135		return -1;
136
137	ret = timer_create(clockid, NULL, &timer);
138	if (ret)
139		return -1;
140
141	itimer.it_value.tv_nsec = NSEC_PER_SEC / 4;
142	ret = timer_settime(timer, 0, &itimer, NULL);
143	if (ret)
144		return -1;
145
146	rqtp.tv_nsec = NSEC_PER_SEC / 2;
147	ret = clock_nanosleep(clockid, 0, &rqtp, &rmtp);
148	if (ret != EINTR)
149		return -1;
150
151	ret = timer_delete(timer);
152	if (ret)
153		return -1;
154
155	sa.sa_handler = SIG_DFL;
156	ret = sigaction(SIGALRM, &sa, NULL);
157	if (ret)
158		return -1;
159
160	if (!in_order((struct timespec) {}, rmtp))
161		return -1;
162
163	if (!in_order(rmtp, rqtp))
164		return -1;
165
166	return 0;
167}
168
169int main(int argc, char **argv)
170{
171	long long length;
172	int clockid, ret;
173	int max_clocks = CLOCK_TAI + 1;
174
175	ksft_print_header();
176	ksft_set_plan(max_clocks);
177
178	for (clockid = CLOCK_REALTIME; clockid < max_clocks; clockid++) {
179
180		/* Skip cputime clockids since nanosleep won't increment cputime */
181		if (clockid == CLOCK_PROCESS_CPUTIME_ID ||
182				clockid == CLOCK_THREAD_CPUTIME_ID ||
183				clockid == CLOCK_HWSPECIFIC) {
184			ksft_test_result_skip("%-31s\n", clockstring(clockid));
185			continue;
186		}
187
188		fflush(stdout);
189
190		length = 10;
191		while (length <= (NSEC_PER_SEC * 10)) {
192			ret = nanosleep_test(clockid, length);
193			if (ret == UNSUPPORTED) {
194				ksft_test_result_skip("%-31s\n", clockstring(clockid));
195				goto next;
196			}
197			if (ret < 0) {
198				ksft_test_result_fail("%-31s\n", clockstring(clockid));
199				ksft_exit_fail();
200			}
201			length *= 100;
202		}
203		ret = nanosleep_test_remaining(clockid);
204		if (ret < 0) {
205			ksft_test_result_fail("%-31s\n", clockstring(clockid));
206			ksft_exit_fail();
207		}
208		ksft_test_result_pass("%-31s\n", clockstring(clockid));
209next:
210		ret = 0;
211	}
212	ksft_exit_pass();
213}
~/ Read-only file preview.