git-svn-id: http://ltt.polymtl.ca/svn@136 04897980-b3bd-0310-b5e0-8ef037075253
[lttv.git] / ltt / branches / poly / ltt / tracefile.c
index 12dbd11163517192b8dda9ef7ee1327f3a99d716..928f541af152f0bbf859ea91dfd0f7d6936a20fc 100644 (file)
@@ -102,7 +102,11 @@ LttTracefile* ltt_tracefile_open(LttTrace * t, char * fileName)
 
   // Is the file large enough to contain a trace 
   if(lTDFStat.st_size < sizeof(BlockStart) + EVENT_HEADER_SIZE){
-    g_error("The input data file %s does not contain a trace\n", fileName);
+    g_print("The input data file %s does not contain a trace\n", fileName);
+    g_free(tf->name);
+    close(tf->fd);
+    g_free(tf);
+    return NULL;
   }
   
   //store the size of the file
@@ -129,6 +133,7 @@ void ltt_tracefile_open_cpu(LttTrace *t, char * tracefile_name)
 {
   LttTracefile * tf;
   tf = ltt_tracefile_open(t,tracefile_name);
+  if(!tf) return;
   t->per_cpu_tracefile_number++;
   g_ptr_array_add(t->per_cpu_tracefiles, tf);
 }
@@ -144,11 +149,12 @@ void ltt_tracefile_open_control(LttTrace *t, char * control_name)
   int i;
 
   tf = ltt_tracefile_open(t,control_name);
+  if(!tf) return;
   t->control_tracefile_number++;
   g_ptr_array_add(t->control_tracefiles,tf);
 
   //parse facilities tracefile to get base_id
-  if(strcmp(control_name,"facilities") ==0){
+  if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){
     while(1){
       ev = ltt_tracefile_read(tf);
       if(!ev)return; // end of file
@@ -156,8 +162,8 @@ void ltt_tracefile_open_control(LttTrace *t, char * control_name)
       if(ev->event_id == TRACE_FACILITY_LOAD){
        pos = ev->data;
        fLoad.name = (char*)pos;
-       fLoad.checksum = *(LttChecksum*)(pos + sizeof(pos));
-       fLoad.base_code = *(uint32_t*)(pos + sizeof(pos) + sizeof(LttChecksum));
+       fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name));
+       fLoad.base_code = *(uint32_t*)(pos + strlen(fLoad.name) + sizeof(LttChecksum));
 
        for(i=0;i<t->facility_number;i++){
          f = (LttFacility*)g_ptr_array_index(t->facilities,i);
@@ -174,6 +180,7 @@ void ltt_tracefile_open_control(LttTrace *t, char * control_name)
       }else if(ev->event_id == TRACE_BLOCK_END){
        break;
       }else g_error("Not valid facilities trace file\n");
+      g_free(ev);
     }
   }
 }
@@ -189,6 +196,7 @@ void ltt_tracefile_close(LttTracefile *t)
 {
   g_free(t->name);
   g_free(t->buffer);
+  close(t->fd);
   g_free(t);
 }
 
@@ -223,6 +231,7 @@ void getSystemInfo(LttSystemDescription* des, char * pathname)
   for(i=0;i<entry_number;i++){
     if(fgets(buf,DIR_NAME_SIZE, fp)== NULL)
       g_error("Not a valid file: %s\n", pathname);
+    buf[strlen(buf)-1] = '\0';
     ptr = buf;
     while(isspace(*ptr)) ptr++;
     switch(i){
@@ -297,24 +306,24 @@ void getSystemInfo(LttSystemDescription* des, char * pathname)
         if(strncmp("ltt_major_version=",ptr,18)!=0)
           g_error("Not a valid file: %s\n", pathname);
         ptr += 18; 
-        ptr++;//skip begining "
-        ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
+        //      ptr++;//skip begining "
+        //      ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
         des->ltt_major_version = (unsigned)atoi(ptr);
         break;
        case 13:
         if(strncmp("ltt_minor_version=",ptr,18)!=0)
           g_error("Not a valid file: %s\n", pathname);
         ptr += 18; 
-        ptr++;//skip begining "
-        ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
+        //      ptr++;//skip begining "
+        //      ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
         des->ltt_minor_version = (unsigned)atoi(ptr);
         break;
        case 14:
         if(strncmp("ltt_block_size=",ptr,15)!=0)
           g_error("Not a valid file: %s\n", pathname);
         ptr += 15; 
-        ptr++;//skip begining "
-        ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
+        //      ptr++;//skip begining "
+        //      ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
         des->ltt_block_size = (unsigned)atoi(ptr);
         break;
        default:
@@ -369,7 +378,7 @@ void getFacilityInfo(LttTrace *t, char* eventdefs)
   closedir(dir);
   
   for(j=0;j<t->facility_number;j++){
-    f = (LttFacility*)g_ptr_array_index(t->facilities, i);
+    f = (LttFacility*)g_ptr_array_index(t->facilities, j);
     for(i=0; i<f->event_number; i++){
       et = f->events[i];
       setFieldsOffset(NULL, et, NULL, t);
@@ -387,8 +396,8 @@ void getControlFileInfo(LttTrace *t, char* control)
   if(!dir) g_error("Can not open directory: %s\n", control);
 
   while((entry = readdir(dir)) != NULL){
-    if(strcmp(entry->d_name,"facilities") != 0 ||
-       strcmp(entry->d_name,"interrupts") != 0 ||
+    if(strcmp(entry->d_name,"facilities") != 0 &&
+       strcmp(entry->d_name,"interrupts") != 0 &&
        strcmp(entry->d_name,"processes") != 0) continue;
     
     strcpy(name,control);
@@ -408,7 +417,7 @@ void getCpuFileInfo(LttTrace *t, char* cpu)
   if(!dir) g_error("Can not open directory: %s\n", cpu);
 
   while((entry = readdir(dir)) != NULL){
-    if(strcmp(entry->d_name,".") != 0 ||
+    if(strcmp(entry->d_name,".") != 0 &&
        strcmp(entry->d_name,"..") != 0 ){
       strcpy(name,cpu);
       strcat(name,entry->d_name);
@@ -474,15 +483,15 @@ LttTrace *ltt_trace_open(char *pathname)
   strcat(tmp,"system.xml");
   getSystemInfo(sys_description, tmp);
 
+  //get facilities info
+  getFacilityInfo(t,eventdefs);
+  
   //get control tracefile info
   getControlFileInfo(t,control);
 
   //get cpu tracefile info
   getCpuFileInfo(t,cpu);
 
-  //get facilities info
-  getFacilityInfo(t,eventdefs);
-  
   return t;
 }
 
@@ -706,7 +715,8 @@ void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
   LttTime lttTime;
   int headTime = timecmp(&(t->a_block_start->time), &time);
   int tailTime = timecmp(&(t->a_block_end->time), &time);
-  
+  LttEvent * ev;
+
   if(headTime < 0 && tailTime > 0){
     lttTime = getEventTime(t);
     err = timecmp(&lttTime, &time);
@@ -719,10 +729,12 @@ void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
       }
     }else if(err < 0){
       err = t->which_block;
-      if(ltt_tracefile_read(t) == NULL){
-       g_printf("End of file\n");      
+      ev = ltt_tracefile_read(t);
+      if(ev == NULL){
+       g_print("End of file\n");      
        return;
       }
+      g_free(ev);
       if(t->which_block == err)
        return ltt_tracefile_seek_time(t,time);
     }else return;    
@@ -743,14 +755,18 @@ void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
       err=readBlock(t,t->which_block+1);
       if(err) g_error("Can not read tracefile: %s\n", t->name); 
     }else {
-      g_printf("End of file\n");      
+      g_print("End of file\n");      
       return;      
     }    
     if(tailTime < 0) return ltt_tracefile_seek_time(t, time);
   }else if(headTime == 0){
     updateTracefile(t);
   }else if(tailTime == 0){
-    t->cur_event_pos = t->a_block_end - EVENT_HEADER_SIZE;
+    t->cur_event_pos = t->a_block_end;
+    t->current_event_time = time;  
+    t->cur_heart_beat_number = 0;
+    t->prev_event_time.tv_sec = 0;
+    t->prev_event_time.tv_nsec = 0;
     return;
   }
 }
@@ -774,7 +790,7 @@ LttEvent *ltt_tracefile_read(LttTracefile *t)
     t->cur_heart_beat_number++;
 
   t->prev_event_time  = t->current_event_time;
-  t->current_event_time = getEventTime(t);
+  //  t->current_event_time = getEventTime(t);
 
   lttEvent->time_delta = *(uint32_t*)(t->cur_event_pos + EVENT_ID_SIZE);
   lttEvent->event_time = t->current_event_time;
@@ -787,7 +803,11 @@ LttEvent *ltt_tracefile_read(LttTracefile *t)
 
   //update the fields of the current event and go to the next event
   err = skipEvent(t);
-  if(err == ENOENT) return NULL;
+  if(err == ENOMEM){
+    g_free(lttEvent);
+    return NULL;
+  }
+  if(err == ENOENT) return lttEvent;
   if(err == ERANGE) g_error("event id is out of range\n");
   if(err)g_error("Can not read tracefile\n");
 
@@ -838,14 +858,11 @@ int readBlock(LttTracefile * tf, int whichBlock)
   if(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;
-    tf->current_event_time  = tf->a_block_end->time;
   }else{
     tf->prev_block_end_time.tv_sec = 0;
     tf->prev_block_end_time.tv_nsec = 0;
     tf->prev_event_time.tv_sec = 0;
     tf->prev_event_time.tv_nsec = 0;
-    tf->current_event_time.tv_sec = 0;
-    tf->current_event_time.tv_nsec = 0;
   }
 
   nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET);
@@ -866,7 +883,7 @@ int readBlock(LttTracefile * tf, int whichBlock)
 
   getCyclePerNsec(tf);
 
-  //  tf->current_event_time = getEventTime(tf);  
+  tf->current_event_time = getEventTime(tf);  
 
   return 0;  
 }
@@ -883,9 +900,7 @@ void updateTracefile(LttTracefile * tf)
 {
   tf->which_event = 1;
   tf->cur_event_pos = tf->buffer;
-  //  tf->current_event_time = getEventTime(tf);  
-  tf->current_event_time.tv_sec = 0;  
-  tf->current_event_time.tv_nsec = 0;  
+  tf->current_event_time = getEventTime(tf);  
   tf->cur_heart_beat_number = 0;
 
   tf->prev_event_time.tv_sec = 0;
@@ -901,7 +916,8 @@ void updateTracefile(LttTracefile * tf)
  *    0               : success
  *    EINVAL          : lseek fail
  *    EIO             : can not read from the file
- *    ENOENT          : end of file
+ *    ENOMEM          : end of file
+ *    ENOENT          : last event
  *    ERANGE          : event id is out of range
  ****************************************************************************/
 
@@ -911,31 +927,56 @@ int skipEvent(LttTracefile * t)
   void * evData;
   LttEventType * evT;
   LttField * rootFld;
+  static int evCount = 0;
+
+  if(evCount){
+    if(t->which_block == t->block_number && 
+       evCount == t->which_event){
+      return ENOMEM;
+    }else evCount = 0;
+  }
 
   evId   = (int)(*(uint16_t *)(t->cur_event_pos));
   evData = t->cur_event_pos + EVENT_HEADER_SIZE;
-  evT    = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
 
-  if(evT) rootFld = evT->root_field;
-  else return ERANGE;
+  //regard BLOCK_START, END and HEARTBEAT as special case, there are buildin events
+  if(evId != TRACE_BLOCK_START && evId != TRACE_BLOCK_END && evId != TRACE_TIME_HEARTBEAT){
+    evT    = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
+    
+    if(evT) rootFld = evT->root_field;
+    else return ERANGE;
   
-  //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){
-    setFieldsOffset(t, evT, evData, t->trace);
+    if(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){
+       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;
+  }else{
+    if(evId == TRACE_BLOCK_START || evId == TRACE_BLOCK_END){
+      t->cur_event_pos += sizeof(BlockStart) + EVENT_HEADER_SIZE;
+    }else{
+      t->cur_event_pos += sizeof(TimeHeartbeat) + EVENT_HEADER_SIZE;
+    }
   }
-  t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
-
-  evT->latest_block = t->which_block;
-  evT->latest_event = t->which_event;
   
   //the next event is in the next block
   if(evId == TRACE_BLOCK_END){
-    if(t->which_block == t->block_number) return ENOENT;
+    if(t->which_block == t->block_number){
+      t->which_event++;
+      evCount = t->which_event;
+      return ENOENT;
+    }
     err = readBlock(t, t->which_block + 1);
     if(err) return err;
   }else{
     t->which_event++;
+    t->current_event_time = getEventTime(t);
   }
 
   return 0;
@@ -1024,7 +1065,8 @@ void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
   LttField * rootFld = evT->root_field;
   //  rootFld->base_address = evD;
 
-  rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);  
+  if(rootFld)
+    rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);  
 }
 
 /*****************************************************************************
@@ -1047,7 +1089,7 @@ int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot,
   int size, size1, element_number, i, offset1, offset2;
   LttType * type = fld->field_type;
 
-  if(!t){
+  if(t){
     if(evT->latest_block==t->which_block && evT->latest_event==t->which_event){
       return fld->field_size;
     } 
@@ -1108,7 +1150,7 @@ int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot,
     if(fld->field_fixed == -1){
       fld->field_fixed = 0;
     }else{//0: string
-      size = sizeof((char*)evD) + 1; //include end : '\0'
+      size = strlen((char*)evD) + 1; //include end : '\0'
     }
 
   }else if(type->type_class == LTT_STRUCT){
This page took 0.030042 seconds and 4 git commands to generate.