1 | /* $Id: cpu-numa.cpp 85074 2020-07-06 17:17:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * numa - NUMA / memory benchmark.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2020 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 <iprt/test.h>
|
---|
32 |
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | //#if defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
|
---|
35 | //# include <iprt/asm-amd64-x86.h>
|
---|
36 | //#endif
|
---|
37 | #include <iprt/mem.h>
|
---|
38 | #include <iprt/mp.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/thread.h>
|
---|
41 | #include <iprt/time.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | /*********************************************************************************************************************************
|
---|
45 | * Global Variables *
|
---|
46 | *********************************************************************************************************************************/
|
---|
47 | /** The number of threads to skip when testing. */
|
---|
48 | static uint32_t g_cThreadsToSkip = 1;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Gets the next online CPU.
|
---|
52 | *
|
---|
53 | * @returns Next CPU index or RTCPUSET_MAX_CPUS.
|
---|
54 | * @param iCurCpu The current CPU (index).
|
---|
55 | */
|
---|
56 | static int getNextCpu(unsigned iCurCpu)
|
---|
57 | {
|
---|
58 | /* Skip to the next chip. */
|
---|
59 | iCurCpu = (iCurCpu / g_cThreadsToSkip) * g_cThreadsToSkip;
|
---|
60 | iCurCpu += g_cThreadsToSkip;
|
---|
61 |
|
---|
62 | /* Skip offline cpus. */
|
---|
63 | while ( iCurCpu < RTCPUSET_MAX_CPUS
|
---|
64 | && !RTMpIsCpuOnline(iCurCpu) )
|
---|
65 | iCurCpu++;
|
---|
66 |
|
---|
67 | /* Make sure we're within bounds (in case of bad input). */
|
---|
68 | if (iCurCpu > RTCPUSET_MAX_CPUS)
|
---|
69 | iCurCpu = RTCPUSET_MAX_CPUS;
|
---|
70 | return iCurCpu;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | static void doTest(RTTEST hTest)
|
---|
75 | {
|
---|
76 | NOREF(hTest);
|
---|
77 | uint32_t iAllocCpu = 0;
|
---|
78 | while (iAllocCpu < RTCPUSET_MAX_CPUS)
|
---|
79 | {
|
---|
80 | const uint32_t cbTestSet = _1M * 32;
|
---|
81 | const uint32_t cIterations = 384;
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * Change CPU and allocate a chunk of memory.
|
---|
85 | */
|
---|
86 | RTTESTI_CHECK_RC_OK_RETV(RTThreadSetAffinityToCpu(RTMpCpuIdFromSetIndex(iAllocCpu)));
|
---|
87 |
|
---|
88 | void *pvTest = RTMemPageAlloc(cbTestSet); /* may be leaked, who cares */
|
---|
89 | RTTESTI_CHECK_RETV(pvTest != NULL);
|
---|
90 | memset(pvTest, 0xef, cbTestSet);
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Do the tests.
|
---|
94 | */
|
---|
95 | uint32_t iAccessCpu = 0;
|
---|
96 | while (iAccessCpu < RTCPUSET_MAX_CPUS)
|
---|
97 | {
|
---|
98 | RTTESTI_CHECK_RC_OK_RETV(RTThreadSetAffinityToCpu(RTMpCpuIdFromSetIndex(iAccessCpu)));
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * The write test.
|
---|
102 | */
|
---|
103 | RTTimeNanoTS(); RTThreadYield();
|
---|
104 | uint64_t u64StartTS = RTTimeNanoTS();
|
---|
105 | for (uint32_t i = 0; i < cIterations; i++)
|
---|
106 | {
|
---|
107 | ASMCompilerBarrier(); /* paranoia */
|
---|
108 | memset(pvTest, i, cbTestSet);
|
---|
109 | }
|
---|
110 | uint64_t const cNsElapsedWrite = RTTimeNanoTS() - u64StartTS;
|
---|
111 | uint64_t cMBPerSec = (uint64_t)( ((uint64_t)cIterations * cbTestSet) /* bytes */
|
---|
112 | / ((long double)cNsElapsedWrite / RT_NS_1SEC_64) /* seconds */
|
---|
113 | / _1M /* MB */ );
|
---|
114 | RTTestIValueF(cMBPerSec, RTTESTUNIT_MEGABYTES_PER_SEC, "cpu%02u-mem%02u-write", iAllocCpu, iAccessCpu);
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * The read test.
|
---|
118 | */
|
---|
119 | memset(pvTest, 0, cbTestSet);
|
---|
120 | RTTimeNanoTS(); RTThreadYield();
|
---|
121 | u64StartTS = RTTimeNanoTS();
|
---|
122 | for (uint32_t i = 0; i < cIterations; i++)
|
---|
123 | {
|
---|
124 | #if 1
|
---|
125 | size_t u = 0;
|
---|
126 | size_t volatile *puCur = (size_t volatile *)pvTest;
|
---|
127 | size_t volatile *puEnd = puCur + cbTestSet / sizeof(size_t);
|
---|
128 | while (puCur != puEnd)
|
---|
129 | u += *puCur++;
|
---|
130 | #else
|
---|
131 | ASMCompilerBarrier(); /* paranoia */
|
---|
132 | void *pvFound = memchr(pvTest, (i & 127) + 1, cbTestSet);
|
---|
133 | RTTESTI_CHECK(pvFound == NULL);
|
---|
134 | #endif
|
---|
135 | }
|
---|
136 | uint64_t const cNsElapsedRead = RTTimeNanoTS() - u64StartTS;
|
---|
137 | cMBPerSec = (uint64_t)( ((uint64_t)cIterations * cbTestSet) /* bytes */
|
---|
138 | / ((long double)cNsElapsedRead / RT_NS_1SEC_64) /* seconds */
|
---|
139 | / _1M /* MB */ );
|
---|
140 | RTTestIValueF(cMBPerSec, RTTESTUNIT_MEGABYTES_PER_SEC, "cpu%02u-mem%02u-read", iAllocCpu, iAccessCpu);
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * The read/write test.
|
---|
144 | */
|
---|
145 | RTTimeNanoTS(); RTThreadYield();
|
---|
146 | u64StartTS = RTTimeNanoTS();
|
---|
147 | for (uint32_t i = 0; i < cIterations; i++)
|
---|
148 | {
|
---|
149 | ASMCompilerBarrier(); /* paranoia */
|
---|
150 | memcpy(pvTest, (uint8_t *)pvTest + cbTestSet / 2, cbTestSet / 2);
|
---|
151 | }
|
---|
152 | uint64_t const cNsElapsedRW = RTTimeNanoTS() - u64StartTS;
|
---|
153 | cMBPerSec = (uint64_t)( ((uint64_t)cIterations * cbTestSet) /* bytes */
|
---|
154 | / ((long double)cNsElapsedRW / RT_NS_1SEC_64) /* seconds */
|
---|
155 | / _1M /* MB */ );
|
---|
156 | RTTestIValueF(cMBPerSec, RTTESTUNIT_MEGABYTES_PER_SEC, "cpu%02u-mem%02u-read-write", iAllocCpu, iAccessCpu);
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * Total time.
|
---|
160 | */
|
---|
161 | RTTestIValueF(cNsElapsedRead + cNsElapsedWrite + cNsElapsedRW, RTTESTUNIT_NS,
|
---|
162 | "cpu%02u-mem%02u-time", iAllocCpu, iAccessCpu);
|
---|
163 |
|
---|
164 | /* advance */
|
---|
165 | iAccessCpu = getNextCpu(iAccessCpu);
|
---|
166 | }
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * Clean up and advance to the next CPU.
|
---|
170 | */
|
---|
171 | RTMemPageFree(pvTest, cbTestSet);
|
---|
172 | iAllocCpu = getNextCpu(iAllocCpu);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | int main(int argc, char **argv)
|
---|
178 | {
|
---|
179 | RTTEST hTest;
|
---|
180 | RTEXITCODE rcExit = RTTestInitAndCreate("numa-1", &hTest);
|
---|
181 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
182 | return rcExit;
|
---|
183 | RTTestBanner(hTest);
|
---|
184 |
|
---|
185 | #if defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
|
---|
186 | /** @todo figure basic topology. */
|
---|
187 | #endif
|
---|
188 | if (argc == 2)
|
---|
189 | g_cThreadsToSkip = RTStrToUInt8(argv[1]);
|
---|
190 |
|
---|
191 | doTest(hTest);
|
---|
192 |
|
---|
193 | return RTTestSummaryAndDestroy(hTest);
|
---|
194 | }
|
---|
195 |
|
---|