-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.hh
199 lines (168 loc) · 5.7 KB
/
clock.hh
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// -*- mode: c++; c-basic-offset: 2; -*-
#pragma once
/**
* @file clock.hh
* @date novembre 10, 2021
* @brief Clock primitives for benchmarking workloads
*/
#include <cstdint>
#ifndef NVSL_ASSERT
#include <cassert>
#endif
#include <algorithm>
#include <chrono>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
namespace nvsl {
inline std::string ns_to_hr_clk(const size_t ns_total) {
std::stringstream ss;
const size_t s = ns_total / (1000000000);
const size_t ms = (ns_total - s * 1000000000) / (1000000);
const size_t us = (ns_total - s * 1000000000 - ms * 1000000) / (1000);
const size_t ns = (ns_total - s * 1000000000 - ms * 1000000 - us * 1000);
ss << s << "s " << ms << "ms " << us << "us " << ns << "ns";
return ss.str();
}
/** @brief Clock object based on std::chrono::high_resolution_clock */
class Clock {
private:
std::chrono::system_clock::time_point start_clk;
bool running = false;
size_t total_ns = 0;
std::vector<size_t> raw_values;
std::vector<size_t> sorted_raw_values;
size_t events = 0;
/**< Total number of values to store */
static constexpr size_t RAW_VAL_CNT = 1024 * 1024 * 100;
public:
Clock(bool reserve_raw_vals = false) {
if (reserve_raw_vals)
raw_values.reserve(RAW_VAL_CNT);
}
/** @brief Start the timer */
void tick() {
running = true;
this->start_clk = std::chrono::high_resolution_clock::now();
}
/** @brief Stop the timer */
void tock() {
using namespace std::chrono;
const auto end_clk = high_resolution_clock::now();
if (!running) {
#if defined(NVSL_ERROR) && defined(DBGE)
NVSL_ERROR("Clock not running");
#else
std::cerr << "Clock not running" << std::endl;
exit(1);
#endif
}
running = false;
const auto elapsed =
duration_cast<nanoseconds>(end_clk - start_clk).count();
this->total_ns += elapsed;
#define OVERFLOW_MSG \
"Raw values buffer overflow, increase array size or " \
"operations/loop"
#if defined(NVSL_ASSERT) && defined(DBGE)
NVSL_ASSERT(this->raw_values.size() < RAW_VAL_CNT, OVERFLOW_MSG);
#else
if (this->raw_values.size() > RAW_VAL_CNT) {
fprintf(stderr, OVERFLOW_MSG "\n");
exit(1);
}
#endif
this->raw_values.push_back(elapsed);
this->events++;
}
void reset() {
this->running = false;
this->total_ns = 0;
this->events = 0;
this->raw_values.clear();
this->sorted_raw_values.clear();
}
size_t ns() const { return this->total_ns; }
size_t us() const { return this->total_ns / 1000; }
size_t ms() const { return this->total_ns / 1000000; }
size_t s() const { return this->total_ns / 1000000000; }
/** @brief Total time elapsed */
const std::string summarize() const {
std::stringstream ss;
size_t ns_total = this->ns();
ss << "Total ns: " << this->ns() << std::endl;
ss << "Total time: " << ns_to_hr_clk(ns_total) << std::endl;
return ss.str();
}
/**
* @brief Prepare clock object to calculate percentile/summary
* @details non-const function to update the internal state so that other
* functions can be const
*/
void reconcile() {
sorted_raw_values.reserve(raw_values.size());
std::copy(raw_values.begin(), raw_values.end(),
std::back_inserter(sorted_raw_values));
std::sort(sorted_raw_values.begin(), sorted_raw_values.end());
}
/**
* @brief Calculate the percentile value
* @param[in] pc Percentile out of 100
*/
size_t percentile(const size_t pc) const {
if (sorted_raw_values.size() == 0) {
#if defined(NVSL_ERROR)
NVSL_ERROR("Clock not reconcile. Call reconcile()");
#else
assert(0 && "Clock not reconciled. Call reconcile()");
#endif
}
const auto sz = sorted_raw_values.size();
const auto idx = std::max(0UL, (size_t)((sz * pc) / 100.0) - 1);
return sorted_raw_values[idx];
}
/**
* @brief Summary with operations per second
* @param[in] total_ops Total number of operations performed
* @param[in] distribution Generate distribution in summary
*/
std::string summarize(size_t total_ops, bool distribution = false) const {
std::stringstream ss;
#ifdef NVSL_ASSERT
NVSL_ASSERT(total_ops != 0, "total ops cannot be zero");
#else
assert(total_ops != 0 && "Total ops cannot be zero");
#endif
size_t ops_per_iter = total_ops / raw_values.size();
ss << this->summarize() << "ops: " << total_ops
<< "\nops/s: " << (total_ops * (1000000000)) / ((double)this->ns())
<< "\ntime/op: "
<< ns_to_hr_clk((size_t)(this->ns() / (double)total_ops))
<< "\nns/op: " << (this->ns() / (double)total_ops);
if (distribution) {
ss << "\np50/op: "
<< ns_to_hr_clk((size_t)(this->percentile(50)) / ops_per_iter)
<< "\np90/op: "
<< ns_to_hr_clk((size_t)(this->percentile(90)) / ops_per_iter)
<< "\np99/op: "
<< ns_to_hr_clk((size_t)(this->percentile(99)) / ops_per_iter)
<< "\ntime/op: "
<< ns_to_hr_clk((size_t)(this->ns() / (double)total_ops));
}
return ss.str();
}
size_t ns_per_op(size_t total_ops) const { return this->ns() / total_ops; }
size_t ns_per_event() const {
if (events != 0) {
return this->ns() / events;
} else {
return 0;
}
}
size_t percentile_per_op(const size_t total_ops, const size_t pc) const {
const auto ops_per_iter = total_ops / raw_values.size();
return this->percentile(pc) / ops_per_iter;
}
};
} // namespace nvsl