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