2 * Copyright (C) 2010-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
16 #include <common/common.h>
17 #include <common/time.h>
19 #include "ust-registry.h"
20 #include "ust-clock.h"
24 #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
27 #define NR_CLOCK_OFFSET_SAMPLES 10
29 struct offset_sample
{
30 int64_t offset
; /* correlation offset */
31 uint64_t measure_delta
; /* lower is better */
35 int _lttng_field_statedump(struct ust_registry_session
*session
,
36 const struct ustctl_field
*fields
, size_t nr_fields
,
37 size_t *iter_field
, size_t nesting
);
40 int fls(unsigned int x
)
46 if (!(x
& 0xFFFF0000U
)) {
50 if (!(x
& 0xFF000000U
)) {
54 if (!(x
& 0xF0000000U
)) {
58 if (!(x
& 0xC0000000U
)) {
62 if (!(x
& 0x80000000U
)) {
69 int get_count_order(unsigned int count
)
73 order
= fls(count
) - 1;
74 if (count
& (count
- 1)) {
82 * Returns offset where to write in metadata array, or negative error value on error.
85 ssize_t
metadata_reserve(struct ust_registry_session
*session
, size_t len
)
87 size_t new_len
= session
->metadata_len
+ len
;
88 size_t new_alloc_len
= new_len
;
89 size_t old_alloc_len
= session
->metadata_alloc_len
;
92 if (new_alloc_len
> (UINT32_MAX
>> 1))
94 if ((old_alloc_len
<< 1) > (UINT32_MAX
>> 1))
97 if (new_alloc_len
> old_alloc_len
) {
101 max_t(size_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
102 newptr
= realloc(session
->metadata
, new_alloc_len
);
105 session
->metadata
= newptr
;
106 /* We zero directly the memory from start of allocation. */
107 memset(&session
->metadata
[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
108 session
->metadata_alloc_len
= new_alloc_len
;
110 ret
= session
->metadata_len
;
111 session
->metadata_len
+= len
;
116 int metadata_file_append(struct ust_registry_session
*session
,
117 const char *str
, size_t len
)
121 if (session
->metadata_fd
< 0) {
124 /* Write to metadata file */
125 written
= lttng_write(session
->metadata_fd
, str
, len
);
126 if (written
!= len
) {
133 * We have exclusive access to our metadata buffer (protected by the
134 * ust_lock), so we can do racy operations such as looking for
135 * remaining space left in packet and write, since mutual exclusion
136 * protects us from concurrent writes.
139 int lttng_metadata_printf(struct ust_registry_session
*session
,
140 const char *fmt
, ...)
149 ret
= vasprintf(&str
, fmt
, ap
);
155 offset
= metadata_reserve(session
, len
);
160 memcpy(&session
->metadata
[offset
], str
, len
);
161 ret
= metadata_file_append(session
, str
, len
);
163 PERROR("Error appending to metadata file");
166 DBG3("Append to metadata: \"%s\"", str
);
175 int print_tabs(struct ust_registry_session
*session
, size_t nesting
)
179 for (i
= 0; i
< nesting
; i
++) {
182 ret
= lttng_metadata_printf(session
, " ");
191 void sanitize_ctf_identifier(char *out
, const char *in
)
195 for (i
= 0; i
< LTTNG_UST_SYM_NAME_LEN
; i
++) {
209 int print_escaped_ctf_string(struct ust_registry_session
*session
, const char *string
)
217 while (cur
!= '\0') {
220 ret
= lttng_metadata_printf(session
, "%s", "\\n");
224 ret
= lttng_metadata_printf(session
, "%c", '\\');
228 /* We still print the current char */
231 ret
= lttng_metadata_printf(session
, "%c", cur
);
245 /* Called with session registry mutex held. */
247 int ust_metadata_enum_statedump(struct ust_registry_session
*session
,
248 const char *enum_name
,
250 const struct ustctl_integer_type
*container_type
,
251 const char *field_name
, size_t *iter_field
, size_t nesting
)
253 struct ust_registry_enum
*reg_enum
;
254 const struct ustctl_enum_entry
*entries
;
258 char identifier
[LTTNG_UST_SYM_NAME_LEN
];
261 reg_enum
= ust_registry_lookup_enum_by_id(session
, enum_name
, enum_id
);
263 /* reg_enum can still be used because session registry mutex is held. */
268 entries
= reg_enum
->entries
;
269 nr_entries
= reg_enum
->nr_entries
;
271 ret
= print_tabs(session
, nesting
);
275 ret
= lttng_metadata_printf(session
,
276 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n",
277 container_type
->size
,
278 container_type
->alignment
,
279 container_type
->signedness
,
280 (container_type
->encoding
== ustctl_encode_none
)
282 : (container_type
->encoding
== ustctl_encode_UTF8
)
285 container_type
->base
);
290 /* Dump all entries */
291 for (i
= 0; i
< nr_entries
; i
++) {
292 const struct ustctl_enum_entry
*entry
= &entries
[i
];
295 ret
= print_tabs(session
, nesting
);
299 ret
= lttng_metadata_printf(session
,
304 len
= strlen(entry
->string
);
305 /* Escape the character '"' */
306 for (j
= 0; j
< len
; j
++) {
307 char c
= entry
->string
[j
];
311 ret
= lttng_metadata_printf(session
,
315 ret
= lttng_metadata_printf(session
,
319 ret
= lttng_metadata_printf(session
,
327 ret
= lttng_metadata_printf(session
, "\"");
332 if (entry
->u
.extra
.options
&
333 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
) {
334 ret
= lttng_metadata_printf(session
, ",\n");
339 ret
= lttng_metadata_printf(session
,
345 if (entry
->start
.signedness
) {
346 ret
= lttng_metadata_printf(session
,
347 "%lld", (long long) entry
->start
.value
);
349 ret
= lttng_metadata_printf(session
,
350 "%llu", entry
->start
.value
);
356 if (entry
->start
.signedness
== entry
->end
.signedness
&&
357 entry
->start
.value
==
359 ret
= lttng_metadata_printf(session
, ",\n");
361 if (entry
->end
.signedness
) {
362 ret
= lttng_metadata_printf(session
,
364 (long long) entry
->end
.value
);
366 ret
= lttng_metadata_printf(session
,
377 sanitize_ctf_identifier(identifier
, field_name
);
378 ret
= print_tabs(session
, nesting
);
382 ret
= lttng_metadata_printf(session
, "} _%s;\n",
390 int _lttng_variant_statedump(struct ust_registry_session
*session
,
391 const struct ustctl_field
*fields
, size_t nr_fields
,
392 size_t *iter_field
, size_t nesting
)
394 const struct ustctl_field
*variant
= &fields
[*iter_field
];
395 uint32_t nr_choices
, i
;
397 char identifier
[LTTNG_UST_SYM_NAME_LEN
];
399 if (variant
->type
.atype
!= ustctl_atype_variant
) {
403 nr_choices
= variant
->type
.u
.variant
.nr_choices
;
405 sanitize_ctf_identifier(identifier
, variant
->type
.u
.variant
.tag_name
);
406 ret
= print_tabs(session
, nesting
);
410 ret
= lttng_metadata_printf(session
,
417 for (i
= 0; i
< nr_choices
; i
++) {
418 if (*iter_field
>= nr_fields
) {
422 ret
= _lttng_field_statedump(session
,
424 iter_field
, nesting
+ 1);
429 sanitize_ctf_identifier(identifier
, variant
->name
);
430 ret
= print_tabs(session
, nesting
);
434 ret
= lttng_metadata_printf(session
,
445 int _lttng_field_statedump(struct ust_registry_session
*session
,
446 const struct ustctl_field
*fields
, size_t nr_fields
,
447 size_t *iter_field
, size_t nesting
)
450 const char *bo_be
= " byte_order = be;";
451 const char *bo_le
= " byte_order = le;";
452 const char *bo_native
= "";
453 const char *bo_reverse
;
454 const struct ustctl_field
*field
;
456 if (*iter_field
>= nr_fields
) {
460 field
= &fields
[*iter_field
];
462 if (session
->byte_order
== BIG_ENDIAN
) {
468 switch (field
->type
.atype
) {
469 case ustctl_atype_integer
:
470 ret
= print_tabs(session
, nesting
);
474 ret
= lttng_metadata_printf(session
,
475 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
476 field
->type
.u
.basic
.integer
.size
,
477 field
->type
.u
.basic
.integer
.alignment
,
478 field
->type
.u
.basic
.integer
.signedness
,
479 (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_none
)
481 : (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
484 field
->type
.u
.basic
.integer
.base
,
485 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
489 case ustctl_atype_enum
:
490 ret
= ust_metadata_enum_statedump(session
,
491 field
->type
.u
.basic
.enumeration
.name
,
492 field
->type
.u
.basic
.enumeration
.id
,
493 &field
->type
.u
.basic
.enumeration
.container_type
,
494 field
->name
, iter_field
, nesting
);
496 case ustctl_atype_float
:
497 ret
= print_tabs(session
, nesting
);
501 ret
= lttng_metadata_printf(session
,
502 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
503 field
->type
.u
.basic
._float
.exp_dig
,
504 field
->type
.u
.basic
._float
.mant_dig
,
505 field
->type
.u
.basic
._float
.alignment
,
506 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
510 case ustctl_atype_array
:
512 const struct ustctl_basic_type
*elem_type
;
514 ret
= print_tabs(session
, nesting
);
518 elem_type
= &field
->type
.u
.array
.elem_type
;
519 ret
= lttng_metadata_printf(session
,
520 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
521 elem_type
->u
.basic
.integer
.size
,
522 elem_type
->u
.basic
.integer
.alignment
,
523 elem_type
->u
.basic
.integer
.signedness
,
524 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
526 : (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
529 elem_type
->u
.basic
.integer
.base
,
530 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
531 field
->name
, field
->type
.u
.array
.length
);
535 case ustctl_atype_sequence
:
537 const struct ustctl_basic_type
*elem_type
;
538 const struct ustctl_basic_type
*length_type
;
540 elem_type
= &field
->type
.u
.sequence
.elem_type
;
541 length_type
= &field
->type
.u
.sequence
.length_type
;
542 ret
= print_tabs(session
, nesting
);
546 ret
= lttng_metadata_printf(session
,
547 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
548 length_type
->u
.basic
.integer
.size
,
549 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
550 length_type
->u
.basic
.integer
.signedness
,
551 (length_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
553 : ((length_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
556 length_type
->u
.basic
.integer
.base
,
557 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
563 ret
= print_tabs(session
, nesting
);
567 ret
= lttng_metadata_printf(session
,
568 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
569 elem_type
->u
.basic
.integer
.size
,
570 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
571 elem_type
->u
.basic
.integer
.signedness
,
572 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
574 : ((elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
577 elem_type
->u
.basic
.integer
.base
,
578 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
585 case ustctl_atype_string
:
586 /* Default encoding is UTF8 */
587 ret
= print_tabs(session
, nesting
);
591 ret
= lttng_metadata_printf(session
,
593 field
->type
.u
.basic
.string
.encoding
== ustctl_encode_ASCII
?
594 " { encoding = ASCII; }" : "",
598 case ustctl_atype_variant
:
599 ret
= _lttng_variant_statedump(session
, fields
, nr_fields
, iter_field
, nesting
);
604 case ustctl_atype_struct
:
605 ret
= print_tabs(session
, nesting
);
609 ret
= lttng_metadata_printf(session
,
622 int _lttng_context_metadata_statedump(struct ust_registry_session
*session
,
623 size_t nr_ctx_fields
,
624 struct ustctl_field
*ctx
)
632 if (i
>= nr_ctx_fields
) {
635 ret
= _lttng_field_statedump(session
, ctx
,
636 nr_ctx_fields
, &i
, 2);
645 int _lttng_fields_metadata_statedump(struct ust_registry_session
*session
,
646 struct ust_registry_event
*event
)
652 if (i
>= event
->nr_fields
) {
655 ret
= _lttng_field_statedump(session
, event
->fields
,
656 event
->nr_fields
, &i
, 2);
665 * Should be called with session registry mutex held.
667 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
668 struct ust_registry_channel
*chan
,
669 struct ust_registry_event
*event
)
673 /* Don't dump metadata events */
674 if (chan
->chan_id
== -1U)
677 ret
= lttng_metadata_printf(session
,
681 " stream_id = %u;\n",
688 ret
= lttng_metadata_printf(session
,
690 event
->loglevel_value
);
694 if (event
->model_emf_uri
) {
695 ret
= lttng_metadata_printf(session
,
696 " model.emf.uri = \"%s\";\n",
697 event
->model_emf_uri
);
702 ret
= lttng_metadata_printf(session
,
703 " fields := struct {\n"
708 ret
= _lttng_fields_metadata_statedump(session
, event
);
712 ret
= lttng_metadata_printf(session
,
717 event
->metadata_dumped
= 1;
724 * Should be called with session registry mutex held.
726 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
727 struct ust_registry_channel
*chan
)
731 /* Don't dump metadata events */
732 if (chan
->chan_id
== -1U)
735 if (!chan
->header_type
)
738 ret
= lttng_metadata_printf(session
,
741 " event.header := %s;\n"
742 " packet.context := struct packet_context;\n",
744 chan
->header_type
== USTCTL_CHANNEL_HEADER_COMPACT
?
745 "struct event_header_compact" :
746 "struct event_header_large");
750 if (chan
->ctx_fields
) {
751 ret
= lttng_metadata_printf(session
,
752 " event.context := struct {\n");
756 ret
= _lttng_context_metadata_statedump(session
,
761 if (chan
->ctx_fields
) {
762 ret
= lttng_metadata_printf(session
,
768 ret
= lttng_metadata_printf(session
,
770 /* Flag success of metadata dump. */
771 chan
->metadata_dumped
= 1;
778 int _lttng_stream_packet_context_declare(struct ust_registry_session
*session
)
780 return lttng_metadata_printf(session
,
781 "struct packet_context {\n"
782 " uint64_clock_monotonic_t timestamp_begin;\n"
783 " uint64_clock_monotonic_t timestamp_end;\n"
784 " uint64_t content_size;\n"
785 " uint64_t packet_size;\n"
786 " uint64_t packet_seq_num;\n"
787 " unsigned long events_discarded;\n"
788 " uint32_t cpu_id;\n"
796 * id 31 is reserved to indicate an extended header.
799 * id: range: 0 - 65534.
800 * id 65535 is reserved to indicate an extended header.
803 int _lttng_event_header_declare(struct ust_registry_session
*session
)
805 return lttng_metadata_printf(session
,
806 "struct event_header_compact {\n"
807 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
810 " uint27_clock_monotonic_t timestamp;\n"
814 " uint64_clock_monotonic_t timestamp;\n"
819 "struct event_header_large {\n"
820 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
823 " uint32_clock_monotonic_t timestamp;\n"
827 " uint64_clock_monotonic_t timestamp;\n"
831 session
->uint32_t_alignment
,
832 session
->uint16_t_alignment
837 * The offset between monotonic and realtime clock can be negative if
838 * the system sets the REALTIME clock to 0 after boot.
841 int measure_single_clock_offset(struct offset_sample
*sample
)
843 uint64_t monotonic_avg
, monotonic
[2], measure_delta
, realtime
;
844 uint64_t tcf
= trace_clock_freq();
845 struct timespec rts
= { 0, 0 };
848 monotonic
[0] = trace_clock_read64();
849 ret
= lttng_clock_gettime(CLOCK_REALTIME
, &rts
);
853 monotonic
[1] = trace_clock_read64();
854 measure_delta
= monotonic
[1] - monotonic
[0];
855 if (measure_delta
> sample
->measure_delta
) {
857 * Discard value if it took longer to read than the best
862 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
863 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
864 if (tcf
== NSEC_PER_SEC
) {
865 realtime
+= rts
.tv_nsec
;
867 realtime
+= (uint64_t) rts
.tv_nsec
* tcf
/ NSEC_PER_SEC
;
869 sample
->offset
= (int64_t) realtime
- monotonic_avg
;
870 sample
->measure_delta
= measure_delta
;
875 * Approximation of NTP time of day to clock monotonic correlation,
876 * taken at start of trace. Keep the measurement that took the less time
877 * to complete, thus removing imprecision caused by preemption.
878 * May return a negative offset.
881 int64_t measure_clock_offset(void)
884 struct offset_sample offset_best_sample
= {
886 .measure_delta
= UINT64_MAX
,
889 for (i
= 0; i
< NR_CLOCK_OFFSET_SAMPLES
; i
++) {
890 if (measure_single_clock_offset(&offset_best_sample
)) {
894 return offset_best_sample
.offset
;
898 int print_metadata_session_information(struct ust_registry_session
*registry
)
901 struct ltt_session
*session
= NULL
;
902 char creation_datetime
[ISO8601_STR_LEN
];
905 session
= session_find_by_id(registry
->tracing_id
);
911 /* Print the trace name */
912 ret
= lttng_metadata_printf(registry
, " trace_name = \"");
918 * This is necessary since the creation time is present in the session
919 * name when it is generated.
921 if (session
->has_auto_generated_name
) {
922 ret
= print_escaped_ctf_string(registry
, DEFAULT_SESSION_NAME
);
924 ret
= print_escaped_ctf_string(registry
, session
->name
);
930 ret
= lttng_metadata_printf(registry
, "\";\n");
935 /* Prepare creation time */
936 ret
= time_to_iso8601_str(session
->creation_time
, creation_datetime
,
937 sizeof(creation_datetime
));
942 /* Output the reste of the information */
943 ret
= lttng_metadata_printf(registry
,
944 " trace_creation_datetime = \"%s\";\n"
945 " hostname = \"%s\";\n",
946 creation_datetime
, session
->hostname
);
953 session_put(session
);
960 int print_metadata_app_information(struct ust_registry_session
*registry
,
964 char datetime
[ISO8601_STR_LEN
];
971 ret
= time_to_iso8601_str(
972 app
->registration_time
, datetime
, sizeof(datetime
));
977 ret
= lttng_metadata_printf(registry
,
978 " tracer_patchlevel = %u;\n"
980 " procname = \"%s\";\n"
981 " vpid_datetime = \"%s\";\n",
982 app
->version
.patchlevel
, (int) app
->pid
, app
->name
,
990 * Should be called with session registry mutex held.
992 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
997 char uuid_s
[LTTNG_UUID_STR_LEN
],
998 clock_uuid_s
[LTTNG_UUID_STR_LEN
];
1003 lttng_uuid_to_str(session
->uuid
, uuid_s
);
1006 ret
= lttng_metadata_printf(session
,
1007 "/* CTF %u.%u */\n\n",
1014 ret
= lttng_metadata_printf(session
,
1015 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1016 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1017 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1018 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1019 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1020 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1021 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1027 " byte_order = %s;\n"
1028 " packet.header := struct {\n"
1029 " uint32_t magic;\n"
1030 " uint8_t uuid[16];\n"
1031 " uint32_t stream_id;\n"
1032 " uint64_t stream_instance_id;\n"
1035 session
->uint8_t_alignment
,
1036 session
->uint16_t_alignment
,
1037 session
->uint32_t_alignment
,
1038 session
->uint64_t_alignment
,
1039 session
->bits_per_long
,
1040 session
->long_alignment
,
1044 session
->byte_order
== BIG_ENDIAN
? "be" : "le"
1049 ret
= lttng_metadata_printf(session
,
1051 " domain = \"ust\";\n"
1052 " tracer_name = \"lttng-ust\";\n"
1053 " tracer_major = %u;\n"
1054 " tracer_minor = %u;\n"
1055 " tracer_buffering_scheme = \"%s\";\n"
1056 " tracer_buffering_id = %u;\n"
1057 " architecture_bit_width = %u;\n",
1060 app
? "pid" : "uid",
1061 app
? (int) app
->pid
: (int) session
->tracing_uid
,
1062 session
->bits_per_long
);
1067 ret
= print_metadata_session_information(session
);
1073 * If per-application registry, we can output extra information
1074 * about the application.
1076 ret
= print_metadata_app_information(session
, app
);
1081 ret
= lttng_metadata_printf(session
,
1088 ret
= lttng_metadata_printf(session
,
1090 " name = \"%s\";\n",
1096 if (!trace_clock_uuid(clock_uuid_s
)) {
1097 ret
= lttng_metadata_printf(session
,
1098 " uuid = \"%s\";\n",
1105 ret
= lttng_metadata_printf(session
,
1106 " description = \"%s\";\n"
1107 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1108 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1109 " offset = %" PRId64
";\n"
1111 trace_clock_description(),
1113 measure_clock_offset()
1118 ret
= lttng_metadata_printf(session
,
1119 "typealias integer {\n"
1120 " size = 27; align = 1; signed = false;\n"
1121 " map = clock.%s.value;\n"
1122 "} := uint27_clock_monotonic_t;\n"
1124 "typealias integer {\n"
1125 " size = 32; align = %u; signed = false;\n"
1126 " map = clock.%s.value;\n"
1127 "} := uint32_clock_monotonic_t;\n"
1129 "typealias integer {\n"
1130 " size = 64; align = %u; signed = false;\n"
1131 " map = clock.%s.value;\n"
1132 "} := uint64_clock_monotonic_t;\n\n",
1134 session
->uint32_t_alignment
,
1136 session
->uint64_t_alignment
,
1142 ret
= _lttng_stream_packet_context_declare(session
);
1146 ret
= _lttng_event_header_declare(session
);