VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstTimer.cpp@ 3125

Last change on this file since 3125 was 2981, checked in by vboxsync, 17 years ago

InnoTek -> innotek: all the headers and comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1/* $Id: tstTimer.cpp 2981 2007-06-01 16:01:28Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime Testcase - Timers.
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#include <iprt/timer.h>
26#include <iprt/time.h>
27#include <iprt/thread.h>
28#include <iprt/runtime.h>
29#include <iprt/stream.h>
30#include <iprt/err.h>
31
32
33
34/*******************************************************************************
35* Global Variables *
36*******************************************************************************/
37static volatile unsigned gcTicks;
38static volatile uint64_t gu64Min;
39static volatile uint64_t gu64Max;
40static volatile uint64_t gu64Prev;
41
42static DECLCALLBACK(void) TimerCallback(PRTTIMER pTimer, void *pvUser)
43{
44 gcTicks++;
45
46 const uint64_t u64Now = RTTimeNanoTS();
47 if (gu64Prev)
48 {
49 const uint64_t u64Delta = u64Now - gu64Prev;
50 if (u64Delta < gu64Min)
51 gu64Min = u64Delta;
52 if (u64Delta > gu64Max)
53 gu64Max = u64Delta;
54 }
55 gu64Prev = u64Now;
56}
57
58
59int main()
60{
61 /*
62 * Init runtime
63 */
64 unsigned cErrors = 0;
65 int rc = RTR3Init();
66 if (RT_FAILURE(rc))
67 {
68 RTPrintf("tstTimer: RTR3Init() -> %d\n", rc);
69 return 1;
70 }
71
72 /*
73 * Check that the clock is reliable.
74 */
75 RTPrintf("tstTimer: TESTING - RTTimeNanoTS() for 2sec\n");
76 uint64_t uTSMillies = RTTimeMilliTS();
77 uint64_t uTSBegin = RTTimeNanoTS();
78 uint64_t uTSLast = uTSBegin;
79 uint64_t uTSDiff;
80 uint64_t cIterations = 0;
81
82 do
83 {
84 uint64_t uTS = RTTimeNanoTS();
85 if (uTS < uTSLast)
86 {
87 RTPrintf("tstTimer: FAILURE - RTTimeNanoTS() is unreliable. uTS=%RU64 uTSLast=%RU64\n", uTS, uTSLast);
88 cErrors++;
89 }
90 if (++cIterations > (2*1000*1000*1000))
91 {
92 RTPrintf("tstTimer: FAILURE - RTTimeNanoTS() is unreliable. cIterations=%RU64 uTS=%RU64 uTSBegin=%RU64\n", cIterations, uTS, uTSBegin);
93 return 1;
94 }
95 uTSLast = uTS;
96 uTSDiff = uTSLast - uTSBegin;
97 } while (uTSDiff < (2*1000*1000*1000));
98 uTSMillies = RTTimeMilliTS() - uTSMillies;
99 if (uTSMillies >= 2500 || uTSMillies <= 1500)
100 {
101 RTPrintf("tstTimer: FAILURE - uTSMillies=%RI64 uTSBegin=%RU64 uTSLast=%RU64 uTSDiff=%RU64\n",
102 uTSMillies, uTSBegin, uTSLast, uTSDiff);
103 cErrors++;
104 }
105 if (!cErrors)
106 RTPrintf("tstTimer: OK - RTTimeNanoTS()\n");
107
108 /*
109 * Tests.
110 */
111 static struct
112 {
113 unsigned uMilliesInterval;
114 unsigned uMilliesWait;
115 unsigned cLower;
116 unsigned cUpper;
117 } aTests[] =
118 {
119 { 32, 2000, 0, 0 },
120 { 20, 2000, 0, 0 },
121 { 10, 2000, 0, 0 },
122 { 8, 2000, 0, 0 },
123 { 2, 2000, 0, 0 },
124 { 1, 2000, 0, 0 }
125 };
126
127 unsigned i = 0;
128 for (i = 0; i < ELEMENTS(aTests); i++)
129 {
130 aTests[i].cLower = (aTests[i].uMilliesWait - aTests[i].uMilliesWait / 10) / aTests[i].uMilliesInterval;
131 aTests[i].cUpper = (aTests[i].uMilliesWait + aTests[i].uMilliesWait / 10) / aTests[i].uMilliesInterval;
132
133 RTPrintf("tstTimer: TESTING - %d ms interval, %d ms wait, expects %d-%d ticks.\n",
134 aTests[i].uMilliesInterval, aTests[i].uMilliesWait, aTests[i].cLower, aTests[i].cUpper);
135
136 /*
137 * Start timer which ticks every 10ms.
138 */
139 gcTicks = 0;
140 PRTTIMER pTimer;
141 gu64Max = 0;
142 gu64Min = UINT64_MAX;
143 gu64Prev = 0;
144 rc = RTTimerCreate(&pTimer, aTests[i].uMilliesInterval, TimerCallback, NULL);
145 if (RT_FAILURE(rc))
146 {
147 RTPrintf("RTTimerCreate(,%d,) -> %d\n", aTests[i].uMilliesInterval, rc);
148 cErrors++;
149 continue;
150 }
151
152 /*
153 * Active waiting for 2 seconds and then destroy it.
154 */
155 uint64_t uTSBegin = RTTimeNanoTS();
156 while (RTTimeNanoTS() - uTSBegin < (uint64_t)aTests[i].uMilliesWait * 1000000)
157 /* nothing */;
158 uint64_t uTSEnd = RTTimeNanoTS();
159 uint64_t uTSDiff = uTSEnd - uTSBegin;
160 RTPrintf("uTS=%RI64 (%RU64 - %RU64)\n", uTSDiff, uTSBegin, uTSEnd);
161 if (RT_FAILURE(rc))
162 RTPrintf("warning: RTThreadSleep ended prematurely with %d\n", rc);
163 rc = RTTimerDestroy(pTimer);
164 if (RT_FAILURE(rc))
165 {
166 RTPrintf("tstTimer: FAILURE - RTTimerDestroy() -> %d gcTicks=%d\n", rc, gcTicks);
167 cErrors++;
168 continue;
169 }
170 unsigned cTicks = gcTicks;
171 RTThreadSleep(100);
172 if (gcTicks != cTicks)
173 {
174 RTPrintf("tstTimer: FAILURE - RTTimerDestroy() didn't really stop the timer! gcTicks=%d cTicks=%d\n", gcTicks, cTicks);
175 cErrors++;
176 continue;
177 }
178
179 /*
180 * Check the number of ticks.
181 */
182 if (gcTicks < aTests[i].cLower)
183 {
184 RTPrintf("tstTimer: FAILURE - Too few ticks gcTicks=%d (expected %d-%d)", gcTicks, aTests[i].cUpper, aTests[i].cLower);
185 cErrors++;
186 }
187 else if (gcTicks > aTests[i].cUpper)
188 {
189 RTPrintf("tstTimer: FAILURE - Too many ticks gcTicks=%d (expected %d-%d)", gcTicks, aTests[i].cUpper, aTests[i].cLower);
190 cErrors++;
191 }
192 else
193 RTPrintf("tstTimer: OK - gcTicks=%d", gcTicks);
194 RTPrintf(" min=%RU64 max=%RU64\n", gu64Min, gu64Max);
195 }
196
197 /*
198 * Summary.
199 */
200 if (!cErrors)
201 RTPrintf("tstTimer: SUCCESS\n");
202 else
203 RTPrintf("tstTimer: FAILURE %d errors\n", cErrors);
204 return !!cErrors;
205}
206
207
208
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