18c2ecf20Sopenharmony_ci/* Leap second stress test
28c2ecf20Sopenharmony_ci *              by: John Stultz (john.stultz@linaro.org)
38c2ecf20Sopenharmony_ci *              (C) Copyright IBM 2012
48c2ecf20Sopenharmony_ci *              (C) Copyright 2013, 2015 Linaro Limited
58c2ecf20Sopenharmony_ci *              Licensed under the GPLv2
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  This test signals the kernel to insert a leap second
88c2ecf20Sopenharmony_ci *  every day at midnight GMT. This allows for stessing the
98c2ecf20Sopenharmony_ci *  kernel's leap-second behavior, as well as how well applications
108c2ecf20Sopenharmony_ci *  handle the leap-second discontinuity.
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *  Usage: leap-a-day [-s] [-i <num>]
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *  Options:
158c2ecf20Sopenharmony_ci *	-s:	Each iteration, set the date to 10 seconds before midnight GMT.
168c2ecf20Sopenharmony_ci *		This speeds up the number of leapsecond transitions tested,
178c2ecf20Sopenharmony_ci *		but because it calls settimeofday frequently, advancing the
188c2ecf20Sopenharmony_ci *		time by 24 hours every ~16 seconds, it may cause application
198c2ecf20Sopenharmony_ci *		disruption.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci *	-i:	Number of iterations to run (default: infinite)
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci *  Other notes: Disabling NTP prior to running this is advised, as the two
248c2ecf20Sopenharmony_ci *		 may conflict in their commands to the kernel.
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci *  To build:
278c2ecf20Sopenharmony_ci *	$ gcc leap-a-day.c -o leap-a-day -lrt
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci *   This program is free software: you can redistribute it and/or modify
308c2ecf20Sopenharmony_ci *   it under the terms of the GNU General Public License as published by
318c2ecf20Sopenharmony_ci *   the Free Software Foundation, either version 2 of the License, or
328c2ecf20Sopenharmony_ci *   (at your option) any later version.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci *   This program is distributed in the hope that it will be useful,
358c2ecf20Sopenharmony_ci *   but WITHOUT ANY WARRANTY; without even the implied warranty of
368c2ecf20Sopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
378c2ecf20Sopenharmony_ci *   GNU General Public License for more details.
388c2ecf20Sopenharmony_ci */
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#include <stdio.h>
438c2ecf20Sopenharmony_ci#include <stdlib.h>
448c2ecf20Sopenharmony_ci#include <time.h>
458c2ecf20Sopenharmony_ci#include <sys/time.h>
468c2ecf20Sopenharmony_ci#include <sys/timex.h>
478c2ecf20Sopenharmony_ci#include <sys/errno.h>
488c2ecf20Sopenharmony_ci#include <string.h>
498c2ecf20Sopenharmony_ci#include <signal.h>
508c2ecf20Sopenharmony_ci#include <unistd.h>
518c2ecf20Sopenharmony_ci#include "../kselftest.h"
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define NSEC_PER_SEC 1000000000ULL
548c2ecf20Sopenharmony_ci#define CLOCK_TAI 11
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_citime_t next_leap;
578c2ecf20Sopenharmony_ciint error_found;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/* returns 1 if a <= b, 0 otherwise */
608c2ecf20Sopenharmony_cistatic inline int in_order(struct timespec a, struct timespec b)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	if (a.tv_sec < b.tv_sec)
638c2ecf20Sopenharmony_ci		return 1;
648c2ecf20Sopenharmony_ci	if (a.tv_sec > b.tv_sec)
658c2ecf20Sopenharmony_ci		return 0;
668c2ecf20Sopenharmony_ci	if (a.tv_nsec > b.tv_nsec)
678c2ecf20Sopenharmony_ci		return 0;
688c2ecf20Sopenharmony_ci	return 1;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistruct timespec timespec_add(struct timespec ts, unsigned long long ns)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	ts.tv_nsec += ns;
748c2ecf20Sopenharmony_ci	while (ts.tv_nsec >= NSEC_PER_SEC) {
758c2ecf20Sopenharmony_ci		ts.tv_nsec -= NSEC_PER_SEC;
768c2ecf20Sopenharmony_ci		ts.tv_sec++;
778c2ecf20Sopenharmony_ci	}
788c2ecf20Sopenharmony_ci	return ts;
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cichar *time_state_str(int state)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	switch (state) {
848c2ecf20Sopenharmony_ci	case TIME_OK:	return "TIME_OK";
858c2ecf20Sopenharmony_ci	case TIME_INS:	return "TIME_INS";
868c2ecf20Sopenharmony_ci	case TIME_DEL:	return "TIME_DEL";
878c2ecf20Sopenharmony_ci	case TIME_OOP:	return "TIME_OOP";
888c2ecf20Sopenharmony_ci	case TIME_WAIT:	return "TIME_WAIT";
898c2ecf20Sopenharmony_ci	case TIME_BAD:	return "TIME_BAD";
908c2ecf20Sopenharmony_ci	}
918c2ecf20Sopenharmony_ci	return "ERROR";
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/* clear NTP time_status & time_state */
958c2ecf20Sopenharmony_ciint clear_time_state(void)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	struct timex tx;
988c2ecf20Sopenharmony_ci	int ret;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	/*
1018c2ecf20Sopenharmony_ci	 * We have to call adjtime twice here, as kernels
1028c2ecf20Sopenharmony_ci	 * prior to 6b1859dba01c7 (included in 3.5 and
1038c2ecf20Sopenharmony_ci	 * -stable), had an issue with the state machine
1048c2ecf20Sopenharmony_ci	 * and wouldn't clear the STA_INS/DEL flag directly.
1058c2ecf20Sopenharmony_ci	 */
1068c2ecf20Sopenharmony_ci	tx.modes = ADJ_STATUS;
1078c2ecf20Sopenharmony_ci	tx.status = STA_PLL;
1088c2ecf20Sopenharmony_ci	ret = adjtimex(&tx);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/* Clear maxerror, as it can cause UNSYNC to be set */
1118c2ecf20Sopenharmony_ci	tx.modes = ADJ_MAXERROR;
1128c2ecf20Sopenharmony_ci	tx.maxerror = 0;
1138c2ecf20Sopenharmony_ci	ret = adjtimex(&tx);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	/* Clear the status */
1168c2ecf20Sopenharmony_ci	tx.modes = ADJ_STATUS;
1178c2ecf20Sopenharmony_ci	tx.status = 0;
1188c2ecf20Sopenharmony_ci	ret = adjtimex(&tx);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	return ret;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* Make sure we cleanup on ctrl-c */
1248c2ecf20Sopenharmony_civoid handler(int unused)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	clear_time_state();
1278c2ecf20Sopenharmony_ci	exit(0);
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_civoid sigalarm(int signo)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	struct timex tx;
1338c2ecf20Sopenharmony_ci	int ret;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	tx.modes = 0;
1368c2ecf20Sopenharmony_ci	ret = adjtimex(&tx);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	if (tx.time.tv_sec < next_leap) {
1398c2ecf20Sopenharmony_ci		printf("Error: Early timer expiration! (Should be %ld)\n", next_leap);
1408c2ecf20Sopenharmony_ci		error_found = 1;
1418c2ecf20Sopenharmony_ci		printf("adjtimex: %10ld sec + %6ld us (%i)\t%s\n",
1428c2ecf20Sopenharmony_ci					tx.time.tv_sec,
1438c2ecf20Sopenharmony_ci					tx.time.tv_usec,
1448c2ecf20Sopenharmony_ci					tx.tai,
1458c2ecf20Sopenharmony_ci					time_state_str(ret));
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci	if (ret != TIME_WAIT) {
1488c2ecf20Sopenharmony_ci		printf("Error: Timer seeing incorrect NTP state? (Should be TIME_WAIT)\n");
1498c2ecf20Sopenharmony_ci		error_found = 1;
1508c2ecf20Sopenharmony_ci		printf("adjtimex: %10ld sec + %6ld us (%i)\t%s\n",
1518c2ecf20Sopenharmony_ci					tx.time.tv_sec,
1528c2ecf20Sopenharmony_ci					tx.time.tv_usec,
1538c2ecf20Sopenharmony_ci					tx.tai,
1548c2ecf20Sopenharmony_ci					time_state_str(ret));
1558c2ecf20Sopenharmony_ci	}
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/* Test for known hrtimer failure */
1608c2ecf20Sopenharmony_civoid test_hrtimer_failure(void)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	struct timespec now, target;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	clock_gettime(CLOCK_REALTIME, &now);
1658c2ecf20Sopenharmony_ci	target = timespec_add(now, NSEC_PER_SEC/2);
1668c2ecf20Sopenharmony_ci	clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &target, NULL);
1678c2ecf20Sopenharmony_ci	clock_gettime(CLOCK_REALTIME, &now);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	if (!in_order(target, now)) {
1708c2ecf20Sopenharmony_ci		printf("ERROR: hrtimer early expiration failure observed.\n");
1718c2ecf20Sopenharmony_ci		error_found = 1;
1728c2ecf20Sopenharmony_ci	}
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ciint main(int argc, char **argv)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	timer_t tm1;
1788c2ecf20Sopenharmony_ci	struct itimerspec its1;
1798c2ecf20Sopenharmony_ci	struct sigevent se;
1808c2ecf20Sopenharmony_ci	struct sigaction act;
1818c2ecf20Sopenharmony_ci	int signum = SIGRTMAX;
1828c2ecf20Sopenharmony_ci	int settime = 1;
1838c2ecf20Sopenharmony_ci	int tai_time = 0;
1848c2ecf20Sopenharmony_ci	int insert = 1;
1858c2ecf20Sopenharmony_ci	int iterations = 10;
1868c2ecf20Sopenharmony_ci	int opt;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	/* Process arguments */
1898c2ecf20Sopenharmony_ci	while ((opt = getopt(argc, argv, "sti:")) != -1) {
1908c2ecf20Sopenharmony_ci		switch (opt) {
1918c2ecf20Sopenharmony_ci		case 'w':
1928c2ecf20Sopenharmony_ci			printf("Only setting leap-flag, not changing time. It could take up to a day for leap to trigger.\n");
1938c2ecf20Sopenharmony_ci			settime = 0;
1948c2ecf20Sopenharmony_ci			break;
1958c2ecf20Sopenharmony_ci		case 'i':
1968c2ecf20Sopenharmony_ci			iterations = atoi(optarg);
1978c2ecf20Sopenharmony_ci			break;
1988c2ecf20Sopenharmony_ci		case 't':
1998c2ecf20Sopenharmony_ci			tai_time = 1;
2008c2ecf20Sopenharmony_ci			break;
2018c2ecf20Sopenharmony_ci		default:
2028c2ecf20Sopenharmony_ci			printf("Usage: %s [-w] [-i <iterations>]\n", argv[0]);
2038c2ecf20Sopenharmony_ci			printf("	-w: Set flag and wait for leap second each iteration");
2048c2ecf20Sopenharmony_ci			printf("	    (default sets time to right before leapsecond)\n");
2058c2ecf20Sopenharmony_ci			printf("	-i: Number of iterations (-1 = infinite, default is 10)\n");
2068c2ecf20Sopenharmony_ci			printf("	-t: Print TAI time\n");
2078c2ecf20Sopenharmony_ci			exit(-1);
2088c2ecf20Sopenharmony_ci		}
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	/* Make sure TAI support is present if -t was used */
2128c2ecf20Sopenharmony_ci	if (tai_time) {
2138c2ecf20Sopenharmony_ci		struct timespec ts;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci		if (clock_gettime(CLOCK_TAI, &ts)) {
2168c2ecf20Sopenharmony_ci			printf("System doesn't support CLOCK_TAI\n");
2178c2ecf20Sopenharmony_ci			ksft_exit_fail();
2188c2ecf20Sopenharmony_ci		}
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	signal(SIGINT, handler);
2228c2ecf20Sopenharmony_ci	signal(SIGKILL, handler);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	/* Set up timer signal handler: */
2258c2ecf20Sopenharmony_ci	sigfillset(&act.sa_mask);
2268c2ecf20Sopenharmony_ci	act.sa_flags = 0;
2278c2ecf20Sopenharmony_ci	act.sa_handler = sigalarm;
2288c2ecf20Sopenharmony_ci	sigaction(signum, &act, NULL);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	if (iterations < 0)
2318c2ecf20Sopenharmony_ci		printf("This runs continuously. Press ctrl-c to stop\n");
2328c2ecf20Sopenharmony_ci	else
2338c2ecf20Sopenharmony_ci		printf("Running for %i iterations. Press ctrl-c to stop\n", iterations);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	printf("\n");
2368c2ecf20Sopenharmony_ci	while (1) {
2378c2ecf20Sopenharmony_ci		int ret;
2388c2ecf20Sopenharmony_ci		struct timespec ts;
2398c2ecf20Sopenharmony_ci		struct timex tx;
2408c2ecf20Sopenharmony_ci		time_t now;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci		/* Get the current time */
2438c2ecf20Sopenharmony_ci		clock_gettime(CLOCK_REALTIME, &ts);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci		/* Calculate the next possible leap second 23:59:60 GMT */
2468c2ecf20Sopenharmony_ci		next_leap = ts.tv_sec;
2478c2ecf20Sopenharmony_ci		next_leap += 86400 - (next_leap % 86400);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci		if (settime) {
2508c2ecf20Sopenharmony_ci			struct timeval tv;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci			tv.tv_sec = next_leap - 10;
2538c2ecf20Sopenharmony_ci			tv.tv_usec = 0;
2548c2ecf20Sopenharmony_ci			settimeofday(&tv, NULL);
2558c2ecf20Sopenharmony_ci			printf("Setting time to %s", ctime(&tv.tv_sec));
2568c2ecf20Sopenharmony_ci		}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci		/* Reset NTP time state */
2598c2ecf20Sopenharmony_ci		clear_time_state();
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci		/* Set the leap second insert flag */
2628c2ecf20Sopenharmony_ci		tx.modes = ADJ_STATUS;
2638c2ecf20Sopenharmony_ci		if (insert)
2648c2ecf20Sopenharmony_ci			tx.status = STA_INS;
2658c2ecf20Sopenharmony_ci		else
2668c2ecf20Sopenharmony_ci			tx.status = STA_DEL;
2678c2ecf20Sopenharmony_ci		ret = adjtimex(&tx);
2688c2ecf20Sopenharmony_ci		if (ret < 0) {
2698c2ecf20Sopenharmony_ci			printf("Error: Problem setting STA_INS/STA_DEL!: %s\n",
2708c2ecf20Sopenharmony_ci							time_state_str(ret));
2718c2ecf20Sopenharmony_ci			return ksft_exit_fail();
2728c2ecf20Sopenharmony_ci		}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci		/* Validate STA_INS was set */
2758c2ecf20Sopenharmony_ci		tx.modes = 0;
2768c2ecf20Sopenharmony_ci		ret = adjtimex(&tx);
2778c2ecf20Sopenharmony_ci		if (tx.status != STA_INS && tx.status != STA_DEL) {
2788c2ecf20Sopenharmony_ci			printf("Error: STA_INS/STA_DEL not set!: %s\n",
2798c2ecf20Sopenharmony_ci							time_state_str(ret));
2808c2ecf20Sopenharmony_ci			return ksft_exit_fail();
2818c2ecf20Sopenharmony_ci		}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci		if (tai_time) {
2848c2ecf20Sopenharmony_ci			printf("Using TAI time,"
2858c2ecf20Sopenharmony_ci				" no inconsistencies should be seen!\n");
2868c2ecf20Sopenharmony_ci		}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci		printf("Scheduling leap second for %s", ctime(&next_leap));
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci		/* Set up timer */
2918c2ecf20Sopenharmony_ci		printf("Setting timer for %ld -  %s", next_leap, ctime(&next_leap));
2928c2ecf20Sopenharmony_ci		memset(&se, 0, sizeof(se));
2938c2ecf20Sopenharmony_ci		se.sigev_notify = SIGEV_SIGNAL;
2948c2ecf20Sopenharmony_ci		se.sigev_signo = signum;
2958c2ecf20Sopenharmony_ci		se.sigev_value.sival_int = 0;
2968c2ecf20Sopenharmony_ci		if (timer_create(CLOCK_REALTIME, &se, &tm1) == -1) {
2978c2ecf20Sopenharmony_ci			printf("Error: timer_create failed\n");
2988c2ecf20Sopenharmony_ci			return ksft_exit_fail();
2998c2ecf20Sopenharmony_ci		}
3008c2ecf20Sopenharmony_ci		its1.it_value.tv_sec = next_leap;
3018c2ecf20Sopenharmony_ci		its1.it_value.tv_nsec = 0;
3028c2ecf20Sopenharmony_ci		its1.it_interval.tv_sec = 0;
3038c2ecf20Sopenharmony_ci		its1.it_interval.tv_nsec = 0;
3048c2ecf20Sopenharmony_ci		timer_settime(tm1, TIMER_ABSTIME, &its1, NULL);
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci		/* Wake up 3 seconds before leap */
3078c2ecf20Sopenharmony_ci		ts.tv_sec = next_leap - 3;
3088c2ecf20Sopenharmony_ci		ts.tv_nsec = 0;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci		while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &ts, NULL))
3128c2ecf20Sopenharmony_ci			printf("Something woke us up, returning to sleep\n");
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci		/* Validate STA_INS is still set */
3158c2ecf20Sopenharmony_ci		tx.modes = 0;
3168c2ecf20Sopenharmony_ci		ret = adjtimex(&tx);
3178c2ecf20Sopenharmony_ci		if (tx.status != STA_INS && tx.status != STA_DEL) {
3188c2ecf20Sopenharmony_ci			printf("Something cleared STA_INS/STA_DEL, setting it again.\n");
3198c2ecf20Sopenharmony_ci			tx.modes = ADJ_STATUS;
3208c2ecf20Sopenharmony_ci			if (insert)
3218c2ecf20Sopenharmony_ci				tx.status = STA_INS;
3228c2ecf20Sopenharmony_ci			else
3238c2ecf20Sopenharmony_ci				tx.status = STA_DEL;
3248c2ecf20Sopenharmony_ci			ret = adjtimex(&tx);
3258c2ecf20Sopenharmony_ci		}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci		/* Check adjtimex output every half second */
3288c2ecf20Sopenharmony_ci		now = tx.time.tv_sec;
3298c2ecf20Sopenharmony_ci		while (now < next_leap + 2) {
3308c2ecf20Sopenharmony_ci			char buf[26];
3318c2ecf20Sopenharmony_ci			struct timespec tai;
3328c2ecf20Sopenharmony_ci			int ret;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci			tx.modes = 0;
3358c2ecf20Sopenharmony_ci			ret = adjtimex(&tx);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci			if (tai_time) {
3388c2ecf20Sopenharmony_ci				clock_gettime(CLOCK_TAI, &tai);
3398c2ecf20Sopenharmony_ci				printf("%ld sec, %9ld ns\t%s\n",
3408c2ecf20Sopenharmony_ci						tai.tv_sec,
3418c2ecf20Sopenharmony_ci						tai.tv_nsec,
3428c2ecf20Sopenharmony_ci						time_state_str(ret));
3438c2ecf20Sopenharmony_ci			} else {
3448c2ecf20Sopenharmony_ci				ctime_r(&tx.time.tv_sec, buf);
3458c2ecf20Sopenharmony_ci				buf[strlen(buf)-1] = 0; /*remove trailing\n */
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci				printf("%s + %6ld us (%i)\t%s\n",
3488c2ecf20Sopenharmony_ci						buf,
3498c2ecf20Sopenharmony_ci						tx.time.tv_usec,
3508c2ecf20Sopenharmony_ci						tx.tai,
3518c2ecf20Sopenharmony_ci						time_state_str(ret));
3528c2ecf20Sopenharmony_ci			}
3538c2ecf20Sopenharmony_ci			now = tx.time.tv_sec;
3548c2ecf20Sopenharmony_ci			/* Sleep for another half second */
3558c2ecf20Sopenharmony_ci			ts.tv_sec = 0;
3568c2ecf20Sopenharmony_ci			ts.tv_nsec = NSEC_PER_SEC / 2;
3578c2ecf20Sopenharmony_ci			clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
3588c2ecf20Sopenharmony_ci		}
3598c2ecf20Sopenharmony_ci		/* Switch to using other mode */
3608c2ecf20Sopenharmony_ci		insert = !insert;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci		/* Note if kernel has known hrtimer failure */
3638c2ecf20Sopenharmony_ci		test_hrtimer_failure();
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci		printf("Leap complete\n");
3668c2ecf20Sopenharmony_ci		if (error_found) {
3678c2ecf20Sopenharmony_ci			printf("Errors observed\n");
3688c2ecf20Sopenharmony_ci			clear_time_state();
3698c2ecf20Sopenharmony_ci			return ksft_exit_fail();
3708c2ecf20Sopenharmony_ci		}
3718c2ecf20Sopenharmony_ci		printf("\n");
3728c2ecf20Sopenharmony_ci		if ((iterations != -1) && !(--iterations))
3738c2ecf20Sopenharmony_ci			break;
3748c2ecf20Sopenharmony_ci	}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	clear_time_state();
3778c2ecf20Sopenharmony_ci	return ksft_exit_pass();
3788c2ecf20Sopenharmony_ci}
379