821bc1e4 |
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 write_to_buffer(DEST, SRC, SIZE) \ |
12 | do\ |
13 | {\ |
14 | memcpy(DEST, SRC, SIZE);\ |
15 | DEST += SIZE;\ |
16 | } while(0); |
17 | |
18 | int readFile(int fd, void * buf, size_t size, char * mesg) |
19 | { |
20 | ssize_t nbBytes; |
21 | nbBytes = read(fd, buf, size); |
22 | if(nbBytes != size){ |
23 | printf("%s\n",mesg); |
24 | exit(1); |
25 | } |
26 | return 0; |
27 | } |
28 | |
29 | void getDataEndianType(char * size, char * endian) |
30 | { |
31 | int i = 1; |
32 | char c = (char) i; |
33 | int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *); |
34 | |
35 | if(c == 1) strcpy(endian,"LITTLE_ENDIAN"); |
36 | else strcpy(endian, "BIG_ENDIAN"); |
37 | |
38 | if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4) |
39 | strcpy(size,"LP32"); |
40 | else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4) |
41 | strcpy(size,"ILP32"); |
42 | else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8) |
43 | strcpy(size,"LP64"); |
44 | else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8) |
45 | strcpy(size,"ILP64"); |
46 | else strcpy(size,"UNKNOWN"); |
47 | } |
48 | |
49 | #define BUFFER_SIZE 80 |
50 | |
51 | typedef struct _buffer_start{ |
52 | uint32_t seconds; |
53 | uint32_t nanoseconds; |
54 | uint64_t cycle_count; |
55 | uint32_t block_id; |
56 | } __attribute__ ((packed)) buffer_start; |
57 | |
58 | typedef struct _heartbeat{ |
59 | uint32_t seconds; |
60 | uint32_t nanoseconds; |
61 | uint64_t cycle_count; |
62 | } __attribute__ ((packed)) heartbeat; |
63 | |
64 | |
65 | int main(int argc, char ** argv){ |
66 | |
67 | int fd, *fdCpu; |
68 | FILE * fp; |
69 | int fdFac, fdIntr, fdProc; |
70 | char arch_size[BUFFER_SIZE]; |
71 | char endian[BUFFER_SIZE]; |
72 | char node_name[BUFFER_SIZE]; |
73 | char domainname[BUFFER_SIZE]; |
74 | char kernel_name[BUFFER_SIZE]; |
75 | char kernel_release[BUFFER_SIZE]; |
76 | char kernel_version[BUFFER_SIZE]; |
77 | char machine[BUFFER_SIZE]; |
78 | char processor[BUFFER_SIZE]; |
79 | char hardware_platform[BUFFER_SIZE]; |
80 | char operating_system[BUFFER_SIZE]; |
81 | int cpu; |
82 | int ltt_block_size; |
83 | int ltt_major_version; |
84 | int ltt_minor_version; |
85 | int ltt_log_cpu; |
86 | char buf[BUFFER_SIZE]; |
87 | int i,j; |
88 | |
89 | uint8_t cpu_id; |
90 | struct stat lTDFStat; |
91 | off_t file_size; |
92 | int block_number, block_size; |
93 | char * buffer, **buf_out, cpuStr[BUFFER_SIZE]; |
94 | void ** write_pos; |
95 | trace_start *tStart; |
96 | trace_buffer_start *tBufStart; |
97 | trace_buffer_end *tBufEnd; |
98 | trace_file_system * tFileSys; |
99 | uint16_t newId; |
100 | uint8_t evId, startId; |
101 | uint32_t time_delta, startTimeDelta; |
102 | void * cur_pos, *end_pos; |
103 | buffer_start start; |
104 | buffer_start end; |
105 | heartbeat beat; |
106 | int beat_count = 0; |
107 | int *size_count; |
108 | gboolean * has_event; |
109 | uint32_t size_lost; |
110 | int reserve_size = sizeof(buffer_start) + sizeof(uint16_t) + 2*sizeof(uint32_t);//lost_size and buffer_end event |
111 | |
112 | if(argc != 3){ |
113 | printf("need a trace file and cpu number\n"); |
114 | exit(1); |
115 | } |
116 | |
117 | cpu = atoi(argv[2]); |
118 | printf("cpu number = %d\n", cpu); |
119 | |
120 | |
121 | getDataEndianType(arch_size, endian); |
122 | printf("Arch_size: %s, Endian: %s\n", arch_size, endian); |
123 | |
124 | fp = fopen("sysInfo.out","r"); |
125 | if(!fp){ |
126 | g_error("Unable to open file sysInfo.out\n"); |
127 | } |
128 | |
129 | for(i=0;i<9;i++){ |
130 | if(!fgets(buf,BUFFER_SIZE-1,fp)) |
131 | g_error("The format of sysInfo.out is not right\n"); |
132 | if(strncmp(buf,"node_name=",10)==0){ |
133 | strcpy(node_name,&buf[10]); |
134 | node_name[strlen(node_name)-1] = '\0'; |
135 | }else if(strncmp(buf,"domainname=",11)==0){ |
136 | strcpy(domainname,&buf[11]); |
137 | domainname[strlen(domainname)-1] = '\0'; |
138 | }else if(strncmp(buf,"kernel_name=",12)==0){ |
139 | strcpy(kernel_name,&buf[12]); |
140 | kernel_name[strlen(kernel_name)-1] = '\0'; |
141 | }else if(strncmp(buf,"kernel_release=",15)==0){ |
142 | strcpy(kernel_release,&buf[15]); |
143 | kernel_release[strlen(kernel_release)-1] = '\0'; |
144 | }else if(strncmp(buf,"kernel_version=",15)==0){ |
145 | strcpy(kernel_version,&buf[15]); |
146 | kernel_version[strlen(kernel_version)-1] = '\0'; |
147 | }else if(strncmp(buf,"machine=",8)==0){ |
148 | strcpy(machine,&buf[8]); |
149 | machine[strlen(machine)-1] = '\0'; |
150 | }else if(strncmp(buf,"processor=",10)==0){ |
151 | strcpy(processor,&buf[10]); |
152 | processor[strlen(processor)-1] = '\0'; |
153 | }else if(strncmp(buf,"hardware_platform=",18)==0){ |
154 | strcpy(hardware_platform,&buf[18]); |
155 | hardware_platform[strlen(hardware_platform)-1] = '\0'; |
156 | }else if(strncmp(buf,"operating_system=",17)==0){ |
157 | strcpy(operating_system,&buf[17]); |
158 | operating_system[strlen(operating_system)-1] = '\0'; |
159 | } |
160 | } |
161 | fclose(fp); |
162 | |
163 | if(mkdir("foo", S_IFDIR | S_IRWXU | S_IRGRP | S_IROTH)) g_error("can not make foo directory"); |
164 | if(mkdir("foo/info", S_IFDIR | S_IRWXU | S_IRGRP | S_IROTH)) g_error("can not make foo/info directory"); |
165 | if(mkdir("foo/cpu", S_IFDIR | S_IRWXU | S_IRGRP | S_IROTH)) g_error("can not make foo/cpu directory"); |
166 | if(mkdir("foo/control", S_IFDIR | S_IRWXU | S_IRGRP | S_IROTH)) g_error("can not make foo/control directory"); |
167 | if(mkdir("foo/eventdefs", S_IFDIR | S_IRWXU | S_IRGRP | S_IROTH)) g_error("can not make foo/eventdefs directory"); |
168 | |
169 | fp = fopen("foo/info/system.xml","w"); |
170 | if(!fp){ |
171 | g_error("Unable to open file system.xml\n"); |
172 | } |
173 | |
174 | fd = open(argv[1], O_RDONLY, 0); |
175 | if(fd < 0){ |
176 | g_error("Unable to open input data file %s\n", argv[1]); |
177 | } |
178 | |
179 | fdFac = open("foo/control/facilities",O_CREAT | O_RDWR | O_TRUNC,S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); |
180 | if(fdFac < 0){ |
181 | g_error("Unable to open file facilities\n"); |
182 | } |
183 | fdIntr = open("foo/control/interrupts",O_CREAT | O_RDWR | O_TRUNC,S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); |
184 | if(fdIntr<0){ |
185 | g_error("Unable to open file interrupts\n"); |
186 | } |
187 | fdProc = open("foo/control/processes",O_CREAT | O_RDWR | O_TRUNC,S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); |
188 | if(fdIntr<0){ |
189 | g_error("Unable to open file process\n"); |
190 | } |
191 | |
192 | |
193 | |
194 | if(fstat(fd, &lTDFStat) < 0){ |
195 | g_error("Unable to get the status of the input data file\n"); |
196 | } |
197 | file_size = lTDFStat.st_size; |
198 | |
199 | buffer = g_new(char, 4000); |
200 | readFile(fd,(void*)buffer, 3500, "Unable to read block header"); |
201 | |
202 | cur_pos = buffer; |
203 | evId = *(uint8_t *)cur_pos; |
204 | cur_pos += sizeof(uint8_t); |
205 | newId = evId; |
206 | time_delta = *(uint32_t*)cur_pos; |
207 | cur_pos += sizeof(uint32_t); |
208 | tBufStart = (trace_buffer_start*)cur_pos; |
209 | cur_pos += sizeof(trace_buffer_start); |
210 | cur_pos += sizeof(uint16_t); //Skip event size |
211 | |
212 | evId = *(uint8_t *)cur_pos; |
213 | cur_pos += sizeof(uint8_t); |
214 | time_delta = *(uint32_t*)cur_pos; |
215 | cur_pos += sizeof(uint32_t); |
216 | tStart = (trace_start*)cur_pos; |
217 | |
218 | startId = newId; |
219 | startTimeDelta = time_delta; |
220 | start.seconds = tBufStart->Time.tv_sec; |
221 | start.nanoseconds = tBufStart->Time.tv_usec; |
222 | start.cycle_count = tBufStart->TSC; |
223 | start.block_id = tBufStart->ID; |
224 | end.block_id = start.block_id; |
225 | |
226 | ltt_major_version = tStart->MajorVersion; |
227 | ltt_minor_version = tStart->MinorVersion; |
228 | ltt_block_size = tStart->BufferSize; |
229 | ltt_log_cpu = tStart->LogCPUID; |
230 | |
231 | block_size = ltt_block_size; |
232 | block_number = file_size/block_size; |
233 | |
234 | g_free(buffer); |
235 | buffer = g_new(char, block_size); |
236 | buf_out = g_new(char*,cpu); |
237 | write_pos = g_new(void*, cpu); |
238 | fdCpu = g_new(int, cpu); |
239 | size_count = g_new(int, cpu); |
240 | has_event = g_new(gboolean, cpu); |
241 | for(i=0;i<cpu;i++){ |
242 | has_event[i] = FALSE; |
243 | if(i==0)has_event[i] = TRUE; |
244 | buf_out[i] = g_new(char, block_size); |
245 | write_pos[i] = NULL;; |
246 | sprintf(cpuStr,"foo/cpu/%d\0",i); |
247 | fdCpu[i] = open(cpuStr, O_CREAT | O_RDWR | O_TRUNC,S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); //for cpu i |
248 | if(fdCpu[i] < 0) g_error("Unable to open cpu file %d\n", i); |
249 | } |
250 | lseek(fd,0,SEEK_SET); |
251 | |
252 | |
253 | for(i=0;i<block_number;i++){ |
254 | int event_count = 0; |
255 | beat_count = 0; |
256 | |
257 | for(j=0;j<cpu;j++) write_pos[j] = buf_out[j]; |
258 | |
259 | readFile(fd,(void*)buffer, block_size, "Unable to read block header"); |
260 | |
261 | end_pos = buffer + block_size; //end of the buffer |
262 | size_lost = *(uint32_t*)(end_pos - sizeof(uint32_t)); |
263 | |
264 | end_pos = buffer + block_size - size_lost ; //buffer_end event |
265 | tBufEnd = (trace_buffer_end*)end_pos; |
266 | end.seconds = tBufEnd->Time.tv_sec; |
267 | end.nanoseconds = tBufEnd->Time.tv_usec; |
268 | end.cycle_count = tBufEnd->TSC; |
269 | |
270 | //skip buffer start and trace start events |
271 | if(i==0) //the first block |
272 | cur_pos = buffer + sizeof(trace_buffer_start) + sizeof(trace_start) + 2*(sizeof(uint8_t)+sizeof(uint16_t)+sizeof(uint32_t)); |
273 | else //other blocks |
274 | cur_pos = buffer + sizeof(trace_buffer_start) + sizeof(uint8_t)+sizeof(uint16_t)+sizeof(uint32_t); |
275 | |
276 | for(j=0;j<cpu;j++) |
277 | size_count[j] = sizeof(buffer_start) + sizeof(uint16_t) + sizeof(uint32_t); |
278 | |
279 | //for cpu 0, always make records |
280 | write_to_buffer(write_pos[0],(void*)&startId, sizeof(uint16_t)); |
281 | write_to_buffer(write_pos[0],(void*)&startTimeDelta, sizeof(uint32_t)); |
282 | write_to_buffer(write_pos[0],(void*)&start, sizeof(buffer_start)); |
283 | |
284 | while(1){ |
285 | int event_size; |
286 | uint64_t timeDelta; |
287 | uint8_t subId; |
288 | |
289 | if(ltt_log_cpu){ |
290 | cpu_id = *(uint8_t*)cur_pos; |
291 | cur_pos += sizeof(uint8_t); |
292 | if(cpu_id != 0 && has_event[cpu_id] == FALSE){ |
293 | has_event[cpu_id] = TRUE; |
294 | write_to_buffer(write_pos[cpu_id],(void*)&startId,sizeof(uint16_t)); |
295 | write_to_buffer(write_pos[cpu_id],(void*)&startTimeDelta, sizeof(uint32_t)); |
296 | write_to_buffer(write_pos[cpu_id],(void*)&start, sizeof(buffer_start)); |
297 | } |
298 | } |
299 | evId = *(uint8_t *)cur_pos; |
300 | newId = evId; |
301 | cur_pos += sizeof(uint8_t); |
302 | time_delta = *(uint32_t*)cur_pos; |
303 | cur_pos += sizeof(uint32_t); |
304 | |
305 | if(ltt_log_cpu){ |
306 | write_to_buffer(write_pos[cpu_id],(void*)&newId,sizeof(uint16_t)); |
307 | write_to_buffer(write_pos[cpu_id],(void*)&time_delta, sizeof(uint32_t)); |
308 | }else{ |
309 | write_to_buffer(write_pos[0],(void*)&newId,sizeof(uint16_t)); |
310 | write_to_buffer(write_pos[0],(void*)&time_delta, sizeof(uint32_t)); |
311 | } |
312 | |
313 | if(evId == TRACE_BUFFER_END){ |
314 | if(ltt_log_cpu){ |
315 | int size, i; |
316 | if(has_event[i]) |
317 | write_to_buffer(write_pos[cpu_id],(void*)&end,sizeof(buffer_start)); |
318 | for(i=0;i<cpu;i++){ |
319 | if(has_event[i]){ |
320 | size = block_size - size_count[i]; |
321 | write_pos[i] = buf_out[i] + block_size - sizeof(uint32_t); |
322 | write_to_buffer(write_pos[i],(void*)&size, sizeof(uint32_t)); |
323 | write(fdCpu[i],(void*)buf_out[i], block_size); |
324 | } |
325 | } |
326 | }else { |
327 | int size = block_size - size_count[0]; |
328 | write_to_buffer(write_pos[0],(void*)&end,sizeof(buffer_start)); |
329 | write_pos[0] = buf_out[0] + block_size - sizeof(uint32_t); |
330 | write_to_buffer(write_pos[0],(void*)&size, sizeof(uint32_t)); |
331 | write(fdCpu[0],(void*)buf_out[0], block_size); |
332 | } |
333 | break; |
334 | } |
335 | |
336 | event_count++; |
337 | switch(evId){ |
338 | case TRACE_SYSCALL_ENTRY: |
339 | event_size = sizeof(trace_syscall_entry); |
340 | break; |
341 | case TRACE_SYSCALL_EXIT: |
342 | event_size = 0; |
343 | break; |
344 | case TRACE_TRAP_ENTRY: |
345 | event_size = sizeof(trace_trap_entry); |
346 | break; |
347 | case TRACE_TRAP_EXIT: |
348 | event_size = 0; |
349 | break; |
350 | case TRACE_IRQ_ENTRY: |
351 | event_size = sizeof(trace_irq_entry); |
352 | timeDelta = time_delta; |
353 | write(fdIntr,(void*)&newId, sizeof(uint16_t)); |
354 | write(fdIntr,(void*)&timeDelta, sizeof(uint64_t)); |
355 | write(fdIntr,cur_pos, event_size); |
356 | break; |
357 | case TRACE_IRQ_EXIT: |
358 | event_size = 0; |
359 | timeDelta = time_delta; |
360 | write(fdIntr,(void*)&newId, sizeof(uint16_t)); |
361 | write(fdIntr,(void*)&timeDelta, sizeof(uint64_t)); |
362 | break; |
363 | case TRACE_SCHEDCHANGE: |
364 | event_size = sizeof(trace_schedchange); |
365 | break; |
366 | case TRACE_KERNEL_TIMER: |
367 | event_size = 0; |
368 | break; |
369 | case TRACE_SOFT_IRQ: |
370 | event_size = sizeof(trace_soft_irq); |
371 | timeDelta = time_delta; |
372 | write(fdIntr,(void*)&newId, sizeof(uint16_t)); |
373 | write(fdIntr,(void*)&timeDelta, sizeof(uint64_t)); |
374 | write(fdIntr,cur_pos, event_size); |
375 | break; |
376 | case TRACE_PROCESS: |
377 | event_size = sizeof(trace_process); |
378 | timeDelta = time_delta; |
379 | subId = *(uint8_t*)cur_pos; |
380 | if(subId == TRACE_PROCESS_FORK || subId ==TRACE_PROCESS_EXIT){ |
381 | write(fdProc,(void*)&newId, sizeof(uint16_t)); |
382 | write(fdProc,(void*)&timeDelta, sizeof(uint64_t)); |
383 | write(fdProc,cur_pos, event_size); |
384 | } |
385 | break; |
386 | case TRACE_FILE_SYSTEM: |
387 | event_size = sizeof(trace_file_system); |
388 | break; |
389 | case TRACE_TIMER: |
390 | event_size = sizeof(trace_timer); |
391 | break; |
392 | case TRACE_MEMORY: |
393 | event_size = sizeof(trace_memory); |
394 | break; |
395 | case TRACE_SOCKET: |
396 | event_size = sizeof(trace_socket); |
397 | break; |
398 | case TRACE_IPC: |
399 | event_size = sizeof(trace_ipc); |
400 | break; |
401 | case TRACE_NETWORK: |
402 | event_size = sizeof(trace_network); |
403 | break; |
404 | case TRACE_HEARTBEAT: |
405 | beat_count++; |
406 | beat.seconds = 0; |
407 | beat.nanoseconds = 0; |
408 | beat.cycle_count = start.cycle_count + beat_count * (0XFFFF+1); |
409 | event_size = 0; |
410 | break; |
411 | default: |
412 | event_size = -1; |
413 | break; |
414 | } |
415 | if(evId != TRACE_FILE_SYSTEM && event_size >=0){ |
416 | if(ltt_log_cpu){ |
417 | size_count[cpu_id] += sizeof(uint16_t) + sizeof(uint32_t) + event_size; |
418 | if(size_count[cpu_id] > block_size - reserve_size){ |
419 | printf("size count exceeds the limit of the buffer\n"); |
420 | exit(1); |
421 | } |
422 | write_to_buffer(write_pos[cpu_id], cur_pos, event_size); |
423 | }else{ |
424 | size_count[0] += sizeof(uint16_t) + sizeof(uint32_t) + event_size; |
425 | if(size_count[0] > block_size - reserve_size){ |
426 | printf("size count exceeds the limit of the buffer\n"); |
427 | exit(1); |
428 | } |
429 | write_to_buffer(write_pos[0], cur_pos, event_size); |
430 | } |
431 | |
432 | if(evId == TRACE_HEARTBEAT){ |
433 | if(ltt_log_cpu){ |
434 | write_to_buffer(write_pos[cpu_id], cur_pos, sizeof(heartbeat)); |
435 | }else{ |
436 | write_to_buffer(write_pos[0], cur_pos, sizeof(heartbeat)); |
437 | } |
438 | } |
439 | |
440 | cur_pos += event_size + sizeof(uint16_t); //skip data_size |
441 | }else if(evId == TRACE_FILE_SYSTEM){ |
442 | size_t nbBytes; |
443 | tFileSys = (trace_file_system*)cur_pos; |
444 | subId = tFileSys->event_sub_id; |
445 | if(subId == TRACE_FILE_SYSTEM_OPEN || subId == TRACE_FILE_SYSTEM_EXEC){ |
446 | nbBytes = tFileSys->event_data2 +1; |
447 | }else nbBytes = 0; |
448 | nbBytes += event_size; |
449 | |
450 | // printf("bytes : %d\n", nbBytes); |
451 | |
452 | if(ltt_log_cpu){ |
453 | size_count[cpu_id] += nbBytes + sizeof(uint16_t) + sizeof(uint32_t); |
454 | if(size_count[cpu_id] > block_size - reserve_size){ |
455 | printf("size count exceeds the limit of the buffer\n"); |
456 | exit(1); |
457 | } |
458 | write_to_buffer(write_pos[cpu_id], cur_pos, nbBytes); |
459 | }else{ |
460 | size_count[0] += nbBytes + sizeof(uint16_t) + sizeof(uint32_t); |
461 | if(size_count[0] > block_size - reserve_size){ |
462 | printf("size count exceeds the limit of the buffer\n"); |
463 | exit(1); |
464 | } |
465 | write_to_buffer(write_pos[0], cur_pos, nbBytes); |
466 | } |
467 | cur_pos += nbBytes + sizeof(uint16_t); //skip data_size |
468 | }else if(event_size == -1){ |
469 | printf("Unknown event: evId=%d, i=%d, event_count=%d\n", newId, i, event_count); |
470 | exit(1); |
471 | } |
472 | } //end while(1) |
473 | |
474 | } |
475 | |
476 | |
477 | |
478 | |
479 | |
480 | //write to system.xml |
481 | fprintf(fp,"<system\n"); |
482 | fprintf(fp,"node_name=\"%s\"\n", node_name); |
483 | fprintf(fp,"domainname=\"%s\"\n", domainname); |
484 | fprintf(fp,"cpu=%d\n", cpu); |
485 | fprintf(fp,"arch_size=\"%s\"\n", arch_size); |
486 | fprintf(fp,"endian=\"%s\"\n",endian); |
487 | fprintf(fp,"kernel_name=\"%s\"\n",kernel_name); |
488 | fprintf(fp,"kernel_release=\"%s\"\n",kernel_release); |
489 | fprintf(fp,"kernel_version=\"%s\"\n",kernel_version); |
490 | fprintf(fp,"machine=\"%s\"\n",machine); |
491 | fprintf(fp,"processor=\"%s\"\n",processor); |
492 | fprintf(fp,"hardware_platform=\"%s\"\n",hardware_platform); |
493 | fprintf(fp,"operating_system=\"%s\"\n",operating_system); |
494 | fprintf(fp,"ltt_major_version=%d\n",ltt_major_version); |
495 | fprintf(fp,"ltt_minor_version=%d\n",ltt_minor_version); |
496 | fprintf(fp,"ltt_block_size=%d\n",ltt_block_size); |
497 | fprintf(fp,">\n"); |
498 | fprintf(fp,"This is just a test\n"); |
499 | fprintf(fp,"</system>\n"); |
500 | fflush(fp); |
501 | |
502 | fclose(fp); |
503 | |
504 | close(fdFac); |
505 | close(fdIntr); |
506 | close(fdProc); |
507 | close(fd); |
508 | for(i=0;i<cpu;i++) close(fdCpu[i]); |
509 | |
510 | } |
511 | |