1 | /* $Id: tstRTTime.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Simple RTTime tests (requires GIP).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-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 <iprt/time.h>
|
---|
32 |
|
---|
33 | #include <iprt/errcore.h>
|
---|
34 | #include <iprt/initterm.h>
|
---|
35 | #include <iprt/message.h>
|
---|
36 | #include <iprt/test.h>
|
---|
37 | #include <iprt/thread.h>
|
---|
38 |
|
---|
39 | int main()
|
---|
40 | {
|
---|
41 | /*
|
---|
42 | * Init.
|
---|
43 | */
|
---|
44 | RTTEST hTest;
|
---|
45 | RTEXITCODE rcExit = RTTestInitExAndCreate(0, NULL, RTR3INIT_FLAGS_SUPLIB, "tstRTTime", &hTest);
|
---|
46 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
47 | return rcExit;
|
---|
48 | RTTestBanner(hTest);
|
---|
49 |
|
---|
50 | /*
|
---|
51 | * RTNanoTimeTS() shall never return something which
|
---|
52 | * is less or equal to the return of the previous call.
|
---|
53 | */
|
---|
54 |
|
---|
55 | RTTimeSystemNanoTS(); RTTimeNanoTS(); RTThreadYield();
|
---|
56 | uint64_t u64RTStartTS = RTTimeNanoTS();
|
---|
57 | uint64_t u64OSStartTS = RTTimeSystemNanoTS();
|
---|
58 |
|
---|
59 | uint32_t i;
|
---|
60 | uint64_t u64Prev = RTTimeNanoTS();
|
---|
61 | for (i = 0; i < 100*_1M; i++)
|
---|
62 | {
|
---|
63 | uint64_t u64 = RTTimeNanoTS();
|
---|
64 | if (u64 <= u64Prev)
|
---|
65 | {
|
---|
66 | /** @todo wrapping detection. */
|
---|
67 | RTTestFailed(hTest, "i=%#010x u64=%#llx u64Prev=%#llx (1)\n", i, u64, u64Prev);
|
---|
68 | if (RTTestErrorCount(hTest) >= 256)
|
---|
69 | break;
|
---|
70 | RTThreadYield();
|
---|
71 | u64 = RTTimeNanoTS();
|
---|
72 | }
|
---|
73 | else if (u64 - u64Prev > 1000000000 /* 1sec */)
|
---|
74 | {
|
---|
75 | RTTestFailed(hTest, "i=%#010x u64=%#llx u64Prev=%#llx delta=%lld\n", i, u64, u64Prev, u64 - u64Prev);
|
---|
76 | if (RTTestErrorCount(hTest) >= 256)
|
---|
77 | break;
|
---|
78 | RTThreadYield();
|
---|
79 | u64 = RTTimeNanoTS();
|
---|
80 | }
|
---|
81 | if (!(i & (_1M*2 - 1)))
|
---|
82 | {
|
---|
83 | RTTestPrintf(hTest, RTTESTLVL_INFO, "i=%#010x u64=%#llx u64Prev=%#llx delta=%lld\n", i, u64, u64Prev, u64 - u64Prev);
|
---|
84 | RTThreadYield();
|
---|
85 | u64 = RTTimeNanoTS();
|
---|
86 | }
|
---|
87 | u64Prev = u64;
|
---|
88 | }
|
---|
89 |
|
---|
90 | RTTimeSystemNanoTS(); RTTimeNanoTS(); RTThreadYield();
|
---|
91 | uint64_t u64RTElapsedTS = RTTimeNanoTS();
|
---|
92 | uint64_t u64OSElapsedTS = RTTimeSystemNanoTS();
|
---|
93 | u64RTElapsedTS -= u64RTStartTS;
|
---|
94 | u64OSElapsedTS -= u64OSStartTS;
|
---|
95 | int64_t i64Diff = u64OSElapsedTS >= u64RTElapsedTS ? u64OSElapsedTS - u64RTElapsedTS : u64RTElapsedTS - u64OSElapsedTS;
|
---|
96 | if (i64Diff > (int64_t)(u64OSElapsedTS / 1000))
|
---|
97 | RTTestFailed(hTest, "total time differs too much! u64OSElapsedTS=%#llx u64RTElapsedTS=%#llx delta=%lld\n",
|
---|
98 | u64OSElapsedTS, u64RTElapsedTS, u64OSElapsedTS - u64RTElapsedTS);
|
---|
99 | else
|
---|
100 | {
|
---|
101 | if (u64OSElapsedTS >= u64RTElapsedTS)
|
---|
102 | RTTestValue(hTest, "Total time delta", u64OSElapsedTS - u64RTElapsedTS, RTTESTUNIT_NS);
|
---|
103 | else
|
---|
104 | RTTestValue(hTest, "Total time delta", u64RTElapsedTS - u64OSElapsedTS, RTTESTUNIT_NS);
|
---|
105 | RTTestPrintf(hTest, RTTESTLVL_INFO, "total time difference: u64OSElapsedTS=%#llx u64RTElapsedTS=%#llx delta=%lld\n",
|
---|
106 | u64OSElapsedTS, u64RTElapsedTS, u64OSElapsedTS - u64RTElapsedTS);
|
---|
107 | }
|
---|
108 |
|
---|
109 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86) /** @todo This isn't really x86 or AMD64 specific... */
|
---|
110 | RTTestValue(hTest, "RTTimeDbgSteps", RTTimeDbgSteps(), RTTESTUNIT_OCCURRENCES);
|
---|
111 | RTTestValue(hTest, "RTTimeDbgSteps pp", ((uint64_t)RTTimeDbgSteps() * 1000) / i, RTTESTUNIT_PP1K);
|
---|
112 | RTTestValue(hTest, "RTTimeDbgExpired", RTTimeDbgExpired(), RTTESTUNIT_OCCURRENCES);
|
---|
113 | RTTestValue(hTest, "RTTimeDbgExpired pp", ((uint64_t)RTTimeDbgExpired() * 1000) / i, RTTESTUNIT_PP1K);
|
---|
114 | RTTestValue(hTest, "RTTimeDbgBad", RTTimeDbgBad(), RTTESTUNIT_OCCURRENCES);
|
---|
115 | RTTestValue(hTest, "RTTimeDbgBad pp", ((uint64_t)RTTimeDbgBad() * 1000) / i, RTTESTUNIT_PP1K);
|
---|
116 | RTTestValue(hTest, "RTTimeDbgRaces", RTTimeDbgRaces(), RTTESTUNIT_OCCURRENCES);
|
---|
117 | RTTestValue(hTest, "RTTimeDbgRaces pp", ((uint64_t)RTTimeDbgRaces() * 1000) / i, RTTESTUNIT_PP1K);
|
---|
118 | #endif
|
---|
119 |
|
---|
120 | return RTTestSummaryAndDestroy(hTest);
|
---|
121 | }
|
---|