Skip to content
Open
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
9 changes: 7 additions & 2 deletions include/ProfilerLib/Profiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,16 @@ class Profiler {

void disable();

void enable();

[[nodiscard]] bool isEnabled() const;

private:
void save();
void submitEvent(const TraceEvent &event);

private:
// Forcing the event to be submitted causes it to be written even if the profiler is disabled. This is useful when a
// task consists of multiple events (such as scopeevent) where the profiler might be disabled during task execution.
void submitEvent(const TraceEvent &event, bool force = false);

std::string name;
std::filesystem::path outputPath;
Expand Down
1 change: 1 addition & 0 deletions include/ProfilerLib/ScopeEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ScopeEvent {
~ScopeEvent();

private:
bool active;
Profiler &p;
};

Expand Down
8 changes: 6 additions & 2 deletions src/Profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Profiler::Profiler(std::string name, std::filesystem::path outputPath) :
submitInstantEvent("Profiler \"" + this->name + "\" starting", Scope::Global);
}

void Profiler::submitEvent(const TraceEvent &event) {
if (!this->enabled) {
void Profiler::submitEvent(const TraceEvent &event, bool force) {
if (!this->enabled && !force) {
return;
}
std::lock_guard<std::mutex> lock(eventListMutex);
Expand Down Expand Up @@ -116,6 +116,10 @@ void Profiler::disable() {
this->enabled = false;
}

void Profiler::enable() {
this->enabled = true;
}

[[nodiscard]] bool Profiler::isEnabled() const {
return enabled;
};
2 changes: 1 addition & 1 deletion src/ProfilerUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ namespace profilerUtil {
}

std::size_t tidHash() {
return std::hash<std::thread::id>{}(tid());
return std::hash<std::thread::id>{}(tid()) & 0xFFFFFFF;
}
} // namespace profilerUtil
11 changes: 10 additions & 1 deletion src/ScopeEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@
#include "TraceEvent.hpp" // for TraceEvent, TraceEventType, Trac...

ScopeEvent::ScopeEvent(Profiler &profiler, std::string name) : p{profiler} {
active = p.isEnabled();
if (!active) {
return;
}
TraceEvent e;
e.ph = TraceEventType::DurationBegin;
e.name = p.name + ": " + std::move(name);
p.submitEvent(e);
}

ScopeEvent::~ScopeEvent() {
if (!active) {
// Prevent submitting DurationEnd when the profiler was disabled at DurationBegin
return;
}
TraceEvent e;
e.ph = TraceEventType::DurationEnd;
p.submitEvent(e);
// Submit the event even if profiler was disabled in the meantime (only consider enabled state at start)
p.submitEvent(e, true);
}