VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTR0Timer.cpp@ 93115

Last change on this file since 93115 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

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