Add a util memory-eater-locked to allocate non-swappable memory.
It can be useful for memory related test.
BUG=chromium:821046
TEST=manual
Change-Id: I45f790e3957b8552977608d7a7d3ce53b13431cb
Reviewed-on: https://chromium-review.googlesource.com/988892
Commit-Ready: Cheng-Yu Lee <cylee@google.com>
Tested-by: Cheng-Yu Lee <cylee@google.com>
Reviewed-by: Chung-yih Wang <cywang@chromium.org>
diff --git a/memory-eater-locked/Makefile b/memory-eater-locked/Makefile
new file mode 100644
index 0000000..1438ca0
--- /dev/null
+++ b/memory-eater-locked/Makefile
@@ -0,0 +1,12 @@
+# Copyright 2018 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+TARGET = memory-eater-locked
+SRC = memory-eater-locked.c
+
+$(TARGET): $(SRC)
+ $(CC) -o $@ $^
+
+clean:
+ $(RM) $(TARGET)
diff --git a/memory-eater-locked/memory-eater-locked.c b/memory-eater-locked/memory-eater-locked.c
new file mode 100644
index 0000000..b90802c
--- /dev/null
+++ b/memory-eater-locked/memory-eater-locked.c
@@ -0,0 +1,32 @@
+// Copyright 2018 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+int main(int argc, char** argv) {
+ char* buf;
+ size_t i, size;
+ if (argc < 2) {
+ fprintf(stderr, "Usage: ./memory-eater-locked <size in MB>\n");
+ return 1;
+ }
+ if (mlockall(MCL_FUTURE)) {
+ fprintf(stderr, "Failed to lock pages: %d\n", errno);
+ return 1;
+ }
+ size = strtol(argv[1], NULL, 10);
+ size *= 1024 * 1024;
+ printf("Allocating %zd bytes\n", size);
+ buf = (char*) malloc(size);
+ memset(buf, 'x', size);
+ printf("Done\n");
+ fflush(stdout);
+ pause();
+ return 0;
+}