Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Improve usage deltas reported to parents.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed May 5, 2020
1 parent fe69fb6 commit ce315cd
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions synapse/logging/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ class LoggingContext(object):
"name",
"parent_context",
"_resource_usage",
"_prev_resource_usage",
"usage_start",
"main_thread",
"finished",
Expand All @@ -259,6 +260,7 @@ def __init__(self, name=None, parent_context=None, request=None) -> None:

# track the resources used by this context so far
self._resource_usage = ContextResourceUsage()
self._prev_resource_usage = None # type: Optional[ContextResourceUsage]

# The thread resource usage when the logcontext became active. None
# if the context is not currently active.
Expand Down Expand Up @@ -434,12 +436,18 @@ def stop(self, rusage: "Optional[resource._RUsage]") -> None:
self._resource_usage.ru_utime += utime_delta
self._resource_usage.ru_stime += stime_delta

# if we have a parent, pass our CPU usage stats on
# if we have a parent, pass the change in our usage stats on.
if self.parent_context:
self.parent_context._resource_usage += self._resource_usage

# reset them in case we get entered again
self._resource_usage.reset()
if self._prev_resource_usage:
delta_resource_usage = (
self._resource_usage - self._prev_resource_usage
)
else:
delta_resource_usage = self._resource_usage
self.parent_context._resource_usage += delta_resource_usage

# Store the current usage for future deltas.
self._prev_resource_usage = self._resource_usage.copy()
finally:
self.usage_start = None

Expand Down

0 comments on commit ce315cd

Please sign in to comment.