VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/TMAll.cpp@ 2248

Last change on this file since 2248 was 2248, checked in by vboxsync, 18 years ago

Implementing timer syncrhonous virtual clock.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 42.8 KB
Line 
1/* $Id: TMAll.cpp 2248 2007-04-19 21:43:29Z vboxsync $ */
2/** @file
3 * TM - Timeout Manager, all contexts.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_TM
27#include <VBox/tm.h>
28#include <VBox/mm.h>
29#ifdef IN_RING3
30# include <VBox/rem.h>
31#endif
32#include "TMInternal.h"
33#include <VBox/vm.h>
34
35#include <VBox/param.h>
36#include <VBox/err.h>
37#include <VBox/log.h>
38#include <VBox/sup.h>
39#include <iprt/time.h>
40#include <iprt/assert.h>
41#include <iprt/asm.h>
42#ifdef IN_RING3
43# include <iprt/thread.h>
44#endif
45
46
47/**
48 * Schedule the queue which was changed.
49 */
50DECLINLINE(void) tmSchedule(PTMTIMER pTimer)
51{
52 PVM pVM = pTimer->CTXALLSUFF(pVM);
53 if (VM_IS_EMT(pVM))
54 {
55 STAM_PROFILE_START(&pVM->tm.s.CTXALLSUFF(StatScheduleOne), a);
56 PTMTIMERQUEUE pQueue = &pVM->tm.s.CTXALLSUFF(paTimerQueues)[pTimer->enmClock];
57 Log3(("tmSchedule: tmTimerQueueSchedule\n"));
58 tmTimerQueueSchedule(pVM, pQueue);
59#ifdef VBOX_STRICT
60 tmTimerQueuesSanityChecks(pVM, "tmSchedule");
61#endif
62 STAM_PROFILE_STOP(&pVM->tm.s.CTXALLSUFF(StatScheduleOne), a);
63 }
64 else if (!VM_FF_ISSET(pVM, VM_FF_TIMER)) /**@todo only do this when arming the timer. */
65 {
66 STAM_COUNTER_INC(&pVM->tm.s.StatScheduleSetFF);
67 VM_FF_SET(pVM, VM_FF_TIMER);
68#ifdef IN_RING3
69 REMR3NotifyTimerPending(pVM);
70 VMR3NotifyFF(pVM, true);
71#endif
72 }
73}
74
75
76/**
77 * Try change the state to enmStateNew from enmStateOld
78 * and link the timer into the scheduling queue.
79 *
80 * @returns Success indicator.
81 * @param pTimer Timer in question.
82 * @param enmStateNew The new timer state.
83 * @param enmStateOld The old timer state.
84 */
85DECLINLINE(bool) tmTimerTry(PTMTIMER pTimer, TMTIMERSTATE enmStateNew, TMTIMERSTATE enmStateOld)
86{
87 /*
88 * Attempt state change.
89 */
90 bool fRc;
91 TM_TRY_SET_STATE(pTimer, enmStateNew, enmStateOld, fRc);
92 return fRc;
93}
94
95
96/**
97 * Links the timer onto the scheduling queue.
98 *
99 * @param pQueue The timer queue the timer belongs to.
100 * @param pTimer The timer.
101 */
102DECLINLINE(void) tmTimerLink(PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
103{
104 Assert(!pTimer->offScheduleNext);
105 const int32_t offHeadNew = (intptr_t)pTimer - (intptr_t)pQueue;
106 int32_t offHead;
107 do
108 {
109 offHead = pQueue->offSchedule;
110 if (offHead)
111 pTimer->offScheduleNext = ((intptr_t)pQueue + offHead) - (intptr_t)pTimer;
112 else
113 pTimer->offScheduleNext = 0;
114 } while (!ASMAtomicCmpXchgS32(&pQueue->offSchedule, offHeadNew, offHead));
115}
116
117
118/**
119 * Try change the state to enmStateNew from enmStateOld
120 * and link the timer into the scheduling queue.
121 *
122 * @returns Success indicator.
123 * @param pTimer Timer in question.
124 * @param enmStateNew The new timer state.
125 * @param enmStateOld The old timer state.
126 */
127DECLINLINE(bool) tmTimerTryWithLink(PTMTIMER pTimer, TMTIMERSTATE enmStateNew, TMTIMERSTATE enmStateOld)
128{
129 if (tmTimerTry(pTimer, enmStateNew, enmStateOld))
130 {
131 tmTimerLink(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(paTimerQueues)[pTimer->enmClock], pTimer);
132 return true;
133 }
134 return false;
135}
136
137
138#ifdef VBOX_HIGH_RES_TIMERS_HACK
139/**
140 * Set FF if we've passed the next virtual event.
141 *
142 * This function is called before FFs are checked in the inner execution EM loops.
143 *
144 * @returns Virtual timer ticks to the next event.
145 * @thread The emulation thread.
146 */
147TMDECL(uint64_t) TMTimerPoll(PVM pVM)
148{
149 /*
150 * Return straight away if the timer FF is already set.
151 */
152 if (VM_FF_ISSET(pVM, VM_FF_TIMER))
153 {
154 STAM_COUNTER_INC(&pVM->tm.s.StatPollAlreadySet);
155 return 0;
156 }
157
158 /*
159 * Get current time and check the expire times of the two relevant queues.
160 */
161 const uint64_t u64Now = TMVirtualGet(pVM);
162
163 /* TMCLOCK_VIRTUAL */
164 const uint64_t u64Expire1 = pVM->tm.s.CTXALLSUFF(paTimerQueues)[TMCLOCK_VIRTUAL].u64Expire;
165 const int64_t i64Delta1 = u64Expire1 - u64Now;
166 if (i64Delta1 <= 0)
167 {
168 LogFlow(("TMTimerPoll: expire1=%RU64 <= now=%RU64\n", u64Expire1, u64Now));
169 STAM_COUNTER_INC(&pVM->tm.s.StatPollVirtual);
170 VM_FF_SET(pVM, VM_FF_TIMER);
171#ifdef IN_RING3
172 REMR3NotifyTimerPending(pVM);
173#endif
174 return 0;
175 }
176
177 /* TMCLOCK_VIRTUAL_SYNC */
178 const uint64_t u64Expire2 = pVM->tm.s.CTXALLSUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire;
179 const int64_t i64Delta2 = u64Expire2 - (u64Now - pVM->tm.s.u64VirtualSyncOffset);
180 if (i64Delta2 <= 0)
181 {
182 LogFlow(("TMTimerPoll: expire2=%RU64 <= now=%RU64\n", u64Expire2, u64Now));
183 STAM_COUNTER_INC(&pVM->tm.s.StatPollVirtualSync);
184 VM_FF_SET(pVM, VM_FF_TIMER);
185#ifdef IN_RING3
186 REMR3NotifyTimerPending(pVM);
187#endif
188 return 0;
189 }
190
191 /*
192 * Return the time self to the next event.
193 */
194 STAM_COUNTER_INC(&pVM->tm.s.StatPollMiss);
195 return RT_MIN(i64Delta1, i64Delta2);
196}
197#endif
198
199/**
200 * Gets the host context ring-3 pointer of the timer.
201 *
202 * @returns HC R3 pointer.
203 * @param pTimer Timer handle as returned by one of the create functions.
204 */
205TMDECL(PTMTIMERR3) TMTimerR3Ptr(PTMTIMER pTimer)
206{
207 return (PTMTIMERR3)MMHyperCCToR3(pTimer->CTXALLSUFF(pVM), pTimer);
208}
209
210
211/**
212 * Gets the host context ring-0 pointer of the timer.
213 *
214 * @returns HC R0 pointer.
215 * @param pTimer Timer handle as returned by one of the create functions.
216 */
217TMDECL(PTMTIMERR0) TMTimerR0Ptr(PTMTIMER pTimer)
218{
219 return (PTMTIMERR0)MMHyperCCToR0(pTimer->CTXALLSUFF(pVM), pTimer);
220}
221
222
223/**
224 * Gets the GC pointer of the timer.
225 *
226 * @returns GC pointer.
227 * @param pTimer Timer handle as returned by one of the create functions.
228 */
229TMDECL(PTMTIMERGC) TMTimerGCPtr(PTMTIMER pTimer)
230{
231 return (PTMTIMERGC)MMHyperCCToGC(pTimer->CTXALLSUFF(pVM), pTimer);
232}
233
234
235/**
236 * Destroy a timer
237 *
238 * @returns VBox status.
239 * @param pTimer Timer handle as returned by one of the create functions.
240 */
241TMDECL(int) TMTimerDestroy(PTMTIMER pTimer)
242{
243 int cRetries = 1000;
244 do
245 {
246 /*
247 * Change to any of the DESTROY states if valid.
248 */
249 TMTIMERSTATE enmState = pTimer->enmState;
250 Log2(("TMTimerDestroy: pTimer=%p:{.enmState=%s, .pszDesc='%s'} cRetries=%d\n",
251 pTimer, tmTimerState(enmState), HCSTRING(pTimer->pszDesc), cRetries));
252 switch (enmState)
253 {
254 case TMTIMERSTATE_EXPIRED:
255 if (!VM_IS_EMT(pTimer->CTXALLSUFF(pVM)))
256 {
257 AssertMsgFailed(("Attempted timer destruction from other thread while expire pending! (%s)\n", HCSTRING(pTimer->pszDesc)));
258 return VERR_INVALID_PARAMETER;
259 }
260 /* fall thru */
261 case TMTIMERSTATE_STOPPED:
262 if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_DESTROY, enmState))
263 {
264 tmSchedule(pTimer);
265 return VINF_SUCCESS;
266 }
267 break;
268
269 case TMTIMERSTATE_ACTIVE:
270 if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_STOP_DESTROY, enmState))
271 {
272 tmSchedule(pTimer);
273 return VINF_SUCCESS;
274 }
275 break;
276
277 case TMTIMERSTATE_PENDING_STOP:
278 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
279 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_DESTROY, enmState))
280 {
281 tmSchedule(pTimer);
282 return VINF_SUCCESS;
283 }
284 break;
285
286 case TMTIMERSTATE_PENDING_DESTROY:
287 case TMTIMERSTATE_PENDING_STOP_DESTROY:
288 AssertMsgFailed(("How many times do you think you can destroy the same timer... (%s)\n", HCSTRING(pTimer->pszDesc)));
289 return VERR_INVALID_PARAMETER;
290
291 case TMTIMERSTATE_PENDING_RESCHEDULE:
292 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_DESTROY, enmState))
293 {
294 tmSchedule(pTimer);
295 return VINF_SUCCESS;
296 }
297 break;
298
299 case TMTIMERSTATE_PENDING_SCHEDULE:
300 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_DESTROY, enmState))
301 {
302 tmSchedule(pTimer);
303 return VINF_SUCCESS;
304 }
305 break;
306
307 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
308 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
309#ifdef IN_RING3
310 if (!RTThreadYield())
311 RTThreadSleep(1);
312#endif
313 break;
314
315 /*
316 * Invalid states.
317 */
318 case TMTIMERSTATE_FREE:
319 AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, HCSTRING(pTimer->pszDesc)));
320 return VERR_TM_INVALID_STATE;
321 default:
322 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, HCSTRING(pTimer->pszDesc)));
323 return VERR_TM_UNKNOWN_STATE;
324 }
325 } while (cRetries-- > 0);
326
327 AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, HCSTRING(pTimer->pszDesc)));
328 return VERR_INTERNAL_ERROR;
329}
330
331
332/**
333 * Arm a timer with a (new) expire time.
334 *
335 * @returns VBox status.
336 * @param pTimer Timer handle as returned by one of the create functions.
337 * @param u64Expire New expire time.
338 */
339TMDECL(int) TMTimerSet(PTMTIMER pTimer, uint64_t u64Expire)
340{
341 STAM_PROFILE_START(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerSet), a);
342
343 /** @todo find the most frequently used paths and make them skip tmSchedule and tmTimerTryWithLink. */
344 int cRetries = 1000;
345 do
346 {
347 /*
348 * Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
349 */
350 TMTIMERSTATE enmState = pTimer->enmState;
351 Log2(("TMTimerSet: pTimer=%p:{.enmState=%s, .pszDesc='%s'} cRetries=%d u64Expire=%llu\n",
352 pTimer, tmTimerState(enmState), HCSTRING(pTimer->pszDesc), cRetries, u64Expire));
353 switch (enmState)
354 {
355 case TMTIMERSTATE_EXPIRED:
356 case TMTIMERSTATE_STOPPED:
357 if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
358 {
359 Assert(!pTimer->offPrev);
360 Assert(!pTimer->offNext);
361 pTimer->u64Expire = u64Expire;
362 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
363 tmSchedule(pTimer);
364 STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerSet), a);
365 return VINF_SUCCESS;
366 }
367 break;
368
369 case TMTIMERSTATE_PENDING_SCHEDULE:
370 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
371 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE, enmState))
372 {
373 Assert(!pTimer->offPrev);
374 Assert(!pTimer->offNext);
375 pTimer->u64Expire = u64Expire;
376 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_SCHEDULE);
377 tmSchedule(pTimer);
378 STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerSet), a);
379 return VINF_SUCCESS;
380 }
381 break;
382
383
384 case TMTIMERSTATE_ACTIVE:
385 if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
386 {
387 pTimer->u64Expire = u64Expire;
388 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
389 tmSchedule(pTimer);
390 STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerSet), a);
391 return VINF_SUCCESS;
392 }
393 break;
394
395 case TMTIMERSTATE_PENDING_RESCHEDULE:
396 case TMTIMERSTATE_PENDING_STOP:
397 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE, enmState))
398 {
399 pTimer->u64Expire = u64Expire;
400 TM_SET_STATE(pTimer, TMTIMERSTATE_PENDING_RESCHEDULE);
401 tmSchedule(pTimer);
402 STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerSet), a);
403 return VINF_SUCCESS;
404 }
405 break;
406
407
408 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
409 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
410#ifdef IN_RING3
411 if (!RTThreadYield())
412 RTThreadSleep(1);
413#else
414/** @todo call host context and yield after a couple of iterations */
415#endif
416 break;
417
418 /*
419 * Invalid states.
420 */
421 case TMTIMERSTATE_PENDING_DESTROY:
422 case TMTIMERSTATE_PENDING_STOP_DESTROY:
423 case TMTIMERSTATE_FREE:
424 AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, HCSTRING(pTimer->pszDesc)));
425 return VERR_TM_INVALID_STATE;
426 default:
427 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, HCSTRING(pTimer->pszDesc)));
428 return VERR_TM_UNKNOWN_STATE;
429 }
430 } while (cRetries-- > 0);
431
432 AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, HCSTRING(pTimer->pszDesc)));
433 STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerSet), a);
434 return VERR_INTERNAL_ERROR;
435}
436
437
438/**
439 * Arm a timer with a (new) expire time relative to current clock.
440 *
441 * @returns VBox status.
442 * @param pTimer Timer handle as returned by one of the create functions.
443 * @param cMilliesToNext Number of millieseconds to the next tick.
444 */
445TMDECL(int) TMTimerSetMillies(PTMTIMER pTimer, uint32_t cMilliesToNext)
446{
447 PVM pVM = pTimer->CTXALLSUFF(pVM);
448 switch (pTimer->enmClock)
449 {
450 case TMCLOCK_VIRTUAL:
451 return TMTimerSet(pTimer, cMilliesToNext * (uint64_t)TMCLOCK_FREQ_VIRTUAL / 1000 + TMVirtualGet(pVM));
452 case TMCLOCK_VIRTUAL_SYNC:
453 return TMTimerSet(pTimer, cMilliesToNext * (uint64_t)TMCLOCK_FREQ_VIRTUAL / 1000 + TMVirtualSyncGet(pVM));
454 case TMCLOCK_REAL:
455 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
456 return TMTimerSet(pTimer, cMilliesToNext + TMRealGet(pVM));
457 case TMCLOCK_TSC:
458 return TMTimerSet(pTimer, cMilliesToNext * pVM->tm.s.cTSCTicksPerSecond / 1000 + TMCpuTickGet(pVM));
459
460 default:
461 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
462 return VERR_INTERNAL_ERROR;
463 }
464}
465
466
467/**
468 * Stop the timer.
469 * Use TMR3TimerArm() to "un-stop" the timer.
470 *
471 * @returns VBox status.
472 * @param pTimer Timer handle as returned by one of the create functions.
473 */
474TMDECL(int) TMTimerStop(PTMTIMER pTimer)
475{
476 STAM_PROFILE_START(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerStop), a);
477 /** @todo see if this function needs optimizing. */
478 int cRetries = 1000;
479 do
480 {
481 /*
482 * Change to any of the SET_EXPIRE states if valid and then to SCHEDULE or RESCHEDULE.
483 */
484 TMTIMERSTATE enmState = pTimer->enmState;
485 Log2(("TMTimerStop: pTimer=%p:{.enmState=%s, .pszDesc='%s'} cRetries=%d\n",
486 pTimer, tmTimerState(enmState), HCSTRING(pTimer->pszDesc), cRetries));
487 switch (enmState)
488 {
489 case TMTIMERSTATE_EXPIRED:
490 //AssertMsgFailed(("You don't stop an expired timer dude!\n"));
491 return VERR_INVALID_PARAMETER;
492
493 case TMTIMERSTATE_STOPPED:
494 case TMTIMERSTATE_PENDING_STOP:
495 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
496 STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerStop), a);
497 return VINF_SUCCESS;
498
499 case TMTIMERSTATE_PENDING_SCHEDULE:
500 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP_SCHEDULE, enmState))
501 {
502 Assert(!pTimer->offPrev);
503 Assert(!pTimer->offNext);
504 tmSchedule(pTimer);
505 STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerStop), a);
506 return VINF_SUCCESS;
507 }
508
509 case TMTIMERSTATE_PENDING_RESCHEDULE:
510 if (tmTimerTry(pTimer, TMTIMERSTATE_PENDING_STOP, enmState))
511 {
512 tmSchedule(pTimer);
513 STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerStop), a);
514 return VINF_SUCCESS;
515 }
516 break;
517
518 case TMTIMERSTATE_ACTIVE:
519 if (tmTimerTryWithLink(pTimer, TMTIMERSTATE_PENDING_STOP, enmState))
520 {
521 tmSchedule(pTimer);
522 STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerStop), a);
523 return VINF_SUCCESS;
524 }
525 break;
526
527 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
528 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
529#ifdef IN_RING3
530 if (!RTThreadYield())
531 RTThreadSleep(1);
532#else
533/**@todo call host and yield cpu after a while. */
534#endif
535 break;
536
537 /*
538 * Invalid states.
539 */
540 case TMTIMERSTATE_PENDING_DESTROY:
541 case TMTIMERSTATE_PENDING_STOP_DESTROY:
542 case TMTIMERSTATE_FREE:
543 AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, HCSTRING(pTimer->pszDesc)));
544 return VERR_TM_INVALID_STATE;
545 default:
546 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, HCSTRING(pTimer->pszDesc)));
547 return VERR_TM_UNKNOWN_STATE;
548 }
549 } while (cRetries-- > 0);
550
551 AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, HCSTRING(pTimer->pszDesc)));
552 STAM_PROFILE_STOP(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatTimerStop), a);
553 return VERR_INTERNAL_ERROR;
554}
555
556
557/**
558 * Get the current clock time.
559 * Handy for calculating the new expire time.
560 *
561 * @returns Current clock time.
562 * @param pTimer Timer handle as returned by one of the create functions.
563 */
564TMDECL(uint64_t) TMTimerGet(PTMTIMER pTimer)
565{
566 uint64_t u64;
567 PVM pVM = pTimer->CTXALLSUFF(pVM);
568 switch (pTimer->enmClock)
569 {
570 case TMCLOCK_VIRTUAL:
571 u64 = TMVirtualGet(pVM);
572 break;
573 case TMCLOCK_VIRTUAL_SYNC:
574 u64 = TMVirtualSyncGet(pVM);
575 break;
576 case TMCLOCK_REAL:
577 u64 = TMRealGet(pVM);
578 break;
579 case TMCLOCK_TSC:
580 u64 = TMCpuTickGet(pVM);
581 break;
582
583 default:
584 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
585 return ~(uint64_t)0;
586 }
587 //Log2(("TMTimerGet: returns %llu (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
588 // u64, pTimer, tmTimerState(pTimer->enmState), HCSTRING(pTimer->pszDesc)));
589 return u64;
590}
591
592
593/**
594 * Get the freqency of the timer clock.
595 *
596 * @returns Clock frequency (as Hz of course).
597 * @param pTimer Timer handle as returned by one of the create functions.
598 */
599TMDECL(uint64_t) TMTimerGetFreq(PTMTIMER pTimer)
600{
601 switch (pTimer->enmClock)
602 {
603 case TMCLOCK_VIRTUAL:
604 case TMCLOCK_VIRTUAL_SYNC:
605 return TMCLOCK_FREQ_VIRTUAL;
606
607 case TMCLOCK_REAL:
608 return TMCLOCK_FREQ_REAL;
609
610 case TMCLOCK_TSC:
611 return TMCpuTicksPerSecond(pTimer->CTXALLSUFF(pVM));
612
613 default:
614 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
615 return 0;
616 }
617}
618
619
620/**
621 * Get the current clock time as nanoseconds.
622 *
623 * @returns The timer clock as nanoseconds.
624 * @param pTimer Timer handle as returned by one of the create functions.
625 */
626TMDECL(uint64_t) TMTimerGetNano(PTMTIMER pTimer)
627{
628 return TMTimerToNano(pTimer, TMTimerGet(pTimer));
629}
630
631
632/**
633 * Get the current clock time as microseconds.
634 *
635 * @returns The timer clock as microseconds.
636 * @param pTimer Timer handle as returned by one of the create functions.
637 */
638TMDECL(uint64_t) TMTimerGetMicro(PTMTIMER pTimer)
639{
640 return TMTimerToMicro(pTimer, TMTimerGet(pTimer));
641}
642
643
644/**
645 * Get the current clock time as milliseconds.
646 *
647 * @returns The timer clock as milliseconds.
648 * @param pTimer Timer handle as returned by one of the create functions.
649 */
650TMDECL(uint64_t) TMTimerGetMilli(PTMTIMER pTimer)
651{
652 return TMTimerToMilli(pTimer, TMTimerGet(pTimer));
653}
654
655
656/**
657 * Converts the specified timer clock time to nanoseconds.
658 *
659 * @returns nanoseconds.
660 * @param pTimer Timer handle as returned by one of the create functions.
661 * @param u64Ticks The clock ticks.
662 * @remark There could be rounding errors here. We just do a simple integere divide
663 * without any adjustments.
664 */
665TMDECL(uint64_t) TMTimerToNano(PTMTIMER pTimer, uint64_t u64Ticks)
666{
667 switch (pTimer->enmClock)
668 {
669 case TMCLOCK_VIRTUAL:
670 case TMCLOCK_VIRTUAL_SYNC:
671 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
672 return u64Ticks;
673
674 case TMCLOCK_REAL:
675 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
676 return u64Ticks * 1000000;
677
678 case TMCLOCK_TSC:
679 AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
680 return 0;
681
682 default:
683 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
684 return 0;
685 }
686}
687
688
689/**
690 * Converts the specified timer clock time to microseconds.
691 *
692 * @returns microseconds.
693 * @param pTimer Timer handle as returned by one of the create functions.
694 * @param u64Ticks The clock ticks.
695 * @remark There could be rounding errors here. We just do a simple integere divide
696 * without any adjustments.
697 */
698TMDECL(uint64_t) TMTimerToMicro(PTMTIMER pTimer, uint64_t u64Ticks)
699{
700 switch (pTimer->enmClock)
701 {
702 case TMCLOCK_VIRTUAL:
703 case TMCLOCK_VIRTUAL_SYNC:
704 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
705 return u64Ticks / 1000;
706
707 case TMCLOCK_REAL:
708 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
709 return u64Ticks * 1000;
710
711 case TMCLOCK_TSC:
712 AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
713 return 0;
714
715 default:
716 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
717 return 0;
718 }
719}
720
721
722/**
723 * Converts the specified timer clock time to milliseconds.
724 *
725 * @returns milliseconds.
726 * @param pTimer Timer handle as returned by one of the create functions.
727 * @param u64Ticks The clock ticks.
728 * @remark There could be rounding errors here. We just do a simple integere divide
729 * without any adjustments.
730 */
731TMDECL(uint64_t) TMTimerToMilli(PTMTIMER pTimer, uint64_t u64Ticks)
732{
733 switch (pTimer->enmClock)
734 {
735 case TMCLOCK_VIRTUAL:
736 case TMCLOCK_VIRTUAL_SYNC:
737 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
738 return u64Ticks / 1000000;
739
740 case TMCLOCK_REAL:
741 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
742 return u64Ticks;
743
744 case TMCLOCK_TSC:
745 AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
746 return 0;
747
748 default:
749 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
750 return 0;
751 }
752}
753
754
755/**
756 * Converts the specified nanosecond timestamp to timer clock ticks.
757 *
758 * @returns timer clock ticks.
759 * @param pTimer Timer handle as returned by one of the create functions.
760 * @param u64NanoTS The nanosecond value ticks to convert.
761 * @remark There could be rounding and overflow errors here.
762 */
763TMDECL(uint64_t) TMTimerFromNano(PTMTIMER pTimer, uint64_t u64NanoTS)
764{
765 switch (pTimer->enmClock)
766 {
767 case TMCLOCK_VIRTUAL:
768 case TMCLOCK_VIRTUAL_SYNC:
769 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
770 return u64NanoTS;
771
772 case TMCLOCK_REAL:
773 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
774 return u64NanoTS / 1000000;
775
776 case TMCLOCK_TSC:
777 AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
778 return 0;
779
780 default:
781 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
782 return 0;
783 }
784}
785
786
787/**
788 * Converts the specified microsecond timestamp to timer clock ticks.
789 *
790 * @returns timer clock ticks.
791 * @param pTimer Timer handle as returned by one of the create functions.
792 * @param u64MicroTS The microsecond value ticks to convert.
793 * @remark There could be rounding and overflow errors here.
794 */
795TMDECL(uint64_t) TMTimerFromMicro(PTMTIMER pTimer, uint64_t u64MicroTS)
796{
797 switch (pTimer->enmClock)
798 {
799 case TMCLOCK_VIRTUAL:
800 case TMCLOCK_VIRTUAL_SYNC:
801 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
802 return u64MicroTS * 1000;
803
804 case TMCLOCK_REAL:
805 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
806 return u64MicroTS / 1000;
807
808 case TMCLOCK_TSC:
809 AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
810 return 0;
811
812 default:
813 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
814 return 0;
815 }
816}
817
818
819/**
820 * Converts the specified millisecond timestamp to timer clock ticks.
821 *
822 * @returns timer clock ticks.
823 * @param pTimer Timer handle as returned by one of the create functions.
824 * @param u64MilliTS The millisecond value ticks to convert.
825 * @remark There could be rounding and overflow errors here.
826 */
827TMDECL(uint64_t) TMTimerFromMilli(PTMTIMER pTimer, uint64_t u64MilliTS)
828{
829 switch (pTimer->enmClock)
830 {
831 case TMCLOCK_VIRTUAL:
832 case TMCLOCK_VIRTUAL_SYNC:
833 AssertCompile(TMCLOCK_FREQ_VIRTUAL == 1000000000);
834 return u64MilliTS * 1000000;
835
836 case TMCLOCK_REAL:
837 AssertCompile(TMCLOCK_FREQ_REAL == 1000);
838 return u64MilliTS;
839
840 case TMCLOCK_TSC:
841 AssertReleaseMsgFailed(("TMCLOCK_TSC conversions are not implemented\n"));
842 return 0;
843
844 default:
845 AssertMsgFailed(("Invalid enmClock=%d\n", pTimer->enmClock));
846 return 0;
847 }
848}
849
850
851/**
852 * Get the expire time of the timer.
853 * Only valid for active timers.
854 *
855 * @returns Expire time of the timer.
856 * @param pTimer Timer handle as returned by one of the create functions.
857 */
858TMDECL(uint64_t) TMTimerGetExpire(PTMTIMER pTimer)
859{
860 int cRetries = 1000;
861 do
862 {
863 TMTIMERSTATE enmState = pTimer->enmState;
864 switch (enmState)
865 {
866 case TMTIMERSTATE_EXPIRED:
867 case TMTIMERSTATE_STOPPED:
868 case TMTIMERSTATE_PENDING_STOP:
869 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
870 Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
871 pTimer, tmTimerState(pTimer->enmState), HCSTRING(pTimer->pszDesc)));
872 return ~(uint64_t)0;
873
874 case TMTIMERSTATE_ACTIVE:
875 case TMTIMERSTATE_PENDING_RESCHEDULE:
876 case TMTIMERSTATE_PENDING_SCHEDULE:
877 Log2(("TMTimerGetExpire: returns %llu (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
878 pTimer->u64Expire, pTimer, tmTimerState(pTimer->enmState), HCSTRING(pTimer->pszDesc)));
879 return pTimer->u64Expire;
880
881 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
882 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
883#ifdef IN_RING3
884 if (!RTThreadYield())
885 RTThreadSleep(1);
886#endif
887 break;
888
889 /*
890 * Invalid states.
891 */
892 case TMTIMERSTATE_PENDING_DESTROY:
893 case TMTIMERSTATE_PENDING_STOP_DESTROY:
894 case TMTIMERSTATE_FREE:
895 AssertMsgFailed(("Invalid timer state %d (%s)\n", enmState, HCSTRING(pTimer->pszDesc)));
896 Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
897 pTimer, tmTimerState(pTimer->enmState), HCSTRING(pTimer->pszDesc)));
898 return ~(uint64_t)0;
899 default:
900 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, HCSTRING(pTimer->pszDesc)));
901 return ~(uint64_t)0;
902 }
903 } while (cRetries-- > 0);
904
905 AssertMsgFailed(("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, HCSTRING(pTimer->pszDesc)));
906 Log2(("TMTimerGetExpire: returns ~0 (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
907 pTimer, tmTimerState(pTimer->enmState), HCSTRING(pTimer->pszDesc)));
908 return ~(uint64_t)0;
909}
910
911
912/**
913 * Checks if a timer is active or not.
914 *
915 * @returns True if active.
916 * @returns False if not active.
917 * @param pTimer Timer handle as returned by one of the create functions.
918 */
919TMDECL(bool) TMTimerIsActive(PTMTIMER pTimer)
920{
921 TMTIMERSTATE enmState = pTimer->enmState;
922 switch (enmState)
923 {
924 case TMTIMERSTATE_STOPPED:
925 case TMTIMERSTATE_EXPIRED:
926 case TMTIMERSTATE_PENDING_STOP:
927 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
928 Log2(("TMTimerIsActive: returns false (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
929 pTimer, tmTimerState(pTimer->enmState), HCSTRING(pTimer->pszDesc)));
930 return false;
931
932 case TMTIMERSTATE_ACTIVE:
933 case TMTIMERSTATE_PENDING_RESCHEDULE:
934 case TMTIMERSTATE_PENDING_SCHEDULE:
935 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
936 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
937 Log2(("TMTimerIsActive: returns true (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
938 pTimer, tmTimerState(pTimer->enmState), HCSTRING(pTimer->pszDesc)));
939 return true;
940
941 /*
942 * Invalid states.
943 */
944 case TMTIMERSTATE_PENDING_DESTROY:
945 case TMTIMERSTATE_PENDING_STOP_DESTROY:
946 case TMTIMERSTATE_FREE:
947 AssertMsgFailed(("Invalid timer state %s (%s)\n", tmTimerState(enmState), HCSTRING(pTimer->pszDesc)));
948 Log2(("TMTimerIsActive: returns false (pTimer=%p:{.enmState=%s, .pszDesc='%s'})\n",
949 pTimer, tmTimerState(pTimer->enmState), HCSTRING(pTimer->pszDesc)));
950 return false;
951 default:
952 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, HCSTRING(pTimer->pszDesc)));
953 return false;
954 }
955}
956
957
958/**
959 * Convert state to string.
960 *
961 * @returns Readonly status name.
962 * @param enmState State.
963 */
964const char *tmTimerState(TMTIMERSTATE enmState)
965{
966 switch (enmState)
967 {
968#define CASE(state) case state: return #state + sizeof("TMTIMERSTATE_") - 1
969 CASE(TMTIMERSTATE_STOPPED);
970 CASE(TMTIMERSTATE_ACTIVE);
971 CASE(TMTIMERSTATE_EXPIRED);
972 CASE(TMTIMERSTATE_PENDING_STOP);
973 CASE(TMTIMERSTATE_PENDING_STOP_SCHEDULE);
974 CASE(TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE);
975 CASE(TMTIMERSTATE_PENDING_SCHEDULE);
976 CASE(TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE);
977 CASE(TMTIMERSTATE_PENDING_RESCHEDULE);
978 CASE(TMTIMERSTATE_PENDING_STOP_DESTROY);
979 CASE(TMTIMERSTATE_PENDING_DESTROY);
980 CASE(TMTIMERSTATE_FREE);
981 default:
982 AssertMsgFailed(("Invalid state enmState=%d\n", enmState));
983 return "Invalid state!";
984#undef CASE
985 }
986}
987
988
989/**
990 * Schedules the given timer on the given queue.
991 *
992 * @param pQueue The timer queue.
993 * @param pTimer The timer that needs scheduling.
994 */
995DECLINLINE(void) tmTimerQueueScheduleOne(PTMTIMERQUEUE pQueue, PTMTIMER pTimer)
996{
997 /*
998 * Processing.
999 */
1000 switch (pTimer->enmState)
1001 {
1002 /*
1003 * Reschedule timer (in the active list).
1004 */
1005 case TMTIMERSTATE_PENDING_RESCHEDULE:
1006 {
1007 const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
1008 const PTMTIMER pNext = TMTIMER_GET_NEXT(pTimer);
1009 if (pPrev)
1010 TMTIMER_SET_NEXT(pPrev, pNext);
1011 else
1012 {
1013 TMTIMER_SET_HEAD(pQueue, pNext);
1014 pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
1015 }
1016 if (pNext)
1017 TMTIMER_SET_PREV(pNext, pPrev);
1018 pTimer->offNext = 0;
1019 pTimer->offPrev = 0;
1020 /* fall thru */
1021 }
1022
1023 /*
1024 * Schedule timer (insert into the active list).
1025 */
1026 case TMTIMERSTATE_PENDING_SCHEDULE:
1027 {
1028 Assert(!pTimer->offNext); Assert(!pTimer->offPrev);
1029 TM_SET_STATE(pTimer, TMTIMERSTATE_ACTIVE);
1030 PTMTIMER pCur = TMTIMER_GET_HEAD(pQueue);
1031 if (pCur)
1032 {
1033 const uint64_t u64Expire = pTimer->u64Expire;
1034 for (;; pCur = TMTIMER_GET_NEXT(pCur))
1035 {
1036 if (pCur->u64Expire > u64Expire)
1037 {
1038 const PTMTIMER pPrev = TMTIMER_GET_PREV(pCur);
1039 TMTIMER_SET_NEXT(pTimer, pCur);
1040 TMTIMER_SET_PREV(pTimer, pPrev);
1041 if (pPrev)
1042 TMTIMER_SET_NEXT(pPrev, pTimer);
1043 else
1044 {
1045 TMTIMER_SET_HEAD(pQueue, pTimer);
1046 pQueue->u64Expire = u64Expire;
1047 }
1048 TMTIMER_SET_PREV(pCur, pTimer);
1049 break;
1050 }
1051 else if (!pCur->offNext)
1052 {
1053 TMTIMER_SET_NEXT(pCur, pTimer);
1054 TMTIMER_SET_PREV(pTimer, pCur);
1055 break;
1056 }
1057 }
1058 }
1059 else
1060 {
1061 TMTIMER_SET_HEAD(pQueue, pTimer);
1062 pQueue->u64Expire = pTimer->u64Expire;
1063 }
1064 break;
1065 }
1066
1067 /*
1068 * Stop the timer in active list.
1069 */
1070 case TMTIMERSTATE_PENDING_STOP:
1071 {
1072 const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
1073 const PTMTIMER pNext = TMTIMER_GET_NEXT(pTimer);
1074 if (pPrev)
1075 TMTIMER_SET_NEXT(pPrev, pNext);
1076 else
1077 {
1078 TMTIMER_SET_HEAD(pQueue, pNext);
1079 pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
1080 }
1081 if (pNext)
1082 TMTIMER_SET_PREV(pNext, pPrev);
1083 pTimer->offNext = 0;
1084 pTimer->offPrev = 0;
1085 /* fall thru */
1086 }
1087
1088 /*
1089 * Stop the timer (not on the active list).
1090 */
1091 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1092 Assert(!pTimer->offNext); Assert(!pTimer->offPrev);
1093 TM_SET_STATE(pTimer, TMTIMERSTATE_STOPPED);
1094 break;
1095
1096 /*
1097 * Stop & destroy the timer.
1098 */
1099 case TMTIMERSTATE_PENDING_STOP_DESTROY:
1100 {
1101 const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
1102 const PTMTIMER pNext = TMTIMER_GET_NEXT(pTimer);
1103 if (pPrev)
1104 TMTIMER_SET_NEXT(pPrev, pNext);
1105 else
1106 {
1107 TMTIMER_SET_HEAD(pQueue, pNext);
1108 pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
1109 }
1110 if (pNext)
1111 TMTIMER_SET_PREV(pNext, pPrev);
1112 pTimer->offNext = 0;
1113 pTimer->offPrev = 0;
1114 /* fall thru */
1115 }
1116
1117 /*
1118 * Destroy the timer.
1119 */
1120 case TMTIMERSTATE_PENDING_DESTROY:
1121 {
1122 Assert(!pTimer->offNext); Assert(!pTimer->offPrev);
1123 PVM pVM = pTimer->CTXALLSUFF(pVM);
1124 const PTMTIMER pBigPrev = (PTMTIMER)(pTimer->pBigPrev ? MMHyperR3ToCC(pVM, pTimer->pBigPrev) : NULL);
1125 const PTMTIMER pBigNext = (PTMTIMER)(pTimer->pBigNext ? MMHyperR3ToCC(pVM, pTimer->pBigNext) : NULL);
1126
1127 /* unlink from created list */
1128 if (pBigPrev)
1129 pBigPrev->pBigNext = pTimer->pBigNext;
1130 else
1131 pVM->tm.s.pCreated = pTimer->pBigNext;
1132 if (pBigNext)
1133 pBigNext->pBigPrev = pTimer->pBigPrev;
1134 pTimer->pBigNext = 0;
1135 pTimer->pBigPrev = 0;
1136
1137 /* free */
1138 Log2(("TM: Inserting %p into the free list ahead of %p!\n", pTimer, pVM->tm.s.pFree));
1139 pTimer->pBigNext = pVM->tm.s.pFree;
1140 pVM->tm.s.pFree = (PTMTIMERR3)MMHyperCCToR3(pVM, pTimer);
1141 TM_SET_STATE(pTimer, TMTIMERSTATE_FREE);
1142 break;
1143 }
1144
1145 /*
1146 * Postpone these until they get into the right state.
1147 */
1148 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1149 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1150 tmTimerLink(pQueue, pTimer);
1151 STAM_COUNTER_INC(&pTimer->CTXALLSUFF(pVM)->tm.s.CTXALLSUFF(StatPostponed));
1152 break;
1153
1154 /*
1155 * None of these can be in the schedule.
1156 */
1157 case TMTIMERSTATE_FREE:
1158 case TMTIMERSTATE_STOPPED:
1159 case TMTIMERSTATE_ACTIVE:
1160 case TMTIMERSTATE_EXPIRED:
1161 AssertMsgFailed(("Timer (%p) in the scheduling list has an invalid state %s (%d)!",
1162 pTimer, tmTimerState(pTimer->enmState), pTimer->enmState));
1163 break;
1164 }
1165}
1166
1167
1168/**
1169 * Schedules the specified timer queue.
1170 *
1171 * @param pVM The VM to run the timers for.
1172 * @param pQueue The queue to schedule.
1173 */
1174void tmTimerQueueSchedule(PVM pVM, PTMTIMERQUEUE pQueue)
1175{
1176 VM_ASSERT_EMT(pVM);
1177
1178 /*
1179 * Dequeue the scheduling list and iterate it.
1180 */
1181 int32_t offNext = ASMAtomicXchgS32(&pQueue->offSchedule, 0);
1182 Log2(("tmTimerQueueSchedule: pQueue=%p:{.enmClock=%d, offNext=%RI32}\n", pQueue, pQueue->enmClock, offNext));
1183 if (!offNext)
1184 return;
1185 PTMTIMER pNext = (PTMTIMER)((intptr_t)pQueue + offNext);
1186 while (pNext)
1187 {
1188 /*
1189 * Unlink the head timer and find the next one.
1190 */
1191 PTMTIMER pTimer = pNext;
1192 pNext = pNext->offScheduleNext ? (PTMTIMER)((intptr_t)pNext + pNext->offScheduleNext) : NULL;
1193 pTimer->offScheduleNext = 0;
1194
1195 /*
1196 * Do the scheduling.
1197 */
1198 Log2(("tmTimerQueueSchedule: pTimer=%p:{.enmState=%s, .enmClock=%d, .enmType=%d, .pszDesc=%s}\n",
1199 pTimer, tmTimerState(pTimer->enmState), pTimer->enmClock, pTimer->enmType, HCSTRING(pTimer->pszDesc)));
1200 tmTimerQueueScheduleOne(pQueue, pTimer);
1201 Log2(("tmTimerQueueSchedule: new %s\n", tmTimerState(pTimer->enmState)));
1202 } /* foreach timer in current schedule batch. */
1203}
1204
1205
1206#ifdef VBOX_STRICT
1207/**
1208 * Checks that the timer queues are sane.
1209 *
1210 * @param pVM VM handle.
1211 */
1212void tmTimerQueuesSanityChecks(PVM pVM, const char *pszWhere)
1213{
1214 /*
1215 * Check the linking of the active lists.
1216 */
1217 for (int i = 0; i < TMCLOCK_MAX; i++)
1218 {
1219 PTMTIMERQUEUE pQueue = &pVM->tm.s.CTXALLSUFF(paTimerQueues)[i];
1220 Assert((int)pQueue->enmClock == i);
1221 PTMTIMER pPrev = NULL;
1222 for (PTMTIMER pCur = TMTIMER_GET_HEAD(pQueue); pCur; pPrev = pCur, pCur = TMTIMER_GET_NEXT(pCur))
1223 {
1224 AssertMsg((int)pCur->enmClock == i, ("%s: %d != %d\n", pszWhere, pCur->enmClock, i));
1225 AssertMsg(TMTIMER_GET_PREV(pCur) == pPrev, ("%s: %p != %p\n", pszWhere, TMTIMER_GET_PREV(pCur), pPrev));
1226 TMTIMERSTATE enmState = pCur->enmState;
1227 switch (enmState)
1228 {
1229 case TMTIMERSTATE_ACTIVE:
1230 AssertMsg(!pCur->offScheduleNext, ("%s: %RI32\n", pszWhere, pCur->offScheduleNext));
1231 break;
1232 case TMTIMERSTATE_PENDING_STOP:
1233 case TMTIMERSTATE_PENDING_STOP_DESTROY:
1234 case TMTIMERSTATE_PENDING_RESCHEDULE:
1235 break;
1236 default:
1237 AssertMsgFailed(("%s: Invalid state enmState=%d %s\n", pszWhere, enmState, tmTimerState(enmState)));
1238 break;
1239 }
1240 }
1241 }
1242
1243
1244# ifdef IN_RING3
1245 /*
1246 * Do the big list and check that active timers all are in the active lists.
1247 */
1248 PTMTIMERHC pPrev = NULL;
1249 for (PTMTIMERHC pCur = pVM->tm.s.pCreated; pCur; pPrev = pCur, pCur = pCur->pBigNext)
1250 {
1251 Assert(pCur->pBigPrev == pPrev);
1252 Assert((unsigned)pCur->enmClock < (unsigned)TMCLOCK_MAX);
1253
1254 TMTIMERSTATE enmState = pCur->enmState;
1255 switch (enmState)
1256 {
1257 case TMTIMERSTATE_ACTIVE:
1258 case TMTIMERSTATE_PENDING_STOP:
1259 case TMTIMERSTATE_PENDING_STOP_DESTROY:
1260 case TMTIMERSTATE_PENDING_RESCHEDULE:
1261 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1262 {
1263 PTMTIMERHC pCurAct = TMTIMER_GET_HEAD(&pVM->tm.s.CTXALLSUFF(paTimerQueues)[pCur->enmClock]);
1264 Assert(pCur->offPrev || pCur == pCurAct);
1265 while (pCurAct && pCurAct != pCur)
1266 pCurAct = TMTIMER_GET_NEXT(pCurAct);
1267 Assert(pCurAct == pCur);
1268 break;
1269 }
1270
1271 case TMTIMERSTATE_PENDING_DESTROY:
1272 case TMTIMERSTATE_PENDING_SCHEDULE:
1273 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1274 case TMTIMERSTATE_STOPPED:
1275 case TMTIMERSTATE_EXPIRED:
1276 {
1277 Assert(!pCur->offNext);
1278 Assert(!pCur->offPrev);
1279 for (PTMTIMERHC pCurAct = TMTIMER_GET_HEAD(&pVM->tm.s.CTXALLSUFF(paTimerQueues)[pCur->enmClock]);
1280 pCurAct;
1281 pCurAct = TMTIMER_GET_NEXT(pCurAct))
1282 {
1283 Assert(pCurAct != pCur);
1284 Assert(TMTIMER_GET_NEXT(pCurAct) != pCur);
1285 Assert(TMTIMER_GET_PREV(pCurAct) != pCur);
1286 }
1287 break;
1288 }
1289
1290 /* ignore */
1291 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1292 break;
1293
1294 /* shouldn't get here! */
1295 default:
1296 AssertMsgFailed(("Invalid state enmState=%d %s\n", enmState, tmTimerState(enmState)));
1297 break;
1298 }
1299 }
1300# endif /* IN_RING3 */
1301}
1302#endif /* !VBOX_STRICT */
1303
1304
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