1 | /* $Id: tstRTTime.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Simple RTTime tests (requires GIP).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/time.h>
|
---|
42 |
|
---|
43 | #include <iprt/errcore.h>
|
---|
44 | #include <iprt/initterm.h>
|
---|
45 | #include <iprt/message.h>
|
---|
46 | #include <iprt/test.h>
|
---|
47 | #include <iprt/thread.h>
|
---|
48 |
|
---|
49 | int main()
|
---|
50 | {
|
---|
51 | /*
|
---|
52 | * Init.
|
---|
53 | */
|
---|
54 | RTTEST hTest;
|
---|
55 | RTEXITCODE rcExit = RTTestInitExAndCreate(0, NULL, RTR3INIT_FLAGS_SUPLIB, "tstRTTime", &hTest);
|
---|
56 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
57 | return rcExit;
|
---|
58 | RTTestBanner(hTest);
|
---|
59 |
|
---|
60 | /*
|
---|
61 | * RTNanoTimeTS() shall never return something which
|
---|
62 | * is less or equal to the return of the previous call.
|
---|
63 | */
|
---|
64 |
|
---|
65 | RTTimeSystemNanoTS(); RTTimeNanoTS(); RTThreadYield();
|
---|
66 | uint64_t u64RTStartTS = RTTimeNanoTS();
|
---|
67 | uint64_t u64OSStartTS = RTTimeSystemNanoTS();
|
---|
68 |
|
---|
69 | uint32_t i;
|
---|
70 | uint64_t u64Prev = RTTimeNanoTS();
|
---|
71 | for (i = 0; i < 100*_1M; i++)
|
---|
72 | {
|
---|
73 | uint64_t u64 = RTTimeNanoTS();
|
---|
74 | if (u64 <= u64Prev)
|
---|
75 | {
|
---|
76 | /** @todo wrapping detection. */
|
---|
77 | RTTestFailed(hTest, "i=%#010x u64=%#llx u64Prev=%#llx (1)\n", i, u64, u64Prev);
|
---|
78 | if (RTTestErrorCount(hTest) >= 256)
|
---|
79 | break;
|
---|
80 | RTThreadYield();
|
---|
81 | u64 = RTTimeNanoTS();
|
---|
82 | }
|
---|
83 | else if (u64 - u64Prev > 1000000000 /* 1sec */)
|
---|
84 | {
|
---|
85 | RTTestFailed(hTest, "i=%#010x u64=%#llx u64Prev=%#llx delta=%lld\n", i, u64, u64Prev, u64 - u64Prev);
|
---|
86 | if (RTTestErrorCount(hTest) >= 256)
|
---|
87 | break;
|
---|
88 | RTThreadYield();
|
---|
89 | u64 = RTTimeNanoTS();
|
---|
90 | }
|
---|
91 | if (!(i & (_1M*2 - 1)))
|
---|
92 | {
|
---|
93 | RTTestPrintf(hTest, RTTESTLVL_INFO, "i=%#010x u64=%#llx u64Prev=%#llx delta=%lld\n", i, u64, u64Prev, u64 - u64Prev);
|
---|
94 | RTThreadYield();
|
---|
95 | u64 = RTTimeNanoTS();
|
---|
96 | }
|
---|
97 | u64Prev = u64;
|
---|
98 | }
|
---|
99 |
|
---|
100 | RTTimeSystemNanoTS(); RTTimeNanoTS(); RTThreadYield();
|
---|
101 | uint64_t u64RTElapsedTS = RTTimeNanoTS();
|
---|
102 | uint64_t u64OSElapsedTS = RTTimeSystemNanoTS();
|
---|
103 | u64RTElapsedTS -= u64RTStartTS;
|
---|
104 | u64OSElapsedTS -= u64OSStartTS;
|
---|
105 | int64_t i64Diff = u64OSElapsedTS >= u64RTElapsedTS ? u64OSElapsedTS - u64RTElapsedTS : u64RTElapsedTS - u64OSElapsedTS;
|
---|
106 | if (i64Diff > (int64_t)(u64OSElapsedTS / 1000))
|
---|
107 | RTTestFailed(hTest, "total time differs too much! u64OSElapsedTS=%#llx u64RTElapsedTS=%#llx delta=%lld\n",
|
---|
108 | u64OSElapsedTS, u64RTElapsedTS, u64OSElapsedTS - u64RTElapsedTS);
|
---|
109 | else
|
---|
110 | {
|
---|
111 | if (u64OSElapsedTS >= u64RTElapsedTS)
|
---|
112 | RTTestValue(hTest, "Total time delta", u64OSElapsedTS - u64RTElapsedTS, RTTESTUNIT_NS);
|
---|
113 | else
|
---|
114 | RTTestValue(hTest, "Total time delta", u64RTElapsedTS - u64OSElapsedTS, RTTESTUNIT_NS);
|
---|
115 | RTTestPrintf(hTest, RTTESTLVL_INFO, "total time difference: u64OSElapsedTS=%#llx u64RTElapsedTS=%#llx delta=%lld\n",
|
---|
116 | u64OSElapsedTS, u64RTElapsedTS, u64OSElapsedTS - u64RTElapsedTS);
|
---|
117 | }
|
---|
118 |
|
---|
119 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86) /** @todo This isn't really x86 or AMD64 specific... */
|
---|
120 | RTTestValue(hTest, "RTTimeDbgSteps", RTTimeDbgSteps(), RTTESTUNIT_OCCURRENCES);
|
---|
121 | RTTestValue(hTest, "RTTimeDbgSteps pp", ((uint64_t)RTTimeDbgSteps() * 1000) / i, RTTESTUNIT_PP1K);
|
---|
122 | RTTestValue(hTest, "RTTimeDbgExpired", RTTimeDbgExpired(), RTTESTUNIT_OCCURRENCES);
|
---|
123 | RTTestValue(hTest, "RTTimeDbgExpired pp", ((uint64_t)RTTimeDbgExpired() * 1000) / i, RTTESTUNIT_PP1K);
|
---|
124 | RTTestValue(hTest, "RTTimeDbgBad", RTTimeDbgBad(), RTTESTUNIT_OCCURRENCES);
|
---|
125 | RTTestValue(hTest, "RTTimeDbgBad pp", ((uint64_t)RTTimeDbgBad() * 1000) / i, RTTESTUNIT_PP1K);
|
---|
126 | RTTestValue(hTest, "RTTimeDbgRaces", RTTimeDbgRaces(), RTTESTUNIT_OCCURRENCES);
|
---|
127 | RTTestValue(hTest, "RTTimeDbgRaces pp", ((uint64_t)RTTimeDbgRaces() * 1000) / i, RTTESTUNIT_PP1K);
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | return RTTestSummaryAndDestroy(hTest);
|
---|
131 | }
|
---|