memory watchdog periodically read the memory usage of the main test process and print it out until terminated stdin should refer to the process procpidstatm we don t pass the process pid to avoid a race condition in case of unlikely pid recycling if the process crashes reading from the proc entry will fail with esrch import sys import time from test support import getpagesize while true pagesize getpagesize sys stdin seek0 statm sys stdin read data intstatm split5 sys stdout write process data size data 1fgn formatdatadata pagesize 1024 3 sys stdout flush time sleep1 stdin should refer to the process proc pid statm we don t pass the process pid to avoid a race condition in case of unlikely pid recycling if the process crashes reading from the proc entry will fail with esrch
import sys
import time
from test.support import get_pagesize


while True:
    page_size = get_pagesize()
    sys.stdin.seek(0)
    statm = sys.stdin.read()
    data = int(statm.split()[5])
    sys.stdout.write(" ... process data size: {data:.1f}G\n"
                     .format(data=data * page_size / (1024 ** 3)))
    sys.stdout.flush()
    time.sleep(1)