1 | /* $Id: tstTime.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime Testcase - Simple RTTime tests.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #ifdef RT_OS_WINDOWS
|
---|
22 | # include <Windows.h>
|
---|
23 |
|
---|
24 | #elif defined RT_OS_L4
|
---|
25 |
|
---|
26 | #else /* posix */
|
---|
27 | # include <sys/time.h>
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | #include <iprt/time.h>
|
---|
31 | #include <iprt/stream.h>
|
---|
32 | #include <iprt/runtime.h>
|
---|
33 | #include <iprt/thread.h>
|
---|
34 | #include <VBox/sup.h>
|
---|
35 |
|
---|
36 | DECLINLINE(uint64_t) OSNanoTS(void)
|
---|
37 | {
|
---|
38 | #ifdef RT_OS_WINDOWS
|
---|
39 | uint64_t u64; /* manual say larger integer, should be safe to assume it's the same. */
|
---|
40 | GetSystemTimeAsFileTime((LPFILETIME)&u64);
|
---|
41 | return u64 * 100;
|
---|
42 |
|
---|
43 | #elif defined RT_OS_L4
|
---|
44 | /** @todo fix a different timesource on l4. */
|
---|
45 | return RTTimeNanoTS();
|
---|
46 |
|
---|
47 | #else /* posix */
|
---|
48 |
|
---|
49 | struct timeval tv;
|
---|
50 | gettimeofday(&tv, NULL);
|
---|
51 | return (uint64_t)tv.tv_sec * (uint64_t)(1000 * 1000 * 1000)
|
---|
52 | + (uint64_t)(tv.tv_usec * 1000);
|
---|
53 | #endif
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 | int main()
|
---|
59 | {
|
---|
60 | unsigned cErrors = 0;
|
---|
61 | int i;
|
---|
62 | RTR3Init();
|
---|
63 | SUPInit();
|
---|
64 | RTPrintf("tstTime: TESTING...\n");
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * RTNanoTimeTS() shall never return something which
|
---|
68 | * is less or equal to the return of the previous call.
|
---|
69 | */
|
---|
70 |
|
---|
71 | OSNanoTS(); RTTimeNanoTS(); RTThreadYield();
|
---|
72 | uint64_t u64RTStartTS = RTTimeNanoTS();
|
---|
73 | uint64_t u64OSStartTS = OSNanoTS();
|
---|
74 |
|
---|
75 | uint64_t u64Prev = RTTimeNanoTS();
|
---|
76 | for (i = 0; i < 100*_1M; i++)
|
---|
77 | {
|
---|
78 | uint64_t u64 = RTTimeNanoTS();
|
---|
79 | if (u64 <= u64Prev)
|
---|
80 | {
|
---|
81 | /** @todo wrapping detection. */
|
---|
82 | RTPrintf("tstTime: error: i=%#010x u64=%#llx u64Prev=%#llx (1)\n", i, u64, u64Prev);
|
---|
83 | cErrors++;
|
---|
84 | RTThreadYield();
|
---|
85 | u64 = RTTimeNanoTS();
|
---|
86 | }
|
---|
87 | else if (u64 - u64Prev > 1000000000 /* 1sec */)
|
---|
88 | {
|
---|
89 | RTPrintf("tstTime: error: i=%#010x u64=%#llx u64Prev=%#llx delta=%lld\n", i, u64, u64Prev, u64 - u64Prev);
|
---|
90 | cErrors++;
|
---|
91 | RTThreadYield();
|
---|
92 | u64 = RTTimeNanoTS();
|
---|
93 | }
|
---|
94 | if (!(i & (_1M*2 - 1)))
|
---|
95 | {
|
---|
96 | RTPrintf("tstTime: i=%#010x u64=%#llx u64Prev=%#llx delta=%lld\n", i, u64, u64Prev, u64 - u64Prev);
|
---|
97 | RTThreadYield();
|
---|
98 | u64 = RTTimeNanoTS();
|
---|
99 | }
|
---|
100 | u64Prev = u64;
|
---|
101 | }
|
---|
102 |
|
---|
103 | OSNanoTS(); RTTimeNanoTS(); RTThreadYield();
|
---|
104 | uint64_t u64RTElapsedTS = RTTimeNanoTS();
|
---|
105 | uint64_t u64OSElapsedTS = OSNanoTS();
|
---|
106 | u64RTElapsedTS -= u64RTStartTS;
|
---|
107 | u64OSElapsedTS -= u64OSStartTS;
|
---|
108 | int64_t i64Diff = u64OSElapsedTS >= u64RTElapsedTS ? u64OSElapsedTS - u64RTElapsedTS : u64RTElapsedTS - u64OSElapsedTS;
|
---|
109 | if (i64Diff > (int64_t)(u64OSElapsedTS / 1000))
|
---|
110 | {
|
---|
111 | RTPrintf("tstTime: error: total time differs too much! u64OSElapsedTS=%#llx u64RTElapsedTS=%#llx delta=%lld\n",
|
---|
112 | u64OSElapsedTS, u64RTElapsedTS, u64OSElapsedTS - u64RTElapsedTS);
|
---|
113 | cErrors++;
|
---|
114 | }
|
---|
115 | else
|
---|
116 | RTPrintf("tstTime: total time difference: u64OSElapsedTS=%#llx u64RTElapsedTS=%#llx delta=%lld\n",
|
---|
117 | u64OSElapsedTS, u64RTElapsedTS, u64OSElapsedTS - u64RTElapsedTS);
|
---|
118 |
|
---|
119 | RTPrintf("RTTime1nsSteps -> %u (%d ppt)\n", RTTime1nsSteps(), (RTTime1nsSteps() * 1000) / i);
|
---|
120 | if (!cErrors)
|
---|
121 | RTPrintf("tstTime: SUCCESS\n");
|
---|
122 | else
|
---|
123 | RTPrintf("tstTime: FAILURE - %d errors\n", cErrors);
|
---|
124 | return !!cErrors;
|
---|
125 | }
|
---|