Skip to content

Instantly share code, notes, and snippets.

@cirocosta
Last active October 21, 2020 11:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cirocosta/16cd12fc87c684db7e105615e4bc99c0 to your computer and use it in GitHub Desktop.
Save cirocosta/16cd12fc87c684db7e105615e4bc99c0 to your computer and use it in GitHub Desktop.
example showing how MADV_FREE does not incur in RSS reduction right away
#include <signal.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
bool cont = true;
static const ptrdiff_t len = 1 << 25; // 32 MB
void
handle_signal(int sig)
{
if (sig != SIGINT) {
return;
}
cont ^= true;
}
void
wait()
{
cont = false;
signal(SIGINT, handle_signal);
printf("CTRL+C to continue\n");
while (!cont) {
sleep(1);
}
}
int
main(void)
{
char *start, *end;
void* pb;
pb = sbrk(0);
if (pb == (void*)-1) {
perror("sbrk");
return 1;
}
start = (char*)pb;
end = start + len;
// "allocate" mem by increasing the program break
//
if (!~brk(end)) {
perror("brk");
return 1;
}
wait();
// "touch" the memory so that we get it really utilized - at this point,
// we should see the faults taking place, and both RSS and active anon
// going up
//
for (; start < end;) {
*(start++) = 123;
}
wait();
// let the kernel know that we don't really need half of the memory we
// allocated anymore - while this will not change RSS, it'll definitely
// change active and inactive.
//
if (!~madvise(pb + (len >> 1), (len >> 1), MADV_FREE)) {
perror("madvise");
return 1;
}
wait();
return 0;
}
diff --git a/tmp/1 b/tmp/2
index 3e93f6b..f37efef 100644
--- a/tmp/1
+++ b/tmp/2
@@ -1,33 +1,33 @@
cache 0
-rss 278528
+rss 33800192
rss_huge 0
shmem 0
mapped_file 0
dirty 0
writeback 0
-pgpgin 61545
+pgpgin 69729
pgpgout 61524
-pgfault 75009
+pgfault 83193
pgmajfault 0
inactive_anon 0
-active_anon 352256
+active_anon 33906688
inactive_file 0
active_file 0
unevictable 0
hierarchical_memory_limit 9223372036854771712
total_cache 0
-total_rss 278528
+total_rss 33800192
total_rss_huge 0
total_shmem 0
total_mapped_file 0
total_dirty 0
total_writeback 0
-total_pgpgin 61545
+total_pgpgin 69729
total_pgpgout 61524
-total_pgfault 75009
+total_pgfault 83193
total_pgmajfault 0
total_inactive_anon 0
-total_active_anon 352256
+total_active_anon 33906688
total_inactive_file 0
total_active_file 0
total_unevictable 0
diff --git a/tmp/2 b/tmp/3
index f37efef..cc81ef9 100644
--- a/tmp/2
+++ b/tmp/3
@@ -10,8 +10,8 @@ pgpgout 61524
pgfault 83193
pgmajfault 0
inactive_anon 0
-active_anon 33906688
-inactive_file 0
+active_anon 17129472
+inactive_file 16777216
active_file 0
unevictable 0
hierarchical_memory_limit 9223372036854771712
@@ -27,7 +27,7 @@ total_pgpgout 61524
total_pgfault 83193
total_pgmajfault 0
total_inactive_anon 0
-total_active_anon 33906688
-total_inactive_file 0
+total_active_anon 17129472
+total_inactive_file 16777216
total_active_file 0
total_unevictable 0
1. compile it
gcc -O2 -static -o sample ./madvise-sample.c
2. run it in a terminal that has the current proc in a cgroup
mkdir /sys/fs/cgroup/memory/test
echo $$ > /sys/fs/cgroup/memory/test/cgroup.procs
./sample
3. in another terminal, observe `memory.stat` for that cgroup
cat /sys/fs/cgroup/memory/test/memory.stat
4. use CTRL+C to make `sample` advance - see `memory.stat` as you do it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment