1 | /* $Id: time-1.c 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Query the time and check that it always goes forward, POSIX only.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <time.h>
|
---|
33 | #include <sys/time.h>
|
---|
34 |
|
---|
35 |
|
---|
36 |
|
---|
37 | int main()
|
---|
38 | {
|
---|
39 | unsigned cErrors = 0;
|
---|
40 | #ifdef USE_CLOCK_MONOTONIC
|
---|
41 | struct timespec aTs[2];
|
---|
42 | struct timespec *pCur = &aTs[0];
|
---|
43 | struct timespec *pPrev = &aTs[1];
|
---|
44 | struct timespec *pTmp;
|
---|
45 | #else
|
---|
46 | struct timeval aTv[2];
|
---|
47 | struct timeval *pCur = &aTv[0];
|
---|
48 | struct timeval *pPrev = &aTv[1];
|
---|
49 | struct timeval *pTmp;
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | #ifdef USE_CLOCK_MONOTONIC
|
---|
53 | clock_gettime(CLOCK_MONOTONIC, pPrev);
|
---|
54 | #else
|
---|
55 | gettimeofday(pPrev, NULL);
|
---|
56 | #endif
|
---|
57 | for (;;)
|
---|
58 | {
|
---|
59 | #ifdef USE_CLOCK_MONOTONIC
|
---|
60 | clock_gettime(CLOCK_MONOTONIC, pCur);
|
---|
61 | #else
|
---|
62 | gettimeofday(pCur, NULL);
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | if ( pCur->tv_sec == pPrev->tv_sec
|
---|
66 | #ifdef USE_CLOCK_MONOTONIC
|
---|
67 | && pCur->tv_nsec < pPrev->tv_nsec
|
---|
68 | #else
|
---|
69 | && pCur->tv_usec < pPrev->tv_usec
|
---|
70 | #endif
|
---|
71 | )
|
---|
72 | {
|
---|
73 | #ifdef USE_CLOCK_MONOTONIC
|
---|
74 | printf("tv_nsec in the past: %ld.%09u < %ld.%09u - %u nsec\n",
|
---|
75 | (long)pCur->tv_sec, (unsigned)pCur->tv_nsec,
|
---|
76 | (long)pPrev->tv_sec, (unsigned)pPrev->tv_nsec,
|
---|
77 | (unsigned)pPrev->tv_nsec - (unsigned)pCur->tv_nsec);
|
---|
78 | #else
|
---|
79 | printf("tv_usec in the past: %ld.%06u < %ld.%06u - %u usec\n",
|
---|
80 | (long)pCur->tv_sec, (unsigned)pCur->tv_usec,
|
---|
81 | (long)pPrev->tv_sec, (unsigned)pPrev->tv_usec,
|
---|
82 | (unsigned)pPrev->tv_usec - (unsigned)pCur->tv_usec);
|
---|
83 | #endif
|
---|
84 | cErrors++;
|
---|
85 | if (cErrors > 1000)
|
---|
86 | break;
|
---|
87 | }
|
---|
88 | else if (pCur->tv_sec < pPrev->tv_sec)
|
---|
89 | {
|
---|
90 | #ifdef USE_CLOCK_MONOTONIC
|
---|
91 | printf("tv_sec in the past: %ld.%09u < %ld.%09u\n",
|
---|
92 | (long)pCur->tv_sec, (unsigned)pCur->tv_nsec,
|
---|
93 | (long)pPrev->tv_sec, (unsigned)pPrev->tv_nsec);
|
---|
94 | #else
|
---|
95 | printf("tv_sec in the past: %ld.%06u < %ld.%06u\n",
|
---|
96 | (long)pCur->tv_sec, (unsigned)pCur->tv_usec,
|
---|
97 | (long)pPrev->tv_sec, (unsigned)pPrev->tv_usec);
|
---|
98 | #endif
|
---|
99 | cErrors++;
|
---|
100 | if (cErrors > 1000)
|
---|
101 | break;
|
---|
102 | }
|
---|
103 | else
|
---|
104 | {
|
---|
105 | /* swap */
|
---|
106 | pTmp = pPrev;
|
---|
107 | pPrev = pCur;
|
---|
108 | pCur = pTmp;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | return 1;
|
---|
113 | }
|
---|