Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/ipc/utils/profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

#ifdef IPC_TOOLKIT_WITH_PROFILER

#include <tbb/task_arena.h>

#include <fstream>
#include <queue>

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;
}
Comment thread
zfergus marked this conversation as resolved.

Profiler& profiler()
{
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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
}

Expand Down
15 changes: 6 additions & 9 deletions src/ipc/utils/profiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

#include <chrono>
#include <iostream>
#include <thread>

// Helper macro to stringify/paste after expansion
#define IPC_TOOLKIT_PROFILE_BLOCK_CONCAT_IMPL(a, b) a##b
Expand Down Expand Up @@ -74,21 +73,19 @@ 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.
/// 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:
/// @brief The profiling data stored as a JSON object.
nlohmann::json m_data;

/// @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();
Expand Down
4 changes: 4 additions & 0 deletions tests/src/tests/utils/test_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
#include <tbb/parallel_for.h>
#include <tbb/task_arena.h>

#include <thread>

using namespace ipc;

TEST_CASE("Profiler", "[profiler]")
{
profiler().reset();

Comment thread
zfergus marked this conversation as resolved.
constexpr int sleep_time_ms = 100;
constexpr int num_threads = 10;

Expand Down
Loading