20 #ifndef GALOIS_STAT_MANAGER_H
21 #define GALOIS_STAT_MANAGER_H
26 #include <type_traits>
28 #include <sys/resource.h>
31 #include <boost/uuid/uuid.hpp>
32 #include <boost/uuid/uuid_generators.hpp>
33 #include <boost/uuid/uuid_io.hpp>
35 #include "galois/config.h"
68 const T&
min(
void)
const {
return m_min; }
80 const T&
max(
void)
const {
return m_max; }
91 void add(
const T& val) {
96 const T&
sum(
void)
const {
return m_sum; }
98 const size_t&
count(
void)
const {
return m_count; }
100 T
avg()
const {
return m_sum / m_count; }
103 template <
typename T>
111 void add(
const T& val) { m_vec.push_back(val); }
113 const Vec&
values(
void)
const {
return m_vec; }
116 template <
typename T>
128 const Str&
name(
void)
const {
return m_name; }
130 void add(
const T&)
const {}
133 template <
typename T,
typename... Bases>
151 static constexpr
const char* StatTotalNames[] = {
"SINGLE",
"TMIN",
"TMAX",
159 static const char*
str(
const Type& t) {
return StatTotalNames[t]; }
164 template <
typename Stat_tp>
165 struct BasicStatMap {
167 using Stat = Stat_tp;
171 using const_iterator =
typename StatMap::const_iterator;
177 const Str* getOrInsertSymbol(
const Str& s) {
178 auto p = symbols.insert(s);
182 const Str* getSymbol(
const Str& s)
const {
183 auto i = symbols.find(s);
185 if (i == symbols.cend()) {
193 template <
typename... Args>
194 Stat& getOrInsertStat(
const Str& region,
const Str& category,
197 const Str* ln = getOrInsertSymbol(region);
198 const Str* cat = getOrInsertSymbol(category);
200 auto tpl = std::make_tuple(ln, cat);
202 auto p = statMap.emplace(tpl, Stat(std::forward<Args>(args)...));
204 return p.first->second;
207 const_iterator findStat(
const Str& region,
const Str& category)
const {
209 const Str* ln = getSymbol(region);
210 const Str* cat = getSymbol(category);
211 auto tpl = std::make_tuple(ln, cat);
213 auto i = statMap.find(tpl);
218 const Stat& getStat(
const Str& region,
const Str& category)
const {
220 auto i = findStat(region, category);
221 assert(i != statMap.end());
225 template <
typename T,
typename... Args>
226 void addToStat(
const Str& region,
const Str& category,
const T& val,
227 Args&&... statArgs) {
229 getOrInsertStat(region, category, std::forward<Args>(statArgs)...);
233 const_iterator cbegin(
void)
const {
return statMap.cbegin(); }
234 const_iterator cend(
void)
const {
return statMap.cend(); }
236 const Str& region(
const const_iterator& i)
const {
237 return *(std::get<0>(i->first));
240 const Str& category(
const const_iterator& i)
const {
241 return *(std::get<1>(i->first));
244 const Stat& stat(
const const_iterator& i)
const {
return i->second; }
247 template <
typename T>
248 using VecStat_with_MinMaxSum =
251 template <
typename T>
252 struct VecStat :
public VecStat_with_MinMaxSum<T> {
254 using Base = VecStat_with_MinMaxSum<T>;
258 explicit VecStat(
const StatTotal::Type& type) : Base(), m_totalTy(type) {}
262 T total(
void)
const {
267 assert(Base::values().size() > 0);
268 return Base::values()[0];
289 struct VecStat<gstl::
Str> :
public AggregStat<gstl::Str>::with_mem {
291 using Base = AggregStat<gstl::Str>::with_mem;
295 explicit VecStat(
const StatTotal::Type& type) : Base(), m_totalTy(type) {}
304 assert(Base::values().size() > 0);
305 return Base::values()[0];
313 template <
typename T>
314 using VecStatManager = BasicStatMap<VecStat<T>>;
316 template <
typename T>
321 explicit ScalarStat(
const StatTotal::Type& type) : m_val(), m_totalTy(type) {}
323 void add(
const T& v) { m_val += v; }
325 operator const T&(void)
const {
return m_val; }
330 template <
typename T>
331 using ScalarStatManager = BasicStatMap<ScalarStat<T>>;
340 static constexpr
const char*
const SEP =
", ";
342 static constexpr
const char*
const TSTAT_NAME =
"ThreadValues";
343 static constexpr
const char*
const TSTAT_ENV_VAR =
"PRINT_PER_THREAD_STATS";
347 template <
typename T>
349 return std::is_same<T, Str>::value ?
"PARAM" :
"STAT";
353 template <
typename T>
354 struct StatManagerImpl {
356 using MergedStats = internal::VecStatManager<T>;
357 using const_iterator =
typename MergedStats::const_iterator;
358 using Stat =
typename MergedStats::Stat;
367 perThreadManagers.getLocal()->addToStat(region, category, val, type);
376 for (
unsigned t = 0; t < perThreadManagers.size(); ++t) {
378 const auto* manager = perThreadManagers.getRemote(t);
380 for (
auto i = manager->cbegin(), end_i = manager->cend(); i != end_i;
382 result.addToStat(manager->region(i), manager->category(i),
383 T(manager->stat(i)), manager->stat(i).totalTy());
390 const_iterator cbegin(
void)
const {
return result.cbegin(); }
391 const_iterator cend(
void)
const {
return result.cend(); }
393 const Str& region(
const const_iterator& i)
const {
394 return result.region(i);
397 const Str& category(
const const_iterator& i)
const {
398 return result.category(i);
401 const Stat& stat(
const const_iterator& i)
const {
return result.stat(i); }
403 template <
typename S,
typename V>
404 void readStat(
const const_iterator& i, S& region, S& category, T& total,
406 region = this->region(i);
407 category = this->category(i);
409 total = this->stat(i).total();
410 type = this->stat(i).totalTy();
413 thrdVals = this->stat(i).values();
416 void print(std::ostream& out)
const {
418 for (
auto i = cbegin(), end_i = cend(); i != end_i; ++i) {
419 out << statKind<T>() <<
SEP << this->region(i) <<
SEP
420 << this->category(i) <<
SEP;
422 const auto& s = this->stat(i);
429 out << statKind<T>() << SEP << this->region(i) << SEP
430 << this->category(i) <<
SEP;
433 const char* sep =
"";
434 for (
const auto& v : s.values()) {
445 using IntStats = StatManagerImpl<int64_t>;
446 using FPstats = StatManagerImpl<double>;
447 using StrStats = StatManagerImpl<Str>;
448 using int_iterator =
typename IntStats::const_iterator;
449 using fp_iterator =
typename FPstats::const_iterator;
450 using str_iterator =
typename StrStats::const_iterator;
452 std::string m_outfile;
459 intStats.mergeStats();
460 fpStats.mergeStats();
461 strStats.mergeStats();
465 int_iterator
intEnd(
void)
const;
467 fp_iterator
fpBegin(
void)
const;
468 fp_iterator
fpEnd(
void)
const;
473 template <
typename S,
typename V>
477 intStats.readStat(i, region, category, total, type, vec);
480 template <
typename S,
typename V>
481 void readFPstat(
const fp_iterator& i, S& region, S& category,
double& total,
484 fpStats.readStat(i, region, category, total, type, vec);
487 template <
typename S,
typename V>
488 void readParam(
const str_iterator& i, S& region, S& category,
Str& total,
491 strStats.readStat(i, region, category, total, type, vec);
499 explicit StatManager(
const std::string& outfile =
"");
505 template <
typename S1,
typename S2,
typename T,
506 typename = std::enable_if_t<std::is_integral<T>::value ||
507 std::is_floating_point<T>::value>>
508 void addToStat(
const S1& region,
const S2& category,
const T& val,
511 if (std::is_floating_point<T>::value) {
521 template <
typename S1,
typename S2,
typename V>
522 void addToParam(
const S1& region,
const S2& category,
const V& val) {
532 void setSysStatManager(StatManager* sm);
533 StatManager* sysStatManager(
void);
537 template <
typename S1,
typename S2,
typename T>
538 inline void reportStat(
const S1& region,
const S2& category,
const T& value,
540 internal::sysStatManager()->addToStat(region, category, value, type);
543 template <
typename S1,
typename S2,
typename T>
549 template <
typename S1,
typename S2,
typename T>
555 template <
typename S1,
typename S2,
typename T>
561 template <
typename S1,
typename S2,
typename T>
567 template <
typename S1,
typename S2,
typename T>
573 template <
bool Report = false,
typename S1,
typename S2,
typename T>
577 internal::sysStatManager()->addToStat(region, category, value, type);
580 template <
bool Report = false,
typename S1,
typename S2,
typename T>
587 template <
bool Report = false,
typename S1,
typename S2,
typename T>
594 template <
bool Report = false,
typename S1,
typename S2,
typename T>
601 template <
bool Report = false,
typename S1,
typename S2,
typename T>
608 template <
bool Report = false,
typename S1,
typename S2,
typename T>
615 template <
typename S1,
typename S2,
typename V>
616 void reportParam(
const S1& region,
const S2& category,
const V& value) {
617 internal::sysStatManager()->addToParam(region, category, value);
636 #endif // GALOIS_STAT_MANAGER_H
static constexpr const char *const TSTAT_ENV_VAR
Definition: Statistics.h:343
void reportStatCond_Single(const S1 ®ion, const S2 &category, const T &value)
Definition: Statistics.h:581
void reportStatCond(const S1 ®ion, const S2 &category, const T &value, const StatTotal::Type &type)
Definition: Statistics.h:574
void reportRUsage(const std::string &id)
Reports maximum resident set size and page faults stats using rusage.
Definition: Statistics.cpp:47
RunningSum(void)
Definition: Statistics.h:89
const T & min(void) const
Definition: Statistics.h:68
void readParam(const str_iterator &i, S ®ion, S &category, Str &total, StatTotal::Type &type, V &vec) const
Definition: Statistics.h:488
StatManager(const std::string &outfile="")
Definition: Statistics.cpp:35
Definition: Statistics.h:134
fp_iterator fpBegin(void) const
Definition: Statistics.cpp:104
static constexpr const char *const TSTAT_NAME
Definition: Statistics.h:342
void reportStat_Tmax(const S1 ®ion, const S2 &category, const T &value)
Definition: Statistics.h:556
Definition: Statistics.h:157
void reportParam(const S1 ®ion, const S2 &category, const V &value)
Definition: Statistics.h:616
void reportStatCond_Tmax(const S1 ®ion, const S2 &category, const T &value)
Definition: Statistics.h:595
Definition: Statistics.h:157
std::map< K, V, C, FixedSizeAlloc< std::pair< const K, V >>> Map
Definition: gstl.h:65
void reportStatCond_Tavg(const S1 ®ion, const S2 &category, const T &value)
Definition: Statistics.h:609
void add(const T &val)
Definition: Statistics.h:147
static constexpr const char *const SEP
Definition: Statistics.h:340
Type
Definition: Statistics.h:157
#define GALOIS_DIE(...)
Definition: gIO.h:96
void add(const T &val)
Definition: Statistics.h:78
str_iterator paramBegin(void) const
Definition: Statistics.cpp:111
void setStatFile(const std::string &outfile)
Definition: Statistics.cpp:39
void reportStat_Tsum(const S1 ®ion, const S2 &category, const T &value)
Definition: Statistics.h:562
void reportStatCond_Tsum(const S1 ®ion, const S2 &category, const T &value)
Definition: Statistics.h:602
Definition: Statistics.h:104
void add(const T &val)
Definition: Statistics.h:111
Definition: Statistics.h:155
std::set< T, C, FixedSizeAlloc< T >> Set
Definition: gstl.h:62
Definition: Statistics.h:60
virtual ~StatManager()
Definition: Statistics.cpp:37
str_iterator paramEnd(void) const
Definition: Statistics.cpp:114
void reportPageAlloc(const char *category)
Reports Galois system memory stats for all threads.
Definition: Statistics.cpp:128
galois::gstl::Str Str
Definition: Statistics.h:338
void setName(Str &&name)
Definition: Statistics.h:126
T avg() const
Definition: Statistics.h:100
void print(void)
Definition: Statistics.cpp:68
void readFPstat(const fp_iterator &i, S ®ion, S &category, double &total, StatTotal::Type &type, V &vec) const
Definition: Statistics.h:481
std::vector< T, Pow2Alloc< T >> Vector
[STL vector using Pow_2_VarSizeAlloc]
Definition: gstl.h:52
Definition: Statistics.h:335
void reportStat_Single(const S1 ®ion, const S2 &category, const T &value)
Definition: Statistics.h:544
const Ty max(std::atomic< Ty > &a, const Ty &b)
Definition: AtomicHelpers.h:40
Definition: PerThreadStorage.h:88
Definition: Statistics.h:117
static bool printingThreadVals(void)
Definition: Statistics.cpp:64
void setName(const Str &name)
Definition: Statistics.h:124
void mergeStats(void)
Definition: Statistics.h:458
const Str & name(void) const
Definition: Statistics.h:128
Definition: Statistics.h:84
Str makeStr(const T &x)
Definition: gstl.h:102
int_iterator intBegin(void) const
Definition: Statistics.cpp:97
const size_t & count(void) const
Definition: Statistics.h:98
void reportNumaAlloc(const char *category)
Reports NUMA memory stats for all NUMA nodes.
Definition: Statistics.cpp:136
Definition: Statistics.h:72
const Vec & values(void) const
Definition: Statistics.h:113
void reportStatCond_Tmin(const S1 ®ion, const S2 &category, const T &value)
Definition: Statistics.h:588
static const char * str(const Type &t)
Definition: Statistics.h:159
const Ty min(std::atomic< Ty > &a, const Ty &b)
Definition: AtomicHelpers.h:70
void reportStat_Tavg(const S1 ®ion, const S2 &category, const T &value)
Definition: Statistics.h:568
AggregStat< T, RunningSum< T >, Bases...> with_sum
Definition: Statistics.h:141
int_iterator intEnd(void) const
Definition: Statistics.cpp:100
RunningMin(void)
Definition: Statistics.h:64
const Ty add(std::atomic< Ty > &a, const Ty &b)
Definition: AtomicHelpers.h:98
void addToStat(const S1 ®ion, const S2 &category, const T &val, const StatTotal::Type &type)
Definition: Statistics.h:508
void add(const T &val)
Definition: Statistics.h:91
Definition: Statistics.h:157
boost::uuids::uuid getRandUUID()
Definition: Statistics.cpp:28
virtual void printStats(std::ostream &out)
Definition: Statistics.cpp:82
fp_iterator fpEnd(void) const
Definition: Statistics.cpp:107
RunningMax(void)
Definition: Statistics.h:76
void add(const T &val)
Definition: Statistics.h:66
void reportStat(const S1 ®ion, const S2 &category, const T &value, const StatTotal::Type &type)
Definition: Statistics.h:538
static constexpr const char * statKind(void)
Definition: Statistics.h:348
const T & max(void) const
Definition: Statistics.h:80
Definition: Statistics.h:157
void printHeader(std::ostream &out) const
Definition: Statistics.cpp:90
void setStatFile(const std::string &f)
Definition: Statistics.cpp:43
const T & sum(void) const
Definition: Statistics.h:96
static constexpr const char *const TSTAT_SEP
Definition: Statistics.h:341
void reportStat_Tmin(const S1 ®ion, const S2 &category, const T &value)
Definition: Statistics.h:550
Definition: Statistics.h:157
void addToParam(const S1 ®ion, const S2 &category, const V &val)
Definition: Statistics.h:522
std::basic_string< char, std::char_traits< char >, Pow2Alloc< char >> Str
Definition: gstl.h:75
void readIntStat(const int_iterator &i, S ®ion, S &category, int64_t &total, StatTotal::Type &type, V &vec) const
Definition: Statistics.h:474
void add(const T &) const
Definition: Statistics.h:130