artdaq  v3_06_00
FragmentReceiverManager.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 <condition_variable>
8 
9 #include "fhiclcpp/fwd.h"
10 
11 #include "artdaq-core/Data/Fragment.hh"
12 #include "artdaq/TransferPlugins/TransferInterface.hh"
13 #include "artdaq/DAQrate/detail/FragCounter.hh"
14 #include "artdaq-utilities/Plugins/MetricManager.hh"
15 
16 namespace artdaq
17 {
18  class FragmentReceiverManager;
19  class FragmentStoreElement;
20 }
21 
28 {
29 public:
30 
45  explicit FragmentReceiverManager(const fhicl::ParameterSet& ps);
46 
50  virtual ~FragmentReceiverManager();
51 
58  FragmentPtr recvFragment(int& rank, size_t timeout_usec = 0);
59 
64  size_t count() const;
65 
71  size_t slotCount(size_t rank) const;
72 
77  size_t byteCount() const;
78 
82  void start_threads();
83 
88  std::set<int> enabled_sources() const;
89 
94  std::set<int> running_sources() const;
95 
96 private:
97  void runReceiver_(int);
98 
99  bool fragments_ready_() const;
100 
101  int get_next_source_() const;
102 
103  std::atomic<bool> stop_requested_;
104 
105  std::map<int, boost::thread> source_threads_;
106  std::map<int, std::unique_ptr<TransferInterface>> source_plugins_;
107  std::unordered_map<int, std::pair<size_t, double>> source_metric_data_;
108  std::unordered_map<int, std::chrono::steady_clock::time_point> source_metric_send_time_;
109  std::unordered_map<int, std::atomic<bool>> enabled_sources_;
110  std::unordered_map<int, std::atomic<bool>> running_sources_;
111 
112  std::map<int, FragmentStoreElement> fragment_store_;
113 
114  std::mutex input_cv_mutex_;
115  std::condition_variable input_cv_;
116  std::mutex output_cv_mutex_;
117  std::condition_variable output_cv_;
118 
119  detail::FragCounter recv_frag_count_; // Number of frags received per source.
120  detail::FragCounter recv_frag_size_; // Number of bytes received per source.
121  detail::FragCounter recv_seq_count_; // For counting sequence IDs
122  bool suppress_noisy_senders_;
123  size_t suppression_threshold_;
124 
125  size_t receive_timeout_;
126  mutable int last_source_;
127 };
128 
137 {
138 public:
143  : frags_()
144  , empty_(true)
145  , eod_marker_(-1)
146  {
147  std::cout << "FragmentStoreElement CONSTRUCTOR" << std::endl;
148  }
149 
154  bool empty() const
155  {
156  return empty_;
157  }
158 
163  void emplace_front(FragmentPtr&& frag)
164  {
165  std::unique_lock<std::mutex> lk(mutex_);
166  frags_.emplace_front(std::move(frag));
167  empty_ = false;
168  }
169 
174  void emplace_back(FragmentPtr&& frag)
175  {
176  std::unique_lock<std::mutex> lk(mutex_);
177  frags_.emplace_back(std::move(frag));
178  empty_ = false;
179  }
180 
185  FragmentPtr front()
186  {
187  std::unique_lock<std::mutex> lk(mutex_);
188  auto current_fragment = std::move(frags_.front());
189  frags_.pop_front();
190  empty_ = frags_.size() == 0;
191  return current_fragment;
192  }
193 
198  void SetEndOfData(size_t eod) { eod_marker_ = eod; }
203  size_t GetEndOfData() const { return eod_marker_; }
204 
209  size_t size() const { return frags_.size(); }
210 
211 private:
212  mutable std::mutex mutex_;
213  FragmentPtrs frags_;
214  std::atomic<bool> empty_;
215  size_t eod_marker_;
216 };
217 
218 inline
219 size_t
221 count() const
222 {
223  return recv_frag_count_.count();
224 }
225 
226 inline
227 size_t
229 slotCount(size_t rank) const
230 {
231  return recv_frag_count_.slotCount(rank);
232 }
233 
234 inline
235 size_t
237 byteCount() const
238 {
239  return recv_frag_size_.count();
240 }
241 #endif //ARTDAQ_DAQRATE_DATATRANSFERMANAGER_HH
void start_threads()
Start receiver threads for all enabled sources.
std::set< int > enabled_sources() const
Get the list of enabled sources.
void emplace_front(FragmentPtr &&frag)
Add a Fragment to the front of the FragmentStoreElement.
size_t byteCount() const
Get the total size of all data recieved by this FragmentReceiverManager.
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...
size_t count() const
Return the count of Fragment objects received by this FragmentReceiverManager.
virtual ~FragmentReceiverManager()
FragmentReceiverManager Destructor.
Receives Fragment objects from one or more DataSenderManager instances using TransferInterface plugin...
Keep track of the count of Fragments received from a set of sources.
Definition: FragCounter.hh:20
bool empty() const
Are any Fragment objects contained in this FragmentStoreElement?
FragmentReceiverManager(const fhicl::ParameterSet &ps)
FragmentReceiverManager Constructor.
void SetEndOfData(size_t eod)
Set the End-Of-Data marker value for this Receiver.
size_t size() const
Get the number of Fragments stored in this FragmentStoreElement.
FragmentPtr recvFragment(int &rank, size_t timeout_usec=0)
Receive a Fragment.
size_t slotCount(size_t rank) const
Get the count of Fragment objects received by this FragmentReceiverManager from a given source...
size_t GetEndOfData() const
Get the value of the End-Of-Data marker for this Receiver.
FragmentStoreElement()
FragmentStoreElement Constructor.
std::set< int > running_sources() const
Get the list of sources which are still receiving data.
FragmentPtr front()
Remove the first Fragment from the FragmentStoreElement and return it.
size_t count() const
Get the total number of Fragments received.
Definition: FragCounter.hh:128