From e321ef8d9b95a9588f4fc5dc9ffbfd0ba3684fd5 Mon Sep 17 00:00:00 2001 From: Pierre Marieu Date: Thu, 29 Apr 2021 16:16:04 +0100 Subject: [PATCH] Add synthetic frame for `queue.get(block=True)` Whenever some python code is waiting for an item to arrive in a queue, the current flame grpah will only show the function where the queue.get is called, the rest of the stack is not showing. This is unfortunate because we would need to remap those stacks into WAITING state. This change adds another synthetic frame for this specific frame when the source code at the end of the sampled stack has `.get(block=True` This looks like it could trigger a lot of false positives but it will only happen when this is the leaf of the stack so we would need this code to call some native code that does not appear in the stack but where we spend a lot of time. --- codeguru_profiler_agent/sampling_utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/codeguru_profiler_agent/sampling_utils.py b/codeguru_profiler_agent/sampling_utils.py index 2fd4f6a..7f47cbc 100644 --- a/codeguru_profiler_agent/sampling_utils.py +++ b/codeguru_profiler_agent/sampling_utils.py @@ -11,6 +11,7 @@ TIME_SLEEP_FRAME = Frame(name="") LXML_SCHEMA_FRAME = Frame(name="lxml.etree:XMLSchema:__init__") +QUEUE_BLOCKING_GET_FRAME = Frame(name="") def get_stacks(threads_to_sample, excluded_threads, max_depth): @@ -78,6 +79,8 @@ def _maybe_append_synthetic_frame(result, frame, line_no): line = linecache.getline(frame.f_code.co_filename, line_no).strip() if "sleep(" in line: result.append(TIME_SLEEP_FRAME) + elif ".get(block=True" in line: + result.append(QUEUE_BLOCKING_GET_FRAME) elif "etree.XMLSchema(" in line: result.append(LXML_SCHEMA_FRAME)