diff --git a/core-buffer/include/TypeMap.hpp b/core-buffer/include/TypeMap.hpp index 2967101..c07bf8c 100644 --- a/core-buffer/include/TypeMap.hpp +++ b/core-buffer/include/TypeMap.hpp @@ -3,9 +3,10 @@ #include #include +#include -enum class TypeMap { +enum class TypeList { VOID, CHAR, INT8, @@ -19,47 +20,50 @@ 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 ac42fa5..81c6a8d 100644 --- a/jfj-combined/include/JfjFrameCache.hpp +++ b/jfj-combined/include/JfjFrameCache.hpp @@ -30,15 +30,19 @@ **/ class FrameCache{ public: - FrameCache(uint64_t _C, uint64_t N_MOD, std::function callback): - m_CAP(_C), m_MOD(N_MOD), m_valid(_C, 0), m_fill(_C, 0), m_lock(_C), - m_buffer(_C, ImageBinaryFormat(512*N_MOD, 1024, sizeof(uint16_t))), - f_send(callback), m_watchdog(500, flush_all) { - + FrameCache(uint64_t N_CAP, uint64_t N_MOD, std::function callback): + m_CAP(N_CAP), m_MOD(N_MOD), m_valid(N_CAP, 0), m_fill(N_CAP, 0), m_lock(N_CAP), + m_buffer(N_CAP, ImageBinaryFormat(512*N_MOD, 1024, sizeof(uint16_t))), + f_send(callback) { // Initialize buffer metadata for(auto& it: m_buffer){ memset(&it.meta, 0, sizeof(it.meta)); } - // Start watchdog - m_watchdog.Start(); + + + std::function wd_callback = std::bind(&FrameCache::flush_all, this); + + m_watchdog = new Watchdog(500, wd_callback); + m_watchdog->Start(); + // Start drain worker m_drainer = std::thread(&FrameCache::drain_loop, this); }; @@ -57,11 +61,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); } } @@ -73,6 +77,7 @@ public: char* ptr_dest = m_buffer[idx].data.data() + moduleIDX * m_blocksize; std::memcpy((void*)ptr_dest, (void*)&inc_frame.data, m_blocksize); m_fill[idx]++; + m_watchdog->Kick(); // Queue for draining if(m_fill[idx]==m_MOD-1){ @@ -92,10 +97,10 @@ protected: 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_fill[idx] = 0; m_valid[idx] = 1; } @@ -148,9 +153,10 @@ protected: std::vector m_buffer; /** Watchdog timer and flush queue **/ - Watchdog m_watchdog; - std::thread m_drainer; + Watchdog *m_watchdog; + std::thread m_drainer; std::deque drain_queue(); + }; #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 1993e5b..14275cc 100644 --- a/jfj-combined/include/ZmqImagePublisher.hpp +++ b/jfj-combined/include/ZmqImagePublisher.hpp @@ -71,8 +71,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; } } };