6cd62ccf |
1 | #include <stdio.h> |
2 | #include <fcntl.h> |
3 | #include <sys/stat.h> |
4 | #include <sys/types.h> |
963b5f2d |
5 | #include <dirent.h> |
6cd62ccf |
6 | #include <linux/errno.h> |
7 | |
fcdf0ec2 |
8 | #include <ltt/LTTTypes.h> |
6cd62ccf |
9 | #include "parser.h" |
963b5f2d |
10 | #include <ltt/trace.h> |
6cd62ccf |
11 | |
963b5f2d |
12 | #define DIR_NAME_SIZE 256 |
6cd62ccf |
13 | |
14 | /* set the offset of the fields belonging to the event, |
15 | need the information of the archecture */ |
40331ba8 |
16 | void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace *t); |
6cd62ccf |
17 | |
18 | /* get the size of the field type according to the archtecture's |
19 | size and endian type(info of the archecture) */ |
40331ba8 |
20 | int getFieldtypeSize(LttTracefile * tf, LttEventType * evT, int offsetRoot, |
21 | int offsetParent, LttField *fld, void *evD, LttTrace* t); |
6cd62ccf |
22 | |
23 | /* read a fixed size or a block information from the file (fd) */ |
24 | int readFile(int fd, void * buf, size_t size, char * mesg); |
963b5f2d |
25 | int readBlock(LttTracefile * tf, int whichBlock); |
6cd62ccf |
26 | |
27 | /* calculate cycles per nsec for current block */ |
963b5f2d |
28 | void getCyclePerNsec(LttTracefile * t); |
6cd62ccf |
29 | |
30 | /* reinitialize the info of the block which is already in the buffer */ |
963b5f2d |
31 | void updateTracefile(LttTracefile * tf); |
6cd62ccf |
32 | |
33 | /* go to the next event */ |
963b5f2d |
34 | int skipEvent(LttTracefile * t); |
6cd62ccf |
35 | |
6cd62ccf |
36 | |
37 | /***************************************************************************** |
38 | *Function name |
963b5f2d |
39 | * ltt_tracefile_open : open a trace file, construct a LttTracefile |
6cd62ccf |
40 | *Input params |
963b5f2d |
41 | * t : the trace containing the tracefile |
42 | * fileName : path name of the trace file |
6cd62ccf |
43 | *Return value |
963b5f2d |
44 | * : a pointer to a tracefile |
6cd62ccf |
45 | ****************************************************************************/ |
46 | |
963b5f2d |
47 | LttTracefile* ltt_tracefile_open(LttTrace * t, char * fileName) |
6cd62ccf |
48 | { |
963b5f2d |
49 | LttTracefile * tf; |
50 | struct stat lTDFStat; /* Trace data file status */ |
51 | BlockStart a_block_start; |
6cd62ccf |
52 | |
963b5f2d |
53 | tf = g_new(LttTracefile, 1); |
6cd62ccf |
54 | |
55 | //open the file |
963b5f2d |
56 | tf->name = g_strdup(fileName); |
57 | tf->trace = t; |
6cd62ccf |
58 | tf->fd = open(fileName, O_RDONLY, 0); |
59 | if(tf->fd < 0){ |
60 | g_error("Unable to open input data file %s\n", fileName); |
61 | } |
62 | |
63 | // Get the file's status |
64 | if(fstat(tf->fd, &lTDFStat) < 0){ |
65 | g_error("Unable to get the status of the input data file %s\n", fileName); |
66 | } |
67 | |
68 | // Is the file large enough to contain a trace |
963b5f2d |
69 | if(lTDFStat.st_size < sizeof(BlockStart) + EVENT_HEADER_SIZE){ |
8710c6c7 |
70 | g_print("The input data file %s does not contain a trace\n", fileName); |
663bd9ff |
71 | g_free(tf->name); |
72 | close(tf->fd); |
73 | g_free(tf); |
8710c6c7 |
74 | return NULL; |
6cd62ccf |
75 | } |
76 | |
77 | //store the size of the file |
78 | tf->file_size = lTDFStat.st_size; |
963b5f2d |
79 | tf->block_size = t->system_description->ltt_block_size; |
80 | tf->block_number = tf->file_size / tf->block_size; |
81 | tf->which_block = 0; |
6cd62ccf |
82 | |
83 | //allocate memory to contain the info of a block |
963b5f2d |
84 | tf->buffer = (void *) g_new(char, t->system_description->ltt_block_size); |
6cd62ccf |
85 | |
963b5f2d |
86 | //read the first block |
87 | if(readBlock(tf,1)) exit(1); |
6cd62ccf |
88 | |
89 | return tf; |
90 | } |
91 | |
6cd62ccf |
92 | |
93 | /***************************************************************************** |
963b5f2d |
94 | *Open control and per cpu tracefiles |
6cd62ccf |
95 | ****************************************************************************/ |
96 | |
963b5f2d |
97 | void ltt_tracefile_open_cpu(LttTrace *t, char * tracefile_name) |
6cd62ccf |
98 | { |
963b5f2d |
99 | LttTracefile * tf; |
100 | tf = ltt_tracefile_open(t,tracefile_name); |
4a6c8e36 |
101 | if(!tf) return; |
963b5f2d |
102 | t->per_cpu_tracefile_number++; |
103 | g_ptr_array_add(t->per_cpu_tracefiles, tf); |
6cd62ccf |
104 | } |
105 | |
963b5f2d |
106 | void ltt_tracefile_open_control(LttTrace *t, char * control_name) |
6cd62ccf |
107 | { |
963b5f2d |
108 | LttTracefile * tf; |
109 | LttEvent * ev; |
110 | LttFacility * f; |
111 | uint16_t evId; |
112 | void * pos; |
113 | FacilityLoad fLoad; |
114 | int i; |
115 | |
116 | tf = ltt_tracefile_open(t,control_name); |
8710c6c7 |
117 | if(!tf) return; |
963b5f2d |
118 | t->control_tracefile_number++; |
119 | g_ptr_array_add(t->control_tracefiles,tf); |
120 | |
121 | //parse facilities tracefile to get base_id |
542ceddd |
122 | if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){ |
963b5f2d |
123 | while(1){ |
40331ba8 |
124 | ev = ltt_tracefile_read(tf); |
125 | if(!ev)return; // end of file |
126 | |
127 | if(ev->event_id == TRACE_FACILITY_LOAD){ |
128 | pos = ev->data; |
963b5f2d |
129 | fLoad.name = (char*)pos; |
47a166fc |
130 | fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name)); |
131 | fLoad.base_code = *(uint32_t*)(pos + strlen(fLoad.name) + sizeof(LttChecksum)); |
963b5f2d |
132 | |
133 | for(i=0;i<t->facility_number;i++){ |
134 | f = (LttFacility*)g_ptr_array_index(t->facilities,i); |
135 | if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){ |
136 | f->base_id = fLoad.base_code; |
137 | break; |
138 | } |
139 | } |
140 | if(i==t->facility_number) |
141 | g_error("Facility: %s, checksum: %d is not founded\n", |
142 | fLoad.name,fLoad.checksum); |
40331ba8 |
143 | }else if(ev->event_id == TRACE_BLOCK_START){ |
144 | continue; |
145 | }else if(ev->event_id == TRACE_BLOCK_END){ |
146 | break; |
963b5f2d |
147 | }else g_error("Not valid facilities trace file\n"); |
148 | } |
149 | } |
6cd62ccf |
150 | } |
151 | |
152 | /***************************************************************************** |
153 | *Function name |
963b5f2d |
154 | * ltt_tracefile_close: close a trace file, |
6cd62ccf |
155 | *Input params |
963b5f2d |
156 | * t : tracefile which will be closed |
6cd62ccf |
157 | ****************************************************************************/ |
158 | |
963b5f2d |
159 | void ltt_tracefile_close(LttTracefile *t) |
6cd62ccf |
160 | { |
963b5f2d |
161 | g_free(t->name); |
162 | g_free(t->buffer); |
663bd9ff |
163 | close(t->fd); |
963b5f2d |
164 | g_free(t); |
165 | } |
6cd62ccf |
166 | |
6cd62ccf |
167 | |
963b5f2d |
168 | /***************************************************************************** |
169 | *Get system information |
170 | ****************************************************************************/ |
171 | void getSystemInfo(LttSystemDescription* des, char * pathname) |
172 | { |
173 | FILE * fp; |
174 | int i; |
175 | int entry_number = 15; |
176 | char buf[DIR_NAME_SIZE]; |
177 | char description[4*DIR_NAME_SIZE]; |
178 | char * ptr; |
179 | |
180 | fp = fopen(pathname,"r"); |
181 | if(!fp){ |
182 | g_error("Can not open file : %s\n", pathname); |
6cd62ccf |
183 | } |
963b5f2d |
184 | |
185 | while(fgets(buf,DIR_NAME_SIZE, fp)!= NULL){ |
186 | ptr = buf; |
187 | while(isspace(*ptr)) ptr++; |
188 | if(strlen(ptr) == 0) continue; |
189 | break; |
6cd62ccf |
190 | } |
191 | |
963b5f2d |
192 | if(strlen(ptr) == 0) g_error("Not a valid file: %s\n", pathname); |
193 | if(strncmp("<system",ptr,7) !=0)g_error("Not a valid file: %s\n", pathname); |
194 | |
195 | for(i=0;i<entry_number;i++){ |
196 | if(fgets(buf,DIR_NAME_SIZE, fp)== NULL) |
197 | g_error("Not a valid file: %s\n", pathname); |
8710c6c7 |
198 | buf[strlen(buf)-1] = '\0'; |
963b5f2d |
199 | ptr = buf; |
200 | while(isspace(*ptr)) ptr++; |
201 | switch(i){ |
202 | case 0: |
203 | if(strncmp("node_name=",ptr,10)!=0) |
204 | g_error("Not a valid file: %s\n", pathname); |
205 | des->node_name = g_strdup(ptr+10); |
206 | break; |
207 | case 1: |
208 | if(strncmp("domainname=",ptr,11)!=0) |
209 | g_error("Not a valid file: %s\n", pathname); |
210 | des->domain_name = g_strdup(ptr+11); |
211 | break; |
212 | case 2: |
213 | if(strncmp("cpu=",ptr,4)!=0) |
214 | g_error("Not a valid file: %s\n", pathname); |
215 | des->nb_cpu = (unsigned)atoi(ptr+4); |
216 | break; |
217 | case 3: |
218 | if(strncmp("arch_size=",ptr,10)!=0) |
219 | g_error("Not a valid file: %s\n", pathname); |
220 | if(strcmp(ptr+10,"\"LP32\"") == 0) des->size = LTT_LP32; |
221 | else if(strcmp(ptr+10,"\"ILP32\"") == 0) des->size = LTT_ILP32; |
222 | else if(strcmp(ptr+10,"\"LP64\"") == 0) des->size = LTT_LP64; |
223 | else if(strcmp(ptr+10,"\"ILP64\"") == 0) des->size = LTT_ILP64; |
224 | else if(strcmp(ptr+10,"\"UNKNOWN\"") == 0) des->size = LTT_UNKNOWN; |
225 | break; |
226 | case 4: |
227 | if(strncmp("endian=",ptr,7)!=0) |
228 | g_error("Not a valid file: %s\n", pathname); |
229 | if(strcmp(ptr+7,"\"LITTLE_ENDIAN\"") == 0) |
230 | des->endian = LTT_LITTLE_ENDIAN; |
231 | else if(strcmp(ptr+7,"\"BIG_ENDIAN\"") == 0) |
232 | des->endian = LTT_BIG_ENDIAN; |
233 | break; |
234 | case 5: |
235 | if(strncmp("kernel_name=",ptr,12)!=0) |
236 | g_error("Not a valid file: %s\n", pathname); |
237 | des->kernel_name = g_strdup(ptr+12); |
238 | break; |
239 | case 6: |
240 | if(strncmp("kernel_release=",ptr,15)!=0) |
241 | g_error("Not a valid file: %s\n", pathname); |
242 | des->kernel_release = g_strdup(ptr+15); |
243 | break; |
244 | case 7: |
245 | if(strncmp("kernel_version=",ptr,15)!=0) |
246 | g_error("Not a valid file: %s\n", pathname); |
247 | des->kernel_version = g_strdup(ptr+15); |
248 | break; |
249 | case 8: |
250 | if(strncmp("machine=",ptr,8)!=0) |
251 | g_error("Not a valid file: %s\n", pathname); |
252 | des->machine = g_strdup(ptr+8); |
253 | break; |
254 | case 9: |
255 | if(strncmp("processor=",ptr,10)!=0) |
256 | g_error("Not a valid file: %s\n", pathname); |
257 | des->processor = g_strdup(ptr+10); |
258 | break; |
259 | case 10: |
260 | if(strncmp("hardware_platform=",ptr,18)!=0) |
261 | g_error("Not a valid file: %s\n", pathname); |
262 | des->hardware_platform = g_strdup(ptr+18); |
263 | break; |
264 | case 11: |
265 | if(strncmp("operating_system=",ptr,17)!=0) |
266 | g_error("Not a valid file: %s\n", pathname); |
267 | des->operating_system = g_strdup(ptr+17); |
268 | break; |
269 | case 12: |
270 | if(strncmp("ltt_major_version=",ptr,18)!=0) |
271 | g_error("Not a valid file: %s\n", pathname); |
272 | ptr += 18; |
8710c6c7 |
273 | // ptr++;//skip begining " |
274 | // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending " |
963b5f2d |
275 | des->ltt_major_version = (unsigned)atoi(ptr); |
276 | break; |
277 | case 13: |
278 | if(strncmp("ltt_minor_version=",ptr,18)!=0) |
279 | g_error("Not a valid file: %s\n", pathname); |
280 | ptr += 18; |
8710c6c7 |
281 | // ptr++;//skip begining " |
282 | // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending " |
963b5f2d |
283 | des->ltt_minor_version = (unsigned)atoi(ptr); |
284 | break; |
285 | case 14: |
286 | if(strncmp("ltt_block_size=",ptr,15)!=0) |
287 | g_error("Not a valid file: %s\n", pathname); |
288 | ptr += 15; |
8710c6c7 |
289 | // ptr++;//skip begining " |
290 | // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending " |
963b5f2d |
291 | des->ltt_block_size = (unsigned)atoi(ptr); |
292 | break; |
293 | default: |
294 | g_error("Not a valid file: %s\n", pathname); |
295 | } |
6cd62ccf |
296 | } |
297 | |
963b5f2d |
298 | //get description |
299 | description[0] = '\0'; |
300 | if(fgets(buf,DIR_NAME_SIZE, fp)== NULL) |
301 | g_error("Not a valid file: %s\n", pathname); |
302 | ptr = buf; |
303 | while(isspace(*ptr)) ptr++; |
304 | if(*ptr != '>') g_error("Not a valid file: %s\n", pathname); |
305 | while((ptr=fgets(buf,DIR_NAME_SIZE, fp))!= NULL){ |
306 | ptr = buf; |
307 | while(isspace(*ptr)) ptr++; |
308 | if(strncmp("</system>",ptr,9) == 0 )break; |
309 | strcat(description, buf); |
310 | } |
311 | if(!ptr)g_error("Not a valid file: %s\n", pathname); |
312 | if(description[0] = '\0')des->description = NULL; |
313 | des->description = g_strdup(description); |
6cd62ccf |
314 | |
963b5f2d |
315 | fclose(fp); |
6cd62ccf |
316 | } |
317 | |
318 | /***************************************************************************** |
963b5f2d |
319 | *The following functions get facility/tracefile information |
6cd62ccf |
320 | ****************************************************************************/ |
321 | |
963b5f2d |
322 | void getFacilityInfo(LttTrace *t, char* eventdefs) |
6cd62ccf |
323 | { |
963b5f2d |
324 | DIR * dir; |
325 | struct dirent *entry; |
326 | char * ptr; |
327 | int i,j; |
328 | LttFacility * f; |
329 | LttEventType * et; |
e8bb1a73 |
330 | char name[DIR_NAME_SIZE]; |
963b5f2d |
331 | |
332 | dir = opendir(eventdefs); |
333 | if(!dir) g_error("Can not open directory: %s\n", eventdefs); |
334 | |
335 | while((entry = readdir(dir)) != NULL){ |
336 | ptr = &entry->d_name[strlen(entry->d_name)-4]; |
337 | if(strcmp(ptr,".xml") != 0) continue; |
e8bb1a73 |
338 | strcpy(name,eventdefs); |
339 | strcat(name,entry->d_name); |
340 | ltt_facility_open(t,name); |
963b5f2d |
341 | } |
342 | closedir(dir); |
343 | |
963b5f2d |
344 | for(j=0;j<t->facility_number;j++){ |
8710c6c7 |
345 | f = (LttFacility*)g_ptr_array_index(t->facilities, j); |
963b5f2d |
346 | for(i=0; i<f->event_number; i++){ |
347 | et = f->events[i]; |
40331ba8 |
348 | setFieldsOffset(NULL, et, NULL, t); |
963b5f2d |
349 | } |
350 | } |
351 | } |
6cd62ccf |
352 | |
963b5f2d |
353 | void getControlFileInfo(LttTrace *t, char* control) |
6cd62ccf |
354 | { |
963b5f2d |
355 | DIR * dir; |
356 | struct dirent *entry; |
e8bb1a73 |
357 | char name[DIR_NAME_SIZE]; |
963b5f2d |
358 | |
359 | dir = opendir(control); |
360 | if(!dir) g_error("Can not open directory: %s\n", control); |
361 | |
362 | while((entry = readdir(dir)) != NULL){ |
8710c6c7 |
363 | if(strcmp(entry->d_name,"facilities") != 0 && |
364 | strcmp(entry->d_name,"interrupts") != 0 && |
963b5f2d |
365 | strcmp(entry->d_name,"processes") != 0) continue; |
366 | |
e8bb1a73 |
367 | strcpy(name,control); |
368 | strcat(name,entry->d_name); |
369 | ltt_tracefile_open_control(t,name); |
963b5f2d |
370 | } |
371 | closedir(dir); |
6cd62ccf |
372 | } |
373 | |
963b5f2d |
374 | void getCpuFileInfo(LttTrace *t, char* cpu) |
6cd62ccf |
375 | { |
963b5f2d |
376 | DIR * dir; |
377 | struct dirent *entry; |
e8bb1a73 |
378 | char name[DIR_NAME_SIZE]; |
963b5f2d |
379 | |
380 | dir = opendir(cpu); |
381 | if(!dir) g_error("Can not open directory: %s\n", cpu); |
382 | |
383 | while((entry = readdir(dir)) != NULL){ |
8710c6c7 |
384 | if(strcmp(entry->d_name,".") != 0 && |
963b5f2d |
385 | strcmp(entry->d_name,"..") != 0 ){ |
e8bb1a73 |
386 | strcpy(name,cpu); |
387 | strcat(name,entry->d_name); |
388 | ltt_tracefile_open_cpu(t,name); |
963b5f2d |
389 | }else continue; |
390 | } |
391 | closedir(dir); |
6cd62ccf |
392 | } |
393 | |
963b5f2d |
394 | /***************************************************************************** |
395 | *A trace is specified as a pathname to the directory containing all the |
396 | *associated data (control tracefiles, per cpu tracefiles, event |
397 | *descriptions...). |
398 | * |
399 | *When a trace is closed, all the associated facilities, types and fields |
400 | *are released as well. |
401 | ****************************************************************************/ |
6cd62ccf |
402 | |
f7afe191 |
403 | LttTrace *ltt_trace_open(const char *pathname) |
6cd62ccf |
404 | { |
963b5f2d |
405 | LttTrace * t; |
406 | LttSystemDescription * sys_description; |
407 | char eventdefs[DIR_NAME_SIZE]; |
408 | char info[DIR_NAME_SIZE]; |
409 | char control[DIR_NAME_SIZE]; |
410 | char cpu[DIR_NAME_SIZE]; |
411 | char tmp[DIR_NAME_SIZE]; |
412 | gboolean has_slash = FALSE; |
413 | |
414 | //establish the pathname to different directories |
415 | if(pathname[strlen(pathname)-1] == '/')has_slash = TRUE; |
416 | strcpy(eventdefs,pathname); |
417 | if(!has_slash)strcat(eventdefs,"/"); |
418 | strcat(eventdefs,"eventdefs/"); |
419 | |
420 | strcpy(info,pathname); |
421 | if(!has_slash)strcat(info,"/"); |
422 | strcat(info,"info/"); |
423 | |
424 | strcpy(control,pathname); |
425 | if(!has_slash)strcat(control,"/"); |
426 | strcat(control,"control/"); |
427 | |
428 | strcpy(cpu,pathname); |
429 | if(!has_slash)strcat(cpu,"/"); |
430 | strcat(cpu,"cpu/"); |
431 | |
432 | //new trace |
433 | t = g_new(LttTrace, 1); |
434 | sys_description = g_new(LttSystemDescription, 1); |
435 | t->pathname = g_strdup(pathname); |
436 | t->facility_number = 0; |
437 | t->control_tracefile_number = 0; |
438 | t->per_cpu_tracefile_number = 0; |
439 | t->system_description = sys_description; |
440 | t->control_tracefiles = g_ptr_array_new(); |
441 | t->per_cpu_tracefiles = g_ptr_array_new(); |
442 | t->facilities = g_ptr_array_new(); |
443 | getDataEndianType(&(t->my_arch_size), &(t->my_arch_endian)); |
444 | |
445 | //get system description |
446 | strcpy(tmp,info); |
447 | strcat(tmp,"system.xml"); |
448 | getSystemInfo(sys_description, tmp); |
449 | |
b333a76b |
450 | //get facilities info |
451 | getFacilityInfo(t,eventdefs); |
452 | |
963b5f2d |
453 | //get control tracefile info |
454 | getControlFileInfo(t,control); |
455 | |
456 | //get cpu tracefile info |
457 | getCpuFileInfo(t,cpu); |
458 | |
963b5f2d |
459 | return t; |
6cd62ccf |
460 | } |
461 | |
f7afe191 |
462 | /****************************************************************************** |
463 | * When we copy a trace, we want all the opening actions to happen again : |
464 | * the trace will be reopened and totally independant from the original. |
465 | * That's why we call ltt_trace_open. |
466 | *****************************************************************************/ |
467 | LttTrace *ltt_trace_copy(LttTrace *self) |
468 | { |
469 | return ltt_trace_open(self->pathname); |
470 | } |
471 | |
963b5f2d |
472 | void ltt_trace_close(LttTrace *t) |
6cd62ccf |
473 | { |
963b5f2d |
474 | int i; |
475 | LttTracefile * tf; |
476 | LttFacility * f; |
477 | |
478 | g_free(t->pathname); |
479 | |
480 | //free system_description |
481 | g_free(t->system_description->description); |
482 | g_free(t->system_description->node_name); |
483 | g_free(t->system_description->domain_name); |
484 | g_free(t->system_description->kernel_name); |
485 | g_free(t->system_description->kernel_release); |
486 | g_free(t->system_description->kernel_version); |
487 | g_free(t->system_description->machine); |
488 | g_free(t->system_description->processor); |
489 | g_free(t->system_description->hardware_platform); |
490 | g_free(t->system_description->operating_system); |
491 | g_free(t->system_description); |
492 | |
493 | //free control_tracefiles |
494 | for(i=0;i<t->control_tracefile_number;i++){ |
495 | tf = (LttTracefile*)g_ptr_array_index(t->control_tracefiles,i); |
496 | ltt_tracefile_close(tf); |
497 | } |
09ad4797 |
498 | g_ptr_array_free(t->control_tracefiles, TRUE); |
963b5f2d |
499 | |
500 | //free per_cpu_tracefiles |
501 | for(i=0;i<t->per_cpu_tracefile_number;i++){ |
502 | tf = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles,i); |
503 | ltt_tracefile_close(tf); |
504 | } |
09ad4797 |
505 | g_ptr_array_free(t->per_cpu_tracefiles, TRUE); |
963b5f2d |
506 | |
507 | //free facilities |
508 | for(i=0;i<t->facility_number;i++){ |
509 | f = (LttFacility*)g_ptr_array_index(t->facilities,i); |
510 | ltt_facility_close(f); |
511 | } |
09ad4797 |
512 | g_ptr_array_free(t->facilities, TRUE); |
963b5f2d |
513 | |
514 | g_free(t); |
09ad4797 |
515 | |
516 | g_blow_chunks(); |
6cd62ccf |
517 | } |
518 | |
963b5f2d |
519 | |
6cd62ccf |
520 | /***************************************************************************** |
963b5f2d |
521 | *Get the system description of the trace |
6cd62ccf |
522 | ****************************************************************************/ |
523 | |
963b5f2d |
524 | LttSystemDescription *ltt_trace_system_description(LttTrace *t) |
6cd62ccf |
525 | { |
963b5f2d |
526 | return t->system_description; |
6cd62ccf |
527 | } |
528 | |
529 | /***************************************************************************** |
963b5f2d |
530 | * The following functions discover the facilities of the trace |
6cd62ccf |
531 | ****************************************************************************/ |
532 | |
963b5f2d |
533 | unsigned ltt_trace_facility_number(LttTrace *t) |
534 | { |
535 | return (unsigned)(t->facility_number); |
536 | } |
537 | |
538 | LttFacility *ltt_trace_facility_get(LttTrace *t, unsigned i) |
6cd62ccf |
539 | { |
963b5f2d |
540 | return (LttFacility*)g_ptr_array_index(t->facilities, i); |
6cd62ccf |
541 | } |
542 | |
543 | /***************************************************************************** |
544 | *Function name |
963b5f2d |
545 | * ltt_trace_facility_find : find facilities in the trace |
6cd62ccf |
546 | *Input params |
963b5f2d |
547 | * t : the trace |
548 | * name : facility name |
549 | *Output params |
550 | * position : position of the facility in the trace |
6cd62ccf |
551 | *Return value |
963b5f2d |
552 | * : the number of facilities |
6cd62ccf |
553 | ****************************************************************************/ |
554 | |
963b5f2d |
555 | unsigned ltt_trace_facility_find(LttTrace *t, char *name, unsigned *position) |
6cd62ccf |
556 | { |
963b5f2d |
557 | int i, count=0; |
558 | LttFacility * f; |
8a3005f3 |
559 | for(i=0;i<t->facility_number;i++){ |
963b5f2d |
560 | f = (LttFacility*)g_ptr_array_index(t->facilities, i); |
561 | if(strcmp(f->name,name)==0){ |
562 | count++; |
563 | if(count==1) *position = i; |
564 | }else{ |
565 | if(count) break; |
566 | } |
567 | } |
568 | return count; |
6cd62ccf |
569 | } |
570 | |
571 | /***************************************************************************** |
963b5f2d |
572 | * Functions to discover all the event types in the trace |
6cd62ccf |
573 | ****************************************************************************/ |
574 | |
963b5f2d |
575 | unsigned ltt_trace_eventtype_number(LttTrace *t) |
6cd62ccf |
576 | { |
963b5f2d |
577 | int i; |
578 | unsigned count = 0; |
579 | LttFacility * f; |
b445142a |
580 | for(i=0;i<t->facility_number;i++){ |
963b5f2d |
581 | f = (LttFacility*)g_ptr_array_index(t->facilities, i); |
582 | count += f->event_number; |
583 | } |
584 | return count; |
6cd62ccf |
585 | } |
586 | |
963b5f2d |
587 | LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id) |
6cd62ccf |
588 | { |
963b5f2d |
589 | LttFacility * facility; |
590 | int i; |
591 | for(i=0;i<trace->facility_number;i++){ |
592 | facility = (LttFacility*) g_ptr_array_index(trace->facilities,i); |
593 | if(id >= facility->base_id && |
594 | id < facility->base_id + facility->event_number) |
595 | break; |
596 | } |
597 | if(i==trace->facility_number) return NULL; |
598 | else return facility; |
6cd62ccf |
599 | } |
600 | |
963b5f2d |
601 | LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId) |
6cd62ccf |
602 | { |
963b5f2d |
603 | LttFacility * f; |
604 | f = ltt_trace_facility_by_id(t,evId); |
605 | if(!f) return NULL; |
606 | return f->events[evId - f->base_id]; |
6cd62ccf |
607 | } |
608 | |
609 | /***************************************************************************** |
963b5f2d |
610 | *There is one "per cpu" tracefile for each CPU, numbered from 0 to |
611 | *the maximum number of CPU in the system. When the number of CPU installed |
612 | *is less than the maximum, some positions are unused. There are also a |
613 | *number of "control" tracefiles (facilities, interrupts...). |
6cd62ccf |
614 | ****************************************************************************/ |
963b5f2d |
615 | unsigned ltt_trace_control_tracefile_number(LttTrace *t) |
6cd62ccf |
616 | { |
963b5f2d |
617 | return t->control_tracefile_number; |
6cd62ccf |
618 | } |
619 | |
963b5f2d |
620 | unsigned ltt_trace_per_cpu_tracefile_number(LttTrace *t) |
6cd62ccf |
621 | { |
963b5f2d |
622 | return t->per_cpu_tracefile_number; |
6cd62ccf |
623 | } |
624 | |
625 | /***************************************************************************** |
963b5f2d |
626 | *It is possible to search for the tracefiles by name or by CPU position. |
627 | *The index within the tracefiles of the same type is returned if found |
628 | *and a negative value otherwise. |
6cd62ccf |
629 | ****************************************************************************/ |
630 | |
963b5f2d |
631 | int ltt_trace_control_tracefile_find(LttTrace *t, char *name) |
6cd62ccf |
632 | { |
963b5f2d |
633 | LttTracefile * tracefile; |
634 | int i; |
635 | for(i=0;i<t->control_tracefile_number;i++){ |
636 | tracefile = (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i); |
637 | if(strcmp(tracefile->name, name)==0)break; |
638 | } |
639 | if(i == t->control_tracefile_number) return -1; |
640 | return i; |
6cd62ccf |
641 | } |
642 | |
963b5f2d |
643 | int ltt_trace_per_cpu_tracefile_find(LttTrace *t, unsigned i) |
6cd62ccf |
644 | { |
963b5f2d |
645 | LttTracefile * tracefile; |
646 | int j, name; |
647 | for(j=0;j<t->per_cpu_tracefile_number;j++){ |
648 | tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, j); |
649 | name = atoi(tracefile->name); |
650 | if(name == (int)i)break; |
651 | } |
652 | if(j == t->per_cpu_tracefile_number) return -1; |
653 | return j; |
6cd62ccf |
654 | } |
655 | |
656 | /***************************************************************************** |
963b5f2d |
657 | *Get a specific tracefile |
6cd62ccf |
658 | ****************************************************************************/ |
659 | |
963b5f2d |
660 | LttTracefile *ltt_trace_control_tracefile_get(LttTrace *t, unsigned i) |
6cd62ccf |
661 | { |
5598cfe3 |
662 | return (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i); |
963b5f2d |
663 | } |
664 | |
665 | LttTracefile *ltt_trace_per_cpu_tracefile_get(LttTrace *t, unsigned i) |
666 | { |
667 | return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i); |
6cd62ccf |
668 | } |
669 | |
487ad181 |
670 | /***************************************************************************** |
671 | * Get the start time and end time of the trace |
672 | ****************************************************************************/ |
673 | |
674 | void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end) |
675 | { |
676 | LttTime startSmall, startTmp, endBig, endTmp; |
677 | int i, j=0; |
678 | LttTracefile * tf; |
679 | |
680 | for(i=0;i<t->control_tracefile_number;i++){ |
681 | tf = g_ptr_array_index(t->control_tracefiles, i); |
682 | readBlock(tf,1); |
683 | startTmp = tf->a_block_start->time; |
684 | readBlock(tf,tf->block_number); |
685 | endTmp = tf->a_block_end->time; |
686 | if(i==0){ |
687 | startSmall = startTmp; |
688 | endBig = endTmp; |
689 | j = 1; |
690 | continue; |
691 | } |
308711e5 |
692 | if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp; |
693 | if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp; |
487ad181 |
694 | } |
695 | |
696 | for(i=0;i<t->per_cpu_tracefile_number;i++){ |
697 | tf = g_ptr_array_index(t->per_cpu_tracefiles, i); |
698 | readBlock(tf,1); |
699 | startTmp = tf->a_block_start->time; |
700 | readBlock(tf,tf->block_number); |
701 | endTmp = tf->a_block_end->time; |
702 | if(j == 0 && i==0){ |
703 | startSmall = startTmp; |
704 | endBig = endTmp; |
705 | continue; |
706 | } |
308711e5 |
707 | if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp; |
708 | if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp; |
487ad181 |
709 | } |
710 | |
711 | *start = startSmall; |
712 | *end = endBig; |
713 | } |
714 | |
715 | |
6cd62ccf |
716 | /***************************************************************************** |
963b5f2d |
717 | *Get the name of a tracefile |
6cd62ccf |
718 | ****************************************************************************/ |
719 | |
963b5f2d |
720 | char *ltt_tracefile_name(LttTracefile *tf) |
6cd62ccf |
721 | { |
963b5f2d |
722 | return tf->name; |
6cd62ccf |
723 | } |
724 | |
80da81ad |
725 | /***************************************************************************** |
726 | * Get the number of blocks in the tracefile |
727 | ****************************************************************************/ |
728 | |
729 | unsigned ltt_tracefile_block_number(LttTracefile *tf) |
730 | { |
731 | return tf->block_number; |
732 | } |
733 | |
6cd62ccf |
734 | /***************************************************************************** |
735 | *Function name |
736 | * ltt_tracefile_seek_time: seek to the first event of the trace with time |
737 | * larger or equal to time |
738 | *Input params |
739 | * t : tracefile |
740 | * time : criteria of the time |
6cd62ccf |
741 | ****************************************************************************/ |
742 | |
963b5f2d |
743 | void ltt_tracefile_seek_time(LttTracefile *t, LttTime time) |
6cd62ccf |
744 | { |
745 | int err; |
963b5f2d |
746 | LttTime lttTime; |
308711e5 |
747 | int headTime = ltt_time_compare(t->a_block_start->time, time); |
748 | int tailTime = ltt_time_compare(t->a_block_end->time, time); |
62e55dd6 |
749 | LttEvent * ev; |
750 | |
6cd62ccf |
751 | if(headTime < 0 && tailTime > 0){ |
308711e5 |
752 | if(ltt_time_compare(t->a_block_end->time, t->current_event_time) !=0) { |
db55eaae |
753 | lttTime = getEventTime(t); |
308711e5 |
754 | err = ltt_time_compare(lttTime, time); |
db55eaae |
755 | if(err > 0){ |
308711e5 |
756 | if(t->which_event==2 || (&t->prev_event_time,&time)<0){ |
1a3b8cbd |
757 | return; |
db55eaae |
758 | }else{ |
759 | updateTracefile(t); |
760 | return ltt_tracefile_seek_time(t, time); |
1a3b8cbd |
761 | } |
db55eaae |
762 | }else if(err < 0){ |
763 | while(1){ |
764 | ev = ltt_tracefile_read(t); |
765 | if(ev == NULL){ |
766 | g_print("End of file\n"); |
767 | return; |
768 | } |
769 | lttTime = getEventTime(t); |
308711e5 |
770 | err = ltt_time_compare(lttTime, time); |
db55eaae |
771 | if(err >= 0)return; |
772 | } |
773 | }else return; |
774 | }else{//we are at the end of the block |
775 | updateTracefile(t); |
776 | return ltt_tracefile_seek_time(t, time); |
777 | } |
e37c1372 |
778 | }else if(headTime >= 0){ |
6cd62ccf |
779 | if(t->which_block == 1){ |
780 | updateTracefile(t); |
781 | }else{ |
308711e5 |
782 | if(ltt_time_compare(t->prev_block_end_time, time) >= 0 ){ |
6cd62ccf |
783 | err=readBlock(t,t->which_block-1); |
963b5f2d |
784 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
6cd62ccf |
785 | return ltt_tracefile_seek_time(t, time) ; |
786 | }else{ |
787 | updateTracefile(t); |
788 | } |
789 | } |
40331ba8 |
790 | }else if(tailTime < 0){ |
6cd62ccf |
791 | if(t->which_block != t->block_number){ |
792 | err=readBlock(t,t->which_block+1); |
963b5f2d |
793 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
794 | }else { |
25975c93 |
795 | g_print("End of file\n"); |
963b5f2d |
796 | return; |
797 | } |
6cd62ccf |
798 | if(tailTime < 0) return ltt_tracefile_seek_time(t, time); |
40331ba8 |
799 | }else if(tailTime == 0){ |
e37c1372 |
800 | t->cur_event_pos = t->last_event_pos; |
62e55dd6 |
801 | t->current_event_time = time; |
802 | t->cur_heart_beat_number = 0; |
803 | t->prev_event_time.tv_sec = 0; |
804 | t->prev_event_time.tv_nsec = 0; |
40331ba8 |
805 | return; |
6cd62ccf |
806 | } |
6cd62ccf |
807 | } |
808 | |
80da81ad |
809 | /***************************************************************************** |
810 | * Seek to the first event with position equal or larger to ep |
811 | ****************************************************************************/ |
812 | |
813 | void ltt_tracefile_seek_position(LttTracefile *t, LttEventPosition *ep) |
814 | { |
815 | //if we are at the right place, just return |
816 | if(t->which_block == ep->block_num && t->which_event == ep->event_num) |
817 | return; |
818 | |
819 | if(t->which_block == ep->block_num) updateTracefile(t); |
820 | else readBlock(t,ep->block_num); |
821 | |
822 | //event offset is availiable |
823 | if(ep->old_position){ |
824 | t->cur_heart_beat_number = ep->heart_beat_number; |
825 | t->cur_event_pos = t->buffer + ep->event_offset; |
826 | return; |
827 | } |
828 | |
829 | //only block number and event index are availiable |
830 | while(t->which_event < ep->event_num) ltt_tracefile_read(t); |
831 | |
832 | return; |
833 | } |
834 | |
6cd62ccf |
835 | /***************************************************************************** |
836 | *Function name |
40331ba8 |
837 | * ltt_tracefile_read : read the current event, set the pointer to the next |
6cd62ccf |
838 | *Input params |
839 | * t : tracefile |
840 | *Return value |
963b5f2d |
841 | * LttEvent * : an event to be processed |
6cd62ccf |
842 | ****************************************************************************/ |
843 | |
963b5f2d |
844 | LttEvent *ltt_tracefile_read(LttTracefile *t) |
6cd62ccf |
845 | { |
7525f9e5 |
846 | LttEvent * lttEvent = &t->an_event; |
963b5f2d |
847 | int err; |
6cd62ccf |
848 | |
bdc36259 |
849 | if(t->cur_event_pos == t->buffer + t->block_size){ |
850 | if(t->which_block == t->block_number){ |
bdc36259 |
851 | return NULL; |
852 | } |
853 | err = readBlock(t, t->which_block + 1); |
854 | if(err)g_error("Can not read tracefile"); |
855 | } |
856 | |
963b5f2d |
857 | lttEvent->event_id = (int)(*(uint16_t *)(t->cur_event_pos)); |
858 | if(lttEvent->event_id == TRACE_TIME_HEARTBEAT) |
859 | t->cur_heart_beat_number++; |
6cd62ccf |
860 | |
40331ba8 |
861 | t->prev_event_time = t->current_event_time; |
62e55dd6 |
862 | // t->current_event_time = getEventTime(t); |
6cd62ccf |
863 | |
963b5f2d |
864 | lttEvent->time_delta = *(uint32_t*)(t->cur_event_pos + EVENT_ID_SIZE); |
865 | lttEvent->event_time = t->current_event_time; |
866 | |
6cd62ccf |
867 | lttEvent->tracefile = t; |
868 | lttEvent->data = t->cur_event_pos + EVENT_HEADER_SIZE; |
908f42fa |
869 | lttEvent->which_block = t->which_block; |
870 | lttEvent->which_event = t->which_event; |
6cd62ccf |
871 | |
40331ba8 |
872 | //update the fields of the current event and go to the next event |
873 | err = skipEvent(t); |
40331ba8 |
874 | if(err == ERANGE) g_error("event id is out of range\n"); |
40331ba8 |
875 | |
e4eced0f |
876 | lttEvent->event_cycle_count = t->cur_cycle_count; |
877 | |
6cd62ccf |
878 | return lttEvent; |
879 | } |
880 | |
881 | /**************************************************************************** |
882 | *Function name |
883 | * readFile : wrap function to read from a file |
884 | *Input Params |
885 | * fd : file descriptor |
886 | * buf : buf to contain the content |
887 | * size : number of bytes to be read |
888 | * mesg : message to be printed if some thing goes wrong |
889 | *return value |
890 | * 0 : success |
891 | * EIO : can not read from the file |
892 | ****************************************************************************/ |
893 | |
894 | int readFile(int fd, void * buf, size_t size, char * mesg) |
895 | { |
896 | ssize_t nbBytes; |
897 | nbBytes = read(fd, buf, size); |
898 | if(nbBytes != size){ |
899 | printf("%s\n",mesg); |
900 | return EIO; |
901 | } |
902 | return 0; |
903 | } |
904 | |
905 | /**************************************************************************** |
906 | *Function name |
907 | * readBlock : read a block from the file |
908 | *Input Params |
909 | * lttdes : ltt trace file |
910 | * whichBlock : the block which will be read |
911 | *return value |
912 | * 0 : success |
913 | * EINVAL : lseek fail |
914 | * EIO : can not read from the file |
915 | ****************************************************************************/ |
916 | |
963b5f2d |
917 | int readBlock(LttTracefile * tf, int whichBlock) |
6cd62ccf |
918 | { |
919 | off_t nbBytes; |
920 | uint32_t lostSize; |
921 | |
922 | if(whichBlock - tf->which_block == 1 && tf->which_block != 0){ |
963b5f2d |
923 | tf->prev_block_end_time = tf->a_block_end->time; |
40331ba8 |
924 | tf->prev_event_time = tf->a_block_end->time; |
6cd62ccf |
925 | }else{ |
926 | tf->prev_block_end_time.tv_sec = 0; |
927 | tf->prev_block_end_time.tv_nsec = 0; |
40331ba8 |
928 | tf->prev_event_time.tv_sec = 0; |
929 | tf->prev_event_time.tv_nsec = 0; |
6cd62ccf |
930 | } |
6cd62ccf |
931 | |
963b5f2d |
932 | nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET); |
6cd62ccf |
933 | if(nbBytes == -1) return EINVAL; |
934 | |
963b5f2d |
935 | if(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block")) |
936 | return EIO; |
6cd62ccf |
937 | |
963b5f2d |
938 | tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE); |
939 | lostSize = *(uint32_t*)(tf->buffer + tf->block_size - sizeof(uint32_t)); |
940 | tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size - |
941 | lostSize + EVENT_HEADER_SIZE); |
e37c1372 |
942 | tf->last_event_pos = tf->buffer + tf->block_size - lostSize; |
6cd62ccf |
943 | |
6cd62ccf |
944 | tf->which_block = whichBlock; |
963b5f2d |
945 | tf->which_event = 1; |
40331ba8 |
946 | tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev |
6cd62ccf |
947 | tf->cur_heart_beat_number = 0; |
963b5f2d |
948 | |
6cd62ccf |
949 | getCyclePerNsec(tf); |
950 | |
62e55dd6 |
951 | tf->current_event_time = getEventTime(tf); |
40331ba8 |
952 | |
6cd62ccf |
953 | return 0; |
954 | } |
955 | |
956 | /***************************************************************************** |
957 | *Function name |
958 | * updateTracefile : reinitialize the info of the block which is already |
959 | * in the buffer |
960 | *Input params |
961 | * tf : tracefile |
962 | ****************************************************************************/ |
963 | |
963b5f2d |
964 | void updateTracefile(LttTracefile * tf) |
6cd62ccf |
965 | { |
963b5f2d |
966 | tf->which_event = 1; |
40331ba8 |
967 | tf->cur_event_pos = tf->buffer; |
62e55dd6 |
968 | tf->current_event_time = getEventTime(tf); |
6cd62ccf |
969 | tf->cur_heart_beat_number = 0; |
970 | |
971 | tf->prev_event_time.tv_sec = 0; |
972 | tf->prev_event_time.tv_nsec = 0; |
973 | } |
974 | |
975 | /***************************************************************************** |
976 | *Function name |
977 | * skipEvent : go to the next event, update the fields of the current event |
978 | *Input params |
979 | * t : tracefile |
980 | *return value |
981 | * 0 : success |
6cd62ccf |
982 | * ERANGE : event id is out of range |
983 | ****************************************************************************/ |
984 | |
963b5f2d |
985 | int skipEvent(LttTracefile * t) |
6cd62ccf |
986 | { |
987 | int evId, err; |
988 | void * evData; |
963b5f2d |
989 | LttEventType * evT; |
990 | LttField * rootFld; |
6cd62ccf |
991 | |
963b5f2d |
992 | evId = (int)(*(uint16_t *)(t->cur_event_pos)); |
6cd62ccf |
993 | evData = t->cur_event_pos + EVENT_HEADER_SIZE; |
6cd62ccf |
994 | |
908f42fa |
995 | evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId); |
47a166fc |
996 | |
908f42fa |
997 | if(evT) rootFld = evT->root_field; |
998 | else return ERANGE; |
6cd62ccf |
999 | |
908f42fa |
1000 | if(rootFld){ |
1001 | //event has string/sequence or the last event is not the same event |
1002 | if((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event) |
1003 | && rootFld->field_fixed == 0){ |
1004 | setFieldsOffset(t, evT, evData, t->trace); |
47a166fc |
1005 | } |
908f42fa |
1006 | t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size; |
1007 | }else t->cur_event_pos += EVENT_HEADER_SIZE; |
1008 | |
1009 | evT->latest_block = t->which_block; |
1010 | evT->latest_event = t->which_event; |
1011 | |
6cd62ccf |
1012 | //the next event is in the next block |
963b5f2d |
1013 | if(evId == TRACE_BLOCK_END){ |
bdc36259 |
1014 | t->cur_event_pos = t->buffer + t->block_size; |
6cd62ccf |
1015 | }else{ |
1016 | t->which_event++; |
62e55dd6 |
1017 | t->current_event_time = getEventTime(t); |
6cd62ccf |
1018 | } |
1019 | |
1020 | return 0; |
1021 | } |
1022 | |
1023 | /***************************************************************************** |
1024 | *Function name |
1025 | * getCyclePerNsec : calculate cycles per nsec for current block |
1026 | *Input Params |
1027 | * t : tracefile |
1028 | ****************************************************************************/ |
1029 | |
963b5f2d |
1030 | void getCyclePerNsec(LttTracefile * t) |
6cd62ccf |
1031 | { |
963b5f2d |
1032 | LttTime lBufTotalTime; /* Total time for this buffer */ |
1033 | LttCycleCount lBufTotalNSec; /* Total time for this buffer in nsecs */ |
1034 | LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */ |
6cd62ccf |
1035 | |
1036 | /* Calculate the total time for this buffer */ |
308711e5 |
1037 | lBufTotalTime = ltt_time_sub(t->a_block_end->time, t->a_block_start->time); |
6cd62ccf |
1038 | |
1039 | /* Calculate the total cycles for this bufffer */ |
e4eced0f |
1040 | lBufTotalCycle = t->a_block_end->cycle_count; |
1041 | lBufTotalCycle -= t->a_block_start->cycle_count; |
6cd62ccf |
1042 | |
1043 | /* Convert the total time to nsecs */ |
e4eced0f |
1044 | lBufTotalNSec = lBufTotalTime.tv_sec; |
308711e5 |
1045 | lBufTotalNSec *= NANOSECONDS_PER_SECOND; |
e4eced0f |
1046 | lBufTotalNSec += lBufTotalTime.tv_nsec; |
6cd62ccf |
1047 | |
1048 | t->cycle_per_nsec = (double)lBufTotalCycle / (double)lBufTotalNSec; |
1049 | } |
1050 | |
1051 | /**************************************************************************** |
1052 | *Function name |
1053 | * getEventTime : obtain the time of an event |
1054 | *Input params |
1055 | * tf : tracefile |
1056 | *Return value |
963b5f2d |
1057 | * LttTime : the time of the event |
6cd62ccf |
1058 | ****************************************************************************/ |
1059 | |
963b5f2d |
1060 | LttTime getEventTime(LttTracefile * tf) |
6cd62ccf |
1061 | { |
963b5f2d |
1062 | LttTime time; |
1063 | LttCycleCount cycle_count; // cycle count for the current event |
1064 | LttCycleCount lEventTotalCycle; // Total cycles from start for event |
1065 | double lEventNSec; // Total usecs from start for event |
1066 | LttTime lTimeOffset; // Time offset in struct LttTime |
40331ba8 |
1067 | uint16_t evId; |
e4eced0f |
1068 | int64_t nanoSec, tmpCycleCount = (((uint64_t)1)<<32); |
1069 | static LttCycleCount preCycleCount = 0; |
1070 | static int count = 0; |
1071 | |
40331ba8 |
1072 | evId = *(uint16_t*)tf->cur_event_pos; |
e4eced0f |
1073 | if(evId == TRACE_BLOCK_START){ |
1074 | count = 0; |
1075 | preCycleCount = 0; |
1076 | tf->cur_cycle_count = tf->a_block_start->cycle_count; |
40331ba8 |
1077 | return tf->a_block_start->time; |
e4eced0f |
1078 | }else if(evId == TRACE_BLOCK_END){ |
1079 | count = 0; |
1080 | preCycleCount = 0; |
1081 | tf->cur_cycle_count = tf->a_block_end->cycle_count; |
40331ba8 |
1082 | return tf->a_block_end->time; |
e4eced0f |
1083 | } |
40331ba8 |
1084 | |
e4eced0f |
1085 | // Calculate total time in cycles from start of buffer for this event |
963b5f2d |
1086 | cycle_count = (LttCycleCount)*(uint32_t*)(tf->cur_event_pos + EVENT_ID_SIZE); |
e4eced0f |
1087 | |
1088 | if(cycle_count < preCycleCount)count++; |
1089 | preCycleCount = cycle_count; |
1090 | cycle_count += tmpCycleCount * count; |
1091 | |
1092 | if(tf->cur_heart_beat_number > count) |
1093 | cycle_count += tmpCycleCount * (tf->cur_heart_beat_number - count); |
1094 | |
1095 | tf->cur_cycle_count = cycle_count; |
1096 | |
1097 | lEventTotalCycle = cycle_count; |
1098 | lEventTotalCycle -= tf->a_block_start->cycle_count; |
6cd62ccf |
1099 | |
963b5f2d |
1100 | // Convert it to nsecs |
6cd62ccf |
1101 | lEventNSec = lEventTotalCycle / tf->cycle_per_nsec; |
e4eced0f |
1102 | nanoSec = lEventNSec; |
1103 | |
963b5f2d |
1104 | // Determine offset in struct LttTime |
308711e5 |
1105 | lTimeOffset.tv_nsec = nanoSec % NANOSECONDS_PER_SECOND; |
1106 | lTimeOffset.tv_sec = nanoSec / NANOSECONDS_PER_SECOND; |
6cd62ccf |
1107 | |
308711e5 |
1108 | time = ltt_time_add(tf->a_block_start->time, lTimeOffset); |
e4eced0f |
1109 | |
6cd62ccf |
1110 | return time; |
1111 | } |
1112 | |
1113 | /***************************************************************************** |
1114 | *Function name |
1115 | * setFieldsOffset : set offset of the fields |
1116 | *Input params |
1117 | * tracefile : opened trace file |
1118 | * evT : the event type |
1119 | * evD : event data, it may be NULL |
1120 | ****************************************************************************/ |
1121 | |
40331ba8 |
1122 | void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t) |
6cd62ccf |
1123 | { |
963b5f2d |
1124 | LttField * rootFld = evT->root_field; |
6cd62ccf |
1125 | // rootFld->base_address = evD; |
1126 | |
8710c6c7 |
1127 | if(rootFld) |
1128 | rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t); |
6cd62ccf |
1129 | } |
1130 | |
1131 | /***************************************************************************** |
1132 | *Function name |
1133 | * getFieldtypeSize: get the size of the field type (primitive type) |
1134 | *Input params |
1135 | * tracefile : opened trace file |
1136 | * evT : event type |
1137 | * offsetRoot : offset from the root |
1138 | * offsetParent : offset from the parrent |
1139 | * fld : field |
1140 | * evD : event data, it may be NULL |
1141 | *Return value |
1142 | * int : size of the field |
1143 | ****************************************************************************/ |
1144 | |
963b5f2d |
1145 | int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot, |
40331ba8 |
1146 | int offsetParent, LttField * fld, void *evD, LttTrace *trace) |
6cd62ccf |
1147 | { |
1148 | int size, size1, element_number, i, offset1, offset2; |
963b5f2d |
1149 | LttType * type = fld->field_type; |
6cd62ccf |
1150 | |
8710c6c7 |
1151 | if(t){ |
963b5f2d |
1152 | if(evT->latest_block==t->which_block && evT->latest_event==t->which_event){ |
1153 | return fld->field_size; |
1154 | } |
1155 | } |
6cd62ccf |
1156 | |
1157 | if(fld->field_fixed == 1){ |
1158 | if(fld == evT->root_field) return fld->field_size; |
1159 | } |
1160 | |
1161 | if(type->type_class != LTT_STRUCT && type->type_class != LTT_ARRAY && |
1162 | type->type_class != LTT_SEQUENCE && type->type_class != LTT_STRING){ |
1163 | if(fld->field_fixed == -1){ |
40331ba8 |
1164 | size = (int) ltt_type_size(trace, type); |
6cd62ccf |
1165 | fld->field_fixed = 1; |
1166 | }else size = fld->field_size; |
1167 | |
1168 | }else if(type->type_class == LTT_ARRAY){ |
1169 | element_number = (int) type->element_number; |
1170 | if(fld->field_fixed == -1){ |
40331ba8 |
1171 | size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace); |
6cd62ccf |
1172 | if(size == 0){ //has string or sequence |
1173 | fld->field_fixed = 0; |
1174 | }else{ |
1175 | fld->field_fixed = 1; |
1176 | size *= element_number; |
1177 | } |
1178 | }else if(fld->field_fixed == 0){// has string or sequence |
1179 | size = 0; |
1180 | for(i=0;i<element_number;i++){ |
1181 | size += getFieldtypeSize(t, evT, offsetRoot+size,size, |
40331ba8 |
1182 | fld->child[0], evD+size, trace); |
6cd62ccf |
1183 | } |
1184 | }else size = fld->field_size; |
1185 | |
1186 | }else if(type->type_class == LTT_SEQUENCE){ |
40331ba8 |
1187 | size1 = (int) ltt_type_size(trace, type); |
6cd62ccf |
1188 | if(fld->field_fixed == -1){ |
908f42fa |
1189 | fld->sequ_number_size = size1; |
6cd62ccf |
1190 | fld->field_fixed = 0; |
40331ba8 |
1191 | size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace); |
6cd62ccf |
1192 | fld->element_size = size; |
1193 | }else{//0: sequence |
1194 | element_number = getIntNumber(size1,evD); |
1195 | type->element_number = element_number; |
1196 | if(fld->element_size > 0){ |
1197 | size = element_number * fld->element_size; |
1198 | }else{//sequence has string or sequence |
1199 | size = 0; |
1200 | for(i=0;i<element_number;i++){ |
1201 | size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1, |
40331ba8 |
1202 | fld->child[0], evD+size+size1, trace); |
6cd62ccf |
1203 | } |
1204 | } |
1205 | size += size1; |
1206 | } |
1207 | |
1208 | }else if(type->type_class == LTT_STRING){ |
1209 | size = 0; |
1210 | if(fld->field_fixed == -1){ |
1211 | fld->field_fixed = 0; |
1212 | }else{//0: string |
47a166fc |
1213 | size = strlen((char*)evD) + 1; //include end : '\0' |
6cd62ccf |
1214 | } |
1215 | |
1216 | }else if(type->type_class == LTT_STRUCT){ |
1217 | element_number = (int) type->element_number; |
1218 | size = 0; |
1219 | if(fld->field_fixed == -1){ |
1220 | offset1 = offsetRoot; |
1221 | offset2 = 0; |
1222 | for(i=0;i<element_number;i++){ |
40331ba8 |
1223 | size1=getFieldtypeSize(t, evT,offset1,offset2, fld->child[i], NULL, trace); |
6cd62ccf |
1224 | if(size1 > 0 && size >= 0){ |
1225 | size += size1; |
1226 | if(offset1 >= 0) offset1 += size1; |
1227 | offset2 += size1; |
1228 | }else{ |
1229 | size = -1; |
1230 | offset1 = -1; |
1231 | offset2 = -1; |
1232 | } |
1233 | } |
1234 | if(size == -1){ |
1235 | fld->field_fixed = 0; |
1236 | size = 0; |
1237 | }else fld->field_fixed = 1; |
1238 | }else if(fld->field_fixed == 0){ |
1239 | offset1 = offsetRoot; |
1240 | offset2 = 0; |
1241 | for(i=0;i<element_number;i++){ |
40331ba8 |
1242 | size=getFieldtypeSize(t,evT,offset1,offset2,fld->child[i],evD+offset2, trace); |
6cd62ccf |
1243 | offset1 += size; |
1244 | offset2 += size; |
1245 | } |
1246 | size = offset2; |
1247 | }else size = fld->field_size; |
1248 | } |
1249 | |
1250 | fld->offset_root = offsetRoot; |
1251 | fld->offset_parent = offsetParent; |
1252 | if(!evD){ |
1253 | fld->fixed_root = (offsetRoot==-1) ? 0 : 1; |
1254 | fld->fixed_parent = (offsetParent==-1) ? 0 : 1; |
1255 | } |
1256 | fld->field_size = size; |
1257 | |
1258 | return size; |
1259 | } |
1260 | |
6cd62ccf |
1261 | |
1262 | /***************************************************************************** |
1263 | *Function name |
1264 | * getIntNumber : get an integer number |
1265 | *Input params |
1266 | * size : the size of the integer |
1267 | * evD : the event data |
1268 | *Return value |
1269 | * int : an integer |
1270 | ****************************************************************************/ |
1271 | |
1272 | int getIntNumber(int size, void *evD) |
1273 | { |
1274 | int64_t i; |
1275 | if(size == 1) i = *(int8_t *)evD; |
1276 | else if(size == 2) i = *(int16_t *)evD; |
1277 | else if(size == 4) i = *(int32_t *)evD; |
1278 | else if(size == 8) i = *(int64_t *)evD; |
1279 | |
1280 | return (int) i; |
1281 | } |
1282 | |
1283 | /***************************************************************************** |
1284 | *Function name |
1285 | * getDataEndianType : get the data type size and endian type of the local |
1286 | * machine |
1287 | *Input params |
1288 | * size : size of data type |
1289 | * endian : endian type, little or big |
1290 | ****************************************************************************/ |
1291 | |
963b5f2d |
1292 | void getDataEndianType(LttArchSize * size, LttArchEndian * endian) |
6cd62ccf |
1293 | { |
1294 | int i = 1; |
1295 | char c = (char) i; |
1296 | int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *); |
1297 | |
1298 | if(c == 1) *endian = LTT_LITTLE_ENDIAN; |
1299 | else *endian = LTT_BIG_ENDIAN; |
1300 | |
1301 | if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4) |
1302 | *size = LTT_LP32; |
1303 | else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4) |
1304 | *size = LTT_ILP32; |
1305 | else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8) |
1306 | *size = LTT_LP64; |
1307 | else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8) |
1308 | *size = LTT_ILP64; |
1309 | else *size = LTT_UNKNOWN; |
1310 | } |
1311 | |