xref: /dragonfly/test/stress/eatmem.c (revision 333227be)
1 /*
2  * EATMEM.C
3  *
4  * (c)Copyright 2003 Matthew Dillon <dillon@backplane.com>.  This code is
5  *    hereby placed in the public domain.
6  *
7  * This program 'eats' memory by allocating and touching it.  Make sure your
8  * datasize resource limit is sufficient for the amount of memory you specify.
9  *
10  * $DragonFly: src/test/stress/eatmem.c,v 1.1 2003/08/25 19:41:00 dillon Exp $
11  */
12 #include <sys/types.h>
13 #include <sys/mman.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 int
19 main(int ac, char **av)
20 {
21     int bytes;
22     int i;
23     char *ptr;
24 
25     if (ac == 1) {
26 	printf("eatmem MB [msec/page]\n");
27 	printf("specifying msec/page will cause eatmem to loop forever\n");
28 	exit(1);
29     }
30     bytes = strtol(av[1], NULL, 0) * 1024 * 1024;
31     ptr = malloc(bytes);
32 
33     for (;;) {
34 	for (i = 0; i < bytes; i += 4096) {
35 	    ++ptr[i];
36 	    if (av[2] && strtol(av[2], NULL, 0))
37 		usleep(1000 * strtol(av[2], NULL, 0));
38 	    if (i == 0)
39 		puts("loop0");
40 	}
41 	if (ac != 3)
42 		break;
43 	puts("loop");
44     }
45     puts("ok");
46     sleep(10);
47     puts("done");
48     exit(0);
49 }
50