memory_watchdog.py 859 B

12345678910111213141516171819202122232425262728
  1. """Memory watchdog: periodically read the memory usage of the main test process
  2. and 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. import os
  7. import sys
  8. import time
  9. try:
  10. page_size = os.sysconf('SC_PAGESIZE')
  11. except (ValueError, AttributeError):
  12. try:
  13. page_size = os.sysconf('SC_PAGE_SIZE')
  14. except (ValueError, AttributeError):
  15. page_size = 4096
  16. while True:
  17. sys.stdin.seek(0)
  18. statm = sys.stdin.read()
  19. data = int(statm.split()[5])
  20. sys.stdout.write(" ... process data size: {data:.1f}G\n"
  21. .format(data=data * page_size / (1024 ** 3)))
  22. sys.stdout.flush()
  23. time.sleep(1)