-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimed-task.h
More file actions
208 lines (165 loc) · 7.09 KB
/
timed-task.h
File metadata and controls
208 lines (165 loc) · 7.09 KB
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
200
201
202
203
204
205
206
207
208
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
#ifndef TIMED_TASK_WRAPPER_H
#define TIMED_TASK_WRAPPER_H
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
using namespace std;
typedef chrono::steady_clock Clock;
typedef chrono::time_point<Clock, chrono::nanoseconds> time_point_ns;
typedef chrono::nanoseconds duration_ns;
enum TimeUnit: uint64_t {
nanoseconds = 1,
microseconds = nanoseconds * 1'000,
milliseconds = microseconds * 1'000,
seconds = milliseconds * 1'000,
minutes = seconds * 60,
hours = minutes * 60
};
class StatisticsCollector {
private:
size_t samples = 0;
uint64_t acummulation = 0;
uint64_t compensation = 0;
uint64_t maxVariance = 0;
uint64_t minVariance = numeric_limits<uint64_t >::max();
size_t toleraceExcessionCount = 0;
public:
StatisticsCollector() = default;
virtual ~StatisticsCollector() = default;
void calcSleepCompensation(duration_ns amountSlept) {
compensation += amountSlept.count();
}
void calcExecTimeError(time_point_ns start, time_point_ns end, const duration_ns expected) {
auto execTime = (end - start);
uint64_t error = (chrono::abs(execTime - expected).count()); // both upper or lower differences are considered error, hence abs()
accumulate(error);
considerVariance(error);
if (execTime > (expected * 1.05)) { // 5% extra
toleraceExcessionCount++;
}
}
void accumulate(uint64_t amount) {
acummulation += amount;
samples++;
}
void considerVariance(uint64_t errorSample) {
maxVariance = std::max(errorSample, maxVariance);
minVariance = std::min(errorSample, minVariance);
}
void printResults(TimeUnit samplingUnit = TimeUnit::nanoseconds) {
uint64_t deviation_average_ns = acummulation / samples;
uint64_t compensation_average_ns = compensation / samples;
long double convertedDeviation = (double) deviation_average_ns / samplingUnit;
long double convertedCompensation = (double) compensation_average_ns / samplingUnit;
long double convertedMaxVariance = (double) maxVariance / samplingUnit;
long double convertedMinVariance = (double) minVariance / samplingUnit;
string unitName = "nanoseconds";
switch (samplingUnit) {
case nanoseconds: break;
case microseconds: unitName = "microseconds"; break;
case milliseconds: unitName = "milliseconds"; break;
case seconds: unitName = "seconds"; break;
case minutes: unitName = "minutes"; break;
case hours: unitName = "hours"; break;
}
cout.precision(6);
cout << " --------------- // --------------\n";
cout << "Samples taken: " << samples << endl;
cout << "Deviation average: " << fixed << convertedDeviation << " " << unitName << endl;
cout << "Compensation average: " << fixed << convertedCompensation << " " << unitName << endl;
cout << "Max variance: " << fixed << convertedMaxVariance << " " << unitName << endl;
cout << "Min variance: " << fixed << convertedMinVariance << " " << unitName << endl;
cout << "Tolerance exceeded " << toleraceExcessionCount << " times\n";
}
};
class TimerTask {
public:
TimerTask(uint64_t rate, TimeUnit unit, bool enableStatistics = true) :
rate_(rate),
ratio_(unit),
statisticsEnabled(enableStatistics) {
init();
}
TimerTask(const TimerTask&) = delete; // copy and assignment not allowed
TimerTask& operator=(const TimerTask&) = delete;
virtual ~TimerTask() {
if (running_) // don't call again if stop() was explicitly called
stop(); // if it goes out of scope, stop the timer
}
void setRate(uint64_t rate, TimeUnit unit, bool imediate = true) {
this->rate_ = rate;
this->ratio_ = unit;
if (imediate) {
restart(); // restart imediate
} else {
// todo wait the pending execution to restart | IDK how
}
}
void stop() {
if (workerThread_.joinable()) {
{
lock_guard<mutex> lock(mtx_);
running_ = false;
}
cv_.notify_one(); // wake up immediately
workerThread_.join();
}
if (statisticsEnabled) {
collector.printResults(milliseconds);
}
}
private:
void init() {
if (rate_ != 0) { // avoid a non-sleeping timer
workerThread_ = std::thread([&] {
running_ = true;
duration_ns full_sleep_time_nanos = std::chrono::nanoseconds(ratio_ * rate_);
for (;;) {
// we need to measure the execution to ensure we keep the cadence between cycles
auto start = Clock::now();
doAction(); // call virtual function
auto stop = Clock::now();
auto sleepTime = full_sleep_time_nanos - (stop - start); // compensate doAction timing
if (sleepTime.count() < 0) {
sleepTime = full_sleep_time_nanos - (chrono::abs(sleepTime) % full_sleep_time_nanos);
// there's no reason to know how many cycles were exceeded so we
// only get the remainder to know how much was exceeded in the CURRENT cycle
// the compensation only applies to next cycle where the action has finished
/* x means the sleep time, already compensated
* the space between each | is considered a full cycle of the timer
*
* if the action takes more than one cycle,
* compensation will take place at the surpassed cycle
* --->xxxx------>x----------->xxxx------->
* |-------|-------|-------|-------|-------|
* */
}
sleepTime -= duration_ns(50'000); // 50 µsec as offset for operations above and below
unique_lock<mutex> lock(mtx_); // usual proccess to sleep a condition variable
cout << sleepTime.count() << endl;
if (cv_.wait_for(lock, sleepTime, [this] { return !running_; })) {
break; // running == false, so exit this thread
}
collector.calcExecTimeError(start, Clock::now(), full_sleep_time_nanos);
collector.calcSleepCompensation(sleepTime);
}
});
}
}
void restart() {
stop(); // stop right now and start it again with the new rate
init();
}
virtual void doAction() = 0;
uint64_t rate_;
TimeUnit ratio_;
StatisticsCollector collector{};
bool statisticsEnabled;
bool running_{false};
thread workerThread_;
mutex mtx_;
condition_variable cv_;
};
#endif //TIMED_TASK_WRAPPER_H