VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/TM.cpp@ 38080

Last change on this file since 38080 was 37527, checked in by vboxsync, 14 years ago

TM: Virtual sync timer locking fixes and assertions.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 135.1 KB
Line 
1/* $Id: TM.cpp 37527 2011-06-17 10:18:02Z vboxsync $ */
2/** @file
3 * TM - Time Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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/** @page pg_tm TM - The Time Manager
19 *
20 * The Time Manager abstracts the CPU clocks and manages timers used by the VMM,
21 * device and drivers.
22 *
23 * @see grp_tm
24 *
25 *
26 * @section sec_tm_clocks Clocks
27 *
28 * There are currently 4 clocks:
29 * - Virtual (guest).
30 * - Synchronous virtual (guest).
31 * - CPU Tick (TSC) (guest). Only current use is rdtsc emulation. Usually a
32 * function of the virtual clock.
33 * - Real (host). This is only used for display updates atm.
34 *
35 * The most important clocks are the three first ones and of these the second is
36 * the most interesting.
37 *
38 *
39 * The synchronous virtual clock is tied to the virtual clock except that it
40 * will take into account timer delivery lag caused by host scheduling. It will
41 * normally never advance beyond the head timer, and when lagging too far behind
42 * it will gradually speed up to catch up with the virtual clock. All devices
43 * implementing time sources accessible to and used by the guest is using this
44 * clock (for timers and other things). This ensures consistency between the
45 * time sources.
46 *
47 * The virtual clock is implemented as an offset to a monotonic, high
48 * resolution, wall clock. The current time source is using the RTTimeNanoTS()
49 * machinery based upon the Global Info Pages (GIP), that is, we're using TSC
50 * deltas (usually 10 ms) to fill the gaps between GIP updates. The result is
51 * a fairly high res clock that works in all contexts and on all hosts. The
52 * virtual clock is paused when the VM isn't in the running state.
53 *
54 * The CPU tick (TSC) is normally virtualized as a function of the synchronous
55 * virtual clock, where the frequency defaults to the host cpu frequency (as we
56 * measure it). In this mode it is possible to configure the frequency. Another
57 * (non-default) option is to use the raw unmodified host TSC values. And yet
58 * another, to tie it to time spent executing guest code. All these things are
59 * configurable should non-default behavior be desirable.
60 *
61 * The real clock is a monotonic clock (when available) with relatively low
62 * resolution, though this a bit host specific. Note that we're currently not
63 * servicing timers using the real clock when the VM is not running, this is
64 * simply because it has not been needed yet therefore not implemented.
65 *
66 *
67 * @subsection subsec_tm_timesync Guest Time Sync / UTC time
68 *
69 * Guest time syncing is primarily taken care of by the VMM device. The
70 * principle is very simple, the guest additions periodically asks the VMM
71 * device what the current UTC time is and makes adjustments accordingly.
72 *
73 * A complicating factor is that the synchronous virtual clock might be doing
74 * catchups and the guest perception is currently a little bit behind the world
75 * but it will (hopefully) be catching up soon as we're feeding timer interrupts
76 * at a slightly higher rate. Adjusting the guest clock to the current wall
77 * time in the real world would be a bad idea then because the guest will be
78 * advancing too fast and run ahead of world time (if the catchup works out).
79 * To solve this problem TM provides the VMM device with an UTC time source that
80 * gets adjusted with the current lag, so that when the guest eventually catches
81 * up the lag it will be showing correct real world time.
82 *
83 *
84 * @section sec_tm_timers Timers
85 *
86 * The timers can use any of the TM clocks described in the previous section.
87 * Each clock has its own scheduling facility, or timer queue if you like.
88 * There are a few factors which makes it a bit complex. First, there is the
89 * usual R0 vs R3 vs. RC thing. Then there are multiple threads, and then there
90 * is the timer thread that periodically checks whether any timers has expired
91 * without EMT noticing. On the API level, all but the create and save APIs
92 * must be multithreaded. EMT will always run the timers.
93 *
94 * The design is using a doubly linked list of active timers which is ordered
95 * by expire date. This list is only modified by the EMT thread. Updates to
96 * the list are batched in a singly linked list, which is then processed by the
97 * EMT thread at the first opportunity (immediately, next time EMT modifies a
98 * timer on that clock, or next timer timeout). Both lists are offset based and
99 * all the elements are therefore allocated from the hyper heap.
100 *
101 * For figuring out when there is need to schedule and run timers TM will:
102 * - Poll whenever somebody queries the virtual clock.
103 * - Poll the virtual clocks from the EM and REM loops.
104 * - Poll the virtual clocks from trap exit path.
105 * - Poll the virtual clocks and calculate first timeout from the halt loop.
106 * - Employ a thread which periodically (100Hz) polls all the timer queues.
107 *
108 *
109 * @image html TMTIMER-Statechart-Diagram.gif
110 *
111 * @section sec_tm_timer Logging
112 *
113 * Level 2: Logs a most of the timer state transitions and queue servicing.
114 * Level 3: Logs a few oddments.
115 * Level 4: Logs TMCLOCK_VIRTUAL_SYNC catch-up events.
116 *
117 */
118
119/*******************************************************************************
120* Header Files *
121*******************************************************************************/
122#define LOG_GROUP LOG_GROUP_TM
123#include <VBox/vmm/tm.h>
124#include <iprt/asm-amd64-x86.h> /* for SUPGetCpuHzFromGIP from sup.h */
125#include <VBox/vmm/vmm.h>
126#include <VBox/vmm/mm.h>
127#include <VBox/vmm/ssm.h>
128#include <VBox/vmm/dbgf.h>
129#include <VBox/vmm/dbgftrace.h>
130#include <VBox/vmm/rem.h>
131#include <VBox/vmm/pdmapi.h>
132#include <VBox/vmm/iom.h>
133#include "TMInternal.h"
134#include <VBox/vmm/vm.h>
135
136#include <VBox/vmm/pdmdev.h>
137#include <VBox/param.h>
138#include <VBox/err.h>
139
140#include <VBox/log.h>
141#include <iprt/asm.h>
142#include <iprt/asm-math.h>
143#include <iprt/assert.h>
144#include <iprt/thread.h>
145#include <iprt/time.h>
146#include <iprt/timer.h>
147#include <iprt/semaphore.h>
148#include <iprt/string.h>
149#include <iprt/env.h>
150
151#include "TMInline.h"
152
153
154/*******************************************************************************
155* Defined Constants And Macros *
156*******************************************************************************/
157/** The current saved state version.*/
158#define TM_SAVED_STATE_VERSION 3
159
160
161/*******************************************************************************
162* Internal Functions *
163*******************************************************************************/
164static bool tmR3HasFixedTSC(PVM pVM);
165static uint64_t tmR3CalibrateTSC(PVM pVM);
166static DECLCALLBACK(int) tmR3Save(PVM pVM, PSSMHANDLE pSSM);
167static DECLCALLBACK(int) tmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
168static DECLCALLBACK(void) tmR3TimerCallback(PRTTIMER pTimer, void *pvUser, uint64_t iTick);
169static void tmR3TimerQueueRun(PVM pVM, PTMTIMERQUEUE pQueue);
170static void tmR3TimerQueueRunVirtualSync(PVM pVM);
171static DECLCALLBACK(int) tmR3SetWarpDrive(PVM pVM, uint32_t u32Percent);
172#ifndef VBOX_WITHOUT_NS_ACCOUNTING
173static DECLCALLBACK(void) tmR3CpuLoadTimer(PVM pVM, PTMTIMER pTimer, void *pvUser);
174#endif
175static DECLCALLBACK(void) tmR3TimerInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
176static DECLCALLBACK(void) tmR3TimerInfoActive(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
177static DECLCALLBACK(void) tmR3InfoClocks(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
178
179
180/**
181 * Initializes the TM.
182 *
183 * @returns VBox status code.
184 * @param pVM The VM to operate on.
185 */
186VMM_INT_DECL(int) TMR3Init(PVM pVM)
187{
188 LogFlow(("TMR3Init:\n"));
189
190 /*
191 * Assert alignment and sizes.
192 */
193 AssertCompileMemberAlignment(VM, tm.s, 32);
194 AssertCompile(sizeof(pVM->tm.s) <= sizeof(pVM->tm.padding));
195 AssertCompileMemberAlignment(TM, TimerCritSect, 8);
196 AssertCompileMemberAlignment(TM, VirtualSyncLock, 8);
197
198 /*
199 * Init the structure.
200 */
201 void *pv;
202 int rc = MMHyperAlloc(pVM, sizeof(pVM->tm.s.paTimerQueuesR3[0]) * TMCLOCK_MAX, 0, MM_TAG_TM, &pv);
203 AssertRCReturn(rc, rc);
204 pVM->tm.s.paTimerQueuesR3 = (PTMTIMERQUEUE)pv;
205 pVM->tm.s.paTimerQueuesR0 = MMHyperR3ToR0(pVM, pv);
206 pVM->tm.s.paTimerQueuesRC = MMHyperR3ToRC(pVM, pv);
207
208 pVM->tm.s.offVM = RT_OFFSETOF(VM, tm.s);
209 pVM->tm.s.idTimerCpu = pVM->cCpus - 1; /* The last CPU. */
210 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].enmClock = TMCLOCK_VIRTUAL;
211 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].u64Expire = INT64_MAX;
212 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].enmClock = TMCLOCK_VIRTUAL_SYNC;
213 pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].u64Expire = INT64_MAX;
214 pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].enmClock = TMCLOCK_REAL;
215 pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].u64Expire = INT64_MAX;
216 pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].enmClock = TMCLOCK_TSC;
217 pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].u64Expire = INT64_MAX;
218
219
220 /*
221 * We directly use the GIP to calculate the virtual time. We map the
222 * the GIP into the guest context so we can do this calculation there
223 * as well and save costly world switches.
224 */
225 pVM->tm.s.pvGIPR3 = (void *)g_pSUPGlobalInfoPage;
226 AssertMsgReturn(pVM->tm.s.pvGIPR3, ("GIP support is now required!\n"), VERR_INTERNAL_ERROR);
227 AssertMsgReturn((g_pSUPGlobalInfoPage->u32Version >> 16) == (SUPGLOBALINFOPAGE_VERSION >> 16),
228 ("Unsupported GIP version!\n"), VERR_INTERNAL_ERROR);
229
230 RTHCPHYS HCPhysGIP;
231 rc = SUPR3GipGetPhys(&HCPhysGIP);
232 AssertMsgRCReturn(rc, ("Failed to get GIP physical address!\n"), rc);
233
234 RTGCPTR GCPtr;
235#ifdef SUP_WITH_LOTS_OF_CPUS
236 rc = MMR3HyperMapHCPhys(pVM, pVM->tm.s.pvGIPR3, NIL_RTR0PTR, HCPhysGIP, (size_t)g_pSUPGlobalInfoPage->cPages * PAGE_SIZE,
237 "GIP", &GCPtr);
238#else
239 rc = MMR3HyperMapHCPhys(pVM, pVM->tm.s.pvGIPR3, NIL_RTR0PTR, HCPhysGIP, PAGE_SIZE, "GIP", &GCPtr);
240#endif
241 if (RT_FAILURE(rc))
242 {
243 AssertMsgFailed(("Failed to map GIP into GC, rc=%Rrc!\n", rc));
244 return rc;
245 }
246 pVM->tm.s.pvGIPRC = GCPtr;
247 LogFlow(("TMR3Init: HCPhysGIP=%RHp at %RRv\n", HCPhysGIP, pVM->tm.s.pvGIPRC));
248 MMR3HyperReserve(pVM, PAGE_SIZE, "fence", NULL);
249
250 /* Check assumptions made in TMAllVirtual.cpp about the GIP update interval. */
251 if ( g_pSUPGlobalInfoPage->u32Magic == SUPGLOBALINFOPAGE_MAGIC
252 && g_pSUPGlobalInfoPage->u32UpdateIntervalNS >= 250000000 /* 0.25s */)
253 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
254 N_("The GIP update interval is too big. u32UpdateIntervalNS=%RU32 (u32UpdateHz=%RU32)"),
255 g_pSUPGlobalInfoPage->u32UpdateIntervalNS, g_pSUPGlobalInfoPage->u32UpdateHz);
256 LogRel(("TM: GIP - u32Mode=%d (%s) u32UpdateHz=%u\n", g_pSUPGlobalInfoPage->u32Mode,
257 g_pSUPGlobalInfoPage->u32Mode == SUPGIPMODE_SYNC_TSC ? "SyncTSC"
258 : g_pSUPGlobalInfoPage->u32Mode == SUPGIPMODE_ASYNC_TSC ? "AsyncTSC" : "Unknown",
259 g_pSUPGlobalInfoPage->u32UpdateHz));
260
261 /*
262 * Setup the VirtualGetRaw backend.
263 */
264 pVM->tm.s.VirtualGetRawDataR3.pu64Prev = &pVM->tm.s.u64VirtualRawPrev;
265 pVM->tm.s.VirtualGetRawDataR3.pfnBad = tmVirtualNanoTSBad;
266 pVM->tm.s.VirtualGetRawDataR3.pfnRediscover = tmVirtualNanoTSRediscover;
267 if (ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SSE2)
268 {
269 if (g_pSUPGlobalInfoPage->u32Mode == SUPGIPMODE_SYNC_TSC)
270 pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLFenceSync;
271 else
272 pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLFenceAsync;
273 }
274 else
275 {
276 if (g_pSUPGlobalInfoPage->u32Mode == SUPGIPMODE_SYNC_TSC)
277 pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLegacySync;
278 else
279 pVM->tm.s.pfnVirtualGetRawR3 = RTTimeNanoTSLegacyAsync;
280 }
281
282 pVM->tm.s.VirtualGetRawDataRC.pu64Prev = MMHyperR3ToRC(pVM, (void *)&pVM->tm.s.u64VirtualRawPrev);
283 pVM->tm.s.VirtualGetRawDataR0.pu64Prev = MMHyperR3ToR0(pVM, (void *)&pVM->tm.s.u64VirtualRawPrev);
284 AssertReturn(pVM->tm.s.VirtualGetRawDataR0.pu64Prev, VERR_INTERNAL_ERROR);
285 /* The rest is done in TMR3InitFinalize since it's too early to call PDM. */
286
287 /*
288 * Init the locks.
289 */
290 rc = PDMR3CritSectInit(pVM, &pVM->tm.s.TimerCritSect, RT_SRC_POS, "TM Timer Lock");
291 if (RT_FAILURE(rc))
292 return rc;
293 rc = PDMR3CritSectInit(pVM, &pVM->tm.s.VirtualSyncLock, RT_SRC_POS, "TM VirtualSync Lock");
294 if (RT_FAILURE(rc))
295 return rc;
296
297 /*
298 * Get our CFGM node, create it if necessary.
299 */
300 PCFGMNODE pCfgHandle = CFGMR3GetChild(CFGMR3GetRoot(pVM), "TM");
301 if (!pCfgHandle)
302 {
303 rc = CFGMR3InsertNode(CFGMR3GetRoot(pVM), "TM", &pCfgHandle);
304 AssertRCReturn(rc, rc);
305 }
306
307 /*
308 * Determine the TSC configuration and frequency.
309 */
310 /* mode */
311 /** @cfgm{/TM/TSCVirtualized,bool,true}
312 * Use a virtualize TSC, i.e. trap all TSC access. */
313 rc = CFGMR3QueryBool(pCfgHandle, "TSCVirtualized", &pVM->tm.s.fTSCVirtualized);
314 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
315 pVM->tm.s.fTSCVirtualized = true; /* trap rdtsc */
316 else if (RT_FAILURE(rc))
317 return VMSetError(pVM, rc, RT_SRC_POS,
318 N_("Configuration error: Failed to querying bool value \"UseRealTSC\""));
319
320 /* source */
321 /** @cfgm{/TM/UseRealTSC,bool,false}
322 * Use the real TSC as time source for the TSC instead of the synchronous
323 * virtual clock (false, default). */
324 rc = CFGMR3QueryBool(pCfgHandle, "UseRealTSC", &pVM->tm.s.fTSCUseRealTSC);
325 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
326 pVM->tm.s.fTSCUseRealTSC = false; /* use virtual time */
327 else if (RT_FAILURE(rc))
328 return VMSetError(pVM, rc, RT_SRC_POS,
329 N_("Configuration error: Failed to querying bool value \"UseRealTSC\""));
330 if (!pVM->tm.s.fTSCUseRealTSC)
331 pVM->tm.s.fTSCVirtualized = true;
332
333 /* TSC reliability */
334 /** @cfgm{/TM/MaybeUseOffsettedHostTSC,bool,detect}
335 * Whether the CPU has a fixed TSC rate and may be used in offsetted mode with
336 * VT-x/AMD-V execution. This is autodetected in a very restrictive way by
337 * default. */
338 rc = CFGMR3QueryBool(pCfgHandle, "MaybeUseOffsettedHostTSC", &pVM->tm.s.fMaybeUseOffsettedHostTSC);
339 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
340 {
341 if (!pVM->tm.s.fTSCUseRealTSC)
342 pVM->tm.s.fMaybeUseOffsettedHostTSC = tmR3HasFixedTSC(pVM);
343 else
344 pVM->tm.s.fMaybeUseOffsettedHostTSC = true;
345 }
346
347 /** @cfgm{TM/TSCTicksPerSecond, uint32_t, Current TSC frequency from GIP}
348 * The number of TSC ticks per second (i.e. the TSC frequency). This will
349 * override TSCUseRealTSC, TSCVirtualized and MaybeUseOffsettedHostTSC.
350 */
351 rc = CFGMR3QueryU64(pCfgHandle, "TSCTicksPerSecond", &pVM->tm.s.cTSCTicksPerSecond);
352 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
353 {
354 pVM->tm.s.cTSCTicksPerSecond = tmR3CalibrateTSC(pVM);
355 if ( !pVM->tm.s.fTSCUseRealTSC
356 && pVM->tm.s.cTSCTicksPerSecond >= _4G)
357 {
358 pVM->tm.s.cTSCTicksPerSecond = _4G - 1; /* (A limitation of our math code) */
359 pVM->tm.s.fMaybeUseOffsettedHostTSC = false;
360 }
361 }
362 else if (RT_FAILURE(rc))
363 return VMSetError(pVM, rc, RT_SRC_POS,
364 N_("Configuration error: Failed to querying uint64_t value \"TSCTicksPerSecond\""));
365 else if ( pVM->tm.s.cTSCTicksPerSecond < _1M
366 || pVM->tm.s.cTSCTicksPerSecond >= _4G)
367 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
368 N_("Configuration error: \"TSCTicksPerSecond\" = %RI64 is not in the range 1MHz..4GHz-1"),
369 pVM->tm.s.cTSCTicksPerSecond);
370 else
371 {
372 pVM->tm.s.fTSCUseRealTSC = pVM->tm.s.fMaybeUseOffsettedHostTSC = false;
373 pVM->tm.s.fTSCVirtualized = true;
374 }
375
376 /** @cfgm{TM/TSCTiedToExecution, bool, false}
377 * Whether the TSC should be tied to execution. This will exclude most of the
378 * virtualization overhead, but will by default include the time spent in the
379 * halt state (see TM/TSCNotTiedToHalt). This setting will override all other
380 * TSC settings except for TSCTicksPerSecond and TSCNotTiedToHalt, which should
381 * be used avoided or used with great care. Note that this will only work right
382 * together with VT-x or AMD-V, and with a single virtual CPU. */
383 rc = CFGMR3QueryBoolDef(pCfgHandle, "TSCTiedToExecution", &pVM->tm.s.fTSCTiedToExecution, false);
384 if (RT_FAILURE(rc))
385 return VMSetError(pVM, rc, RT_SRC_POS,
386 N_("Configuration error: Failed to querying bool value \"TSCTiedToExecution\""));
387 if (pVM->tm.s.fTSCTiedToExecution)
388 {
389 /* tied to execution, override all other settings. */
390 pVM->tm.s.fTSCVirtualized = true;
391 pVM->tm.s.fTSCUseRealTSC = true;
392 pVM->tm.s.fMaybeUseOffsettedHostTSC = false;
393 }
394
395 /** @cfgm{TM/TSCNotTiedToHalt, bool, true}
396 * For overriding the default of TM/TSCTiedToExecution, i.e. set this to false
397 * to make the TSC freeze during HLT. */
398 rc = CFGMR3QueryBoolDef(pCfgHandle, "TSCNotTiedToHalt", &pVM->tm.s.fTSCNotTiedToHalt, false);
399 if (RT_FAILURE(rc))
400 return VMSetError(pVM, rc, RT_SRC_POS,
401 N_("Configuration error: Failed to querying bool value \"TSCNotTiedToHalt\""));
402
403 /* setup and report */
404 if (pVM->tm.s.fTSCVirtualized)
405 CPUMR3SetCR4Feature(pVM, X86_CR4_TSD, ~X86_CR4_TSD);
406 else
407 CPUMR3SetCR4Feature(pVM, 0, ~X86_CR4_TSD);
408 LogRel(("TM: cTSCTicksPerSecond=%#RX64 (%'RU64) fTSCVirtualized=%RTbool fTSCUseRealTSC=%RTbool\n"
409 "TM: fMaybeUseOffsettedHostTSC=%RTbool TSCTiedToExecution=%RTbool TSCNotTiedToHalt=%RTbool\n",
410 pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.fTSCVirtualized, pVM->tm.s.fTSCUseRealTSC,
411 pVM->tm.s.fMaybeUseOffsettedHostTSC, pVM->tm.s.fTSCTiedToExecution, pVM->tm.s.fTSCNotTiedToHalt));
412
413 /*
414 * Configure the timer synchronous virtual time.
415 */
416 /** @cfgm{TM/ScheduleSlack, uint32_t, ns, 0, UINT32_MAX, 100000}
417 * Scheduling slack when processing timers. */
418 rc = CFGMR3QueryU32(pCfgHandle, "ScheduleSlack", &pVM->tm.s.u32VirtualSyncScheduleSlack);
419 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
420 pVM->tm.s.u32VirtualSyncScheduleSlack = 100000; /* 0.100ms (ASSUMES virtual time is nanoseconds) */
421 else if (RT_FAILURE(rc))
422 return VMSetError(pVM, rc, RT_SRC_POS,
423 N_("Configuration error: Failed to querying 32-bit integer value \"ScheduleSlack\""));
424
425 /** @cfgm{TM/CatchUpStopThreshold, uint64_t, ns, 0, UINT64_MAX, 500000}
426 * When to stop a catch-up, considering it successful. */
427 rc = CFGMR3QueryU64(pCfgHandle, "CatchUpStopThreshold", &pVM->tm.s.u64VirtualSyncCatchUpStopThreshold);
428 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
429 pVM->tm.s.u64VirtualSyncCatchUpStopThreshold = 500000; /* 0.5ms */
430 else if (RT_FAILURE(rc))
431 return VMSetError(pVM, rc, RT_SRC_POS,
432 N_("Configuration error: Failed to querying 64-bit integer value \"CatchUpStopThreshold\""));
433
434 /** @cfgm{TM/CatchUpGiveUpThreshold, uint64_t, ns, 0, UINT64_MAX, 60000000000}
435 * When to give up a catch-up attempt. */
436 rc = CFGMR3QueryU64(pCfgHandle, "CatchUpGiveUpThreshold", &pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold);
437 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
438 pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold = UINT64_C(60000000000); /* 60 sec */
439 else if (RT_FAILURE(rc))
440 return VMSetError(pVM, rc, RT_SRC_POS,
441 N_("Configuration error: Failed to querying 64-bit integer value \"CatchUpGiveUpThreshold\""));
442
443
444 /** @cfgm{TM/CatchUpPrecentage[0..9], uint32_t, %, 1, 2000, various}
445 * The catch-up percent for a given period. */
446 /** @cfgm{TM/CatchUpStartThreshold[0..9], uint64_t, ns, 0, UINT64_MAX,
447 * The catch-up period threshold, or if you like, when a period starts. */
448#define TM_CFG_PERIOD(iPeriod, DefStart, DefPct) \
449 do \
450 { \
451 uint64_t u64; \
452 rc = CFGMR3QueryU64(pCfgHandle, "CatchUpStartThreshold" #iPeriod, &u64); \
453 if (rc == VERR_CFGM_VALUE_NOT_FOUND) \
454 u64 = UINT64_C(DefStart); \
455 else if (RT_FAILURE(rc)) \
456 return VMSetError(pVM, rc, RT_SRC_POS, N_("Configuration error: Failed to querying 64-bit integer value \"CatchUpThreshold" #iPeriod "\"")); \
457 if ( (iPeriod > 0 && u64 <= pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod - 1].u64Start) \
458 || u64 >= pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold) \
459 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("Configuration error: Invalid start of period #" #iPeriod ": %'RU64"), u64); \
460 pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod].u64Start = u64; \
461 rc = CFGMR3QueryU32(pCfgHandle, "CatchUpPrecentage" #iPeriod, &pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod].u32Percentage); \
462 if (rc == VERR_CFGM_VALUE_NOT_FOUND) \
463 pVM->tm.s.aVirtualSyncCatchUpPeriods[iPeriod].u32Percentage = (DefPct); \
464 else if (RT_FAILURE(rc)) \
465 return VMSetError(pVM, rc, RT_SRC_POS, N_("Configuration error: Failed to querying 32-bit integer value \"CatchUpPrecentage" #iPeriod "\"")); \
466 } while (0)
467 /* This needs more tuning. Not sure if we really need so many period and be so gentle. */
468 TM_CFG_PERIOD(0, 750000, 5); /* 0.75ms at 1.05x */
469 TM_CFG_PERIOD(1, 1500000, 10); /* 1.50ms at 1.10x */
470 TM_CFG_PERIOD(2, 8000000, 25); /* 8ms at 1.25x */
471 TM_CFG_PERIOD(3, 30000000, 50); /* 30ms at 1.50x */
472 TM_CFG_PERIOD(4, 75000000, 75); /* 75ms at 1.75x */
473 TM_CFG_PERIOD(5, 175000000, 100); /* 175ms at 2x */
474 TM_CFG_PERIOD(6, 500000000, 200); /* 500ms at 3x */
475 TM_CFG_PERIOD(7, 3000000000, 300); /* 3s at 4x */
476 TM_CFG_PERIOD(8,30000000000, 400); /* 30s at 5x */
477 TM_CFG_PERIOD(9,55000000000, 500); /* 55s at 6x */
478 AssertCompile(RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods) == 10);
479#undef TM_CFG_PERIOD
480
481 /*
482 * Configure real world time (UTC).
483 */
484 /** @cfgm{TM/UTCOffset, int64_t, ns, INT64_MIN, INT64_MAX, 0}
485 * The UTC offset. This is used to put the guest back or forwards in time. */
486 rc = CFGMR3QueryS64(pCfgHandle, "UTCOffset", &pVM->tm.s.offUTC);
487 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
488 pVM->tm.s.offUTC = 0; /* ns */
489 else if (RT_FAILURE(rc))
490 return VMSetError(pVM, rc, RT_SRC_POS,
491 N_("Configuration error: Failed to querying 64-bit integer value \"UTCOffset\""));
492
493 /*
494 * Setup the warp drive.
495 */
496 /** @cfgm{TM/WarpDrivePercentage, uint32_t, %, 0, 20000, 100}
497 * The warp drive percentage, 100% is normal speed. This is used to speed up
498 * or slow down the virtual clock, which can be useful for fast forwarding
499 * borring periods during tests. */
500 rc = CFGMR3QueryU32(pCfgHandle, "WarpDrivePercentage", &pVM->tm.s.u32VirtualWarpDrivePercentage);
501 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
502 rc = CFGMR3QueryU32(CFGMR3GetRoot(pVM), "WarpDrivePercentage", &pVM->tm.s.u32VirtualWarpDrivePercentage); /* legacy */
503 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
504 pVM->tm.s.u32VirtualWarpDrivePercentage = 100;
505 else if (RT_FAILURE(rc))
506 return VMSetError(pVM, rc, RT_SRC_POS,
507 N_("Configuration error: Failed to querying uint32_t value \"WarpDrivePercent\""));
508 else if ( pVM->tm.s.u32VirtualWarpDrivePercentage < 2
509 || pVM->tm.s.u32VirtualWarpDrivePercentage > 20000)
510 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
511 N_("Configuration error: \"WarpDrivePercent\" = %RI32 is not in the range 2..20000"),
512 pVM->tm.s.u32VirtualWarpDrivePercentage);
513 pVM->tm.s.fVirtualWarpDrive = pVM->tm.s.u32VirtualWarpDrivePercentage != 100;
514 if (pVM->tm.s.fVirtualWarpDrive)
515 LogRel(("TM: u32VirtualWarpDrivePercentage=%RI32\n", pVM->tm.s.u32VirtualWarpDrivePercentage));
516
517 /*
518 * Gather the Host Hz configuration values.
519 */
520 rc = CFGMR3QueryU32Def(pCfgHandle, "HostHzMax", &pVM->tm.s.cHostHzMax, 20000);
521 if (RT_FAILURE(rc))
522 return VMSetError(pVM, rc, RT_SRC_POS,
523 N_("Configuration error: Failed to querying uint32_t value \"HostHzMax\""));
524
525 rc = CFGMR3QueryU32Def(pCfgHandle, "HostHzFudgeFactorTimerCpu", &pVM->tm.s.cPctHostHzFudgeFactorTimerCpu, 111);
526 if (RT_FAILURE(rc))
527 return VMSetError(pVM, rc, RT_SRC_POS,
528 N_("Configuration error: Failed to querying uint32_t value \"HostHzFudgeFactorTimerCpu\""));
529
530 rc = CFGMR3QueryU32Def(pCfgHandle, "HostHzFudgeFactorOtherCpu", &pVM->tm.s.cPctHostHzFudgeFactorOtherCpu, 110);
531 if (RT_FAILURE(rc))
532 return VMSetError(pVM, rc, RT_SRC_POS,
533 N_("Configuration error: Failed to querying uint32_t value \"HostHzFudgeFactorOtherCpu\""));
534
535 rc = CFGMR3QueryU32Def(pCfgHandle, "HostHzFudgeFactorCatchUp100", &pVM->tm.s.cPctHostHzFudgeFactorCatchUp100, 300);
536 if (RT_FAILURE(rc))
537 return VMSetError(pVM, rc, RT_SRC_POS,
538 N_("Configuration error: Failed to querying uint32_t value \"HostHzFudgeFactorCatchUp100\""));
539
540 rc = CFGMR3QueryU32Def(pCfgHandle, "HostHzFudgeFactorCatchUp200", &pVM->tm.s.cPctHostHzFudgeFactorCatchUp200, 250);
541 if (RT_FAILURE(rc))
542 return VMSetError(pVM, rc, RT_SRC_POS,
543 N_("Configuration error: Failed to querying uint32_t value \"HostHzFudgeFactorCatchUp200\""));
544
545 rc = CFGMR3QueryU32Def(pCfgHandle, "HostHzFudgeFactorCatchUp400", &pVM->tm.s.cPctHostHzFudgeFactorCatchUp400, 200);
546 if (RT_FAILURE(rc))
547 return VMSetError(pVM, rc, RT_SRC_POS,
548 N_("Configuration error: Failed to querying uint32_t value \"HostHzFudgeFactorCatchUp400\""));
549
550 /*
551 * Start the timer (guard against REM not yielding).
552 */
553 /** @cfgm{TM/TimerMillies, uint32_t, ms, 1, 1000, 10}
554 * The watchdog timer interval. */
555 uint32_t u32Millies;
556 rc = CFGMR3QueryU32(pCfgHandle, "TimerMillies", &u32Millies);
557 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
558 u32Millies = 10;
559 else if (RT_FAILURE(rc))
560 return VMSetError(pVM, rc, RT_SRC_POS,
561 N_("Configuration error: Failed to query uint32_t value \"TimerMillies\""));
562 rc = RTTimerCreate(&pVM->tm.s.pTimer, u32Millies, tmR3TimerCallback, pVM);
563 if (RT_FAILURE(rc))
564 {
565 AssertMsgFailed(("Failed to create timer, u32Millies=%d rc=%Rrc.\n", u32Millies, rc));
566 return rc;
567 }
568 Log(("TM: Created timer %p firing every %d milliseconds\n", pVM->tm.s.pTimer, u32Millies));
569 pVM->tm.s.u32TimerMillies = u32Millies;
570
571 /*
572 * Register saved state.
573 */
574 rc = SSMR3RegisterInternal(pVM, "tm", 1, TM_SAVED_STATE_VERSION, sizeof(uint64_t) * 8,
575 NULL, NULL, NULL,
576 NULL, tmR3Save, NULL,
577 NULL, tmR3Load, NULL);
578 if (RT_FAILURE(rc))
579 return rc;
580
581 /*
582 * Register statistics.
583 */
584 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataR3.c1nsSteps,STAMTYPE_U32, "/TM/R3/1nsSteps", STAMUNIT_OCCURENCES, "Virtual time 1ns steps (due to TSC / GIP variations).");
585 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataR3.cBadPrev, STAMTYPE_U32, "/TM/R3/cBadPrev", STAMUNIT_OCCURENCES, "Times the previous virtual time was considered erratic (shouldn't ever happen).");
586 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataR0.c1nsSteps,STAMTYPE_U32, "/TM/R0/1nsSteps", STAMUNIT_OCCURENCES, "Virtual time 1ns steps (due to TSC / GIP variations).");
587 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataR0.cBadPrev, STAMTYPE_U32, "/TM/R0/cBadPrev", STAMUNIT_OCCURENCES, "Times the previous virtual time was considered erratic (shouldn't ever happen).");
588 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataRC.c1nsSteps,STAMTYPE_U32, "/TM/RC/1nsSteps", STAMUNIT_OCCURENCES, "Virtual time 1ns steps (due to TSC / GIP variations).");
589 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.VirtualGetRawDataRC.cBadPrev, STAMTYPE_U32, "/TM/RC/cBadPrev", STAMUNIT_OCCURENCES, "Times the previous virtual time was considered erratic (shouldn't ever happen).");
590 STAM_REL_REG( pVM,(void*)&pVM->tm.s.offVirtualSync, STAMTYPE_U64, "/TM/VirtualSync/CurrentOffset", STAMUNIT_NS, "The current offset. (subtract GivenUp to get the lag)");
591 STAM_REL_REG_USED(pVM,(void*)&pVM->tm.s.offVirtualSyncGivenUp, STAMTYPE_U64, "/TM/VirtualSync/GivenUp", STAMUNIT_NS, "Nanoseconds of the 'CurrentOffset' that's been given up and won't ever be attempted caught up with.");
592 STAM_REL_REG( pVM,(void*)&pVM->tm.s.uMaxHzHint, STAMTYPE_U32, "/TM/MaxHzHint", STAMUNIT_HZ, "Max guest timer frequency hint.");
593
594#ifdef VBOX_WITH_STATISTICS
595 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataR3.cExpired, STAMTYPE_U32, "/TM/R3/cExpired", STAMUNIT_OCCURENCES, "Times the TSC interval expired (overlaps 1ns steps).");
596 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataR3.cUpdateRaces,STAMTYPE_U32, "/TM/R3/cUpdateRaces", STAMUNIT_OCCURENCES, "Thread races when updating the previous timestamp.");
597 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataR0.cExpired, STAMTYPE_U32, "/TM/R0/cExpired", STAMUNIT_OCCURENCES, "Times the TSC interval expired (overlaps 1ns steps).");
598 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataR0.cUpdateRaces,STAMTYPE_U32, "/TM/R0/cUpdateRaces", STAMUNIT_OCCURENCES, "Thread races when updating the previous timestamp.");
599 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataRC.cExpired, STAMTYPE_U32, "/TM/RC/cExpired", STAMUNIT_OCCURENCES, "Times the TSC interval expired (overlaps 1ns steps).");
600 STAM_REG_USED(pVM,(void *)&pVM->tm.s.VirtualGetRawDataRC.cUpdateRaces,STAMTYPE_U32, "/TM/RC/cUpdateRaces", STAMUNIT_OCCURENCES, "Thread races when updating the previous timestamp.");
601 STAM_REG(pVM, &pVM->tm.s.StatDoQueues, STAMTYPE_PROFILE, "/TM/DoQueues", STAMUNIT_TICKS_PER_CALL, "Profiling timer TMR3TimerQueuesDo.");
602 STAM_REG(pVM, &pVM->tm.s.aStatDoQueues[TMCLOCK_VIRTUAL], STAMTYPE_PROFILE_ADV, "/TM/DoQueues/Virtual", STAMUNIT_TICKS_PER_CALL, "Time spent on the virtual clock queue.");
603 STAM_REG(pVM, &pVM->tm.s.aStatDoQueues[TMCLOCK_VIRTUAL_SYNC], STAMTYPE_PROFILE_ADV, "/TM/DoQueues/VirtualSync", STAMUNIT_TICKS_PER_CALL, "Time spent on the virtual sync clock queue.");
604 STAM_REG(pVM, &pVM->tm.s.aStatDoQueues[TMCLOCK_REAL], STAMTYPE_PROFILE_ADV, "/TM/DoQueues/Real", STAMUNIT_TICKS_PER_CALL, "Time spent on the real clock queue.");
605
606 STAM_REG(pVM, &pVM->tm.s.StatPoll, STAMTYPE_COUNTER, "/TM/Poll", STAMUNIT_OCCURENCES, "TMTimerPoll calls.");
607 STAM_REG(pVM, &pVM->tm.s.StatPollAlreadySet, STAMTYPE_COUNTER, "/TM/Poll/AlreadySet", STAMUNIT_OCCURENCES, "TMTimerPoll calls where the FF was already set.");
608 STAM_REG(pVM, &pVM->tm.s.StatPollELoop, STAMTYPE_COUNTER, "/TM/Poll/ELoop", STAMUNIT_OCCURENCES, "Times TMTimerPoll has given up getting a consistent virtual sync data set.");
609 STAM_REG(pVM, &pVM->tm.s.StatPollMiss, STAMTYPE_COUNTER, "/TM/Poll/Miss", STAMUNIT_OCCURENCES, "TMTimerPoll calls where nothing had expired.");
610 STAM_REG(pVM, &pVM->tm.s.StatPollRunning, STAMTYPE_COUNTER, "/TM/Poll/Running", STAMUNIT_OCCURENCES, "TMTimerPoll calls where the queues were being run.");
611 STAM_REG(pVM, &pVM->tm.s.StatPollSimple, STAMTYPE_COUNTER, "/TM/Poll/Simple", STAMUNIT_OCCURENCES, "TMTimerPoll calls where we could take the simple path.");
612 STAM_REG(pVM, &pVM->tm.s.StatPollVirtual, STAMTYPE_COUNTER, "/TM/Poll/HitsVirtual", STAMUNIT_OCCURENCES, "The number of times TMTimerPoll found an expired TMCLOCK_VIRTUAL queue.");
613 STAM_REG(pVM, &pVM->tm.s.StatPollVirtualSync, STAMTYPE_COUNTER, "/TM/Poll/HitsVirtualSync", STAMUNIT_OCCURENCES, "The number of times TMTimerPoll found an expired TMCLOCK_VIRTUAL_SYNC queue.");
614
615 STAM_REG(pVM, &pVM->tm.s.StatPostponedR3, STAMTYPE_COUNTER, "/TM/PostponedR3", STAMUNIT_OCCURENCES, "Postponed due to unschedulable state, in ring-3.");
616 STAM_REG(pVM, &pVM->tm.s.StatPostponedRZ, STAMTYPE_COUNTER, "/TM/PostponedRZ", STAMUNIT_OCCURENCES, "Postponed due to unschedulable state, in ring-0 / RC.");
617
618 STAM_REG(pVM, &pVM->tm.s.StatScheduleOneR3, STAMTYPE_PROFILE, "/TM/ScheduleOneR3", STAMUNIT_TICKS_PER_CALL, "Profiling the scheduling of one queue during a TMTimer* call in EMT.");
619 STAM_REG(pVM, &pVM->tm.s.StatScheduleOneRZ, STAMTYPE_PROFILE, "/TM/ScheduleOneRZ", STAMUNIT_TICKS_PER_CALL, "Profiling the scheduling of one queue during a TMTimer* call in EMT.");
620 STAM_REG(pVM, &pVM->tm.s.StatScheduleSetFF, STAMTYPE_COUNTER, "/TM/ScheduleSetFF", STAMUNIT_OCCURENCES, "The number of times the timer FF was set instead of doing scheduling.");
621
622 STAM_REG(pVM, &pVM->tm.s.StatTimerSet, STAMTYPE_COUNTER, "/TM/TimerSet", STAMUNIT_OCCURENCES, "Calls, except virtual sync timers");
623 STAM_REG(pVM, &pVM->tm.s.StatTimerSetOpt, STAMTYPE_COUNTER, "/TM/TimerSet/Opt", STAMUNIT_OCCURENCES, "Optimized path taken.");
624 STAM_REG(pVM, &pVM->tm.s.StatTimerSetR3, STAMTYPE_PROFILE, "/TM/TimerSet/R3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-3.");
625 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRZ, STAMTYPE_PROFILE, "/TM/TimerSet/RZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-0 / RC.");
626 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStActive, STAMTYPE_COUNTER, "/TM/TimerSet/StActive", STAMUNIT_OCCURENCES, "ACTIVE");
627 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStExpDeliver, STAMTYPE_COUNTER, "/TM/TimerSet/StExpDeliver", STAMUNIT_OCCURENCES, "EXPIRED_DELIVER");
628 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStOther, STAMTYPE_COUNTER, "/TM/TimerSet/StOther", STAMUNIT_OCCURENCES, "Other states");
629 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStPendStop, STAMTYPE_COUNTER, "/TM/TimerSet/StPendStop", STAMUNIT_OCCURENCES, "PENDING_STOP");
630 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStPendStopSched, STAMTYPE_COUNTER, "/TM/TimerSet/StPendStopSched", STAMUNIT_OCCURENCES, "PENDING_STOP_SCHEDULE");
631 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStPendSched, STAMTYPE_COUNTER, "/TM/TimerSet/StPendSched", STAMUNIT_OCCURENCES, "PENDING_SCHEDULE");
632 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStPendResched, STAMTYPE_COUNTER, "/TM/TimerSet/StPendResched", STAMUNIT_OCCURENCES, "PENDING_RESCHEDULE");
633 STAM_REG(pVM, &pVM->tm.s.StatTimerSetStStopped, STAMTYPE_COUNTER, "/TM/TimerSet/StStopped", STAMUNIT_OCCURENCES, "STOPPED");
634
635 STAM_REG(pVM, &pVM->tm.s.StatTimerSetVs, STAMTYPE_COUNTER, "/TM/TimerSetVs", STAMUNIT_OCCURENCES, "TMTimerSet calls on virtual sync timers");
636 STAM_REG(pVM, &pVM->tm.s.StatTimerSetVsR3, STAMTYPE_PROFILE, "/TM/TimerSetVs/R3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-3 on virtual sync timers.");
637 STAM_REG(pVM, &pVM->tm.s.StatTimerSetVsRZ, STAMTYPE_PROFILE, "/TM/TimerSetVs/RZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSet calls made in ring-0 / RC on virtual sync timers.");
638 STAM_REG(pVM, &pVM->tm.s.StatTimerSetVsStActive, STAMTYPE_COUNTER, "/TM/TimerSetVs/StActive", STAMUNIT_OCCURENCES, "ACTIVE");
639 STAM_REG(pVM, &pVM->tm.s.StatTimerSetVsStExpDeliver, STAMTYPE_COUNTER, "/TM/TimerSetVs/StExpDeliver", STAMUNIT_OCCURENCES, "EXPIRED_DELIVER");
640 STAM_REG(pVM, &pVM->tm.s.StatTimerSetVsStStopped, STAMTYPE_COUNTER, "/TM/TimerSetVs/StStopped", STAMUNIT_OCCURENCES, "STOPPED");
641
642 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelative, STAMTYPE_COUNTER, "/TM/TimerSetRelative", STAMUNIT_OCCURENCES, "Calls, except virtual sync timers");
643 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeOpt, STAMTYPE_COUNTER, "/TM/TimerSetRelative/Opt", STAMUNIT_OCCURENCES, "Optimized path taken.");
644 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeR3, STAMTYPE_PROFILE, "/TM/TimerSetRelative/R3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSetRelative calls made in ring-3 (sans virtual sync).");
645 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeRZ, STAMTYPE_PROFILE, "/TM/TimerSetRelative/RZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSetReltaive calls made in ring-0 / RC (sans virtual sync).");
646 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStActive, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StActive", STAMUNIT_OCCURENCES, "ACTIVE");
647 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStExpDeliver, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StExpDeliver", STAMUNIT_OCCURENCES, "EXPIRED_DELIVER");
648 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStOther, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StOther", STAMUNIT_OCCURENCES, "Other states");
649 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStPendStop, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StPendStop", STAMUNIT_OCCURENCES, "PENDING_STOP");
650 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStPendStopSched, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StPendStopSched",STAMUNIT_OCCURENCES, "PENDING_STOP_SCHEDULE");
651 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStPendSched, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StPendSched", STAMUNIT_OCCURENCES, "PENDING_SCHEDULE");
652 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStPendResched, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StPendResched", STAMUNIT_OCCURENCES, "PENDING_RESCHEDULE");
653 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeStStopped, STAMTYPE_COUNTER, "/TM/TimerSetRelative/StStopped", STAMUNIT_OCCURENCES, "STOPPED");
654
655 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeVs, STAMTYPE_COUNTER, "/TM/TimerSetRelativeVs", STAMUNIT_OCCURENCES, "TMTimerSetRelative calls on virtual sync timers");
656 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeVsR3, STAMTYPE_PROFILE, "/TM/TimerSetRelativeVs/R3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSetRelative calls made in ring-3 on virtual sync timers.");
657 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeVsRZ, STAMTYPE_PROFILE, "/TM/TimerSetRelativeVs/RZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerSetReltaive calls made in ring-0 / RC on virtual sync timers.");
658 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeVsStActive, STAMTYPE_COUNTER, "/TM/TimerSetRelativeVs/StActive", STAMUNIT_OCCURENCES, "ACTIVE");
659 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeVsStExpDeliver, STAMTYPE_COUNTER, "/TM/TimerSetRelativeVs/StExpDeliver", STAMUNIT_OCCURENCES, "EXPIRED_DELIVER");
660 STAM_REG(pVM, &pVM->tm.s.StatTimerSetRelativeVsStStopped, STAMTYPE_COUNTER, "/TM/TimerSetRelativeVs/StStopped", STAMUNIT_OCCURENCES, "STOPPED");
661
662 STAM_REG(pVM, &pVM->tm.s.StatTimerStopR3, STAMTYPE_PROFILE, "/TM/TimerStopR3", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerStop calls made in ring-3.");
663 STAM_REG(pVM, &pVM->tm.s.StatTimerStopRZ, STAMTYPE_PROFILE, "/TM/TimerStopRZ", STAMUNIT_TICKS_PER_CALL, "Profiling TMTimerStop calls made in ring-0 / RC.");
664
665 STAM_REG(pVM, &pVM->tm.s.StatVirtualGet, STAMTYPE_COUNTER, "/TM/VirtualGet", STAMUNIT_OCCURENCES, "The number of times TMTimerGet was called when the clock was running.");
666 STAM_REG(pVM, &pVM->tm.s.StatVirtualGetSetFF, STAMTYPE_COUNTER, "/TM/VirtualGetSetFF", STAMUNIT_OCCURENCES, "Times we set the FF when calling TMTimerGet.");
667 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGet, STAMTYPE_COUNTER, "/TM/VirtualSyncGet", STAMUNIT_OCCURENCES, "The number of times tmVirtualSyncGetEx was called.");
668 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetAdjLast, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/AdjLast", STAMUNIT_OCCURENCES, "Times we've adjusted against the last returned time stamp .");
669 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetELoop, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/ELoop", STAMUNIT_OCCURENCES, "Times tmVirtualSyncGetEx has given up getting a consistent virtual sync data set.");
670 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetExpired, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/Expired", STAMUNIT_OCCURENCES, "Times tmVirtualSyncGetEx encountered an expired timer stopping the clock.");
671 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetLocked, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/Locked", STAMUNIT_OCCURENCES, "Times we successfully acquired the lock in tmVirtualSyncGetEx.");
672 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetLockless, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/Lockless", STAMUNIT_OCCURENCES, "Times tmVirtualSyncGetEx returned without needing to take the lock.");
673 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGetSetFF, STAMTYPE_COUNTER, "/TM/VirtualSyncGet/SetFF", STAMUNIT_OCCURENCES, "Times we set the FF when calling tmVirtualSyncGetEx.");
674 STAM_REG(pVM, &pVM->tm.s.StatVirtualPause, STAMTYPE_COUNTER, "/TM/VirtualPause", STAMUNIT_OCCURENCES, "The number of times TMR3TimerPause was called.");
675 STAM_REG(pVM, &pVM->tm.s.StatVirtualResume, STAMTYPE_COUNTER, "/TM/VirtualResume", STAMUNIT_OCCURENCES, "The number of times TMR3TimerResume was called.");
676
677 STAM_REG(pVM, &pVM->tm.s.StatTimerCallbackSetFF, STAMTYPE_COUNTER, "/TM/CallbackSetFF", STAMUNIT_OCCURENCES, "The number of times the timer callback set FF.");
678
679 STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupLE010, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupLE010", STAMUNIT_OCCURENCES, "In catch-up mode, 10% or lower.");
680 STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupLE025, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupLE025", STAMUNIT_OCCURENCES, "In catch-up mode, 25%-11%.");
681 STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupLE100, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupLE100", STAMUNIT_OCCURENCES, "In catch-up mode, 100%-26%.");
682 STAM_REG(pVM, &pVM->tm.s.StatTSCCatchupOther, STAMTYPE_COUNTER, "/TM/TSC/Intercept/CatchupOther", STAMUNIT_OCCURENCES, "In catch-up mode, > 100%.");
683 STAM_REG(pVM, &pVM->tm.s.StatTSCNotFixed, STAMTYPE_COUNTER, "/TM/TSC/Intercept/NotFixed", STAMUNIT_OCCURENCES, "TSC is not fixed, it may run at variable speed.");
684 STAM_REG(pVM, &pVM->tm.s.StatTSCNotTicking, STAMTYPE_COUNTER, "/TM/TSC/Intercept/NotTicking", STAMUNIT_OCCURENCES, "TSC is not ticking.");
685 STAM_REG(pVM, &pVM->tm.s.StatTSCSyncNotTicking, STAMTYPE_COUNTER, "/TM/TSC/Intercept/SyncNotTicking", STAMUNIT_OCCURENCES, "VirtualSync isn't ticking.");
686 STAM_REG(pVM, &pVM->tm.s.StatTSCWarp, STAMTYPE_COUNTER, "/TM/TSC/Intercept/Warp", STAMUNIT_OCCURENCES, "Warpdrive is active.");
687 STAM_REG(pVM, &pVM->tm.s.StatTSCSet, STAMTYPE_COUNTER, "/TM/TSC/Sets", STAMUNIT_OCCURENCES, "Calls to TMCpuTickSet.");
688 STAM_REG(pVM, &pVM->tm.s.StatTSCUnderflow, STAMTYPE_COUNTER, "/TM/TSC/Underflow", STAMUNIT_OCCURENCES, "TSC underflow; corrected with last seen value .");
689#endif /* VBOX_WITH_STATISTICS */
690
691 for (VMCPUID i = 0; i < pVM->cCpus; i++)
692 {
693 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.offTSCRawSrc, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS, "TSC offset relative the raw source", "/TM/TSC/offCPU%u", i);
694#ifndef VBOX_WITHOUT_NS_ACCOUNTING
695# if defined(VBOX_WITH_STATISTICS) || defined(VBOX_WITH_NS_ACCOUNTING_STATS)
696 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.StatNsTotal, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Resettable: Total CPU run time.", "/TM/CPU/%02u", i);
697 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.StatNsExecuting, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_OCCURENCE, "Resettable: Time spent executing guest code.", "/TM/CPU/%02u/PrfExecuting", i);
698 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.StatNsExecLong, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_OCCURENCE, "Resettable: Time spent executing guest code - long hauls.", "/TM/CPU/%02u/PrfExecLong", i);
699 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.StatNsExecShort, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_OCCURENCE, "Resettable: Time spent executing guest code - short stretches.", "/TM/CPU/%02u/PrfExecShort", i);
700 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.StatNsExecTiny, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_OCCURENCE, "Resettable: Time spent executing guest code - tiny bits.", "/TM/CPU/%02u/PrfExecTiny", i);
701 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.StatNsHalted, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_OCCURENCE, "Resettable: Time spent halted.", "/TM/CPU/%02u/PrfHalted", i);
702 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.StatNsOther, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_OCCURENCE, "Resettable: Time spent in the VMM or preempted.", "/TM/CPU/%02u/PrfOther", i);
703# endif
704 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.cNsTotal, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Total CPU run time.", "/TM/CPU/%02u/cNsTotal", i);
705 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.cNsExecuting, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Time spent executing guest code.", "/TM/CPU/%02u/cNsExecuting", i);
706 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.cNsHalted, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Time spent halted.", "/TM/CPU/%02u/cNsHalted", i);
707 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.cNsOther, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Time spent in the VMM or preempted.", "/TM/CPU/%02u/cNsOther", i);
708 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.cPeriodsExecuting, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Times executed guest code.", "/TM/CPU/%02u/cPeriodsExecuting", i);
709 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.cPeriodsHalted, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Times halted.", "/TM/CPU/%02u/cPeriodsHalted", i);
710 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.CpuLoad.cPctExecuting, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent executing guest code recently.", "/TM/CPU/%02u/pctExecuting", i);
711 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.CpuLoad.cPctHalted, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent halted recently.", "/TM/CPU/%02u/pctHalted", i);
712 STAMR3RegisterF(pVM, &pVM->aCpus[i].tm.s.CpuLoad.cPctOther, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent in the VMM or preempted recently.", "/TM/CPU/%02u/pctOther", i);
713#endif
714 }
715#ifndef VBOX_WITHOUT_NS_ACCOUNTING
716 STAMR3RegisterF(pVM, &pVM->tm.s.CpuLoad.cPctExecuting, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent executing guest code recently.", "/TM/CPU/pctExecuting");
717 STAMR3RegisterF(pVM, &pVM->tm.s.CpuLoad.cPctHalted, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent halted recently.", "/TM/CPU/pctHalted");
718 STAMR3RegisterF(pVM, &pVM->tm.s.CpuLoad.cPctOther, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "Time spent in the VMM or preempted recently.", "/TM/CPU/pctOther");
719#endif
720
721#ifdef VBOX_WITH_STATISTICS
722 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncCatchup, STAMTYPE_PROFILE_ADV, "/TM/VirtualSync/CatchUp", STAMUNIT_TICKS_PER_OCCURENCE, "Counting and measuring the times spent catching up.");
723 STAM_REG(pVM, (void *)&pVM->tm.s.fVirtualSyncCatchUp, STAMTYPE_U8, "/TM/VirtualSync/CatchUpActive", STAMUNIT_NONE, "Catch-Up active indicator.");
724 STAM_REG(pVM, (void *)&pVM->tm.s.u32VirtualSyncCatchUpPercentage, STAMTYPE_U32, "/TM/VirtualSync/CatchUpPercentage", STAMUNIT_PCT, "The catch-up percentage. (+100/100 to get clock multiplier)");
725 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncFF, STAMTYPE_PROFILE, "/TM/VirtualSync/FF", STAMUNIT_TICKS_PER_OCCURENCE, "Time spent in TMR3VirtualSyncFF by all but the dedicate timer EMT.");
726 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGiveUp, STAMTYPE_COUNTER, "/TM/VirtualSync/GiveUp", STAMUNIT_OCCURENCES, "Times the catch-up was abandoned.");
727 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncGiveUpBeforeStarting, STAMTYPE_COUNTER, "/TM/VirtualSync/GiveUpBeforeStarting",STAMUNIT_OCCURENCES, "Times the catch-up was abandoned before even starting. (Typically debugging++.)");
728 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRun, STAMTYPE_COUNTER, "/TM/VirtualSync/Run", STAMUNIT_OCCURENCES, "Times the virtual sync timer queue was considered.");
729 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunRestart, STAMTYPE_COUNTER, "/TM/VirtualSync/Run/Restarts", STAMUNIT_OCCURENCES, "Times the clock was restarted after a run.");
730 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunStop, STAMTYPE_COUNTER, "/TM/VirtualSync/Run/Stop", STAMUNIT_OCCURENCES, "Times the clock was stopped when calculating the current time before examining the timers.");
731 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunStoppedAlready, STAMTYPE_COUNTER, "/TM/VirtualSync/Run/StoppedAlready", STAMUNIT_OCCURENCES, "Times the clock was already stopped elsewhere (TMVirtualSyncGet).");
732 STAM_REG(pVM, &pVM->tm.s.StatVirtualSyncRunSlack, STAMTYPE_PROFILE, "/TM/VirtualSync/Run/Slack", STAMUNIT_NS_PER_OCCURENCE, "The scheduling slack. (Catch-up handed out when running timers.)");
733 for (unsigned i = 0; i < RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods); i++)
734 {
735 STAMR3RegisterF(pVM, &pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_PCT, "The catch-up percentage.", "/TM/VirtualSync/Periods/%u", i);
736 STAMR3RegisterF(pVM, &pVM->tm.s.aStatVirtualSyncCatchupAdjust[i], STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Times adjusted to this period.", "/TM/VirtualSync/Periods/%u/Adjust", i);
737 STAMR3RegisterF(pVM, &pVM->tm.s.aStatVirtualSyncCatchupInitial[i], STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Times started in this period.", "/TM/VirtualSync/Periods/%u/Initial", i);
738 STAMR3RegisterF(pVM, &pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u64Start, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_NS, "Start of this period (lag).", "/TM/VirtualSync/Periods/%u/Start", i);
739 }
740#endif /* VBOX_WITH_STATISTICS */
741
742 /*
743 * Register info handlers.
744 */
745 DBGFR3InfoRegisterInternalEx(pVM, "timers", "Dumps all timers. No arguments.", tmR3TimerInfo, DBGFINFO_FLAGS_RUN_ON_EMT);
746 DBGFR3InfoRegisterInternalEx(pVM, "activetimers", "Dumps active all timers. No arguments.", tmR3TimerInfoActive, DBGFINFO_FLAGS_RUN_ON_EMT);
747 DBGFR3InfoRegisterInternalEx(pVM, "clocks", "Display the time of the various clocks.", tmR3InfoClocks, DBGFINFO_FLAGS_RUN_ON_EMT);
748
749 return VINF_SUCCESS;
750}
751
752
753/**
754 * Checks if the host CPU has a fixed TSC frequency.
755 *
756 * @returns true if it has, false if it hasn't.
757 *
758 * @remark This test doesn't bother with very old CPUs that don't do power
759 * management or any other stuff that might influence the TSC rate.
760 * This isn't currently relevant.
761 */
762static bool tmR3HasFixedTSC(PVM pVM)
763{
764 if (ASMHasCpuId())
765 {
766 uint32_t uEAX, uEBX, uECX, uEDX;
767
768 if (CPUMGetHostCpuVendor(pVM) == CPUMCPUVENDOR_AMD)
769 {
770 /*
771 * AuthenticAMD - Check for APM support and that TscInvariant is set.
772 *
773 * This test isn't correct with respect to fixed/non-fixed TSC and
774 * older models, but this isn't relevant since the result is currently
775 * only used for making a decision on AMD-V models.
776 */
777 ASMCpuId(0x80000000, &uEAX, &uEBX, &uECX, &uEDX);
778 if (uEAX >= 0x80000007)
779 {
780 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
781
782 ASMCpuId(0x80000007, &uEAX, &uEBX, &uECX, &uEDX);
783 if ( (uEDX & X86_CPUID_AMD_ADVPOWER_EDX_TSCINVAR) /* TscInvariant */
784 && pGip->u32Mode == SUPGIPMODE_SYNC_TSC /* no fixed tsc if the gip timer is in async mode */)
785 return true;
786 }
787 }
788 else if (CPUMGetHostCpuVendor(pVM) == CPUMCPUVENDOR_INTEL)
789 {
790 /*
791 * GenuineIntel - Check the model number.
792 *
793 * This test is lacking in the same way and for the same reasons
794 * as the AMD test above.
795 */
796 ASMCpuId(1, &uEAX, &uEBX, &uECX, &uEDX);
797 unsigned uModel = (uEAX >> 4) & 0x0f;
798 unsigned uFamily = (uEAX >> 8) & 0x0f;
799 if (uFamily == 0x0f)
800 uFamily += (uEAX >> 20) & 0xff;
801 if (uFamily >= 0x06)
802 uModel += ((uEAX >> 16) & 0x0f) << 4;
803 if ( (uFamily == 0x0f /*P4*/ && uModel >= 0x03)
804 || (uFamily == 0x06 /*P2/P3*/ && uModel >= 0x0e))
805 return true;
806 }
807 }
808 return false;
809}
810
811
812/**
813 * Calibrate the CPU tick.
814 *
815 * @returns Number of ticks per second.
816 */
817static uint64_t tmR3CalibrateTSC(PVM pVM)
818{
819 /*
820 * Use GIP when available present.
821 */
822 uint64_t u64Hz = SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
823 if (u64Hz != UINT64_MAX)
824 {
825 if (tmR3HasFixedTSC(pVM))
826 /* Sleep a bit to get a more reliable CpuHz value. */
827 RTThreadSleep(32);
828 else
829 {
830 /* Spin for 40ms to try push up the CPU frequency and get a more reliable CpuHz value. */
831 const uint64_t u64 = RTTimeMilliTS();
832 while ((RTTimeMilliTS() - u64) < 40 /*ms*/)
833 /* nothing */;
834 }
835
836 u64Hz = SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
837 if (u64Hz != UINT64_MAX)
838 return u64Hz;
839 }
840
841 /* call this once first to make sure it's initialized. */
842 RTTimeNanoTS();
843
844 /*
845 * Yield the CPU to increase our chances of getting
846 * a correct value.
847 */
848 RTThreadYield(); /* Try avoid interruptions between TSC and NanoTS samplings. */
849 static const unsigned s_auSleep[5] = { 50, 30, 30, 40, 40 };
850 uint64_t au64Samples[5];
851 unsigned i;
852 for (i = 0; i < RT_ELEMENTS(au64Samples); i++)
853 {
854 RTMSINTERVAL cMillies;
855 int cTries = 5;
856 uint64_t u64Start = ASMReadTSC();
857 uint64_t u64End;
858 uint64_t StartTS = RTTimeNanoTS();
859 uint64_t EndTS;
860 do
861 {
862 RTThreadSleep(s_auSleep[i]);
863 u64End = ASMReadTSC();
864 EndTS = RTTimeNanoTS();
865 cMillies = (RTMSINTERVAL)((EndTS - StartTS + 500000) / 1000000);
866 } while ( cMillies == 0 /* the sleep may be interrupted... */
867 || (cMillies < 20 && --cTries > 0));
868 uint64_t u64Diff = u64End - u64Start;
869
870 au64Samples[i] = (u64Diff * 1000) / cMillies;
871 AssertMsg(cTries > 0, ("cMillies=%d i=%d\n", cMillies, i));
872 }
873
874 /*
875 * Discard the highest and lowest results and calculate the average.
876 */
877 unsigned iHigh = 0;
878 unsigned iLow = 0;
879 for (i = 1; i < RT_ELEMENTS(au64Samples); i++)
880 {
881 if (au64Samples[i] < au64Samples[iLow])
882 iLow = i;
883 if (au64Samples[i] > au64Samples[iHigh])
884 iHigh = i;
885 }
886 au64Samples[iLow] = 0;
887 au64Samples[iHigh] = 0;
888
889 u64Hz = au64Samples[0];
890 for (i = 1; i < RT_ELEMENTS(au64Samples); i++)
891 u64Hz += au64Samples[i];
892 u64Hz /= RT_ELEMENTS(au64Samples) - 2;
893
894 return u64Hz;
895}
896
897
898/**
899 * Finalizes the TM initialization.
900 *
901 * @returns VBox status code.
902 * @param pVM The VM to operate on.
903 */
904VMM_INT_DECL(int) TMR3InitFinalize(PVM pVM)
905{
906 int rc;
907
908 /*
909 * Resolve symbols.
910 */
911 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "tmVirtualNanoTSBad", &pVM->tm.s.VirtualGetRawDataRC.pfnBad);
912 AssertRCReturn(rc, rc);
913 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "tmVirtualNanoTSRediscover", &pVM->tm.s.VirtualGetRawDataRC.pfnRediscover);
914 AssertRCReturn(rc, rc);
915 if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceSync)
916 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "RTTimeNanoTSLFenceSync", &pVM->tm.s.pfnVirtualGetRawRC);
917 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceAsync)
918 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "RTTimeNanoTSLFenceAsync", &pVM->tm.s.pfnVirtualGetRawRC);
919 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacySync)
920 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "RTTimeNanoTSLegacySync", &pVM->tm.s.pfnVirtualGetRawRC);
921 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacyAsync)
922 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "RTTimeNanoTSLegacyAsync", &pVM->tm.s.pfnVirtualGetRawRC);
923 else
924 AssertFatalFailed();
925 AssertRCReturn(rc, rc);
926
927 rc = PDMR3LdrGetSymbolR0(pVM, NULL, "tmVirtualNanoTSBad", &pVM->tm.s.VirtualGetRawDataR0.pfnBad);
928 AssertRCReturn(rc, rc);
929 rc = PDMR3LdrGetSymbolR0(pVM, NULL, "tmVirtualNanoTSRediscover", &pVM->tm.s.VirtualGetRawDataR0.pfnRediscover);
930 AssertRCReturn(rc, rc);
931 if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceSync)
932 rc = PDMR3LdrGetSymbolR0(pVM, NULL, "RTTimeNanoTSLFenceSync", &pVM->tm.s.pfnVirtualGetRawR0);
933 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceAsync)
934 rc = PDMR3LdrGetSymbolR0(pVM, NULL, "RTTimeNanoTSLFenceAsync", &pVM->tm.s.pfnVirtualGetRawR0);
935 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacySync)
936 rc = PDMR3LdrGetSymbolR0(pVM, NULL, "RTTimeNanoTSLegacySync", &pVM->tm.s.pfnVirtualGetRawR0);
937 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacyAsync)
938 rc = PDMR3LdrGetSymbolR0(pVM, NULL, "RTTimeNanoTSLegacyAsync", &pVM->tm.s.pfnVirtualGetRawR0);
939 else
940 AssertFatalFailed();
941 AssertRCReturn(rc, rc);
942
943#ifndef VBOX_WITHOUT_NS_ACCOUNTING
944 /*
945 * Create a timer for refreshing the CPU load stats.
946 */
947 PTMTIMER pTimer;
948 rc = TMR3TimerCreateInternal(pVM, TMCLOCK_REAL, tmR3CpuLoadTimer, NULL, "CPU Load Timer", &pTimer);
949 if (RT_SUCCESS(rc))
950 rc = TMTimerSetMillies(pTimer, 1000);
951#endif
952
953 return rc;
954}
955
956
957/**
958 * Applies relocations to data and code managed by this
959 * component. This function will be called at init and
960 * whenever the VMM need to relocate it self inside the GC.
961 *
962 * @param pVM The VM.
963 * @param offDelta Relocation delta relative to old location.
964 */
965VMM_INT_DECL(void) TMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
966{
967 int rc;
968 LogFlow(("TMR3Relocate\n"));
969
970 pVM->tm.s.pvGIPRC = MMHyperR3ToRC(pVM, pVM->tm.s.pvGIPR3);
971 pVM->tm.s.paTimerQueuesRC = MMHyperR3ToRC(pVM, pVM->tm.s.paTimerQueuesR3);
972 pVM->tm.s.paTimerQueuesR0 = MMHyperR3ToR0(pVM, pVM->tm.s.paTimerQueuesR3);
973
974 pVM->tm.s.VirtualGetRawDataRC.pu64Prev = MMHyperR3ToRC(pVM, (void *)&pVM->tm.s.u64VirtualRawPrev);
975 AssertFatal(pVM->tm.s.VirtualGetRawDataRC.pu64Prev);
976 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "tmVirtualNanoTSBad", &pVM->tm.s.VirtualGetRawDataRC.pfnBad);
977 AssertFatalRC(rc);
978 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "tmVirtualNanoTSRediscover", &pVM->tm.s.VirtualGetRawDataRC.pfnRediscover);
979 AssertFatalRC(rc);
980
981 if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceSync)
982 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "RTTimeNanoTSLFenceSync", &pVM->tm.s.pfnVirtualGetRawRC);
983 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLFenceAsync)
984 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "RTTimeNanoTSLFenceAsync", &pVM->tm.s.pfnVirtualGetRawRC);
985 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacySync)
986 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "RTTimeNanoTSLegacySync", &pVM->tm.s.pfnVirtualGetRawRC);
987 else if (pVM->tm.s.pfnVirtualGetRawR3 == RTTimeNanoTSLegacyAsync)
988 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "RTTimeNanoTSLegacyAsync", &pVM->tm.s.pfnVirtualGetRawRC);
989 else
990 AssertFatalFailed();
991 AssertFatalRC(rc);
992
993 /*
994 * Iterate the timers updating the pVMRC pointers.
995 */
996 for (PTMTIMER pTimer = pVM->tm.s.pCreated; pTimer; pTimer = pTimer->pBigNext)
997 {
998 pTimer->pVMRC = pVM->pVMRC;
999 pTimer->pVMR0 = pVM->pVMR0;
1000 }
1001}
1002
1003
1004/**
1005 * Terminates the TM.
1006 *
1007 * Termination means cleaning up and freeing all resources,
1008 * the VM it self is at this point powered off or suspended.
1009 *
1010 * @returns VBox status code.
1011 * @param pVM The VM to operate on.
1012 */
1013VMM_INT_DECL(int) TMR3Term(PVM pVM)
1014{
1015 AssertMsg(pVM->tm.s.offVM, ("bad init order!\n"));
1016 if (pVM->tm.s.pTimer)
1017 {
1018 int rc = RTTimerDestroy(pVM->tm.s.pTimer);
1019 AssertRC(rc);
1020 pVM->tm.s.pTimer = NULL;
1021 }
1022
1023 return VINF_SUCCESS;
1024}
1025
1026
1027/**
1028 * The VM is being reset.
1029 *
1030 * For the TM component this means that a rescheduling is preformed,
1031 * the FF is cleared and but without running the queues. We'll have to
1032 * check if this makes sense or not, but it seems like a good idea now....
1033 *
1034 * @param pVM VM handle.
1035 */
1036VMM_INT_DECL(void) TMR3Reset(PVM pVM)
1037{
1038 LogFlow(("TMR3Reset:\n"));
1039 VM_ASSERT_EMT(pVM);
1040 TM_LOCK_TIMERS(pVM);
1041
1042 /*
1043 * Abort any pending catch up.
1044 * This isn't perfect...
1045 */
1046 if (pVM->tm.s.fVirtualSyncCatchUp)
1047 {
1048 const uint64_t offVirtualNow = TMVirtualGetNoCheck(pVM);
1049 const uint64_t offVirtualSyncNow = TMVirtualSyncGetNoCheck(pVM);
1050 if (pVM->tm.s.fVirtualSyncCatchUp)
1051 {
1052 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
1053
1054 const uint64_t offOld = pVM->tm.s.offVirtualSyncGivenUp;
1055 const uint64_t offNew = offVirtualNow - offVirtualSyncNow;
1056 Assert(offOld <= offNew);
1057 ASMAtomicWriteU64((uint64_t volatile *)&pVM->tm.s.offVirtualSyncGivenUp, offNew);
1058 ASMAtomicWriteU64((uint64_t volatile *)&pVM->tm.s.offVirtualSync, offNew);
1059 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
1060 LogRel(("TM: Aborting catch-up attempt on reset with a %'RU64 ns lag on reset; new total: %'RU64 ns\n", offNew - offOld, offNew));
1061 }
1062 }
1063
1064 /*
1065 * Process the queues.
1066 */
1067 for (int i = 0; i < TMCLOCK_MAX; i++)
1068 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[i]);
1069#ifdef VBOX_STRICT
1070 tmTimerQueuesSanityChecks(pVM, "TMR3Reset");
1071#endif
1072
1073 PVMCPU pVCpuDst = &pVM->aCpus[pVM->tm.s.idTimerCpu];
1074 VMCPU_FF_CLEAR(pVCpuDst, VMCPU_FF_TIMER); /** @todo FIXME: this isn't right. */
1075 TM_UNLOCK_TIMERS(pVM);
1076}
1077
1078
1079/**
1080 * Resolve a builtin RC symbol.
1081 * Called by PDM when loading or relocating GC modules.
1082 *
1083 * @returns VBox status
1084 * @param pVM VM Handle.
1085 * @param pszSymbol Symbol to resolve.
1086 * @param pRCPtrValue Where to store the symbol value.
1087 * @remark This has to work before TMR3Relocate() is called.
1088 */
1089VMM_INT_DECL(int) TMR3GetImportRC(PVM pVM, const char *pszSymbol, PRTRCPTR pRCPtrValue)
1090{
1091 if (!strcmp(pszSymbol, "g_pSUPGlobalInfoPage"))
1092 *pRCPtrValue = MMHyperR3ToRC(pVM, &pVM->tm.s.pvGIPRC);
1093 //else if (..)
1094 else
1095 return VERR_SYMBOL_NOT_FOUND;
1096 return VINF_SUCCESS;
1097}
1098
1099
1100/**
1101 * Execute state save operation.
1102 *
1103 * @returns VBox status code.
1104 * @param pVM VM Handle.
1105 * @param pSSM SSM operation handle.
1106 */
1107static DECLCALLBACK(int) tmR3Save(PVM pVM, PSSMHANDLE pSSM)
1108{
1109 LogFlow(("tmR3Save:\n"));
1110#ifdef VBOX_STRICT
1111 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1112 {
1113 PVMCPU pVCpu = &pVM->aCpus[i];
1114 Assert(!pVCpu->tm.s.fTSCTicking);
1115 }
1116 Assert(!pVM->tm.s.cVirtualTicking);
1117 Assert(!pVM->tm.s.fVirtualSyncTicking);
1118#endif
1119
1120 /*
1121 * Save the virtual clocks.
1122 */
1123 /* the virtual clock. */
1124 SSMR3PutU64(pSSM, TMCLOCK_FREQ_VIRTUAL);
1125 SSMR3PutU64(pSSM, pVM->tm.s.u64Virtual);
1126
1127 /* the virtual timer synchronous clock. */
1128 SSMR3PutU64(pSSM, pVM->tm.s.u64VirtualSync);
1129 SSMR3PutU64(pSSM, pVM->tm.s.offVirtualSync);
1130 SSMR3PutU64(pSSM, pVM->tm.s.offVirtualSyncGivenUp);
1131 SSMR3PutU64(pSSM, pVM->tm.s.u64VirtualSyncCatchUpPrev);
1132 SSMR3PutBool(pSSM, pVM->tm.s.fVirtualSyncCatchUp);
1133
1134 /* real time clock */
1135 SSMR3PutU64(pSSM, TMCLOCK_FREQ_REAL);
1136
1137 /* the cpu tick clock. */
1138 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1139 {
1140 PVMCPU pVCpu = &pVM->aCpus[i];
1141 SSMR3PutU64(pSSM, TMCpuTickGet(pVCpu));
1142 }
1143 return SSMR3PutU64(pSSM, pVM->tm.s.cTSCTicksPerSecond);
1144}
1145
1146
1147/**
1148 * Execute state load operation.
1149 *
1150 * @returns VBox status code.
1151 * @param pVM VM Handle.
1152 * @param pSSM SSM operation handle.
1153 * @param uVersion Data layout version.
1154 * @param uPass The data pass.
1155 */
1156static DECLCALLBACK(int) tmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1157{
1158 LogFlow(("tmR3Load:\n"));
1159
1160 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
1161#ifdef VBOX_STRICT
1162 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1163 {
1164 PVMCPU pVCpu = &pVM->aCpus[i];
1165 Assert(!pVCpu->tm.s.fTSCTicking);
1166 }
1167 Assert(!pVM->tm.s.cVirtualTicking);
1168 Assert(!pVM->tm.s.fVirtualSyncTicking);
1169#endif
1170
1171 /*
1172 * Validate version.
1173 */
1174 if (uVersion != TM_SAVED_STATE_VERSION)
1175 {
1176 AssertMsgFailed(("tmR3Load: Invalid version uVersion=%d!\n", uVersion));
1177 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1178 }
1179
1180 /*
1181 * Load the virtual clock.
1182 */
1183 pVM->tm.s.cVirtualTicking = 0;
1184 /* the virtual clock. */
1185 uint64_t u64Hz;
1186 int rc = SSMR3GetU64(pSSM, &u64Hz);
1187 if (RT_FAILURE(rc))
1188 return rc;
1189 if (u64Hz != TMCLOCK_FREQ_VIRTUAL)
1190 {
1191 AssertMsgFailed(("The virtual clock frequency differs! Saved: %'RU64 Binary: %'RU64\n",
1192 u64Hz, TMCLOCK_FREQ_VIRTUAL));
1193 return VERR_SSM_VIRTUAL_CLOCK_HZ;
1194 }
1195 SSMR3GetU64(pSSM, &pVM->tm.s.u64Virtual);
1196 pVM->tm.s.u64VirtualOffset = 0;
1197
1198 /* the virtual timer synchronous clock. */
1199 pVM->tm.s.fVirtualSyncTicking = false;
1200 uint64_t u64;
1201 SSMR3GetU64(pSSM, &u64);
1202 pVM->tm.s.u64VirtualSync = u64;
1203 SSMR3GetU64(pSSM, &u64);
1204 pVM->tm.s.offVirtualSync = u64;
1205 SSMR3GetU64(pSSM, &u64);
1206 pVM->tm.s.offVirtualSyncGivenUp = u64;
1207 SSMR3GetU64(pSSM, &u64);
1208 pVM->tm.s.u64VirtualSyncCatchUpPrev = u64;
1209 bool f;
1210 SSMR3GetBool(pSSM, &f);
1211 pVM->tm.s.fVirtualSyncCatchUp = f;
1212
1213 /* the real clock */
1214 rc = SSMR3GetU64(pSSM, &u64Hz);
1215 if (RT_FAILURE(rc))
1216 return rc;
1217 if (u64Hz != TMCLOCK_FREQ_REAL)
1218 {
1219 AssertMsgFailed(("The real clock frequency differs! Saved: %'RU64 Binary: %'RU64\n",
1220 u64Hz, TMCLOCK_FREQ_REAL));
1221 return VERR_SSM_VIRTUAL_CLOCK_HZ; /* misleading... */
1222 }
1223
1224 /* the cpu tick clock. */
1225 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1226 {
1227 PVMCPU pVCpu = &pVM->aCpus[i];
1228
1229 pVCpu->tm.s.fTSCTicking = false;
1230 SSMR3GetU64(pSSM, &pVCpu->tm.s.u64TSC);
1231
1232 if (pVM->tm.s.fTSCUseRealTSC)
1233 pVCpu->tm.s.offTSCRawSrc = 0; /** @todo TSC restore stuff and HWACC. */
1234 }
1235
1236 rc = SSMR3GetU64(pSSM, &u64Hz);
1237 if (RT_FAILURE(rc))
1238 return rc;
1239 if (!pVM->tm.s.fTSCUseRealTSC)
1240 pVM->tm.s.cTSCTicksPerSecond = u64Hz;
1241
1242 LogRel(("TM: cTSCTicksPerSecond=%#RX64 (%'RU64) fTSCVirtualized=%RTbool fTSCUseRealTSC=%RTbool (state load)\n",
1243 pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.cTSCTicksPerSecond, pVM->tm.s.fTSCVirtualized, pVM->tm.s.fTSCUseRealTSC));
1244
1245 /*
1246 * Make sure timers get rescheduled immediately.
1247 */
1248 PVMCPU pVCpuDst = &pVM->aCpus[pVM->tm.s.idTimerCpu];
1249 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
1250
1251 return VINF_SUCCESS;
1252}
1253
1254
1255/**
1256 * Internal TMR3TimerCreate worker.
1257 *
1258 * @returns VBox status code.
1259 * @param pVM The VM handle.
1260 * @param enmClock The timer clock.
1261 * @param pszDesc The timer description.
1262 * @param ppTimer Where to store the timer pointer on success.
1263 */
1264static int tmr3TimerCreate(PVM pVM, TMCLOCK enmClock, const char *pszDesc, PPTMTIMERR3 ppTimer)
1265{
1266 VM_ASSERT_EMT(pVM);
1267
1268 /*
1269 * Allocate the timer.
1270 */
1271 PTMTIMERR3 pTimer = NULL;
1272 if (pVM->tm.s.pFree && VM_IS_EMT(pVM))
1273 {
1274 pTimer = pVM->tm.s.pFree;
1275 pVM->tm.s.pFree = pTimer->pBigNext;
1276 Log3(("TM: Recycling timer %p, new free head %p.\n", pTimer, pTimer->pBigNext));
1277 }
1278
1279 if (!pTimer)
1280 {
1281 int rc = MMHyperAlloc(pVM, sizeof(*pTimer), 0, MM_TAG_TM, (void **)&pTimer);
1282 if (RT_FAILURE(rc))
1283 return rc;
1284 Log3(("TM: Allocated new timer %p\n", pTimer));
1285 }
1286
1287 /*
1288 * Initialize it.
1289 */
1290 pTimer->u64Expire = 0;
1291 pTimer->enmClock = enmClock;
1292 pTimer->pVMR3 = pVM;
1293 pTimer->pVMR0 = pVM->pVMR0;
1294 pTimer->pVMRC = pVM->pVMRC;
1295 pTimer->enmState = TMTIMERSTATE_STOPPED;
1296 pTimer->offScheduleNext = 0;
1297 pTimer->offNext = 0;
1298 pTimer->offPrev = 0;
1299 pTimer->pvUser = NULL;
1300 pTimer->pCritSect = NULL;
1301 pTimer->pszDesc = pszDesc;
1302
1303 /* insert into the list of created timers. */
1304 TM_LOCK_TIMERS(pVM);
1305 pTimer->pBigPrev = NULL;
1306 pTimer->pBigNext = pVM->tm.s.pCreated;
1307 pVM->tm.s.pCreated = pTimer;
1308 if (pTimer->pBigNext)
1309 pTimer->pBigNext->pBigPrev = pTimer;
1310#ifdef VBOX_STRICT
1311 tmTimerQueuesSanityChecks(pVM, "tmR3TimerCreate");
1312#endif
1313 TM_UNLOCK_TIMERS(pVM);
1314
1315 *ppTimer = pTimer;
1316 return VINF_SUCCESS;
1317}
1318
1319
1320/**
1321 * Creates a device timer.
1322 *
1323 * @returns VBox status.
1324 * @param pVM The VM to create the timer in.
1325 * @param pDevIns Device instance.
1326 * @param enmClock The clock to use on this timer.
1327 * @param pfnCallback Callback function.
1328 * @param pvUser The user argument to the callback.
1329 * @param fFlags Timer creation flags, see grp_tm_timer_flags.
1330 * @param pszDesc Pointer to description string which must stay around
1331 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
1332 * @param ppTimer Where to store the timer on success.
1333 */
1334VMM_INT_DECL(int) TMR3TimerCreateDevice(PVM pVM, PPDMDEVINS pDevIns, TMCLOCK enmClock,
1335 PFNTMTIMERDEV pfnCallback, void *pvUser,
1336 uint32_t fFlags, const char *pszDesc, PPTMTIMERR3 ppTimer)
1337{
1338 AssertReturn(!(fFlags & ~(TMTIMER_FLAGS_NO_CRIT_SECT)), VERR_INVALID_PARAMETER);
1339
1340 /*
1341 * Allocate and init stuff.
1342 */
1343 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, ppTimer);
1344 if (RT_SUCCESS(rc))
1345 {
1346 (*ppTimer)->enmType = TMTIMERTYPE_DEV;
1347 (*ppTimer)->u.Dev.pfnTimer = pfnCallback;
1348 (*ppTimer)->u.Dev.pDevIns = pDevIns;
1349 (*ppTimer)->pvUser = pvUser;
1350 if (!(fFlags & TMTIMER_FLAGS_NO_CRIT_SECT))
1351 (*ppTimer)->pCritSect = PDMR3DevGetCritSect(pVM, pDevIns);
1352 Log(("TM: Created device timer %p clock %d callback %p '%s'\n", (*ppTimer), enmClock, pfnCallback, pszDesc));
1353 }
1354
1355 return rc;
1356}
1357
1358
1359
1360
1361/**
1362 * Creates a USB device timer.
1363 *
1364 * @returns VBox status.
1365 * @param pVM The VM to create the timer in.
1366 * @param pUsbIns The USB device instance.
1367 * @param enmClock The clock to use on this timer.
1368 * @param pfnCallback Callback function.
1369 * @param pvUser The user argument to the callback.
1370 * @param fFlags Timer creation flags, see grp_tm_timer_flags.
1371 * @param pszDesc Pointer to description string which must stay around
1372 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
1373 * @param ppTimer Where to store the timer on success.
1374 */
1375VMM_INT_DECL(int) TMR3TimerCreateUsb(PVM pVM, PPDMUSBINS pUsbIns, TMCLOCK enmClock,
1376 PFNTMTIMERUSB pfnCallback, void *pvUser,
1377 uint32_t fFlags, const char *pszDesc, PPTMTIMERR3 ppTimer)
1378{
1379 AssertReturn(!(fFlags & ~(TMTIMER_FLAGS_NO_CRIT_SECT)), VERR_INVALID_PARAMETER);
1380
1381 /*
1382 * Allocate and init stuff.
1383 */
1384 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, ppTimer);
1385 if (RT_SUCCESS(rc))
1386 {
1387 (*ppTimer)->enmType = TMTIMERTYPE_USB;
1388 (*ppTimer)->u.Usb.pfnTimer = pfnCallback;
1389 (*ppTimer)->u.Usb.pUsbIns = pUsbIns;
1390 (*ppTimer)->pvUser = pvUser;
1391 //if (!(fFlags & TMTIMER_FLAGS_NO_CRIT_SECT))
1392 //{
1393 // if (pDevIns->pCritSectR3)
1394 // (*ppTimer)->pCritSect = pUsbIns->pCritSectR3;
1395 // else
1396 // (*ppTimer)->pCritSect = IOMR3GetCritSect(pVM);
1397 //}
1398 Log(("TM: Created USB device timer %p clock %d callback %p '%s'\n", (*ppTimer), enmClock, pfnCallback, pszDesc));
1399 }
1400
1401 return rc;
1402}
1403
1404
1405/**
1406 * Creates a driver timer.
1407 *
1408 * @returns VBox status.
1409 * @param pVM The VM to create the timer in.
1410 * @param pDrvIns Driver instance.
1411 * @param enmClock The clock to use on this timer.
1412 * @param pfnCallback Callback function.
1413 * @param pvUser The user argument to the callback.
1414 * @param fFlags Timer creation flags, see grp_tm_timer_flags.
1415 * @param pszDesc Pointer to description string which must stay around
1416 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
1417 * @param ppTimer Where to store the timer on success.
1418 */
1419VMM_INT_DECL(int) TMR3TimerCreateDriver(PVM pVM, PPDMDRVINS pDrvIns, TMCLOCK enmClock, PFNTMTIMERDRV pfnCallback, void *pvUser,
1420 uint32_t fFlags, const char *pszDesc, PPTMTIMERR3 ppTimer)
1421{
1422 AssertReturn(!(fFlags & ~(TMTIMER_FLAGS_NO_CRIT_SECT)), VERR_INVALID_PARAMETER);
1423
1424 /*
1425 * Allocate and init stuff.
1426 */
1427 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, ppTimer);
1428 if (RT_SUCCESS(rc))
1429 {
1430 (*ppTimer)->enmType = TMTIMERTYPE_DRV;
1431 (*ppTimer)->u.Drv.pfnTimer = pfnCallback;
1432 (*ppTimer)->u.Drv.pDrvIns = pDrvIns;
1433 (*ppTimer)->pvUser = pvUser;
1434 Log(("TM: Created device timer %p clock %d callback %p '%s'\n", (*ppTimer), enmClock, pfnCallback, pszDesc));
1435 }
1436
1437 return rc;
1438}
1439
1440
1441/**
1442 * Creates an internal timer.
1443 *
1444 * @returns VBox status.
1445 * @param pVM The VM to create the timer in.
1446 * @param enmClock The clock to use on this timer.
1447 * @param pfnCallback Callback function.
1448 * @param pvUser User argument to be passed to the callback.
1449 * @param pszDesc Pointer to description string which must stay around
1450 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
1451 * @param ppTimer Where to store the timer on success.
1452 */
1453VMMR3DECL(int) TMR3TimerCreateInternal(PVM pVM, TMCLOCK enmClock, PFNTMTIMERINT pfnCallback, void *pvUser, const char *pszDesc, PPTMTIMERR3 ppTimer)
1454{
1455 /*
1456 * Allocate and init stuff.
1457 */
1458 PTMTIMER pTimer;
1459 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, &pTimer);
1460 if (RT_SUCCESS(rc))
1461 {
1462 pTimer->enmType = TMTIMERTYPE_INTERNAL;
1463 pTimer->u.Internal.pfnTimer = pfnCallback;
1464 pTimer->pvUser = pvUser;
1465 *ppTimer = pTimer;
1466 Log(("TM: Created internal timer %p clock %d callback %p '%s'\n", pTimer, enmClock, pfnCallback, pszDesc));
1467 }
1468
1469 return rc;
1470}
1471
1472/**
1473 * Creates an external timer.
1474 *
1475 * @returns Timer handle on success.
1476 * @returns NULL on failure.
1477 * @param pVM The VM to create the timer in.
1478 * @param enmClock The clock to use on this timer.
1479 * @param pfnCallback Callback function.
1480 * @param pvUser User argument.
1481 * @param pszDesc Pointer to description string which must stay around
1482 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
1483 */
1484VMMR3DECL(PTMTIMERR3) TMR3TimerCreateExternal(PVM pVM, TMCLOCK enmClock, PFNTMTIMEREXT pfnCallback, void *pvUser, const char *pszDesc)
1485{
1486 /*
1487 * Allocate and init stuff.
1488 */
1489 PTMTIMERR3 pTimer;
1490 int rc = tmr3TimerCreate(pVM, enmClock, pszDesc, &pTimer);
1491 if (RT_SUCCESS(rc))
1492 {
1493 pTimer->enmType = TMTIMERTYPE_EXTERNAL;
1494 pTimer->u.External.pfnTimer = pfnCallback;
1495 pTimer->pvUser = pvUser;
1496 Log(("TM: Created external timer %p clock %d callback %p '%s'\n", pTimer, enmClock, pfnCallback, pszDesc));
1497 return pTimer;
1498 }
1499
1500 return NULL;
1501}
1502
1503
1504/**
1505 * Destroy a timer
1506 *
1507 * @returns VBox status.
1508 * @param pTimer Timer handle as returned by one of the create functions.
1509 */
1510VMMR3DECL(int) TMR3TimerDestroy(PTMTIMER pTimer)
1511{
1512 /*
1513 * Be extra careful here.
1514 */
1515 if (!pTimer)
1516 return VINF_SUCCESS;
1517 AssertPtr(pTimer);
1518 Assert((unsigned)pTimer->enmClock < (unsigned)TMCLOCK_MAX);
1519
1520 PVM pVM = pTimer->CTX_SUFF(pVM);
1521 PTMTIMERQUEUE pQueue = &pVM->tm.s.CTX_SUFF(paTimerQueues)[pTimer->enmClock];
1522 bool fActive = false;
1523 bool fPending = false;
1524
1525 AssertMsg( !pTimer->pCritSect
1526 || VMR3GetState(pVM) != VMSTATE_RUNNING
1527 || PDMCritSectIsOwner(pTimer->pCritSect), ("%s\n", pTimer->pszDesc));
1528
1529 /*
1530 * The rest of the game happens behind the lock, just
1531 * like create does. All the work is done here.
1532 */
1533 TM_LOCK_TIMERS(pVM);
1534 for (int cRetries = 1000;; cRetries--)
1535 {
1536 /*
1537 * Change to the DESTROY state.
1538 */
1539 TMTIMERSTATE enmState = pTimer->enmState;
1540 TMTIMERSTATE enmNewState = enmState;
1541 Log2(("TMTimerDestroy: %p:{.enmState=%s, .pszDesc='%s'} cRetries=%d\n",
1542 pTimer, tmTimerState(enmState), R3STRING(pTimer->pszDesc), cRetries));
1543 switch (enmState)
1544 {
1545 case TMTIMERSTATE_STOPPED:
1546 case TMTIMERSTATE_EXPIRED_DELIVER:
1547 break;
1548
1549 case TMTIMERSTATE_ACTIVE:
1550 fActive = true;
1551 break;
1552
1553 case TMTIMERSTATE_PENDING_STOP:
1554 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
1555 case TMTIMERSTATE_PENDING_RESCHEDULE:
1556 fActive = true;
1557 fPending = true;
1558 break;
1559
1560 case TMTIMERSTATE_PENDING_SCHEDULE:
1561 fPending = true;
1562 break;
1563
1564 /*
1565 * This shouldn't happen as the caller should make sure there are no races.
1566 */
1567 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
1568 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
1569 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
1570 AssertMsgFailed(("%p:.enmState=%s %s\n", pTimer, tmTimerState(enmState), pTimer->pszDesc));
1571 TM_UNLOCK_TIMERS(pVM);
1572 if (!RTThreadYield())
1573 RTThreadSleep(1);
1574 AssertMsgReturn(cRetries > 0, ("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, pTimer->pszDesc),
1575 VERR_TM_UNSTABLE_STATE);
1576 TM_LOCK_TIMERS(pVM);
1577 continue;
1578
1579 /*
1580 * Invalid states.
1581 */
1582 case TMTIMERSTATE_FREE:
1583 case TMTIMERSTATE_DESTROY:
1584 TM_UNLOCK_TIMERS(pVM);
1585 AssertLogRelMsgFailedReturn(("pTimer=%p %s\n", pTimer, tmTimerState(enmState)), VERR_TM_INVALID_STATE);
1586
1587 default:
1588 AssertMsgFailed(("Unknown timer state %d (%s)\n", enmState, R3STRING(pTimer->pszDesc)));
1589 TM_UNLOCK_TIMERS(pVM);
1590 return VERR_TM_UNKNOWN_STATE;
1591 }
1592
1593 /*
1594 * Try switch to the destroy state.
1595 * This should always succeed as the caller should make sure there are no race.
1596 */
1597 bool fRc;
1598 TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_DESTROY, enmState, fRc);
1599 if (fRc)
1600 break;
1601 AssertMsgFailed(("%p:.enmState=%s %s\n", pTimer, tmTimerState(enmState), pTimer->pszDesc));
1602 TM_UNLOCK_TIMERS(pVM);
1603 AssertMsgReturn(cRetries > 0, ("Failed waiting for stable state. state=%d (%s)\n", pTimer->enmState, pTimer->pszDesc),
1604 VERR_TM_UNSTABLE_STATE);
1605 TM_LOCK_TIMERS(pVM);
1606 }
1607
1608 /*
1609 * Unlink from the active list.
1610 */
1611 if (fActive)
1612 {
1613 const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
1614 const PTMTIMER pNext = TMTIMER_GET_NEXT(pTimer);
1615 if (pPrev)
1616 TMTIMER_SET_NEXT(pPrev, pNext);
1617 else
1618 {
1619 TMTIMER_SET_HEAD(pQueue, pNext);
1620 pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
1621 }
1622 if (pNext)
1623 TMTIMER_SET_PREV(pNext, pPrev);
1624 pTimer->offNext = 0;
1625 pTimer->offPrev = 0;
1626 }
1627
1628 /*
1629 * Unlink from the schedule list by running it.
1630 */
1631 if (fPending)
1632 {
1633 Log3(("TMR3TimerDestroy: tmTimerQueueSchedule\n"));
1634 STAM_PROFILE_START(&pVM->tm.s.CTX_SUFF_Z(StatScheduleOne), a);
1635 Assert(pQueue->offSchedule);
1636 tmTimerQueueSchedule(pVM, pQueue);
1637 }
1638
1639 /*
1640 * Read to move the timer from the created list and onto the free list.
1641 */
1642 Assert(!pTimer->offNext); Assert(!pTimer->offPrev); Assert(!pTimer->offScheduleNext);
1643
1644 /* unlink from created list */
1645 if (pTimer->pBigPrev)
1646 pTimer->pBigPrev->pBigNext = pTimer->pBigNext;
1647 else
1648 pVM->tm.s.pCreated = pTimer->pBigNext;
1649 if (pTimer->pBigNext)
1650 pTimer->pBigNext->pBigPrev = pTimer->pBigPrev;
1651 pTimer->pBigNext = 0;
1652 pTimer->pBigPrev = 0;
1653
1654 /* free */
1655 Log2(("TM: Inserting %p into the free list ahead of %p!\n", pTimer, pVM->tm.s.pFree));
1656 TM_SET_STATE(pTimer, TMTIMERSTATE_FREE);
1657 pTimer->pBigNext = pVM->tm.s.pFree;
1658 pVM->tm.s.pFree = pTimer;
1659
1660#ifdef VBOX_STRICT
1661 tmTimerQueuesSanityChecks(pVM, "TMR3TimerDestroy");
1662#endif
1663 TM_UNLOCK_TIMERS(pVM);
1664 return VINF_SUCCESS;
1665}
1666
1667
1668/**
1669 * Destroy all timers owned by a device.
1670 *
1671 * @returns VBox status.
1672 * @param pVM VM handle.
1673 * @param pDevIns Device which timers should be destroyed.
1674 */
1675VMM_INT_DECL(int) TMR3TimerDestroyDevice(PVM pVM, PPDMDEVINS pDevIns)
1676{
1677 LogFlow(("TMR3TimerDestroyDevice: pDevIns=%p\n", pDevIns));
1678 if (!pDevIns)
1679 return VERR_INVALID_PARAMETER;
1680
1681 TM_LOCK_TIMERS(pVM);
1682 PTMTIMER pCur = pVM->tm.s.pCreated;
1683 while (pCur)
1684 {
1685 PTMTIMER pDestroy = pCur;
1686 pCur = pDestroy->pBigNext;
1687 if ( pDestroy->enmType == TMTIMERTYPE_DEV
1688 && pDestroy->u.Dev.pDevIns == pDevIns)
1689 {
1690 int rc = TMR3TimerDestroy(pDestroy);
1691 AssertRC(rc);
1692 }
1693 }
1694 TM_UNLOCK_TIMERS(pVM);
1695
1696 LogFlow(("TMR3TimerDestroyDevice: returns VINF_SUCCESS\n"));
1697 return VINF_SUCCESS;
1698}
1699
1700
1701/**
1702 * Destroy all timers owned by a USB device.
1703 *
1704 * @returns VBox status.
1705 * @param pVM VM handle.
1706 * @param pUsbIns USB device which timers should be destroyed.
1707 */
1708VMM_INT_DECL(int) TMR3TimerDestroyUsb(PVM pVM, PPDMUSBINS pUsbIns)
1709{
1710 LogFlow(("TMR3TimerDestroyUsb: pUsbIns=%p\n", pUsbIns));
1711 if (!pUsbIns)
1712 return VERR_INVALID_PARAMETER;
1713
1714 TM_LOCK_TIMERS(pVM);
1715 PTMTIMER pCur = pVM->tm.s.pCreated;
1716 while (pCur)
1717 {
1718 PTMTIMER pDestroy = pCur;
1719 pCur = pDestroy->pBigNext;
1720 if ( pDestroy->enmType == TMTIMERTYPE_USB
1721 && pDestroy->u.Usb.pUsbIns == pUsbIns)
1722 {
1723 int rc = TMR3TimerDestroy(pDestroy);
1724 AssertRC(rc);
1725 }
1726 }
1727 TM_UNLOCK_TIMERS(pVM);
1728
1729 LogFlow(("TMR3TimerDestroyUsb: returns VINF_SUCCESS\n"));
1730 return VINF_SUCCESS;
1731}
1732
1733
1734/**
1735 * Destroy all timers owned by a driver.
1736 *
1737 * @returns VBox status.
1738 * @param pVM VM handle.
1739 * @param pDrvIns Driver which timers should be destroyed.
1740 */
1741VMM_INT_DECL(int) TMR3TimerDestroyDriver(PVM pVM, PPDMDRVINS pDrvIns)
1742{
1743 LogFlow(("TMR3TimerDestroyDriver: pDrvIns=%p\n", pDrvIns));
1744 if (!pDrvIns)
1745 return VERR_INVALID_PARAMETER;
1746
1747 TM_LOCK_TIMERS(pVM);
1748 PTMTIMER pCur = pVM->tm.s.pCreated;
1749 while (pCur)
1750 {
1751 PTMTIMER pDestroy = pCur;
1752 pCur = pDestroy->pBigNext;
1753 if ( pDestroy->enmType == TMTIMERTYPE_DRV
1754 && pDestroy->u.Drv.pDrvIns == pDrvIns)
1755 {
1756 int rc = TMR3TimerDestroy(pDestroy);
1757 AssertRC(rc);
1758 }
1759 }
1760 TM_UNLOCK_TIMERS(pVM);
1761
1762 LogFlow(("TMR3TimerDestroyDriver: returns VINF_SUCCESS\n"));
1763 return VINF_SUCCESS;
1764}
1765
1766
1767/**
1768 * Internal function for getting the clock time.
1769 *
1770 * @returns clock time.
1771 * @param pVM The VM handle.
1772 * @param enmClock The clock.
1773 */
1774DECLINLINE(uint64_t) tmClock(PVM pVM, TMCLOCK enmClock)
1775{
1776 switch (enmClock)
1777 {
1778 case TMCLOCK_VIRTUAL: return TMVirtualGet(pVM);
1779 case TMCLOCK_VIRTUAL_SYNC: return TMVirtualSyncGet(pVM);
1780 case TMCLOCK_REAL: return TMRealGet(pVM);
1781 case TMCLOCK_TSC: return TMCpuTickGet(&pVM->aCpus[0] /* just take VCPU 0 */);
1782 default:
1783 AssertMsgFailed(("enmClock=%d\n", enmClock));
1784 return ~(uint64_t)0;
1785 }
1786}
1787
1788
1789/**
1790 * Checks if the sync queue has one or more expired timers.
1791 *
1792 * @returns true / false.
1793 *
1794 * @param pVM The VM handle.
1795 * @param enmClock The queue.
1796 */
1797DECLINLINE(bool) tmR3HasExpiredTimer(PVM pVM, TMCLOCK enmClock)
1798{
1799 const uint64_t u64Expire = pVM->tm.s.CTX_SUFF(paTimerQueues)[enmClock].u64Expire;
1800 return u64Expire != INT64_MAX && u64Expire <= tmClock(pVM, enmClock);
1801}
1802
1803
1804/**
1805 * Checks for expired timers in all the queues.
1806 *
1807 * @returns true / false.
1808 * @param pVM The VM handle.
1809 */
1810DECLINLINE(bool) tmR3AnyExpiredTimers(PVM pVM)
1811{
1812 /*
1813 * Combine the time calculation for the first two since we're not on EMT
1814 * TMVirtualSyncGet only permits EMT.
1815 */
1816 uint64_t u64Now = TMVirtualGetNoCheck(pVM);
1817 if (pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL].u64Expire <= u64Now)
1818 return true;
1819 u64Now = pVM->tm.s.fVirtualSyncTicking
1820 ? u64Now - pVM->tm.s.offVirtualSync
1821 : pVM->tm.s.u64VirtualSync;
1822 if (pVM->tm.s.CTX_SUFF(paTimerQueues)[TMCLOCK_VIRTUAL_SYNC].u64Expire <= u64Now)
1823 return true;
1824
1825 /*
1826 * The remaining timers.
1827 */
1828 if (tmR3HasExpiredTimer(pVM, TMCLOCK_REAL))
1829 return true;
1830 if (tmR3HasExpiredTimer(pVM, TMCLOCK_TSC))
1831 return true;
1832 return false;
1833}
1834
1835
1836/**
1837 * Schedule timer callback.
1838 *
1839 * @param pTimer Timer handle.
1840 * @param pvUser VM handle.
1841 * @thread Timer thread.
1842 *
1843 * @remark We cannot do the scheduling and queues running from a timer handler
1844 * since it's not executing in EMT, and even if it was it would be async
1845 * and we wouldn't know the state of the affairs.
1846 * So, we'll just raise the timer FF and force any REM execution to exit.
1847 */
1848static DECLCALLBACK(void) tmR3TimerCallback(PRTTIMER pTimer, void *pvUser, uint64_t /*iTick*/)
1849{
1850 PVM pVM = (PVM)pvUser;
1851 PVMCPU pVCpuDst = &pVM->aCpus[pVM->tm.s.idTimerCpu];
1852
1853 AssertCompile(TMCLOCK_MAX == 4);
1854#ifdef DEBUG_Sander /* very annoying, keep it private. */
1855 if (VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER))
1856 Log(("tmR3TimerCallback: timer event still pending!!\n"));
1857#endif
1858 if ( !VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER)
1859 && ( pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].offSchedule /** @todo FIXME - reconsider offSchedule as a reason for running the timer queues. */
1860 || pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].offSchedule
1861 || pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].offSchedule
1862 || pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].offSchedule
1863 || tmR3AnyExpiredTimers(pVM)
1864 )
1865 && !VMCPU_FF_ISSET(pVCpuDst, VMCPU_FF_TIMER)
1866 && !pVM->tm.s.fRunningQueues
1867 )
1868 {
1869 Log5(("TM(%u): FF: 0 -> 1\n", __LINE__));
1870 VMCPU_FF_SET(pVCpuDst, VMCPU_FF_TIMER);
1871 REMR3NotifyTimerPending(pVM, pVCpuDst);
1872 VMR3NotifyCpuFFU(pVCpuDst->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM /** @todo | VMNOTIFYFF_FLAGS_POKE ?*/);
1873 STAM_COUNTER_INC(&pVM->tm.s.StatTimerCallbackSetFF);
1874 }
1875}
1876
1877
1878/**
1879 * Schedules and runs any pending timers.
1880 *
1881 * This is normally called from a forced action handler in EMT.
1882 *
1883 * @param pVM The VM to run the timers for.
1884 *
1885 * @thread EMT (actually EMT0, but we fend off the others)
1886 */
1887VMMR3DECL(void) TMR3TimerQueuesDo(PVM pVM)
1888{
1889 /*
1890 * Only the dedicated timer EMT should do stuff here.
1891 * (fRunningQueues is only used as an indicator.)
1892 */
1893 Assert(pVM->tm.s.idTimerCpu < pVM->cCpus);
1894 PVMCPU pVCpuDst = &pVM->aCpus[pVM->tm.s.idTimerCpu];
1895 if (VMMGetCpu(pVM) != pVCpuDst)
1896 {
1897 Assert(pVM->cCpus > 1);
1898 return;
1899 }
1900 STAM_PROFILE_START(&pVM->tm.s.StatDoQueues, a);
1901 Log2(("TMR3TimerQueuesDo:\n"));
1902 Assert(!pVM->tm.s.fRunningQueues);
1903 ASMAtomicWriteBool(&pVM->tm.s.fRunningQueues, true);
1904 TM_LOCK_TIMERS(pVM);
1905
1906 /*
1907 * Process the queues.
1908 */
1909 AssertCompile(TMCLOCK_MAX == 4);
1910
1911 /* TMCLOCK_VIRTUAL_SYNC (see also TMR3VirtualSyncFF) */
1912 STAM_PROFILE_ADV_START(&pVM->tm.s.aStatDoQueues[TMCLOCK_VIRTUAL_SYNC], s1);
1913 PDMCritSectEnter(&pVM->tm.s.VirtualSyncLock, VERR_IGNORED);
1914 ASMAtomicWriteBool(&pVM->tm.s.fRunningVirtualSyncQueue, true);
1915 VMCPU_FF_CLEAR(pVCpuDst, VMCPU_FF_TIMER); /* Clear the FF once we started working for real. */
1916
1917 Assert(!pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].offSchedule);
1918 tmR3TimerQueueRunVirtualSync(pVM);
1919 if (pVM->tm.s.fVirtualSyncTicking) /** @todo move into tmR3TimerQueueRunVirtualSync - FIXME */
1920 VM_FF_CLEAR(pVM, VM_FF_TM_VIRTUAL_SYNC);
1921
1922 ASMAtomicWriteBool(&pVM->tm.s.fRunningVirtualSyncQueue, false);
1923 PDMCritSectLeave(&pVM->tm.s.VirtualSyncLock);
1924 STAM_PROFILE_ADV_STOP(&pVM->tm.s.aStatDoQueues[TMCLOCK_VIRTUAL_SYNC], s1);
1925
1926 /* TMCLOCK_VIRTUAL */
1927 STAM_PROFILE_ADV_START(&pVM->tm.s.aStatDoQueues[TMCLOCK_VIRTUAL], s2);
1928 if (pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL].offSchedule)
1929 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL]);
1930 tmR3TimerQueueRun(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL]);
1931 STAM_PROFILE_ADV_STOP(&pVM->tm.s.aStatDoQueues[TMCLOCK_VIRTUAL], s2);
1932
1933 /* TMCLOCK_TSC */
1934 Assert(!pVM->tm.s.paTimerQueuesR3[TMCLOCK_TSC].offActive); /* not used */
1935
1936 /* TMCLOCK_REAL */
1937 STAM_PROFILE_ADV_START(&pVM->tm.s.aStatDoQueues[TMCLOCK_REAL], s3);
1938 if (pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL].offSchedule)
1939 tmTimerQueueSchedule(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL]);
1940 tmR3TimerQueueRun(pVM, &pVM->tm.s.paTimerQueuesR3[TMCLOCK_REAL]);
1941 STAM_PROFILE_ADV_STOP(&pVM->tm.s.aStatDoQueues[TMCLOCK_REAL], s3);
1942
1943#ifdef VBOX_STRICT
1944 /* check that we didn't screw up. */
1945 tmTimerQueuesSanityChecks(pVM, "TMR3TimerQueuesDo");
1946#endif
1947
1948 /* done */
1949 Log2(("TMR3TimerQueuesDo: returns void\n"));
1950 ASMAtomicWriteBool(&pVM->tm.s.fRunningQueues, false);
1951 TM_UNLOCK_TIMERS(pVM);
1952 STAM_PROFILE_STOP(&pVM->tm.s.StatDoQueues, a);
1953}
1954
1955//RT_C_DECLS_BEGIN
1956//int iomLock(PVM pVM);
1957//void iomUnlock(PVM pVM);
1958//RT_C_DECLS_END
1959
1960
1961/**
1962 * Schedules and runs any pending times in the specified queue.
1963 *
1964 * This is normally called from a forced action handler in EMT.
1965 *
1966 * @param pVM The VM to run the timers for.
1967 * @param pQueue The queue to run.
1968 */
1969static void tmR3TimerQueueRun(PVM pVM, PTMTIMERQUEUE pQueue)
1970{
1971 VM_ASSERT_EMT(pVM);
1972
1973 /*
1974 * Run timers.
1975 *
1976 * We check the clock once and run all timers which are ACTIVE
1977 * and have an expire time less or equal to the time we read.
1978 *
1979 * N.B. A generic unlink must be applied since other threads
1980 * are allowed to mess with any active timer at any time.
1981 * However, we only allow EMT to handle EXPIRED_PENDING
1982 * timers, thus enabling the timer handler function to
1983 * arm the timer again.
1984 */
1985 PTMTIMER pNext = TMTIMER_GET_HEAD(pQueue);
1986 if (!pNext)
1987 return;
1988 const uint64_t u64Now = tmClock(pVM, pQueue->enmClock);
1989 while (pNext && pNext->u64Expire <= u64Now)
1990 {
1991 PTMTIMER pTimer = pNext;
1992 pNext = TMTIMER_GET_NEXT(pTimer);
1993 PPDMCRITSECT pCritSect = pTimer->pCritSect;
1994 if (pCritSect)
1995 PDMCritSectEnter(pCritSect, VERR_INTERNAL_ERROR);
1996 Log2(("tmR3TimerQueueRun: %p:{.enmState=%s, .enmClock=%d, .enmType=%d, u64Expire=%llx (now=%llx) .pszDesc=%s}\n",
1997 pTimer, tmTimerState(pTimer->enmState), pTimer->enmClock, pTimer->enmType, pTimer->u64Expire, u64Now, pTimer->pszDesc));
1998 bool fRc;
1999 TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_EXPIRED_GET_UNLINK, TMTIMERSTATE_ACTIVE, fRc);
2000 if (fRc)
2001 {
2002 Assert(!pTimer->offScheduleNext); /* this can trigger falsely */
2003
2004 /* unlink */
2005 const PTMTIMER pPrev = TMTIMER_GET_PREV(pTimer);
2006 if (pPrev)
2007 TMTIMER_SET_NEXT(pPrev, pNext);
2008 else
2009 {
2010 TMTIMER_SET_HEAD(pQueue, pNext);
2011 pQueue->u64Expire = pNext ? pNext->u64Expire : INT64_MAX;
2012 }
2013 if (pNext)
2014 TMTIMER_SET_PREV(pNext, pPrev);
2015 pTimer->offNext = 0;
2016 pTimer->offPrev = 0;
2017
2018 /* fire */
2019 TM_SET_STATE(pTimer, TMTIMERSTATE_EXPIRED_DELIVER);
2020 switch (pTimer->enmType)
2021 {
2022 case TMTIMERTYPE_DEV: pTimer->u.Dev.pfnTimer(pTimer->u.Dev.pDevIns, pTimer, pTimer->pvUser); break;
2023 case TMTIMERTYPE_USB: pTimer->u.Usb.pfnTimer(pTimer->u.Usb.pUsbIns, pTimer, pTimer->pvUser); break;
2024 case TMTIMERTYPE_DRV: pTimer->u.Drv.pfnTimer(pTimer->u.Drv.pDrvIns, pTimer, pTimer->pvUser); break;
2025 case TMTIMERTYPE_INTERNAL: pTimer->u.Internal.pfnTimer(pVM, pTimer, pTimer->pvUser); break;
2026 case TMTIMERTYPE_EXTERNAL: pTimer->u.External.pfnTimer(pTimer->pvUser); break;
2027 default:
2028 AssertMsgFailed(("Invalid timer type %d (%s)\n", pTimer->enmType, pTimer->pszDesc));
2029 break;
2030 }
2031
2032 /* change the state if it wasn't changed already in the handler. */
2033 TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_STOPPED, TMTIMERSTATE_EXPIRED_DELIVER, fRc);
2034 Log2(("tmR3TimerQueueRun: new state %s\n", tmTimerState(pTimer->enmState)));
2035 }
2036 if (pCritSect)
2037 PDMCritSectLeave(pCritSect);
2038 } /* run loop */
2039}
2040
2041
2042/**
2043 * Schedules and runs any pending times in the timer queue for the
2044 * synchronous virtual clock.
2045 *
2046 * This scheduling is a bit different from the other queues as it need
2047 * to implement the special requirements of the timer synchronous virtual
2048 * clock, thus this 2nd queue run function.
2049 *
2050 * @param pVM The VM to run the timers for.
2051 *
2052 * @remarks The caller must the Virtual Sync lock. Owning the TM lock is no
2053 * longer important.
2054 */
2055static void tmR3TimerQueueRunVirtualSync(PVM pVM)
2056{
2057 PTMTIMERQUEUE const pQueue = &pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC];
2058 VM_ASSERT_EMT(pVM);
2059 Assert(PDMCritSectIsOwner(&pVM->tm.s.VirtualSyncLock));
2060
2061 /*
2062 * Any timers?
2063 */
2064 PTMTIMER pNext = TMTIMER_GET_HEAD(pQueue);
2065 if (RT_UNLIKELY(!pNext))
2066 {
2067 Assert(pVM->tm.s.fVirtualSyncTicking || !pVM->tm.s.cVirtualTicking);
2068 return;
2069 }
2070 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRun);
2071
2072 /*
2073 * Calculate the time frame for which we will dispatch timers.
2074 *
2075 * We use a time frame ranging from the current sync time (which is most likely the
2076 * same as the head timer) and some configurable period (100000ns) up towards the
2077 * current virtual time. This period might also need to be restricted by the catch-up
2078 * rate so frequent calls to this function won't accelerate the time too much, however
2079 * this will be implemented at a later point if necessary.
2080 *
2081 * Without this frame we would 1) having to run timers much more frequently
2082 * and 2) lag behind at a steady rate.
2083 */
2084 const uint64_t u64VirtualNow = TMVirtualGetNoCheck(pVM);
2085 uint64_t const offSyncGivenUp = pVM->tm.s.offVirtualSyncGivenUp;
2086 uint64_t u64Now;
2087 if (!pVM->tm.s.fVirtualSyncTicking)
2088 {
2089 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRunStoppedAlready);
2090 u64Now = pVM->tm.s.u64VirtualSync;
2091 Assert(u64Now <= pNext->u64Expire);
2092 }
2093 else
2094 {
2095 /* Calc 'now'. */
2096 bool fStopCatchup = false;
2097 bool fUpdateStuff = false;
2098 uint64_t off = pVM->tm.s.offVirtualSync;
2099 if (pVM->tm.s.fVirtualSyncCatchUp)
2100 {
2101 uint64_t u64Delta = u64VirtualNow - pVM->tm.s.u64VirtualSyncCatchUpPrev;
2102 if (RT_LIKELY(!(u64Delta >> 32)))
2103 {
2104 uint64_t u64Sub = ASMMultU64ByU32DivByU32(u64Delta, pVM->tm.s.u32VirtualSyncCatchUpPercentage, 100);
2105 if (off > u64Sub + offSyncGivenUp)
2106 {
2107 off -= u64Sub;
2108 Log4(("TM: %'RU64/-%'8RU64: sub %'RU64 [tmR3TimerQueueRunVirtualSync]\n", u64VirtualNow - off, off - offSyncGivenUp, u64Sub));
2109 }
2110 else
2111 {
2112 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
2113 fStopCatchup = true;
2114 off = offSyncGivenUp;
2115 }
2116 fUpdateStuff = true;
2117 }
2118 }
2119 u64Now = u64VirtualNow - off;
2120
2121 /* Adjust against last returned time. */
2122 uint64_t u64Last = ASMAtomicUoReadU64(&pVM->tm.s.u64VirtualSync);
2123 if (u64Last > u64Now)
2124 {
2125 u64Now = u64Last + 1;
2126 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGetAdjLast);
2127 }
2128
2129 /* Check if stopped by expired timer. */
2130 uint64_t u64Expire = pNext->u64Expire;
2131 if (u64Now >= pNext->u64Expire)
2132 {
2133 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRunStop);
2134 u64Now = pNext->u64Expire;
2135 ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSync, u64Now);
2136 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncTicking, false);
2137 Log4(("TM: %'RU64/-%'8RU64: exp tmr [tmR3TimerQueueRunVirtualSync]\n", u64Now, u64VirtualNow - u64Now - offSyncGivenUp));
2138 }
2139 else
2140 {
2141 ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSync, u64Now);
2142 if (fUpdateStuff)
2143 {
2144 ASMAtomicWriteU64(&pVM->tm.s.offVirtualSync, off);
2145 ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSyncCatchUpPrev, u64VirtualNow);
2146 ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSync, u64Now);
2147 if (fStopCatchup)
2148 {
2149 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
2150 Log4(("TM: %'RU64/0: caught up [tmR3TimerQueueRunVirtualSync]\n", u64VirtualNow));
2151 }
2152 }
2153 }
2154 }
2155
2156 /* calc end of frame. */
2157 uint64_t u64Max = u64Now + pVM->tm.s.u32VirtualSyncScheduleSlack;
2158 if (u64Max > u64VirtualNow - offSyncGivenUp)
2159 u64Max = u64VirtualNow - offSyncGivenUp;
2160
2161 /* assert sanity */
2162 Assert(u64Now <= u64VirtualNow - offSyncGivenUp);
2163 Assert(u64Max <= u64VirtualNow - offSyncGivenUp);
2164 Assert(u64Now <= u64Max);
2165 Assert(offSyncGivenUp == pVM->tm.s.offVirtualSyncGivenUp);
2166
2167 /*
2168 * Process the expired timers moving the clock along as we progress.
2169 */
2170#ifdef VBOX_STRICT
2171 uint64_t u64Prev = u64Now; NOREF(u64Prev);
2172#endif
2173 while (pNext && pNext->u64Expire <= u64Max)
2174 {
2175 /* Advance */
2176 PTMTIMER pTimer = pNext;
2177 pNext = TMTIMER_GET_NEXT(pTimer);
2178
2179 /* Take the associated lock. */
2180 PPDMCRITSECT pCritSect = pTimer->pCritSect;
2181 if (pCritSect)
2182 PDMCritSectEnter(pCritSect, VERR_INTERNAL_ERROR);
2183
2184 Log2(("tmR3TimerQueueRun: %p:{.enmState=%s, .enmClock=%d, .enmType=%d, u64Expire=%llx (now=%llx) .pszDesc=%s}\n",
2185 pTimer, tmTimerState(pTimer->enmState), pTimer->enmClock, pTimer->enmType, pTimer->u64Expire, u64Now, pTimer->pszDesc));
2186
2187 /* Advance the clock - don't permit timers to be out of order or armed
2188 in the 'past'. */
2189#ifdef VBOX_STRICT
2190 AssertMsg(pTimer->u64Expire >= u64Prev, ("%'RU64 < %'RU64 %s\n", pTimer->u64Expire, u64Prev, pTimer->pszDesc));
2191 u64Prev = pTimer->u64Expire;
2192#endif
2193 ASMAtomicWriteU64(&pVM->tm.s.u64VirtualSync, pTimer->u64Expire);
2194 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncTicking, false);
2195
2196 /* Unlink it, change the state and do the callout. */
2197 tmTimerQueueUnlinkActive(pQueue, pTimer);
2198 TM_SET_STATE(pTimer, TMTIMERSTATE_EXPIRED_DELIVER);
2199 switch (pTimer->enmType)
2200 {
2201 case TMTIMERTYPE_DEV: pTimer->u.Dev.pfnTimer(pTimer->u.Dev.pDevIns, pTimer, pTimer->pvUser); break;
2202 case TMTIMERTYPE_USB: pTimer->u.Usb.pfnTimer(pTimer->u.Usb.pUsbIns, pTimer, pTimer->pvUser); break;
2203 case TMTIMERTYPE_DRV: pTimer->u.Drv.pfnTimer(pTimer->u.Drv.pDrvIns, pTimer, pTimer->pvUser); break;
2204 case TMTIMERTYPE_INTERNAL: pTimer->u.Internal.pfnTimer(pVM, pTimer, pTimer->pvUser); break;
2205 case TMTIMERTYPE_EXTERNAL: pTimer->u.External.pfnTimer(pTimer->pvUser); break;
2206 default:
2207 AssertMsgFailed(("Invalid timer type %d (%s)\n", pTimer->enmType, pTimer->pszDesc));
2208 break;
2209 }
2210
2211 /* Change the state if it wasn't changed already in the handler.
2212 Reset the Hz hint too since this is the same as TMTimerStop. */
2213 bool fRc;
2214 TM_TRY_SET_STATE(pTimer, TMTIMERSTATE_STOPPED, TMTIMERSTATE_EXPIRED_DELIVER, fRc);
2215 if (fRc && pTimer->uHzHint)
2216 {
2217 if (pTimer->uHzHint >= pVM->tm.s.uMaxHzHint)
2218 ASMAtomicWriteBool(&pVM->tm.s.fHzHintNeedsUpdating, true);
2219 pTimer->uHzHint = 0;
2220 }
2221 Log2(("tmR3TimerQueueRun: new state %s\n", tmTimerState(pTimer->enmState)));
2222
2223 /* Leave the associated lock. */
2224 if (pCritSect)
2225 PDMCritSectLeave(pCritSect);
2226 } /* run loop */
2227
2228
2229 /*
2230 * Restart the clock if it was stopped to serve any timers,
2231 * and start/adjust catch-up if necessary.
2232 */
2233 if ( !pVM->tm.s.fVirtualSyncTicking
2234 && pVM->tm.s.cVirtualTicking)
2235 {
2236 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncRunRestart);
2237
2238 /* calc the slack we've handed out. */
2239 const uint64_t u64VirtualNow2 = TMVirtualGetNoCheck(pVM);
2240 Assert(u64VirtualNow2 >= u64VirtualNow);
2241 AssertMsg(pVM->tm.s.u64VirtualSync >= u64Now, ("%'RU64 < %'RU64\n", pVM->tm.s.u64VirtualSync, u64Now));
2242 const uint64_t offSlack = pVM->tm.s.u64VirtualSync - u64Now;
2243 STAM_STATS({
2244 if (offSlack)
2245 {
2246 PSTAMPROFILE p = &pVM->tm.s.StatVirtualSyncRunSlack;
2247 p->cPeriods++;
2248 p->cTicks += offSlack;
2249 if (p->cTicksMax < offSlack) p->cTicksMax = offSlack;
2250 if (p->cTicksMin > offSlack) p->cTicksMin = offSlack;
2251 }
2252 });
2253
2254 /* Let the time run a little bit while we were busy running timers(?). */
2255 uint64_t u64Elapsed;
2256#define MAX_ELAPSED 30000U /* ns */
2257 if (offSlack > MAX_ELAPSED)
2258 u64Elapsed = 0;
2259 else
2260 {
2261 u64Elapsed = u64VirtualNow2 - u64VirtualNow;
2262 if (u64Elapsed > MAX_ELAPSED)
2263 u64Elapsed = MAX_ELAPSED;
2264 u64Elapsed = u64Elapsed > offSlack ? u64Elapsed - offSlack : 0;
2265 }
2266#undef MAX_ELAPSED
2267
2268 /* Calc the current offset. */
2269 uint64_t offNew = u64VirtualNow2 - pVM->tm.s.u64VirtualSync - u64Elapsed;
2270 Assert(!(offNew & RT_BIT_64(63)));
2271 uint64_t offLag = offNew - pVM->tm.s.offVirtualSyncGivenUp;
2272 Assert(!(offLag & RT_BIT_64(63)));
2273
2274 /*
2275 * Deal with starting, adjusting and stopping catchup.
2276 */
2277 if (pVM->tm.s.fVirtualSyncCatchUp)
2278 {
2279 if (offLag <= pVM->tm.s.u64VirtualSyncCatchUpStopThreshold)
2280 {
2281 /* stop */
2282 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
2283 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
2284 Log4(("TM: %'RU64/-%'8RU64: caught up [pt]\n", u64VirtualNow2 - offNew, offLag));
2285 }
2286 else if (offLag <= pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold)
2287 {
2288 /* adjust */
2289 unsigned i = 0;
2290 while ( i + 1 < RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods)
2291 && offLag >= pVM->tm.s.aVirtualSyncCatchUpPeriods[i + 1].u64Start)
2292 i++;
2293 if (pVM->tm.s.u32VirtualSyncCatchUpPercentage < pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage)
2294 {
2295 STAM_COUNTER_INC(&pVM->tm.s.aStatVirtualSyncCatchupAdjust[i]);
2296 ASMAtomicWriteU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage, pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage);
2297 Log4(("TM: %'RU64/%'8RU64: adj %u%%\n", u64VirtualNow2 - offNew, offLag, pVM->tm.s.u32VirtualSyncCatchUpPercentage));
2298 }
2299 pVM->tm.s.u64VirtualSyncCatchUpPrev = u64VirtualNow2;
2300 }
2301 else
2302 {
2303 /* give up */
2304 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGiveUp);
2305 STAM_PROFILE_ADV_STOP(&pVM->tm.s.StatVirtualSyncCatchup, c);
2306 ASMAtomicWriteU64((uint64_t volatile *)&pVM->tm.s.offVirtualSyncGivenUp, offNew);
2307 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, false);
2308 Log4(("TM: %'RU64/%'8RU64: give up %u%%\n", u64VirtualNow2 - offNew, offLag, pVM->tm.s.u32VirtualSyncCatchUpPercentage));
2309 LogRel(("TM: Giving up catch-up attempt at a %'RU64 ns lag; new total: %'RU64 ns\n", offLag, offNew));
2310 }
2311 }
2312 else if (offLag >= pVM->tm.s.aVirtualSyncCatchUpPeriods[0].u64Start)
2313 {
2314 if (offLag <= pVM->tm.s.u64VirtualSyncCatchUpGiveUpThreshold)
2315 {
2316 /* start */
2317 STAM_PROFILE_ADV_START(&pVM->tm.s.StatVirtualSyncCatchup, c);
2318 unsigned i = 0;
2319 while ( i + 1 < RT_ELEMENTS(pVM->tm.s.aVirtualSyncCatchUpPeriods)
2320 && offLag >= pVM->tm.s.aVirtualSyncCatchUpPeriods[i + 1].u64Start)
2321 i++;
2322 STAM_COUNTER_INC(&pVM->tm.s.aStatVirtualSyncCatchupInitial[i]);
2323 ASMAtomicWriteU32(&pVM->tm.s.u32VirtualSyncCatchUpPercentage, pVM->tm.s.aVirtualSyncCatchUpPeriods[i].u32Percentage);
2324 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncCatchUp, true);
2325 Log4(("TM: %'RU64/%'8RU64: catch-up %u%%\n", u64VirtualNow2 - offNew, offLag, pVM->tm.s.u32VirtualSyncCatchUpPercentage));
2326 }
2327 else
2328 {
2329 /* don't bother */
2330 STAM_COUNTER_INC(&pVM->tm.s.StatVirtualSyncGiveUpBeforeStarting);
2331 ASMAtomicWriteU64((uint64_t volatile *)&pVM->tm.s.offVirtualSyncGivenUp, offNew);
2332 Log4(("TM: %'RU64/%'8RU64: give up\n", u64VirtualNow2 - offNew, offLag));
2333 LogRel(("TM: Not bothering to attempt catching up a %'RU64 ns lag; new total: %'RU64\n", offLag, offNew));
2334 }
2335 }
2336
2337 /*
2338 * Update the offset and restart the clock.
2339 */
2340 Assert(!(offNew & RT_BIT_64(63)));
2341 ASMAtomicWriteU64(&pVM->tm.s.offVirtualSync, offNew);
2342 ASMAtomicWriteBool(&pVM->tm.s.fVirtualSyncTicking, true);
2343 }
2344}
2345
2346
2347/**
2348 * Deals with stopped Virtual Sync clock.
2349 *
2350 * This is called by the forced action flag handling code in EM when it
2351 * encounters the VM_FF_TM_VIRTUAL_SYNC flag. It is called by all VCPUs and they
2352 * will block on the VirtualSyncLock until the pending timers has been executed
2353 * and the clock restarted.
2354 *
2355 * @param pVM The VM to run the timers for.
2356 * @param pVCpu The virtual CPU we're running at.
2357 *
2358 * @thread EMTs
2359 */
2360VMMR3_INT_DECL(void) TMR3VirtualSyncFF(PVM pVM, PVMCPU pVCpu)
2361{
2362 Log2(("TMR3VirtualSyncFF:\n"));
2363
2364 /*
2365 * The EMT doing the timers is diverted to them.
2366 */
2367 if (pVCpu->idCpu == pVM->tm.s.idTimerCpu)
2368 TMR3TimerQueuesDo(pVM);
2369 /*
2370 * The other EMTs will block on the virtual sync lock and the first owner
2371 * will run the queue and thus restarting the clock.
2372 *
2373 * Note! This is very suboptimal code wrt to resuming execution when there
2374 * are more than two Virtual CPUs, since they will all have to enter
2375 * the critical section one by one. But it's a very simple solution
2376 * which will have to do the job for now.
2377 */
2378 else
2379 {
2380 STAM_PROFILE_START(&pVM->tm.s.StatVirtualSyncFF, a);
2381 PDMCritSectEnter(&pVM->tm.s.VirtualSyncLock, VERR_IGNORED);
2382 if (pVM->tm.s.fVirtualSyncTicking)
2383 {
2384 STAM_PROFILE_STOP(&pVM->tm.s.StatVirtualSyncFF, a); /* before the unlock! */
2385 PDMCritSectLeave(&pVM->tm.s.VirtualSyncLock);
2386 Log2(("TMR3VirtualSyncFF: ticking\n"));
2387 }
2388 else
2389 {
2390 PDMCritSectLeave(&pVM->tm.s.VirtualSyncLock);
2391
2392 /* try run it. */
2393 TM_LOCK_TIMERS(pVM);
2394 PDMCritSectEnter(&pVM->tm.s.VirtualSyncLock, VERR_IGNORED);
2395 if (pVM->tm.s.fVirtualSyncTicking)
2396 Log2(("TMR3VirtualSyncFF: ticking (2)\n"));
2397 else
2398 {
2399 ASMAtomicWriteBool(&pVM->tm.s.fRunningVirtualSyncQueue, true);
2400 Log2(("TMR3VirtualSyncFF: running queue\n"));
2401
2402 Assert(!pVM->tm.s.paTimerQueuesR3[TMCLOCK_VIRTUAL_SYNC].offSchedule);
2403 tmR3TimerQueueRunVirtualSync(pVM);
2404 if (pVM->tm.s.fVirtualSyncTicking) /** @todo move into tmR3TimerQueueRunVirtualSync - FIXME */
2405 VM_FF_CLEAR(pVM, VM_FF_TM_VIRTUAL_SYNC);
2406
2407 ASMAtomicWriteBool(&pVM->tm.s.fRunningVirtualSyncQueue, false);
2408 }
2409 STAM_PROFILE_STOP(&pVM->tm.s.StatVirtualSyncFF, a); /* before the unlock! */
2410 PDMCritSectLeave(&pVM->tm.s.VirtualSyncLock);
2411 TM_UNLOCK_TIMERS(pVM);
2412 }
2413 }
2414}
2415
2416
2417/** @name Saved state values
2418 * @{ */
2419#define TMTIMERSTATE_SAVED_PENDING_STOP 4
2420#define TMTIMERSTATE_SAVED_PENDING_SCHEDULE 7
2421/** @} */
2422
2423
2424/**
2425 * Saves the state of a timer to a saved state.
2426 *
2427 * @returns VBox status.
2428 * @param pTimer Timer to save.
2429 * @param pSSM Save State Manager handle.
2430 */
2431VMMR3DECL(int) TMR3TimerSave(PTMTIMERR3 pTimer, PSSMHANDLE pSSM)
2432{
2433 LogFlow(("TMR3TimerSave: %p:{enmState=%s, .pszDesc={%s}} pSSM=%p\n", pTimer, tmTimerState(pTimer->enmState), pTimer->pszDesc, pSSM));
2434 switch (pTimer->enmState)
2435 {
2436 case TMTIMERSTATE_STOPPED:
2437 case TMTIMERSTATE_PENDING_STOP:
2438 case TMTIMERSTATE_PENDING_STOP_SCHEDULE:
2439 return SSMR3PutU8(pSSM, TMTIMERSTATE_SAVED_PENDING_STOP);
2440
2441 case TMTIMERSTATE_PENDING_SCHEDULE_SET_EXPIRE:
2442 case TMTIMERSTATE_PENDING_RESCHEDULE_SET_EXPIRE:
2443 AssertMsgFailed(("u64Expire is being updated! (%s)\n", pTimer->pszDesc));
2444 if (!RTThreadYield())
2445 RTThreadSleep(1);
2446 /* fall thru */
2447 case TMTIMERSTATE_ACTIVE:
2448 case TMTIMERSTATE_PENDING_SCHEDULE:
2449 case TMTIMERSTATE_PENDING_RESCHEDULE:
2450 SSMR3PutU8(pSSM, TMTIMERSTATE_SAVED_PENDING_SCHEDULE);
2451 return SSMR3PutU64(pSSM, pTimer->u64Expire);
2452
2453 case TMTIMERSTATE_EXPIRED_GET_UNLINK:
2454 case TMTIMERSTATE_EXPIRED_DELIVER:
2455 case TMTIMERSTATE_DESTROY:
2456 case TMTIMERSTATE_FREE:
2457 AssertMsgFailed(("Invalid timer state %d %s (%s)\n", pTimer->enmState, tmTimerState(pTimer->enmState), pTimer->pszDesc));
2458 return SSMR3HandleSetStatus(pSSM, VERR_TM_INVALID_STATE);
2459 }
2460
2461 AssertMsgFailed(("Unknown timer state %d (%s)\n", pTimer->enmState, pTimer->pszDesc));
2462 return SSMR3HandleSetStatus(pSSM, VERR_TM_UNKNOWN_STATE);
2463}
2464
2465
2466/**
2467 * Loads the state of a timer from a saved state.
2468 *
2469 * @returns VBox status.
2470 * @param pTimer Timer to restore.
2471 * @param pSSM Save State Manager handle.
2472 */
2473VMMR3DECL(int) TMR3TimerLoad(PTMTIMERR3 pTimer, PSSMHANDLE pSSM)
2474{
2475 Assert(pTimer); Assert(pSSM); VM_ASSERT_EMT(pTimer->pVMR3);
2476 LogFlow(("TMR3TimerLoad: %p:{enmState=%s, .pszDesc={%s}} pSSM=%p\n", pTimer, tmTimerState(pTimer->enmState), pTimer->pszDesc, pSSM));
2477
2478 /*
2479 * Load the state and validate it.
2480 */
2481 uint8_t u8State;
2482 int rc = SSMR3GetU8(pSSM, &u8State);
2483 if (RT_FAILURE(rc))
2484 return rc;
2485#if 1 /* Workaround for accidental state shift in r47786 (2009-05-26 19:12:12). */ /** @todo remove this in a few weeks! */
2486 if ( u8State == TMTIMERSTATE_SAVED_PENDING_STOP + 1
2487 || u8State == TMTIMERSTATE_SAVED_PENDING_SCHEDULE + 1)
2488 u8State--;
2489#endif
2490 if ( u8State != TMTIMERSTATE_SAVED_PENDING_STOP
2491 && u8State != TMTIMERSTATE_SAVED_PENDING_SCHEDULE)
2492 {
2493 AssertLogRelMsgFailed(("u8State=%d\n", u8State));
2494 return SSMR3HandleSetStatus(pSSM, VERR_TM_LOAD_STATE);
2495 }
2496
2497 /* Enter the critical sections to make TMTimerSet/Stop happy. */
2498 if (pTimer->enmClock == TMCLOCK_VIRTUAL_SYNC)
2499 PDMCritSectEnter(&pTimer->pVMR3->tm.s.VirtualSyncLock, VERR_IGNORED);
2500 PPDMCRITSECT pCritSect = pTimer->pCritSect;
2501 if (pCritSect)
2502 PDMCritSectEnter(pCritSect, VERR_IGNORED);
2503
2504 if (u8State == TMTIMERSTATE_SAVED_PENDING_SCHEDULE)
2505 {
2506 /*
2507 * Load the expire time.
2508 */
2509 uint64_t u64Expire;
2510 rc = SSMR3GetU64(pSSM, &u64Expire);
2511 if (RT_FAILURE(rc))
2512 return rc;
2513
2514 /*
2515 * Set it.
2516 */
2517 Log(("u8State=%d u64Expire=%llu\n", u8State, u64Expire));
2518 rc = TMTimerSet(pTimer, u64Expire);
2519 }
2520 else
2521 {
2522 /*
2523 * Stop it.
2524 */
2525 Log(("u8State=%d\n", u8State));
2526 rc = TMTimerStop(pTimer);
2527 }
2528
2529 if (pCritSect)
2530 PDMCritSectLeave(pCritSect);
2531 if (pTimer->enmClock == TMCLOCK_VIRTUAL_SYNC)
2532 PDMCritSectLeave(&pTimer->pVMR3->tm.s.VirtualSyncLock);
2533
2534 /*
2535 * On failure set SSM status.
2536 */
2537 if (RT_FAILURE(rc))
2538 rc = SSMR3HandleSetStatus(pSSM, rc);
2539 return rc;
2540}
2541
2542
2543/**
2544 * Associates a critical section with a timer.
2545 *
2546 * The critical section will be entered prior to doing the timer call back, thus
2547 * avoiding potential races between the timer thread and other threads trying to
2548 * stop or adjust the timer expiration while it's being delivered. The timer
2549 * thread will leave the critical section when the timer callback returns.
2550 *
2551 * In strict builds, ownership of the critical section will be asserted by
2552 * TMTimerSet, TMTimerStop, TMTimerGetExpire and TMTimerDestroy (when called at
2553 * runtime).
2554 *
2555 * @retval VINF_SUCCESS on success.
2556 * @retval VERR_INVALID_HANDLE if the timer handle is NULL or invalid
2557 * (asserted).
2558 * @retval VERR_INVALID_PARAMETER if pCritSect is NULL or has an invalid magic
2559 * (asserted).
2560 * @retval VERR_ALREADY_EXISTS if a critical section was already associated
2561 * with the timer (asserted).
2562 * @retval VERR_INVALID_STATE if the timer isn't stopped.
2563 *
2564 * @param pTimer The timer handle.
2565 * @param pCritSect The critical section. The caller must make sure this
2566 * is around for the life time of the timer.
2567 *
2568 * @thread Any, but the caller is responsible for making sure the timer is not
2569 * active.
2570 */
2571VMMR3DECL(int) TMR3TimerSetCritSect(PTMTIMERR3 pTimer, PPDMCRITSECT pCritSect)
2572{
2573 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
2574 AssertPtrReturn(pCritSect, VERR_INVALID_PARAMETER);
2575 const char *pszName = PDMR3CritSectName(pCritSect); /* exploited for validation */
2576 AssertReturn(pszName, VERR_INVALID_PARAMETER);
2577 AssertReturn(!pTimer->pCritSect, VERR_ALREADY_EXISTS);
2578 AssertReturn(pTimer->enmState == TMTIMERSTATE_STOPPED, VERR_INVALID_STATE);
2579 LogFlow(("pTimer=%p (%s) pCritSect=%p (%s)\n", pTimer, pTimer->pszDesc, pCritSect, pszName));
2580
2581 pTimer->pCritSect = pCritSect;
2582 return VINF_SUCCESS;
2583}
2584
2585
2586/**
2587 * Get the real world UTC time adjusted for VM lag.
2588 *
2589 * @returns pTime.
2590 * @param pVM The VM instance.
2591 * @param pTime Where to store the time.
2592 */
2593VMMR3_INT_DECL(PRTTIMESPEC) TMR3UtcNow(PVM pVM, PRTTIMESPEC pTime)
2594{
2595 RTTimeNow(pTime);
2596 RTTimeSpecSubNano(pTime, ASMAtomicReadU64(&pVM->tm.s.offVirtualSync) - ASMAtomicReadU64((uint64_t volatile *)&pVM->tm.s.offVirtualSyncGivenUp));
2597 RTTimeSpecAddNano(pTime, pVM->tm.s.offUTC);
2598 return pTime;
2599}
2600
2601
2602/**
2603 * Pauses all clocks except TMCLOCK_REAL.
2604 *
2605 * @returns VBox status code, all errors are asserted.
2606 * @param pVM The VM handle.
2607 * @param pVCpu The virtual CPU handle.
2608 * @thread EMT corresponding to the virtual CPU handle.
2609 */
2610VMMR3DECL(int) TMR3NotifySuspend(PVM pVM, PVMCPU pVCpu)
2611{
2612 VMCPU_ASSERT_EMT(pVCpu);
2613
2614 /*
2615 * The shared virtual clock (includes virtual sync which is tied to it).
2616 */
2617 TM_LOCK_TIMERS(pVM); /* Paranoia: Exploiting the timer lock here. */
2618 int rc = tmVirtualPauseLocked(pVM);
2619 TM_UNLOCK_TIMERS(pVM);
2620 if (RT_FAILURE(rc))
2621 return rc;
2622
2623 /*
2624 * Pause the TSC last since it is normally linked to the virtual
2625 * sync clock, so the above code may actually stop both clock.
2626 */
2627 rc = tmCpuTickPause(pVM, pVCpu);
2628 if (RT_FAILURE(rc))
2629 return rc;
2630
2631#ifndef VBOX_WITHOUT_NS_ACCOUNTING
2632 /*
2633 * Update cNsTotal.
2634 */
2635 uint32_t uGen = ASMAtomicIncU32(&pVCpu->tm.s.uTimesGen); Assert(uGen & 1);
2636 pVCpu->tm.s.cNsTotal = RTTimeNanoTS() - pVCpu->tm.s.u64NsTsStartTotal;
2637 pVCpu->tm.s.cNsOther = pVCpu->tm.s.cNsTotal - pVCpu->tm.s.cNsExecuting - pVCpu->tm.s.cNsHalted;
2638 ASMAtomicWriteU32(&pVCpu->tm.s.uTimesGen, (uGen | 1) + 1);
2639#endif
2640
2641 return VINF_SUCCESS;
2642}
2643
2644
2645/**
2646 * Resumes all clocks except TMCLOCK_REAL.
2647 *
2648 * @returns VBox status code, all errors are asserted.
2649 * @param pVM The VM handle.
2650 * @param pVCpu The virtual CPU handle.
2651 * @thread EMT corresponding to the virtual CPU handle.
2652 */
2653VMMR3DECL(int) TMR3NotifyResume(PVM pVM, PVMCPU pVCpu)
2654{
2655 VMCPU_ASSERT_EMT(pVCpu);
2656 int rc;
2657
2658#ifndef VBOX_WITHOUT_NS_ACCOUNTING
2659 /*
2660 * Set u64NsTsStartTotal. There is no need to back this out if either of
2661 * the two calls below fail.
2662 */
2663 pVCpu->tm.s.u64NsTsStartTotal = RTTimeNanoTS() - pVCpu->tm.s.cNsTotal;
2664#endif
2665
2666 /*
2667 * Resume the TSC first since it is normally linked to the virtual sync
2668 * clock, so it may actually not be resumed until we've executed the code
2669 * below.
2670 */
2671 if (!pVM->tm.s.fTSCTiedToExecution)
2672 {
2673 rc = tmCpuTickResume(pVM, pVCpu);
2674 if (RT_FAILURE(rc))
2675 return rc;
2676 }
2677
2678 /*
2679 * The shared virtual clock (includes virtual sync which is tied to it).
2680 */
2681 TM_LOCK_TIMERS(pVM); /* Paranoia: Exploiting the timer lock here. */
2682 rc = tmVirtualResumeLocked(pVM);
2683 TM_UNLOCK_TIMERS(pVM);
2684
2685 return rc;
2686}
2687
2688
2689/**
2690 * Sets the warp drive percent of the virtual time.
2691 *
2692 * @returns VBox status code.
2693 * @param pVM The VM handle.
2694 * @param u32Percent The new percentage. 100 means normal operation.
2695 */
2696VMMDECL(int) TMR3SetWarpDrive(PVM pVM, uint32_t u32Percent)
2697{
2698 return VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)tmR3SetWarpDrive, 2, pVM, u32Percent);
2699}
2700
2701
2702/**
2703 * EMT worker for TMR3SetWarpDrive.
2704 *
2705 * @returns VBox status code.
2706 * @param pVM The VM handle.
2707 * @param u32Percent See TMR3SetWarpDrive().
2708 * @internal
2709 */
2710static DECLCALLBACK(int) tmR3SetWarpDrive(PVM pVM, uint32_t u32Percent)
2711{
2712 PVMCPU pVCpu = VMMGetCpu(pVM);
2713
2714 /*
2715 * Validate it.
2716 */
2717 AssertMsgReturn(u32Percent >= 2 && u32Percent <= 20000,
2718 ("%RX32 is not between 2 and 20000 (inclusive).\n", u32Percent),
2719 VERR_INVALID_PARAMETER);
2720
2721/** @todo This isn't a feature specific to virtual time, move the variables to
2722 * TM level and make it affect TMR3UTCNow as well! */
2723
2724 /*
2725 * If the time is running we'll have to pause it before we can change
2726 * the warp drive settings.
2727 */
2728 TM_LOCK_TIMERS(pVM); /* Paranoia: Exploiting the timer lock here. */
2729 bool fPaused = !!pVM->tm.s.cVirtualTicking;
2730 if (fPaused) /** @todo this isn't really working, but wtf. */
2731 TMR3NotifySuspend(pVM, pVCpu);
2732
2733 pVM->tm.s.u32VirtualWarpDrivePercentage = u32Percent;
2734 pVM->tm.s.fVirtualWarpDrive = u32Percent != 100;
2735 LogRel(("TM: u32VirtualWarpDrivePercentage=%RI32 fVirtualWarpDrive=%RTbool\n",
2736 pVM->tm.s.u32VirtualWarpDrivePercentage, pVM->tm.s.fVirtualWarpDrive));
2737
2738 if (fPaused)
2739 TMR3NotifyResume(pVM, pVCpu);
2740 TM_UNLOCK_TIMERS(pVM);
2741 return VINF_SUCCESS;
2742}
2743
2744
2745/**
2746 * Gets the performance information for one virtual CPU as seen by the VMM.
2747 *
2748 * The returned times covers the period where the VM is running and will be
2749 * reset when restoring a previous VM state (at least for the time being).
2750 *
2751 * @retval VINF_SUCCESS on success.
2752 * @retval VERR_NOT_IMPLEMENTED if not compiled in.
2753 * @retval VERR_INVALID_STATE if the VM handle is bad.
2754 * @retval VERR_INVALID_PARAMETER if idCpu is out of range.
2755 *
2756 * @param pVM The VM handle.
2757 * @param idCpu The ID of the virtual CPU which times to get.
2758 * @param pcNsTotal Where to store the total run time (nano seconds) of
2759 * the CPU, i.e. the sum of the three other returns.
2760 * Optional.
2761 * @param pcNsExecuting Where to store the time (nano seconds) spent
2762 * executing guest code. Optional.
2763 * @param pcNsHalted Where to store the time (nano seconds) spent
2764 * halted. Optional
2765 * @param pcNsOther Where to store the time (nano seconds) spent
2766 * preempted by the host scheduler, on virtualization
2767 * overhead and on other tasks.
2768 */
2769VMMR3DECL(int) TMR3GetCpuLoadTimes(PVM pVM, VMCPUID idCpu, uint64_t *pcNsTotal, uint64_t *pcNsExecuting,
2770 uint64_t *pcNsHalted, uint64_t *pcNsOther)
2771{
2772 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_STATE);
2773 AssertReturn(idCpu < pVM->cCpus, VERR_INVALID_PARAMETER);
2774
2775#ifndef VBOX_WITHOUT_NS_ACCOUNTING
2776 /*
2777 * Get a stable result set.
2778 * This should be way quicker than an EMT request.
2779 */
2780 PVMCPU pVCpu = &pVM->aCpus[idCpu];
2781 uint32_t uTimesGen = ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen);
2782 uint64_t cNsTotal = pVCpu->tm.s.cNsTotal;
2783 uint64_t cNsExecuting = pVCpu->tm.s.cNsExecuting;
2784 uint64_t cNsHalted = pVCpu->tm.s.cNsHalted;
2785 uint64_t cNsOther = pVCpu->tm.s.cNsOther;
2786 while ( (uTimesGen & 1) /* update in progress */
2787 || uTimesGen != ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen))
2788 {
2789 RTThreadYield();
2790 uTimesGen = ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen);
2791 cNsTotal = pVCpu->tm.s.cNsTotal;
2792 cNsExecuting = pVCpu->tm.s.cNsExecuting;
2793 cNsHalted = pVCpu->tm.s.cNsHalted;
2794 cNsOther = pVCpu->tm.s.cNsOther;
2795 }
2796
2797 /*
2798 * Fill in the return values.
2799 */
2800 if (pcNsTotal)
2801 *pcNsTotal = cNsTotal;
2802 if (pcNsExecuting)
2803 *pcNsExecuting = cNsExecuting;
2804 if (pcNsHalted)
2805 *pcNsHalted = cNsHalted;
2806 if (pcNsOther)
2807 *pcNsOther = cNsOther;
2808
2809 return VINF_SUCCESS;
2810
2811#else
2812 return VERR_NOT_IMPLEMENTED;
2813#endif
2814}
2815
2816#ifndef VBOX_WITHOUT_NS_ACCOUNTING
2817
2818/**
2819 * Helper for tmR3CpuLoadTimer.
2820 * @returns
2821 * @param pState The state to update.
2822 * @param cNsTotalDelta Total time.
2823 * @param cNsExecutingDelta Time executing.
2824 * @param cNsHaltedDelta Time halted.
2825 */
2826DECLINLINE(void) tmR3CpuLoadTimerMakeUpdate(PTMCPULOADSTATE pState,
2827 uint64_t cNsTotal,
2828 uint64_t cNsExecuting,
2829 uint64_t cNsHalted)
2830{
2831 /* Calc deltas */
2832 uint64_t cNsTotalDelta = cNsTotal - pState->cNsPrevTotal;
2833 pState->cNsPrevTotal = cNsTotal;
2834
2835 uint64_t cNsExecutingDelta = cNsExecuting - pState->cNsPrevExecuting;
2836 pState->cNsPrevExecuting = cNsExecuting;
2837
2838 uint64_t cNsHaltedDelta = cNsHalted - pState->cNsPrevHalted;
2839 pState->cNsPrevHalted = cNsHalted;
2840
2841 /* Calc pcts. */
2842 if (!cNsTotalDelta)
2843 {
2844 pState->cPctExecuting = 0;
2845 pState->cPctHalted = 100;
2846 pState->cPctOther = 0;
2847 }
2848 else if (cNsTotalDelta < UINT64_MAX / 4)
2849 {
2850 pState->cPctExecuting = (uint8_t)(cNsExecutingDelta * 100 / cNsTotalDelta);
2851 pState->cPctHalted = (uint8_t)(cNsHaltedDelta * 100 / cNsTotalDelta);
2852 pState->cPctOther = (uint8_t)((cNsTotalDelta - cNsExecutingDelta - cNsHaltedDelta) * 100 / cNsTotalDelta);
2853 }
2854 else
2855 {
2856 pState->cPctExecuting = 0;
2857 pState->cPctHalted = 100;
2858 pState->cPctOther = 0;
2859 }
2860}
2861
2862
2863/**
2864 * Timer callback that calculates the CPU load since the last time it was
2865 * called.
2866 *
2867 * @param pVM The VM handle.
2868 * @param pTimer The timer.
2869 * @param pvUser NULL, unused.
2870 */
2871static DECLCALLBACK(void) tmR3CpuLoadTimer(PVM pVM, PTMTIMER pTimer, void *pvUser)
2872{
2873 /*
2874 * Re-arm the timer first.
2875 */
2876 int rc = TMTimerSetMillies(pTimer, 1000);
2877 AssertLogRelRC(rc);
2878 NOREF(pvUser);
2879
2880 /*
2881 * Update the values for each CPU.
2882 */
2883 uint64_t cNsTotalAll = 0;
2884 uint64_t cNsExecutingAll = 0;
2885 uint64_t cNsHaltedAll = 0;
2886 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
2887 {
2888 PVMCPU pVCpu = &pVM->aCpus[iCpu];
2889
2890 /* Try get a stable data set. */
2891 uint32_t cTries = 3;
2892 uint32_t uTimesGen = ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen);
2893 uint64_t cNsTotal = pVCpu->tm.s.cNsTotal;
2894 uint64_t cNsExecuting = pVCpu->tm.s.cNsExecuting;
2895 uint64_t cNsHalted = pVCpu->tm.s.cNsHalted;
2896 while (RT_UNLIKELY( (uTimesGen & 1) /* update in progress */
2897 || uTimesGen != ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen)))
2898 {
2899 if (!--cTries)
2900 break;
2901 ASMNopPause();
2902 uTimesGen = ASMAtomicReadU32(&pVCpu->tm.s.uTimesGen);
2903 cNsTotal = pVCpu->tm.s.cNsTotal;
2904 cNsExecuting = pVCpu->tm.s.cNsExecuting;
2905 cNsHalted = pVCpu->tm.s.cNsHalted;
2906 }
2907
2908 /* Totals */
2909 cNsTotalAll += cNsTotal;
2910 cNsExecutingAll += cNsExecuting;
2911 cNsHaltedAll += cNsHalted;
2912
2913 /* Calc the PCTs and update the state. */
2914 tmR3CpuLoadTimerMakeUpdate(&pVCpu->tm.s.CpuLoad, cNsTotal, cNsExecuting, cNsHalted);
2915 }
2916
2917 /*
2918 * Update the value for all the CPUs.
2919 */
2920 tmR3CpuLoadTimerMakeUpdate(&pVM->tm.s.CpuLoad, cNsTotalAll, cNsExecutingAll, cNsHaltedAll);
2921
2922 /** @todo Try add 1, 5 and 15 min load stats. */
2923
2924}
2925
2926#endif /* !VBOX_WITHOUT_NS_ACCOUNTING */
2927
2928/**
2929 * Gets the 5 char clock name for the info tables.
2930 *
2931 * @returns The name.
2932 * @param enmClock The clock.
2933 */
2934DECLINLINE(const char *) tmR3Get5CharClockName(TMCLOCK enmClock)
2935{
2936 switch (enmClock)
2937 {
2938 case TMCLOCK_REAL: return "Real ";
2939 case TMCLOCK_VIRTUAL: return "Virt ";
2940 case TMCLOCK_VIRTUAL_SYNC: return "VrSy ";
2941 case TMCLOCK_TSC: return "TSC ";
2942 default: return "Bad ";
2943 }
2944}
2945
2946
2947/**
2948 * Display all timers.
2949 *
2950 * @param pVM VM Handle.
2951 * @param pHlp The info helpers.
2952 * @param pszArgs Arguments, ignored.
2953 */
2954static DECLCALLBACK(void) tmR3TimerInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2955{
2956 NOREF(pszArgs);
2957 pHlp->pfnPrintf(pHlp,
2958 "Timers (pVM=%p)\n"
2959 "%.*s %.*s %.*s %.*s Clock %18s %18s %6s %-25s Description\n",
2960 pVM,
2961 sizeof(RTR3PTR) * 2, "pTimerR3 ",
2962 sizeof(int32_t) * 2, "offNext ",
2963 sizeof(int32_t) * 2, "offPrev ",
2964 sizeof(int32_t) * 2, "offSched ",
2965 "Time",
2966 "Expire",
2967 "HzHint",
2968 "State");
2969 TM_LOCK_TIMERS(pVM);
2970 for (PTMTIMERR3 pTimer = pVM->tm.s.pCreated; pTimer; pTimer = pTimer->pBigNext)
2971 {
2972 pHlp->pfnPrintf(pHlp,
2973 "%p %08RX32 %08RX32 %08RX32 %s %18RU64 %18RU64 %6RU32 %-25s %s\n",
2974 pTimer,
2975 pTimer->offNext,
2976 pTimer->offPrev,
2977 pTimer->offScheduleNext,
2978 tmR3Get5CharClockName(pTimer->enmClock),
2979 TMTimerGet(pTimer),
2980 pTimer->u64Expire,
2981 pTimer->uHzHint,
2982 tmTimerState(pTimer->enmState),
2983 pTimer->pszDesc);
2984 }
2985 TM_UNLOCK_TIMERS(pVM);
2986}
2987
2988
2989/**
2990 * Display all active timers.
2991 *
2992 * @param pVM VM Handle.
2993 * @param pHlp The info helpers.
2994 * @param pszArgs Arguments, ignored.
2995 */
2996static DECLCALLBACK(void) tmR3TimerInfoActive(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2997{
2998 NOREF(pszArgs);
2999 pHlp->pfnPrintf(pHlp,
3000 "Active Timers (pVM=%p)\n"
3001 "%.*s %.*s %.*s %.*s Clock %18s %18s %6s %-25s Description\n",
3002 pVM,
3003 sizeof(RTR3PTR) * 2, "pTimerR3 ",
3004 sizeof(int32_t) * 2, "offNext ",
3005 sizeof(int32_t) * 2, "offPrev ",
3006 sizeof(int32_t) * 2, "offSched ",
3007 "Time",
3008 "Expire",
3009 "HzHint",
3010 "State");
3011 for (unsigned iQueue = 0; iQueue < TMCLOCK_MAX; iQueue++)
3012 {
3013 TM_LOCK_TIMERS(pVM);
3014 for (PTMTIMERR3 pTimer = TMTIMER_GET_HEAD(&pVM->tm.s.paTimerQueuesR3[iQueue]);
3015 pTimer;
3016 pTimer = TMTIMER_GET_NEXT(pTimer))
3017 {
3018 pHlp->pfnPrintf(pHlp,
3019 "%p %08RX32 %08RX32 %08RX32 %s %18RU64 %18RU64 %6RU32 %-25s %s\n",
3020 pTimer,
3021 pTimer->offNext,
3022 pTimer->offPrev,
3023 pTimer->offScheduleNext,
3024 tmR3Get5CharClockName(pTimer->enmClock),
3025 TMTimerGet(pTimer),
3026 pTimer->u64Expire,
3027 pTimer->uHzHint,
3028 tmTimerState(pTimer->enmState),
3029 pTimer->pszDesc);
3030 }
3031 TM_UNLOCK_TIMERS(pVM);
3032 }
3033}
3034
3035
3036/**
3037 * Display all clocks.
3038 *
3039 * @param pVM VM Handle.
3040 * @param pHlp The info helpers.
3041 * @param pszArgs Arguments, ignored.
3042 */
3043static DECLCALLBACK(void) tmR3InfoClocks(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3044{
3045 NOREF(pszArgs);
3046
3047 /*
3048 * Read the times first to avoid more than necessary time variation.
3049 */
3050 const uint64_t u64Virtual = TMVirtualGet(pVM);
3051 const uint64_t u64VirtualSync = TMVirtualSyncGet(pVM);
3052 const uint64_t u64Real = TMRealGet(pVM);
3053
3054 for (VMCPUID i = 0; i < pVM->cCpus; i++)
3055 {
3056 PVMCPU pVCpu = &pVM->aCpus[i];
3057 uint64_t u64TSC = TMCpuTickGet(pVCpu);
3058
3059 /*
3060 * TSC
3061 */
3062 pHlp->pfnPrintf(pHlp,
3063 "Cpu Tick: %18RU64 (%#016RX64) %RU64Hz %s%s",
3064 u64TSC, u64TSC, TMCpuTicksPerSecond(pVM),
3065 pVCpu->tm.s.fTSCTicking ? "ticking" : "paused",
3066 pVM->tm.s.fTSCVirtualized ? " - virtualized" : "");
3067 if (pVM->tm.s.fTSCUseRealTSC)
3068 {
3069 pHlp->pfnPrintf(pHlp, " - real tsc");
3070 if (pVCpu->tm.s.offTSCRawSrc)
3071 pHlp->pfnPrintf(pHlp, "\n offset %RU64", pVCpu->tm.s.offTSCRawSrc);
3072 }
3073 else
3074 pHlp->pfnPrintf(pHlp, " - virtual clock");
3075 pHlp->pfnPrintf(pHlp, "\n");
3076 }
3077
3078 /*
3079 * virtual
3080 */
3081 pHlp->pfnPrintf(pHlp,
3082 " Virtual: %18RU64 (%#016RX64) %RU64Hz %s",
3083 u64Virtual, u64Virtual, TMVirtualGetFreq(pVM),
3084 pVM->tm.s.cVirtualTicking ? "ticking" : "paused");
3085 if (pVM->tm.s.fVirtualWarpDrive)
3086 pHlp->pfnPrintf(pHlp, " WarpDrive %RU32 %%", pVM->tm.s.u32VirtualWarpDrivePercentage);
3087 pHlp->pfnPrintf(pHlp, "\n");
3088
3089 /*
3090 * virtual sync
3091 */
3092 pHlp->pfnPrintf(pHlp,
3093 "VirtSync: %18RU64 (%#016RX64) %s%s",
3094 u64VirtualSync, u64VirtualSync,
3095 pVM->tm.s.fVirtualSyncTicking ? "ticking" : "paused",
3096 pVM->tm.s.fVirtualSyncCatchUp ? " - catchup" : "");
3097 if (pVM->tm.s.offVirtualSync)
3098 {
3099 pHlp->pfnPrintf(pHlp, "\n offset %RU64", pVM->tm.s.offVirtualSync);
3100 if (pVM->tm.s.u32VirtualSyncCatchUpPercentage)
3101 pHlp->pfnPrintf(pHlp, " catch-up rate %u %%", pVM->tm.s.u32VirtualSyncCatchUpPercentage);
3102 }
3103 pHlp->pfnPrintf(pHlp, "\n");
3104
3105 /*
3106 * real
3107 */
3108 pHlp->pfnPrintf(pHlp,
3109 " Real: %18RU64 (%#016RX64) %RU64Hz\n",
3110 u64Real, u64Real, TMRealGetFreq(pVM));
3111}
3112
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