VirtualBox

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

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

rebranding: IPRT files again.

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