Files

16 lines
788 B
Python

#!/usr/bin/env python3
"""Evict the page cache of the given files (a directory = every regular file in it) with posix_fadvise(DONTNEED). Per-file, user level, read-only files; this is NOT drop_caches.
python3 -I baseline-evict.py FILE_OR_DIR [...]
Clean unmapped pages are dropped; pages that are mapped or dirty stay. Prints how much of each file is still resident (mincore through fincore is done by the caller).
"""
import os, sys
for a in sys.argv[1:]:
paths = [os.path.join(a, f) for f in sorted(os.listdir(a))] if os.path.isdir(a) else [a]
for p in paths:
if not os.path.isfile(p):
continue
fd = os.open(p, os.O_RDONLY)
os.posix_fadvise(fd, 0, 0, os.POSIX_FADV_DONTNEED)
os.close(fd)
print("evicted %s" % p, flush=True)