VirtualBox

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

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

Biggest check-in ever. New source code headers for all (C) innotek files.

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