artdaq  v2_02_03
 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:
154  FragmentStoreElement() : frags_()
155  , empty_(true)
156  {
157  std::cout << "FragmentStoreElement CONSTRUCTOR" << std::endl;
158  }
159 
164  bool empty() const
165  {
166  return empty_;
167  }
168 
173  void emplace_front(FragmentPtr&& frag)
174  {
175  std::unique_lock<std::mutex> lk(mutex_);
176  frags_.emplace_front(std::move(frag));
177  empty_ = false;
178  }
179 
184  void emplace_back(FragmentPtr&& frag)
185  {
186  std::unique_lock<std::mutex> lk(mutex_);
187  frags_.emplace_back(std::move(frag));
188  empty_ = false;
189  }
190 
195  FragmentPtr front()
196  {
197  std::unique_lock<std::mutex> lk(mutex_);
198  auto current_fragment = std::move(frags_.front());
199  frags_.pop_front();
200  empty_ = frags_.size() == 0;
201  return std::move(current_fragment);
202  }
203 
204 private:
205  mutable std::mutex mutex_;
206  FragmentPtrs frags_;
207  std::atomic<bool> empty_;
208 };
209 
210 inline
211 size_t
213 count() const
214 {
215  return recv_frag_count_.count();
216 }
217 
218 inline
219 size_t
221 slotCount(size_t rank) const
222 {
223  return recv_frag_count_.slotCount(rank);
224 }
225 
226 inline
227 size_t
229 byteCount() const
230 {
231  return recv_frag_size_.count();
232 }
233 #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?
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.
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.