VirtualBox

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

Last change on this file since 32970 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1/* $Id: tstTimerLR.cpp 28800 2010-04-27 08:22:32Z 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/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) TimerLRCallback(RTTIMERLR hTimerLR, void *pvUser, uint64_t iTick)
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 { 1000, 2500, 3, 3 }, /* (keep in mind the immediate first tick) */
125 { 250, 2000, 6, 10 },
126 { 100, 2000, 17, 23 },
127 };
128
129 unsigned i = 0;
130 for (i = 0; i < RT_ELEMENTS(aTests); i++)
131 {
132 //aTests[i].cLower = (aTests[i].uMilliesWait - aTests[i].uMilliesWait / 10) / aTests[i].uMilliesInterval;
133 //aTests[i].cUpper = (aTests[i].uMilliesWait + aTests[i].uMilliesWait / 10) / aTests[i].uMilliesInterval;
134
135 RTPrintf("\n"
136 "tstTimer: TESTING - %d ms interval, %d ms wait, expects %d-%d ticks.\n",
137 aTests[i].uMilliesInterval, aTests[i].uMilliesWait, aTests[i].cLower, aTests[i].cUpper);
138
139 /*
140 * Start timer which ticks every 10ms.
141 */
142 gcTicks = 0;
143 RTTIMERLR hTimerLR;
144 gu64Max = 0;
145 gu64Min = UINT64_MAX;
146 gu64Prev = 0;
147 rc = RTTimerLRCreateEx(&hTimerLR, aTests[i].uMilliesInterval * (uint64_t)1000000, 0, TimerLRCallback, NULL);
148 if (RT_FAILURE(rc))
149 {
150 RTPrintf("RTTimerLRCreateEX(,%u*1M,,,) -> %d\n", aTests[i].uMilliesInterval, rc);
151 cErrors++;
152 continue;
153 }
154
155 /*
156 * Start the timer an actively wait for it for the period requested.
157 */
158 uTSBegin = RTTimeNanoTS();
159 rc = RTTimerLRStart(hTimerLR, 0);
160 if (RT_FAILURE(rc))
161 {
162 RTPrintf("tstTimer: FAILURE - RTTimerLRStart() -> %Rrc\n", rc);
163 cErrors++;
164 }
165
166 while (RTTimeNanoTS() - uTSBegin < (uint64_t)aTests[i].uMilliesWait * 1000000)
167 /* nothing */;
168
169 /* don't stop it, destroy it because there are potential races in destroying an active timer. */
170 rc = RTTimerLRDestroy(hTimerLR);
171 if (RT_FAILURE(rc))
172 {
173 RTPrintf("tstTimer: FAILURE - RTTimerLRDestroy() -> %d gcTicks=%d\n", rc, gcTicks);
174 cErrors++;
175 }
176
177 uint64_t uTSEnd = RTTimeNanoTS();
178 uTSDiff = uTSEnd - uTSBegin;
179 RTPrintf("uTS=%RI64 (%RU64 - %RU64)\n", uTSDiff, uTSBegin, uTSEnd);
180
181 /* Check that it really stopped. */
182 unsigned cTicks = gcTicks;
183 RTThreadSleep(aTests[i].uMilliesInterval * 2);
184 if (gcTicks != cTicks)
185 {
186 RTPrintf("tstTimer: FAILURE - RTTimerLRDestroy() didn't really stop the timer! gcTicks=%d cTicks=%d\n", gcTicks, cTicks);
187 cErrors++;
188 continue;
189 }
190
191 /*
192 * Check the number of ticks.
193 */
194 if (gcTicks < aTests[i].cLower)
195 {
196 RTPrintf("tstTimer: FAILURE - Too few ticks gcTicks=%d (expected %d-%d)", gcTicks, aTests[i].cUpper, aTests[i].cLower);
197 cErrors++;
198 }
199 else if (gcTicks > aTests[i].cUpper)
200 {
201 RTPrintf("tstTimer: FAILURE - Too many ticks gcTicks=%d (expected %d-%d)", gcTicks, aTests[i].cUpper, aTests[i].cLower);
202 cErrors++;
203 }
204 else
205 RTPrintf("tstTimer: OK - gcTicks=%d", gcTicks);
206 RTPrintf(" min=%RU64 max=%RU64\n", gu64Min, gu64Max);
207 }
208
209 /*
210 * Test multiple timers running at once.
211 */
212 /** @todo multiple LR timer testcase. */
213
214 /*
215 * Summary.
216 */
217 if (!cErrors)
218 RTPrintf("tstTimer: SUCCESS\n");
219 else
220 RTPrintf("tstTimer: FAILURE %d errors\n", cErrors);
221 return !!cErrors;
222}
223
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