| 1 | #include <stdio.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <sys/stat.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <dirent.h> |
| 6 | #include <linux/errno.h> |
| 7 | |
| 8 | #include "parser.h" |
| 9 | #include <ltt/ltt.h> |
| 10 | #include "ltt-private.h" |
| 11 | #include <ltt/trace.h> |
| 12 | #include <ltt/facility.h> |
| 13 | |
| 14 | #define DIR_NAME_SIZE 256 |
| 15 | |
| 16 | /* set the offset of the fields belonging to the event, |
| 17 | need the information of the archecture */ |
| 18 | void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace *t); |
| 19 | |
| 20 | /* get the size of the field type according to the archtecture's |
| 21 | size and endian type(info of the archecture) */ |
| 22 | int getFieldtypeSize(LttTracefile * tf, LttEventType * evT, int offsetRoot, |
| 23 | int offsetParent, LttField *fld, void *evD, LttTrace* t); |
| 24 | |
| 25 | /* read a fixed size or a block information from the file (fd) */ |
| 26 | int readFile(int fd, void * buf, size_t size, char * mesg); |
| 27 | int readBlock(LttTracefile * tf, int whichBlock); |
| 28 | |
| 29 | /* calculate cycles per nsec for current block */ |
| 30 | void getCyclePerNsec(LttTracefile * t); |
| 31 | |
| 32 | /* reinitialize the info of the block which is already in the buffer */ |
| 33 | void updateTracefile(LttTracefile * tf); |
| 34 | |
| 35 | /* go to the next event */ |
| 36 | int skipEvent(LttTracefile * t); |
| 37 | |
| 38 | |
| 39 | /* Functions to parse system.xml file (using glib xml parser) */ |
| 40 | static void parser_start_element (GMarkupParseContext *context, |
| 41 | const gchar *element_name, |
| 42 | const gchar **attribute_names, |
| 43 | const gchar **attribute_values, |
| 44 | gpointer user_data, |
| 45 | GError **error) |
| 46 | { |
| 47 | int i=0; |
| 48 | LttSystemDescription* des = (LttSystemDescription* )user_data; |
| 49 | if(strcmp("system", element_name)){ |
| 50 | g_warning("This is not system.xml file\n"); |
| 51 | exit(1); |
| 52 | } |
| 53 | |
| 54 | while(attribute_names[i]){ |
| 55 | if(strcmp("node_name", attribute_names[i])==0){ |
| 56 | des->node_name = g_strdup(attribute_values[i]); |
| 57 | }else if(strcmp("domainname", attribute_names[i])==0){ |
| 58 | des->domain_name = g_strdup(attribute_values[i]); |
| 59 | }else if(strcmp("cpu", attribute_names[i])==0){ |
| 60 | des->nb_cpu = atoi(attribute_values[i]); |
| 61 | }else if(strcmp("arch_size", attribute_names[i])==0){ |
| 62 | if(strcmp(attribute_values[i],"LP32") == 0) des->size = LTT_LP32; |
| 63 | else if(strcmp(attribute_values[i],"ILP32") == 0) des->size = LTT_ILP32; |
| 64 | else if(strcmp(attribute_values[i],"LP64") == 0) des->size = LTT_LP64; |
| 65 | else if(strcmp(attribute_values[i],"ILP64") == 0) des->size = LTT_ILP64; |
| 66 | else if(strcmp(attribute_values[i],"UNKNOWN") == 0) des->size = LTT_UNKNOWN; |
| 67 | }else if(strcmp("endian", attribute_names[i])==0){ |
| 68 | if(strcmp(attribute_values[i],"LITTLE_ENDIAN") == 0) |
| 69 | des->endian = LTT_LITTLE_ENDIAN; |
| 70 | else if(strcmp(attribute_values[i],"BIG_ENDIAN") == 0) |
| 71 | des->endian = LTT_BIG_ENDIAN; |
| 72 | }else if(strcmp("kernel_name", attribute_names[i])==0){ |
| 73 | des->kernel_name = g_strdup(attribute_values[i]); |
| 74 | }else if(strcmp("kernel_release", attribute_names[i])==0){ |
| 75 | des->kernel_release = g_strdup(attribute_values[i]); |
| 76 | }else if(strcmp("kernel_version", attribute_names[i])==0){ |
| 77 | des->kernel_version = g_strdup(attribute_values[i]); |
| 78 | }else if(strcmp("machine", attribute_names[i])==0){ |
| 79 | des->machine = g_strdup(attribute_values[i]); |
| 80 | }else if(strcmp("processor", attribute_names[i])==0){ |
| 81 | des->processor = g_strdup(attribute_values[i]); |
| 82 | }else if(strcmp("hardware_platform", attribute_names[i])==0){ |
| 83 | des->hardware_platform = g_strdup(attribute_values[i]); |
| 84 | }else if(strcmp("operating_system", attribute_names[i])==0){ |
| 85 | des->operating_system = g_strdup(attribute_values[i]); |
| 86 | }else if(strcmp("ltt_major_version", attribute_names[i])==0){ |
| 87 | des->ltt_major_version = atoi(attribute_values[i]); |
| 88 | }else if(strcmp("ltt_minor_version", attribute_names[i])==0){ |
| 89 | des->ltt_minor_version = atoi(attribute_values[i]); |
| 90 | }else if(strcmp("ltt_block_size", attribute_names[i])==0){ |
| 91 | des->ltt_block_size = atoi(attribute_values[i]); |
| 92 | }else{ |
| 93 | g_warning("Not a valid attribute\n"); |
| 94 | exit(1); |
| 95 | } |
| 96 | i++; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static void parser_end_element (GMarkupParseContext *context, |
| 101 | const gchar *element_name, |
| 102 | gpointer user_data, |
| 103 | GError **error) |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | static void parser_characters (GMarkupParseContext *context, |
| 108 | const gchar *text, |
| 109 | gsize text_len, |
| 110 | gpointer user_data, |
| 111 | GError **error) |
| 112 | { |
| 113 | LttSystemDescription* des = (LttSystemDescription* )user_data; |
| 114 | des->description = g_strdup(text); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /***************************************************************************** |
| 119 | *Function name |
| 120 | * ltt_tracefile_open : open a trace file, construct a LttTracefile |
| 121 | *Input params |
| 122 | * t : the trace containing the tracefile |
| 123 | * fileName : path name of the trace file |
| 124 | *Return value |
| 125 | * : a pointer to a tracefile |
| 126 | ****************************************************************************/ |
| 127 | |
| 128 | LttTracefile* ltt_tracefile_open(LttTrace * t, char * fileName) |
| 129 | { |
| 130 | LttTracefile * tf; |
| 131 | struct stat lTDFStat; /* Trace data file status */ |
| 132 | BlockStart a_block_start; |
| 133 | |
| 134 | tf = g_new(LttTracefile, 1); |
| 135 | |
| 136 | //open the file |
| 137 | tf->name = g_strdup(fileName); |
| 138 | tf->trace = t; |
| 139 | tf->fd = open(fileName, O_RDONLY, 0); |
| 140 | if(tf->fd < 0){ |
| 141 | g_error("Unable to open input data file %s\n", fileName); |
| 142 | } |
| 143 | |
| 144 | // Get the file's status |
| 145 | if(fstat(tf->fd, &lTDFStat) < 0){ |
| 146 | g_error("Unable to get the status of the input data file %s\n", fileName); |
| 147 | } |
| 148 | |
| 149 | // Is the file large enough to contain a trace |
| 150 | if(lTDFStat.st_size < sizeof(BlockStart) + EVENT_HEADER_SIZE){ |
| 151 | g_print("The input data file %s does not contain a trace\n", fileName); |
| 152 | g_free(tf->name); |
| 153 | close(tf->fd); |
| 154 | g_free(tf); |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | //store the size of the file |
| 159 | tf->file_size = lTDFStat.st_size; |
| 160 | tf->block_size = t->system_description->ltt_block_size; |
| 161 | tf->block_number = tf->file_size / tf->block_size; |
| 162 | tf->which_block = 0; |
| 163 | |
| 164 | //allocate memory to contain the info of a block |
| 165 | tf->buffer = (void *) g_new(char, t->system_description->ltt_block_size); |
| 166 | |
| 167 | //read the first block |
| 168 | if(readBlock(tf,1)) exit(1); |
| 169 | |
| 170 | return tf; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /***************************************************************************** |
| 175 | *Open control and per cpu tracefiles |
| 176 | ****************************************************************************/ |
| 177 | |
| 178 | void ltt_tracefile_open_cpu(LttTrace *t, char * tracefile_name) |
| 179 | { |
| 180 | LttTracefile * tf; |
| 181 | tf = ltt_tracefile_open(t,tracefile_name); |
| 182 | if(!tf) return; |
| 183 | t->per_cpu_tracefile_number++; |
| 184 | g_ptr_array_add(t->per_cpu_tracefiles, tf); |
| 185 | } |
| 186 | |
| 187 | void ltt_tracefile_open_control(LttTrace *t, char * control_name) |
| 188 | { |
| 189 | LttTracefile * tf; |
| 190 | LttEvent * ev; |
| 191 | LttFacility * f; |
| 192 | guint16 evId; |
| 193 | void * pos; |
| 194 | FacilityLoad fLoad; |
| 195 | int i; |
| 196 | |
| 197 | tf = ltt_tracefile_open(t,control_name); |
| 198 | if(!tf) return; |
| 199 | t->control_tracefile_number++; |
| 200 | g_ptr_array_add(t->control_tracefiles,tf); |
| 201 | |
| 202 | //parse facilities tracefile to get base_id |
| 203 | if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){ |
| 204 | while(1){ |
| 205 | ev = ltt_tracefile_read(tf); |
| 206 | if(!ev)return; // end of file |
| 207 | |
| 208 | if(ev->event_id == TRACE_FACILITY_LOAD){ |
| 209 | pos = ev->data; |
| 210 | fLoad.name = (char*)pos; |
| 211 | fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name)); |
| 212 | fLoad.base_code = *(guint32 *)(pos + strlen(fLoad.name) + sizeof(LttChecksum)); |
| 213 | |
| 214 | for(i=0;i<t->facility_number;i++){ |
| 215 | f = (LttFacility*)g_ptr_array_index(t->facilities,i); |
| 216 | if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){ |
| 217 | f->base_id = fLoad.base_code; |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | if(i==t->facility_number) |
| 222 | g_error("Facility: %s, checksum: %d is not founded\n", |
| 223 | fLoad.name,fLoad.checksum); |
| 224 | }else if(ev->event_id == TRACE_BLOCK_START){ |
| 225 | continue; |
| 226 | }else if(ev->event_id == TRACE_BLOCK_END){ |
| 227 | break; |
| 228 | }else g_error("Not valid facilities trace file\n"); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /***************************************************************************** |
| 234 | *Function name |
| 235 | * ltt_tracefile_close: close a trace file, |
| 236 | *Input params |
| 237 | * t : tracefile which will be closed |
| 238 | ****************************************************************************/ |
| 239 | |
| 240 | void ltt_tracefile_close(LttTracefile *t) |
| 241 | { |
| 242 | g_free(t->name); |
| 243 | g_free(t->buffer); |
| 244 | close(t->fd); |
| 245 | g_free(t); |
| 246 | } |
| 247 | |
| 248 | |
| 249 | /***************************************************************************** |
| 250 | *Get system information |
| 251 | ****************************************************************************/ |
| 252 | void getSystemInfo(LttSystemDescription* des, char * pathname) |
| 253 | { |
| 254 | FILE * fp; |
| 255 | char buf[DIR_NAME_SIZE]; |
| 256 | char description[4*DIR_NAME_SIZE]; |
| 257 | |
| 258 | GMarkupParseContext * context; |
| 259 | GError * error; |
| 260 | GMarkupParser markup_parser = |
| 261 | { |
| 262 | parser_start_element, |
| 263 | parser_end_element, |
| 264 | parser_characters, |
| 265 | NULL, /* passthrough */ |
| 266 | NULL /* error */ |
| 267 | }; |
| 268 | |
| 269 | fp = fopen(pathname,"r"); |
| 270 | if(!fp){ |
| 271 | g_error("Can not open file : %s\n", pathname); |
| 272 | } |
| 273 | |
| 274 | context = g_markup_parse_context_new(&markup_parser, 0, des,NULL); |
| 275 | |
| 276 | while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){ |
| 277 | if(!g_markup_parse_context_parse(context, buf, DIR_NAME_SIZE, &error)){ |
| 278 | g_warning("Can not parse xml file: \n%s\n", error->message); |
| 279 | exit(1); |
| 280 | } |
| 281 | } |
| 282 | fclose(fp); |
| 283 | } |
| 284 | |
| 285 | /***************************************************************************** |
| 286 | *The following functions get facility/tracefile information |
| 287 | ****************************************************************************/ |
| 288 | |
| 289 | void getFacilityInfo(LttTrace *t, char* eventdefs) |
| 290 | { |
| 291 | DIR * dir; |
| 292 | struct dirent *entry; |
| 293 | char * ptr; |
| 294 | int i,j; |
| 295 | LttFacility * f; |
| 296 | LttEventType * et; |
| 297 | char name[DIR_NAME_SIZE]; |
| 298 | |
| 299 | dir = opendir(eventdefs); |
| 300 | if(!dir) g_error("Can not open directory: %s\n", eventdefs); |
| 301 | |
| 302 | while((entry = readdir(dir)) != NULL){ |
| 303 | ptr = &entry->d_name[strlen(entry->d_name)-4]; |
| 304 | if(strcmp(ptr,".xml") != 0) continue; |
| 305 | strcpy(name,eventdefs); |
| 306 | strcat(name,entry->d_name); |
| 307 | ltt_facility_open(t,name); |
| 308 | } |
| 309 | closedir(dir); |
| 310 | |
| 311 | for(j=0;j<t->facility_number;j++){ |
| 312 | f = (LttFacility*)g_ptr_array_index(t->facilities, j); |
| 313 | for(i=0; i<f->event_number; i++){ |
| 314 | et = f->events[i]; |
| 315 | setFieldsOffset(NULL, et, NULL, t); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | void getControlFileInfo(LttTrace *t, char* control) |
| 321 | { |
| 322 | DIR * dir; |
| 323 | struct dirent *entry; |
| 324 | char name[DIR_NAME_SIZE]; |
| 325 | |
| 326 | dir = opendir(control); |
| 327 | if(!dir) g_error("Can not open directory: %s\n", control); |
| 328 | |
| 329 | while((entry = readdir(dir)) != NULL){ |
| 330 | if(strcmp(entry->d_name,"facilities") != 0 && |
| 331 | strcmp(entry->d_name,"interrupts") != 0 && |
| 332 | strcmp(entry->d_name,"processes") != 0) continue; |
| 333 | |
| 334 | strcpy(name,control); |
| 335 | strcat(name,entry->d_name); |
| 336 | ltt_tracefile_open_control(t,name); |
| 337 | } |
| 338 | closedir(dir); |
| 339 | } |
| 340 | |
| 341 | void getCpuFileInfo(LttTrace *t, char* cpu) |
| 342 | { |
| 343 | DIR * dir; |
| 344 | struct dirent *entry; |
| 345 | char name[DIR_NAME_SIZE]; |
| 346 | |
| 347 | dir = opendir(cpu); |
| 348 | if(!dir) g_error("Can not open directory: %s\n", cpu); |
| 349 | |
| 350 | while((entry = readdir(dir)) != NULL){ |
| 351 | if(strcmp(entry->d_name,".") != 0 && |
| 352 | strcmp(entry->d_name,"..") != 0 ){ |
| 353 | strcpy(name,cpu); |
| 354 | strcat(name,entry->d_name); |
| 355 | ltt_tracefile_open_cpu(t,name); |
| 356 | }else continue; |
| 357 | } |
| 358 | closedir(dir); |
| 359 | } |
| 360 | |
| 361 | /***************************************************************************** |
| 362 | *A trace is specified as a pathname to the directory containing all the |
| 363 | *associated data (control tracefiles, per cpu tracefiles, event |
| 364 | *descriptions...). |
| 365 | * |
| 366 | *When a trace is closed, all the associated facilities, types and fields |
| 367 | *are released as well. |
| 368 | * |
| 369 | * MD : If pathname is already absolute, we do not add current working |
| 370 | * directory to it. |
| 371 | * |
| 372 | ****************************************************************************/ |
| 373 | |
| 374 | void get_absolute_pathname(const char *pathname, char * abs_pathname) |
| 375 | { |
| 376 | char * ptr, *ptr1; |
| 377 | size_t size = DIR_NAME_SIZE; |
| 378 | abs_pathname[0] = '\0'; |
| 379 | |
| 380 | if(pathname[0] == '/') |
| 381 | { |
| 382 | strcat(abs_pathname, pathname); |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | if(!getcwd(abs_pathname, size)){ |
| 387 | g_warning("Can not get current working directory\n"); |
| 388 | strcat(abs_pathname, pathname); |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | strcat(abs_pathname,"/"); |
| 393 | |
| 394 | ptr = (char*)pathname; |
| 395 | ptr1 = ptr + 1; |
| 396 | while(*ptr == '.' && *ptr1 == '.'){ |
| 397 | ptr += 3; |
| 398 | ptr1 = ptr + 1; |
| 399 | } |
| 400 | strcat(abs_pathname,ptr); |
| 401 | } |
| 402 | |
| 403 | LttTrace *ltt_trace_open(const char *pathname) |
| 404 | { |
| 405 | LttTrace * t; |
| 406 | LttSystemDescription * sys_description; |
| 407 | char eventdefs[DIR_NAME_SIZE]; |
| 408 | char info[DIR_NAME_SIZE]; |
| 409 | char control[DIR_NAME_SIZE]; |
| 410 | char cpu[DIR_NAME_SIZE]; |
| 411 | char tmp[DIR_NAME_SIZE]; |
| 412 | char abs_path[DIR_NAME_SIZE]; |
| 413 | gboolean has_slash = FALSE; |
| 414 | |
| 415 | get_absolute_pathname(pathname, abs_path); |
| 416 | |
| 417 | //establish the pathname to different directories |
| 418 | if(abs_path[strlen(abs_path)-1] == '/')has_slash = TRUE; |
| 419 | strcpy(eventdefs,abs_path); |
| 420 | if(!has_slash)strcat(eventdefs,"/"); |
| 421 | strcat(eventdefs,"eventdefs/"); |
| 422 | |
| 423 | strcpy(info,abs_path); |
| 424 | if(!has_slash)strcat(info,"/"); |
| 425 | strcat(info,"info/"); |
| 426 | |
| 427 | strcpy(control,abs_path); |
| 428 | if(!has_slash)strcat(control,"/"); |
| 429 | strcat(control,"control/"); |
| 430 | |
| 431 | strcpy(cpu,abs_path); |
| 432 | if(!has_slash)strcat(cpu,"/"); |
| 433 | strcat(cpu,"cpu/"); |
| 434 | |
| 435 | //new trace |
| 436 | t = g_new(LttTrace, 1); |
| 437 | sys_description = g_new(LttSystemDescription, 1); |
| 438 | t->pathname = g_strdup(abs_path); |
| 439 | t->facility_number = 0; |
| 440 | t->control_tracefile_number = 0; |
| 441 | t->per_cpu_tracefile_number = 0; |
| 442 | t->system_description = sys_description; |
| 443 | t->control_tracefiles = g_ptr_array_new(); |
| 444 | t->per_cpu_tracefiles = g_ptr_array_new(); |
| 445 | t->facilities = g_ptr_array_new(); |
| 446 | getDataEndianType(&(t->my_arch_size), &(t->my_arch_endian)); |
| 447 | |
| 448 | //get system description |
| 449 | strcpy(tmp,info); |
| 450 | strcat(tmp,"system.xml"); |
| 451 | getSystemInfo(sys_description, tmp); |
| 452 | |
| 453 | //get facilities info |
| 454 | getFacilityInfo(t,eventdefs); |
| 455 | |
| 456 | //get control tracefile info |
| 457 | getControlFileInfo(t,control); |
| 458 | |
| 459 | //get cpu tracefile info |
| 460 | getCpuFileInfo(t,cpu); |
| 461 | |
| 462 | return t; |
| 463 | } |
| 464 | |
| 465 | char * ltt_trace_name(LttTrace *t) |
| 466 | { |
| 467 | return t->pathname; |
| 468 | } |
| 469 | |
| 470 | |
| 471 | /****************************************************************************** |
| 472 | * When we copy a trace, we want all the opening actions to happen again : |
| 473 | * the trace will be reopened and totally independant from the original. |
| 474 | * That's why we call ltt_trace_open. |
| 475 | *****************************************************************************/ |
| 476 | LttTrace *ltt_trace_copy(LttTrace *self) |
| 477 | { |
| 478 | return ltt_trace_open(self->pathname); |
| 479 | } |
| 480 | |
| 481 | void ltt_trace_close(LttTrace *t) |
| 482 | { |
| 483 | int i; |
| 484 | LttTracefile * tf; |
| 485 | LttFacility * f; |
| 486 | |
| 487 | g_free(t->pathname); |
| 488 | |
| 489 | //free system_description |
| 490 | g_free(t->system_description->description); |
| 491 | g_free(t->system_description->node_name); |
| 492 | g_free(t->system_description->domain_name); |
| 493 | g_free(t->system_description->kernel_name); |
| 494 | g_free(t->system_description->kernel_release); |
| 495 | g_free(t->system_description->kernel_version); |
| 496 | g_free(t->system_description->machine); |
| 497 | g_free(t->system_description->processor); |
| 498 | g_free(t->system_description->hardware_platform); |
| 499 | g_free(t->system_description->operating_system); |
| 500 | g_free(t->system_description); |
| 501 | |
| 502 | //free control_tracefiles |
| 503 | for(i=0;i<t->control_tracefile_number;i++){ |
| 504 | tf = (LttTracefile*)g_ptr_array_index(t->control_tracefiles,i); |
| 505 | ltt_tracefile_close(tf); |
| 506 | } |
| 507 | g_ptr_array_free(t->control_tracefiles, TRUE); |
| 508 | |
| 509 | //free per_cpu_tracefiles |
| 510 | for(i=0;i<t->per_cpu_tracefile_number;i++){ |
| 511 | tf = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles,i); |
| 512 | ltt_tracefile_close(tf); |
| 513 | } |
| 514 | g_ptr_array_free(t->per_cpu_tracefiles, TRUE); |
| 515 | |
| 516 | //free facilities |
| 517 | for(i=0;i<t->facility_number;i++){ |
| 518 | f = (LttFacility*)g_ptr_array_index(t->facilities,i); |
| 519 | ltt_facility_close(f); |
| 520 | } |
| 521 | g_ptr_array_free(t->facilities, TRUE); |
| 522 | |
| 523 | g_free(t); |
| 524 | |
| 525 | g_blow_chunks(); |
| 526 | } |
| 527 | |
| 528 | |
| 529 | /***************************************************************************** |
| 530 | *Get the system description of the trace |
| 531 | ****************************************************************************/ |
| 532 | |
| 533 | LttSystemDescription *ltt_trace_system_description(LttTrace *t) |
| 534 | { |
| 535 | return t->system_description; |
| 536 | } |
| 537 | |
| 538 | /***************************************************************************** |
| 539 | * The following functions discover the facilities of the trace |
| 540 | ****************************************************************************/ |
| 541 | |
| 542 | unsigned ltt_trace_facility_number(LttTrace *t) |
| 543 | { |
| 544 | return (unsigned)(t->facility_number); |
| 545 | } |
| 546 | |
| 547 | LttFacility *ltt_trace_facility_get(LttTrace *t, unsigned i) |
| 548 | { |
| 549 | return (LttFacility*)g_ptr_array_index(t->facilities, i); |
| 550 | } |
| 551 | |
| 552 | /***************************************************************************** |
| 553 | *Function name |
| 554 | * ltt_trace_facility_find : find facilities in the trace |
| 555 | *Input params |
| 556 | * t : the trace |
| 557 | * name : facility name |
| 558 | *Output params |
| 559 | * position : position of the facility in the trace |
| 560 | *Return value |
| 561 | * : the number of facilities |
| 562 | ****************************************************************************/ |
| 563 | |
| 564 | unsigned ltt_trace_facility_find(LttTrace *t, char *name, unsigned *position) |
| 565 | { |
| 566 | int i, count=0; |
| 567 | LttFacility * f; |
| 568 | for(i=0;i<t->facility_number;i++){ |
| 569 | f = (LttFacility*)g_ptr_array_index(t->facilities, i); |
| 570 | if(strcmp(f->name,name)==0){ |
| 571 | count++; |
| 572 | if(count==1) *position = i; |
| 573 | }else{ |
| 574 | if(count) break; |
| 575 | } |
| 576 | } |
| 577 | return count; |
| 578 | } |
| 579 | |
| 580 | /***************************************************************************** |
| 581 | * Functions to discover all the event types in the trace |
| 582 | ****************************************************************************/ |
| 583 | |
| 584 | unsigned ltt_trace_eventtype_number(LttTrace *t) |
| 585 | { |
| 586 | int i; |
| 587 | unsigned count = 0; |
| 588 | LttFacility * f; |
| 589 | for(i=0;i<t->facility_number;i++){ |
| 590 | f = (LttFacility*)g_ptr_array_index(t->facilities, i); |
| 591 | count += f->event_number; |
| 592 | } |
| 593 | return count; |
| 594 | } |
| 595 | |
| 596 | LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id) |
| 597 | { |
| 598 | LttFacility * facility; |
| 599 | int i; |
| 600 | for(i=0;i<trace->facility_number;i++){ |
| 601 | facility = (LttFacility*) g_ptr_array_index(trace->facilities,i); |
| 602 | if(id >= facility->base_id && |
| 603 | id < facility->base_id + facility->event_number) |
| 604 | break; |
| 605 | } |
| 606 | if(i==trace->facility_number) return NULL; |
| 607 | else return facility; |
| 608 | } |
| 609 | |
| 610 | LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId) |
| 611 | { |
| 612 | LttFacility * f; |
| 613 | f = ltt_trace_facility_by_id(t,evId); |
| 614 | if(!f) return NULL; |
| 615 | return f->events[evId - f->base_id]; |
| 616 | } |
| 617 | |
| 618 | /***************************************************************************** |
| 619 | *There is one "per cpu" tracefile for each CPU, numbered from 0 to |
| 620 | *the maximum number of CPU in the system. When the number of CPU installed |
| 621 | *is less than the maximum, some positions are unused. There are also a |
| 622 | *number of "control" tracefiles (facilities, interrupts...). |
| 623 | ****************************************************************************/ |
| 624 | unsigned ltt_trace_control_tracefile_number(LttTrace *t) |
| 625 | { |
| 626 | return t->control_tracefile_number; |
| 627 | } |
| 628 | |
| 629 | unsigned ltt_trace_per_cpu_tracefile_number(LttTrace *t) |
| 630 | { |
| 631 | return t->per_cpu_tracefile_number; |
| 632 | } |
| 633 | |
| 634 | /***************************************************************************** |
| 635 | *It is possible to search for the tracefiles by name or by CPU position. |
| 636 | *The index within the tracefiles of the same type is returned if found |
| 637 | *and a negative value otherwise. |
| 638 | ****************************************************************************/ |
| 639 | |
| 640 | int ltt_trace_control_tracefile_find(LttTrace *t, char *name) |
| 641 | { |
| 642 | LttTracefile * tracefile; |
| 643 | int i; |
| 644 | for(i=0;i<t->control_tracefile_number;i++){ |
| 645 | tracefile = (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i); |
| 646 | if(strcmp(tracefile->name, name)==0)break; |
| 647 | } |
| 648 | if(i == t->control_tracefile_number) return -1; |
| 649 | return i; |
| 650 | } |
| 651 | |
| 652 | int ltt_trace_per_cpu_tracefile_find(LttTrace *t, unsigned i) |
| 653 | { |
| 654 | LttTracefile * tracefile; |
| 655 | int j, name; |
| 656 | for(j=0;j<t->per_cpu_tracefile_number;j++){ |
| 657 | tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, j); |
| 658 | name = atoi(tracefile->name); |
| 659 | if(name == (int)i)break; |
| 660 | } |
| 661 | if(j == t->per_cpu_tracefile_number) return -1; |
| 662 | return j; |
| 663 | } |
| 664 | |
| 665 | /***************************************************************************** |
| 666 | *Get a specific tracefile |
| 667 | ****************************************************************************/ |
| 668 | |
| 669 | LttTracefile *ltt_trace_control_tracefile_get(LttTrace *t, unsigned i) |
| 670 | { |
| 671 | return (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i); |
| 672 | } |
| 673 | |
| 674 | LttTracefile *ltt_trace_per_cpu_tracefile_get(LttTrace *t, unsigned i) |
| 675 | { |
| 676 | return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i); |
| 677 | } |
| 678 | |
| 679 | /***************************************************************************** |
| 680 | * Get the start time and end time of the trace |
| 681 | ****************************************************************************/ |
| 682 | |
| 683 | void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end) |
| 684 | { |
| 685 | LttTime startSmall, startTmp, endBig, endTmp; |
| 686 | int i, j=0; |
| 687 | LttTracefile * tf; |
| 688 | |
| 689 | for(i=0;i<t->control_tracefile_number;i++){ |
| 690 | tf = g_ptr_array_index(t->control_tracefiles, i); |
| 691 | readBlock(tf,1); |
| 692 | startTmp = tf->a_block_start->time; |
| 693 | readBlock(tf,tf->block_number); |
| 694 | endTmp = tf->a_block_end->time; |
| 695 | if(i==0){ |
| 696 | startSmall = startTmp; |
| 697 | endBig = endTmp; |
| 698 | j = 1; |
| 699 | continue; |
| 700 | } |
| 701 | if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp; |
| 702 | if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp; |
| 703 | } |
| 704 | |
| 705 | for(i=0;i<t->per_cpu_tracefile_number;i++){ |
| 706 | tf = g_ptr_array_index(t->per_cpu_tracefiles, i); |
| 707 | readBlock(tf,1); |
| 708 | startTmp = tf->a_block_start->time; |
| 709 | readBlock(tf,tf->block_number); |
| 710 | endTmp = tf->a_block_end->time; |
| 711 | if(j == 0 && i==0){ |
| 712 | startSmall = startTmp; |
| 713 | endBig = endTmp; |
| 714 | continue; |
| 715 | } |
| 716 | if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp; |
| 717 | if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp; |
| 718 | } |
| 719 | |
| 720 | *start = startSmall; |
| 721 | *end = endBig; |
| 722 | } |
| 723 | |
| 724 | |
| 725 | /***************************************************************************** |
| 726 | *Get the name of a tracefile |
| 727 | ****************************************************************************/ |
| 728 | |
| 729 | char *ltt_tracefile_name(LttTracefile *tf) |
| 730 | { |
| 731 | return tf->name; |
| 732 | } |
| 733 | |
| 734 | /***************************************************************************** |
| 735 | * Get the number of blocks in the tracefile |
| 736 | ****************************************************************************/ |
| 737 | |
| 738 | unsigned ltt_tracefile_block_number(LttTracefile *tf) |
| 739 | { |
| 740 | return tf->block_number; |
| 741 | } |
| 742 | |
| 743 | /***************************************************************************** |
| 744 | *Function name |
| 745 | * ltt_tracefile_seek_time: seek to the first event of the trace with time |
| 746 | * larger or equal to time |
| 747 | *Input params |
| 748 | * t : tracefile |
| 749 | * time : criteria of the time |
| 750 | ****************************************************************************/ |
| 751 | void ltt_tracefile_find_time_block(LttTracefile *t, LttTime time, |
| 752 | int start_block, int end_block) |
| 753 | { |
| 754 | int err, tmp_block, s, e; |
| 755 | int headTime; |
| 756 | int tailTime; |
| 757 | |
| 758 | err=readBlock(t,start_block); |
| 759 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
| 760 | if(start_block == end_block)return; |
| 761 | |
| 762 | tailTime = ltt_time_compare(t->a_block_end->time, time); |
| 763 | if(tailTime >= 0) return; |
| 764 | |
| 765 | err=readBlock(t,end_block); |
| 766 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
| 767 | if(start_block+1 == end_block)return; |
| 768 | |
| 769 | headTime = ltt_time_compare(t->a_block_start->time, time); |
| 770 | if(headTime <= 0 ) return; |
| 771 | |
| 772 | tmp_block = (end_block + start_block)/2; |
| 773 | err=readBlock(t,tmp_block); |
| 774 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
| 775 | |
| 776 | headTime = ltt_time_compare(t->a_block_start->time, time); |
| 777 | tailTime = ltt_time_compare(t->a_block_end->time, time); |
| 778 | if(headTime <= 0 && tailTime >= 0) return; |
| 779 | |
| 780 | if(headTime > 0){ |
| 781 | s = start_block + 1; |
| 782 | e = tmp_block - 1; |
| 783 | if(s <= e) |
| 784 | ltt_tracefile_find_time_block(t, time, s, e); |
| 785 | else return; |
| 786 | } |
| 787 | |
| 788 | if(tailTime < 0){ |
| 789 | s = tmp_block + 1; |
| 790 | e = end_block - 1; |
| 791 | if(s <= e) |
| 792 | ltt_tracefile_find_time_block(t, time, s, e); |
| 793 | else return; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | void ltt_tracefile_backward_find_time_block(LttTracefile *t, LttTime time) |
| 798 | { |
| 799 | int t_time, h_time, err; |
| 800 | err=readBlock(t,t->which_block-1); |
| 801 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
| 802 | h_time = ltt_time_compare(t->a_block_start->time, time); |
| 803 | t_time = ltt_time_compare(t->a_block_end->time, time); |
| 804 | if(h_time == 0){ |
| 805 | int tmp; |
| 806 | if(t->which_block == 1) return; |
| 807 | err=readBlock(t,t->which_block-1); |
| 808 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
| 809 | tmp = ltt_time_compare(t->a_block_end->time, time); |
| 810 | if(tmp == 0) return ltt_tracefile_seek_time(t, time); |
| 811 | err=readBlock(t,t->which_block+1); |
| 812 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
| 813 | }else if(h_time > 0){ |
| 814 | ltt_tracefile_find_time_block(t, time, 1, t->which_block); |
| 815 | return ltt_tracefile_seek_time(t, time) ; |
| 816 | }else{ |
| 817 | if(t_time >= 0) return ltt_tracefile_seek_time(t, time); |
| 818 | err=readBlock(t,t->which_block+1); |
| 819 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | void ltt_tracefile_seek_time(LttTracefile *t, LttTime time) |
| 824 | { |
| 825 | int err; |
| 826 | LttTime lttTime; |
| 827 | int headTime = ltt_time_compare(t->a_block_start->time, time); |
| 828 | int tailTime = ltt_time_compare(t->a_block_end->time, time); |
| 829 | LttEvent * ev; |
| 830 | |
| 831 | if(headTime < 0 && tailTime > 0){ |
| 832 | if(ltt_time_compare(t->a_block_end->time, t->current_event_time) !=0) { |
| 833 | lttTime = getEventTime(t); |
| 834 | err = ltt_time_compare(lttTime, time); |
| 835 | if(err > 0){ |
| 836 | if(t->which_event==2 || (&t->prev_event_time,&time)<0){ |
| 837 | return; |
| 838 | }else{ |
| 839 | updateTracefile(t); |
| 840 | return ltt_tracefile_seek_time(t, time); |
| 841 | } |
| 842 | }else if(err < 0){ |
| 843 | while(1){ |
| 844 | ev = ltt_tracefile_read(t); |
| 845 | if(ev == NULL){ |
| 846 | g_print("End of file\n"); |
| 847 | return; |
| 848 | } |
| 849 | lttTime = getEventTime(t); |
| 850 | err = ltt_time_compare(lttTime, time); |
| 851 | if(err >= 0)return; |
| 852 | } |
| 853 | }else return; |
| 854 | }else{//we are at the end of the block |
| 855 | updateTracefile(t); |
| 856 | return ltt_tracefile_seek_time(t, time); |
| 857 | } |
| 858 | }else if(headTime >= 0){ |
| 859 | if(t->which_block == 1){ |
| 860 | updateTracefile(t); |
| 861 | }else{ |
| 862 | if(ltt_time_compare(t->prev_block_end_time, time) >= 0 || |
| 863 | (t->prev_block_end_time.tv_sec == 0 && |
| 864 | t->prev_block_end_time.tv_nsec == 0 )){ |
| 865 | ltt_tracefile_backward_find_time_block(t, time); |
| 866 | }else{ |
| 867 | updateTracefile(t); |
| 868 | } |
| 869 | } |
| 870 | }else if(tailTime < 0){ |
| 871 | if(t->which_block != t->block_number){ |
| 872 | ltt_tracefile_find_time_block(t, time, t->which_block+1, t->block_number); |
| 873 | return ltt_tracefile_seek_time(t, time); |
| 874 | }else { |
| 875 | t->cur_event_pos = t->buffer + t->block_size; |
| 876 | g_print("End of file\n"); |
| 877 | return; |
| 878 | } |
| 879 | }else if(tailTime == 0){ |
| 880 | t->cur_event_pos = t->last_event_pos; |
| 881 | t->current_event_time = time; |
| 882 | t->cur_heart_beat_number = 0; |
| 883 | t->prev_event_time.tv_sec = 0; |
| 884 | t->prev_event_time.tv_nsec = 0; |
| 885 | return; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | /***************************************************************************** |
| 890 | * Seek to the first event with position equal or larger to ep |
| 891 | ****************************************************************************/ |
| 892 | |
| 893 | void ltt_tracefile_seek_position(LttTracefile *t, LttEventPosition *ep) |
| 894 | { |
| 895 | //if we are at the right place, just return |
| 896 | if(t->which_block == ep->block_num && t->which_event == ep->event_num) |
| 897 | return; |
| 898 | |
| 899 | if(t->which_block == ep->block_num) updateTracefile(t); |
| 900 | else readBlock(t,ep->block_num); |
| 901 | |
| 902 | //event offset is availiable |
| 903 | if(ep->old_position){ |
| 904 | t->cur_heart_beat_number = ep->heart_beat_number; |
| 905 | t->cur_event_pos = t->buffer + ep->event_offset; |
| 906 | return; |
| 907 | } |
| 908 | |
| 909 | //only block number and event index are availiable |
| 910 | while(t->which_event < ep->event_num) ltt_tracefile_read(t); |
| 911 | |
| 912 | return; |
| 913 | } |
| 914 | |
| 915 | /***************************************************************************** |
| 916 | *Function name |
| 917 | * ltt_tracefile_read : read the current event, set the pointer to the next |
| 918 | *Input params |
| 919 | * t : tracefile |
| 920 | *Return value |
| 921 | * LttEvent * : an event to be processed |
| 922 | ****************************************************************************/ |
| 923 | |
| 924 | LttEvent *ltt_tracefile_read(LttTracefile *t) |
| 925 | { |
| 926 | LttEvent * lttEvent = &t->an_event; |
| 927 | int err; |
| 928 | |
| 929 | if(t->cur_event_pos == t->buffer + t->block_size){ |
| 930 | if(t->which_block == t->block_number){ |
| 931 | return NULL; |
| 932 | } |
| 933 | err = readBlock(t, t->which_block + 1); |
| 934 | if(err)g_error("Can not read tracefile"); |
| 935 | } |
| 936 | |
| 937 | lttEvent->event_id = (int)(*(guint16 *)(t->cur_event_pos)); |
| 938 | if(lttEvent->event_id == TRACE_TIME_HEARTBEAT) |
| 939 | t->cur_heart_beat_number++; |
| 940 | |
| 941 | t->prev_event_time = t->current_event_time; |
| 942 | // t->current_event_time = getEventTime(t); |
| 943 | |
| 944 | lttEvent->time_delta = *(guint32 *)(t->cur_event_pos + EVENT_ID_SIZE); |
| 945 | lttEvent->event_time = t->current_event_time; |
| 946 | |
| 947 | lttEvent->tracefile = t; |
| 948 | lttEvent->data = t->cur_event_pos + EVENT_HEADER_SIZE; |
| 949 | lttEvent->which_block = t->which_block; |
| 950 | lttEvent->which_event = t->which_event; |
| 951 | |
| 952 | //update the fields of the current event and go to the next event |
| 953 | err = skipEvent(t); |
| 954 | if(err == ERANGE) g_error("event id is out of range\n"); |
| 955 | |
| 956 | lttEvent->event_cycle_count = t->cur_cycle_count; |
| 957 | |
| 958 | return lttEvent; |
| 959 | } |
| 960 | |
| 961 | /**************************************************************************** |
| 962 | *Function name |
| 963 | * readFile : wrap function to read from a file |
| 964 | *Input Params |
| 965 | * fd : file descriptor |
| 966 | * buf : buf to contain the content |
| 967 | * size : number of bytes to be read |
| 968 | * mesg : message to be printed if some thing goes wrong |
| 969 | *return value |
| 970 | * 0 : success |
| 971 | * EIO : can not read from the file |
| 972 | ****************************************************************************/ |
| 973 | |
| 974 | int readFile(int fd, void * buf, size_t size, char * mesg) |
| 975 | { |
| 976 | ssize_t nbBytes; |
| 977 | nbBytes = read(fd, buf, size); |
| 978 | if(nbBytes != size){ |
| 979 | printf("%s\n",mesg); |
| 980 | return EIO; |
| 981 | } |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | /**************************************************************************** |
| 986 | *Function name |
| 987 | * readBlock : read a block from the file |
| 988 | *Input Params |
| 989 | * lttdes : ltt trace file |
| 990 | * whichBlock : the block which will be read |
| 991 | *return value |
| 992 | * 0 : success |
| 993 | * EINVAL : lseek fail |
| 994 | * EIO : can not read from the file |
| 995 | ****************************************************************************/ |
| 996 | |
| 997 | int readBlock(LttTracefile * tf, int whichBlock) |
| 998 | { |
| 999 | off_t nbBytes; |
| 1000 | guint32 lostSize; |
| 1001 | |
| 1002 | if(whichBlock - tf->which_block == 1 && tf->which_block != 0){ |
| 1003 | tf->prev_block_end_time = tf->a_block_end->time; |
| 1004 | tf->prev_event_time = tf->a_block_end->time; |
| 1005 | }else{ |
| 1006 | tf->prev_block_end_time.tv_sec = 0; |
| 1007 | tf->prev_block_end_time.tv_nsec = 0; |
| 1008 | tf->prev_event_time.tv_sec = 0; |
| 1009 | tf->prev_event_time.tv_nsec = 0; |
| 1010 | } |
| 1011 | |
| 1012 | nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET); |
| 1013 | if(nbBytes == -1) return EINVAL; |
| 1014 | |
| 1015 | if(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block")) |
| 1016 | return EIO; |
| 1017 | |
| 1018 | tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE); |
| 1019 | lostSize = *(guint32 *)(tf->buffer + tf->block_size - sizeof(guint32)); |
| 1020 | tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size - |
| 1021 | lostSize + EVENT_HEADER_SIZE); |
| 1022 | tf->last_event_pos = tf->buffer + tf->block_size - lostSize; |
| 1023 | |
| 1024 | tf->which_block = whichBlock; |
| 1025 | tf->which_event = 1; |
| 1026 | tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev |
| 1027 | tf->cur_heart_beat_number = 0; |
| 1028 | |
| 1029 | getCyclePerNsec(tf); |
| 1030 | |
| 1031 | tf->current_event_time = getEventTime(tf); |
| 1032 | |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
| 1036 | /***************************************************************************** |
| 1037 | *Function name |
| 1038 | * updateTracefile : reinitialize the info of the block which is already |
| 1039 | * in the buffer |
| 1040 | *Input params |
| 1041 | * tf : tracefile |
| 1042 | ****************************************************************************/ |
| 1043 | |
| 1044 | void updateTracefile(LttTracefile * tf) |
| 1045 | { |
| 1046 | tf->which_event = 1; |
| 1047 | tf->cur_event_pos = tf->buffer; |
| 1048 | tf->current_event_time = getEventTime(tf); |
| 1049 | tf->cur_heart_beat_number = 0; |
| 1050 | |
| 1051 | tf->prev_event_time.tv_sec = 0; |
| 1052 | tf->prev_event_time.tv_nsec = 0; |
| 1053 | } |
| 1054 | |
| 1055 | /***************************************************************************** |
| 1056 | *Function name |
| 1057 | * skipEvent : go to the next event, update the fields of the current event |
| 1058 | *Input params |
| 1059 | * t : tracefile |
| 1060 | *return value |
| 1061 | * 0 : success |
| 1062 | * ERANGE : event id is out of range |
| 1063 | ****************************************************************************/ |
| 1064 | |
| 1065 | int skipEvent(LttTracefile * t) |
| 1066 | { |
| 1067 | int evId, err; |
| 1068 | void * evData; |
| 1069 | LttEventType * evT; |
| 1070 | LttField * rootFld; |
| 1071 | |
| 1072 | evId = (int)(*(guint16 *)(t->cur_event_pos)); |
| 1073 | evData = t->cur_event_pos + EVENT_HEADER_SIZE; |
| 1074 | |
| 1075 | evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId); |
| 1076 | |
| 1077 | if(evT) rootFld = evT->root_field; |
| 1078 | else return ERANGE; |
| 1079 | |
| 1080 | if(rootFld){ |
| 1081 | //event has string/sequence or the last event is not the same event |
| 1082 | if((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event) |
| 1083 | && rootFld->field_fixed == 0){ |
| 1084 | setFieldsOffset(t, evT, evData, t->trace); |
| 1085 | } |
| 1086 | t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size; |
| 1087 | }else t->cur_event_pos += EVENT_HEADER_SIZE; |
| 1088 | |
| 1089 | evT->latest_block = t->which_block; |
| 1090 | evT->latest_event = t->which_event; |
| 1091 | |
| 1092 | //the next event is in the next block |
| 1093 | if(evId == TRACE_BLOCK_END){ |
| 1094 | t->cur_event_pos = t->buffer + t->block_size; |
| 1095 | }else{ |
| 1096 | t->which_event++; |
| 1097 | t->current_event_time = getEventTime(t); |
| 1098 | } |
| 1099 | |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | /***************************************************************************** |
| 1104 | *Function name |
| 1105 | * getCyclePerNsec : calculate cycles per nsec for current block |
| 1106 | *Input Params |
| 1107 | * t : tracefile |
| 1108 | ****************************************************************************/ |
| 1109 | |
| 1110 | void getCyclePerNsec(LttTracefile * t) |
| 1111 | { |
| 1112 | LttTime lBufTotalTime; /* Total time for this buffer */ |
| 1113 | LttCycleCount lBufTotalNSec; /* Total time for this buffer in nsecs */ |
| 1114 | LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */ |
| 1115 | |
| 1116 | /* Calculate the total time for this buffer */ |
| 1117 | lBufTotalTime = ltt_time_sub(t->a_block_end->time, t->a_block_start->time); |
| 1118 | |
| 1119 | /* Calculate the total cycles for this bufffer */ |
| 1120 | lBufTotalCycle = t->a_block_end->cycle_count; |
| 1121 | lBufTotalCycle -= t->a_block_start->cycle_count; |
| 1122 | |
| 1123 | /* Convert the total time to nsecs */ |
| 1124 | lBufTotalNSec = lBufTotalTime.tv_sec; |
| 1125 | lBufTotalNSec *= NANOSECONDS_PER_SECOND; |
| 1126 | lBufTotalNSec += lBufTotalTime.tv_nsec; |
| 1127 | |
| 1128 | t->cycle_per_nsec = (double)lBufTotalCycle / (double)lBufTotalNSec; |
| 1129 | } |
| 1130 | |
| 1131 | /**************************************************************************** |
| 1132 | *Function name |
| 1133 | * getEventTime : obtain the time of an event |
| 1134 | *Input params |
| 1135 | * tf : tracefile |
| 1136 | *Return value |
| 1137 | * LttTime : the time of the event |
| 1138 | ****************************************************************************/ |
| 1139 | |
| 1140 | LttTime getEventTime(LttTracefile * tf) |
| 1141 | { |
| 1142 | LttTime time; |
| 1143 | LttCycleCount cycle_count; // cycle count for the current event |
| 1144 | LttCycleCount lEventTotalCycle; // Total cycles from start for event |
| 1145 | double lEventNSec; // Total usecs from start for event |
| 1146 | LttTime lTimeOffset; // Time offset in struct LttTime |
| 1147 | guint16 evId; |
| 1148 | gint64 nanoSec, tmpCycleCount = (((guint64)1)<<32); |
| 1149 | |
| 1150 | evId = *(guint16 *)tf->cur_event_pos; |
| 1151 | if(evId == TRACE_BLOCK_START){ |
| 1152 | tf->count = 0; |
| 1153 | tf->pre_cycle_count = 0; |
| 1154 | tf->cur_cycle_count = tf->a_block_start->cycle_count; |
| 1155 | return tf->a_block_start->time; |
| 1156 | }else if(evId == TRACE_BLOCK_END){ |
| 1157 | tf->count = 0; |
| 1158 | tf->pre_cycle_count = 0; |
| 1159 | tf->cur_cycle_count = tf->a_block_end->cycle_count; |
| 1160 | return tf->a_block_end->time; |
| 1161 | } |
| 1162 | |
| 1163 | // Calculate total time in cycles from start of buffer for this event |
| 1164 | cycle_count = (LttCycleCount)*(guint32 *)(tf->cur_event_pos + EVENT_ID_SIZE); |
| 1165 | |
| 1166 | if(cycle_count < tf->pre_cycle_count)tf->count++; |
| 1167 | tf->pre_cycle_count = cycle_count; |
| 1168 | cycle_count += tmpCycleCount * tf->count; |
| 1169 | |
| 1170 | if(tf->cur_heart_beat_number > tf->count) |
| 1171 | cycle_count += tmpCycleCount * (tf->cur_heart_beat_number - tf->count); |
| 1172 | |
| 1173 | tf->cur_cycle_count = cycle_count; |
| 1174 | |
| 1175 | lEventTotalCycle = cycle_count; |
| 1176 | lEventTotalCycle -= tf->a_block_start->cycle_count; |
| 1177 | |
| 1178 | // Convert it to nsecs |
| 1179 | lEventNSec = lEventTotalCycle / tf->cycle_per_nsec; |
| 1180 | nanoSec = lEventNSec; |
| 1181 | |
| 1182 | // Determine offset in struct LttTime |
| 1183 | lTimeOffset.tv_nsec = nanoSec % NANOSECONDS_PER_SECOND; |
| 1184 | lTimeOffset.tv_sec = nanoSec / NANOSECONDS_PER_SECOND; |
| 1185 | |
| 1186 | time = ltt_time_add(tf->a_block_start->time, lTimeOffset); |
| 1187 | |
| 1188 | return time; |
| 1189 | } |
| 1190 | |
| 1191 | /***************************************************************************** |
| 1192 | *Function name |
| 1193 | * setFieldsOffset : set offset of the fields |
| 1194 | *Input params |
| 1195 | * tracefile : opened trace file |
| 1196 | * evT : the event type |
| 1197 | * evD : event data, it may be NULL |
| 1198 | ****************************************************************************/ |
| 1199 | |
| 1200 | void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t) |
| 1201 | { |
| 1202 | LttField * rootFld = evT->root_field; |
| 1203 | // rootFld->base_address = evD; |
| 1204 | |
| 1205 | if(rootFld) |
| 1206 | rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t); |
| 1207 | } |
| 1208 | |
| 1209 | /***************************************************************************** |
| 1210 | *Function name |
| 1211 | * getFieldtypeSize: get the size of the field type (primitive type) |
| 1212 | *Input params |
| 1213 | * tracefile : opened trace file |
| 1214 | * evT : event type |
| 1215 | * offsetRoot : offset from the root |
| 1216 | * offsetParent : offset from the parrent |
| 1217 | * fld : field |
| 1218 | * evD : event data, it may be NULL |
| 1219 | *Return value |
| 1220 | * int : size of the field |
| 1221 | ****************************************************************************/ |
| 1222 | |
| 1223 | int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot, |
| 1224 | int offsetParent, LttField * fld, void *evD, LttTrace *trace) |
| 1225 | { |
| 1226 | int size, size1, element_number, i, offset1, offset2; |
| 1227 | LttType * type = fld->field_type; |
| 1228 | |
| 1229 | if(t){ |
| 1230 | if(evT->latest_block==t->which_block && evT->latest_event==t->which_event){ |
| 1231 | return fld->field_size; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | if(fld->field_fixed == 1){ |
| 1236 | if(fld == evT->root_field) return fld->field_size; |
| 1237 | } |
| 1238 | |
| 1239 | if(type->type_class != LTT_STRUCT && type->type_class != LTT_ARRAY && |
| 1240 | type->type_class != LTT_SEQUENCE && type->type_class != LTT_STRING){ |
| 1241 | if(fld->field_fixed == -1){ |
| 1242 | size = (int) ltt_type_size(trace, type); |
| 1243 | fld->field_fixed = 1; |
| 1244 | }else size = fld->field_size; |
| 1245 | |
| 1246 | }else if(type->type_class == LTT_ARRAY){ |
| 1247 | element_number = (int) type->element_number; |
| 1248 | if(fld->field_fixed == -1){ |
| 1249 | size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace); |
| 1250 | if(size == 0){ //has string or sequence |
| 1251 | fld->field_fixed = 0; |
| 1252 | }else{ |
| 1253 | fld->field_fixed = 1; |
| 1254 | size *= element_number; |
| 1255 | } |
| 1256 | }else if(fld->field_fixed == 0){// has string or sequence |
| 1257 | size = 0; |
| 1258 | for(i=0;i<element_number;i++){ |
| 1259 | size += getFieldtypeSize(t, evT, offsetRoot+size,size, |
| 1260 | fld->child[0], evD+size, trace); |
| 1261 | } |
| 1262 | }else size = fld->field_size; |
| 1263 | |
| 1264 | }else if(type->type_class == LTT_SEQUENCE){ |
| 1265 | size1 = (int) ltt_type_size(trace, type); |
| 1266 | if(fld->field_fixed == -1){ |
| 1267 | fld->sequ_number_size = size1; |
| 1268 | fld->field_fixed = 0; |
| 1269 | size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace); |
| 1270 | fld->element_size = size; |
| 1271 | }else{//0: sequence |
| 1272 | element_number = getIntNumber(size1,evD); |
| 1273 | type->element_number = element_number; |
| 1274 | if(fld->element_size > 0){ |
| 1275 | size = element_number * fld->element_size; |
| 1276 | }else{//sequence has string or sequence |
| 1277 | size = 0; |
| 1278 | for(i=0;i<element_number;i++){ |
| 1279 | size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1, |
| 1280 | fld->child[0], evD+size+size1, trace); |
| 1281 | } |
| 1282 | } |
| 1283 | size += size1; |
| 1284 | } |
| 1285 | |
| 1286 | }else if(type->type_class == LTT_STRING){ |
| 1287 | size = 0; |
| 1288 | if(fld->field_fixed == -1){ |
| 1289 | fld->field_fixed = 0; |
| 1290 | }else{//0: string |
| 1291 | size = strlen((char*)evD) + 1; //include end : '\0' |
| 1292 | } |
| 1293 | |
| 1294 | }else if(type->type_class == LTT_STRUCT){ |
| 1295 | element_number = (int) type->element_number; |
| 1296 | size = 0; |
| 1297 | if(fld->field_fixed == -1){ |
| 1298 | offset1 = offsetRoot; |
| 1299 | offset2 = 0; |
| 1300 | for(i=0;i<element_number;i++){ |
| 1301 | size1=getFieldtypeSize(t, evT,offset1,offset2, fld->child[i], NULL, trace); |
| 1302 | if(size1 > 0 && size >= 0){ |
| 1303 | size += size1; |
| 1304 | if(offset1 >= 0) offset1 += size1; |
| 1305 | offset2 += size1; |
| 1306 | }else{ |
| 1307 | size = -1; |
| 1308 | offset1 = -1; |
| 1309 | offset2 = -1; |
| 1310 | } |
| 1311 | } |
| 1312 | if(size == -1){ |
| 1313 | fld->field_fixed = 0; |
| 1314 | size = 0; |
| 1315 | }else fld->field_fixed = 1; |
| 1316 | }else if(fld->field_fixed == 0){ |
| 1317 | offset1 = offsetRoot; |
| 1318 | offset2 = 0; |
| 1319 | for(i=0;i<element_number;i++){ |
| 1320 | size=getFieldtypeSize(t,evT,offset1,offset2,fld->child[i],evD+offset2, trace); |
| 1321 | offset1 += size; |
| 1322 | offset2 += size; |
| 1323 | } |
| 1324 | size = offset2; |
| 1325 | }else size = fld->field_size; |
| 1326 | } |
| 1327 | |
| 1328 | fld->offset_root = offsetRoot; |
| 1329 | fld->offset_parent = offsetParent; |
| 1330 | if(!evD){ |
| 1331 | fld->fixed_root = (offsetRoot==-1) ? 0 : 1; |
| 1332 | fld->fixed_parent = (offsetParent==-1) ? 0 : 1; |
| 1333 | } |
| 1334 | fld->field_size = size; |
| 1335 | |
| 1336 | return size; |
| 1337 | } |
| 1338 | |
| 1339 | |
| 1340 | /***************************************************************************** |
| 1341 | *Function name |
| 1342 | * getIntNumber : get an integer number |
| 1343 | *Input params |
| 1344 | * size : the size of the integer |
| 1345 | * evD : the event data |
| 1346 | *Return value |
| 1347 | * int : an integer |
| 1348 | ****************************************************************************/ |
| 1349 | |
| 1350 | int getIntNumber(int size, void *evD) |
| 1351 | { |
| 1352 | gint64 i; |
| 1353 | if(size == 1) i = *(gint8 *)evD; |
| 1354 | else if(size == 2) i = *(gint16 *)evD; |
| 1355 | else if(size == 4) i = *(gint32 *)evD; |
| 1356 | else if(size == 8) i = *(gint64 *)evD; |
| 1357 | |
| 1358 | return (int) i; |
| 1359 | } |
| 1360 | |
| 1361 | /***************************************************************************** |
| 1362 | *Function name |
| 1363 | * getDataEndianType : get the data type size and endian type of the local |
| 1364 | * machine |
| 1365 | *Input params |
| 1366 | * size : size of data type |
| 1367 | * endian : endian type, little or big |
| 1368 | ****************************************************************************/ |
| 1369 | |
| 1370 | void getDataEndianType(LttArchSize * size, LttArchEndian * endian) |
| 1371 | { |
| 1372 | int i = 1; |
| 1373 | char c = (char) i; |
| 1374 | int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *); |
| 1375 | |
| 1376 | if(c == 1) *endian = LTT_LITTLE_ENDIAN; |
| 1377 | else *endian = LTT_BIG_ENDIAN; |
| 1378 | |
| 1379 | if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4) |
| 1380 | *size = LTT_LP32; |
| 1381 | else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4) |
| 1382 | *size = LTT_ILP32; |
| 1383 | else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8) |
| 1384 | *size = LTT_LP64; |
| 1385 | else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8) |
| 1386 | *size = LTT_ILP64; |
| 1387 | else *size = LTT_UNKNOWN; |
| 1388 | } |
| 1389 | |
| 1390 | /* get the node name of the system */ |
| 1391 | |
| 1392 | char * ltt_trace_system_description_node_name (LttSystemDescription * s) |
| 1393 | { |
| 1394 | return s->node_name; |
| 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | /* get the domain name of the system */ |
| 1399 | |
| 1400 | char * ltt_trace_system_description_domain_name (LttSystemDescription * s) |
| 1401 | { |
| 1402 | return s->domain_name; |
| 1403 | } |
| 1404 | |
| 1405 | |
| 1406 | /* get the description of the system */ |
| 1407 | |
| 1408 | char * ltt_trace_system_description_description (LttSystemDescription * s) |
| 1409 | { |
| 1410 | return s->description; |
| 1411 | } |
| 1412 | |
| 1413 | |
| 1414 | /* get the start time of the trace */ |
| 1415 | |
| 1416 | LttTime ltt_trace_system_description_trace_start_time(LttSystemDescription *s) |
| 1417 | { |
| 1418 | return s->trace_start; |
| 1419 | } |
| 1420 | |