tracefile.c per block cycle counter fix
[lttv.git] / ltt / branches / poly / ltt / tracefile.c
index f244a48009a7cfef9e65ccfcb70b07bb19566614..e603c79b68430faff02cfc81b08b3e807bfd787a 100644 (file)
 #define DIR_NAME_SIZE 256
 #define __UNUSED__ __attribute__((__unused__))
 
+
+/* obtain the time of an event */
+
+static inline LttTime getEventTime(LttTracefile * tf);
+
+
 /* set the offset of the fields belonging to the event,
    need the information of the archecture */
 void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace *t);
@@ -679,6 +685,8 @@ unsigned ltt_trace_eventtype_number(LttTrace *t)
   return count;
 }
 
+/* FIXME : performances could be improved with a better design for this
+ * function */
 LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id)
 {
   LttFacility * facility = NULL;
@@ -687,8 +695,8 @@ LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id)
   for(i=0;i<trace->facility_number;i++){
     LttFacility *iter_facility =
                       (LttFacility*) g_ptr_array_index(trace->facilities,i);
-    if(id >= iter_facility->base_id && 
-       id < iter_facility->base_id + iter_facility->event_number) {
+    if(unlikely(id >= iter_facility->base_id && 
+       id < iter_facility->base_id + iter_facility->event_number)) {
       facility = iter_facility;
       break;
     }
@@ -987,13 +995,13 @@ void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
 void ltt_tracefile_seek_position(LttTracefile *t, const LttEventPosition *ep)
 {
   //if we are at the right place, just return
-  if(t->which_block == ep->block_num && t->which_event == ep->event_num)
+  if(likely(t->which_block == ep->block_num && t->which_event == ep->event_num))
     return;
   
-  if(t->which_block == ep->block_num) updateTracefile(t);
+  if(likely(t->which_block == ep->block_num)) updateTracefile(t);
   else readBlock(t,ep->block_num);
   //event offset is available
-  if(ep->old_position){
+  if(likely(ep->old_position)){
     int err;
 
     t->which_event = ep->event_num;
@@ -1013,7 +1021,7 @@ void ltt_tracefile_seek_position(LttTracefile *t, const LttEventPosition *ep)
 
     //update the fields of the current event and go to the next event
     err = skipEvent(t);
-    if(err == ERANGE) g_error("event id is out of range\n");
+    if(unlikely(err == ERANGE)) g_error("event id is out of range\n");
       
     return;
   }
@@ -1023,7 +1031,7 @@ void ltt_tracefile_seek_position(LttTracefile *t, const LttEventPosition *ep)
   g_warning("using slow O(n) tracefile seek position");
 
   LttEvent event;
-  while(t->which_event < ep->event_num) ltt_tracefile_read(t, &event);
+  while(likely(t->which_event < ep->event_num)) ltt_tracefile_read(t, &event);
 
   return;
 }
@@ -1041,16 +1049,16 @@ LttEvent *ltt_tracefile_read(LttTracefile *t, LttEvent *event)
 {
   int err;
 
-  if(t->cur_event_pos == t->buffer + t->block_size){
-    if(t->which_block == t->block_number){
+  if(unlikely(t->cur_event_pos == t->buffer + t->block_size)){
+    if(unlikely(t->which_block == t->block_number)){
       return NULL;
     }
     err = readBlock(t, t->which_block + 1);
-    if(err)g_error("Can not read tracefile");    
+    if(unlikely(err))g_error("Can not read tracefile");    
   }
 
   event->event_id = (int)(*(guint16 *)(t->cur_event_pos));
-  if(event->event_id == TRACE_TIME_HEARTBEAT)
+  if(unlikely(event->event_id == TRACE_TIME_HEARTBEAT))
     t->cur_heart_beat_number++;
 
   t->prev_event_time  = t->current_event_time;
@@ -1077,7 +1085,7 @@ LttEvent *ltt_tracefile_read(LttTracefile *t, LttEvent *event)
 
   //update the fields of the current event and go to the next event
   err = skipEvent(t);
-  if(err == ERANGE) g_error("event id is out of range\n");
+  if(unlikely(err == ERANGE)) g_error("event id is out of range\n");
 
   return event;
 }
@@ -1110,6 +1118,157 @@ int readFile(int fd, void * buf, size_t size, char * mesg)
    return 0;
 }
 
+/*****************************************************************************
+ *Function name
+ *    skipEvent_pre_read_cycles : go to the next event,
+ *             update the necessary fields of the current event
+ *             increment the cycle counter, save it at the end.
+ *Input params 
+ *    t         : tracefile
+ *return value 
+ *    0               : success
+ *    ERANGE          : event id is out of range
+ ****************************************************************************/
+
+int skipEvent_pre_read_cycles(LttTracefile * t)
+{
+  int evId;
+  void * evData;
+  LttEventType * evT;
+  LttField * rootFld;
+
+  evId   = (int)(*(guint16 *)(t->cur_event_pos));
+  evData = t->cur_event_pos + EVENT_HEADER_SIZE;
+
+  evT    = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
+    
+  if(likely(evT)) rootFld = evT->root_field;
+  else return ERANGE;
+  
+  if(likely(rootFld)){
+    //event has string/sequence or the last event is not the same event
+    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;
+  }else t->cur_event_pos += EVENT_HEADER_SIZE;
+    
+  //evT->latest_block = t->which_block;
+  //evT->latest_event = t->which_event;
+  
+  //the next event is in the next block
+  //if(unlikely(evId == TRACE_BLOCK_END)){
+    // Specify end of buffer reached.
+  //  t->cur_event_pos = t->buffer + t->block_size;
+  //}else{
+    //g_critical("COUNT : %lu", t->cur_cycle_count);
+    //t->which_event++;
+   // t->current_event_time = getEventTime(t);
+  //}
+
+  return 0;
+}
+
+
+
+
+/*****************************************************************************
+ *Function name
+ *    ltt_tracefile_pre_read_cycles : 
+ *          read the current event, increment the cycle counter
+ *Input params
+ *    t                  : tracefile
+ *Return value
+ * False : end of bloc reached
+ ****************************************************************************/
+
+gboolean ltt_tracefile_pre_read_cycles(LttTracefile *tf)
+{
+  int err;
+  //LttEvent event;
+
+ // if(unlikely(t->cur_event_pos == t->buffer + t->block_size)){
+    //if(unlikely(t->which_block == t->block_number)){
+    //  return FALSE;
+    //}
+   // return FALSE; // end of bloc reached
+    //err = readBlock(t, t->which_block + 1);
+    //if(unlikely(err))g_error("Can not read tracefile");    
+  //}
+
+  //event.event_id = (int)(*(guint16 *)(t->cur_event_pos));
+  //if(unlikely(event.event_id == TRACE_TIME_HEARTBEAT))
+  //  t->cur_heart_beat_number++;
+
+  //t->prev_event_time  = t->current_event_time;
+  //  t->current_event_time = getEventTime(t);
+
+  //event.time_delta = *(guint32 *)(t->cur_event_pos + EVENT_ID_SIZE);
+  //event.event_time = t->current_event_time;
+  //event.event_cycle_count = t->cur_cycle_count;
+
+  //event.tracefile = t;
+  //event.data = t->cur_event_pos + EVENT_HEADER_SIZE;  
+  //event.which_block = t->which_block;
+  //event.which_event = t->which_event;
+
+  /* This is a workaround for fast position seek */
+  //event.last_event_pos = t->last_event_pos;
+  //event.prev_block_end_time = t->prev_block_end_time;
+  //event.prev_event_time = t->prev_event_time;
+  //event.pre_cycle_count = t->pre_cycle_count;
+  //event.count = t->count;
+  /* end of workaround */
+
+
+  /* Increment the cycle counter for the bloc */
+  LttTime       time;
+  LttCycleCount cycle_count;      // cycle count for the current event
+  LttCycleCount lEventTotalCycle; // Total cycles 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;
+  // Calculate total time in cycles from start of buffer for this event
+  cycle_count = (LttCycleCount)*(guint32 *)(tf->cur_event_pos + EVENT_ID_SIZE);
+  //g_debug("event cycle count %llu", cycle_count);
+  
+  if(unlikely(cycle_count < tf->pre_cycle_count)) tf->count++;
+  tf->pre_cycle_count = cycle_count;
+  cycle_count += (LttCycleCount)tf->count << 32;  
+  
+  //FIXME (MD)
+  //  if(tf->cur_heart_beat_number > tf->count)
+  //    cycle_count += (tf->cur_heart_beat_number - tf->count) << 32;  
+
+  tf->cur_cycle_count = cycle_count;
+  //g_debug("cur cycle count %llu", cycle_count);
+
+
+
+
+  if(unlikely(evId == TRACE_BLOCK_START)){
+    //g_debug("BLOCK START");
+  }else if(unlikely(evId == TRACE_BLOCK_END)){
+    //g_debug("BLOCK END");
+
+    /* The goal of all this pre reading */
+    tf->a_block_end->cycle_count = tf->cur_cycle_count;
+    //g_debug("end of block cycle count : %llu", tf->cur_cycle_count);
+    return FALSE;
+  }
+
+  //update the fields of the current event and go to the next event
+  err = skipEvent_pre_read_cycles(tf);
+  if(unlikely(err == ERANGE)) g_error("event id is out of range\n");
+
+
+  return TRUE;
+}
+
 /****************************************************************************
  *Function name
  *    readBlock       : read a block from the file
@@ -1127,7 +1286,7 @@ int readBlock(LttTracefile * tf, int whichBlock)
   off_t nbBytes;
   guint32 lostSize;
 
-  if(whichBlock - tf->which_block == 1 && tf->which_block != 0){
+  if(likely(whichBlock - tf->which_block == 1 && tf->which_block != 0)){
     tf->prev_block_end_time = tf->a_block_end->time;
     tf->prev_event_time     = tf->a_block_end->time;
   }else{
@@ -1138,9 +1297,9 @@ int readBlock(LttTracefile * tf, int whichBlock)
   }
 
   nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET);
-  if(nbBytes == -1) return EINVAL;
+  if(unlikely(nbBytes == -1)) return EINVAL;
   
-  if(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block")) 
+  if(unlikely(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block")))
     return EIO;
 
   tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE);
@@ -1153,6 +1312,17 @@ int readBlock(LttTracefile * tf, int whichBlock)
   tf->which_event = 1;
   tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev
   tf->cur_heart_beat_number = 0;
+  
+  /* read the whole block to precalculate total of cycles in it */
+  tf->count = 0;
+  tf->pre_cycle_count = 0;
+  tf->cur_cycle_count = tf->a_block_start->cycle_count;
+  while(likely(ltt_tracefile_pre_read_cycles(tf)));
+
+  /* put back pointer at the beginning */
+  tf->which_event = 1;
+  tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev
+  tf->cur_heart_beat_number = 0;
 
   getCyclePerNsec(tf);
 
@@ -1202,13 +1372,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;
@@ -1218,7 +1388,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++;
@@ -1232,6 +1402,8 @@ int skipEvent(LttTracefile * t)
 /*****************************************************************************
  *Function name
  *    getCyclePerNsec : calculate cycles per nsec for current block
+ *    MD: should have tracefile_read the whole block, so we know the
+ *    total of cycles in it before being called.
  *Input Params
  *    t               : tracefile
  ****************************************************************************/
@@ -1239,8 +1411,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);
@@ -1250,39 +1422,46 @@ 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
  *    LttTime        : the time of the event
  ****************************************************************************/
 
-LttTime getEventTime(LttTracefile * tf)
+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
-  LttCycleCount lEventNSec;       // Total usecs from start for event
+  LttCycleCount lEventNSec;       // Total nsecs from start for event
   LttTime       lTimeOffset;      // Time offset in struct LttTime
   guint16       evId;
-  LttCycleCount tmpCycleCount = (((LttCycleCount)1)<<32);
 
   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;
@@ -1292,12 +1471,13 @@ 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 += tmpCycleCount * tf->count;  
+  cycle_count += (LttCycleCount)tf->count << 32;  
   
+  //FIXME (MD)
   //  if(tf->cur_heart_beat_number > tf->count)
-  //    cycle_count += tmpCycleCount * (tf->cur_heart_beat_number - tf->count);  
+  //    cycle_count += (tf->cur_heart_beat_number - tf->count) << 32;  
 
   tf->cur_cycle_count = cycle_count;
 
@@ -1305,11 +1485,11 @@ 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.tv_nsec = lEventNSec % NANOSECONDS_PER_SECOND;
-  lTimeOffset.tv_sec  = lEventNSec / NANOSECONDS_PER_SECOND;
+  lTimeOffset = ltt_time_from_double(lEventNSec);
 
   time = ltt_time_add(tf->a_block_start->time, lTimeOffset);  
   
@@ -1330,7 +1510,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);  
 }
 
@@ -1354,13 +1534,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;
   }     
 
@@ -1421,7 +1601,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.03124 seconds and 4 git commands to generate.