From ce315cd8a799884d90b1b9489a3a9729f5456e4b Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 5 May 2020 10:45:15 -0400 Subject: [PATCH] Improve usage deltas reported to parents. --- synapse/logging/context.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/synapse/logging/context.py b/synapse/logging/context.py index 856534e91a75..527e6a5cc418 100644 --- a/synapse/logging/context.py +++ b/synapse/logging/context.py @@ -245,6 +245,7 @@ class LoggingContext(object): "name", "parent_context", "_resource_usage", + "_prev_resource_usage", "usage_start", "main_thread", "finished", @@ -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. @@ -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