-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtimer.cpp
104 lines (95 loc) · 3.36 KB
/
timer.cpp
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
/* dns64perf++ - C++14 DNS64 performance tester
* Based on dns64perf by Gabor Lencse <[email protected]>
* (http://ipv6.tilb.sze.hu/dns64perf/)
* Copyright (C) 2017 Daniel Bakai <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#include "timer.h"
#include "spin_sleep.hpp"
#include <iostream>
Timer::Timer(const std::string &threadName, std::function<void(void)> &&prepare,
std::function<void(void)> &&task,
std::chrono::nanoseconds interval, size_t n)
: thread_name_{threadName}, prepare_{prepare}, task_{task},
interval_{interval}, n_{n}, stop_{false} {}
void Timer::run() {
std::chrono::high_resolution_clock::time_point before, starttime;
std::chrono::nanoseconds interval, function_execution_time, sleep_time,
full_time;
size_t n;
prepare_();
n = n_;
starttime = std::chrono::high_resolution_clock::now();
while (!stop_ && n > 0) {
before = std::chrono::high_resolution_clock::now();
interval =
n_ * interval_ - std::chrono::duration_cast<std::chrono::nanoseconds>(
before - starttime);
if (interval.count() < 0) {
interval = std::chrono::nanoseconds{0};
} else {
interval /= n;
}
task_();
function_execution_time =
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::high_resolution_clock::now() - before);
#ifdef DEBUG
if (function_execution_time > interval) {
std::cerr << "Can't keep up!" << std::endl;
}
#endif
--n;
sleep_time = interval - function_execution_time;
if (sleep_time.count() > 0) {
#ifdef DEBUG
before = std::chrono::high_resolution_clock::now();
#endif
spinsleep::sleep_for(sleep_time);
#ifdef DEBUG
std::chrono::nanoseconds real_sleep_time =
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::high_resolution_clock::now() - before);
double diff =
((double)real_sleep_time.count()) / ((double)sleep_time.count());
if (diff > 1.05 || diff < 0.95) {
fprintf(stderr, "Timer is off by %.02f%%!\n", (diff - 1) * 100);
}
#endif
}
}
full_time = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::high_resolution_clock::now() - starttime);
fprintf(stderr, "Full timer execution took %lu ns, %.02f%% of specified.\n",
full_time.count(),
((double)full_time.count() / (n_ * interval_.count())) * 100);
}
Timer::~Timer() {
if (!stop_) {
thread_.join();
}
}
void Timer::start() {
thread_ = std::thread{&Timer::run, this};
pthread_setname_np(thread_.native_handle(), thread_name_.c_str());
}
void Timer::stop() {
if (!stop_) {
stop_ = true;
thread_.join();
}
}