Skip to content

Commit

Permalink
Clean up the timer class.
Browse files Browse the repository at this point in the history
  • Loading branch information
fruffy committed Dec 14, 2022
1 parent 5d4a791 commit 48dbe1c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
40 changes: 19 additions & 21 deletions backends/p4tools/common/lib/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ using Clock = std::chrono::high_resolution_clock;
struct CounterEntry {
const char* name;
std::unordered_map<std::string, std::unique_ptr<CounterEntry>> counters;
Clock::duration duration;
Clock::duration duration{};

/// Lookup existing or create new child counter.
CounterEntry* open_subcounter(const char* name) {
CounterEntry* openSubcounter(const char* name) {
auto it = counters.find(name);
if (it == counters.end()) {
it = counters.emplace(name, new CounterEntry(name)).first;
Expand Down Expand Up @@ -50,52 +50,50 @@ struct RootCounter {
//
// however libgc cannot scan thread local data, which can lead to premature object
// garbage collection.
static RootCounter root;
root.counter.duration = Clock::now() - root.start;
return root;
static RootCounter ROOT;
ROOT.counter.duration = Clock::now() - ROOT.start;
return ROOT;
}

CounterEntry* getCurrent() const { return current; }

void setCurrent(CounterEntry* c) { current = c; }

private:
RootCounter() : counter("") {
current = &counter;
start = Clock::now();
}
RootCounter() : counter(""), current(&counter) { start = Clock::now(); }
};

} // namespace

#pragma GCC diagnostic ignored "-Wsubobject-linkage"
// RAII helper which manages lifetime of one timer invocation.
struct ScopedTimer::Ctx {
struct ScopedTimerCtx {
CounterEntry* parent = nullptr;
CounterEntry* self = nullptr;
Clock::time_point start_time;
Clock::time_point startTime;

explicit Ctx(const char* counter_name) {
start_time = Clock::now();
explicit ScopedTimerCtx(const char* timerName)
: parent(RootCounter::get().getCurrent()), self(parent->openSubcounter(timerName)) {
startTime = Clock::now();
// Push new active counter - the current active counter becomes the parent of this
// counter, and this counter becomes the current active counter.
parent = RootCounter::get().getCurrent();
self = parent->open_subcounter(counter_name);
RootCounter::get().setCurrent(self);
}
~Ctx() {
~ScopedTimerCtx() {
// Close the current timer invocation, measure time and add it to the counter.
auto duration = Clock::now() - start_time;
auto duration = Clock::now() - startTime;
self->add(duration);
// Restore previous counter as current.
RootCounter::get().setCurrent(parent);
}
};

ScopedTimer::ScopedTimer(const char* name) : ctx(new ScopedTimer::Ctx(name)) {}
ScopedTimer::ScopedTimer(const char* name) : ctx(new ScopedTimerCtx(name)) {}

ScopedTimer::~ScopedTimer() = default;

void withTimer(const char* counter_name, std::function<void()> fn) {
ScopedTimer timer(counter_name);
void withTimer(const char* timerName, const std::function<void()>& fn) {
ScopedTimer timer(timerName);
fn();
}

Expand Down Expand Up @@ -127,7 +125,7 @@ static void formatCounters(std::vector<TimerEntry>& out, CounterEntry& current,

std::vector<TimerEntry> getTimers() {
std::vector<TimerEntry> ret;
std::string namePrefix = "";
std::string namePrefix;
formatCounters(ret, RootCounter::get().counter, namePrefix, 0);
return ret;
}
Expand Down
10 changes: 5 additions & 5 deletions backends/p4tools/common/lib/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace P4Tools {
/// });
///
/// uses two separate counters denoted as "A.C" and "B.C".
void withTimer(const char* timer_name, std::function<void()> fn);
void withTimer(const char* timerName, const std::function<void()>& fn);

struct TimerEntry {
/// Counter name. If a timer "Y" was invoked inside timer "X", its timer name is "X.Y".
Expand All @@ -37,6 +37,9 @@ struct TimerEntry {
/// Returns list of all timers for and their current values.
std::vector<TimerEntry> getTimers();

// Internal implementation.
struct ScopedTimerCtx;

/// Similar to withTimer function, measures execution time elapsed from instance creation to
/// destruction.
class ScopedTimer {
Expand All @@ -46,10 +49,7 @@ class ScopedTimer {
~ScopedTimer();

private:
// Internal implementation.
struct Ctx;

std::unique_ptr<Ctx> ctx;
std::unique_ptr<ScopedTimerCtx> ctx;
};

} // namespace P4Tools
Expand Down

0 comments on commit 48dbe1c

Please sign in to comment.