19 #include "artdaq/TransferPlugins/detail/Timeout.hh"
20 #include "artdaq/DAQdata/Globals.hh"
21 #include "artdaq-core/Utilities/TimeUtils.hh"
26 Timeout::timeoutspec::timeoutspec()
27 : desc(), tag(), function()
29 , missed_periods(), check()
30 { TRACE( 18,
"Timeout::timeoutspec ctor this=%p",
this );
32 Timeout::timeoutspec::timeoutspec(
const timeoutspec & other )
33 : desc(other.desc), tag(other.tag), function(other.function)
34 , ts(other.ts), period(other.period)
35 , missed_periods(other.missed_periods), check(other.check)
36 { TRACE( 18,
"Timeout::timeoutspec copy ctor" );
39 { TRACE( 18,
"Timeout::timeoutspec copy assignment (operator=) other.desc="+other.
desc );
55 TRACE( 16,
"Timeout ctor" );
69 tmo.
tmo_tod_us = start_us ? start_us : artdaq::TimeUtils::gettimeofday_us() + period_us;
94 TRACE( 19,
"add_periodic - desc="+std::string(desc)+
" period_us=%lu start_us=%lu"
95 , period_us, start_us );
114 tmo.
tmo_tod_us = artdaq::TimeUtils::gettimeofday_us() + rel_ms * 1000;
125 tmo.
desc = desc.c_str();
129 tmo.
tmo_tod_us = artdaq::TimeUtils::gettimeofday_us() + rel_ms * 1000;
136 , uint64_t* tmo_tod_us)
140 TRACE( 15,
"get_next_expired_timeout b4 get_clear_next_expired_timeout" );
141 skipped = get_clear_next_expired_timeout(tmo, artdaq::TimeUtils::gettimeofday_us());
144 TRACE( 18,
"get_next_expired_timeout - get_clear_next_expired_timeout returned false" );
145 desc = std::string(
"");
160 std::unique_lock<std::mutex> ulock(lock_mutex_);
161 size_t active_time_size = active_time_.size();
162 if (active_time_size == 0)
164 TRACE( 17,
"get_next_timeout_delay active_.size() == 0" );
169 TRACE( 17,
"get_next_timeout_delay active_.size() != 0 %lu",active_time_size );
170 uint64_t tod_us = artdaq::TimeUtils::gettimeofday_us();
171 timeoutspec* tmo = &tmospecs_[(*(active_time_.begin())).second];
190 tmo = delay_us / 1000;
199 std::map<uint64_t, size_t>::iterator itactive;
200 std::list<size_t>::iterator itfree;
201 for (
unsigned ii = 0; ii < tmospecs_.size(); ++ii)
202 tmospecs_[ii].check = 1;
203 for (itactive = active_time_.begin(); itactive != active_time_.end(); ++itactive)
204 tmospecs_[(*itactive).second].check--;
205 for (itfree = free_.begin(); itfree != free_.end(); ++itfree)
206 tmospecs_[*itfree].check--;
207 for (
unsigned ii = 0; ii < tmospecs_.size(); ++ii)
208 if (tmospecs_[ii].check != 0)
return false;
213 Timeout::timeoutlist_init()
215 size_t list_sz = tmospecs_.size();
216 for (
size_t ii = 0; ii < list_sz; ++ii)
218 free_.push_front(ii);
223 int Timeout::get_clear_next_expired_timeout(timeoutspec& tmo
224 , uint64_t tod_now_us)
227 if (active_time_.size() == 0)
229 TRACE( 17,
"get_clear_next_expired_timeout - nothing to get/clear!" );
234 std::unique_lock<std::mutex> ulock(lock_mutex_);
235 std::multimap<uint64_t, size_t>::iterator itfront = active_time_.begin();
236 size_t idx = (*itfront).second;
237 if (tmospecs_[idx].tmo_tod_us < tod_now_us)
239 tmo = tmospecs_[idx];
240 TRACE( 17,
"get_clear_next_expired_timeout - clearing tag=%p desc="+tmo.desc+
" period=%lu idx=%lu"
241 , tmo.tag, tmo.period_us, idx );
243 active_time_.erase(itfront);
250 uint64_t period_us = tmo.period_us;
251 delta_us = tod_now_us - tmo.tmo_tod_us;
252 skipped = delta_us / period_us;
253 assert( skipped >= 0 );
254 tmo.missed_periods += skipped;
257 period_us += period_us * skipped;
258 tmospecs_[idx].tmo_tod_us += period_us;
259 active_time_.insert(std::pair<uint64_t, size_t>(tmospecs_[idx].tmo_tod_us, idx));
261 ,
"get_clear_next_expired_timeout - periodic timeout desc="+tmo.desc
262 +
" period_us=%lu delta_us=%lu skipped=%d next tmo at:%lu"
263 , period_us, delta_us, skipped, tmospecs_[idx].tmo_tod_us );
268 std::unordered_multimap<std::string, size_t>::iterator i2;
269 i2 = active_desc_.equal_range(tmospecs_[idx].desc).first;
272 if (i2->second == idx)
276 active_desc_.erase(i2);
277 free_.push_front(idx);
282 TRACE( 17,
"get_clear_next_expired_timeout - front %lu NOT before ts_now %lu - not clearing!"
283 , tmospecs_[idx].tmo_tod_us, tod_now_us );
293 TRACE( 18,
"copy_in_timeout desc="+std::string(desc) );
308 assert( free_.size() );
311 std::unique_lock<std::mutex> ulock(lock_mutex_);
312 size_t idx = free_.front();
314 tmospecs_[idx] = tmo;
315 TRACE( 20,
"copy_in_timeout timeoutspec desc="+tmo.desc );
316 active_time_.insert(std::pair<uint64_t, size_t>(tmo.tmo_tod_us, idx));
317 active_desc_.insert(std::pair<std::string, size_t>(tmo.desc, idx));
325 std::unordered_multimap<std::string, size_t>::iterator ii, ee;
326 std::unique_lock<std::mutex> ulock(lock_mutex_);
327 auto pairOfIters = active_desc_.equal_range(desc);
328 ii = pairOfIters.first;
329 ee = pairOfIters.second;
330 for (; ii != ee && ii->first.compare(desc) == 0; ++ii)
332 size_t idx = ii->second;
333 if (tmospecs_[idx].tag == tag)
337 uint64_t tmo_tod_us = tmospecs_[idx].tmo_tod_us;
340 std::multimap<uint64_t, size_t>::iterator i2;
341 i2 = active_time_.equal_range(tmo_tod_us).first;
344 if (i2->second == idx)
349 active_desc_.erase(ii);
350 active_time_.erase(i2);
351 free_.push_front(idx);
355 TRACE( 22,
"cancel_timeout returning %d", retsts );
362 std::map<uint64_t, size_t>::iterator ii = active_time_.begin(), ee = active_time_.end();
363 for (; ii != ee; ++ii)
365 TRACE( 3,
"list_active_time %lu desc="+tmospecs_[(*ii).second].desc, (*ii).first );
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.