-
Notifications
You must be signed in to change notification settings - Fork 7
/
timing.h
72 lines (55 loc) · 1.82 KB
/
timing.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once
#include <boost/thread/thread.hpp>
#include <boost/thread/tss.hpp>
#include <chrono>
#include <ctime>
#include <map>
#include <memory>
#include <string>
namespace cz {
class Timing {
public:
// |int| overflows on FASHION-MNIST.
using IntType = long long;
using DoubleType = long double;
static Timing* Instance() {
if (!collect_histogram_)
return dummy_instance_.get();
// Don't delete p during cleanup.
static boost::thread_specific_ptr<Timing> tls([](Timing* t) {});
if (!tls.get()) {
boost::mutex::scoped_lock l(pti_mtx_);
auto& p = per_thread_instance_[boost::this_thread::get_id()];
if (!p.get())
p = std::make_unique<Timing>();
tls.reset(p.get());
}
return tls.get();
}
// Should be diabled during large scale benchmarks.
void SetCollectHistogram(bool collect_histogram);
void StartTimer(const char* tag);
void EndTimer(const char* tag);
double GetTotalSeconds(const char* tag);
void BinCount(const char* name, int bin);
void IncreaseSample(const char* name, size_t sample);
void IncreaseSample(const char* name, int sample);
std::string CollectMetricsString();
private:
void CollectMetrics();
void DumpSamplesToBins();
std::string ToDebugString();
std::map<const char*, std::chrono::high_resolution_clock::time_point>
start_time_;
std::map<const char*, std::pair<IntType, DoubleType>>
timer_count_total_seconds_;
std::map<const char*, std::map<IntType, IntType>> bins_;
std::map<const char*, std::map<IntType, IntType>> samples_;
static bool collect_histogram_;
static boost::mutex pti_mtx_;
static std::map<boost::thread::id, std::unique_ptr<Timing>>
per_thread_instance_;
// Only used when |collect_histogram_| is false;
static std::unique_ptr<Timing> dummy_instance_;
};
} // namespace cz