Skip to content

Commit 0bbaed0

Browse files
authored
Merge pull request #427 from ESMValGroup/log_max_memory_used
Log max memory used by the recipe in the main log
2 parents f1332fe + ff34b14 commit 0bbaed0

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

esmvalcore/_task.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ def _get_resource_usage(process, start_time, children=True):
4848
'Disk write (GB)',
4949
]
5050
fmt = '{}\t' * len(entries[:-1]) + '{}\n'
51-
yield fmt.format(*entries)
51+
yield (fmt.format(*entries), 0.)
5252

5353
# Compute resource usage
5454
gigabyte = float(2**30)
5555
precision = [1, 1, None, 1, None, 3, 3]
5656
cache = {}
57+
max_memory = 0.
5758
try:
5859
process.io_counters()
5960
except AttributeError:
@@ -97,7 +98,8 @@ def _get_resource_usage(process, start_time, children=True):
9798
entries.insert(0, time.time() - start_time)
9899
entries = [round(entry, p) for entry, p in zip(entries, precision)]
99100
entries.insert(0, datetime.datetime.utcnow())
100-
yield fmt.format(*entries)
101+
max_memory = max(max_memory, entries[4])
102+
yield (fmt.format(*entries), max_memory)
101103

102104

103105
@contextlib.contextmanager
@@ -110,10 +112,16 @@ def _log_resource_usage():
110112
process = psutil.Process(pid)
111113
start_time = time.time()
112114
with open(filename, 'w') as file:
113-
for msg in _get_resource_usage(process, start_time, children):
115+
for msg, max_mem in _get_resource_usage(process, start_time,
116+
children):
114117
file.write(msg)
115118
time.sleep(interval)
116119
if halt.is_set():
120+
logger.info(
121+
'Maximum memory used (estimate): %.1f GB', max_mem)
122+
logger.info(
123+
'Sampled every second. It may be inaccurate if short '
124+
'but high spikes in memory consumption occur.')
117125
return
118126

119127
thread = threading.Thread(target=_log_resource_usage)

0 commit comments

Comments
 (0)