2 * Copyright (C) 2010-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
16 #include <common/common.hpp>
17 #include <common/time.hpp>
20 #include "ust-registry.hpp"
21 #include "ust-clock.hpp"
22 #include "ust-app.hpp"
24 #define NR_CLOCK_OFFSET_SAMPLES 10
27 struct offset_sample
{
28 /* correlation offset */
31 uint64_t measure_delta
;
36 int _lttng_field_statedump(ust_registry_session
*session
,
37 const struct lttng_ust_ctl_field
*fields
, size_t nr_fields
,
38 size_t *iter_field
, size_t nesting
);
41 int get_count_order(unsigned int count
)
45 order
= lttng_fls(count
) - 1;
46 if (count
& (count
- 1)) {
49 LTTNG_ASSERT(order
>= 0);
54 * Returns offset where to write in metadata array, or negative error value on error.
57 ssize_t
metadata_reserve(ust_registry_session
*session
, size_t len
)
59 size_t new_len
= session
->_metadata_len
+ len
;
60 size_t new_alloc_len
= new_len
;
61 size_t old_alloc_len
= session
->_metadata_alloc_len
;
64 if (new_alloc_len
> (UINT32_MAX
>> 1))
66 if ((old_alloc_len
<< 1) > (UINT32_MAX
>> 1))
69 if (new_alloc_len
> old_alloc_len
) {
73 std::max
<size_t>(1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
74 newptr
= (char *) realloc(session
->_metadata
, new_alloc_len
);
77 session
->_metadata
= newptr
;
78 /* We zero directly the memory from start of allocation. */
79 memset(&session
->_metadata
[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
80 session
->_metadata_alloc_len
= new_alloc_len
;
82 ret
= session
->_metadata_len
;
83 session
->_metadata_len
+= len
;
88 int metadata_file_append(ust_registry_session
*session
,
89 const char *str
, size_t len
)
93 if (session
->_metadata_fd
< 0) {
96 /* Write to metadata file */
97 written
= lttng_write(session
->_metadata_fd
, str
, len
);
105 * We have exclusive access to our metadata buffer (protected by the
106 * ust_lock), so we can do racy operations such as looking for
107 * remaining space left in packet and write, since mutual exclusion
108 * protects us from concurrent writes.
110 static ATTR_FORMAT_PRINTF(2, 3)
111 int lttng_metadata_printf(ust_registry_session
*session
,
112 const char *fmt
, ...)
121 ret
= vasprintf(&str
, fmt
, ap
);
127 offset
= metadata_reserve(session
, len
);
132 memcpy(&session
->_metadata
[offset
], str
, len
);
133 ret
= metadata_file_append(session
, str
, len
);
135 PERROR("Error appending to metadata file");
138 DBG3("Append to metadata: \"%s\"", str
);
147 int print_tabs(ust_registry_session
*session
, size_t nesting
)
151 for (i
= 0; i
< nesting
; i
++) {
154 ret
= lttng_metadata_printf(session
, " ");
163 void sanitize_ctf_identifier(char *out
, const char *in
)
167 for (i
= 0; i
< LTTNG_UST_ABI_SYM_NAME_LEN
; i
++) {
181 int print_escaped_ctf_string(ust_registry_session
*session
, const char *string
)
189 while (cur
!= '\0') {
192 ret
= lttng_metadata_printf(session
, "%s", "\\n");
196 ret
= lttng_metadata_printf(session
, "%c", '\\');
200 /* We still print the current char */
203 ret
= lttng_metadata_printf(session
, "%c", cur
);
217 /* Called with session registry mutex held. */
219 int ust_metadata_enum_statedump(ust_registry_session
*session
,
220 const char *enum_name
,
222 const struct lttng_ust_ctl_integer_type
*container_type
,
223 const char *field_name
, size_t *iter_field
, size_t nesting
)
225 struct ust_registry_enum
*reg_enum
;
226 const struct lttng_ust_ctl_enum_entry
*entries
;
230 char identifier
[LTTNG_UST_ABI_SYM_NAME_LEN
];
233 reg_enum
= ust_registry_lookup_enum_by_id(session
, enum_name
, enum_id
);
235 /* reg_enum can still be used because session registry mutex is held. */
240 entries
= reg_enum
->entries
;
241 nr_entries
= reg_enum
->nr_entries
;
243 ret
= print_tabs(session
, nesting
);
247 ret
= lttng_metadata_printf(session
,
248 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n",
249 container_type
->size
,
250 container_type
->alignment
,
251 container_type
->signedness
,
252 (container_type
->encoding
== lttng_ust_ctl_encode_none
)
254 : (container_type
->encoding
== lttng_ust_ctl_encode_UTF8
)
257 container_type
->base
);
262 /* Dump all entries */
263 for (i
= 0; i
< nr_entries
; i
++) {
264 const struct lttng_ust_ctl_enum_entry
*entry
= &entries
[i
];
267 ret
= print_tabs(session
, nesting
);
271 ret
= lttng_metadata_printf(session
,
276 len
= strlen(entry
->string
);
277 /* Escape the character '"' */
278 for (j
= 0; j
< len
; j
++) {
279 char c
= entry
->string
[j
];
283 ret
= lttng_metadata_printf(session
,
287 ret
= lttng_metadata_printf(session
,
291 ret
= lttng_metadata_printf(session
,
299 ret
= lttng_metadata_printf(session
, "\"");
304 if (entry
->u
.extra
.options
&
305 LTTNG_UST_CTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
) {
306 ret
= lttng_metadata_printf(session
, ",\n");
311 ret
= lttng_metadata_printf(session
,
317 if (entry
->start
.signedness
) {
318 ret
= lttng_metadata_printf(session
,
319 "%" PRId64
, entry
->start
.value
);
321 ret
= lttng_metadata_printf(session
,
322 "%" PRIu64
, entry
->start
.value
);
328 if (entry
->start
.signedness
== entry
->end
.signedness
&&
329 entry
->start
.value
==
331 ret
= lttng_metadata_printf(session
, ",\n");
333 if (entry
->end
.signedness
) {
334 ret
= lttng_metadata_printf(session
,
335 " ... %" PRId64
",\n",
338 ret
= lttng_metadata_printf(session
,
339 " ... %" PRIu64
",\n",
349 sanitize_ctf_identifier(identifier
, field_name
);
350 ret
= print_tabs(session
, nesting
);
354 ret
= lttng_metadata_printf(session
, "} _%s;\n",
362 int _lttng_variant_statedump(ust_registry_session
*session
,
363 uint32_t nr_choices
, const char *tag_name
,
365 const struct lttng_ust_ctl_field
*fields
, size_t nr_fields
,
366 size_t *iter_field
, size_t nesting
)
368 const struct lttng_ust_ctl_field
*variant
= &fields
[*iter_field
];
371 char identifier
[LTTNG_UST_ABI_SYM_NAME_LEN
];
374 sanitize_ctf_identifier(identifier
, tag_name
);
376 ret
= print_tabs(session
, nesting
);
380 ret
= lttng_metadata_printf(session
,
381 "struct { } align(%u) _%s_padding;\n",
382 alignment
* CHAR_BIT
,
388 ret
= print_tabs(session
, nesting
);
392 ret
= lttng_metadata_printf(session
,
399 for (i
= 0; i
< nr_choices
; i
++) {
400 if (*iter_field
>= nr_fields
) {
404 ret
= _lttng_field_statedump(session
,
406 iter_field
, nesting
+ 1);
411 sanitize_ctf_identifier(identifier
, variant
->name
);
412 ret
= print_tabs(session
, nesting
);
416 ret
= lttng_metadata_printf(session
,
427 int _lttng_field_statedump(ust_registry_session
*session
,
428 const struct lttng_ust_ctl_field
*fields
, size_t nr_fields
,
429 size_t *iter_field
, size_t nesting
)
432 const char *bo_be
= " byte_order = be;";
433 const char *bo_le
= " byte_order = le;";
434 const char *bo_native
= "";
435 const char *bo_reverse
;
436 const struct lttng_ust_ctl_field
*field
;
438 if (*iter_field
>= nr_fields
) {
442 field
= &fields
[*iter_field
];
444 if (session
->_byte_order
== BIG_ENDIAN
) {
450 switch (field
->type
.atype
) {
451 case lttng_ust_ctl_atype_integer
:
452 ret
= print_tabs(session
, nesting
);
456 ret
= lttng_metadata_printf(session
,
457 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
458 field
->type
.u
.integer
.size
,
459 field
->type
.u
.integer
.alignment
,
460 field
->type
.u
.integer
.signedness
,
461 (field
->type
.u
.integer
.encoding
== lttng_ust_ctl_encode_none
)
463 : (field
->type
.u
.integer
.encoding
== lttng_ust_ctl_encode_UTF8
)
466 field
->type
.u
.integer
.base
,
467 field
->type
.u
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
471 case lttng_ust_ctl_atype_enum
:
472 ret
= ust_metadata_enum_statedump(session
,
473 field
->type
.u
.legacy
.basic
.enumeration
.name
,
474 field
->type
.u
.legacy
.basic
.enumeration
.id
,
475 &field
->type
.u
.legacy
.basic
.enumeration
.container_type
,
476 field
->name
, iter_field
, nesting
);
478 case lttng_ust_ctl_atype_float
:
479 ret
= print_tabs(session
, nesting
);
483 ret
= lttng_metadata_printf(session
,
484 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
485 field
->type
.u
._float
.exp_dig
,
486 field
->type
.u
._float
.mant_dig
,
487 field
->type
.u
._float
.alignment
,
488 field
->type
.u
._float
.reverse_byte_order
? bo_reverse
: bo_native
,
492 case lttng_ust_ctl_atype_array
:
494 const struct lttng_ust_ctl_basic_type
*elem_type
;
496 ret
= print_tabs(session
, nesting
);
500 elem_type
= &field
->type
.u
.legacy
.array
.elem_type
;
501 /* Only integers are currently supported in arrays. */
502 if (elem_type
->atype
!= lttng_ust_ctl_atype_integer
) {
506 ret
= lttng_metadata_printf(session
,
507 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
508 elem_type
->u
.basic
.integer
.size
,
509 elem_type
->u
.basic
.integer
.alignment
,
510 elem_type
->u
.basic
.integer
.signedness
,
511 (elem_type
->u
.basic
.integer
.encoding
== lttng_ust_ctl_encode_none
)
513 : (elem_type
->u
.basic
.integer
.encoding
== lttng_ust_ctl_encode_UTF8
)
516 elem_type
->u
.basic
.integer
.base
,
517 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
518 field
->name
, field
->type
.u
.legacy
.array
.length
);
522 case lttng_ust_ctl_atype_array_nestable
:
524 uint32_t array_length
;
525 const struct lttng_ust_ctl_field
*array_nestable
;
526 const struct lttng_ust_ctl_type
*elem_type
;
528 array_length
= field
->type
.u
.array_nestable
.length
;
531 if (*iter_field
>= nr_fields
) {
535 array_nestable
= &fields
[*iter_field
];
536 elem_type
= &array_nestable
->type
;
538 /* Only integers are currently supported in arrays. */
539 if (elem_type
->atype
!= lttng_ust_ctl_atype_integer
) {
544 if (field
->type
.u
.array_nestable
.alignment
) {
545 ret
= print_tabs(session
, nesting
);
549 ret
= lttng_metadata_printf(session
,
550 "struct { } align(%u) _%s_padding;\n",
551 field
->type
.u
.array_nestable
.alignment
* CHAR_BIT
,
558 ret
= print_tabs(session
, nesting
);
562 ret
= lttng_metadata_printf(session
,
563 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
564 elem_type
->u
.integer
.size
,
565 elem_type
->u
.integer
.alignment
,
566 elem_type
->u
.integer
.signedness
,
567 (elem_type
->u
.integer
.encoding
== lttng_ust_ctl_encode_none
)
569 : (elem_type
->u
.integer
.encoding
== lttng_ust_ctl_encode_UTF8
)
572 elem_type
->u
.integer
.base
,
573 elem_type
->u
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
574 field
->name
, array_length
);
578 case lttng_ust_ctl_atype_sequence
:
580 const struct lttng_ust_ctl_basic_type
*elem_type
;
581 const struct lttng_ust_ctl_basic_type
*length_type
;
583 elem_type
= &field
->type
.u
.legacy
.sequence
.elem_type
;
584 length_type
= &field
->type
.u
.legacy
.sequence
.length_type
;
585 ret
= print_tabs(session
, nesting
);
590 /* Only integers are currently supported in sequences. */
591 if (elem_type
->atype
!= lttng_ust_ctl_atype_integer
) {
596 ret
= lttng_metadata_printf(session
,
597 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
598 length_type
->u
.basic
.integer
.size
,
599 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
600 length_type
->u
.basic
.integer
.signedness
,
601 (length_type
->u
.basic
.integer
.encoding
== lttng_ust_ctl_encode_none
)
603 : ((length_type
->u
.basic
.integer
.encoding
== lttng_ust_ctl_encode_UTF8
)
606 length_type
->u
.basic
.integer
.base
,
607 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
613 ret
= print_tabs(session
, nesting
);
617 ret
= lttng_metadata_printf(session
,
618 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
619 elem_type
->u
.basic
.integer
.size
,
620 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
621 elem_type
->u
.basic
.integer
.signedness
,
622 (elem_type
->u
.basic
.integer
.encoding
== lttng_ust_ctl_encode_none
)
624 : ((elem_type
->u
.basic
.integer
.encoding
== lttng_ust_ctl_encode_UTF8
)
627 elem_type
->u
.basic
.integer
.base
,
628 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
634 case lttng_ust_ctl_atype_sequence_nestable
:
636 const struct lttng_ust_ctl_field
*sequence_nestable
;
637 const struct lttng_ust_ctl_type
*elem_type
;
640 if (*iter_field
>= nr_fields
) {
644 sequence_nestable
= &fields
[*iter_field
];
645 elem_type
= &sequence_nestable
->type
;
647 /* Only integers are currently supported in sequences. */
648 if (elem_type
->atype
!= lttng_ust_ctl_atype_integer
) {
653 if (field
->type
.u
.sequence_nestable
.alignment
) {
654 ret
= print_tabs(session
, nesting
);
658 ret
= lttng_metadata_printf(session
,
659 "struct { } align(%u) _%s_padding;\n",
660 field
->type
.u
.sequence_nestable
.alignment
* CHAR_BIT
,
667 ret
= print_tabs(session
, nesting
);
671 ret
= lttng_metadata_printf(session
,
672 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ _%s ];\n",
673 elem_type
->u
.integer
.size
,
674 (unsigned int) elem_type
->u
.integer
.alignment
,
675 elem_type
->u
.integer
.signedness
,
676 (elem_type
->u
.integer
.encoding
== lttng_ust_ctl_encode_none
)
678 : ((elem_type
->u
.integer
.encoding
== lttng_ust_ctl_encode_UTF8
)
681 elem_type
->u
.integer
.base
,
682 elem_type
->u
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
684 field
->type
.u
.sequence_nestable
.length_name
);
688 case lttng_ust_ctl_atype_string
:
689 /* Default encoding is UTF8 */
690 ret
= print_tabs(session
, nesting
);
694 ret
= lttng_metadata_printf(session
,
696 field
->type
.u
.string
.encoding
== lttng_ust_ctl_encode_ASCII
?
697 " { encoding = ASCII; }" : "",
701 case lttng_ust_ctl_atype_variant
:
702 ret
= _lttng_variant_statedump(session
,
703 field
->type
.u
.legacy
.variant
.nr_choices
,
704 field
->type
.u
.legacy
.variant
.tag_name
,
706 fields
, nr_fields
, iter_field
, nesting
);
711 case lttng_ust_ctl_atype_variant_nestable
:
712 ret
= _lttng_variant_statedump(session
,
713 field
->type
.u
.variant_nestable
.nr_choices
,
714 field
->type
.u
.variant_nestable
.tag_name
,
715 field
->type
.u
.variant_nestable
.alignment
,
716 fields
, nr_fields
, iter_field
, nesting
);
721 case lttng_ust_ctl_atype_struct
:
722 if (field
->type
.u
.legacy
._struct
.nr_fields
!= 0) {
723 /* Currently only 0-length structures are supported. */
727 ret
= print_tabs(session
, nesting
);
731 ret
= lttng_metadata_printf(session
,
736 case lttng_ust_ctl_atype_struct_nestable
:
737 if (field
->type
.u
.struct_nestable
.nr_fields
!= 0) {
738 /* Currently only 0-length structures are supported. */
742 ret
= print_tabs(session
, nesting
);
746 if (field
->type
.u
.struct_nestable
.alignment
) {
747 ret
= lttng_metadata_printf(session
,
748 "struct {} align(%u) _%s;\n",
749 field
->type
.u
.struct_nestable
.alignment
* CHAR_BIT
,
755 ret
= lttng_metadata_printf(session
,
761 case lttng_ust_ctl_atype_enum_nestable
:
763 const struct lttng_ust_ctl_field
*container_field
;
764 const struct lttng_ust_ctl_type
*container_type
;
767 if (*iter_field
>= nr_fields
) {
771 container_field
= &fields
[*iter_field
];
772 container_type
= &container_field
->type
;
774 /* Only integers are supported as container types. */
775 if (container_type
->atype
!= lttng_ust_ctl_atype_integer
) {
779 ret
= ust_metadata_enum_statedump(session
,
780 field
->type
.u
.enum_nestable
.name
,
781 field
->type
.u
.enum_nestable
.id
,
782 &container_type
->u
.integer
,
783 field
->name
, iter_field
, nesting
);
794 int _lttng_context_metadata_statedump(ust_registry_session
*session
,
795 size_t nr_ctx_fields
,
796 struct lttng_ust_ctl_field
*ctx
)
804 if (i
>= nr_ctx_fields
) {
807 ret
= _lttng_field_statedump(session
, ctx
,
808 nr_ctx_fields
, &i
, 2);
817 int _lttng_fields_metadata_statedump(ust_registry_session
*session
,
818 struct ust_registry_event
*event
)
824 if (i
>= event
->nr_fields
) {
827 ret
= _lttng_field_statedump(session
, event
->fields
,
828 event
->nr_fields
, &i
, 2);
837 * Should be called with session registry mutex held.
839 int ust_metadata_event_statedump(ust_registry_session
*session
,
840 struct ust_registry_channel
*chan
,
841 struct ust_registry_event
*event
)
845 /* Don't dump metadata events */
846 if (chan
->chan_id
== -1U)
850 * We don't want to output an event's metadata before its parent
851 * stream's metadata. If the stream's metadata hasn't been output yet,
852 * skip this event. Its metadata will be output when we output the
855 if (!chan
->metadata_dumped
|| event
->metadata_dumped
) {
859 ret
= lttng_metadata_printf(session
,
863 " stream_id = %u;\n",
871 ret
= lttng_metadata_printf(session
,
873 event
->loglevel_value
);
878 if (event
->model_emf_uri
) {
879 ret
= lttng_metadata_printf(session
,
880 " model.emf.uri = \"%s\";\n",
881 event
->model_emf_uri
);
887 ret
= lttng_metadata_printf(session
,
888 " fields := struct {\n"
894 ret
= _lttng_fields_metadata_statedump(session
, event
);
899 ret
= lttng_metadata_printf(session
,
905 event
->metadata_dumped
= 1;
912 * Should be called with session registry mutex held.
914 * RCU read lock must be held by the caller.
916 int ust_metadata_channel_statedump(ust_registry_session
*session
,
917 struct ust_registry_channel
*chan
)
921 ASSERT_RCU_READ_LOCKED();
923 /* Don't dump metadata events */
924 if (chan
->chan_id
== -1U)
927 if (!chan
->header_type
)
930 ret
= lttng_metadata_printf(session
,
933 " event.header := %s;\n"
934 " packet.context := struct packet_context;\n",
936 chan
->header_type
== LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT
?
937 "struct event_header_compact" :
938 "struct event_header_large");
943 if (chan
->ctx_fields
) {
944 ret
= lttng_metadata_printf(session
,
945 " event.context := struct {\n");
950 ret
= _lttng_context_metadata_statedump(session
,
956 if (chan
->ctx_fields
) {
957 ret
= lttng_metadata_printf(session
,
964 ret
= lttng_metadata_printf(session
,
970 /* Flag success of metadata dump. */
971 chan
->metadata_dumped
= 1;
974 * Output the metadata of any existing event.
976 * Sort the events by id. This is not necessary, but it's nice to have
977 * a more predictable order in the metadata file.
979 std::vector
<ust_registry_event
*> events
;
981 cds_lfht_iter event_iter
;
982 ust_registry_event
*event
;
983 cds_lfht_for_each_entry(chan
->events
->ht
, &event_iter
, event
,
985 events
.push_back(event
);
989 std::sort(events
.begin(), events
.end(),
990 [] (ust_registry_event
*a
, ust_registry_event
*b
) {
991 return a
->id
< b
->id
;
994 for (ust_registry_event
*event
: events
) {
995 ust_metadata_event_statedump(session
, chan
, event
);
1002 int _lttng_stream_packet_context_declare(ust_registry_session
*session
)
1004 return lttng_metadata_printf(session
,
1005 "struct packet_context {\n"
1006 " uint64_clock_monotonic_t timestamp_begin;\n"
1007 " uint64_clock_monotonic_t timestamp_end;\n"
1008 " uint64_t content_size;\n"
1009 " uint64_t packet_size;\n"
1010 " uint64_t packet_seq_num;\n"
1011 " unsigned long events_discarded;\n"
1012 " uint32_t cpu_id;\n"
1019 * id: range: 0 - 30.
1020 * id 31 is reserved to indicate an extended header.
1023 * id: range: 0 - 65534.
1024 * id 65535 is reserved to indicate an extended header.
1027 int _lttng_event_header_declare(ust_registry_session
*session
)
1029 return lttng_metadata_printf(session
,
1030 "struct event_header_compact {\n"
1031 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1034 " uint27_clock_monotonic_t timestamp;\n"
1038 " uint64_clock_monotonic_t timestamp;\n"
1043 "struct event_header_large {\n"
1044 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1047 " uint32_clock_monotonic_t timestamp;\n"
1051 " uint64_clock_monotonic_t timestamp;\n"
1055 session
->_uint32_t_alignment
,
1056 session
->_uint16_t_alignment
1061 * The offset between monotonic and realtime clock can be negative if
1062 * the system sets the REALTIME clock to 0 after boot.
1065 int measure_single_clock_offset(struct offset_sample
*sample
)
1067 uint64_t monotonic_avg
, monotonic
[2], measure_delta
, realtime
;
1068 uint64_t tcf
= trace_clock_freq();
1069 struct timespec rts
= { 0, 0 };
1072 monotonic
[0] = trace_clock_read64();
1073 ret
= lttng_clock_gettime(CLOCK_REALTIME
, &rts
);
1077 monotonic
[1] = trace_clock_read64();
1078 measure_delta
= monotonic
[1] - monotonic
[0];
1079 if (measure_delta
> sample
->measure_delta
) {
1081 * Discard value if it took longer to read than the best
1086 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
1087 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
1088 if (tcf
== NSEC_PER_SEC
) {
1089 realtime
+= rts
.tv_nsec
;
1091 realtime
+= (uint64_t) rts
.tv_nsec
* tcf
/ NSEC_PER_SEC
;
1093 sample
->offset
= (int64_t) realtime
- monotonic_avg
;
1094 sample
->measure_delta
= measure_delta
;
1099 * Approximation of NTP time of day to clock monotonic correlation,
1100 * taken at start of trace. Keep the measurement that took the less time
1101 * to complete, thus removing imprecision caused by preemption.
1102 * May return a negative offset.
1105 int64_t measure_clock_offset(void)
1108 struct offset_sample offset_best_sample
= {
1110 .measure_delta
= UINT64_MAX
,
1113 for (i
= 0; i
< NR_CLOCK_OFFSET_SAMPLES
; i
++) {
1114 if (measure_single_clock_offset(&offset_best_sample
)) {
1118 return offset_best_sample
.offset
;
1122 int print_metadata_session_information(ust_registry_session
*registry
)
1125 struct ltt_session
*session
= NULL
;
1126 char creation_datetime
[ISO8601_STR_LEN
];
1129 session
= session_find_by_id(registry
->_tracing_id
);
1135 /* Print the trace name */
1136 ret
= lttng_metadata_printf(registry
, " trace_name = \"");
1142 * This is necessary since the creation time is present in the session
1143 * name when it is generated.
1145 if (session
->has_auto_generated_name
) {
1146 ret
= print_escaped_ctf_string(registry
, DEFAULT_SESSION_NAME
);
1148 ret
= print_escaped_ctf_string(registry
, session
->name
);
1154 ret
= lttng_metadata_printf(registry
, "\";\n");
1159 /* Prepare creation time */
1160 ret
= time_to_iso8601_str(session
->creation_time
, creation_datetime
,
1161 sizeof(creation_datetime
));
1166 /* Output the reste of the information */
1167 ret
= lttng_metadata_printf(registry
,
1168 " trace_creation_datetime = \"%s\";\n"
1169 " hostname = \"%s\";\n",
1170 creation_datetime
, session
->hostname
);
1177 session_put(session
);
1184 int print_metadata_app_information(ust_registry_session
*registry
)
1186 if (registry
->get_buffering_scheme() != LTTNG_BUFFER_PER_PID
) {
1190 const auto *per_pid_session
= static_cast<const ust_registry_session_per_pid
*>(registry
);
1192 char datetime
[ISO8601_STR_LEN
];
1193 int ret
= time_to_iso8601_str(
1194 per_pid_session
->_app_creation_time
, datetime
, sizeof(datetime
));
1199 ret
= lttng_metadata_printf(registry
,
1200 " tracer_patchlevel = %u;\n"
1202 " procname = \"%s\";\n"
1203 " vpid_datetime = \"%s\";\n",
1204 per_pid_session
->_tracer_patch_level_version
, (int) per_pid_session
->_vpid
,
1205 per_pid_session
->_procname
.c_str(), datetime
);
1210 * Should be called with session registry mutex held.
1212 int ust_metadata_session_statedump(ust_registry_session
*session
)
1214 char uuid_s
[LTTNG_UUID_STR_LEN
], clock_uuid_s
[LTTNG_UUID_STR_LEN
];
1217 LTTNG_ASSERT(session
);
1219 lttng_uuid_to_str(session
->_uuid
, uuid_s
);
1222 ret
= lttng_metadata_printf(session
,
1223 "/* CTF %u.%u */\n\n",
1230 ret
= lttng_metadata_printf(session
,
1231 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1232 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1233 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1234 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1235 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1236 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1237 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1243 " byte_order = %s;\n"
1244 " packet.header := struct {\n"
1245 " uint32_t magic;\n"
1246 " uint8_t uuid[16];\n"
1247 " uint32_t stream_id;\n"
1248 " uint64_t stream_instance_id;\n"
1251 session
->_uint8_t_alignment
,
1252 session
->_uint16_t_alignment
,
1253 session
->_uint32_t_alignment
,
1254 session
->_uint64_t_alignment
,
1255 session
->_bits_per_long
,
1256 session
->_long_alignment
,
1260 session
->_byte_order
== BIG_ENDIAN
? "be" : "le"
1266 ret
= lttng_metadata_printf(session
,
1268 " domain = \"ust\";\n"
1269 " tracer_name = \"lttng-ust\";\n"
1270 " tracer_major = %u;\n"
1271 " tracer_minor = %u;\n"
1272 " tracer_buffering_scheme = \"%s\";\n"
1273 " tracer_buffering_id = %u;\n"
1274 " architecture_bit_width = %u;\n",
1275 session
->_app_tracer_version_major
, session
->_app_tracer_version_minor
,
1276 session
->get_buffering_scheme() == LTTNG_BUFFER_PER_PID
? "pid" : "uid",
1277 session
->get_buffering_scheme() == LTTNG_BUFFER_PER_PID
?
1278 (int) static_cast<ust_registry_session_per_pid
*>(session
)->_vpid
:
1279 (int) static_cast<ust_registry_session_per_uid
*>(session
)->_tracing_uid
,
1280 session
->_bits_per_long
);
1285 ret
= print_metadata_session_information(session
);
1291 * If per-application registry, we can output extra information
1292 * about the application.
1294 ret
= print_metadata_app_information(session
);
1299 ret
= lttng_metadata_printf(session
,
1306 ret
= lttng_metadata_printf(session
,
1308 " name = \"%s\";\n",
1315 if (!trace_clock_uuid(clock_uuid_s
)) {
1316 ret
= lttng_metadata_printf(session
,
1317 " uuid = \"%s\";\n",
1325 ret
= lttng_metadata_printf(session
,
1326 " description = \"%s\";\n"
1327 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1328 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1329 " offset = %" PRId64
";\n"
1331 trace_clock_description(),
1333 measure_clock_offset()
1339 ret
= lttng_metadata_printf(session
,
1340 "typealias integer {\n"
1341 " size = 27; align = 1; signed = false;\n"
1342 " map = clock.%s.value;\n"
1343 "} := uint27_clock_monotonic_t;\n"
1345 "typealias integer {\n"
1346 " size = 32; align = %u; signed = false;\n"
1347 " map = clock.%s.value;\n"
1348 "} := uint32_clock_monotonic_t;\n"
1350 "typealias integer {\n"
1351 " size = 64; align = %u; signed = false;\n"
1352 " map = clock.%s.value;\n"
1353 "} := uint64_clock_monotonic_t;\n\n",
1355 session
->_uint32_t_alignment
,
1357 session
->_uint64_t_alignment
,
1364 ret
= _lttng_stream_packet_context_declare(session
);
1369 ret
= _lttng_event_header_declare(session
);