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