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