VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/TMAllCpu.cpp@ 16524

Last change on this file since 16524 was 13586, checked in by vboxsync, 16 years ago

TM: extended the statistics to get an idea about the normal catchup rate.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.0 KB
Line 
1/* $Id: TMAllCpu.cpp 13586 2008-10-27 16:33:51Z vboxsync $ */
2/** @file
3 * TM - Timeout Manager, CPU Time, All Contexts.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_TM
27#include <VBox/tm.h>
28#include "../TMInternal.h"
29#include <VBox/vm.h>
30#include <VBox/sup.h>
31
32#include <VBox/param.h>
33#include <VBox/err.h>
34#include <iprt/assert.h>
35#include <iprt/asm.h>
36#include <VBox/log.h>
37
38
39/**
40 * Gets the raw cpu tick from current virtual time.
41 */
42DECLINLINE(uint64_t) tmCpuTickGetRawVirtual(PVM pVM, bool fCheckTimers)
43{
44 uint64_t u64 = TMVirtualSyncGetEx(pVM, fCheckTimers);
45 if (u64 != TMCLOCK_FREQ_VIRTUAL)
46 u64 = ASMMultU64ByU32DivByU32(u64, pVM->tm.s.cTSCTicksPerSecond, TMCLOCK_FREQ_VIRTUAL);
47 return u64;
48}
49
50
51/**
52 * Resumes the CPU timestamp counter ticking.
53 *
54 * @returns VBox status code.
55 * @param pVM The VM to operate on.
56 * @internal
57 */
58int tmCpuTickResume(PVM pVM)
59{
60 if (!pVM->tm.s.fTSCTicking)
61 {
62 pVM->tm.s.fTSCTicking = true;
63 if (pVM->tm.s.fTSCVirtualized)
64 {
65 if (pVM->tm.s.fTSCUseRealTSC)
66 pVM->tm.s.u64TSCOffset = ASMReadTSC() - pVM->tm.s.u64TSC;
67 else
68 pVM->tm.s.u64TSCOffset = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
69 - pVM->tm.s.u64TSC;
70 }
71 return VINF_SUCCESS;
72 }
73 AssertFailed();
74 return VERR_INTERNAL_ERROR;
75}
76
77
78/**
79 * Resumes the CPU timestamp counter ticking.
80 *
81 * @returns VBox status code.
82 * @param pVM The VM to operate on.
83 * @todo replace this with TMNotifyResume
84 */
85VMMDECL(int) TMCpuTickResume(PVM pVM)
86{
87 if (!pVM->tm.s.fTSCTiedToExecution)
88 return tmCpuTickResume(pVM);
89 /* ignored */
90 return VINF_SUCCESS;
91}
92
93
94/**
95 * Pauses the CPU timestamp counter ticking.
96 *
97 * @returns VBox status code.
98 * @param pVM The VM to operate on.
99 * @internal
100 */
101int tmCpuTickPause(PVM pVM)
102{
103 if (pVM->tm.s.fTSCTicking)
104 {
105 pVM->tm.s.u64TSC = TMCpuTickGet(pVM);
106 pVM->tm.s.fTSCTicking = false;
107 return VINF_SUCCESS;
108 }
109 AssertFailed();
110 return VERR_INTERNAL_ERROR;
111}
112
113
114/**
115 * Pauses the CPU timestamp counter ticking.
116 *
117 * @returns VBox status code.
118 * @param pVM The VM to operate on.
119 * @todo replace this with TMNotifySuspend
120 */
121VMMDECL(int) TMCpuTickPause(PVM pVM)
122{
123 if (!pVM->tm.s.fTSCTiedToExecution)
124 return tmCpuTickPause(pVM);
125 /* ignored */
126 return VINF_SUCCESS;
127}
128
129
130/**
131 * Checks if AMD-V / VT-x can use an offsetted hardware TSC or not.
132 *
133 * @returns true/false accordingly.
134 * @param pVM The VM handle.
135 * @param poffRealTSC The offset against the TSC of the current CPU.
136 * Can be NULL.
137 * @thread EMT.
138 */
139VMMDECL(bool) TMCpuTickCanUseRealTSC(PVM pVM, uint64_t *poffRealTSC)
140{
141 /*
142 * We require:
143 * 1. A fixed TSC, this is checked at init time.
144 * 2. That the TSC is ticking (we shouldn't be here if it isn't)
145 * 3. Either that we're using the real TSC as time source or
146 * a) we don't have any lag to catch up, and
147 * b) the virtual sync clock hasn't been halted by an expired timer, and
148 * c) we're not using warp drive (accelerated virtual guest time).
149 */
150 if ( pVM->tm.s.fMaybeUseOffsettedHostTSC
151 && RT_LIKELY(pVM->tm.s.fTSCTicking)
152 && ( pVM->tm.s.fTSCUseRealTSC
153 || ( !pVM->tm.s.fVirtualSyncCatchUp
154 && RT_LIKELY(pVM->tm.s.fVirtualSyncTicking)
155 && !pVM->tm.s.fVirtualWarpDrive))
156 )
157 {
158 if (!pVM->tm.s.fTSCUseRealTSC)
159 {
160 /* The source is the timer synchronous virtual clock. */
161 Assert(pVM->tm.s.fTSCVirtualized);
162
163 if (poffRealTSC)
164 {
165 uint64_t u64Now = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
166 - pVM->tm.s.u64TSCOffset;
167 /** @todo When we start collecting statistics on how much time we spend executing
168 * guest code before exiting, we should check this against the next virtual sync
169 * timer timeout. If it's lower than the avg. length, we should trap rdtsc to increase
170 * the chance that we'll get interrupted right after the timer expired. */
171 *poffRealTSC = u64Now - ASMReadTSC();
172 }
173 }
174 else if (poffRealTSC)
175 {
176 /* The source is the real TSC. */
177 if (pVM->tm.s.fTSCVirtualized)
178 *poffRealTSC = pVM->tm.s.u64TSCOffset;
179 else
180 *poffRealTSC = 0;
181 }
182 /** @todo count this? */
183 return true;
184 }
185
186#ifdef VBOX_WITH_STATISTICS
187 /* Sample the reason for refusing. */
188 if (!pVM->tm.s.fMaybeUseOffsettedHostTSC)
189 STAM_COUNTER_INC(&pVM->tm.s.StatTSCNotFixed);
190 else if (!pVM->tm.s.fTSCTicking)
191 STAM_COUNTER_INC(&pVM->tm.s.StatTSCNotTicking);
192 else if (!pVM->tm.s.fTSCUseRealTSC)
193 {
194 if (pVM->tm.s.fVirtualSyncCatchUp)
195 {
196 if (pVM->tm.s.u32VirtualSyncCatchUpPercentage <= 10)
197 STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupLE010);
198 else if (pVM->tm.s.u32VirtualSyncCatchUpPercentage <= 25)
199 STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupLE025);
200 else if (pVM->tm.s.u32VirtualSyncCatchUpPercentage <= 100)
201 STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupLE100);
202 else
203 STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupOther);
204 }
205 else if (!pVM->tm.s.fVirtualSyncTicking)
206 STAM_COUNTER_INC(&pVM->tm.s.StatTSCSyncNotTicking);
207 else if (pVM->tm.s.fVirtualWarpDrive)
208 STAM_COUNTER_INC(&pVM->tm.s.StatTSCWarp);
209 }
210#endif
211 return false;
212}
213
214
215/**
216 * Read the current CPU timstamp counter.
217 *
218 * @returns Gets the CPU tsc.
219 * @param pVM The VM to operate on.
220 */
221VMMDECL(uint64_t) TMCpuTickGet(PVM pVM)
222{
223 uint64_t u64;
224 if (RT_LIKELY(pVM->tm.s.fTSCTicking))
225 {
226 if (pVM->tm.s.fTSCVirtualized)
227 {
228 if (pVM->tm.s.fTSCUseRealTSC)
229 u64 = ASMReadTSC();
230 else
231 u64 = tmCpuTickGetRawVirtual(pVM, true /* check for pending timers */);
232 u64 -= pVM->tm.s.u64TSCOffset;
233 }
234 else
235 u64 = ASMReadTSC();
236 }
237 else
238 u64 = pVM->tm.s.u64TSC;
239 return u64;
240}
241
242
243/**
244 * Sets the current CPU timestamp counter.
245 *
246 * @returns VBox status code.
247 * @param pVM The VM to operate on.
248 * @param u64Tick The new timestamp value.
249 */
250VMMDECL(int) TMCpuTickSet(PVM pVM, uint64_t u64Tick)
251{
252 Assert(!pVM->tm.s.fTSCTicking);
253 pVM->tm.s.u64TSC = u64Tick;
254 return VINF_SUCCESS;
255}
256
257
258/**
259 * Get the timestamp frequency.
260 *
261 * @returns Number of ticks per second.
262 * @param pVM The VM.
263 */
264VMMDECL(uint64_t) TMCpuTicksPerSecond(PVM pVM)
265{
266 if (pVM->tm.s.fTSCUseRealTSC)
267 {
268 uint64_t cTSCTicksPerSecond = SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
269 if (RT_LIKELY(cTSCTicksPerSecond != ~(uint64_t)0))
270 return cTSCTicksPerSecond;
271 }
272 return pVM->tm.s.cTSCTicksPerSecond;
273}
274
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