From e5605b9078fbd3705c0902ebdc998de1f60c066f Mon Sep 17 00:00:00 2001 From: Kartik Pattaswamy <62078498+kpattaswamy@users.noreply.github.com> Date: Wed, 24 Apr 2024 10:12:48 -0700 Subject: [PATCH] [Mongo] Clear the streamID from the request/response maps after stitching (#1878) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: This PR modifies mongo's stitcher logic to clear streamID’s from request/response maps once all frames of a streamID have been consumed. We observed high CPU use allocated to `FramesSize()` & `EraseExpiredFrames()`, this was due to the size of the maps increasing with new streamIDs and having to continuously loop over those growing maps to cleanup. Clearing the streamID's from the maps after stitching significantly reduces the CPU allocated to the cleanup logic for mongo, the exact details for mongo's streamID reuse needs to be determined to further adapt the stitcher logic. Type of change: /kind bug Test Plan: Existing tests still pass, ran the px-mongo demo and observed lower CPU allocated to mongo in the PEM through flamegraph. --------- Signed-off-by: Kartik Pattaswamy --- .../socket_tracer/protocols/mongodb/stitcher.cc | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/stirling/source_connectors/socket_tracer/protocols/mongodb/stitcher.cc b/src/stirling/source_connectors/socket_tracer/protocols/mongodb/stitcher.cc index 635cbf63f46..d2ecf0ef1ca 100644 --- a/src/stirling/source_connectors/socket_tracer/protocols/mongodb/stitcher.cc +++ b/src/stirling/source_connectors/socket_tracer/protocols/mongodb/stitcher.cc @@ -183,7 +183,13 @@ RecordsWithErrorCount StitchFrames( ++erase_until_iter; } - req_deque.erase(req_deque.begin(), erase_until_iter); + // Determine whether to clear the StreamID from the map or only the frames that have been + // consumed. + if (erase_until_iter == req_deque.end()) { + reqs->erase(stream_id); + } else { + req_deque.erase(req_deque.begin(), erase_until_iter); + } stream_id_pair.second = true; } @@ -195,9 +201,11 @@ RecordsWithErrorCount StitchFrames( error_count++; } } - resp_deque.clear(); } + // Clear the response map. + resps->clear(); + // Clear the state. auto it = state->stream_order.begin(); while (it != state->stream_order.end()) {