VirtualBox

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

Last change on this file since 22245 was 22245, checked in by vboxsync, 15 years ago

Only update the last seen tsc when the new one is larger.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.0 KB
Line 
1/* $Id: TMAllCpu.cpp 22245 2009-08-13 15:58:00Z 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 * @param pVCpu The VMCPU to operate on.
57 * @internal
58 */
59int tmCpuTickResume(PVM pVM, PVMCPU pVCpu)
60{
61 if (!pVCpu->tm.s.fTSCTicking)
62 {
63 pVCpu->tm.s.fTSCTicking = true;
64 if (pVM->tm.s.fTSCVirtualized)
65 {
66 /** @todo Test that pausing and resuming doesn't cause lag! (I.e. that we're
67 * unpaused before the virtual time and stopped after it. */
68 if (pVM->tm.s.fTSCUseRealTSC)
69 pVCpu->tm.s.offTSCRawSrc = ASMReadTSC() - pVCpu->tm.s.u64TSC;
70 else
71 pVCpu->tm.s.offTSCRawSrc = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
72 - pVCpu->tm.s.u64TSC;
73 }
74 return VINF_SUCCESS;
75 }
76 AssertFailed();
77 return VERR_INTERNAL_ERROR;
78}
79
80
81/**
82 * Pauses the CPU timestamp counter ticking.
83 *
84 * @returns VBox status code.
85 * @param pVM The VM to operate on.
86 * @param pVCpu The VMCPU to operate on.
87 * @internal
88 */
89int tmCpuTickPause(PVM pVM, PVMCPU pVCpu)
90{
91 if (pVCpu->tm.s.fTSCTicking)
92 {
93 pVCpu->tm.s.u64TSC = TMCpuTickGetNoCheck(pVCpu);
94 pVCpu->tm.s.fTSCTicking = false;
95 return VINF_SUCCESS;
96 }
97 AssertFailed();
98 return VERR_INTERNAL_ERROR;
99}
100
101
102/**
103 * Checks if AMD-V / VT-x can use an offsetted hardware TSC or not.
104 *
105 * @returns true/false accordingly.
106 * @param pVCpu The VMCPU to operate on.
107 * @param poffRealTSC The offset against the TSC of the current CPU.
108 * Can be NULL.
109 * @thread EMT.
110 */
111VMMDECL(bool) TMCpuTickCanUseRealTSC(PVMCPU pVCpu, uint64_t *poffRealTSC)
112{
113 PVM pVM = pVCpu->CTX_SUFF(pVM);
114
115 /*
116 * We require:
117 * 1. A fixed TSC, this is checked at init time.
118 * 2. That the TSC is ticking (we shouldn't be here if it isn't)
119 * 3. Either that we're using the real TSC as time source or
120 * a) we don't have any lag to catch up, and
121 * b) the virtual sync clock hasn't been halted by an expired timer, and
122 * c) we're not using warp drive (accelerated virtual guest time).
123 */
124 if ( pVM->tm.s.fMaybeUseOffsettedHostTSC
125 && RT_LIKELY(pVCpu->tm.s.fTSCTicking)
126 && ( pVM->tm.s.fTSCUseRealTSC
127 || ( !pVM->tm.s.fVirtualSyncCatchUp
128 && RT_LIKELY(pVM->tm.s.fVirtualSyncTicking)
129 && !pVM->tm.s.fVirtualWarpDrive))
130 )
131 {
132 if (!pVM->tm.s.fTSCUseRealTSC)
133 {
134 /* The source is the timer synchronous virtual clock. */
135 Assert(pVM->tm.s.fTSCVirtualized);
136
137 if (poffRealTSC)
138 {
139 uint64_t u64Now = tmCpuTickGetRawVirtual(pVM, false /* don't check for pending timers */)
140 - pVCpu->tm.s.offTSCRawSrc;
141 /** @todo When we start collecting statistics on how much time we spend executing
142 * guest code before exiting, we should check this against the next virtual sync
143 * timer timeout. If it's lower than the avg. length, we should trap rdtsc to increase
144 * the chance that we'll get interrupted right after the timer expired. */
145 *poffRealTSC = u64Now - ASMReadTSC();
146 }
147 }
148 else if (poffRealTSC)
149 {
150 /* The source is the real TSC. */
151 if (pVM->tm.s.fTSCVirtualized)
152 *poffRealTSC = pVCpu->tm.s.offTSCRawSrc;
153 else
154 *poffRealTSC = 0;
155 }
156 /** @todo count this? */
157 return true;
158 }
159
160#ifdef VBOX_WITH_STATISTICS
161 /* Sample the reason for refusing. */
162 if (!pVM->tm.s.fMaybeUseOffsettedHostTSC)
163 STAM_COUNTER_INC(&pVM->tm.s.StatTSCNotFixed);
164 else if (!pVCpu->tm.s.fTSCTicking)
165 STAM_COUNTER_INC(&pVM->tm.s.StatTSCNotTicking);
166 else if (!pVM->tm.s.fTSCUseRealTSC)
167 {
168 if (pVM->tm.s.fVirtualSyncCatchUp)
169 {
170 if (pVM->tm.s.u32VirtualSyncCatchUpPercentage <= 10)
171 STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupLE010);
172 else if (pVM->tm.s.u32VirtualSyncCatchUpPercentage <= 25)
173 STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupLE025);
174 else if (pVM->tm.s.u32VirtualSyncCatchUpPercentage <= 100)
175 STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupLE100);
176 else
177 STAM_COUNTER_INC(&pVM->tm.s.StatTSCCatchupOther);
178 }
179 else if (!pVM->tm.s.fVirtualSyncTicking)
180 STAM_COUNTER_INC(&pVM->tm.s.StatTSCSyncNotTicking);
181 else if (pVM->tm.s.fVirtualWarpDrive)
182 STAM_COUNTER_INC(&pVM->tm.s.StatTSCWarp);
183 }
184#endif
185 return false;
186}
187
188
189/**
190 * Read the current CPU timstamp counter.
191 *
192 * @returns Gets the CPU tsc.
193 * @param pVCpu The VMCPU to operate on.
194 */
195DECLINLINE(uint64_t) tmCpuTickGetInternal(PVMCPU pVCpu, bool fCheckTimers)
196{
197 uint64_t u64;
198
199 if (RT_LIKELY(pVCpu->tm.s.fTSCTicking))
200 {
201 PVM pVM = pVCpu->CTX_SUFF(pVM);
202 if (pVM->tm.s.fTSCVirtualized)
203 {
204 if (pVM->tm.s.fTSCUseRealTSC)
205 u64 = ASMReadTSC();
206 else
207 u64 = tmCpuTickGetRawVirtual(pVM, fCheckTimers);
208 u64 -= pVCpu->tm.s.offTSCRawSrc;
209 }
210 else
211 u64 = ASMReadTSC();
212
213 /* Never return a value lower than what the guest has already seen. */
214 if (u64 < pVCpu->tm.s.u64TSCLastSeen)
215 {
216 STAM_COUNTER_INC(&pVM->tm.s.StatTSCUnderflow);
217 pVCpu->tm.s.u64TSCLastSeen += 64; /* @todo choose a good increment here */
218 u64 = pVCpu->tm.s.u64TSCLastSeen;
219 }
220 }
221 else
222 u64 = pVCpu->tm.s.u64TSC;
223 return u64;
224}
225
226
227/**
228 * Read the current CPU timstamp counter.
229 *
230 * @returns Gets the CPU tsc.
231 * @param pVCpu The VMCPU to operate on.
232 */
233VMMDECL(uint64_t) TMCpuTickGet(PVMCPU pVCpu)
234{
235 return tmCpuTickGetInternal(pVCpu, true /* fCheckTimers */);
236}
237
238
239/**
240 * Read the current CPU timstamp counter, don't check for expired timers.
241 *
242 * @returns Gets the CPU tsc.
243 * @param pVCpu The VMCPU to operate on.
244 */
245VMMDECL(uint64_t) TMCpuTickGetNoCheck(PVMCPU pVCpu)
246{
247 return tmCpuTickGetInternal(pVCpu, false /* fCheckTimers */);
248}
249
250
251/**
252 * Sets the current CPU timestamp counter.
253 *
254 * @returns VBox status code.
255 * @param pVM The VM handle.
256 * @param pVCpu The virtual CPU to operate on.
257 * @param u64Tick The new timestamp value.
258 *
259 * @thread EMT which TSC is to be set.
260 */
261VMMDECL(int) TMCpuTickSet(PVM pVM, PVMCPU pVCpu, uint64_t u64Tick)
262{
263 VMCPU_ASSERT_EMT(pVCpu);
264 STAM_COUNTER_INC(&pVM->tm.s.StatTSCSet);
265
266 /*
267 * This is easier to do when the TSC is paused since resume will
268 * do all the calcuations for us. Actually, we don't need to
269 * call tmCpuTickPause here since we overwrite u64TSC anyway.
270 */
271 bool fTSCTicking = pVCpu->tm.s.fTSCTicking;
272 pVCpu->tm.s.fTSCTicking = false;
273 pVCpu->tm.s.u64TSC = u64Tick;
274 if (fTSCTicking)
275 tmCpuTickResume(pVM, pVCpu);
276 /** @todo Try help synchronizing it better among the virtual CPUs? */
277
278 return VINF_SUCCESS;
279}
280
281/**
282 * Sets the last seen CPU timestamp counter.
283 *
284 * @returns VBox status code.
285 * @param pVCpu The virtual CPU to operate on.
286 * @param u64LastSeenTick The last seen timestamp value.
287 *
288 * @thread EMT which TSC is to be set.
289 */
290VMMDECL(int) TMCpuTickSetLastSeen(PVMCPU pVCpu, uint64_t u64LastSeenTick)
291{
292 VMCPU_ASSERT_EMT(pVCpu);
293
294 LogFlow(("TMCpuTickSetLastSeen %RX64\n", u64LastSeenTick));
295 if (pVCpu->tm.s.u64TSCLastSeen < u64LastSeenTick)
296 pVCpu->tm.s.u64TSCLastSeen = u64LastSeenTick;
297 return VINF_SUCCESS;
298}
299
300/**
301 * Gets the last seen CPU timestamp counter.
302 *
303 * @returns last seen TSC
304 * @param pVCpu The virtual CPU to operate on.
305 *
306 * @thread EMT which TSC is to be set.
307 */
308VMMDECL(uint64_t) TMCpuTickGetLastSeen(PVMCPU pVCpu)
309{
310 VMCPU_ASSERT_EMT(pVCpu);
311
312 return pVCpu->tm.s.u64TSCLastSeen;
313}
314
315
316/**
317 * Get the timestamp frequency.
318 *
319 * @returns Number of ticks per second.
320 * @param pVM The VM.
321 */
322VMMDECL(uint64_t) TMCpuTicksPerSecond(PVM pVM)
323{
324 if (pVM->tm.s.fTSCUseRealTSC)
325 {
326 uint64_t cTSCTicksPerSecond = SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage);
327 if (RT_LIKELY(cTSCTicksPerSecond != ~(uint64_t)0))
328 return cTSCTicksPerSecond;
329 }
330 return pVM->tm.s.cTSCTicksPerSecond;
331}
332
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