-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysdrain.cpp
544 lines (462 loc) · 13.8 KB
/
sysdrain.cpp
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/* sysdrain - Drain CPU and RAM for fun and profit
* Copyright (C) 2014 Erik Edlund <erik.edlund@32767.se>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#if defined(_WIN32) || defined(_WIN64) || defined(__WINRT__)
#define ENV_WINDOWS
#ifdef _MSC_VER
#define ENV_WINDOWS_MSC
#endif
#include <sdkddkver.h>
#else
#define ENV_UNIX
#endif
#include <algorithm>
#include <list>
#include <mutex>
#include <new>
#include <random>
#include <thread>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <time.h>
#include <fcntl.h>
#include <inttypes.h>
#include <unistd.h>
#ifdef ENV_UNIX
#include <sys/stat.h>
#include <sys/sysinfo.h>
#else
#include <windows.h>
#endif
#define SYSDRAIN_MINRANDSHIFT 10
#define SYSDRAIN_MAXRANDSHIFT 22
#define SYSDRAIN_OPTIONS "hves:m:M:t:"
static std::size_t g_verbosity = 0;
static std::size_t g_werror = 0;
static std::mutex g_print_mutex;
static std::mutex g_rand_mutex;
static int rand_uniform_range(int min, int max)
{
static std::random_device rd;
static std::mt19937 mt(rd());
std::lock_guard<std::mutex> lock(g_rand_mutex);
std::uniform_int_distribution<int> dist(min, max);
return dist(mt);
}
#define do_error(Exit, CanExit, ...) \
do { \
g_print_mutex.lock(); \
std::fprintf(stderr, "error: "); \
std::fprintf(stderr, __VA_ARGS__); \
std::fprintf(stderr, " (%s:%d)", __FILE__, __LINE__); \
std::fprintf(stderr, "\n"); \
g_print_mutex.unlock(); \
if (CanExit) \
Exit; \
} while (0)
#define error(...) do_error(exit(EXIT_FAILURE), 1, __VA_ARGS__)
#define do_warning(Exit, CanExit, ...) \
do { \
g_print_mutex.lock(); \
std::fprintf(stderr, "warning: "); \
std::fprintf(stderr, __VA_ARGS__); \
std::fprintf(stderr, " (%s:%d)", __FILE__, __LINE__); \
std::fprintf(stderr, "\n"); \
g_print_mutex.unlock(); \
if (g_werror) \
do_error(Exit, CanExit, "warnings treated as errors"); \
} while (0)
#define warning(...) do_warning(exit(EXIT_FAILURE), 1, __VA_ARGS__)
enum {
VERBOSITY_0 = 0,
VERBOSITY_1,
VERBOSITY_2
};
#define message(Level, ...) \
do { \
if (g_verbosity >= Level) { \
g_print_mutex.lock(); \
std::fprintf(stdout, __VA_ARGS__); \
std::fprintf(stdout, "\n"); \
g_print_mutex.unlock(); \
} \
} while (0)
#define opt_stoi(Opt, Arg, Dest) \
do { \
try { \
Dest = std::stoi(Arg); \
} \
catch (std::exception e) { \
error("arg -%c: %s (%c=%s)", Opt, e.what(), Opt, Arg); \
} \
} while (0)
static inline void* memrfrob(void *s, size_t n)
{
auto p = reinterpret_cast<char*>(s);
while (n-- > 0)
*p++ ^= rand_uniform_range(0, 255);
return s;
}
struct sysdrain {
/* Number of allocated pointer slots. */
std::uint64_t d_slots;
/* Pointers to allocated memory. */
char** d_ptrslots;
/* Size of pointers in %d_ptrslots. */
std::uint64_t* d_szslots;
/* The last occupied index of %d_ptrslots. */
std::uint64_t d_end;
/* Total size of allocated memory. */
std::uint64_t d_memsz;
/* Local %so_targetsz. */
std::uint64_t d_lcl_targetsz;
/* Local %so_ceilsz. */
std::uint64_t d_lcl_ceilsz;
/* Local %so_floorsz. */
std::uint64_t d_lcl_floorsz;
};
struct sysdrainoptions {
/* Random seed. */
unsigned int so_seed;
/* Number of threads to spawn. */
std::uint32_t so_threads;
/* User requested drain percentage. */
std::uint64_t so_targetpercent;
/* Target size based on given %so_targetpercent. */
std::uint64_t so_targetsz;
/* Approx. max allocated memory. */
std::uint64_t so_ceilsz;
/* Approx. min allocated memory. */
std::uint64_t so_floorsz;
};
typedef void (*drain_task)(struct sysdrain&);
static void drain_task_memset(struct sysdrain& drain, int v)
{
message(VERBOSITY_2, "drain_task_memset");
std::uint64_t slot = rand_uniform_range(0, drain.d_end);
std::uint64_t slotsz = drain.d_szslots[slot];
std::memset(drain.d_ptrslots[slot], v, slotsz);
}
static void drain_task_memset0(struct sysdrain& drain)
{
message(VERBOSITY_2, "drain_task_memset0");
drain_task_memset(drain, 0);
}
static void drain_task_memsetX(struct sysdrain& drain)
{
message(VERBOSITY_2, "drain_task_memsetX");
drain_task_memset(drain, rand_uniform_range(0, 255));
}
#ifdef ENV_UNIX
static void drain_task_read(struct sysdrain& drain, const char* path)
{
int rdfd = ::open(path, O_RDONLY, 0);
if (rdfd < 0) {
error("failed to open path \"%s\": %s", path, ::strerror(errno));
}
std::uint64_t slot = rand_uniform_range(0, drain.d_end);
std::uint64_t slotsz = drain.d_szslots[slot];
if (::read(rdfd, drain.d_ptrslots[slot], slotsz) < 0) {
error("failed to read \"%s\": %s", path, ::strerror(errno));
}
::close(rdfd);
}
static void drain_task_read_devurandom(struct sysdrain& drain)
{
message(VERBOSITY_2, "drain_task_read_devurandom");
drain_task_read(drain, "/dev/urandom");
}
static void drain_task_read_devzero(struct sysdrain& drain)
{
message(VERBOSITY_2, "drain_task_read_devzero");
drain_task_read(drain, "/dev/zero");
}
#endif
static void drain_task_copy(struct sysdrain& drain)
{
message(VERBOSITY_2, "drain_task_copy");
std::uint64_t slot_a;
std::uint64_t slot_b;
do {
slot_a = rand_uniform_range(0, drain.d_end);
slot_b = rand_uniform_range(0, drain.d_end);
} while (slot_a == slot_b);
std::uint64_t slot_asz = drain.d_szslots[slot_a];
std::uint64_t slot_bsz = drain.d_szslots[slot_b];
std::uint64_t fromsz = std::min(slot_asz, slot_bsz);
std::uint64_t tosz = std::max(slot_asz, slot_bsz);
char* from = drain.d_ptrslots[slot_asz < slot_bsz ? slot_a : slot_b];
char* to = drain.d_ptrslots[slot_asz < slot_bsz ? slot_b : slot_a];
for (std::uint64_t offset = 0; offset < tosz; offset += fromsz) {
memcpy(to + offset, from, fromsz);
}
}
static void drain_task_memrfrob(struct sysdrain& drain)
{
message(VERBOSITY_2, "drain_task_memrfrob");
std::uint64_t slot = rand_uniform_range(0, drain.d_end);
std::uint64_t slotsz = drain.d_szslots[slot];
memrfrob(drain.d_ptrslots[slot], slotsz);
}
static std::uint32_t nthreads(const struct sysdrainoptions& sdopts)
{
if (sdopts.so_threads)
return sdopts.so_threads;
else {
std::uint32_t threads = std::thread::hardware_concurrency();
if (!threads) {
warning("failed to get hardware concurrency, using 1 thread");
return 1;
} else
return threads;
}
}
static void prepare(struct sysdrainoptions& sdopts)
{
sdopts.so_threads = nthreads(sdopts);
auto print_percentage = sdopts.so_targetsz == 0;
auto free_ram = 0ULL;
#ifdef ENV_UNIX
struct sysinfo sysi;
::sysinfo(&sysi);
free_ram = sysi.freeram;
#else
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
::GlobalMemoryStatusEx(&status);
free_ram = status.ullTotalPhys;
#endif
message(VERBOSITY_1, "\n");
message(VERBOSITY_1, "free ram: %llu", free_ram);
if (sdopts.so_targetsz) {
sdopts.so_targetsz = free_ram - sdopts.so_targetsz;
std::uint64_t diff = (std::uint64_t)(sdopts.so_targetsz / 10.0);
sdopts.so_ceilsz = sdopts.so_targetsz + diff;
sdopts.so_floorsz = sdopts.so_targetsz - diff;
} else {
double chfactor = sdopts.so_targetpercent / 100.0;
sdopts.so_targetsz = (std::uint64_t)(free_ram * chfactor);
sdopts.so_ceilsz = (std::uint64_t)(free_ram * (chfactor + 0.1));
sdopts.so_floorsz = (std::uint64_t)(free_ram * (chfactor - 0.1));
}
if (print_percentage) {
message(VERBOSITY_1, "percent request: %" PRIu64 "%%",
sdopts.so_targetpercent);
}
message(VERBOSITY_1, "drain target: %" PRIu64, sdopts.so_targetsz);
message(VERBOSITY_1, "drain ceiling: %" PRIu64, sdopts.so_ceilsz);
message(VERBOSITY_1, "drain floor: %" PRIu64, sdopts.so_floorsz);
}
static void handle_more_memory(struct sysdrain& drain,
const struct sysdrainoptions& sdopts, std::uint64_t steps)
{
(void)sdopts;
while (drain.d_memsz < drain.d_lcl_ceilsz && steps > 0) {
if (drain.d_end + 1 > drain.d_slots) {
error("out of slots");
}
std::uint64_t new_slot = drain.d_end + 1;
std::uint64_t new_slotsz = 1 << rand_uniform_range(
SYSDRAIN_MINRANDSHIFT,
SYSDRAIN_MAXRANDSHIFT
);
try {
drain.d_ptrslots[new_slot] = new char[new_slotsz];
drain.d_szslots[new_slot] = new_slotsz;
drain.d_end += 1;
drain.d_memsz += new_slotsz;
message(VERBOSITY_2, "slot %" PRIu64 " filled with %" PRIu64 " bytes",
new_slot, new_slotsz);
}
catch (std::exception& e) {
message(VERBOSITY_1, "failed to satisfy request for %" PRIu64 " bytes"
" for slot %" PRIu64 " (%s)", new_slotsz, new_slot, e.what());
}
steps--;
}
}
static void handle_less_memory(struct sysdrain& drain,
const struct sysdrainoptions& sdopts, std::uint64_t steps)
{
(void)sdopts;
while (drain.d_lcl_floorsz < drain.d_memsz && steps > 0) {
std::uint64_t slot = rand_uniform_range(0, drain.d_end);
std::uint64_t slotsz = drain.d_szslots[slot];
delete [] drain.d_ptrslots[slot];
drain.d_ptrslots[slot] = drain.d_ptrslots[drain.d_end];
drain.d_ptrslots[drain.d_end] = NULL;
drain.d_szslots[slot] = drain.d_szslots[drain.d_end];
drain.d_szslots[drain.d_end] = 0;
drain.d_end -= 1;
drain.d_memsz -= slotsz;
message(VERBOSITY_2, "%" PRIu64 " bytes freed from slot %" PRIu64,
slotsz, slot);
steps--;
}
}
static void handle_memory(struct sysdrain& drain,
const struct sysdrainoptions& sdopts)
{
int situation_nominal = (
drain.d_memsz >= drain.d_lcl_floorsz &&
drain.d_memsz <= drain.d_lcl_ceilsz
);
if (!situation_nominal || rand_uniform_range(0, 4) == 0) {
if (drain.d_memsz < drain.d_lcl_ceilsz)
handle_more_memory(drain, sdopts, 64);
else
handle_less_memory(drain, sdopts, 64);
}
message(VERBOSITY_2, "drained bytes: %" PRIu64, drain.d_memsz);
}
static void handle_workload(struct sysdrain& drain,
const struct sysdrainoptions& sdopts)
{
drain_task drain_tasks[] = {
#ifdef ENV_UNIX
drain_task_memset0,
drain_task_memsetX,
drain_task_read_devurandom,
drain_task_read_devzero,
drain_task_copy,
drain_task_memrfrob
#else
drain_task_memset0,
drain_task_memsetX,
drain_task_copy,
drain_task_memrfrob
#endif
};
(void)sdopts;
auto ndrain_tasks = sizeof(drain_tasks) / sizeof(drain_task);
auto tasknr = rand_uniform_range(0, ndrain_tasks - 1);
drain_task task = drain_tasks[tasknr];
task(drain);
}
static void drain(const struct sysdrainoptions& sdopts)
{
struct sysdrain drain;
std::memset(&drain, 0, sizeof(drain));
drain.d_lcl_targetsz = sdopts.so_targetsz / sdopts.so_threads;
drain.d_lcl_ceilsz = sdopts.so_ceilsz / sdopts.so_threads;
drain.d_lcl_floorsz = sdopts.so_floorsz / sdopts.so_threads;
message(VERBOSITY_1, "\n[drainaddr=%p] thread spawned\n"
"\tdrain target: %" PRIu64 "\n"
"\tdrain ceiling: %" PRIu64 "\n"
"\tdrain floor: %" PRIu64 "\n",
(void*)&drain,
drain.d_lcl_targetsz,
drain.d_lcl_ceilsz,
drain.d_lcl_floorsz
);
drain.d_slots = static_cast<std::uint64_t>((
drain.d_lcl_ceilsz / (1 << SYSDRAIN_MINRANDSHIFT)
) + 1);
drain.d_ptrslots = new char*[drain.d_slots];
drain.d_szslots = new std::uint64_t[drain.d_slots];
if (!drain.d_ptrslots || !drain.d_szslots) {
do_error(pthread_exit(NULL), 1, "failed to allocate slots");
}
drain.d_end = 0;
std::srand(sdopts.so_seed);
for (;;) {
handle_memory(drain, sdopts);
handle_workload(drain, sdopts);
}
}
static void usage(const char* const exe, std::FILE* const dest)
{
std::fprintf(dest,
"\nUsage: %s [-%s]\n"
"\nexample: %s\n\n"
" -h print this message (to stdout) and quit\n"
" -v be more verbose\n"
" -e turn warnings into errors\n"
" -s <int> random seed\n"
" -m <int> percent of free RAM to drain (m >= 10, m <= 90)\n"
" -M <int> amount of RAM that should be left free\n"
" -t <int> number of threads to use\n"
"\n", exe, SYSDRAIN_OPTIONS, exe);
std::exit(dest == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
int main(int argc, char* argv[])
{
if (argc == 0)
usage("sysdrain", stderr);
struct sysdrainoptions sdopts;
std::memset(&sdopts, 0, sizeof(sdopts));
sdopts.so_seed = ::time(NULL);
sdopts.so_targetpercent = 70;
std::uint64_t optarglen;
int option;
while ((option = ::getopt(argc, argv, SYSDRAIN_OPTIONS)) != EOF) {
switch (option) {
case 'h':
usage(argv[0], stdout);
break;
case 'v':
g_verbosity++;
break;
case 'e':
g_werror++;
break;
case 's':
opt_stoi(option, optarg, sdopts.so_seed);
break;
case 'm':
optarglen = std::strlen(optarg);
if (optarglen > 2 && optarg[optarglen - 1] == '%')
optarg[optarglen - 1] = '\0';
opt_stoi(option, optarg, sdopts.so_targetpercent);
if (sdopts.so_targetpercent < 10 || sdopts.so_targetpercent > 90)
error("invalid memory drain request: %" PRIu64 "%%\n",
sdopts.so_targetpercent);
break;
case 'M':
opt_stoi(option, optarg, sdopts.so_targetsz);
if (sdopts.so_targetsz && sdopts.so_targetsz < 33554432)
warning("leaving very little free RAM");
break;
case 't':
opt_stoi(option, optarg, sdopts.so_threads);
break;
default:
/* Ignore it.
*/
warning("unrecognized option -%c", option);
break;
}
}
prepare(sdopts);
try {
std::list<std::thread*> threads;
for (std::uint32_t i = 0; i < sdopts.so_threads; ++i)
threads.push_back(new std::thread(drain, sdopts));
for (auto it : threads)
it->join();
}
catch (std::exception& e) {
error("%s", e.what());
}
std::exit(EXIT_SUCCESS);
}