VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/timer-r0drv-linux.c@ 27194

最後變更 在這個檔案從27194是 27194,由 vboxsync 提交於 15 年 前

r0drv/linux/timer: adapt to Linux kernels >= 2.6.31 where mod_timer() does not pin the timer to the current CPU core anymore

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 30.4 KB
 
1/* $Id: timer-r0drv-linux.c 27194 2010-03-09 09:13:35Z vboxsync $ */
2/** @file
3 * IPRT - Timers, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "the-linux-kernel.h"
36#include "internal/iprt.h"
37
38#include <iprt/timer.h>
39#include <iprt/time.h>
40#include <iprt/mp.h>
41#include <iprt/cpuset.h>
42#include <iprt/spinlock.h>
43#include <iprt/err.h>
44#include <iprt/asm.h>
45#include <iprt/assert.h>
46#include <iprt/alloc.h>
47
48#include "internal/magics.h"
49
50/* We use the API of Linux 2.6.28+ (hrtimer_add_expires_ns()) */
51#if !defined(RT_USE_LINUX_HRTIMER) \
52 && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28) \
53 && 0 /* currently disabled */
54# define RT_USE_LINUX_HRTIMER
55#endif
56
57/* This check must match the ktime usage in rtTimeGetSystemNanoTS() / time-r0drv-linux.c. */
58#if defined(RT_USE_LINUX_HRTIMER) \
59 && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
60# error "RT_USE_LINUX_HRTIMER requires 2.6.28 or later, sorry."
61#endif
62
63#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31)
64# define mod_timer_pinned mod_timer
65#endif
66
67
68/*******************************************************************************
69* Structures and Typedefs *
70*******************************************************************************/
71/**
72 * Timer state machine.
73 *
74 * This is used to try handle the issues with MP events and
75 * timers that runs on all CPUs. It's relatively nasty :-/
76 */
77typedef enum RTTIMERLNXSTATE
78{
79 /** Stopped. */
80 RTTIMERLNXSTATE_STOPPED = 0,
81 /** Transient state; next ACTIVE. */
82 RTTIMERLNXSTATE_STARTING,
83 /** Transient state; next ACTIVE. (not really necessary) */
84 RTTIMERLNXSTATE_MP_STARTING,
85 /** Active. */
86 RTTIMERLNXSTATE_ACTIVE,
87 /** Transient state; next STOPPED. */
88 RTTIMERLNXSTATE_STOPPING,
89 /** Transient state; next STOPPED. */
90 RTTIMERLNXSTATE_MP_STOPPING,
91 /** The usual 32-bit hack. */
92 RTTIMERLNXSTATE_32BIT_HACK = 0x7fffffff
93} RTTIMERLNXSTATE;
94
95
96/**
97 * A Linux sub-timer.
98 */
99typedef struct RTTIMERLNXSUBTIMER
100{
101 /** The linux timer structure. */
102#ifdef RT_USE_LINUX_HRTIMER
103 struct hrtimer LnxTimer;
104#else
105 struct timer_list LnxTimer;
106 /** The start of the current run (ns).
107 * This is used to calculate when the timer ought to fire the next time. */
108 uint64_t u64StartTS;
109 /** The start of the current run (ns).
110 * This is used to calculate when the timer ought to fire the next time. */
111 uint64_t u64NextTS;
112#endif
113 /** The current tick number (since u64StartTS). */
114 uint64_t iTick;
115 /** Pointer to the parent timer. */
116 PRTTIMER pParent;
117#ifndef RT_USE_LINUX_HRTIMER
118 /** The u64NextTS in jiffies. */
119 unsigned long ulNextJiffies;
120#endif
121 /** The current sub-timer state. */
122 RTTIMERLNXSTATE volatile enmState;
123} RTTIMERLNXSUBTIMER;
124/** Pointer to a linux sub-timer. */
125typedef RTTIMERLNXSUBTIMER *PRTTIMERLNXSUBTIMER;
126AssertCompileMemberOffset(RTTIMERLNXSUBTIMER, LnxTimer, 0);
127
128
129/**
130 * The internal representation of an Linux timer handle.
131 */
132typedef struct RTTIMER
133{
134 /** Magic.
135 * This is RTTIMER_MAGIC, but changes to something else before the timer
136 * is destroyed to indicate clearly that thread should exit. */
137 uint32_t volatile u32Magic;
138 /** Spinlock synchronizing the fSuspended and MP event handling.
139 * This is NIL_RTSPINLOCK if cCpus == 1. */
140 RTSPINLOCK hSpinlock;
141 /** Flag indicating that the timer is suspended. */
142 bool volatile fSuspended;
143 /** Whether the timer must run on one specific CPU or not. */
144 bool fSpecificCpu;
145#ifdef CONFIG_SMP
146 /** Whether the timer must run on all CPUs or not. */
147 bool fAllCpus;
148#endif /* else: All -> specific on non-SMP kernels */
149 /** The CPU it must run on if fSpecificCpu is set. */
150 RTCPUID idCpu;
151 /** The number of CPUs this timer should run on. */
152 RTCPUID cCpus;
153 /** Callback. */
154 PFNRTTIMER pfnTimer;
155 /** User argument. */
156 void *pvUser;
157 /** The timer interval. 0 if one-shot. */
158 uint64_t u64NanoInterval;
159#ifndef RT_USE_LINUX_HRTIMER
160 /** This is set to the number of jiffies between ticks if the interval is
161 * an exact number of jiffies. */
162 unsigned long cJiffies;
163#endif
164 /** Sub-timers.
165 * Normally there is just one, but for RTTIMER_FLAGS_CPU_ALL this will contain
166 * an entry for all possible cpus. In that case the index will be the same as
167 * for the RTCpuSet. */
168 RTTIMERLNXSUBTIMER aSubTimers[1];
169} RTTIMER;
170
171
172/**
173 * A rtTimerLinuxStartOnCpu and rtTimerLinuxStartOnCpu argument package.
174 */
175typedef struct RTTIMERLINUXSTARTONCPUARGS
176{
177 /** The current time (RTTimeNanoTS). */
178 uint64_t u64Now;
179 /** When to start firing (delta). */
180 uint64_t u64First;
181} RTTIMERLINUXSTARTONCPUARGS;
182/** Pointer to a rtTimerLinuxStartOnCpu argument package. */
183typedef RTTIMERLINUXSTARTONCPUARGS *PRTTIMERLINUXSTARTONCPUARGS;
184
185
186/**
187 * Sets the state.
188 */
189DECLINLINE(void) rtTimerLnxSetState(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState)
190{
191 ASMAtomicWriteU32((uint32_t volatile *)penmState, enmNewState);
192}
193
194
195/**
196 * Sets the state if it has a certain value.
197 *
198 * @return true if xchg was done.
199 * @return false if xchg wasn't done.
200 */
201DECLINLINE(bool) rtTimerLnxCmpXchgState(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState, RTTIMERLNXSTATE enmCurState)
202{
203 return ASMAtomicCmpXchgU32((uint32_t volatile *)penmState, enmNewState, enmCurState);
204}
205
206
207/**
208 * Gets the state.
209 */
210DECLINLINE(RTTIMERLNXSTATE) rtTimerLnxGetState(RTTIMERLNXSTATE volatile *penmState)
211{
212 return (RTTIMERLNXSTATE)ASMAtomicUoReadU32((uint32_t volatile *)penmState);
213}
214
215
216#ifdef RT_USE_LINUX_HRTIMER
217/**
218 * Converts a nano second time stamp to ktime_t.
219 *
220 * ASSUMES RTTimeNanoTS() is implemented using ktime_get_ts().
221 *
222 * @returns ktime_t.
223 * @param cNanoSecs Nanoseconds.
224 */
225DECLINLINE(ktime_t) rtTimerLnxNanoToKt(uint64_t cNanoSecs)
226{
227 /* With some luck the compiler optimizes the division out of this... (Bet it doesn't.) */
228 return ktime_set(cNanoSecs / 1000000000, cNanoSecs % 1000000000);
229}
230
231/**
232 * Converts ktime_t to a nano second time stamp.
233 *
234 * ASSUMES RTTimeNanoTS() is implemented using ktime_get_ts().
235 *
236 * @returns nano second time stamp.
237 * @param Kt ktime_t.
238 */
239DECLINLINE(uint64_t) rtTimerLnxKtToNano(ktime_t Kt)
240{
241 return ktime_to_ns(Kt);
242}
243
244#else /* ! RT_USE_LINUX_HRTIMER */
245
246/**
247 * Converts a nano second interval to jiffies.
248 *
249 * @returns Jiffies.
250 * @param cNanoSecs Nanoseconds.
251 */
252DECLINLINE(unsigned long) rtTimerLnxNanoToJiffies(uint64_t cNanoSecs)
253{
254 /* this can be made even better... */
255 if (cNanoSecs > (uint64_t)TICK_NSEC * MAX_JIFFY_OFFSET)
256 return MAX_JIFFY_OFFSET;
257# if ARCH_BITS == 32
258 if (RT_LIKELY(cNanoSecs <= UINT32_MAX))
259 return ((uint32_t)cNanoSecs + (TICK_NSEC-1)) / TICK_NSEC;
260# endif
261 return (cNanoSecs + (TICK_NSEC-1)) / TICK_NSEC;
262}
263#endif /* ! RT_USE_LINUX_HRTIMER */
264
265
266/**
267 * Starts a sub-timer (RTTimerStart).
268 *
269 * @param pSubTimer The sub-timer to start.
270 * @param u64Now The current timestamp (RTTimeNanoTS()).
271 * @param u64First The interval from u64Now to the first time the timer should fire.
272 * @param fPinned true = timer pinned to a specific CPU,
273 * false = timer can migrate between CPUs
274 */
275static void rtTimerLnxStartSubTimer(PRTTIMERLNXSUBTIMER pSubTimer, uint64_t u64Now, uint64_t u64First, bool fPinned)
276{
277 /*
278 * Calc when it should start firing.
279 */
280 uint64_t u64NextTS = u64Now + u64First;
281#ifndef RT_USE_LINUX_HRTIMER
282 pSubTimer->u64StartTS = u64NextTS;
283 pSubTimer->u64NextTS = u64NextTS;
284#endif
285
286 pSubTimer->iTick = 0;
287
288#ifdef RT_USE_LINUX_HRTIMER
289 hrtimer_start(&pSubTimer->LnxTimer, rtTimerLnxNanoToKt(u64NextTS), HRTIMER_MODE_ABS);
290#else
291 {
292 unsigned long cJiffies = !u64First ? 0 : rtTimerLnxNanoToJiffies(u64First);
293 pSubTimer->ulNextJiffies = jiffies + cJiffies;
294 if (fPinned)
295 mod_timer_pinned(&pSubTimer->LnxTimer, pSubTimer->ulNextJiffies);
296 else
297 mod_timer(&pSubTimer->LnxTimer, pSubTimer->ulNextJiffies);
298 }
299#endif
300
301 rtTimerLnxSetState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE);
302}
303
304
305/**
306 * Stops a sub-timer (RTTimerStart and rtTimerLinuxMpEvent()).
307 *
308 * @param pSubTimer The sub-timer.
309 */
310static void rtTimerLnxStopSubTimer(PRTTIMERLNXSUBTIMER pSubTimer)
311{
312#ifdef RT_USE_LINUX_HRTIMER
313 hrtimer_cancel(&pSubTimer->LnxTimer);
314#else
315 if (timer_pending(&pSubTimer->LnxTimer))
316 del_timer_sync(&pSubTimer->LnxTimer);
317#endif
318
319 rtTimerLnxSetState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED);
320}
321
322
323#ifdef RT_USE_LINUX_HRTIMER
324/**
325 * Timer callback function.
326 * @returns HRTIMER_NORESTART or HRTIMER_RESTART depending on whether it's a one-shot or interval timer.
327 * @param pHrTimer Pointer to the sub-timer structure.
328 */
329static enum hrtimer_restart rtTimerLinuxCallback(struct hrtimer *pHrTimer)
330#else
331/**
332 * Timer callback function.
333 * @param ulUser Address of the sub-timer structure.
334 */
335static void rtTimerLinuxCallback(unsigned long ulUser)
336#endif
337{
338#ifdef RT_USE_LINUX_HRTIMER
339 enum hrtimer_restart rc;
340 PRTTIMERLNXSUBTIMER pSubTimer = (PRTTIMERLNXSUBTIMER)pHrTimer;
341#else
342 PRTTIMERLNXSUBTIMER pSubTimer = (PRTTIMERLNXSUBTIMER)ulUser;
343#endif
344 PRTTIMER pTimer = pSubTimer->pParent;
345
346 /*
347 * Don't call the handler if the timer has been suspended.
348 * Also, when running on all CPUS, make sure we don't call out twice
349 * on a CPU because of timer migration.
350 *
351 * For the specific cpu case, we're just ignoring timer migration for now... (bad)
352 */
353 if ( ASMAtomicUoReadBool(&pTimer->fSuspended)
354#ifdef CONFIG_SMP
355 || ( pTimer->fAllCpus
356 && (RTCPUID)(pSubTimer - &pTimer->aSubTimers[0]) != RTMpCpuId())
357#endif
358 )
359 {
360 rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_ACTIVE);
361# ifdef RT_USE_LINUX_HRTIMER
362 rc = HRTIMER_NORESTART;
363# endif
364 }
365 else if (!pTimer->u64NanoInterval)
366 {
367 /*
368 * One shot timer, stop it before dispatching it.
369 */
370 if (pTimer->cCpus == 1)
371 ASMAtomicWriteBool(&pTimer->fSuspended, true);
372 rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_ACTIVE);
373#ifdef RT_USE_LINUX_HRTIMER
374 rc = HRTIMER_NORESTART;
375#else
376 /* detached before we're called, nothing to do for this case. */
377#endif
378
379 pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pSubTimer->iTick);
380 }
381 else
382 {
383 const uint64_t iTick = ++pSubTimer->iTick;
384
385#ifdef RT_USE_LINUX_HRTIMER
386 hrtimer_add_expires_ns(&pSubTimer->LnxTimer, pTimer->u64NanoInterval);
387 rc = HRTIMER_RESTART;
388#else
389 const uint64_t u64NanoTS = RTTimeNanoTS();
390
391 /*
392 * Interval timer, calculate the next timeout and re-arm it.
393 *
394 * The first time around, we'll re-adjust the u64StartTS to
395 * try prevent some jittering if we were started at a bad time.
396 * This may of course backfire with highres timers...
397 */
398 if (RT_UNLIKELY(iTick == 1))
399 {
400 pSubTimer->u64StartTS = pSubTimer->u64NextTS = u64NanoTS;
401 pSubTimer->ulNextJiffies = jiffies;
402 }
403
404 pSubTimer->u64NextTS += pTimer->u64NanoInterval;
405 if (pTimer->cJiffies)
406 {
407 pSubTimer->ulNextJiffies += pTimer->cJiffies;
408 /* Prevent overflows when the jiffies counter wraps around.
409 * Special thanks to Ken Preslan for helping debugging! */
410 while (time_before(pSubTimer->ulNextJiffies, jiffies))
411 {
412 pSubTimer->ulNextJiffies += pTimer->cJiffies;
413 pSubTimer->u64NextTS += pTimer->u64NanoInterval;
414 }
415 }
416 else
417 {
418 while (pSubTimer->u64NextTS < u64NanoTS)
419 pSubTimer->u64NextTS += pTimer->u64NanoInterval;
420 pSubTimer->ulNextJiffies = jiffies + rtTimerLnxNanoToJiffies(pSubTimer->u64NextTS - u64NanoTS);
421 }
422
423 if (pTimer->fSpecificCpu || pTimer->fAllCpus)
424 mod_timer_pinned(&pSubTimer->LnxTimer, pSubTimer->ulNextJiffies);
425 else
426 mod_timer(&pSubTimer->LnxTimer, pSubTimer->ulNextJiffies);
427#endif
428
429 /*
430 * Run the timer.
431 */
432 pTimer->pfnTimer(pTimer, pTimer->pvUser, iTick);
433 }
434
435#ifdef RT_USE_LINUX_HRTIMER
436 return rc;
437#endif
438}
439
440
441#ifdef CONFIG_SMP
442
443/**
444 * Per-cpu callback function (RTMpOnAll/RTMpOnSpecific).
445 *
446 * @param idCpu The current CPU.
447 * @param pvUser1 Pointer to the timer.
448 * @param pvUser2 Pointer to the argument structure.
449 */
450static DECLCALLBACK(void) rtTimerLnxStartAllOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
451{
452 PRTTIMERLINUXSTARTONCPUARGS pArgs = (PRTTIMERLINUXSTARTONCPUARGS)pvUser2;
453 PRTTIMER pTimer = (PRTTIMER)pvUser1;
454 Assert(idCpu < pTimer->cCpus);
455 rtTimerLnxStartSubTimer(&pTimer->aSubTimers[idCpu], pArgs->u64Now, pArgs->u64First, true /*fPinned*/);
456}
457
458
459/**
460 * Worker for RTTimerStart() that takes care of the ugly bit.s
461 *
462 * @returns RTTimerStart() return value.
463 * @param pTimer The timer.
464 * @param pArgs The argument structure.
465 */
466static int rtTimerLnxStartAll(PRTTIMER pTimer, PRTTIMERLINUXSTARTONCPUARGS pArgs)
467{
468 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
469 RTCPUID iCpu;
470 RTCPUSET OnlineSet;
471 RTCPUSET OnlineSet2;
472 int rc2;
473
474 /*
475 * Prepare all the sub-timers for the startup and then flag the timer
476 * as a whole as non-suspended, make sure we get them all before
477 * clearing fSuspended as the MP handler will be waiting on this
478 * should something happen while we're looping.
479 */
480 RTSpinlockAcquire(pTimer->hSpinlock, &Tmp);
481
482 do
483 {
484 RTMpGetOnlineSet(&OnlineSet);
485 for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
486 {
487 Assert(pTimer->aSubTimers[iCpu].enmState != RTTIMERLNXSTATE_MP_STOPPING);
488 rtTimerLnxSetState(&pTimer->aSubTimers[iCpu].enmState,
489 RTCpuSetIsMember(&OnlineSet, iCpu)
490 ? RTTIMERLNXSTATE_STARTING
491 : RTTIMERLNXSTATE_STOPPED);
492 }
493 } while (!RTCpuSetIsEqual(&OnlineSet, RTMpGetOnlineSet(&OnlineSet2)));
494
495 ASMAtomicWriteBool(&pTimer->fSuspended, false);
496
497 RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
498
499 /*
500 * Start them (can't find any exported function that allows me to
501 * do this without the cross calls).
502 */
503 pArgs->u64Now = RTTimeNanoTS();
504 rc2 = RTMpOnAll(rtTimerLnxStartAllOnCpu, pTimer, pArgs);
505 AssertRC(rc2); /* screw this if it fails. */
506
507 /*
508 * Reset the sub-timers who didn't start up (ALL CPUs case).
509 * CPUs that comes online between the
510 */
511 RTSpinlockAcquire(pTimer->hSpinlock, &Tmp);
512
513 for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
514 if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_STARTING))
515 {
516 /** @todo very odd case for a rainy day. Cpus that temporarily went offline while
517 * we were between calls needs to nudged as the MP handler will ignore events for
518 * them because of the STARTING state. This is an extremely unlikely case - not that
519 * that means anything in my experience... ;-) */
520 }
521
522 RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
523
524 return VINF_SUCCESS;
525}
526
527
528/**
529 * Worker for RTTimerStop() that takes care of the ugly SMP bits.
530 *
531 * @returns RTTimerStop() return value.
532 * @param pTimer The timer (valid).
533 */
534static int rtTimerLnxStopAll(PRTTIMER pTimer)
535{
536 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
537 RTCPUID iCpu;
538
539
540 /*
541 * Mark the timer as suspended and flag all timers as stopping, except
542 * for those being stopped by an MP event.
543 */
544 RTSpinlockAcquire(pTimer->hSpinlock, &Tmp);
545
546 ASMAtomicWriteBool(&pTimer->fSuspended, true);
547 for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
548 {
549 RTTIMERLNXSTATE enmState;
550 do
551 {
552 enmState = rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState);
553 if ( enmState == RTTIMERLNXSTATE_STOPPED
554 || enmState == RTTIMERLNXSTATE_MP_STOPPING)
555 break;
556 Assert(enmState == RTTIMERLNXSTATE_ACTIVE);
557 } while (!rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_STOPPING, enmState));
558 }
559
560 RTSpinlockRelease(pTimer->hSpinlock, &Tmp);
561
562 /*
563 * Do the actual stopping. Fortunately, this doesn't require any IPIs.
564 * Unfortunately it cannot be done synchronously from within the spinlock,
565 * because we might end up in an active waiting for a handler to complete.
566 */
567 for (iCpu = 0; iCpu < pTimer->cCpus; iCpu++)
568 if (rtTimerLnxGetState(&pTimer->aSubTimers[iCpu].enmState) == RTTIMERLNXSTATE_STOPPING)
569 rtTimerLnxStopSubTimer(&pTimer->aSubTimers[iCpu]);
570
571 return VINF_SUCCESS;
572}
573
574
575/**
576 * Per-cpu callback function (RTMpOnSpecific) used by rtTimerLinuxMpEvent()
577 * to start a sub-timer on a cpu that just have come online.
578 *
579 * @param idCpu The current CPU.
580 * @param pvUser1 Pointer to the timer.
581 * @param pvUser2 Pointer to the argument structure.
582 */
583static DECLCALLBACK(void) rtTimerLinuxMpStartOnCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
584{
585 PRTTIMERLINUXSTARTONCPUARGS pArgs = (PRTTIMERLINUXSTARTONCPUARGS)pvUser2;
586 PRTTIMER pTimer = (PRTTIMER)pvUser1;
587 RTSPINLOCK hSpinlock;
588 Assert(idCpu < pTimer->cCpus);
589
590 /*
591 * We have to be kind of careful here as we might be racing RTTimerStop
592 * (and/or RTTimerDestroy, thus the paranoia.
593 */
594 hSpinlock = pTimer->hSpinlock;
595 if ( hSpinlock != NIL_RTSPINLOCK
596 && pTimer->u32Magic == RTTIMER_MAGIC)
597 {
598 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
599 RTSpinlockAcquire(hSpinlock, &Tmp);
600
601 if ( !ASMAtomicUoReadBool(&pTimer->fSuspended)
602 && pTimer->u32Magic == RTTIMER_MAGIC)
603 {
604 /* We're sane and the timer is not suspended yet. */
605 PRTTIMERLNXSUBTIMER pSubTimer = &pTimer->aSubTimers[idCpu];
606 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STARTING, RTTIMERLNXSTATE_STOPPED))
607 rtTimerLnxStartSubTimer(pSubTimer, pArgs->u64Now, pArgs->u64First, true /*fPinned*/);
608 }
609
610 RTSpinlockRelease(hSpinlock, &Tmp);
611 }
612}
613
614
615/**
616 * MP event notification callback.
617 *
618 * @param enmEvent The event.
619 * @param idCpu The cpu it applies to.
620 * @param pvUser The timer.
621 */
622static DECLCALLBACK(void) rtTimerLinuxMpEvent(RTMPEVENT enmEvent, RTCPUID idCpu, void *pvUser)
623{
624 PRTTIMER pTimer = (PRTTIMER)pvUser;
625 PRTTIMERLNXSUBTIMER pSubTimer = &pTimer->aSubTimers[idCpu];
626 RTSPINLOCK hSpinlock;
627 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
628
629 Assert(idCpu < pTimer->cCpus);
630
631 /*
632 * Some initial paranoia.
633 */
634 if (pTimer->u32Magic != RTTIMER_MAGIC)
635 return;
636 hSpinlock = pTimer->hSpinlock;
637 if (hSpinlock == NIL_RTSPINLOCK)
638 return;
639
640 RTSpinlockAcquire(hSpinlock, &Tmp);
641
642 /* Is it active? */
643 if ( !ASMAtomicUoReadBool(&pTimer->fSuspended)
644 && pTimer->u32Magic == RTTIMER_MAGIC)
645 {
646 switch (enmEvent)
647 {
648 /*
649 * Try do it without leaving the spin lock, but if we have to, retake it
650 * when we're on the right cpu.
651 */
652 case RTMPEVENT_ONLINE:
653 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STARTING, RTTIMERLNXSTATE_STOPPED))
654 {
655 RTTIMERLINUXSTARTONCPUARGS Args;
656 Args.u64Now = RTTimeNanoTS();
657 Args.u64First = 0;
658
659 if (RTMpCpuId() == idCpu)
660 rtTimerLnxStartSubTimer(pSubTimer, Args.u64Now, Args.u64First, true /*fPinned*/);
661 else
662 {
663 rtTimerLnxSetState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED); /* we'll recheck it. */
664 RTSpinlockRelease(hSpinlock, &Tmp);
665
666 RTMpOnSpecific(idCpu, rtTimerLinuxMpStartOnCpu, pTimer, &Args);
667 return; /* we've left the spinlock */
668 }
669 }
670 break;
671
672 /*
673 * The CPU is (going) offline, make sure the sub-timer is stopped.
674 *
675 * Linux will migrate it to a different CPU, but we don't want this. The
676 * timer function is checking for this.
677 */
678 case RTMPEVENT_OFFLINE:
679 if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STOPPING, RTTIMERLNXSTATE_ACTIVE))
680 {
681 RTSpinlockRelease(hSpinlock, &Tmp);
682
683 rtTimerLnxStopSubTimer(pSubTimer);
684 return; /* we've left the spinlock */
685 }
686 break;
687 }
688 }
689
690 RTSpinlockRelease(hSpinlock, &Tmp);
691}
692
693#endif /* CONFIG_SMP */
694
695
696/**
697 * Callback function use by RTTimerStart via RTMpOnSpecific to start
698 * a timer running on a specific CPU.
699 *
700 * @param idCpu The current CPU.
701 * @param pvUser1 Pointer to the timer.
702 * @param pvUser2 Pointer to the argument structure.
703 */
704static DECLCALLBACK(void) rtTimerLnxStartOnSpecificCpu(RTCPUID idCpu, void *pvUser1, void *pvUser2)
705{
706 PRTTIMERLINUXSTARTONCPUARGS pArgs = (PRTTIMERLINUXSTARTONCPUARGS)pvUser2;
707 PRTTIMER pTimer = (PRTTIMER)pvUser1;
708 rtTimerLnxStartSubTimer(&pTimer->aSubTimers[0], pArgs->u64Now, pArgs->u64First, true /*fPinned*/);
709}
710
711
712RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
713{
714 RTTIMERLINUXSTARTONCPUARGS Args;
715 int rc2;
716
717 /*
718 * Validate.
719 */
720 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
721 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
722
723 if (!ASMAtomicUoReadBool(&pTimer->fSuspended))
724 return VERR_TIMER_ACTIVE;
725
726 Args.u64First = u64First;
727#ifdef CONFIG_SMP
728 /*
729 * Omnit timer?
730 */
731 if (pTimer->fAllCpus)
732 return rtTimerLnxStartAll(pTimer, &Args);
733#endif
734
735 /*
736 * Simple timer - Pretty straight forward.
737 */
738 Args.u64Now = RTTimeNanoTS();
739 rtTimerLnxSetState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STARTING);
740 ASMAtomicWriteBool(&pTimer->fSuspended, false);
741 if (!pTimer->fSpecificCpu)
742 rtTimerLnxStartSubTimer(&pTimer->aSubTimers[0], Args.u64Now, Args.u64First, false /*fPinned*/);
743 else
744 {
745 rc2 = RTMpOnSpecific(pTimer->idCpu, rtTimerLnxStartOnSpecificCpu, pTimer, &Args);
746 if (RT_FAILURE(rc2))
747 {
748 /* Suspend it, the cpu id is probably invalid or offline. */
749 ASMAtomicWriteBool(&pTimer->fSuspended, true);
750 rtTimerLnxSetState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STOPPED);
751 return rc2;
752 }
753 }
754
755 return VINF_SUCCESS;
756}
757RT_EXPORT_SYMBOL(RTTimerStart);
758
759
760RTDECL(int) RTTimerStop(PRTTIMER pTimer)
761{
762
763 /*
764 * Validate.
765 */
766 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
767 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
768
769 if (ASMAtomicUoReadBool(&pTimer->fSuspended))
770 return VERR_TIMER_SUSPENDED;
771
772#ifdef CONFIG_SMP
773 /*
774 * Omni timer?
775 */
776 if (pTimer->fAllCpus)
777 return rtTimerLnxStopAll(pTimer);
778#endif
779
780 /*
781 * Simple timer.
782 */
783 ASMAtomicWriteBool(&pTimer->fSuspended, true);
784 rtTimerLnxSetState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STOPPING);
785 rtTimerLnxStopSubTimer(&pTimer->aSubTimers[0]);
786
787 return VINF_SUCCESS;
788}
789RT_EXPORT_SYMBOL(RTTimerStop);
790
791
792RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
793{
794 RTSPINLOCK hSpinlock;
795
796 /* It's ok to pass NULL pointer. */
797 if (pTimer == /*NIL_RTTIMER*/ NULL)
798 return VINF_SUCCESS;
799 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
800 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
801
802 /*
803 * Remove the MP notifications first because it'll reduce the risk of
804 * us overtaking any MP event that might theoretically be racing us here.
805 */
806 hSpinlock = pTimer->hSpinlock;
807#ifdef CONFIG_SMP
808 if ( pTimer->cCpus > 1
809 && hSpinlock != NIL_RTSPINLOCK)
810 {
811 int rc = RTMpNotificationDeregister(rtTimerLinuxMpEvent, pTimer);
812 AssertRC(rc);
813 }
814#endif /* CONFIG_SMP */
815
816 /*
817 * Stop the timer if it's running.
818 */
819 if (!ASMAtomicUoReadBool(&pTimer->fSuspended))
820 RTTimerStop(pTimer);
821
822 /*
823 * Uninitialize the structure and free the associated resources.
824 * The spinlock goes last.
825 */
826 ASMAtomicWriteU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
827 RTMemFree(pTimer);
828 if (hSpinlock != NIL_RTSPINLOCK)
829 RTSpinlockDestroy(hSpinlock);
830
831 return VINF_SUCCESS;
832}
833RT_EXPORT_SYMBOL(RTTimerDestroy);
834
835
836RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
837{
838 PRTTIMER pTimer;
839 RTCPUID iCpu;
840 unsigned cCpus;
841
842 *ppTimer = NULL;
843
844 /*
845 * Validate flags.
846 */
847 if (!RTTIMER_FLAGS_ARE_VALID(fFlags))
848 return VERR_INVALID_PARAMETER;
849 if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
850 && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL
851 && !RTMpIsCpuOnline(fFlags & RTTIMER_FLAGS_CPU_MASK))
852 return (fFlags & RTTIMER_FLAGS_CPU_MASK) > RTMpGetMaxCpuId()
853 ? VERR_CPU_NOT_FOUND
854 : VERR_CPU_OFFLINE;
855
856 /*
857 * Allocate the timer handler.
858 */
859 cCpus = 1;
860#ifdef CONFIG_SMP
861 if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL)
862 {
863 cCpus = RTMpGetMaxCpuId() + 1;
864 Assert(cCpus <= RTCPUSET_MAX_CPUS); /* On linux we have a 1:1 relationship between cpuid and set index. */
865 AssertReturn(u64NanoInterval, VERR_NOT_IMPLEMENTED); /* We don't implement single shot on all cpus, sorry. */
866 }
867#endif
868
869 pTimer = (PRTTIMER)RTMemAllocZ(RT_OFFSETOF(RTTIMER, aSubTimers[cCpus]));
870 if (!pTimer)
871 return VERR_NO_MEMORY;
872
873 /*
874 * Initialize it.
875 */
876 pTimer->u32Magic = RTTIMER_MAGIC;
877 pTimer->hSpinlock = NIL_RTSPINLOCK;
878 pTimer->fSuspended = true;
879#ifdef CONFIG_SMP
880 pTimer->fSpecificCpu = (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC) && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL;
881 pTimer->fAllCpus = (fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL;
882 pTimer->idCpu = fFlags & RTTIMER_FLAGS_CPU_MASK;
883#else
884 pTimer->fSpecificCpu = !!(fFlags & RTTIMER_FLAGS_CPU_SPECIFIC);
885 pTimer->idCpu = RTMpCpuId();
886#endif
887 pTimer->cCpus = cCpus;
888 pTimer->pfnTimer = pfnTimer;
889 pTimer->pvUser = pvUser;
890 pTimer->u64NanoInterval = u64NanoInterval;
891#ifndef RT_USE_LINUX_HRTIMER
892 pTimer->cJiffies = u64NanoInterval / RTTimerGetSystemGranularity();
893 if (pTimer->cJiffies * RTTimerGetSystemGranularity() != u64NanoInterval)
894 pTimer->cJiffies = 0;
895#endif
896
897 for (iCpu = 0; iCpu < cCpus; iCpu++)
898 {
899#ifdef RT_USE_LINUX_HRTIMER
900 hrtimer_init(&pTimer->aSubTimers[iCpu].LnxTimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
901 pTimer->aSubTimers[iCpu].LnxTimer.function = rtTimerLinuxCallback;
902#else
903 init_timer(&pTimer->aSubTimers[iCpu].LnxTimer);
904 pTimer->aSubTimers[iCpu].LnxTimer.data = (unsigned long)&pTimer->aSubTimers[iCpu];
905 pTimer->aSubTimers[iCpu].LnxTimer.function = rtTimerLinuxCallback;
906 pTimer->aSubTimers[iCpu].LnxTimer.expires = jiffies;
907 pTimer->aSubTimers[iCpu].u64StartTS = 0;
908 pTimer->aSubTimers[iCpu].u64NextTS = 0;
909#endif
910 pTimer->aSubTimers[iCpu].iTick = 0;
911 pTimer->aSubTimers[iCpu].pParent = pTimer;
912 pTimer->aSubTimers[iCpu].enmState = RTTIMERLNXSTATE_STOPPED;
913 }
914
915#ifdef CONFIG_SMP
916 /*
917 * If this is running on ALL cpus, we'll have to register a callback
918 * for MP events (so timers can be started/stopped on cpus going
919 * online/offline). We also create the spinlock for syncrhonizing
920 * stop/start/mp-event.
921 */
922 if (cCpus > 1)
923 {
924 int rc = RTSpinlockCreate(&pTimer->hSpinlock);
925 if (RT_SUCCESS(rc))
926 rc = RTMpNotificationRegister(rtTimerLinuxMpEvent, pTimer);
927 else
928 pTimer->hSpinlock = NIL_RTSPINLOCK;
929 if (RT_FAILURE(rc))
930 {
931 RTTimerDestroy(pTimer);
932 return rc;
933 }
934 }
935#endif /* CONFIG_SMP */
936
937 *ppTimer = pTimer;
938 return VINF_SUCCESS;
939}
940RT_EXPORT_SYMBOL(RTTimerCreateEx);
941
942
943RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
944{
945#ifdef RT_USE_LINUX_HRTIMER
946 struct timespec Ts;
947 int rc = hrtimer_get_res(CLOCK_MONOTONIC, &Ts);
948 if (!rc)
949 {
950 Assert(!Ts.tv_sec);
951 return Ts.tv_nsec;
952 }
953#endif
954 return 1000000000 / HZ; /* ns */
955}
956RT_EXPORT_SYMBOL(RTTimerGetSystemGranularity);
957
958
959RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
960{
961 return VERR_NOT_SUPPORTED;
962}
963RT_EXPORT_SYMBOL(RTTimerRequestSystemGranularity);
964
965
966RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
967{
968 return VERR_NOT_SUPPORTED;
969}
970RT_EXPORT_SYMBOL(RTTimerReleaseSystemGranularity);
971
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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