4 * LTTng-UST metadata generation
6 * Copyright (C) 2010-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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.
30 #include <common/common.h>
31 #include <common/time.h>
33 #include "ust-registry.h"
34 #include "ust-clock.h"
38 #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
41 #define NR_CLOCK_OFFSET_SAMPLES 10
43 struct offset_sample
{
44 int64_t offset
; /* correlation offset */
45 uint64_t measure_delta
; /* lower is better */
49 int _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
);
54 int fls(unsigned int x
)
60 if (!(x
& 0xFFFF0000U
)) {
64 if (!(x
& 0xFF000000U
)) {
68 if (!(x
& 0xF0000000U
)) {
72 if (!(x
& 0xC0000000U
)) {
76 if (!(x
& 0x80000000U
)) {
84 int get_count_order(unsigned int count
)
88 order
= fls(count
) - 1;
89 if (count
& (count
- 1))
95 * Returns offset where to write in metadata array, or negative error value on error.
98 ssize_t
metadata_reserve(struct ust_registry_session
*session
, size_t len
)
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
;
105 if (new_alloc_len
> (UINT32_MAX
>> 1))
107 if ((old_alloc_len
<< 1) > (UINT32_MAX
>> 1))
110 if (new_alloc_len
> old_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
);
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
;
123 ret
= session
->metadata_len
;
124 session
->metadata_len
+= len
;
129 int metadata_file_append(struct ust_registry_session
*session
,
130 const char *str
, size_t len
)
134 if (session
->metadata_fd
< 0) {
137 /* Write to metadata file */
138 written
= lttng_write(session
->metadata_fd
, str
, len
);
139 if (written
!= len
) {
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.
152 int lttng_metadata_printf(struct ust_registry_session
*session
,
153 const char *fmt
, ...)
162 ret
= vasprintf(&str
, fmt
, ap
);
168 offset
= metadata_reserve(session
, len
);
173 memcpy(&session
->metadata
[offset
], str
, len
);
174 ret
= metadata_file_append(session
, str
, len
);
176 PERROR("Error appending to metadata file");
179 DBG3("Append to metadata: \"%s\"", str
);
188 int print_tabs(struct ust_registry_session
*session
, size_t nesting
)
192 for (i
= 0; i
< nesting
; i
++) {
195 ret
= lttng_metadata_printf(session
, " ");
204 void sanitize_ctf_identifier(char *out
, const char *in
)
208 for (i
= 0; i
< LTTNG_UST_SYM_NAME_LEN
; i
++) {
221 /* Called with session registry mutex held. */
223 int ust_metadata_enum_statedump(struct ust_registry_session
*session
,
224 const char *enum_name
,
226 const struct ustctl_integer_type
*container_type
,
227 const char *field_name
, size_t *iter_field
, size_t nesting
)
229 struct ust_registry_enum
*reg_enum
;
230 const struct ustctl_enum_entry
*entries
;
234 char identifier
[LTTNG_UST_SYM_NAME_LEN
];
237 reg_enum
= ust_registry_lookup_enum_by_id(session
, enum_name
, enum_id
);
239 /* reg_enum can still be used because session registry mutex is held. */
244 entries
= reg_enum
->entries
;
245 nr_entries
= reg_enum
->nr_entries
;
247 ret
= print_tabs(session
, nesting
);
251 ret
= lttng_metadata_printf(session
,
252 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n",
253 container_type
->size
,
254 container_type
->alignment
,
255 container_type
->signedness
,
256 (container_type
->encoding
== ustctl_encode_none
)
258 : (container_type
->encoding
== ustctl_encode_UTF8
)
261 container_type
->base
);
266 /* Dump all entries */
267 for (i
= 0; i
< nr_entries
; i
++) {
268 const struct ustctl_enum_entry
*entry
= &entries
[i
];
271 ret
= print_tabs(session
, nesting
);
275 ret
= lttng_metadata_printf(session
,
280 len
= strlen(entry
->string
);
281 /* Escape the character '"' */
282 for (j
= 0; j
< len
; j
++) {
283 char c
= entry
->string
[j
];
287 ret
= lttng_metadata_printf(session
,
291 ret
= lttng_metadata_printf(session
,
295 ret
= lttng_metadata_printf(session
,
303 ret
= lttng_metadata_printf(session
, "\"");
308 if (entry
->u
.extra
.options
&
309 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
) {
310 ret
= lttng_metadata_printf(session
, ",\n");
315 ret
= lttng_metadata_printf(session
,
321 if (entry
->start
.signedness
) {
322 ret
= lttng_metadata_printf(session
,
323 "%lld", (long long) entry
->start
.value
);
325 ret
= lttng_metadata_printf(session
,
326 "%llu", entry
->start
.value
);
332 if (entry
->start
.signedness
== entry
->end
.signedness
&&
333 entry
->start
.value
==
335 ret
= lttng_metadata_printf(session
, ",\n");
337 if (entry
->end
.signedness
) {
338 ret
= lttng_metadata_printf(session
,
340 (long long) entry
->end
.value
);
342 ret
= lttng_metadata_printf(session
,
353 sanitize_ctf_identifier(identifier
, field_name
);
354 ret
= print_tabs(session
, nesting
);
358 ret
= lttng_metadata_printf(session
, "} _%s;\n",
366 int _lttng_variant_statedump(struct ust_registry_session
*session
,
367 const struct ustctl_field
*fields
, size_t nr_fields
,
368 size_t *iter_field
, size_t nesting
)
370 const struct ustctl_field
*variant
= &fields
[*iter_field
];
371 uint32_t nr_choices
, i
;
373 char identifier
[LTTNG_UST_SYM_NAME_LEN
];
375 if (variant
->type
.atype
!= ustctl_atype_variant
) {
379 nr_choices
= variant
->type
.u
.variant
.nr_choices
;
381 sanitize_ctf_identifier(identifier
, variant
->type
.u
.variant
.tag_name
);
382 ret
= print_tabs(session
, nesting
);
386 ret
= lttng_metadata_printf(session
,
393 for (i
= 0; i
< nr_choices
; i
++) {
394 if (*iter_field
>= nr_fields
) {
398 ret
= _lttng_field_statedump(session
,
400 iter_field
, nesting
+ 1);
405 sanitize_ctf_identifier(identifier
, variant
->name
);
406 ret
= print_tabs(session
, nesting
);
410 ret
= lttng_metadata_printf(session
,
421 int _lttng_field_statedump(struct ust_registry_session
*session
,
422 const struct ustctl_field
*fields
, size_t nr_fields
,
423 size_t *iter_field
, size_t nesting
)
426 const char *bo_be
= " byte_order = be;";
427 const char *bo_le
= " byte_order = le;";
428 const char *bo_native
= "";
429 const char *bo_reverse
;
430 const struct ustctl_field
*field
;
432 if (*iter_field
>= nr_fields
) {
436 field
= &fields
[*iter_field
];
438 if (session
->byte_order
== BIG_ENDIAN
) {
444 switch (field
->type
.atype
) {
445 case ustctl_atype_integer
:
446 ret
= print_tabs(session
, nesting
);
450 ret
= lttng_metadata_printf(session
,
451 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
452 field
->type
.u
.basic
.integer
.size
,
453 field
->type
.u
.basic
.integer
.alignment
,
454 field
->type
.u
.basic
.integer
.signedness
,
455 (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_none
)
457 : (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
460 field
->type
.u
.basic
.integer
.base
,
461 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
465 case ustctl_atype_enum
:
466 ret
= ust_metadata_enum_statedump(session
,
467 field
->type
.u
.basic
.enumeration
.name
,
468 field
->type
.u
.basic
.enumeration
.id
,
469 &field
->type
.u
.basic
.enumeration
.container_type
,
470 field
->name
, iter_field
, nesting
);
472 case ustctl_atype_float
:
473 ret
= print_tabs(session
, nesting
);
477 ret
= lttng_metadata_printf(session
,
478 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
479 field
->type
.u
.basic
._float
.exp_dig
,
480 field
->type
.u
.basic
._float
.mant_dig
,
481 field
->type
.u
.basic
._float
.alignment
,
482 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
486 case ustctl_atype_array
:
488 const struct ustctl_basic_type
*elem_type
;
490 ret
= print_tabs(session
, nesting
);
494 elem_type
= &field
->type
.u
.array
.elem_type
;
495 ret
= lttng_metadata_printf(session
,
496 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
497 elem_type
->u
.basic
.integer
.size
,
498 elem_type
->u
.basic
.integer
.alignment
,
499 elem_type
->u
.basic
.integer
.signedness
,
500 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
502 : (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
505 elem_type
->u
.basic
.integer
.base
,
506 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
507 field
->name
, field
->type
.u
.array
.length
);
511 case ustctl_atype_sequence
:
513 const struct ustctl_basic_type
*elem_type
;
514 const struct ustctl_basic_type
*length_type
;
516 elem_type
= &field
->type
.u
.sequence
.elem_type
;
517 length_type
= &field
->type
.u
.sequence
.length_type
;
518 ret
= print_tabs(session
, nesting
);
522 ret
= lttng_metadata_printf(session
,
523 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
524 length_type
->u
.basic
.integer
.size
,
525 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
526 length_type
->u
.basic
.integer
.signedness
,
527 (length_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
529 : ((length_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
532 length_type
->u
.basic
.integer
.base
,
533 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
539 ret
= print_tabs(session
, nesting
);
543 ret
= lttng_metadata_printf(session
,
544 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
545 elem_type
->u
.basic
.integer
.size
,
546 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
547 elem_type
->u
.basic
.integer
.signedness
,
548 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
550 : ((elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
553 elem_type
->u
.basic
.integer
.base
,
554 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
561 case ustctl_atype_string
:
562 /* Default encoding is UTF8 */
563 ret
= print_tabs(session
, nesting
);
567 ret
= lttng_metadata_printf(session
,
569 field
->type
.u
.basic
.string
.encoding
== ustctl_encode_ASCII
?
570 " { encoding = ASCII; }" : "",
574 case ustctl_atype_variant
:
575 ret
= _lttng_variant_statedump(session
, fields
, nr_fields
, iter_field
, nesting
);
580 case ustctl_atype_struct
:
581 ret
= print_tabs(session
, nesting
);
585 ret
= lttng_metadata_printf(session
,
598 int _lttng_context_metadata_statedump(struct ust_registry_session
*session
,
599 size_t nr_ctx_fields
,
600 struct ustctl_field
*ctx
)
608 if (i
>= nr_ctx_fields
) {
611 ret
= _lttng_field_statedump(session
, ctx
,
612 nr_ctx_fields
, &i
, 2);
621 int _lttng_fields_metadata_statedump(struct ust_registry_session
*session
,
622 struct ust_registry_event
*event
)
628 if (i
>= event
->nr_fields
) {
631 ret
= _lttng_field_statedump(session
, event
->fields
,
632 event
->nr_fields
, &i
, 2);
641 * Should be called with session registry mutex held.
643 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
644 struct ust_registry_channel
*chan
,
645 struct ust_registry_event
*event
)
649 /* Don't dump metadata events */
650 if (chan
->chan_id
== -1U)
653 ret
= lttng_metadata_printf(session
,
657 " stream_id = %u;\n",
664 ret
= lttng_metadata_printf(session
,
666 event
->loglevel_value
);
670 if (event
->model_emf_uri
) {
671 ret
= lttng_metadata_printf(session
,
672 " model.emf.uri = \"%s\";\n",
673 event
->model_emf_uri
);
678 ret
= lttng_metadata_printf(session
,
679 " fields := struct {\n"
684 ret
= _lttng_fields_metadata_statedump(session
, event
);
688 ret
= lttng_metadata_printf(session
,
693 event
->metadata_dumped
= 1;
700 * Should be called with session registry mutex held.
702 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
703 struct ust_registry_channel
*chan
)
707 /* Don't dump metadata events */
708 if (chan
->chan_id
== -1U)
711 if (!chan
->header_type
)
714 ret
= lttng_metadata_printf(session
,
717 " event.header := %s;\n"
718 " packet.context := struct packet_context;\n",
720 chan
->header_type
== USTCTL_CHANNEL_HEADER_COMPACT
?
721 "struct event_header_compact" :
722 "struct event_header_large");
726 if (chan
->ctx_fields
) {
727 ret
= lttng_metadata_printf(session
,
728 " event.context := struct {\n");
732 ret
= _lttng_context_metadata_statedump(session
,
737 if (chan
->ctx_fields
) {
738 ret
= lttng_metadata_printf(session
,
744 ret
= lttng_metadata_printf(session
,
746 /* Flag success of metadata dump. */
747 chan
->metadata_dumped
= 1;
754 int _lttng_stream_packet_context_declare(struct ust_registry_session
*session
)
756 return lttng_metadata_printf(session
,
757 "struct packet_context {\n"
758 " uint64_clock_monotonic_t timestamp_begin;\n"
759 " uint64_clock_monotonic_t timestamp_end;\n"
760 " uint64_t content_size;\n"
761 " uint64_t packet_size;\n"
762 " uint64_t packet_seq_num;\n"
763 " unsigned long events_discarded;\n"
764 " uint32_t cpu_id;\n"
772 * id 31 is reserved to indicate an extended header.
775 * id: range: 0 - 65534.
776 * id 65535 is reserved to indicate an extended header.
779 int _lttng_event_header_declare(struct ust_registry_session
*session
)
781 return lttng_metadata_printf(session
,
782 "struct event_header_compact {\n"
783 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
786 " uint27_clock_monotonic_t timestamp;\n"
790 " uint64_clock_monotonic_t timestamp;\n"
795 "struct event_header_large {\n"
796 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
799 " uint32_clock_monotonic_t timestamp;\n"
803 " uint64_clock_monotonic_t timestamp;\n"
807 session
->uint32_t_alignment
,
808 session
->uint16_t_alignment
813 * The offset between monotonic and realtime clock can be negative if
814 * the system sets the REALTIME clock to 0 after boot.
817 int measure_single_clock_offset(struct offset_sample
*sample
)
819 uint64_t monotonic_avg
, monotonic
[2], measure_delta
, realtime
;
820 uint64_t tcf
= trace_clock_freq();
821 struct timespec rts
= { 0, 0 };
824 monotonic
[0] = trace_clock_read64();
825 ret
= lttng_clock_gettime(CLOCK_REALTIME
, &rts
);
829 monotonic
[1] = trace_clock_read64();
830 measure_delta
= monotonic
[1] - monotonic
[0];
831 if (measure_delta
> sample
->measure_delta
) {
833 * Discard value if it took longer to read than the best
838 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
839 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
840 if (tcf
== NSEC_PER_SEC
) {
841 realtime
+= rts
.tv_nsec
;
843 realtime
+= (uint64_t) rts
.tv_nsec
* tcf
/ NSEC_PER_SEC
;
845 sample
->offset
= (int64_t) realtime
- monotonic_avg
;
846 sample
->measure_delta
= measure_delta
;
851 * Approximation of NTP time of day to clock monotonic correlation,
852 * taken at start of trace. Keep the measurement that took the less time
853 * to complete, thus removing imprecision caused by preemption.
854 * May return a negative offset.
857 int64_t measure_clock_offset(void)
860 struct offset_sample offset_best_sample
= {
862 .measure_delta
= UINT64_MAX
,
865 for (i
= 0; i
< NR_CLOCK_OFFSET_SAMPLES
; i
++) {
866 if (measure_single_clock_offset(&offset_best_sample
)) {
870 return offset_best_sample
.offset
;
874 * Should be called with session registry mutex held.
876 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
881 unsigned char *uuid_c
;
882 char uuid_s
[UUID_STR_LEN
],
883 clock_uuid_s
[UUID_STR_LEN
];
885 char hostname
[HOST_NAME_MAX
];
889 uuid_c
= session
->uuid
;
891 snprintf(uuid_s
, sizeof(uuid_s
),
892 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
893 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
894 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
895 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
896 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
899 ret
= lttng_metadata_printf(session
,
900 "/* CTF %u.%u */\n\n",
907 ret
= lttng_metadata_printf(session
,
908 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
909 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
910 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
911 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
912 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
913 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
914 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
920 " byte_order = %s;\n"
921 " packet.header := struct {\n"
923 " uint8_t uuid[16];\n"
924 " uint32_t stream_id;\n"
925 " uint64_t stream_instance_id;\n"
928 session
->uint8_t_alignment
,
929 session
->uint16_t_alignment
,
930 session
->uint32_t_alignment
,
931 session
->uint64_t_alignment
,
932 session
->bits_per_long
,
933 session
->long_alignment
,
937 session
->byte_order
== BIG_ENDIAN
? "be" : "le"
942 /* ignore error, just use empty string if error. */
944 ret
= gethostname(hostname
, sizeof(hostname
));
945 if (ret
&& errno
== ENAMETOOLONG
)
946 hostname
[HOST_NAME_MAX
- 1] = '\0';
947 ret
= lttng_metadata_printf(session
,
949 " hostname = \"%s\";\n"
950 " domain = \"ust\";\n"
951 " tracer_name = \"lttng-ust\";\n"
952 " tracer_major = %u;\n"
953 " tracer_minor = %u;\n",
962 * If per-application registry, we can output extra information
963 * about the application.
966 ret
= lttng_metadata_printf(session
,
967 " tracer_patchlevel = %u;\n"
969 " procname = \"%s\";\n",
970 app
->version
.patchlevel
,
978 ret
= lttng_metadata_printf(session
,
985 ret
= lttng_metadata_printf(session
,
993 if (!trace_clock_uuid(clock_uuid_s
)) {
994 ret
= lttng_metadata_printf(session
,
1002 ret
= lttng_metadata_printf(session
,
1003 " description = \"%s\";\n"
1004 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1005 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1006 " offset = %" PRId64
";\n"
1008 trace_clock_description(),
1010 measure_clock_offset()
1015 ret
= lttng_metadata_printf(session
,
1016 "typealias integer {\n"
1017 " size = 27; align = 1; signed = false;\n"
1018 " map = clock.%s.value;\n"
1019 "} := uint27_clock_monotonic_t;\n"
1021 "typealias integer {\n"
1022 " size = 32; align = %u; signed = false;\n"
1023 " map = clock.%s.value;\n"
1024 "} := uint32_clock_monotonic_t;\n"
1026 "typealias integer {\n"
1027 " size = 64; align = %u; signed = false;\n"
1028 " map = clock.%s.value;\n"
1029 "} := uint64_clock_monotonic_t;\n\n",
1031 session
->uint32_t_alignment
,
1033 session
->uint64_t_alignment
,
1039 ret
= _lttng_stream_packet_context_declare(session
);
1043 ret
= _lttng_event_header_declare(session
);