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