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
;
101 uint8_t evId
, 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 time_delta
= *(uint32_t*)cur_pos
;
216 cur_pos
+= sizeof(uint32_t);
217 tStart
= (trace_start
*)cur_pos
;
220 startTimeDelta
= time_delta
;
221 start
.seconds
= tBufStart
->Time
.tv_sec
;
222 start
.nanoseconds
= tBufStart
->Time
.tv_usec
;
223 start
.cycle_count
= tBufStart
->TSC
;
224 start
.block_id
= tBufStart
->ID
;
225 end
.block_id
= start
.block_id
;
227 ltt_major_version
= tStart
->MajorVersion
;
228 ltt_minor_version
= tStart
->MinorVersion
;
229 ltt_block_size
= tStart
->BufferSize
;
230 ltt_log_cpu
= tStart
->LogCPUID
;
232 block_size
= ltt_block_size
;
233 block_number
= file_size
/block_size
;
236 buffer
= g_new(char, block_size
);
237 buf_fac
= g_new(char, block_size
);
238 write_pos_fac
= buf_fac
;
239 buf_intr
= g_new(char, block_size
);
240 write_pos_intr
= buf_intr
;
241 buf_proc
= g_new(char, block_size
);
242 write_pos_proc
= buf_proc
;
244 buf_out
= g_new(char*,cpu
);
245 write_pos
= g_new(void*, cpu
);
246 fdCpu
= g_new(int, cpu
);
247 size_count
= g_new(int, cpu
);
248 has_event
= g_new(gboolean
, cpu
);
250 has_event
[i
] = FALSE
;
251 if(i
==0)has_event
[i
] = TRUE
;
252 buf_out
[i
] = g_new(char, block_size
);
253 write_pos
[i
] = NULL
;;
254 sprintf(cpuStr
,"foo/cpu/%d\0",i
);
255 fdCpu
[i
] = open(cpuStr
, O_CREAT
| O_RDWR
| O_TRUNC
,S_IRUSR
|S_IWUSR
| S_IRGRP
| S_IROTH
); //for cpu i
256 if(fdCpu
[i
] < 0) g_error("Unable to open cpu file %d\n", i
);
258 lseek(fd
,0,SEEK_SET
);
261 for(i
=0;i
<block_number
;i
++){
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");
272 end_pos
= buffer
+ block_size
; //end of the buffer
273 size_lost
= *(uint32_t*)(end_pos
- sizeof(uint32_t));
275 end_pos
= buffer
+ block_size
- size_lost
; //buffer_end event
276 tBufEnd
= (trace_buffer_end
*)end_pos
;
277 end
.seconds
= tBufEnd
->Time
.tv_sec
;
278 end
.nanoseconds
= tBufEnd
->Time
.tv_usec
;
279 end
.cycle_count
= tBufEnd
->TSC
;
281 //skip buffer start and trace start events
282 if(i
==0) //the first block
283 cur_pos
= buffer
+ sizeof(trace_buffer_start
) + sizeof(trace_start
) + 2*(sizeof(uint8_t)+sizeof(uint16_t)+sizeof(uint32_t));
285 cur_pos
= buffer
+ sizeof(trace_buffer_start
) + sizeof(uint8_t)+sizeof(uint16_t)+sizeof(uint32_t);
288 size_count
[j
] = sizeof(buffer_start
) + sizeof(uint16_t) + sizeof(uint32_t);
290 //for cpu 0, always make records
291 write_to_buffer(write_pos
[0],(void*)&startId
, sizeof(uint16_t));
292 write_to_buffer(write_pos
[0],(void*)&startTimeDelta
, sizeof(uint32_t));
293 write_to_buffer(write_pos
[0],(void*)&start
, sizeof(buffer_start
));
295 //write start block event into processes and interrupts files
296 write_to_buffer(write_pos_intr
,(void*)&startId
, sizeof(uint16_t));
297 write_to_buffer(write_pos_intr
,(void*)&startTimeDelta
, sizeof(uint32_t));
298 write_to_buffer(write_pos_intr
,(void*)&start
, sizeof(buffer_start
));
299 write_to_buffer(write_pos_proc
,(void*)&startId
, sizeof(uint16_t));
300 write_to_buffer(write_pos_proc
,(void*)&startTimeDelta
, sizeof(uint32_t));
301 write_to_buffer(write_pos_proc
,(void*)&start
, sizeof(buffer_start
));
309 cpu_id
= *(uint8_t*)cur_pos
;
310 cur_pos
+= sizeof(uint8_t);
311 if(cpu_id
!= 0 && has_event
[cpu_id
] == FALSE
){
312 has_event
[cpu_id
] = TRUE
;
313 write_to_buffer(write_pos
[cpu_id
],(void*)&startId
,sizeof(uint16_t));
314 write_to_buffer(write_pos
[cpu_id
],(void*)&startTimeDelta
, sizeof(uint32_t));
315 write_to_buffer(write_pos
[cpu_id
],(void*)&start
, sizeof(buffer_start
));
318 evId
= *(uint8_t *)cur_pos
;
320 cur_pos
+= sizeof(uint8_t);
321 time_delta
= *(uint32_t*)cur_pos
;
322 cur_pos
+= sizeof(uint32_t);
325 write_to_buffer(write_pos
[cpu_id
],(void*)&newId
,sizeof(uint16_t));
326 write_to_buffer(write_pos
[cpu_id
],(void*)&time_delta
, sizeof(uint32_t));
328 write_to_buffer(write_pos
[0],(void*)&newId
,sizeof(uint16_t));
329 write_to_buffer(write_pos
[0],(void*)&time_delta
, sizeof(uint32_t));
332 if(evId
== TRACE_BUFFER_END
){
336 write_to_buffer(write_pos
[cpu_id
],(void*)&end
,sizeof(buffer_start
));
339 size
= block_size
- size_count
[i
];
340 write_pos
[i
] = buf_out
[i
] + block_size
- sizeof(uint32_t);
341 write_to_buffer(write_pos
[i
],(void*)&size
, sizeof(uint32_t));
342 write(fdCpu
[i
],(void*)buf_out
[i
], block_size
);
346 int size
= block_size
- size_count
[0];
347 write_to_buffer(write_pos
[0],(void*)&end
,sizeof(buffer_start
));
348 write_pos
[0] = buf_out
[0] + block_size
- sizeof(uint32_t);
349 write_to_buffer(write_pos
[0],(void*)&size
, sizeof(uint32_t));
350 write(fdCpu
[0],(void*)buf_out
[0], block_size
);
353 //write out processes and intrrupts files
355 int size_intr
=(int) (write_pos_intr
- (void*)buf_intr
);
356 int size_proc
=(int) (write_pos_proc
- (void*)buf_proc
);
357 write_to_buffer(write_pos_intr
,(void*)&end
,sizeof(buffer_start
));
358 write_to_buffer(write_pos_proc
,(void*)&end
,sizeof(buffer_start
));
359 write_pos_intr
= buf_intr
+ block_size
- sizeof(uint32_t);
360 write_pos_proc
= buf_intr
+ block_size
- sizeof(uint32_t);
361 write_to_buffer(write_pos_intr
,(void*)&size_intr
, sizeof(uint32_t));
362 write_to_buffer(write_pos_proc
,(void*)&size_proc
, sizeof(uint32_t));
363 write(fdIntr
,(void*)buf_intr
,block_size
);
364 write(fdProc
,(void*)buf_proc
,block_size
);
371 case TRACE_SYSCALL_ENTRY
:
372 event_size
= sizeof(trace_syscall_entry
);
374 case TRACE_SYSCALL_EXIT
:
377 case TRACE_TRAP_ENTRY
:
378 event_size
= sizeof(trace_trap_entry
);
380 case TRACE_TRAP_EXIT
:
383 case TRACE_IRQ_ENTRY
:
384 event_size
= sizeof(trace_irq_entry
);
385 timeDelta
= time_delta
;
386 write_to_buffer(write_pos_intr
,(void*)&newId
, sizeof(uint16_t));
387 write_to_buffer(write_pos_intr
,(void*)&timeDelta
, sizeof(uint64_t));
388 write_to_buffer(write_pos_intr
,cur_pos
, event_size
);
392 timeDelta
= time_delta
;
393 write_to_buffer(write_pos_intr
,(void*)&newId
, sizeof(uint16_t));
394 write_to_buffer(write_pos_intr
,(void*)&timeDelta
, sizeof(uint64_t));
396 case TRACE_SCHEDCHANGE
:
397 event_size
= sizeof(trace_schedchange
);
399 case TRACE_KERNEL_TIMER
:
403 event_size
= sizeof(trace_soft_irq
);
404 timeDelta
= time_delta
;
405 write_to_buffer(write_pos_intr
,(void*)&newId
, sizeof(uint16_t));
406 write_to_buffer(write_pos_intr
,(void*)&timeDelta
, sizeof(uint64_t));
407 write_to_buffer(write_pos_intr
,cur_pos
, event_size
);
410 event_size
= sizeof(trace_process
);
411 timeDelta
= time_delta
;
412 subId
= *(uint8_t*)cur_pos
;
413 if(subId
== TRACE_PROCESS_FORK
|| subId
==TRACE_PROCESS_EXIT
){
414 write_to_buffer(write_pos_proc
,(void*)&newId
, sizeof(uint16_t));
415 write_to_buffer(write_pos_proc
,(void*)&timeDelta
, sizeof(uint64_t));
416 write_to_buffer(write_pos_proc
,cur_pos
, event_size
);
419 case TRACE_FILE_SYSTEM
:
420 event_size
= sizeof(trace_file_system
);
423 event_size
= sizeof(trace_timer
);
426 event_size
= sizeof(trace_memory
);
429 event_size
= sizeof(trace_socket
);
432 event_size
= sizeof(trace_ipc
);
435 event_size
= sizeof(trace_network
);
437 case TRACE_HEARTBEAT
:
440 beat
.nanoseconds
= 0;
441 beat
.cycle_count
= start
.cycle_count
+ beat_count
* (0XFFFF+1);
448 if(evId
!= TRACE_FILE_SYSTEM
&& event_size
>=0){
450 size_count
[cpu_id
] += sizeof(uint16_t) + sizeof(uint32_t) + event_size
;
451 if(size_count
[cpu_id
] > block_size
- reserve_size
){
452 printf("size count exceeds the limit of the buffer\n");
455 write_to_buffer(write_pos
[cpu_id
], cur_pos
, event_size
);
457 size_count
[0] += sizeof(uint16_t) + sizeof(uint32_t) + event_size
;
458 if(size_count
[0] > block_size
- reserve_size
){
459 printf("size count exceeds the limit of the buffer\n");
462 write_to_buffer(write_pos
[0], cur_pos
, event_size
);
465 if(evId
== TRACE_HEARTBEAT
){
467 write_to_buffer(write_pos
[cpu_id
], cur_pos
, sizeof(heartbeat
));
469 write_to_buffer(write_pos
[0], cur_pos
, sizeof(heartbeat
));
473 cur_pos
+= event_size
+ sizeof(uint16_t); //skip data_size
474 }else if(evId
== TRACE_FILE_SYSTEM
){
476 tFileSys
= (trace_file_system
*)cur_pos
;
477 subId
= tFileSys
->event_sub_id
;
478 if(subId
== TRACE_FILE_SYSTEM_OPEN
|| subId
== TRACE_FILE_SYSTEM_EXEC
){
479 nbBytes
= tFileSys
->event_data2
+1;
481 nbBytes
+= event_size
;
483 // printf("bytes : %d\n", nbBytes);
486 size_count
[cpu_id
] += nbBytes
+ sizeof(uint16_t) + sizeof(uint32_t);
487 if(size_count
[cpu_id
] > block_size
- reserve_size
){
488 printf("size count exceeds the limit of the buffer\n");
491 write_to_buffer(write_pos
[cpu_id
], cur_pos
, nbBytes
);
493 size_count
[0] += nbBytes
+ sizeof(uint16_t) + sizeof(uint32_t);
494 if(size_count
[0] > block_size
- reserve_size
){
495 printf("size count exceeds the limit of the buffer\n");
498 write_to_buffer(write_pos
[0], cur_pos
, nbBytes
);
500 cur_pos
+= nbBytes
+ sizeof(uint16_t); //skip data_size
501 }else if(event_size
== -1){
502 printf("Unknown event: evId=%d, i=%d, event_count=%d\n", newId
, i
, event_count
);
513 //write to system.xml
514 fprintf(fp
,"<system\n");
515 fprintf(fp
,"node_name=\"%s\"\n", node_name
);
516 fprintf(fp
,"domainname=\"%s\"\n", domainname
);
517 fprintf(fp
,"cpu=%d\n", cpu
);
518 fprintf(fp
,"arch_size=\"%s\"\n", arch_size
);
519 fprintf(fp
,"endian=\"%s\"\n",endian
);
520 fprintf(fp
,"kernel_name=\"%s\"\n",kernel_name
);
521 fprintf(fp
,"kernel_release=\"%s\"\n",kernel_release
);
522 fprintf(fp
,"kernel_version=\"%s\"\n",kernel_version
);
523 fprintf(fp
,"machine=\"%s\"\n",machine
);
524 fprintf(fp
,"processor=\"%s\"\n",processor
);
525 fprintf(fp
,"hardware_platform=\"%s\"\n",hardware_platform
);
526 fprintf(fp
,"operating_system=\"%s\"\n",operating_system
);
527 fprintf(fp
,"ltt_major_version=%d\n",ltt_major_version
);
528 fprintf(fp
,"ltt_minor_version=%d\n",ltt_minor_version
);
529 fprintf(fp
,"ltt_block_size=%d\n",ltt_block_size
);
531 fprintf(fp
,"This is just a test\n");
532 fprintf(fp
,"</system>\n");
541 for(i
=0;i
<cpu
;i
++) close(fdCpu
[i
]);
This page took 0.064642 seconds and 4 git commands to generate.