likely/unlikely branch prediction
[lttv.git] / ltt / branches / poly / ltt / tracefile.c
index dd84a3966b36b4aa2edc4ffbdbcfee46fe96e37c..8092201b058ac3ce02afe750d89e8314704fe789 100644 (file)
@@ -1208,13 +1208,13 @@ int skipEvent(LttTracefile * t)
 
   evT    = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
     
-  if(evT) rootFld = evT->root_field;
+  if(likely(evT)) rootFld = evT->root_field;
   else return ERANGE;
   
-  if(rootFld){
+  if(likely(rootFld)){
     //event has string/sequence or the last event is not the same event
-    if((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event) 
-       && rootFld->field_fixed == 0){
+    if(likely((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event)
+       && rootFld->field_fixed == 0)){
       setFieldsOffset(t, evT, evData, t->trace);
     }
     t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
@@ -1224,7 +1224,7 @@ int skipEvent(LttTracefile * t)
   evT->latest_event = t->which_event;
 
   //the next event is in the next block
-  if(evId == TRACE_BLOCK_END){
+  if(unlikely(evId == TRACE_BLOCK_END)){
     t->cur_event_pos = t->buffer + t->block_size;
   }else{
     t->which_event++;
@@ -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,17 +1285,17 @@ 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;
 
   evId = *(guint16 *)tf->cur_event_pos;
-  if(evId == TRACE_BLOCK_START){
+  if(unlikely(evId == TRACE_BLOCK_START)){
     tf->count = 0;
     tf->pre_cycle_count = 0;
     tf->cur_cycle_count = tf->a_block_start->cycle_count;
     return tf->a_block_start->time;
-  }else if(evId == TRACE_BLOCK_END){
+  }else if(unlikely(evId == TRACE_BLOCK_END)){
     tf->count = 0;
     tf->pre_cycle_count = 0;
     tf->cur_cycle_count = tf->a_block_end->cycle_count;
@@ -1297,7 +1305,7 @@ static inline LttTime getEventTime(LttTracefile * tf)
   // Calculate total time in cycles from start of buffer for this event
   cycle_count = (LttCycleCount)*(guint32 *)(tf->cur_event_pos + EVENT_ID_SIZE);
   
-  if(cycle_count < tf->pre_cycle_count)tf->count++;
+  if(unlikely(cycle_count < tf->pre_cycle_count)) tf->count++;
   tf->pre_cycle_count = cycle_count;
   cycle_count += (LttCycleCount)tf->count << 32;  
   
@@ -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);
 
@@ -1334,7 +1343,7 @@ void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
   LttField * rootFld = evT->root_field;
   //  rootFld->base_address = evD;
 
-  if(rootFld)
+  if(likely(rootFld))
     rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);  
 }
 
@@ -1358,13 +1367,13 @@ int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot,
   int size, size1, element_number, i, offset1, offset2;
   LttType * type = fld->field_type;
 
-  if(t){
-    if(evT->latest_block==t->which_block && evT->latest_event==t->which_event){
+  if(likely(t)){
+    if(unlikely(evT->latest_block==t->which_block && evT->latest_event==t->which_event)){
       return fld->field_size;
     } 
   }
 
-  if(fld->field_fixed == 1){
+  if(likely(fld->field_fixed == 1)){
     if(fld == evT->root_field) return fld->field_size;
   }     
 
@@ -1425,7 +1434,7 @@ int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot,
     case LTT_STRUCT:
       element_number = (int) type->element_number;
       size = 0;
-      if(fld->field_fixed == -1){      
+      if(fld->field_fixed == -1){
         offset1 = offsetRoot;
         offset2 = 0;
         for(i=0;i<element_number;i++){
This page took 0.024836 seconds and 4 git commands to generate.