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