VirtualBox

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

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

iprt/posix timers: Cleanup fixing the create/destroy races, a termination crash, and thread handle leak.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1/* $Id: tstTimer.cpp 10941 2008-07-29 16:53:12Z 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, uint64_t iTick)
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("\n"
143 "tstTimer: TESTING - %d ms interval, %d ms wait, expects %d-%d ticks.\n",
144 aTests[i].uMilliesInterval, aTests[i].uMilliesWait, aTests[i].cLower, aTests[i].cUpper);
145
146 /*
147 * Start timer which ticks every 10ms.
148 */
149 gcTicks = 0;
150 PRTTIMER pTimer;
151 gu64Max = 0;
152 gu64Min = UINT64_MAX;
153 gu64Prev = 0;
154 rc = RTTimerCreate(&pTimer, aTests[i].uMilliesInterval, TimerCallback, NULL);
155 if (RT_FAILURE(rc))
156 {
157 RTPrintf("RTTimerCreate(,%d,) -> %d\n", aTests[i].uMilliesInterval, rc);
158 cErrors++;
159 continue;
160 }
161
162 /*
163 * Active waiting for 2 seconds and then destroy it.
164 */
165 uint64_t uTSBegin = RTTimeNanoTS();
166 while (RTTimeNanoTS() - uTSBegin < (uint64_t)aTests[i].uMilliesWait * 1000000)
167 /* nothing */;
168 uint64_t uTSEnd = RTTimeNanoTS();
169 uint64_t uTSDiff = uTSEnd - uTSBegin;
170 RTPrintf("uTS=%RI64 (%RU64 - %RU64)\n", uTSDiff, uTSBegin, uTSEnd);
171 if (RT_FAILURE(rc))
172 RTPrintf("warning: RTThreadSleep ended prematurely with %d\n", rc);
173 rc = RTTimerDestroy(pTimer);
174 if (RT_FAILURE(rc))
175 {
176 RTPrintf("tstTimer: FAILURE - RTTimerDestroy() -> %d gcTicks=%d\n", rc, gcTicks);
177 cErrors++;
178 continue;
179 }
180 unsigned cTicks = gcTicks;
181 RTThreadSleep(100);
182 if (gcTicks != cTicks)
183 {
184 RTPrintf("tstTimer: FAILURE - RTTimerDestroy() 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 * Summary.
209 */
210 if (!cErrors)
211 RTPrintf("tstTimer: SUCCESS\n");
212 else
213 RTPrintf("tstTimer: FAILURE %d errors\n", cErrors);
214 return !!cErrors;
215}
216
217
218
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette