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