push back ltt_isalpha and ltt_isalnum, now static
[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;
685 unsigned int i;
686 for(i=0;i<trace->facility_number;i++){
687 facility = (LttFacility*) g_ptr_array_index(trace->facilities,i);
688 if(id >= facility->base_id &&
689 id < facility->base_id + facility->event_number)
690 break;
691 }
692 if(i==trace->facility_number) return NULL;
693 else return facility;
694 }
695
696 LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
697 {
698 LttFacility * f;
699 f = ltt_trace_facility_by_id(t,evId);
700 if(!f) return NULL;
701 return f->events[evId - f->base_id];
702 }
703
704 /*****************************************************************************
705 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
706 *the maximum number of CPU in the system. When the number of CPU installed
707 *is less than the maximum, some positions are unused. There are also a
708 *number of "control" tracefiles (facilities, interrupts...).
709 ****************************************************************************/
710 unsigned ltt_trace_control_tracefile_number(LttTrace *t)
711 {
712 return t->control_tracefile_number;
713 }
714
715 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace *t)
716 {
717 return t->per_cpu_tracefile_number;
718 }
719
720 /*****************************************************************************
721 *It is possible to search for the tracefiles by name or by CPU position.
722 *The index within the tracefiles of the same type is returned if found
723 *and a negative value otherwise.
724 ****************************************************************************/
725
726 int ltt_trace_control_tracefile_find(LttTrace *t, const gchar *name)
727 {
728 LttTracefile * tracefile;
729 unsigned int i;
730 for(i=0;i<t->control_tracefile_number;i++){
731 tracefile = (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
732 if(strcmp(tracefile->name, name)==0)break;
733 }
734 if(i == t->control_tracefile_number) return -1;
735 return i;
736 }
737
738 /* not really useful. We just have to know that cpu tracefiles
739 * comes before control tracefiles.
740 */
741 int ltt_trace_per_cpu_tracefile_find(LttTrace *t, const gchar *name)
742 {
743 LttTracefile * tracefile;
744 unsigned int i;
745 for(i=0;i<t->per_cpu_tracefile_number;i++){
746 tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
747 if(strcmp(tracefile->name, name)==0)break;
748 }
749 if(i == t->per_cpu_tracefile_number) return -1;
750 return i;
751 }
752
753 /*****************************************************************************
754 *Get a specific tracefile
755 ****************************************************************************/
756
757 LttTracefile *ltt_trace_control_tracefile_get(LttTrace *t, unsigned i)
758 {
759 return (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
760 }
761
762 LttTracefile *ltt_trace_per_cpu_tracefile_get(LttTrace *t, unsigned i)
763 {
764 return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
765 }
766
767 /*****************************************************************************
768 * Get the start time and end time of the trace
769 ****************************************************************************/
770
771 void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
772 {
773 LttTime startSmall, startTmp, endBig, endTmp;
774 unsigned int i, j=0;
775 LttTracefile * tf;
776
777 for(i=0;i<t->control_tracefile_number;i++){
778 tf = g_ptr_array_index(t->control_tracefiles, i);
779 readBlock(tf,1);
780 startTmp = tf->a_block_start->time;
781 readBlock(tf,tf->block_number);
782 endTmp = tf->a_block_end->time;
783 if(i==0){
784 startSmall = startTmp;
785 endBig = endTmp;
786 j = 1;
787 continue;
788 }
789 if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp;
790 if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp;
791 }
792
793 for(i=0;i<t->per_cpu_tracefile_number;i++){
794 tf = g_ptr_array_index(t->per_cpu_tracefiles, i);
795 readBlock(tf,1);
796 startTmp = tf->a_block_start->time;
797 readBlock(tf,tf->block_number);
798 endTmp = tf->a_block_end->time;
799 if(j == 0 && i==0){
800 startSmall = startTmp;
801 endBig = endTmp;
802 continue;
803 }
804 if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp;
805 if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp;
806 }
807
808 if(start != NULL) *start = startSmall;
809 if(end != NULL) *end = endBig;
810 }
811
812
813 /*****************************************************************************
814 *Get the name of a tracefile
815 ****************************************************************************/
816
817 char *ltt_tracefile_name(LttTracefile *tf)
818 {
819 return tf->name;
820 }
821
822 /*****************************************************************************
823 * Get the number of blocks in the tracefile
824 ****************************************************************************/
825
826 unsigned ltt_tracefile_block_number(LttTracefile *tf)
827 {
828 return tf->block_number;
829 }
830
831 /*****************************************************************************
832 *Function name
833 * ltt_tracefile_seek_time: seek to the first event of the trace with time
834 * larger or equal to time
835 *Input params
836 * t : tracefile
837 * time : criteria of the time
838 ****************************************************************************/
839 void ltt_tracefile_find_time_block(LttTracefile *t, LttTime time,
840 int start_block, int end_block)
841 {
842 int err, tmp_block, s, e;
843 int headTime;
844 int tailTime;
845
846 err=readBlock(t,start_block);
847 if(err) g_error("Can not read tracefile: %s\n", t->name);
848 if(start_block == end_block)return;
849
850 tailTime = ltt_time_compare(t->a_block_end->time, time);
851 if(tailTime >= 0) return;
852
853 err=readBlock(t,end_block);
854 if(err) g_error("Can not read tracefile: %s\n", t->name);
855 if(start_block+1 == end_block)return;
856
857 headTime = ltt_time_compare(t->a_block_start->time, time);
858 if(headTime <= 0 ) return;
859
860 tmp_block = (end_block + start_block)/2;
861 err=readBlock(t,tmp_block);
862 if(err) g_error("Can not read tracefile: %s\n", t->name);
863
864 headTime = ltt_time_compare(t->a_block_start->time, time);
865 tailTime = ltt_time_compare(t->a_block_end->time, time);
866 if(headTime <= 0 && tailTime >= 0) return;
867
868 if(headTime > 0){
869 s = start_block + 1;
870 e = tmp_block - 1;
871 if(s <= e)
872 ltt_tracefile_find_time_block(t, time, s, e);
873 else return;
874 }
875
876 if(tailTime < 0){
877 s = tmp_block + 1;
878 e = end_block - 1;
879 if(s <= e)
880 ltt_tracefile_find_time_block(t, time, s, e);
881 else return;
882 }
883 }
884
885 void ltt_tracefile_backward_find_time_block(LttTracefile *t, LttTime time)
886 {
887 int t_time, h_time, err;
888 err=readBlock(t,t->which_block-1);
889 if(err) g_error("Can not read tracefile: %s\n", t->name);
890 h_time = ltt_time_compare(t->a_block_start->time, time);
891 t_time = ltt_time_compare(t->a_block_end->time, time);
892 if(h_time == 0){
893 int tmp;
894 if(t->which_block == 1) return;
895 err=readBlock(t,t->which_block-1);
896 if(err) g_error("Can not read tracefile: %s\n", t->name);
897 tmp = ltt_time_compare(t->a_block_end->time, time);
898 if(tmp == 0) return ltt_tracefile_seek_time(t, time);
899 err=readBlock(t,t->which_block+1);
900 if(err) g_error("Can not read tracefile: %s\n", t->name);
901 }else if(h_time > 0){
902 ltt_tracefile_find_time_block(t, time, 1, t->which_block);
903 return ltt_tracefile_seek_time(t, time) ;
904 }else{
905 if(t_time >= 0) return ltt_tracefile_seek_time(t, time);
906 err=readBlock(t,t->which_block+1);
907 if(err) g_error("Can not read tracefile: %s\n", t->name);
908 }
909 }
910
911 void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
912 {
913 int err;
914 LttTime lttTime;
915 int headTime = ltt_time_compare(t->a_block_start->time, time);
916 int tailTime = ltt_time_compare(t->a_block_end->time, time);
917 LttEvent ev;
918
919 if(headTime < 0 && tailTime > 0){
920 if(ltt_time_compare(t->a_block_end->time, t->current_event_time) !=0) {
921 lttTime = getEventTime(t);
922 err = ltt_time_compare(lttTime, time);
923 if(err > 0){
924 if(t->which_event==2 || ltt_time_compare(t->prev_event_time,time)<0){
925 return;
926 }else{
927 updateTracefile(t);
928 return ltt_tracefile_seek_time(t, time);
929 }
930 }else if(err < 0){
931 while(1){
932 if(ltt_tracefile_read(t,&ev) == NULL) {
933 g_print("End of file\n");
934 return;
935 }
936 lttTime = getEventTime(t);
937 err = ltt_time_compare(lttTime, time);
938 if(err >= 0)return;
939 }
940 }else return;
941 }else{//we are at the end of the block
942 updateTracefile(t);
943 return ltt_tracefile_seek_time(t, time);
944 }
945 }else if(headTime >= 0){
946 if(t->which_block == 1){
947 updateTracefile(t);
948 }else{
949 if(ltt_time_compare(t->prev_block_end_time, time) >= 0 ||
950 (t->prev_block_end_time.tv_sec == 0 &&
951 t->prev_block_end_time.tv_nsec == 0 )){
952 ltt_tracefile_backward_find_time_block(t, time);
953 }else{
954 updateTracefile(t);
955 }
956 }
957 }else if(tailTime < 0){
958 if(t->which_block != t->block_number){
959 ltt_tracefile_find_time_block(t, time, t->which_block+1, t->block_number);
960 return ltt_tracefile_seek_time(t, time);
961 }else {
962 t->cur_event_pos = t->buffer + t->block_size;
963 g_print("End of file\n");
964 return;
965 }
966 }else if(tailTime == 0){
967 t->cur_event_pos = t->last_event_pos;
968 t->current_event_time = time;
969 t->cur_heart_beat_number = 0;
970 t->prev_event_time.tv_sec = 0;
971 t->prev_event_time.tv_nsec = 0;
972 return;
973 }
974 }
975
976 /*****************************************************************************
977 * Seek to the first event with position equal or larger to ep
978 *
979 * Modified by Mathieu Desnoyers to used faster offset position instead of
980 * re-reading the whole buffer.
981 ****************************************************************************/
982
983 void ltt_tracefile_seek_position(LttTracefile *t, const LttEventPosition *ep)
984 {
985 //if we are at the right place, just return
986 if(t->which_block == ep->block_num && t->which_event == ep->event_num)
987 return;
988
989 if(t->which_block == ep->block_num) updateTracefile(t);
990 else readBlock(t,ep->block_num);
991 //event offset is available
992 if(ep->old_position){
993 int err;
994
995 t->which_event = ep->event_num;
996 t->cur_event_pos = t->buffer + ep->event_offset;
997 t->prev_event_time = ep->event_time;
998 t->current_event_time = ep->event_time;
999 t->cur_heart_beat_number = ep->heart_beat_number;
1000 t->cur_cycle_count = ep->event_cycle_count;
1001
1002 /* This is a workaround for fast position seek */
1003 t->last_event_pos = ep->last_event_pos;
1004 t->prev_block_end_time = ep->prev_block_end_time;
1005 t->prev_event_time = ep->prev_event_time;
1006 t->pre_cycle_count = ep->pre_cycle_count;
1007 t->count = ep->count;
1008 /* end of workaround */
1009
1010 //update the fields of the current event and go to the next event
1011 err = skipEvent(t);
1012 if(err == ERANGE) g_error("event id is out of range\n");
1013
1014 return;
1015 }
1016
1017 //only block number and event index are available
1018 //MD: warning : this is slow!
1019 g_warning("using slow O(n) tracefile seek position");
1020
1021 LttEvent event;
1022 while(t->which_event < ep->event_num) ltt_tracefile_read(t, &event);
1023
1024 return;
1025 }
1026
1027 /*****************************************************************************
1028 *Function name
1029 * ltt_tracefile_read : read the current event, set the pointer to the next
1030 *Input params
1031 * t : tracefile
1032 *Return value
1033 * LttEvent * : an event to be processed
1034 ****************************************************************************/
1035
1036 LttEvent *ltt_tracefile_read(LttTracefile *t, LttEvent *event)
1037 {
1038 int err;
1039
1040 if(t->cur_event_pos == t->buffer + t->block_size){
1041 if(t->which_block == t->block_number){
1042 return NULL;
1043 }
1044 err = readBlock(t, t->which_block + 1);
1045 if(err)g_error("Can not read tracefile");
1046 }
1047
1048 event->event_id = (int)(*(guint16 *)(t->cur_event_pos));
1049 if(event->event_id == TRACE_TIME_HEARTBEAT)
1050 t->cur_heart_beat_number++;
1051
1052 t->prev_event_time = t->current_event_time;
1053 // t->current_event_time = getEventTime(t);
1054
1055 event->time_delta = *(guint32 *)(t->cur_event_pos + EVENT_ID_SIZE);
1056 event->event_time = t->current_event_time;
1057 event->event_cycle_count = t->cur_cycle_count;
1058
1059 event->tracefile = t;
1060 event->data = t->cur_event_pos + EVENT_HEADER_SIZE;
1061 event->which_block = t->which_block;
1062 event->which_event = t->which_event;
1063
1064 /* This is a workaround for fast position seek */
1065 event->last_event_pos = t->last_event_pos;
1066 event->prev_block_end_time = t->prev_block_end_time;
1067 event->prev_event_time = t->prev_event_time;
1068 event->pre_cycle_count = t->pre_cycle_count;
1069 event->count = t->count;
1070 /* end of workaround */
1071
1072
1073
1074 //update the fields of the current event and go to the next event
1075 err = skipEvent(t);
1076 if(err == ERANGE) g_error("event id is out of range\n");
1077
1078 return event;
1079 }
1080
1081 /****************************************************************************
1082 *Function name
1083 * readFile : wrap function to read from a file
1084 *Input Params
1085 * fd : file descriptor
1086 * buf : buf to contain the content
1087 * size : number of bytes to be read
1088 * mesg : message to be printed if some thing goes wrong
1089 *return value
1090 * 0 : success
1091 * EIO : can not read from the file
1092 ****************************************************************************/
1093
1094 int readFile(int fd, void * buf, size_t size, char * mesg)
1095 {
1096 ssize_t nbBytes = read(fd, buf, size);
1097
1098 if((size_t)nbBytes != size) {
1099 if(nbBytes < 0) {
1100 perror("Error in readFile : ");
1101 } else {
1102 g_warning("%s",mesg);
1103 }
1104 return EIO;
1105 }
1106 return 0;
1107 }
1108
1109 /****************************************************************************
1110 *Function name
1111 * readBlock : read a block from the file
1112 *Input Params
1113 * lttdes : ltt trace file
1114 * whichBlock : the block which will be read
1115 *return value
1116 * 0 : success
1117 * EINVAL : lseek fail
1118 * EIO : can not read from the file
1119 ****************************************************************************/
1120
1121 int readBlock(LttTracefile * tf, int whichBlock)
1122 {
1123 off_t nbBytes;
1124 guint32 lostSize;
1125
1126 if(whichBlock - tf->which_block == 1 && tf->which_block != 0){
1127 tf->prev_block_end_time = tf->a_block_end->time;
1128 tf->prev_event_time = tf->a_block_end->time;
1129 }else{
1130 tf->prev_block_end_time.tv_sec = 0;
1131 tf->prev_block_end_time.tv_nsec = 0;
1132 tf->prev_event_time.tv_sec = 0;
1133 tf->prev_event_time.tv_nsec = 0;
1134 }
1135
1136 nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET);
1137 if(nbBytes == -1) return EINVAL;
1138
1139 if(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block"))
1140 return EIO;
1141
1142 tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE);
1143 lostSize = *(guint32 *)(tf->buffer + tf->block_size - sizeof(guint32));
1144 tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size -
1145 lostSize + EVENT_HEADER_SIZE);
1146 tf->last_event_pos = tf->buffer + tf->block_size - lostSize;
1147
1148 tf->which_block = whichBlock;
1149 tf->which_event = 1;
1150 tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev
1151 tf->cur_heart_beat_number = 0;
1152
1153 getCyclePerNsec(tf);
1154
1155 tf->current_event_time = getEventTime(tf);
1156
1157 return 0;
1158 }
1159
1160 /*****************************************************************************
1161 *Function name
1162 * updateTracefile : reinitialize the info of the block which is already
1163 * in the buffer
1164 *Input params
1165 * tf : tracefile
1166 ****************************************************************************/
1167
1168 void updateTracefile(LttTracefile * tf)
1169 {
1170 tf->which_event = 1;
1171 tf->cur_event_pos = tf->buffer;
1172 tf->current_event_time = getEventTime(tf);
1173 tf->cur_heart_beat_number = 0;
1174
1175 tf->prev_event_time.tv_sec = 0;
1176 tf->prev_event_time.tv_nsec = 0;
1177 }
1178
1179 /*****************************************************************************
1180 *Function name
1181 * skipEvent : go to the next event, update the fields of the current event
1182 *Input params
1183 * t : tracefile
1184 *return value
1185 * 0 : success
1186 * ERANGE : event id is out of range
1187 ****************************************************************************/
1188
1189 int skipEvent(LttTracefile * t)
1190 {
1191 int evId;
1192 void * evData;
1193 LttEventType * evT;
1194 LttField * rootFld;
1195
1196 evId = (int)(*(guint16 *)(t->cur_event_pos));
1197 evData = t->cur_event_pos + EVENT_HEADER_SIZE;
1198
1199 evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
1200
1201 if(evT) rootFld = evT->root_field;
1202 else return ERANGE;
1203
1204 if(rootFld){
1205 //event has string/sequence or the last event is not the same event
1206 if((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event)
1207 && rootFld->field_fixed == 0){
1208 setFieldsOffset(t, evT, evData, t->trace);
1209 }
1210 t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
1211 }else t->cur_event_pos += EVENT_HEADER_SIZE;
1212
1213 evT->latest_block = t->which_block;
1214 evT->latest_event = t->which_event;
1215
1216 //the next event is in the next block
1217 if(evId == TRACE_BLOCK_END){
1218 t->cur_event_pos = t->buffer + t->block_size;
1219 }else{
1220 t->which_event++;
1221 t->current_event_time = getEventTime(t);
1222 }
1223
1224 return 0;
1225 }
1226
1227
1228 /*****************************************************************************
1229 *Function name
1230 * getCyclePerNsec : calculate cycles per nsec for current block
1231 *Input Params
1232 * t : tracefile
1233 ****************************************************************************/
1234
1235 void getCyclePerNsec(LttTracefile * t)
1236 {
1237 LttTime lBufTotalTime; /* Total time for this buffer */
1238 LttCycleCount lBufTotalNSec; /* Total time for this buffer in nsecs */
1239 LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */
1240
1241 /* Calculate the total time for this buffer */
1242 lBufTotalTime = ltt_time_sub(t->a_block_end->time, t->a_block_start->time);
1243
1244 /* Calculate the total cycles for this bufffer */
1245 lBufTotalCycle = t->a_block_end->cycle_count;
1246 lBufTotalCycle -= t->a_block_start->cycle_count;
1247
1248 /* Convert the total time to nsecs */
1249 lBufTotalNSec = lBufTotalTime.tv_sec;
1250 lBufTotalNSec *= NANOSECONDS_PER_SECOND;
1251 lBufTotalNSec += lBufTotalTime.tv_nsec;
1252
1253 t->cycle_per_nsec = (double)lBufTotalCycle / (double)lBufTotalNSec;
1254 }
1255
1256 /****************************************************************************
1257 *Function name
1258 * getEventTime : obtain the time of an event
1259 *Input params
1260 * tf : tracefile
1261 *Return value
1262 * LttTime : the time of the event
1263 ****************************************************************************/
1264
1265 LttTime getEventTime(LttTracefile * tf)
1266 {
1267 LttTime time;
1268 LttCycleCount cycle_count; // cycle count for the current event
1269 LttCycleCount lEventTotalCycle; // Total cycles from start for event
1270 LttCycleCount lEventNSec; // Total usecs from start for event
1271 LttTime lTimeOffset; // Time offset in struct LttTime
1272 guint16 evId;
1273 LttCycleCount tmpCycleCount = (((LttCycleCount)1)<<32);
1274
1275 evId = *(guint16 *)tf->cur_event_pos;
1276 if(evId == TRACE_BLOCK_START){
1277 tf->count = 0;
1278 tf->pre_cycle_count = 0;
1279 tf->cur_cycle_count = tf->a_block_start->cycle_count;
1280 return tf->a_block_start->time;
1281 }else if(evId == TRACE_BLOCK_END){
1282 tf->count = 0;
1283 tf->pre_cycle_count = 0;
1284 tf->cur_cycle_count = tf->a_block_end->cycle_count;
1285 return tf->a_block_end->time;
1286 }
1287
1288 // Calculate total time in cycles from start of buffer for this event
1289 cycle_count = (LttCycleCount)*(guint32 *)(tf->cur_event_pos + EVENT_ID_SIZE);
1290
1291 if(cycle_count < tf->pre_cycle_count)tf->count++;
1292 tf->pre_cycle_count = cycle_count;
1293 cycle_count += tmpCycleCount * tf->count;
1294
1295 // if(tf->cur_heart_beat_number > tf->count)
1296 // cycle_count += tmpCycleCount * (tf->cur_heart_beat_number - tf->count);
1297
1298 tf->cur_cycle_count = cycle_count;
1299
1300 lEventTotalCycle = cycle_count;
1301 lEventTotalCycle -= tf->a_block_start->cycle_count;
1302
1303 // Convert it to nsecs
1304 lEventNSec = (double)lEventTotalCycle / (double)tf->cycle_per_nsec;
1305
1306 // Determine offset in struct LttTime
1307 lTimeOffset.tv_nsec = lEventNSec % NANOSECONDS_PER_SECOND;
1308 lTimeOffset.tv_sec = lEventNSec / NANOSECONDS_PER_SECOND;
1309
1310 time = ltt_time_add(tf->a_block_start->time, lTimeOffset);
1311
1312 return time;
1313 }
1314
1315 /*****************************************************************************
1316 *Function name
1317 * setFieldsOffset : set offset of the fields
1318 *Input params
1319 * tracefile : opened trace file
1320 * evT : the event type
1321 * evD : event data, it may be NULL
1322 ****************************************************************************/
1323
1324 void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
1325 {
1326 LttField * rootFld = evT->root_field;
1327 // rootFld->base_address = evD;
1328
1329 if(rootFld)
1330 rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);
1331 }
1332
1333 /*****************************************************************************
1334 *Function name
1335 * getFieldtypeSize: get the size of the field type (primitive type)
1336 *Input params
1337 * tracefile : opened trace file
1338 * evT : event type
1339 * offsetRoot : offset from the root
1340 * offsetParent : offset from the parrent
1341 * fld : field
1342 * evD : event data, it may be NULL
1343 *Return value
1344 * int : size of the field
1345 ****************************************************************************/
1346
1347 int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot,
1348 int offsetParent, LttField * fld, void *evD, LttTrace *trace)
1349 {
1350 int size, size1, element_number, i, offset1, offset2;
1351 LttType * type = fld->field_type;
1352
1353 if(t){
1354 if(evT->latest_block==t->which_block && evT->latest_event==t->which_event){
1355 return fld->field_size;
1356 }
1357 }
1358
1359 if(fld->field_fixed == 1){
1360 if(fld == evT->root_field) return fld->field_size;
1361 }
1362
1363 if(type->type_class != LTT_STRUCT && type->type_class != LTT_ARRAY &&
1364 type->type_class != LTT_SEQUENCE && type->type_class != LTT_STRING){
1365 if(fld->field_fixed == -1){
1366 size = (int) ltt_type_size(trace, type);
1367 fld->field_fixed = 1;
1368 }else size = fld->field_size;
1369
1370 }else if(type->type_class == LTT_ARRAY){
1371 element_number = (int) type->element_number;
1372 if(fld->field_fixed == -1){
1373 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
1374 if(size == 0){ //has string or sequence
1375 fld->field_fixed = 0;
1376 }else{
1377 fld->field_fixed = 1;
1378 size *= element_number;
1379 }
1380 }else if(fld->field_fixed == 0){// has string or sequence
1381 size = 0;
1382 for(i=0;i<element_number;i++){
1383 size += getFieldtypeSize(t, evT, offsetRoot+size,size,
1384 fld->child[0], evD+size, trace);
1385 }
1386 }else size = fld->field_size;
1387
1388 }else if(type->type_class == 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,0,fld->child[0], NULL, trace);
1394 fld->element_size = size;
1395 }else{//0: sequence
1396 element_number = getIntNumber(size1,evD);
1397 type->element_number = element_number;
1398 if(fld->element_size > 0){
1399 size = element_number * fld->element_size;
1400 }else{//sequence has string or sequence
1401 size = 0;
1402 for(i=0;i<element_number;i++){
1403 size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1,
1404 fld->child[0], evD+size+size1, trace);
1405 }
1406 }
1407 size += size1;
1408 }
1409
1410 }else if(type->type_class == LTT_STRING){
1411 size = 0;
1412 if(fld->field_fixed == -1){
1413 fld->field_fixed = 0;
1414 }else{//0: string
1415 size = strlen((char*)evD) + 1; //include end : '\0'
1416 }
1417
1418 }else if(type->type_class == LTT_STRUCT){
1419 element_number = (int) type->element_number;
1420 size = 0;
1421 if(fld->field_fixed == -1){
1422 offset1 = offsetRoot;
1423 offset2 = 0;
1424 for(i=0;i<element_number;i++){
1425 size1=getFieldtypeSize(t, evT,offset1,offset2, fld->child[i], NULL, trace);
1426 if(size1 > 0 && size >= 0){
1427 size += size1;
1428 if(offset1 >= 0) offset1 += size1;
1429 offset2 += size1;
1430 }else{
1431 size = -1;
1432 offset1 = -1;
1433 offset2 = -1;
1434 }
1435 }
1436 if(size == -1){
1437 fld->field_fixed = 0;
1438 size = 0;
1439 }else fld->field_fixed = 1;
1440 }else if(fld->field_fixed == 0){
1441 offset1 = offsetRoot;
1442 offset2 = 0;
1443 for(i=0;i<element_number;i++){
1444 size=getFieldtypeSize(t,evT,offset1,offset2,fld->child[i],evD+offset2, trace);
1445 offset1 += size;
1446 offset2 += size;
1447 }
1448 size = offset2;
1449 }else size = fld->field_size;
1450 }
1451
1452 fld->offset_root = offsetRoot;
1453 fld->offset_parent = offsetParent;
1454 if(!evD){
1455 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1456 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1457 }
1458 fld->field_size = size;
1459
1460 return size;
1461 }
1462
1463
1464 /*****************************************************************************
1465 *Function name
1466 * getIntNumber : get an integer number
1467 *Input params
1468 * size : the size of the integer
1469 * evD : the event data
1470 *Return value
1471 * int : an integer
1472 ****************************************************************************/
1473
1474 int getIntNumber(int size, void *evD)
1475 {
1476 gint64 i;
1477 if(size == 1) i = *(gint8 *)evD;
1478 else if(size == 2) i = *(gint16 *)evD;
1479 else if(size == 4) i = *(gint32 *)evD;
1480 else if(size == 8) i = *(gint64 *)evD;
1481
1482 return (int) i;
1483 }
1484
1485 /*****************************************************************************
1486 *Function name
1487 * getDataEndianType : get the data type size and endian type of the local
1488 * machine
1489 *Input params
1490 * size : size of data type
1491 * endian : endian type, little or big
1492 ****************************************************************************/
1493
1494 void getDataEndianType(LttArchSize * size, LttArchEndian * endian)
1495 {
1496 int i = 1;
1497 char c = (char) i;
1498 int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *);
1499
1500 if(c == 1) *endian = LTT_LITTLE_ENDIAN;
1501 else *endian = LTT_BIG_ENDIAN;
1502
1503 if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4)
1504 *size = LTT_LP32;
1505 else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4)
1506 *size = LTT_ILP32;
1507 else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8)
1508 *size = LTT_LP64;
1509 else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8)
1510 *size = LTT_ILP64;
1511 else *size = LTT_UNKNOWN;
1512 }
1513
1514 /* get the node name of the system */
1515
1516 char * ltt_trace_system_description_node_name (LttSystemDescription * s)
1517 {
1518 return s->node_name;
1519 }
1520
1521
1522 /* get the domain name of the system */
1523
1524 char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
1525 {
1526 return s->domain_name;
1527 }
1528
1529
1530 /* get the description of the system */
1531
1532 char * ltt_trace_system_description_description (LttSystemDescription * s)
1533 {
1534 return s->description;
1535 }
1536
1537
1538 /* get the start time of the trace */
1539
1540 LttTime ltt_trace_system_description_trace_start_time(LttSystemDescription *s)
1541 {
1542 return s->trace_start;
1543 }
1544
1545
1546 LttTracefile *ltt_tracefile_new()
1547 {
1548 return g_new(LttTracefile, 1);
1549 }
1550
1551 void ltt_tracefile_destroy(LttTracefile *tf)
1552 {
1553 g_free(tf);
1554 }
1555
1556 void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
1557 {
1558 *dest = *src;
1559 }
1560
This page took 0.065818 seconds and 5 git commands to generate.