artdaq  v2_03_02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
EventStore.hh
1 #ifndef artdaq_DAQrate_EventStore_hh
2 #define artdaq_DAQrate_EventStore_hh
3 
4 #include "artdaq/DAQdata/Globals.hh" // Before trace.h gets included in ConcurrentQueue (from GlobalQueue)
5 #include "artdaq-core/Data/RawEvent.hh"
6 #include "artdaq-core/Core/GlobalQueue.hh"
7 #include "artdaq-utilities/Plugins/MetricManager.hh"
8 #include "artdaq/DAQrate/detail/RequestMessage.hh"
9 #include "fhiclcpp/ParameterSet.h"
10 
11 #include <map>
12 #include <memory>
13 #include <chrono>
14 //#include <thread>
15 #include <future>
16 #include <stdint.h>
17 #include <arpa/inet.h>
18 #include <netinet/in.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 
22 namespace artdaq
23 {
24 
49  class EventStore
50  {
51  public:
56  typedef int (ART_CMDLINE_FCN)(int, char**);
57 
62  typedef int (ART_CFGSTRING_FCN)(const std::string&);
63 
64  typedef RawEvent::run_id_t run_id_t;
65  typedef RawEvent::subrun_id_t subrun_id_t;
66  typedef Fragment::sequence_id_t sequence_id_t;
67  typedef std::map<sequence_id_t, RawEvent_ptr> EventMap;
68 
69  static const std::string EVENT_RATE_STAT_KEY;
70  static const std::string INCOMPLETE_EVENT_STAT_KEY;
71 
75  enum class EventStoreInsertResult : int
76  {
78  SUCCESS,
81  };
82 
86  EventStore() = delete;
87 
91  EventStore(EventStore const&) = delete;
92 
97  EventStore& operator=(EventStore const&) = delete;
98 
130  EventStore(const fhicl::ParameterSet& pset,
131  size_t num_fragments_per_event, run_id_t run,
132  size_t event_queue_depth, size_t max_incomplete_events);
133 
145  EventStore(const fhicl::ParameterSet& pset,
146  size_t num_fragments_per_event, run_id_t run,
147  int argc, char* argv[],
148  ART_CMDLINE_FCN* reader);
149 
160  EventStore(const fhicl::ParameterSet& pset,
161  size_t num_fragments_per_event, run_id_t run,
162  const std::string& configString,
163  ART_CFGSTRING_FCN* reader);
164 
168  virtual ~EventStore();
169 
184  void insert(FragmentPtr pfrag,
185  bool printWarningWhenFragmentIsDropped = true);
186 
202  EventStoreInsertResult insert(FragmentPtr pfrag, FragmentPtr& rejectedFragment);
203 
214  bool endOfData(int& readerReturnValue);
215 
227  void setSeqIDModulus(unsigned int seqIDModulus);
228 
234  bool flushData();
235 
240  void startRun(run_id_t runID);
241 
245  void startSubrun();
246 
251  subrun_id_t subrunID() const { return subrun_id_; }
252 
257  bool endRun();
258 
263  bool endSubrun();
264 
268  void sendMetrics();
269 
274  size_t incompleteEventCount() const { return events_.size(); }
275 
280  void setRequestMode(detail::RequestMessageMode mode) { request_mode_ = mode; }
281 
282  private:
283  // id_ is the unique identifier of this object; MPI programs will
284  // use the MPI rank to fill in this value.
285  size_t const num_fragments_per_event_;
286  size_t const max_queue_size_;
287  size_t const max_incomplete_count_;
288  run_id_t run_id_;
289  subrun_id_t subrun_id_;
290  EventMap events_;
291  RawEventQueue& queue_;
292  std::chrono::steady_clock::time_point reader_thread_launch_time_;
293  std::future<int> reader_thread_;
294 
295  bool send_requests_;
296  std::mutex request_mutex_;
297  std::map<Fragment::sequence_id_t, Fragment::timestamp_t> active_requests_;
298  int request_port_;
299  size_t request_delay_;
300  int request_socket_;
301  struct sockaddr_in request_addr_;
302  std::string multicast_out_addr_;
303  detail::RequestMessageMode request_mode_;
304 
305  unsigned int seqIDModulus_;
306  sequence_id_t lastFlushedSeqID_;
307  sequence_id_t highestSeqIDSeen_;
308 
309  artdaq::detail::seconds const enq_timeout_;
310  size_t enq_check_count_;
311  bool const printSummaryStats_;
312 
313  int incomplete_event_report_interval_ms_;
314  std::chrono::steady_clock::time_point last_incomplete_event_report_time_;
315 
316  bool send_routing_tokens_;
317  int token_port_;
318  int token_socket_;
319  struct sockaddr_in token_addr_;
320  std::string token_address_;
321 
322  int art_thread_wait_ms_;
323 
324  private:
325  void initStatistics_();
326 
327  void reportStatistics_();
328 
329  void setup_requests_(std::string trigger_addr);
330 
331  void send_request_();
332 
333  void do_send_request_();
334 
335  void setup_tokens_();
336 
337  void send_routing_token_(int nSlots);
338  };
339 }
340 #endif /* artdaq_DAQrate_EventStore_hh */
void insert(FragmentPtr pfrag, bool printWarningWhenFragmentIsDropped=true)
Give ownership of the Fragment to the EventStore.
Definition: EventStore.cc:98
static const std::string EVENT_RATE_STAT_KEY
Key for the Event Rate MonitoredQuantity.
Definition: EventStore.hh:69
void setRequestMode(detail::RequestMessageMode mode)
Set the mode for RequestMessages. Used to indicate when EventStore should enter &quot;EndOfRun&quot; mode...
Definition: EventStore.hh:280
bool endOfData(int &readerReturnValue)
Indicate that the end of input has been reached to the art thread.
Definition: EventStore.cc:259
EventStore & operator=(EventStore const &)=delete
Copy Assignment operator is deleted.
EventStoreInsertResult
This enumeration contains possible status codes of insertion attempts.
Definition: EventStore.hh:75
void startRun(run_id_t runID)
Start a Run.
Definition: EventStore.cc:317
Fragment::sequence_id_t sequence_id_t
Copy Fragment::sequence_id_t into local scope.
Definition: EventStore.hh:66
EventStore()=delete
Default Constructor is deleted.
virtual ~EventStore()
EventStore Destructor.
Definition: EventStore.cc:85
The Fragment was successfully inserted.
size_t incompleteEventCount() const
Get the number of events currently being built in the EventStore.
Definition: EventStore.hh:274
std::map< sequence_id_t, RawEvent_ptr > EventMap
An EventMap is a map of RawEvent_ptr objects, keyed by sequence ID.
Definition: EventStore.hh:67
void startSubrun()
Start a new Subrun, incrementing the subrun number.
Definition: EventStore.cc:356
RawEvent::subrun_id_t subrun_id_t
Copy RawEvent::subrun_id_t into local scope.
Definition: EventStore.hh:65
The EventStore class collects Fragment objects, until it receives a complete event, at which point the event is handed over to the art thread.
Definition: EventStore.hh:49
int( ART_CMDLINE_FCN)(int, char **)
An art function that accepts standard C main arguments.
Definition: EventStore.hh:56
static const std::string INCOMPLETE_EVENT_STAT_KEY
Key for the Incomplete Events MonitoredQuantity.
Definition: EventStore.hh:70
The EventStore is full, but the Fragment was accepted as it is for an already-open event...
subrun_id_t subrunID() const
Get the current subrun number.
Definition: EventStore.hh:251
void setSeqIDModulus(unsigned int seqIDModulus)
Set the parameter that will be used to determine which sequence IDs get grouped together into events...
Definition: EventStore.cc:274
void sendMetrics()
Send metrics to the MetricManager, if one has been instantiated in the application.
Definition: EventStore.cc:646
int( ART_CFGSTRING_FCN)(const std::string &)
An art function that accepts a fhicl::ParameterSet as a string.
Definition: EventStore.hh:62
bool flushData()
Push any incomplete events onto the queue.
Definition: EventStore.cc:279
RawEvent::run_id_t run_id_t
Copy RawEvent::run_id_t into local scope.
Definition: EventStore.hh:64
The EventStore is full, and the Fragment was rejected.
The Fragment was rejected, because the RawEventQueue is full.
bool endRun()
Send an EndOfRunFragment to the art thread.
Definition: EventStore.cc:366
bool endSubrun()
Send an EndOfSubRunFragment to the art thread.
Definition: EventStore.cc:382