convert now only updates adaptation TSC on heartbeat, block start and block end events
[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
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 unsigned int num = t->facility_number;
685 LttFacility * f;
686
687 for(i=0;i<num;i++){
688 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
689 count += f->event_number;
690 }
691 return count;
692 }
693
694 /* FIXME : performances could be improved with a better design for this
695 * function : sequential search through a container has never been the
696 * best on the critical path. */
697 LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id)
698 {
699 LttFacility * facility = NULL;
700 unsigned int i;
701 unsigned int num = trace->facility_number;
702 GPtrArray *facilities = trace->facilities;
703
704 for(i=0;unlikely(i<num);){
705 LttFacility *iter_facility =
706 (LttFacility*) g_ptr_array_index(facilities,i);
707 unsigned base_id = iter_facility->base_id;
708
709 if(likely(id >= base_id &&
710 id < base_id + iter_facility->event_number)) {
711 facility = iter_facility;
712 break;
713 } else {
714 i++;
715 }
716 }
717
718 return facility;
719 }
720
721 LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
722 {
723 LttEventType *event_type;
724
725 LttFacility * f;
726 f = ltt_trace_facility_by_id(t,evId);
727
728 if(unlikely(!f)) event_type = NULL;
729 else event_type = f->events[evId - f->base_id];
730
731 return event_type;
732 }
733
734 /*****************************************************************************
735 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
736 *the maximum number of CPU in the system. When the number of CPU installed
737 *is less than the maximum, some positions are unused. There are also a
738 *number of "control" tracefiles (facilities, interrupts...).
739 ****************************************************************************/
740 unsigned ltt_trace_control_tracefile_number(LttTrace *t)
741 {
742 return t->control_tracefile_number;
743 }
744
745 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace *t)
746 {
747 return t->per_cpu_tracefile_number;
748 }
749
750 /*****************************************************************************
751 *It is possible to search for the tracefiles by name or by CPU position.
752 *The index within the tracefiles of the same type is returned if found
753 *and a negative value otherwise.
754 ****************************************************************************/
755
756 int ltt_trace_control_tracefile_find(LttTrace *t, const gchar *name)
757 {
758 LttTracefile * tracefile;
759 unsigned int i;
760 for(i=0;i<t->control_tracefile_number;i++){
761 tracefile = (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
762 if(strcmp(tracefile->name, name)==0)break;
763 }
764 if(i == t->control_tracefile_number) return -1;
765 return i;
766 }
767
768 /* not really useful. We just have to know that cpu tracefiles
769 * comes before control tracefiles.
770 */
771 int ltt_trace_per_cpu_tracefile_find(LttTrace *t, const gchar *name)
772 {
773 LttTracefile * tracefile;
774 unsigned int i;
775 for(i=0;i<t->per_cpu_tracefile_number;i++){
776 tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
777 if(strcmp(tracefile->name, name)==0)break;
778 }
779 if(i == t->per_cpu_tracefile_number) return -1;
780 return i;
781 }
782
783 /*****************************************************************************
784 *Get a specific tracefile
785 ****************************************************************************/
786
787 LttTracefile *ltt_trace_control_tracefile_get(LttTrace *t, unsigned i)
788 {
789 return (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
790 }
791
792 LttTracefile *ltt_trace_per_cpu_tracefile_get(LttTrace *t, unsigned i)
793 {
794 return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
795 }
796
797 /*****************************************************************************
798 * Get the start time and end time of the trace
799 ****************************************************************************/
800
801 void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
802 {
803 LttTime startSmall, startTmp, endBig, endTmp;
804 unsigned int i, j=0;
805 LttTracefile * tf;
806
807 for(i=0;i<t->control_tracefile_number;i++){
808 tf = g_ptr_array_index(t->control_tracefiles, i);
809 readBlock(tf,1);
810 startTmp = tf->a_block_start->time;
811 readBlock(tf,tf->block_number);
812 endTmp = tf->a_block_end->time;
813 if(i==0){
814 startSmall = startTmp;
815 endBig = endTmp;
816 j = 1;
817 continue;
818 }
819 if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp;
820 if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp;
821 }
822
823 for(i=0;i<t->per_cpu_tracefile_number;i++){
824 tf = g_ptr_array_index(t->per_cpu_tracefiles, i);
825 readBlock(tf,1);
826 startTmp = tf->a_block_start->time;
827 readBlock(tf,tf->block_number);
828 endTmp = tf->a_block_end->time;
829 if(j == 0 && i==0){
830 startSmall = startTmp;
831 endBig = endTmp;
832 continue;
833 }
834 if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp;
835 if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp;
836 }
837
838 if(start != NULL) *start = startSmall;
839 if(end != NULL) *end = endBig;
840 }
841
842
843 /*****************************************************************************
844 *Get the name of a tracefile
845 ****************************************************************************/
846
847 char *ltt_tracefile_name(LttTracefile *tf)
848 {
849 return tf->name;
850 }
851
852 /*****************************************************************************
853 * Get the number of blocks in the tracefile
854 ****************************************************************************/
855
856 unsigned ltt_tracefile_block_number(LttTracefile *tf)
857 {
858 return tf->block_number;
859 }
860
861 /*****************************************************************************
862 *Function name
863 * ltt_tracefile_seek_time: seek to the first event of the trace with time
864 * larger or equal to time
865 *Input params
866 * t : tracefile
867 * time : criteria of the time
868 ****************************************************************************/
869 void ltt_tracefile_find_time_block(LttTracefile *t, LttTime time,
870 int start_block, int end_block)
871 {
872 int err, tmp_block, s, e;
873 int headTime;
874 int tailTime;
875
876 err=readBlock(t,start_block);
877 if(err) g_error("Can not read tracefile: %s\n", t->name);
878 if(start_block == end_block)return;
879
880 tailTime = ltt_time_compare(t->a_block_end->time, time);
881 if(tailTime >= 0) return;
882
883 err=readBlock(t,end_block);
884 if(err) g_error("Can not read tracefile: %s\n", t->name);
885 if(start_block+1 == end_block)return;
886
887 headTime = ltt_time_compare(t->a_block_start->time, time);
888 if(headTime <= 0 ) return;
889
890 tmp_block = (end_block + start_block)/2;
891 err=readBlock(t,tmp_block);
892 if(err) g_error("Can not read tracefile: %s\n", t->name);
893
894 headTime = ltt_time_compare(t->a_block_start->time, time);
895 tailTime = ltt_time_compare(t->a_block_end->time, time);
896 if(headTime <= 0 && tailTime >= 0) return;
897
898 if(headTime > 0){
899 s = start_block + 1;
900 e = tmp_block - 1;
901 if(s <= e)
902 ltt_tracefile_find_time_block(t, time, s, e);
903 else return;
904 }
905
906 if(tailTime < 0){
907 s = tmp_block + 1;
908 e = end_block - 1;
909 if(s <= e)
910 ltt_tracefile_find_time_block(t, time, s, e);
911 else return;
912 }
913 }
914
915 void ltt_tracefile_backward_find_time_block(LttTracefile *t, LttTime time)
916 {
917 int t_time, h_time, err;
918 err=readBlock(t,t->which_block-1);
919 if(err) g_error("Can not read tracefile: %s\n", t->name);
920 h_time = ltt_time_compare(t->a_block_start->time, time);
921 t_time = ltt_time_compare(t->a_block_end->time, time);
922 if(h_time == 0){
923 int tmp;
924 if(t->which_block == 1) return;
925 err=readBlock(t,t->which_block-1);
926 if(err) g_error("Can not read tracefile: %s\n", t->name);
927 tmp = ltt_time_compare(t->a_block_end->time, time);
928 if(tmp == 0) return ltt_tracefile_seek_time(t, time);
929 err=readBlock(t,t->which_block+1);
930 if(err) g_error("Can not read tracefile: %s\n", t->name);
931 }else if(h_time > 0){
932 ltt_tracefile_find_time_block(t, time, 1, t->which_block);
933 return ltt_tracefile_seek_time(t, time) ;
934 }else{
935 if(t_time >= 0) return ltt_tracefile_seek_time(t, time);
936 err=readBlock(t,t->which_block+1);
937 if(err) g_error("Can not read tracefile: %s\n", t->name);
938 }
939 }
940
941 void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
942 {
943 int err;
944 LttTime lttTime;
945 int headTime = ltt_time_compare(t->a_block_start->time, time);
946 int tailTime = ltt_time_compare(t->a_block_end->time, time);
947 LttEvent ev;
948
949 if(headTime < 0 && tailTime > 0){
950 if(ltt_time_compare(t->a_block_end->time, t->current_event_time) !=0) {
951 lttTime = getEventTime(t);
952 err = ltt_time_compare(lttTime, time);
953 if(err > 0){
954 if(t->which_event==2 || ltt_time_compare(t->prev_event_time,time)<0){
955 return;
956 }else{
957 updateTracefile(t);
958 return ltt_tracefile_seek_time(t, time);
959 }
960 }else if(err < 0){
961 while(1){
962 if(ltt_tracefile_read(t,&ev) == NULL) {
963 g_print("End of file\n");
964 return;
965 }
966 lttTime = getEventTime(t);
967 err = ltt_time_compare(lttTime, time);
968 if(err >= 0)return;
969 }
970 }else return;
971 }else{//we are at the end of the block
972 updateTracefile(t);
973 return ltt_tracefile_seek_time(t, time);
974 }
975 }else if(headTime >= 0){
976 if(t->which_block == 1){
977 updateTracefile(t);
978 }else{
979 if(ltt_time_compare(t->prev_block_end_time, time) >= 0 ||
980 (t->prev_block_end_time.tv_sec == 0 &&
981 t->prev_block_end_time.tv_nsec == 0 )){
982 ltt_tracefile_backward_find_time_block(t, time);
983 }else{
984 updateTracefile(t);
985 }
986 }
987 }else if(tailTime < 0){
988 if(t->which_block != t->block_number){
989 ltt_tracefile_find_time_block(t, time, t->which_block+1, t->block_number);
990 return ltt_tracefile_seek_time(t, time);
991 }else {
992 t->cur_event_pos = t->buffer + t->block_size;
993 g_print("End of file\n");
994 return;
995 }
996 }else if(tailTime == 0){
997 t->cur_event_pos = t->last_event_pos;
998 t->current_event_time = time;
999 t->cur_heart_beat_number = 0;
1000 t->prev_event_time.tv_sec = 0;
1001 t->prev_event_time.tv_nsec = 0;
1002 return;
1003 }
1004 }
1005
1006 /*****************************************************************************
1007 * Seek to the first event with position equal or larger to ep
1008 *
1009 * Modified by Mathieu Desnoyers to used faster offset position instead of
1010 * re-reading the whole buffer.
1011 ****************************************************************************/
1012
1013 void ltt_tracefile_seek_position(LttTracefile *t, const LttEventPosition *ep)
1014 {
1015 //if we are at the right place, just return
1016 if(likely(t->which_block == ep->block_num && t->which_event == ep->event_num))
1017 return;
1018
1019 if(likely(t->which_block == ep->block_num)) updateTracefile(t);
1020 else readBlock(t,ep->block_num);
1021 //event offset is available
1022 if(likely(ep->old_position)){
1023 int err;
1024
1025 t->which_event = ep->event_num;
1026 t->cur_event_pos = t->buffer + ep->event_offset;
1027 t->prev_event_time = ep->event_time;
1028 t->current_event_time = ep->event_time;
1029 t->cur_heart_beat_number = ep->heart_beat_number;
1030 t->cur_cycle_count = ep->event_cycle_count;
1031
1032 /* This is a workaround for fast position seek */
1033 t->last_event_pos = ep->last_event_pos;
1034 t->prev_block_end_time = ep->prev_block_end_time;
1035 t->prev_event_time = ep->prev_event_time;
1036 t->pre_cycle_count = ep->pre_cycle_count;
1037 t->count = ep->count;
1038 t->overflow_nsec = ep->overflow_nsec;
1039 t->last_heartbeat = ep->last_heartbeat;
1040 /* end of workaround */
1041
1042 //update the fields of the current event and go to the next event
1043 err = skipEvent(t);
1044 if(unlikely(err == ERANGE)) g_error("event id is out of range\n");
1045
1046 return;
1047 }
1048
1049 //only block number and event index are available
1050 //MD: warning : this is slow!
1051 g_warning("using slow O(n) tracefile seek position");
1052
1053 LttEvent event;
1054 while(likely(t->which_event < ep->event_num)) ltt_tracefile_read(t, &event);
1055
1056 return;
1057 }
1058
1059 /*****************************************************************************
1060 *Function name
1061 * ltt_tracefile_read : read the current event, set the pointer to the next
1062 *Input params
1063 * t : tracefile
1064 *Return value
1065 * LttEvent * : an event to be processed
1066 ****************************************************************************/
1067
1068 LttEvent *ltt_tracefile_read(LttTracefile *t, LttEvent *event)
1069 {
1070 int err;
1071
1072 if(unlikely(t->cur_event_pos == t->buffer + t->block_size)){
1073 if(unlikely(t->which_block == t->block_number)){
1074 return NULL;
1075 }
1076 err = readBlock(t, t->which_block + 1);
1077 if(unlikely(err))g_error("Can not read tracefile");
1078 }
1079
1080 event->event_id = (int)(*(guint16 *)(t->cur_event_pos));
1081 if(unlikely(event->event_id == TRACE_TIME_HEARTBEAT))
1082 t->cur_heart_beat_number++;
1083
1084 t->prev_event_time = t->current_event_time;
1085 // t->current_event_time = getEventTime(t);
1086
1087 event->time_delta = *(guint32 *)(t->cur_event_pos + EVENT_ID_SIZE);
1088 event->event_time = t->current_event_time;
1089 event->event_cycle_count = t->cur_cycle_count;
1090
1091 event->tracefile = t;
1092 event->data = t->cur_event_pos + EVENT_HEADER_SIZE;
1093 event->which_block = t->which_block;
1094 event->which_event = t->which_event;
1095
1096 /* This is a workaround for fast position seek */
1097 event->last_event_pos = t->last_event_pos;
1098 event->prev_block_end_time = t->prev_block_end_time;
1099 event->prev_event_time = t->prev_event_time;
1100 event->pre_cycle_count = t->pre_cycle_count;
1101 event->count = t->count;
1102 event->overflow_nsec = t->overflow_nsec;
1103 event->last_heartbeat = t->last_heartbeat;
1104
1105 /* end of workaround */
1106
1107
1108
1109 //update the fields of the current event and go to the next event
1110 err = skipEvent(t);
1111 if(unlikely(err == ERANGE)) g_error("event id is out of range\n");
1112
1113 return event;
1114 }
1115
1116 /****************************************************************************
1117 *Function name
1118 * readFile : wrap function to read from a file
1119 *Input Params
1120 * fd : file descriptor
1121 * buf : buf to contain the content
1122 * size : number of bytes to be read
1123 * mesg : message to be printed if some thing goes wrong
1124 *return value
1125 * 0 : success
1126 * EIO : can not read from the file
1127 ****************************************************************************/
1128
1129 int readFile(int fd, void * buf, size_t size, char * mesg)
1130 {
1131 ssize_t nbBytes = read(fd, buf, size);
1132
1133 if((size_t)nbBytes != size) {
1134 if(nbBytes < 0) {
1135 perror("Error in readFile : ");
1136 } else {
1137 g_warning("%s",mesg);
1138 }
1139 return EIO;
1140 }
1141 return 0;
1142 }
1143
1144 /*****************************************************************************
1145 *Function name
1146 * skipEvent_pre_read_cycles : go to the next event,
1147 * update the necessary fields of the current event
1148 * increment the cycle counter, save it at the end.
1149 *Input params
1150 * t : tracefile
1151 *return value
1152 * 0 : success
1153 * ERANGE : event id is out of range
1154 ****************************************************************************/
1155 #if 0
1156 int skipEvent_pre_read_cycles(LttTracefile * t)
1157 {
1158 int evId;
1159 void * evData;
1160 LttEventType * evT;
1161 LttField * rootFld;
1162
1163 evId = (int)(*(guint16 *)(t->cur_event_pos));
1164 evData = t->cur_event_pos + EVENT_HEADER_SIZE;
1165
1166 evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
1167
1168 if(likely(evT)) rootFld = evT->root_field;
1169 else return ERANGE;
1170
1171 if(likely(rootFld)){
1172 //event has string/sequence or the last event is not the same event
1173 if(likely((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event)
1174 && rootFld->field_fixed == 0)){
1175 setFieldsOffset(t, evT, evData, t->trace);
1176 }
1177 t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
1178 }else t->cur_event_pos += EVENT_HEADER_SIZE;
1179
1180 //evT->latest_block = t->which_block;
1181 //evT->latest_event = t->which_event;
1182
1183 //the next event is in the next block
1184 //if(unlikely(evId == TRACE_BLOCK_END)){
1185 // Specify end of buffer reached.
1186 // t->cur_event_pos = t->buffer + t->block_size;
1187 //}else{
1188 //g_critical("COUNT : %lu", t->cur_cycle_count);
1189 //t->which_event++;
1190 // t->current_event_time = getEventTime(t);
1191 //}
1192
1193 return 0;
1194 }
1195 #endif //0
1196
1197
1198
1199 /*****************************************************************************
1200 *Function name
1201 * ltt_tracefile_pre_read_cycles :
1202 * read the current event, increment the cycle counter
1203 *Input params
1204 * t : tracefile
1205 *Return value
1206 * False : end of bloc reached
1207 ****************************************************************************/
1208 #if 0
1209 gboolean ltt_tracefile_pre_read_cycles(LttTracefile *tf)
1210 {
1211 int err;
1212 //LttEvent event;
1213
1214 // if(unlikely(t->cur_event_pos == t->buffer + t->block_size)){
1215 //if(unlikely(t->which_block == t->block_number)){
1216 // return FALSE;
1217 //}
1218 // return FALSE; // end of bloc reached
1219 //err = readBlock(t, t->which_block + 1);
1220 //if(unlikely(err))g_error("Can not read tracefile");
1221 //}
1222
1223 //event.event_id = (int)(*(guint16 *)(t->cur_event_pos));
1224 //if(unlikely(event.event_id == TRACE_TIME_HEARTBEAT))
1225 // t->cur_heart_beat_number++;
1226
1227 //t->prev_event_time = t->current_event_time;
1228 // t->current_event_time = getEventTime(t);
1229
1230 //event.time_delta = *(guint32 *)(t->cur_event_pos + EVENT_ID_SIZE);
1231 //event.event_time = t->current_event_time;
1232 //event.event_cycle_count = t->cur_cycle_count;
1233
1234 //event.tracefile = t;
1235 //event.data = t->cur_event_pos + EVENT_HEADER_SIZE;
1236 //event.which_block = t->which_block;
1237 //event.which_event = t->which_event;
1238
1239 /* This is a workaround for fast position seek */
1240 //event.last_event_pos = t->last_event_pos;
1241 //event.prev_block_end_time = t->prev_block_end_time;
1242 //event.prev_event_time = t->prev_event_time;
1243 //event.pre_cycle_count = t->pre_cycle_count;
1244 //event.count = t->count;
1245 //event.last_heartbeat = t->last_heartbeat;
1246 /* end of workaround */
1247
1248
1249 /* Increment the cycle counter for the bloc */
1250 LttTime time;
1251 LttCycleCount cycle_count; // cycle count for the current event
1252 LttCycleCount lEventTotalCycle; // Total cycles from start for event
1253 LttCycleCount lEventNSec; // Total nsecs from start for event
1254 LttTime lTimeOffset; // Time offset in struct LttTime
1255 guint16 evId;
1256
1257 evId = *(guint16 *)tf->cur_event_pos;
1258
1259 // Calculate total time in cycles from start of buffer for this event
1260 cycle_count = (LttCycleCount)*(guint32 *)(tf->cur_event_pos + EVENT_ID_SIZE);
1261 //g_debug("event cycle count %llu", cycle_count);
1262 //
1263 //gint64 delta_count = (gint64)(cycle_count - tf->pre_cycle_count);
1264 //LttCycleCount res_delta_count;
1265 gboolean comp_count = cycle_count < tf->pre_cycle_count;
1266 tf->pre_cycle_count = cycle_count;
1267
1268 if(unlikely(comp_count)) {
1269 /* Wrapped */
1270 tf->count++; //increment wrap count
1271 }
1272
1273 //if(unlikely(cycle_count < tf->pre_cycle_count)) tf->count++;
1274 //if(unlikely(delta_count < 0)) {
1275 // tf->count++; //increment wrap count
1276 // keep in mind that delta_count is negative here.
1277 // res_delta_count = delta_count + 0x100000000ULL ;
1278 //} else
1279 // res_delta_count = (LttCycleCount)delta_count;
1280
1281 //cycle_count += (LttCycleCount)tf->count << 32;
1282
1283 //FIXME (MD)
1284 // if(tf->cur_heart_beat_number > tf->count)
1285 // cycle_count += (tf->cur_heart_beat_number - tf->count) << 32;
1286
1287 //tf->cur_cycle_count = tf->cur_cycle_count + res_delta_count;
1288 tf->cur_cycle_count = cycle_count | ((LttCycleCount)tf->count << 32);
1289 //g_debug("cur cycle count %llu", tf->cur_cycle_count);
1290
1291
1292
1293
1294 if(unlikely(evId == TRACE_BLOCK_START)){
1295 //g_debug("BLOCK START");
1296 }else if(unlikely(evId == TRACE_BLOCK_END)){
1297 //g_debug("BLOCK END");
1298
1299 /* The goal of all this pre reading */
1300 tf->a_block_end->cycle_count = tf->cur_cycle_count;
1301 //g_debug("end of block cycle count : %llu", tf->cur_cycle_count);
1302
1303 return FALSE;
1304 }
1305
1306 //update the fields of the current event and go to the next event
1307 err = skipEvent_pre_read_cycles(tf);
1308 if(unlikely(err == ERANGE)) g_error("event id is out of range\n");
1309
1310
1311 return TRUE;
1312 }
1313 #endif //0
1314
1315 /****************************************************************************
1316 *Function name
1317 * readBlock : read a block from the file
1318 *Input Params
1319 * lttdes : ltt trace file
1320 * whichBlock : the block which will be read
1321 *return value
1322 * 0 : success
1323 * EINVAL : lseek fail
1324 * EIO : can not read from the file
1325 ****************************************************************************/
1326
1327 int readBlock(LttTracefile * tf, int whichBlock)
1328 {
1329 off_t nbBytes;
1330 guint32 lostSize;
1331
1332 /* same block already opened requested */
1333 if((guint)whichBlock == tf->which_block) return 0;
1334
1335 if(likely(whichBlock - tf->which_block == 1 && tf->which_block != 0)){
1336 tf->prev_block_end_time = tf->a_block_end->time;
1337 tf->prev_event_time = tf->a_block_end->time;
1338 }else{
1339 tf->prev_block_end_time.tv_sec = 0;
1340 tf->prev_block_end_time.tv_nsec = 0;
1341 tf->prev_event_time.tv_sec = 0;
1342 tf->prev_event_time.tv_nsec = 0;
1343 }
1344
1345 nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET);
1346 if(unlikely(nbBytes == -1)) return EINVAL;
1347
1348 if(unlikely(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block")))
1349 return EIO;
1350
1351 tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE);
1352 lostSize = *(guint32 *)(tf->buffer + tf->block_size - sizeof(guint32));
1353 tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size
1354 - sizeof(guint32) - lostSize - sizeof(BlockEnd));
1355 tf->last_event_pos = tf->buffer + tf->block_size -
1356 sizeof(guint32) - lostSize
1357 - sizeof(BlockEnd) - EVENT_HEADER_SIZE;
1358
1359 tf->which_block = whichBlock;
1360 tf->which_event = 1;
1361 tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev
1362 tf->cur_heart_beat_number = 0;
1363 tf->last_heartbeat = NULL;
1364
1365 /* read the whole block to precalculate total of cycles in it */
1366 tf->count = 0;
1367 tf->pre_cycle_count = 0;
1368 tf->cur_cycle_count = 0;
1369 //g_debug("precalculating cycles begin for block %i", whichBlock);
1370 /* End of block event already has 64 bits cycle counter! */
1371 //while(likely(ltt_tracefile_pre_read_cycles(tf)));
1372 /* Rough approximation of cycles per usec to calculate
1373 * the real block start and end time.
1374 */
1375 getCyclePerNsec(tf);
1376 /* we are at end position, make end time more precise */
1377 /* Start overflow_nsec to a negative value : takes account of the
1378 * start of block cycle counter */
1379 //tf->overflow_nsec = (-((double)tf->a_block_start->cycle_count)
1380 // * tf->nsec_per_cycle);
1381 /* put back the numbers corresponding to end time */
1382 //tf->overflow_nsec += tf->one_overflow_nsec * tf->count;
1383
1384 //tf->a_block_end->time = getEventTime(tf);
1385
1386 /* Make start time more precise */
1387 /* Start overflow_nsec to a negative value : takes account of the
1388 * start of block cycle counter */
1389 tf->overflow_nsec = (-((double)(tf->a_block_start->cycle_count&0xFFFFFFFF))
1390 * tf->nsec_per_cycle);
1391
1392 tf->a_block_start->time = getEventTime(tf);
1393
1394 {
1395 guint64 lEventNSec;
1396 LttTime lTimeOffset;
1397 /* End time more precise */
1398 lEventNSec = ((double)
1399 (tf->a_block_end->cycle_count - tf->a_block_start->cycle_count)
1400 * tf->nsec_per_cycle);
1401 //g_assert(lEventNSec >= 0);
1402 lTimeOffset = ltt_time_from_uint64(lEventNSec);
1403 tf->a_block_end->time = ltt_time_add(tf->a_block_start->time, lTimeOffset);
1404 }
1405
1406
1407 //g_debug("precalculating cycles end for block %i", whichBlock);
1408
1409 #if 0
1410 /* put back pointer at the beginning */
1411 tf->count = 0;
1412 tf->pre_cycle_count = 0;
1413 tf->cur_cycle_count = 0;
1414 tf->which_event = 1;
1415 tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev
1416 tf->cur_heart_beat_number = 0;
1417 tf->last_heartbeat = NULL;
1418 #endif //0
1419
1420 /* recalculate the cycles per nsec, with now more precise start and end time
1421 */
1422 getCyclePerNsec(tf);
1423
1424 tf->overflow_nsec = (-((double)(tf->a_block_start->cycle_count&0xFFFFFFFF))
1425 * tf->nsec_per_cycle);
1426
1427 tf->current_event_time = getEventTime(tf);
1428
1429 return 0;
1430 }
1431
1432 /*****************************************************************************
1433 *Function name
1434 * updateTracefile : reinitialize the info of the block which is already
1435 * in the buffer
1436 *Input params
1437 * tf : tracefile
1438 ****************************************************************************/
1439
1440 void updateTracefile(LttTracefile * tf)
1441 {
1442 tf->which_event = 1;
1443 tf->cur_event_pos = tf->buffer;
1444 tf->current_event_time = getEventTime(tf);
1445 tf->cur_heart_beat_number = 0;
1446
1447 tf->prev_event_time.tv_sec = 0;
1448 tf->prev_event_time.tv_nsec = 0;
1449 tf->count = 0;
1450
1451 tf->overflow_nsec = (-((double)tf->a_block_start->cycle_count)
1452 * tf->nsec_per_cycle);
1453
1454 }
1455
1456 /*****************************************************************************
1457 *Function name
1458 * skipEvent : go to the next event, update the fields of the current event
1459 *Input params
1460 * t : tracefile
1461 *return value
1462 * 0 : success
1463 * ERANGE : event id is out of range
1464 ****************************************************************************/
1465
1466 int skipEvent(LttTracefile * t)
1467 {
1468 int evId;
1469 void * evData;
1470 LttEventType * evT;
1471 LttField * rootFld;
1472
1473 evId = (int)(*(guint16 *)(t->cur_event_pos));
1474 evData = t->cur_event_pos + EVENT_HEADER_SIZE;
1475
1476 evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
1477
1478 if(likely(evT)) rootFld = evT->root_field;
1479 else return ERANGE;
1480
1481 if(likely(rootFld)){
1482 //event has string/sequence or the last event is not the same event
1483 if(likely((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event)
1484 && rootFld->field_fixed == 0)){
1485 setFieldsOffset(t, evT, evData, t->trace);
1486 }
1487 t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
1488 }else t->cur_event_pos += EVENT_HEADER_SIZE;
1489
1490 evT->latest_block = t->which_block;
1491 evT->latest_event = t->which_event;
1492
1493 //the next event is in the next block
1494 if(unlikely(evId == TRACE_BLOCK_END)){
1495 t->cur_event_pos = t->buffer + t->block_size;
1496 }else{
1497 t->which_event++;
1498 t->current_event_time = getEventTime(t);
1499 }
1500
1501 return 0;
1502 }
1503
1504
1505 /*****************************************************************************
1506 *Function name
1507 * getCyclePerNsec : calculate cycles per nsec for current block
1508 * MD: should have tracefile_read the whole block, so we know the
1509 * total of cycles in it before being called.
1510 *Input Params
1511 * t : tracefile
1512 ****************************************************************************/
1513
1514 void getCyclePerNsec(LttTracefile * t)
1515 {
1516 LttTime lBufTotalTime; /* Total time for this buffer */
1517 double lBufTotalNSec; /* Total time for this buffer in nsecs */
1518 LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */
1519
1520 /* Calculate the total time for this buffer */
1521 lBufTotalTime = ltt_time_sub(t->a_block_end->time, t->a_block_start->time);
1522
1523 /* Calculate the total cycles for this bufffer */
1524 lBufTotalCycle = t->a_block_end->cycle_count;
1525 lBufTotalCycle -= t->a_block_start->cycle_count;
1526
1527 /* Convert the total time to double */
1528 lBufTotalNSec = ltt_time_to_double(lBufTotalTime);
1529
1530 t->nsec_per_cycle = (double)lBufTotalNSec / (double)lBufTotalCycle;
1531
1532 /* Pre-multiply one overflow (2^32 cycles) by nsec_per_cycle */
1533 t->one_overflow_nsec = t->nsec_per_cycle * (double)0x100000000ULL;
1534
1535 /* See : http://www.azillionmonkeys.com/qed/adiv.html */
1536 // precalculate the reciprocal, so divisions will be really fast.
1537 // 2^32-1 == 0xFFFFFFFFULL
1538 //{
1539 // double int_res = lBufTotalCycle/lBufTotalNSec;
1540 // t->cycles_per_nsec_reciprocal =
1541 // ((0xFFFF+int_res)/int_res);
1542 //}
1543
1544 }
1545
1546 /****************************************************************************
1547 *Function name
1548 * getEventTime : obtain the time of an event
1549 * NOTE : this function _really_ is on critical path.
1550 *Input params
1551 * tf : tracefile
1552 *Return value
1553 * LttTime : the time of the event
1554 ****************************************************************************/
1555
1556 static inline LttTime getEventTime(LttTracefile * tf)
1557 {
1558 LttTime time;
1559 LttCycleCount cycle_count; // cycle count for the current event
1560 //LttCycleCount lEventTotalCycle; // Total cycles from start for event
1561 gint64 lEventNSec; // Total nsecs from start for event
1562 LttTime lTimeOffset; // Time offset in struct LttTime
1563 guint16 evId;
1564
1565 evId = *(guint16 *)tf->cur_event_pos;
1566 //if(unlikely(evId == TRACE_BLOCK_START)){
1567 // tf->count = 0;
1568 // tf->pre_cycle_count = 0;
1569 // tf->cur_cycle_count = tf->a_block_start->cycle_count;
1570 // return tf->a_block_start->time;
1571 //}//else if(unlikely(evId == TRACE_BLOCK_END)){
1572 //tf->count = 0;
1573 //tf->pre_cycle_count = 0;
1574 //tf->cur_cycle_count = tf->a_block_end->cycle_count;
1575 //return tf->a_block_end->time;
1576 //}
1577
1578 // Calculate total time in cycles from start of buffer for this event
1579
1580
1581
1582 //if(unlikely(cycle_count < tf->pre_cycle_count)) tf->count++;
1583 //if(unlikely(delta_count < 0)) {
1584 // tf->count++; //increment wrap count
1585 // keep in mind that delta_count is negative here.
1586 // res_delta_count = delta_count + 0x100000000ULL ;
1587 //} else
1588 // res_delta_count = (LttCycleCount)delta_count;
1589
1590 //cycle_count += (LttCycleCount)tf->count << 32;
1591
1592 //FIXME (MD)
1593 // if(tf->cur_heart_beat_number > tf->count)
1594 // cycle_count += (tf->cur_heart_beat_number - tf->count) << 32;
1595
1596 //tf->cur_cycle_count = tf->cur_cycle_count + res_delta_count;
1597 //
1598 //
1599 // Total cycle counter of the event.
1600 //tf->cur_cycle_count = cycle_count | ((LttCycleCount)tf->count << 32);
1601
1602 //g_debug("cur cycle count %llu", tf->cur_cycle_count);
1603
1604 // Total number of cycles since the beginning of the block
1605 //lEventTotalCycle = tf->cur_cycle_count
1606 // - tf->a_block_start->cycle_count;
1607
1608
1609
1610 #if 0
1611 // Calculate total time in cycles from start of buffer for this event
1612 cycle_count = (LttCycleCount)*(guint32 *)(tf->cur_event_pos + EVENT_ID_SIZE);
1613
1614 if(unlikely(cycle_count < tf->pre_cycle_count)) tf->count++;
1615 tf->pre_cycle_count = cycle_count;
1616 cycle_count += (LttCycleCount)tf->count << 32;
1617
1618 //FIXME (MD)
1619 // if(tf->cur_heart_beat_number > tf->count)
1620 // cycle_count += (tf->cur_heart_beat_number - tf->count) << 32;
1621
1622 tf->cur_cycle_count = cycle_count;
1623
1624 lEventTotalCycle = cycle_count;
1625 lEventTotalCycle -= tf->a_block_start->cycle_count;
1626 #endif //0
1627 // Convert it to nsecs
1628 //lEventNSec = (double)lEventTotalCycle * (double)tf->nsec_per_cycle;
1629 //lEventNSec = (tf->cycles_per_nsec_reciprocal * lEventTotalCycle) >> 16;
1630
1631 // Determine offset in struct LttTime
1632 //lTimeOffset = ltt_time_from_double(lEventNSec);
1633 //
1634 // We do not substract block start cycle count here, it has already been done
1635 // on the overflow_nsec
1636 // The result should never be negative, because the cycle count of
1637 // the event following the block start should be >= the previous one.
1638
1639 /* keep the overflow count correct. The heartbeat event makes sure
1640 * that we do not miss an overflow.*/
1641
1642 cycle_count = (LttCycleCount)*(guint32 *)(tf->cur_event_pos + EVENT_ID_SIZE);
1643 //g_debug("event cycle count %llu", cycle_count);
1644 //
1645 //gint64 delta_count = (gint64)(cycle_count - tf->pre_cycle_count);
1646 //LttCycleCount res_delta_count;
1647 gboolean comp_count = cycle_count < tf->pre_cycle_count;
1648 tf->pre_cycle_count = cycle_count;
1649
1650 if(unlikely(comp_count)) {
1651 /* Wrapped */
1652 tf->overflow_nsec += tf->one_overflow_nsec;
1653 tf->count++; //increment overflow count
1654 }
1655
1656 if(unlikely(evId == TRACE_BLOCK_START)) {
1657 lEventNSec = 0;
1658 } else if(unlikely(evId == TRACE_BLOCK_END)) {
1659 lEventNSec = ((double)
1660 (tf->a_block_end->cycle_count - tf->a_block_start->cycle_count)
1661 * tf->nsec_per_cycle);
1662 #if 0
1663 g_printf("CYCLES COUNTED : %llu",
1664 (gint64)((double)cycle_count * tf->nsec_per_cycle)
1665 +tf->overflow_nsec
1666 +tf->a_block_start->cycle_count);
1667 #endif //0
1668
1669 }
1670 #if 0
1671 else if(unlikely(evId == TRACE_TIME_HEARTBEAT)) {
1672
1673 tf->last_heartbeat = (TimeHeartbeat*)(tf->cur_event_pos+EVENT_HEADER_SIZE);
1674 lEventNSec = ((double)(tf->last_heartbeat->cycle_count
1675 - tf->a_block_start->cycle_count)
1676 * tf->nsec_per_cycle);
1677 }
1678 #endif //0
1679 else {
1680 lEventNSec = (gint64)((double)cycle_count * tf->nsec_per_cycle)
1681 +tf->overflow_nsec;
1682 }
1683
1684 //g_assert(lEventNSec >= 0);
1685 lTimeOffset = ltt_time_from_uint64(lEventNSec);
1686
1687 time = ltt_time_add(tf->a_block_start->time, lTimeOffset);
1688
1689 return time;
1690 }
1691
1692 /*****************************************************************************
1693 *Function name
1694 * setFieldsOffset : set offset of the fields
1695 *Input params
1696 * tracefile : opened trace file
1697 * evT : the event type
1698 * evD : event data, it may be NULL
1699 ****************************************************************************/
1700
1701 void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
1702 {
1703 LttField * rootFld = evT->root_field;
1704 // rootFld->base_address = evD;
1705
1706 if(likely(rootFld))
1707 rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);
1708 }
1709
1710 /*****************************************************************************
1711 *Function name
1712 * getFieldtypeSize: get the size of the field type (primitive type)
1713 *Input params
1714 * tracefile : opened trace file
1715 * evT : event type
1716 * offsetRoot : offset from the root
1717 * offsetParent : offset from the parrent
1718 * fld : field
1719 * evD : event data, it may be NULL
1720 *Return value
1721 * int : size of the field
1722 ****************************************************************************/
1723
1724 static inline gint getFieldtypeSize(LttTracefile * t,
1725 LttEventType * evT, gint offsetRoot,
1726 gint offsetParent, LttField * fld, void *evD, LttTrace *trace)
1727 {
1728 gint size, size1, element_number, i, offset1, offset2;
1729 LttType * type = fld->field_type;
1730
1731 if(unlikely(t && evT->latest_block==t->which_block &&
1732 evT->latest_event==t->which_event)){
1733 size = fld->field_size;
1734 goto end_getFieldtypeSize;
1735 } else {
1736 /* This likely has been tested with gcov : half of them.. */
1737 if(unlikely(fld->field_fixed == 1)){
1738 /* tested : none */
1739 if(unlikely(fld == evT->root_field)) {
1740 size = fld->field_size;
1741 goto end_getFieldtypeSize;
1742 }
1743 }
1744
1745 /* From gcov profiling : half string, half struct, can we gain something
1746 * from that ? (Mathieu) */
1747 switch(type->type_class) {
1748 case LTT_ARRAY:
1749 element_number = (int) type->element_number;
1750 if(fld->field_fixed == -1){
1751 size = getFieldtypeSize(t, evT, offsetRoot,
1752 0,fld->child[0], NULL, trace);
1753 if(size == 0){ //has string or sequence
1754 fld->field_fixed = 0;
1755 }else{
1756 fld->field_fixed = 1;
1757 size *= element_number;
1758 }
1759 }else if(fld->field_fixed == 0){// has string or sequence
1760 size = 0;
1761 for(i=0;i<element_number;i++){
1762 size += getFieldtypeSize(t, evT, offsetRoot+size,size,
1763 fld->child[0], evD+size, trace);
1764 }
1765 }else size = fld->field_size;
1766 if(unlikely(!evD)){
1767 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1768 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1769 }
1770
1771 break;
1772
1773 case LTT_SEQUENCE:
1774 size1 = (int) ltt_type_size(trace, type);
1775 if(fld->field_fixed == -1){
1776 fld->sequ_number_size = size1;
1777 fld->field_fixed = 0;
1778 size = getFieldtypeSize(t, evT, offsetRoot,
1779 0,fld->child[0], NULL, trace);
1780 fld->element_size = size;
1781 }else{//0: sequence
1782 element_number = getIntNumber(size1,evD);
1783 type->element_number = element_number;
1784 if(fld->element_size > 0){
1785 size = element_number * fld->element_size;
1786 }else{//sequence has string or sequence
1787 size = 0;
1788 for(i=0;i<element_number;i++){
1789 size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1,
1790 fld->child[0], evD+size+size1, trace);
1791 }
1792 }
1793 size += size1;
1794 }
1795 if(unlikely(!evD)){
1796 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1797 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1798 }
1799
1800 break;
1801
1802 case LTT_STRING:
1803 size = 0;
1804 if(fld->field_fixed == -1){
1805 fld->field_fixed = 0;
1806 }else{//0: string
1807 /* Hope my implementation is faster than strlen (Mathieu) */
1808 char *ptr=(char*)evD;
1809 size = 1;
1810 /* from gcov : many many strings are empty, make it the common case.*/
1811 while(unlikely(*ptr != '\0')) { size++; ptr++; }
1812 //size = ptr - (char*)evD + 1; //include end : '\0'
1813 }
1814 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1815 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1816
1817 break;
1818
1819 case LTT_STRUCT:
1820 element_number = (int) type->element_number;
1821 size = 0;
1822 /* tested with gcov */
1823 if(unlikely(fld->field_fixed == -1)){
1824 offset1 = offsetRoot;
1825 offset2 = 0;
1826 for(i=0;i<element_number;i++){
1827 size1=getFieldtypeSize(t, evT,offset1,offset2,
1828 fld->child[i], NULL, trace);
1829 if(likely(size1 > 0 && size >= 0)){
1830 size += size1;
1831 if(likely(offset1 >= 0)) offset1 += size1;
1832 offset2 += size1;
1833 }else{
1834 size = -1;
1835 offset1 = -1;
1836 offset2 = -1;
1837 }
1838 }
1839 if(unlikely(size == -1)){
1840 fld->field_fixed = 0;
1841 size = 0;
1842 }else fld->field_fixed = 1;
1843 }else if(likely(fld->field_fixed == 0)){
1844 offset1 = offsetRoot;
1845 offset2 = 0;
1846 for(i=0;unlikely(i<element_number);i++){
1847 size=getFieldtypeSize(t,evT,offset1,offset2,
1848 fld->child[i],evD+offset2, trace);
1849 offset1 += size;
1850 offset2 += size;
1851 }
1852 size = offset2;
1853 }else size = fld->field_size;
1854 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1855 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1856 break;
1857
1858 default:
1859 if(unlikely(fld->field_fixed == -1)){
1860 size = (int) ltt_type_size(trace, type);
1861 fld->field_fixed = 1;
1862 }else size = fld->field_size;
1863 if(unlikely(!evD)){
1864 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1865 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1866 }
1867 break;
1868 }
1869 }
1870
1871 fld->offset_root = offsetRoot;
1872 fld->offset_parent = offsetParent;
1873 fld->field_size = size;
1874
1875 end_getFieldtypeSize:
1876
1877 return size;
1878 }
1879
1880
1881 /*****************************************************************************
1882 *Function name
1883 * getIntNumber : get an integer number
1884 *Input params
1885 * size : the size of the integer
1886 * evD : the event data
1887 *Return value
1888 * gint64 : a 64 bits integer
1889 ****************************************************************************/
1890
1891 gint64 getIntNumber(int size, void *evD)
1892 {
1893 gint64 i;
1894
1895 switch(size) {
1896 case 1: i = *(gint8 *)evD; break;
1897 case 2: i = *(gint16 *)evD; break;
1898 case 4: i = *(gint32 *)evD; break;
1899 case 8: i = *(gint64 *)evD; break;
1900 default: i = *(gint64 *)evD;
1901 g_critical("getIntNumber : integer size %d unknown", size);
1902 break;
1903 }
1904
1905 #if 0
1906 if(size == 1) i = *(gint8 *)evD;
1907 else if(size == 2) i = *(gint16 *)evD;
1908 else if(size == 4) i = *(gint32 *)evD;
1909 else if(size == 8) i = *(gint64 *)evD;
1910 #endif //0
1911
1912 return (gint64)i;
1913 }
1914
1915 /*****************************************************************************
1916 *Function name
1917 * getDataEndianType : get the data type size and endian type of the local
1918 * machine
1919 *Input params
1920 * size : size of data type
1921 * endian : endian type, little or big
1922 ****************************************************************************/
1923
1924 void getDataEndianType(LttArchSize * size, LttArchEndian * endian)
1925 {
1926 int i = 1;
1927 char c = (char) i;
1928 int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *);
1929
1930 if(c == 1) *endian = LTT_LITTLE_ENDIAN;
1931 else *endian = LTT_BIG_ENDIAN;
1932
1933 if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4)
1934 *size = LTT_LP32;
1935 else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4)
1936 *size = LTT_ILP32;
1937 else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8)
1938 *size = LTT_LP64;
1939 else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8)
1940 *size = LTT_ILP64;
1941 else *size = LTT_UNKNOWN;
1942 }
1943
1944 /* get the node name of the system */
1945
1946 char * ltt_trace_system_description_node_name (LttSystemDescription * s)
1947 {
1948 return s->node_name;
1949 }
1950
1951
1952 /* get the domain name of the system */
1953
1954 char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
1955 {
1956 return s->domain_name;
1957 }
1958
1959
1960 /* get the description of the system */
1961
1962 char * ltt_trace_system_description_description (LttSystemDescription * s)
1963 {
1964 return s->description;
1965 }
1966
1967
1968 /* get the start time of the trace */
1969
1970 LttTime ltt_trace_system_description_trace_start_time(LttSystemDescription *s)
1971 {
1972 return s->trace_start;
1973 }
1974
1975
1976 LttTracefile *ltt_tracefile_new()
1977 {
1978 return g_new(LttTracefile, 1);
1979 }
1980
1981 void ltt_tracefile_destroy(LttTracefile *tf)
1982 {
1983 g_free(tf);
1984 }
1985
1986 void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
1987 {
1988 *dest = *src;
1989 }
1990
This page took 0.075167 seconds and 5 git commands to generate.