artdaq  v3_01_00
FragCounter.hh
1 #ifndef artdaq_DAQrate_detail_FragCounter_hh
2 #define artdaq_DAQrate_detail_FragCounter_hh
3 
4 #include <unordered_map>
5 #include <atomic>
6 #include <limits>
7 #include <mutex>
8 
9 namespace artdaq
10 {
11  namespace detail
12  {
13  class FragCounter;
14  }
15 }
16 
21 {
22 public:
26  explicit FragCounter();
27 
32  void incSlot(size_t slot);
33 
39  void incSlot(size_t slot, size_t inc);
40 
46  void setSlot(size_t slot, size_t val);
47 
52  size_t nSlots() const;
53 
58  size_t count() const;
59 
65  size_t slotCount(size_t slot) const;
66 
71  size_t minCount() const;
72 
78  size_t operator[](size_t slot) const { return slotCount(slot); }
79 
80 private:
81  mutable std::mutex receipts_mutex_;
82  std::unordered_map<size_t, std::atomic<size_t>> receipts_;
83 };
84 
85 inline
88  : receipts_() {}
89 
90 inline
91 void
93 incSlot(size_t slot)
94 {
95  incSlot(slot, 1);
96 }
97 
98 inline
99 void
101 incSlot(size_t slot, size_t inc)
102 {
103  std::unique_lock<std::mutex> lk(receipts_mutex_);
104  receipts_[slot].fetch_add(inc);
105 }
106 
107 inline
108 void
110 setSlot(size_t slot, size_t val)
111 {
112  std::unique_lock<std::mutex> lk(receipts_mutex_);
113  receipts_[slot] = val;
114 }
115 
116 inline
117 size_t
119 nSlots() const
120 {
121  std::unique_lock<std::mutex> lk(receipts_mutex_);
122  return receipts_.size();
123 }
124 
125 inline
126 size_t
128 count() const
129 {
130  std::unique_lock<std::mutex> lk(receipts_mutex_);
131  size_t acc = 0;
132  for (auto& it : receipts_)
133  {
134  acc += it.second;
135  }
136  return acc;
137 }
138 
139 inline
140 size_t
142 slotCount(size_t slot) const
143 {
144  std::unique_lock<std::mutex> lk(receipts_mutex_);
145  return receipts_.count(slot) ? receipts_.at(slot).load() : 0;
146 }
147 
148 inline
149 size_t
151 minCount() const
152 {
153  std::unique_lock<std::mutex> lk(receipts_mutex_);
154  size_t min = std::numeric_limits<size_t>::max();
155  for (auto& it : receipts_)
156  {
157  if (it.second < min) min = it.second;
158  }
159  return min;
160 }
161 
162 #endif /* artdaq_DAQrate_detail_FragCounter_hh */
size_t operator[](size_t slot) const
Get the current count for the requested slot.
Definition: FragCounter.hh:78
Keep track of the count of Fragments received from a set of sources.
Definition: FragCounter.hh:20
void setSlot(size_t slot, size_t val)
Set the given slot to the given value.
Definition: FragCounter.hh:110
void incSlot(size_t slot)
Increment the given slot by one.
Definition: FragCounter.hh:93
size_t nSlots() const
Get the number of slots this FragCounter instance is tracking.
Definition: FragCounter.hh:119
size_t slotCount(size_t slot) const
Get the current count for the requested slot.
Definition: FragCounter.hh:142
size_t minCount() const
Get the minimum slot count.
Definition: FragCounter.hh:151
FragCounter()
Default Constructor.
Definition: FragCounter.hh:87
size_t count() const
Get the total number of Fragments received.
Definition: FragCounter.hh:128