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