From 11ecf556aeb65f3c4066a7ac073f876e13a8df65 Mon Sep 17 00:00:00 2001 From: Mohacsi Istvan Date: Wed, 7 Jul 2021 16:10:23 +0200 Subject: [PATCH] Watchdog timer seems fine --- core-buffer/include/TypeMap.hpp | 73 ++++++++++++---------- core-buffer/include/formats.hpp | 2 +- core-buffer/src/RamBuffer.cpp | 2 +- jfj-combined/include/JfjFrameCache.hpp | 28 ++++++--- jfj-combined/include/Watchdog.hpp | 11 ++-- jfj-combined/include/ZmqImagePublisher.hpp | 4 +- 6 files changed, 70 insertions(+), 50 deletions(-) diff --git a/core-buffer/include/TypeMap.hpp b/core-buffer/include/TypeMap.hpp index fb23ff7..6f3a4f8 100644 --- a/core-buffer/include/TypeMap.hpp +++ b/core-buffer/include/TypeMap.hpp @@ -1,10 +1,12 @@ #ifndef SF_DAQ_BUFFER_TYPEMAP_HPP #define SF_DAQ_BUFFER_TYPEMAP_HPP +#include #include +#include -enum class TypeMap { +enum class TypeList { VOID, CHAR, INT8, @@ -18,47 +20,52 @@ enum class TypeMap { FLOAT, DOUBLE, COMPLEX_FLOAT, - COMPLEX_DOUBLE + COMPLEX_DOUBLE, }; -struct Type{ + + + +struct DataType{ const size_t size; const int value; }; -const std::unordered_map TypeTable = { - { typeid(void), {sizeof(void), TypeMap::VOID} }, - { typeid(char), {sizeof(char), TypeMap::CHAR} }, - { typeid(int8_t), {sizeof(int8_t), TypeMap::INT8} }, - { typeid(uint8_t), {sizeof(uint8_t), TypeMap::UINT8} }, - { typeid(int16_t), {sizeof(int16_t), TypeMap::INT16} }, - { typeid(uint16_t), {sizeof(uint16_t), TypeMap::UINT16} }, - { typeid(int32_t), {sizeof(int32_t), TypeMap::INT32} }, - { typeid(uint32_t), {sizeof(uint32_t), TypeMap::UINT32} }, - { typeid(int64_t), {sizeof(int64_t), TypeMap::INT64} }, - { typeid(uint64_t), {sizeof(uint64_t), TypeMap::UINT64} }, - { typeid(float), {sizeof(float), TypeMap::float} }, - { typeid(double), {sizeof(double), TypeMap::DOUBLE} }, - { typeid(std::complex), {sizeof(std::complex), TypeMap::COMPLEX_FLOAT} }, - { typeid(std::complex), {sizeof(std::complex), TypeMap::COMPLEX_DOUBLE} } +const std::unordered_map TypeTable = { + { typeid(void), {sizeof(char), (int)TypeList::VOID} }, + { typeid(char), {sizeof(char), (int)TypeList::CHAR} }, + { typeid(int8_t), {sizeof(int8_t), (int)TypeList::INT8} }, + { typeid(uint8_t), {sizeof(uint8_t), (int)TypeList::UINT8} }, + { typeid(int16_t), {sizeof(int16_t), (int)TypeList::INT16} }, + { typeid(uint16_t), {sizeof(uint16_t), (int)TypeList::UINT16} }, + { typeid(int32_t), {sizeof(int32_t), (int)TypeList::INT32} }, + { typeid(uint32_t), {sizeof(uint32_t), (int)TypeList::UINT32} }, + { typeid(int64_t), {sizeof(int64_t), (int)TypeList::INT64} }, + { typeid(uint64_t), {sizeof(uint64_t), (int)TypeList::UINT64} }, + { typeid(float), {sizeof(float), (int)TypeList::FLOAT} }, + { typeid(double), {sizeof(double), (int)TypeList::DOUBLE} }, + { typeid(std::complex), {sizeof(std::complex), (int)TypeList::COMPLEX_FLOAT} }, + { typeid(std::complex), {sizeof(std::complex), (int)TypeList::COMPLEX_DOUBLE} }, }; -const std::unordered_map TypeTable = { - { TypeMap::VOID, {sizeof(void), TypeMap::VOID} }, - { TypeMap::CHAR, {sizeof(char), TypeMap::CHAR} }, - { TypeMap::INT8, {sizeof(int8_t), TypeMap::INT8} }, - { TypeMap::UINT8, {sizeof(uint8_t), TypeMap::UINT8} }, - { TypeMap::INT16, {sizeof(int16_t), TypeMap::INT16} }, - { TypeMap::UINT16, {sizeof(uint16_t), TypeMap::UINT16} }, - { TypeMap::INT32, {sizeof(int32_t), TypeMap::INT32} }, - { TypeMap::UINT32, {sizeof(uint32_t), TypeMap::UINT32} }, - { TypeMap::INT64, {sizeof(int64_t), TypeMap::INT64} }, - { TypeMap::UINT64, {sizeof(uint64_t), TypeMap::UINT64} }, - { TypeMap::FLOAT, {sizeof(float), TypeMap::float} }, - { TypeMap::DOUBLE, {sizeof(double), TypeMap::DOUBLE} }, - { TypeMap::COMPLEX_FLOAT, {sizeof(std::complex), TypeMap::COMPLEX_FLOAT} }, - { TypeMap::COMPLEX_DOUBLE, {sizeof(std::complex), TypeMap::COMPLEX_DOUBLE} } + +const std::unordered_map TypeMap = { + { (int)TypeList::VOID, {sizeof(char), (int)TypeList::VOID} }, + { (int)TypeList::CHAR, {sizeof(char), (int)TypeList::CHAR} }, + { (int)TypeList::INT8, {sizeof(int8_t), (int)TypeList::INT8} }, + { (int)TypeList::UINT8, {sizeof(uint8_t), (int)TypeList::UINT8} }, + { (int)TypeList::INT16, {sizeof(int16_t), (int)TypeList::INT16} }, + { (int)TypeList::UINT16, {sizeof(uint16_t), (int)TypeList::UINT16} }, + { (int)TypeList::INT32, {sizeof(int32_t), (int)TypeList::INT32} }, + { (int)TypeList::UINT32, {sizeof(uint32_t), (int)TypeList::UINT32} }, + { (int)TypeList::INT64, {sizeof(int64_t), (int)TypeList::INT64} }, + { (int)TypeList::UINT64, {sizeof(uint64_t), (int)TypeList::UINT64} }, + { (int)TypeList::FLOAT, {sizeof(float), (int)TypeList::FLOAT} }, + { (int)TypeList::DOUBLE, {sizeof(double), (int)TypeList::DOUBLE} }, + { (int)TypeList::COMPLEX_FLOAT, {sizeof(std::complex), (int)TypeList::COMPLEX_FLOAT} }, + { (int)TypeList::COMPLEX_DOUBLE, {sizeof(std::complex), (int)TypeList::COMPLEX_DOUBLE} }, }; + #endif // SF_DAQ_BUFFER_TYPEMAP_HPP diff --git a/core-buffer/include/formats.hpp b/core-buffer/include/formats.hpp index 462deb5..735b04f 100644 --- a/core-buffer/include/formats.hpp +++ b/core-buffer/include/formats.hpp @@ -38,7 +38,7 @@ struct ImageMetadata { uint64_t user_1; // extra field for custom needs uint64_t user_2; // extra field for custom needs -} +}; #pragma pack(pop) struct ModuleFrameBuffer { diff --git a/core-buffer/src/RamBuffer.cpp b/core-buffer/src/RamBuffer.cpp index ef5f349..1b90dd9 100644 --- a/core-buffer/src/RamBuffer.cpp +++ b/core-buffer/src/RamBuffer.cpp @@ -169,7 +169,7 @@ char* RamBuffer::read_image(const uint64_t pulse_id) const void RamBuffer::write_image(const ImageMetadata& src_meta, const char *src_data) { const int slot_n = src_meta.id % n_slots_; - const int image_n_bytes = src_meta.height * src_meta.width * TypeMap[src_meta.dtype].size; + const int image_n_bytes = src_meta.height * src_meta.width * TypeMap.at(src_meta.dtype).size; char *dst_data = image_buffer_ + (image_n_bytes * slot_n); diff --git a/jfj-combined/include/JfjFrameCache.hpp b/jfj-combined/include/JfjFrameCache.hpp index aa6e185..e874c23 100644 --- a/jfj-combined/include/JfjFrameCache.hpp +++ b/jfj-combined/include/JfjFrameCache.hpp @@ -27,13 +27,20 @@ public: FrameCache(uint64_t _C, uint64_t N_MOD, std::function callback): m_CAP(_C), m_M(N_MOD), m_valid(_C, 0), m_lock(_C), m_buffer(_C, ImageBinaryFormat(512*N_MOD, 1024, sizeof(uint16_t))), - f_send(callback), m_watchdog(500, flush_all) { + f_send(callback) { // Initialize buffer metadata for(auto& it: m_buffer){ memset(&it.meta, 0, sizeof(it.meta)); } - m_watchdog.Start(); + + + std::function wd_callback = std::bind(&FrameCache::flush_all, this); + + m_watchdog = new Watchdog(500, wd_callback); + m_watchdog->Start(); }; + + /** Emplace Place a recorded frame to it's corresponding module location. @@ -47,11 +54,11 @@ public: const uint64_t idx = pulseID % m_CAP; // A new frame is starting - if(inc_frame.meta.pulse_id != m_buffer[idx].meta.pulse_id){ + if(inc_frame.meta.pulse_id != m_buffer[idx].meta.id){ // Unique lock to flush and start a new one std::unique_lock p_guard(m_lock[idx]); // Check if condition persists after getting the mutex - if(inc_frame.meta.pulse_id != m_buffer[idx].meta.pulse_id){ + if(inc_frame.meta.pulse_id != m_buffer[idx].meta.id){ start_line(idx, inc_frame.meta); } } @@ -62,6 +69,9 @@ public: // Calculate destination pointer and copy data char* ptr_dest = m_buffer[idx].data.data() + moduleIDX * m_blocksize; std::memcpy((void*)ptr_dest, (void*)&inc_frame.data, m_blocksize); + + m_watchdog->Kick(); + } void flush_all(){ @@ -91,10 +101,10 @@ public: if(m_valid[idx]){ f_send(m_buffer[idx]); } // 2. Init new frame - m_buffer[idx].meta.pulse_id = inc_frame.pulse_id; - m_buffer[idx].meta.frame_index = inc_frame.frame_index; - m_buffer[idx].meta.daq_rec = inc_frame.daq_rec; - m_buffer[idx].meta.is_good_image = true; + m_buffer[idx].meta.id = inc_frame.pulse_id; + m_buffer[idx].meta.user_1 = inc_frame.frame_index; + m_buffer[idx].meta.user_2 = inc_frame.daq_rec; + m_buffer[idx].meta.status = true; m_valid[idx] = 1; } @@ -112,7 +122,7 @@ private: std::vector m_buffer; /** Watchdog timer **/ - Watchdog m_watchdog; + Watchdog *m_watchdog; }; #endif // SF_DAQ_FRAME_CACHE_HPP diff --git a/jfj-combined/include/Watchdog.hpp b/jfj-combined/include/Watchdog.hpp index 0758273..2cbe84b 100644 --- a/jfj-combined/include/Watchdog.hpp +++ b/jfj-combined/include/Watchdog.hpp @@ -14,17 +14,17 @@ **/ class Watchdog{ public: - Watchdog(uint32_t timeout, std::function callback): m_timeout(timeout), m_callback(callback) {}; + Watchdog(int64_t timeout, std::function callback): m_timeout(timeout), m_callback(callback) {}; ~Watchdog() { Stop(); }; void Start(); void Stop(); void Kick(); protected: - uint32_t m_timeout; + int64_t m_timeout; std::atomic m_running = false; std::function m_callback; - std::chrono::time_point m_lastkick; + std::chrono::time_point m_lastkick; std::thread m_thread; std::mutex m_mutex; @@ -55,8 +55,11 @@ void Watchdog::Kick(){ } void Watchdog::Loop(){ + std::cout << "Starting watchdog" << std::endl; while(m_running){ - if((std::chrono::now() - m_lastkick) < m_timeout){ + auto elapsed = std::chrono::duration_cast(std::chrono::steady_clock::now() - m_lastkick); + if(elapsed.count() < m_timeout){ + // std::cout << "Elapsed " << (int64_t)elapsed.count() << " of " << m_timeout << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(1)); } else { std::cout << "Expired timer" << std::endl; diff --git a/jfj-combined/include/ZmqImagePublisher.hpp b/jfj-combined/include/ZmqImagePublisher.hpp index a53751f..cc1dea1 100644 --- a/jfj-combined/include/ZmqImagePublisher.hpp +++ b/jfj-combined/include/ZmqImagePublisher.hpp @@ -70,8 +70,8 @@ class ZmqImagePublisher: public ZmqPublisher { len = m_socket.send(image.data.data(), image.data.size(), 0); ASSERT_TRUE( len >=0, "Failed to send image data" ) - if(image.meta.pulse_id%100==0){ - std::cout << "Sent ZMQ stream of pulse: " << image.meta.pulse_id << std::endl; + if(image.meta.id%100==0){ + std::cout << "Sent ZMQ stream of pulse: " << image.meta.id << std::endl; } } };