1 | /* $Id: tstGIP-2.cpp 54252 2015-02-17 19:24:45Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * SUP Testcase - Global Info Page interface (ring 3).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2014 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 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include <VBox/sup.h>
|
---|
31 | #include <VBox/err.h>
|
---|
32 | #include <VBox/param.h>
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/alloc.h>
|
---|
36 | #include <iprt/thread.h>
|
---|
37 | #include <iprt/stream.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/initterm.h>
|
---|
40 | #include <iprt/getopt.h>
|
---|
41 | #include <iprt/x86.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Checks whether the CPU advertises an invariant TSC or not.
|
---|
46 | *
|
---|
47 | * @returns true if invariant, false otherwise.
|
---|
48 | */
|
---|
49 | bool tstIsInvariantTsc(void)
|
---|
50 | {
|
---|
51 | if (ASMHasCpuId())
|
---|
52 | {
|
---|
53 | uint32_t uEax, uEbx, uEcx, uEdx;
|
---|
54 | ASMCpuId(0x80000000, &uEax, &uEbx, &uEcx, &uEdx);
|
---|
55 | if (uEax >= 0x80000007)
|
---|
56 | {
|
---|
57 | ASMCpuId(0x80000007, &uEax, &uEbx, &uEcx, &uEdx);
|
---|
58 | if (uEdx & X86_CPUID_AMD_ADVPOWER_EDX_TSCINVAR)
|
---|
59 | return true;
|
---|
60 | }
|
---|
61 | }
|
---|
62 | return false;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | int main(int argc, char **argv)
|
---|
67 | {
|
---|
68 | RTR3InitExe(argc, &argv, 0);
|
---|
69 |
|
---|
70 | /*
|
---|
71 | * Parse args
|
---|
72 | */
|
---|
73 | static const RTGETOPTDEF g_aOptions[] =
|
---|
74 | {
|
---|
75 | { "--iterations", 'i', RTGETOPT_REQ_INT32 },
|
---|
76 | { "--hex", 'h', RTGETOPT_REQ_NOTHING },
|
---|
77 | { "--decimal", 'd', RTGETOPT_REQ_NOTHING },
|
---|
78 | { "--spin", 's', RTGETOPT_REQ_NOTHING },
|
---|
79 | { "--reference", 'r', RTGETOPT_REQ_UINT64 }, /* reference value of CpuHz, display the
|
---|
80 | * CpuHz deviation in a separate column. */
|
---|
81 | };
|
---|
82 |
|
---|
83 | uint32_t cIterations = 40;
|
---|
84 | bool fHex = true;
|
---|
85 | bool fSpin = false;
|
---|
86 | int ch;
|
---|
87 | uint64_t uCpuHzRef = 0;
|
---|
88 | uint64_t uCpuHzOverallDeviation = 0;
|
---|
89 | int64_t iCpuHzMaxDeviation = 0;
|
---|
90 | int32_t cCpuHzOverallDevCnt = 0;
|
---|
91 | RTGETOPTUNION ValueUnion;
|
---|
92 | RTGETOPTSTATE GetState;
|
---|
93 | RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
94 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
95 | {
|
---|
96 | switch (ch)
|
---|
97 | {
|
---|
98 | case 'i':
|
---|
99 | cIterations = ValueUnion.u32;
|
---|
100 | break;
|
---|
101 |
|
---|
102 | case 'd':
|
---|
103 | fHex = false;
|
---|
104 | break;
|
---|
105 |
|
---|
106 | case 'h':
|
---|
107 | fHex = true;
|
---|
108 | break;
|
---|
109 |
|
---|
110 | case 's':
|
---|
111 | fSpin = true;
|
---|
112 | break;
|
---|
113 |
|
---|
114 | case 'r':
|
---|
115 | uCpuHzRef = ValueUnion.u64;
|
---|
116 | break;
|
---|
117 |
|
---|
118 | default:
|
---|
119 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Init
|
---|
125 | */
|
---|
126 | PSUPDRVSESSION pSession = NIL_RTR0PTR;
|
---|
127 | int rc = SUPR3Init(&pSession);
|
---|
128 | if (RT_SUCCESS(rc))
|
---|
129 | {
|
---|
130 | if (g_pSUPGlobalInfoPage)
|
---|
131 | {
|
---|
132 | RTPrintf("tstGIP-2: cCpus=%d u32UpdateHz=%RU32 u32UpdateIntervalNS=%RU32 u64NanoTSLastUpdateHz=%RX64 u64CpuHz=%RU64 uCpuHzRef=%RU64 u32Mode=%d (%s) u32Version=%#x\n",
|
---|
133 | g_pSUPGlobalInfoPage->cCpus,
|
---|
134 | g_pSUPGlobalInfoPage->u32UpdateHz,
|
---|
135 | g_pSUPGlobalInfoPage->u32UpdateIntervalNS,
|
---|
136 | g_pSUPGlobalInfoPage->u64NanoTSLastUpdateHz,
|
---|
137 | g_pSUPGlobalInfoPage->u64CpuHz,
|
---|
138 | uCpuHzRef,
|
---|
139 | g_pSUPGlobalInfoPage->u32Mode,
|
---|
140 | SUPGetGIPModeName(g_pSUPGlobalInfoPage),
|
---|
141 | g_pSUPGlobalInfoPage->u32Version);
|
---|
142 | RTPrintf(fHex
|
---|
143 | ? "tstGIP-2: it: u64NanoTS delta u64TSC UpIntTSC H TransId CpuHz %sTSC Interval History...\n"
|
---|
144 | : "tstGIP-2: it: u64NanoTS delta u64TSC UpIntTSC H TransId CpuHz %sTSC Interval History...\n",
|
---|
145 | uCpuHzRef ? " CpuHz deviation " : "");
|
---|
146 | static SUPGIPCPU s_aaCPUs[2][256];
|
---|
147 | for (uint32_t i = 0; i < cIterations; i++)
|
---|
148 | {
|
---|
149 | /* copy the data */
|
---|
150 | memcpy(&s_aaCPUs[i & 1][0], &g_pSUPGlobalInfoPage->aCPUs[0], g_pSUPGlobalInfoPage->cCpus * sizeof(g_pSUPGlobalInfoPage->aCPUs[0]));
|
---|
151 |
|
---|
152 | /* display it & find something to spin on. */
|
---|
153 | uint32_t u32TransactionId = 0;
|
---|
154 | uint32_t volatile *pu32TransactionId = NULL;
|
---|
155 | for (unsigned iCpu = 0; iCpu < g_pSUPGlobalInfoPage->cCpus; iCpu++)
|
---|
156 | if ( g_pSUPGlobalInfoPage->aCPUs[iCpu].u64CpuHz > 0
|
---|
157 | && g_pSUPGlobalInfoPage->aCPUs[iCpu].u64CpuHz != _4G + 1)
|
---|
158 | {
|
---|
159 | char szCpuHzDeviation[32];
|
---|
160 | PSUPGIPCPU pPrevCpu = &s_aaCPUs[!(i & 1)][iCpu];
|
---|
161 | PSUPGIPCPU pCpu = &s_aaCPUs[i & 1][iCpu];
|
---|
162 | if (uCpuHzRef)
|
---|
163 | {
|
---|
164 | int64_t iCpuHzDeviation = pCpu->u64CpuHz - uCpuHzRef;
|
---|
165 | uint64_t uCpuHzDeviation = RT_ABS(iCpuHzDeviation);
|
---|
166 | if (uCpuHzDeviation > 999999999)
|
---|
167 | RTStrPrintf(szCpuHzDeviation, sizeof(szCpuHzDeviation), "%17s ", "?");
|
---|
168 | else
|
---|
169 | {
|
---|
170 | /* Wait until the history validation code takes effect. */
|
---|
171 | if (pCpu->u32TransactionId > 23 + (8 * 2) + 1)
|
---|
172 | {
|
---|
173 | if (RT_ABS(iCpuHzDeviation) > RT_ABS(iCpuHzMaxDeviation))
|
---|
174 | iCpuHzMaxDeviation = iCpuHzDeviation;
|
---|
175 | uCpuHzOverallDeviation += uCpuHzDeviation;
|
---|
176 | cCpuHzOverallDevCnt++;
|
---|
177 | }
|
---|
178 | uint32_t uPct = (uint32_t)(uCpuHzDeviation * 100000 / uCpuHzRef + 5);
|
---|
179 | RTStrPrintf(szCpuHzDeviation, sizeof(szCpuHzDeviation), "%10RI64%3d.%02d%% ",
|
---|
180 | iCpuHzDeviation, uPct / 1000, (uPct % 1000) / 10);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | else
|
---|
184 | szCpuHzDeviation[0] = '\0';
|
---|
185 | RTPrintf(fHex
|
---|
186 | ? "tstGIP-2: %4d/%d: %016llx %09llx %016llx %08x %d %08x %15llu %s%08x %08x %08x %08x %08x %08x %08x %08x (%d)\n"
|
---|
187 | : "tstGIP-2: %4d/%d: %016llu %09llu %016llu %010u %d %010u %15llu %s%08x %08x %08x %08x %08x %08x %08x %08x (%d)\n",
|
---|
188 | i, iCpu,
|
---|
189 | pCpu->u64NanoTS,
|
---|
190 | i ? pCpu->u64NanoTS - pPrevCpu->u64NanoTS : 0,
|
---|
191 | pCpu->u64TSC,
|
---|
192 | pCpu->u32UpdateIntervalTSC,
|
---|
193 | pCpu->iTSCHistoryHead,
|
---|
194 | pCpu->u32TransactionId,
|
---|
195 | pCpu->u64CpuHz,
|
---|
196 | szCpuHzDeviation,
|
---|
197 | pCpu->au32TSCHistory[0],
|
---|
198 | pCpu->au32TSCHistory[1],
|
---|
199 | pCpu->au32TSCHistory[2],
|
---|
200 | pCpu->au32TSCHistory[3],
|
---|
201 | pCpu->au32TSCHistory[4],
|
---|
202 | pCpu->au32TSCHistory[5],
|
---|
203 | pCpu->au32TSCHistory[6],
|
---|
204 | pCpu->au32TSCHistory[7],
|
---|
205 | pCpu->cErrors);
|
---|
206 | if (!pu32TransactionId)
|
---|
207 | {
|
---|
208 | pu32TransactionId = &g_pSUPGlobalInfoPage->aCPUs[iCpu].u32TransactionId;
|
---|
209 | u32TransactionId = pCpu->u32TransactionId;
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | /* wait a bit / spin */
|
---|
214 | if (!fSpin)
|
---|
215 | RTThreadSleep(9);
|
---|
216 | else
|
---|
217 | {
|
---|
218 | if (pu32TransactionId)
|
---|
219 | {
|
---|
220 | uint32_t uTmp;
|
---|
221 | while ( u32TransactionId == (uTmp = *pu32TransactionId)
|
---|
222 | || (uTmp & 1))
|
---|
223 | ASMNopPause();
|
---|
224 | }
|
---|
225 | else
|
---|
226 | RTThreadSleep(1);
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | /*
|
---|
231 | * Display TSC deltas.
|
---|
232 | *
|
---|
233 | * First iterative over the APIC ID array to get mostly consistent CPUID to APIC ID mapping.
|
---|
234 | * Then iterate over the offline CPUs. It is possible that there's a race between the online/offline
|
---|
235 | * states between the two iterations, but that cannot be helped from ring-3 anyway and not a biggie.
|
---|
236 | */
|
---|
237 | RTPrintf("tstGIP-2: TSC deltas:\n");
|
---|
238 | RTPrintf("tstGIP-2: idApic: i64TSCDelta\n");
|
---|
239 | for (unsigned i = 0; i < RT_ELEMENTS(g_pSUPGlobalInfoPage->aiCpuFromApicId); i++)
|
---|
240 | {
|
---|
241 | uint16_t iCpu = g_pSUPGlobalInfoPage->aiCpuFromApicId[i];
|
---|
242 | if (iCpu != UINT16_MAX)
|
---|
243 | {
|
---|
244 | RTPrintf("tstGIP-2: %7d: %lld\n", g_pSUPGlobalInfoPage->aCPUs[iCpu].idApic,
|
---|
245 | g_pSUPGlobalInfoPage->aCPUs[iCpu].i64TSCDelta);
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | for (unsigned iCpu = 0; iCpu < g_pSUPGlobalInfoPage->cCpus; iCpu++)
|
---|
250 | if (g_pSUPGlobalInfoPage->aCPUs[iCpu].idApic == UINT16_MAX)
|
---|
251 | RTPrintf("tstGIP-2: offline: %lld\n", g_pSUPGlobalInfoPage->aCPUs[iCpu].i64TSCDelta);
|
---|
252 |
|
---|
253 | RTPrintf("tstGIP-2: enmUseTscDelta=%d fGetGipCpu=%#x\n",
|
---|
254 | g_pSUPGlobalInfoPage->enmUseTscDelta, g_pSUPGlobalInfoPage->fGetGipCpu);
|
---|
255 | if ( uCpuHzRef
|
---|
256 | && cCpuHzOverallDevCnt)
|
---|
257 | {
|
---|
258 | uint32_t uPct = (uint32_t)(uCpuHzOverallDeviation * 100000 / cCpuHzOverallDevCnt / uCpuHzRef + 5);
|
---|
259 | RTPrintf("tstGIP-2: Average CpuHz deviation: %d.%02d%%\n",
|
---|
260 | uPct / 1000, (uPct % 1000) / 10);
|
---|
261 |
|
---|
262 | uint32_t uMaxPct = (uint32_t)(RT_ABS(iCpuHzMaxDeviation) * 100000 / uCpuHzRef + 5);
|
---|
263 | RTPrintf("tstGIP-2: Maximum CpuHz deviation: %d.%02d%% (%RI64 ticks)\n",
|
---|
264 | uMaxPct / 1000, (uMaxPct % 1000) / 10, iCpuHzMaxDeviation);
|
---|
265 | }
|
---|
266 | }
|
---|
267 | else
|
---|
268 | {
|
---|
269 | RTPrintf("tstGIP-2: g_pSUPGlobalInfoPage is NULL\n");
|
---|
270 | rc = -1;
|
---|
271 | }
|
---|
272 |
|
---|
273 | SUPR3Term(false /*fForced*/);
|
---|
274 | }
|
---|
275 | else
|
---|
276 | RTPrintf("tstGIP-2: SUPR3Init failed: %Rrc\n", rc);
|
---|
277 | return !!rc;
|
---|
278 | }
|
---|
279 |
|
---|