likely/unlikely branch prediction
[lttv.git] / ltt / branches / poly / ltt / tracefile.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Xiangxiu Yang
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
17 */
18
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <string.h>
22 #include <dirent.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <errno.h>
26 #include <unistd.h>
27
28 // For realpath
29 #include <limits.h>
30 #include <stdlib.h>
31
32
33 #include "parser.h"
34 #include <ltt/ltt.h>
35 #include "ltt-private.h"
36 #include <ltt/trace.h>
37 #include <ltt/facility.h>
38 #include <ltt/event.h>
39 #include <ltt/type.h>
40
41 #define DIR_NAME_SIZE 256
42 #define __UNUSED__ __attribute__((__unused__))
43
44
45 /* obtain the time of an event */
46
47 static inline LttTime getEventTime(LttTracefile * tf);
48
49
50 /* set the offset of the fields belonging to the event,
51 need the information of the archecture */
52 void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace *t);
53
54 /* get the size of the field type according to the archtecture's
55 size and endian type(info of the archecture) */
56 int getFieldtypeSize(LttTracefile * tf, LttEventType * evT, int offsetRoot,
57 int offsetParent, LttField *fld, void *evD, LttTrace* t);
58
59 /* read a fixed size or a block information from the file (fd) */
60 int readFile(int fd, void * buf, size_t size, char * mesg);
61 int readBlock(LttTracefile * tf, int whichBlock);
62
63 /* calculate cycles per nsec for current block */
64 void getCyclePerNsec(LttTracefile * t);
65
66 /* reinitialize the info of the block which is already in the buffer */
67 void updateTracefile(LttTracefile * tf);
68
69 /* go to the next event */
70 int skipEvent(LttTracefile * t);
71
72
73 /* Functions to parse system.xml file (using glib xml parser) */
74 static void parser_start_element (GMarkupParseContext __UNUSED__ *context,
75 const gchar *element_name,
76 const gchar **attribute_names,
77 const gchar **attribute_values,
78 gpointer user_data,
79 GError **error)
80 {
81 int i=0;
82 LttSystemDescription* des = (LttSystemDescription* )user_data;
83 if(strcmp("system", element_name)){
84 *error = g_error_new(G_MARKUP_ERROR,
85 G_LOG_LEVEL_WARNING,
86 "This is not system.xml file");
87 return;
88 }
89
90 while(attribute_names[i]){
91 if(strcmp("node_name", attribute_names[i])==0){
92 des->node_name = g_strdup(attribute_values[i]);
93 }else if(strcmp("domainname", attribute_names[i])==0){
94 des->domain_name = g_strdup(attribute_values[i]);
95 }else if(strcmp("cpu", attribute_names[i])==0){
96 des->nb_cpu = atoi(attribute_values[i]);
97 }else if(strcmp("arch_size", attribute_names[i])==0){
98 if(strcmp(attribute_values[i],"LP32") == 0) des->size = LTT_LP32;
99 else if(strcmp(attribute_values[i],"ILP32") == 0) des->size = LTT_ILP32;
100 else if(strcmp(attribute_values[i],"LP64") == 0) des->size = LTT_LP64;
101 else if(strcmp(attribute_values[i],"ILP64") == 0) des->size = LTT_ILP64;
102 else if(strcmp(attribute_values[i],"UNKNOWN") == 0) des->size = LTT_UNKNOWN;
103 }else if(strcmp("endian", attribute_names[i])==0){
104 if(strcmp(attribute_values[i],"LITTLE_ENDIAN") == 0)
105 des->endian = LTT_LITTLE_ENDIAN;
106 else if(strcmp(attribute_values[i],"BIG_ENDIAN") == 0)
107 des->endian = LTT_BIG_ENDIAN;
108 }else if(strcmp("kernel_name", attribute_names[i])==0){
109 des->kernel_name = g_strdup(attribute_values[i]);
110 }else if(strcmp("kernel_release", attribute_names[i])==0){
111 des->kernel_release = g_strdup(attribute_values[i]);
112 }else if(strcmp("kernel_version", attribute_names[i])==0){
113 des->kernel_version = g_strdup(attribute_values[i]);
114 }else if(strcmp("machine", attribute_names[i])==0){
115 des->machine = g_strdup(attribute_values[i]);
116 }else if(strcmp("processor", attribute_names[i])==0){
117 des->processor = g_strdup(attribute_values[i]);
118 }else if(strcmp("hardware_platform", attribute_names[i])==0){
119 des->hardware_platform = g_strdup(attribute_values[i]);
120 }else if(strcmp("operating_system", attribute_names[i])==0){
121 des->operating_system = g_strdup(attribute_values[i]);
122 }else if(strcmp("ltt_major_version", attribute_names[i])==0){
123 des->ltt_major_version = atoi(attribute_values[i]);
124 }else if(strcmp("ltt_minor_version", attribute_names[i])==0){
125 des->ltt_minor_version = atoi(attribute_values[i]);
126 }else if(strcmp("ltt_block_size", attribute_names[i])==0){
127 des->ltt_block_size = atoi(attribute_values[i]);
128 }else{
129 *error = g_error_new(G_MARKUP_ERROR,
130 G_LOG_LEVEL_WARNING,
131 "Not a valid attribute");
132 return;
133 }
134 i++;
135 }
136 }
137
138 static void parser_characters (GMarkupParseContext __UNUSED__ *context,
139 const gchar *text,
140 gsize __UNUSED__ text_len,
141 gpointer user_data,
142 GError __UNUSED__ **error)
143 {
144 LttSystemDescription* des = (LttSystemDescription* )user_data;
145 des->description = g_strdup(text);
146 }
147
148
149 /*****************************************************************************
150 *Function name
151 * ltt_tracefile_open : open a trace file, construct a LttTracefile
152 *Input params
153 * t : the trace containing the tracefile
154 * fileName : path name of the trace file
155 *Return value
156 * : a pointer to a tracefile
157 ****************************************************************************/
158
159 LttTracefile* ltt_tracefile_open(LttTrace * t, char * fileName)
160 {
161 LttTracefile * tf;
162 struct stat lTDFStat; /* Trace data file status */
163
164 tf = g_new(LttTracefile, 1);
165
166 //open the file
167 tf->name = g_strdup(fileName);
168 tf->trace = t;
169 tf->fd = open(fileName, O_RDONLY, 0);
170 if(tf->fd < 0){
171 g_warning("Unable to open input data file %s\n", fileName);
172 g_free(tf->name);
173 g_free(tf);
174 return NULL;
175 }
176
177 // Get the file's status
178 if(fstat(tf->fd, &lTDFStat) < 0){
179 g_warning("Unable to get the status of the input data file %s\n", fileName);
180 g_free(tf->name);
181 close(tf->fd);
182 g_free(tf);
183 return NULL;
184 }
185
186 // Is the file large enough to contain a trace
187 if(lTDFStat.st_size < (off_t)(sizeof(BlockStart) + EVENT_HEADER_SIZE)){
188 g_print("The input data file %s does not contain a trace\n", fileName);
189 g_free(tf->name);
190 close(tf->fd);
191 g_free(tf);
192 return NULL;
193 }
194
195 //store the size of the file
196 tf->file_size = lTDFStat.st_size;
197 tf->block_size = t->system_description->ltt_block_size;
198 tf->block_number = tf->file_size / tf->block_size;
199 tf->which_block = 0;
200
201 //allocate memory to contain the info of a block
202 tf->buffer = (void *) g_new(char, t->system_description->ltt_block_size);
203
204 //read the first block
205 if(readBlock(tf,1)) exit(1);
206
207 return tf;
208 }
209
210
211 /*****************************************************************************
212 *Open control and per cpu tracefiles
213 ****************************************************************************/
214
215 void ltt_tracefile_open_cpu(LttTrace *t, char * tracefile_name)
216 {
217 LttTracefile * tf;
218 tf = ltt_tracefile_open(t,tracefile_name);
219 if(!tf) return;
220 t->per_cpu_tracefile_number++;
221 g_ptr_array_add(t->per_cpu_tracefiles, tf);
222 }
223
224 gint ltt_tracefile_open_control(LttTrace *t, char * control_name)
225 {
226 LttTracefile * tf;
227 LttEvent ev;
228 LttFacility * f;
229 void * pos;
230 FacilityLoad fLoad;
231 unsigned int i;
232
233 tf = ltt_tracefile_open(t,control_name);
234 if(!tf) {
235 g_warning("ltt_tracefile_open_control : bad file descriptor");
236 return -1;
237 }
238 t->control_tracefile_number++;
239 g_ptr_array_add(t->control_tracefiles,tf);
240
241 //parse facilities tracefile to get base_id
242 if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){
243 while(1){
244 if(!ltt_tracefile_read(tf,&ev)) return 0; // end of file
245
246 if(ev.event_id == TRACE_FACILITY_LOAD){
247 pos = ev.data;
248 fLoad.name = (char*)pos;
249 fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name));
250 fLoad.base_code = *(guint32 *)(pos + strlen(fLoad.name) + sizeof(LttChecksum));
251
252 for(i=0;i<t->facility_number;i++){
253 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
254 if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){
255 f->base_id = fLoad.base_code;
256 break;
257 }
258 }
259 if(i==t->facility_number) {
260 g_warning("Facility: %s, checksum: %u is not found",
261 fLoad.name,(unsigned int)fLoad.checksum);
262 return -1;
263 }
264 }else if(ev.event_id == TRACE_BLOCK_START){
265 continue;
266 }else if(ev.event_id == TRACE_BLOCK_END){
267 break;
268 }else {
269 g_warning("Not valid facilities trace file");
270 return -1;
271 }
272 }
273 }
274 return 0;
275 }
276
277 /*****************************************************************************
278 *Function name
279 * ltt_tracefile_close: close a trace file,
280 *Input params
281 * t : tracefile which will be closed
282 ****************************************************************************/
283
284 void ltt_tracefile_close(LttTracefile *t)
285 {
286 g_free(t->name);
287 g_free(t->buffer);
288 close(t->fd);
289 g_free(t);
290 }
291
292
293 /*****************************************************************************
294 *Get system information
295 ****************************************************************************/
296 gint getSystemInfo(LttSystemDescription* des, char * pathname)
297 {
298 FILE * fp;
299 char buf[DIR_NAME_SIZE];
300
301 GMarkupParseContext * context;
302 GError * error = NULL;
303 GMarkupParser markup_parser =
304 {
305 parser_start_element,
306 NULL,
307 parser_characters,
308 NULL, /* passthrough */
309 NULL /* error */
310 };
311
312 fp = fopen(pathname,"r");
313 if(!fp){
314 g_warning("Can not open file : %s\n", pathname);
315 return -1;
316 }
317
318 context = g_markup_parse_context_new(&markup_parser, 0, des,NULL);
319
320 while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
321 if(!g_markup_parse_context_parse(context, buf, DIR_NAME_SIZE, &error)){
322 if(error != NULL) {
323 g_warning("Can not parse xml file: \n%s\n", error->message);
324 g_error_free(error);
325 }
326 g_markup_parse_context_free(context);
327 fclose(fp);
328 return -1;
329 }
330 }
331 g_markup_parse_context_free(context);
332 fclose(fp);
333 return 0;
334 }
335
336 /*****************************************************************************
337 *The following functions get facility/tracefile information
338 ****************************************************************************/
339
340 gint getFacilityInfo(LttTrace *t, char* eventdefs)
341 {
342 DIR * dir;
343 struct dirent *entry;
344 char * ptr;
345 unsigned int i,j;
346 LttFacility * f;
347 LttEventType * et;
348 char name[DIR_NAME_SIZE];
349
350 dir = opendir(eventdefs);
351 if(!dir) {
352 g_warning("Can not open directory: %s\n", eventdefs);
353 return -1;
354 }
355
356 while((entry = readdir(dir)) != NULL){
357 ptr = &entry->d_name[strlen(entry->d_name)-4];
358 if(strcmp(ptr,".xml") != 0) continue;
359 strcpy(name,eventdefs);
360 strcat(name,entry->d_name);
361 ltt_facility_open(t,name);
362 }
363 closedir(dir);
364
365 for(j=0;j<t->facility_number;j++){
366 f = (LttFacility*)g_ptr_array_index(t->facilities, j);
367 for(i=0; i<f->event_number; i++){
368 et = f->events[i];
369 setFieldsOffset(NULL, et, NULL, t);
370 }
371 }
372 return 0;
373 }
374
375 gint getControlFileInfo(LttTrace *t, char* control)
376 {
377 DIR * dir;
378 struct dirent *entry;
379 char name[DIR_NAME_SIZE];
380
381 dir = opendir(control);
382 if(!dir) {
383 g_warning("Can not open directory: %s\n", control);
384 return -1;
385 }
386
387 while((entry = readdir(dir)) != NULL){
388 if(strcmp(entry->d_name,"facilities") != 0 &&
389 strcmp(entry->d_name,"interrupts") != 0 &&
390 strcmp(entry->d_name,"processes") != 0) continue;
391
392 strcpy(name,control);
393 strcat(name,entry->d_name);
394 if(ltt_tracefile_open_control(t,name))
395 return -1;
396 }
397 closedir(dir);
398 return 0;
399 }
400
401 gint getCpuFileInfo(LttTrace *t, char* cpu)
402 {
403 DIR * dir;
404 struct dirent *entry;
405 char name[DIR_NAME_SIZE];
406
407 dir = opendir(cpu);
408 if(!dir) {
409 g_warning("Can not open directory: %s\n", cpu);
410 return -1;
411 }
412
413 while((entry = readdir(dir)) != NULL){
414 if(strcmp(entry->d_name,".") != 0 &&
415 strcmp(entry->d_name,"..") != 0 &&
416 strcmp(entry->d_name,".svn") != 0){
417 strcpy(name,cpu);
418 strcat(name,entry->d_name);
419 ltt_tracefile_open_cpu(t,name);
420 }else continue;
421 }
422 closedir(dir);
423 return 0;
424 }
425
426 /*****************************************************************************
427 *A trace is specified as a pathname to the directory containing all the
428 *associated data (control tracefiles, per cpu tracefiles, event
429 *descriptions...).
430 *
431 *When a trace is closed, all the associated facilities, types and fields
432 *are released as well.
433 */
434
435
436 /****************************************************************************
437 * get_absolute_pathname
438 *
439 * return the unique pathname in the system
440 *
441 * MD : Fixed this function so it uses realpath, dealing well with
442 * forgotten cases (.. were not used correctly before).
443 *
444 ****************************************************************************/
445 void get_absolute_pathname(const char *pathname, char * abs_pathname)
446 {
447 abs_pathname[0] = '\0';
448
449 if ( realpath (pathname, abs_pathname) != NULL)
450 return;
451 else
452 {
453 /* error, return the original path unmodified */
454 strcpy(abs_pathname, pathname);
455 return;
456 }
457 return;
458 }
459
460 LttTrace *ltt_trace_open(const char *pathname)
461 {
462 LttTrace * t;
463 LttSystemDescription * sys_description;
464 char eventdefs[DIR_NAME_SIZE];
465 char info[DIR_NAME_SIZE];
466 char control[DIR_NAME_SIZE];
467 char cpu[DIR_NAME_SIZE];
468 char tmp[DIR_NAME_SIZE];
469 char abs_path[DIR_NAME_SIZE];
470 gboolean has_slash = FALSE;
471
472 get_absolute_pathname(pathname, abs_path);
473 //establish the pathname to different directories
474 if(abs_path[strlen(abs_path)-1] == '/')has_slash = TRUE;
475 strcpy(eventdefs,abs_path);
476 if(!has_slash)strcat(eventdefs,"/");
477 strcat(eventdefs,"eventdefs/");
478
479 strcpy(info,abs_path);
480 if(!has_slash)strcat(info,"/");
481 strcat(info,"info/");
482
483 strcpy(control,abs_path);
484 if(!has_slash)strcat(control,"/");
485 strcat(control,"control/");
486
487 strcpy(cpu,abs_path);
488 if(!has_slash)strcat(cpu,"/");
489 strcat(cpu,"cpu/");
490
491 //new trace
492 sys_description = g_new(LttSystemDescription, 1);
493 t = g_new(LttTrace, 1);
494 t->pathname = g_strdup(abs_path);
495 t->facility_number = 0;
496 t->control_tracefile_number = 0;
497 t->per_cpu_tracefile_number = 0;
498 t->system_description = sys_description;
499 t->control_tracefiles = g_ptr_array_new();
500 t->per_cpu_tracefiles = g_ptr_array_new();
501 t->facilities = g_ptr_array_new();
502 getDataEndianType(&(t->my_arch_size), &(t->my_arch_endian));
503
504 //get system description
505 strcpy(tmp,info);
506 strcat(tmp,"system.xml");
507 if(getSystemInfo(sys_description, tmp)) {
508 g_ptr_array_free(t->facilities, TRUE);
509 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
510 g_ptr_array_free(t->control_tracefiles, TRUE);
511 g_free(sys_description);
512 g_free(t->pathname);
513 g_free(t);
514 return NULL;
515 }
516
517
518
519 //get facilities info
520 if(getFacilityInfo(t,eventdefs)) {
521 g_ptr_array_free(t->facilities, TRUE);
522 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
523 g_ptr_array_free(t->control_tracefiles, TRUE);
524 g_free(sys_description);
525 g_free(t->pathname);
526 g_free(t);
527 return NULL;
528 }
529
530 //get control tracefile info
531 getControlFileInfo(t,control);
532 /*
533 if(getControlFileInfo(t,control)) {
534 g_ptr_array_free(t->facilities, TRUE);
535 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
536 g_ptr_array_free(t->control_tracefiles, TRUE);
537 g_free(sys_description);
538 g_free(t->pathname);
539 g_free(t);
540 return NULL;
541 }*/ // With fatal error
542
543 //get cpu tracefile info
544 if(getCpuFileInfo(t,cpu)) {
545 g_ptr_array_free(t->facilities, TRUE);
546 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
547 g_ptr_array_free(t->control_tracefiles, TRUE);
548 g_free(sys_description);
549 g_free(t->pathname);
550 g_free(t);
551 return NULL;
552 }
553
554 return t;
555 }
556
557 char * ltt_trace_name(LttTrace *t)
558 {
559 return t->pathname;
560 }
561
562
563 /******************************************************************************
564 * When we copy a trace, we want all the opening actions to happen again :
565 * the trace will be reopened and totally independant from the original.
566 * That's why we call ltt_trace_open.
567 *****************************************************************************/
568 LttTrace *ltt_trace_copy(LttTrace *self)
569 {
570 return ltt_trace_open(self->pathname);
571 }
572
573 void ltt_trace_close(LttTrace *t)
574 {
575 unsigned int i;
576 LttTracefile * tf;
577 LttFacility * f;
578
579 g_free(t->pathname);
580
581 //free system_description
582 g_free(t->system_description->description);
583 g_free(t->system_description->node_name);
584 g_free(t->system_description->domain_name);
585 g_free(t->system_description->kernel_name);
586 g_free(t->system_description->kernel_release);
587 g_free(t->system_description->kernel_version);
588 g_free(t->system_description->machine);
589 g_free(t->system_description->processor);
590 g_free(t->system_description->hardware_platform);
591 g_free(t->system_description->operating_system);
592 g_free(t->system_description);
593
594 //free control_tracefiles
595 for(i=0;i<t->control_tracefile_number;i++){
596 tf = (LttTracefile*)g_ptr_array_index(t->control_tracefiles,i);
597 ltt_tracefile_close(tf);
598 }
599 g_ptr_array_free(t->control_tracefiles, TRUE);
600
601 //free per_cpu_tracefiles
602 for(i=0;i<t->per_cpu_tracefile_number;i++){
603 tf = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles,i);
604 ltt_tracefile_close(tf);
605 }
606 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
607
608 //free facilities
609 for(i=0;i<t->facility_number;i++){
610 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
611 ltt_facility_close(f);
612 }
613 g_ptr_array_free(t->facilities, TRUE);
614
615 g_free(t);
616
617 g_blow_chunks();
618 }
619
620
621 /*****************************************************************************
622 *Get the system description of the trace
623 ****************************************************************************/
624
625 LttSystemDescription *ltt_trace_system_description(LttTrace *t)
626 {
627 return t->system_description;
628 }
629
630 /*****************************************************************************
631 * The following functions discover the facilities of the trace
632 ****************************************************************************/
633
634 unsigned ltt_trace_facility_number(LttTrace *t)
635 {
636 return (unsigned)(t->facility_number);
637 }
638
639 LttFacility *ltt_trace_facility_get(LttTrace *t, unsigned i)
640 {
641 return (LttFacility*)g_ptr_array_index(t->facilities, i);
642 }
643
644 /*****************************************************************************
645 *Function name
646 * ltt_trace_facility_find : find facilities in the trace
647 *Input params
648 * t : the trace
649 * name : facility name
650 *Output params
651 * position : position of the facility in the trace
652 *Return value
653 * : the number of facilities
654 ****************************************************************************/
655
656 unsigned ltt_trace_facility_find(LttTrace *t, char *name, unsigned *position)
657 {
658 unsigned int i, count=0;
659 LttFacility * f;
660 for(i=0;i<t->facility_number;i++){
661 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
662 if(strcmp(f->name,name)==0){
663 count++;
664 if(count==1) *position = i;
665 }else{
666 if(count) break;
667 }
668 }
669 return count;
670 }
671
672 /*****************************************************************************
673 * Functions to discover all the event types in the trace
674 ****************************************************************************/
675
676 unsigned ltt_trace_eventtype_number(LttTrace *t)
677 {
678 unsigned int i;
679 unsigned count = 0;
680 LttFacility * f;
681 for(i=0;i<t->facility_number;i++){
682 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
683 count += f->event_number;
684 }
685 return count;
686 }
687
688 LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id)
689 {
690 LttFacility * facility = NULL;
691 unsigned int i;
692
693 for(i=0;i<trace->facility_number;i++){
694 LttFacility *iter_facility =
695 (LttFacility*) g_ptr_array_index(trace->facilities,i);
696 if(id >= iter_facility->base_id &&
697 id < iter_facility->base_id + iter_facility->event_number) {
698 facility = iter_facility;
699 break;
700 }
701 }
702
703 return facility;
704 }
705
706 LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
707 {
708 LttFacility * f;
709 f = ltt_trace_facility_by_id(t,evId);
710 if(!f) return NULL;
711 return f->events[evId - f->base_id];
712 }
713
714 /*****************************************************************************
715 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
716 *the maximum number of CPU in the system. When the number of CPU installed
717 *is less than the maximum, some positions are unused. There are also a
718 *number of "control" tracefiles (facilities, interrupts...).
719 ****************************************************************************/
720 unsigned ltt_trace_control_tracefile_number(LttTrace *t)
721 {
722 return t->control_tracefile_number;
723 }
724
725 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace *t)
726 {
727 return t->per_cpu_tracefile_number;
728 }
729
730 /*****************************************************************************
731 *It is possible to search for the tracefiles by name or by CPU position.
732 *The index within the tracefiles of the same type is returned if found
733 *and a negative value otherwise.
734 ****************************************************************************/
735
736 int ltt_trace_control_tracefile_find(LttTrace *t, const gchar *name)
737 {
738 LttTracefile * tracefile;
739 unsigned int i;
740 for(i=0;i<t->control_tracefile_number;i++){
741 tracefile = (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
742 if(strcmp(tracefile->name, name)==0)break;
743 }
744 if(i == t->control_tracefile_number) return -1;
745 return i;
746 }
747
748 /* not really useful. We just have to know that cpu tracefiles
749 * comes before control tracefiles.
750 */
751 int ltt_trace_per_cpu_tracefile_find(LttTrace *t, const gchar *name)
752 {
753 LttTracefile * tracefile;
754 unsigned int i;
755 for(i=0;i<t->per_cpu_tracefile_number;i++){
756 tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
757 if(strcmp(tracefile->name, name)==0)break;
758 }
759 if(i == t->per_cpu_tracefile_number) return -1;
760 return i;
761 }
762
763 /*****************************************************************************
764 *Get a specific tracefile
765 ****************************************************************************/
766
767 LttTracefile *ltt_trace_control_tracefile_get(LttTrace *t, unsigned i)
768 {
769 return (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
770 }
771
772 LttTracefile *ltt_trace_per_cpu_tracefile_get(LttTrace *t, unsigned i)
773 {
774 return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
775 }
776
777 /*****************************************************************************
778 * Get the start time and end time of the trace
779 ****************************************************************************/
780
781 void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
782 {
783 LttTime startSmall, startTmp, endBig, endTmp;
784 unsigned int i, j=0;
785 LttTracefile * tf;
786
787 for(i=0;i<t->control_tracefile_number;i++){
788 tf = g_ptr_array_index(t->control_tracefiles, i);
789 readBlock(tf,1);
790 startTmp = tf->a_block_start->time;
791 readBlock(tf,tf->block_number);
792 endTmp = tf->a_block_end->time;
793 if(i==0){
794 startSmall = startTmp;
795 endBig = endTmp;
796 j = 1;
797 continue;
798 }
799 if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp;
800 if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp;
801 }
802
803 for(i=0;i<t->per_cpu_tracefile_number;i++){
804 tf = g_ptr_array_index(t->per_cpu_tracefiles, i);
805 readBlock(tf,1);
806 startTmp = tf->a_block_start->time;
807 readBlock(tf,tf->block_number);
808 endTmp = tf->a_block_end->time;
809 if(j == 0 && i==0){
810 startSmall = startTmp;
811 endBig = endTmp;
812 continue;
813 }
814 if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp;
815 if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp;
816 }
817
818 if(start != NULL) *start = startSmall;
819 if(end != NULL) *end = endBig;
820 }
821
822
823 /*****************************************************************************
824 *Get the name of a tracefile
825 ****************************************************************************/
826
827 char *ltt_tracefile_name(LttTracefile *tf)
828 {
829 return tf->name;
830 }
831
832 /*****************************************************************************
833 * Get the number of blocks in the tracefile
834 ****************************************************************************/
835
836 unsigned ltt_tracefile_block_number(LttTracefile *tf)
837 {
838 return tf->block_number;
839 }
840
841 /*****************************************************************************
842 *Function name
843 * ltt_tracefile_seek_time: seek to the first event of the trace with time
844 * larger or equal to time
845 *Input params
846 * t : tracefile
847 * time : criteria of the time
848 ****************************************************************************/
849 void ltt_tracefile_find_time_block(LttTracefile *t, LttTime time,
850 int start_block, int end_block)
851 {
852 int err, tmp_block, s, e;
853 int headTime;
854 int tailTime;
855
856 err=readBlock(t,start_block);
857 if(err) g_error("Can not read tracefile: %s\n", t->name);
858 if(start_block == end_block)return;
859
860 tailTime = ltt_time_compare(t->a_block_end->time, time);
861 if(tailTime >= 0) return;
862
863 err=readBlock(t,end_block);
864 if(err) g_error("Can not read tracefile: %s\n", t->name);
865 if(start_block+1 == end_block)return;
866
867 headTime = ltt_time_compare(t->a_block_start->time, time);
868 if(headTime <= 0 ) return;
869
870 tmp_block = (end_block + start_block)/2;
871 err=readBlock(t,tmp_block);
872 if(err) g_error("Can not read tracefile: %s\n", t->name);
873
874 headTime = ltt_time_compare(t->a_block_start->time, time);
875 tailTime = ltt_time_compare(t->a_block_end->time, time);
876 if(headTime <= 0 && tailTime >= 0) return;
877
878 if(headTime > 0){
879 s = start_block + 1;
880 e = tmp_block - 1;
881 if(s <= e)
882 ltt_tracefile_find_time_block(t, time, s, e);
883 else return;
884 }
885
886 if(tailTime < 0){
887 s = tmp_block + 1;
888 e = end_block - 1;
889 if(s <= e)
890 ltt_tracefile_find_time_block(t, time, s, e);
891 else return;
892 }
893 }
894
895 void ltt_tracefile_backward_find_time_block(LttTracefile *t, LttTime time)
896 {
897 int t_time, h_time, err;
898 err=readBlock(t,t->which_block-1);
899 if(err) g_error("Can not read tracefile: %s\n", t->name);
900 h_time = ltt_time_compare(t->a_block_start->time, time);
901 t_time = ltt_time_compare(t->a_block_end->time, time);
902 if(h_time == 0){
903 int tmp;
904 if(t->which_block == 1) return;
905 err=readBlock(t,t->which_block-1);
906 if(err) g_error("Can not read tracefile: %s\n", t->name);
907 tmp = ltt_time_compare(t->a_block_end->time, time);
908 if(tmp == 0) return ltt_tracefile_seek_time(t, time);
909 err=readBlock(t,t->which_block+1);
910 if(err) g_error("Can not read tracefile: %s\n", t->name);
911 }else if(h_time > 0){
912 ltt_tracefile_find_time_block(t, time, 1, t->which_block);
913 return ltt_tracefile_seek_time(t, time) ;
914 }else{
915 if(t_time >= 0) return ltt_tracefile_seek_time(t, time);
916 err=readBlock(t,t->which_block+1);
917 if(err) g_error("Can not read tracefile: %s\n", t->name);
918 }
919 }
920
921 void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
922 {
923 int err;
924 LttTime lttTime;
925 int headTime = ltt_time_compare(t->a_block_start->time, time);
926 int tailTime = ltt_time_compare(t->a_block_end->time, time);
927 LttEvent ev;
928
929 if(headTime < 0 && tailTime > 0){
930 if(ltt_time_compare(t->a_block_end->time, t->current_event_time) !=0) {
931 lttTime = getEventTime(t);
932 err = ltt_time_compare(lttTime, time);
933 if(err > 0){
934 if(t->which_event==2 || ltt_time_compare(t->prev_event_time,time)<0){
935 return;
936 }else{
937 updateTracefile(t);
938 return ltt_tracefile_seek_time(t, time);
939 }
940 }else if(err < 0){
941 while(1){
942 if(ltt_tracefile_read(t,&ev) == NULL) {
943 g_print("End of file\n");
944 return;
945 }
946 lttTime = getEventTime(t);
947 err = ltt_time_compare(lttTime, time);
948 if(err >= 0)return;
949 }
950 }else return;
951 }else{//we are at the end of the block
952 updateTracefile(t);
953 return ltt_tracefile_seek_time(t, time);
954 }
955 }else if(headTime >= 0){
956 if(t->which_block == 1){
957 updateTracefile(t);
958 }else{
959 if(ltt_time_compare(t->prev_block_end_time, time) >= 0 ||
960 (t->prev_block_end_time.tv_sec == 0 &&
961 t->prev_block_end_time.tv_nsec == 0 )){
962 ltt_tracefile_backward_find_time_block(t, time);
963 }else{
964 updateTracefile(t);
965 }
966 }
967 }else if(tailTime < 0){
968 if(t->which_block != t->block_number){
969 ltt_tracefile_find_time_block(t, time, t->which_block+1, t->block_number);
970 return ltt_tracefile_seek_time(t, time);
971 }else {
972 t->cur_event_pos = t->buffer + t->block_size;
973 g_print("End of file\n");
974 return;
975 }
976 }else if(tailTime == 0){
977 t->cur_event_pos = t->last_event_pos;
978 t->current_event_time = time;
979 t->cur_heart_beat_number = 0;
980 t->prev_event_time.tv_sec = 0;
981 t->prev_event_time.tv_nsec = 0;
982 return;
983 }
984 }
985
986 /*****************************************************************************
987 * Seek to the first event with position equal or larger to ep
988 *
989 * Modified by Mathieu Desnoyers to used faster offset position instead of
990 * re-reading the whole buffer.
991 ****************************************************************************/
992
993 void ltt_tracefile_seek_position(LttTracefile *t, const LttEventPosition *ep)
994 {
995 //if we are at the right place, just return
996 if(t->which_block == ep->block_num && t->which_event == ep->event_num)
997 return;
998
999 if(t->which_block == ep->block_num) updateTracefile(t);
1000 else readBlock(t,ep->block_num);
1001 //event offset is available
1002 if(ep->old_position){
1003 int err;
1004
1005 t->which_event = ep->event_num;
1006 t->cur_event_pos = t->buffer + ep->event_offset;
1007 t->prev_event_time = ep->event_time;
1008 t->current_event_time = ep->event_time;
1009 t->cur_heart_beat_number = ep->heart_beat_number;
1010 t->cur_cycle_count = ep->event_cycle_count;
1011
1012 /* This is a workaround for fast position seek */
1013 t->last_event_pos = ep->last_event_pos;
1014 t->prev_block_end_time = ep->prev_block_end_time;
1015 t->prev_event_time = ep->prev_event_time;
1016 t->pre_cycle_count = ep->pre_cycle_count;
1017 t->count = ep->count;
1018 /* end of workaround */
1019
1020 //update the fields of the current event and go to the next event
1021 err = skipEvent(t);
1022 if(err == ERANGE) g_error("event id is out of range\n");
1023
1024 return;
1025 }
1026
1027 //only block number and event index are available
1028 //MD: warning : this is slow!
1029 g_warning("using slow O(n) tracefile seek position");
1030
1031 LttEvent event;
1032 while(t->which_event < ep->event_num) ltt_tracefile_read(t, &event);
1033
1034 return;
1035 }
1036
1037 /*****************************************************************************
1038 *Function name
1039 * ltt_tracefile_read : read the current event, set the pointer to the next
1040 *Input params
1041 * t : tracefile
1042 *Return value
1043 * LttEvent * : an event to be processed
1044 ****************************************************************************/
1045
1046 LttEvent *ltt_tracefile_read(LttTracefile *t, LttEvent *event)
1047 {
1048 int err;
1049
1050 if(t->cur_event_pos == t->buffer + t->block_size){
1051 if(t->which_block == t->block_number){
1052 return NULL;
1053 }
1054 err = readBlock(t, t->which_block + 1);
1055 if(err)g_error("Can not read tracefile");
1056 }
1057
1058 event->event_id = (int)(*(guint16 *)(t->cur_event_pos));
1059 if(event->event_id == TRACE_TIME_HEARTBEAT)
1060 t->cur_heart_beat_number++;
1061
1062 t->prev_event_time = t->current_event_time;
1063 // t->current_event_time = getEventTime(t);
1064
1065 event->time_delta = *(guint32 *)(t->cur_event_pos + EVENT_ID_SIZE);
1066 event->event_time = t->current_event_time;
1067 event->event_cycle_count = t->cur_cycle_count;
1068
1069 event->tracefile = t;
1070 event->data = t->cur_event_pos + EVENT_HEADER_SIZE;
1071 event->which_block = t->which_block;
1072 event->which_event = t->which_event;
1073
1074 /* This is a workaround for fast position seek */
1075 event->last_event_pos = t->last_event_pos;
1076 event->prev_block_end_time = t->prev_block_end_time;
1077 event->prev_event_time = t->prev_event_time;
1078 event->pre_cycle_count = t->pre_cycle_count;
1079 event->count = t->count;
1080 /* end of workaround */
1081
1082
1083
1084 //update the fields of the current event and go to the next event
1085 err = skipEvent(t);
1086 if(err == ERANGE) g_error("event id is out of range\n");
1087
1088 return event;
1089 }
1090
1091 /****************************************************************************
1092 *Function name
1093 * readFile : wrap function to read from a file
1094 *Input Params
1095 * fd : file descriptor
1096 * buf : buf to contain the content
1097 * size : number of bytes to be read
1098 * mesg : message to be printed if some thing goes wrong
1099 *return value
1100 * 0 : success
1101 * EIO : can not read from the file
1102 ****************************************************************************/
1103
1104 int readFile(int fd, void * buf, size_t size, char * mesg)
1105 {
1106 ssize_t nbBytes = read(fd, buf, size);
1107
1108 if((size_t)nbBytes != size) {
1109 if(nbBytes < 0) {
1110 perror("Error in readFile : ");
1111 } else {
1112 g_warning("%s",mesg);
1113 }
1114 return EIO;
1115 }
1116 return 0;
1117 }
1118
1119 /****************************************************************************
1120 *Function name
1121 * readBlock : read a block from the file
1122 *Input Params
1123 * lttdes : ltt trace file
1124 * whichBlock : the block which will be read
1125 *return value
1126 * 0 : success
1127 * EINVAL : lseek fail
1128 * EIO : can not read from the file
1129 ****************************************************************************/
1130
1131 int readBlock(LttTracefile * tf, int whichBlock)
1132 {
1133 off_t nbBytes;
1134 guint32 lostSize;
1135
1136 if(whichBlock - tf->which_block == 1 && tf->which_block != 0){
1137 tf->prev_block_end_time = tf->a_block_end->time;
1138 tf->prev_event_time = tf->a_block_end->time;
1139 }else{
1140 tf->prev_block_end_time.tv_sec = 0;
1141 tf->prev_block_end_time.tv_nsec = 0;
1142 tf->prev_event_time.tv_sec = 0;
1143 tf->prev_event_time.tv_nsec = 0;
1144 }
1145
1146 nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET);
1147 if(nbBytes == -1) return EINVAL;
1148
1149 if(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block"))
1150 return EIO;
1151
1152 tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE);
1153 lostSize = *(guint32 *)(tf->buffer + tf->block_size - sizeof(guint32));
1154 tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size -
1155 lostSize + EVENT_HEADER_SIZE);
1156 tf->last_event_pos = tf->buffer + tf->block_size - lostSize;
1157
1158 tf->which_block = whichBlock;
1159 tf->which_event = 1;
1160 tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev
1161 tf->cur_heart_beat_number = 0;
1162
1163 getCyclePerNsec(tf);
1164
1165 tf->current_event_time = getEventTime(tf);
1166
1167 return 0;
1168 }
1169
1170 /*****************************************************************************
1171 *Function name
1172 * updateTracefile : reinitialize the info of the block which is already
1173 * in the buffer
1174 *Input params
1175 * tf : tracefile
1176 ****************************************************************************/
1177
1178 void updateTracefile(LttTracefile * tf)
1179 {
1180 tf->which_event = 1;
1181 tf->cur_event_pos = tf->buffer;
1182 tf->current_event_time = getEventTime(tf);
1183 tf->cur_heart_beat_number = 0;
1184
1185 tf->prev_event_time.tv_sec = 0;
1186 tf->prev_event_time.tv_nsec = 0;
1187 }
1188
1189 /*****************************************************************************
1190 *Function name
1191 * skipEvent : go to the next event, update the fields of the current event
1192 *Input params
1193 * t : tracefile
1194 *return value
1195 * 0 : success
1196 * ERANGE : event id is out of range
1197 ****************************************************************************/
1198
1199 int skipEvent(LttTracefile * t)
1200 {
1201 int evId;
1202 void * evData;
1203 LttEventType * evT;
1204 LttField * rootFld;
1205
1206 evId = (int)(*(guint16 *)(t->cur_event_pos));
1207 evData = t->cur_event_pos + EVENT_HEADER_SIZE;
1208
1209 evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
1210
1211 if(likely(evT)) rootFld = evT->root_field;
1212 else return ERANGE;
1213
1214 if(likely(rootFld)){
1215 //event has string/sequence or the last event is not the same event
1216 if(likely((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event)
1217 && rootFld->field_fixed == 0)){
1218 setFieldsOffset(t, evT, evData, t->trace);
1219 }
1220 t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
1221 }else t->cur_event_pos += EVENT_HEADER_SIZE;
1222
1223 evT->latest_block = t->which_block;
1224 evT->latest_event = t->which_event;
1225
1226 //the next event is in the next block
1227 if(unlikely(evId == TRACE_BLOCK_END)){
1228 t->cur_event_pos = t->buffer + t->block_size;
1229 }else{
1230 t->which_event++;
1231 t->current_event_time = getEventTime(t);
1232 }
1233
1234 return 0;
1235 }
1236
1237
1238 /*****************************************************************************
1239 *Function name
1240 * getCyclePerNsec : calculate cycles per nsec for current block
1241 *Input Params
1242 * t : tracefile
1243 ****************************************************************************/
1244
1245 void getCyclePerNsec(LttTracefile * t)
1246 {
1247 LttTime lBufTotalTime; /* Total time for this buffer */
1248 double lBufTotalNSec; /* Total time for this buffer in nsecs */
1249 double lBufTotalCycle;/* Total cycles for this buffer */
1250
1251 /* Calculate the total time for this buffer */
1252 lBufTotalTime = ltt_time_sub(t->a_block_end->time, t->a_block_start->time);
1253
1254 /* Calculate the total cycles for this bufffer */
1255 lBufTotalCycle = t->a_block_end->cycle_count;
1256 lBufTotalCycle -= t->a_block_start->cycle_count;
1257
1258 /* Convert the total time to nsecs */
1259 lBufTotalNSec = ltt_time_to_double(lBufTotalTime);
1260
1261 t->nsec_per_cycle = (double)lBufTotalNSec / (double)lBufTotalCycle;
1262 /* See : http://www.azillionmonkeys.com/qed/adiv.html */
1263 // precalculate the reciprocal, so divisions will be really fast.
1264 // 2^32-1 == 0xFFFFFFFFULL
1265 //{
1266 // double int_res = lBufTotalCycle/lBufTotalNSec;
1267 // t->cycles_per_nsec_reciprocal =
1268 // ((0xFFFF+int_res)/int_res);
1269 //}
1270
1271 }
1272
1273 /****************************************************************************
1274 *Function name
1275 * getEventTime : obtain the time of an event
1276 * NOTE : this function _really_ is on critical path.
1277 *Input params
1278 * tf : tracefile
1279 *Return value
1280 * LttTime : the time of the event
1281 ****************************************************************************/
1282
1283 static inline LttTime getEventTime(LttTracefile * tf)
1284 {
1285 LttTime time;
1286 LttCycleCount cycle_count; // cycle count for the current event
1287 LttCycleCount lEventTotalCycle; // Total cycles from start for event
1288 LttCycleCount lEventNSec; // Total nsecs from start for event
1289 LttTime lTimeOffset; // Time offset in struct LttTime
1290 guint16 evId;
1291
1292 evId = *(guint16 *)tf->cur_event_pos;
1293 if(unlikely(evId == TRACE_BLOCK_START)){
1294 tf->count = 0;
1295 tf->pre_cycle_count = 0;
1296 tf->cur_cycle_count = tf->a_block_start->cycle_count;
1297 return tf->a_block_start->time;
1298 }else if(unlikely(evId == TRACE_BLOCK_END)){
1299 tf->count = 0;
1300 tf->pre_cycle_count = 0;
1301 tf->cur_cycle_count = tf->a_block_end->cycle_count;
1302 return tf->a_block_end->time;
1303 }
1304
1305 // Calculate total time in cycles from start of buffer for this event
1306 cycle_count = (LttCycleCount)*(guint32 *)(tf->cur_event_pos + EVENT_ID_SIZE);
1307
1308 if(unlikely(cycle_count < tf->pre_cycle_count)) tf->count++;
1309 tf->pre_cycle_count = cycle_count;
1310 cycle_count += (LttCycleCount)tf->count << 32;
1311
1312 // if(tf->cur_heart_beat_number > tf->count)
1313 // cycle_count += tmpCycleCount * (tf->cur_heart_beat_number - tf->count);
1314
1315 tf->cur_cycle_count = cycle_count;
1316
1317 lEventTotalCycle = cycle_count;
1318 lEventTotalCycle -= tf->a_block_start->cycle_count;
1319
1320 // Convert it to nsecs
1321 lEventNSec = (double)lEventTotalCycle * (double)tf->nsec_per_cycle;
1322 //lEventNSec = (tf->cycles_per_nsec_reciprocal * lEventTotalCycle) >> 16;
1323
1324 // Determine offset in struct LttTime
1325 lTimeOffset = ltt_time_from_double(lEventNSec);
1326
1327 time = ltt_time_add(tf->a_block_start->time, lTimeOffset);
1328
1329 return time;
1330 }
1331
1332 /*****************************************************************************
1333 *Function name
1334 * setFieldsOffset : set offset of the fields
1335 *Input params
1336 * tracefile : opened trace file
1337 * evT : the event type
1338 * evD : event data, it may be NULL
1339 ****************************************************************************/
1340
1341 void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
1342 {
1343 LttField * rootFld = evT->root_field;
1344 // rootFld->base_address = evD;
1345
1346 if(likely(rootFld))
1347 rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);
1348 }
1349
1350 /*****************************************************************************
1351 *Function name
1352 * getFieldtypeSize: get the size of the field type (primitive type)
1353 *Input params
1354 * tracefile : opened trace file
1355 * evT : event type
1356 * offsetRoot : offset from the root
1357 * offsetParent : offset from the parrent
1358 * fld : field
1359 * evD : event data, it may be NULL
1360 *Return value
1361 * int : size of the field
1362 ****************************************************************************/
1363
1364 int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot,
1365 int offsetParent, LttField * fld, void *evD, LttTrace *trace)
1366 {
1367 int size, size1, element_number, i, offset1, offset2;
1368 LttType * type = fld->field_type;
1369
1370 if(likely(t)){
1371 if(unlikely(evT->latest_block==t->which_block && evT->latest_event==t->which_event)){
1372 return fld->field_size;
1373 }
1374 }
1375
1376 if(likely(fld->field_fixed == 1)){
1377 if(fld == evT->root_field) return fld->field_size;
1378 }
1379
1380 switch(type->type_class) {
1381 case LTT_ARRAY:
1382 element_number = (int) type->element_number;
1383 if(fld->field_fixed == -1){
1384 size = getFieldtypeSize(t, evT, offsetRoot,
1385 0,fld->child[0], NULL, trace);
1386 if(size == 0){ //has string or sequence
1387 fld->field_fixed = 0;
1388 }else{
1389 fld->field_fixed = 1;
1390 size *= element_number;
1391 }
1392 }else if(fld->field_fixed == 0){// has string or sequence
1393 size = 0;
1394 for(i=0;i<element_number;i++){
1395 size += getFieldtypeSize(t, evT, offsetRoot+size,size,
1396 fld->child[0], evD+size, trace);
1397 }
1398 }else size = fld->field_size;
1399 break;
1400
1401 case LTT_SEQUENCE:
1402 size1 = (int) ltt_type_size(trace, type);
1403 if(fld->field_fixed == -1){
1404 fld->sequ_number_size = size1;
1405 fld->field_fixed = 0;
1406 size = getFieldtypeSize(t, evT, offsetRoot,
1407 0,fld->child[0], NULL, trace);
1408 fld->element_size = size;
1409 }else{//0: sequence
1410 element_number = getIntNumber(size1,evD);
1411 type->element_number = element_number;
1412 if(fld->element_size > 0){
1413 size = element_number * fld->element_size;
1414 }else{//sequence has string or sequence
1415 size = 0;
1416 for(i=0;i<element_number;i++){
1417 size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1,
1418 fld->child[0], evD+size+size1, trace);
1419 }
1420 }
1421 size += size1;
1422 }
1423 break;
1424
1425 case LTT_STRING:
1426 size = 0;
1427 if(fld->field_fixed == -1){
1428 fld->field_fixed = 0;
1429 }else{//0: string
1430 size = strlen((char*)evD) + 1; //include end : '\0'
1431 }
1432 break;
1433
1434 case LTT_STRUCT:
1435 element_number = (int) type->element_number;
1436 size = 0;
1437 if(fld->field_fixed == -1){
1438 offset1 = offsetRoot;
1439 offset2 = 0;
1440 for(i=0;i<element_number;i++){
1441 size1=getFieldtypeSize(t, evT,offset1,offset2,
1442 fld->child[i], NULL, trace);
1443 if(size1 > 0 && size >= 0){
1444 size += size1;
1445 if(offset1 >= 0) offset1 += size1;
1446 offset2 += size1;
1447 }else{
1448 size = -1;
1449 offset1 = -1;
1450 offset2 = -1;
1451 }
1452 }
1453 if(size == -1){
1454 fld->field_fixed = 0;
1455 size = 0;
1456 }else fld->field_fixed = 1;
1457 }else if(fld->field_fixed == 0){
1458 offset1 = offsetRoot;
1459 offset2 = 0;
1460 for(i=0;i<element_number;i++){
1461 size=getFieldtypeSize(t,evT,offset1,offset2,
1462 fld->child[i],evD+offset2, trace);
1463 offset1 += size;
1464 offset2 += size;
1465 }
1466 size = offset2;
1467 }else size = fld->field_size;
1468 break;
1469
1470 default:
1471 if(fld->field_fixed == -1){
1472 size = (int) ltt_type_size(trace, type);
1473 fld->field_fixed = 1;
1474 }else size = fld->field_size;
1475 break;
1476 }
1477
1478
1479
1480 #if 0
1481 if(type->type_class != LTT_STRUCT && type->type_class != LTT_ARRAY &&
1482 type->type_class != LTT_SEQUENCE && type->type_class != LTT_STRING){
1483 if(fld->field_fixed == -1){
1484 size = (int) ltt_type_size(trace, type);
1485 fld->field_fixed = 1;
1486 }else size = fld->field_size;
1487
1488 }else if(type->type_class == LTT_ARRAY){
1489 element_number = (int) type->element_number;
1490 if(fld->field_fixed == -1){
1491 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
1492 if(size == 0){ //has string or sequence
1493 fld->field_fixed = 0;
1494 }else{
1495 fld->field_fixed = 1;
1496 size *= element_number;
1497 }
1498 }else if(fld->field_fixed == 0){// has string or sequence
1499 size = 0;
1500 for(i=0;i<element_number;i++){
1501 size += getFieldtypeSize(t, evT, offsetRoot+size,size,
1502 fld->child[0], evD+size, trace);
1503 }
1504 }else size = fld->field_size;
1505
1506 }else if(type->type_class == LTT_SEQUENCE){
1507 size1 = (int) ltt_type_size(trace, type);
1508 if(fld->field_fixed == -1){
1509 fld->sequ_number_size = size1;
1510 fld->field_fixed = 0;
1511 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
1512 fld->element_size = size;
1513 }else{//0: sequence
1514 element_number = getIntNumber(size1,evD);
1515 type->element_number = element_number;
1516 if(fld->element_size > 0){
1517 size = element_number * fld->element_size;
1518 }else{//sequence has string or sequence
1519 size = 0;
1520 for(i=0;i<element_number;i++){
1521 size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1,
1522 fld->child[0], evD+size+size1, trace);
1523 }
1524 }
1525 size += size1;
1526 }
1527
1528 }else if(type->type_class == LTT_STRING){
1529 size = 0;
1530 if(fld->field_fixed == -1){
1531 fld->field_fixed = 0;
1532 }else{//0: string
1533 size = strlen((char*)evD) + 1; //include end : '\0'
1534 }
1535
1536 }else if(type->type_class == LTT_STRUCT){
1537 element_number = (int) type->element_number;
1538 size = 0;
1539 if(fld->field_fixed == -1){
1540 offset1 = offsetRoot;
1541 offset2 = 0;
1542 for(i=0;i<element_number;i++){
1543 size1=getFieldtypeSize(t, evT,offset1,offset2, fld->child[i], NULL, trace);
1544 if(size1 > 0 && size >= 0){
1545 size += size1;
1546 if(offset1 >= 0) offset1 += size1;
1547 offset2 += size1;
1548 }else{
1549 size = -1;
1550 offset1 = -1;
1551 offset2 = -1;
1552 }
1553 }
1554 if(size == -1){
1555 fld->field_fixed = 0;
1556 size = 0;
1557 }else fld->field_fixed = 1;
1558 }else if(fld->field_fixed == 0){
1559 offset1 = offsetRoot;
1560 offset2 = 0;
1561 for(i=0;i<element_number;i++){
1562 size=getFieldtypeSize(t,evT,offset1,offset2,fld->child[i],evD+offset2, trace);
1563 offset1 += size;
1564 offset2 += size;
1565 }
1566 size = offset2;
1567 }else size = fld->field_size;
1568 }
1569 #endif //0
1570
1571 fld->offset_root = offsetRoot;
1572 fld->offset_parent = offsetParent;
1573 if(!evD){
1574 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1575 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1576 }
1577 fld->field_size = size;
1578
1579 return size;
1580 }
1581
1582
1583 /*****************************************************************************
1584 *Function name
1585 * getIntNumber : get an integer number
1586 *Input params
1587 * size : the size of the integer
1588 * evD : the event data
1589 *Return value
1590 * gint64 : a 64 bits integer
1591 ****************************************************************************/
1592
1593 gint64 getIntNumber(int size, void *evD)
1594 {
1595 gint64 i;
1596
1597 switch(size) {
1598 case 1: i = *(gint8 *)evD; break;
1599 case 2: i = *(gint16 *)evD; break;
1600 case 4: i = *(gint32 *)evD; break;
1601 case 8: i = *(gint64 *)evD; break;
1602 default: i = *(gint64 *)evD;
1603 g_critical("getIntNumber : integer size %d unknown", size);
1604 break;
1605 }
1606
1607 #if 0
1608 if(size == 1) i = *(gint8 *)evD;
1609 else if(size == 2) i = *(gint16 *)evD;
1610 else if(size == 4) i = *(gint32 *)evD;
1611 else if(size == 8) i = *(gint64 *)evD;
1612 #endif //0
1613
1614 return (gint64)i;
1615 }
1616
1617 /*****************************************************************************
1618 *Function name
1619 * getDataEndianType : get the data type size and endian type of the local
1620 * machine
1621 *Input params
1622 * size : size of data type
1623 * endian : endian type, little or big
1624 ****************************************************************************/
1625
1626 void getDataEndianType(LttArchSize * size, LttArchEndian * endian)
1627 {
1628 int i = 1;
1629 char c = (char) i;
1630 int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *);
1631
1632 if(c == 1) *endian = LTT_LITTLE_ENDIAN;
1633 else *endian = LTT_BIG_ENDIAN;
1634
1635 if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4)
1636 *size = LTT_LP32;
1637 else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4)
1638 *size = LTT_ILP32;
1639 else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8)
1640 *size = LTT_LP64;
1641 else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8)
1642 *size = LTT_ILP64;
1643 else *size = LTT_UNKNOWN;
1644 }
1645
1646 /* get the node name of the system */
1647
1648 char * ltt_trace_system_description_node_name (LttSystemDescription * s)
1649 {
1650 return s->node_name;
1651 }
1652
1653
1654 /* get the domain name of the system */
1655
1656 char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
1657 {
1658 return s->domain_name;
1659 }
1660
1661
1662 /* get the description of the system */
1663
1664 char * ltt_trace_system_description_description (LttSystemDescription * s)
1665 {
1666 return s->description;
1667 }
1668
1669
1670 /* get the start time of the trace */
1671
1672 LttTime ltt_trace_system_description_trace_start_time(LttSystemDescription *s)
1673 {
1674 return s->trace_start;
1675 }
1676
1677
1678 LttTracefile *ltt_tracefile_new()
1679 {
1680 return g_new(LttTracefile, 1);
1681 }
1682
1683 void ltt_tracefile_destroy(LttTracefile *tf)
1684 {
1685 g_free(tf);
1686 }
1687
1688 void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
1689 {
1690 *dest = *src;
1691 }
1692
This page took 0.068488 seconds and 4 git commands to generate.