Skip to content

Commit 08ea458

Browse files
authored
feat: report resource usage before and after builds (#1023)
To diagnose out of disk and out of memory errors, log free disk space and available memory before and after each recipe is built and each mulled test.
1 parent 5910a10 commit 08ea458

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

bioconda_utils/bioconda_utils-requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ platformdirs=4.* #
6868

6969
# build failure output
7070
tabulate=0.9.* #
71+
72+
# resource reporting for builds
73+
psutil #

bioconda_utils/build.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def build(recipe: str, pkg_paths: List[str] = None,
135135
build_failure_record.remove()
136136

137137
try:
138+
report_resources(f"Starting build for {recipe}", docker_builder is not None)
138139
if docker_builder is not None:
139140
docker_builder.build_recipe(recipe_dir=os.path.abspath(recipe),
140141
build_args=' '.join(args),
@@ -182,18 +183,23 @@ def build(recipe: str, pkg_paths: List[str] = None,
182183
if raise_error:
183184
raise exc
184185
return BuildResult(False, None)
186+
finally:
187+
report_resources(f"Finished build for {recipe}", docker_builder is not None)
185188

186189
if mulled_test:
187190
logger.info('TEST START via mulled-build %s', recipe)
188191
mulled_images = []
189192
for pkg_path in pkg_paths:
190193
try:
194+
report_resources(f"Starting mulled build for {pkg_path}")
191195
pkg_test.test_package(pkg_path, base_image=base_image,
192196
conda_image=mulled_conda_image,
193197
live_logs=live_logs)
194198
except sp.CalledProcessError:
195199
logger.error('TEST FAILED: %s', recipe)
196200
return BuildResult(False, None)
201+
finally:
202+
report_resources(f"Finished mulled build for {pkg_path}")
197203
logger.info("TEST SUCCESS %s", recipe)
198204
mulled_images.append(pkg_test.get_image_name(pkg_path))
199205
return BuildResult(True, mulled_images)
@@ -511,3 +517,12 @@ def build_recipes(recipe_folder: str, config_path: str, recipes: List[str],
511517
logger.info("BUILD SUMMARY: successfully built %s of %s recipes",
512518
len(built_recipes), len(recipes))
513519
return True
520+
521+
def report_resources(message, show_docker=True):
522+
free_space_mb = utils.get_free_space()
523+
free_mem_mb = utils.get_free_memory_mb()
524+
free_mem_percent = utils.get_free_memory_percent()
525+
logger.info("{0} Free disk space: {1:.2f} MB. Free memory: {2:.2f} MB ({3:.2f}%)".format(message, free_space_mb, free_mem_mb, free_mem_percent))
526+
if show_docker:
527+
cmd = ['docker', 'system', 'df']
528+
utils.run(cmd, mask=False, live=True)

bioconda_utils/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import json
2020
import queue
2121
import warnings
22+
import psutil
2223

2324
from threading import Event, Thread
2425
from pathlib import PurePath
@@ -361,6 +362,16 @@ def get_free_space():
361362
return s.f_frsize * s.f_bavail / (1024 ** 2)
362363

363364

365+
def get_free_memory_percent():
366+
"""Return free memory as a percentage of total memory"""
367+
return psutil.virtual_memory().available * 100 / psutil.virtual_memory().total
368+
369+
370+
def get_free_memory_mb():
371+
"""Return free memory as megabytes"""
372+
return psutil.virtual_memory().available / (1024 ** 2)
373+
374+
364375
def allowed_env_var(s, docker=False):
365376
for pattern in ENV_VAR_WHITELIST:
366377
if fnmatch.fnmatch(s, pattern):

0 commit comments

Comments
 (0)