1 | /* $Id: tstTimer.cpp 48935 2013-10-07 21:19:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Timers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2011 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 | #include <iprt/string.h>
|
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Global Variables *
|
---|
43 | *******************************************************************************/
|
---|
44 | static volatile unsigned gcTicks;
|
---|
45 | static volatile uint64_t gu64Min;
|
---|
46 | static volatile uint64_t gu64Max;
|
---|
47 | static volatile uint64_t gu64Prev;
|
---|
48 | static volatile uint64_t gu64Norm;
|
---|
49 |
|
---|
50 | static uint32_t cFrequency[200];
|
---|
51 |
|
---|
52 | static DECLCALLBACK(void) TimerCallback(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
|
---|
53 | {
|
---|
54 | gcTicks++;
|
---|
55 |
|
---|
56 | const uint64_t u64Now = RTTimeNanoTS();
|
---|
57 | if (gu64Prev)
|
---|
58 | {
|
---|
59 | const uint64_t u64Delta = u64Now - gu64Prev;
|
---|
60 | if (u64Delta < gu64Min)
|
---|
61 | gu64Min = u64Delta;
|
---|
62 | if (u64Delta > gu64Max)
|
---|
63 | gu64Max = u64Delta;
|
---|
64 | int i = (int)( RT_ELEMENTS(cFrequency)
|
---|
65 | - (u64Delta * (RT_ELEMENTS(cFrequency) / 2) / gu64Norm));
|
---|
66 | if (i >= 0 && i < (int)RT_ELEMENTS(cFrequency))
|
---|
67 | cFrequency[i]++;
|
---|
68 | }
|
---|
69 | gu64Prev = u64Now;
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | int main()
|
---|
74 | {
|
---|
75 | /*
|
---|
76 | * Init runtime
|
---|
77 | */
|
---|
78 | unsigned cErrors = 0;
|
---|
79 | int rc = RTR3InitExeNoArguments(0);
|
---|
80 | if (RT_FAILURE(rc))
|
---|
81 | return RTMsgInitFailure(rc);
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * Check that the clock is reliable.
|
---|
85 | */
|
---|
86 | RTPrintf("tstTimer: TESTING - RTTimeNanoTS() for 2sec\n");
|
---|
87 | uint64_t uTSMillies = RTTimeMilliTS();
|
---|
88 | uint64_t uTSBegin = RTTimeNanoTS();
|
---|
89 | uint64_t uTSLast = uTSBegin;
|
---|
90 | uint64_t uTSDiff;
|
---|
91 | uint64_t cIterations = 0;
|
---|
92 |
|
---|
93 | do
|
---|
94 | {
|
---|
95 | uint64_t uTS = RTTimeNanoTS();
|
---|
96 | if (uTS < uTSLast)
|
---|
97 | {
|
---|
98 | RTPrintf("tstTimer: FAILURE - RTTimeNanoTS() is unreliable. uTS=%RU64 uTSLast=%RU64\n", uTS, uTSLast);
|
---|
99 | cErrors++;
|
---|
100 | }
|
---|
101 | if (++cIterations > (2*1000*1000*1000))
|
---|
102 | {
|
---|
103 | RTPrintf("tstTimer: FAILURE - RTTimeNanoTS() is unreliable. cIterations=%RU64 uTS=%RU64 uTSBegin=%RU64\n", cIterations, uTS, uTSBegin);
|
---|
104 | return 1;
|
---|
105 | }
|
---|
106 | uTSLast = uTS;
|
---|
107 | uTSDiff = uTSLast - uTSBegin;
|
---|
108 | } while (uTSDiff < (2*1000*1000*1000));
|
---|
109 | uTSMillies = RTTimeMilliTS() - uTSMillies;
|
---|
110 | if (uTSMillies >= 2500 || uTSMillies <= 1500)
|
---|
111 | {
|
---|
112 | RTPrintf("tstTimer: FAILURE - uTSMillies=%RI64 uTSBegin=%RU64 uTSLast=%RU64 uTSDiff=%RU64\n",
|
---|
113 | uTSMillies, uTSBegin, uTSLast, uTSDiff);
|
---|
114 | cErrors++;
|
---|
115 | }
|
---|
116 | if (!cErrors)
|
---|
117 | RTPrintf("tstTimer: OK - RTTimeNanoTS()\n");
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * Tests.
|
---|
121 | */
|
---|
122 | static struct
|
---|
123 | {
|
---|
124 | unsigned uMicroInterval;
|
---|
125 | unsigned uMilliesWait;
|
---|
126 | unsigned cLower;
|
---|
127 | unsigned cUpper;
|
---|
128 | } aTests[] =
|
---|
129 | {
|
---|
130 | { 32000, 2000, 0, 0 },
|
---|
131 | { 20000, 2000, 0, 0 },
|
---|
132 | { 10000, 2000, 0, 0 },
|
---|
133 | { 8000, 2000, 0, 0 },
|
---|
134 | { 2000, 2000, 0, 0 },
|
---|
135 | { 1000, 2000, 0, 0 },
|
---|
136 | { 500, 5000, 0, 0 },
|
---|
137 | { 200, 5000, 0, 0 },
|
---|
138 | { 100, 5000, 0, 0 }
|
---|
139 | };
|
---|
140 |
|
---|
141 | unsigned i = 0;
|
---|
142 | for (i = 0; i < RT_ELEMENTS(aTests); i++)
|
---|
143 | {
|
---|
144 | aTests[i].cLower = (aTests[i].uMilliesWait*1000 - aTests[i].uMilliesWait*100) / aTests[i].uMicroInterval;
|
---|
145 | aTests[i].cUpper = (aTests[i].uMilliesWait*1000 + aTests[i].uMilliesWait*100) / aTests[i].uMicroInterval;
|
---|
146 | gu64Norm = aTests[i].uMicroInterval*1000;
|
---|
147 |
|
---|
148 | RTPrintf("\n"
|
---|
149 | "tstTimer: TESTING - %d us interval, %d ms wait, expects %d-%d ticks.\n",
|
---|
150 | aTests[i].uMicroInterval, aTests[i].uMilliesWait, aTests[i].cLower, aTests[i].cUpper);
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * Start timer which ticks every 10ms.
|
---|
154 | */
|
---|
155 | gcTicks = 0;
|
---|
156 | PRTTIMER pTimer;
|
---|
157 | gu64Max = 0;
|
---|
158 | gu64Min = UINT64_MAX;
|
---|
159 | gu64Prev = 0;
|
---|
160 | RT_ZERO(cFrequency);
|
---|
161 | #ifdef RT_OS_WINDOWS
|
---|
162 | if (aTests[i].uMicroInterval < 1000)
|
---|
163 | continue;
|
---|
164 | rc = RTTimerCreate(&pTimer, aTests[i].uMicroInterval / 1000, TimerCallback, NULL);
|
---|
165 | #else
|
---|
166 | rc = RTTimerCreateEx(&pTimer, aTests[i].uMicroInterval * (uint64_t)1000, 0, TimerCallback, NULL);
|
---|
167 | #endif
|
---|
168 | if (RT_FAILURE(rc))
|
---|
169 | {
|
---|
170 | RTPrintf("tstTimer: FAILURE - RTTimerCreateEx(,%u*1M,,,) -> %Rrc\n", aTests[i].uMicroInterval, rc);
|
---|
171 | cErrors++;
|
---|
172 | continue;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /*
|
---|
176 | * Start the timer and active waiting for the requested test period.
|
---|
177 | */
|
---|
178 | uTSBegin = RTTimeNanoTS();
|
---|
179 | #ifndef RT_OS_WINDOWS
|
---|
180 | rc = RTTimerStart(pTimer, 0);
|
---|
181 | if (RT_FAILURE(rc))
|
---|
182 | {
|
---|
183 | RTPrintf("tstTimer: FAILURE - RTTimerStart(,0) -> %Rrc\n", aTests[i].uMicroInterval, rc);
|
---|
184 | cErrors++;
|
---|
185 | }
|
---|
186 | #endif
|
---|
187 |
|
---|
188 | while (RTTimeNanoTS() - uTSBegin < (uint64_t)aTests[i].uMilliesWait * 1000000)
|
---|
189 | /* nothing */;
|
---|
190 |
|
---|
191 | /* destroy the timer */
|
---|
192 | uint64_t uTSEnd = RTTimeNanoTS();
|
---|
193 | uTSDiff = uTSEnd - uTSBegin;
|
---|
194 | rc = RTTimerDestroy(pTimer);
|
---|
195 | if (RT_FAILURE(rc))
|
---|
196 | {
|
---|
197 | RTPrintf("tstTimer: FAILURE - RTTimerDestroy() -> %d gcTicks=%d\n", rc, gcTicks);
|
---|
198 | cErrors++;
|
---|
199 | }
|
---|
200 |
|
---|
201 | RTPrintf("tstTimer: uTS=%RI64 (%RU64 - %RU64)\n", uTSDiff, uTSBegin, uTSEnd);
|
---|
202 | unsigned cTicks = gcTicks;
|
---|
203 | RTThreadSleep(aTests[i].uMicroInterval/1000 * 3);
|
---|
204 | if (gcTicks != cTicks)
|
---|
205 | {
|
---|
206 | RTPrintf("tstTimer: FAILURE - RTTimerDestroy() didn't really stop the timer! gcTicks=%d cTicks=%d\n", gcTicks, cTicks);
|
---|
207 | cErrors++;
|
---|
208 | continue;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /*
|
---|
212 | * Check the number of ticks.
|
---|
213 | */
|
---|
214 | if (gcTicks < aTests[i].cLower)
|
---|
215 | {
|
---|
216 | RTPrintf("tstTimer: FAILURE - Too few ticks gcTicks=%d (expected %d-%d)", gcTicks, aTests[i].cUpper, aTests[i].cLower);
|
---|
217 | cErrors++;
|
---|
218 | }
|
---|
219 | else if (gcTicks > aTests[i].cUpper)
|
---|
220 | {
|
---|
221 | RTPrintf("tstTimer: FAILURE - Too many ticks gcTicks=%d (expected %d-%d)", gcTicks, aTests[i].cUpper, aTests[i].cLower);
|
---|
222 | cErrors++;
|
---|
223 | }
|
---|
224 | else
|
---|
225 | RTPrintf("tstTimer: OK - gcTicks=%d", gcTicks);
|
---|
226 | RTPrintf(" min=%RU64 max=%RU64\n", gu64Min, gu64Max);
|
---|
227 |
|
---|
228 | for (int j = 0; j < (int)RT_ELEMENTS(cFrequency); j++)
|
---|
229 | {
|
---|
230 | uint32_t len = cFrequency[j] * 70 / gcTicks;
|
---|
231 | uint32_t deviation = j - RT_ELEMENTS(cFrequency) / 2;
|
---|
232 | uint64_t u64FreqPercent = (uint64_t)cFrequency[j] * 10000 / gcTicks;
|
---|
233 | uint64_t u64FreqPercentFrac = u64FreqPercent % 100;
|
---|
234 | u64FreqPercent = u64FreqPercent / 100;
|
---|
235 | RTPrintf("%+4d%c %6u %3llu.%02llu%% ",
|
---|
236 | deviation, deviation == 0 ? ' ' : '%', cFrequency[j],
|
---|
237 | u64FreqPercent, u64FreqPercentFrac);
|
---|
238 | for (unsigned k = 0; k < len; k++)
|
---|
239 | RTPrintf("*");
|
---|
240 | RTPrintf("\n");
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | /*
|
---|
245 | * Summary.
|
---|
246 | */
|
---|
247 | if (!cErrors)
|
---|
248 | RTPrintf("tstTimer: SUCCESS\n");
|
---|
249 | else
|
---|
250 | RTPrintf("tstTimer: FAILURE %d errors\n", cErrors);
|
---|
251 | return !!cErrors;
|
---|
252 | }
|
---|
253 |
|
---|