Skip to content

Commit

Permalink
Put stats collection under static variable (#790)
Browse files Browse the repository at this point in the history
Disable by default unused code

Signed-off-by: Alan Jowett <[email protected]>
  • Loading branch information
Alan-Jowett authored Nov 11, 2024
1 parent c34b769 commit 455555b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
4 changes: 0 additions & 4 deletions src/crab_utils/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,13 @@ void CrabStats::reset() {
sw.clear();
}

void CrabStats::count(const std::string& name) { ++(*counters)[name]; }
void CrabStats::count_max(const std::string& name, const unsigned v) {
(*counters)[name] = std::max((*counters)[name], v);
}

unsigned CrabStats::uset(const std::string& n, const unsigned v) { return (*counters)[n] = v; }
unsigned CrabStats::get(const std::string& n) { return (*counters)[n]; }

void CrabStats::start(const std::string& name) { (*sw)[name].start(); }
void CrabStats::stop(const std::string& name) { (*sw)[name].stop(); }
void CrabStats::resume(const std::string& name) { (*sw)[name].resume(); }

/** Outputs all statistics to std output */
void CrabStats::Print(std::ostream& OS) {
Expand Down
27 changes: 23 additions & 4 deletions src/crab_utils/stats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ inline std::ostream& operator<<(std::ostream& OS, const Stopwatch& sw) {
}

class CrabStats {
/// Controls whether statistics collection is active.
/// When false, all statistics methods become no-ops for better performance.
static constexpr bool enabled = false;
static thread_local lazy_allocator<std::map<std::string, unsigned>> counters;
static thread_local lazy_allocator<std::map<std::string, Stopwatch>> sw;

Expand All @@ -44,13 +47,29 @@ class CrabStats {
/* counters */
static unsigned get(const std::string& n);
static unsigned uset(const std::string& n, unsigned v);
static void count(const std::string& name);
static void count(const std::string& name) {
if constexpr (enabled) {
++(*counters)[name];
}
}
static void count_max(const std::string& name, unsigned v);

/* stop watch */
static void start(const std::string& name);
static void stop(const std::string& name);
static void resume(const std::string& name);
static void start(const std::string& name) {
if constexpr (enabled) {
(*sw)[name].start();
}
}
static void stop(const std::string& name) {
if constexpr (enabled) {
(*sw)[name].stop();
}
}
static void resume(const std::string& name) {
if constexpr (enabled) {
(*sw)[name].resume();
}
}

/** Outputs all statistics to std output */
static void Print(std::ostream& OS);
Expand Down

0 comments on commit 455555b

Please sign in to comment.