18c2ecf20Sopenharmony_ci/* Make sure timers don't return early
28c2ecf20Sopenharmony_ci *              by: john stultz (johnstul@us.ibm.com)
38c2ecf20Sopenharmony_ci *		    John Stultz (john.stultz@linaro.org)
48c2ecf20Sopenharmony_ci *              (C) Copyright IBM 2012
58c2ecf20Sopenharmony_ci *              (C) Copyright Linaro 2013 2015
68c2ecf20Sopenharmony_ci *              Licensed under the GPLv2
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci *  To build:
98c2ecf20Sopenharmony_ci *	$ gcc nanosleep.c -o nanosleep -lrt
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *   This program is free software: you can redistribute it and/or modify
128c2ecf20Sopenharmony_ci *   it under the terms of the GNU General Public License as published by
138c2ecf20Sopenharmony_ci *   the Free Software Foundation, either version 2 of the License, or
148c2ecf20Sopenharmony_ci *   (at your option) any later version.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *   This program is distributed in the hope that it will be useful,
178c2ecf20Sopenharmony_ci *   but WITHOUT ANY WARRANTY; without even the implied warranty of
188c2ecf20Sopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
198c2ecf20Sopenharmony_ci *   GNU General Public License for more details.
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <errno.h>
238c2ecf20Sopenharmony_ci#include <stdio.h>
248c2ecf20Sopenharmony_ci#include <stdlib.h>
258c2ecf20Sopenharmony_ci#include <time.h>
268c2ecf20Sopenharmony_ci#include <sys/time.h>
278c2ecf20Sopenharmony_ci#include <sys/timex.h>
288c2ecf20Sopenharmony_ci#include <string.h>
298c2ecf20Sopenharmony_ci#include <signal.h>
308c2ecf20Sopenharmony_ci#include "../kselftest.h"
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define NSEC_PER_SEC 1000000000ULL
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define CLOCK_REALTIME			0
358c2ecf20Sopenharmony_ci#define CLOCK_MONOTONIC			1
368c2ecf20Sopenharmony_ci#define CLOCK_PROCESS_CPUTIME_ID	2
378c2ecf20Sopenharmony_ci#define CLOCK_THREAD_CPUTIME_ID		3
388c2ecf20Sopenharmony_ci#define CLOCK_MONOTONIC_RAW		4
398c2ecf20Sopenharmony_ci#define CLOCK_REALTIME_COARSE		5
408c2ecf20Sopenharmony_ci#define CLOCK_MONOTONIC_COARSE		6
418c2ecf20Sopenharmony_ci#define CLOCK_BOOTTIME			7
428c2ecf20Sopenharmony_ci#define CLOCK_REALTIME_ALARM		8
438c2ecf20Sopenharmony_ci#define CLOCK_BOOTTIME_ALARM		9
448c2ecf20Sopenharmony_ci#define CLOCK_HWSPECIFIC		10
458c2ecf20Sopenharmony_ci#define CLOCK_TAI			11
468c2ecf20Sopenharmony_ci#define NR_CLOCKIDS			12
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define UNSUPPORTED 0xf00f
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cichar *clockstring(int clockid)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	switch (clockid) {
538c2ecf20Sopenharmony_ci	case CLOCK_REALTIME:
548c2ecf20Sopenharmony_ci		return "CLOCK_REALTIME";
558c2ecf20Sopenharmony_ci	case CLOCK_MONOTONIC:
568c2ecf20Sopenharmony_ci		return "CLOCK_MONOTONIC";
578c2ecf20Sopenharmony_ci	case CLOCK_PROCESS_CPUTIME_ID:
588c2ecf20Sopenharmony_ci		return "CLOCK_PROCESS_CPUTIME_ID";
598c2ecf20Sopenharmony_ci	case CLOCK_THREAD_CPUTIME_ID:
608c2ecf20Sopenharmony_ci		return "CLOCK_THREAD_CPUTIME_ID";
618c2ecf20Sopenharmony_ci	case CLOCK_MONOTONIC_RAW:
628c2ecf20Sopenharmony_ci		return "CLOCK_MONOTONIC_RAW";
638c2ecf20Sopenharmony_ci	case CLOCK_REALTIME_COARSE:
648c2ecf20Sopenharmony_ci		return "CLOCK_REALTIME_COARSE";
658c2ecf20Sopenharmony_ci	case CLOCK_MONOTONIC_COARSE:
668c2ecf20Sopenharmony_ci		return "CLOCK_MONOTONIC_COARSE";
678c2ecf20Sopenharmony_ci	case CLOCK_BOOTTIME:
688c2ecf20Sopenharmony_ci		return "CLOCK_BOOTTIME";
698c2ecf20Sopenharmony_ci	case CLOCK_REALTIME_ALARM:
708c2ecf20Sopenharmony_ci		return "CLOCK_REALTIME_ALARM";
718c2ecf20Sopenharmony_ci	case CLOCK_BOOTTIME_ALARM:
728c2ecf20Sopenharmony_ci		return "CLOCK_BOOTTIME_ALARM";
738c2ecf20Sopenharmony_ci	case CLOCK_TAI:
748c2ecf20Sopenharmony_ci		return "CLOCK_TAI";
758c2ecf20Sopenharmony_ci	};
768c2ecf20Sopenharmony_ci	return "UNKNOWN_CLOCKID";
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/* returns 1 if a <= b, 0 otherwise */
808c2ecf20Sopenharmony_cistatic inline int in_order(struct timespec a, struct timespec b)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	if (a.tv_sec < b.tv_sec)
838c2ecf20Sopenharmony_ci		return 1;
848c2ecf20Sopenharmony_ci	if (a.tv_sec > b.tv_sec)
858c2ecf20Sopenharmony_ci		return 0;
868c2ecf20Sopenharmony_ci	if (a.tv_nsec > b.tv_nsec)
878c2ecf20Sopenharmony_ci		return 0;
888c2ecf20Sopenharmony_ci	return 1;
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistruct timespec timespec_add(struct timespec ts, unsigned long long ns)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	ts.tv_nsec += ns;
948c2ecf20Sopenharmony_ci	while (ts.tv_nsec >= NSEC_PER_SEC) {
958c2ecf20Sopenharmony_ci		ts.tv_nsec -= NSEC_PER_SEC;
968c2ecf20Sopenharmony_ci		ts.tv_sec++;
978c2ecf20Sopenharmony_ci	}
988c2ecf20Sopenharmony_ci	return ts;
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciint nanosleep_test(int clockid, long long ns)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	struct timespec now, target, rel;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	/* First check abs time */
1068c2ecf20Sopenharmony_ci	if (clock_gettime(clockid, &now))
1078c2ecf20Sopenharmony_ci		return UNSUPPORTED;
1088c2ecf20Sopenharmony_ci	target = timespec_add(now, ns);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	if (clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL))
1118c2ecf20Sopenharmony_ci		return UNSUPPORTED;
1128c2ecf20Sopenharmony_ci	clock_gettime(clockid, &now);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	if (!in_order(target, now))
1158c2ecf20Sopenharmony_ci		return -1;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	/* Second check reltime */
1188c2ecf20Sopenharmony_ci	clock_gettime(clockid, &now);
1198c2ecf20Sopenharmony_ci	rel.tv_sec = 0;
1208c2ecf20Sopenharmony_ci	rel.tv_nsec = 0;
1218c2ecf20Sopenharmony_ci	rel = timespec_add(rel, ns);
1228c2ecf20Sopenharmony_ci	target = timespec_add(now, ns);
1238c2ecf20Sopenharmony_ci	clock_nanosleep(clockid, 0, &rel, NULL);
1248c2ecf20Sopenharmony_ci	clock_gettime(clockid, &now);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	if (!in_order(target, now))
1278c2ecf20Sopenharmony_ci		return -1;
1288c2ecf20Sopenharmony_ci	return 0;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ciint main(int argc, char **argv)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	long long length;
1348c2ecf20Sopenharmony_ci	int clockid, ret;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	for (clockid = CLOCK_REALTIME; clockid < NR_CLOCKIDS; clockid++) {
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci		/* Skip cputime clockids since nanosleep won't increment cputime */
1398c2ecf20Sopenharmony_ci		if (clockid == CLOCK_PROCESS_CPUTIME_ID ||
1408c2ecf20Sopenharmony_ci				clockid == CLOCK_THREAD_CPUTIME_ID ||
1418c2ecf20Sopenharmony_ci				clockid == CLOCK_HWSPECIFIC)
1428c2ecf20Sopenharmony_ci			continue;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci		printf("Nanosleep %-31s ", clockstring(clockid));
1458c2ecf20Sopenharmony_ci		fflush(stdout);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci		length = 10;
1488c2ecf20Sopenharmony_ci		while (length <= (NSEC_PER_SEC * 10)) {
1498c2ecf20Sopenharmony_ci			ret = nanosleep_test(clockid, length);
1508c2ecf20Sopenharmony_ci			if (ret == UNSUPPORTED) {
1518c2ecf20Sopenharmony_ci				printf("[UNSUPPORTED]\n");
1528c2ecf20Sopenharmony_ci				goto next;
1538c2ecf20Sopenharmony_ci			}
1548c2ecf20Sopenharmony_ci			if (ret < 0) {
1558c2ecf20Sopenharmony_ci				printf("[FAILED]\n");
1568c2ecf20Sopenharmony_ci				return ksft_exit_fail();
1578c2ecf20Sopenharmony_ci			}
1588c2ecf20Sopenharmony_ci			length *= 100;
1598c2ecf20Sopenharmony_ci		}
1608c2ecf20Sopenharmony_ci		printf("[OK]\n");
1618c2ecf20Sopenharmony_cinext:
1628c2ecf20Sopenharmony_ci		ret = 0;
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci	return ksft_exit_pass();
1658c2ecf20Sopenharmony_ci}
166