An object which periodically watches the process' memory consumption and prints it out.
| 1157 | |
| 1158 | |
| 1159 | class _MemoryWatchdog: |
| 1160 | """An object which periodically watches the process' memory consumption |
| 1161 | and prints it out. |
| 1162 | """ |
| 1163 | |
| 1164 | def __init__(self): |
| 1165 | self.procfile = '/proc/{pid}/statm'.format(pid=os.getpid()) |
| 1166 | self.started = False |
| 1167 | |
| 1168 | def start(self): |
| 1169 | import warnings |
| 1170 | try: |
| 1171 | f = open(self.procfile, 'r') |
| 1172 | except OSError as e: |
| 1173 | logging.getLogger(__name__).warning('/proc not available for stats: %s', e, exc_info=e) |
| 1174 | sys.stderr.flush() |
| 1175 | return |
| 1176 | |
| 1177 | import subprocess |
| 1178 | with f: |
| 1179 | watchdog_script = findfile("memory_watchdog.py") |
| 1180 | self.mem_watchdog = subprocess.Popen([sys.executable, watchdog_script], |
| 1181 | stdin=f, |
| 1182 | stderr=subprocess.DEVNULL) |
| 1183 | self.started = True |
| 1184 | |
| 1185 | def stop(self): |
| 1186 | if self.started: |
| 1187 | self.mem_watchdog.terminate() |
| 1188 | self.mem_watchdog.wait() |
| 1189 | |
| 1190 | |
| 1191 | def bigmemtest(size, memuse, dry_run=True): |