1 | /* $Id: tstRTR0Timer.cpp 106924 2024-11-11 11:57:12Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT R0 Testcase - Timers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/timer.h>
|
---|
42 |
|
---|
43 | #include <iprt/asm.h>
|
---|
44 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
45 | # include <iprt/asm-amd64-x86.h>
|
---|
46 | #elif defined(RT_ARCH_ARM64)
|
---|
47 | # include <iprt/asm-arm.h>
|
---|
48 | #endif
|
---|
49 | #include <iprt/cpuset.h>
|
---|
50 | #include <iprt/err.h>
|
---|
51 | #include <iprt/mem.h>
|
---|
52 | #include <iprt/mp.h>
|
---|
53 | #include <iprt/param.h>
|
---|
54 | #include <iprt/string.h>
|
---|
55 | #include <iprt/thread.h>
|
---|
56 | #include <iprt/time.h>
|
---|
57 | #include <VBox/sup.h>
|
---|
58 | #include "tstRTR0Timer.h"
|
---|
59 | #include "tstRTR0Common.h"
|
---|
60 |
|
---|
61 |
|
---|
62 | /*********************************************************************************************************************************
|
---|
63 | * Structures and Typedefs *
|
---|
64 | *********************************************************************************************************************************/
|
---|
65 | typedef struct
|
---|
66 | {
|
---|
67 | /** Array of nano second timestamp of the first few shots. */
|
---|
68 | uint64_t volatile aShotNsTSes[10];
|
---|
69 | /** The number of shots. */
|
---|
70 | uint32_t volatile cShots;
|
---|
71 | /** The shot at which action is to be taken. */
|
---|
72 | uint32_t iActionShot;
|
---|
73 | /** The RC of whatever operation performed in the handler. */
|
---|
74 | int volatile rc;
|
---|
75 | /** Set if it's a periodic test. */
|
---|
76 | bool fPeriodic;
|
---|
77 | /** Test specific stuff. */
|
---|
78 | union
|
---|
79 | {
|
---|
80 | /** tstRTR0TimerCallbackU32ChangeInterval parameters. */
|
---|
81 | struct
|
---|
82 | {
|
---|
83 | /** The interval change step. */
|
---|
84 | uint32_t cNsChangeStep;
|
---|
85 | /** The current timer interval. */
|
---|
86 | uint32_t cNsCurInterval;
|
---|
87 | /** The minimum interval. */
|
---|
88 | uint32_t cNsMinInterval;
|
---|
89 | /** The maximum interval. */
|
---|
90 | uint32_t cNsMaxInterval;
|
---|
91 | /** Direction flag; false = decrement, true = increment. */
|
---|
92 | bool fDirection;
|
---|
93 | /** The number of steps between each change. */
|
---|
94 | uint8_t cStepsBetween;
|
---|
95 | } ChgInt;
|
---|
96 | /** tstRTR0TimerCallbackSpecific parameters. */
|
---|
97 | struct
|
---|
98 | {
|
---|
99 | /** The expected CPU. */
|
---|
100 | RTCPUID idCpu;
|
---|
101 | /** Set if this failed. */
|
---|
102 | bool fFailed;
|
---|
103 | } Specific;
|
---|
104 | } u;
|
---|
105 | } TSTRTR0TIMERS1;
|
---|
106 | typedef TSTRTR0TIMERS1 *PTSTRTR0TIMERS1;
|
---|
107 |
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Per cpu state for an omni timer test.
|
---|
111 | */
|
---|
112 | typedef struct TSTRTR0TIMEROMNI1
|
---|
113 | {
|
---|
114 | /** When we started receiving timer callbacks on this CPU. */
|
---|
115 | uint64_t u64Start;
|
---|
116 | /** When we received the last tick on this timer. */
|
---|
117 | uint64_t u64Last;
|
---|
118 | /** The number of ticks received on this CPU. */
|
---|
119 | uint32_t volatile cTicks;
|
---|
120 | uint32_t u32Padding;
|
---|
121 | } TSTRTR0TIMEROMNI1;
|
---|
122 | typedef TSTRTR0TIMEROMNI1 *PTSTRTR0TIMEROMNI1;
|
---|
123 |
|
---|
124 |
|
---|
125 | /*********************************************************************************************************************************
|
---|
126 | * Global Variables *
|
---|
127 | *********************************************************************************************************************************/
|
---|
128 | /**
|
---|
129 | * Latency data.
|
---|
130 | */
|
---|
131 | static struct TSTRTR0TIMEROMNILATENCY
|
---|
132 | {
|
---|
133 | /** The number of samples. */
|
---|
134 | volatile uint32_t cSamples;
|
---|
135 | uint32_t auPadding[3];
|
---|
136 | struct
|
---|
137 | {
|
---|
138 | uint64_t uTsc;
|
---|
139 | uint64_t uNanoTs;
|
---|
140 | } aSamples[4096];
|
---|
141 | } g_aOmniLatency[16];
|
---|
142 |
|
---|
143 |
|
---|
144 |
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Callback for the omni timer latency test, adds a sample to g_aOmniLatency.
|
---|
148 | *
|
---|
149 | * @param pTimer The timer.
|
---|
150 | * @param iTick The current tick.
|
---|
151 | * @param pvUser The user argument.
|
---|
152 | */
|
---|
153 | static DECLCALLBACK(void) tstRTR0TimerCallbackLatencyOmni(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
|
---|
154 | {
|
---|
155 | RTCPUID idCpu = RTMpCpuId();
|
---|
156 | uint32_t iCpu = RTMpCpuIdToSetIndex(idCpu);
|
---|
157 | NOREF(pTimer); NOREF(pvUser); NOREF(iTick);
|
---|
158 |
|
---|
159 | //RTR0TESTR0_CHECK_MSG(iCpu < RT_ELEMENTS(g_aOmniLatency), ("iCpu=%d idCpu=%u\n", iCpu, idCpu));
|
---|
160 | if (iCpu < RT_ELEMENTS(g_aOmniLatency))
|
---|
161 | {
|
---|
162 | uint32_t iSample = g_aOmniLatency[iCpu].cSamples;
|
---|
163 | if (iSample < RT_ELEMENTS(g_aOmniLatency[iCpu].aSamples))
|
---|
164 | {
|
---|
165 | g_aOmniLatency[iCpu].aSamples[iSample].uTsc = ASMReadTSC();
|
---|
166 | g_aOmniLatency[iCpu].aSamples[iSample].uNanoTs = RTTimeSystemNanoTS();
|
---|
167 | g_aOmniLatency[iCpu].cSamples = iSample + 1;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Callback which increments a 32-bit counter.
|
---|
175 | *
|
---|
176 | * @param pTimer The timer.
|
---|
177 | * @param iTick The current tick.
|
---|
178 | * @param pvUser The user argument.
|
---|
179 | */
|
---|
180 | static DECLCALLBACK(void) tstRTR0TimerCallbackOmni(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
|
---|
181 | {
|
---|
182 | PTSTRTR0TIMEROMNI1 paStates = (PTSTRTR0TIMEROMNI1)pvUser;
|
---|
183 | RTCPUID idCpu = RTMpCpuId();
|
---|
184 | uint32_t iCpu = RTMpCpuIdToSetIndex(idCpu);
|
---|
185 | NOREF(pTimer);
|
---|
186 |
|
---|
187 | RTR0TESTR0_CHECK_MSG(iCpu < RTCPUSET_MAX_CPUS, ("iCpu=%d idCpu=%u\n", iCpu, idCpu));
|
---|
188 | if (iCpu < RTCPUSET_MAX_CPUS)
|
---|
189 | {
|
---|
190 | uint32_t iCountedTick = ASMAtomicIncU32(&paStates[iCpu].cTicks);
|
---|
191 | RTR0TESTR0_CHECK_MSG(iCountedTick == iTick,
|
---|
192 | ("iCountedTick=%u iTick=%u iCpu=%d idCpu=%u\n", iCountedTick, iTick, iCpu, idCpu));
|
---|
193 | paStates[iCpu].u64Last = RTTimeSystemNanoTS();
|
---|
194 | if (!paStates[iCpu].u64Start)
|
---|
195 | {
|
---|
196 | paStates[iCpu].u64Start = paStates[iCpu].u64Last;
|
---|
197 | RTR0TESTR0_CHECK_MSG(iCountedTick == 1, ("iCountedTick=%u iCpu=%d idCpu=%u\n", iCountedTick, iCpu, idCpu));
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Callback for one-shot resolution detection.
|
---|
205 | *
|
---|
206 | * @param pTimer The timer.
|
---|
207 | * @param iTick The current tick.
|
---|
208 | * @param pvUser Points to variable with the start TS, update to time
|
---|
209 | * elapsed till this call.
|
---|
210 | */
|
---|
211 | static DECLCALLBACK(void) tstRTR0TimerCallbackOneShotElapsed(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
|
---|
212 | {
|
---|
213 | RT_NOREF(pTimer, iTick);
|
---|
214 | uint64_t *puNanoTS = (uint64_t *)pvUser;
|
---|
215 | *puNanoTS = RTTimeSystemNanoTS() - *puNanoTS;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Callback which increments a 32-bit counter.
|
---|
221 | *
|
---|
222 | * @param pTimer The timer.
|
---|
223 | * @param iTick The current tick.
|
---|
224 | * @param pvUser The user argument.
|
---|
225 | */
|
---|
226 | static DECLCALLBACK(void) tstRTR0TimerCallbackSpecific(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
|
---|
227 | {
|
---|
228 | PTSTRTR0TIMERS1 pState = (PTSTRTR0TIMERS1)pvUser;
|
---|
229 | uint32_t iShot = ASMAtomicIncU32(&pState->cShots);
|
---|
230 | NOREF(pTimer);
|
---|
231 |
|
---|
232 | if (iShot <= RT_ELEMENTS(pState->aShotNsTSes))
|
---|
233 | pState->aShotNsTSes[iShot - 1] = RTTimeSystemNanoTS();
|
---|
234 |
|
---|
235 | RTCPUID idCpu = RTMpCpuId();
|
---|
236 | if (pState->u.Specific.idCpu != idCpu)
|
---|
237 | pState->u.Specific.fFailed = true;
|
---|
238 | RTR0TESTR0_CHECK_MSG(pState->u.Specific.idCpu == idCpu, ("idCpu=%u, expected %u\n", idCpu, pState->u.Specific.idCpu));
|
---|
239 |
|
---|
240 | if (pState->fPeriodic)
|
---|
241 | RTR0TESTR0_CHECK_MSG(iShot == iTick, ("iShot=%u iTick=%u\n", iShot, iTick));
|
---|
242 | else
|
---|
243 | RTR0TESTR0_CHECK_MSG(iTick == 1, ("iShot=%u iTick=%u\n", iShot, iTick));
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Callback which changes the interval at each invocation.
|
---|
249 | *
|
---|
250 | * The changes are governed by TSTRTR0TIMERS1::ChangeInterval. The callback
|
---|
251 | * calls RTTimerStop at iActionShot.
|
---|
252 | *
|
---|
253 | * @param pTimer The timer.
|
---|
254 | * @param iTick The current tick.
|
---|
255 | * @param pvUser The user argument.
|
---|
256 | */
|
---|
257 | static DECLCALLBACK(void) tstRTR0TimerCallbackChangeInterval(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
|
---|
258 | {
|
---|
259 | PTSTRTR0TIMERS1 pState = (PTSTRTR0TIMERS1)pvUser;
|
---|
260 | uint32_t iShot = ASMAtomicIncU32(&pState->cShots) - 1;
|
---|
261 |
|
---|
262 | if (iShot < RT_ELEMENTS(pState->aShotNsTSes))
|
---|
263 | pState->aShotNsTSes[iShot] = RTTimeSystemNanoTS();
|
---|
264 | if (pState->fPeriodic)
|
---|
265 | RTR0TESTR0_CHECK_MSG(iShot + 1 == iTick, ("iShot=%u iTick=%u\n", iShot, iTick));
|
---|
266 | else
|
---|
267 | RTR0TESTR0_CHECK_MSG(iTick == 1, ("iShot=%u iTick=%u\n", iShot, iTick));
|
---|
268 |
|
---|
269 | if (!(iShot % pState->u.ChgInt.cStepsBetween))
|
---|
270 | {
|
---|
271 | if (pState->u.ChgInt.fDirection)
|
---|
272 | {
|
---|
273 | pState->u.ChgInt.cNsCurInterval += pState->u.ChgInt.cNsChangeStep;
|
---|
274 | if ( pState->u.ChgInt.cNsCurInterval > pState->u.ChgInt.cNsMaxInterval
|
---|
275 | || pState->u.ChgInt.cNsCurInterval < pState->u.ChgInt.cNsMinInterval
|
---|
276 | || !pState->u.ChgInt.cNsCurInterval)
|
---|
277 | {
|
---|
278 | pState->u.ChgInt.cNsCurInterval = pState->u.ChgInt.cNsMaxInterval;
|
---|
279 | pState->u.ChgInt.fDirection = false;
|
---|
280 | }
|
---|
281 | }
|
---|
282 | else
|
---|
283 | {
|
---|
284 | pState->u.ChgInt.cNsCurInterval -= pState->u.ChgInt.cNsChangeStep;
|
---|
285 | if ( pState->u.ChgInt.cNsCurInterval < pState->u.ChgInt.cNsMinInterval
|
---|
286 | || pState->u.ChgInt.cNsCurInterval > pState->u.ChgInt.cNsMaxInterval
|
---|
287 | || pState->u.ChgInt.cNsCurInterval)
|
---|
288 | {
|
---|
289 | pState->u.ChgInt.cNsCurInterval = pState->u.ChgInt.cNsMinInterval;
|
---|
290 | pState->u.ChgInt.fDirection = true;
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | RTR0TESTR0_CHECK_RC(RTTimerChangeInterval(pTimer, pState->u.ChgInt.cNsCurInterval), VINF_SUCCESS);
|
---|
295 | }
|
---|
296 |
|
---|
297 | if (iShot == pState->iActionShot)
|
---|
298 | RTR0TESTR0_CHECK_RC(pState->rc = RTTimerStop(pTimer), VINF_SUCCESS);
|
---|
299 | }
|
---|
300 |
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * Callback which increments destroy the timer when it fires.
|
---|
304 | *
|
---|
305 | * @param pTimer The timer.
|
---|
306 | * @param iTick The current tick.
|
---|
307 | * @param pvUser The user argument.
|
---|
308 | */
|
---|
309 | static DECLCALLBACK(void) tstRTR0TimerCallbackDestroyOnce(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
|
---|
310 | {
|
---|
311 | PTSTRTR0TIMERS1 pState = (PTSTRTR0TIMERS1)pvUser;
|
---|
312 | uint32_t iShot = ASMAtomicIncU32(&pState->cShots);
|
---|
313 |
|
---|
314 | if (iShot <= RT_ELEMENTS(pState->aShotNsTSes))
|
---|
315 | pState->aShotNsTSes[iShot - 1] = RTTimeSystemNanoTS();
|
---|
316 | if (pState->fPeriodic)
|
---|
317 | RTR0TESTR0_CHECK_MSG(iShot == iTick, ("iShot=%u iTick=%u\n", iShot, iTick));
|
---|
318 | else
|
---|
319 | RTR0TESTR0_CHECK_MSG(iTick == 1, ("iShot=%u iTick=%u\n", iShot, iTick));
|
---|
320 |
|
---|
321 | if (iShot == pState->iActionShot + 1)
|
---|
322 | RTR0TESTR0_CHECK_RC(pState->rc = RTTimerDestroy(pTimer), VINF_SUCCESS);
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Callback which increments restarts a timer once.
|
---|
328 | *
|
---|
329 | * @param pTimer The timer.
|
---|
330 | * @param iTick The current tick.
|
---|
331 | * @param pvUser The user argument.
|
---|
332 | */
|
---|
333 | static DECLCALLBACK(void) tstRTR0TimerCallbackRestartOnce(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
|
---|
334 | {
|
---|
335 | PTSTRTR0TIMERS1 pState = (PTSTRTR0TIMERS1)pvUser;
|
---|
336 | uint32_t iShot = ASMAtomicIncU32(&pState->cShots);
|
---|
337 |
|
---|
338 | if (iShot <= RT_ELEMENTS(pState->aShotNsTSes))
|
---|
339 | pState->aShotNsTSes[iShot - 1] = RTTimeSystemNanoTS();
|
---|
340 | if (pState->fPeriodic)
|
---|
341 | RTR0TESTR0_CHECK_MSG(iShot == iTick, ("iShot=%u iTick=%u\n", iShot, iTick));
|
---|
342 | else
|
---|
343 | RTR0TESTR0_CHECK_MSG(iTick == 1, ("iShot=%u iTick=%u\n", iShot, iTick));
|
---|
344 |
|
---|
345 | if (iShot == pState->iActionShot + 1)
|
---|
346 | RTR0TESTR0_CHECK_RC(pState->rc = RTTimerStart(pTimer, 10000000 /* 10ms */), VINF_SUCCESS);
|
---|
347 | }
|
---|
348 |
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Callback which increments a 32-bit counter.
|
---|
352 | *
|
---|
353 | * @param pTimer The timer.
|
---|
354 | * @param iTick The current tick.
|
---|
355 | * @param pvUser The user argument.
|
---|
356 | */
|
---|
357 | static DECLCALLBACK(void) tstRTR0TimerCallbackU32Counter(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
|
---|
358 | {
|
---|
359 | PTSTRTR0TIMERS1 pState = (PTSTRTR0TIMERS1)pvUser;
|
---|
360 | uint32_t iShot = ASMAtomicIncU32(&pState->cShots);
|
---|
361 | NOREF(pTimer);
|
---|
362 |
|
---|
363 | if (iShot <= RT_ELEMENTS(pState->aShotNsTSes))
|
---|
364 | pState->aShotNsTSes[iShot - 1] = RTTimeSystemNanoTS();
|
---|
365 | if (pState->fPeriodic)
|
---|
366 | RTR0TESTR0_CHECK_MSG(iShot == iTick, ("iShot=%u iTick=%u\n", iShot, iTick));
|
---|
367 | else
|
---|
368 | RTR0TESTR0_CHECK_MSG(iTick == 1, ("iShot=%u iTick=%u\n", iShot, iTick));
|
---|
369 | }
|
---|
370 |
|
---|
371 |
|
---|
372 | #ifdef SOME_UNUSED_FUNCTION
|
---|
373 | /**
|
---|
374 | * Checks that the interval between two timer shots are within the specified
|
---|
375 | * range.
|
---|
376 | *
|
---|
377 | * @returns 0 if ok, 1 if bad.
|
---|
378 | * @param iShot The shot number (for bitching).
|
---|
379 | * @param uPrevTS The time stamp of the previous shot (ns).
|
---|
380 | * @param uThisTS The timer stamp of this shot (ns).
|
---|
381 | * @param uMin The minimum interval (ns).
|
---|
382 | * @param uMax The maximum interval (ns).
|
---|
383 | */
|
---|
384 | static int tstRTR0TimerCheckShotInterval(uint32_t iShot, uint64_t uPrevTS, uint64_t uThisTS, uint32_t uMin, uint32_t uMax)
|
---|
385 | {
|
---|
386 | uint64_t uDelta = uThisTS - uPrevTS;
|
---|
387 | RTR0TESTR0_CHECK_MSG_RET(uDelta >= uMin, ("iShot=%u uDelta=%lld uMin=%u\n", iShot, uDelta, uMin), 1);
|
---|
388 | RTR0TESTR0_CHECK_MSG_RET(uDelta <= uMax, ("iShot=%u uDelta=%lld uMax=%u\n", iShot, uDelta, uMax), 1);
|
---|
389 | return 0;
|
---|
390 | }
|
---|
391 | #endif
|
---|
392 |
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Checks that the interval between timer shots are within a certain range.
|
---|
396 | *
|
---|
397 | * @returns Number of violations (i.e. 0 is ok).
|
---|
398 | * @param pState The state.
|
---|
399 | * @param uStartNsTS The start time stamp (ns).
|
---|
400 | * @param uMin The minimum interval (ns).
|
---|
401 | * @param uMax The maximum interval (ns).
|
---|
402 | */
|
---|
403 | static int tstRTR0TimerCheckShotIntervals(PTSTRTR0TIMERS1 pState, uint64_t uStartNsTS, uint32_t uMin, uint32_t uMax)
|
---|
404 | {
|
---|
405 | uint64_t uMaxDelta = 0;
|
---|
406 | uint64_t uMinDelta = UINT64_MAX;
|
---|
407 | uint32_t cBadShots = 0;
|
---|
408 | uint32_t cShots = pState->cShots;
|
---|
409 | uint64_t uPrevTS = uStartNsTS;
|
---|
410 | for (uint32_t iShot = 0; iShot < cShots; iShot++)
|
---|
411 | {
|
---|
412 | uint64_t uThisTS = pState->aShotNsTSes[iShot];
|
---|
413 | uint64_t uDelta = uThisTS - uPrevTS;
|
---|
414 | if (uDelta > uMaxDelta)
|
---|
415 | uMaxDelta = uDelta;
|
---|
416 | if (uDelta < uMinDelta)
|
---|
417 | uMinDelta = uDelta;
|
---|
418 | cBadShots += !(uDelta >= uMin && uDelta <= uMax);
|
---|
419 |
|
---|
420 | RTR0TESTR0_CHECK_MSG(uDelta >= uMin, ("iShot=%u uDelta=%lld uMin=%u\n", iShot, uDelta, uMin));
|
---|
421 | RTR0TESTR0_CHECK_MSG(uDelta <= uMax, ("iShot=%u uDelta=%lld uMax=%u\n", iShot, uDelta, uMax));
|
---|
422 |
|
---|
423 | uPrevTS = uThisTS;
|
---|
424 | }
|
---|
425 |
|
---|
426 | RTR0TestR0Info("uMaxDelta=%llu uMinDelta=%llu\n", uMaxDelta, uMinDelta);
|
---|
427 | return cBadShots;
|
---|
428 | }
|
---|
429 |
|
---|
430 |
|
---|
431 | /**
|
---|
432 | * Service request callback function.
|
---|
433 | *
|
---|
434 | * @returns VBox status code.
|
---|
435 | * @param pSession The caller's session.
|
---|
436 | * @param u64Arg 64-bit integer argument.
|
---|
437 | * @param pReqHdr The request header. Input / Output. Optional.
|
---|
438 | */
|
---|
439 | DECLEXPORT(int) TSTRTR0TimerSrvReqHandler(PSUPDRVSESSION pSession, uint32_t uOperation,
|
---|
440 | uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr)
|
---|
441 | {
|
---|
442 | RTR0TESTR0_SRV_REQ_PROLOG_RET(pReqHdr);
|
---|
443 | NOREF(pSession);
|
---|
444 |
|
---|
445 | /*
|
---|
446 | * Common parameter and state variables.
|
---|
447 | */
|
---|
448 | uint32_t const cNsSysHz = RTTimerGetSystemGranularity();
|
---|
449 | uint32_t const cNsMaxHighResHz = 10000; /** @todo need API for this */
|
---|
450 | TSTRTR0TIMERS1 State;
|
---|
451 | if ( cNsSysHz < UINT32_C(1000)
|
---|
452 | || cNsSysHz > UINT32_C(1000000000)
|
---|
453 | || cNsMaxHighResHz < UINT32_C(1)
|
---|
454 | || cNsMaxHighResHz > UINT32_C(1000000000))
|
---|
455 | {
|
---|
456 | RTR0TESTR0_CHECK_MSG(cNsSysHz > UINT32_C(1000) && cNsSysHz < UINT32_C(1000000000), ("%u", cNsSysHz));
|
---|
457 | RTR0TESTR0_CHECK_MSG(cNsMaxHighResHz > UINT32_C(1) && cNsMaxHighResHz < UINT32_C(1000000000), ("%u", cNsMaxHighResHz));
|
---|
458 | RTR0TESTR0_SRV_REQ_EPILOG(pReqHdr);
|
---|
459 | return VINF_SUCCESS;
|
---|
460 | }
|
---|
461 |
|
---|
462 | /*
|
---|
463 | * The big switch.
|
---|
464 | */
|
---|
465 | switch (uOperation)
|
---|
466 | {
|
---|
467 | RTR0TESTR0_IMPLEMENT_SANITY_CASES();
|
---|
468 | RTR0TESTR0_IMPLEMENT_DEFAULT_CASE(uOperation);
|
---|
469 |
|
---|
470 | case TSTRTR0TIMER_ONE_SHOT_BASIC:
|
---|
471 | case TSTRTR0TIMER_ONE_SHOT_BASIC_HIRES:
|
---|
472 | {
|
---|
473 | /* Create a one-shot timer and take one shot. */
|
---|
474 | PRTTIMER pTimer;
|
---|
475 | uint32_t fFlags = TSTRTR0TIMER_IS_HIRES(uOperation) ? RTTIMER_FLAGS_HIGH_RES : 0;
|
---|
476 | int rc = RTTimerCreateEx(&pTimer, 0, fFlags, tstRTR0TimerCallbackU32Counter, &State);
|
---|
477 | if (rc == VERR_NOT_SUPPORTED)
|
---|
478 | {
|
---|
479 | RTR0TestR0Info("one-shot timer are not supported, skipping\n");
|
---|
480 | RTR0TESTR0_SKIP();
|
---|
481 | break;
|
---|
482 | }
|
---|
483 | RTR0TESTR0_CHECK_RC_BREAK(rc, VINF_SUCCESS);
|
---|
484 |
|
---|
485 | do /* break loop */
|
---|
486 | {
|
---|
487 | RT_ZERO(State); ASMAtomicWriteU32(&State.cShots, State.cShots);
|
---|
488 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStart(pTimer, 0), VINF_SUCCESS);
|
---|
489 | for (uint32_t i = 0; i < 1000 && !ASMAtomicUoReadU32(&State.cShots); i++)
|
---|
490 | RTThreadSleep(5);
|
---|
491 | RTR0TESTR0_CHECK_MSG_BREAK(ASMAtomicUoReadU32(&State.cShots) == 1, ("cShots=%u\n", State.cShots));
|
---|
492 |
|
---|
493 | /* check that it is restartable. */
|
---|
494 | RT_ZERO(State); ASMAtomicWriteU32(&State.cShots, State.cShots);
|
---|
495 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStart(pTimer, 0), VINF_SUCCESS);
|
---|
496 | for (uint32_t i = 0; i < 1000 && !ASMAtomicUoReadU32(&State.cShots); i++)
|
---|
497 | RTThreadSleep(5);
|
---|
498 | RTR0TESTR0_CHECK_MSG_BREAK(ASMAtomicUoReadU32(&State.cShots) == 1, ("cShots=%u\n", State.cShots));
|
---|
499 |
|
---|
500 | /* check that it respects the timeout value and can be cancelled. */
|
---|
501 | RT_ZERO(State); ASMAtomicWriteU32(&State.cShots, State.cShots);
|
---|
502 | RTR0TESTR0_CHECK_RC(RTTimerStart(pTimer, 5*UINT64_C(1000000000)), VINF_SUCCESS);
|
---|
503 | RTR0TESTR0_CHECK_RC(RTTimerStop(pTimer), VINF_SUCCESS);
|
---|
504 | RTThreadSleep(1);
|
---|
505 | RTR0TESTR0_CHECK_MSG_BREAK(ASMAtomicUoReadU32(&State.cShots) == 0, ("cShots=%u\n", State.cShots));
|
---|
506 |
|
---|
507 | /* Check some double starts and stops (shall not assert). */
|
---|
508 | RT_ZERO(State); ASMAtomicWriteU32(&State.cShots, State.cShots);
|
---|
509 | RTR0TESTR0_CHECK_RC(RTTimerStart(pTimer, 5*UINT64_C(1000000000)), VINF_SUCCESS);
|
---|
510 | RTR0TESTR0_CHECK_RC(RTTimerStart(pTimer, 0), VERR_TIMER_ACTIVE);
|
---|
511 | RTR0TESTR0_CHECK_RC(RTTimerStop(pTimer), VINF_SUCCESS);
|
---|
512 | RTR0TESTR0_CHECK_RC(RTTimerStop(pTimer), VERR_TIMER_SUSPENDED);
|
---|
513 | RTThreadSleep(1);
|
---|
514 | RTR0TESTR0_CHECK_MSG_BREAK(ASMAtomicUoReadU32(&State.cShots) == 0, ("cShots=%u\n", State.cShots));
|
---|
515 | } while (0);
|
---|
516 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(pTimer), VINF_SUCCESS);
|
---|
517 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(NULL), VINF_SUCCESS);
|
---|
518 | break;
|
---|
519 | }
|
---|
520 |
|
---|
521 | case TSTRTR0TIMER_ONE_SHOT_RESTART:
|
---|
522 | case TSTRTR0TIMER_ONE_SHOT_RESTART_HIRES:
|
---|
523 | {
|
---|
524 | #if !defined(RT_OS_SOLARIS) /* Not expected to work on all hosts. */
|
---|
525 | /* Create a one-shot timer and restart it in the callback handler. */
|
---|
526 | PRTTIMER pTimer;
|
---|
527 | uint32_t fFlags = TSTRTR0TIMER_IS_HIRES(uOperation) ? RTTIMER_FLAGS_HIGH_RES : 0;
|
---|
528 | for (uint32_t iTest = 0; iTest < 2; iTest++)
|
---|
529 | {
|
---|
530 | int rc = RTTimerCreateEx(&pTimer, 0, fFlags, tstRTR0TimerCallbackRestartOnce, &State);
|
---|
531 | if (rc == VERR_NOT_SUPPORTED)
|
---|
532 | {
|
---|
533 | RTR0TestR0Info("one-shot timer are not supported, skipping\n");
|
---|
534 | RTR0TESTR0_SKIP();
|
---|
535 | break;
|
---|
536 | }
|
---|
537 | RTR0TESTR0_CHECK_RC_BREAK(rc, VINF_SUCCESS);
|
---|
538 |
|
---|
539 | RT_ZERO(State);
|
---|
540 | State.iActionShot = 0;
|
---|
541 | ASMAtomicWriteU32(&State.cShots, State.cShots);
|
---|
542 | do /* break loop */
|
---|
543 | {
|
---|
544 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStart(pTimer, cNsSysHz * iTest), VINF_SUCCESS);
|
---|
545 | for (uint32_t i = 0; i < 1000 && ASMAtomicUoReadU32(&State.cShots) < 2; i++)
|
---|
546 | RTThreadSleep(5);
|
---|
547 | RTR0TESTR0_CHECK_MSG_BREAK(ASMAtomicUoReadU32(&State.cShots) == 2, ("cShots=%u\n", State.cShots));
|
---|
548 | } while (0);
|
---|
549 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(pTimer), VINF_SUCCESS);
|
---|
550 | }
|
---|
551 | #else
|
---|
552 | RTR0TestR0Info("restarting from callback not supported on this platform\n");
|
---|
553 | RTR0TESTR0_SKIP();
|
---|
554 | #endif
|
---|
555 | break;
|
---|
556 | }
|
---|
557 |
|
---|
558 | case TSTRTR0TIMER_ONE_SHOT_DESTROY:
|
---|
559 | case TSTRTR0TIMER_ONE_SHOT_DESTROY_HIRES:
|
---|
560 | {
|
---|
561 | #if !defined(RT_OS_SOLARIS) && !defined(RT_OS_WINDOWS) /* Not expected to work on all hosts. */
|
---|
562 | /* Create a one-shot timer and destroy it in the callback handler. */
|
---|
563 | PRTTIMER pTimer;
|
---|
564 | uint32_t fFlags = TSTRTR0TIMER_IS_HIRES(uOperation) ? RTTIMER_FLAGS_HIGH_RES : 0;
|
---|
565 | for (uint32_t iTest = 0; iTest < 2; iTest++)
|
---|
566 | {
|
---|
567 | int rc = RTTimerCreateEx(&pTimer, 0, fFlags, tstRTR0TimerCallbackDestroyOnce, &State);
|
---|
568 | if (rc == VERR_NOT_SUPPORTED)
|
---|
569 | {
|
---|
570 | RTR0TestR0Info("one-shot timer are not supported, skipping\n");
|
---|
571 | RTR0TESTR0_SKIP();
|
---|
572 | break;
|
---|
573 | }
|
---|
574 | RTR0TESTR0_CHECK_RC_BREAK(rc, VINF_SUCCESS);
|
---|
575 |
|
---|
576 | RT_ZERO(State);
|
---|
577 | State.rc = VERR_IPE_UNINITIALIZED_STATUS;
|
---|
578 | State.iActionShot = 0;
|
---|
579 | ASMAtomicWriteU32(&State.cShots, State.cShots);
|
---|
580 | do /* break loop */
|
---|
581 | {
|
---|
582 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStart(pTimer, cNsSysHz * iTest), VINF_SUCCESS);
|
---|
583 | for (uint32_t i = 0; i < 1000 && (ASMAtomicUoReadU32(&State.cShots) < 1 || State.rc == VERR_IPE_UNINITIALIZED_STATUS); i++)
|
---|
584 | RTThreadSleep(5);
|
---|
585 | RTR0TESTR0_CHECK_MSG_BREAK(ASMAtomicReadU32(&State.cShots) == 1, ("cShots=%u\n", State.cShots));
|
---|
586 | RTR0TESTR0_CHECK_MSG_BREAK(State.rc == VINF_SUCCESS, ("rc=%Rrc\n", State.rc));
|
---|
587 | } while (0);
|
---|
588 | if (RT_FAILURE(State.rc))
|
---|
589 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(pTimer), VINF_SUCCESS);
|
---|
590 | }
|
---|
591 | #else
|
---|
592 | RTR0TestR0Info("destroying from callback not supported on this platform\n");
|
---|
593 | RTR0TESTR0_SKIP();
|
---|
594 | #endif
|
---|
595 | break;
|
---|
596 | }
|
---|
597 |
|
---|
598 | case TSTRTR0TIMER_ONE_SHOT_SPECIFIC:
|
---|
599 | case TSTRTR0TIMER_ONE_SHOT_SPECIFIC_HIRES:
|
---|
600 | {
|
---|
601 | PRTTIMER pTimer = NULL;
|
---|
602 | RTCPUSET OnlineSet;
|
---|
603 | RTMpGetOnlineSet(&OnlineSet);
|
---|
604 | for (uint32_t iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
|
---|
605 | if (RTCpuSetIsMemberByIndex(&OnlineSet, iCpu))
|
---|
606 | {
|
---|
607 | RT_ZERO(State);
|
---|
608 | State.iActionShot = 0;
|
---|
609 | State.rc = VINF_SUCCESS;
|
---|
610 | State.u.Specific.idCpu = RTMpCpuIdFromSetIndex(iCpu);
|
---|
611 | ASMAtomicWriteU32(&State.cShots, State.cShots);
|
---|
612 |
|
---|
613 | uint32_t fFlags = TSTRTR0TIMER_IS_HIRES(uOperation) ? RTTIMER_FLAGS_HIGH_RES : 0;
|
---|
614 | fFlags |= RTTIMER_FLAGS_CPU(iCpu);
|
---|
615 | int rc = RTTimerCreateEx(&pTimer, 0, fFlags, tstRTR0TimerCallbackSpecific, &State);
|
---|
616 | if (rc == VERR_NOT_SUPPORTED)
|
---|
617 | {
|
---|
618 | RTR0TestR0Info("one-shot specific timer are not supported, skipping\n");
|
---|
619 | RTR0TESTR0_SKIP();
|
---|
620 | break;
|
---|
621 | }
|
---|
622 | RTR0TESTR0_CHECK_RC_BREAK(rc, VINF_SUCCESS);
|
---|
623 |
|
---|
624 | for (uint32_t i = 0; i < 5 && !RTR0TestR0HaveErrors(); i++)
|
---|
625 | {
|
---|
626 | ASMAtomicWriteU32(&State.cShots, 0);
|
---|
627 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStart(pTimer, (i & 2 ? cNsSysHz : cNsSysHz / 2) * (i & 1)), VINF_SUCCESS);
|
---|
628 | uint64_t cNsElapsed = RTTimeSystemNanoTS();
|
---|
629 | for (uint32_t j = 0; j < 1000 && ASMAtomicUoReadU32(&State.cShots) < 1; j++)
|
---|
630 | RTThreadSleep(5);
|
---|
631 | cNsElapsed = RTTimeSystemNanoTS() - cNsElapsed;
|
---|
632 | RTR0TESTR0_CHECK_MSG_BREAK(ASMAtomicReadU32(&State.cShots) == 1,
|
---|
633 | ("cShots=%u iCpu=%u i=%u iCurCpu=%u cNsElapsed=%'llu\n",
|
---|
634 | State.cShots, iCpu, i, RTMpCpuIdToSetIndex(RTMpCpuId()), cNsElapsed ));
|
---|
635 | RTR0TESTR0_CHECK_MSG_BREAK(State.rc == VINF_SUCCESS, ("rc=%Rrc\n", State.rc));
|
---|
636 | RTR0TESTR0_CHECK_MSG_BREAK(!State.u.Specific.fFailed, ("iCpu=%u i=%u\n", iCpu, i));
|
---|
637 | }
|
---|
638 |
|
---|
639 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(pTimer), VINF_SUCCESS);
|
---|
640 | pTimer = NULL;
|
---|
641 | if (RTR0TestR0HaveErrors())
|
---|
642 | break;
|
---|
643 |
|
---|
644 | RTMpGetOnlineSet(&OnlineSet);
|
---|
645 | }
|
---|
646 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(pTimer), VINF_SUCCESS);
|
---|
647 | break;
|
---|
648 | }
|
---|
649 |
|
---|
650 | case TSTRTR0TIMER_ONE_SHOT_RESOLUTION:
|
---|
651 | case TSTRTR0TIMER_ONE_SHOT_RESOLUTION_HIRES:
|
---|
652 | {
|
---|
653 | /* Just create a timer and do a number of RTTimerStart with a small
|
---|
654 | interval and see how quickly it gets called. */
|
---|
655 | PRTTIMER pTimer;
|
---|
656 | uint32_t fFlags = TSTRTR0TIMER_IS_HIRES(uOperation) ? RTTIMER_FLAGS_HIGH_RES : 0;
|
---|
657 | uint64_t volatile cNsElapsed = 0;
|
---|
658 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerCreateEx(&pTimer, 0, fFlags, tstRTR0TimerCallbackOneShotElapsed, (void *)&cNsElapsed),
|
---|
659 | VINF_SUCCESS);
|
---|
660 |
|
---|
661 | uint32_t cTotal = 0;
|
---|
662 | uint32_t cNsTotal = 0;
|
---|
663 | uint32_t cNsMin = UINT32_MAX;
|
---|
664 | uint32_t cNsMax = 0;
|
---|
665 | for (uint32_t i = 0; i < 200; i++)
|
---|
666 | {
|
---|
667 | cNsElapsed = RTTimeSystemNanoTS();
|
---|
668 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStart(pTimer, RT_NS_1US), VINF_SUCCESS);
|
---|
669 | RTThreadSleep(10);
|
---|
670 | cTotal += 1;
|
---|
671 | cNsTotal += cNsElapsed;
|
---|
672 | if (cNsMin > cNsElapsed)
|
---|
673 | cNsMin = cNsElapsed;
|
---|
674 | if (cNsMax < cNsElapsed)
|
---|
675 | cNsMax = cNsElapsed;
|
---|
676 | }
|
---|
677 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(pTimer), VINF_SUCCESS);
|
---|
678 | pTimer = NULL;
|
---|
679 | RTR0TestR0Info("nsMin=%u nsAvg=%u nsMax=%u cTotal=%u\n", cNsMin, cNsTotal / cTotal, cNsMax, cTotal);
|
---|
680 | break;
|
---|
681 | }
|
---|
682 |
|
---|
683 | case TSTRTR0TIMER_PERIODIC_BASIC:
|
---|
684 | case TSTRTR0TIMER_PERIODIC_BASIC_HIRES:
|
---|
685 | {
|
---|
686 | /* Create a periodic timer running at 10 HZ. */
|
---|
687 | uint32_t const u10HzAsNs = RT_NS_1SEC / 10;
|
---|
688 | uint32_t const u10HzAsNsMin = u10HzAsNs - u10HzAsNs / 2;
|
---|
689 | uint32_t const u10HzAsNsMax = u10HzAsNs + u10HzAsNs / 2;
|
---|
690 | PRTTIMER pTimer;
|
---|
691 | uint32_t fFlags = TSTRTR0TIMER_IS_HIRES(uOperation) ? RTTIMER_FLAGS_HIGH_RES : 0;
|
---|
692 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerCreateEx(&pTimer, u10HzAsNs, fFlags, tstRTR0TimerCallbackU32Counter, &State),
|
---|
693 | VINF_SUCCESS);
|
---|
694 |
|
---|
695 | for (uint32_t iTest = 0; iTest < 2; iTest++)
|
---|
696 | {
|
---|
697 | RT_ZERO(State);
|
---|
698 | State.fPeriodic = true;
|
---|
699 | ASMAtomicWriteU32(&State.cShots, State.cShots);
|
---|
700 |
|
---|
701 | uint64_t uStartNsTS = RTTimeSystemNanoTS();
|
---|
702 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStart(pTimer, u10HzAsNs), VINF_SUCCESS);
|
---|
703 | for (uint32_t i = 0; i < 1000 && ASMAtomicUoReadU32(&State.cShots) < 10; i++)
|
---|
704 | RTThreadSleep(10);
|
---|
705 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStop(pTimer), VINF_SUCCESS);
|
---|
706 | RTR0TESTR0_CHECK_MSG_BREAK(ASMAtomicUoReadU32(&State.cShots) == 10, ("cShots=%u\n", State.cShots));
|
---|
707 | if (tstRTR0TimerCheckShotIntervals(&State, uStartNsTS, u10HzAsNsMin, u10HzAsNsMax))
|
---|
708 | break;
|
---|
709 | RTThreadSleep(1); /** @todo RTTimerStop doesn't currently make sure the timer callback not is running
|
---|
710 | * before returning on windows, linux (low res) and possible other plaforms. */
|
---|
711 | }
|
---|
712 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(pTimer), VINF_SUCCESS);
|
---|
713 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(NULL), VINF_SUCCESS);
|
---|
714 | break;
|
---|
715 | }
|
---|
716 |
|
---|
717 | case TSTRTR0TIMER_PERIODIC_CSSD_LOOPS:
|
---|
718 | case TSTRTR0TIMER_PERIODIC_CSSD_LOOPS_HIRES:
|
---|
719 | {
|
---|
720 | /* create, start, stop & destroy high res timers a number of times. */
|
---|
721 | uint32_t fFlags = TSTRTR0TIMER_IS_HIRES(uOperation) ? RTTIMER_FLAGS_HIGH_RES : 0;
|
---|
722 | for (uint32_t i = 0; i < 40; i++)
|
---|
723 | {
|
---|
724 | PRTTIMER pTimer;
|
---|
725 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerCreateEx(&pTimer, cNsSysHz, fFlags, tstRTR0TimerCallbackU32Counter, &State),
|
---|
726 | VINF_SUCCESS);
|
---|
727 | for (uint32_t j = 0; j < 10; j++)
|
---|
728 | {
|
---|
729 | RT_ZERO(State);
|
---|
730 | State.fPeriodic = true;
|
---|
731 | ASMAtomicWriteU32(&State.cShots, State.cShots); /* ordered, necessary? */
|
---|
732 |
|
---|
733 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStart(pTimer, i < 20 ? 0 : cNsSysHz), VINF_SUCCESS);
|
---|
734 | for (uint32_t k = 0; k < 1000 && ASMAtomicUoReadU32(&State.cShots) < 2; k++)
|
---|
735 | RTThreadSleep(1);
|
---|
736 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStop(pTimer), VINF_SUCCESS);
|
---|
737 | RTThreadSleep(1); /** @todo RTTimerStop doesn't currently make sure the timer callback not is running
|
---|
738 | * before returning on windows, linux (low res) and possible other plaforms. */
|
---|
739 | }
|
---|
740 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(pTimer), VINF_SUCCESS);
|
---|
741 | }
|
---|
742 | break;
|
---|
743 | }
|
---|
744 |
|
---|
745 | case TSTRTR0TIMER_PERIODIC_CHANGE_INTERVAL:
|
---|
746 | case TSTRTR0TIMER_PERIODIC_CHANGE_INTERVAL_HIRES:
|
---|
747 | {
|
---|
748 | /* Initialize the test parameters, using the u64Arg value for selecting variations. */
|
---|
749 | RT_ZERO(State);
|
---|
750 | State.cShots = 0;
|
---|
751 | State.rc = VERR_IPE_UNINITIALIZED_STATUS;
|
---|
752 | State.iActionShot = 42;
|
---|
753 | State.fPeriodic = true;
|
---|
754 | State.u.ChgInt.fDirection = !!(u64Arg & 1);
|
---|
755 | if (uOperation == TSTRTR0TIMER_PERIODIC_CHANGE_INTERVAL_HIRES)
|
---|
756 | {
|
---|
757 | State.u.ChgInt.cNsMaxInterval = RT_MAX(cNsMaxHighResHz * 10, 20000000); /* 10x / 20 ms */
|
---|
758 | State.u.ChgInt.cNsMinInterval = RT_MAX(cNsMaxHighResHz, 10000); /* min / 10 us */
|
---|
759 | }
|
---|
760 | else
|
---|
761 | {
|
---|
762 | State.u.ChgInt.cNsMaxInterval = cNsSysHz * 4;
|
---|
763 | State.u.ChgInt.cNsMinInterval = cNsSysHz;
|
---|
764 | }
|
---|
765 | State.u.ChgInt.cNsChangeStep = (State.u.ChgInt.cNsMaxInterval - State.u.ChgInt.cNsMinInterval) / 10;
|
---|
766 | State.u.ChgInt.cNsCurInterval = State.u.ChgInt.fDirection
|
---|
767 | ? State.u.ChgInt.cNsMaxInterval : State.u.ChgInt.cNsMinInterval;
|
---|
768 | State.u.ChgInt.cStepsBetween = u64Arg & 4 ? 1 : 3;
|
---|
769 | RTR0TESTR0_CHECK_MSG_BREAK(State.u.ChgInt.cNsMinInterval > 1000, ("%u\n", State.u.ChgInt.cNsMinInterval));
|
---|
770 | RTR0TESTR0_CHECK_MSG_BREAK(State.u.ChgInt.cNsMaxInterval > State.u.ChgInt.cNsMinInterval, ("max=%u min=%u\n", State.u.ChgInt.cNsMaxInterval, State.u.ChgInt.cNsMinInterval));
|
---|
771 | ASMAtomicWriteU32(&State.cShots, State.cShots);
|
---|
772 |
|
---|
773 | /* create the timer and check if RTTimerChangeInterval is supported. */
|
---|
774 | PRTTIMER pTimer;
|
---|
775 | uint32_t fFlags = TSTRTR0TIMER_IS_HIRES(uOperation) ? RTTIMER_FLAGS_HIGH_RES : 0;
|
---|
776 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerCreateEx(&pTimer, cNsSysHz, fFlags, tstRTR0TimerCallbackChangeInterval, &State),
|
---|
777 | VINF_SUCCESS);
|
---|
778 | int rc = RTTimerChangeInterval(pTimer, State.u.ChgInt.cNsMinInterval);
|
---|
779 | if (rc == VERR_NOT_SUPPORTED)
|
---|
780 | {
|
---|
781 | RTR0TestR0Info("RTTimerChangeInterval not supported, skipped");
|
---|
782 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(pTimer), VINF_SUCCESS);
|
---|
783 | RTR0TESTR0_SKIP();
|
---|
784 | break;
|
---|
785 | }
|
---|
786 |
|
---|
787 | /* do the test. */
|
---|
788 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStart(pTimer, u64Arg & 2 ? State.u.ChgInt.cNsCurInterval : 0), VINF_SUCCESS);
|
---|
789 | for (uint32_t k = 0;
|
---|
790 | k < 1000
|
---|
791 | && ASMAtomicReadU32(&State.cShots) <= State.iActionShot
|
---|
792 | && State.rc == VERR_IPE_UNINITIALIZED_STATUS;
|
---|
793 | k++)
|
---|
794 | RTThreadSleep(10);
|
---|
795 |
|
---|
796 | rc = RTTimerStop(pTimer);
|
---|
797 | RTR0TESTR0_CHECK_MSG_BREAK(rc == VERR_TIMER_SUSPENDED || rc == VINF_SUCCESS, ("rc = %Rrc (RTTimerStop)\n", rc));
|
---|
798 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(pTimer), VINF_SUCCESS);
|
---|
799 | break;
|
---|
800 | }
|
---|
801 |
|
---|
802 | case TSTRTR0TIMER_PERIODIC_SPECIFIC:
|
---|
803 | case TSTRTR0TIMER_PERIODIC_SPECIFIC_HIRES:
|
---|
804 | {
|
---|
805 | PRTTIMER pTimer = NULL;
|
---|
806 | RTCPUSET OnlineSet;
|
---|
807 | RTMpGetOnlineSet(&OnlineSet);
|
---|
808 | for (uint32_t iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
|
---|
809 | if (RTCpuSetIsMemberByIndex(&OnlineSet, iCpu))
|
---|
810 | {
|
---|
811 | RT_ZERO(State);
|
---|
812 | State.iActionShot = 0;
|
---|
813 | State.rc = VINF_SUCCESS;
|
---|
814 | State.fPeriodic = true;
|
---|
815 | State.u.Specific.idCpu = RTMpCpuIdFromSetIndex(iCpu);
|
---|
816 | ASMAtomicWriteU32(&State.cShots, State.cShots);
|
---|
817 |
|
---|
818 | uint32_t fFlags = TSTRTR0TIMER_IS_HIRES(uOperation) ? RTTIMER_FLAGS_HIGH_RES : 0;
|
---|
819 | fFlags |= RTTIMER_FLAGS_CPU(iCpu);
|
---|
820 | int rc = RTTimerCreateEx(&pTimer, cNsSysHz, fFlags, tstRTR0TimerCallbackSpecific, &State);
|
---|
821 | if (rc == VERR_NOT_SUPPORTED)
|
---|
822 | {
|
---|
823 | RTR0TestR0Info("specific timer are not supported, skipping\n");
|
---|
824 | RTR0TESTR0_SKIP();
|
---|
825 | break;
|
---|
826 | }
|
---|
827 | RTR0TESTR0_CHECK_RC_BREAK(rc, VINF_SUCCESS);
|
---|
828 |
|
---|
829 | for (uint32_t i = 0; i < 3 && !RTR0TestR0HaveErrors(); i++)
|
---|
830 | {
|
---|
831 | ASMAtomicWriteU32(&State.cShots, 0);
|
---|
832 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStart(pTimer, (i & 2 ? cNsSysHz : cNsSysHz / 2) * (i & 1)), VINF_SUCCESS);
|
---|
833 | uint64_t cNsElapsed = RTTimeSystemNanoTS();
|
---|
834 | for (uint32_t j = 0; j < 1000 && ASMAtomicUoReadU32(&State.cShots) < 8; j++)
|
---|
835 | RTThreadSleep(5);
|
---|
836 | cNsElapsed = RTTimeSystemNanoTS() - cNsElapsed;
|
---|
837 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStop(pTimer), VINF_SUCCESS);
|
---|
838 | RTR0TESTR0_CHECK_MSG_BREAK(ASMAtomicReadU32(&State.cShots) > 5,
|
---|
839 | ("cShots=%u iCpu=%u i=%u iCurCpu=%u cNsElapsed=%'llu\n",
|
---|
840 | State.cShots, iCpu, i, RTMpCpuIdToSetIndex(RTMpCpuId()), cNsElapsed));
|
---|
841 | RTThreadSleep(1); /** @todo RTTimerStop doesn't currently make sure the timer callback not is running
|
---|
842 | * before returning on windows, linux (low res) and possible other plaforms. */
|
---|
843 | RTR0TESTR0_CHECK_MSG_BREAK(State.rc == VINF_SUCCESS, ("rc=%Rrc\n", State.rc));
|
---|
844 | RTR0TESTR0_CHECK_MSG_BREAK(!State.u.Specific.fFailed, ("iCpu=%u i=%u\n", iCpu, i));
|
---|
845 | }
|
---|
846 |
|
---|
847 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(pTimer), VINF_SUCCESS);
|
---|
848 | pTimer = NULL;
|
---|
849 | if (RTR0TestR0HaveErrors())
|
---|
850 | break;
|
---|
851 |
|
---|
852 | RTMpGetOnlineSet(&OnlineSet);
|
---|
853 | }
|
---|
854 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(pTimer), VINF_SUCCESS);
|
---|
855 | break;
|
---|
856 | }
|
---|
857 |
|
---|
858 | case TSTRTR0TIMER_PERIODIC_OMNI:
|
---|
859 | case TSTRTR0TIMER_PERIODIC_OMNI_HIRES:
|
---|
860 | {
|
---|
861 | /* Create a periodic timer running at max host frequency, but no more than 1000 Hz. */
|
---|
862 | uint32_t cNsInterval = cNsSysHz;
|
---|
863 | while (cNsInterval < UINT32_C(1000000))
|
---|
864 | cNsInterval *= 2;
|
---|
865 | PTSTRTR0TIMEROMNI1 paStates = (PTSTRTR0TIMEROMNI1)RTMemAllocZ(sizeof(paStates[0]) * RTCPUSET_MAX_CPUS);
|
---|
866 | RTR0TESTR0_CHECK_MSG_BREAK(paStates, ("%d\n", RTCPUSET_MAX_CPUS));
|
---|
867 |
|
---|
868 | PRTTIMER pTimer;
|
---|
869 | uint32_t fFlags = (TSTRTR0TIMER_IS_HIRES(uOperation) ? RTTIMER_FLAGS_HIGH_RES : 0)
|
---|
870 | | RTTIMER_FLAGS_CPU_ALL;
|
---|
871 | int rc = RTTimerCreateEx(&pTimer, cNsInterval, fFlags, tstRTR0TimerCallbackOmni, paStates);
|
---|
872 | if (rc == VERR_NOT_SUPPORTED)
|
---|
873 | {
|
---|
874 | RTR0TESTR0_SKIP_BREAK();
|
---|
875 | }
|
---|
876 | RTR0TESTR0_CHECK_RC_BREAK(rc, VINF_SUCCESS);
|
---|
877 |
|
---|
878 | for (uint32_t iTest = 0; iTest < 3 && !RTR0TestR0HaveErrors(); iTest++)
|
---|
879 | {
|
---|
880 | /* reset the state */
|
---|
881 | for (uint32_t iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
|
---|
882 | {
|
---|
883 | paStates[iCpu].u64Start = 0;
|
---|
884 | paStates[iCpu].u64Last = 0;
|
---|
885 | ASMAtomicWriteU32(&paStates[iCpu].cTicks, 0);
|
---|
886 | }
|
---|
887 |
|
---|
888 | /* run it for 5 seconds. */
|
---|
889 | RTCPUSET OnlineSet;
|
---|
890 | uint64_t uStartNsTS = RTTimeSystemNanoTS();
|
---|
891 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStart(pTimer, 0), VINF_SUCCESS);
|
---|
892 | RTMpGetOnlineSet(&OnlineSet);
|
---|
893 |
|
---|
894 | for (uint32_t i = 0; i < 5000 && RTTimeSystemNanoTS() - uStartNsTS <= UINT64_C(5000000000); i++)
|
---|
895 | RTThreadSleep(2);
|
---|
896 |
|
---|
897 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStop(pTimer), VINF_SUCCESS);
|
---|
898 | uint64_t cNsElapsedX = RTTimeNanoTS() - uStartNsTS;
|
---|
899 |
|
---|
900 | /* Do a min/max on the start and stop times and calculate the test period. */
|
---|
901 | uint64_t u64MinStart = UINT64_MAX;
|
---|
902 | uint64_t u64MaxStop = 0;
|
---|
903 | for (uint32_t iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
|
---|
904 | {
|
---|
905 | if (paStates[iCpu].u64Start)
|
---|
906 | {
|
---|
907 | if (paStates[iCpu].u64Start < u64MinStart)
|
---|
908 | u64MinStart = paStates[iCpu].u64Start;
|
---|
909 | if (paStates[iCpu].u64Last > u64MaxStop)
|
---|
910 | u64MaxStop = paStates[iCpu].u64Last;
|
---|
911 | }
|
---|
912 | }
|
---|
913 | RTR0TESTR0_CHECK_MSG(u64MinStart < u64MaxStop, ("%llu, %llu", u64MinStart, u64MaxStop));
|
---|
914 | uint64_t cNsElapsed = u64MaxStop - u64MinStart;
|
---|
915 | RTR0TESTR0_CHECK_MSG(cNsElapsed <= cNsElapsedX + 100000, ("%llu, %llu", cNsElapsed, cNsElapsedX)); /* the fudge factor is time drift */
|
---|
916 | uint32_t cAvgTicks = cNsElapsed / cNsInterval + 1;
|
---|
917 |
|
---|
918 | /* Check tick counts. ASSUMES no cpu on- or offlining.
|
---|
919 | This only catches really bad stuff. */
|
---|
920 | uint32_t cMargin = TSTRTR0TIMER_IS_HIRES(uOperation) ? 10 : 5; /* Allow a wider deviation for the non hires timers. */
|
---|
921 | uint32_t cMinTicks = cAvgTicks - cAvgTicks / cMargin;
|
---|
922 | uint32_t cMaxTicks = cAvgTicks + cAvgTicks / cMargin + 1;
|
---|
923 | for (uint32_t iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
|
---|
924 | if (paStates[iCpu].cTicks)
|
---|
925 | {
|
---|
926 | RTR0TESTR0_CHECK_MSG(RTCpuSetIsMemberByIndex(&OnlineSet, iCpu), ("%d\n", iCpu));
|
---|
927 | RTR0TESTR0_CHECK_MSG(paStates[iCpu].cTicks <= cMaxTicks && paStates[iCpu].cTicks >= cMinTicks,
|
---|
928 | ("min=%u, ticks=%u, avg=%u max=%u, iCpu=%u, iCpuCurr=%u, interval=%'u, elapsed=%'llu/%'llu\n",
|
---|
929 | cMinTicks, paStates[iCpu].cTicks, cAvgTicks, cMaxTicks, iCpu,
|
---|
930 | RTMpCpuIdToSetIndex(RTMpCpuId()),
|
---|
931 | cNsInterval, cNsElapsed, cNsElapsedX));
|
---|
932 | }
|
---|
933 | else
|
---|
934 | RTR0TESTR0_CHECK_MSG(!RTCpuSetIsMemberByIndex(&OnlineSet, iCpu), ("%d\n", iCpu));
|
---|
935 | }
|
---|
936 |
|
---|
937 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(pTimer), VINF_SUCCESS);
|
---|
938 | RTMemFree(paStates);
|
---|
939 | break;
|
---|
940 | }
|
---|
941 |
|
---|
942 |
|
---|
943 | case TSTRTR0TIMER_LATENCY_OMNI:
|
---|
944 | case TSTRTR0TIMER_LATENCY_OMNI_HIRES:
|
---|
945 | {
|
---|
946 | /*
|
---|
947 | * Create a periodic timer running at max host frequency, but no more than 1000 Hz.
|
---|
948 | * Unless it's a high resolution timer, which we try at double the rate.
|
---|
949 | * Windows seems to limit the highres stuff to around 500-600 us interval.
|
---|
950 | */
|
---|
951 | PRTTIMER pTimer;
|
---|
952 | uint32_t fFlags = (TSTRTR0TIMER_IS_HIRES(uOperation) ? RTTIMER_FLAGS_HIGH_RES : 0)
|
---|
953 | | RTTIMER_FLAGS_CPU_ALL;
|
---|
954 | uint32_t const cNsMinInterval = TSTRTR0TIMER_IS_HIRES(uOperation) ? cNsMaxHighResHz : RT_NS_1MS;
|
---|
955 | uint32_t cNsInterval = TSTRTR0TIMER_IS_HIRES(uOperation) ? cNsSysHz / 2 : cNsSysHz;
|
---|
956 | while (cNsInterval < cNsMinInterval)
|
---|
957 | cNsInterval *= 2;
|
---|
958 | int rc = RTTimerCreateEx(&pTimer, cNsInterval, fFlags, tstRTR0TimerCallbackLatencyOmni, NULL);
|
---|
959 | if (rc == VERR_NOT_SUPPORTED)
|
---|
960 | {
|
---|
961 | RTR0TESTR0_SKIP_BREAK();
|
---|
962 | }
|
---|
963 | RTR0TESTR0_CHECK_RC_BREAK(rc, VINF_SUCCESS);
|
---|
964 |
|
---|
965 | /*
|
---|
966 | * Reset the state and run the test for 4 seconds.
|
---|
967 | */
|
---|
968 | RT_ZERO(g_aOmniLatency);
|
---|
969 |
|
---|
970 | RTCPUSET OnlineSet;
|
---|
971 | uint64_t uStartNsTS = RTTimeSystemNanoTS();
|
---|
972 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStart(pTimer, 0), VINF_SUCCESS);
|
---|
973 | RTMpGetOnlineSet(&OnlineSet);
|
---|
974 |
|
---|
975 | for (uint32_t i = 0; i < 5000 && RTTimeSystemNanoTS() - uStartNsTS <= UINT64_C(4000000000); i++)
|
---|
976 | RTThreadSleep(2);
|
---|
977 |
|
---|
978 | RTR0TESTR0_CHECK_RC_BREAK(RTTimerStop(pTimer), VINF_SUCCESS);
|
---|
979 |
|
---|
980 | /*
|
---|
981 | * Process the result.
|
---|
982 | */
|
---|
983 | int32_t cNsLow = cNsInterval / 4 * 3; /* 75% */
|
---|
984 | int32_t cNsHigh = cNsInterval / 4 * 5; /* 125% */
|
---|
985 | uint32_t cTotal = 0;
|
---|
986 | uint32_t cLow = 0;
|
---|
987 | uint32_t cHigh = 0;
|
---|
988 | for (uint32_t iCpu = 0; iCpu < RT_ELEMENTS(g_aOmniLatency); iCpu++)
|
---|
989 | {
|
---|
990 | uint32_t cSamples = g_aOmniLatency[iCpu].cSamples;
|
---|
991 | if (cSamples > 1)
|
---|
992 | {
|
---|
993 | cTotal += cSamples - 1;
|
---|
994 | for (uint32_t iSample = 1; iSample < cSamples; iSample++)
|
---|
995 | {
|
---|
996 | int64_t cNsDelta = g_aOmniLatency[iCpu].aSamples[iSample].uNanoTs
|
---|
997 | - g_aOmniLatency[iCpu].aSamples[iSample - 1].uNanoTs;
|
---|
998 | if (cNsDelta < cNsLow)
|
---|
999 | cLow++;
|
---|
1000 | else if (cNsDelta > cNsHigh)
|
---|
1001 | cHigh++;
|
---|
1002 | }
|
---|
1003 | }
|
---|
1004 | }
|
---|
1005 | RTR0TestR0Info("125%%: %u; 75%%: %u; total: %u", cHigh, cLow, cTotal);
|
---|
1006 | RTR0TESTR0_CHECK_RC(RTTimerDestroy(pTimer), VINF_SUCCESS);
|
---|
1007 | #if 1
|
---|
1008 | RTR0TestR0Info("cNsSysHz=%u cNsInterval=%RU32 cNsLow=%d cNsHigh=%d", cNsSysHz, cNsInterval, cNsLow, cNsHigh);
|
---|
1009 | if (TSTRTR0TIMER_IS_HIRES(uOperation))
|
---|
1010 | RTR0TestR0Info("RTTimerCanDoHighResolution -> %d", RTTimerCanDoHighResolution());
|
---|
1011 | for (uint32_t iSample = 1; iSample < 6; iSample++)
|
---|
1012 | RTR0TestR0Info("%RU64/%#RU64",
|
---|
1013 | g_aOmniLatency[0].aSamples[iSample].uNanoTs - g_aOmniLatency[0].aSamples[iSample - 1].uNanoTs,
|
---|
1014 | g_aOmniLatency[0].aSamples[iSample].uTsc - g_aOmniLatency[0].aSamples[iSample - 1].uTsc);
|
---|
1015 | #endif
|
---|
1016 | break;
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | RTR0TESTR0_SRV_REQ_EPILOG(pReqHdr);
|
---|
1022 | /* The error indicator is the '!' in the message buffer. */
|
---|
1023 | return VINF_SUCCESS;
|
---|
1024 | }
|
---|
1025 |
|
---|