From 1d1d7431b759d5027929362bdc668ccd64db6753 Mon Sep 17 00:00:00 2001 From: Michael Vrable Date: Mon, 18 Oct 2010 13:03:15 -0700 Subject: [PATCH] Small utility to use free memory in a system. For benchmarking, to take memory away from the page cache. --- microbench/CMakeLists.txt | 2 ++ microbench/lockmem.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 microbench/lockmem.c diff --git a/microbench/CMakeLists.txt b/microbench/CMakeLists.txt index 368517e..deadde4 100644 --- a/microbench/CMakeLists.txt +++ b/microbench/CMakeLists.txt @@ -7,4 +7,6 @@ target_link_libraries(readbench pthread rt) add_executable(statbench statbench.c) target_link_libraries(statbench pthread rt) +add_executable(lockmem lockmem.c) + set(CMAKE_C_FLAGS "-Wall -std=gnu99 ${CMAKE_C_FLAGS}") diff --git a/microbench/lockmem.c b/microbench/lockmem.c new file mode 100644 index 0000000..c0ea9d2 --- /dev/null +++ b/microbench/lockmem.c @@ -0,0 +1,38 @@ +/* A very simple program to lock a specified amount of memory (in megabytes) + * into memory. Used to decrease the available amount of free memory on a + * system for benchmarking purposes. */ + +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + if (argc != 2) { + fprintf(stderr, + "Usage: %s \n" + "Locks the specified number of megabytes of RAM.\n", + argv[0]); + return 1; + } + + size_t bufsize = atoi(argv[1]) << 20; + char *buf = malloc(bufsize); + if (buf == NULL) { + fprintf(stderr, "Unable to allocate buffer: %m\n"); + return 1; + } + + if (mlock(buf, bufsize) < 0) { + fprintf(stderr, "Unable to lock memory: %m\n"); + return 1; + } + + /* Infinite loop; simply wait for the program to be killed. */ + while (1) { + sleep(3600); + } + + return 0; +} -- 2.20.1