1 | /* $Id: tstSupTscDelta.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * SUP Testcase - Global Info Page TSC Delta Measurement Utility.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2015-2019 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 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <VBox/sup.h>
|
---|
32 | #include <iprt/errcore.h>
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/stream.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/getopt.h>
|
---|
37 | #include <iprt/test.h>
|
---|
38 | #include <iprt/thread.h>
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 | int main(int argc, char **argv)
|
---|
43 | {
|
---|
44 | RTTEST hTest;
|
---|
45 | RTEXITCODE rcExit = RTTestInitExAndCreate(argc, &argv, 0 /*fRtInit*/, "tstSupTscDelta", &hTest);
|
---|
46 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
47 | return rcExit;
|
---|
48 |
|
---|
49 | /*
|
---|
50 | * Parse args
|
---|
51 | */
|
---|
52 | static const RTGETOPTDEF g_aOptions[] =
|
---|
53 | {
|
---|
54 | { "--iterations", 'i', RTGETOPT_REQ_INT32 },
|
---|
55 | { "--delay", 'd', RTGETOPT_REQ_INT32 },
|
---|
56 | };
|
---|
57 |
|
---|
58 | uint32_t cIterations = 0; /* Currently 0 so that it doesn't upset testing. */
|
---|
59 | uint32_t cMsSleepBetweenIterations = 10;
|
---|
60 |
|
---|
61 | int ch;
|
---|
62 | RTGETOPTUNION ValueUnion;
|
---|
63 | RTGETOPTSTATE GetState;
|
---|
64 | RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
|
---|
65 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
66 | {
|
---|
67 | switch (ch)
|
---|
68 | {
|
---|
69 | case 'd':
|
---|
70 | cMsSleepBetweenIterations = ValueUnion.u32;
|
---|
71 | break;
|
---|
72 | case 'i':
|
---|
73 | cIterations = ValueUnion.u32;
|
---|
74 | break;
|
---|
75 |
|
---|
76 | default:
|
---|
77 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | if (!cIterations)
|
---|
81 | return RTTestSkipAndDestroy(hTest, "Nothing to do. The --iterations argument is 0 or not given.");
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * Init
|
---|
85 | */
|
---|
86 | PSUPDRVSESSION pSession = NIL_RTR0PTR;
|
---|
87 | int rc = SUPR3Init(&pSession);
|
---|
88 | if (RT_SUCCESS(rc))
|
---|
89 | {
|
---|
90 | PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
|
---|
91 | if (pGip)
|
---|
92 | {
|
---|
93 | if (pGip->enmUseTscDelta < SUPGIPUSETSCDELTA_PRACTICALLY_ZERO)
|
---|
94 | return RTTestSkipAndDestroy(hTest, "No deltas to play with: enmUseTscDelta=%d\n", pGip->enmUseTscDelta);
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * Init stats.
|
---|
98 | */
|
---|
99 | struct
|
---|
100 | {
|
---|
101 | int64_t iLowest;
|
---|
102 | int64_t iHighest;
|
---|
103 | int64_t iTotal;
|
---|
104 | uint64_t uAbsMin;
|
---|
105 | uint64_t uAbsMax;
|
---|
106 | uint64_t uAbsTotal;
|
---|
107 | } aCpuStats[RTCPUSET_MAX_CPUS];
|
---|
108 | RT_ZERO(aCpuStats);
|
---|
109 | for (uint32_t i = 0; i < pGip->cCpus; i++)
|
---|
110 | {
|
---|
111 | aCpuStats[i].iLowest = INT64_MAX;
|
---|
112 | aCpuStats[i].iHighest = INT64_MIN;
|
---|
113 | aCpuStats[i].uAbsMin = UINT64_MAX;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * Do the work.
|
---|
118 | */
|
---|
119 | for (uint32_t iIteration = 0; ; iIteration++)
|
---|
120 | {
|
---|
121 | /*
|
---|
122 | * Display the current deltas and gather statistics.
|
---|
123 | */
|
---|
124 | RTPrintf("tstSupTscDelta: Iteration #%u results:", iIteration);
|
---|
125 | for (uint32_t iCpu = 0; iCpu < pGip->cCpus; iCpu++)
|
---|
126 | {
|
---|
127 | int64_t iTscDelta = pGip->aCPUs[iCpu].i64TSCDelta;
|
---|
128 |
|
---|
129 | /* print */
|
---|
130 | if ((iCpu % 4) == 0)
|
---|
131 | RTPrintf("\ntstSupTscDelta:");
|
---|
132 | if (pGip->aCPUs[iCpu].enmState != SUPGIPCPUSTATE_ONLINE)
|
---|
133 | RTPrintf(" %02x: offline ", iCpu);
|
---|
134 | else if (iTscDelta != INT64_MAX)
|
---|
135 | RTPrintf(" %02x: %-12lld", iCpu, iTscDelta);
|
---|
136 | else
|
---|
137 | RTPrintf(" %02x: INT64_MAX ", iCpu);
|
---|
138 |
|
---|
139 | /* stats */
|
---|
140 | if ( iTscDelta != INT64_MAX
|
---|
141 | && pGip->aCPUs[iCpu].enmState == SUPGIPCPUSTATE_ONLINE)
|
---|
142 | {
|
---|
143 | if (aCpuStats[iCpu].iLowest > iTscDelta)
|
---|
144 | aCpuStats[iCpu].iLowest = iTscDelta;
|
---|
145 | if (aCpuStats[iCpu].iHighest < iTscDelta)
|
---|
146 | aCpuStats[iCpu].iHighest = iTscDelta;
|
---|
147 | aCpuStats[iCpu].iTotal += iTscDelta;
|
---|
148 |
|
---|
149 | uint64_t uAbsTscDelta = iTscDelta >= 0 ? (uint64_t)iTscDelta : (uint64_t)-iTscDelta;
|
---|
150 | if (aCpuStats[iCpu].uAbsMin > uAbsTscDelta)
|
---|
151 | aCpuStats[iCpu].uAbsMin = uAbsTscDelta;
|
---|
152 | if (aCpuStats[iCpu].uAbsMax < uAbsTscDelta)
|
---|
153 | aCpuStats[iCpu].uAbsMax = uAbsTscDelta;
|
---|
154 | aCpuStats[iCpu].uAbsTotal += uAbsTscDelta;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | if (((pGip->cCpus - 1) % 4) != 0)
|
---|
158 | RTPrintf("\n");
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * Done?
|
---|
162 | */
|
---|
163 | if (iIteration + 1 >= cIterations)
|
---|
164 | break;
|
---|
165 |
|
---|
166 | /*
|
---|
167 | * Force a new measurement.
|
---|
168 | */
|
---|
169 | RTThreadSleep(cMsSleepBetweenIterations);
|
---|
170 | for (uint32_t iCpu = 0; iCpu < pGip->cCpus; iCpu++)
|
---|
171 | if (pGip->aCPUs[iCpu].enmState == SUPGIPCPUSTATE_ONLINE)
|
---|
172 | {
|
---|
173 | rc = SUPR3TscDeltaMeasure(pGip->aCPUs[iCpu].idCpu, false /*fAsync*/, true /*fForce*/, 64, 16 /*ms*/);
|
---|
174 | if (RT_FAILURE(rc))
|
---|
175 | RTTestFailed(hTest, "SUPR3TscDeltaMeasure failed on %#x: %Rrc", pGip->aCPUs[iCpu].idCpu, rc);
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | /*
|
---|
180 | * Display statistics that we've gathered.
|
---|
181 | */
|
---|
182 | RTPrintf("tstSupTscDelta: Results:\n");
|
---|
183 | int64_t iLowest = INT64_MAX;
|
---|
184 | int64_t iHighest = INT64_MIN;
|
---|
185 | int64_t iTotal = 0;
|
---|
186 | uint32_t cTotal = 0;
|
---|
187 | for (uint32_t iCpu = 0; iCpu < pGip->cCpus; iCpu++)
|
---|
188 | {
|
---|
189 | if (pGip->aCPUs[iCpu].enmState != SUPGIPCPUSTATE_ONLINE)
|
---|
190 | RTPrintf("tstSupTscDelta: %02x: offline\n", iCpu);
|
---|
191 | else
|
---|
192 | {
|
---|
193 | RTPrintf("tstSupTscDelta: %02x: lowest=%-12lld highest=%-12lld average=%-12lld spread=%-12lld\n",
|
---|
194 | iCpu,
|
---|
195 | aCpuStats[iCpu].iLowest,
|
---|
196 | aCpuStats[iCpu].iHighest,
|
---|
197 | aCpuStats[iCpu].iTotal / cIterations,
|
---|
198 | aCpuStats[iCpu].iHighest - aCpuStats[iCpu].iLowest);
|
---|
199 | RTPrintf( "tstSupTscDelta: absmin=%-12llu absmax=%-12llu absavg=%-12llu idCpu=%#4x idApic=%#4x\n",
|
---|
200 | aCpuStats[iCpu].uAbsMin,
|
---|
201 | aCpuStats[iCpu].uAbsMax,
|
---|
202 | aCpuStats[iCpu].uAbsTotal / cIterations,
|
---|
203 | pGip->aCPUs[iCpu].idCpu,
|
---|
204 | pGip->aCPUs[iCpu].idApic);
|
---|
205 | if (iLowest > aCpuStats[iCpu].iLowest)
|
---|
206 | iLowest = aCpuStats[iCpu].iLowest;
|
---|
207 | if (iHighest < aCpuStats[iCpu].iHighest)
|
---|
208 | iHighest = aCpuStats[iCpu].iHighest;
|
---|
209 | iTotal += aCpuStats[iCpu].iHighest;
|
---|
210 | cTotal += cIterations;
|
---|
211 | }
|
---|
212 | }
|
---|
213 | RTPrintf("tstSupTscDelta: all: lowest=%-12lld highest=%-12lld average=%-12lld spread=%-12lld\n",
|
---|
214 | iLowest, iHighest, iTotal / cTotal, iHighest - iLowest);
|
---|
215 | }
|
---|
216 | else
|
---|
217 | RTTestFailed(hTest, "g_pSUPGlobalInfoPage is NULL");
|
---|
218 |
|
---|
219 | SUPR3Term(false /*fForced*/);
|
---|
220 | }
|
---|
221 | else
|
---|
222 | RTTestFailed(hTest, "SUPR3Init failed: %Rrc", rc);
|
---|
223 | return RTTestSummaryAndDestroy(hTest);
|
---|
224 | }
|
---|
225 |
|
---|