otsdaq-mu2e-stm  5.02.01
dqm.hh
1 #ifndef DQM_HH
2 #define DQM_HH
3 
4 #include <boost/interprocess/shared_memory_object.hpp>
5 #include <boost/interprocess/mapped_region.hpp>
6 #include <vector>
7 #include <cstring>
8 #include <iostream>
9 #include <memory>
10 
11 // Async Logger code
12 #include "Mu2e-STMDAQ/utils/async_logger.hh"
13 // Signal handler header
14 #include "Mu2e-STMDAQ/utils/signal_handler.hh"
15 // Ring buffer code
16 #include "Mu2e-STMDAQ/utils/ring_buffer.hh"
17 // Operations base header
18 #include "Mu2e-STMDAQ/utils/operations_base.hh"
19 
20 using namespace boost::interprocess;
21 
22 // const int MAX_BUFFERS = 10; // Maximum buffers tracked
23 // const int BUFFER_MAX_ENTRIES = 100; // Max entries stored per buffer
24 // const int MAX_THREADS = 16; // Maximum number of worker threads
25 
26 // Structure to hold DQM data in shared memory
27 // struct DQMData {
28 // uint64_t totalBytesProcessed; // Total bytes processed across all threads
29 // size_t activeThreads; // Number of active worker threads
30 // uint64_t threadBytesProcessed[MAX_THREADS]; // Bytes processed per thread
31 // int16_t bufferData[MAX_BUFFERS][BUFFER_MAX_ENTRIES]; // Raw buffer data
32 // float bufferAverages[MAX_BUFFERS]; // Average of buffer values
33 // int bufferSizes[MAX_BUFFERS]; // Number of entries per buffer
34 // int bufferCount; // Total number of buffers
35 // float threshold; // Threshold for alerts
36 // int paused; // Pause flag for stopping monitoring
37 
38 
39 // double baseline_mean_prev; // previous run mean
40 // double baseline_rms_prev; // previous run rms
41 // double baseline_mean_current; // current run mean
42 // double baseline_rms_current; // current run rms
43 // double baseline_inst[MAX_BUFFERS]; // instantaneous values per buffer
44 // double baseline_inst_rms[MAX_BUFFERS]; // instantaneous rms
45 // uint64_t baseline_inst_time[MAX_BUFFERS]; // timestamps for instantaneous values
46 // int baseline_inst_count; // number of instantaneous points stored
47 
48 // };
49 
50 // Class for managing shared memory communication
51 class DQM : public OperationBase {
52 
53 private:
54 
55  // Store reference to the Config instance
56  const Config& cfg;
57  // Async Logger
58  const std::shared_ptr<AsyncLogger>& logger;
59  // Shared SignalHandler
60  const std::shared_ptr<SignalHandler>& signal;
61 
62  // The channel number
63  const int CHAN;
64 
65  // Shared memory object
66  boost::interprocess::shared_memory_object shm;
67  // Maps shared memory to process address space
68  boost::interprocess::mapped_region region;
69  // Shared memory name
70  std::string shm_name;
71  // Shared memory size
72  size_t shm_size;
73  // Track whether this instance created the shared memory
74  bool isOwner;
75 
76  // A function map for the operation manager
77  std::unordered_map<std::string, std::function<void(std::shared_ptr<DataStruct>&)>> functionMap;
78 
79 public:
80 
81  // Constructor
82  DQM(const Config& cfg_,
83  const std::shared_ptr<AsyncLogger>& logger_,
84  const std::shared_ptr<SignalHandler>& signal_,
85  const int CHAN_);
86 
87  // Destructor: Cleans up shared memory when the program exits
88  ~DQM() {
89  // try {
90  // // Loger to user
91  // std::cout << "Cleaning up shared memory: " << shm_name << std::endl;
92  // // Remove shared memory if it exists
93  // shared_memory_object::remove(shm_name.c_str());
94  // std::cout << "Shared memory removed successfully.\n";
95  // } catch (const std::exception& e) {
96  // std::cerr << "Error removing shared memory: " << e.what() << std::endl;
97  // }
98  std::cout << "DQM destructor called.\n";
99  }
100 
101  // Thread to get DQM data from buffer
102  void dqm_thread(std::shared_ptr<DataStruct>& buffer);
103 
104  // Function to update shared memory with buffer and processing statistics
105  // void updateBufferData(uint64_t bytesProcessed,
106  // const std::vector<uint64_t>& threadBytes,
107  // const std::vector<std::vector<int16_t>>& bufferData);
108 
109  // Execute function for the operation manager
110  void execute(const std::string& methodName, std::shared_ptr<DataStruct>& buffer) override {
111  if (functionMap.find(methodName) != functionMap.end()) {
112  functionMap[methodName](buffer);
113  } else {
114  std::cerr << "Error: Invalid method name '" << methodName << "' in DQM\n";
115  }
116  }
117 
118 };
119 
120 #endif // DQM_SHM_HH
Definition: config.hh:19
Definition: dqm.hh:51