449cb9d7 |
1 | /* This file is part of the Linux Trace Toolkit viewer |
3aee1200 |
2 | * Copyright (C) 2005 Mathieu Desnoyers |
449cb9d7 |
3 | * |
3aee1200 |
4 | * Complete rewrite from the original version made by XangXiu Yang. |
5 | * |
1b44b0b5 |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public |
8 | * License Version 2.1 as published by the Free Software Foundation. |
449cb9d7 |
9 | * |
1b44b0b5 |
10 | * This library is distributed in the hope that it will be useful, |
449cb9d7 |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1b44b0b5 |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. |
449cb9d7 |
14 | * |
1b44b0b5 |
15 | * You should have received a copy of the GNU Lesser General Public |
16 | * License along with this library; if not, write to the |
17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | * Boston, MA 02111-1307, USA. |
449cb9d7 |
19 | */ |
20 | |
4e4d11b3 |
21 | #ifdef HAVE_CONFIG_H |
22 | #include <config.h> |
23 | #endif |
24 | |
6cd62ccf |
25 | #include <stdio.h> |
26 | #include <fcntl.h> |
8d1e6362 |
27 | #include <string.h> |
28 | #include <dirent.h> |
6cd62ccf |
29 | #include <sys/stat.h> |
30 | #include <sys/types.h> |
8d1e6362 |
31 | #include <errno.h> |
32 | #include <unistd.h> |
42db9bf1 |
33 | #include <math.h> |
cdf90f40 |
34 | #include <glib.h> |
3aee1200 |
35 | #include <malloc.h> |
36 | #include <sys/mman.h> |
dd3a6d39 |
37 | #include <string.h> |
6cd62ccf |
38 | |
ef35d837 |
39 | // For realpath |
40 | #include <limits.h> |
41 | #include <stdlib.h> |
42 | |
43 | |
6cd62ccf |
44 | #include "parser.h" |
a5dcde2f |
45 | #include <ltt/ltt.h> |
46 | #include "ltt-private.h" |
963b5f2d |
47 | #include <ltt/trace.h> |
c02ea99f |
48 | #include <ltt/event.h> |
29af7cfd |
49 | //#include <ltt/type.h> |
1a2ceb63 |
50 | #include <ltt/ltt-types.h> |
bb38a290 |
51 | #include <ltt/marker.h> |
3aee1200 |
52 | |
53 | /* Facility names used in this file */ |
54 | |
55 | GQuark LTT_FACILITY_NAME_HEARTBEAT, |
d1bb700c |
56 | LTT_EVENT_NAME_HEARTBEAT, |
57 | LTT_EVENT_NAME_HEARTBEAT_FULL; |
3aee1200 |
58 | GQuark LTT_TRACEFILE_NAME_FACILITIES; |
59 | |
86005ded |
60 | #ifndef g_open |
61 | #define g_open open |
62 | #endif |
63 | |
64 | |
51b5991e |
65 | #define __UNUSED__ __attribute__((__unused__)) |
6cd62ccf |
66 | |
a1062ddd |
67 | #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format) |
3aee1200 |
68 | |
69 | #ifndef g_debug |
a1062ddd |
70 | #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format) |
3aee1200 |
71 | #endif |
a1062ddd |
72 | |
45e14832 |
73 | #define g_close close |
8959a0c8 |
74 | |
b77d1b57 |
75 | /* Those macros must be called from within a function where page_size is a known |
76 | * variable */ |
77 | #define PAGE_MASK (~(page_size-1)) |
78 | #define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK) |
79 | |
6cd62ccf |
80 | /* set the offset of the fields belonging to the event, |
81 | need the information of the archecture */ |
f104d082 |
82 | //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type); |
eed2ef37 |
83 | //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data); |
6cd62ccf |
84 | |
3aee1200 |
85 | /* get the size of the field type according to |
86 | * The facility size information. */ |
f104d082 |
87 | #if 0 |
3aee1200 |
88 | static inline void preset_field_type_size(LttTracefile *tf, |
89 | LttEventType *event_type, |
90 | off_t offset_root, off_t offset_parent, |
91 | enum field_status *fixed_root, enum field_status *fixed_parent, |
92 | LttField *field); |
f104d082 |
93 | #endif //0 |
6cd62ccf |
94 | |
3aee1200 |
95 | /* map a fixed size or a block information from the file (fd) */ |
96 | static gint map_block(LttTracefile * tf, guint block_num); |
97 | |
98 | /* calculate nsec per cycles for current block */ |
791dffa6 |
99 | #if 0 |
100 | static guint32 calc_nsecs_per_cycle(LttTracefile * t); |
101 | static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles); |
102 | #endif //0 |
6cd62ccf |
103 | |
3aee1200 |
104 | /* go to the next event */ |
105 | static int ltt_seek_next_event(LttTracefile *tf); |
6cd62ccf |
106 | |
3c165eaf |
107 | //void ltt_update_event_size(LttTracefile *tf); |
eed2ef37 |
108 | |
3c165eaf |
109 | static int open_tracefiles(LttTrace *trace, gchar *root_path, |
110 | gchar *relative_path); |
111 | static int ltt_process_facility_tracefile(LttTracefile *tf); |
112 | static void ltt_tracefile_time_span_get(LttTracefile *tf, |
113 | LttTime *start, LttTime *end); |
114 | static void group_time_span_get(GQuark name, gpointer data, gpointer user_data); |
115 | static gint map_block(LttTracefile * tf, guint block_num); |
116 | static int ltt_seek_next_event(LttTracefile *tf); |
117 | static void __attribute__((constructor)) init(void); |
118 | static void ltt_update_event_size(LttTracefile *tf); |
119 | //void precompute_offsets(LttFacility *fac, LttEventType *event); |
2312de30 |
120 | |
3aee1200 |
121 | #if 0 |
43da6a59 |
122 | /* Functions to parse system.xml file (using glib xml parser) */ |
51b5991e |
123 | static void parser_start_element (GMarkupParseContext __UNUSED__ *context, |
43da6a59 |
124 | const gchar *element_name, |
125 | const gchar **attribute_names, |
126 | const gchar **attribute_values, |
127 | gpointer user_data, |
128 | GError **error) |
129 | { |
130 | int i=0; |
131 | LttSystemDescription* des = (LttSystemDescription* )user_data; |
132 | if(strcmp("system", element_name)){ |
2a74fbf4 |
133 | *error = g_error_new(G_MARKUP_ERROR, |
134 | G_LOG_LEVEL_WARNING, |
135 | "This is not system.xml file"); |
136 | return; |
43da6a59 |
137 | } |
138 | |
139 | while(attribute_names[i]){ |
140 | if(strcmp("node_name", attribute_names[i])==0){ |
141 | des->node_name = g_strdup(attribute_values[i]); |
142 | }else if(strcmp("domainname", attribute_names[i])==0){ |
143 | des->domain_name = g_strdup(attribute_values[i]); |
144 | }else if(strcmp("cpu", attribute_names[i])==0){ |
145 | des->nb_cpu = atoi(attribute_values[i]); |
146 | }else if(strcmp("arch_size", attribute_names[i])==0){ |
147 | if(strcmp(attribute_values[i],"LP32") == 0) des->size = LTT_LP32; |
148 | else if(strcmp(attribute_values[i],"ILP32") == 0) des->size = LTT_ILP32; |
149 | else if(strcmp(attribute_values[i],"LP64") == 0) des->size = LTT_LP64; |
150 | else if(strcmp(attribute_values[i],"ILP64") == 0) des->size = LTT_ILP64; |
151 | else if(strcmp(attribute_values[i],"UNKNOWN") == 0) des->size = LTT_UNKNOWN; |
152 | }else if(strcmp("endian", attribute_names[i])==0){ |
153 | if(strcmp(attribute_values[i],"LITTLE_ENDIAN") == 0) |
154 | des->endian = LTT_LITTLE_ENDIAN; |
155 | else if(strcmp(attribute_values[i],"BIG_ENDIAN") == 0) |
156 | des->endian = LTT_BIG_ENDIAN; |
157 | }else if(strcmp("kernel_name", attribute_names[i])==0){ |
158 | des->kernel_name = g_strdup(attribute_values[i]); |
159 | }else if(strcmp("kernel_release", attribute_names[i])==0){ |
160 | des->kernel_release = g_strdup(attribute_values[i]); |
161 | }else if(strcmp("kernel_version", attribute_names[i])==0){ |
162 | des->kernel_version = g_strdup(attribute_values[i]); |
163 | }else if(strcmp("machine", attribute_names[i])==0){ |
164 | des->machine = g_strdup(attribute_values[i]); |
165 | }else if(strcmp("processor", attribute_names[i])==0){ |
166 | des->processor = g_strdup(attribute_values[i]); |
167 | }else if(strcmp("hardware_platform", attribute_names[i])==0){ |
168 | des->hardware_platform = g_strdup(attribute_values[i]); |
169 | }else if(strcmp("operating_system", attribute_names[i])==0){ |
170 | des->operating_system = g_strdup(attribute_values[i]); |
171 | }else if(strcmp("ltt_major_version", attribute_names[i])==0){ |
172 | des->ltt_major_version = atoi(attribute_values[i]); |
173 | }else if(strcmp("ltt_minor_version", attribute_names[i])==0){ |
174 | des->ltt_minor_version = atoi(attribute_values[i]); |
175 | }else if(strcmp("ltt_block_size", attribute_names[i])==0){ |
176 | des->ltt_block_size = atoi(attribute_values[i]); |
177 | }else{ |
2a74fbf4 |
178 | *error = g_error_new(G_MARKUP_ERROR, |
179 | G_LOG_LEVEL_WARNING, |
180 | "Not a valid attribute"); |
181 | return; |
43da6a59 |
182 | } |
183 | i++; |
184 | } |
185 | } |
186 | |
51b5991e |
187 | static void parser_characters (GMarkupParseContext __UNUSED__ *context, |
43da6a59 |
188 | const gchar *text, |
51b5991e |
189 | gsize __UNUSED__ text_len, |
43da6a59 |
190 | gpointer user_data, |
51b5991e |
191 | GError __UNUSED__ **error) |
43da6a59 |
192 | { |
193 | LttSystemDescription* des = (LttSystemDescription* )user_data; |
194 | des->description = g_strdup(text); |
195 | } |
3aee1200 |
196 | #endif //0 |
f104d082 |
197 | |
3c165eaf |
198 | #if 0 |
77175651 |
199 | LttFacility *ltt_trace_get_facility_by_num(LttTrace *t, |
3aee1200 |
200 | guint num) |
201 | { |
202 | g_assert(num < t->facilities_by_num->len); |
203 | |
204 | return &g_array_index(t->facilities_by_num, LttFacility, num); |
205 | |
206 | } |
3c165eaf |
207 | #endif //0 |
43da6a59 |
208 | |
b56dcdf2 |
209 | guint ltt_trace_get_num_cpu(LttTrace *t) |
210 | { |
211 | return t->num_cpu; |
212 | } |
213 | |
43da6a59 |
214 | |
b7576a11 |
215 | /* trace can be NULL |
216 | * |
217 | * Return value : 0 success, 1 bad tracefile |
218 | */ |
219 | int parse_trace_header(void *header, LttTracefile *tf, LttTrace *t) |
220 | { |
221 | guint32 *magic_number = (guint32*)header; |
222 | struct ltt_trace_header_any *any = (struct ltt_trace_header_any *)header; |
223 | |
224 | if(*magic_number == LTT_MAGIC_NUMBER) |
225 | tf->reverse_bo = 0; |
226 | else if(*magic_number == LTT_REV_MAGIC_NUMBER) |
227 | tf->reverse_bo = 1; |
228 | else /* invalid magic number, bad tracefile ! */ |
229 | return 1; |
230 | |
231 | /* Get float byte order : might be different from int byte order |
232 | * (or is set to 0 if the trace has no float (kernel trace)) */ |
233 | tf->float_word_order = any->float_word_order; |
256a5b3a |
234 | tf->alignment = any->alignment; |
90440c06 |
235 | tf->has_heartbeat = any->has_heartbeat; |
b7576a11 |
236 | |
237 | if(t) { |
238 | t->arch_type = ltt_get_uint32(LTT_GET_BO(tf), |
239 | &any->arch_type); |
240 | t->arch_variant = ltt_get_uint32(LTT_GET_BO(tf), |
241 | &any->arch_variant); |
242 | t->arch_size = any->arch_size; |
243 | t->ltt_major_version = any->major_version; |
244 | t->ltt_minor_version = any->minor_version; |
245 | t->flight_recorder = any->flight_recorder; |
3c165eaf |
246 | // t->compact_facilities = NULL; |
b7576a11 |
247 | } |
248 | |
b7576a11 |
249 | switch(any->major_version) { |
250 | |
251 | case 0: |
3c165eaf |
252 | g_warning("Unsupported trace version : %hhu.%hhu", |
253 | any->major_version, any->minor_version); |
254 | return 1; |
255 | break; |
256 | case 1: |
b7576a11 |
257 | switch(any->minor_version) { |
3c165eaf |
258 | case 0: |
b7576a11 |
259 | { |
3c165eaf |
260 | struct ltt_trace_header_1_0 *vheader = |
261 | (struct ltt_trace_header_1_0 *)header; |
823820eb |
262 | tf->buffer_header_size = |
263 | sizeof(struct ltt_block_start_header) |
3c165eaf |
264 | + sizeof(struct ltt_trace_header_1_0); |
62e4e7bf |
265 | tf->tsc_lsb_truncate = vheader->tsc_lsb_truncate; |
266 | tf->tscbits = vheader->tscbits; |
267 | tf->tsc_msb_cutoff = 32 - tf->tsc_lsb_truncate - tf->tscbits; |
dd3a6d39 |
268 | tf->tsc_mask = ((1ULL << (tf->tscbits))-1); |
269 | tf->tsc_mask = tf->tsc_mask << tf->tsc_lsb_truncate; |
62e4e7bf |
270 | tf->tsc_mask_next_bit = (1ULL<<(tf->tscbits)); |
271 | tf->tsc_mask_next_bit = tf->tsc_mask_next_bit << tf->tsc_lsb_truncate; |
e939e5b2 |
272 | if(t) { |
273 | t->start_freq = ltt_get_uint64(LTT_GET_BO(tf), |
274 | &vheader->start_freq); |
a3999b49 |
275 | t->freq_scale = ltt_get_uint32(LTT_GET_BO(tf), |
276 | &vheader->freq_scale); |
e939e5b2 |
277 | t->start_tsc = ltt_get_uint64(LTT_GET_BO(tf), |
278 | &vheader->start_tsc); |
279 | t->start_monotonic = ltt_get_uint64(LTT_GET_BO(tf), |
280 | &vheader->start_monotonic); |
281 | t->start_time.tv_sec = ltt_get_uint64(LTT_GET_BO(tf), |
282 | &vheader->start_time_sec); |
283 | t->start_time.tv_nsec = ltt_get_uint64(LTT_GET_BO(tf), |
284 | &vheader->start_time_usec); |
285 | t->start_time.tv_nsec *= 1000; /* microsec to nanosec */ |
286 | |
287 | t->start_time_from_tsc = ltt_time_from_uint64( |
c7f86478 |
288 | (double)t->start_tsc |
289 | * (1000000000.0 / tf->trace->freq_scale) |
62e4e7bf |
290 | / (double)t->start_freq); |
291 | t->compact_event_bits = 0; |
e939e5b2 |
292 | } |
293 | } |
294 | break; |
b7576a11 |
295 | default: |
986e2a7c |
296 | g_warning("Unsupported trace version : %hhu.%hhu", |
3c165eaf |
297 | any->major_version, any->minor_version); |
b7576a11 |
298 | return 1; |
299 | } |
300 | break; |
b7576a11 |
301 | default: |
302 | g_warning("Unsupported trace version : %hhu.%hhu", |
303 | any->major_version, any->minor_version); |
304 | return 1; |
305 | } |
306 | |
307 | |
308 | return 0; |
309 | } |
310 | |
311 | |
312 | |
6cd62ccf |
313 | /***************************************************************************** |
314 | *Function name |
963b5f2d |
315 | * ltt_tracefile_open : open a trace file, construct a LttTracefile |
6cd62ccf |
316 | *Input params |
963b5f2d |
317 | * t : the trace containing the tracefile |
318 | * fileName : path name of the trace file |
3aee1200 |
319 | * tf : the tracefile structure |
6cd62ccf |
320 | *Return value |
3aee1200 |
321 | * : 0 for success, -1 otherwise. |
6cd62ccf |
322 | ****************************************************************************/ |
323 | |
3aee1200 |
324 | gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf) |
6cd62ccf |
325 | { |
963b5f2d |
326 | struct stat lTDFStat; /* Trace data file status */ |
3aee1200 |
327 | struct ltt_block_start_header *header; |
b77d1b57 |
328 | int page_size = getpagesize(); |
6cd62ccf |
329 | |
330 | //open the file |
d3d34f49 |
331 | tf->long_name = g_quark_from_string(fileName); |
963b5f2d |
332 | tf->trace = t; |
3865ea09 |
333 | tf->fd = open(fileName, O_RDONLY); |
6cd62ccf |
334 | if(tf->fd < 0){ |
2a74fbf4 |
335 | g_warning("Unable to open input data file %s\n", fileName); |
3aee1200 |
336 | goto end; |
6cd62ccf |
337 | } |
338 | |
339 | // Get the file's status |
340 | if(fstat(tf->fd, &lTDFStat) < 0){ |
2a74fbf4 |
341 | g_warning("Unable to get the status of the input data file %s\n", fileName); |
3aee1200 |
342 | goto close_file; |
6cd62ccf |
343 | } |
344 | |
345 | // Is the file large enough to contain a trace |
823820eb |
346 | if(lTDFStat.st_size < |
9ea7b94b |
347 | (off_t)(sizeof(struct ltt_block_start_header) |
348 | + sizeof(struct ltt_trace_header_any))){ |
8710c6c7 |
349 | g_print("The input data file %s does not contain a trace\n", fileName); |
3aee1200 |
350 | goto close_file; |
351 | } |
352 | |
353 | /* Temporarily map the buffer start header to get trace information */ |
354 | /* Multiple of pages aligned head */ |
b77d1b57 |
355 | tf->buffer.head = mmap(0, |
9ea7b94b |
356 | PAGE_ALIGN(sizeof(struct ltt_block_start_header) |
357 | + sizeof(struct ltt_trace_header_any)), PROT_READ, |
3aee1200 |
358 | MAP_PRIVATE, tf->fd, 0); |
3865ea09 |
359 | if(tf->buffer.head == MAP_FAILED) { |
3aee1200 |
360 | perror("Error in allocating memory for buffer of tracefile"); |
361 | goto close_file; |
6cd62ccf |
362 | } |
3865ea09 |
363 | g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned. |
3aee1200 |
364 | |
365 | header = (struct ltt_block_start_header*)tf->buffer.head; |
6cd62ccf |
366 | |
51551c6f |
367 | if(parse_trace_header(header->trace, tf, NULL)) { |
368 | g_warning("parse_trace_header error"); |
369 | goto unmap_file; |
370 | } |
3aee1200 |
371 | |
6cd62ccf |
372 | //store the size of the file |
373 | tf->file_size = lTDFStat.st_size; |
f628823c |
374 | tf->buf_size = ltt_get_uint32(LTT_GET_BO(tf), &header->buf_size); |
375 | tf->num_blocks = tf->file_size / tf->buf_size; |
376 | |
377 | if(munmap(tf->buffer.head, |
9ea7b94b |
378 | PAGE_ALIGN(sizeof(struct ltt_block_start_header) |
379 | + sizeof(struct ltt_trace_header_any)))) { |
f628823c |
380 | g_warning("unmap size : %u\n", |
9ea7b94b |
381 | PAGE_ALIGN(sizeof(struct ltt_block_start_header) |
382 | + sizeof(struct ltt_trace_header_any))); |
f628823c |
383 | perror("munmap error"); |
384 | g_assert(0); |
385 | } |
3aee1200 |
386 | tf->buffer.head = NULL; |
6cd62ccf |
387 | |
963b5f2d |
388 | //read the first block |
3aee1200 |
389 | if(map_block(tf,0)) { |
390 | perror("Cannot map block for tracefile"); |
391 | goto close_file; |
392 | } |
393 | |
394 | return 0; |
6cd62ccf |
395 | |
3aee1200 |
396 | /* Error */ |
397 | unmap_file: |
f628823c |
398 | if(munmap(tf->buffer.head, |
9ea7b94b |
399 | PAGE_ALIGN(sizeof(struct ltt_block_start_header) |
400 | + sizeof(struct ltt_trace_header_any)))) { |
f628823c |
401 | g_warning("unmap size : %u\n", |
9ea7b94b |
402 | PAGE_ALIGN(sizeof(struct ltt_block_start_header) |
403 | + sizeof(struct ltt_trace_header_any))); |
f628823c |
404 | perror("munmap error"); |
405 | g_assert(0); |
406 | } |
3aee1200 |
407 | close_file: |
3865ea09 |
408 | close(tf->fd); |
3aee1200 |
409 | end: |
410 | return -1; |
6cd62ccf |
411 | } |
412 | |
d3d34f49 |
413 | LttTrace *ltt_tracefile_get_trace(LttTracefile *tf) |
414 | { |
415 | return tf->trace; |
416 | } |
417 | |
3aee1200 |
418 | #if 0 |
6cd62ccf |
419 | /***************************************************************************** |
963b5f2d |
420 | *Open control and per cpu tracefiles |
6cd62ccf |
421 | ****************************************************************************/ |
422 | |
45e14832 |
423 | void ltt_tracefile_open_cpu(LttTrace *t, gchar * tracefile_name) |
6cd62ccf |
424 | { |
963b5f2d |
425 | LttTracefile * tf; |
426 | tf = ltt_tracefile_open(t,tracefile_name); |
4a6c8e36 |
427 | if(!tf) return; |
963b5f2d |
428 | t->per_cpu_tracefile_number++; |
429 | g_ptr_array_add(t->per_cpu_tracefiles, tf); |
6cd62ccf |
430 | } |
431 | |
45e14832 |
432 | gint ltt_tracefile_open_control(LttTrace *t, gchar * control_name) |
6cd62ccf |
433 | { |
963b5f2d |
434 | LttTracefile * tf; |
c02ea99f |
435 | LttEvent ev; |
963b5f2d |
436 | LttFacility * f; |
963b5f2d |
437 | void * pos; |
438 | FacilityLoad fLoad; |
8d1e6362 |
439 | unsigned int i; |
963b5f2d |
440 | |
441 | tf = ltt_tracefile_open(t,control_name); |
2a74fbf4 |
442 | if(!tf) { |
443 | g_warning("ltt_tracefile_open_control : bad file descriptor"); |
444 | return -1; |
445 | } |
963b5f2d |
446 | t->control_tracefile_number++; |
447 | g_ptr_array_add(t->control_tracefiles,tf); |
448 | |
449 | //parse facilities tracefile to get base_id |
542ceddd |
450 | if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){ |
963b5f2d |
451 | while(1){ |
c02ea99f |
452 | if(!ltt_tracefile_read(tf,&ev)) return 0; // end of file |
40331ba8 |
453 | |
c02ea99f |
454 | if(ev.event_id == TRACE_FACILITY_LOAD){ |
455 | pos = ev.data; |
45e14832 |
456 | fLoad.name = (gchar*)pos; |
47a166fc |
457 | fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name)); |
cbd41522 |
458 | fLoad.base_code = *(guint32 *)(pos + strlen(fLoad.name) + sizeof(LttChecksum)); |
963b5f2d |
459 | |
460 | for(i=0;i<t->facility_number;i++){ |
461 | f = (LttFacility*)g_ptr_array_index(t->facilities,i); |
462 | if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){ |
463 | f->base_id = fLoad.base_code; |
464 | break; |
465 | } |
466 | } |
2a74fbf4 |
467 | if(i==t->facility_number) { |
8d1e6362 |
468 | g_warning("Facility: %s, checksum: %u is not found", |
469 | fLoad.name,(unsigned int)fLoad.checksum); |
2a74fbf4 |
470 | return -1; |
471 | } |
c02ea99f |
472 | }else if(ev.event_id == TRACE_BLOCK_START){ |
40331ba8 |
473 | continue; |
c02ea99f |
474 | }else if(ev.event_id == TRACE_BLOCK_END){ |
40331ba8 |
475 | break; |
2a74fbf4 |
476 | }else { |
8d1e6362 |
477 | g_warning("Not valid facilities trace file"); |
2a74fbf4 |
478 | return -1; |
479 | } |
963b5f2d |
480 | } |
481 | } |
2a74fbf4 |
482 | return 0; |
6cd62ccf |
483 | } |
3aee1200 |
484 | #endif //0 |
6cd62ccf |
485 | |
486 | /***************************************************************************** |
487 | *Function name |
963b5f2d |
488 | * ltt_tracefile_close: close a trace file, |
6cd62ccf |
489 | *Input params |
963b5f2d |
490 | * t : tracefile which will be closed |
6cd62ccf |
491 | ****************************************************************************/ |
492 | |
963b5f2d |
493 | void ltt_tracefile_close(LttTracefile *t) |
6cd62ccf |
494 | { |
f628823c |
495 | int page_size = getpagesize(); |
496 | |
3aee1200 |
497 | if(t->buffer.head != NULL) |
f628823c |
498 | if(munmap(t->buffer.head, PAGE_ALIGN(t->buf_size))) { |
499 | g_warning("unmap size : %u\n", |
500 | PAGE_ALIGN(t->buf_size)); |
501 | perror("munmap error"); |
502 | g_assert(0); |
503 | } |
504 | |
3865ea09 |
505 | close(t->fd); |
963b5f2d |
506 | } |
6cd62ccf |
507 | |
6cd62ccf |
508 | |
963b5f2d |
509 | /***************************************************************************** |
510 | *Get system information |
511 | ****************************************************************************/ |
3aee1200 |
512 | #if 0 |
45e14832 |
513 | gint getSystemInfo(LttSystemDescription* des, gchar * pathname) |
963b5f2d |
514 | { |
45e14832 |
515 | int fd; |
516 | GIOChannel *iochan; |
517 | gchar *buf = NULL; |
518 | gsize length; |
43da6a59 |
519 | |
520 | GMarkupParseContext * context; |
2a74fbf4 |
521 | GError * error = NULL; |
43da6a59 |
522 | GMarkupParser markup_parser = |
523 | { |
524 | parser_start_element, |
8d1e6362 |
525 | NULL, |
43da6a59 |
526 | parser_characters, |
527 | NULL, /* passthrough */ |
528 | NULL /* error */ |
529 | }; |
963b5f2d |
530 | |
45e14832 |
531 | fd = g_open(pathname, O_RDONLY, 0); |
532 | if(fd == -1){ |
2a74fbf4 |
533 | g_warning("Can not open file : %s\n", pathname); |
534 | return -1; |
6cd62ccf |
535 | } |
963b5f2d |
536 | |
45e14832 |
537 | iochan = g_io_channel_unix_new(fd); |
538 | |
43da6a59 |
539 | context = g_markup_parse_context_new(&markup_parser, 0, des,NULL); |
6cd62ccf |
540 | |
45e14832 |
541 | //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){ |
542 | while(g_io_channel_read_line(iochan, &buf, &length, NULL, &error) |
543 | != G_IO_STATUS_EOF) { |
544 | |
545 | if(error != NULL) { |
546 | g_warning("Can not read xml file: \n%s\n", error->message); |
547 | g_error_free(error); |
548 | } |
549 | if(!g_markup_parse_context_parse(context, buf, length, &error)){ |
2a74fbf4 |
550 | if(error != NULL) { |
551 | g_warning("Can not parse xml file: \n%s\n", error->message); |
552 | g_error_free(error); |
553 | } |
554 | g_markup_parse_context_free(context); |
45e14832 |
555 | |
556 | g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */ |
557 | if(error != NULL) { |
558 | g_warning("Can not close file: \n%s\n", error->message); |
559 | g_error_free(error); |
560 | } |
561 | |
562 | close(fd); |
2a74fbf4 |
563 | return -1; |
43da6a59 |
564 | } |
963b5f2d |
565 | } |
2a74fbf4 |
566 | g_markup_parse_context_free(context); |
45e14832 |
567 | |
568 | g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */ |
569 | if(error != NULL) { |
570 | g_warning("Can not close file: \n%s\n", error->message); |
571 | g_error_free(error); |
572 | } |
573 | |
574 | g_close(fd); |
575 | |
576 | g_free(buf); |
2a74fbf4 |
577 | return 0; |
6cd62ccf |
578 | } |
3aee1200 |
579 | #endif //0 |
6cd62ccf |
580 | |
581 | /***************************************************************************** |
963b5f2d |
582 | *The following functions get facility/tracefile information |
6cd62ccf |
583 | ****************************************************************************/ |
3aee1200 |
584 | #if 0 |
45e14832 |
585 | gint getFacilityInfo(LttTrace *t, gchar* eventdefs) |
6cd62ccf |
586 | { |
45e14832 |
587 | GDir * dir; |
588 | const gchar * name; |
8d1e6362 |
589 | unsigned int i,j; |
963b5f2d |
590 | LttFacility * f; |
591 | LttEventType * et; |
45e14832 |
592 | gchar fullname[DIR_NAME_SIZE]; |
593 | GError * error = NULL; |
594 | |
595 | dir = g_dir_open(eventdefs, 0, &error); |
963b5f2d |
596 | |
45e14832 |
597 | if(error != NULL) { |
598 | g_warning("Can not open directory: %s, %s\n", eventdefs, error->message); |
599 | g_error_free(error); |
2a74fbf4 |
600 | return -1; |
601 | } |
963b5f2d |
602 | |
45e14832 |
603 | while((name = g_dir_read_name(dir)) != NULL){ |
604 | if(!g_pattern_match_simple("*.xml", name)) continue; |
605 | strcpy(fullname,eventdefs); |
606 | strcat(fullname,name); |
607 | ltt_facility_open(t,fullname); |
608 | } |
609 | g_dir_close(dir); |
963b5f2d |
610 | |
963b5f2d |
611 | for(j=0;j<t->facility_number;j++){ |
8710c6c7 |
612 | f = (LttFacility*)g_ptr_array_index(t->facilities, j); |
963b5f2d |
613 | for(i=0; i<f->event_number; i++){ |
614 | et = f->events[i]; |
40331ba8 |
615 | setFieldsOffset(NULL, et, NULL, t); |
963b5f2d |
616 | } |
617 | } |
2a74fbf4 |
618 | return 0; |
963b5f2d |
619 | } |
3aee1200 |
620 | #endif //0 |
6cd62ccf |
621 | |
963b5f2d |
622 | /***************************************************************************** |
623 | *A trace is specified as a pathname to the directory containing all the |
624 | *associated data (control tracefiles, per cpu tracefiles, event |
625 | *descriptions...). |
626 | * |
627 | *When a trace is closed, all the associated facilities, types and fields |
628 | *are released as well. |
8d1e6362 |
629 | */ |
630 | |
631 | |
632 | /**************************************************************************** |
633 | * get_absolute_pathname |
803229fa |
634 | * |
8d1e6362 |
635 | * return the unique pathname in the system |
636 | * |
ef35d837 |
637 | * MD : Fixed this function so it uses realpath, dealing well with |
638 | * forgotten cases (.. were not used correctly before). |
803229fa |
639 | * |
963b5f2d |
640 | ****************************************************************************/ |
45e14832 |
641 | void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname) |
9f797243 |
642 | { |
9f797243 |
643 | abs_pathname[0] = '\0'; |
803229fa |
644 | |
ef35d837 |
645 | if ( realpath (pathname, abs_pathname) != NULL) |
646 | return; |
647 | else |
648 | { |
8d1e6362 |
649 | /* error, return the original path unmodified */ |
ef35d837 |
650 | strcpy(abs_pathname, pathname); |
9f797243 |
651 | return; |
652 | } |
ef35d837 |
653 | return; |
9f797243 |
654 | } |
655 | |
3aee1200 |
656 | /* Search for something like : .*_.* |
657 | * |
658 | * The left side is the name, the right side is the number. |
659 | */ |
660 | |
ae3d0f50 |
661 | int get_tracefile_name_number(gchar *raw_name, |
3aee1200 |
662 | GQuark *name, |
ae3d0f50 |
663 | guint *num, |
62e4e7bf |
664 | guint *tid, |
665 | guint *pgid, |
666 | guint64 *creation) |
6cd62ccf |
667 | { |
3aee1200 |
668 | guint raw_name_len = strlen(raw_name); |
669 | gchar char_name[PATH_MAX]; |
3aee1200 |
670 | int i; |
671 | int underscore_pos; |
672 | long int cpu_num; |
673 | gchar *endptr; |
62e4e7bf |
674 | gchar *tmpptr; |
3aee1200 |
675 | |
676 | for(i=raw_name_len-1;i>=0;i--) { |
677 | if(raw_name[i] == '_') break; |
678 | } |
ae3d0f50 |
679 | if(i==-1) { /* Either not found or name length is 0 */ |
62e4e7bf |
680 | /* This is a userspace tracefile */ |
681 | strncpy(char_name, raw_name, raw_name_len); |
682 | char_name[raw_name_len] = '\0'; |
683 | *name = g_quark_from_string(char_name); |
684 | *num = 0; /* unknown cpu */ |
685 | for(i=0;i<raw_name_len;i++) { |
686 | if(raw_name[i] == '/') { |
687 | break; |
688 | } |
689 | } |
690 | i++; |
691 | for(;i<raw_name_len;i++) { |
692 | if(raw_name[i] == '/') { |
693 | break; |
694 | } |
695 | } |
696 | i++; |
697 | for(;i<raw_name_len;i++) { |
698 | if(raw_name[i] == '-') { |
699 | break; |
700 | } |
701 | } |
702 | if(i == raw_name_len) return -1; |
703 | i++; |
704 | tmpptr = &raw_name[i]; |
705 | for(;i<raw_name_len;i++) { |
706 | if(raw_name[i] == '.') { |
707 | raw_name[i] = ' '; |
708 | break; |
709 | } |
710 | } |
711 | *tid = strtoul(tmpptr, &endptr, 10); |
712 | if(endptr == tmpptr) |
713 | return -1; /* No digit */ |
714 | if(*tid == ULONG_MAX) |
715 | return -1; /* underflow / overflow */ |
716 | i++; |
717 | tmpptr = &raw_name[i]; |
718 | for(;i<raw_name_len;i++) { |
719 | if(raw_name[i] == '.') { |
720 | raw_name[i] = ' '; |
721 | break; |
722 | } |
723 | } |
724 | *pgid = strtoul(tmpptr, &endptr, 10); |
725 | if(endptr == tmpptr) |
726 | return -1; /* No digit */ |
727 | if(*pgid == ULONG_MAX) |
728 | return -1; /* underflow / overflow */ |
729 | i++; |
730 | tmpptr = &raw_name[i]; |
731 | *creation = strtoull(tmpptr, &endptr, 10); |
732 | if(endptr == tmpptr) |
733 | return -1; /* No digit */ |
734 | if(*creation == G_MAXUINT64) |
735 | return -1; /* underflow / overflow */ |
736 | } else { |
737 | underscore_pos = i; |
738 | |
739 | cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10); |
740 | |
741 | if(endptr == raw_name+underscore_pos+1) |
742 | return -1; /* No digit */ |
743 | if(cpu_num == LONG_MIN || cpu_num == LONG_MAX) |
744 | return -1; /* underflow / overflow */ |
745 | |
746 | strncpy(char_name, raw_name, underscore_pos); |
747 | char_name[underscore_pos] = '\0'; |
748 | |
749 | *name = g_quark_from_string(char_name); |
750 | *num = cpu_num; |
751 | } |
69bd59ed |
752 | |
b333a76b |
753 | |
3aee1200 |
754 | return 0; |
755 | } |
963b5f2d |
756 | |
3aee1200 |
757 | |
3865ea09 |
758 | GData **ltt_trace_get_tracefiles_groups(LttTrace *trace) |
77175651 |
759 | { |
3865ea09 |
760 | return &trace->tracefiles; |
77175651 |
761 | } |
762 | |
763 | |
3865ea09 |
764 | void compute_tracefile_group(GQuark key_id, |
765 | GArray *group, |
766 | struct compute_tracefile_group_args *args) |
77175651 |
767 | { |
768 | int i; |
769 | LttTracefile *tf; |
770 | |
771 | for(i=0; i<group->len; i++) { |
772 | tf = &g_array_index (group, LttTracefile, i); |
773 | if(tf->cpu_online) |
3865ea09 |
774 | args->func(tf, args->func_args); |
77175651 |
775 | } |
776 | } |
777 | |
778 | |
3aee1200 |
779 | void ltt_tracefile_group_destroy(gpointer data) |
780 | { |
781 | GArray *group = (GArray *)data; |
782 | int i; |
783 | LttTracefile *tf; |
784 | |
785 | for(i=0; i<group->len; i++) { |
786 | tf = &g_array_index (group, LttTracefile, i); |
787 | if(tf->cpu_online) |
788 | ltt_tracefile_close(tf); |
789 | } |
790 | g_array_free(group, TRUE); |
6cd62ccf |
791 | } |
792 | |
3aee1200 |
793 | gboolean ltt_tracefile_group_has_cpu_online(gpointer data) |
49bf71b5 |
794 | { |
3aee1200 |
795 | GArray *group = (GArray *)data; |
796 | int i; |
797 | LttTracefile *tf; |
798 | |
799 | for(i=0; i<group->len; i++) { |
800 | tf = &g_array_index (group, LttTracefile, i); |
3c165eaf |
801 | if(tf->cpu_online) |
802 | return 1; |
3aee1200 |
803 | } |
804 | return 0; |
49bf71b5 |
805 | } |
806 | |
807 | |
3aee1200 |
808 | /* Open each tracefile under a specific directory. Put them in a |
809 | * GData : permits to access them using their tracefile group pathname. |
810 | * i.e. access control/modules tracefile group by index : |
811 | * "control/module". |
3865ea09 |
812 | * |
813 | * relative path is the path relative to the trace root |
814 | * root path is the full path |
3aee1200 |
815 | * |
4a55f63e |
816 | * A tracefile group is simply an array where all the per cpu tracefiles sit. |
3aee1200 |
817 | */ |
818 | |
3c165eaf |
819 | int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_path) |
f7afe191 |
820 | { |
62e4e7bf |
821 | DIR *dir = opendir(root_path); |
822 | struct dirent *entry; |
823 | struct stat stat_buf; |
824 | int ret; |
3865ea09 |
825 | |
62e4e7bf |
826 | gchar path[PATH_MAX]; |
827 | int path_len; |
828 | gchar *path_ptr; |
3aee1200 |
829 | |
3865ea09 |
830 | int rel_path_len; |
69bd59ed |
831 | gchar rel_path[PATH_MAX]; |
832 | gchar *rel_path_ptr; |
cb03932a |
833 | LttTracefile tmp_tf; |
3865ea09 |
834 | |
62e4e7bf |
835 | if(dir == NULL) { |
836 | perror(root_path); |
837 | return ENOENT; |
838 | } |
3aee1200 |
839 | |
62e4e7bf |
840 | strncpy(path, root_path, PATH_MAX-1); |
841 | path_len = strlen(path); |
842 | path[path_len] = '/'; |
843 | path_len++; |
844 | path_ptr = path + path_len; |
3aee1200 |
845 | |
3865ea09 |
846 | strncpy(rel_path, relative_path, PATH_MAX-1); |
847 | rel_path_len = strlen(rel_path); |
848 | rel_path[rel_path_len] = '/'; |
849 | rel_path_len++; |
850 | rel_path_ptr = rel_path + rel_path_len; |
851 | |
62e4e7bf |
852 | while((entry = readdir(dir)) != NULL) { |
853 | |
854 | if(entry->d_name[0] == '.') continue; |
855 | |
856 | strncpy(path_ptr, entry->d_name, PATH_MAX - path_len); |
857 | strncpy(rel_path_ptr, entry->d_name, PATH_MAX - rel_path_len); |
858 | |
859 | ret = stat(path, &stat_buf); |
860 | if(ret == -1) { |
861 | perror(path); |
862 | continue; |
863 | } |
864 | |
865 | g_debug("Tracefile file or directory : %s\n", path); |
866 | |
74a588bb |
867 | if(strcmp(rel_path, "/eventdefs") == 0) continue; |
868 | |
62e4e7bf |
869 | if(S_ISDIR(stat_buf.st_mode)) { |
3aee1200 |
870 | |
62e4e7bf |
871 | g_debug("Entering subdirectory...\n"); |
872 | ret = open_tracefiles(trace, path, rel_path); |
873 | if(ret < 0) continue; |
874 | } else if(S_ISREG(stat_buf.st_mode)) { |
875 | GQuark name; |
ae3d0f50 |
876 | guint num, tid, pgid; |
62e4e7bf |
877 | guint64 creation; |
3aee1200 |
878 | GArray *group; |
ae3d0f50 |
879 | num = tid = pgid = 0; |
62e4e7bf |
880 | creation = 0; |
ae3d0f50 |
881 | if(get_tracefile_name_number(rel_path, &name, &num, &tid, &pgid, &creation)) |
3aee1200 |
882 | continue; /* invalid name */ |
3865ea09 |
883 | |
62e4e7bf |
884 | g_debug("Opening file.\n"); |
cb03932a |
885 | if(ltt_tracefile_open(trace, path, &tmp_tf)) { |
886 | g_info("Error opening tracefile %s", path); |
887 | |
888 | continue; /* error opening the tracefile : bad magic number ? */ |
889 | } |
890 | |
3865ea09 |
891 | g_debug("Tracefile name is %s and number is %u", |
892 | g_quark_to_string(name), num); |
893 | |
cb03932a |
894 | tmp_tf.cpu_online = 1; |
895 | tmp_tf.cpu_num = num; |
d3d34f49 |
896 | tmp_tf.name = name; |
62e4e7bf |
897 | tmp_tf.tid = tid; |
898 | tmp_tf.pgid = pgid; |
899 | tmp_tf.creation = creation; |
d1bb700c |
900 | if(tmp_tf.name == g_quark_from_string("/compact") |
62e4e7bf |
901 | || tmp_tf.name == g_quark_from_string("/flight-compact")) |
902 | tmp_tf.compact = 1; |
d1bb700c |
903 | else |
62e4e7bf |
904 | tmp_tf.compact = 0; |
3865ea09 |
905 | group = g_datalist_id_get_data(&trace->tracefiles, name); |
3aee1200 |
906 | if(group == NULL) { |
907 | /* Elements are automatically cleared when the array is allocated. |
908 | * It makes the cpu_online variable set to 0 : cpu offline, by default. |
909 | */ |
910 | group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10); |
3865ea09 |
911 | g_datalist_id_set_data_full(&trace->tracefiles, name, |
3aee1200 |
912 | group, ltt_tracefile_group_destroy); |
913 | } |
cb03932a |
914 | |
3aee1200 |
915 | /* Add the per cpu tracefile to the named group */ |
916 | unsigned int old_len = group->len; |
917 | if(num+1 > old_len) |
918 | group = g_array_set_size(group, num+1); |
cb03932a |
919 | g_array_index (group, LttTracefile, num) = tmp_tf; |
3aee1200 |
920 | |
62e4e7bf |
921 | } |
922 | } |
923 | |
924 | closedir(dir); |
3aee1200 |
925 | |
62e4e7bf |
926 | return 0; |
f7afe191 |
927 | } |
928 | |
3aee1200 |
929 | /* ltt_get_facility_description |
930 | * |
29e7c5b3 |
931 | * Opens the file corresponding to the requested facility (identified by fac_id |
3aee1200 |
932 | * and checksum). |
933 | * |
934 | * The name searched is : %trace root%/eventdefs/facname_checksum.xml |
935 | * |
936 | * Returns 0 on success, or 1 on failure. |
937 | */ |
3c165eaf |
938 | #if 0 |
3aee1200 |
939 | static int ltt_get_facility_description(LttFacility *f, |
940 | LttTrace *t, |
941 | LttTracefile *fac_tf) |
6cd62ccf |
942 | { |
3aee1200 |
943 | char desc_file_name[PATH_MAX]; |
944 | const gchar *text; |
945 | guint textlen; |
946 | gint err; |
62e4e7bf |
947 | gint arch_spec; |
948 | gint fac_name_len; |
3aee1200 |
949 | |
950 | text = g_quark_to_string(t->pathname); |
951 | textlen = strlen(text); |
952 | |
953 | if(textlen >= PATH_MAX) goto name_error; |
954 | strcpy(desc_file_name, text); |
955 | |
956 | text = "/eventdefs/"; |
957 | textlen+=strlen(text); |
958 | if(textlen >= PATH_MAX) goto name_error; |
959 | strcat(desc_file_name, text); |
960 | |
961 | text = g_quark_to_string(f->name); |
62e4e7bf |
962 | fac_name_len = strlen(text); |
f5d7967f |
963 | textlen+=fac_name_len; |
3aee1200 |
964 | if(textlen >= PATH_MAX) goto name_error; |
965 | strcat(desc_file_name, text); |
f5d7967f |
966 | |
62e4e7bf |
967 | /* arch specific facilities are named like this : name_arch */ |
968 | if(fac_name_len+1 < sizeof("_arch")) |
969 | arch_spec = 0; |
970 | else { |
971 | if(!strcmp(&text[fac_name_len+1-sizeof("_arch")], "_arch")) |
972 | arch_spec = 1; |
973 | else |
974 | arch_spec = 0; |
975 | } |
f5d7967f |
976 | |
29e7c5b3 |
977 | #if 0 |
3aee1200 |
978 | text = "_"; |
979 | textlen+=strlen(text); |
980 | if(textlen >= PATH_MAX) goto name_error; |
981 | strcat(desc_file_name, text); |
963b5f2d |
982 | |
3aee1200 |
983 | err = snprintf(desc_file_name+textlen, PATH_MAX-textlen-1, |
984 | "%u", f->checksum); |
d2083cab |
985 | if(err < 0) goto name_error; |
3aee1200 |
986 | |
987 | textlen=strlen(desc_file_name); |
988 | |
29e7c5b3 |
989 | #endif //0 |
62e4e7bf |
990 | |
991 | if(arch_spec) { |
992 | switch(t->arch_type) { |
993 | case LTT_ARCH_TYPE_I386: |
994 | text = "_i386"; |
995 | break; |
996 | case LTT_ARCH_TYPE_PPC: |
997 | text = "_ppc"; |
998 | break; |
999 | case LTT_ARCH_TYPE_SH: |
1000 | text = "_sh"; |
1001 | break; |
1002 | case LTT_ARCH_TYPE_S390: |
1003 | text = "_s390"; |
1004 | break; |
1005 | case LTT_ARCH_TYPE_MIPS: |
1006 | text = "_mips"; |
1007 | break; |
1008 | case LTT_ARCH_TYPE_ARM: |
1009 | text = "_arm"; |
1010 | break; |
1011 | case LTT_ARCH_TYPE_PPC64: |
1012 | text = "_ppc64"; |
1013 | break; |
1014 | case LTT_ARCH_TYPE_X86_64: |
1015 | text = "_x86_64"; |
1016 | break; |
1017 | case LTT_ARCH_TYPE_C2: |
1018 | text = "_c2"; |
1019 | break; |
1020 | case LTT_ARCH_TYPE_POWERPC: |
1021 | text = "_powerpc"; |
1022 | break; |
1023 | default: |
1024 | g_error("Trace from unsupported architecture."); |
1025 | } |
1026 | textlen+=strlen(text); |
1027 | if(textlen >= PATH_MAX) goto name_error; |
1028 | strcat(desc_file_name, text); |
1029 | } |
1030 | |
3aee1200 |
1031 | text = ".xml"; |
1032 | textlen+=strlen(text); |
1033 | if(textlen >= PATH_MAX) goto name_error; |
1034 | strcat(desc_file_name, text); |
29e7c5b3 |
1035 | |
3aee1200 |
1036 | err = ltt_facility_open(f, t, desc_file_name); |
1037 | if(err) goto facility_error; |
3aee1200 |
1038 | |
1039 | return 0; |
1040 | |
1041 | facility_error: |
1042 | name_error: |
1043 | return 1; |
1044 | } |
1045 | |
1046 | static void ltt_fac_ids_destroy(gpointer data) |
1047 | { |
1048 | GArray *fac_ids = (GArray *)data; |
963b5f2d |
1049 | |
3aee1200 |
1050 | g_array_free(fac_ids, TRUE); |
1051 | } |
3c165eaf |
1052 | #endif //0 |
3aee1200 |
1053 | |
1054 | /* Presumes the tracefile is already seeked at the beginning. It makes sense, |
1055 | * because it must be done just after the opening */ |
3c165eaf |
1056 | int ltt_process_facility_tracefile(LttTracefile *tf) |
3aee1200 |
1057 | { |
1058 | int err; |
29af7cfd |
1059 | //LttFacility *fac; |
1060 | //GArray *fac_ids; |
cb03932a |
1061 | guint i; |
29af7cfd |
1062 | //LttEventType *et; |
3aee1200 |
1063 | |
1064 | while(1) { |
1065 | err = ltt_tracefile_read_seek(tf); |
1066 | if(err == EPERM) goto seek_error; |
1067 | else if(err == ERANGE) break; /* End of tracefile */ |
1068 | |
1069 | err = ltt_tracefile_read_update_event(tf); |
1070 | if(err) goto update_error; |
1071 | |
1072 | /* We are on a facility load/or facility unload/ or heartbeat event */ |
1073 | /* The rules are : |
1074 | * * facility 0 is hardcoded : this is the core facility. It will be shown |
1075 | * in the facility array though, and is shown as "loaded builtin" in the |
1076 | * trace. |
1077 | * It contains event : |
1078 | * 0 : facility load |
1079 | * 1 : facility unload |
1080 | * 2 : state dump facility load |
d2083cab |
1081 | * 3 : heartbeat |
3aee1200 |
1082 | */ |
3c165eaf |
1083 | if(tf->event.event_id >= MARKER_CORE_IDS) { |
d2083cab |
1084 | /* Should only contain core facility */ |
3aee1200 |
1085 | g_warning("Error in processing facility file %s, " |
3c165eaf |
1086 | "should not contain event id %u.", g_quark_to_string(tf->name), |
1087 | tf->event.event_id); |
3aee1200 |
1088 | err = EPERM; |
3c165eaf |
1089 | goto event_id_error; |
d2083cab |
1090 | } else { |
3aee1200 |
1091 | |
2e13d6af |
1092 | char *pos; |
3c165eaf |
1093 | const char *marker_name, *format; |
1094 | uint16_t id; |
1095 | guint8 int_size, long_size, pointer_size, size_t_size, alignment; |
3aee1200 |
1096 | |
1097 | // FIXME align |
3c165eaf |
1098 | switch((enum marker_id)tf->event.event_id) { |
1099 | case MARKER_ID_SET_MARKER_ID: |
2e13d6af |
1100 | marker_name = pos = tf->event.data; |
3c165eaf |
1101 | g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s", marker_name); |
2e13d6af |
1102 | pos += strlen(marker_name) + 1; |
1103 | //remove genevent compatibility |
256a5b3a |
1104 | //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment); |
1105 | pos += ltt_align((size_t)pos, sizeof(uint16_t), tf->alignment); |
3c165eaf |
1106 | id = ltt_get_uint16(LTT_GET_BO(tf), pos); |
1107 | pos += sizeof(guint16); |
1108 | int_size = *(guint8*)pos; |
1109 | pos += sizeof(guint8); |
1110 | long_size = *(guint8*)pos; |
1111 | pos += sizeof(guint8); |
1112 | pointer_size = *(guint8*)pos; |
1113 | pos += sizeof(guint8); |
1114 | size_t_size = *(guint8*)pos; |
1115 | pos += sizeof(guint8); |
1116 | alignment = *(guint8*)pos; |
1117 | pos += sizeof(guint8); |
1118 | marker_id_event(tf->trace, g_quark_from_string(marker_name), |
1119 | id, int_size, long_size, |
1120 | pointer_size, size_t_size, alignment); |
3aee1200 |
1121 | break; |
3c165eaf |
1122 | case MARKER_ID_SET_MARKER_FORMAT: |
2e13d6af |
1123 | marker_name = pos = tf->event.data; |
3c165eaf |
1124 | g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s", |
1125 | marker_name); |
2e13d6af |
1126 | pos += strlen(marker_name) + 1; |
1127 | //break genevent. |
256a5b3a |
1128 | //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment); |
2e13d6af |
1129 | format = pos; |
3c165eaf |
1130 | pos += strlen(format) + 1; |
2e13d6af |
1131 | //break genevent |
256a5b3a |
1132 | //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment); |
3c165eaf |
1133 | marker_format_event(tf->trace, g_quark_from_string(marker_name), |
1134 | format); |
1135 | /* get information from dictionnary TODO */ |
3aee1200 |
1136 | break; |
3c165eaf |
1137 | case MARKER_ID_HEARTBEAT_32: |
1138 | case MARKER_ID_HEARTBEAT_64: |
d1bb700c |
1139 | break; |
3aee1200 |
1140 | default: |
1141 | g_warning("Error in processing facility file %s, " |
3c165eaf |
1142 | "unknown event id %hhu.", |
3aee1200 |
1143 | g_quark_to_string(tf->name), |
1144 | tf->event.event_id); |
1145 | err = EPERM; |
1146 | goto event_id_error; |
1147 | } |
1148 | } |
963b5f2d |
1149 | } |
3aee1200 |
1150 | return 0; |
963b5f2d |
1151 | |
3aee1200 |
1152 | /* Error handling */ |
3aee1200 |
1153 | event_id_error: |
3aee1200 |
1154 | update_error: |
1155 | seek_error: |
d2083cab |
1156 | g_warning("An error occured in facility tracefile parsing"); |
3aee1200 |
1157 | return err; |
6cd62ccf |
1158 | } |
1159 | |
963b5f2d |
1160 | |
3aee1200 |
1161 | LttTrace *ltt_trace_open(const gchar *pathname) |
1162 | { |
1163 | gchar abs_path[PATH_MAX]; |
1164 | LttTrace * t; |
1165 | LttTracefile *tf; |
1166 | GArray *group; |
b56dcdf2 |
1167 | int i, ret; |
16fcbb80 |
1168 | struct ltt_block_start_header *header; |
62e4e7bf |
1169 | DIR *dir; |
1170 | struct dirent *entry; |
b56dcdf2 |
1171 | guint control_found = 0; |
1172 | guint eventdefs_found = 0; |
62e4e7bf |
1173 | struct stat stat_buf; |
b56dcdf2 |
1174 | gchar path[PATH_MAX]; |
3aee1200 |
1175 | |
1176 | t = g_new(LttTrace, 1); |
1177 | if(!t) goto alloc_error; |
1178 | |
1179 | get_absolute_pathname(pathname, abs_path); |
1180 | t->pathname = g_quark_from_string(abs_path); |
1181 | |
3aee1200 |
1182 | g_datalist_init(&t->tracefiles); |
b56dcdf2 |
1183 | |
1184 | /* Test to see if it looks like a trace */ |
62e4e7bf |
1185 | dir = opendir(abs_path); |
1186 | if(dir == NULL) { |
1187 | perror(abs_path); |
1188 | goto open_error; |
1189 | } |
1190 | while((entry = readdir(dir)) != NULL) { |
b56dcdf2 |
1191 | strcpy(path, abs_path); |
1192 | strcat(path, "/"); |
1193 | strcat(path, entry->d_name); |
62e4e7bf |
1194 | ret = stat(path, &stat_buf); |
1195 | if(ret == -1) { |
1196 | perror(path); |
1197 | continue; |
1198 | } |
1199 | if(S_ISDIR(stat_buf.st_mode)) { |
b56dcdf2 |
1200 | if(strcmp(entry->d_name, "control") == 0) { |
1201 | control_found = 1; |
1202 | } |
1203 | if(strcmp(entry->d_name, "eventdefs") == 0) { |
1204 | eventdefs_found = 1; |
1205 | } |
1206 | } |
1207 | } |
1208 | closedir(dir); |
1209 | |
1210 | if(!control_found || !eventdefs_found) goto find_error; |
1211 | |
1212 | /* Open all the tracefiles */ |
e45551ac |
1213 | if(open_tracefiles(t, abs_path, "")) { |
1214 | g_warning("Error opening tracefile %s", abs_path); |
b56dcdf2 |
1215 | goto find_error; |
e45551ac |
1216 | } |
3aee1200 |
1217 | |
3aee1200 |
1218 | /* Parse each trace control/facilitiesN files : get runtime fac. info */ |
1219 | group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_FACILITIES); |
1220 | if(group == NULL) { |
1221 | g_error("Trace %s has no facility tracefile", abs_path); |
3865ea09 |
1222 | g_assert(0); |
3aee1200 |
1223 | goto facilities_error; |
1224 | } |
1225 | |
16fcbb80 |
1226 | /* Get the trace information for the control/facility 0 tracefile */ |
1227 | g_assert(group->len > 0); |
1228 | tf = &g_array_index (group, LttTracefile, 0); |
1229 | header = (struct ltt_block_start_header*)tf->buffer.head; |
51551c6f |
1230 | g_assert(parse_trace_header(header->trace, |
1231 | tf, t) == 0); |
b56dcdf2 |
1232 | |
1233 | t->num_cpu = group->len; |
16fcbb80 |
1234 | |
d79909d1 |
1235 | ret = allocate_marker_data(t); |
b44ba972 |
1236 | if (ret) |
d79909d1 |
1237 | g_error("Error in allocating marker data"); |
1238 | |
3aee1200 |
1239 | for(i=0; i<group->len; i++) { |
1240 | tf = &g_array_index (group, LttTracefile, i); |
1241 | if(ltt_process_facility_tracefile(tf)) |
1242 | goto facilities_error; |
1243 | } |
dd3a6d39 |
1244 | |
3aee1200 |
1245 | return t; |
1246 | |
1247 | /* Error handling */ |
1248 | facilities_error: |
d79909d1 |
1249 | destroy_marker_data(t); |
b56dcdf2 |
1250 | find_error: |
3aee1200 |
1251 | g_datalist_clear(&t->tracefiles); |
b56dcdf2 |
1252 | open_error: |
3aee1200 |
1253 | g_free(t); |
1254 | alloc_error: |
1255 | return NULL; |
1256 | |
1257 | } |
6cd62ccf |
1258 | |
826f1ab2 |
1259 | GQuark ltt_trace_name(const LttTrace *t) |
6cd62ccf |
1260 | { |
3aee1200 |
1261 | return t->pathname; |
6cd62ccf |
1262 | } |
1263 | |
6cd62ccf |
1264 | |
3aee1200 |
1265 | /****************************************************************************** |
1266 | * When we copy a trace, we want all the opening actions to happen again : |
1267 | * the trace will be reopened and totally independant from the original. |
1268 | * That's why we call ltt_trace_open. |
1269 | *****************************************************************************/ |
1270 | LttTrace *ltt_trace_copy(LttTrace *self) |
963b5f2d |
1271 | { |
3aee1200 |
1272 | return ltt_trace_open(g_quark_to_string(self->pathname)); |
963b5f2d |
1273 | } |
1274 | |
3aee1200 |
1275 | void ltt_trace_close(LttTrace *t) |
6cd62ccf |
1276 | { |
3aee1200 |
1277 | g_datalist_clear(&t->tracefiles); |
1278 | g_free(t); |
6cd62ccf |
1279 | } |
1280 | |
3aee1200 |
1281 | |
6cd62ccf |
1282 | /***************************************************************************** |
3aee1200 |
1283 | *Get the system description of the trace |
6cd62ccf |
1284 | ****************************************************************************/ |
3c165eaf |
1285 | #if 0 |
3aee1200 |
1286 | LttFacility *ltt_trace_facility_by_id(LttTrace *t, guint8 id) |
6cd62ccf |
1287 | { |
3aee1200 |
1288 | g_assert(id < t->facilities_by_num->len); |
1289 | return &g_array_index(t->facilities_by_num, LttFacility, id); |
1290 | } |
1291 | |
1292 | /* ltt_trace_facility_get_by_name |
1293 | * |
1294 | * Returns the GArray of facility indexes. All the fac_ids that matches the |
1295 | * requested facility name. |
1296 | * |
1297 | * If name is not found, returns NULL. |
1298 | */ |
1299 | GArray *ltt_trace_facility_get_by_name(LttTrace *t, GQuark name) |
1300 | { |
1301 | return g_datalist_id_get_data(&t->facilities_by_name, name); |
6cd62ccf |
1302 | } |
3c165eaf |
1303 | #endif //0 |
6cd62ccf |
1304 | |
1305 | /***************************************************************************** |
963b5f2d |
1306 | * Functions to discover all the event types in the trace |
6cd62ccf |
1307 | ****************************************************************************/ |
1308 | |
3aee1200 |
1309 | #if 0 |
963b5f2d |
1310 | unsigned ltt_trace_eventtype_number(LttTrace *t) |
6cd62ccf |
1311 | { |
8d1e6362 |
1312 | unsigned int i; |
963b5f2d |
1313 | unsigned count = 0; |
dc1cad90 |
1314 | unsigned int num = t->facility_number; |
963b5f2d |
1315 | LttFacility * f; |
dc1cad90 |
1316 | |
1317 | for(i=0;i<num;i++){ |
963b5f2d |
1318 | f = (LttFacility*)g_ptr_array_index(t->facilities, i); |
1319 | count += f->event_number; |
1320 | } |
1321 | return count; |
6cd62ccf |
1322 | } |
3aee1200 |
1323 | #endif //0 |
6cd62ccf |
1324 | |
3aee1200 |
1325 | #if 0 |
1326 | //use an iteration on all the trace facilities, and inside iteration on all the |
1327 | //event types in each facilities instead. |
963b5f2d |
1328 | LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId) |
6cd62ccf |
1329 | { |
6e8c19d4 |
1330 | LttEventType *event_type; |
1331 | |
963b5f2d |
1332 | LttFacility * f; |
1333 | f = ltt_trace_facility_by_id(t,evId); |
6e8c19d4 |
1334 | |
1335 | if(unlikely(!f)) event_type = NULL; |
1336 | else event_type = f->events[evId - f->base_id]; |
1337 | |
1338 | return event_type; |
6cd62ccf |
1339 | } |
3aee1200 |
1340 | #endif //0 |
6cd62ccf |
1341 | |
3aee1200 |
1342 | #if 0 |
6cd62ccf |
1343 | /***************************************************************************** |
3aee1200 |
1344 | * ltt_trace_find_tracefile |
1345 | * |
1346 | * Find a tracefile by name and index in the group. |
1347 | * |
1348 | * Returns a pointer to the tracefiles, else NULL. |
6cd62ccf |
1349 | ****************************************************************************/ |
6cd62ccf |
1350 | |
3aee1200 |
1351 | LttTracefile *ltt_trace_find_tracefile(LttTrace *t, const gchar *name) |
6cd62ccf |
1352 | { |
6cd62ccf |
1353 | } |
3aee1200 |
1354 | #endif //0 |
6cd62ccf |
1355 | |
1356 | /***************************************************************************** |
3aee1200 |
1357 | * Get the start time and end time of the trace |
6cd62ccf |
1358 | ****************************************************************************/ |
1359 | |
3c165eaf |
1360 | void ltt_tracefile_time_span_get(LttTracefile *tf, |
3aee1200 |
1361 | LttTime *start, LttTime *end) |
6cd62ccf |
1362 | { |
3aee1200 |
1363 | int err; |
6cd62ccf |
1364 | |
3aee1200 |
1365 | err = map_block(tf, 0); |
1366 | if(unlikely(err)) { |
1367 | g_error("Can not map block"); |
1368 | *start = ltt_time_infinite; |
1369 | } else |
1370 | *start = tf->buffer.begin.timestamp; |
1371 | |
1372 | err = map_block(tf, tf->num_blocks - 1); /* Last block */ |
1373 | if(unlikely(err)) { |
1374 | g_error("Can not map block"); |
1375 | *end = ltt_time_zero; |
1376 | } else |
1377 | *end = tf->buffer.end.timestamp; |
6cd62ccf |
1378 | } |
1379 | |
3aee1200 |
1380 | struct tracefile_time_span_get_args { |
1381 | LttTrace *t; |
1382 | LttTime *start; |
1383 | LttTime *end; |
1384 | }; |
963b5f2d |
1385 | |
3c165eaf |
1386 | void group_time_span_get(GQuark name, gpointer data, gpointer user_data) |
963b5f2d |
1387 | { |
3aee1200 |
1388 | struct tracefile_time_span_get_args *args = |
1389 | (struct tracefile_time_span_get_args*)user_data; |
1390 | |
1391 | GArray *group = (GArray *)data; |
1392 | int i; |
1393 | LttTracefile *tf; |
1394 | LttTime tmp_start; |
1395 | LttTime tmp_end; |
1396 | |
1397 | for(i=0; i<group->len; i++) { |
1398 | tf = &g_array_index (group, LttTracefile, i); |
1399 | if(tf->cpu_online) { |
1400 | ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end); |
1401 | if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start; |
1402 | if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end; |
1403 | } |
1404 | } |
6cd62ccf |
1405 | } |
1406 | |
487ad181 |
1407 | void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end) |
1408 | { |
3aee1200 |
1409 | LttTime min_start = ltt_time_infinite; |
1410 | LttTime max_end = ltt_time_zero; |
1411 | struct tracefile_time_span_get_args args = { t, &min_start, &max_end }; |
487ad181 |
1412 | |
3aee1200 |
1413 | g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args); |
1414 | |
1415 | if(start != NULL) *start = min_start; |
1416 | if(end != NULL) *end = max_end; |
1417 | |
487ad181 |
1418 | } |
1419 | |
1420 | |
6cd62ccf |
1421 | /***************************************************************************** |
963b5f2d |
1422 | *Get the name of a tracefile |
6cd62ccf |
1423 | ****************************************************************************/ |
1424 | |
348c6ba8 |
1425 | GQuark ltt_tracefile_name(const LttTracefile *tf) |
6cd62ccf |
1426 | { |
963b5f2d |
1427 | return tf->name; |
6cd62ccf |
1428 | } |
1429 | |
348c6ba8 |
1430 | GQuark ltt_tracefile_long_name(const LttTracefile *tf) |
1431 | { |
1432 | return tf->long_name; |
1433 | } |
1434 | |
1435 | |
d3d34f49 |
1436 | |
ae3d0f50 |
1437 | guint ltt_tracefile_cpu(LttTracefile *tf) |
d3d34f49 |
1438 | { |
1439 | return tf->cpu_num; |
1440 | } |
1441 | |
ae3d0f50 |
1442 | guint ltt_tracefile_tid(LttTracefile *tf) |
1443 | { |
1444 | return tf->tid; |
1445 | } |
1446 | |
1447 | guint ltt_tracefile_pgid(LttTracefile *tf) |
1448 | { |
1449 | return tf->pgid; |
1450 | } |
1451 | |
1452 | guint64 ltt_tracefile_creation(LttTracefile *tf) |
1453 | { |
1454 | return tf->creation; |
1455 | } |
80da81ad |
1456 | /***************************************************************************** |
1457 | * Get the number of blocks in the tracefile |
1458 | ****************************************************************************/ |
1459 | |
3aee1200 |
1460 | guint ltt_tracefile_block_number(LttTracefile *tf) |
80da81ad |
1461 | { |
3aee1200 |
1462 | return tf->num_blocks; |
80da81ad |
1463 | } |
1464 | |
caf7a67a |
1465 | |
3aee1200 |
1466 | /* Seek to the first event in a tracefile that has a time equal or greater than |
1467 | * the time passed in parameter. |
1468 | * |
1469 | * If the time parameter is outside the tracefile time span, seek to the first |
27304273 |
1470 | * event or if after, return ERANGE. |
3aee1200 |
1471 | * |
1472 | * If the time parameter is before the first event, we have to seek specially to |
1473 | * there. |
1474 | * |
27304273 |
1475 | * If the time is after the end of the trace, return ERANGE. |
3aee1200 |
1476 | * |
1477 | * Do a binary search to find the right block, then a sequential search in the |
1478 | * block to find the event. |
1479 | * |
1480 | * In the special case where the time requested fits inside a block that has no |
1481 | * event corresponding to the requested time, the first event of the next block |
1482 | * will be seeked. |
1483 | * |
1484 | * IMPORTANT NOTE : // FIXME everywhere... |
1485 | * |
1486 | * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time : |
1487 | * you will jump over an event if you do. |
1488 | * |
1489 | * Return value : 0 : no error, the tf->event can be used |
27304273 |
1490 | * ERANGE : time if after the last event of the trace |
3aee1200 |
1491 | * otherwise : this is an error. |
1492 | * |
1493 | * */ |
1494 | |
1495 | int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time) |
1496 | { |
1497 | int ret = 0; |
1498 | int err; |
1499 | unsigned int block_num, high, low; |
1500 | |
1501 | /* seek at the beginning of trace */ |
1502 | err = map_block(tf, 0); /* First block */ |
1503 | if(unlikely(err)) { |
1504 | g_error("Can not map block"); |
1505 | goto fail; |
caf7a67a |
1506 | } |
1507 | |
3aee1200 |
1508 | /* If the time is lower or equal the beginning of the trace, |
1509 | * go to the first event. */ |
1510 | if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) { |
1511 | ret = ltt_tracefile_read(tf); |
27304273 |
1512 | if(ret == ERANGE) goto range; |
1513 | else if (ret) goto fail; |
3aee1200 |
1514 | goto found; /* There is either no event in the trace or the event points |
1515 | to the first event in the trace */ |
1516 | } |
caf7a67a |
1517 | |
3aee1200 |
1518 | err = map_block(tf, tf->num_blocks - 1); /* Last block */ |
1519 | if(unlikely(err)) { |
1520 | g_error("Can not map block"); |
1521 | goto fail; |
caf7a67a |
1522 | } |
6cd62ccf |
1523 | |
27304273 |
1524 | /* If the time is after the end of the trace, return ERANGE. */ |
1525 | if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) { |
1526 | goto range; |
3aee1200 |
1527 | } |
62e55dd6 |
1528 | |
3aee1200 |
1529 | /* Binary search the block */ |
1530 | high = tf->num_blocks - 1; |
1531 | low = 0; |
1532 | |
1533 | while(1) { |
1534 | block_num = ((high-low) / 2) + low; |
1535 | |
1536 | err = map_block(tf, block_num); |
1537 | if(unlikely(err)) { |
1538 | g_error("Can not map block"); |
1539 | goto fail; |
db55eaae |
1540 | } |
3aee1200 |
1541 | if(high == low) { |
1542 | /* We cannot divide anymore : this is what would happen if the time |
1543 | * requested was exactly between two consecutive buffers'end and start |
1544 | * timestamps. This is also what would happend if we didn't deal with out |
1545 | * of span cases prior in this function. */ |
1546 | /* The event is right in the buffer! |
1547 | * (or in the next buffer first event) */ |
1548 | while(1) { |
1549 | ret = ltt_tracefile_read(tf); |
27304273 |
1550 | if(ret == ERANGE) goto range; /* ERANGE or EPERM */ |
3aee1200 |
1551 | else if(ret) goto fail; |
1552 | |
d9e13a0f |
1553 | if(ltt_time_compare(time, tf->event.event_time) <= 0) |
e45551ac |
1554 | goto found; |
3aee1200 |
1555 | } |
1556 | |
27304273 |
1557 | } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) { |
3aee1200 |
1558 | /* go to lower part */ |
b1369bef |
1559 | high = block_num - 1; |
3aee1200 |
1560 | } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) { |
1561 | /* go to higher part */ |
b1369bef |
1562 | low = block_num + 1; |
3aee1200 |
1563 | } else {/* The event is right in the buffer! |
1564 | (or in the next buffer first event) */ |
1565 | while(1) { |
27304273 |
1566 | ret = ltt_tracefile_read(tf); |
1567 | if(ret == ERANGE) goto range; /* ERANGE or EPERM */ |
3aee1200 |
1568 | else if(ret) goto fail; |
1569 | |
d9e13a0f |
1570 | if(ltt_time_compare(time, tf->event.event_time) <= 0) |
3aee1200 |
1571 | break; |
6cd62ccf |
1572 | } |
3aee1200 |
1573 | goto found; |
6cd62ccf |
1574 | } |
6cd62ccf |
1575 | } |
3aee1200 |
1576 | |
1577 | found: |
1578 | return 0; |
27304273 |
1579 | range: |
1580 | return ERANGE; |
3aee1200 |
1581 | |
1582 | /* Error handling */ |
1583 | fail: |
1584 | g_error("ltt_tracefile_seek_time failed on tracefile %s", |
1585 | g_quark_to_string(tf->name)); |
1586 | return EPERM; |
6cd62ccf |
1587 | } |
1588 | |
80da81ad |
1589 | |
3aee1200 |
1590 | int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep) { |
80da81ad |
1591 | |
3aee1200 |
1592 | int err; |
1593 | |
1594 | if(ep->tracefile != tf) { |
1595 | goto fail; |
80da81ad |
1596 | } |
1597 | |
3aee1200 |
1598 | err = map_block(tf, ep->block); |
1599 | if(unlikely(err)) { |
1600 | g_error("Can not map block"); |
1601 | goto fail; |
1602 | } |
1603 | |
1604 | tf->event.offset = ep->offset; |
18206708 |
1605 | |
62e4e7bf |
1606 | /* Put back the event real tsc */ |
1607 | tf->event.tsc = ep->tsc; |
1608 | tf->buffer.tsc = ep->tsc; |
78f79181 |
1609 | |
3aee1200 |
1610 | err = ltt_tracefile_read_update_event(tf); |
1611 | if(err) goto fail; |
1612 | err = ltt_tracefile_read_op(tf); |
1613 | if(err) goto fail; |
80da81ad |
1614 | |
a0c1f622 |
1615 | return 0; |
3aee1200 |
1616 | |
1617 | fail: |
1618 | g_error("ltt_tracefile_seek_time failed on tracefile %s", |
1619 | g_quark_to_string(tf->name)); |
a0c1f622 |
1620 | return 1; |
3aee1200 |
1621 | } |
1622 | |
ae3d0f50 |
1623 | LttTime ltt_interpolate_time_from_tsc(LttTracefile *tf, guint64 tsc) |
3aee1200 |
1624 | { |
1625 | LttTime time; |
62e4e7bf |
1626 | |
1627 | if(tsc > tf->trace->start_tsc) { |
1628 | time = ltt_time_from_uint64( |
1629 | (double)(tsc - tf->trace->start_tsc) |
1630 | * (1000000000.0 / tf->trace->freq_scale) |
1631 | / (double)tf->trace->start_freq); |
1632 | time = ltt_time_add(tf->trace->start_time_from_tsc, time); |
1633 | } else { |
1634 | time = ltt_time_from_uint64( |
1635 | (double)(tf->trace->start_tsc - tsc) |
1636 | * (1000000000.0 / tf->trace->freq_scale) |
1637 | / (double)tf->trace->start_freq); |
1638 | time = ltt_time_sub(tf->trace->start_time_from_tsc, time); |
1639 | } |
3aee1200 |
1640 | return time; |
80da81ad |
1641 | } |
1642 | |
ae3d0f50 |
1643 | /* Calculate the real event time based on the buffer boundaries */ |
1644 | LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event) |
1645 | { |
62e4e7bf |
1646 | return ltt_interpolate_time_from_tsc(tf, tf->buffer.tsc); |
ae3d0f50 |
1647 | } |
1648 | |
eed2ef37 |
1649 | |
1650 | /* Get the current event of the tracefile : valid until the next read */ |
1651 | LttEvent *ltt_tracefile_get_event(LttTracefile *tf) |
1652 | { |
1653 | return &tf->event; |
1654 | } |
1655 | |
1656 | |
1657 | |
6cd62ccf |
1658 | /***************************************************************************** |
1659 | *Function name |
3aee1200 |
1660 | * ltt_tracefile_read : Read the next event in the tracefile |
6cd62ccf |
1661 | *Input params |
1662 | * t : tracefile |
1663 | *Return value |
3aee1200 |
1664 | * |
1665 | * Returns 0 if an event can be used in tf->event. |
d822520b |
1666 | * Returns ERANGE on end of trace. The event in tf->event still can be used |
1667 | * (if the last block was not empty). |
3aee1200 |
1668 | * Returns EPERM on error. |
1669 | * |
1670 | * This function does make the tracefile event structure point to the event |
1671 | * currently pointed to by the tf->event. |
1672 | * |
1673 | * Note : you must call a ltt_tracefile_seek to the beginning of the trace to |
1674 | * reinitialize it after an error if you want results to be coherent. |
1675 | * It would be the case if a end of trace last buffer has no event : the end |
1676 | * of trace wouldn't be returned, but an error. |
1677 | * We make the assumption there is at least one event per buffer. |
6cd62ccf |
1678 | ****************************************************************************/ |
1679 | |
3aee1200 |
1680 | int ltt_tracefile_read(LttTracefile *tf) |
6cd62ccf |
1681 | { |
963b5f2d |
1682 | int err; |
6cd62ccf |
1683 | |
3aee1200 |
1684 | err = ltt_tracefile_read_seek(tf); |
1685 | if(err) return err; |
1686 | err = ltt_tracefile_read_update_event(tf); |
1687 | if(err) return err; |
1688 | err = ltt_tracefile_read_op(tf); |
1689 | if(err) return err; |
1690 | |
1691 | return 0; |
1692 | } |
1693 | |
1694 | int ltt_tracefile_read_seek(LttTracefile *tf) |
1695 | { |
1696 | int err; |
1697 | |
1698 | /* Get next buffer until we finally have an event, or end of trace */ |
1699 | while(1) { |
1700 | err = ltt_seek_next_event(tf); |
1701 | if(unlikely(err == ENOPROTOOPT)) { |
1702 | return EPERM; |
bdc36259 |
1703 | } |
bdc36259 |
1704 | |
3aee1200 |
1705 | /* Are we at the end of the buffer ? */ |
1706 | if(err == ERANGE) { |
1707 | if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */ |
1708 | return ERANGE; |
1709 | } else { |
1710 | /* get next block */ |
1711 | err = map_block(tf, tf->buffer.index + 1); |
1712 | if(unlikely(err)) { |
1713 | g_error("Can not map block"); |
1714 | return EPERM; |
1715 | } |
1716 | } |
1717 | } else break; /* We found an event ! */ |
1718 | } |
2dee981d |
1719 | |
3aee1200 |
1720 | return 0; |
1721 | } |
18206708 |
1722 | |
1723 | |
3aee1200 |
1724 | /* do specific operation on events */ |
1725 | int ltt_tracefile_read_op(LttTracefile *tf) |
1726 | { |
3aee1200 |
1727 | LttEvent *event; |
1728 | |
1729 | event = &tf->event; |
18206708 |
1730 | |
3aee1200 |
1731 | /* do event specific operation */ |
40331ba8 |
1732 | |
3aee1200 |
1733 | /* do something if its an heartbeat event : increment the heartbeat count */ |
1734 | //if(event->facility_id == LTT_FACILITY_CORE) |
1735 | // if(event->event_id == LTT_EVENT_HEARTBEAT) |
1736 | // tf->cur_heart_beat_number++; |
1737 | |
1738 | return 0; |
6cd62ccf |
1739 | } |
1740 | |
6cd62ccf |
1741 | |
3aee1200 |
1742 | /* same as ltt_tracefile_read, but does not seek to the next event nor call |
1743 | * event specific operation. */ |
1744 | int ltt_tracefile_read_update_event(LttTracefile *tf) |
6cd62ccf |
1745 | { |
3aee1200 |
1746 | void * pos; |
1747 | LttEvent *event; |
1748 | |
1749 | event = &tf->event; |
eed2ef37 |
1750 | pos = tf->buffer.head + event->offset; |
3aee1200 |
1751 | |
1752 | /* Read event header */ |
1753 | |
62e4e7bf |
1754 | /* Align the head */ |
1755 | if(!tf->compact) |
256a5b3a |
1756 | pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment); |
62e4e7bf |
1757 | else { |
1758 | g_assert(tf->has_heartbeat); |
256a5b3a |
1759 | pos += ltt_align((size_t)pos, sizeof(uint32_t), tf->alignment); |
62e4e7bf |
1760 | } |
3aee1200 |
1761 | |
62e4e7bf |
1762 | if(tf->has_heartbeat) { |
1763 | event->timestamp = ltt_get_uint32(LTT_GET_BO(tf), |
1764 | pos); |
d1bb700c |
1765 | if(!tf->compact) { |
1766 | /* 32 bits -> 64 bits tsc */ |
1767 | /* note : still works for seek and non seek cases. */ |
1768 | if(event->timestamp < (0xFFFFFFFFULL&tf->buffer.tsc)) { |
1769 | tf->buffer.tsc = ((tf->buffer.tsc&0xFFFFFFFF00000000ULL) |
1770 | + 0x100000000ULL) |
1771 | | (guint64)event->timestamp; |
1772 | event->tsc = tf->buffer.tsc; |
1773 | } else { |
1774 | /* no overflow */ |
1775 | tf->buffer.tsc = (tf->buffer.tsc&0xFFFFFFFF00000000ULL) |
1776 | | (guint64)event->timestamp; |
1777 | event->tsc = tf->buffer.tsc; |
62e4e7bf |
1778 | event->compact_data = 0; |
d1bb700c |
1779 | } |
1780 | } else { |
1781 | /* Compact header */ |
1782 | /* We keep the LSB of the previous timestamp, to make sure |
1783 | * we never go back */ |
1784 | event->event_id = event->timestamp >> tf->tscbits; |
dd3a6d39 |
1785 | event->event_id = event->event_id & ((1 << tf->trace->compact_event_bits) - 1); |
1786 | event->compact_data = event->timestamp >> |
62e4e7bf |
1787 | (tf->trace->compact_event_bits + tf->tscbits); |
dd3a6d39 |
1788 | //printf("tsc bits %u, ev bits %u init data %u\n", |
62e4e7bf |
1789 | // tf->tscbits, tf->trace->compact_event_bits, event->compact_data); |
dd3a6d39 |
1790 | /* Put the compact data back in original endianness */ |
1791 | event->compact_data = ltt_get_uint32(LTT_GET_BO(tf), &event->compact_data); |
d1bb700c |
1792 | event->event_size = 0xFFFF; |
dd3a6d39 |
1793 | //printf("Found compact event %d\n", event->event_id); |
1794 | //printf("Compact data %d\n", event->compact_data); |
d1bb700c |
1795 | event->timestamp = event->timestamp << tf->tsc_lsb_truncate; |
1796 | event->timestamp = event->timestamp & tf->tsc_mask; |
dd3a6d39 |
1797 | //printf("timestamp 0x%lX\n", event->timestamp); |
1798 | //printf("mask 0x%llX\n", tf->tsc_mask); |
1799 | //printf("mask_next 0x%llX\n", tf->tsc_mask_next_bit); |
1800 | //printf("previous tsc 0x%llX\n", tf->buffer.tsc); |
1801 | //printf("previous tsc&mask 0x%llX\n", tf->tsc_mask&tf->buffer.tsc); |
1802 | //printf("previous tsc&(~mask) 0x%llX\n", tf->buffer.tsc&(~tf->tsc_mask)); |
d1bb700c |
1803 | if(event->timestamp < (tf->tsc_mask&tf->buffer.tsc)) { |
dd3a6d39 |
1804 | //printf("wrap\n"); |
d1bb700c |
1805 | tf->buffer.tsc = ((tf->buffer.tsc&(~tf->tsc_mask)) |
1806 | + tf->tsc_mask_next_bit) |
1807 | | (guint64)event->timestamp; |
1808 | event->tsc = tf->buffer.tsc; |
1809 | } else { |
dd3a6d39 |
1810 | //printf("no wrap\n"); |
d1bb700c |
1811 | /* no overflow */ |
1812 | tf->buffer.tsc = (tf->buffer.tsc&(~tf->tsc_mask)) |
1813 | | (guint64)event->timestamp; |
1814 | event->tsc = tf->buffer.tsc; |
1815 | } |
dd3a6d39 |
1816 | //printf("current tsc 0x%llX\n", tf->buffer.tsc); |
d1bb700c |
1817 | } |
62e4e7bf |
1818 | pos += sizeof(guint32); |
1819 | } else { |
1820 | event->tsc = ltt_get_uint64(LTT_GET_BO(tf), pos); |
1821 | tf->buffer.tsc = event->tsc; |
1822 | event->compact_data = 0; |
1823 | pos += sizeof(guint64); |
1824 | } |
1825 | event->event_time = ltt_interpolate_time(tf, event); |
3aee1200 |
1826 | |
d1bb700c |
1827 | if(!tf->compact) { |
3c165eaf |
1828 | event->event_id = *(guint16*)pos; |
1829 | pos += sizeof(guint16); |
d1bb700c |
1830 | |
1831 | event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos); |
1832 | pos += sizeof(guint16); |
1833 | } else { |
1834 | /* Compact event */ |
1835 | } |
62e4e7bf |
1836 | /* Align the head */ |
1837 | if(!tf->compact) |
256a5b3a |
1838 | pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment); |
f104d082 |
1839 | |
3aee1200 |
1840 | event->data = pos; |
1841 | |
77175651 |
1842 | /* get the data size and update the event fields with the current |
d1bb700c |
1843 | * information. Also update the time if a heartbeat_full event is found. */ |
eed2ef37 |
1844 | ltt_update_event_size(tf); |
77175651 |
1845 | |
3aee1200 |
1846 | return 0; |
6cd62ccf |
1847 | } |
1848 | |
507915ee |
1849 | |
6cd62ccf |
1850 | /**************************************************************************** |
1851 | *Function name |
3aee1200 |
1852 | * map_block : map a block from the file |
6cd62ccf |
1853 | *Input Params |
1854 | * lttdes : ltt trace file |
1855 | * whichBlock : the block which will be read |
1856 | *return value |
1857 | * 0 : success |
1858 | * EINVAL : lseek fail |
1859 | * EIO : can not read from the file |
1860 | ****************************************************************************/ |
1861 | |
3c165eaf |
1862 | gint map_block(LttTracefile * tf, guint block_num) |
6cd62ccf |
1863 | { |
b77d1b57 |
1864 | int page_size = getpagesize(); |
3aee1200 |
1865 | struct ltt_block_start_header *header; |
1866 | |
1867 | g_assert(block_num < tf->num_blocks); |
6cd62ccf |
1868 | |
f628823c |
1869 | if(tf->buffer.head != NULL) { |
1870 | if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buf_size))) { |
1871 | g_warning("unmap size : %u\n", |
1872 | PAGE_ALIGN(tf->buf_size)); |
1873 | perror("munmap error"); |
1874 | g_assert(0); |
1875 | } |
1876 | } |
1877 | |
ac849774 |
1878 | |
3aee1200 |
1879 | /* Multiple of pages aligned head */ |
b77d1b57 |
1880 | tf->buffer.head = mmap(0, |
f628823c |
1881 | PAGE_ALIGN(tf->buf_size), |
b77d1b57 |
1882 | PROT_READ, MAP_PRIVATE, tf->fd, |
f628823c |
1883 | PAGE_ALIGN((off_t)tf->buf_size * (off_t)block_num)); |
3aee1200 |
1884 | |
3865ea09 |
1885 | if(tf->buffer.head == MAP_FAILED) { |
3aee1200 |
1886 | perror("Error in allocating memory for buffer of tracefile"); |
3865ea09 |
1887 | g_assert(0); |
3aee1200 |
1888 | goto map_error; |
6cd62ccf |
1889 | } |
3865ea09 |
1890 | g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned. |
3aee1200 |
1891 | |
6cd62ccf |
1892 | |
3aee1200 |
1893 | tf->buffer.index = block_num; |
1894 | |
1895 | header = (struct ltt_block_start_header*)tf->buffer.head; |
1896 | |
791dffa6 |
1897 | #if 0 |
986e2a7c |
1898 | tf->buffer.begin.timestamp = ltt_time_add( |
1899 | ltt_time_from_uint64( |
1900 | ltt_get_uint64(LTT_GET_BO(tf), |
1901 | &header->begin.timestamp) |
1902 | - tf->trace->start_monotonic), |
1903 | tf->trace->start_time); |
791dffa6 |
1904 | #endif //0 |
348c6ba8 |
1905 | //g_debug("block %u begin : %lu.%lu", block_num, |
1906 | // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec); |
3aee1200 |
1907 | tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf), |
1908 | &header->begin.cycle_count); |
986e2a7c |
1909 | tf->buffer.begin.freq = ltt_get_uint64(LTT_GET_BO(tf), |
1910 | &header->begin.freq); |
62e4e7bf |
1911 | if(tf->buffer.begin.freq == 0) |
1912 | tf->buffer.begin.freq = tf->trace->start_freq; |
ae3d0f50 |
1913 | |
1914 | tf->buffer.begin.timestamp = ltt_interpolate_time_from_tsc(tf, |
62e4e7bf |
1915 | tf->buffer.begin.cycle_count); |
ae3d0f50 |
1916 | #if 0 |
62e4e7bf |
1917 | ltt_time_add( |
791dffa6 |
1918 | ltt_time_from_uint64( |
88612d17 |
1919 | (double)(tf->buffer.begin.cycle_count |
1920 | - tf->trace->start_tsc) * 1000000.0 |
791dffa6 |
1921 | / (double)tf->trace->start_freq), |
62e4e7bf |
1922 | tf->trace->start_time_from_tsc); |
ae3d0f50 |
1923 | #endif //0 |
791dffa6 |
1924 | #if 0 |
1925 | |
986e2a7c |
1926 | tf->buffer.end.timestamp = ltt_time_add( |
1927 | ltt_time_from_uint64( |
1928 | ltt_get_uint64(LTT_GET_BO(tf), |
1929 | &header->end.timestamp) |
1930 | - tf->trace->start_monotonic), |
1931 | tf->trace->start_time); |
791dffa6 |
1932 | #endif //0 |
348c6ba8 |
1933 | //g_debug("block %u end : %lu.%lu", block_num, |
1934 | // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec); |
3aee1200 |
1935 | tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf), |
1936 | &header->end.cycle_count); |
986e2a7c |
1937 | tf->buffer.end.freq = ltt_get_uint64(LTT_GET_BO(tf), |
1938 | &header->end.freq); |
62e4e7bf |
1939 | if(tf->buffer.end.freq == 0) |
1940 | tf->buffer.end.freq = tf->trace->start_freq; |
1941 | |
3aee1200 |
1942 | tf->buffer.lost_size = ltt_get_uint32(LTT_GET_BO(tf), |
986e2a7c |
1943 | &header->lost_size); |
ae3d0f50 |
1944 | tf->buffer.end.timestamp = ltt_interpolate_time_from_tsc(tf, |
62e4e7bf |
1945 | tf->buffer.end.cycle_count); |
ae3d0f50 |
1946 | #if 0 |
62e4e7bf |
1947 | ltt_time_add( |
791dffa6 |
1948 | ltt_time_from_uint64( |
88612d17 |
1949 | (double)(tf->buffer.end.cycle_count |
1950 | - tf->trace->start_tsc) * 1000000.0 |
791dffa6 |
1951 | / (double)tf->trace->start_freq), |
88612d17 |
1952 | tf->trace->start_time_from_tsc); |
ae3d0f50 |
1953 | #endif //0 |
3aee1200 |
1954 | tf->buffer.tsc = tf->buffer.begin.cycle_count; |
1955 | tf->event.tsc = tf->buffer.tsc; |
986e2a7c |
1956 | tf->buffer.freq = tf->buffer.begin.freq; |
3aee1200 |
1957 | |
1958 | /* FIXME |
1959 | * eventually support variable buffer size : will need a partial pre-read of |
1960 | * the headers to create an index when we open the trace... eventually. */ |
f628823c |
1961 | g_assert(tf->buf_size == ltt_get_uint32(LTT_GET_BO(tf), |
3aee1200 |
1962 | &header->buf_size)); |
507915ee |
1963 | |
3aee1200 |
1964 | /* Now that the buffer is mapped, calculate the time interpolation for the |
1965 | * block. */ |
1966 | |
791dffa6 |
1967 | // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf); |
1968 | //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf); |
3aee1200 |
1969 | |
1970 | /* Make the current event point to the beginning of the buffer : |
1971 | * it means that the event read must get the first event. */ |
1972 | tf->event.tracefile = tf; |
1973 | tf->event.block = block_num; |
eed2ef37 |
1974 | tf->event.offset = 0; |
3aee1200 |
1975 | |
1976 | return 0; |
6cd62ccf |
1977 | |
3aee1200 |
1978 | map_error: |
1979 | return -errno; |
40331ba8 |
1980 | |
6cd62ccf |
1981 | } |
1982 | |
77175651 |
1983 | /* It will update the fields offsets too */ |
1984 | void ltt_update_event_size(LttTracefile *tf) |
6cd62ccf |
1985 | { |
2312de30 |
1986 | off_t size = 0; |
d1bb700c |
1987 | char *tscdata; |
d2007fbd |
1988 | struct marker_info *info; |
44f317b7 |
1989 | |
d2007fbd |
1990 | switch((enum marker_id)tf->event.event_id) { |
1991 | case MARKER_ID_SET_MARKER_ID: |
44f317b7 |
1992 | size = strlen((char*)tf->event.data) + 1; |
d2007fbd |
1993 | //g_debug("marker %s id set", (char*)tf->event.data); |
256a5b3a |
1994 | size += ltt_align(size, sizeof(guint16), tf->alignment); |
d2007fbd |
1995 | size += sizeof(guint16); |
3c165eaf |
1996 | size += sizeof(guint8); |
1997 | size += sizeof(guint8); |
1998 | size += sizeof(guint8); |
1999 | size += sizeof(guint8); |
2000 | size += sizeof(guint8); |
44f317b7 |
2001 | break; |
d2007fbd |
2002 | case MARKER_ID_SET_MARKER_FORMAT: |
2003 | //g_debug("marker %s format set", (char*)tf->event.data); |
44f317b7 |
2004 | size = strlen((char*)tf->event.data) + 1; |
2e13d6af |
2005 | size += strlen((char*)tf->event.data + size) + 1; |
44f317b7 |
2006 | break; |
d2007fbd |
2007 | case MARKER_ID_HEARTBEAT_32: |
2008 | //g_debug("Update Event heartbeat 32 bits"); |
44f317b7 |
2009 | break; |
d2007fbd |
2010 | case MARKER_ID_HEARTBEAT_64: |
2011 | //g_debug("Update Event heartbeat 64 bits"); |
d1bb700c |
2012 | tscdata = (char*)(tf->event.data); |
2013 | tf->event.tsc = ltt_get_uint64(LTT_GET_BO(tf), tscdata); |
2014 | tf->buffer.tsc = tf->event.tsc; |
2015 | tf->event.event_time = ltt_interpolate_time(tf, &tf->event); |
256a5b3a |
2016 | size = ltt_align(size, sizeof(guint64), tf->alignment); |
d2007fbd |
2017 | size += sizeof(guint64); |
d1bb700c |
2018 | break; |
256a5b3a |
2019 | } |
2020 | |
2021 | info = marker_get_info_from_id(tf->trace, tf->event.event_id); |
2022 | if (tf->event.event_id >= MARKER_CORE_IDS) |
2023 | g_assert(info != NULL); |
2024 | |
2025 | /* Do not update field offsets of core markers when initially reading the |
2026 | * facility tracefile when the infos about these markers do not exist yet. |
2027 | */ |
2028 | if (likely(info && info->fields)) { |
2029 | if (info->size != -1) |
2030 | size = info->size; |
2031 | else |
2032 | size = marker_update_fields_offsets(marker_get_info_from_id(tf->trace, |
2033 | tf->event.event_id), tf->event.data); |
44f317b7 |
2034 | } |
c4afd5d8 |
2035 | |
d2007fbd |
2036 | tf->event.data_size = size; |
2037 | |
2038 | /* Check consistency between kernel and LTTV structure sizes */ |
2039 | if(tf->event.event_size == 0xFFFF) { |
2040 | /* Event size too big to fit in the event size field */ |
2041 | tf->event.event_size = tf->event.data_size; |
2042 | } |
2043 | if (tf->event.data_size != tf->event.event_size) { |
3c165eaf |
2044 | struct marker_info *info = marker_get_info_from_id(tf->trace, |
2045 | tf->event.event_id); |
2046 | g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u", |
2047 | g_quark_to_string(info->name), |
2048 | tf->event.event_size, tf->event.data_size); |
d2007fbd |
2049 | exit(-1); |
2050 | } |
2051 | |
2052 | #if 0 |
44f317b7 |
2053 | LttEventType *event_type = |
2054 | ltt_facility_eventtype_get(f, tf->event.event_id); |
c4afd5d8 |
2055 | |
44f317b7 |
2056 | if(!event_type) { |
2057 | g_warning("Unknown event id %hhu in facility %s in tracefile %s", |
2058 | tf->event.event_id, |
2059 | g_quark_to_string(f->name), |
2060 | g_quark_to_string(tf->name)); |
2061 | goto event_type_error; |
3aee1200 |
2062 | } |
3aee1200 |
2063 | |
44f317b7 |
2064 | /* Compute the dynamic offsets */ |
2065 | compute_offsets(tf, f, event_type, &size, tf->event.data); |
2066 | |
2067 | //g_debug("Event root field : f.e %hhu.%hhu size %zd", |
2068 | // tf->event.facility_id, |
2069 | // tf->event.event_id, size); |
7cbcdb89 |
2070 | |
2071 | no_offset: |
77175651 |
2072 | tf->event.data_size = size; |
eed2ef37 |
2073 | |
b77d1b57 |
2074 | /* Check consistency between kernel and LTTV structure sizes */ |
62e4e7bf |
2075 | if(tf->event.event_size == 0xFFFF) { |
2076 | /* Event size too big to fit in the event size field */ |
2077 | tf->event.event_size = tf->event.data_size; |
2078 | } |
f63ebe51 |
2079 | if (tf->event.data_size != tf->event.event_size) { |
2080 | g_error("Kernel/LTTV event size differs for event %s.%s: kernel %u, LTTV %u", |
62e4e7bf |
2081 | g_quark_to_string(f->name), g_quark_to_string(event_type->name), |
2082 | tf->event.event_size, tf->event.data_size); |
f63ebe51 |
2083 | exit(-1); |
2084 | } |
2085 | //g_assert(tf->event.data_size == tf->event.event_size); |
9eb6aa7a |
2086 | |
eed2ef37 |
2087 | return; |
2088 | |
c4afd5d8 |
2089 | event_type_error: |
3aee1200 |
2090 | event_id_error: |
62e4e7bf |
2091 | if(tf->event.event_size == 0xFFFF) { |
2092 | g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes"); |
2093 | } |
2094 | /* The facility is unknown : use the kernel information about this event |
2095 | * to jump over it. */ |
9d7e0c25 |
2096 | tf->event.data_size = tf->event.event_size; |
d2007fbd |
2097 | #endif //0 |
6cd62ccf |
2098 | } |
2099 | |
6cd62ccf |
2100 | |
3aee1200 |
2101 | /* Take the tf current event offset and use the event facility id and event id |
2102 | * to figure out where is the next event offset. |
2103 | * |
2104 | * This is an internal function not aiming at being used elsewhere : it will |
2105 | * not jump over the current block limits. Please consider using |
2106 | * ltt_tracefile_read to do this. |
2107 | * |
2108 | * Returns 0 on success |
2109 | * ERANGE if we are at the end of the buffer. |
2110 | * ENOPROTOOPT if an error occured when getting the current event size. |
2111 | */ |
3c165eaf |
2112 | int ltt_seek_next_event(LttTracefile *tf) |
6cd62ccf |
2113 | { |
3aee1200 |
2114 | int ret = 0; |
2115 | void *pos; |
3aee1200 |
2116 | |
2117 | /* seek over the buffer header if we are at the buffer start */ |
eed2ef37 |
2118 | if(tf->event.offset == 0) { |
51551c6f |
2119 | tf->event.offset += tf->buffer_header_size; |
b77d1b57 |
2120 | |
f628823c |
2121 | if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) { |
b77d1b57 |
2122 | ret = ERANGE; |
2123 | } |
3aee1200 |
2124 | goto found; |
2125 | } |
6cd62ccf |
2126 | |
6cd62ccf |
2127 | |
3aee1200 |
2128 | pos = tf->event.data; |
2129 | |
77175651 |
2130 | if(tf->event.data_size < 0) goto error; |
3aee1200 |
2131 | |
77175651 |
2132 | pos += (size_t)tf->event.data_size; |
3aee1200 |
2133 | |
eed2ef37 |
2134 | tf->event.offset = pos - tf->buffer.head; |
cb03932a |
2135 | |
f628823c |
2136 | if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) { |
cb03932a |
2137 | ret = ERANGE; |
2138 | goto found; |
2139 | } |
36d36c9f |
2140 | g_assert(tf->event.offset < tf->buf_size - tf->buffer.lost_size); |
3aee1200 |
2141 | |
2142 | found: |
2143 | return ret; |
2144 | |
2145 | error: |
2146 | g_error("Error in ltt_seek_next_event for tracefile %s", |
2147 | g_quark_to_string(tf->name)); |
2148 | return ENOPROTOOPT; |
6cd62ccf |
2149 | } |
2150 | |
791dffa6 |
2151 | #if 0 |
6cd62ccf |
2152 | /***************************************************************************** |
2153 | *Function name |
3aee1200 |
2154 | * calc_nsecs_per_cycle : calculate nsecs per cycle for current block |
986e2a7c |
2155 | * |
2156 | * 1.0 / (freq(khz) *1000) * 1000000000 |
6cd62ccf |
2157 | *Input Params |
2158 | * t : tracefile |
2159 | ****************************************************************************/ |
986e2a7c |
2160 | /* from timer_tsc.c */ |
2161 | #define CYC2NS_SCALE_FACTOR 10 |
791dffa6 |
2162 | static guint32 calc_nsecs_per_cycle(LttTracefile * tf) |
6cd62ccf |
2163 | { |
986e2a7c |
2164 | //return 1e6 / (double)tf->buffer.freq; |
791dffa6 |
2165 | guint32 cpu_mhz = tf->buffer.freq / 1000; |
2166 | guint32 cyc2ns_scale = (1000 << CYC2NS_SCALE_FACTOR)/cpu_mhz; |
6cd62ccf |
2167 | |
791dffa6 |
2168 | return cyc2ns_scale; |
986e2a7c |
2169 | // return 1e6 / (double)tf->buffer.freq; |
3aee1200 |
2170 | } |
791dffa6 |
2171 | |
2172 | static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles) |
2173 | { |
2174 | return (cycles * tf->buffer.cyc2ns_scale) >> CYC2NS_SCALE_FACTOR; |
2175 | } |
2176 | #endif //0 |
2177 | |
3aee1200 |
2178 | #if 0 |
2179 | void setFieldsOffset(LttTracefile *tf, LttEventType *evT,void *evD) |
2180 | { |
2181 | LttField * rootFld = evT->root_field; |
2182 | // rootFld->base_address = evD; |
2dee981d |
2183 | |
3aee1200 |
2184 | if(likely(rootFld)) |
2185 | rootFld->field_size = getFieldtypeSize(tf, evT->facility, |
2186 | evT, 0,0,rootFld, evD); |
6cd62ccf |
2187 | } |
3aee1200 |
2188 | #endif //0 |
f104d082 |
2189 | #if 0 |
3aee1200 |
2190 | /***************************************************************************** |
6cd62ccf |
2191 | *Function name |
3aee1200 |
2192 | * set_fields_offsets : set the precomputable offset of the fields |
6cd62ccf |
2193 | *Input params |
3aee1200 |
2194 | * tracefile : opened trace file |
2195 | * event_type : the event type |
6cd62ccf |
2196 | ****************************************************************************/ |
2197 | |
3aee1200 |
2198 | void set_fields_offsets(LttTracefile *tf, LttEventType *event_type) |
6cd62ccf |
2199 | { |
3aee1200 |
2200 | LttField *field = event_type->root_field; |
2201 | enum field_status fixed_root = FIELD_FIXED, fixed_parent = FIELD_FIXED; |
2202 | |
2203 | if(likely(field)) |
2204 | preset_field_type_size(tf, event_type, 0, 0, |
2205 | &fixed_root, &fixed_parent, |
2206 | field); |
2207 | |
2208 | } |
f104d082 |
2209 | #endif //0 |
2210 | |
2211 | |
2212 | /***************************************************************************** |
2213 | *Function name |
2214 | * get_alignment : Get the alignment needed for a field. |
2215 | *Input params |
f104d082 |
2216 | * field : field |
2217 | * |
2218 | * returns : The size on which it must be aligned. |
2219 | * |
2220 | ****************************************************************************/ |
3c165eaf |
2221 | #if 0 |
743e50fd |
2222 | off_t get_alignment(LttField *field) |
f104d082 |
2223 | { |
2312de30 |
2224 | LttType *type = &field->field_type; |
f104d082 |
2225 | |
2226 | switch(type->type_class) { |
2227 | case LTT_INT_FIXED: |
2228 | case LTT_UINT_FIXED: |
2229 | case LTT_POINTER: |
2230 | case LTT_CHAR: |
2231 | case LTT_UCHAR: |
2232 | case LTT_SHORT: |
2233 | case LTT_USHORT: |
2234 | case LTT_INT: |
2235 | case LTT_UINT: |
2236 | case LTT_LONG: |
2237 | case LTT_ULONG: |
2238 | case LTT_SIZE_T: |
2239 | case LTT_SSIZE_T: |
2240 | case LTT_OFF_T: |
2241 | case LTT_FLOAT: |
2242 | case LTT_ENUM: |
2243 | /* Align offset on type size */ |
83e160f2 |
2244 | g_assert(field->field_size != 0); |
f104d082 |
2245 | return field->field_size; |
2246 | break; |
2247 | case LTT_STRING: |
83e160f2 |
2248 | return 1; |
f104d082 |
2249 | break; |
2250 | case LTT_ARRAY: |
2251 | g_assert(type->fields->len == 1); |
2252 | { |
2253 | LttField *child = &g_array_index(type->fields, LttField, 0); |
743e50fd |
2254 | return get_alignment(child); |
f104d082 |
2255 | } |
2256 | break; |
2257 | case LTT_SEQUENCE: |
2258 | g_assert(type->fields->len == 2); |
2259 | { |
83e160f2 |
2260 | off_t localign = 1; |
f104d082 |
2261 | LttField *child = &g_array_index(type->fields, LttField, 0); |
2262 | |
743e50fd |
2263 | localign = max(localign, get_alignment(child)); |
f104d082 |
2264 | |
2265 | child = &g_array_index(type->fields, LttField, 1); |
743e50fd |
2266 | localign = max(localign, get_alignment(child)); |
f104d082 |
2267 | |
2268 | return localign; |
2269 | } |
2270 | break; |
2271 | case LTT_STRUCT: |
2272 | case LTT_UNION: |
2273 | { |
2274 | guint i; |
83e160f2 |
2275 | off_t localign = 1; |
f104d082 |
2276 | |
2277 | for(i=0; i<type->fields->len; i++) { |
2278 | LttField *child = &g_array_index(type->fields, LttField, i); |
743e50fd |
2279 | localign = max(localign, get_alignment(child)); |
f104d082 |
2280 | } |
2281 | return localign; |
2282 | } |
2283 | break; |
2284 | case LTT_NONE: |
2285 | default: |
2286 | g_error("get_alignment : unknown type"); |
83e160f2 |
2287 | return -1; |
f104d082 |
2288 | } |
f104d082 |
2289 | } |
2290 | |
3c165eaf |
2291 | #endif //0 |
2292 | |
f104d082 |
2293 | /***************************************************************************** |
2294 | *Function name |
2295 | * field_compute_static_size : Determine the size of fields known by their |
2296 | * sole definition. Unions, arrays and struct sizes might be known, but |
2297 | * the parser does not give that information. |
2298 | *Input params |
2299 | * tf : tracefile |
2300 | * field : field |
2301 | * |
2302 | ****************************************************************************/ |
3c165eaf |
2303 | #if 0 |
743e50fd |
2304 | void field_compute_static_size(LttFacility *fac, LttField *field) |
f104d082 |
2305 | { |
2312de30 |
2306 | LttType *type = &field->field_type; |
f104d082 |
2307 | |
2308 | switch(type->type_class) { |
2309 | case LTT_INT_FIXED: |
2310 | case LTT_UINT_FIXED: |
2311 | case LTT_POINTER: |
2312 | case LTT_CHAR: |
2313 | case LTT_UCHAR: |
2314 | case LTT_SHORT: |
2315 | case LTT_USHORT: |
2316 | case LTT_INT: |
2317 | case LTT_UINT: |
2318 | case LTT_LONG: |
2319 | case LTT_ULONG: |
2320 | case LTT_SIZE_T: |
2321 | case LTT_SSIZE_T: |
2322 | case LTT_OFF_T: |
2323 | case LTT_FLOAT: |
2324 | case LTT_ENUM: |
2325 | case LTT_STRING: |
2326 | /* nothing to do */ |
2327 | break; |
2328 | case LTT_ARRAY: |
2329 | /* note this : array type size is the number of elements in the array, |
2330 | * while array field size of the length of the array in bytes */ |
2331 | g_assert(type->fields->len == 1); |
2332 | { |
2333 | LttField *child = &g_array_index(type->fields, LttField, 0); |
743e50fd |
2334 | field_compute_static_size(fac, child); |
f104d082 |
2335 | |
2336 | if(child->field_size != 0) { |
2337 | field->field_size = type->size * child->field_size; |
2338 | field->dynamic_offsets = g_array_sized_new(FALSE, TRUE, |
2339 | sizeof(off_t), type->size); |
2340 | } else { |
2341 | field->field_size = 0; |
2342 | } |
2343 | } |
2344 | break; |
2345 | case LTT_SEQUENCE: |
2346 | g_assert(type->fields->len == 2); |
2347 | { |
2312de30 |
2348 | off_t local_offset = 0; |
f104d082 |
2349 | LttField *child = &g_array_index(type->fields, LttField, 1); |
743e50fd |
2350 | field_compute_static_size(fac, child); |
f104d082 |
2351 | field->field_size = 0; |
2352 | type->size = 0; |
2353 | if(child->field_size != 0) { |
2354 | field->dynamic_offsets = g_array_sized_new(FALSE, TRUE, |
2355 | sizeof(off_t), SEQUENCE_AVG_ELEMENTS); |
2356 | } |
2357 | } |
2358 | break; |
2359 | case LTT_STRUCT: |
2360 | case LTT_UNION: |
2361 | { |
2362 | guint i; |
2363 | for(i=0;i<type->fields->len;i++) { |
2364 | LttField *child = &g_array_index(type->fields, LttField, i); |
743e50fd |
2365 | field_compute_static_size(fac, child); |
f104d082 |
2366 | if(child->field_size != 0) { |
743e50fd |
2367 | type->size += ltt_align(type->size, get_alignment(child), |
2368 | fac->alignment); |
f104d082 |
2369 | type->size += child->field_size; |
2370 | } else { |
2371 | /* As soon as we find a child with variable size, we have |
2372 | * a variable size */ |
2373 | type->size = 0; |
2374 | break; |
2375 | } |
2376 | } |
2377 | field->field_size = type->size; |
2378 | } |
2379 | break; |
2380 | default: |
2381 | g_error("field_static_size : unknown type"); |
2312de30 |
2382 | } |
f104d082 |
2383 | |
2384 | } |
3c165eaf |
2385 | #endif //0 |
f104d082 |
2386 | |
2387 | |
2388 | /***************************************************************************** |
2389 | *Function name |
2390 | * precompute_fields_offsets : set the precomputable offset of the fields |
2391 | *Input params |
743e50fd |
2392 | * fac : facility |
f104d082 |
2393 | * field : the field |
2394 | * offset : pointer to the current offset, must be incremented |
2395 | * |
2396 | * return : 1 : found a variable length field, stop the processing. |
2397 | * 0 otherwise. |
2398 | ****************************************************************************/ |
2399 | |
3c165eaf |
2400 | #if 0 |
dd3a6d39 |
2401 | gint precompute_fields_offsets(LttFacility *fac, LttField *field, off_t *offset, gint is_compact) |
f104d082 |
2402 | { |
2312de30 |
2403 | LttType *type = &field->field_type; |
dd3a6d39 |
2404 | |
2405 | if(unlikely(is_compact)) { |
2406 | g_assert(field->field_size != 0); |
2407 | /* FIXME THIS IS A HUUUUUGE hack : |
2408 | * offset is between the compact_data field in struct LttEvent |
2409 | * and the address of the field root in the memory map. |
2410 | * ark. Both will stay at the same addresses while the event |
2411 | * is readable, so it's ok. |
2412 | */ |
2413 | field->offset_root = 0; |
2414 | field->fixed_root = FIELD_FIXED; |
2415 | return 0; |
2416 | } |
f104d082 |
2417 | |
2418 | switch(type->type_class) { |
2419 | case LTT_INT_FIXED: |
2420 | case LTT_UINT_FIXED: |
2421 | case LTT_POINTER: |
2422 | case LTT_CHAR: |
2423 | case LTT_UCHAR: |
2424 | case LTT_SHORT: |
2425 | case LTT_USHORT: |
2426 | case LTT_INT: |
2427 | case LTT_UINT: |
2428 | case LTT_LONG: |
2429 | case LTT_ULONG: |
2430 | case LTT_SIZE_T: |
2431 | case LTT_SSIZE_T: |
2432 | case LTT_OFF_T: |
2433 | case LTT_FLOAT: |
2434 | case LTT_ENUM: |
743e50fd |
2435 | g_assert(field->field_size != 0); |
f104d082 |
2436 | /* Align offset on type size */ |
743e50fd |
2437 | *offset += ltt_align(*offset, get_alignment(field), |
2438 | fac->alignment); |
f104d082 |
2439 | /* remember offset */ |
2440 | field->offset_root = *offset; |
2441 | field->fixed_root = FIELD_FIXED; |
2442 | /* Increment offset */ |
2443 | *offset += field->field_size; |
2444 | return 0; |
2445 | break; |
2446 | case LTT_STRING: |
2447 | field->offset_root = *offset; |
2448 | field->fixed_root = FIELD_FIXED; |
2449 | return 1; |
2450 | break; |
2451 | case LTT_ARRAY: |
2452 | g_assert(type->fields->len == 1); |
2453 | { |
2454 | LttField *child = &g_array_index(type->fields, LttField, 0); |
2455 | |
743e50fd |
2456 | *offset += ltt_align(*offset, get_alignment(field), |
2457 | fac->alignment); |
f104d082 |
2458 | |
2459 | /* remember offset */ |
2460 | field->offset_root = *offset; |
2461 | field->array_offset = *offset; |
2462 | field->fixed_root = FIELD_FIXED; |
2463 | |
2464 | /* Let the child be variable */ |
2465 | //precompute_fields_offsets(tf, child, offset); |
2466 | |
2467 | if(field->field_size != 0) { |
2468 | /* Increment offset */ |
2469 | /* field_size is the array size in bytes */ |
2470 | *offset += field->field_size; |
2471 | return 0; |
2472 | } else { |
2473 | return 1; |
2474 | } |
2475 | } |
2476 | break; |
2477 | case LTT_SEQUENCE: |
2478 | g_assert(type->fields->len == 2); |
2479 | { |
2480 | LttField *child; |
2481 | guint ret; |
2482 | |
743e50fd |
2483 | *offset += ltt_align(*offset, get_alignment(field), |
2484 | fac->alignment); |
f104d082 |
2485 | |
2486 | /* remember offset */ |
2487 | field->offset_root = *offset; |
2488 | field->fixed_root = FIELD_FIXED; |
2489 | |
2490 | child = &g_array_index(type->fields, LttField, 0); |
dd3a6d39 |
2491 | ret = precompute_fields_offsets(fac, child, offset, is_compact); |
f104d082 |
2492 | g_assert(ret == 0); /* Seq len cannot have variable len */ |
2493 | |
2494 | child = &g_array_index(type->fields, LttField, 1); |
743e50fd |
2495 | *offset += ltt_align(*offset, get_alignment(child), |
2496 | fac->alignment); |
f104d082 |
2497 | field->array_offset = *offset; |
743e50fd |
2498 | /* Let the child be variable. */ |
2499 | //ret = precompute_fields_offsets(fac, child, offset); |
f104d082 |
2500 | |
2501 | /* Cannot precompute fields offsets of sequence members, and has |
2502 | * variable length. */ |
2503 | return 1; |
2504 | } |
2505 | break; |
2506 | case LTT_STRUCT: |
2507 | { |
2508 | LttField *child; |
2509 | guint i; |
2510 | gint ret=0; |
2511 | |
743e50fd |
2512 | *offset += ltt_align(*offset, get_alignment(field), |
2513 | fac->alignment); |
f104d082 |
2514 | /* remember offset */ |
2515 | field->offset_root = *offset; |
2516 | field->fixed_root = FIELD_FIXED; |
2517 | |
2518 | for(i=0; i< type->fields->len; i++) { |
2519 | child = &g_array_index(type->fields, LttField, i); |
dd3a6d39 |
2520 | ret = precompute_fields_offsets(fac, child, offset, is_compact); |
f104d082 |
2521 | |
2522 | if(ret) break; |
2523 | } |
2524 | return ret; |
2525 | } |
2526 | break; |
2527 | case LTT_UNION: |
2528 | { |
2529 | LttField *child; |
2530 | guint i; |
2531 | gint ret=0; |
2532 | |
743e50fd |
2533 | *offset += ltt_align(*offset, get_alignment(field), |
2534 | fac->alignment); |
f104d082 |
2535 | /* remember offset */ |
2536 | field->offset_root = *offset; |
2537 | field->fixed_root = FIELD_FIXED; |
2538 | |
2539 | for(i=0; i< type->fields->len; i++) { |
2540 | *offset = field->offset_root; |
2541 | child = &g_array_index(type->fields, LttField, i); |
dd3a6d39 |
2542 | ret = precompute_fields_offsets(fac, child, offset, is_compact); |
f104d082 |
2543 | |
2544 | if(ret) break; |
2545 | } |
2546 | *offset = field->offset_root + field->field_size; |
2547 | return ret; |
2548 | } |
2549 | |
2550 | break; |
2551 | case LTT_NONE: |
2552 | default: |
2553 | g_error("precompute_fields_offsets : unknown type"); |
2554 | return 1; |
2555 | } |
2556 | |
2557 | } |
2558 | |
3c165eaf |
2559 | #endif //0 |
f104d082 |
2560 | |
3c165eaf |
2561 | #if 0 |
f104d082 |
2562 | /***************************************************************************** |
2563 | *Function name |
2564 | * precompute_offsets : set the precomputable offset of an event type |
2565 | *Input params |
2566 | * tf : tracefile |
2567 | * event : event type |
2568 | * |
2569 | ****************************************************************************/ |
743e50fd |
2570 | void precompute_offsets(LttFacility *fac, LttEventType *event) |
f104d082 |
2571 | { |
2572 | guint i; |
2573 | off_t offset = 0; |
2574 | gint ret; |
2575 | |
2576 | /* First, compute the size of fixed size fields. Will determine size for |
2577 | * arrays, struct and unions, which is not done by the parser */ |
2578 | for(i=0; i<event->fields->len; i++) { |
2579 | LttField *field = &g_array_index(event->fields, LttField, i); |
743e50fd |
2580 | field_compute_static_size(fac, field); |
f104d082 |
2581 | } |
2582 | |
2583 | /* Precompute all known offsets */ |
2584 | for(i=0; i<event->fields->len; i++) { |
2585 | LttField *field = &g_array_index(event->fields, LttField, i); |
dd3a6d39 |
2586 | if(event->has_compact_data && i == 0) |
2587 | ret = precompute_fields_offsets(fac, field, &offset, 1); |
2588 | else |
2589 | ret = precompute_fields_offsets(fac, field, &offset, 0); |
f104d082 |
2590 | if(ret) break; |
2591 | } |
2592 | } |
3c165eaf |
2593 | #endif //0 |
f104d082 |
2594 | |
e4eced0f |
2595 | |
bbf28e50 |
2596 | |
3aee1200 |
2597 | /***************************************************************************** |
2598 | *Function name |
2599 | * preset_field_type_size : set the fixed sizes of the field type |
2600 | *Input params |
2601 | * tf : tracefile |
2602 | * event_type : event type |
2603 | * offset_root : offset from the root |
2604 | * offset_parent : offset from the parent |
2605 | * fixed_root : Do we know a fixed offset to the root ? |
2606 | * fixed_parent : Do we know a fixed offset to the parent ? |
2607 | * field : field |
2608 | ****************************************************************************/ |
f104d082 |
2609 | |
2610 | |
2611 | |
2612 | // preset the fixed size offsets. Calculate them just like genevent-new : an |
2613 | // increment of a *to value that represents the offset from the start of the |
2614 | // event data. |
2615 | // The preset information is : offsets up to (and including) the first element |
2616 | // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET". |
2617 | #if 0 |
3aee1200 |
2618 | void preset_field_type_size(LttTracefile *tf, LttEventType *event_type, |
2619 | off_t offset_root, off_t offset_parent, |
2620 | enum field_status *fixed_root, enum field_status *fixed_parent, |
2621 | LttField *field) |
2622 | { |
2623 | enum field_status local_fixed_root, local_fixed_parent; |
2624 | guint i; |
2625 | LttType *type; |
dfb73233 |
2626 | |
3aee1200 |
2627 | g_assert(field->fixed_root == FIELD_UNKNOWN); |
2628 | g_assert(field->fixed_parent == FIELD_UNKNOWN); |
2629 | g_assert(field->fixed_size == FIELD_UNKNOWN); |
dfb73233 |
2630 | |
3aee1200 |
2631 | type = field->field_type; |
2632 | |
2633 | field->fixed_root = *fixed_root; |
2634 | if(field->fixed_root == FIELD_FIXED) |
2635 | field->offset_root = offset_root; |
2636 | else |
2637 | field->offset_root = 0; |
2638 | |
2639 | field->fixed_parent = *fixed_parent; |
2640 | if(field->fixed_parent == FIELD_FIXED) |
2641 | field->offset_parent = offset_parent; |
2642 | else |
2643 | field->offset_parent = 0; |
2644 | |
2645 | size_t current_root_offset; |
2646 | size_t current_offset; |
2647 | enum field_status current_child_status, final_child_status; |
2648 | size_t max_size; |
2649 | |
2650 | switch(type->type_class) { |
83e160f2 |
2651 | case LTT_INT_FIXED: |
2652 | case LTT_UINT_FIXED: |
2653 | case LTT_CHAR: |
2654 | case LTT_UCHAR: |
2655 | case LTT_SHORT: |
2656 | case LTT_USHORT: |
3aee1200 |
2657 | case LTT_INT: |
2658 | case LTT_UINT: |
2659 | case LTT_FLOAT: |
2660 | case LTT_ENUM: |
2661 | field->field_size = ltt_type_size(tf->trace, type); |
2662 | field->fixed_size = FIELD_FIXED; |
2663 | break; |
2664 | case LTT_POINTER: |
2665 | field->field_size = (off_t)event_type->facility->pointer_size; |
2666 | field->fixed_size = FIELD_FIXED; |
2667 | break; |
2668 | case LTT_LONG: |
2669 | case LTT_ULONG: |
cb03932a |
2670 | field->field_size = (off_t)event_type->facility->long_size; |
3aee1200 |
2671 | field->fixed_size = FIELD_FIXED; |
2672 | break; |
2673 | case LTT_SIZE_T: |
2674 | case LTT_SSIZE_T: |
2675 | case LTT_OFF_T: |
2676 | field->field_size = (off_t)event_type->facility->size_t_size; |
2677 | field->fixed_size = FIELD_FIXED; |
2678 | break; |
2679 | case LTT_SEQUENCE: |
2680 | local_fixed_root = FIELD_VARIABLE; |
2681 | local_fixed_parent = FIELD_VARIABLE; |
2682 | preset_field_type_size(tf, event_type, |
2683 | 0, 0, |
2684 | &local_fixed_root, &local_fixed_parent, |
2685 | field->child[0]); |
2686 | field->fixed_size = FIELD_VARIABLE; |
2687 | field->field_size = 0; |
27304273 |
2688 | *fixed_root = FIELD_VARIABLE; |
2689 | *fixed_parent = FIELD_VARIABLE; |
3aee1200 |
2690 | break; |
2691 | case LTT_STRING: |
2692 | field->fixed_size = FIELD_VARIABLE; |
2693 | field->field_size = 0; |
27304273 |
2694 | *fixed_root = FIELD_VARIABLE; |
2695 | *fixed_parent = FIELD_VARIABLE; |
3aee1200 |
2696 | break; |
2697 | case LTT_ARRAY: |
2698 | local_fixed_root = FIELD_VARIABLE; |
2699 | local_fixed_parent = FIELD_VARIABLE; |
2700 | preset_field_type_size(tf, event_type, |
2701 | 0, 0, |
2702 | &local_fixed_root, &local_fixed_parent, |
2703 | field->child[0]); |
2704 | field->fixed_size = field->child[0]->fixed_size; |
27304273 |
2705 | if(field->fixed_size == FIELD_FIXED) { |
3aee1200 |
2706 | field->field_size = type->element_number * field->child[0]->field_size; |
27304273 |
2707 | } else { |
3aee1200 |
2708 | field->field_size = 0; |
27304273 |
2709 | *fixed_root = FIELD_VARIABLE; |
2710 | *fixed_parent = FIELD_VARIABLE; |
2711 | } |
3aee1200 |
2712 | break; |
2713 | case LTT_STRUCT: |
2714 | current_root_offset = field->offset_root; |
2715 | current_offset = 0; |
2716 | current_child_status = FIELD_FIXED; |
2717 | for(i=0;i<type->element_number;i++) { |
2718 | preset_field_type_size(tf, event_type, |
2719 | current_root_offset, current_offset, |
2720 | fixed_root, ¤t_child_status, |
2721 | field->child[i]); |
2722 | if(current_child_status == FIELD_FIXED) { |
2723 | current_root_offset += field->child[i]->field_size; |
2724 | current_offset += field->child[i]->field_size; |
2725 | } else { |
2726 | current_root_offset = 0; |
2727 | current_offset = 0; |
2728 | } |
2729 | } |
2730 | if(current_child_status != FIELD_FIXED) { |
2731 | *fixed_parent = current_child_status; |
2732 | field->field_size = 0; |
2733 | field->fixed_size = current_child_status; |
2734 | } else { |
2735 | field->field_size = current_offset; |
2736 | field->fixed_size = FIELD_FIXED; |
2737 | } |
2738 | break; |
2739 | case LTT_UNION: |
2740 | current_root_offset = field->offset_root; |
2741 | current_offset = 0; |
2742 | max_size = 0; |
2743 | final_child_status = FIELD_FIXED; |
2744 | for(i=0;i<type->element_number;i++) { |
2745 | enum field_status current_root_child_status = FIELD_FIXED; |
2746 | enum field_status current_child_status = FIELD_FIXED; |
2747 | preset_field_type_size(tf, event_type, |
2748 | current_root_offset, current_offset, |
2749 | ¤t_root_child_status, ¤t_child_status, |
2750 | field->child[i]); |
2751 | if(current_child_status != FIELD_FIXED) |
2752 | final_child_status = current_child_status; |
2753 | else |
2754 | max_size = max(max_size, field->child[i]->field_size); |
2755 | } |
2756 | if(final_child_status != FIELD_FIXED) { |
62e4e7bf |
2757 | g_error("LTTV does not support variable size fields in unions."); |
2758 | /* This will stop the application. */ |
3aee1200 |
2759 | *fixed_root = final_child_status; |
2760 | *fixed_parent = final_child_status; |
2761 | field->field_size = 0; |
2762 | field->fixed_size = current_child_status; |
2763 | } else { |
2764 | field->field_size = max_size; |
2765 | field->fixed_size = FIELD_FIXED; |
2766 | } |
2767 | break; |
83e160f2 |
2768 | case LTT_NONE: |
2769 | g_error("unexpected type NONE"); |
2770 | break; |
dfb73233 |
2771 | } |
2772 | |
6cd62ccf |
2773 | } |
f104d082 |
2774 | #endif //0 |
3aee1200 |
2775 | |
77175651 |
2776 | /***************************************************************************** |
2777 | *Function name |
2778 | * check_fields_compatibility : Check for compatibility between two fields : |
2779 | * do they use the same inner structure ? |
2780 | *Input params |
2781 | * event_type1 : event type |
2782 | * event_type2 : event type |
2783 | * field1 : field |
2784 | * field2 : field |
2785 | *Returns : 0 if identical |
2786 | * 1 if not. |
2787 | ****************************************************************************/ |
f104d082 |
2788 | // this function checks for equality of field types. Therefore, it does not use |
2789 | // per se offsets. For instance, an aligned version of a structure is |
2790 | // compatible with an unaligned version of the same structure. |
3c165eaf |
2791 | #if 0 |
f104d082 |
2792 | gint check_fields_compatibility(LttEventType *event_type1, |
2793 | LttEventType *event_type2, |
2794 | LttField *field1, LttField *field2) |
2795 | { |
2796 | guint different = 0; |
2312de30 |
2797 | LttType *type1; |
2798 | LttType *type2; |
f104d082 |
2799 | |
2800 | if(field1 == NULL) { |
2801 | if(field2 == NULL) goto end; |
2802 | else { |
2803 | different = 1; |
2804 | goto end; |
2805 | } |
2806 | } else if(field2 == NULL) { |
2807 | different = 1; |
2808 | goto end; |
2809 | } |
2810 | |
2312de30 |
2811 | type1 = &field1->field_type; |
2812 | type2 = &field2->field_type; |
f104d082 |
2813 | |
2814 | if(type1->type_class != type2->type_class) { |
2815 | different = 1; |
2816 | goto end; |
2817 | } |
62e4e7bf |
2818 | if(type1->network != type2->network) { |
2819 | different = 1; |
2820 | goto end; |
2821 | } |
f104d082 |
2822 | |
2823 | switch(type1->type_class) { |
2824 | case LTT_INT_FIXED: |
2825 | case LTT_UINT_FIXED: |
2826 | case LTT_POINTER: |
2827 | case LTT_CHAR: |
2828 | case LTT_UCHAR: |
2829 | case LTT_SHORT: |
2830 | case LTT_USHORT: |
2831 | case LTT_INT: |
2832 | case LTT_UINT: |
2833 | case LTT_LONG: |
2834 | case LTT_ULONG: |
2835 | case LTT_SIZE_T: |
2836 | case LTT_SSIZE_T: |
2837 | case LTT_OFF_T: |
2838 | case LTT_FLOAT: |
2839 | case LTT_ENUM: |
2840 | if(field1->field_size != field2->field_size) |
2841 | different = 1; |
2842 | break; |
2843 | case LTT_STRING: |
2844 | break; |
2845 | case LTT_ARRAY: |
2846 | { |
2847 | LttField *child1 = &g_array_index(type1->fields, LttField, 0); |
2848 | LttField *child2 = &g_array_index(type2->fields, LttField, 0); |
2849 | |
2850 | if(type1->size != type2->size) |
2851 | different = 1; |
2852 | if(check_fields_compatibility(event_type1, event_type2, child1, child2)) |
2853 | different = 1; |
2854 | } |
2855 | break; |
2856 | case LTT_SEQUENCE: |
2857 | { |
2858 | LttField *child1 = &g_array_index(type1->fields, LttField, 1); |
2859 | LttField *child2 = &g_array_index(type2->fields, LttField, 1); |
2860 | |
2861 | if(check_fields_compatibility(event_type1, event_type2, child1, child2)) |
2862 | different = 1; |
2863 | } |
2864 | break; |
2865 | case LTT_STRUCT: |
2866 | case LTT_UNION: |
2867 | { |
2868 | LttField *child; |
2869 | guint i; |
2870 | |
2871 | if(type1->fields->len != type2->fields->len) { |
2872 | different = 1; |
2873 | goto end; |
2874 | } |
2875 | |
2876 | for(i=0; i< type1->fields->len; i++) { |
2312de30 |
2877 | LttField *child1; |
2878 | LttField *child2; |
f104d082 |
2879 | child1 = &g_array_index(type1->fields, LttField, i); |
2880 | child2 = &g_array_index(type2->fields, LttField, i); |
2881 | different = check_fields_compatibility(event_type1, |
2882 | event_type2, child1, child2); |
2883 | |
2884 | if(different) break; |
2885 | } |
2886 | } |
2887 | break; |
2888 | case LTT_NONE: |
2889 | default: |
dd3a6d39 |
2890 | g_error("check_fields_compatibility : unknown type"); |
f104d082 |
2891 | } |
2892 | |
2893 | end: |
2894 | return different; |
2895 | } |
3c165eaf |
2896 | #endif //0 |
f104d082 |
2897 | |
2898 | #if 0 |
77175651 |
2899 | gint check_fields_compatibility(LttEventType *event_type1, |
2900 | LttEventType *event_type2, |
2901 | LttField *field1, LttField *field2) |
2902 | { |
2903 | guint different = 0; |
77175651 |
2904 | guint i; |
2905 | LttType *type1; |
2906 | LttType *type2; |
2907 | |
2908 | if(field1 == NULL) { |
2909 | if(field2 == NULL) goto end; |
2910 | else { |
2911 | different = 1; |
2912 | goto end; |
2913 | } |
2914 | } else if(field2 == NULL) { |
2915 | different = 1; |
2916 | goto end; |
2917 | } |
2918 | |
2919 | g_assert(field1->fixed_root != FIELD_UNKNOWN); |
2920 | g_assert(field2->fixed_root != FIELD_UNKNOWN); |
2921 | g_assert(field1->fixed_parent != FIELD_UNKNOWN); |
2922 | g_assert(field2->fixed_parent != FIELD_UNKNOWN); |
2923 | g_assert(field1->fixed_size != FIELD_UNKNOWN); |
2924 | g_assert(field2->fixed_size != FIELD_UNKNOWN); |
2925 | |
2926 | type1 = field1->field_type; |
2927 | type2 = field2->field_type; |
2928 | |
77175651 |
2929 | if(type1->type_class != type2->type_class) { |
2930 | different = 1; |
2931 | goto end; |
2932 | } |
2933 | if(type1->element_name != type2->element_name) { |
2934 | different = 1; |
2935 | goto end; |
2936 | } |
2937 | |
2938 | switch(type1->type_class) { |
83e160f2 |
2939 | case LTT_INT_FIXED: |
2940 | case LTT_UINT_FIXED: |
2941 | case LTT_POINTER: |
2942 | case LTT_CHAR: |
2943 | case LTT_UCHAR: |
2944 | case LTT_SHORT: |
2945 | case LTT_USHORT: |
77175651 |
2946 | case LTT_INT: |
2947 | case LTT_UINT: |
2948 | case LTT_FLOAT: |
2949 | case LTT_POINTER: |
2950 | case LTT_LONG: |
2951 | case LTT_ULONG: |
2952 | case LTT_SIZE_T: |
2953 | case LTT_SSIZE_T: |
2954 | case LTT_OFF_T: |
2955 | if(field1->field_size != field2->field_size) { |
2956 | different = 1; |
2957 | goto end; |
2958 | } |
2959 | break; |
2960 | case LTT_ENUM: |
2961 | if(type1->element_number != type2->element_number) { |
2962 | different = 1; |
2963 | goto end; |
2964 | } |
2965 | for(i=0;i<type1->element_number;i++) { |
2966 | if(type1->enum_strings[i] != type2->enum_strings[i]) { |
2967 | different = 1; |
2968 | goto end; |
2969 | } |
2970 | } |
2971 | break; |
2972 | case LTT_SEQUENCE: |
2973 | /* Two elements : size and child */ |
2974 | g_assert(type1->element_number != type2->element_number); |
2975 | for(i=0;i<type1->element_number;i++) { |
2976 | if(check_fields_compatibility(event_type1, event_type2, |
2977 | field1->child[0], field2->child[0])) { |
2978 | different = 1; |
2979 | goto end; |
2980 | } |
2981 | } |
2982 | break; |
2983 | case LTT_STRING: |
2984 | break; |
2985 | case LTT_ARRAY: |
2986 | if(field1->field_size != field2->field_size) { |
2987 | different = 1; |
2988 | goto end; |
2989 | } |
2990 | /* Two elements : size and child */ |
2991 | g_assert(type1->element_number != type2->element_number); |
2992 | for(i=0;i<type1->element_number;i++) { |
2993 | if(check_fields_compatibility(event_type1, event_type2, |
2994 | field1->child[0], field2->child[0])) { |
2995 | different = 1; |
2996 | goto end; |
2997 | } |
2998 | } |
2999 | break; |
3000 | case LTT_STRUCT: |
3001 | case LTT_UNION: |
3002 | if(type1->element_number != type2->element_number) { |
3003 | different = 1; |
3004 | break; |
3005 | } |
3006 | for(i=0;i<type1->element_number;i++) { |
3007 | if(check_fields_compatibility(event_type1, event_type2, |
3008 | field1->child[0], field2->child[0])) { |
3009 | different = 1; |
3010 | goto end; |
3011 | } |
3012 | } |
3013 | break; |
3014 | } |
3015 | end: |
3016 | return different; |
3017 | } |
3aee1200 |
3018 | #endif //0 |
6cd62ccf |
3019 | |
f104d082 |
3020 | |
6cd62ccf |
3021 | /***************************************************************************** |
3022 | *Function name |
eed2ef37 |
3023 | * ltt_get_int : get an integer number |
6cd62ccf |
3024 | *Input params |
3aee1200 |
3025 | * reverse_byte_order: must we reverse the byte order ? |
6cd62ccf |
3026 | * size : the size of the integer |
3aee1200 |
3027 | * ptr : the data pointer |
6cd62ccf |
3028 | *Return value |
cf74a6f1 |
3029 | * gint64 : a 64 bits integer |
6cd62ccf |
3030 | ****************************************************************************/ |
3031 | |
eed2ef37 |
3032 | gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data) |
6cd62ccf |
3033 | { |
3aee1200 |
3034 | gint64 val; |
cf74a6f1 |
3035 | |
3036 | switch(size) { |
3aee1200 |
3037 | case 1: val = *((gint8*)data); break; |
3038 | case 2: val = ltt_get_int16(reverse_byte_order, data); break; |
3039 | case 4: val = ltt_get_int32(reverse_byte_order, data); break; |
3040 | case 8: val = ltt_get_int64(reverse_byte_order, data); break; |
3041 | default: val = ltt_get_int64(reverse_byte_order, data); |
3042 | g_critical("get_int : integer size %d unknown", size); |
cf74a6f1 |
3043 | break; |
3044 | } |
3045 | |
3aee1200 |
3046 | return val; |
6cd62ccf |
3047 | } |
3aee1200 |
3048 | |
6cd62ccf |
3049 | /***************************************************************************** |
3050 | *Function name |
eed2ef37 |
3051 | * ltt_get_uint : get an unsigned integer number |
6cd62ccf |
3052 | *Input params |
3aee1200 |
3053 | * reverse_byte_order: must we reverse the byte order ? |
3054 | * size : the size of the integer |
3055 | * ptr : the data pointer |
3056 | *Return value |
3057 | * guint64 : a 64 bits unsigned integer |
6cd62ccf |
3058 | ****************************************************************************/ |
3059 | |
eed2ef37 |
3060 | guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data) |
6cd62ccf |
3061 | { |
3aee1200 |
3062 | guint64 val; |
3063 | |
3064 | switch(size) { |
3065 | case 1: val = *((gint8*)data); break; |
3066 | case 2: val = ltt_get_uint16(reverse_byte_order, data); break; |
3067 | case 4: val = ltt_get_uint32(reverse_byte_order, data); break; |
3068 | case 8: val = ltt_get_uint64(reverse_byte_order, data); break; |
3069 | default: val = ltt_get_uint64(reverse_byte_order, data); |
3070 | g_critical("get_uint : unsigned integer size %d unknown", |
3071 | size); |
3072 | break; |
3073 | } |
3074 | |
3075 | return val; |
6cd62ccf |
3076 | } |
3aee1200 |
3077 | |
3078 | |
a5dcde2f |
3079 | /* get the node name of the system */ |
3080 | |
3081 | char * ltt_trace_system_description_node_name (LttSystemDescription * s) |
3082 | { |
3083 | return s->node_name; |
3084 | } |
3085 | |
3086 | |
3087 | /* get the domain name of the system */ |
3088 | |
3089 | char * ltt_trace_system_description_domain_name (LttSystemDescription * s) |
3090 | { |
3091 | return s->domain_name; |
3092 | } |
3093 | |
3094 | |
3095 | /* get the description of the system */ |
3096 | |
3097 | char * ltt_trace_system_description_description (LttSystemDescription * s) |
3098 | { |
3099 | return s->description; |
3100 | } |
3101 | |
3102 | |
bf33dd50 |
3103 | /* get the NTP corrected start time of the trace */ |
7bd563ec |
3104 | LttTime ltt_trace_start_time(LttTrace *t) |
a5dcde2f |
3105 | { |
7bd563ec |
3106 | return t->start_time; |
a5dcde2f |
3107 | } |
3108 | |
bf33dd50 |
3109 | /* get the monotonic start time of the trace */ |
3110 | LttTime ltt_trace_start_time_monotonic(LttTrace *t) |
3111 | { |
3112 | return t->start_time_from_tsc; |
3113 | } |
3114 | |
18206708 |
3115 | LttTracefile *ltt_tracefile_new() |
3116 | { |
3117 | return g_new(LttTracefile, 1); |
3118 | } |
3119 | |
3120 | void ltt_tracefile_destroy(LttTracefile *tf) |
3121 | { |
3122 | g_free(tf); |
3123 | } |
3124 | |
3125 | void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src) |
3126 | { |
3127 | *dest = *src; |
3128 | } |
3129 | |
3aee1200 |
3130 | /* Before library loading... */ |
3131 | |
3c165eaf |
3132 | void init(void) |
3aee1200 |
3133 | { |
3134 | LTT_FACILITY_NAME_HEARTBEAT = g_quark_from_string("heartbeat"); |
3135 | LTT_EVENT_NAME_HEARTBEAT = g_quark_from_string("heartbeat"); |
d1bb700c |
3136 | LTT_EVENT_NAME_HEARTBEAT_FULL = g_quark_from_string("heartbeat_full"); |
3aee1200 |
3137 | |
3865ea09 |
3138 | LTT_TRACEFILE_NAME_FACILITIES = g_quark_from_string("/control/facilities"); |
3aee1200 |
3139 | } |