-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtiming.cc
148 lines (113 loc) · 3.51 KB
/
timing.cc
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "timing.h"
#include <algorithm>
#include <iostream>
#include <vector>
namespace cz {
using namespace std::chrono;
bool Timing::collect_histogram_ = false;
boost::mutex Timing::pti_mtx_;
std::map<boost::thread::id, std::unique_ptr<Timing>>
Timing::per_thread_instance_;
std::unique_ptr<Timing> Timing::dummy_instance_ = std::make_unique<Timing>();
void Timing::SetCollectHistogram(bool collect_histogram) {
collect_histogram_ = collect_histogram;
}
void Timing::StartTimer(const char* tag) {
if (!collect_histogram_)
return;
auto& iter = start_time_[tag];
iter = high_resolution_clock::now();
}
void Timing::EndTimer(const char* tag) {
if (!collect_histogram_)
return;
auto end = high_resolution_clock::now();
timer_count_total_seconds_[tag].first++;
timer_count_total_seconds_[tag].second +=
duration_cast<duration<double>>(end - start_time_[tag]).count();
}
double Timing::GetTotalSeconds(const char* tag) {
return timer_count_total_seconds_[tag].second;
}
void Timing::BinCount(const char* name, int bin) {
if (!collect_histogram_)
return;
bins_[name][bin]++;
}
void Timing::IncreaseSample(const char* name, size_t sample) {
if (!collect_histogram_)
return;
IncreaseSample(name, (int)sample);
}
void Timing::IncreaseSample(const char* name, int sample) {
if (!collect_histogram_)
return;
samples_[name][sample]++;
}
std::string Timing::CollectMetricsString() {
if (!collect_histogram_)
return "|collect_histogram| disabled\n";
CollectMetrics();
return ToDebugString();
}
void Timing::CollectMetrics() {
auto this_id = boost::this_thread::get_id();
Timing* this_instance = Instance();
printf("Timing::CollectMetrics per_thread_instance_.size(): %ld\n",
per_thread_instance_.size());
for (auto& iter : per_thread_instance_) {
iter.second->DumpSamplesToBins();
}
for (const auto& iter : per_thread_instance_) {
if (iter.first == this_id)
continue;
for (const auto& jter : iter.second->timer_count_total_seconds_) {
const auto& tag = jter.first;
timer_count_total_seconds_[tag].first += jter.second.first;
timer_count_total_seconds_[tag].second += jter.second.second;
}
for (const auto& jter : iter.second->bins_) {
const auto& tag = jter.first;
for (const auto& kter : jter.second)
bins_[tag][kter.first] += kter.second;
}
}
}
void Timing::DumpSamplesToBins() {
for (const auto& iter : samples_) {
for (const auto& jter : iter.second) {
BinCount(iter.first, jter.second);
}
}
samples_.clear();
}
std::string Timing::ToDebugString() {
std::string str;
std::vector<std::pair<std::string, std::pair<IntType, DoubleType>>>
sorted_time;
for (const auto& iter : timer_count_total_seconds_) {
sorted_time.push_back(iter);
}
sort(sorted_time.begin(), sorted_time.end());
for (const auto& iter : sorted_time) {
str += iter.first + ": " + std::to_string(iter.second.first) + " timers " +
std::to_string(iter.second.second) + " seconds\n";
}
str += "Bins:\n";
for (const auto& iter : bins_) {
DoubleType sum = 0;
IntType num = 0;
std::string str2 = "{";
for (const auto& jter : iter.second) {
str2 += std::to_string(jter.first) + ": " + std::to_string(jter.second) +
", ";
num += jter.second;
sum += jter.first * jter.second;
}
str2 += "}\n";
str += std::string(iter.first) + ": mean(" + std::to_string(sum / num) +
") " + str2;
}
return str;
}
} // namespace cz