449cb9d7 |
1 | /* This file is part of the Linux Trace Toolkit viewer |
2 | * Copyright (C) 2003-2004 Xiangxiu Yang |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License Version 2 as |
6 | * published by the Free Software Foundation; |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License |
14 | * along with this program; if not, write to the Free Software |
15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
16 | * MA 02111-1307, USA. |
17 | */ |
18 | |
6cd62ccf |
19 | #include <stdio.h> |
20 | #include <fcntl.h> |
21 | #include <sys/stat.h> |
22 | #include <sys/types.h> |
963b5f2d |
23 | #include <dirent.h> |
6cd62ccf |
24 | #include <linux/errno.h> |
25 | |
6cd62ccf |
26 | #include "parser.h" |
a5dcde2f |
27 | #include <ltt/ltt.h> |
28 | #include "ltt-private.h" |
963b5f2d |
29 | #include <ltt/trace.h> |
a5dcde2f |
30 | #include <ltt/facility.h> |
6cd62ccf |
31 | |
963b5f2d |
32 | #define DIR_NAME_SIZE 256 |
6cd62ccf |
33 | |
34 | /* set the offset of the fields belonging to the event, |
35 | need the information of the archecture */ |
40331ba8 |
36 | void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace *t); |
6cd62ccf |
37 | |
38 | /* get the size of the field type according to the archtecture's |
39 | size and endian type(info of the archecture) */ |
40331ba8 |
40 | int getFieldtypeSize(LttTracefile * tf, LttEventType * evT, int offsetRoot, |
41 | int offsetParent, LttField *fld, void *evD, LttTrace* t); |
6cd62ccf |
42 | |
43 | /* read a fixed size or a block information from the file (fd) */ |
44 | int readFile(int fd, void * buf, size_t size, char * mesg); |
963b5f2d |
45 | int readBlock(LttTracefile * tf, int whichBlock); |
6cd62ccf |
46 | |
47 | /* calculate cycles per nsec for current block */ |
963b5f2d |
48 | void getCyclePerNsec(LttTracefile * t); |
6cd62ccf |
49 | |
50 | /* reinitialize the info of the block which is already in the buffer */ |
963b5f2d |
51 | void updateTracefile(LttTracefile * tf); |
6cd62ccf |
52 | |
53 | /* go to the next event */ |
963b5f2d |
54 | int skipEvent(LttTracefile * t); |
6cd62ccf |
55 | |
6cd62ccf |
56 | |
43da6a59 |
57 | /* Functions to parse system.xml file (using glib xml parser) */ |
58 | static void parser_start_element (GMarkupParseContext *context, |
59 | const gchar *element_name, |
60 | const gchar **attribute_names, |
61 | const gchar **attribute_values, |
62 | gpointer user_data, |
63 | GError **error) |
64 | { |
65 | int i=0; |
66 | LttSystemDescription* des = (LttSystemDescription* )user_data; |
67 | if(strcmp("system", element_name)){ |
68 | g_warning("This is not system.xml file\n"); |
69 | exit(1); |
70 | } |
71 | |
72 | while(attribute_names[i]){ |
73 | if(strcmp("node_name", attribute_names[i])==0){ |
74 | des->node_name = g_strdup(attribute_values[i]); |
75 | }else if(strcmp("domainname", attribute_names[i])==0){ |
76 | des->domain_name = g_strdup(attribute_values[i]); |
77 | }else if(strcmp("cpu", attribute_names[i])==0){ |
78 | des->nb_cpu = atoi(attribute_values[i]); |
79 | }else if(strcmp("arch_size", attribute_names[i])==0){ |
80 | if(strcmp(attribute_values[i],"LP32") == 0) des->size = LTT_LP32; |
81 | else if(strcmp(attribute_values[i],"ILP32") == 0) des->size = LTT_ILP32; |
82 | else if(strcmp(attribute_values[i],"LP64") == 0) des->size = LTT_LP64; |
83 | else if(strcmp(attribute_values[i],"ILP64") == 0) des->size = LTT_ILP64; |
84 | else if(strcmp(attribute_values[i],"UNKNOWN") == 0) des->size = LTT_UNKNOWN; |
85 | }else if(strcmp("endian", attribute_names[i])==0){ |
86 | if(strcmp(attribute_values[i],"LITTLE_ENDIAN") == 0) |
87 | des->endian = LTT_LITTLE_ENDIAN; |
88 | else if(strcmp(attribute_values[i],"BIG_ENDIAN") == 0) |
89 | des->endian = LTT_BIG_ENDIAN; |
90 | }else if(strcmp("kernel_name", attribute_names[i])==0){ |
91 | des->kernel_name = g_strdup(attribute_values[i]); |
92 | }else if(strcmp("kernel_release", attribute_names[i])==0){ |
93 | des->kernel_release = g_strdup(attribute_values[i]); |
94 | }else if(strcmp("kernel_version", attribute_names[i])==0){ |
95 | des->kernel_version = g_strdup(attribute_values[i]); |
96 | }else if(strcmp("machine", attribute_names[i])==0){ |
97 | des->machine = g_strdup(attribute_values[i]); |
98 | }else if(strcmp("processor", attribute_names[i])==0){ |
99 | des->processor = g_strdup(attribute_values[i]); |
100 | }else if(strcmp("hardware_platform", attribute_names[i])==0){ |
101 | des->hardware_platform = g_strdup(attribute_values[i]); |
102 | }else if(strcmp("operating_system", attribute_names[i])==0){ |
103 | des->operating_system = g_strdup(attribute_values[i]); |
104 | }else if(strcmp("ltt_major_version", attribute_names[i])==0){ |
105 | des->ltt_major_version = atoi(attribute_values[i]); |
106 | }else if(strcmp("ltt_minor_version", attribute_names[i])==0){ |
107 | des->ltt_minor_version = atoi(attribute_values[i]); |
108 | }else if(strcmp("ltt_block_size", attribute_names[i])==0){ |
109 | des->ltt_block_size = atoi(attribute_values[i]); |
110 | }else{ |
111 | g_warning("Not a valid attribute\n"); |
112 | exit(1); |
113 | } |
114 | i++; |
115 | } |
116 | } |
117 | |
118 | static void parser_end_element (GMarkupParseContext *context, |
119 | const gchar *element_name, |
120 | gpointer user_data, |
121 | GError **error) |
122 | { |
123 | } |
124 | |
125 | static void parser_characters (GMarkupParseContext *context, |
126 | const gchar *text, |
127 | gsize text_len, |
128 | gpointer user_data, |
129 | GError **error) |
130 | { |
131 | LttSystemDescription* des = (LttSystemDescription* )user_data; |
132 | des->description = g_strdup(text); |
133 | } |
134 | |
135 | |
6cd62ccf |
136 | /***************************************************************************** |
137 | *Function name |
963b5f2d |
138 | * ltt_tracefile_open : open a trace file, construct a LttTracefile |
6cd62ccf |
139 | *Input params |
963b5f2d |
140 | * t : the trace containing the tracefile |
141 | * fileName : path name of the trace file |
6cd62ccf |
142 | *Return value |
963b5f2d |
143 | * : a pointer to a tracefile |
6cd62ccf |
144 | ****************************************************************************/ |
145 | |
963b5f2d |
146 | LttTracefile* ltt_tracefile_open(LttTrace * t, char * fileName) |
6cd62ccf |
147 | { |
963b5f2d |
148 | LttTracefile * tf; |
149 | struct stat lTDFStat; /* Trace data file status */ |
150 | BlockStart a_block_start; |
6cd62ccf |
151 | |
963b5f2d |
152 | tf = g_new(LttTracefile, 1); |
6cd62ccf |
153 | |
154 | //open the file |
963b5f2d |
155 | tf->name = g_strdup(fileName); |
156 | tf->trace = t; |
6cd62ccf |
157 | tf->fd = open(fileName, O_RDONLY, 0); |
158 | if(tf->fd < 0){ |
159 | g_error("Unable to open input data file %s\n", fileName); |
160 | } |
161 | |
162 | // Get the file's status |
163 | if(fstat(tf->fd, &lTDFStat) < 0){ |
164 | g_error("Unable to get the status of the input data file %s\n", fileName); |
165 | } |
166 | |
167 | // Is the file large enough to contain a trace |
963b5f2d |
168 | if(lTDFStat.st_size < sizeof(BlockStart) + EVENT_HEADER_SIZE){ |
8710c6c7 |
169 | g_print("The input data file %s does not contain a trace\n", fileName); |
663bd9ff |
170 | g_free(tf->name); |
171 | close(tf->fd); |
172 | g_free(tf); |
8710c6c7 |
173 | return NULL; |
6cd62ccf |
174 | } |
175 | |
176 | //store the size of the file |
177 | tf->file_size = lTDFStat.st_size; |
963b5f2d |
178 | tf->block_size = t->system_description->ltt_block_size; |
179 | tf->block_number = tf->file_size / tf->block_size; |
180 | tf->which_block = 0; |
6cd62ccf |
181 | |
182 | //allocate memory to contain the info of a block |
963b5f2d |
183 | tf->buffer = (void *) g_new(char, t->system_description->ltt_block_size); |
6cd62ccf |
184 | |
963b5f2d |
185 | //read the first block |
186 | if(readBlock(tf,1)) exit(1); |
6cd62ccf |
187 | |
188 | return tf; |
189 | } |
190 | |
6cd62ccf |
191 | |
192 | /***************************************************************************** |
963b5f2d |
193 | *Open control and per cpu tracefiles |
6cd62ccf |
194 | ****************************************************************************/ |
195 | |
963b5f2d |
196 | void ltt_tracefile_open_cpu(LttTrace *t, char * tracefile_name) |
6cd62ccf |
197 | { |
963b5f2d |
198 | LttTracefile * tf; |
199 | tf = ltt_tracefile_open(t,tracefile_name); |
4a6c8e36 |
200 | if(!tf) return; |
963b5f2d |
201 | t->per_cpu_tracefile_number++; |
202 | g_ptr_array_add(t->per_cpu_tracefiles, tf); |
6cd62ccf |
203 | } |
204 | |
963b5f2d |
205 | void ltt_tracefile_open_control(LttTrace *t, char * control_name) |
6cd62ccf |
206 | { |
963b5f2d |
207 | LttTracefile * tf; |
208 | LttEvent * ev; |
209 | LttFacility * f; |
cbd41522 |
210 | guint16 evId; |
963b5f2d |
211 | void * pos; |
212 | FacilityLoad fLoad; |
213 | int i; |
214 | |
215 | tf = ltt_tracefile_open(t,control_name); |
8710c6c7 |
216 | if(!tf) return; |
963b5f2d |
217 | t->control_tracefile_number++; |
218 | g_ptr_array_add(t->control_tracefiles,tf); |
219 | |
220 | //parse facilities tracefile to get base_id |
542ceddd |
221 | if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){ |
963b5f2d |
222 | while(1){ |
40331ba8 |
223 | ev = ltt_tracefile_read(tf); |
224 | if(!ev)return; // end of file |
225 | |
226 | if(ev->event_id == TRACE_FACILITY_LOAD){ |
227 | pos = ev->data; |
963b5f2d |
228 | fLoad.name = (char*)pos; |
47a166fc |
229 | fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name)); |
cbd41522 |
230 | fLoad.base_code = *(guint32 *)(pos + strlen(fLoad.name) + sizeof(LttChecksum)); |
963b5f2d |
231 | |
232 | for(i=0;i<t->facility_number;i++){ |
233 | f = (LttFacility*)g_ptr_array_index(t->facilities,i); |
234 | if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){ |
235 | f->base_id = fLoad.base_code; |
236 | break; |
237 | } |
238 | } |
239 | if(i==t->facility_number) |
240 | g_error("Facility: %s, checksum: %d is not founded\n", |
241 | fLoad.name,fLoad.checksum); |
40331ba8 |
242 | }else if(ev->event_id == TRACE_BLOCK_START){ |
243 | continue; |
244 | }else if(ev->event_id == TRACE_BLOCK_END){ |
245 | break; |
963b5f2d |
246 | }else g_error("Not valid facilities trace file\n"); |
247 | } |
248 | } |
6cd62ccf |
249 | } |
250 | |
251 | /***************************************************************************** |
252 | *Function name |
963b5f2d |
253 | * ltt_tracefile_close: close a trace file, |
6cd62ccf |
254 | *Input params |
963b5f2d |
255 | * t : tracefile which will be closed |
6cd62ccf |
256 | ****************************************************************************/ |
257 | |
963b5f2d |
258 | void ltt_tracefile_close(LttTracefile *t) |
6cd62ccf |
259 | { |
963b5f2d |
260 | g_free(t->name); |
261 | g_free(t->buffer); |
663bd9ff |
262 | close(t->fd); |
963b5f2d |
263 | g_free(t); |
264 | } |
6cd62ccf |
265 | |
6cd62ccf |
266 | |
963b5f2d |
267 | /***************************************************************************** |
268 | *Get system information |
269 | ****************************************************************************/ |
270 | void getSystemInfo(LttSystemDescription* des, char * pathname) |
271 | { |
272 | FILE * fp; |
963b5f2d |
273 | char buf[DIR_NAME_SIZE]; |
274 | char description[4*DIR_NAME_SIZE]; |
43da6a59 |
275 | |
276 | GMarkupParseContext * context; |
277 | GError * error; |
278 | GMarkupParser markup_parser = |
279 | { |
280 | parser_start_element, |
281 | parser_end_element, |
282 | parser_characters, |
283 | NULL, /* passthrough */ |
284 | NULL /* error */ |
285 | }; |
963b5f2d |
286 | |
287 | fp = fopen(pathname,"r"); |
288 | if(!fp){ |
289 | g_error("Can not open file : %s\n", pathname); |
6cd62ccf |
290 | } |
963b5f2d |
291 | |
43da6a59 |
292 | context = g_markup_parse_context_new(&markup_parser, 0, des,NULL); |
6cd62ccf |
293 | |
43da6a59 |
294 | while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){ |
295 | if(!g_markup_parse_context_parse(context, buf, DIR_NAME_SIZE, &error)){ |
296 | g_warning("Can not parse xml file: \n%s\n", error->message); |
297 | exit(1); |
298 | } |
963b5f2d |
299 | } |
963b5f2d |
300 | fclose(fp); |
6cd62ccf |
301 | } |
302 | |
303 | /***************************************************************************** |
963b5f2d |
304 | *The following functions get facility/tracefile information |
6cd62ccf |
305 | ****************************************************************************/ |
306 | |
963b5f2d |
307 | void getFacilityInfo(LttTrace *t, char* eventdefs) |
6cd62ccf |
308 | { |
963b5f2d |
309 | DIR * dir; |
310 | struct dirent *entry; |
311 | char * ptr; |
312 | int i,j; |
313 | LttFacility * f; |
314 | LttEventType * et; |
e8bb1a73 |
315 | char name[DIR_NAME_SIZE]; |
963b5f2d |
316 | |
317 | dir = opendir(eventdefs); |
318 | if(!dir) g_error("Can not open directory: %s\n", eventdefs); |
319 | |
320 | while((entry = readdir(dir)) != NULL){ |
321 | ptr = &entry->d_name[strlen(entry->d_name)-4]; |
322 | if(strcmp(ptr,".xml") != 0) continue; |
e8bb1a73 |
323 | strcpy(name,eventdefs); |
324 | strcat(name,entry->d_name); |
325 | ltt_facility_open(t,name); |
963b5f2d |
326 | } |
327 | closedir(dir); |
328 | |
963b5f2d |
329 | for(j=0;j<t->facility_number;j++){ |
8710c6c7 |
330 | f = (LttFacility*)g_ptr_array_index(t->facilities, j); |
963b5f2d |
331 | for(i=0; i<f->event_number; i++){ |
332 | et = f->events[i]; |
40331ba8 |
333 | setFieldsOffset(NULL, et, NULL, t); |
963b5f2d |
334 | } |
335 | } |
336 | } |
6cd62ccf |
337 | |
963b5f2d |
338 | void getControlFileInfo(LttTrace *t, char* control) |
6cd62ccf |
339 | { |
963b5f2d |
340 | DIR * dir; |
341 | struct dirent *entry; |
e8bb1a73 |
342 | char name[DIR_NAME_SIZE]; |
963b5f2d |
343 | |
344 | dir = opendir(control); |
345 | if(!dir) g_error("Can not open directory: %s\n", control); |
346 | |
347 | while((entry = readdir(dir)) != NULL){ |
8710c6c7 |
348 | if(strcmp(entry->d_name,"facilities") != 0 && |
349 | strcmp(entry->d_name,"interrupts") != 0 && |
963b5f2d |
350 | strcmp(entry->d_name,"processes") != 0) continue; |
351 | |
e8bb1a73 |
352 | strcpy(name,control); |
353 | strcat(name,entry->d_name); |
354 | ltt_tracefile_open_control(t,name); |
963b5f2d |
355 | } |
356 | closedir(dir); |
6cd62ccf |
357 | } |
358 | |
963b5f2d |
359 | void getCpuFileInfo(LttTrace *t, char* cpu) |
6cd62ccf |
360 | { |
963b5f2d |
361 | DIR * dir; |
362 | struct dirent *entry; |
e8bb1a73 |
363 | char name[DIR_NAME_SIZE]; |
963b5f2d |
364 | |
365 | dir = opendir(cpu); |
366 | if(!dir) g_error("Can not open directory: %s\n", cpu); |
367 | |
368 | while((entry = readdir(dir)) != NULL){ |
8710c6c7 |
369 | if(strcmp(entry->d_name,".") != 0 && |
a614481a |
370 | strcmp(entry->d_name,"..") != 0 && |
371 | strcmp(entry->d_name,".svn") != 0){ |
e8bb1a73 |
372 | strcpy(name,cpu); |
373 | strcat(name,entry->d_name); |
374 | ltt_tracefile_open_cpu(t,name); |
963b5f2d |
375 | }else continue; |
376 | } |
377 | closedir(dir); |
6cd62ccf |
378 | } |
379 | |
963b5f2d |
380 | /***************************************************************************** |
381 | *A trace is specified as a pathname to the directory containing all the |
382 | *associated data (control tracefiles, per cpu tracefiles, event |
383 | *descriptions...). |
384 | * |
385 | *When a trace is closed, all the associated facilities, types and fields |
386 | *are released as well. |
803229fa |
387 | * |
388 | * MD : If pathname is already absolute, we do not add current working |
389 | * directory to it. |
390 | * |
963b5f2d |
391 | ****************************************************************************/ |
6cd62ccf |
392 | |
9f797243 |
393 | void get_absolute_pathname(const char *pathname, char * abs_pathname) |
394 | { |
395 | char * ptr, *ptr1; |
396 | size_t size = DIR_NAME_SIZE; |
397 | abs_pathname[0] = '\0'; |
803229fa |
398 | |
399 | if(pathname[0] == '/') |
400 | { |
401 | strcat(abs_pathname, pathname); |
402 | return; |
403 | } |
404 | |
9f797243 |
405 | if(!getcwd(abs_pathname, size)){ |
406 | g_warning("Can not get current working directory\n"); |
407 | strcat(abs_pathname, pathname); |
408 | return; |
409 | } |
803229fa |
410 | |
9f797243 |
411 | strcat(abs_pathname,"/"); |
412 | |
413 | ptr = (char*)pathname; |
414 | ptr1 = ptr + 1; |
415 | while(*ptr == '.' && *ptr1 == '.'){ |
416 | ptr += 3; |
417 | ptr1 = ptr + 1; |
418 | } |
419 | strcat(abs_pathname,ptr); |
420 | } |
421 | |
f7afe191 |
422 | LttTrace *ltt_trace_open(const char *pathname) |
6cd62ccf |
423 | { |
963b5f2d |
424 | LttTrace * t; |
425 | LttSystemDescription * sys_description; |
426 | char eventdefs[DIR_NAME_SIZE]; |
427 | char info[DIR_NAME_SIZE]; |
428 | char control[DIR_NAME_SIZE]; |
429 | char cpu[DIR_NAME_SIZE]; |
430 | char tmp[DIR_NAME_SIZE]; |
9f797243 |
431 | char abs_path[DIR_NAME_SIZE]; |
963b5f2d |
432 | gboolean has_slash = FALSE; |
433 | |
9f797243 |
434 | get_absolute_pathname(pathname, abs_path); |
435 | |
963b5f2d |
436 | //establish the pathname to different directories |
9f797243 |
437 | if(abs_path[strlen(abs_path)-1] == '/')has_slash = TRUE; |
438 | strcpy(eventdefs,abs_path); |
963b5f2d |
439 | if(!has_slash)strcat(eventdefs,"/"); |
440 | strcat(eventdefs,"eventdefs/"); |
441 | |
9f797243 |
442 | strcpy(info,abs_path); |
963b5f2d |
443 | if(!has_slash)strcat(info,"/"); |
444 | strcat(info,"info/"); |
445 | |
9f797243 |
446 | strcpy(control,abs_path); |
963b5f2d |
447 | if(!has_slash)strcat(control,"/"); |
448 | strcat(control,"control/"); |
449 | |
9f797243 |
450 | strcpy(cpu,abs_path); |
963b5f2d |
451 | if(!has_slash)strcat(cpu,"/"); |
452 | strcat(cpu,"cpu/"); |
453 | |
454 | //new trace |
455 | t = g_new(LttTrace, 1); |
456 | sys_description = g_new(LttSystemDescription, 1); |
9f797243 |
457 | t->pathname = g_strdup(abs_path); |
963b5f2d |
458 | t->facility_number = 0; |
459 | t->control_tracefile_number = 0; |
460 | t->per_cpu_tracefile_number = 0; |
461 | t->system_description = sys_description; |
462 | t->control_tracefiles = g_ptr_array_new(); |
463 | t->per_cpu_tracefiles = g_ptr_array_new(); |
464 | t->facilities = g_ptr_array_new(); |
465 | getDataEndianType(&(t->my_arch_size), &(t->my_arch_endian)); |
466 | |
467 | //get system description |
468 | strcpy(tmp,info); |
469 | strcat(tmp,"system.xml"); |
470 | getSystemInfo(sys_description, tmp); |
471 | |
b333a76b |
472 | //get facilities info |
473 | getFacilityInfo(t,eventdefs); |
474 | |
963b5f2d |
475 | //get control tracefile info |
476 | getControlFileInfo(t,control); |
477 | |
478 | //get cpu tracefile info |
479 | getCpuFileInfo(t,cpu); |
480 | |
963b5f2d |
481 | return t; |
6cd62ccf |
482 | } |
483 | |
49bf71b5 |
484 | char * ltt_trace_name(LttTrace *t) |
485 | { |
486 | return t->pathname; |
487 | } |
488 | |
489 | |
f7afe191 |
490 | /****************************************************************************** |
491 | * When we copy a trace, we want all the opening actions to happen again : |
492 | * the trace will be reopened and totally independant from the original. |
493 | * That's why we call ltt_trace_open. |
494 | *****************************************************************************/ |
495 | LttTrace *ltt_trace_copy(LttTrace *self) |
496 | { |
497 | return ltt_trace_open(self->pathname); |
498 | } |
499 | |
963b5f2d |
500 | void ltt_trace_close(LttTrace *t) |
6cd62ccf |
501 | { |
963b5f2d |
502 | int i; |
503 | LttTracefile * tf; |
504 | LttFacility * f; |
505 | |
506 | g_free(t->pathname); |
507 | |
508 | //free system_description |
509 | g_free(t->system_description->description); |
510 | g_free(t->system_description->node_name); |
511 | g_free(t->system_description->domain_name); |
512 | g_free(t->system_description->kernel_name); |
513 | g_free(t->system_description->kernel_release); |
514 | g_free(t->system_description->kernel_version); |
515 | g_free(t->system_description->machine); |
516 | g_free(t->system_description->processor); |
517 | g_free(t->system_description->hardware_platform); |
518 | g_free(t->system_description->operating_system); |
519 | g_free(t->system_description); |
520 | |
521 | //free control_tracefiles |
522 | for(i=0;i<t->control_tracefile_number;i++){ |
523 | tf = (LttTracefile*)g_ptr_array_index(t->control_tracefiles,i); |
524 | ltt_tracefile_close(tf); |
525 | } |
09ad4797 |
526 | g_ptr_array_free(t->control_tracefiles, TRUE); |
963b5f2d |
527 | |
528 | //free per_cpu_tracefiles |
529 | for(i=0;i<t->per_cpu_tracefile_number;i++){ |
530 | tf = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles,i); |
531 | ltt_tracefile_close(tf); |
532 | } |
09ad4797 |
533 | g_ptr_array_free(t->per_cpu_tracefiles, TRUE); |
963b5f2d |
534 | |
535 | //free facilities |
536 | for(i=0;i<t->facility_number;i++){ |
537 | f = (LttFacility*)g_ptr_array_index(t->facilities,i); |
538 | ltt_facility_close(f); |
539 | } |
09ad4797 |
540 | g_ptr_array_free(t->facilities, TRUE); |
963b5f2d |
541 | |
542 | g_free(t); |
09ad4797 |
543 | |
544 | g_blow_chunks(); |
6cd62ccf |
545 | } |
546 | |
963b5f2d |
547 | |
6cd62ccf |
548 | /***************************************************************************** |
963b5f2d |
549 | *Get the system description of the trace |
6cd62ccf |
550 | ****************************************************************************/ |
551 | |
963b5f2d |
552 | LttSystemDescription *ltt_trace_system_description(LttTrace *t) |
6cd62ccf |
553 | { |
963b5f2d |
554 | return t->system_description; |
6cd62ccf |
555 | } |
556 | |
557 | /***************************************************************************** |
963b5f2d |
558 | * The following functions discover the facilities of the trace |
6cd62ccf |
559 | ****************************************************************************/ |
560 | |
963b5f2d |
561 | unsigned ltt_trace_facility_number(LttTrace *t) |
562 | { |
563 | return (unsigned)(t->facility_number); |
564 | } |
565 | |
566 | LttFacility *ltt_trace_facility_get(LttTrace *t, unsigned i) |
6cd62ccf |
567 | { |
963b5f2d |
568 | return (LttFacility*)g_ptr_array_index(t->facilities, i); |
6cd62ccf |
569 | } |
570 | |
571 | /***************************************************************************** |
572 | *Function name |
963b5f2d |
573 | * ltt_trace_facility_find : find facilities in the trace |
6cd62ccf |
574 | *Input params |
963b5f2d |
575 | * t : the trace |
576 | * name : facility name |
577 | *Output params |
578 | * position : position of the facility in the trace |
6cd62ccf |
579 | *Return value |
963b5f2d |
580 | * : the number of facilities |
6cd62ccf |
581 | ****************************************************************************/ |
582 | |
963b5f2d |
583 | unsigned ltt_trace_facility_find(LttTrace *t, char *name, unsigned *position) |
6cd62ccf |
584 | { |
963b5f2d |
585 | int i, count=0; |
586 | LttFacility * f; |
8a3005f3 |
587 | for(i=0;i<t->facility_number;i++){ |
963b5f2d |
588 | f = (LttFacility*)g_ptr_array_index(t->facilities, i); |
589 | if(strcmp(f->name,name)==0){ |
590 | count++; |
591 | if(count==1) *position = i; |
592 | }else{ |
593 | if(count) break; |
594 | } |
595 | } |
596 | return count; |
6cd62ccf |
597 | } |
598 | |
599 | /***************************************************************************** |
963b5f2d |
600 | * Functions to discover all the event types in the trace |
6cd62ccf |
601 | ****************************************************************************/ |
602 | |
963b5f2d |
603 | unsigned ltt_trace_eventtype_number(LttTrace *t) |
6cd62ccf |
604 | { |
963b5f2d |
605 | int i; |
606 | unsigned count = 0; |
607 | LttFacility * f; |
b445142a |
608 | for(i=0;i<t->facility_number;i++){ |
963b5f2d |
609 | f = (LttFacility*)g_ptr_array_index(t->facilities, i); |
610 | count += f->event_number; |
611 | } |
612 | return count; |
6cd62ccf |
613 | } |
614 | |
963b5f2d |
615 | LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id) |
6cd62ccf |
616 | { |
963b5f2d |
617 | LttFacility * facility; |
618 | int i; |
619 | for(i=0;i<trace->facility_number;i++){ |
620 | facility = (LttFacility*) g_ptr_array_index(trace->facilities,i); |
621 | if(id >= facility->base_id && |
622 | id < facility->base_id + facility->event_number) |
623 | break; |
624 | } |
625 | if(i==trace->facility_number) return NULL; |
626 | else return facility; |
6cd62ccf |
627 | } |
628 | |
963b5f2d |
629 | LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId) |
6cd62ccf |
630 | { |
963b5f2d |
631 | LttFacility * f; |
632 | f = ltt_trace_facility_by_id(t,evId); |
633 | if(!f) return NULL; |
634 | return f->events[evId - f->base_id]; |
6cd62ccf |
635 | } |
636 | |
637 | /***************************************************************************** |
963b5f2d |
638 | *There is one "per cpu" tracefile for each CPU, numbered from 0 to |
639 | *the maximum number of CPU in the system. When the number of CPU installed |
640 | *is less than the maximum, some positions are unused. There are also a |
641 | *number of "control" tracefiles (facilities, interrupts...). |
6cd62ccf |
642 | ****************************************************************************/ |
963b5f2d |
643 | unsigned ltt_trace_control_tracefile_number(LttTrace *t) |
6cd62ccf |
644 | { |
963b5f2d |
645 | return t->control_tracefile_number; |
6cd62ccf |
646 | } |
647 | |
963b5f2d |
648 | unsigned ltt_trace_per_cpu_tracefile_number(LttTrace *t) |
6cd62ccf |
649 | { |
963b5f2d |
650 | return t->per_cpu_tracefile_number; |
6cd62ccf |
651 | } |
652 | |
653 | /***************************************************************************** |
963b5f2d |
654 | *It is possible to search for the tracefiles by name or by CPU position. |
655 | *The index within the tracefiles of the same type is returned if found |
656 | *and a negative value otherwise. |
6cd62ccf |
657 | ****************************************************************************/ |
658 | |
963b5f2d |
659 | int ltt_trace_control_tracefile_find(LttTrace *t, char *name) |
6cd62ccf |
660 | { |
963b5f2d |
661 | LttTracefile * tracefile; |
662 | int i; |
663 | for(i=0;i<t->control_tracefile_number;i++){ |
664 | tracefile = (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i); |
665 | if(strcmp(tracefile->name, name)==0)break; |
666 | } |
667 | if(i == t->control_tracefile_number) return -1; |
668 | return i; |
6cd62ccf |
669 | } |
670 | |
963b5f2d |
671 | int ltt_trace_per_cpu_tracefile_find(LttTrace *t, unsigned i) |
6cd62ccf |
672 | { |
963b5f2d |
673 | LttTracefile * tracefile; |
674 | int j, name; |
675 | for(j=0;j<t->per_cpu_tracefile_number;j++){ |
676 | tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, j); |
677 | name = atoi(tracefile->name); |
678 | if(name == (int)i)break; |
679 | } |
680 | if(j == t->per_cpu_tracefile_number) return -1; |
681 | return j; |
6cd62ccf |
682 | } |
683 | |
684 | /***************************************************************************** |
963b5f2d |
685 | *Get a specific tracefile |
6cd62ccf |
686 | ****************************************************************************/ |
687 | |
963b5f2d |
688 | LttTracefile *ltt_trace_control_tracefile_get(LttTrace *t, unsigned i) |
6cd62ccf |
689 | { |
5598cfe3 |
690 | return (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i); |
963b5f2d |
691 | } |
692 | |
693 | LttTracefile *ltt_trace_per_cpu_tracefile_get(LttTrace *t, unsigned i) |
694 | { |
695 | return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i); |
6cd62ccf |
696 | } |
697 | |
487ad181 |
698 | /***************************************************************************** |
699 | * Get the start time and end time of the trace |
700 | ****************************************************************************/ |
701 | |
702 | void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end) |
703 | { |
704 | LttTime startSmall, startTmp, endBig, endTmp; |
705 | int i, j=0; |
706 | LttTracefile * tf; |
707 | |
708 | for(i=0;i<t->control_tracefile_number;i++){ |
709 | tf = g_ptr_array_index(t->control_tracefiles, i); |
710 | readBlock(tf,1); |
711 | startTmp = tf->a_block_start->time; |
712 | readBlock(tf,tf->block_number); |
713 | endTmp = tf->a_block_end->time; |
714 | if(i==0){ |
715 | startSmall = startTmp; |
716 | endBig = endTmp; |
717 | j = 1; |
718 | continue; |
719 | } |
308711e5 |
720 | if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp; |
721 | if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp; |
487ad181 |
722 | } |
723 | |
724 | for(i=0;i<t->per_cpu_tracefile_number;i++){ |
725 | tf = g_ptr_array_index(t->per_cpu_tracefiles, i); |
726 | readBlock(tf,1); |
727 | startTmp = tf->a_block_start->time; |
728 | readBlock(tf,tf->block_number); |
729 | endTmp = tf->a_block_end->time; |
730 | if(j == 0 && i==0){ |
731 | startSmall = startTmp; |
732 | endBig = endTmp; |
733 | continue; |
734 | } |
308711e5 |
735 | if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp; |
736 | if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp; |
487ad181 |
737 | } |
738 | |
739 | *start = startSmall; |
740 | *end = endBig; |
741 | } |
742 | |
743 | |
6cd62ccf |
744 | /***************************************************************************** |
963b5f2d |
745 | *Get the name of a tracefile |
6cd62ccf |
746 | ****************************************************************************/ |
747 | |
963b5f2d |
748 | char *ltt_tracefile_name(LttTracefile *tf) |
6cd62ccf |
749 | { |
963b5f2d |
750 | return tf->name; |
6cd62ccf |
751 | } |
752 | |
80da81ad |
753 | /***************************************************************************** |
754 | * Get the number of blocks in the tracefile |
755 | ****************************************************************************/ |
756 | |
757 | unsigned ltt_tracefile_block_number(LttTracefile *tf) |
758 | { |
759 | return tf->block_number; |
760 | } |
761 | |
6cd62ccf |
762 | /***************************************************************************** |
763 | *Function name |
764 | * ltt_tracefile_seek_time: seek to the first event of the trace with time |
765 | * larger or equal to time |
766 | *Input params |
767 | * t : tracefile |
768 | * time : criteria of the time |
6cd62ccf |
769 | ****************************************************************************/ |
caf7a67a |
770 | void ltt_tracefile_find_time_block(LttTracefile *t, LttTime time, |
771 | int start_block, int end_block) |
772 | { |
773 | int err, tmp_block, s, e; |
774 | int headTime; |
775 | int tailTime; |
776 | |
777 | err=readBlock(t,start_block); |
778 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
779 | if(start_block == end_block)return; |
780 | |
781 | tailTime = ltt_time_compare(t->a_block_end->time, time); |
782 | if(tailTime >= 0) return; |
783 | |
784 | err=readBlock(t,end_block); |
785 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
786 | if(start_block+1 == end_block)return; |
787 | |
788 | headTime = ltt_time_compare(t->a_block_start->time, time); |
789 | if(headTime <= 0 ) return; |
790 | |
791 | tmp_block = (end_block + start_block)/2; |
792 | err=readBlock(t,tmp_block); |
793 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
794 | |
795 | headTime = ltt_time_compare(t->a_block_start->time, time); |
796 | tailTime = ltt_time_compare(t->a_block_end->time, time); |
797 | if(headTime <= 0 && tailTime >= 0) return; |
798 | |
799 | if(headTime > 0){ |
800 | s = start_block + 1; |
801 | e = tmp_block - 1; |
802 | if(s <= e) |
803 | ltt_tracefile_find_time_block(t, time, s, e); |
804 | else return; |
805 | } |
806 | |
807 | if(tailTime < 0){ |
808 | s = tmp_block + 1; |
809 | e = end_block - 1; |
810 | if(s <= e) |
811 | ltt_tracefile_find_time_block(t, time, s, e); |
812 | else return; |
813 | } |
814 | } |
815 | |
816 | void ltt_tracefile_backward_find_time_block(LttTracefile *t, LttTime time) |
817 | { |
818 | int t_time, h_time, err; |
819 | err=readBlock(t,t->which_block-1); |
820 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
821 | h_time = ltt_time_compare(t->a_block_start->time, time); |
822 | t_time = ltt_time_compare(t->a_block_end->time, time); |
823 | if(h_time == 0){ |
824 | int tmp; |
825 | if(t->which_block == 1) return; |
826 | err=readBlock(t,t->which_block-1); |
827 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
828 | tmp = ltt_time_compare(t->a_block_end->time, time); |
829 | if(tmp == 0) return ltt_tracefile_seek_time(t, time); |
830 | err=readBlock(t,t->which_block+1); |
831 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
832 | }else if(h_time > 0){ |
833 | ltt_tracefile_find_time_block(t, time, 1, t->which_block); |
834 | return ltt_tracefile_seek_time(t, time) ; |
835 | }else{ |
836 | if(t_time >= 0) return ltt_tracefile_seek_time(t, time); |
837 | err=readBlock(t,t->which_block+1); |
838 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
839 | } |
840 | } |
6cd62ccf |
841 | |
963b5f2d |
842 | void ltt_tracefile_seek_time(LttTracefile *t, LttTime time) |
6cd62ccf |
843 | { |
844 | int err; |
963b5f2d |
845 | LttTime lttTime; |
308711e5 |
846 | int headTime = ltt_time_compare(t->a_block_start->time, time); |
847 | int tailTime = ltt_time_compare(t->a_block_end->time, time); |
62e55dd6 |
848 | LttEvent * ev; |
849 | |
6cd62ccf |
850 | if(headTime < 0 && tailTime > 0){ |
308711e5 |
851 | if(ltt_time_compare(t->a_block_end->time, t->current_event_time) !=0) { |
db55eaae |
852 | lttTime = getEventTime(t); |
308711e5 |
853 | err = ltt_time_compare(lttTime, time); |
db55eaae |
854 | if(err > 0){ |
308711e5 |
855 | if(t->which_event==2 || (&t->prev_event_time,&time)<0){ |
1a3b8cbd |
856 | return; |
db55eaae |
857 | }else{ |
858 | updateTracefile(t); |
859 | return ltt_tracefile_seek_time(t, time); |
1a3b8cbd |
860 | } |
db55eaae |
861 | }else if(err < 0){ |
862 | while(1){ |
863 | ev = ltt_tracefile_read(t); |
864 | if(ev == NULL){ |
865 | g_print("End of file\n"); |
866 | return; |
867 | } |
868 | lttTime = getEventTime(t); |
308711e5 |
869 | err = ltt_time_compare(lttTime, time); |
db55eaae |
870 | if(err >= 0)return; |
871 | } |
872 | }else return; |
873 | }else{//we are at the end of the block |
874 | updateTracefile(t); |
875 | return ltt_tracefile_seek_time(t, time); |
876 | } |
e37c1372 |
877 | }else if(headTime >= 0){ |
6cd62ccf |
878 | if(t->which_block == 1){ |
879 | updateTracefile(t); |
880 | }else{ |
1ee6a9af |
881 | if(ltt_time_compare(t->prev_block_end_time, time) >= 0 || |
882 | (t->prev_block_end_time.tv_sec == 0 && |
883 | t->prev_block_end_time.tv_nsec == 0 )){ |
caf7a67a |
884 | ltt_tracefile_backward_find_time_block(t, time); |
6cd62ccf |
885 | }else{ |
886 | updateTracefile(t); |
887 | } |
888 | } |
40331ba8 |
889 | }else if(tailTime < 0){ |
6cd62ccf |
890 | if(t->which_block != t->block_number){ |
caf7a67a |
891 | ltt_tracefile_find_time_block(t, time, t->which_block+1, t->block_number); |
892 | return ltt_tracefile_seek_time(t, time); |
963b5f2d |
893 | }else { |
a8c0f09d |
894 | t->cur_event_pos = t->buffer + t->block_size; |
895 | g_print("End of file\n"); |
963b5f2d |
896 | return; |
897 | } |
40331ba8 |
898 | }else if(tailTime == 0){ |
e37c1372 |
899 | t->cur_event_pos = t->last_event_pos; |
62e55dd6 |
900 | t->current_event_time = time; |
901 | t->cur_heart_beat_number = 0; |
902 | t->prev_event_time.tv_sec = 0; |
903 | t->prev_event_time.tv_nsec = 0; |
40331ba8 |
904 | return; |
6cd62ccf |
905 | } |
6cd62ccf |
906 | } |
907 | |
80da81ad |
908 | /***************************************************************************** |
909 | * Seek to the first event with position equal or larger to ep |
910 | ****************************************************************************/ |
911 | |
912 | void ltt_tracefile_seek_position(LttTracefile *t, LttEventPosition *ep) |
913 | { |
914 | //if we are at the right place, just return |
915 | if(t->which_block == ep->block_num && t->which_event == ep->event_num) |
916 | return; |
917 | |
918 | if(t->which_block == ep->block_num) updateTracefile(t); |
919 | else readBlock(t,ep->block_num); |
920 | |
921 | //event offset is availiable |
922 | if(ep->old_position){ |
923 | t->cur_heart_beat_number = ep->heart_beat_number; |
924 | t->cur_event_pos = t->buffer + ep->event_offset; |
925 | return; |
926 | } |
927 | |
928 | //only block number and event index are availiable |
929 | while(t->which_event < ep->event_num) ltt_tracefile_read(t); |
930 | |
931 | return; |
932 | } |
933 | |
6cd62ccf |
934 | /***************************************************************************** |
935 | *Function name |
40331ba8 |
936 | * ltt_tracefile_read : read the current event, set the pointer to the next |
6cd62ccf |
937 | *Input params |
938 | * t : tracefile |
939 | *Return value |
963b5f2d |
940 | * LttEvent * : an event to be processed |
6cd62ccf |
941 | ****************************************************************************/ |
942 | |
963b5f2d |
943 | LttEvent *ltt_tracefile_read(LttTracefile *t) |
6cd62ccf |
944 | { |
7525f9e5 |
945 | LttEvent * lttEvent = &t->an_event; |
963b5f2d |
946 | int err; |
6cd62ccf |
947 | |
bdc36259 |
948 | if(t->cur_event_pos == t->buffer + t->block_size){ |
949 | if(t->which_block == t->block_number){ |
bdc36259 |
950 | return NULL; |
951 | } |
952 | err = readBlock(t, t->which_block + 1); |
953 | if(err)g_error("Can not read tracefile"); |
954 | } |
955 | |
cbd41522 |
956 | lttEvent->event_id = (int)(*(guint16 *)(t->cur_event_pos)); |
963b5f2d |
957 | if(lttEvent->event_id == TRACE_TIME_HEARTBEAT) |
958 | t->cur_heart_beat_number++; |
6cd62ccf |
959 | |
40331ba8 |
960 | t->prev_event_time = t->current_event_time; |
62e55dd6 |
961 | // t->current_event_time = getEventTime(t); |
6cd62ccf |
962 | |
cbd41522 |
963 | lttEvent->time_delta = *(guint32 *)(t->cur_event_pos + EVENT_ID_SIZE); |
963b5f2d |
964 | lttEvent->event_time = t->current_event_time; |
965 | |
6cd62ccf |
966 | lttEvent->tracefile = t; |
967 | lttEvent->data = t->cur_event_pos + EVENT_HEADER_SIZE; |
908f42fa |
968 | lttEvent->which_block = t->which_block; |
969 | lttEvent->which_event = t->which_event; |
6cd62ccf |
970 | |
40331ba8 |
971 | //update the fields of the current event and go to the next event |
972 | err = skipEvent(t); |
40331ba8 |
973 | if(err == ERANGE) g_error("event id is out of range\n"); |
40331ba8 |
974 | |
e4eced0f |
975 | lttEvent->event_cycle_count = t->cur_cycle_count; |
976 | |
6cd62ccf |
977 | return lttEvent; |
978 | } |
979 | |
980 | /**************************************************************************** |
981 | *Function name |
982 | * readFile : wrap function to read from a file |
983 | *Input Params |
984 | * fd : file descriptor |
985 | * buf : buf to contain the content |
986 | * size : number of bytes to be read |
987 | * mesg : message to be printed if some thing goes wrong |
988 | *return value |
989 | * 0 : success |
990 | * EIO : can not read from the file |
991 | ****************************************************************************/ |
992 | |
993 | int readFile(int fd, void * buf, size_t size, char * mesg) |
994 | { |
995 | ssize_t nbBytes; |
996 | nbBytes = read(fd, buf, size); |
997 | if(nbBytes != size){ |
998 | printf("%s\n",mesg); |
999 | return EIO; |
1000 | } |
1001 | return 0; |
1002 | } |
1003 | |
1004 | /**************************************************************************** |
1005 | *Function name |
1006 | * readBlock : read a block from the file |
1007 | *Input Params |
1008 | * lttdes : ltt trace file |
1009 | * whichBlock : the block which will be read |
1010 | *return value |
1011 | * 0 : success |
1012 | * EINVAL : lseek fail |
1013 | * EIO : can not read from the file |
1014 | ****************************************************************************/ |
1015 | |
963b5f2d |
1016 | int readBlock(LttTracefile * tf, int whichBlock) |
6cd62ccf |
1017 | { |
1018 | off_t nbBytes; |
cbd41522 |
1019 | guint32 lostSize; |
6cd62ccf |
1020 | |
1021 | if(whichBlock - tf->which_block == 1 && tf->which_block != 0){ |
963b5f2d |
1022 | tf->prev_block_end_time = tf->a_block_end->time; |
40331ba8 |
1023 | tf->prev_event_time = tf->a_block_end->time; |
6cd62ccf |
1024 | }else{ |
1025 | tf->prev_block_end_time.tv_sec = 0; |
1026 | tf->prev_block_end_time.tv_nsec = 0; |
40331ba8 |
1027 | tf->prev_event_time.tv_sec = 0; |
1028 | tf->prev_event_time.tv_nsec = 0; |
6cd62ccf |
1029 | } |
6cd62ccf |
1030 | |
963b5f2d |
1031 | nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET); |
6cd62ccf |
1032 | if(nbBytes == -1) return EINVAL; |
1033 | |
963b5f2d |
1034 | if(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block")) |
1035 | return EIO; |
6cd62ccf |
1036 | |
963b5f2d |
1037 | tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE); |
cbd41522 |
1038 | lostSize = *(guint32 *)(tf->buffer + tf->block_size - sizeof(guint32)); |
963b5f2d |
1039 | tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size - |
1040 | lostSize + EVENT_HEADER_SIZE); |
e37c1372 |
1041 | tf->last_event_pos = tf->buffer + tf->block_size - lostSize; |
6cd62ccf |
1042 | |
6cd62ccf |
1043 | tf->which_block = whichBlock; |
963b5f2d |
1044 | tf->which_event = 1; |
40331ba8 |
1045 | tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev |
6cd62ccf |
1046 | tf->cur_heart_beat_number = 0; |
963b5f2d |
1047 | |
6cd62ccf |
1048 | getCyclePerNsec(tf); |
1049 | |
62e55dd6 |
1050 | tf->current_event_time = getEventTime(tf); |
40331ba8 |
1051 | |
6cd62ccf |
1052 | return 0; |
1053 | } |
1054 | |
1055 | /***************************************************************************** |
1056 | *Function name |
1057 | * updateTracefile : reinitialize the info of the block which is already |
1058 | * in the buffer |
1059 | *Input params |
1060 | * tf : tracefile |
1061 | ****************************************************************************/ |
1062 | |
963b5f2d |
1063 | void updateTracefile(LttTracefile * tf) |
6cd62ccf |
1064 | { |
963b5f2d |
1065 | tf->which_event = 1; |
40331ba8 |
1066 | tf->cur_event_pos = tf->buffer; |
62e55dd6 |
1067 | tf->current_event_time = getEventTime(tf); |
6cd62ccf |
1068 | tf->cur_heart_beat_number = 0; |
1069 | |
1070 | tf->prev_event_time.tv_sec = 0; |
1071 | tf->prev_event_time.tv_nsec = 0; |
1072 | } |
1073 | |
1074 | /***************************************************************************** |
1075 | *Function name |
1076 | * skipEvent : go to the next event, update the fields of the current event |
1077 | *Input params |
1078 | * t : tracefile |
1079 | *return value |
1080 | * 0 : success |
6cd62ccf |
1081 | * ERANGE : event id is out of range |
1082 | ****************************************************************************/ |
1083 | |
963b5f2d |
1084 | int skipEvent(LttTracefile * t) |
6cd62ccf |
1085 | { |
1086 | int evId, err; |
1087 | void * evData; |
963b5f2d |
1088 | LttEventType * evT; |
1089 | LttField * rootFld; |
6cd62ccf |
1090 | |
cbd41522 |
1091 | evId = (int)(*(guint16 *)(t->cur_event_pos)); |
6cd62ccf |
1092 | evData = t->cur_event_pos + EVENT_HEADER_SIZE; |
6cd62ccf |
1093 | |
908f42fa |
1094 | evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId); |
47a166fc |
1095 | |
908f42fa |
1096 | if(evT) rootFld = evT->root_field; |
1097 | else return ERANGE; |
6cd62ccf |
1098 | |
908f42fa |
1099 | if(rootFld){ |
1100 | //event has string/sequence or the last event is not the same event |
1101 | if((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event) |
1102 | && rootFld->field_fixed == 0){ |
1103 | setFieldsOffset(t, evT, evData, t->trace); |
47a166fc |
1104 | } |
908f42fa |
1105 | t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size; |
1106 | }else t->cur_event_pos += EVENT_HEADER_SIZE; |
1107 | |
1108 | evT->latest_block = t->which_block; |
1109 | evT->latest_event = t->which_event; |
1110 | |
6cd62ccf |
1111 | //the next event is in the next block |
963b5f2d |
1112 | if(evId == TRACE_BLOCK_END){ |
bdc36259 |
1113 | t->cur_event_pos = t->buffer + t->block_size; |
6cd62ccf |
1114 | }else{ |
1115 | t->which_event++; |
62e55dd6 |
1116 | t->current_event_time = getEventTime(t); |
6cd62ccf |
1117 | } |
1118 | |
1119 | return 0; |
1120 | } |
1121 | |
1122 | /***************************************************************************** |
1123 | *Function name |
1124 | * getCyclePerNsec : calculate cycles per nsec for current block |
1125 | *Input Params |
1126 | * t : tracefile |
1127 | ****************************************************************************/ |
1128 | |
963b5f2d |
1129 | void getCyclePerNsec(LttTracefile * t) |
6cd62ccf |
1130 | { |
963b5f2d |
1131 | LttTime lBufTotalTime; /* Total time for this buffer */ |
1132 | LttCycleCount lBufTotalNSec; /* Total time for this buffer in nsecs */ |
1133 | LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */ |
6cd62ccf |
1134 | |
1135 | /* Calculate the total time for this buffer */ |
308711e5 |
1136 | lBufTotalTime = ltt_time_sub(t->a_block_end->time, t->a_block_start->time); |
6cd62ccf |
1137 | |
1138 | /* Calculate the total cycles for this bufffer */ |
e4eced0f |
1139 | lBufTotalCycle = t->a_block_end->cycle_count; |
1140 | lBufTotalCycle -= t->a_block_start->cycle_count; |
6cd62ccf |
1141 | |
1142 | /* Convert the total time to nsecs */ |
e4eced0f |
1143 | lBufTotalNSec = lBufTotalTime.tv_sec; |
308711e5 |
1144 | lBufTotalNSec *= NANOSECONDS_PER_SECOND; |
e4eced0f |
1145 | lBufTotalNSec += lBufTotalTime.tv_nsec; |
6cd62ccf |
1146 | |
1147 | t->cycle_per_nsec = (double)lBufTotalCycle / (double)lBufTotalNSec; |
1148 | } |
1149 | |
1150 | /**************************************************************************** |
1151 | *Function name |
1152 | * getEventTime : obtain the time of an event |
1153 | *Input params |
1154 | * tf : tracefile |
1155 | *Return value |
963b5f2d |
1156 | * LttTime : the time of the event |
6cd62ccf |
1157 | ****************************************************************************/ |
1158 | |
963b5f2d |
1159 | LttTime getEventTime(LttTracefile * tf) |
6cd62ccf |
1160 | { |
963b5f2d |
1161 | LttTime time; |
1162 | LttCycleCount cycle_count; // cycle count for the current event |
1163 | LttCycleCount lEventTotalCycle; // Total cycles from start for event |
1164 | double lEventNSec; // Total usecs from start for event |
1165 | LttTime lTimeOffset; // Time offset in struct LttTime |
cbd41522 |
1166 | guint16 evId; |
1167 | gint64 nanoSec, tmpCycleCount = (((guint64)1)<<32); |
e4eced0f |
1168 | |
cbd41522 |
1169 | evId = *(guint16 *)tf->cur_event_pos; |
e4eced0f |
1170 | if(evId == TRACE_BLOCK_START){ |
dd691a2e |
1171 | tf->count = 0; |
1172 | tf->pre_cycle_count = 0; |
e4eced0f |
1173 | tf->cur_cycle_count = tf->a_block_start->cycle_count; |
40331ba8 |
1174 | return tf->a_block_start->time; |
e4eced0f |
1175 | }else if(evId == TRACE_BLOCK_END){ |
dd691a2e |
1176 | tf->count = 0; |
1177 | tf->pre_cycle_count = 0; |
e4eced0f |
1178 | tf->cur_cycle_count = tf->a_block_end->cycle_count; |
40331ba8 |
1179 | return tf->a_block_end->time; |
e4eced0f |
1180 | } |
40331ba8 |
1181 | |
e4eced0f |
1182 | // Calculate total time in cycles from start of buffer for this event |
cbd41522 |
1183 | cycle_count = (LttCycleCount)*(guint32 *)(tf->cur_event_pos + EVENT_ID_SIZE); |
e4eced0f |
1184 | |
dd691a2e |
1185 | if(cycle_count < tf->pre_cycle_count)tf->count++; |
1186 | tf->pre_cycle_count = cycle_count; |
1187 | cycle_count += tmpCycleCount * tf->count; |
e4eced0f |
1188 | |
dd691a2e |
1189 | if(tf->cur_heart_beat_number > tf->count) |
1190 | cycle_count += tmpCycleCount * (tf->cur_heart_beat_number - tf->count); |
e4eced0f |
1191 | |
1192 | tf->cur_cycle_count = cycle_count; |
1193 | |
1194 | lEventTotalCycle = cycle_count; |
1195 | lEventTotalCycle -= tf->a_block_start->cycle_count; |
6cd62ccf |
1196 | |
963b5f2d |
1197 | // Convert it to nsecs |
6cd62ccf |
1198 | lEventNSec = lEventTotalCycle / tf->cycle_per_nsec; |
e4eced0f |
1199 | nanoSec = lEventNSec; |
1200 | |
963b5f2d |
1201 | // Determine offset in struct LttTime |
308711e5 |
1202 | lTimeOffset.tv_nsec = nanoSec % NANOSECONDS_PER_SECOND; |
1203 | lTimeOffset.tv_sec = nanoSec / NANOSECONDS_PER_SECOND; |
6cd62ccf |
1204 | |
308711e5 |
1205 | time = ltt_time_add(tf->a_block_start->time, lTimeOffset); |
e4eced0f |
1206 | |
6cd62ccf |
1207 | return time; |
1208 | } |
1209 | |
1210 | /***************************************************************************** |
1211 | *Function name |
1212 | * setFieldsOffset : set offset of the fields |
1213 | *Input params |
1214 | * tracefile : opened trace file |
1215 | * evT : the event type |
1216 | * evD : event data, it may be NULL |
1217 | ****************************************************************************/ |
1218 | |
40331ba8 |
1219 | void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t) |
6cd62ccf |
1220 | { |
963b5f2d |
1221 | LttField * rootFld = evT->root_field; |
6cd62ccf |
1222 | // rootFld->base_address = evD; |
1223 | |
8710c6c7 |
1224 | if(rootFld) |
1225 | rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t); |
6cd62ccf |
1226 | } |
1227 | |
1228 | /***************************************************************************** |
1229 | *Function name |
1230 | * getFieldtypeSize: get the size of the field type (primitive type) |
1231 | *Input params |
1232 | * tracefile : opened trace file |
1233 | * evT : event type |
1234 | * offsetRoot : offset from the root |
1235 | * offsetParent : offset from the parrent |
1236 | * fld : field |
1237 | * evD : event data, it may be NULL |
1238 | *Return value |
1239 | * int : size of the field |
1240 | ****************************************************************************/ |
1241 | |
963b5f2d |
1242 | int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot, |
40331ba8 |
1243 | int offsetParent, LttField * fld, void *evD, LttTrace *trace) |
6cd62ccf |
1244 | { |
1245 | int size, size1, element_number, i, offset1, offset2; |
963b5f2d |
1246 | LttType * type = fld->field_type; |
6cd62ccf |
1247 | |
8710c6c7 |
1248 | if(t){ |
963b5f2d |
1249 | if(evT->latest_block==t->which_block && evT->latest_event==t->which_event){ |
1250 | return fld->field_size; |
1251 | } |
1252 | } |
6cd62ccf |
1253 | |
1254 | if(fld->field_fixed == 1){ |
1255 | if(fld == evT->root_field) return fld->field_size; |
1256 | } |
1257 | |
1258 | if(type->type_class != LTT_STRUCT && type->type_class != LTT_ARRAY && |
1259 | type->type_class != LTT_SEQUENCE && type->type_class != LTT_STRING){ |
1260 | if(fld->field_fixed == -1){ |
40331ba8 |
1261 | size = (int) ltt_type_size(trace, type); |
6cd62ccf |
1262 | fld->field_fixed = 1; |
1263 | }else size = fld->field_size; |
1264 | |
1265 | }else if(type->type_class == LTT_ARRAY){ |
1266 | element_number = (int) type->element_number; |
1267 | if(fld->field_fixed == -1){ |
40331ba8 |
1268 | size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace); |
6cd62ccf |
1269 | if(size == 0){ //has string or sequence |
1270 | fld->field_fixed = 0; |
1271 | }else{ |
1272 | fld->field_fixed = 1; |
1273 | size *= element_number; |
1274 | } |
1275 | }else if(fld->field_fixed == 0){// has string or sequence |
1276 | size = 0; |
1277 | for(i=0;i<element_number;i++){ |
1278 | size += getFieldtypeSize(t, evT, offsetRoot+size,size, |
40331ba8 |
1279 | fld->child[0], evD+size, trace); |
6cd62ccf |
1280 | } |
1281 | }else size = fld->field_size; |
1282 | |
1283 | }else if(type->type_class == LTT_SEQUENCE){ |
40331ba8 |
1284 | size1 = (int) ltt_type_size(trace, type); |
6cd62ccf |
1285 | if(fld->field_fixed == -1){ |
908f42fa |
1286 | fld->sequ_number_size = size1; |
6cd62ccf |
1287 | fld->field_fixed = 0; |
40331ba8 |
1288 | size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace); |
6cd62ccf |
1289 | fld->element_size = size; |
1290 | }else{//0: sequence |
1291 | element_number = getIntNumber(size1,evD); |
1292 | type->element_number = element_number; |
1293 | if(fld->element_size > 0){ |
1294 | size = element_number * fld->element_size; |
1295 | }else{//sequence has string or sequence |
1296 | size = 0; |
1297 | for(i=0;i<element_number;i++){ |
1298 | size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1, |
40331ba8 |
1299 | fld->child[0], evD+size+size1, trace); |
6cd62ccf |
1300 | } |
1301 | } |
1302 | size += size1; |
1303 | } |
1304 | |
1305 | }else if(type->type_class == LTT_STRING){ |
1306 | size = 0; |
1307 | if(fld->field_fixed == -1){ |
1308 | fld->field_fixed = 0; |
1309 | }else{//0: string |
47a166fc |
1310 | size = strlen((char*)evD) + 1; //include end : '\0' |
6cd62ccf |
1311 | } |
1312 | |
1313 | }else if(type->type_class == LTT_STRUCT){ |
1314 | element_number = (int) type->element_number; |
1315 | size = 0; |
1316 | if(fld->field_fixed == -1){ |
1317 | offset1 = offsetRoot; |
1318 | offset2 = 0; |
1319 | for(i=0;i<element_number;i++){ |
40331ba8 |
1320 | size1=getFieldtypeSize(t, evT,offset1,offset2, fld->child[i], NULL, trace); |
6cd62ccf |
1321 | if(size1 > 0 && size >= 0){ |
1322 | size += size1; |
1323 | if(offset1 >= 0) offset1 += size1; |
1324 | offset2 += size1; |
1325 | }else{ |
1326 | size = -1; |
1327 | offset1 = -1; |
1328 | offset2 = -1; |
1329 | } |
1330 | } |
1331 | if(size == -1){ |
1332 | fld->field_fixed = 0; |
1333 | size = 0; |
1334 | }else fld->field_fixed = 1; |
1335 | }else if(fld->field_fixed == 0){ |
1336 | offset1 = offsetRoot; |
1337 | offset2 = 0; |
1338 | for(i=0;i<element_number;i++){ |
40331ba8 |
1339 | size=getFieldtypeSize(t,evT,offset1,offset2,fld->child[i],evD+offset2, trace); |
6cd62ccf |
1340 | offset1 += size; |
1341 | offset2 += size; |
1342 | } |
1343 | size = offset2; |
1344 | }else size = fld->field_size; |
1345 | } |
1346 | |
1347 | fld->offset_root = offsetRoot; |
1348 | fld->offset_parent = offsetParent; |
1349 | if(!evD){ |
1350 | fld->fixed_root = (offsetRoot==-1) ? 0 : 1; |
1351 | fld->fixed_parent = (offsetParent==-1) ? 0 : 1; |
1352 | } |
1353 | fld->field_size = size; |
1354 | |
1355 | return size; |
1356 | } |
1357 | |
6cd62ccf |
1358 | |
1359 | /***************************************************************************** |
1360 | *Function name |
1361 | * getIntNumber : get an integer number |
1362 | *Input params |
1363 | * size : the size of the integer |
1364 | * evD : the event data |
1365 | *Return value |
1366 | * int : an integer |
1367 | ****************************************************************************/ |
1368 | |
1369 | int getIntNumber(int size, void *evD) |
1370 | { |
cbd41522 |
1371 | gint64 i; |
1372 | if(size == 1) i = *(gint8 *)evD; |
1373 | else if(size == 2) i = *(gint16 *)evD; |
1374 | else if(size == 4) i = *(gint32 *)evD; |
1375 | else if(size == 8) i = *(gint64 *)evD; |
6cd62ccf |
1376 | |
1377 | return (int) i; |
1378 | } |
1379 | |
1380 | /***************************************************************************** |
1381 | *Function name |
1382 | * getDataEndianType : get the data type size and endian type of the local |
1383 | * machine |
1384 | *Input params |
1385 | * size : size of data type |
1386 | * endian : endian type, little or big |
1387 | ****************************************************************************/ |
1388 | |
963b5f2d |
1389 | void getDataEndianType(LttArchSize * size, LttArchEndian * endian) |
6cd62ccf |
1390 | { |
1391 | int i = 1; |
1392 | char c = (char) i; |
1393 | int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *); |
1394 | |
1395 | if(c == 1) *endian = LTT_LITTLE_ENDIAN; |
1396 | else *endian = LTT_BIG_ENDIAN; |
1397 | |
1398 | if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4) |
1399 | *size = LTT_LP32; |
1400 | else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4) |
1401 | *size = LTT_ILP32; |
1402 | else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8) |
1403 | *size = LTT_LP64; |
1404 | else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8) |
1405 | *size = LTT_ILP64; |
1406 | else *size = LTT_UNKNOWN; |
1407 | } |
1408 | |
a5dcde2f |
1409 | /* get the node name of the system */ |
1410 | |
1411 | char * ltt_trace_system_description_node_name (LttSystemDescription * s) |
1412 | { |
1413 | return s->node_name; |
1414 | } |
1415 | |
1416 | |
1417 | /* get the domain name of the system */ |
1418 | |
1419 | char * ltt_trace_system_description_domain_name (LttSystemDescription * s) |
1420 | { |
1421 | return s->domain_name; |
1422 | } |
1423 | |
1424 | |
1425 | /* get the description of the system */ |
1426 | |
1427 | char * ltt_trace_system_description_description (LttSystemDescription * s) |
1428 | { |
1429 | return s->description; |
1430 | } |
1431 | |
1432 | |
1433 | /* get the start time of the trace */ |
1434 | |
1435 | LttTime ltt_trace_system_description_trace_start_time(LttSystemDescription *s) |
1436 | { |
1437 | return s->trace_start; |
1438 | } |
1439 | |