Skip to content

Commit

Permalink
Add more accurate RSS high water mark measurement for Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
peterallenwebb committed May 19, 2024
1 parent 341803d commit 3d7a25c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/dbt/cli/requires.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from dbt.plugins import set_up_plugin_manager
from dbt.profiler import profiler
from dbt.tracking import active_user, initialize_from_flags, track_run
from dbt.utils import try_get_max_rss_kb
from dbt.version import installed as installed_version
from dbt_common.clients.system import get_env
from dbt_common.context import get_invocation_context, set_invocation_context
Expand Down Expand Up @@ -148,14 +149,17 @@ def wrapper(*args, **kwargs):
import resource

rusage = resource.getrusage(resource.RUSAGE_SELF)
max_rss = try_get_max_rss_kb()
if max_rss is None:
max_rss = rusage.ru_maxrss

Check warning on line 154 in core/dbt/cli/requires.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/cli/requires.py#L154

Added line #L154 was not covered by tests
fire_event(
ResourceReport(
command_name=ctx.command.name,
command_success=success,
command_wall_clock_time=time.perf_counter() - start_func,
process_user_time=rusage.ru_utime,
process_kernel_time=rusage.ru_stime,
process_mem_max_rss=rusage.ru_maxrss,
process_mem_max_rss=max_rss,
process_in_blocks=rusage.ru_inblock,
process_out_blocks=rusage.ru_oublock,
),
Expand Down
18 changes: 18 additions & 0 deletions core/dbt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,21 @@ def strtobool(val: str) -> bool:
return False
else:
raise ValueError("invalid truth value %r" % (val,))


def try_get_max_rss_kb() -> Optional[int]:
"""Attempts to get the high water mark for this process's memory use via
the most reliable and accurate mechanism available through the host OS.
Currently only implemented for Linux."""
try:
# On Linux, the most reliable documented mechanism for getting the RSS
# high-water-mark comes from the line confusingly labeled VmHWM in the
# /proc/self/status virtual file.
with open("/proc/self/status") as f:
for line in f:
if line.startswith("VmHWM:"):
return int(str.split(line)[1])
except Exception:
pass

Check warning on line 404 in core/dbt/utils.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/utils.py#L403-L404

Added lines #L403 - L404 were not covered by tests

return None

Check warning on line 406 in core/dbt/utils.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/utils.py#L406

Added line #L406 was not covered by tests

0 comments on commit 3d7a25c

Please sign in to comment.