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