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