Directory: | ./ |
---|---|
File: | src/realtime_check.cpp |
Date: | 2022-06-29 13:58:11 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 39 | 68 | 57.4% |
Branches: | 18 | 44 | 40.9% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * @file realtime_check.cpp | ||
3 | * @author Maximilien Naveau (maximilien.naveau@gmail.com) | ||
4 | * license License BSD-3-Clause | ||
5 | * @copyright Copyright (c) 2019, New York University and Max Planck | ||
6 | * Gesellschaft. | ||
7 | * @date 2019-05-22 | ||
8 | * | ||
9 | * @brief Utilities to check if the real_time capabilities of an algorithm is | ||
10 | * maintained or not. | ||
11 | */ | ||
12 | #include "real_time_tools/realtime_check.hpp" | ||
13 | #include <fstream> | ||
14 | |||
15 | namespace real_time_tools | ||
16 | { | ||
17 | 1 | RealTimeCheck::RealTimeCheck(double target_frequency, double switch_frequency) | |
18 | { | ||
19 | 1 | this->target_frequency = target_frequency; | |
20 | 1 | this->started = false; | |
21 | 1 | this->ticks = 0; | |
22 | 1 | this->switchs = 0; | |
23 | 1 | this->worse_frequency = std::numeric_limits<double>::max(); | |
24 | 1 | this->current_frequency = std::numeric_limits<double>::max(); | |
25 | 1 | this->switch_frequency = switch_frequency; | |
26 | 1 | this->average_frequency = -1.0; | |
27 | 1 | } | |
28 | |||
29 | ✗ | bool RealTimeCheck::was_realtime_lost() const | |
30 | { | ||
31 | ✗ | if (!this->started) | |
32 | { | ||
33 | ✗ | return false; | |
34 | } | ||
35 | |||
36 | ✗ | if (this->switchs > 0) | |
37 | { | ||
38 | ✗ | return true; | |
39 | } | ||
40 | |||
41 | ✗ | return false; | |
42 | } | ||
43 | |||
44 | 400 | void RealTimeCheck::tick() | |
45 | { | ||
46 |
1/2✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
|
799 | std::lock_guard<std::mutex> guard(this->mutex); |
47 | |||
48 | std::chrono::high_resolution_clock::time_point t = | ||
49 | 400 | std::chrono::high_resolution_clock::now(); | |
50 | |||
51 | 400 | this->ticks += 1; | |
52 | |||
53 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 400 times.
|
400 | if (this->ticks >= std::numeric_limits<uint>::max() - 1) |
54 | { | ||
55 | ✗ | this->started = false; | |
56 | ✗ | return; | |
57 | } | ||
58 | |||
59 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 399 times.
|
400 | if (!this->started) |
60 | { | ||
61 | 1 | this->start_time = t; | |
62 | 1 | this->last_tick = t; | |
63 | 1 | this->started = true; | |
64 | 1 | return; | |
65 | } | ||
66 | |||
67 | // checking if current frequency (as of previous tick) is fine | ||
68 | |||
69 |
1/2✓ Branch 1 taken 399 times.
✗ Branch 2 not taken.
|
798 | auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>( |
70 |
1/2✓ Branch 1 taken 399 times.
✗ Branch 2 not taken.
|
798 | t - this->last_tick) |
71 | 399 | .count(); | |
72 | |||
73 | 399 | double one = 1.0; | |
74 | |||
75 | 399 | current_frequency = one / (pow(10.0, -9.0) * static_cast<double>(nanos)); | |
76 | |||
77 |
2/2✓ Branch 0 taken 398 times.
✓ Branch 1 taken 1 times.
|
399 | if (current_frequency < this->switch_frequency) |
78 | { | ||
79 | 398 | this->switchs += 1; | |
80 | } | ||
81 | |||
82 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 391 times.
|
399 | if (current_frequency < this->worse_frequency) |
83 | { | ||
84 | 8 | this->worse_frequency = current_frequency; | |
85 | } | ||
86 | |||
87 | // updating global frequency | ||
88 | |||
89 |
1/2✓ Branch 1 taken 399 times.
✗ Branch 2 not taken.
|
399 | nanos = std::chrono::duration_cast<std::chrono::nanoseconds>( |
90 |
1/2✓ Branch 1 taken 399 times.
✗ Branch 2 not taken.
|
798 | t - this->start_time) |
91 | .count(); | ||
92 | |||
93 | 798 | this->average_frequency = static_cast<double>(this->ticks) / | |
94 | 399 | (pow(10.0, -9.0) * static_cast<double>(nanos)); | |
95 | |||
96 | // preparing for next iteration | ||
97 | |||
98 |
2/2✓ Branch 1 taken 399 times.
✓ Branch 2 taken 1 times.
|
399 | this->last_tick = t; |
99 | } | ||
100 | |||
101 | ✗ | double RealTimeCheck::get_current_frequency() const | |
102 | { | ||
103 | ✗ | return current_frequency; | |
104 | } | ||
105 | |||
106 | ✗ | bool RealTimeCheck::get_statistics(int &ticks, | |
107 | int &switchs, | ||
108 | double &target_frequency, | ||
109 | double &switch_frequency, | ||
110 | double &average_frequency, | ||
111 | double ¤t_frequency, | ||
112 | double &worse_frequency) | ||
113 | { | ||
114 | ✗ | std::lock_guard<std::mutex> guard(this->mutex); | |
115 | |||
116 | ✗ | if (!this->started) | |
117 | { | ||
118 | ✗ | return false; | |
119 | } | ||
120 | |||
121 | ✗ | ticks = this->ticks; | |
122 | ✗ | switchs = this->switchs; | |
123 | ✗ | average_frequency = this->average_frequency; | |
124 | ✗ | worse_frequency = this->worse_frequency; | |
125 | ✗ | current_frequency = this->current_frequency; | |
126 | ✗ | target_frequency = this->target_frequency; | |
127 | ✗ | switch_frequency = this->switch_frequency; | |
128 | |||
129 | ✗ | return true; | |
130 | } | ||
131 | |||
132 | 1 | double RealTimeCheck::get_average_frequency() | |
133 | { | ||
134 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | std::lock_guard<std::mutex> guard(this->mutex); |
135 | |||
136 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!this->started) |
137 | { | ||
138 | ✗ | return -1.0; | |
139 | } | ||
140 | |||
141 | 1 | return this->average_frequency; | |
142 | } | ||
143 | |||
144 | ✗ | void RealTimeCheck::print() | |
145 | { | ||
146 | int ticks, switchs; | ||
147 | double average_frequency; | ||
148 | double worse_frequency; | ||
149 | double current_frequency; | ||
150 | double target_frequency; | ||
151 | double switch_frequency; | ||
152 | |||
153 | bool ret = get_statistics(ticks, | ||
154 | switchs, | ||
155 | target_frequency, | ||
156 | switch_frequency, | ||
157 | average_frequency, | ||
158 | current_frequency, | ||
159 | ✗ | worse_frequency); | |
160 | |||
161 | ✗ | if (!ret) | |
162 | { | ||
163 | ✗ | printf("failed to get results from realtime check\n"); | |
164 | ✗ | return; | |
165 | } | ||
166 | |||
167 | ✗ | printf( | |
168 | "nb ticks: %d\t" | ||
169 | "nb switchs: %d (i.e below %f)\t" | ||
170 | "target_freq: %f\t" | ||
171 | "average: %f\t" | ||
172 | "current: %f\t" | ||
173 | "worse: %f\n", | ||
174 | ticks, | ||
175 | switchs, | ||
176 | switch_frequency, | ||
177 | target_frequency, | ||
178 | average_frequency, | ||
179 | current_frequency, | ||
180 | worse_frequency); | ||
181 | } | ||
182 |
2/4✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
|
42 | } // namespace real_time_tools |
183 |