21#include "absl/log/check.h"
22#include "absl/strings/str_format.h"
33 static const int64_t kDisplayThreshold = 2;
34 static const int64_t kKiloByte = 1024;
35 static const int64_t kMegaByte = kKiloByte * kKiloByte;
36 static const int64_t kGigaByte = kMegaByte * kKiloByte;
37 if (mem > kDisplayThreshold * kGigaByte) {
38 return absl::StrFormat(
"%.2lf GB", mem * 1.0 / kGigaByte);
39 }
else if (mem > kDisplayThreshold * kMegaByte) {
40 return absl::StrFormat(
"%.2lf MB", mem * 1.0 / kMegaByte);
41 }
else if (mem > kDisplayThreshold * kKiloByte) {
42 return absl::StrFormat(
"%2lf KB", mem * 1.0 / kKiloByte);
44 return absl::StrFormat(
"%d", mem);
59 for (
int i = 0; i < stats_.size(); ++i) {
66bool CompareStatPointers(
const Stat* s1,
const Stat* s2) {
69 return (s1->
Sum() > s2->
Sum());
80 int longest_name_size = 0;
81 std::vector<Stat*> sorted_stats;
82 for (
int i = 0; i < stats_.size(); ++i) {
83 if (!stats_[i]->WorthPrinting())
continue;
86 longest_name_size = std::max(longest_name_size,
size);
87 sorted_stats.push_back(stats_[i]);
89 switch (print_order_) {
91 std::sort(sorted_stats.begin(), sorted_stats.end(), CompareStatPointers);
94 std::sort(sorted_stats.begin(), sorted_stats.end(),
95 [](
const Stat* s1,
const Stat* s2) ->
bool {
96 return s1->Name() < s2->Name();
100 LOG(FATAL) <<
"Unknown print order: " << print_order_;
104 if (sorted_stats.empty())
return "";
107 std::string result(name_ +
" {\n");
108 for (
int i = 0; i < sorted_stats.size(); ++i) {
110 result += sorted_stats[i]->Name();
112 sorted_stats[i]->Name()),
114 result +=
" : " + sorted_stats[i]->ValueAsString();
122 if (ref ==
nullptr) {
133 sum_squares_from_average_(0.0),
142 sum_squares_from_average_(0.0),
177 if (
num_ == 0)
return 0.0;
183 return cycles * seconds_per_cycles;
186std::string TimeDistribution::PrintCyclesAsTime(
double cycles) {
187 DCHECK_GE(cycles, 0.0);
189 double eps1 = 1 + 1e-3;
191 if (sec * eps1 >= 3600.0)
return absl::StrFormat(
"%.2fh", sec / 3600.0);
192 if (sec * eps1 >= 60.0)
return absl::StrFormat(
"%.2fm", sec / 60.0);
193 if (sec * eps1 >= 1.0)
return absl::StrFormat(
"%.2fs", sec);
194 if (sec * eps1 >= 1e-3)
return absl::StrFormat(
"%.2fms", sec * 1e3);
195 if (sec * eps1 >= 1e-6)
return absl::StrFormat(
"%.2fus", sec * 1e6);
196 return absl::StrFormat(
"%.2fns", sec * 1e9);
200 DCHECK_GE(seconds, 0.0);
206 DCHECK_GE(cycles, 0.0);
211 return absl::StrFormat(
212 "%8u [%8s, %8s] %8s %8s %8s\n",
num_, PrintCyclesAsTime(
min_),
213 PrintCyclesAsTime(
max_), PrintCyclesAsTime(
Average()),
218 DCHECK_GE(
value, 0.0);
223 return absl::StrFormat(
"%8u [%7.2f%%, %7.2f%%] %7.2f%% %7.2f%%\n",
num_,
231 return absl::StrFormat(
"%8u [%8.1e, %8.1e] %8.1e %8.1e\n",
num_,
min_,
max_,
240 return absl::StrFormat(
"%8u [%8.f, %8.f] %8.2f %8.2f %8.f\n",
num_,
min_,
static double CyclesToSeconds(int64_t c)
double Average() const
Get the average of the distribution or 0.0 if empty.
double StdDeviation() const
void AddToDistribution(double value)
Adds a value to this sequence and updates the stats.
double sum_squares_from_average_
void Reset() override
Reset this statistic to the same state as if it was newly created.
std::string ValueAsString() const override
Implemented by the subclasses.
std::string ValueAsString() const override
Implemented by the subclasses.
std::string ValueAsString() const override
Implemented by the subclasses.
Base class for a statistic that can be pretty-printed.
std::string Name() const
Only used for display purposes.
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
Base class to print a nice summary of a group of statistics.
TimeDistribution * LookupOrCreateTimeDistribution(std::string 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.
static double CyclesToSeconds(double num_cycles)
void AddTimeInCycles(double cycles)
Adds a time in CPU cycles to this distribution.
const std::string name
A name for logging purposes.
void STLDeleteValues(T *v)
int64_t MemoryUsageProcess()
int UTF8StrLen(StrType str_type)
str_type should be string/StringPiece/Cord
In SWIG mode, we don't want anything besides these top-level includes.
std::string MemoryUsage()
Returns the current thread's total memory usage in an human-readable string.