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