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