VirtualBox

source: vbox/trunk/src/VBox/VMM/TMInternal.h@ 31126

Last change on this file since 31126 was 30799, checked in by vboxsync, 14 years ago

TM: Made it possible to enable the resettable accounting stats in release builds (from the makefile).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 27.6 KB
Line 
1/* $Id: TMInternal.h 30799 2010-07-13 08:16:37Z vboxsync $ */
2/** @file
3 * TM - Internal header file.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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
18#ifndef ___TMInternal_h
19#define ___TMInternal_h
20
21#include <VBox/cdefs.h>
22#include <VBox/types.h>
23#include <iprt/time.h>
24#include <iprt/timer.h>
25#include <iprt/assert.h>
26#include <VBox/stam.h>
27#include <VBox/pdmcritsect.h>
28
29RT_C_DECLS_BEGIN
30
31
32/** @defgroup grp_tm_int Internal
33 * @ingroup grp_tm
34 * @internal
35 * @{
36 */
37
38/** Frequency of the real clock. */
39#define TMCLOCK_FREQ_REAL UINT32_C(1000)
40/** Frequency of the virtual clock. */
41#define TMCLOCK_FREQ_VIRTUAL UINT32_C(1000000000)
42
43
44/**
45 * Timer type.
46 */
47typedef enum TMTIMERTYPE
48{
49 /** Device timer. */
50 TMTIMERTYPE_DEV = 1,
51 /** Driver timer. */
52 TMTIMERTYPE_DRV,
53 /** Internal timer . */
54 TMTIMERTYPE_INTERNAL,
55 /** External timer. */
56 TMTIMERTYPE_EXTERNAL
57} TMTIMERTYPE;
58
59/**
60 * Timer state
61 */
62typedef enum TMTIMERSTATE
63{
64 /** Timer is stopped. */
65 TMTIMERSTATE_STOPPED = 1,
66 /** Timer is active. */
67 TMTIMERSTATE_ACTIVE,
68 /** Timer is expired, getting expire and unlinking. */
69 TMTIMERSTATE_EXPIRED_GET_UNLINK,
70 /** Timer is expired and is being delivered. */
71 TMTIMERSTATE_EXPIRED_DELIVER,
72
73 /** Timer is stopped but still in the active list.
74 * Currently in the ScheduleTimers list. */
75 TMTIMERSTATE_PENDING_STOP,
76 /** Timer is stopped but needs unlinking from the ScheduleTimers list.
77 * Currently in the ScheduleTimers list. */
78 TMTIMERSTATE_PENDING_STOP_SCHEDULE,
79 /** Timer is being modified and will soon be pending scheduling.
80 * Currently in the ScheduleTimers list. */
81 TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE,
82 /** Timer is pending scheduling.
83 * Currently in the ScheduleTimers list. */
84 TMTIMERSTATE_PENDING_SCHEDULE,
85 /** Timer is being modified and will soon be pending rescheduling.
86 * Currently in the ScheduleTimers list and the active list. */
87 TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE,
88 /** Timer is modified and is now pending rescheduling.
89 * Currently in the ScheduleTimers list and the active list. */
90 TMTIMERSTATE_PENDING_RESCHEDULE,
91 /** Timer is being destroyed. */
92 TMTIMERSTATE_DESTROY,
93 /** Timer is free. */
94 TMTIMERSTATE_FREE
95} TMTIMERSTATE;
96
97/** Predicate that returns true if the give state is pending scheduling or
98 * rescheduling of any kind. Will reference the argument more than once! */
99#define TMTIMERSTATE_IS_PENDING_SCHEDULING(enmState) \
100 ( (enmState) <= TMTIMERSTATE_PENDING_RESCHEDULE \
101 && (enmState) >= TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE)
102
103
104/**
105 * Internal representation of a timer.
106 *
107 * For correct serialization (without the use of semaphores and
108 * other blocking/slow constructs) certain rules applies to updating
109 * this structure:
110 * - For thread other than EMT only u64Expire, enmState and pScheduleNext*
111 * are changeable. Everything else is out of bounds.
112 * - Updating of u64Expire timer can only happen in the TMTIMERSTATE_STOPPED
113 * and TMTIMERSTATE_PENDING_RESCHEDULING_SET_EXPIRE states.
114 * - Timers in the TMTIMERSTATE_EXPIRED state are only accessible from EMT.
115 * - Actual destruction of a timer can only be done at scheduling time.
116 */
117typedef struct TMTIMER
118{
119 /** Expire time. */
120 volatile uint64_t u64Expire;
121 /** Clock to apply to u64Expire. */
122 TMCLOCK enmClock;
123 /** Timer callback type. */
124 TMTIMERTYPE enmType;
125 /** Type specific data. */
126 union
127 {
128 /** TMTIMERTYPE_DEV. */
129 struct
130 {
131 /** Callback. */
132 R3PTRTYPE(PFNTMTIMERDEV) pfnTimer;
133 /** Device instance. */
134 PPDMDEVINSR3 pDevIns;
135 } Dev;
136
137 /** TMTIMERTYPE_DRV. */
138 struct
139 {
140 /** Callback. */
141 R3PTRTYPE(PFNTMTIMERDRV) pfnTimer;
142 /** Device instance. */
143 R3PTRTYPE(PPDMDRVINS) pDrvIns;
144 } Drv;
145
146 /** TMTIMERTYPE_INTERNAL. */
147 struct
148 {
149 /** Callback. */
150 R3PTRTYPE(PFNTMTIMERINT) pfnTimer;
151 } Internal;
152
153 /** TMTIMERTYPE_EXTERNAL. */
154 struct
155 {
156 /** Callback. */
157 R3PTRTYPE(PFNTMTIMEREXT) pfnTimer;
158 } External;
159 } u;
160
161 /** Timer state. */
162 volatile TMTIMERSTATE enmState;
163 /** Timer relative offset to the next timer in the schedule list. */
164 int32_t volatile offScheduleNext;
165
166 /** Timer relative offset to the next timer in the chain. */
167 int32_t offNext;
168 /** Timer relative offset to the previous timer in the chain. */
169 int32_t offPrev;
170
171 /** User argument. */
172 RTR3PTR pvUser;
173 /** The critical section associated with the lock. */
174 R3PTRTYPE(PPDMCRITSECT) pCritSect;
175
176 /** Pointer to the next timer in the list of created or free timers. (TM::pTimers or TM::pFree) */
177 PTMTIMERR3 pBigNext;
178 /** Pointer to the previous timer in the list of all created timers. (TM::pTimers) */
179 PTMTIMERR3 pBigPrev;
180 /** Pointer to the timer description. */
181 R3PTRTYPE(const char *) pszDesc;
182 /** Pointer to the VM the timer belongs to - R3 Ptr. */
183 PVMR3 pVMR3;
184 /** Pointer to the VM the timer belongs to - R0 Ptr. */
185 PVMR0 pVMR0;
186 /** Pointer to the VM the timer belongs to - RC Ptr. */
187 PVMRC pVMRC;
188#if HC_ARCH_BITS == 64
189 RTRCPTR padding0; /**< pad structure to multiple of 8 bytes. */
190#endif
191} TMTIMER;
192AssertCompileMemberSize(TMTIMER, enmState, sizeof(uint32_t));
193
194
195/**
196 * Updates a timer state in the correct atomic manner.
197 */
198#if 1
199# define TM_SET_STATE(pTimer, state) \
200 ASMAtomicWriteU32((uint32_t volatile *)&(pTimer)->enmState, state)
201#else
202# define TM_SET_STATE(pTimer, state) \
203 do { \
204 uint32_t uOld1 = (pTimer)->enmState; \
205 Log(("%s: %p: %d -> %d\n", __FUNCTION__, (pTimer), (pTimer)->enmState, state)); \
206 uint32_t uOld2 = ASMAtomicXchgU32((uint32_t volatile *)&(pTimer)->enmState, state); \
207 Assert(uOld1 == uOld2); \
208 } while (0)
209#endif
210
211/**
212 * Tries to updates a timer state in the correct atomic manner.
213 */
214#if 1
215# define TM_TRY_SET_STATE(pTimer, StateNew, StateOld, fRc) \
216 (fRc) = ASMAtomicCmpXchgU32((uint32_t volatile *)&(pTimer)->enmState, StateNew, StateOld)
217#else
218# define TM_TRY_SET_STATE(pTimer, StateNew, StateOld, fRc) \
219 do { (fRc) = ASMAtomicCmpXchgU32((uint32_t volatile *)&(pTimer)->enmState, StateNew, StateOld); \
220 Log(("%s: %p: %d -> %d %RTbool\n", __FUNCTION__, (pTimer), StateOld, StateNew, fRc)); \
221 } while (0)
222#endif
223
224/** Get the previous timer. */
225#define TMTIMER_GET_PREV(pTimer) ((PTMTIMER)((pTimer)->offPrev ? (intptr_t)(pTimer) + (pTimer)->offPrev : 0))
226/** Get the next timer. */
227#define TMTIMER_GET_NEXT(pTimer) ((PTMTIMER)((pTimer)->offNext ? (intptr_t)(pTimer) + (pTimer)->offNext : 0))
228/** Set the previous timer link. */
229#define TMTIMER_SET_PREV(pTimer, pPrev) ((pTimer)->offPrev = (pPrev) ? (intptr_t)(pPrev) - (intptr_t)(pTimer) : 0)
230/** Set the next timer link. */
231#define TMTIMER_SET_NEXT(pTimer, pNext) ((pTimer)->offNext = (pNext) ? (intptr_t)(pNext) - (intptr_t)(pTimer) : 0)
232
233
234/**
235 * A timer queue.
236 *
237 * This is allocated on the hyper heap.
238 */
239typedef struct TMTIMERQUEUE
240{
241 /** The cached expire time for this queue.
242 * Updated by EMT when scheduling the queue or modifying the head timer.
243 * Assigned UINT64_MAX when there is no head timer. */
244 uint64_t u64Expire;
245 /** Doubly linked list of active timers.
246 *
247 * When no scheduling is pending, this list is will be ordered by expire time (ascending).
248 * Access is serialized by only letting the emulation thread (EMT) do changes.
249 *
250 * The offset is relative to the queue structure.
251 */
252 int32_t offActive;
253 /** List of timers pending scheduling of some kind.
254 *
255 * Timer stats allowed in the list are TMTIMERSTATE_PENDING_STOPPING,
256 * TMTIMERSTATE_PENDING_DESTRUCTION, TMTIMERSTATE_PENDING_STOPPING_DESTRUCTION,
257 * TMTIMERSTATE_PENDING_RESCHEDULING and TMTIMERSTATE_PENDING_SCHEDULE.
258 *
259 * The offset is relative to the queue structure.
260 */
261 int32_t volatile offSchedule;
262 /** The clock for this queue. */
263 TMCLOCK enmClock;
264 /** Pad the structure up to 32 bytes. */
265 uint32_t au32Padding[3];
266} TMTIMERQUEUE;
267
268/** Pointer to a timer queue. */
269typedef TMTIMERQUEUE *PTMTIMERQUEUE;
270
271/** Get the head of the active timer list. */
272#define TMTIMER_GET_HEAD(pQueue) ((PTMTIMER)((pQueue)->offActive ? (intptr_t)(pQueue) + (pQueue)->offActive : 0))
273/** Set the head of the active timer list. */
274#define TMTIMER_SET_HEAD(pQueue, pHead) ((pQueue)->offActive = pHead ? (intptr_t)pHead - (intptr_t)(pQueue) : 0)
275
276
277/**
278 * CPU load data set.
279 * Mainly used by tmR3CpuLoadTimer.
280 */
281typedef struct TMCPULOADSTATE
282{
283 /** The percent of the period spent executing guest code. */
284 uint8_t cPctExecuting;
285 /** The percent of the period spent halted. */
286 uint8_t cPctHalted;
287 /** The percent of the period spent on other things. */
288 uint8_t cPctOther;
289 /** Explicit alignment padding */
290 uint8_t au8Alignment[5];
291
292 /** Previous cNsTotal value. */
293 uint64_t cNsPrevTotal;
294 /** Previous cNsExecuting value. */
295 uint64_t cNsPrevExecuting;
296 /** Previous cNsHalted value. */
297 uint64_t cNsPrevHalted;
298} TMCPULOADSTATE;
299AssertCompileSizeAlignment(TMCPULOADSTATE, 8);
300AssertCompileMemberAlignment(TMCPULOADSTATE, cNsPrevTotal, 8);
301/** Pointer to a CPU load data set. */
302typedef TMCPULOADSTATE *PTMCPULOADSTATE;
303
304/**
305 * Converts a TM pointer into a VM pointer.
306 * @returns Pointer to the VM structure the TM is part of.
307 * @param pTM Pointer to TM instance data.
308 */
309#define TM2VM(pTM) ( (PVM)((char*)pTM - pTM->offVM) )
310
311
312/**
313 * TM VM Instance data.
314 * Changes to this must checked against the padding of the cfgm union in VM!
315 */
316typedef struct TM
317{
318 /** Offset to the VM structure.
319 * See TM2VM(). */
320 RTUINT offVM;
321
322 /** Set if we fully virtualize the TSC, i.e. intercept all rdtsc instructions.
323 * Config variable: TSCVirtualized (bool) */
324 bool fTSCVirtualized;
325 /** Set if we use the real TSC as time source or if we use the virtual clock.
326 * If fTSCVirtualized is set we maintain a offset to the TSC and pausing/resuming the
327 * ticking. fTSCVirtualized = false implies fTSCUseRealTSC = true.
328 * Config variable: TSCUseRealTSC (bool) */
329 bool fTSCUseRealTSC;
330 /** Flag indicating that the host TSC is suitable for use in AMD-V and VT-x mode.
331 * Config variable: MaybeUseOffsettedHostTSC (boolean) */
332 bool fMaybeUseOffsettedHostTSC;
333 /** Whether the TSC is tied to the execution of code.
334 * Config variable: TSCTiedToExecution (bool) */
335 bool fTSCTiedToExecution;
336 /** Modifier for fTSCTiedToExecution which pauses the TSC while halting if true.
337 * Config variable: TSCNotTiedToHalt (bool) */
338 bool fTSCNotTiedToHalt;
339 bool afAlignment0[2]; /**< alignment padding */
340 /** The ID of the virtual CPU that normally runs the timers. */
341 VMCPUID idTimerCpu;
342 /** The number of CPU clock ticks per second (TMCLOCK_TSC).
343 * Config variable: TSCTicksPerSecond (64-bit unsigned int)
344 * The config variable implies fTSCVirtualized = true and fTSCUseRealTSC = false. */
345 uint64_t cTSCTicksPerSecond;
346
347 /** Virtual time ticking enabled indicator (counter for each VCPU). (TMCLOCK_VIRTUAL) */
348 uint32_t volatile cVirtualTicking;
349 /** Virtual time is not running at 100%. */
350 bool fVirtualWarpDrive;
351 /** Virtual timer synchronous time ticking enabled indicator (bool). (TMCLOCK_VIRTUAL_SYNC) */
352 bool volatile fVirtualSyncTicking;
353 /** Virtual timer synchronous time catch-up active. */
354 bool volatile fVirtualSyncCatchUp;
355 bool afAlignment1[5]; /**< alignment padding */
356 /** WarpDrive percentage.
357 * 100% is normal (fVirtualSyncNormal == true). When other than 100% we apply
358 * this percentage to the raw time source for the period it's been valid in,
359 * i.e. since u64VirtualWarpDriveStart. */
360 uint32_t u32VirtualWarpDrivePercentage;
361
362 /** The offset of the virtual clock relative to it's timesource.
363 * Only valid if fVirtualTicking is set. */
364 uint64_t u64VirtualOffset;
365 /** The guest virtual time when fVirtualTicking is cleared. */
366 uint64_t u64Virtual;
367 /** When the Warp drive was started or last adjusted.
368 * Only valid when fVirtualWarpDrive is set. */
369 uint64_t u64VirtualWarpDriveStart;
370 /** The previously returned nano TS.
371 * This handles TSC drift on SMP systems and expired interval.
372 * This is a valid range u64NanoTS to u64NanoTS + 1000000000 (ie. 1sec). */
373 uint64_t volatile u64VirtualRawPrev;
374 /** The ring-3 data structure for the RTTimeNanoTS workers used by tmVirtualGetRawNanoTS. */
375 RTTIMENANOTSDATAR3 VirtualGetRawDataR3;
376 /** The ring-0 data structure for the RTTimeNanoTS workers used by tmVirtualGetRawNanoTS. */
377 RTTIMENANOTSDATAR0 VirtualGetRawDataR0;
378 /** The ring-0 data structure for the RTTimeNanoTS workers used by tmVirtualGetRawNanoTS. */
379 RTTIMENANOTSDATARC VirtualGetRawDataRC;
380 /** Pointer to the ring-3 tmVirtualGetRawNanoTS worker function. */
381 R3PTRTYPE(PFNTIMENANOTSINTERNAL) pfnVirtualGetRawR3;
382 /** Pointer to the ring-3 tmVirtualGetRawNanoTS worker function. */
383 R0PTRTYPE(PFNTIMENANOTSINTERNAL) pfnVirtualGetRawR0;
384 /** Pointer to the ring-3 tmVirtualGetRawNanoTS worker function. */
385 RCPTRTYPE(PFNTIMENANOTSINTERNAL) pfnVirtualGetRawRC;
386 /** Alignment. */
387 RTRCPTR AlignmentRCPtr;
388 /** The guest virtual timer synchronous time when fVirtualSyncTicking is cleared. */
389 uint64_t volatile u64VirtualSync;
390 /** The offset of the timer synchronous virtual clock (TMCLOCK_VIRTUAL_SYNC) relative
391 * to the virtual clock (TMCLOCK_VIRTUAL).
392 * (This is accessed by the timer thread and must be updated atomically.) */
393 uint64_t volatile offVirtualSync;
394 /** The offset into offVirtualSync that's been irrevocably given up by failed catch-up attempts.
395 * Thus the current lag is offVirtualSync - offVirtualSyncGivenUp. */
396 uint64_t offVirtualSyncGivenUp;
397 /** The TMCLOCK_VIRTUAL at the previous TMVirtualGetSync call when catch-up is active. */
398 uint64_t volatile u64VirtualSyncCatchUpPrev;
399 /** The current catch-up percentage. */
400 uint32_t volatile u32VirtualSyncCatchUpPercentage;
401 /** How much slack when processing timers. */
402 uint32_t u32VirtualSyncScheduleSlack;
403 /** When to stop catch-up. */
404 uint64_t u64VirtualSyncCatchUpStopThreshold;
405 /** When to give up catch-up. */
406 uint64_t u64VirtualSyncCatchUpGiveUpThreshold;
407/** @def TM_MAX_CATCHUP_PERIODS
408 * The number of catchup rates. */
409#define TM_MAX_CATCHUP_PERIODS 10
410 /** The agressivness of the catch-up relative to how far we've lagged behind.
411 * The idea is to have increasing catch-up percentage as the lag increases. */
412 struct TMCATCHUPPERIOD
413 {
414 uint64_t u64Start; /**< When this period starts. (u64VirtualSyncOffset). */
415 uint32_t u32Percentage; /**< The catch-up percent to apply. */
416 uint32_t u32Alignment; /**< Structure alignment */
417 } aVirtualSyncCatchUpPeriods[TM_MAX_CATCHUP_PERIODS];
418
419 /** The UTC offset in ns.
420 * This is *NOT* for converting UTC to local time. It is for converting real
421 * world UTC time to VM UTC time. This feature is indented for doing date
422 * testing of software and similar.
423 * @todo Implement warpdrive on UTC. */
424 int64_t offUTC;
425
426 /** Timer queues for the different clock types - R3 Ptr */
427 R3PTRTYPE(PTMTIMERQUEUE) paTimerQueuesR3;
428 /** Timer queues for the different clock types - R0 Ptr */
429 R0PTRTYPE(PTMTIMERQUEUE) paTimerQueuesR0;
430 /** Timer queues for the different clock types - RC Ptr */
431 RCPTRTYPE(PTMTIMERQUEUE) paTimerQueuesRC;
432
433 /** Pointer to our RC mapping of the GIP. */
434 RCPTRTYPE(void *) pvGIPRC;
435 /** Pointer to our R3 mapping of the GIP. */
436 R3PTRTYPE(void *) pvGIPR3;
437
438 /** Pointer to a singly linked list of free timers.
439 * This chain is using the TMTIMER::pBigNext members.
440 * Only accessible from the emulation thread. */
441 PTMTIMERR3 pFree;
442
443 /** Pointer to a doubly linked list of created timers.
444 * This chain is using the TMTIMER::pBigNext and TMTIMER::pBigPrev members.
445 * Only accessible from the emulation thread. */
446 PTMTIMERR3 pCreated;
447
448 /** The schedulation timer timer handle (runtime timer).
449 * This timer will do freqent check on pending queue schedulations and
450 * raise VM_FF_TIMER to pull EMTs attention to them.
451 */
452 R3PTRTYPE(PRTTIMER) pTimer;
453 /** Interval in milliseconds of the pTimer timer. */
454 uint32_t u32TimerMillies;
455
456 /** Indicates that queues are being run. */
457 bool volatile fRunningQueues;
458 /** Indicates that the virtual sync queue is being run. */
459 bool volatile fRunningVirtualSyncQueue;
460 /** Alignment */
461 bool afAlignment2[2];
462
463 /** Lock serializing access to the timer lists. */
464 PDMCRITSECT TimerCritSect;
465 /** Lock serializing access to the VirtualSync clock. */
466 PDMCRITSECT VirtualSyncLock;
467
468 /** CPU load state for all the virtual CPUs (tmR3CpuLoadTimer). */
469 TMCPULOADSTATE CpuLoad;
470
471 /** TMR3TimerQueuesDo
472 * @{ */
473 STAMPROFILE StatDoQueues;
474 STAMPROFILEADV aStatDoQueues[TMCLOCK_MAX];
475 /** @} */
476 /** tmSchedule
477 * @{ */
478 STAMPROFILE StatScheduleOneRZ;
479 STAMPROFILE StatScheduleOneR3;
480 STAMCOUNTER StatScheduleSetFF;
481 STAMCOUNTER StatPostponedR3;
482 STAMCOUNTER StatPostponedRZ;
483 /** @} */
484 /** Read the time
485 * @{ */
486 STAMCOUNTER StatVirtualGet;
487 STAMCOUNTER StatVirtualGetSetFF;
488 STAMCOUNTER StatVirtualSyncGet;
489 STAMCOUNTER StatVirtualSyncGetELoop;
490 STAMCOUNTER StatVirtualSyncGetExpired;
491 STAMCOUNTER StatVirtualSyncGetLockless;
492 STAMCOUNTER StatVirtualSyncGetLocked;
493 STAMCOUNTER StatVirtualSyncGetSetFF;
494 STAMCOUNTER StatVirtualPause;
495 STAMCOUNTER StatVirtualResume;
496 /* @} */
497 /** TMTimerPoll
498 * @{ */
499 STAMCOUNTER StatPoll;
500 STAMCOUNTER StatPollAlreadySet;
501 STAMCOUNTER StatPollELoop;
502 STAMCOUNTER StatPollMiss;
503 STAMCOUNTER StatPollRunning;
504 STAMCOUNTER StatPollSimple;
505 STAMCOUNTER StatPollVirtual;
506 STAMCOUNTER StatPollVirtualSync;
507 /** @} */
508 /** TMTimerSet
509 * @{ */
510 STAMCOUNTER StatTimerSet;
511 STAMCOUNTER StatTimerSetOpt;
512 STAMPROFILE StatTimerSetRZ;
513 STAMPROFILE StatTimerSetR3;
514 STAMCOUNTER StatTimerSetStStopped;
515 STAMCOUNTER StatTimerSetStExpDeliver;
516 STAMCOUNTER StatTimerSetStActive;
517 STAMCOUNTER StatTimerSetStPendStop;
518 STAMCOUNTER StatTimerSetStPendStopSched;
519 STAMCOUNTER StatTimerSetStPendSched;
520 STAMCOUNTER StatTimerSetStPendResched;
521 STAMCOUNTER StatTimerSetStOther;
522 /** @} */
523 /** TMTimerSetRelative
524 * @{ */
525 STAMCOUNTER StatTimerSetRelative;
526 STAMPROFILE StatTimerSetRelativeRZ;
527 STAMPROFILE StatTimerSetRelativeR3;
528 STAMCOUNTER StatTimerSetRelativeOpt;
529 STAMCOUNTER StatTimerSetRelativeRacyVirtSync;
530 STAMCOUNTER StatTimerSetRelativeStStopped;
531 STAMCOUNTER StatTimerSetRelativeStExpDeliver;
532 STAMCOUNTER StatTimerSetRelativeStActive;
533 STAMCOUNTER StatTimerSetRelativeStPendStop;
534 STAMCOUNTER StatTimerSetRelativeStPendStopSched;
535 STAMCOUNTER StatTimerSetRelativeStPendSched;
536 STAMCOUNTER StatTimerSetRelativeStPendResched;
537 STAMCOUNTER StatTimerSetRelativeStOther;
538 /** @} */
539 /** TMTimerStop
540 * @{ */
541 STAMPROFILE StatTimerStopRZ;
542 STAMPROFILE StatTimerStopR3;
543 /** @} */
544 /** VirtualSync - Running and Catching Up
545 * @{ */
546 STAMCOUNTER StatVirtualSyncRun;
547 STAMCOUNTER StatVirtualSyncRunRestart;
548 STAMPROFILE StatVirtualSyncRunSlack;
549 STAMCOUNTER StatVirtualSyncRunStop;
550 STAMCOUNTER StatVirtualSyncRunStoppedAlready;
551 STAMCOUNTER StatVirtualSyncGiveUp;
552 STAMCOUNTER StatVirtualSyncGiveUpBeforeStarting;
553 STAMPROFILEADV StatVirtualSyncCatchup;
554 STAMCOUNTER aStatVirtualSyncCatchupInitial[TM_MAX_CATCHUP_PERIODS];
555 STAMCOUNTER aStatVirtualSyncCatchupAdjust[TM_MAX_CATCHUP_PERIODS];
556 /** @} */
557 /** TMR3VirtualSyncFF (non dedicated EMT). */
558 STAMPROFILE StatVirtualSyncFF;
559 /** The timer callback. */
560 STAMCOUNTER StatTimerCallbackSetFF;
561
562 /** Calls to TMCpuTickSet. */
563 STAMCOUNTER StatTSCSet;
564
565 /** @name Reasons for refusing TSC offsetting in TMCpuTickCanUseRealTSC.
566 * @{ */
567 STAMCOUNTER StatTSCNotFixed;
568 STAMCOUNTER StatTSCNotTicking;
569 STAMCOUNTER StatTSCCatchupLE010;
570 STAMCOUNTER StatTSCCatchupLE025;
571 STAMCOUNTER StatTSCCatchupLE100;
572 STAMCOUNTER StatTSCCatchupOther;
573 STAMCOUNTER StatTSCWarp;
574 STAMCOUNTER StatTSCUnderflow;
575 STAMCOUNTER StatTSCSyncNotTicking;
576 /** @} */
577} TM;
578/** Pointer to TM VM instance data. */
579typedef TM *PTM;
580
581/**
582 * TM VMCPU Instance data.
583 * Changes to this must checked against the padding of the tm union in VM!
584 */
585typedef struct TMCPU
586{
587 /** Offset to the VMCPU structure.
588 * See TMCPU2VM(). */
589 RTUINT offVMCPU;
590
591 /** CPU timestamp ticking enabled indicator (bool). (RDTSC) */
592 bool fTSCTicking;
593 bool afAlignment0[3]; /**< alignment padding */
594
595 /** The offset between the raw TSC source and the Guest TSC.
596 * Only valid if fTicking is set and and fTSCUseRealTSC is clear. */
597 uint64_t offTSCRawSrc;
598
599 /** The guest TSC when fTicking is cleared. */
600 uint64_t u64TSC;
601
602 /** The last seen TSC by the guest. */
603 uint64_t u64TSCLastSeen;
604
605#ifndef VBOX_WITHOUT_NS_ACCOUNTING
606 /** The nanosecond timestamp of the CPU start or resume.
607 * This is recalculated when the VM is started so that
608 * cNsTotal = RTTimeNanoTS() - u64NsTsStartCpu. */
609 uint64_t u64NsTsStartTotal;
610 /** The nanosecond timestamp of the last start-execute notification. */
611 uint64_t u64NsTsStartExecuting;
612 /** The nanosecond timestamp of the last start-halt notification. */
613 uint64_t u64NsTsStartHalting;
614 /** The cNsXXX generation. */
615 uint32_t volatile uTimesGen;
616 /** Explicit alignment padding. */
617 uint32_t u32Alignment;
618 /** The number of nanoseconds total run time.
619 * @remarks This is updated when cNsExecuting and cNsHalted are updated. */
620 uint64_t cNsTotal;
621 /** The number of nanoseconds spent executing. */
622 uint64_t cNsExecuting;
623 /** The number of nanoseconds being halted. */
624 uint64_t cNsHalted;
625 /** The number of nanoseconds spent on other things.
626 * @remarks This is updated when cNsExecuting and cNsHalted are updated. */
627 uint64_t cNsOther;
628 /** The number of halts. */
629 uint64_t cPeriodsHalted;
630 /** The number of guest execution runs. */
631 uint64_t cPeriodsExecuting;
632# if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
633 /** Resettable version of cNsTotal. */
634 STAMCOUNTER StatNsTotal;
635 /** Resettable version of cNsExecuting. */
636 STAMPROFILE StatNsExecuting;
637 /** Resettable version of cNsHalted. */
638 STAMPROFILE StatNsHalted;
639 /** Resettable version of cNsOther. */
640 STAMPROFILE StatNsOther;
641# endif
642
643 /** CPU load state for this virtual CPU (tmR3CpuLoadTimer). */
644 TMCPULOADSTATE CpuLoad;
645#endif
646} TMCPU;
647/** Pointer to TM VMCPU instance data. */
648typedef TMCPU *PTMCPU;
649
650#if 0 /* enable this to rule out locking bugs on single cpu guests. */
651# define tmTimerLock(pVM) VINF_SUCCESS
652# define tmTimerTryLock(pVM) VINF_SUCCESS
653# define tmTimerUnlock(pVM) ((void)0)
654# define tmVirtualSyncLock(pVM) VINF_SUCCESS
655# define tmVirtualSyncTryLock(pVM) VINF_SUCCESS
656# define tmVirtualSyncUnlock(pVM) ((void)0)
657# define TM_ASSERT_LOCK(pVM) VM_ASSERT_EMT(pVM)
658#else
659int tmTimerLock(PVM pVM);
660int tmTimerTryLock(PVM pVM);
661void tmTimerUnlock(PVM pVM);
662/** Checks that the caller owns the timer lock. */
663#define TM_ASSERT_LOCK(pVM) Assert(PDMCritSectIsOwner(&pVM->tm.s.TimerCritSect))
664int tmVirtualSyncLock(PVM pVM);
665int tmVirtualSyncTryLock(PVM pVM);
666void tmVirtualSyncUnlock(PVM pVM);
667#endif
668
669const char *tmTimerState(TMTIMERSTATE enmState);
670void tmTimerQueueSchedule(PVM pVM, PTMTIMERQUEUE pQueue);
671#ifdef VBOX_STRICT
672void tmTimerQueuesSanityChecks(PVM pVM, const char *pszWhere);
673#endif
674
675int tmCpuTickPause(PVM pVM, PVMCPU pVCpu);
676int tmCpuTickResume(PVM pVM, PVMCPU pVCpu);
677
678int tmVirtualPauseLocked(PVM pVM);
679int tmVirtualResumeLocked(PVM pVM);
680DECLEXPORT(void) tmVirtualNanoTSBad(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS);
681DECLEXPORT(uint64_t) tmVirtualNanoTSRediscover(PRTTIMENANOTSDATA pData);
682
683
684/** @} */
685
686RT_C_DECLS_END
687
688#endif
689
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