VirtualBox

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

Last change on this file since 7554 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

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