52c9bb1b5a045bf3963a0701e6d0315fec73153b
5 #include <linux/errno.h>
9 #include "LinuxEvents.h"
11 #define write_to_buffer(DEST, SRC, SIZE) \
14 memcpy(DEST, SRC, SIZE);\
18 int readFile(int fd
, void * buf
, size_t size
, char * mesg
)
21 nbBytes
= read(fd
, buf
, size
);
29 void getDataEndianType(char * size
, char * endian
)
33 int sizeInt
=sizeof(int), sizeLong
=sizeof(long), sizePointer
=sizeof(void *);
35 if(c
== 1) strcpy(endian
,"LITTLE_ENDIAN");
36 else strcpy(endian
, "BIG_ENDIAN");
38 if(sizeInt
== 2 && sizeLong
== 4 && sizePointer
== 4)
40 else if(sizeInt
== 4 && sizeLong
== 4 && sizePointer
== 4)
42 else if(sizeInt
== 4 && sizeLong
== 8 && sizePointer
== 8)
44 else if(sizeInt
== 8 && sizeLong
== 8 && sizePointer
== 8)
46 else strcpy(size
,"UNKNOWN");
49 #define BUFFER_SIZE 80
51 typedef struct _buffer_start
{
56 } __attribute__ ((packed
)) buffer_start
;
58 typedef struct _heartbeat
{
62 } __attribute__ ((packed
)) heartbeat
;
65 int main(int argc
, char ** argv
){
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
];
83 int ltt_major_version
;
84 int ltt_minor_version
;
86 char buf
[BUFFER_SIZE
];
92 int block_number
, block_size
;
93 char * buffer
, **buf_out
, cpuStr
[BUFFER_SIZE
];
94 char * buf_fac
, * buf_intr
, * buf_proc
;
95 void ** write_pos
, *write_pos_fac
, * write_pos_intr
, *write_pos_proc
;
97 trace_buffer_start
*tBufStart
;
98 trace_buffer_end
*tBufEnd
;
99 trace_file_system
* tFileSys
;
100 uint16_t newId
, startId
;
102 uint32_t time_delta
, startTimeDelta
;
103 void * cur_pos
, *end_pos
;
109 gboolean
* has_event
;
111 int reserve_size
= sizeof(buffer_start
) + sizeof(uint16_t) + 2*sizeof(uint32_t);//lost_size and buffer_end event
114 printf("need a trace file and cpu number\n");
119 printf("cpu number = %d\n", cpu
);
122 getDataEndianType(arch_size
, endian
);
123 printf("Arch_size: %s, Endian: %s\n", arch_size
, endian
);
125 fp
= fopen("sysInfo.out","r");
127 g_error("Unable to open file sysInfo.out\n");
131 if(!fgets(buf
,BUFFER_SIZE
-1,fp
))
132 g_error("The format of sysInfo.out is not right\n");
133 if(strncmp(buf
,"node_name=",10)==0){
134 strcpy(node_name
,&buf
[10]);
135 node_name
[strlen(node_name
)-1] = '\0';
136 }else if(strncmp(buf
,"domainname=",11)==0){
137 strcpy(domainname
,&buf
[11]);
138 domainname
[strlen(domainname
)-1] = '\0';
139 }else if(strncmp(buf
,"kernel_name=",12)==0){
140 strcpy(kernel_name
,&buf
[12]);
141 kernel_name
[strlen(kernel_name
)-1] = '\0';
142 }else if(strncmp(buf
,"kernel_release=",15)==0){
143 strcpy(kernel_release
,&buf
[15]);
144 kernel_release
[strlen(kernel_release
)-1] = '\0';
145 }else if(strncmp(buf
,"kernel_version=",15)==0){
146 strcpy(kernel_version
,&buf
[15]);
147 kernel_version
[strlen(kernel_version
)-1] = '\0';
148 }else if(strncmp(buf
,"machine=",8)==0){
149 strcpy(machine
,&buf
[8]);
150 machine
[strlen(machine
)-1] = '\0';
151 }else if(strncmp(buf
,"processor=",10)==0){
152 strcpy(processor
,&buf
[10]);
153 processor
[strlen(processor
)-1] = '\0';
154 }else if(strncmp(buf
,"hardware_platform=",18)==0){
155 strcpy(hardware_platform
,&buf
[18]);
156 hardware_platform
[strlen(hardware_platform
)-1] = '\0';
157 }else if(strncmp(buf
,"operating_system=",17)==0){
158 strcpy(operating_system
,&buf
[17]);
159 operating_system
[strlen(operating_system
)-1] = '\0';
164 if(mkdir("foo", S_IFDIR
| S_IRWXU
| S_IRGRP
| S_IROTH
)) g_error("can not make foo directory");
165 if(mkdir("foo/info", S_IFDIR
| S_IRWXU
| S_IRGRP
| S_IROTH
)) g_error("can not make foo/info directory");
166 if(mkdir("foo/cpu", S_IFDIR
| S_IRWXU
| S_IRGRP
| S_IROTH
)) g_error("can not make foo/cpu directory");
167 if(mkdir("foo/control", S_IFDIR
| S_IRWXU
| S_IRGRP
| S_IROTH
)) g_error("can not make foo/control directory");
168 if(mkdir("foo/eventdefs", S_IFDIR
| S_IRWXU
| S_IRGRP
| S_IROTH
)) g_error("can not make foo/eventdefs directory");
170 fp
= fopen("foo/info/system.xml","w");
172 g_error("Unable to open file system.xml\n");
175 fd
= open(argv
[1], O_RDONLY
, 0);
177 g_error("Unable to open input data file %s\n", argv
[1]);
180 fdFac
= open("foo/control/facilities",O_CREAT
| O_RDWR
| O_TRUNC
,S_IRUSR
|S_IWUSR
| S_IRGRP
| S_IROTH
);
182 g_error("Unable to open file facilities\n");
184 fdIntr
= open("foo/control/interrupts",O_CREAT
| O_RDWR
| O_TRUNC
,S_IRUSR
|S_IWUSR
| S_IRGRP
| S_IROTH
);
186 g_error("Unable to open file interrupts\n");
188 fdProc
= open("foo/control/processes",O_CREAT
| O_RDWR
| O_TRUNC
,S_IRUSR
|S_IWUSR
| S_IRGRP
| S_IROTH
);
190 g_error("Unable to open file process\n");
195 if(fstat(fd
, &lTDFStat
) < 0){
196 g_error("Unable to get the status of the input data file\n");
198 file_size
= lTDFStat
.st_size
;
200 buffer
= g_new(char, 4000);
201 readFile(fd
,(void*)buffer
, 3500, "Unable to read block header");
204 evId
= *(uint8_t *)cur_pos
;
205 cur_pos
+= sizeof(uint8_t);
207 time_delta
= *(uint32_t*)cur_pos
;
208 cur_pos
+= sizeof(uint32_t);
209 tBufStart
= (trace_buffer_start
*)cur_pos
;
210 cur_pos
+= sizeof(trace_buffer_start
);
211 cur_pos
+= sizeof(uint16_t); //Skip event size
213 evId
= *(uint8_t *)cur_pos
;
214 cur_pos
+= sizeof(uint8_t);
215 cur_pos
+= sizeof(uint32_t);
216 tStart
= (trace_start
*)cur_pos
;
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
;
226 ltt_major_version
= tStart
->MajorVersion
;
227 ltt_minor_version
= tStart
->MinorVersion
;
228 ltt_block_size
= tStart
->BufferSize
;
229 ltt_log_cpu
= tStart
->LogCPUID
;
231 block_size
= ltt_block_size
;
232 block_number
= file_size
/block_size
;
235 buffer
= g_new(char, block_size
);
236 buf_fac
= g_new(char, block_size
);
237 write_pos_fac
= buf_fac
;
238 buf_intr
= g_new(char, block_size
);
239 write_pos_intr
= buf_intr
;
240 buf_proc
= g_new(char, block_size
);
241 write_pos_proc
= buf_proc
;
243 buf_out
= g_new(char*,cpu
);
244 write_pos
= g_new(void*, cpu
);
245 fdCpu
= g_new(int, cpu
);
246 size_count
= g_new(int, cpu
);
247 has_event
= g_new(gboolean
, cpu
);
249 has_event
[i
] = FALSE
;
250 if(i
==0)has_event
[i
] = TRUE
;
251 buf_out
[i
] = g_new(char, block_size
);
252 write_pos
[i
] = NULL
;;
253 sprintf(cpuStr
,"foo/cpu/%d\0",i
);
254 fdCpu
[i
] = open(cpuStr
, O_CREAT
| O_RDWR
| O_TRUNC
,S_IRUSR
|S_IWUSR
| S_IRGRP
| S_IROTH
); //for cpu i
255 if(fdCpu
[i
] < 0) g_error("Unable to open cpu file %d\n", i
);
257 lseek(fd
,0,SEEK_SET
);
260 for(i
=0;i
<block_number
;i
++){
264 for(j
=1;j
<cpu
;j
++)has_event
[j
] = FALSE
;
265 for(j
=0;j
<cpu
;j
++) write_pos
[j
] = buf_out
[j
];
266 write_pos_intr
= buf_intr
;
267 write_pos_fac
= buf_fac
;
268 write_pos_proc
= buf_proc
;
270 readFile(fd
,(void*)buffer
, block_size
, "Unable to read block header");
273 evId
= *(uint8_t *)cur_pos
;
274 cur_pos
+= sizeof(uint8_t);
276 time_delta
= *(uint32_t*)cur_pos
;
277 cur_pos
+= sizeof(uint32_t);
278 tBufStart
= (trace_buffer_start
*)cur_pos
;
279 cur_pos
+= sizeof(trace_buffer_start
);
280 cur_pos
+= sizeof(uint16_t); //Skip event size
283 startTimeDelta
= time_delta
;
284 start
.seconds
= tBufStart
->Time
.tv_sec
;
285 start
.nanoseconds
= tBufStart
->Time
.tv_usec
;
286 start
.cycle_count
= tBufStart
->TSC
;
287 start
.block_id
= tBufStart
->ID
;
288 end
.block_id
= start
.block_id
;
290 end_pos
= buffer
+ block_size
; //end of the buffer
291 size_lost
= *(uint32_t*)(end_pos
- sizeof(uint32_t));
293 end_pos
= buffer
+ block_size
- size_lost
; //buffer_end event
295 tBufEnd
= (trace_buffer_end
*)(end_pos
+ 2 * sizeof(uint8_t)+sizeof(uint32_t));
297 tBufEnd
= (trace_buffer_end
*)(end_pos
+sizeof(uint8_t)+sizeof(uint32_t));
299 end
.seconds
= tBufEnd
->Time
.tv_sec
;
300 end
.nanoseconds
= tBufEnd
->Time
.tv_usec
;
301 end
.cycle_count
= tBufEnd
->TSC
;
303 //skip buffer start and trace start events
304 if(i
==0) //the first block
305 cur_pos
= buffer
+ sizeof(trace_buffer_start
) + sizeof(trace_start
) + 2*(sizeof(uint8_t)+sizeof(uint16_t)+sizeof(uint32_t));
307 cur_pos
= buffer
+ sizeof(trace_buffer_start
) + sizeof(uint8_t)+sizeof(uint16_t)+sizeof(uint32_t);
310 size_count
[j
] = sizeof(buffer_start
) + sizeof(uint16_t) + sizeof(uint32_t);
312 //for cpu 0, always make records
313 write_to_buffer(write_pos
[0],(void*)&startId
, sizeof(uint16_t));
314 write_to_buffer(write_pos
[0],(void*)&startTimeDelta
, sizeof(uint32_t));
315 write_to_buffer(write_pos
[0],(void*)&start
, sizeof(buffer_start
));
317 //write start block event into processes and interrupts files
318 write_to_buffer(write_pos_intr
,(void*)&startId
, sizeof(uint16_t));
319 write_to_buffer(write_pos_intr
,(void*)&startTimeDelta
, sizeof(uint32_t));
320 write_to_buffer(write_pos_intr
,(void*)&start
, sizeof(buffer_start
));
321 write_to_buffer(write_pos_proc
,(void*)&startId
, sizeof(uint16_t));
322 write_to_buffer(write_pos_proc
,(void*)&startTimeDelta
, sizeof(uint32_t));
323 write_to_buffer(write_pos_proc
,(void*)&start
, sizeof(buffer_start
));
331 cpu_id
= *(uint8_t*)cur_pos
;
332 cur_pos
+= sizeof(uint8_t);
333 if(cpu_id
!= 0 && has_event
[cpu_id
] == FALSE
){
334 has_event
[cpu_id
] = TRUE
;
335 write_to_buffer(write_pos
[cpu_id
],(void*)&startId
,sizeof(uint16_t));
336 write_to_buffer(write_pos
[cpu_id
],(void*)&startTimeDelta
, sizeof(uint32_t));
337 write_to_buffer(write_pos
[cpu_id
],(void*)&start
, sizeof(buffer_start
));
340 evId
= *(uint8_t *)cur_pos
;
342 cur_pos
+= sizeof(uint8_t);
343 time_delta
= *(uint32_t*)cur_pos
;
344 cur_pos
+= sizeof(uint32_t);
347 write_to_buffer(write_pos
[cpu_id
],(void*)&newId
,sizeof(uint16_t));
348 write_to_buffer(write_pos
[cpu_id
],(void*)&time_delta
, sizeof(uint32_t));
350 write_to_buffer(write_pos
[0],(void*)&newId
,sizeof(uint16_t));
351 write_to_buffer(write_pos
[0],(void*)&time_delta
, sizeof(uint32_t));
354 if(evId
== TRACE_BUFFER_END
){
358 write_to_buffer(write_pos
[cpu_id
],(void*)&end
,sizeof(buffer_start
));
361 size
= block_size
- size_count
[i
];
362 write_pos
[i
] = buf_out
[i
] + block_size
- sizeof(uint32_t);
363 write_to_buffer(write_pos
[i
],(void*)&size
, sizeof(uint32_t));
364 write(fdCpu
[i
],(void*)buf_out
[i
], block_size
);
368 int size
= block_size
- size_count
[0];
369 write_to_buffer(write_pos
[0],(void*)&end
,sizeof(buffer_start
));
370 write_pos
[0] = buf_out
[0] + block_size
- sizeof(uint32_t);
371 write_to_buffer(write_pos
[0],(void*)&size
, sizeof(uint32_t));
372 write(fdCpu
[0],(void*)buf_out
[0], block_size
);
375 //write out processes and intrrupts files
377 int size_intr
=(int) (write_pos_intr
- (void*)buf_intr
);
378 int size_proc
=(int) (write_pos_proc
- (void*)buf_proc
);
379 write_to_buffer(write_pos_intr
,(void*)&end
,sizeof(buffer_start
));
380 write_to_buffer(write_pos_proc
,(void*)&end
,sizeof(buffer_start
));
381 write_pos_intr
= buf_intr
+ block_size
- sizeof(uint32_t);
382 write_pos_proc
= buf_intr
+ block_size
- sizeof(uint32_t);
383 write_to_buffer(write_pos_intr
,(void*)&size_intr
, sizeof(uint32_t));
384 write_to_buffer(write_pos_proc
,(void*)&size_proc
, sizeof(uint32_t));
385 write(fdIntr
,(void*)buf_intr
,block_size
);
386 write(fdProc
,(void*)buf_proc
,block_size
);
393 case TRACE_SYSCALL_ENTRY
:
394 event_size
= sizeof(trace_syscall_entry
);
396 case TRACE_SYSCALL_EXIT
:
399 case TRACE_TRAP_ENTRY
:
400 event_size
= sizeof(trace_trap_entry
);
402 case TRACE_TRAP_EXIT
:
405 case TRACE_IRQ_ENTRY
:
406 event_size
= sizeof(trace_irq_entry
);
407 timeDelta
= time_delta
;
408 write_to_buffer(write_pos_intr
,(void*)&newId
, sizeof(uint16_t));
409 write_to_buffer(write_pos_intr
,(void*)&timeDelta
, sizeof(uint64_t));
410 write_to_buffer(write_pos_intr
,cur_pos
, event_size
);
414 timeDelta
= time_delta
;
415 write_to_buffer(write_pos_intr
,(void*)&newId
, sizeof(uint16_t));
416 write_to_buffer(write_pos_intr
,(void*)&timeDelta
, sizeof(uint64_t));
418 case TRACE_SCHEDCHANGE
:
419 event_size
= sizeof(trace_schedchange
);
421 case TRACE_KERNEL_TIMER
:
425 event_size
= sizeof(trace_soft_irq
);
426 timeDelta
= time_delta
;
427 write_to_buffer(write_pos_intr
,(void*)&newId
, sizeof(uint16_t));
428 write_to_buffer(write_pos_intr
,(void*)&timeDelta
, sizeof(uint64_t));
429 write_to_buffer(write_pos_intr
,cur_pos
, event_size
);
432 event_size
= sizeof(trace_process
);
433 timeDelta
= time_delta
;
434 subId
= *(uint8_t*)cur_pos
;
435 if(subId
== TRACE_PROCESS_FORK
|| subId
==TRACE_PROCESS_EXIT
){
436 write_to_buffer(write_pos_proc
,(void*)&newId
, sizeof(uint16_t));
437 write_to_buffer(write_pos_proc
,(void*)&timeDelta
, sizeof(uint64_t));
438 write_to_buffer(write_pos_proc
,cur_pos
, event_size
);
441 case TRACE_FILE_SYSTEM
:
442 event_size
= sizeof(trace_file_system
)- sizeof(char*);
445 event_size
= sizeof(trace_timer
);
448 event_size
= sizeof(trace_memory
);
451 event_size
= sizeof(trace_socket
);
454 event_size
= sizeof(trace_ipc
);
457 event_size
= sizeof(trace_network
);
459 case TRACE_HEARTBEAT
:
462 beat
.nanoseconds
= 0;
463 beat
.cycle_count
= start
.cycle_count
+ beat_count
* (0XFFFF+1);
470 if(evId
!= TRACE_FILE_SYSTEM
&& event_size
>=0){
472 size_count
[cpu_id
] += sizeof(uint16_t) + sizeof(uint32_t) + event_size
;
473 if(size_count
[cpu_id
] > block_size
- reserve_size
){
474 printf("size count exceeds the limit of the buffer\n");
477 write_to_buffer(write_pos
[cpu_id
], cur_pos
, event_size
);
479 size_count
[0] += sizeof(uint16_t) + sizeof(uint32_t) + event_size
;
480 if(size_count
[0] > block_size
- reserve_size
){
481 printf("size count exceeds the limit of the buffer\n");
484 write_to_buffer(write_pos
[0], cur_pos
, event_size
);
487 if(evId
== TRACE_HEARTBEAT
){
489 size_count
[cpu_id
] += sizeof(heartbeat
);
490 write_to_buffer(write_pos
[cpu_id
],(void*)&beat
, sizeof(heartbeat
));
492 size_count
[0] += sizeof(heartbeat
);
493 write_to_buffer(write_pos
[0], (void*)&beat
, sizeof(heartbeat
));
497 cur_pos
+= event_size
+ sizeof(uint16_t); //skip data_size
498 }else if(evId
== TRACE_FILE_SYSTEM
){
501 tFileSys
= (trace_file_system
*)cur_pos
;
502 subId
= tFileSys
->event_sub_id
;
503 if(subId
== TRACE_FILE_SYSTEM_OPEN
|| subId
== TRACE_FILE_SYSTEM_EXEC
){
504 nbBytes
= tFileSys
->event_data2
+1;
508 size_count
[cpu_id
] += nbBytes
+ sizeof(uint16_t) + sizeof(uint32_t)+ event_size
;
509 if(size_count
[cpu_id
] > block_size
- reserve_size
){
510 printf("size count exceeds the limit of the buffer\n");
513 write_to_buffer(write_pos
[cpu_id
], cur_pos
, event_size
);
514 cur_pos
+= event_size
+ sizeof(char*);
516 write_to_buffer(write_pos
[cpu_id
], cur_pos
, nbBytes
);
518 write_to_buffer(write_pos
[cpu_id
], (void*)&c
, 1);
521 size_count
[0] += nbBytes
+ sizeof(uint16_t) + sizeof(uint32_t)+event_size
;
522 if(size_count
[0] > block_size
- reserve_size
){
523 printf("size count exceeds the limit of the buffer\n");
526 write_to_buffer(write_pos
[0], cur_pos
, event_size
);
527 cur_pos
+= event_size
+ sizeof(char*);
529 write_to_buffer(write_pos
[0], cur_pos
, nbBytes
);
531 write_to_buffer(write_pos
[0], (void*)&c
, 1);
534 cur_pos
+= nbBytes
+ sizeof(uint16_t); //skip data_size
535 }else if(event_size
== -1){
536 printf("Unknown event: evId=%d, i=%d, event_count=%d\n", newId
, i
, event_count
);
547 //write to system.xml
548 fprintf(fp
,"<system\n");
549 fprintf(fp
,"node_name=\"%s\"\n", node_name
);
550 fprintf(fp
,"domainname=\"%s\"\n", domainname
);
551 fprintf(fp
,"cpu=%d\n", cpu
);
552 fprintf(fp
,"arch_size=\"%s\"\n", arch_size
);
553 fprintf(fp
,"endian=\"%s\"\n",endian
);
554 fprintf(fp
,"kernel_name=\"%s\"\n",kernel_name
);
555 fprintf(fp
,"kernel_release=\"%s\"\n",kernel_release
);
556 fprintf(fp
,"kernel_version=\"%s\"\n",kernel_version
);
557 fprintf(fp
,"machine=\"%s\"\n",machine
);
558 fprintf(fp
,"processor=\"%s\"\n",processor
);
559 fprintf(fp
,"hardware_platform=\"%s\"\n",hardware_platform
);
560 fprintf(fp
,"operating_system=\"%s\"\n",operating_system
);
561 fprintf(fp
,"ltt_major_version=%d\n",ltt_major_version
);
562 fprintf(fp
,"ltt_minor_version=%d\n",ltt_minor_version
);
563 fprintf(fp
,"ltt_block_size=%d\n",ltt_block_size
);
565 fprintf(fp
,"This is just a test\n");
566 fprintf(fp
,"</system>\n");
575 for(i
=0;i
<cpu
;i
++) close(fdCpu
[i
]);
This page took 0.041139 seconds and 4 git commands to generate.