Add proper per-file copyright notices/licenses and top-level license.
[bluesky.git] / microbench / lockmem.c
1 /* A very simple program to lock a specified amount of memory (in megabytes)
2  * into memory.  Used to decrease the available amount of free memory on a
3  * system for benchmarking purposes. */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/mman.h>
8 #include <unistd.h>
9
10 int main(int argc, char *argv[])
11 {
12     if (argc != 2) {
13         fprintf(stderr,
14                 "Usage: %s <amount in MiB>\n"
15                 "Locks the specified number of megabytes of RAM.\n",
16                 argv[0]);
17         return 1;
18     }
19
20     size_t bufsize = atoi(argv[1]) << 20;
21     char *buf = malloc(bufsize);
22     if (buf == NULL) {
23         fprintf(stderr, "Unable to allocate buffer: %m\n");
24         return 1;
25     }
26
27     if (mlock(buf, bufsize) < 0) {
28         fprintf(stderr, "Unable to lock memory: %m\n");
29         return 1;
30     }
31
32     /* Infinite loop; simply wait for the program to be killed. */
33     while (1) {
34         sleep(3600);
35     }
36
37     return 0;
38 }