Setting tag mom-2.3-5
[mom.git] / leak.c
1 /*
2  * Stupid program that leaks memory at a configurable rate, to test swapmon
3  *
4  * Mark Huang <mlhuang@cs.princeton.edu>
5  * Copyright (C) 2006 The Trustees of Princeton University
6  *
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <getopt.h>
13
14 int
15 main(int argc, char *argv[])
16 {
17         int rate = 16;
18         int size = 1;
19         int leaked;
20
21         for (;;) {
22                 int c, option_index = 0;
23                 static struct option long_options[] = {
24                         { "rate", required_argument, NULL, 'r' },
25                         { "size", optional_argument, NULL, 's' },
26                         { "help", no_argument, NULL, 'h' },
27                         { 0, 0, 0, 0 }
28                 };
29
30                 c = getopt_long(argc, argv, "r:h:s", long_options, &option_index);
31                 if (c == -1)
32                         break;
33
34                 switch (c) {
35                 case 'r':
36                         rate = atoi(optarg);
37                         break;
38                 case 's':
39                         size = atoi(optarg);
40                         break;
41                 case 'h':
42                 default:
43                         fprintf(stderr, "Usage: %s [OPTION]...\n", argv[0]);
44                         fprintf(stderr, "\t-r, --rate=MiB/sec\tRate to leak memory in MiB/sec\n");
45                         fprintf(stderr, "\t-s, --size=MiB\tGrow to size and wait.\n");
46                         return 0;
47                 }
48         }
49
50         leaked = 0;
51         for (;;) {
52                 int i, bufsize = rate * 1024 * 1024;
53                 char *buf = malloc(bufsize);
54                 if (buf && (leaked <= size)) {
55                         /* Touch every page in the buffer */
56                         for (i = 0; i < bufsize; i += 4096)
57                                 buf[i] = 1;
58                         leaked += rate;
59                         printf("\r%d MiB", leaked);
60                         fflush(stdout);
61                 }
62                 sleep(1);
63         }
64
65         return 0;
66 }