1"""Memory watchdog: periodically read the memory usage of the main test process
2and print it out, until terminated."""
3# stdin should refer to the process' /proc/<PID>/statm: we don't pass the
4# process' PID to avoid a race condition in case of - unlikely - PID recycling.
5# If the process crashes, reading from the /proc entry will fail with ESRCH.
6
7
8import os
9import sys
10import time
11
12
13try:
14    page_size = os.sysconf('SC_PAGESIZE')
15except (ValueError, AttributeError):
16    try:
17        page_size = os.sysconf('SC_PAGE_SIZE')
18    except (ValueError, AttributeError):
19        page_size = 4096
20
21while True:
22    sys.stdin.seek(0)
23    statm = sys.stdin.read()
24    data = int(statm.split()[5])
25    sys.stdout.write(" ... process data size: {data:.1f}G\n"
26                     .format(data=data * page_size / (1024 ** 3)))
27    sys.stdout.flush()
28    time.sleep(1)
29