| 1 | #ifdef HAVE_CONFIG_H |
| 2 | #include <config.h> |
| 3 | #endif |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <unistd.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <sys/stat.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <errno.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | #include <glib.h> |
| 15 | #include "LTTTypes.h" |
| 16 | #include "LinuxEvents.h" |
| 17 | |
| 18 | #define TRACE_HEARTBEAT_ID 19 |
| 19 | #define PROCESS_FORK_ID 20 |
| 20 | #define PROCESS_EXIT_ID 21 |
| 21 | |
| 22 | #define INFO_ENTRY 9 |
| 23 | #define OVERFLOW_FIGURE 0x100000000ULL |
| 24 | |
| 25 | typedef struct _new_process |
| 26 | { |
| 27 | uint32_t event_data1; /* Data associated with event */ |
| 28 | uint32_t event_data2; |
| 29 | } LTT_PACKED_STRUCT new_process; |
| 30 | |
| 31 | #define write_to_buffer(DEST, SRC, SIZE) \ |
| 32 | do\ |
| 33 | {\ |
| 34 | memcpy(DEST, SRC, SIZE);\ |
| 35 | DEST += SIZE;\ |
| 36 | } while(0); |
| 37 | |
| 38 | int readFile(int fd, void * buf, size_t size, char * mesg) |
| 39 | { |
| 40 | ssize_t nbBytes = read(fd, buf, size); |
| 41 | |
| 42 | if((size_t)nbBytes != size) { |
| 43 | if(nbBytes < 0) { |
| 44 | perror("Error in readFile : "); |
| 45 | } else { |
| 46 | printf("%s\n",mesg); |
| 47 | } |
| 48 | exit(1); |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | void getDataEndianType(char * size, char * endian) |
| 54 | { |
| 55 | int i = 1; |
| 56 | char c = (char) i; |
| 57 | int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *); |
| 58 | |
| 59 | if(c == 1) strcpy(endian,"LITTLE_ENDIAN"); |
| 60 | else strcpy(endian, "BIG_ENDIAN"); |
| 61 | |
| 62 | if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4) |
| 63 | strcpy(size,"LP32"); |
| 64 | else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4) |
| 65 | strcpy(size,"ILP32"); |
| 66 | else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8) |
| 67 | strcpy(size,"LP64"); |
| 68 | else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8) |
| 69 | strcpy(size,"ILP64"); |
| 70 | else strcpy(size,"UNKNOWN"); |
| 71 | } |
| 72 | |
| 73 | #define BUFFER_SIZE 80 |
| 74 | |
| 75 | typedef struct _buffer_start{ |
| 76 | uint32_t seconds; |
| 77 | uint32_t nanoseconds; |
| 78 | uint64_t cycle_count; |
| 79 | uint32_t block_id; |
| 80 | } __attribute__ ((packed)) buffer_start; |
| 81 | |
| 82 | typedef struct _buffer_end{ |
| 83 | uint32_t seconds; |
| 84 | uint32_t nanoseconds; |
| 85 | uint64_t cycle_count; |
| 86 | uint32_t block_id; |
| 87 | } __attribute__ ((packed)) buffer_end; |
| 88 | |
| 89 | |
| 90 | typedef struct _heartbeat{ |
| 91 | uint32_t seconds; |
| 92 | uint32_t nanoseconds; |
| 93 | uint64_t cycle_count; |
| 94 | } __attribute__ ((packed)) heartbeat; |
| 95 | |
| 96 | |
| 97 | int main(int argc, char ** argv){ |
| 98 | |
| 99 | int fd, fdCpu; |
| 100 | FILE * fp; |
| 101 | int fdFac, fdIntr, fdProc; |
| 102 | char arch_size[BUFFER_SIZE]; |
| 103 | char endian[BUFFER_SIZE]; |
| 104 | char node_name[BUFFER_SIZE]; |
| 105 | char domainname[BUFFER_SIZE]; |
| 106 | char kernel_name[BUFFER_SIZE]; |
| 107 | char kernel_release[BUFFER_SIZE]; |
| 108 | char kernel_version[BUFFER_SIZE]; |
| 109 | char machine[BUFFER_SIZE]; |
| 110 | char processor[BUFFER_SIZE]; |
| 111 | char hardware_platform[BUFFER_SIZE]; |
| 112 | char operating_system[BUFFER_SIZE]; |
| 113 | int cpu; |
| 114 | int ltt_block_size=0; |
| 115 | int ltt_major_version=0; |
| 116 | int ltt_minor_version=0; |
| 117 | int ltt_log_cpu; |
| 118 | guint ltt_trace_start_size = 0; |
| 119 | char buf[BUFFER_SIZE]; |
| 120 | int i, k; |
| 121 | |
| 122 | uint8_t cpu_id; |
| 123 | |
| 124 | char foo[4*BUFFER_SIZE]; |
| 125 | char foo_eventdefs[4*BUFFER_SIZE]; |
| 126 | char foo_control[4*BUFFER_SIZE]; |
| 127 | char foo_cpu[4*BUFFER_SIZE]; |
| 128 | char foo_info[4*BUFFER_SIZE]; |
| 129 | |
| 130 | char foo_control_facilities[4*BUFFER_SIZE]; |
| 131 | char foo_control_processes[4*BUFFER_SIZE]; |
| 132 | char foo_control_interrupts[4*BUFFER_SIZE]; |
| 133 | char foo_info_system[4*BUFFER_SIZE]; |
| 134 | |
| 135 | struct stat lTDFStat; |
| 136 | off_t file_size; |
| 137 | int block_number, block_size; |
| 138 | char * buffer, *buf_out, cpuStr[4*BUFFER_SIZE]; |
| 139 | char * buf_fac, * buf_intr, * buf_proc; |
| 140 | void * write_pos, *write_pos_fac, * write_pos_intr, *write_pos_proc; |
| 141 | trace_start_any *tStart; |
| 142 | trace_buffer_start *tBufStart; |
| 143 | trace_buffer_end *tBufEnd; |
| 144 | trace_file_system * tFileSys; |
| 145 | uint16_t newId, startId, tmpId; |
| 146 | uint8_t evId; |
| 147 | uint32_t time_delta, startTimeDelta; |
| 148 | void * cur_pos, *end_pos; |
| 149 | buffer_start start, start_proc, start_intr; |
| 150 | buffer_end end, end_proc, end_intr; |
| 151 | heartbeat beat; |
| 152 | uint64_t adaptation_tsc; // (Mathieu) |
| 153 | uint32_t size_lost; |
| 154 | int reserve_size = sizeof(buffer_start) + |
| 155 | sizeof(buffer_end) + //buffer_end event |
| 156 | sizeof(uint32_t); //lost size |
| 157 | int nb_para; |
| 158 | |
| 159 | new_process process; |
| 160 | |
| 161 | if(argc < 4){ |
| 162 | printf("Usage : ./convert processfile_name number_of_cpu tracefile1 tracefile2 ... trace_creation_directory\n"); |
| 163 | printf("For more details, see README.\n"); |
| 164 | exit(1); |
| 165 | } |
| 166 | |
| 167 | cpu = atoi(argv[2]); |
| 168 | printf("cpu number = %d\n", cpu); |
| 169 | nb_para = 3 + cpu; |
| 170 | |
| 171 | if(argc != nb_para && argc != nb_para+1){ |
| 172 | printf("need trace files and cpu number or root directory for the new tracefile\n"); |
| 173 | exit(1); |
| 174 | } |
| 175 | |
| 176 | if(argc == nb_para){ |
| 177 | strcpy(foo, "foo"); |
| 178 | strcpy(foo_eventdefs, "foo/eventdefs"); |
| 179 | strcpy(foo_control, "foo/control"); |
| 180 | strcpy(foo_cpu, "foo/cpu"); |
| 181 | strcpy(foo_info, "foo/info"); |
| 182 | }else{ |
| 183 | strcpy(foo, argv[nb_para]); |
| 184 | strcpy(foo_eventdefs, argv[nb_para]); |
| 185 | strcat(foo_eventdefs,"/eventdefs"); |
| 186 | strcpy(foo_control, argv[nb_para]); |
| 187 | strcat(foo_control,"/control"); |
| 188 | strcpy(foo_cpu, argv[nb_para]); |
| 189 | strcat(foo_cpu,"/cpu"); |
| 190 | strcpy(foo_info, argv[nb_para]); |
| 191 | strcat(foo_info,"/info"); |
| 192 | } |
| 193 | strcpy(foo_control_facilities, foo_control); |
| 194 | strcat(foo_control_facilities,"/facilities"); |
| 195 | strcpy(foo_control_processes, foo_control); |
| 196 | strcat(foo_control_processes, "/processes"); |
| 197 | strcpy(foo_control_interrupts, foo_control); |
| 198 | strcat(foo_control_interrupts, "/interrupts"); |
| 199 | strcpy(foo_info_system, foo_info); |
| 200 | strcat(foo_info_system, "/system.xml"); |
| 201 | |
| 202 | |
| 203 | getDataEndianType(arch_size, endian); |
| 204 | printf("Arch_size: %s, Endian: %s\n", arch_size, endian); |
| 205 | |
| 206 | fp = fopen("sysInfo.out","r"); |
| 207 | if(!fp){ |
| 208 | g_error("Unable to open file sysInfo.out\n"); |
| 209 | } |
| 210 | |
| 211 | for(i=0;i<INFO_ENTRY;i++){ |
| 212 | if(!fgets(buf,BUFFER_SIZE-1,fp)) |
| 213 | g_error("The format of sysInfo.out is not right\n"); |
| 214 | if(strncmp(buf,"node_name=",10)==0){ |
| 215 | strcpy(node_name,&buf[10]); |
| 216 | node_name[strlen(node_name)-1] = '\0'; |
| 217 | }else if(strncmp(buf,"domainname=",11)==0){ |
| 218 | strcpy(domainname,&buf[11]); |
| 219 | domainname[strlen(domainname)-1] = '\0'; |
| 220 | }else if(strncmp(buf,"kernel_name=",12)==0){ |
| 221 | strcpy(kernel_name,&buf[12]); |
| 222 | kernel_name[strlen(kernel_name)-1] = '\0'; |
| 223 | }else if(strncmp(buf,"kernel_release=",15)==0){ |
| 224 | strcpy(kernel_release,&buf[15]); |
| 225 | kernel_release[strlen(kernel_release)-1] = '\0'; |
| 226 | }else if(strncmp(buf,"kernel_version=",15)==0){ |
| 227 | strcpy(kernel_version,&buf[15]); |
| 228 | kernel_version[strlen(kernel_version)-1] = '\0'; |
| 229 | }else if(strncmp(buf,"machine=",8)==0){ |
| 230 | strcpy(machine,&buf[8]); |
| 231 | machine[strlen(machine)-1] = '\0'; |
| 232 | }else if(strncmp(buf,"processor=",10)==0){ |
| 233 | strcpy(processor,&buf[10]); |
| 234 | processor[strlen(processor)-1] = '\0'; |
| 235 | }else if(strncmp(buf,"hardware_platform=",18)==0){ |
| 236 | strcpy(hardware_platform,&buf[18]); |
| 237 | hardware_platform[strlen(hardware_platform)-1] = '\0'; |
| 238 | }else if(strncmp(buf,"operating_system=",17)==0){ |
| 239 | strcpy(operating_system,&buf[17]); |
| 240 | operating_system[strlen(operating_system)-1] = '\0'; |
| 241 | } |
| 242 | } |
| 243 | fclose(fp); |
| 244 | |
| 245 | if(mkdir(foo, S_IFDIR | S_IRWXU | S_IRGRP | S_IROTH)) |
| 246 | g_error("can not make %s directory", foo); |
| 247 | if(mkdir(foo_info, S_IFDIR | S_IRWXU | S_IRGRP | S_IROTH)) |
| 248 | g_error("can not make %s directory", foo_info); |
| 249 | if(mkdir(foo_cpu, S_IFDIR | S_IRWXU | S_IRGRP | S_IROTH)) |
| 250 | g_error("can not make %s directory", foo_cpu); |
| 251 | if(mkdir(foo_control, S_IFDIR | S_IRWXU | S_IRGRP | S_IROTH)) |
| 252 | g_error("can not make %s directory", foo_control); |
| 253 | if(mkdir(foo_eventdefs, S_IFDIR | S_IRWXU | S_IRGRP | S_IROTH)) |
| 254 | g_error("can not make %s directory", foo_eventdefs); |
| 255 | |
| 256 | fp = fopen(foo_info_system,"w"); |
| 257 | if(!fp){ |
| 258 | g_error("Unable to open file system.xml\n"); |
| 259 | } |
| 260 | |
| 261 | fdFac = open(foo_control_facilities,O_CREAT | O_RDWR | O_TRUNC,S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); |
| 262 | if(fdFac < 0){ |
| 263 | g_error("Unable to open file facilities\n"); |
| 264 | } |
| 265 | fdIntr = open(foo_control_interrupts,O_CREAT | O_RDWR | O_TRUNC,S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); |
| 266 | if(fdIntr<0){ |
| 267 | g_error("Unable to open file interrupts\n"); |
| 268 | } |
| 269 | fdProc = open(foo_control_processes,O_CREAT | O_RDWR | O_TRUNC,S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); |
| 270 | if(fdProc<0){ |
| 271 | g_error("Unable to open file process\n"); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | for(k=0;k<cpu;k++){ |
| 276 | fd = open(argv[nb_para-cpu+k], O_RDONLY, 0); |
| 277 | if(fd < 0){ |
| 278 | g_error("Unable to open input data file %s\n", argv[nb_para-cpu+k]); |
| 279 | } |
| 280 | |
| 281 | if(fstat(fd, &lTDFStat) < 0){ |
| 282 | g_error("Unable to get the status of the input data file\n"); |
| 283 | } |
| 284 | file_size = lTDFStat.st_size; |
| 285 | |
| 286 | buffer = g_new(char, 4000); |
| 287 | readFile(fd,(void*)buffer, 3500, "Unable to read block header"); |
| 288 | |
| 289 | cur_pos= buffer; |
| 290 | evId = *(uint8_t *)cur_pos; |
| 291 | cur_pos += sizeof(uint8_t); |
| 292 | newId = evId; |
| 293 | time_delta = *(uint32_t*)cur_pos; |
| 294 | cur_pos += sizeof(uint32_t); |
| 295 | tBufStart = (trace_buffer_start*)cur_pos; |
| 296 | cur_pos += sizeof(trace_buffer_start); |
| 297 | cur_pos += sizeof(uint16_t); //Skip event size |
| 298 | |
| 299 | evId = *(uint8_t *)cur_pos; |
| 300 | g_assert(evId == TRACE_START); |
| 301 | cur_pos += sizeof(uint8_t); //skip EvId |
| 302 | cur_pos += sizeof(uint32_t); //skip time delta |
| 303 | tStart = (trace_start_any*)cur_pos; |
| 304 | if(tStart->MagicNumber != TRACER_MAGIC_NUMBER) |
| 305 | g_error("Trace magic number does not match : %lx, should be %lx", |
| 306 | tStart->MagicNumber, TRACER_MAGIC_NUMBER); |
| 307 | if(tStart->MajorVersion != TRACER_SUP_VERSION_MAJOR) |
| 308 | g_error("Trace Major number does match : %hu, should be %u", |
| 309 | tStart->MajorVersion, TRACER_SUP_VERSION_MAJOR); |
| 310 | |
| 311 | startId = newId; |
| 312 | startTimeDelta = time_delta; |
| 313 | start.seconds = tBufStart->Time.tv_sec; |
| 314 | /* Fix (Mathieu) */ |
| 315 | start.nanoseconds = tBufStart->Time.tv_usec * 1000; |
| 316 | start.cycle_count = tBufStart->TSC; |
| 317 | start.block_id = tBufStart->ID; |
| 318 | end.block_id = start.block_id; |
| 319 | |
| 320 | |
| 321 | g_printf("Trace version %hu.%hu detected\n", |
| 322 | tStart->MajorVersion, |
| 323 | tStart->MinorVersion); |
| 324 | if(tStart->MinorVersion == 2) { |
| 325 | trace_start_2_2* tStart_2_2 = (trace_start_2_2*)tStart; |
| 326 | ltt_major_version = tStart_2_2->MajorVersion; |
| 327 | ltt_minor_version = tStart_2_2->MinorVersion; |
| 328 | ltt_block_size = tStart_2_2->BufferSize; |
| 329 | ltt_log_cpu = tStart_2_2->LogCPUID; |
| 330 | ltt_trace_start_size = sizeof(trace_start_2_2); |
| 331 | /* Verify if it's a broken 2.2 format */ |
| 332 | if(*(uint8_t*)(cur_pos + sizeof(trace_start_2_2)) == 0) { |
| 333 | /* Cannot have two trace start events. We cannot detect the problem |
| 334 | * if the flight recording flag is set to 1, as it conflicts |
| 335 | * with TRACE_SYSCALL_ENTRY. |
| 336 | */ |
| 337 | g_warning("This is a 2.3 trace format that has a 2.2 tag. Please upgrade your kernel"); |
| 338 | g_printf("Processing the trace as a 2.3 format\n"); |
| 339 | |
| 340 | tStart->MinorVersion = 3; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if(tStart->MinorVersion == 3) { |
| 345 | trace_start_2_3* tStart_2_3 = (trace_start_2_3*)tStart; |
| 346 | ltt_major_version = tStart_2_3->MajorVersion; |
| 347 | ltt_minor_version = tStart_2_3->MinorVersion; |
| 348 | ltt_block_size = tStart_2_3->BufferSize; |
| 349 | ltt_log_cpu = tStart_2_3->LogCPUID; |
| 350 | ltt_trace_start_size = sizeof(trace_start_2_3); |
| 351 | /* We do not use the flight recorder information for now, because we |
| 352 | * never use the .proc file anyway */ |
| 353 | } |
| 354 | |
| 355 | if(ltt_trace_start_size == 0) |
| 356 | g_error("Minor version unknown : %hu. Supported minors : 2, 3", |
| 357 | tStart->MinorVersion); |
| 358 | |
| 359 | block_size = ltt_block_size;//FIXME |
| 360 | block_number = file_size/ltt_block_size; |
| 361 | |
| 362 | g_free(buffer); |
| 363 | buffer = g_new(char, ltt_block_size); |
| 364 | buf_fac = g_new(char, block_size); |
| 365 | write_pos_fac = buf_fac; |
| 366 | buf_intr = g_new(char, block_size); |
| 367 | write_pos_intr = buf_intr; |
| 368 | buf_proc = g_new(char, block_size); |
| 369 | write_pos_proc = buf_proc; |
| 370 | |
| 371 | buf_out = g_new(char, block_size); |
| 372 | write_pos = buf_out; |
| 373 | sprintf(cpuStr,"%s/%d",foo_cpu,k); |
| 374 | fdCpu = open(cpuStr, O_CREAT | O_RDWR | O_TRUNC,S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); //for cpu k |
| 375 | if(fdCpu < 0) g_error("Unable to open cpu file %d\n", k); |
| 376 | lseek(fd,0,SEEK_SET); |
| 377 | |
| 378 | for(i=0;i<block_number;i++){ |
| 379 | int event_count = 0; |
| 380 | |
| 381 | memset((void*)buf_out, 0, block_size); |
| 382 | write_pos = buf_out; |
| 383 | memset((void*)buf_intr, 0, block_size); |
| 384 | memset((void*)buf_fac, 0, block_size); |
| 385 | memset((void*)buf_proc, 0, block_size); |
| 386 | write_pos_intr = buf_intr; |
| 387 | write_pos_fac = buf_fac; |
| 388 | write_pos_proc = buf_proc; |
| 389 | |
| 390 | memset((void*)buffer,0,ltt_block_size); |
| 391 | readFile(fd,(void*)buffer, ltt_block_size, "Unable to read block header"); |
| 392 | |
| 393 | cur_pos= buffer; |
| 394 | evId = *(uint8_t *)cur_pos; |
| 395 | cur_pos += sizeof(uint8_t); |
| 396 | newId = evId; |
| 397 | time_delta = *(uint32_t*)cur_pos; |
| 398 | cur_pos += sizeof(uint32_t); |
| 399 | tBufStart = (trace_buffer_start*)cur_pos; |
| 400 | cur_pos += sizeof(trace_buffer_start); |
| 401 | cur_pos += sizeof(uint16_t); //Skip event size |
| 402 | |
| 403 | startId = newId; |
| 404 | startTimeDelta = time_delta; |
| 405 | start.seconds = tBufStart->Time.tv_sec; |
| 406 | /* usec -> nsec (Mathieu) */ |
| 407 | start.nanoseconds = tBufStart->Time.tv_usec * 1000; |
| 408 | start.block_id = tBufStart->ID; |
| 409 | end.block_id = start.block_id; |
| 410 | |
| 411 | end_pos = buffer + ltt_block_size; //end of the buffer |
| 412 | size_lost = *(uint32_t*)(end_pos - sizeof(uint32_t)); |
| 413 | |
| 414 | end_pos = buffer + ltt_block_size - size_lost ; //buffer_end event |
| 415 | if(ltt_log_cpu){ |
| 416 | tBufEnd = (trace_buffer_end*)(end_pos + 2 * sizeof(uint8_t)+sizeof(uint32_t)); |
| 417 | }else{ |
| 418 | tBufEnd = (trace_buffer_end*)(end_pos+sizeof(uint8_t)+sizeof(uint32_t)); |
| 419 | } |
| 420 | end.seconds = tBufEnd->Time.tv_sec; |
| 421 | /* usec -> nsec (Mathieu) */ |
| 422 | end.nanoseconds = tBufEnd->Time.tv_usec * 1000; |
| 423 | // only 32 bits :( |
| 424 | //end.cycle_count = tBufEnd->TSC; |
| 425 | |
| 426 | //skip buffer start and trace start events |
| 427 | if(i==0) { |
| 428 | //the first block |
| 429 | adaptation_tsc = (uint64_t)tBufStart->TSC; |
| 430 | cur_pos = buffer + sizeof(trace_buffer_start) |
| 431 | + ltt_trace_start_size |
| 432 | + 2*(sizeof(uint8_t) |
| 433 | + sizeof(uint16_t)+sizeof(uint32_t)); |
| 434 | } else { |
| 435 | //other blocks |
| 436 | cur_pos = buffer + sizeof(trace_buffer_start) |
| 437 | + sizeof(uint8_t) |
| 438 | + sizeof(uint16_t)+sizeof(uint32_t); |
| 439 | |
| 440 | /* Fix (Mathieu) */ |
| 441 | if(time_delta < (0xFFFFFFFFULL&adaptation_tsc)) { |
| 442 | /* Overflow */ |
| 443 | adaptation_tsc = (adaptation_tsc&0xFFFFFFFF00000000ULL) |
| 444 | + 0x100000000ULL |
| 445 | + (uint64_t)time_delta; |
| 446 | } else { |
| 447 | /* No overflow */ |
| 448 | adaptation_tsc = (adaptation_tsc&0xFFFFFFFF00000000ULL) + time_delta; |
| 449 | } |
| 450 | |
| 451 | } |
| 452 | start.cycle_count = adaptation_tsc; |
| 453 | |
| 454 | //write start block event |
| 455 | write_to_buffer(write_pos,(void*)&startId, sizeof(uint16_t)); |
| 456 | write_to_buffer(write_pos,(void*)&startTimeDelta, sizeof(uint32_t)); |
| 457 | write_to_buffer(write_pos,(void*)&start, sizeof(buffer_start)); |
| 458 | |
| 459 | //write start block event into processes and interrupts files |
| 460 | write_to_buffer(write_pos_intr,(void*)&startId, sizeof(uint16_t)); |
| 461 | write_to_buffer(write_pos_intr,(void*)&startTimeDelta, sizeof(uint32_t)); |
| 462 | start_intr = start; |
| 463 | start_intr.nanoseconds -= 20; |
| 464 | write_to_buffer(write_pos_intr,(void*)&start_intr, sizeof(buffer_start)); |
| 465 | |
| 466 | write_to_buffer(write_pos_proc,(void*)&startId, sizeof(uint16_t)); |
| 467 | write_to_buffer(write_pos_proc,(void*)&startTimeDelta, sizeof(uint32_t)); |
| 468 | start_proc = start; |
| 469 | start_proc.nanoseconds -= 40; |
| 470 | write_to_buffer(write_pos_proc,(void*)&start_proc, sizeof(buffer_start)); |
| 471 | |
| 472 | //parse *.proc file to get process and irq info |
| 473 | if(i == 0){ |
| 474 | int lIntID; /* Interrupt ID */ |
| 475 | int lPID, lPPID; /* Process PID and Parent PID */ |
| 476 | char lName[256]; /* Process name */ |
| 477 | FILE * fProc; |
| 478 | uint16_t defaultId; |
| 479 | trace_irq_entry irq; |
| 480 | |
| 481 | fProc = fopen(argv[1],"r"); |
| 482 | if(!fProc){ |
| 483 | g_error("Unable to open file %s\n", argv[1]); |
| 484 | } |
| 485 | |
| 486 | while(fscanf(fProc, "PID: %d; PPID: %d; NAME: %s\n", &lPID, &lPPID, lName) > 0){ |
| 487 | defaultId = PROCESS_FORK_ID; |
| 488 | process.event_data1 = lPID; |
| 489 | process.event_data2 = lPPID; |
| 490 | write_to_buffer(write_pos_proc,(void*)&defaultId, sizeof(uint16_t)); |
| 491 | write_to_buffer(write_pos_proc,(void*)&startTimeDelta, sizeof(uint32_t)); |
| 492 | write_to_buffer(write_pos_proc,(void*)&process, sizeof(new_process)); |
| 493 | } |
| 494 | |
| 495 | while(fscanf(fProc, "IRQ: %d; NAME: ", &lIntID) > 0){ |
| 496 | /* Read 'til the end of the line */ |
| 497 | fgets(lName, 200, fProc); |
| 498 | |
| 499 | defaultId = TRACE_IRQ_ENTRY; |
| 500 | irq.irq_id = lIntID; |
| 501 | irq.kernel = 1; |
| 502 | write_to_buffer(write_pos_intr,(void*)&defaultId, sizeof(uint16_t)); |
| 503 | write_to_buffer(write_pos_intr,(void*)&startTimeDelta, sizeof(uint32_t)); |
| 504 | write_to_buffer(write_pos_intr,(void*)&irq, sizeof(trace_irq_entry)); |
| 505 | } |
| 506 | fclose(fProc); |
| 507 | } |
| 508 | |
| 509 | while(1){ |
| 510 | int event_size; |
| 511 | uint64_t timeDelta; |
| 512 | uint8_t subId; |
| 513 | |
| 514 | if(ltt_log_cpu){ |
| 515 | cpu_id = *(uint8_t*)cur_pos; |
| 516 | cur_pos += sizeof(uint8_t); |
| 517 | } |
| 518 | evId = *(uint8_t *)cur_pos; |
| 519 | newId = evId; |
| 520 | if(evId == TRACE_HEARTBEAT) { |
| 521 | newId = TRACE_HEARTBEAT_ID; |
| 522 | } |
| 523 | cur_pos += sizeof(uint8_t); |
| 524 | time_delta = *(uint32_t*)cur_pos; |
| 525 | cur_pos += sizeof(uint32_t); |
| 526 | |
| 527 | |
| 528 | //write event_id and time_delta |
| 529 | write_to_buffer(write_pos,(void*)&newId,sizeof(uint16_t)); |
| 530 | write_to_buffer(write_pos,(void*)&time_delta, sizeof(uint32_t)); |
| 531 | |
| 532 | /* Fix (Mathieu) */ |
| 533 | if(time_delta < (0xFFFFFFFFULL&adaptation_tsc)) { |
| 534 | /* Overflow */ |
| 535 | adaptation_tsc = (adaptation_tsc&0xFFFFFFFF00000000ULL) + 0x100000000ULL |
| 536 | + (uint64_t)time_delta; |
| 537 | } else { |
| 538 | /* No overflow */ |
| 539 | adaptation_tsc = (adaptation_tsc&0xFFFFFFFF00000000ULL) + time_delta; |
| 540 | } |
| 541 | |
| 542 | |
| 543 | if(evId == TRACE_BUFFER_END){ |
| 544 | #if 0 |
| 545 | /* Fix (Mathieu) */ |
| 546 | if(time_delta < (0xFFFFFFFFULL&adaptation_tsc)) { |
| 547 | /* Overflow */ |
| 548 | adaptation_tsc = (adaptation_tsc&0xFFFFFFFF00000000ULL) + 0x100000000ULL |
| 549 | + (uint64_t)time_delta; |
| 550 | } else { |
| 551 | /* No overflow */ |
| 552 | adaptation_tsc = (adaptation_tsc&0xFFFFFFFF00000000ULL) + time_delta; |
| 553 | } |
| 554 | #endif //0 |
| 555 | end.cycle_count = adaptation_tsc; |
| 556 | int size = (void*)buf_out + block_size - write_pos |
| 557 | - sizeof(buffer_end) - sizeof(uint32_t); |
| 558 | |
| 559 | /* size _lost_ ? */ |
| 560 | //int size = (void*)buf_out + block_size - write_pos |
| 561 | // + sizeof(uint16_t) + sizeof(uint32_t); |
| 562 | g_assert((void*)write_pos < (void*)buf_out + block_size); |
| 563 | write_to_buffer(write_pos,(void*)&end,sizeof(buffer_end)); |
| 564 | write_pos = buf_out + block_size - sizeof(uint32_t); |
| 565 | write_to_buffer(write_pos,(void*)&size, sizeof(uint32_t)); |
| 566 | write(fdCpu,(void*)buf_out, block_size); |
| 567 | |
| 568 | //write out processes and intrrupts files |
| 569 | { |
| 570 | int size_intr = block_size + (void*)buf_intr - write_pos_intr |
| 571 | - sizeof(buffer_end) - sizeof(uint32_t); |
| 572 | int size_proc = block_size + (void*)buf_proc - write_pos_proc |
| 573 | - sizeof(buffer_end) - sizeof(uint32_t); |
| 574 | //int size_intr = block_size - (write_pos_intr - (void*)buf_intr); |
| 575 | //int size_proc = block_size - (write_pos_proc - (void*)buf_proc); |
| 576 | write_to_buffer(write_pos_intr,(void*)&newId,sizeof(uint16_t)); |
| 577 | write_to_buffer(write_pos_intr,(void*)&time_delta, sizeof(uint32_t)); |
| 578 | end_intr = end; |
| 579 | end_intr.nanoseconds -= 20; |
| 580 | write_to_buffer(write_pos_intr,(void*)&end_intr,sizeof(buffer_start)); |
| 581 | |
| 582 | write_to_buffer(write_pos_proc,(void*)&newId,sizeof(uint16_t)); |
| 583 | write_to_buffer(write_pos_proc,(void*)&time_delta, sizeof(uint32_t)); |
| 584 | end_proc = end; |
| 585 | end_proc.nanoseconds -= 40; |
| 586 | write_to_buffer(write_pos_proc,(void*)&end_proc,sizeof(buffer_start)); |
| 587 | |
| 588 | write_pos_intr = buf_intr + block_size - sizeof(uint32_t); |
| 589 | write_pos_proc = buf_proc + block_size - sizeof(uint32_t); |
| 590 | write_to_buffer(write_pos_intr,(void*)&size_intr, sizeof(uint32_t)); |
| 591 | write_to_buffer(write_pos_proc,(void*)&size_proc, sizeof(uint32_t)); |
| 592 | //for now don't output processes and interrupt information |
| 593 | // write(fdIntr,(void*)buf_intr,block_size); |
| 594 | // write(fdProc,(void*)buf_proc,block_size); |
| 595 | } |
| 596 | break; |
| 597 | } |
| 598 | |
| 599 | event_count++; |
| 600 | switch(evId){ |
| 601 | case TRACE_SYSCALL_ENTRY: |
| 602 | event_size = sizeof(trace_syscall_entry); |
| 603 | break; |
| 604 | case TRACE_SYSCALL_EXIT: |
| 605 | event_size = 0; |
| 606 | break; |
| 607 | case TRACE_TRAP_ENTRY: |
| 608 | event_size = sizeof(trace_trap_entry); |
| 609 | break; |
| 610 | case TRACE_TRAP_EXIT: |
| 611 | event_size = 0; |
| 612 | break; |
| 613 | case TRACE_IRQ_ENTRY: |
| 614 | event_size = sizeof(trace_irq_entry); |
| 615 | timeDelta = time_delta; |
| 616 | write_to_buffer(write_pos_intr,(void*)&newId, sizeof(uint16_t)); |
| 617 | write_to_buffer(write_pos_intr,(void*)&timeDelta, sizeof(uint32_t)); |
| 618 | write_to_buffer(write_pos_intr,cur_pos, event_size); |
| 619 | break; |
| 620 | case TRACE_IRQ_EXIT: |
| 621 | event_size = 0; |
| 622 | timeDelta = time_delta; |
| 623 | write_to_buffer(write_pos_intr,(void*)&newId, sizeof(uint16_t)); |
| 624 | write_to_buffer(write_pos_intr,(void*)&timeDelta, sizeof(uint32_t)); |
| 625 | break; |
| 626 | case TRACE_SCHEDCHANGE: |
| 627 | event_size = sizeof(trace_schedchange); |
| 628 | break; |
| 629 | case TRACE_KERNEL_TIMER: |
| 630 | event_size = 0; |
| 631 | break; |
| 632 | case TRACE_SOFT_IRQ: |
| 633 | event_size = sizeof(trace_soft_irq); |
| 634 | // timeDelta = time_delta; |
| 635 | // write_to_buffer(write_pos_intr,(void*)&newId, sizeof(uint16_t)); |
| 636 | // write_to_buffer(write_pos_intr,(void*)&timeDelta, sizeof(uint32_t)); |
| 637 | // write_to_buffer(write_pos_intr,cur_pos, event_size); |
| 638 | break; |
| 639 | case TRACE_PROCESS: |
| 640 | event_size = sizeof(trace_process); |
| 641 | timeDelta = time_delta; |
| 642 | subId = *(uint8_t*)cur_pos; |
| 643 | if(subId == TRACE_PROCESS_FORK || subId ==TRACE_PROCESS_EXIT){ |
| 644 | if( subId == TRACE_PROCESS_FORK)tmpId = PROCESS_FORK_ID; |
| 645 | else tmpId = PROCESS_EXIT_ID; |
| 646 | write_to_buffer(write_pos_proc,(void*)&tmpId, sizeof(uint16_t)); |
| 647 | write_to_buffer(write_pos_proc,(void*)&timeDelta, sizeof(uint32_t)); |
| 648 | |
| 649 | process = *(new_process*)(cur_pos + sizeof(uint8_t)); |
| 650 | write_to_buffer(write_pos_proc,(void*)&process, sizeof(new_process)); |
| 651 | } |
| 652 | break; |
| 653 | case TRACE_FILE_SYSTEM: |
| 654 | event_size = sizeof(trace_file_system)- sizeof(char*); |
| 655 | break; |
| 656 | case TRACE_TIMER: |
| 657 | event_size = sizeof(trace_timer); |
| 658 | break; |
| 659 | case TRACE_MEMORY: |
| 660 | event_size = sizeof(trace_memory); |
| 661 | break; |
| 662 | case TRACE_SOCKET: |
| 663 | event_size = sizeof(trace_socket); |
| 664 | break; |
| 665 | case TRACE_IPC: |
| 666 | event_size = sizeof(trace_ipc); |
| 667 | break; |
| 668 | case TRACE_NETWORK: |
| 669 | event_size = sizeof(trace_network); |
| 670 | break; |
| 671 | case TRACE_HEARTBEAT: |
| 672 | beat.seconds = 0; |
| 673 | beat.nanoseconds = 0; |
| 674 | beat.cycle_count = adaptation_tsc; |
| 675 | event_size = 0; |
| 676 | |
| 677 | write_to_buffer(write_pos_intr,(void*)&newId, sizeof(uint16_t)); |
| 678 | write_to_buffer(write_pos_intr,(void*)&time_delta, sizeof(uint32_t)); |
| 679 | write_to_buffer(write_pos_intr,(void*)&beat, sizeof(heartbeat)); |
| 680 | write_to_buffer(write_pos_proc,(void*)&newId, sizeof(uint16_t)); |
| 681 | write_to_buffer(write_pos_proc,(void*)&time_delta, sizeof(uint32_t)); |
| 682 | write_to_buffer(write_pos_proc,(void*)&beat, sizeof(heartbeat)); |
| 683 | break; |
| 684 | default: |
| 685 | event_size = -1; |
| 686 | break; |
| 687 | } |
| 688 | if(evId != TRACE_FILE_SYSTEM && event_size >=0){ |
| 689 | write_to_buffer(write_pos, cur_pos, event_size); |
| 690 | |
| 691 | if(evId == TRACE_HEARTBEAT){ |
| 692 | write_to_buffer(write_pos, (void*)&beat, sizeof(heartbeat)); |
| 693 | } |
| 694 | |
| 695 | cur_pos += event_size + sizeof(uint16_t); //skip data_size |
| 696 | }else if(evId == TRACE_FILE_SYSTEM){ |
| 697 | size_t nbBytes; |
| 698 | char c = '\0'; |
| 699 | tFileSys = (trace_file_system*)cur_pos; |
| 700 | subId = tFileSys->event_sub_id; |
| 701 | if(subId == TRACE_FILE_SYSTEM_OPEN || subId == TRACE_FILE_SYSTEM_EXEC){ |
| 702 | nbBytes = tFileSys->event_data2 +1; |
| 703 | }else nbBytes = 0; |
| 704 | |
| 705 | write_to_buffer(write_pos, cur_pos, event_size); |
| 706 | cur_pos += event_size + sizeof(char*); |
| 707 | if(nbBytes){ |
| 708 | write_to_buffer(write_pos, cur_pos, nbBytes); |
| 709 | }else{ |
| 710 | write_to_buffer(write_pos, (void*)&c, 1); |
| 711 | } |
| 712 | cur_pos += nbBytes + sizeof(uint16_t); //skip data_size |
| 713 | }else if(event_size == -1){ |
| 714 | printf("Unknown event: evId=%d, i=%d, event_count=%d\n", newId, i, event_count); |
| 715 | exit(1); |
| 716 | } |
| 717 | } //end while(1) |
| 718 | } |
| 719 | close(fd); |
| 720 | close(fdCpu); |
| 721 | g_free(buffer); |
| 722 | buffer = NULL; |
| 723 | g_free(buf_fac); |
| 724 | g_free(buf_intr); |
| 725 | g_free(buf_proc); |
| 726 | g_free(buf_out); |
| 727 | } |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | //write to system.xml |
| 734 | fprintf(fp,"<system \n"); |
| 735 | fprintf(fp,"node_name=\"%s\" \n", node_name); |
| 736 | fprintf(fp,"domainname=\"%s\" \n", domainname); |
| 737 | fprintf(fp,"cpu=\"%d\" \n", cpu); |
| 738 | fprintf(fp,"arch_size=\"%s\" \n", arch_size); |
| 739 | fprintf(fp,"endian=\"%s\" \n",endian); |
| 740 | fprintf(fp,"kernel_name=\"%s\" \n",kernel_name); |
| 741 | fprintf(fp,"kernel_release=\"%s\" \n",kernel_release); |
| 742 | fprintf(fp,"kernel_version=\"%s\" \n",kernel_version); |
| 743 | fprintf(fp,"machine=\"%s\" \n",machine); |
| 744 | fprintf(fp,"processor=\"%s\" \n",processor); |
| 745 | fprintf(fp,"hardware_platform=\"%s\" \n",hardware_platform); |
| 746 | fprintf(fp,"operating_system=\"%s\" \n",operating_system); |
| 747 | fprintf(fp,"ltt_major_version=\"%d\" \n",ltt_major_version); |
| 748 | fprintf(fp,"ltt_minor_version=\"%d\" \n",ltt_minor_version); |
| 749 | fprintf(fp,"ltt_block_size=\"%d\" \n",ltt_block_size); |
| 750 | fprintf(fp,">\n"); |
| 751 | fprintf(fp,"This is just a test\n"); |
| 752 | fprintf(fp,"</system>\n"); |
| 753 | fflush(fp); |
| 754 | |
| 755 | close(fdFac); |
| 756 | close(fdIntr); |
| 757 | close(fdProc); |
| 758 | fclose(fp); |
| 759 | |
| 760 | g_printf("Conversion completed. Don't forget to copy core.xml to eventdefs directory\n"); |
| 761 | |
| 762 | return 0; |
| 763 | } |
| 764 | |