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