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