Commit | Line | Data |
---|---|---|
d0b96690 DG |
1 | /* |
2 | * ust-metadata.c | |
3 | * | |
4 | * LTTng-UST metadata generation | |
5 | * | |
6 | * Copyright (C) 2010-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License, version 2 only, | |
10 | * as published by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License along | |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 | */ | |
21 | ||
6c1c0768 | 22 | #define _LGPL_SOURCE |
d0b96690 DG |
23 | #include <stdint.h> |
24 | #include <string.h> | |
25 | #include <stdarg.h> | |
26 | #include <stdio.h> | |
27 | #include <limits.h> | |
28 | #include <unistd.h> | |
29 | #include <inttypes.h> | |
30 | #include <common/common.h> | |
31 | ||
32 | #include "ust-registry.h" | |
33 | #include "ust-clock.h" | |
34 | #include "ust-app.h" | |
35 | ||
36 | #ifndef max_t | |
37 | #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b))) | |
38 | #endif | |
39 | ||
6e1b0543 | 40 | #define NSEC_PER_SEC 1000000000ULL |
8c645bb0 MD |
41 | #define NR_CLOCK_OFFSET_SAMPLES 10 |
42 | ||
43 | struct offset_sample { | |
44 | uint64_t offset; /* correlation offset */ | |
45 | uint64_t measure_delta; /* lower is better */ | |
46 | }; | |
47 | ||
d0b96690 DG |
48 | static inline |
49 | int fls(unsigned int x) | |
50 | { | |
51 | int r = 32; | |
52 | ||
53 | if (!x) | |
54 | return 0; | |
55 | if (!(x & 0xFFFF0000U)) { | |
56 | x <<= 16; | |
57 | r -= 16; | |
58 | } | |
59 | if (!(x & 0xFF000000U)) { | |
60 | x <<= 8; | |
61 | r -= 8; | |
62 | } | |
63 | if (!(x & 0xF0000000U)) { | |
64 | x <<= 4; | |
65 | r -= 4; | |
66 | } | |
67 | if (!(x & 0xC0000000U)) { | |
68 | x <<= 2; | |
69 | r -= 2; | |
70 | } | |
71 | if (!(x & 0x80000000U)) { | |
72 | x <<= 1; | |
73 | r -= 1; | |
74 | } | |
75 | return r; | |
76 | } | |
77 | ||
78 | static inline | |
79 | int get_count_order(unsigned int count) | |
80 | { | |
81 | int order; | |
82 | ||
83 | order = fls(count) - 1; | |
84 | if (count & (count - 1)) | |
85 | order++; | |
86 | return order; | |
87 | } | |
88 | ||
89 | /* | |
90 | * Returns offset where to write in metadata array, or negative error value on error. | |
91 | */ | |
92 | static | |
93 | ssize_t metadata_reserve(struct ust_registry_session *session, size_t len) | |
94 | { | |
95 | size_t new_len = session->metadata_len + len; | |
96 | size_t new_alloc_len = new_len; | |
97 | size_t old_alloc_len = session->metadata_alloc_len; | |
98 | ssize_t ret; | |
99 | ||
100 | if (new_alloc_len > (UINT32_MAX >> 1)) | |
101 | return -EINVAL; | |
102 | if ((old_alloc_len << 1) > (UINT32_MAX >> 1)) | |
103 | return -EINVAL; | |
104 | ||
105 | if (new_alloc_len > old_alloc_len) { | |
106 | char *newptr; | |
107 | ||
108 | new_alloc_len = | |
109 | max_t(size_t, 1U << get_count_order(new_alloc_len), old_alloc_len << 1); | |
110 | newptr = realloc(session->metadata, new_alloc_len); | |
111 | if (!newptr) | |
112 | return -ENOMEM; | |
113 | session->metadata = newptr; | |
114 | /* We zero directly the memory from start of allocation. */ | |
115 | memset(&session->metadata[old_alloc_len], 0, new_alloc_len - old_alloc_len); | |
116 | session->metadata_alloc_len = new_alloc_len; | |
117 | } | |
118 | ret = session->metadata_len; | |
119 | session->metadata_len += len; | |
120 | return ret; | |
121 | } | |
122 | ||
d7ba1388 MD |
123 | static |
124 | int metadata_file_append(struct ust_registry_session *session, | |
125 | const char *str, size_t len) | |
126 | { | |
127 | ssize_t written; | |
128 | ||
129 | if (session->metadata_fd < 0) { | |
130 | return 0; | |
131 | } | |
132 | /* Write to metadata file */ | |
133 | written = lttng_write(session->metadata_fd, str, len); | |
134 | if (written != len) { | |
135 | return -1; | |
136 | } | |
137 | return 0; | |
138 | } | |
139 | ||
d0b96690 DG |
140 | /* |
141 | * We have exclusive access to our metadata buffer (protected by the | |
142 | * ust_lock), so we can do racy operations such as looking for | |
143 | * remaining space left in packet and write, since mutual exclusion | |
144 | * protects us from concurrent writes. | |
145 | */ | |
146 | static | |
147 | int lttng_metadata_printf(struct ust_registry_session *session, | |
148 | const char *fmt, ...) | |
149 | { | |
150 | char *str = NULL; | |
151 | size_t len; | |
152 | va_list ap; | |
153 | ssize_t offset; | |
154 | int ret; | |
155 | ||
156 | va_start(ap, fmt); | |
157 | ret = vasprintf(&str, fmt, ap); | |
158 | va_end(ap); | |
159 | if (ret < 0) | |
160 | return -ENOMEM; | |
161 | ||
162 | len = strlen(str); | |
163 | offset = metadata_reserve(session, len); | |
164 | if (offset < 0) { | |
165 | ret = offset; | |
166 | goto end; | |
167 | } | |
168 | memcpy(&session->metadata[offset], str, len); | |
d7ba1388 MD |
169 | ret = metadata_file_append(session, str, len); |
170 | if (ret) { | |
171 | PERROR("Error appending to metadata file"); | |
172 | goto end; | |
173 | } | |
d0b96690 DG |
174 | DBG3("Append to metadata: \"%s\"", str); |
175 | ret = 0; | |
176 | ||
177 | end: | |
178 | free(str); | |
179 | return ret; | |
180 | } | |
181 | ||
10b56aef MD |
182 | /* Called with session registry mutex held. */ |
183 | static | |
184 | int ust_metadata_enum_statedump(struct ust_registry_session *session, | |
185 | const char *enum_name, | |
186 | uint64_t enum_id, | |
187 | const struct ustctl_integer_type *container_type, | |
188 | const char *field_name) | |
189 | { | |
190 | struct ust_registry_enum *reg_enum; | |
191 | const struct ustctl_enum_entry *entries; | |
192 | size_t nr_entries; | |
193 | int ret = 0; | |
194 | size_t i; | |
195 | ||
196 | rcu_read_lock(); | |
197 | reg_enum = ust_registry_lookup_enum_by_id(session, enum_name, enum_id); | |
198 | rcu_read_unlock(); | |
199 | /* reg_enum can still be used because session registry mutex is held. */ | |
200 | if (!reg_enum) { | |
201 | ret = -ENOENT; | |
202 | goto end; | |
203 | } | |
204 | entries = reg_enum->entries; | |
205 | nr_entries = reg_enum->nr_entries; | |
206 | ||
207 | ret = lttng_metadata_printf(session, | |
208 | " enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n", | |
209 | container_type->size, | |
210 | container_type->alignment, | |
211 | container_type->signedness, | |
212 | (container_type->encoding == ustctl_encode_none) | |
213 | ? "none" | |
214 | : (container_type->encoding == ustctl_encode_UTF8) | |
215 | ? "UTF8" | |
216 | : "ASCII", | |
217 | container_type->base); | |
218 | if (ret) { | |
219 | goto end; | |
220 | } | |
221 | /* Dump all entries */ | |
222 | for (i = 0; i < nr_entries; i++) { | |
223 | const struct ustctl_enum_entry *entry = &entries[i]; | |
224 | int j, len; | |
225 | ||
226 | ret = lttng_metadata_printf(session, | |
227 | " \""); | |
228 | if (ret) { | |
229 | goto end; | |
230 | } | |
231 | len = strlen(entry->string); | |
232 | /* Escape the character '"' */ | |
233 | for (j = 0; j < len; j++) { | |
234 | char c = entry->string[j]; | |
235 | ||
236 | switch (c) { | |
237 | case '"': | |
238 | ret = lttng_metadata_printf(session, | |
239 | "\\\""); | |
240 | break; | |
241 | case '\\': | |
242 | ret = lttng_metadata_printf(session, | |
243 | "\\\\"); | |
244 | break; | |
245 | default: | |
246 | ret = lttng_metadata_printf(session, | |
247 | "%c", c); | |
248 | break; | |
249 | } | |
250 | if (ret) { | |
251 | goto end; | |
252 | } | |
253 | } | |
254 | ret = lttng_metadata_printf(session, | |
255 | "\" = "); | |
256 | if (ret) { | |
257 | goto end; | |
258 | } | |
259 | if (entry->start == entry->end) { | |
260 | ret = lttng_metadata_printf(session, | |
261 | "%d,\n", | |
262 | entry->start); | |
263 | } else { | |
264 | ret = lttng_metadata_printf(session, | |
265 | "%d ... %d,\n", | |
266 | entry->start, entry->end); | |
267 | } | |
268 | if (ret) { | |
269 | goto end; | |
270 | } | |
271 | } | |
272 | ret = lttng_metadata_printf(session, " } _%s;\n", | |
273 | field_name); | |
274 | end: | |
275 | return ret; | |
276 | } | |
277 | ||
d0b96690 DG |
278 | static |
279 | int _lttng_field_statedump(struct ust_registry_session *session, | |
280 | const struct ustctl_field *field) | |
281 | { | |
282 | int ret = 0; | |
283 | const char *bo_be = " byte_order = be;"; | |
284 | const char *bo_le = " byte_order = le;"; | |
285 | const char *bo_native = ""; | |
286 | const char *bo_reverse; | |
287 | ||
288 | if (session->byte_order == BIG_ENDIAN) | |
289 | bo_reverse = bo_le; | |
290 | else | |
291 | bo_reverse = bo_be; | |
292 | ||
293 | switch (field->type.atype) { | |
294 | case ustctl_atype_integer: | |
295 | ret = lttng_metadata_printf(session, | |
296 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n", | |
297 | field->type.u.basic.integer.size, | |
298 | field->type.u.basic.integer.alignment, | |
299 | field->type.u.basic.integer.signedness, | |
300 | (field->type.u.basic.integer.encoding == ustctl_encode_none) | |
301 | ? "none" | |
302 | : (field->type.u.basic.integer.encoding == ustctl_encode_UTF8) | |
303 | ? "UTF8" | |
304 | : "ASCII", | |
305 | field->type.u.basic.integer.base, | |
306 | field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
307 | field->name); | |
308 | break; | |
10b56aef MD |
309 | case ustctl_atype_enum: |
310 | ret = ust_metadata_enum_statedump(session, | |
311 | field->type.u.basic.enumeration.name, | |
312 | field->type.u.basic.enumeration.id, | |
313 | &field->type.u.basic.enumeration.container_type, | |
314 | field->name); | |
315 | break; | |
d0b96690 DG |
316 | case ustctl_atype_float: |
317 | ret = lttng_metadata_printf(session, | |
318 | " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n", | |
319 | field->type.u.basic._float.exp_dig, | |
320 | field->type.u.basic._float.mant_dig, | |
321 | field->type.u.basic._float.alignment, | |
322 | field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
323 | field->name); | |
324 | break; | |
d0b96690 DG |
325 | case ustctl_atype_array: |
326 | { | |
327 | const struct ustctl_basic_type *elem_type; | |
328 | ||
329 | elem_type = &field->type.u.array.elem_type; | |
330 | ret = lttng_metadata_printf(session, | |
331 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n", | |
332 | elem_type->u.basic.integer.size, | |
333 | elem_type->u.basic.integer.alignment, | |
334 | elem_type->u.basic.integer.signedness, | |
335 | (elem_type->u.basic.integer.encoding == ustctl_encode_none) | |
336 | ? "none" | |
337 | : (elem_type->u.basic.integer.encoding == ustctl_encode_UTF8) | |
338 | ? "UTF8" | |
339 | : "ASCII", | |
340 | elem_type->u.basic.integer.base, | |
341 | elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
342 | field->name, field->type.u.array.length); | |
343 | break; | |
344 | } | |
345 | case ustctl_atype_sequence: | |
346 | { | |
347 | const struct ustctl_basic_type *elem_type; | |
348 | const struct ustctl_basic_type *length_type; | |
349 | ||
350 | elem_type = &field->type.u.sequence.elem_type; | |
351 | length_type = &field->type.u.sequence.length_type; | |
352 | ret = lttng_metadata_printf(session, | |
353 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n", | |
354 | length_type->u.basic.integer.size, | |
355 | (unsigned int) length_type->u.basic.integer.alignment, | |
356 | length_type->u.basic.integer.signedness, | |
357 | (length_type->u.basic.integer.encoding == ustctl_encode_none) | |
358 | ? "none" | |
359 | : ((length_type->u.basic.integer.encoding == ustctl_encode_UTF8) | |
360 | ? "UTF8" | |
361 | : "ASCII"), | |
362 | length_type->u.basic.integer.base, | |
363 | length_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
364 | field->name); | |
365 | if (ret) | |
366 | return ret; | |
367 | ||
368 | ret = lttng_metadata_printf(session, | |
369 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n", | |
370 | elem_type->u.basic.integer.size, | |
371 | (unsigned int) elem_type->u.basic.integer.alignment, | |
372 | elem_type->u.basic.integer.signedness, | |
373 | (elem_type->u.basic.integer.encoding == ustctl_encode_none) | |
374 | ? "none" | |
375 | : ((elem_type->u.basic.integer.encoding == ustctl_encode_UTF8) | |
376 | ? "UTF8" | |
377 | : "ASCII"), | |
378 | elem_type->u.basic.integer.base, | |
379 | elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
380 | field->name, | |
381 | field->name); | |
382 | break; | |
383 | } | |
384 | ||
385 | case ustctl_atype_string: | |
386 | /* Default encoding is UTF8 */ | |
387 | ret = lttng_metadata_printf(session, | |
388 | " string%s _%s;\n", | |
389 | field->type.u.basic.string.encoding == ustctl_encode_ASCII ? | |
390 | " { encoding = ASCII; }" : "", | |
391 | field->name); | |
392 | break; | |
393 | default: | |
394 | return -EINVAL; | |
395 | } | |
396 | return ret; | |
397 | } | |
398 | ||
399 | static | |
400 | int _lttng_context_metadata_statedump(struct ust_registry_session *session, | |
401 | size_t nr_ctx_fields, | |
402 | struct ustctl_field *ctx) | |
403 | { | |
404 | int ret = 0; | |
405 | int i; | |
406 | ||
407 | if (!ctx) | |
408 | return 0; | |
409 | for (i = 0; i < nr_ctx_fields; i++) { | |
410 | const struct ustctl_field *field = &ctx[i]; | |
411 | ||
412 | ret = _lttng_field_statedump(session, field); | |
413 | if (ret) | |
414 | return ret; | |
415 | } | |
416 | return ret; | |
417 | } | |
418 | ||
419 | static | |
420 | int _lttng_fields_metadata_statedump(struct ust_registry_session *session, | |
421 | struct ust_registry_event *event) | |
422 | { | |
423 | int ret = 0; | |
424 | int i; | |
425 | ||
426 | for (i = 0; i < event->nr_fields; i++) { | |
427 | const struct ustctl_field *field = &event->fields[i]; | |
428 | ||
429 | ret = _lttng_field_statedump(session, field); | |
430 | if (ret) | |
431 | return ret; | |
432 | } | |
433 | return ret; | |
434 | } | |
435 | ||
436 | /* | |
437 | * Should be called with session registry mutex held. | |
438 | */ | |
439 | int ust_metadata_event_statedump(struct ust_registry_session *session, | |
440 | struct ust_registry_channel *chan, | |
441 | struct ust_registry_event *event) | |
442 | { | |
443 | int ret = 0; | |
444 | ||
445 | /* Don't dump metadata events */ | |
446 | if (chan->chan_id == -1U) | |
447 | return 0; | |
448 | ||
449 | ret = lttng_metadata_printf(session, | |
450 | "event {\n" | |
451 | " name = \"%s\";\n" | |
452 | " id = %u;\n" | |
453 | " stream_id = %u;\n", | |
454 | event->name, | |
455 | event->id, | |
456 | chan->chan_id); | |
457 | if (ret) | |
458 | goto end; | |
459 | ||
460 | ret = lttng_metadata_printf(session, | |
461 | " loglevel = %d;\n", | |
2106efa0 | 462 | event->loglevel_value); |
d0b96690 DG |
463 | if (ret) |
464 | goto end; | |
465 | ||
466 | if (event->model_emf_uri) { | |
467 | ret = lttng_metadata_printf(session, | |
468 | " model.emf.uri = \"%s\";\n", | |
469 | event->model_emf_uri); | |
470 | if (ret) | |
471 | goto end; | |
472 | } | |
473 | ||
d0b96690 DG |
474 | ret = lttng_metadata_printf(session, |
475 | " fields := struct {\n" | |
476 | ); | |
477 | if (ret) | |
478 | goto end; | |
479 | ||
480 | ret = _lttng_fields_metadata_statedump(session, event); | |
481 | if (ret) | |
482 | goto end; | |
483 | ||
484 | ret = lttng_metadata_printf(session, | |
485 | " };\n" | |
486 | "};\n\n"); | |
487 | if (ret) | |
488 | goto end; | |
7972aab2 | 489 | event->metadata_dumped = 1; |
d0b96690 DG |
490 | |
491 | end: | |
492 | return ret; | |
493 | } | |
494 | ||
495 | /* | |
496 | * Should be called with session registry mutex held. | |
497 | */ | |
498 | int ust_metadata_channel_statedump(struct ust_registry_session *session, | |
499 | struct ust_registry_channel *chan) | |
500 | { | |
501 | int ret = 0; | |
502 | ||
503 | /* Don't dump metadata events */ | |
504 | if (chan->chan_id == -1U) | |
505 | return 0; | |
506 | ||
507 | if (!chan->header_type) | |
508 | return -EINVAL; | |
509 | ||
510 | ret = lttng_metadata_printf(session, | |
511 | "stream {\n" | |
512 | " id = %u;\n" | |
513 | " event.header := %s;\n" | |
514 | " packet.context := struct packet_context;\n", | |
515 | chan->chan_id, | |
516 | chan->header_type == USTCTL_CHANNEL_HEADER_COMPACT ? | |
517 | "struct event_header_compact" : | |
518 | "struct event_header_large"); | |
519 | if (ret) | |
520 | goto end; | |
521 | ||
522 | if (chan->ctx_fields) { | |
523 | ret = lttng_metadata_printf(session, | |
524 | " event.context := struct {\n"); | |
525 | if (ret) | |
526 | goto end; | |
527 | } | |
528 | ret = _lttng_context_metadata_statedump(session, | |
529 | chan->nr_ctx_fields, | |
530 | chan->ctx_fields); | |
531 | if (ret) | |
532 | goto end; | |
533 | if (chan->ctx_fields) { | |
534 | ret = lttng_metadata_printf(session, | |
535 | " };\n"); | |
536 | if (ret) | |
537 | goto end; | |
538 | } | |
539 | ||
540 | ret = lttng_metadata_printf(session, | |
541 | "};\n\n"); | |
7972aab2 DG |
542 | /* Flag success of metadata dump. */ |
543 | chan->metadata_dumped = 1; | |
d0b96690 DG |
544 | |
545 | end: | |
546 | return ret; | |
547 | } | |
548 | ||
549 | static | |
550 | int _lttng_stream_packet_context_declare(struct ust_registry_session *session) | |
551 | { | |
552 | return lttng_metadata_printf(session, | |
553 | "struct packet_context {\n" | |
554 | " uint64_clock_monotonic_t timestamp_begin;\n" | |
555 | " uint64_clock_monotonic_t timestamp_end;\n" | |
556 | " uint64_t content_size;\n" | |
557 | " uint64_t packet_size;\n" | |
558 | " unsigned long events_discarded;\n" | |
559 | " uint32_t cpu_id;\n" | |
560 | "};\n\n" | |
561 | ); | |
562 | } | |
563 | ||
564 | /* | |
565 | * Compact header: | |
566 | * id: range: 0 - 30. | |
567 | * id 31 is reserved to indicate an extended header. | |
568 | * | |
569 | * Large header: | |
570 | * id: range: 0 - 65534. | |
571 | * id 65535 is reserved to indicate an extended header. | |
572 | */ | |
573 | static | |
574 | int _lttng_event_header_declare(struct ust_registry_session *session) | |
575 | { | |
576 | return lttng_metadata_printf(session, | |
577 | "struct event_header_compact {\n" | |
578 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" | |
579 | " variant <id> {\n" | |
580 | " struct {\n" | |
581 | " uint27_clock_monotonic_t timestamp;\n" | |
582 | " } compact;\n" | |
583 | " struct {\n" | |
584 | " uint32_t id;\n" | |
585 | " uint64_clock_monotonic_t timestamp;\n" | |
586 | " } extended;\n" | |
587 | " } v;\n" | |
588 | "} align(%u);\n" | |
589 | "\n" | |
590 | "struct event_header_large {\n" | |
591 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" | |
592 | " variant <id> {\n" | |
593 | " struct {\n" | |
594 | " uint32_clock_monotonic_t timestamp;\n" | |
595 | " } compact;\n" | |
596 | " struct {\n" | |
597 | " uint32_t id;\n" | |
598 | " uint64_clock_monotonic_t timestamp;\n" | |
599 | " } extended;\n" | |
600 | " } v;\n" | |
601 | "} align(%u);\n\n", | |
602 | session->uint32_t_alignment, | |
603 | session->uint16_t_alignment | |
604 | ); | |
605 | } | |
606 | ||
d0b96690 | 607 | static |
8c645bb0 | 608 | int measure_single_clock_offset(struct offset_sample *sample) |
d0b96690 | 609 | { |
8c645bb0 | 610 | uint64_t offset, monotonic[2], measure_delta, realtime; |
fc0bb9fa | 611 | uint64_t tcf = trace_clock_freq(); |
d0b96690 DG |
612 | struct timespec rts = { 0, 0 }; |
613 | int ret; | |
614 | ||
615 | monotonic[0] = trace_clock_read64(); | |
616 | ret = clock_gettime(CLOCK_REALTIME, &rts); | |
8c645bb0 MD |
617 | if (ret < 0) { |
618 | return ret; | |
619 | } | |
d0b96690 | 620 | monotonic[1] = trace_clock_read64(); |
8c645bb0 MD |
621 | measure_delta = monotonic[1] - monotonic[0]; |
622 | if (measure_delta > sample->measure_delta) { | |
623 | /* | |
624 | * Discard value if it took longer to read than the best | |
625 | * sample so far. | |
626 | */ | |
627 | return 0; | |
628 | } | |
d0b96690 | 629 | offset = (monotonic[0] + monotonic[1]) >> 1; |
6e1b0543 MD |
630 | realtime = (uint64_t) rts.tv_sec * tcf; |
631 | if (tcf == NSEC_PER_SEC) { | |
632 | realtime += rts.tv_nsec; | |
633 | } else { | |
634 | realtime += (uint64_t) rts.tv_nsec * tcf / NSEC_PER_SEC; | |
fc0bb9fa | 635 | } |
d0b96690 | 636 | offset = realtime - offset; |
8c645bb0 MD |
637 | sample->offset = offset; |
638 | sample->measure_delta = measure_delta; | |
639 | return 0; | |
d0b96690 DG |
640 | } |
641 | ||
8c645bb0 MD |
642 | /* |
643 | * Approximation of NTP time of day to clock monotonic correlation, | |
644 | * taken at start of trace. Keep the measurement that took the less time | |
645 | * to complete, thus removing imprecision caused by preemption. | |
646 | */ | |
647 | static | |
648 | uint64_t measure_clock_offset(void) | |
649 | { | |
650 | int i; | |
651 | struct offset_sample offset_best_sample = { | |
652 | .offset = 0, | |
653 | .measure_delta = UINT64_MAX, | |
654 | }; | |
655 | ||
656 | for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) { | |
657 | if (measure_single_clock_offset(&offset_best_sample)) { | |
658 | return 0; | |
659 | } | |
660 | } | |
661 | return offset_best_sample.offset; | |
662 | } | |
d0b96690 DG |
663 | |
664 | /* | |
665 | * Should be called with session registry mutex held. | |
666 | */ | |
667 | int ust_metadata_session_statedump(struct ust_registry_session *session, | |
af6142cf MD |
668 | struct ust_app *app, |
669 | uint32_t major, | |
670 | uint32_t minor) | |
d0b96690 DG |
671 | { |
672 | unsigned char *uuid_c; | |
673 | char uuid_s[UUID_STR_LEN], | |
674 | clock_uuid_s[UUID_STR_LEN]; | |
675 | int ret = 0; | |
676 | char hostname[HOST_NAME_MAX]; | |
677 | ||
7972aab2 | 678 | assert(session); |
7972aab2 | 679 | |
d0b96690 DG |
680 | uuid_c = session->uuid; |
681 | ||
682 | snprintf(uuid_s, sizeof(uuid_s), | |
683 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", | |
684 | uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3], | |
685 | uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7], | |
686 | uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11], | |
687 | uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]); | |
688 | ||
d7ba1388 MD |
689 | /* For crash ABI */ |
690 | ret = lttng_metadata_printf(session, | |
691 | "/* CTF %u.%u */\n\n", | |
692 | CTF_SPEC_MAJOR, | |
693 | CTF_SPEC_MINOR); | |
694 | if (ret) { | |
695 | goto end; | |
696 | } | |
697 | ||
d0b96690 DG |
698 | ret = lttng_metadata_printf(session, |
699 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" | |
700 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" | |
701 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" | |
702 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" | |
703 | "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n" | |
704 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" | |
705 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" | |
706 | "\n" | |
707 | "trace {\n" | |
708 | " major = %u;\n" | |
709 | " minor = %u;\n" | |
710 | " uuid = \"%s\";\n" | |
711 | " byte_order = %s;\n" | |
712 | " packet.header := struct {\n" | |
713 | " uint32_t magic;\n" | |
714 | " uint8_t uuid[16];\n" | |
715 | " uint32_t stream_id;\n" | |
716 | " };\n" | |
717 | "};\n\n", | |
718 | session->uint8_t_alignment, | |
719 | session->uint16_t_alignment, | |
720 | session->uint32_t_alignment, | |
721 | session->uint64_t_alignment, | |
722 | session->bits_per_long, | |
723 | session->long_alignment, | |
724 | CTF_SPEC_MAJOR, | |
725 | CTF_SPEC_MINOR, | |
726 | uuid_s, | |
727 | session->byte_order == BIG_ENDIAN ? "be" : "le" | |
728 | ); | |
729 | if (ret) | |
730 | goto end; | |
731 | ||
732 | /* ignore error, just use empty string if error. */ | |
733 | hostname[0] = '\0'; | |
734 | ret = gethostname(hostname, sizeof(hostname)); | |
735 | if (ret && errno == ENAMETOOLONG) | |
736 | hostname[HOST_NAME_MAX - 1] = '\0'; | |
737 | ret = lttng_metadata_printf(session, | |
738 | "env {\n" | |
739 | " hostname = \"%s\";\n" | |
740 | " domain = \"ust\";\n" | |
741 | " tracer_name = \"lttng-ust\";\n" | |
742 | " tracer_major = %u;\n" | |
af6142cf | 743 | " tracer_minor = %u;\n", |
d0b96690 | 744 | hostname, |
af6142cf MD |
745 | major, |
746 | minor | |
d0b96690 DG |
747 | ); |
748 | if (ret) | |
749 | goto end; | |
750 | ||
751 | /* | |
752 | * If per-application registry, we can output extra information | |
753 | * about the application. | |
754 | */ | |
755 | if (app) { | |
756 | ret = lttng_metadata_printf(session, | |
af6142cf | 757 | " tracer_patchlevel = %u;\n" |
d0b96690 | 758 | " vpid = %d;\n" |
d88aee68 | 759 | " procname = \"%s\";\n", |
af6142cf | 760 | app->version.patchlevel, |
d0b96690 DG |
761 | (int) app->pid, |
762 | app->name | |
763 | ); | |
764 | if (ret) | |
765 | goto end; | |
766 | } | |
767 | ||
768 | ret = lttng_metadata_printf(session, | |
769 | "};\n\n" | |
770 | ); | |
771 | if (ret) | |
772 | goto end; | |
773 | ||
774 | ||
775 | ret = lttng_metadata_printf(session, | |
776 | "clock {\n" | |
fc0bb9fa MD |
777 | " name = \"%s\";\n", |
778 | trace_clock_name() | |
d0b96690 DG |
779 | ); |
780 | if (ret) | |
781 | goto end; | |
782 | ||
783 | if (!trace_clock_uuid(clock_uuid_s)) { | |
784 | ret = lttng_metadata_printf(session, | |
785 | " uuid = \"%s\";\n", | |
786 | clock_uuid_s | |
787 | ); | |
788 | if (ret) | |
789 | goto end; | |
790 | } | |
791 | ||
792 | ret = lttng_metadata_printf(session, | |
fc0bb9fa | 793 | " description = \"%s\";\n" |
d0b96690 DG |
794 | " freq = %" PRIu64 "; /* Frequency, in Hz */\n" |
795 | " /* clock value offset from Epoch is: offset * (1/freq) */\n" | |
796 | " offset = %" PRIu64 ";\n" | |
797 | "};\n\n", | |
fc0bb9fa | 798 | trace_clock_description(), |
d0b96690 DG |
799 | trace_clock_freq(), |
800 | measure_clock_offset() | |
801 | ); | |
802 | if (ret) | |
803 | goto end; | |
804 | ||
805 | ret = lttng_metadata_printf(session, | |
806 | "typealias integer {\n" | |
807 | " size = 27; align = 1; signed = false;\n" | |
fc0bb9fa | 808 | " map = clock.%s.value;\n" |
d0b96690 DG |
809 | "} := uint27_clock_monotonic_t;\n" |
810 | "\n" | |
811 | "typealias integer {\n" | |
812 | " size = 32; align = %u; signed = false;\n" | |
fc0bb9fa | 813 | " map = clock.%s.value;\n" |
d0b96690 DG |
814 | "} := uint32_clock_monotonic_t;\n" |
815 | "\n" | |
816 | "typealias integer {\n" | |
817 | " size = 64; align = %u; signed = false;\n" | |
fc0bb9fa | 818 | " map = clock.%s.value;\n" |
d0b96690 | 819 | "} := uint64_clock_monotonic_t;\n\n", |
fc0bb9fa | 820 | trace_clock_name(), |
d0b96690 | 821 | session->uint32_t_alignment, |
fc0bb9fa MD |
822 | trace_clock_name(), |
823 | session->uint64_t_alignment, | |
824 | trace_clock_name() | |
d0b96690 DG |
825 | ); |
826 | if (ret) | |
827 | goto end; | |
828 | ||
829 | ret = _lttng_stream_packet_context_declare(session); | |
830 | if (ret) | |
831 | goto end; | |
832 | ||
833 | ret = _lttng_event_header_declare(session); | |
834 | if (ret) | |
835 | goto end; | |
836 | ||
837 | end: | |
838 | return ret; | |
839 | } |