Last active
December 16, 2023 17:34
-
-
Save azat/ef5ab5cbbc656a033226b335e66daa6e to your computer and use it in GitHub Desktop.
MADV_FREE vs MADV_DONTNEED: how the memory is accounted
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# systemd-run --user --unit test-madvise ./test.py | |
# journalctl -u -r | |
import mmap | |
import resource | |
import os | |
def get_cgroup_id(): | |
with open("/proc/self/cgroup") as f: | |
content = f.read().strip() | |
path = content.split(':')[-1] | |
return path | |
def get_cgroup_mem(cgroup_id): | |
with open('/sys/fs/cgroup/' + cgroup_id + '/memory.stat') as f: | |
content = f.read().strip() | |
content = content.split('\n') | |
content = filter(lambda line: 'active_anon' in line, content) | |
content = map(lambda line: line.split(' '), content) | |
content = list(content) | |
ret = {} | |
for key, value in content: | |
ret[key] = int(value) | |
return ret | |
def get_rss(): | |
with open('/proc/self/statm') as f: | |
rss = f.read().strip().split(' ')[1] | |
return int(rss) | |
def print_mem(prefix, cgroup_id): | |
cgroup_mem = get_cgroup_mem(cgroup_id) | |
rss = get_rss() | |
# metrics are in bytes | |
print('{:30}: rss {:10}, cgroup mem: inactive_anon: {}, active_anon: {}'.format( | |
prefix, | |
rss*4096, | |
cgroup_mem['inactive_anon'], | |
cgroup_mem['active_anon'])) | |
def main(): | |
cgroup_id = get_cgroup_id() | |
print('cgroup id: ' + cgroup_id) | |
print_mem('before mmap', cgroup_id) | |
vma = mmap.mmap(-1, 1<<30, mmap.MAP_PRIVATE) | |
print_mem('after mmap', cgroup_id) | |
vma.write(b'0' * int(1<<30)) | |
print_mem('after mmap fill', cgroup_id) | |
vma.madvise(mmap.MADV_FREE) | |
print_mem('after MADV_FREE', cgroup_id) | |
vma.madvise(mmap.MADV_DONTNEED) | |
print_mem('after MADV_DONTNEED', cgroup_id) | |
vma.close() | |
print_mem('after munmap', cgroup_id) | |
if __name__ == "__main__": | |
main() |
Author
azat
commented
Dec 16, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment