Initted default limits before connecting to NM for first time in case NM except'ns.
[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 leaked;
20
21         for (;;) {
22                 int c, option_index = 0;
23                 static struct option long_options[] = {
24                         { "rate", required_argument, NULL, 'r' },
25                         { "help", no_argument, NULL, 'h' },
26                         { 0, 0, 0, 0 }
27                 };
28
29                 c = getopt_long(argc, argv, "r:h", long_options, &option_index);
30                 if (c == -1)
31                         break;
32
33                 switch (c) {
34                 case 'r':
35                         rate = atoi(optarg);
36                         break;
37                 case 'h':
38                 default:
39                         fprintf(stderr, "Usage: %s [OPTION]...\n", argv[0]);
40                         fprintf(stderr, "\t-r, --rate=MiB/sec\tRate to leak memory in MiB/sec\n");
41                         return 0;
42                 }
43         }
44
45         leaked = 0;
46         for (;;) {
47                 int i, bufsize = rate * 1024 * 1024;
48                 char *buf = malloc(bufsize);
49                 if (buf) {
50                         /* Touch every page in the buffer */
51                         for (i = 0; i < bufsize; i += 4096)
52                                 buf[i] = 1;
53                         leaked += rate;
54                         printf("\r%d MiB", leaked);
55                         fflush(stdout);
56                 }
57                 sleep(1);
58         }
59
60         return 0;
61 }