18c2ecf20Sopenharmony_ci/* Time inconsistency check test 28c2ecf20Sopenharmony_ci * by: john stultz (johnstul@us.ibm.com) 38c2ecf20Sopenharmony_ci * (C) Copyright IBM 2003, 2004, 2005, 2012 48c2ecf20Sopenharmony_ci * (C) Copyright Linaro Limited 2015 58c2ecf20Sopenharmony_ci * Licensed under the GPLv2 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * To build: 88c2ecf20Sopenharmony_ci * $ gcc inconsistency-check.c -o inconsistency-check -lrt 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * This program is free software: you can redistribute it and/or modify 118c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License as published by 128c2ecf20Sopenharmony_ci * the Free Software Foundation, either version 2 of the License, or 138c2ecf20Sopenharmony_ci * (at your option) any later version. 148c2ecf20Sopenharmony_ci * 158c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, 168c2ecf20Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 178c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 188c2ecf20Sopenharmony_ci * GNU General Public License for more details. 198c2ecf20Sopenharmony_ci */ 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#include <stdio.h> 248c2ecf20Sopenharmony_ci#include <unistd.h> 258c2ecf20Sopenharmony_ci#include <stdlib.h> 268c2ecf20Sopenharmony_ci#include <time.h> 278c2ecf20Sopenharmony_ci#include <sys/time.h> 288c2ecf20Sopenharmony_ci#include <sys/timex.h> 298c2ecf20Sopenharmony_ci#include <string.h> 308c2ecf20Sopenharmony_ci#include <signal.h> 318c2ecf20Sopenharmony_ci#include "../kselftest.h" 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define CALLS_PER_LOOP 64 348c2ecf20Sopenharmony_ci#define NSEC_PER_SEC 1000000000ULL 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci#define CLOCK_REALTIME 0 378c2ecf20Sopenharmony_ci#define CLOCK_MONOTONIC 1 388c2ecf20Sopenharmony_ci#define CLOCK_PROCESS_CPUTIME_ID 2 398c2ecf20Sopenharmony_ci#define CLOCK_THREAD_CPUTIME_ID 3 408c2ecf20Sopenharmony_ci#define CLOCK_MONOTONIC_RAW 4 418c2ecf20Sopenharmony_ci#define CLOCK_REALTIME_COARSE 5 428c2ecf20Sopenharmony_ci#define CLOCK_MONOTONIC_COARSE 6 438c2ecf20Sopenharmony_ci#define CLOCK_BOOTTIME 7 448c2ecf20Sopenharmony_ci#define CLOCK_REALTIME_ALARM 8 458c2ecf20Sopenharmony_ci#define CLOCK_BOOTTIME_ALARM 9 468c2ecf20Sopenharmony_ci#define CLOCK_HWSPECIFIC 10 478c2ecf20Sopenharmony_ci#define CLOCK_TAI 11 488c2ecf20Sopenharmony_ci#define NR_CLOCKIDS 12 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 /* use unsigned to avoid false positives on 2038 rollover */ 838c2ecf20Sopenharmony_ci if ((unsigned long)a.tv_sec < (unsigned long)b.tv_sec) 848c2ecf20Sopenharmony_ci return 1; 858c2ecf20Sopenharmony_ci if ((unsigned long)a.tv_sec > (unsigned long)b.tv_sec) 868c2ecf20Sopenharmony_ci return 0; 878c2ecf20Sopenharmony_ci if (a.tv_nsec > b.tv_nsec) 888c2ecf20Sopenharmony_ci return 0; 898c2ecf20Sopenharmony_ci return 1; 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ciint consistency_test(int clock_type, unsigned long seconds) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci struct timespec list[CALLS_PER_LOOP]; 978c2ecf20Sopenharmony_ci int i, inconsistent; 988c2ecf20Sopenharmony_ci long now, then; 998c2ecf20Sopenharmony_ci time_t t; 1008c2ecf20Sopenharmony_ci char *start_str; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci clock_gettime(clock_type, &list[0]); 1038c2ecf20Sopenharmony_ci now = then = list[0].tv_sec; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci /* timestamp start of test */ 1068c2ecf20Sopenharmony_ci t = time(0); 1078c2ecf20Sopenharmony_ci start_str = ctime(&t); 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci while (seconds == -1 || now - then < seconds) { 1108c2ecf20Sopenharmony_ci inconsistent = -1; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci /* Fill list */ 1138c2ecf20Sopenharmony_ci for (i = 0; i < CALLS_PER_LOOP; i++) 1148c2ecf20Sopenharmony_ci clock_gettime(clock_type, &list[i]); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci /* Check for inconsistencies */ 1178c2ecf20Sopenharmony_ci for (i = 0; i < CALLS_PER_LOOP - 1; i++) 1188c2ecf20Sopenharmony_ci if (!in_order(list[i], list[i+1])) 1198c2ecf20Sopenharmony_ci inconsistent = i; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci /* display inconsistency */ 1228c2ecf20Sopenharmony_ci if (inconsistent >= 0) { 1238c2ecf20Sopenharmony_ci unsigned long long delta; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci printf("\%s\n", start_str); 1268c2ecf20Sopenharmony_ci for (i = 0; i < CALLS_PER_LOOP; i++) { 1278c2ecf20Sopenharmony_ci if (i == inconsistent) 1288c2ecf20Sopenharmony_ci printf("--------------------\n"); 1298c2ecf20Sopenharmony_ci printf("%lu:%lu\n", list[i].tv_sec, 1308c2ecf20Sopenharmony_ci list[i].tv_nsec); 1318c2ecf20Sopenharmony_ci if (i == inconsistent + 1) 1328c2ecf20Sopenharmony_ci printf("--------------------\n"); 1338c2ecf20Sopenharmony_ci } 1348c2ecf20Sopenharmony_ci delta = list[inconsistent].tv_sec * NSEC_PER_SEC; 1358c2ecf20Sopenharmony_ci delta += list[inconsistent].tv_nsec; 1368c2ecf20Sopenharmony_ci delta -= list[inconsistent+1].tv_sec * NSEC_PER_SEC; 1378c2ecf20Sopenharmony_ci delta -= list[inconsistent+1].tv_nsec; 1388c2ecf20Sopenharmony_ci printf("Delta: %llu ns\n", delta); 1398c2ecf20Sopenharmony_ci fflush(0); 1408c2ecf20Sopenharmony_ci /* timestamp inconsistency*/ 1418c2ecf20Sopenharmony_ci t = time(0); 1428c2ecf20Sopenharmony_ci printf("%s\n", ctime(&t)); 1438c2ecf20Sopenharmony_ci printf("[FAILED]\n"); 1448c2ecf20Sopenharmony_ci return -1; 1458c2ecf20Sopenharmony_ci } 1468c2ecf20Sopenharmony_ci now = list[0].tv_sec; 1478c2ecf20Sopenharmony_ci } 1488c2ecf20Sopenharmony_ci printf("[OK]\n"); 1498c2ecf20Sopenharmony_ci return 0; 1508c2ecf20Sopenharmony_ci} 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ciint main(int argc, char *argv[]) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci int clockid, opt; 1568c2ecf20Sopenharmony_ci int userclock = CLOCK_REALTIME; 1578c2ecf20Sopenharmony_ci int maxclocks = NR_CLOCKIDS; 1588c2ecf20Sopenharmony_ci int runtime = 10; 1598c2ecf20Sopenharmony_ci struct timespec ts; 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci /* Process arguments */ 1628c2ecf20Sopenharmony_ci while ((opt = getopt(argc, argv, "t:c:")) != -1) { 1638c2ecf20Sopenharmony_ci switch (opt) { 1648c2ecf20Sopenharmony_ci case 't': 1658c2ecf20Sopenharmony_ci runtime = atoi(optarg); 1668c2ecf20Sopenharmony_ci break; 1678c2ecf20Sopenharmony_ci case 'c': 1688c2ecf20Sopenharmony_ci userclock = atoi(optarg); 1698c2ecf20Sopenharmony_ci maxclocks = userclock + 1; 1708c2ecf20Sopenharmony_ci break; 1718c2ecf20Sopenharmony_ci default: 1728c2ecf20Sopenharmony_ci printf("Usage: %s [-t <secs>] [-c <clockid>]\n", argv[0]); 1738c2ecf20Sopenharmony_ci printf(" -t: Number of seconds to run\n"); 1748c2ecf20Sopenharmony_ci printf(" -c: clockid to use (default, all clockids)\n"); 1758c2ecf20Sopenharmony_ci exit(-1); 1768c2ecf20Sopenharmony_ci } 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci setbuf(stdout, NULL); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci for (clockid = userclock; clockid < maxclocks; clockid++) { 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci if (clockid == CLOCK_HWSPECIFIC) 1848c2ecf20Sopenharmony_ci continue; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci if (!clock_gettime(clockid, &ts)) { 1878c2ecf20Sopenharmony_ci printf("Consistent %-30s ", clockstring(clockid)); 1888c2ecf20Sopenharmony_ci if (consistency_test(clockid, runtime)) 1898c2ecf20Sopenharmony_ci return ksft_exit_fail(); 1908c2ecf20Sopenharmony_ci } 1918c2ecf20Sopenharmony_ci } 1928c2ecf20Sopenharmony_ci return ksft_exit_pass(); 1938c2ecf20Sopenharmony_ci} 194