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