RTC Toolkit 6.0.0-pre2
Loading...
Searching...
No Matches
rtctkGenRtrWriter.h
Go to the documentation of this file.
1
12#ifndef RTCTK_GEN_RTR_WRITER_
13#define RTCTK_GEN_RTR_WRITER_
14
18
19#include <boost/accumulators/accumulators.hpp>
20#include <boost/accumulators/statistics/count.hpp>
21#include <boost/accumulators/statistics/max.hpp>
22#include <boost/accumulators/statistics/mean.hpp>
23#include <boost/accumulators/statistics/min.hpp>
24#include <boost/accumulators/statistics/stats.hpp>
25#include <boost/accumulators/statistics/variance.hpp>
26
27namespace rtctk::standaloneTools {
28
29namespace rtctk_cfw = rtctk::componentFramework;
30using namespace std::chrono;
31namespace ba = boost::accumulators;
32
37protected:
38 log4cplus::Logger& m_logger;
39 uint16_t m_idx;
40 std::shared_ptr<rtctk_cfw::RuntimeRepoIf> m_rtr;
41 rtctk_cfw::DataPointPath m_dp;
44
45 std::chrono::microseconds m_sleep_period;
47
48 std::thread m_thread;
49 std::atomic<bool> m_thread_running = true;
50
51 ba::accumulator_set<
52 unsigned,
53 ba::stats<ba::tag::mean, ba::tag::variance, ba::tag::min, ba::tag::max, ba::tag::count>>
55
56public:
57 explicit GenRtrWriterBase(std::shared_ptr<rtctk_cfw::RuntimeRepoIf>& rtr,
58 const rtctk_cfw::DataPointPath& dp)
59 : m_logger(rtctk_cfw::GetLogger("rtctk")), m_rtr(rtr), m_dp(dp) {
60 s_idx++;
61 }
62
63 virtual ~GenRtrWriterBase() noexcept {
64 --s_idx;
65 }
66
67 GenRtrWriterBase() = delete;
70
71 virtual void Initialize(std::size_t dim_x,
72 std::size_t dim_y,
73 std::chrono::microseconds period,
74 uint32_t iterations) = 0;
75
77 m_thread_running = false;
78 } // SignalThreadExit
79
83 void Finalize() {
84 LOG4CPLUS_INFO_FMT(m_logger, "[%s] Finalize", m_dp.ToString().c_str());
85
87
88 if (m_thread.joinable()) {
89 m_thread.join();
90 }
91 //? should we check if dp exists?
92 if (m_dp_created) {
93 LOG4CPLUS_DEBUG_FMT(m_logger, "[%s] Going to delete DP.", m_dp.ToString().c_str());
94 m_rtr->DeleteDataPoint(m_dp);
95 }
96 } // Finalize
97
98 void Join() {
99 m_thread.join();
100 } // Join
101
102 // index of Generic RTR Writer
103 static std::atomic<uint16_t> s_idx;
104}; // GenRtrWriterBase
105
114template <typename T>
116 std::unique_ptr<rtctk_cfw::MatrixBuffer<T>> m_value;
117
121 void WriteDataPoint() {
122 uint16_t error_counter = 0;
123 if (m_iteration_num > 0) {
124 LOG4CPLUS_INFO_FMT(m_logger,
125 "[%s] Going to write value of size: %ld x %ld (%d bytes) %d times.",
126 m_dp.ToString().c_str(),
127 m_value->GetNrows(),
128 m_value->GetNcols(),
131 } else {
132 LOG4CPLUS_INFO_FMT(
133 m_logger,
134 "[%s] Going to write value of size: %ld x %ld (%d bytes) until Ctrl-C.",
135 m_dp.ToString().c_str(),
136 m_value->GetNrows(),
137 m_value->GetNcols(),
139 }
140
141 // Enter the work loop: incrementing iteration counter
142 // and write value to RTR's DP. We continue until the thread is running.
143 uint32_t iteration_counter = 0;
144 while (m_thread_running && (m_iteration_num == 0 ||
145 (m_iteration_num > 0 && iteration_counter < m_iteration_num))) {
146 try {
147 if (iteration_counter % 100 == 0) {
148 LOG4CPLUS_DEBUG_FMT(m_logger,
149 "[%s] Going to write DP. Iteration: %d.",
150 m_dp.ToString().c_str(),
151 iteration_counter);
152 }
153 auto timestamp = high_resolution_clock::now(); // timestamp
154 //? set timestamp to value to be written to RTR
155 auto t1 = std::chrono::steady_clock::now();
156 // write DP
157 m_rtr->WriteDataPoint(m_dp, *m_value);
158 //? EH
159
160 auto t2 = std::chrono::steady_clock::now();
161 unsigned t_diff =
162 std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
163 m_accumulator(t_diff);
164 error_counter = 0;
165 if (iteration_counter % 100 == 0) {
166 auto timestamp_nanos =
167 duration_cast<nanoseconds>(timestamp.time_since_epoch()).count();
168 double timestamp_s = static_cast<double>(timestamp_nanos) / 1e9;
169 LOG4CPLUS_DEBUG_FMT(
170 m_logger,
171 "[%s]\tWrote to DP at time (epoch): %f. Iteration: %d.\tWrite "
172 "time [us]: min: %u max: %u mean: %.2f variance: %.2f",
173 m_dp.ToString().c_str(),
174 timestamp_s,
175 iteration_counter,
176 ba::min(m_accumulator),
177 ba::max(m_accumulator),
178 ba::mean(m_accumulator),
179 ba::variance(m_accumulator));
180 m_accumulator = {};
181 }
182 iteration_counter++;
183 } catch (const std::exception& ex) {
184 LOG4CPLUS_WARN_FMT(m_logger,
185 "[%s ] Iteration: %d exception caught: %s!",
186 m_dp.ToString().c_str(),
187 iteration_counter,
188 ex.what());
189 if (error_counter++ > 5) {
190 LOG4CPLUS_ERROR_FMT(
191 m_logger,
192 "[%s] More than 5 consecutive exception caught. Writing to RTR stoped!",
193 m_dp.ToString().c_str());
194 break;
195 } // if
196 } catch (...) {
197 LOG4CPLUS_WARN_FMT(m_logger,
198 "[%s ] Iteration: %d unknown exception caught!",
199 m_dp.ToString().c_str(),
200 iteration_counter);
201 if (error_counter++ > 5) {
202 LOG4CPLUS_ERROR_FMT(
203 m_logger,
204 "[%s] More than 5 consecutive exception caught. Writing to RTR stoped!",
205 m_dp.ToString().c_str());
206 break;
207 } // if
208 }
209
210 std::this_thread::sleep_for(m_sleep_period);
211 } // while
212
213 LOG4CPLUS_INFO_FMT(m_logger,
214 "[%s] %u / %u values have been written. DONE!",
215 m_dp.ToString().c_str(),
216 iteration_counter,
218 LOG4CPLUS_DEBUG_FMT(m_logger,
219 "[%s]\tWrite "
220 "time [us]: min: %u max: %u med: %.2f variance: %.2f",
221 m_dp.ToString().c_str(),
222 ba::min(m_accumulator),
223 ba::max(m_accumulator),
224 ba::mean(m_accumulator),
225 ba::variance(m_accumulator));
226 } // WriteDataPoint
227
228public:
229 explicit GenRtrWriter(std::shared_ptr<rtctk_cfw::RuntimeRepoIf>& rtr,
230 const rtctk_cfw::DataPointPath& dp)
231 : GenRtrWriterBase(rtr, dp) {
232 }
233
234 virtual ~GenRtrWriter() noexcept {
235 }
236
237 GenRtrWriter() = delete;
238 GenRtrWriter(const GenRtrWriter&) = delete;
240
249 void Initialize(std::size_t dim_x,
250 std::size_t dim_y,
251 std::chrono::microseconds period,
252 uint32_t iterations) {
253 m_sleep_period = period;
254 m_iteration_num = iterations;
255 m_value = std::make_unique<rtctk_cfw::MatrixBuffer<T>>();
256 m_value->resize(dim_x, dim_y);
257 LOG4CPLUS_INFO_FMT(m_logger,
258 "[%s] Created matrix of size: %ld x %ld",
259 m_dp.ToString().c_str(),
260 dim_x,
261 dim_y);
262 m_payload_bytes = m_value->GetNrows() * m_value->GetNcols() * sizeof(T);
263
264 if (!m_rtr->DataPointExists(m_dp)) {
265 try {
266 LOG4CPLUS_DEBUG_FMT(m_logger,
267 "[%s] DP does not exist going to create one.",
268 m_dp.ToString().c_str());
269 m_rtr->CreateDataPoint<rtctk_cfw::MatrixBuffer<T>>(m_dp, *m_value);
270 m_dp_created = true;
271 } catch (const std::exception& ex) {
272 // we need to check if the DP has been created, and thus the problem probably just
273 // to write a value ..
274 // ... in such a case we need to delete it
275 if (m_rtr->DataPointExists(m_dp)) {
276 LOG4CPLUS_WARN_FMT(m_logger,
277 "[%s] Going to delete DP after an error.",
278 m_dp.ToString().c_str());
279 m_rtr->DeleteDataPoint(m_dp);
280 }
281 CII_THROW_WITH_NESTED(
282 rtctk_cfw::RtctkException,
283 ex,
284 "[" + m_dp.ToString() + "] Problem creating DP and setting inital value");
285 } // try-catch
286 } else {
287 LOG4CPLUS_DEBUG_FMT(m_logger, "[%s] DP exists.", m_dp.ToString().c_str());
288 m_dp_created = false;
289 } // if-else
290
291 m_thread = std::thread(&GenRtrWriter::WriteDataPoint, this);
292 } // Initialize
293
294}; // GenRtrWriter
295
296} // namespace rtctk::standaloneTools
297
298#endif // RTCTK_GEN_RTR_WRITER_
uint32_t m_payload_bytes
Definition rtctkGenRtrWriter.h:43
std::chrono::microseconds m_sleep_period
Definition rtctkGenRtrWriter.h:45
std::thread m_thread
Definition rtctkGenRtrWriter.h:48
static std::atomic< uint16_t > s_idx
Definition rtctkGenRtrWriter.h:103
rtctk_cfw::DataPointPath m_dp
Definition rtctkGenRtrWriter.h:41
GenRtrWriterBase(std::shared_ptr< rtctk_cfw::RuntimeRepoIf > &rtr, const rtctk_cfw::DataPointPath &dp)
Definition rtctkGenRtrWriter.h:57
virtual ~GenRtrWriterBase() noexcept
Definition rtctkGenRtrWriter.h:63
GenRtrWriterBase(GenRtrWriterBase &&)=delete
GenRtrWriterBase(const GenRtrWriterBase &)=delete
void Finalize()
Stops publishing thread, and join it, delete data point (DP).
Definition rtctkGenRtrWriter.h:83
uint16_t m_idx
Definition rtctkGenRtrWriter.h:39
uint32_t m_iteration_num
Definition rtctkGenRtrWriter.h:46
bool m_dp_created
Definition rtctkGenRtrWriter.h:42
log4cplus::Logger & m_logger
Definition rtctkGenRtrWriter.h:38
virtual void Initialize(std::size_t dim_x, std::size_t dim_y, std::chrono::microseconds period, uint32_t iterations)=0
std::atomic< bool > m_thread_running
Definition rtctkGenRtrWriter.h:49
void SignalThreadExit()
Definition rtctkGenRtrWriter.h:76
void Join()
Definition rtctkGenRtrWriter.h:98
ba::accumulator_set< unsigned, ba::stats< ba::tag::mean, ba::tag::variance, ba::tag::min, ba::tag::max, ba::tag::count > > m_accumulator
Definition rtctkGenRtrWriter.h:54
std::shared_ptr< rtctk_cfw::RuntimeRepoIf > m_rtr
Definition rtctkGenRtrWriter.h:40
void Initialize(std::size_t dim_x, std::size_t dim_y, std::chrono::microseconds period, uint32_t iterations)
Creates Data Point (DP) and publishing thread.
Definition rtctkGenRtrWriter.h:249
virtual ~GenRtrWriter() noexcept
Definition rtctkGenRtrWriter.h:234
GenRtrWriter(std::shared_ptr< rtctk_cfw::RuntimeRepoIf > &rtr, const rtctk_cfw::DataPointPath &dp)
Definition rtctkGenRtrWriter.h:229
GenRtrWriter(GenRtrWriter &&)=delete
GenRtrWriter(const GenRtrWriter &)=delete
log4cplus::Logger & GetLogger(const std::string &name="app")
Get handle to a specific logger.
Definition logger.cpp:192
Logging Support Library based on log4cplus.
Declaration of the MatrixSpan template class used in APIs.
Definition commandReplier.cpp:22
Definition genDdsPublisher.hpp:20
Header file for RuntimeRepoIf, which defines the API for RuntimeRepoAdapters.