• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Time inconsistency check test
2  *		by: john stultz (johnstul@us.ibm.com)
3  *		(C) Copyright IBM 2003, 2004, 2005
4  */
5 
6 /*
7  * Copyright (C) 2003-2006 IBM
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  * 02111-1307, USA.
23  */
24 
25 #include <stdio.h>
26 #include <time.h>
27 #include <sys/time.h>
28 
29 #define CALLS_PER_LOOP 64
30 #define NSEC_PER_SEC 1000000000ULL
31 
32 /* returns 1 if a <= b, 0 otherwise */
in_order(struct timespec a,struct timespec b)33 static inline int in_order(struct timespec a, struct timespec b)
34 {
35 	if (a.tv_sec < b.tv_sec)
36 		return 1;
37 	if (a.tv_sec > b.tv_sec)
38 		return 0;
39 	if (a.tv_nsec > b.tv_nsec)
40 		return 0;
41 	return 1;
42 }
43 
main(int argc,char * argv[])44 int main(int argc, char *argv[])
45 {
46 	struct timespec list[CALLS_PER_LOOP];
47 	int i, inconsistent;
48 	unsigned long seconds = -1;
49 	long now, then;
50 	int clock_type = CLOCK_MONOTONIC;
51 
52 	if (argc > 1)
53 		seconds = atol(argv[1]);
54 
55 	/* make sure CLOCK_MONOTONIC is supported */
56 	if (clock_gettime(clock_type, &list[0])) {
57 		printf("Using CLOCK_REALTIME\n");
58 		clock_type = CLOCK_REALTIME;
59 	}
60 
61 	clock_gettime(clock_type, &list[0]);
62 	now = then = list[0].tv_sec;
63 
64 	/* timestamp start of test */
65 	system("date");
66 	while (seconds == -1 || now - then < seconds) {
67 		inconsistent = 0;
68 
69 		/* Fill list */
70 		for (i = 0; i < CALLS_PER_LOOP; i++)
71 			clock_gettime(clock_type, &list[i]);
72 
73 		/* Check for inconsistencies */
74 		for (i = 0; i < CALLS_PER_LOOP - 1; i++)
75 			if (!in_order(list[i], list[i + 1]))
76 				inconsistent = i;
77 
78 		/* display inconsistency */
79 		if (inconsistent) {
80 			unsigned long long delta;
81 			for (i = 0; i < CALLS_PER_LOOP; i++) {
82 				if (i == inconsistent)
83 					printf("--------------------\n");
84 				printf("%lu:%lu\n", list[i].tv_sec,
85 				       list[i].tv_nsec);
86 				if (i == inconsistent + 1)
87 					printf("--------------------\n");
88 			}
89 			delta = list[inconsistent].tv_sec * NSEC_PER_SEC;
90 			delta += list[inconsistent].tv_nsec;
91 			delta -= list[inconsistent + 1].tv_sec * NSEC_PER_SEC;
92 			delta -= list[inconsistent + 1].tv_nsec;
93 			printf("Delta: %llu ns\n", delta);
94 			fflush(0);
95 			/* timestamp inconsistency */
96 			system("date");
97 			return -1;
98 		}
99 		now = list[0].tv_sec;
100 	}
101 	return 0;
102 }
103