add stupid test program that leaks memory at a configurable rate
[mom.git] / leak.c
diff --git a/leak.c b/leak.c
new file mode 100644 (file)
index 0000000..271d41b
--- /dev/null
+++ b/leak.c
@@ -0,0 +1,61 @@
+/*
+ * Stupid program that leaks memory at a configurable rate, to test swapmon
+ *
+ * Mark Huang <mlhuang@cs.princeton.edu>
+ * Copyright (C) 2006 The Trustees of Princeton University
+ *
+ * $Id$
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <getopt.h>
+
+int
+main(int argc, char *argv[])
+{
+       int rate = 16;
+       int leaked;
+
+       for (;;) {
+               int c, option_index = 0;
+               static struct option long_options[] = {
+                       { "rate", required_argument, NULL, 'r' },
+                       { "help", no_argument, NULL, 'h' },
+                       { 0, 0, 0, 0 }
+               };
+
+               c = getopt_long(argc, argv, "r:h", long_options, &option_index);
+               if (c == -1)
+                       break;
+
+               switch (c) {
+               case 'r':
+                       rate = atoi(optarg);
+                       break;
+               case 'h':
+               default:
+                       fprintf(stderr, "Usage: %s [OPTION]...\n", argv[0]);
+                       fprintf(stderr, "\t-r, --rate=MiB/sec\tRate to leak memory in MiB/sec\n");
+                       return 0;
+               }
+       }
+
+       leaked = 0;
+       for (;;) {
+               int i, bufsize = rate * 1024 * 1024;
+               char *buf = malloc(bufsize);
+               if (buf) {
+                       /* Touch every page in the buffer */
+                       for (i = 0; i < bufsize; i += 4096)
+                               buf[i] = 1;
+                       leaked += rate;
+                       printf("\r%d MiB", leaked);
+                       fflush(stdout);
+               }
+               sleep(1);
+       }
+
+       return 0;
+}