VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTTime.cpp@ 56335

Last change on this file since 56335 was 56290, checked in by vboxsync, 9 years ago

IPRT: Updated (C) year.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette