From 77d4207dbe76c55becf7f56eb01d443eb0bfc319 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 24 Jan 2024 13:01:35 -0800 Subject: [PATCH] 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. --- toys/pending/memeater.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 toys/pending/memeater.c 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(); +} -- 2.39.2