aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2024-01-24 13:01:35 -0800
committerRob Landley <rob@landley.net>2024-01-25 01:37:07 -0600
commit77d4207dbe76c55becf7f56eb01d443eb0bfc319 (patch)
tree0c953cae3b4da1984ebc34fd94da6d8d1a836d6d
parent6b0ce0acb1f5deb7c9494c10db7854a061ffdb9b (diff)
downloadtoybox-77d4207dbe76c55becf7f56eb01d443eb0bfc319.tar.gz
memeater: consume a given amount of memory.
This seems to be a popular reinvention. There are three different variants in the Android tree, and at least another independent one (intended for Android) on github. This is the minimal intersection of them all, which will hopefully still be useful. (It's very close to the system/extras/ Android one, but assumes that the user can just run under nohup(1) rather than needing the tool to daemonize itself.) There's not much motivation for the for loop rather than a call to memset(); that was just intended as paranoia against "can something cheat me out of my dirty pages?". This still repeats every 256 bytes, though, and I don't have any concrete example of where duplicate page detection or compression kicks in, so although calloc() would not be a suitable replacement, memset() might well be.
-rw-r--r--toys/pending/memeater.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/toys/pending/memeater.c b/toys/pending/memeater.c
new file mode 100644
index 00000000..d4aa5f04
--- /dev/null
+++ b/toys/pending/memeater.c
@@ -0,0 +1,31 @@
+/* memeater.c - consume the specified amount of memory
+ *
+ * Copyright 2024 The Android Open Source Project
+
+USE_MEMEATER(NEWTOY(memeater, "<1", TOYFLAG_USR|TOYFLAG_BIN))
+
+config MEMEATER
+ bool "memeater"
+ default y
+ help
+ usage: memeater SIZE
+
+ Consume the specified amount of memory (in bytes, with optional suffix).
+*/
+
+#define FOR_memeater
+#include "toys.h"
+
+void memeater_main(void)
+{
+ long long size = atolx(*toys.optargs), i;
+ char* p;
+
+ p = xmalloc(size);
+ // Lock the physical pages.
+ if (mlock(p, size)) perror_exit("mlock");
+ // And ensure we weren't cheated by overcommit...
+ for (i = 0; i < size; i++) p[i] = i;
+
+ while (1) pause();
+}