ATLAS Offline Software
Public Types | Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes | Friends | List of all members
Athena::AlgorithmTimer Class Reference

Timer that invokes a user callback once the time is reached. More...

#include <AlgorithmTimer.h>

Collaboration diagram for Athena::AlgorithmTimer:

Public Types

enum  AlgorithmTimerConfig { DEFAULT = 0x0, USEREALTIME = 0x1 }
 Configuration flags for AlgorithmTimer. More...
 
typedef std::function< void()> callbackFct_t
 

Public Member Functions

 AlgorithmTimer (unsigned int milliseconds, callbackFct_t callback=NULL, AlgorithmTimerConfig conf=DEFAULT)
 Create AlgorithmTimer instance and start it (unless seconds==0) More...
 
 ~AlgorithmTimer ()
 Destroy and disable the timer. More...
 
void start ()
 (Re)start the timer with the last timeout used More...
 
void start (unsigned int milliseconds)
 Start the timer. More...
 
void start (ScopedTimer &scope)
 Start the timer. More...
 
unsigned int stop ()
 Stop the timer and return the time left in [ms]. More...
 
unsigned int timeLeft () const
 Returns the time left in milliseconds. More...
 
unsigned int timeout () const
 Returns the currently set timeout (in milliseconds) More...
 
void SetGDBCoreDumpDetails (unsigned int details)
 set the levelof details for (optional) gdb core dump More...
 

Protected Attributes

unsigned int m_timeout
 timeout in milliseconds More...
 
struct sigevent m_sigevent
 for signal handler More...
 
timer_t m_timerid
 timer ID More...
 
callbackFct_t m_onAlarm
 user callback More...
 
std::atomic_bool m_active
 flag protecting race condition at stop More...
 

Private Member Functions

 AlgorithmTimer ()
 no default constructor More...
 
 AlgorithmTimer (const AlgorithmTimer &)
 no copying allowed More...
 
AlgorithmTimeroperator= (const AlgorithmTimer &)
 no copying allowed More...
 
callbackFct_t abortJob ()
 

Private Attributes

unsigned int m_gdb_details
 

Friends

void AlgorithmTimerHandler::onAlarmThread (sigval_t sv)
 

Detailed Description

Timer that invokes a user callback once the time is reached.

AlgorithmTimer automatically disables itself in its destructor. Create it on the stack and it will therefore disable itself once it runs out of scope. Example:

{
Athena::AlgorithmTimer(2000, myAlarmHandler);
// something that takes a long time
}

The overhead of creating a AlgorithmTimer instance is very small. If you are still worried about it, you can re-use the instance by creating it outside the scope and starting/stopping it manually via its start() and stop() method.

To get the best of both worlds you can create the AlgorithmTimer outside the scope and use an even smaller ScopedTimer instance to start the timer. Once the ScopedTimer instance runs out of scope it will disarm the associated AlgorithmTimer.

MyAlgorithm::MyAlgorithm() :
m_algorithmTimer(0, myAlarmHandler) // create it in disabled state
{
}
{
Athena::ScopedTimer alarm(2000); // two seconds
m_algorithmTimer.start(alarm); // this will start the timer
// something that takes a long time
}

Definition at line 86 of file AlgorithmTimer.h.

Member Typedef Documentation

◆ callbackFct_t

typedef std::function<void()> Athena::AlgorithmTimer::callbackFct_t

Definition at line 101 of file AlgorithmTimer.h.

Member Enumeration Documentation

◆ AlgorithmTimerConfig

Configuration flags for AlgorithmTimer.

Enumerator
DEFAULT 

default

USEREALTIME 

use real time instead of system time

Definition at line 95 of file AlgorithmTimer.h.

96  {
97  DEFAULT = 0x0,
98  USEREALTIME = 0x1,
99  };

Constructor & Destructor Documentation

◆ AlgorithmTimer() [1/3]

AlgorithmTimer::AlgorithmTimer ( unsigned int  milliseconds,
callbackFct_t  callback = NULL,
AlgorithmTimerConfig  conf = DEFAULT 
)

Create AlgorithmTimer instance and start it (unless seconds==0)

Parameters
millisecondsTime in milliseconds until expiration of the timer (0 = disabled)
callbackUser callback to invoke once time is reached
useRealTimetrue, if timer should use ral instead od CPU time

Definition at line 55 of file AlgorithmTimer.cxx.

57  :
58  m_timeout (0),
59  m_timerid(),
61  m_gdb_details (0)
62 {
63  // prevent valgrind warning
64  // new warning popped up, needs new glibc for this, see http://bugs.kde.org/show_bug.cgi?id=124478
65  // possibly valgrind will be fixed, eventually.
66  std::memset (&m_sigevent, 0, sizeof (m_sigevent));
67 
68  m_sigevent.sigev_value.sival_ptr = this;
69 
70  if (m_onAlarm == NULL)
71  m_onAlarm = std::bind(&AlgorithmTimer::abortJob,this);
72 
73  m_sigevent.sigev_notify = SIGEV_THREAD;
74  m_sigevent.sigev_signo = 0;
75  m_sigevent.sigev_notify_function = AlgorithmTimerHandler::onAlarmThread;
76  m_sigevent.sigev_notify_attributes = NULL;
77 
78  // Create the timer
79  if ( conf & USEREALTIME )
80  {
81 #ifndef __APPLE__
82  timer_create(CLOCK_REALTIME, &m_sigevent, &m_timerid);
83 #endif
84  }
85  else
86  {
87  /*
88  * can use virtual time only, if defined on this system
89  */
90 #ifndef __APPLE__
91 #ifdef _POSIX_CPUTIME
92  timer_create(_POSIX_CPUTIME, &m_sigevent, &m_timerid);
93 #else
94  timer_create(CLOCK_REALTIME, &m_sigevent, &m_timerid);
95  std::cerr << "\nNo _POSIX_CPUTIME defined on this system !\n\n";
96  // we could also disable this timer in this case !
97  // seconds = 0;
98 #endif
99 #endif
100  }
101  // Start the timer if requested
102  if(milliseconds)
103  start(milliseconds);
104 }

◆ ~AlgorithmTimer()

AlgorithmTimer::~AlgorithmTimer ( )

Destroy and disable the timer.

Definition at line 106 of file AlgorithmTimer.cxx.

107 {
108 #ifndef __APPLE__
109  m_active = false;
110  timer_delete(m_timerid);
111 #endif
112 }

◆ AlgorithmTimer() [2/3]

Athena::AlgorithmTimer::AlgorithmTimer ( )
private

no default constructor

◆ AlgorithmTimer() [3/3]

Athena::AlgorithmTimer::AlgorithmTimer ( const AlgorithmTimer )
private

no copying allowed

Member Function Documentation

◆ abortJob()

AlgorithmTimer::callbackFct_t AlgorithmTimer::abortJob ( )
private

Definition at line 166 of file AlgorithmTimer.cxx.

167 {
168  /*
169  * Print stack trace and abort the job.
170  */
171  std::ostringstream os;
172  os << "Timeout ";
173  os << " (" << this->timeout() << " msec) reached";
174 
175  ServiceHandle<ICoreDumpSvc> coreDumpSvc("CoreDumpSvc", "AlgorithmTimer");
176  if ( coreDumpSvc.retrieve().isSuccess() )
177  {
178  coreDumpSvc->setCoreDumpInfo("Reason", os.str());
179  std::cerr << coreDumpSvc->dump() << std::endl;
180  }
181  else
182  {
183  std::cerr << os.str() << std::endl;
184  }
185  abort();
186 }

◆ operator=()

AlgorithmTimer& Athena::AlgorithmTimer::operator= ( const AlgorithmTimer )
private

no copying allowed

◆ SetGDBCoreDumpDetails()

void Athena::AlgorithmTimer::SetGDBCoreDumpDetails ( unsigned int  details)
inline

set the levelof details for (optional) gdb core dump

Definition at line 153 of file AlgorithmTimer.h.

154  { m_gdb_details = details; };

◆ start() [1/3]

void Athena::AlgorithmTimer::start ( )
inline

(Re)start the timer with the last timeout used

Definition at line 121 of file AlgorithmTimer.h.

121 { start(m_timeout); }

◆ start() [2/3]

void AlgorithmTimer::start ( ScopedTimer scope)

Start the timer.

Parameters
scopeScopedTimer instance to automatically stop the timer

Definition at line 130 of file AlgorithmTimer.cxx.

131 {
132  scope.m_timer = this;
133  start(scope.m_timeout);
134 }

◆ start() [3/3]

void AlgorithmTimer::start ( unsigned int  milliseconds)

Start the timer.

Parameters
millisecondsTime in milliseconds until expiration

Definition at line 114 of file AlgorithmTimer.cxx.

115 {
116  // Set the timer
117  m_timeout = milliseconds;
118 #ifndef __APPLE__
119  itimerspec spec;
120  spec.it_value.tv_sec = int(m_timeout/1000);
121  spec.it_value.tv_nsec = 1000000*(m_timeout%1000);
122  spec.it_interval.tv_sec = 0;
123  spec.it_interval.tv_nsec = 0;
124 
125  m_active = true;
126  timer_settime(m_timerid, 0, &spec, NULL);
127 #endif
128 }

◆ stop()

unsigned int AlgorithmTimer::stop ( )

Stop the timer and return the time left in [ms].

Definition at line 136 of file AlgorithmTimer.cxx.

137 {
138  // stop the timer, and retrieve remaining time
139 #ifndef __APPLE__
140  itimerspec spec;
141  spec.it_value.tv_sec = 0;
142  spec.it_value.tv_nsec = 0;
143  spec.it_interval.tv_sec = 0;
144  spec.it_interval.tv_nsec = 0;
145 
146  itimerspec ovalue;
147  m_active = false;
148  timer_settime(m_timerid, 0, &spec, &ovalue);
149  return 1000*ovalue.it_value.tv_sec + int(ovalue.it_value.tv_nsec/1000000);
150 #else
151  return 0;
152 #endif
153 }

◆ timeLeft()

unsigned int AlgorithmTimer::timeLeft ( ) const

Returns the time left in milliseconds.

Definition at line 155 of file AlgorithmTimer.cxx.

156 {
157 #ifndef __APPLE__
158  itimerspec spec;
159  timer_gettime( m_timerid, &spec );
160  return 1000*spec.it_value.tv_sec + int(spec.it_value.tv_nsec/1000000);
161 #else
162  return 0;
163 #endif
164 }

◆ timeout()

unsigned int Athena::AlgorithmTimer::timeout ( ) const
inline

Returns the currently set timeout (in milliseconds)

Definition at line 148 of file AlgorithmTimer.h.

148 { return m_timeout; }

Friends And Related Function Documentation

◆ AlgorithmTimerHandler::onAlarmThread

void AlgorithmTimerHandler::onAlarmThread ( sigval_t  sv)
friend

Member Data Documentation

◆ m_active

std::atomic_bool Athena::AlgorithmTimer::m_active
protected

flag protecting race condition at stop

Definition at line 162 of file AlgorithmTimer.h.

◆ m_gdb_details

unsigned int Athena::AlgorithmTimer::m_gdb_details
private

Definition at line 173 of file AlgorithmTimer.h.

◆ m_onAlarm

callbackFct_t Athena::AlgorithmTimer::m_onAlarm
protected

user callback

Definition at line 161 of file AlgorithmTimer.h.

◆ m_sigevent

struct sigevent Athena::AlgorithmTimer::m_sigevent
protected

for signal handler

Definition at line 158 of file AlgorithmTimer.h.

◆ m_timeout

unsigned int Athena::AlgorithmTimer::m_timeout
protected

timeout in milliseconds

Definition at line 158 of file AlgorithmTimer.h.

◆ m_timerid

timer_t Athena::AlgorithmTimer::m_timerid
protected

timer ID

Definition at line 160 of file AlgorithmTimer.h.


The documentation for this class was generated from the following files:
Athena::AlgorithmTimer::abortJob
callbackFct_t abortJob()
Definition: AlgorithmTimer.cxx:166
python.compareTCTs.details
details
Definition: compareTCTs.py:214
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
Athena::ScopedTimer
Helper class to create a "scoped cook timer" without having to declare the CookTimer itself within th...
Definition: AlgorithmTimer.h:182
CaloCondBlobAlgs_fillNoiseFromASCII.spec
spec
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:47
Athena::AlgorithmTimer::m_timeout
unsigned int m_timeout
timeout in milliseconds
Definition: AlgorithmTimer.h:154
Athena::AlgorithmTimer::m_timerid
timer_t m_timerid
timer ID
Definition: AlgorithmTimer.h:160
Athena::AlgorithmTimer::m_gdb_details
unsigned int m_gdb_details
Definition: AlgorithmTimer.h:173
runLayerRecalibration.callback
callback
Definition: runLayerRecalibration.py:64
Athena::AlgorithmTimer::DEFAULT
@ DEFAULT
default
Definition: AlgorithmTimer.h:97
LArG4FSStartPointFilterLegacy.execute
execute
Definition: LArG4FSStartPointFilterLegacy.py:20
CoreDumpSvc::setCoreDumpInfo
virtual void setCoreDumpInfo(const std::string &name, const std::string &value) override
Set a name/value pair in the core dump record.
Definition: CoreDumpSvc.cxx:364
Athena::AlgorithmTimer::USEREALTIME
@ USEREALTIME
use real time instead of system time
Definition: AlgorithmTimer.h:98
python.ConfigurableDb.conf
def conf
Definition: ConfigurableDb.py:282
Athena::AlgorithmTimerHandler::onAlarmThread
void onAlarmThread(sigval_t sv)
Function called by signals delivered via threads.
Definition: AlgorithmTimer.cxx:42
Athena::AlgorithmTimer::timeout
unsigned int timeout() const
Returns the currently set timeout (in milliseconds)
Definition: AlgorithmTimer.h:148
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
CoreDumpSvcHandler::coreDumpSvc
CoreDumpSvc * coreDumpSvc(nullptr)
pointer to CoreDumpSvc
Athena::ScopedTimer::m_timer
AlgorithmTimer * m_timer
AlgorithmTimer instance, set by AlgorithmTimer::start()
Definition: AlgorithmTimer.h:189
Athena::ScopedTimer::m_timeout
unsigned int m_timeout
Timeout in milliseconds.
Definition: AlgorithmTimer.h:188
CoreDumpSvc::dump
virtual std::string dump() const override
Print all core dump records.
Definition: CoreDumpSvc.cxx:392
Athena::AlgorithmTimer::start
void start()
(Re)start the timer with the last timeout used
Definition: AlgorithmTimer.h:121
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
Athena::AlgorithmTimer::m_active
std::atomic_bool m_active
flag protecting race condition at stop
Definition: AlgorithmTimer.h:162
Athena::AlgorithmTimer::m_onAlarm
callbackFct_t m_onAlarm
user callback
Definition: AlgorithmTimer.h:161
Athena::AlgorithmTimer::m_sigevent
struct sigevent m_sigevent
for signal handler
Definition: AlgorithmTimer.h:159
Athena::AlgorithmTimer
Timer that invokes a user callback once the time is reached.
Definition: AlgorithmTimer.h:87