13 #define TRACE_NAME "Timeout"
22 #include "artdaq/TransferPlugins/detail/Timeout.hh"
23 #include "artdaq/DAQdata/Globals.hh"
24 #include "artdaq-core/Utilities/TimeUtils.hh"
29 Timeout::timeoutspec::timeoutspec()
30 : desc(), tag(), function()
32 , missed_periods(), check()
34 TLOG(18) <<
"Timeout::timeoutspec ctor this=" <<
this;
36 Timeout::timeoutspec::timeoutspec(
const timeoutspec & other)
37 : desc(other.desc), tag(other.tag), function(other.function)
38 , ts(other.ts), period(other.period)
39 , missed_periods(other.missed_periods), check(other.check)
41 TLOG(18) <<
"Timeout::timeoutspec copy ctor";
45 TLOG(18) <<
"Timeout::timeoutspec copy assignment (operator= other.desc=" << other.
desc <<
")";
50 period = other.period;
61 TLOG(16) <<
"Timeout ctor";
75 tmo.
tmo_tod_us = start_us ? start_us : artdaq::TimeUtils::gettimeofday_us() + period_us;
100 TLOG(19) <<
"add_periodic - desc=" << desc <<
" period_us=" << period_us <<
" start_us=" << start_us;
119 tmo.
tmo_tod_us = artdaq::TimeUtils::gettimeofday_us() + rel_ms * 1000;
130 tmo.
desc = desc.c_str();
134 tmo.
tmo_tod_us = artdaq::TimeUtils::gettimeofday_us() + rel_ms * 1000;
141 , uint64_t* tmo_tod_us)
145 TLOG(15) <<
"get_next_expired_timeout b4 get_clear_next_expired_timeout";
146 skipped = get_clear_next_expired_timeout(tmo, artdaq::TimeUtils::gettimeofday_us());
149 TLOG(18) <<
"get_next_expired_timeout - get_clear_next_expired_timeout returned false";
150 desc = std::string(
"");
165 std::unique_lock<std::mutex> ulock(lock_mutex_);
166 size_t active_time_size = active_time_.size();
167 if (active_time_size == 0)
169 TLOG(17) <<
"get_next_timeout_delay active_.size() == 0";
174 TLOG(17) <<
"get_next_timeout_delay active_.size() != 0: " << active_time_size;
175 uint64_t tod_us = artdaq::TimeUtils::gettimeofday_us();
176 timeoutspec* tmo = &tmospecs_[(*(active_time_.begin())).second];
195 tmo = delay_us / 1000;
204 std::map<uint64_t, size_t>::iterator itactive;
205 std::list<size_t>::iterator itfree;
206 for (
unsigned ii = 0; ii < tmospecs_.size(); ++ii)
207 tmospecs_[ii].check = 1;
208 for (itactive = active_time_.begin(); itactive != active_time_.end(); ++itactive)
209 tmospecs_[(*itactive).second].check--;
210 for (itfree = free_.begin(); itfree != free_.end(); ++itfree)
211 tmospecs_[*itfree].check--;
212 for (
unsigned ii = 0; ii < tmospecs_.size(); ++ii)
213 if (tmospecs_[ii].check != 0)
return false;
218 Timeout::timeoutlist_init()
220 size_t list_sz = tmospecs_.size();
221 for (
size_t ii = 0; ii < list_sz; ++ii)
223 free_.push_front(ii);
228 int Timeout::get_clear_next_expired_timeout(timeoutspec& tmo
229 , uint64_t tod_now_us)
232 if (active_time_.size() == 0)
234 TLOG(17) <<
"get_clear_next_expired_timeout - nothing to get/clear!";
239 std::unique_lock<std::mutex> ulock(lock_mutex_);
240 std::multimap<uint64_t, size_t>::iterator itfront = active_time_.begin();
241 size_t idx = (*itfront).second;
242 if (tmospecs_[idx].tmo_tod_us < tod_now_us)
244 tmo = tmospecs_[idx];
245 TLOG(17) <<
"get_clear_next_expired_timeout - clearing tag=" << tmo.tag <<
" desc=" << tmo.desc <<
" period=" << tmo.period_us <<
" idx=" << idx;
247 active_time_.erase(itfront);
254 uint64_t period_us = tmo.period_us;
255 delta_us = tod_now_us - tmo.tmo_tod_us;
256 skipped = delta_us / period_us;
257 assert(skipped >= 0);
258 tmo.missed_periods += skipped;
261 period_us += period_us * skipped;
262 tmospecs_[idx].tmo_tod_us += period_us;
263 active_time_.insert(std::pair<uint64_t, size_t>(tmospecs_[idx].tmo_tod_us, idx));
264 TLOG(18) <<
"get_clear_next_expired_timeout - periodic timeout desc=" << tmo.desc
265 <<
" period_us=" << period_us <<
" delta_us=" << delta_us
266 <<
" skipped=" << skipped <<
" next tmo at:" << tmospecs_[idx].tmo_tod_us;
271 std::unordered_multimap<std::string, size_t>::iterator i2;
272 i2 = active_desc_.equal_range(tmospecs_[idx].desc).first;
275 if (i2->second == idx)
279 active_desc_.erase(i2);
280 free_.push_front(idx);
285 TLOG(17) <<
"get_clear_next_expired_timeout - front " << tmospecs_[idx].tmo_tod_us <<
" NOT before ts_now " << tod_now_us <<
" - not clearing!";
295 TLOG(18) <<
"copy_in_timeout desc=" + std::string(desc);
310 assert(free_.size());
313 std::unique_lock<std::mutex> ulock(lock_mutex_);
314 size_t idx = free_.front();
316 tmospecs_[idx] = tmo;
317 TLOG(20) <<
"copy_in_timeout timeoutspec desc=" + tmo.desc;
318 active_time_.insert(std::pair<uint64_t, size_t>(tmo.tmo_tod_us, idx));
319 active_desc_.insert(std::pair<std::string, size_t>(tmo.desc, idx));
327 std::unordered_multimap<std::string, size_t>::iterator ii, ee;
328 std::unique_lock<std::mutex> ulock(lock_mutex_);
329 auto pairOfIters = active_desc_.equal_range(desc);
330 ii = pairOfIters.first;
331 ee = pairOfIters.second;
332 for (; ii != ee && ii->first.compare(desc) == 0; ++ii)
334 size_t idx = ii->second;
335 if (tmospecs_[idx].tag == tag)
339 uint64_t tmo_tod_us = tmospecs_[idx].tmo_tod_us;
342 std::multimap<uint64_t, size_t>::iterator i2;
343 i2 = active_time_.equal_range(tmo_tod_us).first;
346 if (i2->second == idx)
351 active_desc_.erase(ii);
352 active_time_.erase(i2);
353 free_.push_front(idx);
357 TLOG(22) <<
"cancel_timeout returning " << retsts;
364 std::map<uint64_t, size_t>::iterator ii = active_time_.begin(), ee = active_time_.end();
365 for (; ii != ee; ++ii)
367 TLOG(TLVL_DEBUG) <<
"list_active_time " << (*ii).first <<
" desc=" << tmospecs_[(*ii).second].desc;
void copy_in_timeout(const char *desc, uint64_t period_us, uint64_t start_us=0)
Add a timeout with the given parameters.
void add_relative(const char *desc, void *tag, std::function< void()> &function, int rel_ms)
Add a periodic timeout to the Timeout container.
void * tag
could be file descriptor (fd)
std::function< void()> function
Function to execute at Timeout.
uint64_t tmo_tod_us
When the function should be executed (gettimeofday, microseconds)
int get_next_timeout_msdly()
Get the amount to wait for the next timeout to occur.
int check
Check the timeoutspec.
int missed_periods
Number of periods that passed while the function was executing.
void get_next_timeout_delay(int64_t *delay_us)
Get the amount to wait for the next timeout to occur.
uint64_t period_us
0 if not periodic
Specification for a Timeout function.
int get_next_expired_timeout(std::string &desc, void **tag, std::function< void()> &function, uint64_t *tmo_tod_us)
Get a timeout that has expired.
bool is_consistent()
Run a consistency check on all confiugured timeouts.
std::string desc
Description of the Timeout function.
bool cancel_timeout(void *tag, std::string desc)
Cancel the timeout having the given description and tag.
void add_periodic(const char *desc, void *tag, std::function< void()> &function, uint64_t period_us, uint64_t start_us=0)
Add a periodic timeout to the Timeout container.
Timeout(int max_tmos=100)
Construct a Timeout object.
void list_active_time()
TRACE all active timeouts.