From 5f7e9ad74119e19ef3194699f2f7b37c64bfbd57 Mon Sep 17 00:00:00 2001 From: Zachary Ferguson Date: Mon, 18 May 2026 17:03:40 -0400 Subject: [PATCH 1/3] Use TBB arena index for profiler recording --- src/ipc/utils/profiler.cpp | 16 +++++++++++----- src/ipc/utils/profiler.hpp | 14 +++++--------- tests/src/tests/utils/test_profiler.cpp | 2 ++ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/ipc/utils/profiler.cpp b/src/ipc/utils/profiler.cpp index a5e61d7b2..7be8fcbf9 100644 --- a/src/ipc/utils/profiler.cpp +++ b/src/ipc/utils/profiler.cpp @@ -2,12 +2,20 @@ #ifdef IPC_TOOLKIT_WITH_PROFILER +#include + #include #include namespace ipc { -Profiler::Profiler() : m_main_thread_id(std::this_thread::get_id()) { } +Profiler::Profiler() = default; + +bool Profiler::is_recording_thread() const +{ + const int idx = tbb::this_task_arena::current_thread_index(); + return idx == tbb::task_arena::not_initialized || idx == 0; +} Profiler& profiler() { @@ -23,7 +31,7 @@ void Profiler::clear() void Profiler::start(const std::string& name) { - if (std::this_thread::get_id() != m_main_thread_id) { + if (!is_recording_thread()) { return; } @@ -39,7 +47,7 @@ void Profiler::start(const std::string& name) void Profiler::stop(const double time_ms) { - if (std::this_thread::get_id() != m_main_thread_id) { + if (!is_recording_thread()) { return; } @@ -63,9 +71,7 @@ void Profiler::stop(const double time_ms) void Profiler::reset() { - m_main_thread_id = std::this_thread::get_id(); m_data.clear(); - // reset the calling thread's scope m_current_scope = nlohmann::json::json_pointer(); // root } diff --git a/src/ipc/utils/profiler.hpp b/src/ipc/utils/profiler.hpp index de955ad1b..f03e6c38e 100644 --- a/src/ipc/utils/profiler.hpp +++ b/src/ipc/utils/profiler.hpp @@ -22,7 +22,6 @@ #include #include -#include // Helper macro to stringify/paste after expansion #define IPC_TOOLKIT_PROFILE_BLOCK_CONCAT_IMPL(a, b) a##b @@ -74,10 +73,11 @@ class Profiler { /// @brief Access the profiling data as a JSON object. nlohmann::json& data() { return m_data; } - bool is_recording_thread() const - { - return std::this_thread::get_id() == m_main_thread_id; - } + /// @brief Returns true if the current thread should record profiling data. + /// Outside any TBB arena (serial code) always records. Inside a + /// TBB arena only the external/coordinator thread (slot 0) records, + /// giving a single-thread estimate of parallel block costs. + bool is_recording_thread() const; protected: /// @brief The profiling data stored as a JSON object. @@ -85,10 +85,6 @@ class Profiler { /// @brief The global scope pointer into the JSON data. nlohmann::json::json_pointer m_current_scope; - - /// @brief The thread that records data; calls from all other threads are - /// silently ignored, giving a single-thread estimate of block costs. - std::thread::id m_main_thread_id; }; Profiler& profiler(); diff --git a/tests/src/tests/utils/test_profiler.cpp b/tests/src/tests/utils/test_profiler.cpp index c24cb20b7..1d1f82f81 100644 --- a/tests/src/tests/utils/test_profiler.cpp +++ b/tests/src/tests/utils/test_profiler.cpp @@ -21,6 +21,8 @@ using namespace ipc; TEST_CASE("Profiler", "[profiler]") { + profiler().reset(); + constexpr int sleep_time_ms = 100; constexpr int num_threads = 10; From 2b97fa5307aea1670085ea6548d50ddfcf9f2284 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 18 May 2026 21:31:17 +0000 Subject: [PATCH 2/3] Add explicit include to test_profiler.cpp Agent-Logs-Url: https://github.com/ipc-sim/ipc-toolkit/sessions/ee560464-e410-4bd3-9196-31881216dee4 Co-authored-by: zfergus <8219522+zfergus@users.noreply.github.com> --- tests/src/tests/utils/test_profiler.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/src/tests/utils/test_profiler.cpp b/tests/src/tests/utils/test_profiler.cpp index 1d1f82f81..5cd7e91c9 100644 --- a/tests/src/tests/utils/test_profiler.cpp +++ b/tests/src/tests/utils/test_profiler.cpp @@ -17,6 +17,8 @@ #include #include +#include + using namespace ipc; TEST_CASE("Profiler", "[profiler]") From befb7cd68e6c7f889807e4463ed79b0bcc93c0da Mon Sep 17 00:00:00 2001 From: Zachary Ferguson Date: Mon, 18 May 2026 17:31:27 -0400 Subject: [PATCH 3/3] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/ipc/utils/profiler.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ipc/utils/profiler.hpp b/src/ipc/utils/profiler.hpp index f03e6c38e..92289bf20 100644 --- a/src/ipc/utils/profiler.hpp +++ b/src/ipc/utils/profiler.hpp @@ -74,9 +74,10 @@ class Profiler { nlohmann::json& data() { return m_data; } /// @brief Returns true if the current thread should record profiling data. - /// Outside any TBB arena (serial code) always records. Inside a - /// TBB arena only the external/coordinator thread (slot 0) records, - /// giving a single-thread estimate of parallel block costs. + /// When the current thread is not in a TBB arena, this returns + /// true. Inside a TBB arena, only the external/coordinator thread + /// (slot 0) records, giving a single-thread estimate of parallel + /// block costs. bool is_recording_thread() const; protected: