VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstTimerLR.cpp@ 39837

Last change on this file since 39837 was 38636, checked in by vboxsync, 13 years ago

*,IPRT: Redid the ring-3 init to always convert the arguments to UTF-8.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1/* $Id: tstTimerLR.cpp 38636 2011-09-05 13:49:45Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Low Resolution Timers.
4 */
5
6/*
7 * Copyright (C) 2006-2008 Oracle Corporation
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/initterm.h>
34#include <iprt/message.h>
35#include <iprt/stream.h>
36#include <iprt/err.h>
37
38
39
40/*******************************************************************************
41* Global Variables *
42*******************************************************************************/
43static volatile unsigned gcTicks;
44static volatile uint64_t gu64Min;
45static volatile uint64_t gu64Max;
46static volatile uint64_t gu64Prev;
47
48static DECLCALLBACK(void) TimerLRCallback(RTTIMERLR hTimerLR, void *pvUser, uint64_t iTick)
49{
50 gcTicks++;
51
52 const uint64_t u64Now = RTTimeNanoTS();
53 if (gu64Prev)
54 {
55 const uint64_t u64Delta = u64Now - gu64Prev;
56 if (u64Delta < gu64Min)
57 gu64Min = u64Delta;
58 if (u64Delta > gu64Max)
59 gu64Max = u64Delta;
60 }
61 gu64Prev = u64Now;
62}
63
64
65int main()
66{
67 /*
68 * Init runtime
69 */
70 unsigned cErrors = 0;
71 int rc = RTR3InitExeNoArguments(0);
72 if (RT_FAILURE(rc))
73 return RTMsgInitFailure(rc);
74
75 /*
76 * Check that the clock is reliable.
77 */
78 RTPrintf("tstTimer: TESTING - RTTimeNanoTS() for 2sec\n");
79 uint64_t uTSMillies = RTTimeMilliTS();
80 uint64_t uTSBegin = RTTimeNanoTS();
81 uint64_t uTSLast = uTSBegin;
82 uint64_t uTSDiff;
83 uint64_t cIterations = 0;
84
85 do
86 {
87 uint64_t uTS = RTTimeNanoTS();
88 if (uTS < uTSLast)
89 {
90 RTPrintf("tstTimer: FAILURE - RTTimeNanoTS() is unreliable. uTS=%RU64 uTSLast=%RU64\n", uTS, uTSLast);
91 cErrors++;
92 }
93 if (++cIterations > (2*1000*1000*1000))
94 {
95 RTPrintf("tstTimer: FAILURE - RTTimeNanoTS() is unreliable. cIterations=%RU64 uTS=%RU64 uTSBegin=%RU64\n", cIterations, uTS, uTSBegin);
96 return 1;
97 }
98 uTSLast = uTS;
99 uTSDiff = uTSLast - uTSBegin;
100 } while (uTSDiff < (2*1000*1000*1000));
101 uTSMillies = RTTimeMilliTS() - uTSMillies;
102 if (uTSMillies >= 2500 || uTSMillies <= 1500)
103 {
104 RTPrintf("tstTimer: FAILURE - uTSMillies=%RI64 uTSBegin=%RU64 uTSLast=%RU64 uTSDiff=%RU64\n",
105 uTSMillies, uTSBegin, uTSLast, uTSDiff);
106 cErrors++;
107 }
108 if (!cErrors)
109 RTPrintf("tstTimer: OK - RTTimeNanoTS()\n");
110
111 /*
112 * Tests.
113 */
114 static struct
115 {
116 unsigned uMilliesInterval;
117 unsigned uMilliesWait;
118 unsigned cLower;
119 unsigned cUpper;
120 } aTests[] =
121 {
122 { 1000, 2500, 3, 3 }, /* (keep in mind the immediate first tick) */
123 { 250, 2000, 6, 10 },
124 { 100, 2000, 17, 23 },
125 };
126
127 unsigned i = 0;
128 for (i = 0; i < RT_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("\n"
134 "tstTimer: TESTING - %d ms interval, %d ms wait, expects %d-%d ticks.\n",
135 aTests[i].uMilliesInterval, aTests[i].uMilliesWait, aTests[i].cLower, aTests[i].cUpper);
136
137 /*
138 * Start timer which ticks every 10ms.
139 */
140 gcTicks = 0;
141 RTTIMERLR hTimerLR;
142 gu64Max = 0;
143 gu64Min = UINT64_MAX;
144 gu64Prev = 0;
145 rc = RTTimerLRCreateEx(&hTimerLR, aTests[i].uMilliesInterval * (uint64_t)1000000, 0, TimerLRCallback, NULL);
146 if (RT_FAILURE(rc))
147 {
148 RTPrintf("RTTimerLRCreateEX(,%u*1M,,,) -> %d\n", aTests[i].uMilliesInterval, rc);
149 cErrors++;
150 continue;
151 }
152
153 /*
154 * Start the timer an actively wait for it for the period requested.
155 */
156 uTSBegin = RTTimeNanoTS();
157 rc = RTTimerLRStart(hTimerLR, 0);
158 if (RT_FAILURE(rc))
159 {
160 RTPrintf("tstTimer: FAILURE - RTTimerLRStart() -> %Rrc\n", rc);
161 cErrors++;
162 }
163
164 while (RTTimeNanoTS() - uTSBegin < (uint64_t)aTests[i].uMilliesWait * 1000000)
165 /* nothing */;
166
167 /* don't stop it, destroy it because there are potential races in destroying an active timer. */
168 rc = RTTimerLRDestroy(hTimerLR);
169 if (RT_FAILURE(rc))
170 {
171 RTPrintf("tstTimer: FAILURE - RTTimerLRDestroy() -> %d gcTicks=%d\n", rc, gcTicks);
172 cErrors++;
173 }
174
175 uint64_t uTSEnd = RTTimeNanoTS();
176 uTSDiff = uTSEnd - uTSBegin;
177 RTPrintf("uTS=%RI64 (%RU64 - %RU64)\n", uTSDiff, uTSBegin, uTSEnd);
178
179 /* Check that it really stopped. */
180 unsigned cTicks = gcTicks;
181 RTThreadSleep(aTests[i].uMilliesInterval * 2);
182 if (gcTicks != cTicks)
183 {
184 RTPrintf("tstTimer: FAILURE - RTTimerLRDestroy() didn't really stop the timer! gcTicks=%d cTicks=%d\n", gcTicks, cTicks);
185 cErrors++;
186 continue;
187 }
188
189 /*
190 * Check the number of ticks.
191 */
192 if (gcTicks < aTests[i].cLower)
193 {
194 RTPrintf("tstTimer: FAILURE - Too few ticks gcTicks=%d (expected %d-%d)", gcTicks, aTests[i].cUpper, aTests[i].cLower);
195 cErrors++;
196 }
197 else if (gcTicks > aTests[i].cUpper)
198 {
199 RTPrintf("tstTimer: FAILURE - Too many ticks gcTicks=%d (expected %d-%d)", gcTicks, aTests[i].cUpper, aTests[i].cLower);
200 cErrors++;
201 }
202 else
203 RTPrintf("tstTimer: OK - gcTicks=%d", gcTicks);
204 RTPrintf(" min=%RU64 max=%RU64\n", gu64Min, gu64Max);
205 }
206
207 /*
208 * Test multiple timers running at once.
209 */
210 /** @todo multiple LR timer testcase. */
211
212 /*
213 * Summary.
214 */
215 if (!cErrors)
216 RTPrintf("tstTimer: SUCCESS\n");
217 else
218 RTPrintf("tstTimer: FAILURE %d errors\n", cErrors);
219 return !!cErrors;
220}
221
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