Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put stats collection under static variable #790

Merged
merged 3 commits into from
Nov 11, 2024
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
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;
Alan-Jowett marked this conversation as resolved.
Show resolved Hide resolved
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
Loading