-
Notifications
You must be signed in to change notification settings - Fork 0
/
memstress.c
393 lines (342 loc) · 9.93 KB
/
memstress.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
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
/*-
* Copyright (c) 2022 Tom Jones <thj@freebsd.org>
* Copyright (c) 2022 Klara Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <err.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <vm/vm_param.h>
#define PAGESZ 4096
#include "libroute.h"
void usage(void) __dead2;
void routestress(bool, bool, int, int, int, int);
void histstore(uint64_t *, uint64_t *, uint64_t);
void histprint(const char **, uint64_t *);
bool vm_veto(int);
void freepages(char *, int, int);
char *allocate_pages(int, int , int , int);
static int veto_count;
static int failure_count;
void
usage(void)
{
printf("usage: memstress [Hhmnrvwxyz]\n");
printf(" memstress: apply memory pressure either through\n");
printf(" memory allocations or route creation\n");
printf(" -H display a route creation time histogram\n");
printf(" -h print help (this message)\n");
printf(" -m perform memory allocation test\n");
printf(" -n allocation size in pages for memory tests\n");
printf(" (default: vm.stats.vm.page_count)\n");
printf(" -r perform route addition tests\n");
printf(" -v veto size in pages (default: vm.v_free_target)\n");
printf(" Size in pages that would veto an allocation/route addition\n");
printf(" -w wait between allocation/addition before freeing\n");
printf(" -x number of addresses in x part of subnet\n");
printf(" -y number of addresses in y part of subnet\n");
printf(" -z number of addresses in z part of subnet\n");
printf(" \n");
printf(" Route addition tests will add routes from 10.z.y.x addresses via 1.0.0.1\n");
printf(" this needs to be configured on an interface or the test will fail immediately\n");
exit(0);
}
int
main(int argc, char **argv)
{
const char *errstr;
char *pagetable = NULL;
bool Hflag, mflag, rflag;
bool work, wait;
int ch, err;
int page_count, free_target;
int x, y, z;
size_t len;
mflag = false;
rflag = false;
work = true;
wait = false;
x = y = z = 100;
veto_count = 0;
failure_count = 0;
/* reuse free_target to find v_free_count , we don't use it for the veto */
len = sizeof(free_target);
err = sysctlbyname("vm.stats.vm.v_free_count", &free_target, &len, NULL, 0);
if (err == -1) {
perror("getting vm.stats.vm.v_free_count");
exit(1);
}
printf("v_free_count at start is %d pages (%ld MB)\n", free_target, ((uint64_t)free_target*PAGESZ)/1048576);
/* reuse free_target to find v_free_severe, we don't use it for the veto */
len = sizeof(free_target);
err = sysctlbyname("vm.v_free_severe", &free_target, &len, NULL, 0);
if (err == -1) {
perror("getting vm.v_free_severe");
exit(1);
}
printf("v_free_severe is %d pages (%ld MB)\n", free_target, ((uint64_t)free_target*PAGESZ)/1048576);
len = sizeof(free_target);
err = sysctlbyname("vm.v_free_target", &free_target, &len, NULL, 0);
if (err == -1) {
perror("getting vm.v_free_target");
exit(1);
}
printf("free_target is %d pages (%ld MB)\n", free_target, ((uint64_t)free_target*PAGESZ)/1048576);
len = sizeof(page_count);
err = sysctlbyname("vm.stats.vm.v_page_count", &page_count, &len, NULL, 0);
if (err == -1) {
perror("getting vm.stats.vm.v_page_count");
exit(1);
}
printf("page_count is %d pages (%ld MB)\n", page_count, ((uint64_t)free_target*PAGESZ)/1048576);
while ((ch = getopt(argc, argv, "Hhmn:rv:wx:y:z:")) != -1) {
switch (ch) {
case 'H':
Hflag = true;
break;
case 'h':
usage();
case 'm':
mflag = true;
rflag = false;
break;
case 'n':
page_count = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL) {
perror("reading page count");
exit(0);
}
break;
case 'r':
rflag = true;
mflag = false;
break;
case 'v':
free_target = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL) {
perror("reading veto target");
exit(0);
}
break;
case 'w':
wait = true;
break;
case 'x':
x = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL) {
perror("reading x");
exit(0);
}
if (x > 254) {
printf("ERROR: x can't be larger than 254 (%d)\n", z);
exit(0);
}
break;
case 'y':
y = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL) {
perror("reading y");
exit(0);
}
if (y > 254) {
printf("ERROR: y can't be larger than 254 (%d)\n", z);
exit(0);
}
break;
case 'z':
z = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL) {
perror("reading z");
exit(0);
}
if (z > 254) {
printf("ERROR: z can't be larger than 254 (%d)\n", z);
exit(0);
}
break;
default:
printf("no idea what %c is for\n", ch);
exit(1);
}
}
if (!mflag && !rflag)
usage();
if (mflag) {
printf("allocating %d pages ( %ld MB) and touching them\n",
page_count, ((uint64_t)page_count*PAGESZ)/1048576);
pagetable = allocate_pages(PAGESZ, page_count, work, free_target);
if (wait) {
printf("Press enter to trigger memory reclaim\n");
getchar();
}
/* Free mmap'd pages */
printf("freeing allocated pages\n");
freepages(pagetable, PAGESZ, page_count);
} else if (rflag) {
routestress(wait, Hflag, free_target, x, y, z);
}
printf("would have vetoed %d times (%d failures)\n", veto_count, failure_count);
}
char *
allocate_pages(int pagesize, int count, int work, int veto)
{
char *pagetable = NULL;
char *page = NULL;
pagetable = calloc(count, sizeof(char *));
if (pagetable == NULL) {
perror("allocate pages");
return NULL;
}
page = pagetable;
for (int i = 0; i < count; page++, i++) {
vm_veto(veto);
page = mmap(NULL, pagesize, PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0);
if (page == MAP_FAILED) {
perror("allocation failed for ...");
failure_count++;
continue;
}
if (work)
memset(page++, 44, pagesize);
}
return pagetable;
}
void
freepages(char *page, int pagesize, int count)
{
for (int i = 0; i < count; page++, i++) {
if (page != NULL || page != MAP_FAILED)
munmap(page, pagesize);
}
}
void
routestress(bool wait, bool printhist, int veto, int xroutes, int yroutes, int zroutes)
{
rt_handle *handle;
struct sockaddr *sa_dest, *sa_gateway;
struct timespec start, end, elapsed;
char buf[] = "xxx.xxx.xxx.xxx";
int error;
#define ONEMS 1000000
const char *bucketlabels[11]
= { "1ms", "2ms", "3ms", "4ms", "5ms", "6ms", "7ms", "8ms", "9ms", "10ms", "100ms" };
uint64_t bucketcuts[11]
= { ONEMS, 2*ONEMS, 3*ONEMS,
4*ONEMS, 5*ONEMS, 6*ONEMS, 7*ONEMS,
8*ONEMS, 9*ONEMS, 10*ONEMS, 100*ONEMS};
uint64_t buckets[11];
memset(buckets, 0, sizeof(uint64_t) * 11);
handle = libroute_open(0);
if (handle == NULL)
err(1, "libroute_open failed");
sa_gateway = str_to_sockaddr(handle, "1.0.0.1");
printf("adding %d routes in blocks of %d\n", zroutes * xroutes * yroutes, yroutes);
for (int z = 0; z < zroutes; z++) {
for (int y = 0; y < yroutes; y++) {
vm_veto(veto);
timespec_get(&start, TIME_UTC);
for (int x = 2; x < xroutes; x++) {
snprintf(buf, sizeof(buf), "10.%d.%d.%d", z, y, x);
sa_dest = str_to_sockaddr(handle, buf);
error = libroute_add(handle, sa_dest, sa_gateway);
if (error == -1) {
err(1, "Failed to add route");
failure_count++;
}
}
timespec_get(&end, TIME_UTC);
timespecsub(&end, &start, &elapsed);
if (elapsed.tv_sec > 0)
printf("it took more than 1 second to add %d routes", yroutes);
histstore(bucketcuts, buckets, (uint64_t)elapsed.tv_nsec);
}
}
if (wait) {
printf("Press enter to route flushing\n");
getchar();
}
printf("flushing created routes\n");
for (int z = 0; z < zroutes; z++) {
for (int x = 0; x < xroutes; x++) {
for (int y = 2; y < yroutes; y++) {
snprintf(buf, sizeof(buf), "10.%d.%d.%d", z, x, y);
sa_dest = str_to_sockaddr(handle, buf);
error = libroute_del(handle, sa_dest);
if (error == -1) {
err(1, "Failed to delete route");
}
}
}
}
libroute_close(handle);
if (printhist)
histprint(bucketlabels, buckets);
}
void
histstore(uint64_t *bc, uint64_t *b, uint64_t value)
{
if (value >= bc[10]) {
b[10]++;
return;
}
for (int i = 0; i < 11; i++) {
if (value < bc[i]) {
b[i]++;
break;
}
}
}
void
histprint(const char **labels, uint64_t *b)
{
int total = 0;
for (int i = 0; i < 11; i++) {
printf("%s: %lu\n", labels[i], b[i]);
total += b[i];
}
printf("%d total readings in histogram\n", total);
}
bool
vm_veto(int target)
{
size_t len;
int free_pages;
int err = 0;
free_pages = 0;
len = sizeof(free_pages);
err = sysctlbyname("vm.stats.vm.v_free_count", &free_pages, &len, NULL, 0);
if (err == -1)
perror("free_pages error");
if (free_pages >= target)
return false;
veto_count++;
return true;
}