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 | } |
524 | g_ptr_array_free(t->control_tracefiles, FALSE); |
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 | } |
531 | g_ptr_array_free(t->per_cpu_tracefiles, FALSE); |
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 | } |
538 | g_ptr_array_free(t->facilities, FALSE); |
539 | |
540 | g_free(t); |
6cd62ccf |
541 | } |
542 | |
963b5f2d |
543 | |
6cd62ccf |
544 | /***************************************************************************** |
963b5f2d |
545 | *Get the system description of the trace |
6cd62ccf |
546 | ****************************************************************************/ |
547 | |
963b5f2d |
548 | LttSystemDescription *ltt_trace_system_description(LttTrace *t) |
6cd62ccf |
549 | { |
963b5f2d |
550 | return t->system_description; |
6cd62ccf |
551 | } |
552 | |
553 | /***************************************************************************** |
963b5f2d |
554 | * The following functions discover the facilities of the trace |
6cd62ccf |
555 | ****************************************************************************/ |
556 | |
963b5f2d |
557 | unsigned ltt_trace_facility_number(LttTrace *t) |
558 | { |
559 | return (unsigned)(t->facility_number); |
560 | } |
561 | |
562 | LttFacility *ltt_trace_facility_get(LttTrace *t, unsigned i) |
6cd62ccf |
563 | { |
963b5f2d |
564 | return (LttFacility*)g_ptr_array_index(t->facilities, i); |
6cd62ccf |
565 | } |
566 | |
567 | /***************************************************************************** |
568 | *Function name |
963b5f2d |
569 | * ltt_trace_facility_find : find facilities in the trace |
6cd62ccf |
570 | *Input params |
963b5f2d |
571 | * t : the trace |
572 | * name : facility name |
573 | *Output params |
574 | * position : position of the facility in the trace |
6cd62ccf |
575 | *Return value |
963b5f2d |
576 | * : the number of facilities |
6cd62ccf |
577 | ****************************************************************************/ |
578 | |
963b5f2d |
579 | unsigned ltt_trace_facility_find(LttTrace *t, char *name, unsigned *position) |
6cd62ccf |
580 | { |
963b5f2d |
581 | int i, count=0; |
582 | LttFacility * f; |
583 | for(i=0;i=t->facility_number;i++){ |
584 | f = (LttFacility*)g_ptr_array_index(t->facilities, i); |
585 | if(strcmp(f->name,name)==0){ |
586 | count++; |
587 | if(count==1) *position = i; |
588 | }else{ |
589 | if(count) break; |
590 | } |
591 | } |
592 | return count; |
6cd62ccf |
593 | } |
594 | |
595 | /***************************************************************************** |
963b5f2d |
596 | * Functions to discover all the event types in the trace |
6cd62ccf |
597 | ****************************************************************************/ |
598 | |
963b5f2d |
599 | unsigned ltt_trace_eventtype_number(LttTrace *t) |
6cd62ccf |
600 | { |
963b5f2d |
601 | int i; |
602 | unsigned count = 0; |
603 | LttFacility * f; |
604 | for(i=0;i=t->facility_number;i++){ |
605 | f = (LttFacility*)g_ptr_array_index(t->facilities, i); |
606 | count += f->event_number; |
607 | } |
608 | return count; |
6cd62ccf |
609 | } |
610 | |
963b5f2d |
611 | LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id) |
6cd62ccf |
612 | { |
963b5f2d |
613 | LttFacility * facility; |
614 | int i; |
615 | for(i=0;i<trace->facility_number;i++){ |
616 | facility = (LttFacility*) g_ptr_array_index(trace->facilities,i); |
617 | if(id >= facility->base_id && |
618 | id < facility->base_id + facility->event_number) |
619 | break; |
620 | } |
621 | if(i==trace->facility_number) return NULL; |
622 | else return facility; |
6cd62ccf |
623 | } |
624 | |
963b5f2d |
625 | LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId) |
6cd62ccf |
626 | { |
963b5f2d |
627 | LttFacility * f; |
628 | f = ltt_trace_facility_by_id(t,evId); |
629 | if(!f) return NULL; |
630 | return f->events[evId - f->base_id]; |
6cd62ccf |
631 | } |
632 | |
633 | /***************************************************************************** |
963b5f2d |
634 | *There is one "per cpu" tracefile for each CPU, numbered from 0 to |
635 | *the maximum number of CPU in the system. When the number of CPU installed |
636 | *is less than the maximum, some positions are unused. There are also a |
637 | *number of "control" tracefiles (facilities, interrupts...). |
6cd62ccf |
638 | ****************************************************************************/ |
963b5f2d |
639 | unsigned ltt_trace_control_tracefile_number(LttTrace *t) |
6cd62ccf |
640 | { |
963b5f2d |
641 | return t->control_tracefile_number; |
6cd62ccf |
642 | } |
643 | |
963b5f2d |
644 | unsigned ltt_trace_per_cpu_tracefile_number(LttTrace *t) |
6cd62ccf |
645 | { |
963b5f2d |
646 | return t->per_cpu_tracefile_number; |
6cd62ccf |
647 | } |
648 | |
649 | /***************************************************************************** |
963b5f2d |
650 | *It is possible to search for the tracefiles by name or by CPU position. |
651 | *The index within the tracefiles of the same type is returned if found |
652 | *and a negative value otherwise. |
6cd62ccf |
653 | ****************************************************************************/ |
654 | |
963b5f2d |
655 | int ltt_trace_control_tracefile_find(LttTrace *t, char *name) |
6cd62ccf |
656 | { |
963b5f2d |
657 | LttTracefile * tracefile; |
658 | int i; |
659 | for(i=0;i<t->control_tracefile_number;i++){ |
660 | tracefile = (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i); |
661 | if(strcmp(tracefile->name, name)==0)break; |
662 | } |
663 | if(i == t->control_tracefile_number) return -1; |
664 | return i; |
6cd62ccf |
665 | } |
666 | |
963b5f2d |
667 | int ltt_trace_per_cpu_tracefile_find(LttTrace *t, unsigned i) |
6cd62ccf |
668 | { |
963b5f2d |
669 | LttTracefile * tracefile; |
670 | int j, name; |
671 | for(j=0;j<t->per_cpu_tracefile_number;j++){ |
672 | tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, j); |
673 | name = atoi(tracefile->name); |
674 | if(name == (int)i)break; |
675 | } |
676 | if(j == t->per_cpu_tracefile_number) return -1; |
677 | return j; |
6cd62ccf |
678 | } |
679 | |
680 | /***************************************************************************** |
963b5f2d |
681 | *Get a specific tracefile |
6cd62ccf |
682 | ****************************************************************************/ |
683 | |
963b5f2d |
684 | LttTracefile *ltt_trace_control_tracefile_get(LttTrace *t, unsigned i) |
6cd62ccf |
685 | { |
963b5f2d |
686 | return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i); |
687 | } |
688 | |
689 | LttTracefile *ltt_trace_per_cpu_tracefile_get(LttTrace *t, unsigned i) |
690 | { |
691 | return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i); |
6cd62ccf |
692 | } |
693 | |
694 | /***************************************************************************** |
963b5f2d |
695 | *Get the name of a tracefile |
6cd62ccf |
696 | ****************************************************************************/ |
697 | |
963b5f2d |
698 | char *ltt_tracefile_name(LttTracefile *tf) |
6cd62ccf |
699 | { |
963b5f2d |
700 | return tf->name; |
6cd62ccf |
701 | } |
702 | |
703 | /***************************************************************************** |
704 | *Function name |
705 | * ltt_tracefile_seek_time: seek to the first event of the trace with time |
706 | * larger or equal to time |
707 | *Input params |
708 | * t : tracefile |
709 | * time : criteria of the time |
6cd62ccf |
710 | ****************************************************************************/ |
711 | |
963b5f2d |
712 | void ltt_tracefile_seek_time(LttTracefile *t, LttTime time) |
6cd62ccf |
713 | { |
714 | int err; |
963b5f2d |
715 | LttTime lttTime; |
716 | int headTime = timecmp(&(t->a_block_start->time), &time); |
717 | int tailTime = timecmp(&(t->a_block_end->time), &time); |
62e55dd6 |
718 | LttEvent * ev; |
719 | |
6cd62ccf |
720 | if(headTime < 0 && tailTime > 0){ |
721 | lttTime = getEventTime(t); |
722 | err = timecmp(<tTime, &time); |
963b5f2d |
723 | if(err > 0){ |
40331ba8 |
724 | if(t->which_event==2 || timecmp(&t->prev_event_time,&time)<0){ |
963b5f2d |
725 | return; |
726 | }else{ |
6cd62ccf |
727 | updateTracefile(t); |
728 | return ltt_tracefile_seek_time(t, time); |
729 | } |
730 | }else if(err < 0){ |
1a3b8cbd |
731 | while(1){ |
732 | ev = ltt_tracefile_read(t); |
733 | if(ev == NULL){ |
734 | g_print("End of file\n"); |
735 | return; |
736 | } |
737 | g_free(ev); |
738 | lttTime = getEventTime(t); |
739 | err = timecmp(<tTime, &time); |
740 | if(err >= 0)return; |
963b5f2d |
741 | } |
963b5f2d |
742 | }else return; |
6cd62ccf |
743 | }else if(headTime > 0){ |
744 | if(t->which_block == 1){ |
745 | updateTracefile(t); |
746 | }else{ |
40331ba8 |
747 | if(timecmp(&(t->prev_block_end_time),&time) >= 0 ){ |
6cd62ccf |
748 | err=readBlock(t,t->which_block-1); |
963b5f2d |
749 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
6cd62ccf |
750 | return ltt_tracefile_seek_time(t, time) ; |
751 | }else{ |
752 | updateTracefile(t); |
753 | } |
754 | } |
40331ba8 |
755 | }else if(tailTime < 0){ |
6cd62ccf |
756 | if(t->which_block != t->block_number){ |
757 | err=readBlock(t,t->which_block+1); |
963b5f2d |
758 | if(err) g_error("Can not read tracefile: %s\n", t->name); |
759 | }else { |
25975c93 |
760 | g_print("End of file\n"); |
963b5f2d |
761 | return; |
762 | } |
6cd62ccf |
763 | if(tailTime < 0) return ltt_tracefile_seek_time(t, time); |
764 | }else if(headTime == 0){ |
765 | updateTracefile(t); |
40331ba8 |
766 | }else if(tailTime == 0){ |
1a3b8cbd |
767 | t->cur_event_pos = t->a_block_end - EVENT_HEADER_SIZE; |
62e55dd6 |
768 | t->current_event_time = time; |
769 | t->cur_heart_beat_number = 0; |
770 | t->prev_event_time.tv_sec = 0; |
771 | t->prev_event_time.tv_nsec = 0; |
40331ba8 |
772 | return; |
6cd62ccf |
773 | } |
6cd62ccf |
774 | } |
775 | |
776 | /***************************************************************************** |
777 | *Function name |
40331ba8 |
778 | * ltt_tracefile_read : read the current event, set the pointer to the next |
6cd62ccf |
779 | *Input params |
780 | * t : tracefile |
781 | *Return value |
963b5f2d |
782 | * LttEvent * : an event to be processed |
6cd62ccf |
783 | ****************************************************************************/ |
784 | |
963b5f2d |
785 | LttEvent *ltt_tracefile_read(LttTracefile *t) |
6cd62ccf |
786 | { |
963b5f2d |
787 | LttEvent * lttEvent = (LttEvent *)g_new(LttEvent, 1); |
788 | int err; |
6cd62ccf |
789 | |
963b5f2d |
790 | lttEvent->event_id = (int)(*(uint16_t *)(t->cur_event_pos)); |
791 | if(lttEvent->event_id == TRACE_TIME_HEARTBEAT) |
792 | t->cur_heart_beat_number++; |
6cd62ccf |
793 | |
40331ba8 |
794 | t->prev_event_time = t->current_event_time; |
62e55dd6 |
795 | // t->current_event_time = getEventTime(t); |
6cd62ccf |
796 | |
963b5f2d |
797 | lttEvent->time_delta = *(uint32_t*)(t->cur_event_pos + EVENT_ID_SIZE); |
798 | lttEvent->event_time = t->current_event_time; |
799 | |
800 | lttEvent->event_cycle_count = ((uint64_t)1)<<32 * t->cur_heart_beat_number |
801 | + lttEvent->time_delta; |
802 | |
6cd62ccf |
803 | lttEvent->tracefile = t; |
804 | lttEvent->data = t->cur_event_pos + EVENT_HEADER_SIZE; |
805 | |
40331ba8 |
806 | //update the fields of the current event and go to the next event |
807 | err = skipEvent(t); |
663bd9ff |
808 | if(err == ENOMEM){ |
809 | g_free(lttEvent); |
810 | return NULL; |
811 | } |
99ff36b1 |
812 | if(err == ENOENT) return lttEvent; |
40331ba8 |
813 | if(err == ERANGE) g_error("event id is out of range\n"); |
814 | if(err)g_error("Can not read tracefile\n"); |
815 | |
6cd62ccf |
816 | return lttEvent; |
817 | } |
818 | |
819 | /**************************************************************************** |
820 | *Function name |
821 | * readFile : wrap function to read from a file |
822 | *Input Params |
823 | * fd : file descriptor |
824 | * buf : buf to contain the content |
825 | * size : number of bytes to be read |
826 | * mesg : message to be printed if some thing goes wrong |
827 | *return value |
828 | * 0 : success |
829 | * EIO : can not read from the file |
830 | ****************************************************************************/ |
831 | |
832 | int readFile(int fd, void * buf, size_t size, char * mesg) |
833 | { |
834 | ssize_t nbBytes; |
835 | nbBytes = read(fd, buf, size); |
836 | if(nbBytes != size){ |
837 | printf("%s\n",mesg); |
838 | return EIO; |
839 | } |
840 | return 0; |
841 | } |
842 | |
843 | /**************************************************************************** |
844 | *Function name |
845 | * readBlock : read a block from the file |
846 | *Input Params |
847 | * lttdes : ltt trace file |
848 | * whichBlock : the block which will be read |
849 | *return value |
850 | * 0 : success |
851 | * EINVAL : lseek fail |
852 | * EIO : can not read from the file |
853 | ****************************************************************************/ |
854 | |
963b5f2d |
855 | int readBlock(LttTracefile * tf, int whichBlock) |
6cd62ccf |
856 | { |
857 | off_t nbBytes; |
858 | uint32_t lostSize; |
859 | |
860 | if(whichBlock - tf->which_block == 1 && tf->which_block != 0){ |
963b5f2d |
861 | tf->prev_block_end_time = tf->a_block_end->time; |
40331ba8 |
862 | tf->prev_event_time = tf->a_block_end->time; |
6cd62ccf |
863 | }else{ |
864 | tf->prev_block_end_time.tv_sec = 0; |
865 | tf->prev_block_end_time.tv_nsec = 0; |
40331ba8 |
866 | tf->prev_event_time.tv_sec = 0; |
867 | tf->prev_event_time.tv_nsec = 0; |
6cd62ccf |
868 | } |
6cd62ccf |
869 | |
963b5f2d |
870 | nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET); |
6cd62ccf |
871 | if(nbBytes == -1) return EINVAL; |
872 | |
963b5f2d |
873 | if(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block")) |
874 | return EIO; |
6cd62ccf |
875 | |
963b5f2d |
876 | tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE); |
877 | lostSize = *(uint32_t*)(tf->buffer + tf->block_size - sizeof(uint32_t)); |
878 | tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size - |
879 | lostSize + EVENT_HEADER_SIZE); |
6cd62ccf |
880 | |
6cd62ccf |
881 | tf->which_block = whichBlock; |
963b5f2d |
882 | tf->which_event = 1; |
40331ba8 |
883 | tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev |
6cd62ccf |
884 | tf->cur_heart_beat_number = 0; |
963b5f2d |
885 | |
6cd62ccf |
886 | getCyclePerNsec(tf); |
887 | |
62e55dd6 |
888 | tf->current_event_time = getEventTime(tf); |
40331ba8 |
889 | |
6cd62ccf |
890 | return 0; |
891 | } |
892 | |
893 | /***************************************************************************** |
894 | *Function name |
895 | * updateTracefile : reinitialize the info of the block which is already |
896 | * in the buffer |
897 | *Input params |
898 | * tf : tracefile |
899 | ****************************************************************************/ |
900 | |
963b5f2d |
901 | void updateTracefile(LttTracefile * tf) |
6cd62ccf |
902 | { |
963b5f2d |
903 | tf->which_event = 1; |
40331ba8 |
904 | tf->cur_event_pos = tf->buffer; |
62e55dd6 |
905 | tf->current_event_time = getEventTime(tf); |
6cd62ccf |
906 | tf->cur_heart_beat_number = 0; |
907 | |
908 | tf->prev_event_time.tv_sec = 0; |
909 | tf->prev_event_time.tv_nsec = 0; |
910 | } |
911 | |
912 | /***************************************************************************** |
913 | *Function name |
914 | * skipEvent : go to the next event, update the fields of the current event |
915 | *Input params |
916 | * t : tracefile |
917 | *return value |
918 | * 0 : success |
919 | * EINVAL : lseek fail |
920 | * EIO : can not read from the file |
99ff36b1 |
921 | * ENOMEM : end of file |
922 | * ENOENT : last event |
6cd62ccf |
923 | * ERANGE : event id is out of range |
924 | ****************************************************************************/ |
925 | |
963b5f2d |
926 | int skipEvent(LttTracefile * t) |
6cd62ccf |
927 | { |
928 | int evId, err; |
929 | void * evData; |
963b5f2d |
930 | LttEventType * evT; |
931 | LttField * rootFld; |
99ff36b1 |
932 | static int evCount = 0; |
933 | |
934 | if(evCount){ |
935 | if(t->which_block == t->block_number && |
936 | evCount == t->which_event){ |
937 | return ENOMEM; |
938 | }else evCount = 0; |
939 | } |
6cd62ccf |
940 | |
963b5f2d |
941 | evId = (int)(*(uint16_t *)(t->cur_event_pos)); |
6cd62ccf |
942 | evData = t->cur_event_pos + EVENT_HEADER_SIZE; |
6cd62ccf |
943 | |
47a166fc |
944 | //regard BLOCK_START, END and HEARTBEAT as special case, there are buildin events |
945 | if(evId != TRACE_BLOCK_START && evId != TRACE_BLOCK_END && evId != TRACE_TIME_HEARTBEAT){ |
946 | evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId); |
947 | |
948 | if(evT) rootFld = evT->root_field; |
949 | else return ERANGE; |
6cd62ccf |
950 | |
47a166fc |
951 | if(rootFld){ |
952 | //event has string/sequence or the last event is not the same event |
953 | if((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event) |
954 | && rootFld->field_fixed == 0){ |
955 | setFieldsOffset(t, evT, evData, t->trace); |
956 | } |
957 | t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size; |
958 | }else t->cur_event_pos += EVENT_HEADER_SIZE; |
959 | |
960 | evT->latest_block = t->which_block; |
961 | evT->latest_event = t->which_event; |
962 | }else{ |
963 | if(evId == TRACE_BLOCK_START || evId == TRACE_BLOCK_END){ |
964 | t->cur_event_pos += sizeof(BlockStart) + EVENT_HEADER_SIZE; |
965 | }else{ |
966 | t->cur_event_pos += sizeof(TimeHeartbeat) + EVENT_HEADER_SIZE; |
967 | } |
6cd62ccf |
968 | } |
6cd62ccf |
969 | |
970 | //the next event is in the next block |
963b5f2d |
971 | if(evId == TRACE_BLOCK_END){ |
99ff36b1 |
972 | if(t->which_block == t->block_number){ |
973 | t->which_event++; |
974 | evCount = t->which_event; |
975 | return ENOENT; |
976 | } |
6cd62ccf |
977 | err = readBlock(t, t->which_block + 1); |
978 | if(err) return err; |
979 | }else{ |
980 | t->which_event++; |
62e55dd6 |
981 | t->current_event_time = getEventTime(t); |
6cd62ccf |
982 | } |
983 | |
984 | return 0; |
985 | } |
986 | |
987 | /***************************************************************************** |
988 | *Function name |
989 | * getCyclePerNsec : calculate cycles per nsec for current block |
990 | *Input Params |
991 | * t : tracefile |
992 | ****************************************************************************/ |
993 | |
963b5f2d |
994 | void getCyclePerNsec(LttTracefile * t) |
6cd62ccf |
995 | { |
963b5f2d |
996 | LttTime lBufTotalTime; /* Total time for this buffer */ |
997 | LttCycleCount lBufTotalNSec; /* Total time for this buffer in nsecs */ |
998 | LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */ |
6cd62ccf |
999 | |
1000 | /* Calculate the total time for this buffer */ |
963b5f2d |
1001 | TimeSub(lBufTotalTime,t->a_block_end->time, t->a_block_start->time); |
6cd62ccf |
1002 | |
1003 | /* Calculate the total cycles for this bufffer */ |
963b5f2d |
1004 | lBufTotalCycle = t->a_block_end->cycle_count |
1005 | - t->a_block_start->cycle_count; |
6cd62ccf |
1006 | |
1007 | /* Convert the total time to nsecs */ |
1008 | lBufTotalNSec = lBufTotalTime.tv_sec * 1000000000 + lBufTotalTime.tv_nsec; |
1009 | |
1010 | t->cycle_per_nsec = (double)lBufTotalCycle / (double)lBufTotalNSec; |
1011 | } |
1012 | |
1013 | /**************************************************************************** |
1014 | *Function name |
1015 | * getEventTime : obtain the time of an event |
1016 | *Input params |
1017 | * tf : tracefile |
1018 | *Return value |
963b5f2d |
1019 | * LttTime : the time of the event |
6cd62ccf |
1020 | ****************************************************************************/ |
1021 | |
963b5f2d |
1022 | LttTime getEventTime(LttTracefile * tf) |
6cd62ccf |
1023 | { |
963b5f2d |
1024 | LttTime time; |
1025 | LttCycleCount cycle_count; // cycle count for the current event |
1026 | LttCycleCount lEventTotalCycle; // Total cycles from start for event |
1027 | double lEventNSec; // Total usecs from start for event |
1028 | LttTime lTimeOffset; // Time offset in struct LttTime |
40331ba8 |
1029 | uint16_t evId; |
6cd62ccf |
1030 | |
40331ba8 |
1031 | evId = *(uint16_t*)tf->cur_event_pos; |
1032 | if(evId == TRACE_BLOCK_START) |
1033 | return tf->a_block_start->time; |
1034 | else if(evId == TRACE_BLOCK_END) |
1035 | return tf->a_block_end->time; |
1036 | |
1037 | |
963b5f2d |
1038 | // Calculate total time in cycles from start of buffer for this event |
1039 | cycle_count = (LttCycleCount)*(uint32_t*)(tf->cur_event_pos + EVENT_ID_SIZE); |
6cd62ccf |
1040 | if(tf->cur_heart_beat_number) |
1041 | cycle_count += ((uint64_t)1)<<32 * tf->cur_heart_beat_number; |
963b5f2d |
1042 | lEventTotalCycle = cycle_count - tf->a_block_start->cycle_count; |
6cd62ccf |
1043 | |
963b5f2d |
1044 | // Convert it to nsecs |
6cd62ccf |
1045 | lEventNSec = lEventTotalCycle / tf->cycle_per_nsec; |
1046 | |
963b5f2d |
1047 | // Determine offset in struct LttTime |
6cd62ccf |
1048 | lTimeOffset.tv_nsec = (long)lEventNSec % 1000000000; |
1049 | lTimeOffset.tv_sec = (long)lEventNSec / 1000000000; |
1050 | |
963b5f2d |
1051 | TimeAdd(time, tf->a_block_start->time, lTimeOffset); |
6cd62ccf |
1052 | |
1053 | return time; |
1054 | } |
1055 | |
1056 | /***************************************************************************** |
1057 | *Function name |
1058 | * setFieldsOffset : set offset of the fields |
1059 | *Input params |
1060 | * tracefile : opened trace file |
1061 | * evT : the event type |
1062 | * evD : event data, it may be NULL |
1063 | ****************************************************************************/ |
1064 | |
40331ba8 |
1065 | void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t) |
6cd62ccf |
1066 | { |
963b5f2d |
1067 | LttField * rootFld = evT->root_field; |
6cd62ccf |
1068 | // rootFld->base_address = evD; |
1069 | |
8710c6c7 |
1070 | if(rootFld) |
1071 | rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t); |
6cd62ccf |
1072 | } |
1073 | |
1074 | /***************************************************************************** |
1075 | *Function name |
1076 | * getFieldtypeSize: get the size of the field type (primitive type) |
1077 | *Input params |
1078 | * tracefile : opened trace file |
1079 | * evT : event type |
1080 | * offsetRoot : offset from the root |
1081 | * offsetParent : offset from the parrent |
1082 | * fld : field |
1083 | * evD : event data, it may be NULL |
1084 | *Return value |
1085 | * int : size of the field |
1086 | ****************************************************************************/ |
1087 | |
963b5f2d |
1088 | int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot, |
40331ba8 |
1089 | int offsetParent, LttField * fld, void *evD, LttTrace *trace) |
6cd62ccf |
1090 | { |
1091 | int size, size1, element_number, i, offset1, offset2; |
963b5f2d |
1092 | LttType * type = fld->field_type; |
6cd62ccf |
1093 | |
8710c6c7 |
1094 | if(t){ |
963b5f2d |
1095 | if(evT->latest_block==t->which_block && evT->latest_event==t->which_event){ |
1096 | return fld->field_size; |
1097 | } |
1098 | } |
6cd62ccf |
1099 | |
1100 | if(fld->field_fixed == 1){ |
1101 | if(fld == evT->root_field) return fld->field_size; |
1102 | } |
1103 | |
1104 | if(type->type_class != LTT_STRUCT && type->type_class != LTT_ARRAY && |
1105 | type->type_class != LTT_SEQUENCE && type->type_class != LTT_STRING){ |
1106 | if(fld->field_fixed == -1){ |
40331ba8 |
1107 | size = (int) ltt_type_size(trace, type); |
6cd62ccf |
1108 | fld->field_fixed = 1; |
1109 | }else size = fld->field_size; |
1110 | |
1111 | }else if(type->type_class == LTT_ARRAY){ |
1112 | element_number = (int) type->element_number; |
1113 | if(fld->field_fixed == -1){ |
40331ba8 |
1114 | size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace); |
6cd62ccf |
1115 | if(size == 0){ //has string or sequence |
1116 | fld->field_fixed = 0; |
1117 | }else{ |
1118 | fld->field_fixed = 1; |
1119 | size *= element_number; |
1120 | } |
1121 | }else if(fld->field_fixed == 0){// has string or sequence |
1122 | size = 0; |
1123 | for(i=0;i<element_number;i++){ |
1124 | size += getFieldtypeSize(t, evT, offsetRoot+size,size, |
40331ba8 |
1125 | fld->child[0], evD+size, trace); |
6cd62ccf |
1126 | } |
1127 | }else size = fld->field_size; |
1128 | |
1129 | }else if(type->type_class == LTT_SEQUENCE){ |
40331ba8 |
1130 | size1 = (int) ltt_type_size(trace, type); |
6cd62ccf |
1131 | if(fld->field_fixed == -1){ |
1132 | fld->field_fixed = 0; |
40331ba8 |
1133 | size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace); |
6cd62ccf |
1134 | fld->element_size = size; |
1135 | }else{//0: sequence |
1136 | element_number = getIntNumber(size1,evD); |
1137 | type->element_number = element_number; |
1138 | if(fld->element_size > 0){ |
1139 | size = element_number * fld->element_size; |
1140 | }else{//sequence has string or sequence |
1141 | size = 0; |
1142 | for(i=0;i<element_number;i++){ |
1143 | size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1, |
40331ba8 |
1144 | fld->child[0], evD+size+size1, trace); |
6cd62ccf |
1145 | } |
1146 | } |
1147 | size += size1; |
1148 | } |
1149 | |
1150 | }else if(type->type_class == LTT_STRING){ |
1151 | size = 0; |
1152 | if(fld->field_fixed == -1){ |
1153 | fld->field_fixed = 0; |
1154 | }else{//0: string |
47a166fc |
1155 | size = strlen((char*)evD) + 1; //include end : '\0' |
6cd62ccf |
1156 | } |
1157 | |
1158 | }else if(type->type_class == LTT_STRUCT){ |
1159 | element_number = (int) type->element_number; |
1160 | size = 0; |
1161 | if(fld->field_fixed == -1){ |
1162 | offset1 = offsetRoot; |
1163 | offset2 = 0; |
1164 | for(i=0;i<element_number;i++){ |
40331ba8 |
1165 | size1=getFieldtypeSize(t, evT,offset1,offset2, fld->child[i], NULL, trace); |
6cd62ccf |
1166 | if(size1 > 0 && size >= 0){ |
1167 | size += size1; |
1168 | if(offset1 >= 0) offset1 += size1; |
1169 | offset2 += size1; |
1170 | }else{ |
1171 | size = -1; |
1172 | offset1 = -1; |
1173 | offset2 = -1; |
1174 | } |
1175 | } |
1176 | if(size == -1){ |
1177 | fld->field_fixed = 0; |
1178 | size = 0; |
1179 | }else fld->field_fixed = 1; |
1180 | }else if(fld->field_fixed == 0){ |
1181 | offset1 = offsetRoot; |
1182 | offset2 = 0; |
1183 | for(i=0;i<element_number;i++){ |
40331ba8 |
1184 | size=getFieldtypeSize(t,evT,offset1,offset2,fld->child[i],evD+offset2, trace); |
6cd62ccf |
1185 | offset1 += size; |
1186 | offset2 += size; |
1187 | } |
1188 | size = offset2; |
1189 | }else size = fld->field_size; |
1190 | } |
1191 | |
1192 | fld->offset_root = offsetRoot; |
1193 | fld->offset_parent = offsetParent; |
1194 | if(!evD){ |
1195 | fld->fixed_root = (offsetRoot==-1) ? 0 : 1; |
1196 | fld->fixed_parent = (offsetParent==-1) ? 0 : 1; |
1197 | } |
1198 | fld->field_size = size; |
1199 | |
1200 | return size; |
1201 | } |
1202 | |
1203 | /***************************************************************************** |
1204 | *Function name |
1205 | * timecmp : compare two time |
1206 | *Input params |
1207 | * t1 : first time |
1208 | * t2 : second time |
1209 | *Return value |
1210 | * int : 0: t1 == t2; -1: t1 < t2; 1: t1 > t2 |
1211 | ****************************************************************************/ |
1212 | |
963b5f2d |
1213 | int timecmp(LttTime * t1, LttTime * t2) |
6cd62ccf |
1214 | { |
963b5f2d |
1215 | LttTime T; |
6cd62ccf |
1216 | TimeSub(T, *t1, *t2); |
1217 | if(T.tv_sec == 0 && T.tv_nsec == 0) return 0; |
1218 | else if(T.tv_sec > 0 || (T.tv_sec==0 && T.tv_nsec > 0)) return 1; |
1219 | else return -1; |
1220 | } |
1221 | |
1222 | /***************************************************************************** |
1223 | *Function name |
1224 | * getIntNumber : get an integer number |
1225 | *Input params |
1226 | * size : the size of the integer |
1227 | * evD : the event data |
1228 | *Return value |
1229 | * int : an integer |
1230 | ****************************************************************************/ |
1231 | |
1232 | int getIntNumber(int size, void *evD) |
1233 | { |
1234 | int64_t i; |
1235 | if(size == 1) i = *(int8_t *)evD; |
1236 | else if(size == 2) i = *(int16_t *)evD; |
1237 | else if(size == 4) i = *(int32_t *)evD; |
1238 | else if(size == 8) i = *(int64_t *)evD; |
1239 | |
1240 | return (int) i; |
1241 | } |
1242 | |
1243 | /***************************************************************************** |
1244 | *Function name |
1245 | * getDataEndianType : get the data type size and endian type of the local |
1246 | * machine |
1247 | *Input params |
1248 | * size : size of data type |
1249 | * endian : endian type, little or big |
1250 | ****************************************************************************/ |
1251 | |
963b5f2d |
1252 | void getDataEndianType(LttArchSize * size, LttArchEndian * endian) |
6cd62ccf |
1253 | { |
1254 | int i = 1; |
1255 | char c = (char) i; |
1256 | int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *); |
1257 | |
1258 | if(c == 1) *endian = LTT_LITTLE_ENDIAN; |
1259 | else *endian = LTT_BIG_ENDIAN; |
1260 | |
1261 | if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4) |
1262 | *size = LTT_LP32; |
1263 | else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4) |
1264 | *size = LTT_ILP32; |
1265 | else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8) |
1266 | *size = LTT_LP64; |
1267 | else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8) |
1268 | *size = LTT_ILP64; |
1269 | else *size = LTT_UNKNOWN; |
1270 | } |
1271 | |