-
Notifications
You must be signed in to change notification settings - Fork 1
/
maxsbrk.c
69 lines (61 loc) · 1.45 KB
/
maxsbrk.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
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
/*
* maxsbrk - Determine how much memory can be allocated and dirtied.
*
* Usage: maxsbrk <alloc> [<chunk>]
*
* alloc - Amount of memory to allocate in MiB. If specified as zero (0),
* memory is allocated until the attempts to do so fail.
*
* chunk - Allocate memory in chunks of this size in MiB. The default chunk
* size is 1MiB.
*
* If invoked as an executable containing the string "hog", continue to
* dirty memory until interrupted.
*/
int
main(int argc, char **argv) {
int megs, chunksize, allocsize, i;
char *x, *base = NULL;
intptr_t incr;
int hog = 0;
if (strstr(argv[0], "hog") != NULL)
hog = 1;
switch (argc) {
case 2:
allocsize = atoi(argv[1]);
chunksize = 1;
break;
case 3:
allocsize = atoi(argv[1]);
chunksize = atoi(argv[2]);
break;
default:
fprintf(stderr, "usage: %s 0|<alloc MiB> [<chunk MiB>]\n", argv[0]);
exit(1);
break;
}
incr = chunksize * 1024ULL * 1024ULL;
for (megs = 0 ; ; megs += chunksize) {
x = sbrk(incr);
if (x == (void *)-1) {
printf("sbrk failed\n");
break;
}
if (base == NULL)
base = x;
memset(x, 0x55, incr);
if (allocsize && megs >= allocsize)
break;
}
printf("%d MiB (%.1f GiB)\n", megs, (double)megs / 1024.0);
if (hog) {
printf("Hog mode enabled, continuing to dirty memory.\n");
for (;; ++hog)
for (i = 0, x = base; i < megs ; ++i, x += chunksize)
memset(x, hog, incr);
}
}