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
];
91 char foo
[4*BUFFER_SIZE
];
92 char foo_eventdefs
[4*BUFFER_SIZE
];
93 char foo_control
[4*BUFFER_SIZE
];
94 char foo_cpu
[4*BUFFER_SIZE
];
95 char foo_info
[4*BUFFER_SIZE
];
97 char foo_control_facilities
[4*BUFFER_SIZE
];
98 char foo_control_processes
[4*BUFFER_SIZE
];
99 char foo_control_interrupts
[4*BUFFER_SIZE
];
100 char foo_info_system
[4*BUFFER_SIZE
];
102 struct stat lTDFStat
;
104 int block_number
, block_size
;
105 char * buffer
, **buf_out
, cpuStr
[4*BUFFER_SIZE
];
106 char * buf_fac
, * buf_intr
, * buf_proc
;
107 void ** write_pos
, *write_pos_fac
, * write_pos_intr
, *write_pos_proc
;
109 trace_buffer_start
*tBufStart
;
110 trace_buffer_end
*tBufEnd
;
111 trace_file_system
* tFileSys
;
112 uint16_t newId
, startId
;
114 uint32_t time_delta
, startTimeDelta
;
115 void * cur_pos
, *end_pos
;
120 gboolean
* has_event
;
122 int reserve_size
= sizeof(buffer_start
) + sizeof(uint16_t) + 2*sizeof(uint32_t);//lost_size and buffer_end event
124 if(argc
!= 3 && argc
!= 4){
125 printf("need a trace file and cpu number or root directory for the new tracefile\n");
131 strcpy(foo_eventdefs
, "foo/eventdefs");
132 strcpy(foo_control
, "foo/control");
133 strcpy(foo_cpu
, "foo/cpu");
134 strcpy(foo_info
, "foo/info");
136 strcpy(foo
, argv
[3]);
137 strcpy(foo_eventdefs
, argv
[3]);
138 strcat(foo_eventdefs
,"/eventdefs");
139 strcpy(foo_control
, argv
[3]);
140 strcat(foo_control
,"/control");
141 strcpy(foo_cpu
, argv
[3]);
142 strcat(foo_cpu
,"/cpu");
143 strcpy(foo_info
, argv
[3]);
144 strcat(foo_info
,"/info");
146 strcpy(foo_control_facilities
, foo_control
);
147 strcat(foo_control_facilities
,"/facilities");
148 strcpy(foo_control_processes
, foo_control
);
149 strcat(foo_control_processes
, "/processes");
150 strcpy(foo_control_interrupts
, foo_control
);
151 strcat(foo_control_interrupts
, "/interrupts");
152 strcpy(foo_info_system
, foo_info
);
153 strcat(foo_info_system
, "/system.xml");
156 printf("cpu number = %d\n", cpu
);
159 getDataEndianType(arch_size
, endian
);
160 printf("Arch_size: %s, Endian: %s\n", arch_size
, endian
);
162 fp
= fopen("sysInfo.out","r");
164 g_error("Unable to open file sysInfo.out\n");
168 if(!fgets(buf
,BUFFER_SIZE
-1,fp
))
169 g_error("The format of sysInfo.out is not right\n");
170 if(strncmp(buf
,"node_name=",10)==0){
171 strcpy(node_name
,&buf
[10]);
172 node_name
[strlen(node_name
)-1] = '\0';
173 }else if(strncmp(buf
,"domainname=",11)==0){
174 strcpy(domainname
,&buf
[11]);
175 domainname
[strlen(domainname
)-1] = '\0';
176 }else if(strncmp(buf
,"kernel_name=",12)==0){
177 strcpy(kernel_name
,&buf
[12]);
178 kernel_name
[strlen(kernel_name
)-1] = '\0';
179 }else if(strncmp(buf
,"kernel_release=",15)==0){
180 strcpy(kernel_release
,&buf
[15]);
181 kernel_release
[strlen(kernel_release
)-1] = '\0';
182 }else if(strncmp(buf
,"kernel_version=",15)==0){
183 strcpy(kernel_version
,&buf
[15]);
184 kernel_version
[strlen(kernel_version
)-1] = '\0';
185 }else if(strncmp(buf
,"machine=",8)==0){
186 strcpy(machine
,&buf
[8]);
187 machine
[strlen(machine
)-1] = '\0';
188 }else if(strncmp(buf
,"processor=",10)==0){
189 strcpy(processor
,&buf
[10]);
190 processor
[strlen(processor
)-1] = '\0';
191 }else if(strncmp(buf
,"hardware_platform=",18)==0){
192 strcpy(hardware_platform
,&buf
[18]);
193 hardware_platform
[strlen(hardware_platform
)-1] = '\0';
194 }else if(strncmp(buf
,"operating_system=",17)==0){
195 strcpy(operating_system
,&buf
[17]);
196 operating_system
[strlen(operating_system
)-1] = '\0';
201 if(mkdir(foo
, S_IFDIR
| S_IRWXU
| S_IRGRP
| S_IROTH
))
202 g_error("can not make %s directory", foo
);
203 if(mkdir(foo_info
, S_IFDIR
| S_IRWXU
| S_IRGRP
| S_IROTH
))
204 g_error("can not make %s directory", foo_info
);
205 if(mkdir(foo_cpu
, S_IFDIR
| S_IRWXU
| S_IRGRP
| S_IROTH
))
206 g_error("can not make %s directory", foo_cpu
);
207 if(mkdir(foo_control
, S_IFDIR
| S_IRWXU
| S_IRGRP
| S_IROTH
))
208 g_error("can not make %s directory", foo_control
);
209 if(mkdir(foo_eventdefs
, S_IFDIR
| S_IRWXU
| S_IRGRP
| S_IROTH
))
210 g_error("can not make %s directory", foo_eventdefs
);
212 fp
= fopen(foo_info_system
,"w");
214 g_error("Unable to open file system.xml\n");
217 fd
= open(argv
[1], O_RDONLY
, 0);
219 g_error("Unable to open input data file %s\n", argv
[1]);
222 fdFac
= open(foo_control_facilities
,O_CREAT
| O_RDWR
| O_TRUNC
,S_IRUSR
|S_IWUSR
| S_IRGRP
| S_IROTH
);
224 g_error("Unable to open file facilities\n");
226 fdIntr
= open(foo_control_interrupts
,O_CREAT
| O_RDWR
| O_TRUNC
,S_IRUSR
|S_IWUSR
| S_IRGRP
| S_IROTH
);
228 g_error("Unable to open file interrupts\n");
230 fdProc
= open(foo_control_processes
,O_CREAT
| O_RDWR
| O_TRUNC
,S_IRUSR
|S_IWUSR
| S_IRGRP
| S_IROTH
);
232 g_error("Unable to open file process\n");
237 if(fstat(fd
, &lTDFStat
) < 0){
238 g_error("Unable to get the status of the input data file\n");
240 file_size
= lTDFStat
.st_size
;
242 buffer
= g_new(char, 4000);
243 readFile(fd
,(void*)buffer
, 3500, "Unable to read block header");
246 evId
= *(uint8_t *)cur_pos
;
247 cur_pos
+= sizeof(uint8_t);
249 time_delta
= *(uint32_t*)cur_pos
;
250 cur_pos
+= sizeof(uint32_t);
251 tBufStart
= (trace_buffer_start
*)cur_pos
;
252 cur_pos
+= sizeof(trace_buffer_start
);
253 cur_pos
+= sizeof(uint16_t); //Skip event size
255 evId
= *(uint8_t *)cur_pos
;
256 cur_pos
+= sizeof(uint8_t);
257 cur_pos
+= sizeof(uint32_t);
258 tStart
= (trace_start
*)cur_pos
;
261 startTimeDelta
= time_delta
;
262 start
.seconds
= tBufStart
->Time
.tv_sec
;
263 start
.nanoseconds
= tBufStart
->Time
.tv_usec
;
264 start
.cycle_count
= tBufStart
->TSC
;
265 start
.block_id
= tBufStart
->ID
;
266 end
.block_id
= start
.block_id
;
268 ltt_major_version
= tStart
->MajorVersion
;
269 ltt_minor_version
= tStart
->MinorVersion
;
270 ltt_block_size
= tStart
->BufferSize
;
271 ltt_log_cpu
= tStart
->LogCPUID
;
273 block_size
= ltt_block_size
;
274 block_number
= file_size
/block_size
;
277 buffer
= g_new(char, block_size
);
278 buf_fac
= g_new(char, block_size
);
279 write_pos_fac
= buf_fac
;
280 buf_intr
= g_new(char, block_size
);
281 write_pos_intr
= buf_intr
;
282 buf_proc
= g_new(char, block_size
);
283 write_pos_proc
= buf_proc
;
285 buf_out
= g_new(char*,cpu
);
286 write_pos
= g_new(void*, cpu
);
287 fdCpu
= g_new(int, cpu
);
288 has_event
= g_new(gboolean
, cpu
);
290 has_event
[i
] = FALSE
;
291 if(i
==0)has_event
[i
] = TRUE
;
292 buf_out
[i
] = g_new(char, block_size
);
293 write_pos
[i
] = NULL
;;
294 sprintf(cpuStr
,"%s/%d\0",foo_cpu
,i
);
295 fdCpu
[i
] = open(cpuStr
, O_CREAT
| O_RDWR
| O_TRUNC
,S_IRUSR
|S_IWUSR
| S_IRGRP
| S_IROTH
); //for cpu i
296 if(fdCpu
[i
] < 0) g_error("Unable to open cpu file %d\n", i
);
298 lseek(fd
,0,SEEK_SET
);
301 for(i
=0;i
<block_number
;i
++){
305 for(j
=1;j
<cpu
;j
++)has_event
[j
] = FALSE
;
307 memset((void*)buf_out
[j
], 0, block_size
);
308 write_pos
[j
] = buf_out
[j
];
310 memset((void*)buf_intr
, 0, block_size
);
311 memset((void*)buf_fac
, 0, block_size
);
312 memset((void*)buf_proc
, 0, block_size
);
313 write_pos_intr
= buf_intr
;
314 write_pos_fac
= buf_fac
;
315 write_pos_proc
= buf_proc
;
317 memset((void*)buffer
,0,block_size
);
318 readFile(fd
,(void*)buffer
, block_size
, "Unable to read block header");
321 evId
= *(uint8_t *)cur_pos
;
322 cur_pos
+= sizeof(uint8_t);
324 time_delta
= *(uint32_t*)cur_pos
;
325 cur_pos
+= sizeof(uint32_t);
326 tBufStart
= (trace_buffer_start
*)cur_pos
;
327 cur_pos
+= sizeof(trace_buffer_start
);
328 cur_pos
+= sizeof(uint16_t); //Skip event size
331 startTimeDelta
= time_delta
;
332 start
.seconds
= tBufStart
->Time
.tv_sec
;
333 start
.nanoseconds
= tBufStart
->Time
.tv_usec
;
334 start
.cycle_count
= tBufStart
->TSC
;
335 start
.block_id
= tBufStart
->ID
;
336 end
.block_id
= start
.block_id
;
338 end_pos
= buffer
+ block_size
; //end of the buffer
339 size_lost
= *(uint32_t*)(end_pos
- sizeof(uint32_t));
341 end_pos
= buffer
+ block_size
- size_lost
; //buffer_end event
343 tBufEnd
= (trace_buffer_end
*)(end_pos
+ 2 * sizeof(uint8_t)+sizeof(uint32_t));
345 tBufEnd
= (trace_buffer_end
*)(end_pos
+sizeof(uint8_t)+sizeof(uint32_t));
347 end
.seconds
= tBufEnd
->Time
.tv_sec
;
348 end
.nanoseconds
= tBufEnd
->Time
.tv_usec
;
349 end
.cycle_count
= tBufEnd
->TSC
;
351 //skip buffer start and trace start events
352 if(i
==0) //the first block
353 cur_pos
= buffer
+ sizeof(trace_buffer_start
) + sizeof(trace_start
) + 2*(sizeof(uint8_t)+sizeof(uint16_t)+sizeof(uint32_t));
355 cur_pos
= buffer
+ sizeof(trace_buffer_start
) + sizeof(uint8_t)+sizeof(uint16_t)+sizeof(uint32_t);
357 //for cpu 0, always make records
358 write_to_buffer(write_pos
[0],(void*)&startId
, sizeof(uint16_t));
359 write_to_buffer(write_pos
[0],(void*)&startTimeDelta
, sizeof(uint32_t));
360 write_to_buffer(write_pos
[0],(void*)&start
, sizeof(buffer_start
));
362 //write start block event into processes and interrupts files
363 write_to_buffer(write_pos_intr
,(void*)&startId
, sizeof(uint16_t));
364 write_to_buffer(write_pos_intr
,(void*)&startTimeDelta
, sizeof(uint32_t));
365 write_to_buffer(write_pos_intr
,(void*)&start
, sizeof(buffer_start
));
366 write_to_buffer(write_pos_proc
,(void*)&startId
, sizeof(uint16_t));
367 write_to_buffer(write_pos_proc
,(void*)&startTimeDelta
, sizeof(uint32_t));
368 write_to_buffer(write_pos_proc
,(void*)&start
, sizeof(buffer_start
));
376 cpu_id
= *(uint8_t*)cur_pos
;
377 cur_pos
+= sizeof(uint8_t);
378 if(cpu_id
!= 0 && has_event
[cpu_id
] == FALSE
){
379 has_event
[cpu_id
] = TRUE
;
380 write_to_buffer(write_pos
[cpu_id
],(void*)&startId
,sizeof(uint16_t));
381 write_to_buffer(write_pos
[cpu_id
],(void*)&startTimeDelta
, sizeof(uint32_t));
382 write_to_buffer(write_pos
[cpu_id
],(void*)&start
, sizeof(buffer_start
));
385 evId
= *(uint8_t *)cur_pos
;
387 if(evId
== TRACE_HEARTBEAT
) {
390 cur_pos
+= sizeof(uint8_t);
391 time_delta
= *(uint32_t*)cur_pos
;
392 cur_pos
+= sizeof(uint32_t);
395 write_to_buffer(write_pos
[cpu_id
],(void*)&newId
,sizeof(uint16_t));
396 write_to_buffer(write_pos
[cpu_id
],(void*)&time_delta
, sizeof(uint32_t));
398 write_to_buffer(write_pos
[0],(void*)&newId
,sizeof(uint16_t));
399 write_to_buffer(write_pos
[0],(void*)&time_delta
, sizeof(uint32_t));
402 if(evId
== TRACE_BUFFER_END
){
406 write_to_buffer(write_pos
[cpu_id
],(void*)&end
,sizeof(buffer_start
));
409 size
= block_size
+ ((void*)buf_out
[i
] - write_pos
[i
])+ sizeof(uint16_t) + sizeof(uint32_t);
410 write_pos
[i
] = buf_out
[i
] + block_size
- sizeof(uint32_t);
411 write_to_buffer(write_pos
[i
],(void*)&size
, sizeof(uint32_t));
412 write(fdCpu
[i
],(void*)buf_out
[i
], block_size
);
416 int size
= block_size
+ ((void*)buf_out
[0] - write_pos
[0])+ sizeof(uint16_t) + sizeof(uint32_t);
417 write_to_buffer(write_pos
[0],(void*)&end
,sizeof(buffer_start
));
418 write_pos
[0] = buf_out
[0] + block_size
- sizeof(uint32_t);
419 write_to_buffer(write_pos
[0],(void*)&size
, sizeof(uint32_t));
420 write(fdCpu
[0],(void*)buf_out
[0], block_size
);
423 //write out processes and intrrupts files
425 int size_intr
=(int) (write_pos_intr
- (void*)buf_intr
);
426 int size_proc
=(int) (write_pos_proc
- (void*)buf_proc
);
427 write_to_buffer(write_pos_intr
,(void*)&end
,sizeof(buffer_start
));
428 write_to_buffer(write_pos_proc
,(void*)&end
,sizeof(buffer_start
));
429 write_pos_intr
= buf_intr
+ block_size
- sizeof(uint32_t);
430 write_pos_proc
= buf_intr
+ block_size
- sizeof(uint32_t);
431 write_to_buffer(write_pos_intr
,(void*)&size_intr
, sizeof(uint32_t));
432 write_to_buffer(write_pos_proc
,(void*)&size_proc
, sizeof(uint32_t));
433 write(fdIntr
,(void*)buf_intr
,block_size
);
434 write(fdProc
,(void*)buf_proc
,block_size
);
441 case TRACE_SYSCALL_ENTRY
:
442 event_size
= sizeof(trace_syscall_entry
);
444 case TRACE_SYSCALL_EXIT
:
447 case TRACE_TRAP_ENTRY
:
448 event_size
= sizeof(trace_trap_entry
);
450 case TRACE_TRAP_EXIT
:
453 case TRACE_IRQ_ENTRY
:
454 event_size
= sizeof(trace_irq_entry
);
455 timeDelta
= time_delta
;
456 write_to_buffer(write_pos_intr
,(void*)&newId
, sizeof(uint16_t));
457 write_to_buffer(write_pos_intr
,(void*)&timeDelta
, sizeof(uint64_t));
458 write_to_buffer(write_pos_intr
,cur_pos
, event_size
);
462 timeDelta
= time_delta
;
463 write_to_buffer(write_pos_intr
,(void*)&newId
, sizeof(uint16_t));
464 write_to_buffer(write_pos_intr
,(void*)&timeDelta
, sizeof(uint64_t));
466 case TRACE_SCHEDCHANGE
:
467 event_size
= sizeof(trace_schedchange
);
469 case TRACE_KERNEL_TIMER
:
473 event_size
= sizeof(trace_soft_irq
);
474 timeDelta
= time_delta
;
475 write_to_buffer(write_pos_intr
,(void*)&newId
, sizeof(uint16_t));
476 write_to_buffer(write_pos_intr
,(void*)&timeDelta
, sizeof(uint64_t));
477 write_to_buffer(write_pos_intr
,cur_pos
, event_size
);
480 event_size
= sizeof(trace_process
);
481 timeDelta
= time_delta
;
482 subId
= *(uint8_t*)cur_pos
;
483 if(subId
== TRACE_PROCESS_FORK
|| subId
==TRACE_PROCESS_EXIT
){
484 write_to_buffer(write_pos_proc
,(void*)&newId
, sizeof(uint16_t));
485 write_to_buffer(write_pos_proc
,(void*)&timeDelta
, sizeof(uint64_t));
486 write_to_buffer(write_pos_proc
,cur_pos
, event_size
);
489 case TRACE_FILE_SYSTEM
:
490 event_size
= sizeof(trace_file_system
)- sizeof(char*);
493 event_size
= sizeof(trace_timer
);
496 event_size
= sizeof(trace_memory
);
499 event_size
= sizeof(trace_socket
);
502 event_size
= sizeof(trace_ipc
);
505 event_size
= sizeof(trace_network
);
507 case TRACE_HEARTBEAT
:
510 beat
.nanoseconds
= 0;
511 beat
.cycle_count
= start
.cycle_count
+ beat_count
* (0XFFFF+1);
518 if(evId
!= TRACE_FILE_SYSTEM
&& event_size
>=0){
520 write_to_buffer(write_pos
[cpu_id
], cur_pos
, event_size
);
522 write_to_buffer(write_pos
[0], cur_pos
, event_size
);
525 if(evId
== TRACE_HEARTBEAT
){
527 write_to_buffer(write_pos
[cpu_id
],(void*)&beat
, sizeof(heartbeat
));
529 write_to_buffer(write_pos
[0], (void*)&beat
, sizeof(heartbeat
));
533 cur_pos
+= event_size
+ sizeof(uint16_t); //skip data_size
534 }else if(evId
== TRACE_FILE_SYSTEM
){
537 tFileSys
= (trace_file_system
*)cur_pos
;
538 subId
= tFileSys
->event_sub_id
;
539 if(subId
== TRACE_FILE_SYSTEM_OPEN
|| subId
== TRACE_FILE_SYSTEM_EXEC
){
540 nbBytes
= tFileSys
->event_data2
+1;
544 write_to_buffer(write_pos
[cpu_id
], cur_pos
, event_size
);
545 cur_pos
+= event_size
+ sizeof(char*);
547 write_to_buffer(write_pos
[cpu_id
], cur_pos
, nbBytes
);
549 write_to_buffer(write_pos
[cpu_id
], (void*)&c
, 1);
552 write_to_buffer(write_pos
[0], cur_pos
, event_size
);
553 cur_pos
+= event_size
+ sizeof(char*);
555 write_to_buffer(write_pos
[0], cur_pos
, nbBytes
);
557 write_to_buffer(write_pos
[0], (void*)&c
, 1);
560 cur_pos
+= nbBytes
+ sizeof(uint16_t); //skip data_size
561 }else if(event_size
== -1){
562 printf("Unknown event: evId=%d, i=%d, event_count=%d\n", newId
, i
, event_count
);
573 //write to system.xml
574 fprintf(fp
,"<system\n");
575 fprintf(fp
,"node_name=\"%s\"\n", node_name
);
576 fprintf(fp
,"domainname=\"%s\"\n", domainname
);
577 fprintf(fp
,"cpu=%d\n", cpu
);
578 fprintf(fp
,"arch_size=\"%s\"\n", arch_size
);
579 fprintf(fp
,"endian=\"%s\"\n",endian
);
580 fprintf(fp
,"kernel_name=\"%s\"\n",kernel_name
);
581 fprintf(fp
,"kernel_release=\"%s\"\n",kernel_release
);
582 fprintf(fp
,"kernel_version=\"%s\"\n",kernel_version
);
583 fprintf(fp
,"machine=\"%s\"\n",machine
);
584 fprintf(fp
,"processor=\"%s\"\n",processor
);
585 fprintf(fp
,"hardware_platform=\"%s\"\n",hardware_platform
);
586 fprintf(fp
,"operating_system=\"%s\"\n",operating_system
);
587 fprintf(fp
,"ltt_major_version=%d\n",ltt_major_version
);
588 fprintf(fp
,"ltt_minor_version=%d\n",ltt_minor_version
);
589 fprintf(fp
,"ltt_block_size=%d\n",ltt_block_size
);
591 fprintf(fp
,"This is just a test\n");
592 fprintf(fp
,"</system>\n");
601 for(i
=0;i
<cpu
;i
++) close(fdCpu
[i
]);
This page took 0.043039 seconds and 4 git commands to generate.