Commit | Line | Data |
---|---|---|
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> |
43ed82b5 | 35 | #include <glib/gprintf.h> |
3aee1200 | 36 | #include <malloc.h> |
37 | #include <sys/mman.h> | |
dd3a6d39 | 38 | #include <string.h> |
43ed82b5 | 39 | #include <ctype.h> |
40 | #include <inttypes.h> | |
6cd62ccf | 41 | |
ef35d837 | 42 | // For realpath |
43 | #include <limits.h> | |
44 | #include <stdlib.h> | |
45 | ||
46 | ||
a5dcde2f | 47 | #include <ltt/ltt.h> |
48 | #include "ltt-private.h" | |
963b5f2d | 49 | #include <ltt/trace.h> |
c02ea99f | 50 | #include <ltt/event.h> |
1a2ceb63 | 51 | #include <ltt/ltt-types.h> |
bb38a290 | 52 | #include <ltt/marker.h> |
3aee1200 | 53 | |
f0241068 MD |
54 | #define DEFAULT_N_BLOCKS 32 |
55 | ||
43ed82b5 | 56 | /* from marker.c */ |
57 | extern long marker_update_fields_offsets(struct marker_info *info, const char *data); | |
1184dc37 MD |
58 | extern void marker_update_event_fields_offsets(GArray *fields_offsets, |
59 | struct marker_info *info); | |
43ed82b5 | 60 | |
2fc874ab | 61 | /* Tracefile names used in this file */ |
3aee1200 | 62 | |
2fc874ab | 63 | GQuark LTT_TRACEFILE_NAME_METADATA; |
3aee1200 | 64 | |
86005ded | 65 | #ifndef g_open |
66 | #define g_open open | |
67 | #endif | |
68 | ||
69 | ||
51b5991e | 70 | #define __UNUSED__ __attribute__((__unused__)) |
6cd62ccf | 71 | |
a1062ddd | 72 | #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format) |
3aee1200 | 73 | |
74 | #ifndef g_debug | |
a1062ddd | 75 | #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format) |
3aee1200 | 76 | #endif |
a1062ddd | 77 | |
45e14832 | 78 | #define g_close close |
8959a0c8 | 79 | |
b77d1b57 | 80 | /* Those macros must be called from within a function where page_size is a known |
81 | * variable */ | |
82 | #define PAGE_MASK (~(page_size-1)) | |
83 | #define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK) | |
84 | ||
6cd62ccf | 85 | /* set the offset of the fields belonging to the event, |
86 | need the information of the archecture */ | |
f104d082 | 87 | //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type); |
eed2ef37 | 88 | //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data); |
6cd62ccf | 89 | |
3aee1200 | 90 | /* map a fixed size or a block information from the file (fd) */ |
91 | static gint map_block(LttTracefile * tf, guint block_num); | |
92 | ||
93 | /* calculate nsec per cycles for current block */ | |
791dffa6 | 94 | #if 0 |
95 | static guint32 calc_nsecs_per_cycle(LttTracefile * t); | |
96 | static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles); | |
97 | #endif //0 | |
6cd62ccf | 98 | |
3aee1200 | 99 | /* go to the next event */ |
100 | static int ltt_seek_next_event(LttTracefile *tf); | |
6cd62ccf | 101 | |
3c165eaf | 102 | static int open_tracefiles(LttTrace *trace, gchar *root_path, |
103 | gchar *relative_path); | |
2fc874ab | 104 | static int ltt_process_metadata_tracefile(LttTracefile *tf); |
3c165eaf | 105 | static void ltt_tracefile_time_span_get(LttTracefile *tf, |
106 | LttTime *start, LttTime *end); | |
107 | static void group_time_span_get(GQuark name, gpointer data, gpointer user_data); | |
108 | static gint map_block(LttTracefile * tf, guint block_num); | |
3c165eaf | 109 | static void ltt_update_event_size(LttTracefile *tf); |
8b0a0cc8 YB |
110 | static LttTrace *_ltt_trace_open(const gchar *pathname, gboolean is_live); |
111 | static int ltt_tracefile_update(LttTracefile *tf); | |
112 | static void update_tracefile_group(GQuark name, gpointer data, gpointer user_data); | |
91f8d488 | 113 | |
114 | /* Enable event debugging */ | |
115 | static int a_event_debug = 0; | |
116 | ||
117 | void ltt_event_debug(int state) | |
118 | { | |
119 | a_event_debug = state; | |
120 | } | |
121 | ||
b7576a11 | 122 | /* trace can be NULL |
123 | * | |
124 | * Return value : 0 success, 1 bad tracefile | |
125 | */ | |
64dd41a5 | 126 | static int parse_trace_header(ltt_subbuffer_header_t *header, |
127 | LttTracefile *tf, LttTrace *t) | |
b7576a11 | 128 | { |
64dd41a5 | 129 | if (header->magic_number == LTT_MAGIC_NUMBER) |
b7576a11 | 130 | tf->reverse_bo = 0; |
64dd41a5 | 131 | else if(header->magic_number == LTT_REV_MAGIC_NUMBER) |
b7576a11 | 132 | tf->reverse_bo = 1; |
133 | else /* invalid magic number, bad tracefile ! */ | |
134 | return 1; | |
64dd41a5 | 135 | |
136 | if(t) { | |
137 | t->ltt_major_version = header->major_version; | |
138 | t->ltt_minor_version = header->minor_version; | |
139 | t->arch_size = header->arch_size; | |
140 | } | |
141 | tf->alignment = header->alignment; | |
142 | ||
b7576a11 | 143 | /* Get float byte order : might be different from int byte order |
144 | * (or is set to 0 if the trace has no float (kernel trace)) */ | |
64dd41a5 | 145 | tf->float_word_order = 0; |
b7576a11 | 146 | |
64dd41a5 | 147 | switch(header->major_version) { |
b7576a11 | 148 | case 0: |
2fc874ab | 149 | case 1: |
3c165eaf | 150 | g_warning("Unsupported trace version : %hhu.%hhu", |
64dd41a5 | 151 | header->major_version, header->minor_version); |
3c165eaf | 152 | return 1; |
153 | break; | |
2fc874ab | 154 | case 2: |
64dd41a5 | 155 | switch(header->minor_version) { |
621ef26f | 156 | case 6: |
b7576a11 | 157 | { |
621ef26f | 158 | struct ltt_subbuffer_header_2_6 *vheader = header; |
1550fba6 | 159 | tf->buffer_header_size = ltt_subbuffer_header_size(); |
64dd41a5 | 160 | tf->tscbits = 27; |
161 | tf->eventbits = 5; | |
2fc874ab | 162 | tf->tsc_mask = ((1ULL << tf->tscbits) - 1); |
163 | tf->tsc_mask_next_bit = (1ULL << tf->tscbits); | |
164 | ||
e939e5b2 | 165 | if(t) { |
166 | t->start_freq = ltt_get_uint64(LTT_GET_BO(tf), | |
167 | &vheader->start_freq); | |
a3999b49 | 168 | t->freq_scale = ltt_get_uint32(LTT_GET_BO(tf), |
169 | &vheader->freq_scale); | |
e939e5b2 | 170 | t->start_tsc = ltt_get_uint64(LTT_GET_BO(tf), |
64dd41a5 | 171 | &vheader->cycle_count_begin); |
172 | t->start_monotonic = 0; | |
e939e5b2 | 173 | t->start_time.tv_sec = ltt_get_uint64(LTT_GET_BO(tf), |
174 | &vheader->start_time_sec); | |
175 | t->start_time.tv_nsec = ltt_get_uint64(LTT_GET_BO(tf), | |
176 | &vheader->start_time_usec); | |
177 | t->start_time.tv_nsec *= 1000; /* microsec to nanosec */ | |
178 | ||
9c9bf2d4 BP |
179 | t->start_time_from_tsc = |
180 | ltt_time_from_uint64(tsc_to_uint64(t->freq_scale, | |
181 | t->start_freq, t->start_tsc)); | |
e939e5b2 | 182 | } |
183 | } | |
184 | break; | |
b7576a11 | 185 | default: |
986e2a7c | 186 | g_warning("Unsupported trace version : %hhu.%hhu", |
64dd41a5 | 187 | header->major_version, header->minor_version); |
b7576a11 | 188 | return 1; |
189 | } | |
190 | break; | |
b7576a11 | 191 | default: |
192 | g_warning("Unsupported trace version : %hhu.%hhu", | |
64dd41a5 | 193 | header->major_version, header->minor_version); |
b7576a11 | 194 | return 1; |
195 | } | |
b7576a11 | 196 | return 0; |
197 | } | |
198 | ||
f0241068 MD |
199 | int get_block_offset_size(LttTracefile *tf, guint block_num, |
200 | uint64_t *offset, uint32_t *size) | |
201 | { | |
202 | uint64_t offa, offb; | |
203 | ||
204 | if (unlikely(block_num >= tf->num_blocks)) | |
205 | return -1; | |
b7576a11 | 206 | |
f0241068 MD |
207 | offa = g_array_index(tf->buf_index, uint64_t, block_num); |
208 | if (likely(block_num < tf->num_blocks - 1)) | |
209 | offb = g_array_index(tf->buf_index, uint64_t, block_num + 1); | |
210 | else | |
211 | offb = tf->file_size; | |
212 | *offset = offa; | |
213 | *size = offb - offa; | |
214 | return 0; | |
215 | } | |
216 | ||
8b0a0cc8 YB |
217 | static int ltt_trace_update_block_index(LttTracefile *tf, uint64_t offset, |
218 | unsigned long firstBlock) | |
f0241068 | 219 | { |
8b0a0cc8 YB |
220 | int i = firstBlock; |
221 | int page_size = getpagesize(); | |
222 | unsigned int header_map_size = PAGE_ALIGN(ltt_subbuffer_header_size()); | |
f0241068 | 223 | |
8b0a0cc8 | 224 | g_assert(tf->buf_index->len == i); |
f0241068 MD |
225 | while (offset < tf->file_size) { |
226 | ltt_subbuffer_header_t *header; | |
227 | uint64_t *off; | |
8b0a0cc8 | 228 | uint64_t size; |
f0241068 MD |
229 | |
230 | /* map block header */ | |
231 | header = mmap(0, header_map_size, PROT_READ, | |
93556665 | 232 | MAP_PRIVATE, tf->fd, (off_t)offset); |
f0241068 MD |
233 | if(header == MAP_FAILED) { |
234 | perror("Error in allocating memory for buffer of tracefile"); | |
235 | return -1; | |
236 | } | |
237 | ||
238 | /* read len, offset += len */ | |
8b0a0cc8 | 239 | size = ltt_get_uint32(LTT_GET_BO(tf), &header->sb_size); |
f0241068 | 240 | |
8b0a0cc8 YB |
241 | /* Only index completly writen blocks */ |
242 | if (offset + size <= tf->file_size) { | |
243 | ||
244 | tf->buf_index = g_array_set_size(tf->buf_index, i + 1); | |
245 | off = &g_array_index(tf->buf_index, uint64_t, i); | |
246 | *off = offset; | |
247 | ||
248 | /* Store current buffer end cycle as the last file timestamp */ | |
249 | /* TODO ybrosseau 2010-11-04: Might want to convert it to a LttTime */ | |
250 | tf->end_timestamp = ltt_get_uint64(LTT_GET_BO(tf), | |
251 | &header->cycle_count_end); | |
252 | ||
253 | ++i; | |
254 | } | |
255 | offset += size; | |
f0241068 MD |
256 | /* unmap block header */ |
257 | if(munmap(header, header_map_size)) { | |
258 | g_warning("unmap size : %u\n", header_map_size); | |
259 | perror("munmap error"); | |
260 | return -1; | |
261 | } | |
f0241068 MD |
262 | } |
263 | tf->num_blocks = i; | |
264 | ||
265 | return 0; | |
266 | } | |
b7576a11 | 267 | |
8b0a0cc8 YB |
268 | /* parse the new information from the file and reajust the number of blocks. |
269 | * | |
270 | * Return value : 0 success, -1 error | |
271 | */ | |
272 | int ltt_trace_continue_block_index(LttTracefile *tf) | |
273 | { | |
274 | int ret; | |
275 | uint64_t offset; | |
276 | uint32_t last_block_size; | |
277 | unsigned long i = tf->num_blocks; | |
278 | int page_size = getpagesize(); | |
279 | unsigned int header_map_size = PAGE_ALIGN(ltt_subbuffer_header_size()); | |
280 | ||
281 | get_block_offset_size(tf, tf->num_blocks-1, &offset, &last_block_size); | |
282 | ||
283 | ltt_subbuffer_header_t *header_tmp = mmap(0, header_map_size, PROT_READ, | |
284 | MAP_PRIVATE, tf->fd, (off_t)offset); | |
285 | if(header_tmp == MAP_FAILED) { | |
286 | perror("Error in allocating memory for buffer of tracefile"); | |
287 | return -1; | |
288 | } | |
289 | ||
290 | /* read len, offset += len */ | |
291 | offset += ltt_get_uint32(LTT_GET_BO(tf), &header_tmp->sb_size); | |
292 | ||
293 | ret = ltt_trace_update_block_index(tf, offset, i); | |
294 | ||
295 | return ret; | |
296 | } | |
297 | ||
298 | int ltt_trace_create_block_index(LttTracefile *tf) | |
299 | { | |
300 | int ret; | |
301 | uint64_t offset = 0; | |
302 | unsigned long i = 0; | |
303 | ||
304 | tf->buf_index = g_array_sized_new(FALSE, TRUE, sizeof(uint64_t), | |
305 | DEFAULT_N_BLOCKS); | |
306 | if(!tf->buf_index) | |
307 | return -1; | |
308 | ret = ltt_trace_update_block_index(tf, offset, i); | |
309 | return ret; | |
310 | } | |
311 | ||
312 | /* | |
313 | Read file header and initialise buffer indexes | |
314 | ||
315 | Return value : 0 for success, -1 otherwise. | |
316 | */ | |
317 | static int ltt_tracefile_init(LttTracefile *tf) | |
318 | { | |
319 | ltt_subbuffer_header_t *header; | |
320 | int page_size = getpagesize(); | |
321 | ||
322 | /* Temporarily map the buffer start header to get trace information */ | |
323 | /* Multiple of pages aligned head */ | |
324 | tf->buffer.head = mmap(0, | |
325 | PAGE_ALIGN(ltt_subbuffer_header_size()), PROT_READ, | |
326 | MAP_PRIVATE, tf->fd, 0); | |
327 | if(tf->buffer.head == MAP_FAILED) { | |
328 | perror("Error in allocating memory for buffer of tracefile"); | |
329 | goto unmap_file; | |
330 | } | |
331 | g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned. | |
332 | ||
333 | header = (ltt_subbuffer_header_t *)tf->buffer.head; | |
334 | ||
335 | if(parse_trace_header(header, tf, tf->trace)) | |
336 | { | |
337 | g_warning("parse_trace_header error"); | |
338 | goto unmap_file; | |
339 | } | |
340 | ||
341 | if(munmap(tf->buffer.head, | |
342 | PAGE_ALIGN(ltt_subbuffer_header_size()))) | |
343 | { | |
344 | g_warning("unmap size : %zu\n", | |
345 | PAGE_ALIGN(ltt_subbuffer_header_size())); | |
346 | perror("munmap error"); | |
347 | g_assert(0); | |
348 | } | |
349 | tf->buffer.head = NULL; | |
350 | ||
351 | /* Create fields offset table */ | |
352 | tf->event.fields_offsets = g_array_sized_new(FALSE, FALSE, | |
353 | sizeof(struct LttField), 1); | |
354 | if (!tf->event.fields_offsets) { | |
355 | g_warning("Cannot create fields offset table"); | |
356 | goto unmap_file; | |
357 | } | |
358 | ||
359 | /* Create block index */ | |
360 | ltt_trace_create_block_index(tf); | |
361 | ||
362 | if(map_block(tf,0)) | |
363 | { | |
364 | perror("Cannot map block for tracefile"); | |
365 | goto unmap_file; | |
366 | } | |
367 | return 0; | |
368 | /* Error */ | |
369 | unmap_file: | |
370 | if(munmap(tf->buffer.head, | |
371 | PAGE_ALIGN(ltt_subbuffer_header_size()))) { | |
372 | g_warning("unmap size : %zu\n", | |
373 | PAGE_ALIGN(ltt_subbuffer_header_size())); | |
374 | perror("munmap error"); | |
375 | g_assert(0); | |
376 | } | |
377 | return -1; | |
378 | } | |
379 | ||
6cd62ccf | 380 | /***************************************************************************** |
381 | *Function name | |
963b5f2d | 382 | * ltt_tracefile_open : open a trace file, construct a LttTracefile |
6cd62ccf | 383 | *Input params |
963b5f2d | 384 | * t : the trace containing the tracefile |
385 | * fileName : path name of the trace file | |
3aee1200 | 386 | * tf : the tracefile structure |
6cd62ccf | 387 | *Return value |
3aee1200 | 388 | * : 0 for success, -1 otherwise. |
6cd62ccf | 389 | ****************************************************************************/ |
8655430b | 390 | static gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf) |
6cd62ccf | 391 | { |
963b5f2d | 392 | struct stat lTDFStat; /* Trace data file status */ |
6cd62ccf | 393 | |
394 | //open the file | |
d3d34f49 | 395 | tf->long_name = g_quark_from_string(fileName); |
963b5f2d | 396 | tf->trace = t; |
3865ea09 | 397 | tf->fd = open(fileName, O_RDONLY); |
f0241068 | 398 | tf->buf_index = NULL; |
8b0a0cc8 | 399 | tf->num_blocks = 0; |
6cd62ccf | 400 | if(tf->fd < 0){ |
2a74fbf4 | 401 | g_warning("Unable to open input data file %s\n", fileName); |
3aee1200 | 402 | goto end; |
6cd62ccf | 403 | } |
8b0a0cc8 | 404 | |
6cd62ccf | 405 | // Get the file's status |
406 | if(fstat(tf->fd, &lTDFStat) < 0){ | |
2a74fbf4 | 407 | g_warning("Unable to get the status of the input data file %s\n", fileName); |
3aee1200 | 408 | goto close_file; |
6cd62ccf | 409 | } |
6cd62ccf | 410 | //store the size of the file |
411 | tf->file_size = lTDFStat.st_size; | |
426f6149 | 412 | tf->events_lost = 0; |
413 | tf->subbuf_corrupt = 0; | |
3aee1200 | 414 | tf->buffer.head = NULL; |
8b0a0cc8 YB |
415 | tf->event.fields_offsets = NULL; |
416 | ||
417 | /* Is the file large enough to contain a trace */ | |
418 | if(lTDFStat.st_size < (off_t)(ltt_subbuffer_header_size())){ | |
419 | if (t->is_live) { | |
420 | /* It a live trace so the file can be empty at the start of the analysis */ | |
421 | goto end; | |
422 | } else { | |
423 | g_print("The input data file %s does not contain a trace\n", fileName); | |
424 | goto close_file; | |
425 | } | |
426 | } | |
427 | ||
428 | if(ltt_tracefile_init(tf) < 0) { | |
429 | goto close_file; | |
3aee1200 | 430 | } |
1184dc37 | 431 | |
3aee1200 | 432 | return 0; |
6cd62ccf | 433 | |
3aee1200 | 434 | /* Error */ |
3aee1200 | 435 | close_file: |
3865ea09 | 436 | close(tf->fd); |
3aee1200 | 437 | end: |
f0241068 MD |
438 | if (tf->buf_index) |
439 | g_array_free(tf->buf_index, TRUE); | |
3aee1200 | 440 | return -1; |
6cd62ccf | 441 | } |
442 | ||
6cd62ccf | 443 | /***************************************************************************** |
444 | *Function name | |
963b5f2d | 445 | * ltt_tracefile_close: close a trace file, |
6cd62ccf | 446 | *Input params |
963b5f2d | 447 | * t : tracefile which will be closed |
6cd62ccf | 448 | ****************************************************************************/ |
449 | ||
8655430b | 450 | static void ltt_tracefile_close(LttTracefile *t) |
6cd62ccf | 451 | { |
f628823c | 452 | int page_size = getpagesize(); |
453 | ||
3aee1200 | 454 | if(t->buffer.head != NULL) |
f0241068 | 455 | if(munmap(t->buffer.head, PAGE_ALIGN(t->buffer.size))) { |
f628823c | 456 | g_warning("unmap size : %u\n", |
f0241068 | 457 | PAGE_ALIGN(t->buffer.size)); |
f628823c | 458 | perror("munmap error"); |
459 | g_assert(0); | |
460 | } | |
461 | ||
3865ea09 | 462 | close(t->fd); |
f0241068 MD |
463 | if (t->buf_index) |
464 | g_array_free(t->buf_index, TRUE); | |
8b0a0cc8 YB |
465 | if (t->event.fields_offsets) { |
466 | g_array_free(t->event.fields_offsets, TRUE); | |
467 | } | |
963b5f2d | 468 | } |
6cd62ccf | 469 | |
8d1e6362 | 470 | /**************************************************************************** |
471 | * get_absolute_pathname | |
803229fa | 472 | * |
8d1e6362 | 473 | * return the unique pathname in the system |
474 | * | |
ef35d837 | 475 | * MD : Fixed this function so it uses realpath, dealing well with |
476 | * forgotten cases (.. were not used correctly before). | |
803229fa | 477 | * |
963b5f2d | 478 | ****************************************************************************/ |
45e14832 | 479 | void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname) |
9f797243 | 480 | { |
9f797243 | 481 | abs_pathname[0] = '\0'; |
803229fa | 482 | |
2fc874ab | 483 | if (realpath(pathname, abs_pathname) != NULL) |
ef35d837 | 484 | return; |
485 | else | |
486 | { | |
8d1e6362 | 487 | /* error, return the original path unmodified */ |
ef35d837 | 488 | strcpy(abs_pathname, pathname); |
9f797243 | 489 | return; |
490 | } | |
ef35d837 | 491 | return; |
9f797243 | 492 | } |
493 | ||
3aee1200 | 494 | /* Search for something like : .*_.* |
495 | * | |
496 | * The left side is the name, the right side is the number. | |
750eb11a | 497 | * Exclude leading /. |
4ad053df | 498 | * Exclude flight- prefix. |
3aee1200 | 499 | */ |
500 | ||
8655430b | 501 | static int get_tracefile_name_number(gchar *raw_name, |
3aee1200 | 502 | GQuark *name, |
ae3d0f50 | 503 | guint *num, |
f64fedd7 | 504 | gulong *tid, |
505 | gulong *pgid, | |
62e4e7bf | 506 | guint64 *creation) |
6cd62ccf | 507 | { |
3aee1200 | 508 | guint raw_name_len = strlen(raw_name); |
509 | gchar char_name[PATH_MAX]; | |
8af0138e | 510 | int i; |
3aee1200 | 511 | int underscore_pos; |
512 | long int cpu_num; | |
513 | gchar *endptr; | |
62e4e7bf | 514 | gchar *tmpptr; |
3aee1200 | 515 | |
750eb11a | 516 | /* skip leading / */ |
517 | for(i = 0; i < raw_name_len-1;i++) { | |
518 | if(raw_name[i] != '/') | |
519 | break; | |
520 | } | |
521 | raw_name = &raw_name[i]; | |
522 | raw_name_len = strlen(raw_name); | |
523 | ||
3aee1200 | 524 | for(i=raw_name_len-1;i>=0;i--) { |
525 | if(raw_name[i] == '_') break; | |
526 | } | |
ae3d0f50 | 527 | if(i==-1) { /* Either not found or name length is 0 */ |
62e4e7bf | 528 | /* This is a userspace tracefile */ |
529 | strncpy(char_name, raw_name, raw_name_len); | |
530 | char_name[raw_name_len] = '\0'; | |
531 | *name = g_quark_from_string(char_name); | |
532 | *num = 0; /* unknown cpu */ | |
533 | for(i=0;i<raw_name_len;i++) { | |
534 | if(raw_name[i] == '/') { | |
535 | break; | |
536 | } | |
537 | } | |
538 | i++; | |
539 | for(;i<raw_name_len;i++) { | |
540 | if(raw_name[i] == '/') { | |
541 | break; | |
542 | } | |
543 | } | |
544 | i++; | |
545 | for(;i<raw_name_len;i++) { | |
546 | if(raw_name[i] == '-') { | |
547 | break; | |
548 | } | |
549 | } | |
550 | if(i == raw_name_len) return -1; | |
551 | i++; | |
552 | tmpptr = &raw_name[i]; | |
553 | for(;i<raw_name_len;i++) { | |
554 | if(raw_name[i] == '.') { | |
555 | raw_name[i] = ' '; | |
556 | break; | |
557 | } | |
558 | } | |
559 | *tid = strtoul(tmpptr, &endptr, 10); | |
560 | if(endptr == tmpptr) | |
561 | return -1; /* No digit */ | |
562 | if(*tid == ULONG_MAX) | |
563 | return -1; /* underflow / overflow */ | |
564 | i++; | |
565 | tmpptr = &raw_name[i]; | |
566 | for(;i<raw_name_len;i++) { | |
567 | if(raw_name[i] == '.') { | |
568 | raw_name[i] = ' '; | |
569 | break; | |
570 | } | |
571 | } | |
572 | *pgid = strtoul(tmpptr, &endptr, 10); | |
573 | if(endptr == tmpptr) | |
574 | return -1; /* No digit */ | |
575 | if(*pgid == ULONG_MAX) | |
576 | return -1; /* underflow / overflow */ | |
577 | i++; | |
578 | tmpptr = &raw_name[i]; | |
579 | *creation = strtoull(tmpptr, &endptr, 10); | |
580 | if(endptr == tmpptr) | |
581 | return -1; /* No digit */ | |
582 | if(*creation == G_MAXUINT64) | |
583 | return -1; /* underflow / overflow */ | |
584 | } else { | |
585 | underscore_pos = i; | |
586 | ||
587 | cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10); | |
588 | ||
589 | if(endptr == raw_name+underscore_pos+1) | |
590 | return -1; /* No digit */ | |
591 | if(cpu_num == LONG_MIN || cpu_num == LONG_MAX) | |
592 | return -1; /* underflow / overflow */ | |
593 | ||
4ad053df | 594 | if (!strncmp(raw_name, "flight-", sizeof("flight-") - 1)) { |
595 | raw_name += sizeof("flight-") - 1; | |
596 | underscore_pos -= sizeof("flight-") - 1; | |
597 | } | |
62e4e7bf | 598 | strncpy(char_name, raw_name, underscore_pos); |
599 | char_name[underscore_pos] = '\0'; | |
62e4e7bf | 600 | *name = g_quark_from_string(char_name); |
601 | *num = cpu_num; | |
602 | } | |
69bd59ed | 603 | |
b333a76b | 604 | |
3aee1200 | 605 | return 0; |
606 | } | |
963b5f2d | 607 | |
3aee1200 | 608 | |
3865ea09 | 609 | GData **ltt_trace_get_tracefiles_groups(LttTrace *trace) |
77175651 | 610 | { |
3865ea09 | 611 | return &trace->tracefiles; |
77175651 | 612 | } |
613 | ||
614 | ||
3865ea09 | 615 | void compute_tracefile_group(GQuark key_id, |
616 | GArray *group, | |
617 | struct compute_tracefile_group_args *args) | |
77175651 | 618 | { |
43ed82b5 | 619 | unsigned int i; |
77175651 | 620 | LttTracefile *tf; |
621 | ||
622 | for(i=0; i<group->len; i++) { | |
623 | tf = &g_array_index (group, LttTracefile, i); | |
624 | if(tf->cpu_online) | |
3865ea09 | 625 | args->func(tf, args->func_args); |
77175651 | 626 | } |
627 | } | |
628 | ||
629 | ||
8655430b | 630 | static void ltt_tracefile_group_destroy(gpointer data) |
3aee1200 | 631 | { |
632 | GArray *group = (GArray *)data; | |
43ed82b5 | 633 | unsigned int i; |
3aee1200 | 634 | LttTracefile *tf; |
635 | ||
2e2088e1 MD |
636 | if (group->len > 0) |
637 | destroy_marker_data(g_array_index (group, LttTracefile, 0).mdata); | |
3aee1200 | 638 | for(i=0; i<group->len; i++) { |
639 | tf = &g_array_index (group, LttTracefile, i); | |
2e2088e1 | 640 | if(tf->cpu_online) |
3aee1200 | 641 | ltt_tracefile_close(tf); |
642 | } | |
643 | g_array_free(group, TRUE); | |
6cd62ccf | 644 | } |
645 | ||
43ed82b5 | 646 | static __attribute__ ((__unused__)) gboolean ltt_tracefile_group_has_cpu_online(gpointer data) |
49bf71b5 | 647 | { |
3aee1200 | 648 | GArray *group = (GArray *)data; |
43ed82b5 | 649 | unsigned int i; |
3aee1200 | 650 | LttTracefile *tf; |
651 | ||
652 | for(i=0; i<group->len; i++) { | |
653 | tf = &g_array_index (group, LttTracefile, i); | |
3c165eaf | 654 | if(tf->cpu_online) |
655 | return 1; | |
3aee1200 | 656 | } |
657 | return 0; | |
49bf71b5 | 658 | } |
659 | ||
660 | ||
3aee1200 | 661 | /* Open each tracefile under a specific directory. Put them in a |
662 | * GData : permits to access them using their tracefile group pathname. | |
663 | * i.e. access control/modules tracefile group by index : | |
664 | * "control/module". | |
3865ea09 | 665 | * |
666 | * relative path is the path relative to the trace root | |
667 | * root path is the full path | |
3aee1200 | 668 | * |
4a55f63e | 669 | * A tracefile group is simply an array where all the per cpu tracefiles sit. |
3aee1200 | 670 | */ |
671 | ||
8655430b | 672 | static int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_path) |
f7afe191 | 673 | { |
62e4e7bf | 674 | DIR *dir = opendir(root_path); |
675 | struct dirent *entry; | |
676 | struct stat stat_buf; | |
2e2088e1 MD |
677 | int ret, i; |
678 | struct marker_data *mdata; | |
3865ea09 | 679 | |
62e4e7bf | 680 | gchar path[PATH_MAX]; |
681 | int path_len; | |
682 | gchar *path_ptr; | |
3aee1200 | 683 | |
3865ea09 | 684 | int rel_path_len; |
69bd59ed | 685 | gchar rel_path[PATH_MAX]; |
686 | gchar *rel_path_ptr; | |
cb03932a | 687 | LttTracefile tmp_tf; |
3865ea09 | 688 | |
62e4e7bf | 689 | if(dir == NULL) { |
690 | perror(root_path); | |
691 | return ENOENT; | |
692 | } | |
3aee1200 | 693 | |
62e4e7bf | 694 | strncpy(path, root_path, PATH_MAX-1); |
695 | path_len = strlen(path); | |
696 | path[path_len] = '/'; | |
697 | path_len++; | |
698 | path_ptr = path + path_len; | |
3aee1200 | 699 | |
3865ea09 | 700 | strncpy(rel_path, relative_path, PATH_MAX-1); |
701 | rel_path_len = strlen(rel_path); | |
702 | rel_path[rel_path_len] = '/'; | |
703 | rel_path_len++; | |
704 | rel_path_ptr = rel_path + rel_path_len; | |
705 | ||
62e4e7bf | 706 | while((entry = readdir(dir)) != NULL) { |
62e4e7bf | 707 | if(entry->d_name[0] == '.') continue; |
708 | ||
709 | strncpy(path_ptr, entry->d_name, PATH_MAX - path_len); | |
710 | strncpy(rel_path_ptr, entry->d_name, PATH_MAX - rel_path_len); | |
711 | ||
712 | ret = stat(path, &stat_buf); | |
713 | if(ret == -1) { | |
714 | perror(path); | |
715 | continue; | |
716 | } | |
717 | ||
718 | g_debug("Tracefile file or directory : %s\n", path); | |
719 | ||
fe452434 | 720 | // if(strcmp(rel_path, "/eventdefs") == 0) continue; |
74a588bb | 721 | |
62e4e7bf | 722 | if(S_ISDIR(stat_buf.st_mode)) { |
3aee1200 | 723 | |
62e4e7bf | 724 | g_debug("Entering subdirectory...\n"); |
725 | ret = open_tracefiles(trace, path, rel_path); | |
726 | if(ret < 0) continue; | |
727 | } else if(S_ISREG(stat_buf.st_mode)) { | |
728 | GQuark name; | |
f64fedd7 | 729 | guint num; |
730 | gulong tid, pgid; | |
62e4e7bf | 731 | guint64 creation; |
3aee1200 | 732 | GArray *group; |
f64fedd7 | 733 | num = 0; |
734 | tid = pgid = 0; | |
62e4e7bf | 735 | creation = 0; |
ae3d0f50 | 736 | if(get_tracefile_name_number(rel_path, &name, &num, &tid, &pgid, &creation)) |
3aee1200 | 737 | continue; /* invalid name */ |
3865ea09 | 738 | |
62e4e7bf | 739 | g_debug("Opening file.\n"); |
cb03932a | 740 | if(ltt_tracefile_open(trace, path, &tmp_tf)) { |
8b0a0cc8 YB |
741 | /* Only consider the error for non live trace */ |
742 | if (!trace->is_live) { | |
743 | ||
744 | g_info("Error opening tracefile %s", path); | |
745 | continue; /* error opening the tracefile : bad magic number ? */ | |
746 | } | |
cb03932a | 747 | } |
748 | ||
3865ea09 | 749 | g_debug("Tracefile name is %s and number is %u", |
750 | g_quark_to_string(name), num); | |
750eb11a | 751 | |
2e2088e1 | 752 | mdata = NULL; |
cb03932a | 753 | tmp_tf.cpu_online = 1; |
754 | tmp_tf.cpu_num = num; | |
d3d34f49 | 755 | tmp_tf.name = name; |
62e4e7bf | 756 | tmp_tf.tid = tid; |
757 | tmp_tf.pgid = pgid; | |
758 | tmp_tf.creation = creation; | |
3865ea09 | 759 | group = g_datalist_id_get_data(&trace->tracefiles, name); |
3aee1200 | 760 | if(group == NULL) { |
761 | /* Elements are automatically cleared when the array is allocated. | |
762 | * It makes the cpu_online variable set to 0 : cpu offline, by default. | |
763 | */ | |
764 | group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10); | |
3865ea09 | 765 | g_datalist_id_set_data_full(&trace->tracefiles, name, |
3aee1200 | 766 | group, ltt_tracefile_group_destroy); |
2e2088e1 MD |
767 | mdata = allocate_marker_data(); |
768 | if (!mdata) | |
769 | g_error("Error in allocating marker data"); | |
3aee1200 | 770 | } |
cb03932a | 771 | |
3aee1200 | 772 | /* Add the per cpu tracefile to the named group */ |
773 | unsigned int old_len = group->len; | |
774 | if(num+1 > old_len) | |
775 | group = g_array_set_size(group, num+1); | |
750eb11a | 776 | |
2e2088e1 MD |
777 | g_assert(group->len > 0); |
778 | if (!mdata) | |
779 | mdata = g_array_index (group, LttTracefile, 0).mdata; | |
780 | ||
cb03932a | 781 | g_array_index (group, LttTracefile, num) = tmp_tf; |
afd57a3c | 782 | g_array_index (group, LttTracefile, num).event.tracefile = |
783 | &g_array_index (group, LttTracefile, num); | |
2e2088e1 MD |
784 | for (i = 0; i < group->len; i++) |
785 | g_array_index (group, LttTracefile, i).mdata = mdata; | |
62e4e7bf | 786 | } |
787 | } | |
788 | ||
789 | closedir(dir); | |
3aee1200 | 790 | |
62e4e7bf | 791 | return 0; |
f7afe191 | 792 | } |
793 | ||
3aee1200 | 794 | |
795 | /* Presumes the tracefile is already seeked at the beginning. It makes sense, | |
796 | * because it must be done just after the opening */ | |
2fc874ab | 797 | static int ltt_process_metadata_tracefile(LttTracefile *tf) |
3aee1200 | 798 | { |
799 | int err; | |
3aee1200 | 800 | |
801 | while(1) { | |
802 | err = ltt_tracefile_read_seek(tf); | |
803 | if(err == EPERM) goto seek_error; | |
804 | else if(err == ERANGE) break; /* End of tracefile */ | |
805 | ||
806 | err = ltt_tracefile_read_update_event(tf); | |
807 | if(err) goto update_error; | |
808 | ||
3aee1200 | 809 | /* The rules are : |
2fc874ab | 810 | * It contains only core events : |
811 | * 0 : set_marker_id | |
812 | * 1 : set_marker_format | |
3aee1200 | 813 | */ |
3c165eaf | 814 | if(tf->event.event_id >= MARKER_CORE_IDS) { |
2fc874ab | 815 | /* Should only contain core events */ |
816 | g_warning("Error in processing metadata file %s, " | |
3c165eaf | 817 | "should not contain event id %u.", g_quark_to_string(tf->name), |
818 | tf->event.event_id); | |
3aee1200 | 819 | err = EPERM; |
3c165eaf | 820 | goto event_id_error; |
d2083cab | 821 | } else { |
2e13d6af | 822 | char *pos; |
750eb11a | 823 | const char *channel_name, *marker_name, *format; |
3c165eaf | 824 | uint16_t id; |
825 | guint8 int_size, long_size, pointer_size, size_t_size, alignment; | |
3aee1200 | 826 | |
3c165eaf | 827 | switch((enum marker_id)tf->event.event_id) { |
828 | case MARKER_ID_SET_MARKER_ID: | |
750eb11a | 829 | channel_name = pos = tf->event.data; |
830 | pos += strlen(channel_name) + 1; | |
831 | marker_name = pos; | |
832 | g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s.%s", | |
833 | channel_name, marker_name); | |
2e13d6af | 834 | pos += strlen(marker_name) + 1; |
2fc874ab | 835 | pos += ltt_align((size_t)pos, sizeof(guint16), tf->alignment); |
3c165eaf | 836 | id = ltt_get_uint16(LTT_GET_BO(tf), pos); |
750eb11a | 837 | g_debug("In MARKER_ID_SET_MARKER_ID of marker %s.%s id %hu", |
838 | channel_name, marker_name, id); | |
3c165eaf | 839 | pos += sizeof(guint16); |
840 | int_size = *(guint8*)pos; | |
841 | pos += sizeof(guint8); | |
842 | long_size = *(guint8*)pos; | |
843 | pos += sizeof(guint8); | |
844 | pointer_size = *(guint8*)pos; | |
845 | pos += sizeof(guint8); | |
846 | size_t_size = *(guint8*)pos; | |
847 | pos += sizeof(guint8); | |
848 | alignment = *(guint8*)pos; | |
849 | pos += sizeof(guint8); | |
750eb11a | 850 | marker_id_event(tf->trace, |
851 | g_quark_from_string(channel_name), | |
852 | g_quark_from_string(marker_name), | |
3c165eaf | 853 | id, int_size, long_size, |
854 | pointer_size, size_t_size, alignment); | |
3aee1200 | 855 | break; |
3c165eaf | 856 | case MARKER_ID_SET_MARKER_FORMAT: |
750eb11a | 857 | channel_name = pos = tf->event.data; |
858 | pos += strlen(channel_name) + 1; | |
859 | marker_name = pos; | |
860 | g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s.%s", | |
861 | channel_name, marker_name); | |
2e13d6af | 862 | pos += strlen(marker_name) + 1; |
2e13d6af | 863 | format = pos; |
3c165eaf | 864 | pos += strlen(format) + 1; |
750eb11a | 865 | marker_format_event(tf->trace, |
866 | g_quark_from_string(channel_name), | |
867 | g_quark_from_string(marker_name), | |
3c165eaf | 868 | format); |
2fc874ab | 869 | /* get information from dictionary TODO */ |
d1bb700c | 870 | break; |
3aee1200 | 871 | default: |
2fc874ab | 872 | g_warning("Error in processing metadata file %s, " |
3c165eaf | 873 | "unknown event id %hhu.", |
3aee1200 | 874 | g_quark_to_string(tf->name), |
875 | tf->event.event_id); | |
876 | err = EPERM; | |
877 | goto event_id_error; | |
878 | } | |
879 | } | |
963b5f2d | 880 | } |
3aee1200 | 881 | return 0; |
963b5f2d | 882 | |
3aee1200 | 883 | /* Error handling */ |
3aee1200 | 884 | event_id_error: |
3aee1200 | 885 | update_error: |
886 | seek_error: | |
2fc874ab | 887 | g_warning("An error occured in metadata tracefile parsing"); |
3aee1200 | 888 | return err; |
6cd62ccf | 889 | } |
890 | ||
8b0a0cc8 YB |
891 | |
892 | LttTrace *ltt_trace_open(const gchar *pathname) | |
893 | { | |
894 | return _ltt_trace_open(pathname, FALSE); | |
895 | } | |
896 | ||
897 | LttTrace *ltt_trace_open_live(const gchar *pathname) | |
898 | { | |
899 | return _ltt_trace_open(pathname, TRUE); | |
900 | } | |
901 | ||
e95fe8f7 | 902 | /* |
903 | * Open a trace and return its LttTrace handle. | |
904 | * | |
905 | * pathname must be the directory of the trace | |
906 | */ | |
963b5f2d | 907 | |
8b0a0cc8 | 908 | static LttTrace *_ltt_trace_open(const gchar *pathname, gboolean is_live) |
3aee1200 | 909 | { |
910 | gchar abs_path[PATH_MAX]; | |
911 | LttTrace * t; | |
912 | LttTracefile *tf; | |
913 | GArray *group; | |
43ed82b5 | 914 | unsigned int i; |
915 | int ret; | |
64dd41a5 | 916 | ltt_subbuffer_header_t *header; |
62e4e7bf | 917 | DIR *dir; |
918 | struct dirent *entry; | |
62e4e7bf | 919 | struct stat stat_buf; |
b56dcdf2 | 920 | gchar path[PATH_MAX]; |
3aee1200 | 921 | |
922 | t = g_new(LttTrace, 1); | |
923 | if(!t) goto alloc_error; | |
924 | ||
925 | get_absolute_pathname(pathname, abs_path); | |
926 | t->pathname = g_quark_from_string(abs_path); | |
927 | ||
3aee1200 | 928 | g_datalist_init(&t->tracefiles); |
b56dcdf2 | 929 | |
930 | /* Test to see if it looks like a trace */ | |
62e4e7bf | 931 | dir = opendir(abs_path); |
932 | if(dir == NULL) { | |
933 | perror(abs_path); | |
934 | goto open_error; | |
935 | } | |
936 | while((entry = readdir(dir)) != NULL) { | |
b56dcdf2 | 937 | strcpy(path, abs_path); |
938 | strcat(path, "/"); | |
939 | strcat(path, entry->d_name); | |
62e4e7bf | 940 | ret = stat(path, &stat_buf); |
941 | if(ret == -1) { | |
942 | perror(path); | |
943 | continue; | |
944 | } | |
b56dcdf2 | 945 | } |
946 | closedir(dir); | |
8b0a0cc8 YB |
947 | |
948 | t->is_live = is_live; | |
949 | t->live_safe_timestamp = ltt_time_zero; | |
950 | ||
b56dcdf2 | 951 | /* Open all the tracefiles */ |
22660d97 | 952 | t->start_freq= 0; |
e45551ac | 953 | if(open_tracefiles(t, abs_path, "")) { |
954 | g_warning("Error opening tracefile %s", abs_path); | |
b56dcdf2 | 955 | goto find_error; |
e45551ac | 956 | } |
3aee1200 | 957 | |
750eb11a | 958 | /* Parse each trace metadata_N files : get runtime fac. info */ |
2fc874ab | 959 | group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_METADATA); |
3aee1200 | 960 | if(group == NULL) { |
8af0138e | 961 | g_warning("Trace %s has no metadata tracefile", abs_path); |
750eb11a | 962 | goto find_error; |
3aee1200 | 963 | } |
964 | ||
e0c7c400 | 965 | /* |
8b0a0cc8 YB |
966 | * Get the trace information for the first valid metadata tracefile. |
967 | * In live trace mode, the metadata_0 might be empty | |
e0c7c400 | 968 | * Getting a correct trace start_time and start_tsc is insured by the fact |
969 | * that no subbuffers are supposed to be lost in the metadata channel. | |
970 | * Therefore, the first subbuffer contains the start_tsc timestamp in its | |
971 | * buffer header. | |
972 | */ | |
16fcbb80 | 973 | g_assert(group->len > 0); |
8b0a0cc8 YB |
974 | header = NULL; |
975 | for(i=0; i<group->len; i++) { | |
976 | tf = &g_array_index (group, LttTracefile, i); | |
977 | header = (ltt_subbuffer_header_t *)tf->buffer.head; | |
978 | if (header) { | |
979 | break; | |
980 | } | |
981 | } | |
982 | if (header == NULL) { | |
983 | g_warning("Trace %s has not one valid metadata tracefile", abs_path); | |
984 | goto find_error; | |
985 | } | |
986 | ||
ac3b1c7d | 987 | ret = parse_trace_header(header, tf, t); |
988 | g_assert(!ret); | |
b56dcdf2 | 989 | |
990 | t->num_cpu = group->len; | |
9c9bf2d4 BP |
991 | t->drift = 1.; |
992 | t->offset = 0.; | |
16fcbb80 | 993 | |
750eb11a | 994 | //ret = allocate_marker_data(t); |
995 | //if (ret) | |
996 | // g_error("Error in allocating marker data"); | |
d79909d1 | 997 | |
3aee1200 | 998 | for(i=0; i<group->len; i++) { |
999 | tf = &g_array_index (group, LttTracefile, i); | |
8b0a0cc8 | 1000 | if (tf->cpu_online && tf->buffer.head ) |
2fc874ab | 1001 | if(ltt_process_metadata_tracefile(tf)) |
750eb11a | 1002 | goto find_error; |
1003 | // goto metadata_error; | |
3aee1200 | 1004 | } |
dd3a6d39 | 1005 | |
3aee1200 | 1006 | return t; |
1007 | ||
1008 | /* Error handling */ | |
750eb11a | 1009 | //metadata_error: |
1010 | // destroy_marker_data(t); | |
b56dcdf2 | 1011 | find_error: |
3aee1200 | 1012 | g_datalist_clear(&t->tracefiles); |
b56dcdf2 | 1013 | open_error: |
3aee1200 | 1014 | g_free(t); |
1015 | alloc_error: | |
1016 | return NULL; | |
1017 | ||
1018 | } | |
6cd62ccf | 1019 | |
8b0a0cc8 YB |
1020 | |
1021 | /***************************************************************************** | |
1022 | Update the informations concerning the tracefile | |
1023 | ||
1024 | Must be called periodically to update trace file and file size | |
1025 | information. | |
1026 | ||
1027 | Input params | |
1028 | tf : the tracefile | |
1029 | Return value | |
1030 | : Number of tracefile with available events | |
1031 | -1 on error. | |
1032 | ****************************************************************************/ | |
1033 | int ltt_trace_update(LttTrace *trace) | |
1034 | { | |
1035 | int trace_updated_count = 0; | |
1036 | ||
1037 | /* Only update live traces */ | |
1038 | if(trace->is_live) { | |
1039 | ||
1040 | /* Iterate on all tracefiles */ | |
1041 | g_datalist_foreach(&trace->tracefiles, | |
1042 | &update_tracefile_group, | |
1043 | &trace_updated_count); | |
1044 | ||
1045 | return trace_updated_count; | |
1046 | } | |
1047 | return 0; | |
1048 | ||
1049 | } | |
1050 | ||
1051 | static void update_tracefile_group(GQuark name, gpointer data, gpointer user_data) | |
1052 | { | |
1053 | int *trace_updated_count = (int *)user_data; | |
1054 | unsigned int i; | |
1055 | LttTracefile *tf; | |
1056 | GArray *group = (GArray *)data; | |
1057 | ||
1058 | g_debug("Updating tracefile group %s", g_quark_to_string(name)); | |
1059 | for(i=0; i<group->len; i++) { | |
1060 | tf = &g_array_index (group, LttTracefile, i); | |
1061 | ||
1062 | /* Update safe timestamp */ | |
1063 | tf->trace->live_safe_timestamp = | |
1064 | LTT_TIME_MAX(tf->trace->live_safe_timestamp, | |
1065 | ltt_interpolate_time_from_tsc(tf, | |
1066 | tf->end_timestamp)); | |
1067 | ||
1068 | /* Update tracefile */ | |
1069 | int ret = ltt_tracefile_update(tf); | |
1070 | if(ret < 0) { | |
1071 | g_warning("LIVE: Cannot update tracefile %s", | |
1072 | g_quark_to_string(ltt_tracefile_long_name(tf))); | |
1073 | *trace_updated_count = -1; | |
1074 | break; | |
1075 | } else { | |
1076 | *trace_updated_count += 1; | |
1077 | } | |
1078 | } | |
1079 | } | |
1080 | ||
1081 | ||
1082 | /***************************************************************************** | |
1083 | *Function name | |
1084 | * ltt_tracefile_update : Update the informations concerning a tracefile | |
1085 | * Must be called periodically to update trace file and file size | |
1086 | information. | |
1087 | *Input params | |
1088 | * tf : the tracefile | |
1089 | *Return value | |
1090 | * : 1 for success and an event is available | |
1091 | 0 for success but no event available, | |
1092 | * -1 on error. | |
1093 | ****************************************************************************/ | |
1094 | static int ltt_tracefile_update(LttTracefile *tf) | |
1095 | { | |
1096 | struct stat lTDFStat; /* Trace data file status */ | |
1097 | if(fstat(tf->fd, &lTDFStat) < 0){ | |
1098 | perror("error in getting the tracefile informations."); | |
1099 | } | |
1100 | ||
1101 | ||
1102 | /* Process the file only on size change */ | |
1103 | if(tf->file_size < lTDFStat.st_size) { | |
1104 | /* Update the size of the file */ | |
1105 | tf->file_size = lTDFStat.st_size; | |
1106 | g_debug("Updating tracefile %s", g_quark_to_string(tf->long_name)); | |
1107 | ||
1108 | if( tf->file_size >= (off_t)(ltt_subbuffer_header_size()) ) { | |
1109 | ||
1110 | if(tf->buf_index == NULL) { | |
1111 | if(ltt_tracefile_init(tf) < 0) { | |
1112 | return -1; | |
1113 | } | |
1114 | if(tf->name == LTT_TRACEFILE_NAME_METADATA) { | |
1115 | ||
1116 | LttTime start; | |
1117 | start.tv_sec = 0; | |
1118 | start.tv_nsec = 0; | |
1119 | ltt_process_metadata_tracefile(tf); | |
1120 | ||
1121 | ltt_tracefile_seek_time(tf, start); | |
1122 | tf->event.offset = 0; | |
1123 | ||
1124 | } | |
1125 | } | |
1126 | else | |
1127 | { | |
1128 | /* Retrieve the new subbuffers and index them */ | |
1129 | ltt_trace_continue_block_index(tf); | |
1130 | ||
1131 | if(tf->name == LTT_TRACEFILE_NAME_METADATA) { | |
1132 | LttEventPosition *pos = ltt_event_position_new(); | |
1133 | ltt_tracefile_get_current_position(tf, pos); | |
1134 | ltt_process_metadata_tracefile(tf); | |
1135 | ltt_tracefile_seek_position(tf, pos); | |
1136 | g_free(pos); | |
1137 | ||
1138 | } | |
1139 | } | |
1140 | ||
1141 | return 1; | |
1142 | } | |
1143 | } | |
1144 | ||
1145 | return 0; | |
1146 | ||
1147 | } | |
1148 | ||
e95fe8f7 | 1149 | /* Open another, completely independant, instance of a trace. |
1150 | * | |
1151 | * A read on this new instance will read the first event of the trace. | |
1152 | * | |
3aee1200 | 1153 | * When we copy a trace, we want all the opening actions to happen again : |
1154 | * the trace will be reopened and totally independant from the original. | |
1155 | * That's why we call ltt_trace_open. | |
e95fe8f7 | 1156 | */ |
3aee1200 | 1157 | LttTrace *ltt_trace_copy(LttTrace *self) |
963b5f2d | 1158 | { |
8b0a0cc8 | 1159 | return _ltt_trace_open(g_quark_to_string(self->pathname), self->is_live); |
963b5f2d | 1160 | } |
1161 | ||
e95fe8f7 | 1162 | /* |
1163 | * Close a trace | |
1164 | */ | |
1165 | ||
3aee1200 | 1166 | void ltt_trace_close(LttTrace *t) |
6cd62ccf | 1167 | { |
3aee1200 | 1168 | g_datalist_clear(&t->tracefiles); |
1169 | g_free(t); | |
6cd62ccf | 1170 | } |
1171 | ||
3aee1200 | 1172 | |
6cd62ccf | 1173 | /***************************************************************************** |
3aee1200 | 1174 | * Get the start time and end time of the trace |
6cd62ccf | 1175 | ****************************************************************************/ |
1176 | ||
3c165eaf | 1177 | void ltt_tracefile_time_span_get(LttTracefile *tf, |
3aee1200 | 1178 | LttTime *start, LttTime *end) |
6cd62ccf | 1179 | { |
3aee1200 | 1180 | int err; |
8b0a0cc8 | 1181 | |
6cd62ccf | 1182 | |
3aee1200 | 1183 | err = map_block(tf, 0); |
8b0a0cc8 YB |
1184 | /* Only empty live traces will return ERANGE */ |
1185 | if(err == ERANGE) { | |
1186 | *start = ltt_time_infinite; | |
1187 | *end = ltt_time_zero; | |
1188 | return; | |
1189 | } else if(unlikely(err)) { | |
3aee1200 | 1190 | g_error("Can not map block"); |
1191 | *start = ltt_time_infinite; | |
1192 | } else | |
1193 | *start = tf->buffer.begin.timestamp; | |
1194 | ||
1195 | err = map_block(tf, tf->num_blocks - 1); /* Last block */ | |
1196 | if(unlikely(err)) { | |
1197 | g_error("Can not map block"); | |
1198 | *end = ltt_time_zero; | |
1199 | } else | |
1200 | *end = tf->buffer.end.timestamp; | |
3729b611 PMF |
1201 | |
1202 | g_assert(end->tv_sec <= G_MAXUINT); | |
6cd62ccf | 1203 | } |
1204 | ||
3aee1200 | 1205 | struct tracefile_time_span_get_args { |
1206 | LttTrace *t; | |
1207 | LttTime *start; | |
1208 | LttTime *end; | |
1209 | }; | |
963b5f2d | 1210 | |
8655430b | 1211 | static void group_time_span_get(GQuark name, gpointer data, gpointer user_data) |
963b5f2d | 1212 | { |
3aee1200 | 1213 | struct tracefile_time_span_get_args *args = |
1214 | (struct tracefile_time_span_get_args*)user_data; | |
1215 | ||
1216 | GArray *group = (GArray *)data; | |
43ed82b5 | 1217 | unsigned int i; |
3aee1200 | 1218 | LttTracefile *tf; |
1219 | LttTime tmp_start; | |
1220 | LttTime tmp_end; | |
1221 | ||
1222 | for(i=0; i<group->len; i++) { | |
1223 | tf = &g_array_index (group, LttTracefile, i); | |
1224 | if(tf->cpu_online) { | |
1225 | ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end); | |
1226 | if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start; | |
1227 | if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end; | |
1228 | } | |
1229 | } | |
6cd62ccf | 1230 | } |
1231 | ||
8655430b | 1232 | /* return the start and end time of a trace */ |
1233 | ||
487ad181 | 1234 | void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end) |
1235 | { | |
3aee1200 | 1236 | LttTime min_start = ltt_time_infinite; |
1237 | LttTime max_end = ltt_time_zero; | |
1238 | struct tracefile_time_span_get_args args = { t, &min_start, &max_end }; | |
487ad181 | 1239 | |
3aee1200 | 1240 | g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args); |
1241 | ||
1242 | if(start != NULL) *start = min_start; | |
1243 | if(end != NULL) *end = max_end; | |
1244 | ||
487ad181 | 1245 | } |
1246 | ||
1247 | ||
3aee1200 | 1248 | /* Seek to the first event in a tracefile that has a time equal or greater than |
1249 | * the time passed in parameter. | |
1250 | * | |
1251 | * If the time parameter is outside the tracefile time span, seek to the first | |
27304273 | 1252 | * event or if after, return ERANGE. |
3aee1200 | 1253 | * |
1254 | * If the time parameter is before the first event, we have to seek specially to | |
1255 | * there. | |
1256 | * | |
27304273 | 1257 | * If the time is after the end of the trace, return ERANGE. |
3aee1200 | 1258 | * |
1259 | * Do a binary search to find the right block, then a sequential search in the | |
1260 | * block to find the event. | |
1261 | * | |
1262 | * In the special case where the time requested fits inside a block that has no | |
1263 | * event corresponding to the requested time, the first event of the next block | |
1264 | * will be seeked. | |
1265 | * | |
1266 | * IMPORTANT NOTE : // FIXME everywhere... | |
1267 | * | |
1268 | * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time : | |
1269 | * you will jump over an event if you do. | |
1270 | * | |
1271 | * Return value : 0 : no error, the tf->event can be used | |
27304273 | 1272 | * ERANGE : time if after the last event of the trace |
3aee1200 | 1273 | * otherwise : this is an error. |
1274 | * | |
1275 | * */ | |
1276 | ||
1277 | int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time) | |
1278 | { | |
1279 | int ret = 0; | |
1280 | int err; | |
1281 | unsigned int block_num, high, low; | |
1282 | ||
1283 | /* seek at the beginning of trace */ | |
1284 | err = map_block(tf, 0); /* First block */ | |
8b0a0cc8 YB |
1285 | if(unlikely(err == ERANGE)) { |
1286 | goto range; | |
1287 | } else if(unlikely(err)) { | |
3aee1200 | 1288 | g_error("Can not map block"); |
1289 | goto fail; | |
caf7a67a | 1290 | } |
1291 | ||
3aee1200 | 1292 | /* If the time is lower or equal the beginning of the trace, |
1293 | * go to the first event. */ | |
1294 | if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) { | |
1295 | ret = ltt_tracefile_read(tf); | |
27304273 | 1296 | if(ret == ERANGE) goto range; |
1297 | else if (ret) goto fail; | |
3aee1200 | 1298 | goto found; /* There is either no event in the trace or the event points |
1299 | to the first event in the trace */ | |
1300 | } | |
caf7a67a | 1301 | |
3aee1200 | 1302 | err = map_block(tf, tf->num_blocks - 1); /* Last block */ |
1303 | if(unlikely(err)) { | |
1304 | g_error("Can not map block"); | |
1305 | goto fail; | |
caf7a67a | 1306 | } |
6cd62ccf | 1307 | |
27304273 | 1308 | /* If the time is after the end of the trace, return ERANGE. */ |
1309 | if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) { | |
1310 | goto range; | |
3aee1200 | 1311 | } |
62e55dd6 | 1312 | |
3aee1200 | 1313 | /* Binary search the block */ |
1314 | high = tf->num_blocks - 1; | |
1315 | low = 0; | |
1316 | ||
1317 | while(1) { | |
1318 | block_num = ((high-low) / 2) + low; | |
1319 | ||
1320 | err = map_block(tf, block_num); | |
1321 | if(unlikely(err)) { | |
1322 | g_error("Can not map block"); | |
1323 | goto fail; | |
db55eaae | 1324 | } |
3aee1200 | 1325 | if(high == low) { |
1326 | /* We cannot divide anymore : this is what would happen if the time | |
1327 | * requested was exactly between two consecutive buffers'end and start | |
1328 | * timestamps. This is also what would happend if we didn't deal with out | |
1329 | * of span cases prior in this function. */ | |
1330 | /* The event is right in the buffer! | |
1331 | * (or in the next buffer first event) */ | |
1332 | while(1) { | |
1333 | ret = ltt_tracefile_read(tf); | |
27304273 | 1334 | if(ret == ERANGE) goto range; /* ERANGE or EPERM */ |
3aee1200 | 1335 | else if(ret) goto fail; |
1336 | ||
d9e13a0f | 1337 | if(ltt_time_compare(time, tf->event.event_time) <= 0) |
e45551ac | 1338 | goto found; |
3aee1200 | 1339 | } |
1340 | ||
27304273 | 1341 | } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) { |
caa44fe6 MD |
1342 | /* |
1343 | * Go to lower part. We don't want block_num - 1 since block_num | |
1344 | * can equal low , in which case high < low. | |
1345 | */ | |
1346 | high = block_num; | |
3aee1200 | 1347 | } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) { |
1348 | /* go to higher part */ | |
b1369bef | 1349 | low = block_num + 1; |
3aee1200 | 1350 | } else {/* The event is right in the buffer! |
1351 | (or in the next buffer first event) */ | |
1352 | while(1) { | |
27304273 | 1353 | ret = ltt_tracefile_read(tf); |
1354 | if(ret == ERANGE) goto range; /* ERANGE or EPERM */ | |
3aee1200 | 1355 | else if(ret) goto fail; |
1356 | ||
d9e13a0f | 1357 | if(ltt_time_compare(time, tf->event.event_time) <= 0) |
3aee1200 | 1358 | break; |
6cd62ccf | 1359 | } |
3aee1200 | 1360 | goto found; |
6cd62ccf | 1361 | } |
6cd62ccf | 1362 | } |
3aee1200 | 1363 | |
1364 | found: | |
1365 | return 0; | |
27304273 | 1366 | range: |
1367 | return ERANGE; | |
3aee1200 | 1368 | |
1369 | /* Error handling */ | |
1370 | fail: | |
1371 | g_error("ltt_tracefile_seek_time failed on tracefile %s", | |
1372 | g_quark_to_string(tf->name)); | |
1373 | return EPERM; | |
6cd62ccf | 1374 | } |
1375 | ||
8b0a0cc8 YB |
1376 | |
1377 | /* Save the current tracefile position in the passed position pointer */ | |
1378 | int ltt_tracefile_get_current_position(const LttTracefile *tf, LttEventPosition *ep) | |
1379 | { | |
1380 | /* TODO ybrosseau 2011-06-07: Maybe add some error checking | |
1381 | (ex: check the validity of the arguments pointer) */ | |
1382 | ltt_event_position(&(tf->event), ep); | |
1383 | return 0; | |
1384 | } | |
1385 | ||
1386 | ||
e95fe8f7 | 1387 | /* Seek to a position indicated by an LttEventPosition |
1388 | */ | |
80da81ad | 1389 | |
8655430b | 1390 | int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep) |
1391 | { | |
3aee1200 | 1392 | int err; |
1393 | ||
1394 | if(ep->tracefile != tf) { | |
1395 | goto fail; | |
80da81ad | 1396 | } |
1397 | ||
3aee1200 | 1398 | err = map_block(tf, ep->block); |
1399 | if(unlikely(err)) { | |
1400 | g_error("Can not map block"); | |
1401 | goto fail; | |
1402 | } | |
1403 | ||
1404 | tf->event.offset = ep->offset; | |
18206708 | 1405 | |
62e4e7bf | 1406 | /* Put back the event real tsc */ |
1407 | tf->event.tsc = ep->tsc; | |
1408 | tf->buffer.tsc = ep->tsc; | |
78f79181 | 1409 | |
3aee1200 | 1410 | err = ltt_tracefile_read_update_event(tf); |
1411 | if(err) goto fail; | |
73faf25a | 1412 | |
1413 | /* deactivate this, as it does nothing for now | |
3aee1200 | 1414 | err = ltt_tracefile_read_op(tf); |
1415 | if(err) goto fail; | |
73faf25a | 1416 | */ |
80da81ad | 1417 | |
a0c1f622 | 1418 | return 0; |
3aee1200 | 1419 | |
1420 | fail: | |
1421 | g_error("ltt_tracefile_seek_time failed on tracefile %s", | |
1422 | g_quark_to_string(tf->name)); | |
a0c1f622 | 1423 | return 1; |
3aee1200 | 1424 | } |
1425 | ||
9c9bf2d4 BP |
1426 | /* |
1427 | * Convert a value in "TSC scale" to a value in nanoseconds | |
1428 | */ | |
1429 | guint64 tsc_to_uint64(guint32 freq_scale, uint64_t start_freq, guint64 tsc) | |
1430 | { | |
1431 | return (double) tsc * NANOSECONDS_PER_SECOND * freq_scale / start_freq; | |
1432 | } | |
1433 | ||
e95fe8f7 | 1434 | /* Given a TSC value, return the LttTime (seconds,nanoseconds) it |
1435 | * corresponds to. | |
1436 | */ | |
ae3d0f50 | 1437 | LttTime ltt_interpolate_time_from_tsc(LttTracefile *tf, guint64 tsc) |
3aee1200 | 1438 | { |
9c9bf2d4 BP |
1439 | return ltt_time_from_uint64(tsc_to_uint64(tf->trace->freq_scale, |
1440 | tf->trace->start_freq, tf->trace->drift * tsc + | |
1441 | tf->trace->offset)); | |
80da81ad | 1442 | } |
1443 | ||
ae3d0f50 | 1444 | /* Calculate the real event time based on the buffer boundaries */ |
1445 | LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event) | |
1446 | { | |
62e4e7bf | 1447 | return ltt_interpolate_time_from_tsc(tf, tf->buffer.tsc); |
ae3d0f50 | 1448 | } |
1449 | ||
eed2ef37 | 1450 | |
1451 | /* Get the current event of the tracefile : valid until the next read */ | |
1452 | LttEvent *ltt_tracefile_get_event(LttTracefile *tf) | |
1453 | { | |
1454 | return &tf->event; | |
1455 | } | |
1456 | ||
6cd62ccf | 1457 | /***************************************************************************** |
1458 | *Function name | |
3aee1200 | 1459 | * ltt_tracefile_read : Read the next event in the tracefile |
6cd62ccf | 1460 | *Input params |
1461 | * t : tracefile | |
1462 | *Return value | |
3aee1200 | 1463 | * |
1464 | * Returns 0 if an event can be used in tf->event. | |
d822520b | 1465 | * Returns ERANGE on end of trace. The event in tf->event still can be used |
1466 | * (if the last block was not empty). | |
3aee1200 | 1467 | * Returns EPERM on error. |
1468 | * | |
1469 | * This function does make the tracefile event structure point to the event | |
1470 | * currently pointed to by the tf->event. | |
1471 | * | |
1472 | * Note : you must call a ltt_tracefile_seek to the beginning of the trace to | |
1473 | * reinitialize it after an error if you want results to be coherent. | |
1474 | * It would be the case if a end of trace last buffer has no event : the end | |
1475 | * of trace wouldn't be returned, but an error. | |
1476 | * We make the assumption there is at least one event per buffer. | |
6cd62ccf | 1477 | ****************************************************************************/ |
1478 | ||
3aee1200 | 1479 | int ltt_tracefile_read(LttTracefile *tf) |
6cd62ccf | 1480 | { |
963b5f2d | 1481 | int err; |
6cd62ccf | 1482 | |
3aee1200 | 1483 | err = ltt_tracefile_read_seek(tf); |
1484 | if(err) return err; | |
1485 | err = ltt_tracefile_read_update_event(tf); | |
1486 | if(err) return err; | |
73faf25a | 1487 | |
1488 | /* deactivate this, as it does nothing for now | |
3aee1200 | 1489 | err = ltt_tracefile_read_op(tf); |
1490 | if(err) return err; | |
73faf25a | 1491 | */ |
3aee1200 | 1492 | |
1493 | return 0; | |
1494 | } | |
1495 | ||
1496 | int ltt_tracefile_read_seek(LttTracefile *tf) | |
1497 | { | |
1498 | int err; | |
1499 | ||
1500 | /* Get next buffer until we finally have an event, or end of trace */ | |
1501 | while(1) { | |
1502 | err = ltt_seek_next_event(tf); | |
1503 | if(unlikely(err == ENOPROTOOPT)) { | |
1504 | return EPERM; | |
bdc36259 | 1505 | } |
bdc36259 | 1506 | |
3aee1200 | 1507 | /* Are we at the end of the buffer ? */ |
1508 | if(err == ERANGE) { | |
1509 | if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */ | |
1510 | return ERANGE; | |
1511 | } else { | |
1512 | /* get next block */ | |
1513 | err = map_block(tf, tf->buffer.index + 1); | |
1514 | if(unlikely(err)) { | |
1515 | g_error("Can not map block"); | |
1516 | return EPERM; | |
1517 | } | |
1518 | } | |
1519 | } else break; /* We found an event ! */ | |
1520 | } | |
2dee981d | 1521 | |
3aee1200 | 1522 | return 0; |
1523 | } | |
18206708 | 1524 | |
e95fe8f7 | 1525 | /* do an operation when reading a new event */ |
18206708 | 1526 | |
73faf25a | 1527 | /* This function does nothing for now */ |
1528 | #if 0 | |
3aee1200 | 1529 | int ltt_tracefile_read_op(LttTracefile *tf) |
1530 | { | |
3aee1200 | 1531 | LttEvent *event; |
1532 | ||
1533 | event = &tf->event; | |
18206708 | 1534 | |
73faf25a | 1535 | /* do event specific operation */ |
1536 | ||
1537 | /* nothing */ | |
40331ba8 | 1538 | |
3aee1200 | 1539 | return 0; |
6cd62ccf | 1540 | } |
73faf25a | 1541 | #endif |
6cd62ccf | 1542 | |
91f8d488 | 1543 | static void print_debug_event_header(LttEvent *ev, void *start_pos, void *end_pos) |
1544 | { | |
1545 | unsigned int offset = 0; | |
1546 | int i, j; | |
1547 | ||
43ed82b5 | 1548 | g_printf("Event header (tracefile %s offset %" PRIx64 "):\n", |
afd57a3c | 1549 | g_quark_to_string(ev->tracefile->long_name), |
f0241068 MD |
1550 | (uint64_t)ev->tracefile->buffer.offset + |
1551 | (long)start_pos - (long)ev->tracefile->buffer.head); | |
91f8d488 | 1552 | |
1553 | while (offset < (long)end_pos - (long)start_pos) { | |
1554 | g_printf("%8lx", (long)start_pos - (long)ev->tracefile->buffer.head + offset); | |
1555 | g_printf(" "); | |
1556 | ||
1557 | for (i = 0; i < 4 ; i++) { | |
1558 | for (j = 0; j < 4; j++) { | |
1559 | if (offset + ((i * 4) + j) < | |
1560 | (long)end_pos - (long)start_pos) | |
1561 | g_printf("%02hhX", | |
d3353231 | 1562 | ((char*)start_pos)[offset + ((i * 4) + j)]); |
91f8d488 | 1563 | else |
1564 | g_printf(" "); | |
1565 | g_printf(" "); | |
1566 | } | |
1567 | if (i < 4) | |
1568 | g_printf(" "); | |
1569 | } | |
1570 | offset+=16; | |
1571 | g_printf("\n"); | |
1572 | } | |
1573 | } | |
1574 | ||
6cd62ccf | 1575 | |
3aee1200 | 1576 | /* same as ltt_tracefile_read, but does not seek to the next event nor call |
1577 | * event specific operation. */ | |
1578 | int ltt_tracefile_read_update_event(LttTracefile *tf) | |
6cd62ccf | 1579 | { |
3aee1200 | 1580 | void * pos; |
1581 | LttEvent *event; | |
91f8d488 | 1582 | void *pos_aligned; |
551bf485 | 1583 | guint16 packed_evid; /* event id reader from the 5 bits in header */ |
3aee1200 | 1584 | |
1585 | event = &tf->event; | |
eed2ef37 | 1586 | pos = tf->buffer.head + event->offset; |
3aee1200 | 1587 | |
1588 | /* Read event header */ | |
1589 | ||
62e4e7bf | 1590 | /* Align the head */ |
2fc874ab | 1591 | pos += ltt_align((size_t)pos, sizeof(guint32), tf->alignment); |
91f8d488 | 1592 | pos_aligned = pos; |
3aee1200 | 1593 | |
2fc874ab | 1594 | event->timestamp = ltt_get_uint32(LTT_GET_BO(tf), pos); |
551bf485 | 1595 | event->event_id = packed_evid = event->timestamp >> tf->tscbits; |
a9048165 | 1596 | event->timestamp = event->timestamp & tf->tsc_mask; |
2fc874ab | 1597 | pos += sizeof(guint32); |
1598 | ||
551bf485 | 1599 | switch (packed_evid) { |
2fc874ab | 1600 | case 29: /* LTT_RFLAG_ID_SIZE_TSC */ |
1601 | event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos); | |
1602 | pos += sizeof(guint16); | |
1603 | event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos); | |
1604 | pos += sizeof(guint16); | |
1605 | if (event->event_size == 0xFFFF) { | |
1606 | event->event_size = ltt_get_uint32(LTT_GET_BO(tf), pos); | |
1607 | pos += sizeof(guint32); | |
d1bb700c | 1608 | } |
2fc874ab | 1609 | pos += ltt_align((size_t)pos, sizeof(guint64), tf->alignment); |
1610 | tf->buffer.tsc = ltt_get_uint64(LTT_GET_BO(tf), pos); | |
62e4e7bf | 1611 | pos += sizeof(guint64); |
2fc874ab | 1612 | break; |
1613 | case 30: /* LTT_RFLAG_ID_SIZE */ | |
f439de06 | 1614 | event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos); |
3c165eaf | 1615 | pos += sizeof(guint16); |
d1bb700c | 1616 | event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos); |
1617 | pos += sizeof(guint16); | |
2fc874ab | 1618 | if (event->event_size == 0xFFFF) { |
1619 | event->event_size = ltt_get_uint32(LTT_GET_BO(tf), pos); | |
1620 | pos += sizeof(guint32); | |
1621 | } | |
1622 | break; | |
1623 | case 31: /* LTT_RFLAG_ID */ | |
1624 | event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos); | |
1625 | pos += sizeof(guint16); | |
1626 | event->event_size = G_MAXUINT; | |
1627 | break; | |
1628 | default: | |
1629 | event->event_size = G_MAXUINT; | |
1630 | break; | |
1631 | } | |
1632 | ||
551bf485 | 1633 | if (likely(packed_evid != 29)) { |
2fc874ab | 1634 | /* No extended timestamp */ |
1635 | if (event->timestamp < (tf->buffer.tsc & tf->tsc_mask)) | |
1636 | tf->buffer.tsc = ((tf->buffer.tsc & ~tf->tsc_mask) /* overflow */ | |
1637 | + tf->tsc_mask_next_bit) | |
1638 | | (guint64)event->timestamp; | |
1639 | else | |
1640 | tf->buffer.tsc = (tf->buffer.tsc & ~tf->tsc_mask) /* no overflow */ | |
1641 | | (guint64)event->timestamp; | |
d1bb700c | 1642 | } |
2fc874ab | 1643 | event->tsc = tf->buffer.tsc; |
1644 | ||
1645 | event->event_time = ltt_interpolate_time(tf, event); | |
91f8d488 | 1646 | |
1647 | if (a_event_debug) | |
1648 | print_debug_event_header(event, pos_aligned, pos); | |
1649 | ||
3aee1200 | 1650 | event->data = pos; |
1651 | ||
2fc874ab | 1652 | /* |
1653 | * Let ltt_update_event_size update event->data according to the largest | |
1654 | * alignment within the payload. | |
1655 | * Get the data size and update the event fields with the current | |
1656 | * information. */ | |
eed2ef37 | 1657 | ltt_update_event_size(tf); |
77175651 | 1658 | |
3aee1200 | 1659 | return 0; |
6cd62ccf | 1660 | } |
1661 | ||
507915ee | 1662 | |
6cd62ccf | 1663 | /**************************************************************************** |
1664 | *Function name | |
3aee1200 | 1665 | * map_block : map a block from the file |
6cd62ccf | 1666 | *Input Params |
1667 | * lttdes : ltt trace file | |
1668 | * whichBlock : the block which will be read | |
1669 | *return value | |
1670 | * 0 : success | |
1671 | * EINVAL : lseek fail | |
1672 | * EIO : can not read from the file | |
1673 | ****************************************************************************/ | |
1674 | ||
8655430b | 1675 | static gint map_block(LttTracefile * tf, guint block_num) |
6cd62ccf | 1676 | { |
b77d1b57 | 1677 | int page_size = getpagesize(); |
64dd41a5 | 1678 | ltt_subbuffer_header_t *header; |
f0241068 MD |
1679 | uint64_t offset; |
1680 | uint32_t size; | |
1681 | int ret; | |
3aee1200 | 1682 | |
8b0a0cc8 YB |
1683 | if(tf->num_blocks == 0) { |
1684 | errno = ERANGE; | |
1685 | return ERANGE; | |
1686 | } | |
1687 | ||
3aee1200 | 1688 | g_assert(block_num < tf->num_blocks); |
6cd62ccf | 1689 | |
f628823c | 1690 | if(tf->buffer.head != NULL) { |
f0241068 | 1691 | if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buffer.size))) { |
f628823c | 1692 | g_warning("unmap size : %u\n", |
f0241068 | 1693 | PAGE_ALIGN(tf->buffer.size)); |
f628823c | 1694 | perror("munmap error"); |
1695 | g_assert(0); | |
1696 | } | |
1697 | } | |
f0241068 MD |
1698 | |
1699 | ret = get_block_offset_size(tf, block_num, &offset, &size); | |
1700 | g_assert(!ret); | |
1701 | ||
93556665 MD |
1702 | g_debug("Map block %u, offset %llu, size %u\n", block_num, |
1703 | (unsigned long long)offset, (unsigned int)size); | |
1704 | ||
3aee1200 | 1705 | /* Multiple of pages aligned head */ |
f0241068 MD |
1706 | tf->buffer.head = mmap(0, (size_t)size, PROT_READ, MAP_PRIVATE, |
1707 | tf->fd, (off_t)offset); | |
3aee1200 | 1708 | |
3865ea09 | 1709 | if(tf->buffer.head == MAP_FAILED) { |
3aee1200 | 1710 | perror("Error in allocating memory for buffer of tracefile"); |
3865ea09 | 1711 | g_assert(0); |
3aee1200 | 1712 | goto map_error; |
6cd62ccf | 1713 | } |
f64fedd7 | 1714 | g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned. |
6cd62ccf | 1715 | |
3aee1200 | 1716 | tf->buffer.index = block_num; |
1717 | ||
64dd41a5 | 1718 | header = (ltt_subbuffer_header_t *)tf->buffer.head; |
3aee1200 | 1719 | |
3aee1200 | 1720 | tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf), |
64dd41a5 | 1721 | &header->cycle_count_begin); |
3aee1200 | 1722 | tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf), |
64dd41a5 | 1723 | &header->cycle_count_end); |
f0241068 MD |
1724 | tf->buffer.offset = offset; |
1725 | tf->buffer.size = ltt_get_uint32(LTT_GET_BO(tf), | |
1726 | &header->sb_size); | |
f0241068 MD |
1727 | tf->buffer.data_size = ltt_get_uint32(LTT_GET_BO(tf), |
1728 | &header->data_size); | |
3aee1200 | 1729 | tf->buffer.tsc = tf->buffer.begin.cycle_count; |
1730 | tf->event.tsc = tf->buffer.tsc; | |
986e2a7c | 1731 | tf->buffer.freq = tf->buffer.begin.freq; |
3aee1200 | 1732 | |
93556665 | 1733 | g_assert(size == tf->buffer.size); |
f0241068 MD |
1734 | g_assert(tf->buffer.data_size <= tf->buffer.size); |
1735 | ||
22660d97 BP |
1736 | if (tf->trace->start_freq) |
1737 | { | |
1738 | tf->buffer.begin.freq = tf->trace->start_freq; | |
1739 | tf->buffer.begin.timestamp = ltt_interpolate_time_from_tsc(tf, | |
1740 | tf->buffer.begin.cycle_count); | |
1741 | tf->buffer.end.freq = tf->trace->start_freq; | |
1742 | tf->buffer.end.timestamp = ltt_interpolate_time_from_tsc(tf, | |
1743 | tf->buffer.end.cycle_count); | |
1744 | } | |
1745 | ||
3aee1200 | 1746 | /* Make the current event point to the beginning of the buffer : |
1747 | * it means that the event read must get the first event. */ | |
1748 | tf->event.tracefile = tf; | |
1749 | tf->event.block = block_num; | |
eed2ef37 | 1750 | tf->event.offset = 0; |
3aee1200 | 1751 | |
a2bbf2e5 | 1752 | if (header->events_lost) { |
1753 | g_warning("%d events lost so far in tracefile %s at block %u", | |
0c2f4984 | 1754 | (guint)header->events_lost, |
a2bbf2e5 | 1755 | g_quark_to_string(tf->long_name), |
1756 | block_num); | |
426f6149 | 1757 | tf->events_lost = header->events_lost; |
1758 | } | |
a2bbf2e5 | 1759 | if (header->subbuf_corrupt) { |
1760 | g_warning("%d subbuffer(s) corrupted so far in tracefile %s at block %u", | |
0c2f4984 | 1761 | (guint)header->subbuf_corrupt, |
a2bbf2e5 | 1762 | g_quark_to_string(tf->long_name), |
1763 | block_num); | |
426f6149 | 1764 | tf->subbuf_corrupt = header->subbuf_corrupt; |
1765 | } | |
1766 | ||
3aee1200 | 1767 | return 0; |
6cd62ccf | 1768 | |
3aee1200 | 1769 | map_error: |
1770 | return -errno; | |
6cd62ccf | 1771 | } |
1772 | ||
91f8d488 | 1773 | static void print_debug_event_data(LttEvent *ev) |
1774 | { | |
1775 | unsigned int offset = 0; | |
1776 | int i, j; | |
1777 | ||
1778 | if (!max(ev->event_size, ev->data_size)) | |
1779 | return; | |
1780 | ||
43ed82b5 | 1781 | g_printf("Event data (tracefile %s offset %" PRIx64 "):\n", |
1782 | g_quark_to_string(ev->tracefile->long_name), | |
f0241068 | 1783 | (uint64_t)ev->tracefile->buffer.offset |
43ed82b5 | 1784 | + (long)ev->data - (long)ev->tracefile->buffer.head); |
91f8d488 | 1785 | |
1786 | while (offset < max(ev->event_size, ev->data_size)) { | |
1787 | g_printf("%8lx", (long)ev->data + offset | |
1788 | - (long)ev->tracefile->buffer.head); | |
1789 | g_printf(" "); | |
1790 | ||
1791 | for (i = 0; i < 4 ; i++) { | |
1792 | for (j = 0; j < 4; j++) { | |
1793 | if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size)) | |
1794 | g_printf("%02hhX", ((char*)ev->data)[offset + ((i * 4) + j)]); | |
1795 | else | |
1796 | g_printf(" "); | |
1797 | g_printf(" "); | |
1798 | } | |
1799 | if (i < 4) | |
1800 | g_printf(" "); | |
1801 | } | |
1802 | ||
1803 | g_printf(" "); | |
1804 | ||
1805 | for (i = 0; i < 4; i++) { | |
1806 | for (j = 0; j < 4; j++) { | |
1807 | if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size)) { | |
1808 | if (isprint(((char*)ev->data)[offset + ((i * 4) + j)])) | |
1809 | g_printf("%c", ((char*)ev->data)[offset + ((i * 4) + j)]); | |
1810 | else | |
1811 | g_printf("."); | |
1812 | } else | |
1813 | g_printf(" "); | |
1814 | } | |
1815 | } | |
1816 | offset+=16; | |
1817 | g_printf("\n"); | |
1818 | } | |
1819 | } | |
1820 | ||
77175651 | 1821 | /* It will update the fields offsets too */ |
1822 | void ltt_update_event_size(LttTracefile *tf) | |
6cd62ccf | 1823 | { |
2312de30 | 1824 | off_t size = 0; |
d2007fbd | 1825 | struct marker_info *info; |
750eb11a | 1826 | |
1827 | if (tf->name == LTT_TRACEFILE_NAME_METADATA) { | |
1828 | switch((enum marker_id)tf->event.event_id) { | |
1829 | case MARKER_ID_SET_MARKER_ID: | |
1830 | size = strlen((char*)tf->event.data) + 1; | |
1831 | g_debug("marker %s id set", (char*)tf->event.data + size); | |
1832 | size += strlen((char*)tf->event.data + size) + 1; | |
1833 | size += ltt_align(size, sizeof(guint16), tf->alignment); | |
1834 | size += sizeof(guint16); | |
1835 | size += sizeof(guint8); | |
1836 | size += sizeof(guint8); | |
1837 | size += sizeof(guint8); | |
1838 | size += sizeof(guint8); | |
1839 | size += sizeof(guint8); | |
1840 | break; | |
1841 | case MARKER_ID_SET_MARKER_FORMAT: | |
1842 | size = strlen((char*)tf->event.data) + 1; | |
1843 | g_debug("marker %s format set", (char*)tf->event.data); | |
1844 | size += strlen((char*)tf->event.data + size) + 1; | |
1845 | size += strlen((char*)tf->event.data + size) + 1; | |
1846 | break; | |
1847 | } | |
256a5b3a | 1848 | } |
1849 | ||
750eb11a | 1850 | info = marker_get_info_from_id(tf->mdata, tf->event.event_id); |
dcf96842 | 1851 | |
256a5b3a | 1852 | if (tf->event.event_id >= MARKER_CORE_IDS) |
1853 | g_assert(info != NULL); | |
1854 | ||
1855 | /* Do not update field offsets of core markers when initially reading the | |
2fc874ab | 1856 | * metadata tracefile when the infos about these markers do not exist yet. |
256a5b3a | 1857 | */ |
1858 | if (likely(info && info->fields)) { | |
2fc874ab | 1859 | /* alignment */ |
196085f2 | 1860 | tf->event.data += ltt_align((off_t)(unsigned long)tf->event.data, |
1861 | info->largest_align, | |
2fc874ab | 1862 | info->alignment); |
1863 | /* size, dynamically computed */ | |
256a5b3a | 1864 | if (info->size != -1) |
1865 | size = info->size; | |
1866 | else | |
1184dc37 MD |
1867 | size = marker_update_fields_offsets(info, tf->event.data); |
1868 | /* Update per-tracefile offsets */ | |
1869 | marker_update_event_fields_offsets(tf->event.fields_offsets, info); | |
44f317b7 | 1870 | } |
c4afd5d8 | 1871 | |
d2007fbd | 1872 | tf->event.data_size = size; |
1873 | ||
1874 | /* Check consistency between kernel and LTTV structure sizes */ | |
2fc874ab | 1875 | if(tf->event.event_size == G_MAXUINT) { |
d2007fbd | 1876 | /* Event size too big to fit in the event size field */ |
1877 | tf->event.event_size = tf->event.data_size; | |
1878 | } | |
91f8d488 | 1879 | |
1880 | if (a_event_debug) | |
1881 | print_debug_event_data(&tf->event); | |
1882 | ||
d2007fbd | 1883 | if (tf->event.data_size != tf->event.event_size) { |
750eb11a | 1884 | struct marker_info *info = marker_get_info_from_id(tf->mdata, |
3c165eaf | 1885 | tf->event.event_id); |
750eb11a | 1886 | if (!info) |
1887 | g_error("Undescribed event %hhu in channel %s", tf->event.event_id, | |
1888 | g_quark_to_string(tf->name)); | |
3c165eaf | 1889 | g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u", |
1890 | g_quark_to_string(info->name), | |
1891 | tf->event.event_size, tf->event.data_size); | |
d2007fbd | 1892 | exit(-1); |
1893 | } | |
6cd62ccf | 1894 | } |
1895 | ||
6cd62ccf | 1896 | |
2fc874ab | 1897 | /* Take the tf current event offset and use the event id to figure out where is |
1898 | * the next event offset. | |
3aee1200 | 1899 | * |
1900 | * This is an internal function not aiming at being used elsewhere : it will | |
1901 | * not jump over the current block limits. Please consider using | |
1902 | * ltt_tracefile_read to do this. | |
1903 | * | |
1904 | * Returns 0 on success | |
1905 | * ERANGE if we are at the end of the buffer. | |
1906 | * ENOPROTOOPT if an error occured when getting the current event size. | |
1907 | */ | |
8655430b | 1908 | static int ltt_seek_next_event(LttTracefile *tf) |
6cd62ccf | 1909 | { |
3aee1200 | 1910 | int ret = 0; |
1911 | void *pos; | |
3aee1200 | 1912 | |
1913 | /* seek over the buffer header if we are at the buffer start */ | |
eed2ef37 | 1914 | if(tf->event.offset == 0) { |
51551c6f | 1915 | tf->event.offset += tf->buffer_header_size; |
b77d1b57 | 1916 | |
f0241068 | 1917 | if(tf->event.offset == tf->buffer.data_size) { |
b77d1b57 | 1918 | ret = ERANGE; |
1919 | } | |
3aee1200 | 1920 | goto found; |
1921 | } | |
2fc874ab | 1922 | |
3aee1200 | 1923 | pos = tf->event.data; |
1924 | ||
77175651 | 1925 | if(tf->event.data_size < 0) goto error; |
3aee1200 | 1926 | |
77175651 | 1927 | pos += (size_t)tf->event.data_size; |
3aee1200 | 1928 | |
eed2ef37 | 1929 | tf->event.offset = pos - tf->buffer.head; |
8b0a0cc8 YB |
1930 | |
1931 | if(tf->event.offset >= tf->buffer.data_size) { | |
cb03932a | 1932 | ret = ERANGE; |
1933 | goto found; | |
1934 | } | |
f0241068 | 1935 | g_assert(tf->event.offset < tf->buffer.data_size); |
3aee1200 | 1936 | |
1937 | found: | |
1938 | return ret; | |
1939 | ||
1940 | error: | |
1941 | g_error("Error in ltt_seek_next_event for tracefile %s", | |
1942 | g_quark_to_string(tf->name)); | |
1943 | return ENOPROTOOPT; | |
6cd62ccf | 1944 | } |
1945 | ||
f104d082 | 1946 | |
6cd62ccf | 1947 | /***************************************************************************** |
1948 | *Function name | |
eed2ef37 | 1949 | * ltt_get_int : get an integer number |
6cd62ccf | 1950 | *Input params |
3aee1200 | 1951 | * reverse_byte_order: must we reverse the byte order ? |
6cd62ccf | 1952 | * size : the size of the integer |
3aee1200 | 1953 | * ptr : the data pointer |
6cd62ccf | 1954 | *Return value |
cf74a6f1 | 1955 | * gint64 : a 64 bits integer |
6cd62ccf | 1956 | ****************************************************************************/ |
1957 | ||
eed2ef37 | 1958 | gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data) |
6cd62ccf | 1959 | { |
3aee1200 | 1960 | gint64 val; |
cf74a6f1 | 1961 | |
1962 | switch(size) { | |
3aee1200 | 1963 | case 1: val = *((gint8*)data); break; |
1964 | case 2: val = ltt_get_int16(reverse_byte_order, data); break; | |
1965 | case 4: val = ltt_get_int32(reverse_byte_order, data); break; | |
1966 | case 8: val = ltt_get_int64(reverse_byte_order, data); break; | |
1967 | default: val = ltt_get_int64(reverse_byte_order, data); | |
1968 | g_critical("get_int : integer size %d unknown", size); | |
cf74a6f1 | 1969 | break; |
1970 | } | |
1971 | ||
3aee1200 | 1972 | return val; |
6cd62ccf | 1973 | } |
3aee1200 | 1974 | |
6cd62ccf | 1975 | /***************************************************************************** |
1976 | *Function name | |
eed2ef37 | 1977 | * ltt_get_uint : get an unsigned integer number |
6cd62ccf | 1978 | *Input params |
3aee1200 | 1979 | * reverse_byte_order: must we reverse the byte order ? |
1980 | * size : the size of the integer | |
1981 | * ptr : the data pointer | |
1982 | *Return value | |
1983 | * guint64 : a 64 bits unsigned integer | |
6cd62ccf | 1984 | ****************************************************************************/ |
1985 | ||
eed2ef37 | 1986 | guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data) |
6cd62ccf | 1987 | { |
3aee1200 | 1988 | guint64 val; |
1989 | ||
1990 | switch(size) { | |
1991 | case 1: val = *((gint8*)data); break; | |
1992 | case 2: val = ltt_get_uint16(reverse_byte_order, data); break; | |
1993 | case 4: val = ltt_get_uint32(reverse_byte_order, data); break; | |
1994 | case 8: val = ltt_get_uint64(reverse_byte_order, data); break; | |
1995 | default: val = ltt_get_uint64(reverse_byte_order, data); | |
1996 | g_critical("get_uint : unsigned integer size %d unknown", | |
1997 | size); | |
1998 | break; | |
1999 | } | |
2000 | ||
2001 | return val; | |
6cd62ccf | 2002 | } |
3aee1200 | 2003 | |
2004 | ||
a5dcde2f | 2005 | /* get the node name of the system */ |
2006 | ||
2007 | char * ltt_trace_system_description_node_name (LttSystemDescription * s) | |
2008 | { | |
2009 | return s->node_name; | |
2010 | } | |
2011 | ||
2012 | ||
2013 | /* get the domain name of the system */ | |
2014 | ||
2015 | char * ltt_trace_system_description_domain_name (LttSystemDescription * s) | |
2016 | { | |
2017 | return s->domain_name; | |
2018 | } | |
2019 | ||
2020 | ||
2021 | /* get the description of the system */ | |
2022 | ||
2023 | char * ltt_trace_system_description_description (LttSystemDescription * s) | |
2024 | { | |
2025 | return s->description; | |
2026 | } | |
2027 | ||
2028 | ||
bf33dd50 | 2029 | /* get the NTP corrected start time of the trace */ |
7bd563ec | 2030 | LttTime ltt_trace_start_time(LttTrace *t) |
a5dcde2f | 2031 | { |
7bd563ec | 2032 | return t->start_time; |
a5dcde2f | 2033 | } |
2034 | ||
bf33dd50 | 2035 | /* get the monotonic start time of the trace */ |
2036 | LttTime ltt_trace_start_time_monotonic(LttTrace *t) | |
2037 | { | |
2038 | return t->start_time_from_tsc; | |
2039 | } | |
2040 | ||
43ed82b5 | 2041 | static __attribute__ ((__unused__)) LttTracefile *ltt_tracefile_new() |
18206708 | 2042 | { |
afd57a3c | 2043 | LttTracefile *tf; |
2044 | tf = g_new(LttTracefile, 1); | |
2045 | tf->event.tracefile = tf; | |
2046 | return tf; | |
18206708 | 2047 | } |
2048 | ||
43ed82b5 | 2049 | static __attribute__ ((__unused__)) void ltt_tracefile_destroy(LttTracefile *tf) |
18206708 | 2050 | { |
2051 | g_free(tf); | |
2052 | } | |
2053 | ||
43ed82b5 | 2054 | static __attribute__ ((__unused__)) void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src) |
18206708 | 2055 | { |
2056 | *dest = *src; | |
2057 | } | |
2058 | ||
3aee1200 | 2059 | /* Before library loading... */ |
2060 | ||
8655430b | 2061 | static __attribute__((constructor)) void init(void) |
3aee1200 | 2062 | { |
750eb11a | 2063 | LTT_TRACEFILE_NAME_METADATA = g_quark_from_string("metadata"); |
3aee1200 | 2064 | } |
a31d2894 WB |
2065 | |
2066 | /***************************************************************************** | |
2067 | *Function name | |
2068 | * ltt_tracefile_open_header : based on ltt_tracefile_open but it stops | |
72984157 WB |
2069 | * when it gets the header |
2070 | *Input params | |
a31d2894 WB |
2071 | * fileName : path to the tracefile |
2072 | * tf : the tracefile (metadata_0) where the header will be read | |
2073 | *Return value | |
2074 | * ltt_subbuffer_header_t : the header containing the version number | |
2075 | ****************************************************************************/ | |
2076 | static ltt_subbuffer_header_t * ltt_tracefile_open_header(gchar *fileName, LttTracefile *tf) | |
2077 | { | |
2078 | struct stat lTDFStat; /* Trace data file status */ | |
2079 | ltt_subbuffer_header_t *header; | |
2080 | int page_size = getpagesize(); | |
72984157 | 2081 | |
a31d2894 WB |
2082 | /* open the file */ |
2083 | tf->long_name = g_quark_from_string(fileName); | |
2084 | tf->fd = open(fileName, O_RDONLY); | |
2085 | if(tf->fd < 0){ | |
2086 | g_warning("Unable to open input data file %s\n", fileName); | |
2087 | goto end; | |
2088 | } | |
72984157 WB |
2089 | |
2090 | /* Get the file's status */ | |
a31d2894 WB |
2091 | if(fstat(tf->fd, &lTDFStat) < 0){ |
2092 | g_warning("Unable to get the status of the input data file %s\n", fileName); | |
2093 | goto close_file; | |
2094 | } | |
72984157 | 2095 | |
a31d2894 WB |
2096 | /* Is the file large enough to contain a trace */ |
2097 | if(lTDFStat.st_size < (off_t)(ltt_subbuffer_header_size())) { | |
2098 | g_print("The input data file %s does not contain a trace\n", fileName); | |
2099 | goto close_file; | |
2100 | } | |
72984157 | 2101 | |
a31d2894 WB |
2102 | /* Temporarily map the buffer start header to get trace information */ |
2103 | /* Multiple of pages aligned head */ | |
2104 | tf->buffer.head = mmap(0,PAGE_ALIGN(ltt_subbuffer_header_size()), PROT_READ, MAP_PRIVATE, tf->fd, 0); | |
72984157 | 2105 | |
a31d2894 WB |
2106 | if(tf->buffer.head == MAP_FAILED) { |
2107 | perror("Error in allocating memory for buffer of tracefile"); | |
2108 | goto close_file; | |
2109 | } | |
2110 | g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned. | |
72984157 | 2111 | |
a31d2894 | 2112 | header = (ltt_subbuffer_header_t *)tf->buffer.head; |
72984157 | 2113 | |
a31d2894 | 2114 | return header; |
72984157 | 2115 | |
a31d2894 WB |
2116 | close_file: |
2117 | close(tf->fd); | |
2118 | end: | |
2119 | return 0; | |
2120 | } | |
2121 | ||
2122 | ||
2123 | /***************************************************************************** | |
2124 | *Function name | |
2125 | * get_version : get the trace version from a metadata_0 trace file | |
72984157 | 2126 | *Input params |
a31d2894 WB |
2127 | * pathname : path to the trace |
2128 | * version_number : the struct that will get the version number | |
2129 | *Return value | |
2130 | * int : 1 if succeed, -1 if error | |
2131 | ****************************************************************************/ | |
72984157 | 2132 | int ltt_get_trace_version(const gchar *pathname, struct LttTraceVersion *version_number) |
a31d2894 WB |
2133 | { |
2134 | gchar abs_path[PATH_MAX]; | |
2135 | int ret = 0; | |
2136 | DIR *dir; | |
2137 | struct dirent *entry; | |
2138 | struct stat stat_buf; | |
2139 | gchar path[PATH_MAX]; | |
72984157 | 2140 | |
a31d2894 WB |
2141 | LttTracefile tmp_tf; |
2142 | LttTrace * t; | |
2143 | ltt_subbuffer_header_t *header; | |
72984157 | 2144 | |
a31d2894 | 2145 | t = g_new(LttTrace, 1); |
72984157 | 2146 | |
a31d2894 | 2147 | get_absolute_pathname(pathname, abs_path); |
72984157 | 2148 | |
a31d2894 WB |
2149 | /* Test to see if it looks like a trace */ |
2150 | dir = opendir(abs_path); | |
72984157 | 2151 | |
a31d2894 WB |
2152 | if(dir == NULL) { |
2153 | perror(abs_path); | |
2154 | goto open_error; | |
2155 | } | |
72984157 | 2156 | |
a31d2894 WB |
2157 | while((entry = readdir(dir)) != NULL) { |
2158 | strcpy(path, abs_path); | |
2159 | strcat(path, "/"); | |
2160 | strcat(path, entry->d_name); | |
2161 | ret = stat(path, &stat_buf); | |
2162 | if(ret == -1) { | |
2163 | perror(path); | |
2164 | continue; | |
2165 | } | |
2166 | } | |
72984157 WB |
2167 | |
2168 | closedir(dir); | |
a31d2894 | 2169 | dir = opendir(abs_path); |
72984157 | 2170 | |
a31d2894 WB |
2171 | while((entry = readdir(dir)) != NULL) { |
2172 | if(entry->d_name[0] == '.') continue; | |
8b0a0cc8 | 2173 | if(g_str_has_prefix(entry->d_name, "metadata_") != 0) continue; |
72984157 | 2174 | |
a31d2894 WB |
2175 | strcpy(path, abs_path); |
2176 | strcat(path, "/"); | |
2177 | strcat(path, entry->d_name); | |
2178 | if(ret == -1) { | |
2179 | perror(path); | |
2180 | continue; | |
2181 | } | |
72984157 WB |
2182 | |
2183 | header = ltt_tracefile_open_header(path, &tmp_tf); | |
2184 | ||
a31d2894 WB |
2185 | if(header == NULL) { |
2186 | g_info("Error getting the header %s", path); | |
2187 | continue; /* error opening the tracefile : bad magic number ? */ | |
2188 | } | |
72984157 WB |
2189 | |
2190 | version_number->ltt_major_version = header->major_version; | |
2191 | version_number->ltt_minor_version = header->minor_version; | |
8b0a0cc8 YB |
2192 | |
2193 | return 1; | |
a31d2894 | 2194 | } |
72984157 | 2195 | |
8b0a0cc8 | 2196 | return -1; |
72984157 | 2197 | |
a31d2894 WB |
2198 | open_error: |
2199 | g_free(t); | |
2200 | return -1; | |
2201 | } |