artdaq  v3_12_02
FragmentBuffer.hh
1 #ifndef artdaq_Application_FragmentBuffer_hh
2 #define artdaq_Application_FragmentBuffer_hh
3 
4 #include "fhiclcpp/types/Sequence.h" // Must pre-empt fhiclcpp/types/Atom.h
5 
6 #include "TRACE/tracemf.h" // Pre-empt TRACE/trace.h from Fragment.hh.
7 #include "artdaq-core/Data/Fragment.hh"
8 
9 #include "artdaq/DAQrate/RequestBuffer.hh"
10 
11 namespace fhicl {
12 class ParameterSet;
13 }
14 #include "fhiclcpp/types/Atom.h"
15 #include "fhiclcpp/types/Comment.h"
16 #include "fhiclcpp/types/ConfigurationTable.h"
17 #include "fhiclcpp/types/Name.h"
18 
19 // Socket Includes
20 #include <arpa/inet.h>
21 #include <netinet/in.h>
22 #include <sys/socket.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 
26 #include <array>
27 #include <atomic>
28 #include <chrono>
29 #include <condition_variable>
30 #include <list>
31 #include <mutex>
32 #include <queue>
33 
34 namespace artdaq {
38 enum class RequestMode
39 {
40  Single,
41  Buffer,
42  Window,
43  SequenceID,
44  Ignored
45 };
46 
90 {
91 public:
95  struct Config
96  {
98  fhicl::Atom<std::string> generator_type{fhicl::Name{"generator"}, fhicl::Comment{"Name of the FragmentBuffer plugin to load"}};
100  fhicl::Atom<Fragment::timestamp_t> request_window_offset{fhicl::Name{"request_window_offset"}, fhicl::Comment{"Request messages contain a timestamp. For Window request mode, start the window this far before the timestamp in the request"}, 0};
102  fhicl::Atom<Fragment::timestamp_t> request_window_width{fhicl::Name{"request_window_width"}, fhicl::Comment{"For Window request mode, the window will be timestamp - offset to timestamp - offset + width"}, 0};
104  fhicl::Atom<Fragment::timestamp_t> stale_fragment_timeout{fhicl::Name{"stale_fragment_timeout"}, fhicl::Comment{"Fragments stored in the fragment generator which are older than the newest stored fragment by at least stale_fragment_timeout units of request timestamp ticks will get discarded"}, 0};
106  fhicl::Atom<bool> buffer_mode_keep_latest{fhicl::Name{"buffer_mode_keep_latest"}, fhicl::Comment{"Keep the latest Fragment when running in Buffer mode, so that each response has at least one Fragment (Fragment will be discarded if new data arrives before next request)"}, false};
108  fhicl::Atom<Fragment::type_t> expected_fragment_type{fhicl::Name{"expected_fragment_type"}, fhicl::Comment{"The type of Fragments this CFG will be generating. \"Empty\" will auto-detect type based on Fragments generated."}, Fragment::type_t(Fragment::EmptyFragmentType)};
110  fhicl::Atom<bool> request_windows_are_unique{fhicl::Name{"request_windows_are_unique"}, fhicl::Comment{"Whether Fragments should be removed from the buffer when matched to a request window"}, true};
112  fhicl::Atom<size_t> missing_request_window_timeout_us{fhicl::Name{"missing_request_window_timeout_us"}, fhicl::Comment{"How long to track missing requests in the \"out - of - order Windows\" list"}, 5000000};
114  fhicl::Atom<size_t> window_close_timeout_us{fhicl::Name{"window_close_timeout_us"}, fhicl::Comment{"How long to wait for the end of the data buffer to pass the end of a request window (measured from the time the request was received)"}, 2000000};
116  fhicl::Atom<bool> separate_data_thread{fhicl::Name{"separate_data_thread"}, fhicl::Comment{"Whether data collection should proceed on its own thread. Required for all data request processing"}, false};
118  fhicl::Atom<bool> circular_buffer_mode{fhicl::Name{"circular_buffer_mode"}, fhicl::Comment{"Whether the data buffer should be treated as a circular buffer on the input side (i.e. old fragments are automatically discarded when the buffer is full to always call getNext_)."}, false};
120  fhicl::Atom<size_t> sleep_on_no_data_us{fhicl::Name{"sleep_on_no_data_us"}, fhicl::Comment{"How long to sleep after calling getNext_ if no data is returned"}, 0};
122  fhicl::Atom<int> data_buffer_depth_fragments{fhicl::Name{"data_buffer_depth_fragments"}, fhicl::Comment{"The max fragments which can be stored before dropping occurs"}, 1000};
124  fhicl::Atom<size_t> data_buffer_depth_mb{fhicl::Name{"data_buffer_depth_mb"}, fhicl::Comment{"The max cumulative size in megabytes of the fragments which can be stored before dropping occurs"}, 1000};
126  fhicl::Atom<bool> separate_monitoring_thread{fhicl::Name{"separate_monitoring_thread"}, fhicl::Comment{"Whether a thread that calls the checkHWStatus_ method should be created"}, false};
128  fhicl::Atom<int64_t> hardware_poll_interval_us{fhicl::Name{"hardware_poll_interval_us"}, fhicl::Comment{"If a separate monitoring thread is used, how often should it call checkHWStatus_"}, 0};
130  fhicl::Atom<int> board_id{fhicl::Name{"board_id"}, fhicl::Comment{"The identification number for this FragmentBuffer"}};
133  fhicl::Sequence<Fragment::fragment_id_t> fragment_ids{fhicl::Name("fragment_ids"), fhicl::Comment("A list of Fragment IDs created by this FragmentBuffer")};
136  fhicl::Atom<int> fragment_id{fhicl::Name{"fragment_id"}, fhicl::Comment{"The Fragment ID created by this FragmentBuffer"}, -99};
138  fhicl::Atom<int> sleep_on_stop_us{fhicl::Name{"sleep_on_stop_us"}, fhicl::Comment{"How long to sleep before returning when stop transition is called"}, 0};
147  fhicl::Atom<std::string> request_mode{fhicl::Name{"request_mode"}, fhicl::Comment{"The mode by which the FragmentBuffer will process reqeusts"}, "ignored"};
148  };
150  using Parameters = fhicl::WrappedTable<Config>;
151 
156  explicit FragmentBuffer(const fhicl::ParameterSet& ps);
157 
163  virtual ~FragmentBuffer();
164 
169  void AddFragmentsToBuffer(FragmentPtrs frags);
170 
174  void Stop() { should_stop_ = true; }
175 
180  void Reset(bool stop);
181 
187  void applyRequestsIgnoredMode(artdaq::FragmentPtrs& frags);
188 
194  void applyRequestsSingleMode(artdaq::FragmentPtrs& frags);
195 
201  void applyRequestsBufferMode(artdaq::FragmentPtrs& frags);
202 
208  void applyRequestsWindowMode(artdaq::FragmentPtrs& frags);
209 
215  void applyRequestsSequenceIDMode(artdaq::FragmentPtrs& frags);
216 
223  void applyRequestsWindowMode_CheckAndFillDataBuffer(artdaq::FragmentPtrs& frags, artdaq::Fragment::fragment_id_t id, artdaq::Fragment::sequence_id_t seq, artdaq::Fragment::timestamp_t ts);
224 
230  bool applyRequests(FragmentPtrs& frags);
231 
240  bool sendEmptyFragment(FragmentPtrs& frags, size_t sequenceId, Fragment::fragment_id_t fragmentId, std::string desc);
241 
248  void sendEmptyFragments(FragmentPtrs& frags, std::map<Fragment::sequence_id_t, Fragment::timestamp_t>& requests);
249 
254  void checkSentWindows(Fragment::sequence_id_t seq);
255 
261  bool waitForDataBufferReady(Fragment::fragment_id_t id);
262 
268  bool dataBufferIsTooLarge(Fragment::fragment_id_t id);
269 
274  void getDataBufferStats(Fragment::fragment_id_t id);
275 
280  {
281  for (auto& id : dataBuffers_) getDataBufferStats(id.first);
282  }
283 
290  void checkDataBuffer(Fragment::fragment_id_t id);
291 
296  {
297  for (auto& id : dataBuffers_) checkDataBuffer(id.first);
298  }
299 
307  std::map<Fragment::sequence_id_t, std::chrono::steady_clock::time_point> GetSentWindowList(Fragment::fragment_id_t id)
308  {
309  if (!dataBuffers_.count(id))
310  {
311  throw cet::exception("DataBufferError") << "Error in FragmentBuffer: Cannot get Sent Windows for ID " << id << " because it does not exist!";
312  }
313 
314  return dataBuffers_[id]->WindowsSent;
315  }
316 
321  std::vector<Fragment::fragment_id_t> fragmentIDs()
322  {
323  std::vector<Fragment::fragment_id_t> output;
324 
325  for (auto& id : dataBuffers_)
326  {
327  output.push_back(id.first);
328  }
329 
330  return output;
331  }
332 
337  RequestMode request_mode() const { return mode_; }
338 
339  // The following functions are not yet implemented, and their
340  // signatures may be subject to change.
341 
342  // John F., 12/6/13 -- do we want Reset and Shutdown commands?
343  // Kurt B., 15-Feb-2014. For the moment, I suspect that we don't
344  // want a Shutdown command. FragmentGenerator instances are
345  // Constructed at Initialization time, and they are destructed
346  // at Shutdown time. So, any shutdown operations that need to be
347  // done should be put in the FragmentGenerator child class
348  // destructors. If we find that want shutdown (or initialization)
349  // operations that are different from destruction (construction),
350  // then we'll have to add InitCmd and ShutdownCmd methods.
351 
352  // virtual void ResetCmd() final {}
353  // virtual void ShutdownCmd() final {}
354 
359  void SetRequestBuffer(std::shared_ptr<RequestBuffer> buffer) { requestBuffer_ = buffer; }
360 
365  artdaq::Fragment::sequence_id_t GetNextSequenceID() const { return next_sequence_id_; }
366 
367 protected:
368  // John F., 12/6/13 -- need to figure out which of these getter
369  // functions should be promoted to "public"
370 
371  // John F., 1/21/15 -- after more than a year, there hasn't been a
372  // single complaint that a FragmentBuffer-derived
373  // class hasn't allowed its users to access these quantities, so
374  // they're probably fine as is
375 
381  artdaq::Fragment::fragment_id_t fragment_id() const
382  {
383  if (dataBuffers_.size() > 1) throw cet::exception("FragmentID") << "fragment_id() was called, indicating that Fragment Generator was expecting one and only one Fragment ID, but " << dataBuffers_.size() << " were declared!";
384  return (*dataBuffers_.begin()).first;
385  }
386 
391  bool check_stop();
392 
397  std::string printMode_();
398 
403  size_t dataBufferFragmentCount_();
404 
405 private:
406  // FHiCL-configurable variables. Note that the C++ variable names
407  // are the FHiCL variable names with a "_" appended
408 
409  // Socket parameters
410  Fragment::sequence_id_t next_sequence_id_;
411  std::shared_ptr<RequestBuffer> requestBuffer_;
412 
413  RequestMode mode_;
414  bool bufferModeKeepLatest_;
415  Fragment::timestamp_t windowOffset_;
416  Fragment::timestamp_t windowWidth_;
417  Fragment::timestamp_t staleTimeout_;
418  Fragment::type_t expectedType_;
419  bool uniqueWindows_;
420  bool sendMissingFragments_;
421  size_t missing_request_window_timeout_us_;
422  size_t window_close_timeout_us_;
423  bool error_on_empty_;
424 
425  bool circularDataBufferMode_;
426 
427  std::mutex dataConditionMutex_;
428  std::condition_variable dataCondition_;
429  int maxDataBufferDepthFragments_;
430  size_t maxDataBufferDepthBytes_;
431 
432  struct DataBuffer
433  {
434  std::atomic<int> DataBufferDepthFragments;
435  std::atomic<size_t> DataBufferDepthBytes;
436  std::map<Fragment::sequence_id_t, std::chrono::steady_clock::time_point> WindowsSent;
437  bool BufferFragmentKept;
438  Fragment::sequence_id_t HighestRequestSeen;
439  FragmentPtrs DataBuffer;
440  std::mutex DataBufferMutex;
441  };
442 
443  std::mutex systemFragmentMutex_;
444  FragmentPtrs systemFragments_;
445  std::atomic<size_t> systemFragmentCount_;
446 
447  std::unordered_map<artdaq::Fragment::fragment_id_t, std::shared_ptr<DataBuffer>> dataBuffers_;
448 
449  std::atomic<bool> should_stop_;
450 };
451 } // namespace artdaq
452 
453 #endif /* artdaq_Application_FragmentBuffer_hh */
void AddFragmentsToBuffer(FragmentPtrs frags)
Add Fragments to the FragmentBuffer.
void checkDataBuffers()
Perform data buffer pruning operations for all buffers.
void getDataBuffersStats()
Calculate the size of all dataBuffers and report appropriate metrics.
RequestMode
The RequestMode enumeration contains the possible ways which FragmentBuffer responds to data requests...
fhicl::Atom< size_t > data_buffer_depth_mb
&quot;data_buffer_depth_mb&quot; (Default: 1000) : The max cumulative size in megabytes of the fragments which ...
size_t dataBufferFragmentCount_()
Get the total number of Fragments in all data buffers.
std::string printMode_()
Return the string representation of the current RequestMode.
bool applyRequests(FragmentPtrs &frags)
See if any requests have been received, and add the corresponding data Fragment objects to the output...
void SetRequestBuffer(std::shared_ptr< RequestBuffer > buffer)
Set the pointer to the RequestBuffer used to retrieve requests.
FragmentBuffer(const fhicl::ParameterSet &ps)
FragmentBuffer Constructor.
bool waitForDataBufferReady(Fragment::fragment_id_t id)
Wait for the data buffer to drain (dataBufferIsTooLarge returns false), periodically reporting status...
std::vector< Fragment::fragment_id_t > fragmentIDs()
Get the list of Fragment IDs handled by this FragmentBuffer.
fhicl::Atom< Fragment::timestamp_t > request_window_offset
&quot;request_window_offset&quot; (Default: 0) : Request messages contain a timestamp. For Window request mode...
fhicl::Atom< std::string > generator_type
&quot;generator&quot; (REQUIRED) Name of the FragmentBuffer plugin to load
fhicl::Atom< int > sleep_on_stop_us
&quot;sleep_on_stop_us&quot; (Default: 0) : How long to sleep before returning when stop transition is called ...
fhicl::Atom< size_t > sleep_on_no_data_us
&quot;sleep_on_no_data_us&quot; (Default: 0 (no sleep)) : How long to sleep after calling getNext_ if no data i...
fhicl::Atom< bool > request_windows_are_unique
&quot;request_windows_are_unique&quot; (Default: true) : Whether Fragments should be removed from the buffer wh...
fhicl::Atom< bool > separate_data_thread
&quot;separate_data_thread&quot; (Default: false) : Whether data collection should proceed on its own thread...
std::map< Fragment::sequence_id_t, std::chrono::steady_clock::time_point > GetSentWindowList(Fragment::fragment_id_t id)
Get the map of Window-mode requests fulfilled by this Fragment Geneerator for the given Fragment ID...
fhicl::Atom< Fragment::timestamp_t > request_window_width
&quot;request_window_width&quot; (Default: 0) : For Window request mode, the window will be timestamp - offset ...
void applyRequestsIgnoredMode(artdaq::FragmentPtrs &frags)
Create fragments using data buffer for request mode Ignored. Precondition: dataBufferMutex_ and reque...
fhicl::Atom< bool > separate_monitoring_thread
&quot;separate_monitoring_thread&quot; (Default: false) : Whether a thread that calls the checkHWStatus_ method...
fhicl::Atom< size_t > window_close_timeout_us
&quot;window_close_timeout_us&quot; (Default: 2000000) : How long to wait for the end of the data buffer to pas...
fhicl::Atom< size_t > missing_request_window_timeout_us
&quot;missing_request_window_timeout_us&quot; (Default: 5000000) : How long to track missing requests in the &quot;o...
void applyRequestsWindowMode_CheckAndFillDataBuffer(artdaq::FragmentPtrs &frags, artdaq::Fragment::fragment_id_t id, artdaq::Fragment::sequence_id_t seq, artdaq::Fragment::timestamp_t ts)
bool check_stop()
Routine used by applyRequests to make sure that all outstanding requests have been fulfilled before r...
void Stop()
Inform the FragmentBuffer that it should stop.
void Reset(bool stop)
Reset the FragmentBuffer (flushes all Fragments from buffers)
fhicl::Atom< Fragment::type_t > expected_fragment_type
&quot;expected_fragment_type&quot; (Default: 231, EmptyFragmentType) : The type of Fragments this CFG will be g...
artdaq::Fragment::sequence_id_t GetNextSequenceID() const
Get the next sequence ID expected by this FragmentBuffer. This is used to track sent windows and miss...
FragmentBuffer is a FragmentGenerator-derived abstract class that defines the interface for a Fragmen...
fhicl::Atom< int > data_buffer_depth_fragments
&quot;data_buffer_depth_fragments&quot; (Default: 1000) : The max fragments which can be stored before dropping...
void checkSentWindows(Fragment::sequence_id_t seq)
Check the windows_sent_ooo_ map for sequence IDs that may be removed.
fhicl::Atom< int64_t > hardware_poll_interval_us
&quot;hardware_poll_interval_us&quot; (Default: 0) : If a separate monitoring thread is used, how often should it call checkHWStatus_
Configuration of the FragmentBuffer. May be used for parameter validation
artdaq::Fragment::fragment_id_t fragment_id() const
Get the Fragment ID of this Fragment generator.
void checkDataBuffer(Fragment::fragment_id_t id)
Perform data buffer pruning operations for the given buffer. If the RequestMode is Single...
bool dataBufferIsTooLarge(Fragment::fragment_id_t id)
Test the configured constraints on the data buffer.
fhicl::Atom< std::string > request_mode
&quot;request_mode&quot; (Deafult: Ignored) : The mode by which the FragmentBuffer will process reqeusts Ignore...
bool sendEmptyFragment(FragmentPtrs &frags, size_t sequenceId, Fragment::fragment_id_t fragmentId, std::string desc)
Send an EmptyFragmentType Fragment.
void applyRequestsBufferMode(artdaq::FragmentPtrs &frags)
Create fragments using data buffer for request mode Buffer. Precondition: dataBufferMutex_ and reques...
void applyRequestsWindowMode(artdaq::FragmentPtrs &frags)
Create fragments using data buffer for request mode Window. Precondition: dataBufferMutex_ and reques...
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation (if desired)
virtual ~FragmentBuffer()
FragmentBuffer Destructor.
void sendEmptyFragments(FragmentPtrs &frags, std::map< Fragment::sequence_id_t, Fragment::timestamp_t > &requests)
This function is for Buffered and Single request modes, as they can only respond to one data request ...
fhicl::Atom< bool > buffer_mode_keep_latest
&quot;buffer_mode_keep_latest&quot; (Default: false): Keep the latest Fragment when running in Buffer mode...
fhicl::Atom< int > board_id
&quot;board_id&quot; (REQUIRED) : The identification number for this FragmentBuffer
fhicl::Atom< Fragment::timestamp_t > stale_fragment_timeout
&quot;stale_fragment_timeout&quot; (Default: 0) : Fragments stored in the fragment generator which are older th...
fhicl::Sequence< Fragment::fragment_id_t > fragment_ids
RequestMode request_mode() const
Get the current request mode of the FragmentBuffer
void applyRequestsSequenceIDMode(artdaq::FragmentPtrs &frags)
Create fragments using data buffer for request mode SequenceID. Precondition: dataBufferMutex_ and re...
fhicl::Atom< bool > circular_buffer_mode
&quot;circular_buffer_mode&quot; (Default: false) : Whether the data buffer should be treated as a circular buf...
void applyRequestsSingleMode(artdaq::FragmentPtrs &frags)
Create fragments using data buffer for request mode Single. Precondition: dataBufferMutex_ and reques...
void getDataBufferStats(Fragment::fragment_id_t id)
Calculate the size of the dataBuffer and report appropriate metrics.