artdaq  v2_03_02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
DataReceiverManager.hh
1 #ifndef ARTDAQ_DAQRATE_DATATRANSFERMANAGER_HH
2 #define ARTDAQ_DAQRATE_DATATRANSFERMANAGER_HH
3 
4 #include <map>
5 #include <set>
6 #include <memory>
7 #include <thread>
8 #include <condition_variable>
9 
10 #include "fhiclcpp/fwd.h"
11 
12 #include "artdaq-core/Data/Fragment.hh"
13 #include "artdaq/TransferPlugins/TransferInterface.hh"
14 #include "artdaq/DAQrate/detail/FragCounter.hh"
15 #include "artdaq-utilities/Plugins/MetricManager.hh"
16 
17 namespace artdaq
18 {
19  class DataReceiverManager;
20  class FragmentStoreElement;
21 }
22 
29 {
30 public:
31 
46  explicit DataReceiverManager(const fhicl::ParameterSet& ps);
47 
51  virtual ~DataReceiverManager();
52 
59  FragmentPtr recvFragment(int& rank, size_t timeout_usec = 0);
60 
65  size_t count() const;
66 
72  size_t slotCount(size_t rank) const;
73 
78  size_t byteCount() const;
79 
83  void start_threads();
84 
89  std::set<int> enabled_sources() const { return enabled_sources_; }
90 
95  void suppress_source(int source);
96 
100  void unsuppressAll();
101 
109  void reject_fragment(int source_rank, FragmentPtr frag);
110 
111 private:
112  void runReceiver_(int);
113 
114  bool fragments_ready_() const;
115 
116  int get_next_source_() const;
117 
118  std::atomic<bool> stop_requested_;
119 
120  std::map<int, std::thread> source_threads_;
121  std::map<int, std::unique_ptr<TransferInterface>> source_plugins_;
122  std::set<int> enabled_sources_;
123  std::set<int> suppressed_sources_;
124 
125  std::map<int, FragmentStoreElement> fragment_store_;
126 
127  std::mutex input_cv_mutex_;
128  std::condition_variable input_cv_;
129  std::mutex output_cv_mutex_;
130  std::condition_variable output_cv_;
131 
132  detail::FragCounter recv_frag_count_; // Number of frags received per source.
133  detail::FragCounter recv_frag_size_; // Number of bytes received per source.
134  detail::FragCounter recv_seq_count_; // For counting sequence IDs
135  bool suppress_noisy_senders_;
136  size_t suppression_threshold_;
137 
138  size_t receive_timeout_;
139 };
140 
149 {
150 public:
155  : frags_()
156  , empty_(true)
157  , eod_marker_(-1)
158  {
159  std::cout << "FragmentStoreElement CONSTRUCTOR" << std::endl;
160  }
161 
166  bool empty() const
167  {
168  return empty_;
169  }
170 
175  void emplace_front(FragmentPtr&& frag)
176  {
177  std::unique_lock<std::mutex> lk(mutex_);
178  frags_.emplace_front(std::move(frag));
179  empty_ = false;
180  }
181 
186  void emplace_back(FragmentPtr&& frag)
187  {
188  std::unique_lock<std::mutex> lk(mutex_);
189  frags_.emplace_back(std::move(frag));
190  empty_ = false;
191  }
192 
197  FragmentPtr front()
198  {
199  std::unique_lock<std::mutex> lk(mutex_);
200  auto current_fragment = std::move(frags_.front());
201  frags_.pop_front();
202  empty_ = frags_.size() == 0;
203  return std::move(current_fragment);
204  }
205 
210  void SetEndOfData(size_t eod) { eod_marker_ = eod; }
215  size_t GetEndOfData() const { return eod_marker_; }
216 
217 private:
218  mutable std::mutex mutex_;
219  FragmentPtrs frags_;
220  std::atomic<bool> empty_;
221  size_t eod_marker_;
222 };
223 
224 inline
225 size_t
227 count() const
228 {
229  return recv_frag_count_.count();
230 }
231 
232 inline
233 size_t
235 slotCount(size_t rank) const
236 {
237  return recv_frag_count_.slotCount(rank);
238 }
239 
240 inline
241 size_t
243 byteCount() const
244 {
245  return recv_frag_size_.count();
246 }
247 #endif //ARTDAQ_DAQRATE_DATATRANSFERMANAGER_HH
void emplace_front(FragmentPtr &&frag)
Add a Fragment to the front of the FragmentStoreElement.
void emplace_back(FragmentPtr &&frag)
Add a Fragment to the end of the FragmentStoreElement.
This class contains tracking information for all Fragment objects which have been received from a spe...
Keep track of the count of Fragments received from a set of sources.
Definition: FragCounter.hh:20
void unsuppressAll()
Re-enable all sources.
void reject_fragment(int source_rank, FragmentPtr frag)
Place the given Fragment back in the FragmentStore (Called when the EventStore is full) ...
bool empty() const
Are any Fragment objects contained in this FragmentStoreElement?
void SetEndOfData(size_t eod)
Set the End-Of-Data marker value for this Receiver.
std::set< int > enabled_sources() const
Get the list of enabled sources.
FragmentPtr recvFragment(int &rank, size_t timeout_usec=0)
Receive a Fragment.
DataReceiverManager(const fhicl::ParameterSet &ps)
DataReceiverManager Constructor.
Receives Fragment objects from one or more DataSenderManager instances using TransferInterface plugin...
size_t slotCount(size_t rank) const
Get the count of Fragment objects received by this DataReceiverManager from a given source...
size_t count() const
Return the count of Fragment objects received by this DataReceiverManager.
size_t GetEndOfData() const
Get the value of the End-Of-Data marker for this Receiver.
FragmentStoreElement()
FragmentStoreElement Constructor.
virtual ~DataReceiverManager()
DataReceiverManager Destructor.
void suppress_source(int source)
Suppress the given source.
FragmentPtr front()
Remove the first Fragment from the FragmentStoreElement and return it.
void start_threads()
Start receiver threads for all enabled sources.
size_t count() const
Get the total number of Fragments received.
Definition: FragCounter.hh:128
size_t byteCount() const
Get the total size of all data recieved by this DataReceiverManager.