artdaq  v3_09_01
FragCounter.hh
1 #ifndef artdaq_DAQrate_detail_FragCounter_hh
2 #define artdaq_DAQrate_detail_FragCounter_hh
3 
4 #include <atomic>
5 #include <limits>
6 #include <mutex>
7 #include <unordered_map>
8 
9 namespace artdaq {
10 namespace detail {
11 class FragCounter;
12 }
13 } // namespace artdaq
14 
19 {
20 public:
24  explicit FragCounter();
25 
30  void incSlot(size_t slot);
31 
37  void incSlot(size_t slot, size_t inc);
38 
44  void setSlot(size_t slot, size_t val);
45 
50  size_t nSlots() const;
51 
56  size_t count() const;
57 
63  size_t slotCount(size_t slot) const;
64 
69  size_t minCount() const;
70 
76  size_t operator[](size_t slot) const { return slotCount(slot); }
77 
78 private:
79  mutable std::mutex receipts_mutex_;
80  std::unordered_map<size_t, std::atomic<size_t>> receipts_;
81 };
82 
85  : receipts_() {}
86 
87 inline void
89  incSlot(size_t slot)
90 {
91  incSlot(slot, 1);
92 }
93 
94 inline void
96  incSlot(size_t slot, size_t inc)
97 {
98  std::unique_lock<std::mutex> lk(receipts_mutex_);
99  receipts_[slot].fetch_add(inc);
100 }
101 
102 inline void
104  setSlot(size_t slot, size_t val)
105 {
106  std::unique_lock<std::mutex> lk(receipts_mutex_);
107  receipts_[slot] = val;
108 }
109 
110 inline size_t
112  nSlots() const
113 {
114  std::unique_lock<std::mutex> lk(receipts_mutex_);
115  return receipts_.size();
116 }
117 
118 inline size_t
120  count() const
121 {
122  std::unique_lock<std::mutex> lk(receipts_mutex_);
123  size_t acc = 0;
124  for (auto& it : receipts_)
125  {
126  acc += it.second;
127  }
128  return acc;
129 }
130 
131 inline size_t
133  slotCount(size_t slot) const
134 {
135  std::unique_lock<std::mutex> lk(receipts_mutex_);
136  return receipts_.count(slot) ? receipts_.at(slot).load() : 0;
137 }
138 
139 inline size_t
141  minCount() const
142 {
143  std::unique_lock<std::mutex> lk(receipts_mutex_);
144  size_t min = std::numeric_limits<size_t>::max();
145  for (auto& it : receipts_)
146  {
147  if (it.second < min) min = it.second;
148  }
149  return min;
150 }
151 
152 #endif /* artdaq_DAQrate_detail_FragCounter_hh */
size_t operator[](size_t slot) const
Get the current count for the requested slot.
Definition: FragCounter.hh:76
Keep track of the count of Fragments received from a set of sources.
Definition: FragCounter.hh:18
void setSlot(size_t slot, size_t val)
Set the given slot to the given value.
Definition: FragCounter.hh:104
void incSlot(size_t slot)
Increment the given slot by one.
Definition: FragCounter.hh:89
size_t nSlots() const
Get the number of slots this FragCounter instance is tracking.
Definition: FragCounter.hh:112
size_t slotCount(size_t slot) const
Get the current count for the requested slot.
Definition: FragCounter.hh:133
size_t minCount() const
Get the minimum slot count.
Definition: FragCounter.hh:141
FragCounter()
Default Constructor.
Definition: FragCounter.hh:84
size_t count() const
Get the total number of Fragments received.
Definition: FragCounter.hh:120