DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
TPGInternalStateHarvester.hpp
Go to the documentation of this file.
1
8#pragma once
9
10#include <unordered_map>
11#include <string>
12#include <vector>
13#include <memory>
14#include <array>
15#include <cstdint>
16#include <thread>
17#include <atomic>
18#include <mutex>
19#include <condition_variable>
20#include <chrono>
21
22
24#include "trgdataformats/Types.hpp" // or the header that defines trgdataformats::channel_t
25
26namespace dunedaq {
27namespace fdreadoutlibs {
28
30public:
31 using ProcRef = std::pair<std::shared_ptr<tpglibs::AbstractProcessor<__m256i>>, int /*pipeline_id*/>;
32
33 // Destructor ensures proper thread cleanup
35
36 void set_processor_references(std::vector<ProcRef> refs);
37 const std::vector<ProcRef>& get_processor_references() const;
38
46 void update_channel_plane_numbers(const std::vector<std::pair<trgdataformats::channel_t,int16_t>>& channel_plane_numbers,
47 uint8_t num_channels_per_pipeline,
48 uint8_t num_pipelines);
49
55 std::unordered_map<trgdataformats::channel_t,
56 std::vector<std::pair<std::string,int16_t>>> harvest_once();
57
58 // --- Multi-threaded interface ---
59
65
71
76 void trigger_harvest();
77
84 std::unordered_map<trgdataformats::channel_t,
85 std::vector<std::pair<std::string,int16_t>>> get_latest_results() const;
86
93
94private:
95
96 // --- Original data structures ---
97 std::vector<ProcRef> m_processor_references;
98 // index: pipeline_id -> vector of (channel, plane) for its 16 lanes
99 std::vector<std::vector<std::pair<trgdataformats::channel_t,int16_t>>> m_channel_plane_numbers_per_pipeline;
102
103 // --- Preallocation caches (rebuilt when refs/channels change) ---
104 std::vector<std::vector<std::string>> m_metric_items_per_proc; // size == m_processor_references.size()
105 std::vector<size_t> m_expected_items_per_pipeline; // size == m_num_pipelines
106 size_t m_expected_total_channels = 0; // == m_num_channels_per_pipeline * m_num_pipelines
107
108 // --- Multi-threaded data structures ---
109 using ResultContainer = std::unordered_map<trgdataformats::channel_t,
110 std::vector<std::pair<std::string,int16_t>>>;
111
112 // Single result container with mutex protection
113 mutable std::mutex m_results_mutex;
115
116 // Thread synchronization
118 std::atomic<bool> m_thread_should_stop{false};
119 std::atomic<bool> m_thread_running{false};
120 std::atomic<bool> m_harvest_requested{false};
121
122 // Synchronization primitives
123 mutable std::mutex m_config_mutex; // Protects configuration changes
124 std::mutex m_collection_mutex; // Protects collection process
125 std::condition_variable m_collection_cv; // Signals collection thread
126
127 // Thread management
129
130 // Rebuild caches after refs or channel layout changes.
132
133};
134
135} // namespace fdreadoutlibs
136} // namespace dunedaq
void stop_collection_thread()
Stop the background collection thread Blocks until thread is fully stopped.
bool is_collection_thread_running() const
Check if collection thread is running.
void start_collection_thread()
Start the background collection thread Must be called before using async collection features.
std::vector< std::vector< std::pair< trgdataformats::channel_t, int16_t > > > m_channel_plane_numbers_per_pipeline
void trigger_harvest()
Signal the collection thread to perform one harvest cycle Non-blocking - returns immediately.
std::unordered_map< trgdataformats::channel_t, std::vector< std::pair< std::string, int16_t > > > harvest_once()
Harvest once, outputs channel -> [(metric_name, value)...].
std::unordered_map< trgdataformats::channel_t, std::vector< std::pair< std::string, int16_t > > > get_latest_results() const
Get the latest collected results (thread-safe, non-blocking) Returns a copy of the most recent harves...
std::pair< std::shared_ptr< tpglibs::AbstractProcessor< __m256i > >, int > ProcRef
const std::vector< ProcRef > & get_processor_references() const
std::vector< std::vector< std::string > > m_metric_items_per_proc
std::unordered_map< trgdataformats::channel_t, std::vector< std::pair< std::string, int16_t > > > ResultContainer
void update_channel_plane_numbers(const std::vector< std::pair< trgdataformats::channel_t, int16_t > > &channel_plane_numbers, uint8_t num_channels_per_pipeline, uint8_t num_pipelines)
Cuts a full list of (channel, plane) into per-pipeline lists of 16 lanes each.
The DUNE-DAQ namespace.