1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (C) 2002 Andi Kleen 4f08c3bdfSopenharmony_ci * Copyright (C) 2017 Cyril Hrubis <chrubis@suse.cz> 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/* 8f08c3bdfSopenharmony_ci * DESCRIPTION 9f08c3bdfSopenharmony_ci * Check if gettimeofday is monotonous 10f08c3bdfSopenharmony_ci * 11f08c3bdfSopenharmony_ci * ALGORITHM 12f08c3bdfSopenharmony_ci * Call gettimeofday() to get a t1 (fist value) 13f08c3bdfSopenharmony_ci * call it again to get t2, see if t2 < t1, set t2 = t1, repeat for 10 sec 14f08c3bdfSopenharmony_ci */ 15f08c3bdfSopenharmony_ci 16f08c3bdfSopenharmony_ci#include <stdint.h> 17f08c3bdfSopenharmony_ci#include <sys/time.h> 18f08c3bdfSopenharmony_ci#include <stdlib.h> 19f08c3bdfSopenharmony_ci#include <unistd.h> 20f08c3bdfSopenharmony_ci#include <time.h> 21f08c3bdfSopenharmony_ci#include <errno.h> 22f08c3bdfSopenharmony_ci 23f08c3bdfSopenharmony_ci#include "tst_test.h" 24f08c3bdfSopenharmony_ci#include "tst_timer.h" 25f08c3bdfSopenharmony_ci#include "lapi/syscalls.h" 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_cistatic volatile sig_atomic_t done; 28f08c3bdfSopenharmony_ci 29f08c3bdfSopenharmony_cistatic void breakout(int sig) 30f08c3bdfSopenharmony_ci{ 31f08c3bdfSopenharmony_ci done = sig; 32f08c3bdfSopenharmony_ci} 33f08c3bdfSopenharmony_ci 34f08c3bdfSopenharmony_cistatic void verify_gettimeofday(void) 35f08c3bdfSopenharmony_ci{ 36f08c3bdfSopenharmony_ci struct __kernel_old_timeval tv1, tv2; 37f08c3bdfSopenharmony_ci unsigned long long cnt = 0; 38f08c3bdfSopenharmony_ci int rtime = tst_remaining_runtime(); 39f08c3bdfSopenharmony_ci 40f08c3bdfSopenharmony_ci done = 0; 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ci alarm(rtime); 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_ci if (tst_syscall(__NR_gettimeofday, &tv1, NULL)) 45f08c3bdfSopenharmony_ci tst_brk(TFAIL | TERRNO, "gettimeofday() failed"); 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_ci while (!done) { 48f08c3bdfSopenharmony_ci if (tst_syscall(__NR_gettimeofday, &tv2, NULL)) 49f08c3bdfSopenharmony_ci tst_brk(TFAIL | TERRNO, "gettimeofday() failed"); 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_ci if (tv2.tv_sec < tv1.tv_sec || 52f08c3bdfSopenharmony_ci (tv2.tv_sec == tv1.tv_sec && tv2.tv_usec < tv1.tv_usec)) { 53f08c3bdfSopenharmony_ci tst_res(TFAIL, 54f08c3bdfSopenharmony_ci "Time is going backwards: old %jd.%jd vs new %jd.%jd!", 55f08c3bdfSopenharmony_ci (intmax_t) tv1.tv_sec, (intmax_t) tv1.tv_usec, 56f08c3bdfSopenharmony_ci (intmax_t) tv2.tv_sec, (intmax_t) tv2.tv_usec); 57f08c3bdfSopenharmony_ci return; 58f08c3bdfSopenharmony_ci } 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_ci tv1 = tv2; 61f08c3bdfSopenharmony_ci cnt++; 62f08c3bdfSopenharmony_ci } 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci tst_res(TINFO, "gettimeofday() called %llu times", cnt); 65f08c3bdfSopenharmony_ci tst_res(TPASS, "gettimeofday() monotonous in %i seconds", rtime); 66f08c3bdfSopenharmony_ci} 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_cistatic void setup(void) 69f08c3bdfSopenharmony_ci{ 70f08c3bdfSopenharmony_ci SAFE_SIGNAL(SIGALRM, breakout); 71f08c3bdfSopenharmony_ci} 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_cistatic struct tst_test test = { 74f08c3bdfSopenharmony_ci .setup = setup, 75f08c3bdfSopenharmony_ci .max_runtime = 10, 76f08c3bdfSopenharmony_ci .test_all = verify_gettimeofday, 77f08c3bdfSopenharmony_ci}; 78