1 | /* $Id: tstGIP-2.cpp 54352 2015-02-22 01:32: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].enmState == SUPGIPCPUSTATE_ONLINE)
|
---|
157 | {
|
---|
158 | char szCpuHzDeviation[32];
|
---|
159 | PSUPGIPCPU pPrevCpu = &s_aaCPUs[!(i & 1)][iCpu];
|
---|
160 | PSUPGIPCPU pCpu = &s_aaCPUs[i & 1][iCpu];
|
---|
161 | if (uCpuHzRef)
|
---|
162 | {
|
---|
163 | int64_t iCpuHzDeviation = pCpu->u64CpuHz - uCpuHzRef;
|
---|
164 | uint64_t uCpuHzDeviation = RT_ABS(iCpuHzDeviation);
|
---|
165 | if (uCpuHzDeviation > 999999999)
|
---|
166 | RTStrPrintf(szCpuHzDeviation, sizeof(szCpuHzDeviation), "%17s ", "?");
|
---|
167 | else
|
---|
168 | {
|
---|
169 | /* Wait until the history validation code takes effect. */
|
---|
170 | if (pCpu->u32TransactionId > 23 + (8 * 2) + 1)
|
---|
171 | {
|
---|
172 | if (RT_ABS(iCpuHzDeviation) > RT_ABS(iCpuHzMaxDeviation))
|
---|
173 | iCpuHzMaxDeviation = iCpuHzDeviation;
|
---|
174 | uCpuHzOverallDeviation += uCpuHzDeviation;
|
---|
175 | cCpuHzOverallDevCnt++;
|
---|
176 | }
|
---|
177 | uint32_t uPct = (uint32_t)(uCpuHzDeviation * 100000 / uCpuHzRef + 5);
|
---|
178 | RTStrPrintf(szCpuHzDeviation, sizeof(szCpuHzDeviation), "%10RI64%3d.%02d%% ",
|
---|
179 | iCpuHzDeviation, uPct / 1000, (uPct % 1000) / 10);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | else
|
---|
183 | szCpuHzDeviation[0] = '\0';
|
---|
184 | RTPrintf(fHex
|
---|
185 | ? "tstGIP-2: %4d/%d: %016llx %09llx %016llx %08x %d %08x %15llu %s%08x %08x %08x %08x %08x %08x %08x %08x (%d)\n"
|
---|
186 | : "tstGIP-2: %4d/%d: %016llu %09llu %016llu %010u %d %010u %15llu %s%08x %08x %08x %08x %08x %08x %08x %08x (%d)\n",
|
---|
187 | i, iCpu,
|
---|
188 | pCpu->u64NanoTS,
|
---|
189 | i ? pCpu->u64NanoTS - pPrevCpu->u64NanoTS : 0,
|
---|
190 | pCpu->u64TSC,
|
---|
191 | pCpu->u32UpdateIntervalTSC,
|
---|
192 | pCpu->iTSCHistoryHead,
|
---|
193 | pCpu->u32TransactionId,
|
---|
194 | pCpu->u64CpuHz,
|
---|
195 | szCpuHzDeviation,
|
---|
196 | pCpu->au32TSCHistory[0],
|
---|
197 | pCpu->au32TSCHistory[1],
|
---|
198 | pCpu->au32TSCHistory[2],
|
---|
199 | pCpu->au32TSCHistory[3],
|
---|
200 | pCpu->au32TSCHistory[4],
|
---|
201 | pCpu->au32TSCHistory[5],
|
---|
202 | pCpu->au32TSCHistory[6],
|
---|
203 | pCpu->au32TSCHistory[7],
|
---|
204 | pCpu->cErrors);
|
---|
205 | if (!pu32TransactionId)
|
---|
206 | {
|
---|
207 | pu32TransactionId = &g_pSUPGlobalInfoPage->aCPUs[iCpu].u32TransactionId;
|
---|
208 | u32TransactionId = pCpu->u32TransactionId;
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | /* wait a bit / spin */
|
---|
213 | if (!fSpin)
|
---|
214 | RTThreadSleep(9);
|
---|
215 | else
|
---|
216 | {
|
---|
217 | if (pu32TransactionId)
|
---|
218 | {
|
---|
219 | uint32_t uTmp;
|
---|
220 | while ( u32TransactionId == (uTmp = *pu32TransactionId)
|
---|
221 | || (uTmp & 1))
|
---|
222 | ASMNopPause();
|
---|
223 | }
|
---|
224 | else
|
---|
225 | RTThreadSleep(1);
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * Display TSC deltas.
|
---|
231 | *
|
---|
232 | * First iterative over the APIC ID array to get mostly consistent CPUID to APIC ID mapping.
|
---|
233 | * Then iterate over the offline CPUs. It is possible that there's a race between the online/offline
|
---|
234 | * states between the two iterations, but that cannot be helped from ring-3 anyway and not a biggie.
|
---|
235 | */
|
---|
236 | RTPrintf("tstGIP-2: TSC deltas:\n");
|
---|
237 | RTPrintf("tstGIP-2: idApic: i64TSCDelta\n");
|
---|
238 | for (unsigned i = 0; i < RT_ELEMENTS(g_pSUPGlobalInfoPage->aiCpuFromApicId); i++)
|
---|
239 | {
|
---|
240 | uint16_t iCpu = g_pSUPGlobalInfoPage->aiCpuFromApicId[i];
|
---|
241 | if (iCpu != UINT16_MAX)
|
---|
242 | {
|
---|
243 | RTPrintf("tstGIP-2: %7d: %lld\n", g_pSUPGlobalInfoPage->aCPUs[iCpu].idApic,
|
---|
244 | g_pSUPGlobalInfoPage->aCPUs[iCpu].i64TSCDelta);
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | for (unsigned iCpu = 0; iCpu < g_pSUPGlobalInfoPage->cCpus; iCpu++)
|
---|
249 | if (g_pSUPGlobalInfoPage->aCPUs[iCpu].idApic == UINT16_MAX)
|
---|
250 | RTPrintf("tstGIP-2: offline: %lld\n", g_pSUPGlobalInfoPage->aCPUs[iCpu].i64TSCDelta);
|
---|
251 |
|
---|
252 | RTPrintf("tstGIP-2: enmUseTscDelta=%d fGetGipCpu=%#x\n",
|
---|
253 | g_pSUPGlobalInfoPage->enmUseTscDelta, g_pSUPGlobalInfoPage->fGetGipCpu);
|
---|
254 | if ( uCpuHzRef
|
---|
255 | && cCpuHzOverallDevCnt)
|
---|
256 | {
|
---|
257 | uint32_t uPct = (uint32_t)(uCpuHzOverallDeviation * 100000 / cCpuHzOverallDevCnt / uCpuHzRef + 5);
|
---|
258 | RTPrintf("tstGIP-2: Average CpuHz deviation: %d.%02d%%\n",
|
---|
259 | uPct / 1000, (uPct % 1000) / 10);
|
---|
260 |
|
---|
261 | uint32_t uMaxPct = (uint32_t)(RT_ABS(iCpuHzMaxDeviation) * 100000 / uCpuHzRef + 5);
|
---|
262 | RTPrintf("tstGIP-2: Maximum CpuHz deviation: %d.%02d%% (%RI64 ticks)\n",
|
---|
263 | uMaxPct / 1000, (uMaxPct % 1000) / 10, iCpuHzMaxDeviation);
|
---|
264 | }
|
---|
265 | }
|
---|
266 | else
|
---|
267 | {
|
---|
268 | RTPrintf("tstGIP-2: g_pSUPGlobalInfoPage is NULL\n");
|
---|
269 | rc = -1;
|
---|
270 | }
|
---|
271 |
|
---|
272 | SUPR3Term(false /*fForced*/);
|
---|
273 | }
|
---|
274 | else
|
---|
275 | RTPrintf("tstGIP-2: SUPR3Init failed: %Rrc\n", rc);
|
---|
276 | return !!rc;
|
---|
277 | }
|
---|
278 |
|
---|