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