From 887208b719ee99e75c44a9366671cf17c83186d0 Mon Sep 17 00:00:00 2001 From: compudj Date: Sun, 15 Aug 2004 01:30:41 +0000 Subject: [PATCH] add linkly and unlikely optimisation to ltt_time_compare git-svn-id: http://ltt.polymtl.ca/svn@769 04897980-b3bd-0310-b5e0-8ef037075253 --- ltt/branches/poly/ltt/ltt-private.h | 3 ++- ltt/branches/poly/ltt/time.h | 10 ++++++---- ltt/branches/poly/ltt/tracefile.c | 27 ++++++++++++++++++--------- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/ltt/branches/poly/ltt/ltt-private.h b/ltt/branches/poly/ltt/ltt-private.h index ae7cc7e8..ae10e90e 100644 --- a/ltt/branches/poly/ltt/ltt-private.h +++ b/ltt/branches/poly/ltt/ltt-private.h @@ -160,7 +160,8 @@ struct _LttTracefile{ BlockEnd * a_block_end; //block end of the block void * cur_event_pos; //the position of the current event void * buffer; //the buffer containing the block - double cycle_per_nsec; //Cycles per nsec + double nsec_per_cycle; //Nsec per cycle + //LttCycleCount cycles_per_nsec_reciprocal; // Optimisation for speed unsigned cur_heart_beat_number; //current number of heart beat in the buf LttCycleCount cur_cycle_count; //current cycle count of the event void * last_event_pos; diff --git a/ltt/branches/poly/ltt/time.h b/ltt/branches/poly/ltt/time.h index 55fb5883..a2570182 100644 --- a/ltt/branches/poly/ltt/time.h +++ b/ltt/branches/poly/ltt/time.h @@ -62,15 +62,17 @@ static inline LttTime ltt_time_add(LttTime t1, LttTime t2) return res; } +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) /* Fastest comparison : t1 > t2 */ static inline int ltt_time_compare(LttTime t1, LttTime t2) { int ret=0; - if(t1.tv_sec > t2.tv_sec) ret = 1; - else if(t1.tv_sec < t2.tv_sec) ret = -1; - else if(t1.tv_nsec > t2.tv_nsec) ret = 1; - else if(t1.tv_nsec < t2.tv_nsec) ret = -1; + if(likely(t1.tv_sec > t2.tv_sec)) ret = 1; + else if(unlikely(t1.tv_sec < t2.tv_sec)) ret = -1; + else if(likely(t1.tv_nsec > t2.tv_nsec)) ret = 1; + else if(unlikely(t1.tv_nsec < t2.tv_nsec)) ret = -1; return ret; } diff --git a/ltt/branches/poly/ltt/tracefile.c b/ltt/branches/poly/ltt/tracefile.c index dd84a396..e5506c0d 100644 --- a/ltt/branches/poly/ltt/tracefile.c +++ b/ltt/branches/poly/ltt/tracefile.c @@ -1245,8 +1245,8 @@ int skipEvent(LttTracefile * t) void getCyclePerNsec(LttTracefile * t) { LttTime lBufTotalTime; /* Total time for this buffer */ - LttCycleCount lBufTotalNSec; /* Total time for this buffer in nsecs */ - LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */ + double lBufTotalNSec; /* Total time for this buffer in nsecs */ + double lBufTotalCycle;/* Total cycles for this buffer */ /* Calculate the total time for this buffer */ lBufTotalTime = ltt_time_sub(t->a_block_end->time, t->a_block_start->time); @@ -1256,16 +1256,24 @@ void getCyclePerNsec(LttTracefile * t) lBufTotalCycle -= t->a_block_start->cycle_count; /* Convert the total time to nsecs */ - lBufTotalNSec = lBufTotalTime.tv_sec; - lBufTotalNSec *= NANOSECONDS_PER_SECOND; - lBufTotalNSec += lBufTotalTime.tv_nsec; + lBufTotalNSec = ltt_time_to_double(lBufTotalTime); - t->cycle_per_nsec = (double)lBufTotalCycle / (double)lBufTotalNSec; + t->nsec_per_cycle = (double)lBufTotalNSec / (double)lBufTotalCycle; + /* See : http://www.azillionmonkeys.com/qed/adiv.html */ + // precalculate the reciprocal, so divisions will be really fast. + // 2^32-1 == 0xFFFFFFFFULL + //{ + // double int_res = lBufTotalCycle/lBufTotalNSec; + // t->cycles_per_nsec_reciprocal = + // ((0xFFFF+int_res)/int_res); + //} + } /**************************************************************************** *Function name * getEventTime : obtain the time of an event + * NOTE : this function _really_ is on critical path. *Input params * tf : tracefile *Return value @@ -1277,7 +1285,7 @@ static inline LttTime getEventTime(LttTracefile * tf) LttTime time; LttCycleCount cycle_count; // cycle count for the current event LttCycleCount lEventTotalCycle; // Total cycles from start for event - double lEventNSec; // Total nsecs from start for event + LttCycleCount lEventNSec; // Total nsecs from start for event LttTime lTimeOffset; // Time offset in struct LttTime guint16 evId; @@ -1310,8 +1318,9 @@ static inline LttTime getEventTime(LttTracefile * tf) lEventTotalCycle -= tf->a_block_start->cycle_count; // Convert it to nsecs - lEventNSec = (double)lEventTotalCycle / (double)tf->cycle_per_nsec; - + lEventNSec = (double)lEventTotalCycle * (double)tf->nsec_per_cycle; + //lEventNSec = (tf->cycles_per_nsec_reciprocal * lEventTotalCycle) >> 16; + // Determine offset in struct LttTime lTimeOffset = ltt_time_from_double(lEventNSec); -- 2.34.1