-
Notifications
You must be signed in to change notification settings - Fork 0
/
eatcore.c
128 lines (119 loc) · 2.79 KB
/
eatcore.c
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
#include <stdio.h>
#include <stdlib.h>
#ifdef LINUX
#include <bsd/stdlib.h>
#endif
#include <unistd.h>
#include <strings.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#define logger(fmt, ...) fprintf(stderr, "%ld eatcore: " fmt "\n", (long) time(NULL), ## __VA_ARGS__)
#define VERSION "0.2.1"
struct context {
char **argv;
int argc;
time_t interval;
size_t increment;
size_t size;
void *p;
size_t max_increments;
size_t current_increments;
int exit_finished;
int random;
};
void set_defaults(struct context *ctx, int argc, char **argv)
{
ctx->argc = argc;
ctx->argv = argv;
ctx->interval = 60;
ctx->increment = 150 * 1048576;
ctx->size = 0;
ctx->p = NULL;
ctx->max_increments = 10;
ctx->current_increments = 0;
ctx->exit_finished = 0;
ctx->random = 0;
}
void parse_commandline(struct context *ctx)
{
int ch;
while ((ch = getopt(ctx->argc, ctx->argv, "hi:n:rs:xv")) != -1) {
switch (ch) {
case 's':
ctx->increment = (size_t) atoi(optarg) * 1048576;
break;
case 'i':
ctx->interval = (time_t) atoi(optarg);
break;
case 'n':
ctx->max_increments = (size_t) atoi(optarg);
break;
case 'r':
ctx->random = 1;
break;
case 'x':
ctx->exit_finished = 1;
break;
case 'v':
puts("version " VERSION);
exit(0);
case 'h':
default:
logger
("usage: eatcore [-i interval_in_seconds] [-s increment_in_bytes] [-n max_increments] [-x] [-r]");
exit(1);
}
}
ctx->argc -= optind;
ctx->argv += optind;
}
void interval(struct context *ctx)
{
if (ctx->exit_finished && ctx->current_increments >= ctx->max_increments) {
logger("Fully allocated. Exiting.");
exit(0);
} else {
logger("Sleeping...");
sleep(ctx->interval);
}
}
void increment(struct context *ctx)
{
size_t newsize = ctx->size + ctx->increment;
ctx->p = realloc(ctx->p, newsize);
if (ctx->p) {
ctx->size = newsize;
logger("Stomach fed to %zuMB. Touching pages...", ctx->size / 1048576);
if (ctx->random) {
arc4random_buf(ctx->p, ctx->size);
} else {
bzero(ctx->p, ctx->size);
}
logger("Finished touching pages. Sleeping %d seconds.",
(int) ctx->interval);
ctx->current_increments++;
} else {
fputs("Allocation failed, aborting.", stderr);
exit(2);
}
}
void eatcore(struct context *ctx)
{
while (1) {
if (ctx->current_increments < ctx->max_increments)
increment(ctx);
interval(ctx);
}
}
int main(int argc, char **argv)
{
struct context ctx;
set_defaults(&ctx, argc, argv);
parse_commandline(&ctx);
logger
("Starting with interval=%d, increment=%d, max_increments=%d, random=%d",
(int) ctx.interval, (int) ctx.increment, (int) ctx.max_increments,
ctx.random);
eatcore(&ctx);
}