1 | /* $Id: tstTime.cpp 2981 2007-06-01 16:01:28Z 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 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #ifdef __WIN__
|
---|
26 | # include <Windows.h>
|
---|
27 |
|
---|
28 | #elif defined __L4__
|
---|
29 |
|
---|
30 | #else /* posix */
|
---|
31 | # include <sys/time.h>
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <iprt/time.h>
|
---|
35 | #include <iprt/stream.h>
|
---|
36 | #include <iprt/runtime.h>
|
---|
37 | #include <iprt/thread.h>
|
---|
38 | #include <VBox/sup.h>
|
---|
39 |
|
---|
40 | DECLINLINE(uint64_t) OSNanoTS(void)
|
---|
41 | {
|
---|
42 | #ifdef __WIN__
|
---|
43 | uint64_t u64; /* manual say larger integer, should be safe to assume it's the same. */
|
---|
44 | GetSystemTimeAsFileTime((LPFILETIME)&u64);
|
---|
45 | return u64 * 100;
|
---|
46 |
|
---|
47 | #elif defined __L4__
|
---|
48 | /** @todo fix a different timesource on l4. */
|
---|
49 | return RTTimeNanoTS();
|
---|
50 |
|
---|
51 | #else /* posix */
|
---|
52 |
|
---|
53 | struct timeval tv;
|
---|
54 | gettimeofday(&tv, NULL);
|
---|
55 | return (uint64_t)tv.tv_sec * (uint64_t)(1000 * 1000 * 1000)
|
---|
56 | + (uint64_t)(tv.tv_usec * 1000);
|
---|
57 | #endif
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 |
|
---|
62 | int main()
|
---|
63 | {
|
---|
64 | unsigned cErrors = 0;
|
---|
65 | int i;
|
---|
66 | RTR3Init();
|
---|
67 | SUPInit();
|
---|
68 | RTPrintf("tstTime: TESTING...\n");
|
---|
69 |
|
---|
70 | /*
|
---|
71 | * RTNanoTimeTS() shall never return something which
|
---|
72 | * is less or equal to the return of the previous call.
|
---|
73 | */
|
---|
74 |
|
---|
75 | OSNanoTS(); RTTimeNanoTS(); RTThreadYield();
|
---|
76 | uint64_t u64RTStartTS = RTTimeNanoTS();
|
---|
77 | uint64_t u64OSStartTS = OSNanoTS();
|
---|
78 |
|
---|
79 | uint64_t u64Prev = RTTimeNanoTS();
|
---|
80 | for (i = 0; i < 100*_1M; i++)
|
---|
81 | {
|
---|
82 | uint64_t u64 = RTTimeNanoTS();
|
---|
83 | if (u64 <= u64Prev)
|
---|
84 | {
|
---|
85 | /** @todo wrapping detection. */
|
---|
86 | RTPrintf("tstTime: error: i=%#010x u64=%#llx u64Prev=%#llx (1)\n", i, u64, u64Prev);
|
---|
87 | cErrors++;
|
---|
88 | RTThreadYield();
|
---|
89 | u64 = RTTimeNanoTS();
|
---|
90 | }
|
---|
91 | else if (u64 - u64Prev > 1000000000 /* 1sec */)
|
---|
92 | {
|
---|
93 | RTPrintf("tstTime: error: i=%#010x u64=%#llx u64Prev=%#llx delta=%lld\n", i, u64, u64Prev, u64 - u64Prev);
|
---|
94 | cErrors++;
|
---|
95 | RTThreadYield();
|
---|
96 | u64 = RTTimeNanoTS();
|
---|
97 | }
|
---|
98 | if (!(i & (_1M*2 - 1)))
|
---|
99 | {
|
---|
100 | RTPrintf("tstTime: i=%#010x u64=%#llx u64Prev=%#llx delta=%lld\n", i, u64, u64Prev, u64 - u64Prev);
|
---|
101 | RTThreadYield();
|
---|
102 | u64 = RTTimeNanoTS();
|
---|
103 | }
|
---|
104 | u64Prev = u64;
|
---|
105 | }
|
---|
106 |
|
---|
107 | OSNanoTS(); RTTimeNanoTS(); RTThreadYield();
|
---|
108 | uint64_t u64RTElapsedTS = RTTimeNanoTS();
|
---|
109 | uint64_t u64OSElapsedTS = OSNanoTS();
|
---|
110 | u64RTElapsedTS -= u64RTStartTS;
|
---|
111 | u64OSElapsedTS -= u64OSStartTS;
|
---|
112 | int64_t i64Diff = u64OSElapsedTS >= u64RTElapsedTS ? u64OSElapsedTS - u64RTElapsedTS : u64RTElapsedTS - u64OSElapsedTS;
|
---|
113 | if (i64Diff > (int64_t)(u64OSElapsedTS / 1000))
|
---|
114 | {
|
---|
115 | RTPrintf("tstTime: error: total time differs too much! u64OSElapsedTS=%#llx u64RTElapsedTS=%#llx delta=%lld\n",
|
---|
116 | u64OSElapsedTS, u64RTElapsedTS, u64OSElapsedTS - u64RTElapsedTS);
|
---|
117 | cErrors++;
|
---|
118 | }
|
---|
119 | else
|
---|
120 | RTPrintf("tstTime: total time difference: u64OSElapsedTS=%#llx u64RTElapsedTS=%#llx delta=%lld\n",
|
---|
121 | u64OSElapsedTS, u64RTElapsedTS, u64OSElapsedTS - u64RTElapsedTS);
|
---|
122 |
|
---|
123 | RTPrintf("RTTime1nsSteps -> %u (%d ppt)\n", RTTime1nsSteps(), (RTTime1nsSteps() * 1000) / i);
|
---|
124 | if (!cErrors)
|
---|
125 | RTPrintf("tstTime: SUCCESS\n");
|
---|
126 | else
|
---|
127 | RTPrintf("tstTime: FAILURE - %d errors\n", cErrors);
|
---|
128 | return !!cErrors;
|
---|
129 | }
|
---|