1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
/* $Id$ */
/** @file
* Load Generator.
*/
/*
* Copyright (C) 2007-2017 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/err.h>
#include <iprt/thread.h>
#include <iprt/time.h>
#include <iprt/initterm.h>
#include <iprt/string.h>
#include <iprt/stream.h>
#include <iprt/param.h>
#include <iprt/path.h>
#include <iprt/process.h>
#include <iprt/mp.h>
#include <iprt/asm.h>
#include <iprt/getopt.h>
#include <VBox/sup.h>
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/** Whether the threads should quit or not. */
static bool volatile g_fQuit = false;
static const char *g_pszProgramName = NULL;
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
static int Error(const char *pszFormat, ...);
static void LoadGenSpin(uint64_t cNanoSeconds)
{
const uint64_t u64StartTS = RTTimeNanoTS();
do
{
for (uint32_t volatile i = 0; i < 10240 && !g_fQuit; i++)
i++;
} while (RTTimeNanoTS() - u64StartTS < cNanoSeconds && !g_fQuit);
}
static DECLCALLBACK(int) LoadGenSpinThreadFunction(RTTHREAD hThreadSelf, void *pvUser)
{
NOREF(hThreadSelf);
LoadGenSpin(*(uint64_t *)pvUser);
return VINF_SUCCESS;
}
static int LoadGenIpiInit(void)
{
/*
* Try make sure the support library is initialized...
*/
SUPR3Init(NULL);
/*
* Load the module.
*/
char szPath[RTPATH_MAX];
int rc = RTPathAppPrivateArchTop(szPath, sizeof(szPath) - sizeof("/loadgenerator.r0"));
if (RT_SUCCESS(rc))
{
strcat(szPath, "/loadgeneratorR0.r0");
void *pvImageBase;
rc = SUPR3LoadServiceModule(szPath, "loadgeneratorR0", "LoadGenR0ServiceReqHandler", &pvImageBase);
if (RT_SUCCESS(rc))
{
/* done */
}
else
Error("SUPR3LoadServiceModule(%s): %Rrc\n", szPath, rc);
}
else
Error("RTPathAppPrivateArch: %Rrc\n", rc);
return rc;
}
static void LoadGenIpi(uint64_t cNanoSeconds)
{
const uint64_t u64StartTS = RTTimeNanoTS();
do
{
int rc = SUPR3CallR0Service("loadgeneratorR0", sizeof("loadgeneratorR0") - 1,
0 /* uOperation */, 1 /* cIpis */, NULL /* pReqHdr */);
if (RT_FAILURE(rc))
{
Error("SUPR3CallR0Service: %Rrc\n", rc);
break;
}
} while (RTTimeNanoTS() - u64StartTS < cNanoSeconds && !g_fQuit);
}
static DECLCALLBACK(int) LoadGenIpiThreadFunction(RTTHREAD hThreadSelf, void *pvUser)
{
LoadGenIpi(*(uint64_t *)pvUser);
NOREF(hThreadSelf);
return VINF_SUCCESS;
}
static int Error(const char *pszFormat, ...)
{
va_list va;
RTStrmPrintf(g_pStdErr, "%s: error: ", g_pszProgramName);
va_start(va, pszFormat);
RTStrmPrintfV(g_pStdErr, pszFormat, va);
va_end(va);
return 1;
}
static int SyntaxError(const char *pszFormat, ...)
{
va_list va;
RTStrmPrintf(g_pStdErr, "%s: syntax error: ", g_pszProgramName);
va_start(va, pszFormat);
RTStrmPrintfV(g_pStdErr, pszFormat, va);
va_end(va);
return 1;
}
int main(int argc, char **argv)
{
static const struct LOADGENTYPE
{
const char *pszName;
int (*pfnInit)(void);
PFNRTTHREAD pfnThread;
} s_aLoadTypes[] =
{
{ "spin", NULL, LoadGenSpinThreadFunction },
{ "ipi", LoadGenIpiInit, LoadGenIpiThreadFunction },
};
unsigned iLoadType = 0;
static RTTHREAD s_aThreads[256];
int rc;
uint32_t cThreads = 1;
bool fScaleByCpus = false;
RTTHREADTYPE enmThreadType = RTTHREADTYPE_DEFAULT;
RTPROCPRIORITY enmProcPriority = RTPROCPRIORITY_DEFAULT;
uint64_t cNanoSeconds = UINT64_MAX;
RTR3InitExe(argc, &argv, 0);
/*
* Set program name.
*/
g_pszProgramName = RTPathFilename(argv[0]);
/*
* Parse arguments.
*/
static const RTGETOPTDEF s_aOptions[] =
{
{ "--number-of-threads", 'n', RTGETOPT_REQ_UINT32 },
{ "--timeout", 't', RTGETOPT_REQ_STRING },
{ "--thread-type", 'p', RTGETOPT_REQ_STRING },
{ "--scale-by-cpus", 'c', RTGETOPT_REQ_NOTHING },
{ "--load", 'l', RTGETOPT_REQ_STRING },
};
int ch;
RTGETOPTUNION ValueUnion;
RTGETOPTSTATE GetState;
RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0 /* fFlags */);
while ((ch = RTGetOpt(&GetState, &ValueUnion)))
{
switch (ch)
{
case 'n':
cThreads = ValueUnion.u64;
if (cThreads == 0 || cThreads > RT_ELEMENTS(s_aThreads))
return SyntaxError("Requested number of threads, %RU32, is out of range (1..%d).\n",
cThreads, RT_ELEMENTS(s_aThreads) - 1);
break;
case 't':
{
char *psz;
rc = RTStrToUInt64Ex(ValueUnion.psz, &psz, 0, &cNanoSeconds);
if (RT_FAILURE(rc))
return SyntaxError("Failed reading the alleged number '%s' (option '%s', rc=%Rrc).\n",
ValueUnion.psz, rc);
while (*psz == ' ' || *psz == '\t')
psz++;
if (*psz)
{
uint64_t u64Factor = 1;
if (!strcmp(psz, "ns"))
u64Factor = 1;
else if (!strcmp(psz, "ms"))
u64Factor = 1000;
else if (!strcmp(psz, "s"))
u64Factor = 1000000000;
else if (!strcmp(psz, "m"))
u64Factor = UINT64_C(60000000000);
else if (!strcmp(psz, "h"))
u64Factor = UINT64_C(3600000000000);
else
return SyntaxError("Unknown time suffix '%s'\n", psz);
uint64_t u64 = cNanoSeconds * u64Factor;
if (u64 < cNanoSeconds || (u64 < u64Factor && u64))
return SyntaxError("Time representation overflowed! (%RU64 * %RU64)\n",
psz, cNanoSeconds, u64Factor);
cNanoSeconds = u64;
}
break;
}
case 'p':
{
enmProcPriority = RTPROCPRIORITY_NORMAL;
uint32_t u32 = RTTHREADTYPE_INVALID;
char *psz;
rc = RTStrToUInt32Ex(ValueUnion.psz, &psz, 0, &u32);
if (RT_FAILURE(rc) || *psz)
{
if (!strcmp(ValueUnion.psz, "default"))
{
enmProcPriority = RTPROCPRIORITY_DEFAULT;
enmThreadType = RTTHREADTYPE_DEFAULT;
}
else if (!strcmp(ValueUnion.psz, "idle"))
{
enmProcPriority = RTPROCPRIORITY_LOW;
enmThreadType = RTTHREADTYPE_INFREQUENT_POLLER;
}
else if (!strcmp(ValueUnion.psz, "high"))
{
enmProcPriority = RTPROCPRIORITY_HIGH;
enmThreadType = RTTHREADTYPE_IO;
}
else
return SyntaxError("can't grok thread type '%s'\n",
ValueUnion.psz);
}
else
{
enmThreadType = (RTTHREADTYPE)u32;
if (enmThreadType <= RTTHREADTYPE_INVALID || enmThreadType >= RTTHREADTYPE_END)
return SyntaxError("thread type '%d' is out of range (%d..%d)\n",
ValueUnion.psz, RTTHREADTYPE_INVALID + 1, RTTHREADTYPE_END - 1);
}
break;
}
case 'c':
fScaleByCpus = true;
break;
case 'l':
{
for (unsigned i = 0; i < RT_ELEMENTS(s_aLoadTypes); i++)
if (!strcmp(s_aLoadTypes[i].pszName, ValueUnion.psz))
{
ValueUnion.psz = NULL;
iLoadType = i;
break;
}
if (ValueUnion.psz)
return SyntaxError("Unknown load type '%s'.\n", ValueUnion.psz);
break;
}
case 'h':
RTStrmPrintf(g_pStdOut,
"Usage: %s [-p|--thread-type <type>] [-t|--timeout <sec|xxx[h|m|s|ms|ns]>] \\\n"
" %*s [-n|--number-of-threads <threads>] [-l|--load <loadtype>]\n"
"\n"
"Load types: spin, ipi.\n"
,
g_pszProgramName, strlen(g_pszProgramName), "");
return 1;
case 'V':
RTPrintf("$Revision$\n");
return 0;
case VINF_GETOPT_NOT_OPTION:
return SyntaxError("Unknown argument #%d: '%s'\n", GetState.iNext-1, ValueUnion.psz);
default:
return RTGetOptPrintError(ch, &ValueUnion);
}
}
/*
* Scale thread count by host cpu count.
*/
if (fScaleByCpus)
{
const unsigned cCpus = RTMpGetOnlineCount();
if (cCpus * cThreads > RT_ELEMENTS(s_aThreads))
return SyntaxError("Requested number of threads, %RU32, is out of range (1..%d) when scaled by %d.\n",
cThreads, RT_ELEMENTS(s_aThreads) - 1, cCpus);
cThreads *= cCpus;
}
/*
* Modify process and thread priority? (ignore failure)
*/
if (enmProcPriority != RTPROCPRIORITY_DEFAULT)
RTProcSetPriority(enmProcPriority);
if (enmThreadType != RTTHREADTYPE_DEFAULT)
RTThreadSetType(RTThreadSelf(), enmThreadType);
/*
* Load type specific init.
*/
if (s_aLoadTypes[iLoadType].pfnInit)
{
rc = s_aLoadTypes[iLoadType].pfnInit();
if (RT_FAILURE(rc))
return 1;
}
/*
* Start threads.
*/
for (unsigned i = 1; i < cThreads; i++)
{
s_aThreads[i] = NIL_RTTHREAD;
rc = RTThreadCreate(&s_aThreads[i], s_aLoadTypes[iLoadType].pfnThread,
&cNanoSeconds, 128*1024, enmThreadType, RTTHREADFLAGS_WAITABLE, "spinner");
if (RT_FAILURE(rc))
{
ASMAtomicXchgBool(&g_fQuit, true);
RTStrmPrintf(g_pStdErr, "%s: failed to create thread #%d, rc=%Rrc\n", g_pszProgramName, i, rc);
while (i-- > 1)
RTThreadWait(s_aThreads[i], 1500, NULL);
return 1;
}
}
/* our selves */
s_aLoadTypes[iLoadType].pfnThread(RTThreadSelf(), &cNanoSeconds);
/*
* Wait for threads.
*/
ASMAtomicXchgBool(&g_fQuit, true);
for (unsigned i = 1; i < cThreads; i++)
RTThreadWait(s_aThreads[i], 1500, NULL);
return 0;
}
|