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