otsdaq-mu2e-stm  5.02.01
dqm_manager.hh
1 #ifndef DQM_MANAGER_HH
2 #define DQM_MANAGER_HH
3 
4 #include <string>
5 #include <unordered_map>
6 #include <memory>
7 #include <thread>
8 #include <atomic>
9 
10 // Async Logger code
11 #include "Mu2e-STMDAQ/utils/async_logger.hh"
12 // Signal handler header
13 #include "Mu2e-STMDAQ/utils/signal_handler.hh"
14 // Data structs header
15 #include "Mu2e-STMDAQ/buffers/data_struct.hh"
16 // Shared memory helpers
17 #include "Mu2e-STMDAQ/dqm/dqm_shm_block.hh"
18 #include "Mu2e-STMDAQ/dqm/shm_manager.hh"
19 // Operations base header
20 #include "Mu2e-STMDAQ/processing/operations_base.hh"
21 
22 // Forward declare Boost shared memory classes to reduce header dependencies
23 namespace boost {
24  namespace interprocess {
25  class shared_memory_object;
26  class mapped_region;
27  }
28 }
29 
30 // Forward declare Op manager so can access number of operations
31 class OperationManager;
32 
33 
34 // Manager class that controls all shared memory blocks and updates them periodically
35 class DQM : public OperationMap {
36 
37 public:
38 
39  // Constructor registers shared memory
40  DQM(const Config& cfg_,
41  const std::shared_ptr<AsyncLogger>& logger,
42  const std::shared_ptr<STMdata>& stm_,
43  const std::shared_ptr<SignalHandler>& signal_,
44  OperationManager* op_man_);
45 
46  // Destructor stops and joins background thread
47  ~DQM(){
48  logger->log("DQM destructor called.",1);
49 
50  };
51 
52  // Need operations number after init
53  void init_shm();
54 
55  // Registers a new SHM block
56  template <typename T>
57  void registerBlock(DQMPageType type, const std::string& shm_name, size_t size);
58 
59  // Gets typed pointer to SHM block
60  template <typename T>
61  T* get(DQMPageType type);
62 
63 private:
64 
65  // Store reference to the Config instnace
66  const Config& cfg;
67 
68  // Async Logger
69  const std::shared_ptr<AsyncLogger>& logger;
70 
71  // STM data info
72  const std::shared_ptr<STMdata>& stm;
73 
74  // Shared SignalHandler
75  const std::shared_ptr<SignalHandler>& signal;
76 
77  // Operation manager pointer
78  OperationManager* op_man;
79 
80  // The reference baseline mean and sigma to be displayed
81  const double baseline_mean_prev;
82  const double baseline_sigma_prev;
83 
84  // Histogram bins in baseline
85  size_t baseline_nbins;
86 
87  // Max noise length
88  size_t noise_len;
89 
90  // Raw data length
91  size_t raw_len;
92 
93  // Peak hist param
94  size_t peak_nbins;
95  size_t min_ph;
96  float inv_wid;
97 
98  // Sliding window histogram
99  std::vector<uint64_t> peak_hist_window;
100  // All data histogram
101  std::vector<uint64_t> peak_hist_all;
102 
103  // The number of buffers in the sliding window histogram
104  const size_t window_size_buffers;
105  // Total histogram counts
106  uint64_t total_window = 0; // Window histogram
107  uint64_t total_all = 0; // All
108 
109  // Buffer histogram variables
110  std::vector<std::vector<uint64_t>> per_buffer_hists;
111  std::size_t window_index = 0;
112 
113  // Running counter of dropped packets
114  size_t num_dropped_packets;
115 
116  // Number of operations
117  size_t op_num;
118 
119  // Write alarms to shared memory
120  void alarm_info(std::shared_ptr<DataStruct>& buffer);
121 
122  // Histogram peak data for DQM
123  void histogram_peaks(std::shared_ptr<DataStruct>& buffer);
124 
125  // Periodic writer loop for shared memory updates
126  void update_dqm(std::shared_ptr<DataStruct>& buffer);
127 
128  // Periodic writer loop for baseline updates
129  void update_dqm_baseline(std::shared_ptr<DataStruct>& buffer);
130 
131  // Periodic writer loop for raw adc data
132  void update_dqm_raw(std::shared_ptr<DataStruct>& buffer);
133 
134  // Periodic writer loop for peak data
135  void update_dqm_peak(std::shared_ptr<DataStruct>& buffer);
136 
137  // Update CPU performace DQM
138  void update_cpu_performance(std::shared_ptr<DataStruct>& buffer);
139 
140  // Get the current time in ns
141  uint64_t get_current_time_ns();
142 
143  // SHM block map
144  std::unordered_map<DQMPageType, std::unique_ptr<DQMBlockBase>> blocks_;
145  // Background thread handle
146  std::thread updater_;
147  // Flag for thread control
148  std::atomic<bool> running_;
149 
150 };
151 
152 
153 // Include template implementation
154 #include "Mu2e-STMDAQ/dqm/dqm_manager.tpp"
155 
156 #endif // DQM_MANAGER_HH
Definition: config.hh:19
Definition: dqm.hh:51