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