68#ifndef OR_TOOLS_UTIL_STATS_H_
69#define OR_TOOLS_UTIL_STATS_H_
76#include "absl/strings/string_view.h"
77#include "absl/time/time.h"
92 explicit Stat(absl::string_view name) : name_(name) {}
99 std::string
Name()
const {
return name_; }
112 virtual double Sum()
const {
return 0; }
124 const std::string name_;
136 : name_(name), stats_(), time_distributions_() {}
167 std::vector<Stat*> stats_;
168 std::map<std::string, TimeDistribution*> time_distributions_;
180 void Reset()
override;
252 const double cycles =
static_cast<double>(timer_.GetCycles());
260 static std::string PrintCyclesAsTime(
double cycles);
272 void Add(
double value);
284 void Add(
double value);
296 void Add(int64_t value);
317 : stat_(stat), also_update_(nullptr) {
327 const double cycles = stat_->StopTimerAndAddElapsedTime();
328 if (also_update_ !=
nullptr) {
329 also_update_->AddTimeInCycles(cycles);
373#ifdef HAS_PERF_SUBSYSTEM
381#define IF_STATS_ENABLED(instructions) instructions
390#define SCOPED_TIME_STAT(stats) \
391 operations_research::ScopedTimeDistributionUpdater scoped_time_stat( \
392 (stats)->LookupOrCreateTimeDistribution(__FUNCTION__))
394#ifdef HAS_PERF_SUBSYSTEM
396inline std::string RemoveOperationsResearchAndGlop(
397 const std::string& pretty_function) {
398 return strings::GlobalReplaceSubstrings(
399 pretty_function, {{
"operations_research::",
""}, {
"glop::",
""}});
402#define SCOPED_INSTRUCTION_COUNT(time_limit) \
403 operations_research::ScopedInstructionCounter scoped_instruction_count( \
404 RemoveOperationsResearchAndGlop(__PRETTY_FUNCTION__), time_limit)
407#define SCOPED_INSTRUCTION_COUNT(time_limit)
417#define IF_STATS_ENABLED(instructions)
418#define SCOPED_TIME_STAT(stats)
419#define SCOPED_INSTRUCTION_COUNT(time_limit)
DisabledScopedInstructionCounter & operator=(const DisabledScopedInstructionCounter &)=delete
DisabledScopedInstructionCounter(absl::string_view)
DisabledScopedInstructionCounter(const DisabledScopedInstructionCounter &)=delete
DisabledScopedTimeDistributionUpdater(TimeDistribution *stat)
DisabledScopedTimeDistributionUpdater(const DisabledScopedTimeDistributionUpdater &)=delete
This type is neither copyable nor movable.
void AlsoUpdate(TimeDistribution *also_update)
DisabledScopedTimeDistributionUpdater & operator=(const DisabledScopedTimeDistributionUpdater &)=delete
double Average() const
Get the average of the distribution or 0.0 if empty.
bool WorthPrinting() const override
Is this stat worth printing? Usually false if nothing was measured.
double Sum() const override
Trivial statistics on all the values added so far.
double StdDeviation() const
~DistributionStat() override
void AddToDistribution(double value)
Adds a value to this sequence and updates the stats.
double sum_squares_from_average_
std::string ValueAsString() const override=0
Implemented by the subclasses.
void Reset() override
Reset this statistic to the same state as if it was newly created.
DistributionStat(absl::string_view name)
std::string ValueAsString() const override
Implemented by the subclasses.
DoubleDistribution(absl::string_view name, StatsGroup *group)
DoubleDistribution(absl::string_view name)
EnabledScopedTimeDistributionUpdater(TimeDistribution *stat)
EnabledScopedTimeDistributionUpdater(const EnabledScopedTimeDistributionUpdater &)=delete
This type is neither copyable nor movable.
void AlsoUpdate(TimeDistribution *also_update)
~EnabledScopedTimeDistributionUpdater()
EnabledScopedTimeDistributionUpdater & operator=(const EnabledScopedTimeDistributionUpdater &)=delete
IntegerDistribution(absl::string_view name, StatsGroup *group)
std::string ValueAsString() const override
Implemented by the subclasses.
IntegerDistribution(absl::string_view name)
std::string ValueAsString() const override
Implemented by the subclasses.
RatioDistribution(absl::string_view name, StatsGroup *group)
RatioDistribution(absl::string_view name)
Base class for a statistic that can be pretty-printed.
std::string Name() const
Only used for display purposes.
virtual bool WorthPrinting() const =0
Is this stat worth printing? Usually false if nothing was measured.
virtual int Priority() const
virtual std::string ValueAsString() const =0
Prints information about this statistic.
Stat(absl::string_view name)
std::string StatString() const
virtual double Sum() const
virtual void Reset()=0
Reset this statistic to the same state as if it was newly created.
Base class to print a nice summary of a group of statistics.
StatsGroup(const StatsGroup &)=delete
This type is neither copyable nor movable.
void SetPrintOrder(PrintOrder print_order)
StatsGroup & operator=(const StatsGroup &)=delete
TimeDistribution * LookupOrCreateTimeDistribution(std::string name)
StatsGroup(absl::string_view name)
@ SORT_BY_PRIORITY_THEN_VALUE
std::string StatString() const
void Reset()
Calls Reset() on all the statistics registered with this group.
void Register(Stat *stat)
std::string ValueAsString() const override
Implemented by the subclasses.
void AddTimeInSec(double seconds)
Adds a time in seconds to this distribution.
TimeDistribution(absl::string_view name)
TimeDistribution(absl::string_view name, StatsGroup *group)
static double CyclesToSeconds(double num_cycles)
void StartTimer()
Starts the timer in preparation of a StopTimerAndAddElapsedTime().
double StopTimerAndAddElapsedTime()
void AddTime(absl::Duration duration)
int Priority() const override
Time distributions have a high priority to be displayed first.
void AddTimeInCycles(double cycles)
Adds a time in CPU cycles to this distribution.
In SWIG mode, we don't want anything besides these top-level includes.
DisabledScopedTimeDistributionUpdater ScopedTimeDistributionUpdater
std::string MemoryUsage()
Returns the current thread's total memory usage in an human-readable string.
DisabledScopedInstructionCounter ScopedInstructionCounter