Download Thesis
Transcript
5.3. TIMERS
57
having two threads competing for the same mutex.
Listing 5.3: High speed timers implemented as a macro
#define MSTARTTIME(var, fullname) static P2G::Instrumentation
::probe probe_##var; \
struct timeval t_##var; \
P2G::t->regProbe(probe_##var,(fullname)); \
pthread_spin_lock( &(probe_##var.mutex)); \
gettimeofday(&t_##var,NULL); \
probe_##var.count += 1; \
pthread_spin_unlock( &(probe_##var.mutex))
#define MSTOPTIME(var) struct timeval ts_##var; gettimeofday(&
ts_##var,NULL); \
time_t sec_##var; \
long usec_##var; \
sec_##var = ts_##var.tv_sec - t_##var.tv_sec; \
usec_##var = ts_##var.tv_usec - t_##var.tv_usec; \
usec_##var += sec_##var * 1000000; \
pthread_spin_lock( &(probe_##var.mutex)); \
probe_##var.time += usec_##var; \
pthread_spin_unlock( &(probe_##var.mutex));
#define STARTTIME(fullname) MSTARTTIME(var, fullname)
#define STOPTIME MSTOPTIME(var)
The probes posed an interesting challenge, with regard to how we could
avoid having lock contention.
We solved this by having each probe
registered at a central place, but operate on its own after it has been
registered. Looking at listings 5.4 we can see that as long as p.init is true,
the call just returns and no global lock is needed. This is the common path
that is taken every time after the first call to regProbe for that probe, and
should not induce a lot of overhead.
Listing 5.4: regProbe code
inline void Timer::regProbe(probe& p, const std::string& bin)
{