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
)) {
83 int get_count_order(unsigned int count
)
87 order
= fls(count
) - 1;
88 if (count
& (count
- 1))
94 * Returns offset where to write in metadata array, or negative error value on error.
97 ssize_t
metadata_reserve(struct ust_registry_session
*session
, size_t len
)
99 size_t new_len
= session
->metadata_len
+ len
;
100 size_t new_alloc_len
= new_len
;
101 size_t old_alloc_len
= session
->metadata_alloc_len
;
104 if (new_alloc_len
> (UINT32_MAX
>> 1))
106 if ((old_alloc_len
<< 1) > (UINT32_MAX
>> 1))
109 if (new_alloc_len
> old_alloc_len
) {
113 max_t(size_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
114 newptr
= realloc(session
->metadata
, new_alloc_len
);
117 session
->metadata
= newptr
;
118 /* We zero directly the memory from start of allocation. */
119 memset(&session
->metadata
[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
120 session
->metadata_alloc_len
= new_alloc_len
;
122 ret
= session
->metadata_len
;
123 session
->metadata_len
+= len
;
128 int metadata_file_append(struct ust_registry_session
*session
,
129 const char *str
, size_t len
)
133 if (session
->metadata_fd
< 0) {
136 /* Write to metadata file */
137 written
= lttng_write(session
->metadata_fd
, str
, len
);
138 if (written
!= len
) {
145 * We have exclusive access to our metadata buffer (protected by the
146 * ust_lock), so we can do racy operations such as looking for
147 * remaining space left in packet and write, since mutual exclusion
148 * protects us from concurrent writes.
151 int lttng_metadata_printf(struct ust_registry_session
*session
,
152 const char *fmt
, ...)
161 ret
= vasprintf(&str
, fmt
, ap
);
167 offset
= metadata_reserve(session
, len
);
172 memcpy(&session
->metadata
[offset
], str
, len
);
173 ret
= metadata_file_append(session
, str
, len
);
175 PERROR("Error appending to metadata file");
178 DBG3("Append to metadata: \"%s\"", str
);
187 int print_tabs(struct ust_registry_session
*session
, size_t nesting
)
191 for (i
= 0; i
< nesting
; i
++) {
194 ret
= lttng_metadata_printf(session
, " ");
203 void sanitize_ctf_identifier(char *out
, const char *in
)
207 for (i
= 0; i
< LTTNG_UST_SYM_NAME_LEN
; i
++) {
220 /* Called with session registry mutex held. */
222 int ust_metadata_enum_statedump(struct ust_registry_session
*session
,
223 const char *enum_name
,
225 const struct ustctl_integer_type
*container_type
,
226 const char *field_name
, size_t *iter_field
, size_t nesting
)
228 struct ust_registry_enum
*reg_enum
;
229 const struct ustctl_enum_entry
*entries
;
233 char identifier
[LTTNG_UST_SYM_NAME_LEN
];
236 reg_enum
= ust_registry_lookup_enum_by_id(session
, enum_name
, enum_id
);
238 /* reg_enum can still be used because session registry mutex is held. */
243 entries
= reg_enum
->entries
;
244 nr_entries
= reg_enum
->nr_entries
;
246 ret
= print_tabs(session
, nesting
);
250 ret
= lttng_metadata_printf(session
,
251 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n",
252 container_type
->size
,
253 container_type
->alignment
,
254 container_type
->signedness
,
255 (container_type
->encoding
== ustctl_encode_none
)
257 : (container_type
->encoding
== ustctl_encode_UTF8
)
260 container_type
->base
);
265 /* Dump all entries */
266 for (i
= 0; i
< nr_entries
; i
++) {
267 const struct ustctl_enum_entry
*entry
= &entries
[i
];
270 ret
= print_tabs(session
, nesting
);
274 ret
= lttng_metadata_printf(session
,
279 len
= strlen(entry
->string
);
280 /* Escape the character '"' */
281 for (j
= 0; j
< len
; j
++) {
282 char c
= entry
->string
[j
];
286 ret
= lttng_metadata_printf(session
,
290 ret
= lttng_metadata_printf(session
,
294 ret
= lttng_metadata_printf(session
,
302 ret
= lttng_metadata_printf(session
, "\"");
307 if (entry
->u
.extra
.options
&
308 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO
) {
309 ret
= lttng_metadata_printf(session
, ",\n");
314 ret
= lttng_metadata_printf(session
,
320 if (entry
->start
.signedness
) {
321 ret
= lttng_metadata_printf(session
,
322 "%lld", (long long) entry
->start
.value
);
324 ret
= lttng_metadata_printf(session
,
325 "%llu", entry
->start
.value
);
331 if (entry
->start
.signedness
== entry
->end
.signedness
&&
332 entry
->start
.value
==
334 ret
= lttng_metadata_printf(session
, ",\n");
336 if (entry
->end
.signedness
) {
337 ret
= lttng_metadata_printf(session
,
339 (long long) entry
->end
.value
);
341 ret
= lttng_metadata_printf(session
,
352 sanitize_ctf_identifier(identifier
, field_name
);
353 ret
= print_tabs(session
, nesting
);
357 ret
= lttng_metadata_printf(session
, "} _%s;\n",
365 int _lttng_variant_statedump(struct ust_registry_session
*session
,
366 const struct ustctl_field
*fields
, size_t nr_fields
,
367 size_t *iter_field
, size_t nesting
)
369 const struct ustctl_field
*variant
= &fields
[*iter_field
];
370 uint32_t nr_choices
, i
;
372 char identifier
[LTTNG_UST_SYM_NAME_LEN
];
374 if (variant
->type
.atype
!= ustctl_atype_variant
) {
378 nr_choices
= variant
->type
.u
.variant
.nr_choices
;
380 sanitize_ctf_identifier(identifier
, variant
->type
.u
.variant
.tag_name
);
381 ret
= print_tabs(session
, nesting
);
385 ret
= lttng_metadata_printf(session
,
392 for (i
= 0; i
< nr_choices
; i
++) {
393 if (*iter_field
>= nr_fields
) {
397 ret
= _lttng_field_statedump(session
,
399 iter_field
, nesting
+ 1);
404 sanitize_ctf_identifier(identifier
, variant
->name
);
405 ret
= print_tabs(session
, nesting
);
406 ret
= lttng_metadata_printf(session
,
417 int _lttng_field_statedump(struct ust_registry_session
*session
,
418 const struct ustctl_field
*fields
, size_t nr_fields
,
419 size_t *iter_field
, size_t nesting
)
422 const char *bo_be
= " byte_order = be;";
423 const char *bo_le
= " byte_order = le;";
424 const char *bo_native
= "";
425 const char *bo_reverse
;
426 const struct ustctl_field
*field
;
428 if (*iter_field
>= nr_fields
) {
432 field
= &fields
[*iter_field
];
434 if (session
->byte_order
== BIG_ENDIAN
) {
440 switch (field
->type
.atype
) {
441 case ustctl_atype_integer
:
442 ret
= print_tabs(session
, nesting
);
446 ret
= lttng_metadata_printf(session
,
447 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
448 field
->type
.u
.basic
.integer
.size
,
449 field
->type
.u
.basic
.integer
.alignment
,
450 field
->type
.u
.basic
.integer
.signedness
,
451 (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_none
)
453 : (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
456 field
->type
.u
.basic
.integer
.base
,
457 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
461 case ustctl_atype_enum
:
462 ret
= ust_metadata_enum_statedump(session
,
463 field
->type
.u
.basic
.enumeration
.name
,
464 field
->type
.u
.basic
.enumeration
.id
,
465 &field
->type
.u
.basic
.enumeration
.container_type
,
466 field
->name
, iter_field
, nesting
);
468 case ustctl_atype_float
:
469 ret
= print_tabs(session
, nesting
);
473 ret
= lttng_metadata_printf(session
,
474 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
475 field
->type
.u
.basic
._float
.exp_dig
,
476 field
->type
.u
.basic
._float
.mant_dig
,
477 field
->type
.u
.basic
._float
.alignment
,
478 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
482 case ustctl_atype_array
:
484 const struct ustctl_basic_type
*elem_type
;
486 ret
= print_tabs(session
, nesting
);
490 elem_type
= &field
->type
.u
.array
.elem_type
;
491 ret
= lttng_metadata_printf(session
,
492 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
493 elem_type
->u
.basic
.integer
.size
,
494 elem_type
->u
.basic
.integer
.alignment
,
495 elem_type
->u
.basic
.integer
.signedness
,
496 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
498 : (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
501 elem_type
->u
.basic
.integer
.base
,
502 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
503 field
->name
, field
->type
.u
.array
.length
);
507 case ustctl_atype_sequence
:
509 const struct ustctl_basic_type
*elem_type
;
510 const struct ustctl_basic_type
*length_type
;
512 elem_type
= &field
->type
.u
.sequence
.elem_type
;
513 length_type
= &field
->type
.u
.sequence
.length_type
;
514 ret
= print_tabs(session
, nesting
);
518 ret
= lttng_metadata_printf(session
,
519 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
520 length_type
->u
.basic
.integer
.size
,
521 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
522 length_type
->u
.basic
.integer
.signedness
,
523 (length_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
525 : ((length_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
528 length_type
->u
.basic
.integer
.base
,
529 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
535 ret
= print_tabs(session
, nesting
);
539 ret
= lttng_metadata_printf(session
,
540 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
541 elem_type
->u
.basic
.integer
.size
,
542 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
543 elem_type
->u
.basic
.integer
.signedness
,
544 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
546 : ((elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
549 elem_type
->u
.basic
.integer
.base
,
550 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
557 case ustctl_atype_string
:
558 /* Default encoding is UTF8 */
559 ret
= print_tabs(session
, nesting
);
563 ret
= lttng_metadata_printf(session
,
565 field
->type
.u
.basic
.string
.encoding
== ustctl_encode_ASCII
?
566 " { encoding = ASCII; }" : "",
570 case ustctl_atype_variant
:
571 ret
= _lttng_variant_statedump(session
, fields
, nr_fields
, iter_field
, nesting
);
576 case ustctl_atype_struct
:
577 ret
= print_tabs(session
, nesting
);
581 ret
= lttng_metadata_printf(session
,
594 int _lttng_context_metadata_statedump(struct ust_registry_session
*session
,
595 size_t nr_ctx_fields
,
596 struct ustctl_field
*ctx
)
604 if (i
>= nr_ctx_fields
) {
607 ret
= _lttng_field_statedump(session
, ctx
,
608 nr_ctx_fields
, &i
, 2);
617 int _lttng_fields_metadata_statedump(struct ust_registry_session
*session
,
618 struct ust_registry_event
*event
)
624 if (i
>= event
->nr_fields
) {
627 ret
= _lttng_field_statedump(session
, event
->fields
,
628 event
->nr_fields
, &i
, 2);
637 * Should be called with session registry mutex held.
639 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
640 struct ust_registry_channel
*chan
,
641 struct ust_registry_event
*event
)
645 /* Don't dump metadata events */
646 if (chan
->chan_id
== -1U)
649 ret
= lttng_metadata_printf(session
,
653 " stream_id = %u;\n",
660 ret
= lttng_metadata_printf(session
,
662 event
->loglevel_value
);
666 if (event
->model_emf_uri
) {
667 ret
= lttng_metadata_printf(session
,
668 " model.emf.uri = \"%s\";\n",
669 event
->model_emf_uri
);
674 ret
= lttng_metadata_printf(session
,
675 " fields := struct {\n"
680 ret
= _lttng_fields_metadata_statedump(session
, event
);
684 ret
= lttng_metadata_printf(session
,
689 event
->metadata_dumped
= 1;
696 * Should be called with session registry mutex held.
698 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
699 struct ust_registry_channel
*chan
)
703 /* Don't dump metadata events */
704 if (chan
->chan_id
== -1U)
707 if (!chan
->header_type
)
710 ret
= lttng_metadata_printf(session
,
713 " event.header := %s;\n"
714 " packet.context := struct packet_context;\n",
716 chan
->header_type
== USTCTL_CHANNEL_HEADER_COMPACT
?
717 "struct event_header_compact" :
718 "struct event_header_large");
722 if (chan
->ctx_fields
) {
723 ret
= lttng_metadata_printf(session
,
724 " event.context := struct {\n");
728 ret
= _lttng_context_metadata_statedump(session
,
733 if (chan
->ctx_fields
) {
734 ret
= lttng_metadata_printf(session
,
740 ret
= lttng_metadata_printf(session
,
742 /* Flag success of metadata dump. */
743 chan
->metadata_dumped
= 1;
750 int _lttng_stream_packet_context_declare(struct ust_registry_session
*session
)
752 return lttng_metadata_printf(session
,
753 "struct packet_context {\n"
754 " uint64_clock_monotonic_t timestamp_begin;\n"
755 " uint64_clock_monotonic_t timestamp_end;\n"
756 " uint64_t content_size;\n"
757 " uint64_t packet_size;\n"
758 " uint64_t packet_seq_num;\n"
759 " unsigned long events_discarded;\n"
760 " uint32_t cpu_id;\n"
768 * id 31 is reserved to indicate an extended header.
771 * id: range: 0 - 65534.
772 * id 65535 is reserved to indicate an extended header.
775 int _lttng_event_header_declare(struct ust_registry_session
*session
)
777 return lttng_metadata_printf(session
,
778 "struct event_header_compact {\n"
779 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
782 " uint27_clock_monotonic_t timestamp;\n"
786 " uint64_clock_monotonic_t timestamp;\n"
791 "struct event_header_large {\n"
792 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
795 " uint32_clock_monotonic_t timestamp;\n"
799 " uint64_clock_monotonic_t timestamp;\n"
803 session
->uint32_t_alignment
,
804 session
->uint16_t_alignment
809 * The offset between monotonic and realtime clock can be negative if
810 * the system sets the REALTIME clock to 0 after boot.
813 int measure_single_clock_offset(struct offset_sample
*sample
)
815 uint64_t monotonic_avg
, monotonic
[2], measure_delta
, realtime
;
816 uint64_t tcf
= trace_clock_freq();
817 struct timespec rts
= { 0, 0 };
820 monotonic
[0] = trace_clock_read64();
821 ret
= lttng_clock_gettime(CLOCK_REALTIME
, &rts
);
825 monotonic
[1] = trace_clock_read64();
826 measure_delta
= monotonic
[1] - monotonic
[0];
827 if (measure_delta
> sample
->measure_delta
) {
829 * Discard value if it took longer to read than the best
834 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
835 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
836 if (tcf
== NSEC_PER_SEC
) {
837 realtime
+= rts
.tv_nsec
;
839 realtime
+= (uint64_t) rts
.tv_nsec
* tcf
/ NSEC_PER_SEC
;
841 sample
->offset
= (int64_t) realtime
- monotonic_avg
;
842 sample
->measure_delta
= measure_delta
;
847 * Approximation of NTP time of day to clock monotonic correlation,
848 * taken at start of trace. Keep the measurement that took the less time
849 * to complete, thus removing imprecision caused by preemption.
850 * May return a negative offset.
853 int64_t measure_clock_offset(void)
856 struct offset_sample offset_best_sample
= {
858 .measure_delta
= UINT64_MAX
,
861 for (i
= 0; i
< NR_CLOCK_OFFSET_SAMPLES
; i
++) {
862 if (measure_single_clock_offset(&offset_best_sample
)) {
866 return offset_best_sample
.offset
;
870 * Should be called with session registry mutex held.
872 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
877 unsigned char *uuid_c
;
878 char uuid_s
[UUID_STR_LEN
],
879 clock_uuid_s
[UUID_STR_LEN
];
881 char hostname
[HOST_NAME_MAX
];
885 uuid_c
= session
->uuid
;
887 snprintf(uuid_s
, sizeof(uuid_s
),
888 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
889 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
890 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
891 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
892 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
895 ret
= lttng_metadata_printf(session
,
896 "/* CTF %u.%u */\n\n",
903 ret
= lttng_metadata_printf(session
,
904 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
905 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
906 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
907 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
908 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
909 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
910 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
916 " byte_order = %s;\n"
917 " packet.header := struct {\n"
919 " uint8_t uuid[16];\n"
920 " uint32_t stream_id;\n"
921 " uint64_t stream_instance_id;\n"
924 session
->uint8_t_alignment
,
925 session
->uint16_t_alignment
,
926 session
->uint32_t_alignment
,
927 session
->uint64_t_alignment
,
928 session
->bits_per_long
,
929 session
->long_alignment
,
933 session
->byte_order
== BIG_ENDIAN
? "be" : "le"
938 /* ignore error, just use empty string if error. */
940 ret
= gethostname(hostname
, sizeof(hostname
));
941 if (ret
&& errno
== ENAMETOOLONG
)
942 hostname
[HOST_NAME_MAX
- 1] = '\0';
943 ret
= lttng_metadata_printf(session
,
945 " hostname = \"%s\";\n"
946 " domain = \"ust\";\n"
947 " tracer_name = \"lttng-ust\";\n"
948 " tracer_major = %u;\n"
949 " tracer_minor = %u;\n",
958 * If per-application registry, we can output extra information
959 * about the application.
962 ret
= lttng_metadata_printf(session
,
963 " tracer_patchlevel = %u;\n"
965 " procname = \"%s\";\n",
966 app
->version
.patchlevel
,
974 ret
= lttng_metadata_printf(session
,
981 ret
= lttng_metadata_printf(session
,
989 if (!trace_clock_uuid(clock_uuid_s
)) {
990 ret
= lttng_metadata_printf(session
,
998 ret
= lttng_metadata_printf(session
,
999 " description = \"%s\";\n"
1000 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1001 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1002 " offset = %" PRId64
";\n"
1004 trace_clock_description(),
1006 measure_clock_offset()
1011 ret
= lttng_metadata_printf(session
,
1012 "typealias integer {\n"
1013 " size = 27; align = 1; signed = false;\n"
1014 " map = clock.%s.value;\n"
1015 "} := uint27_clock_monotonic_t;\n"
1017 "typealias integer {\n"
1018 " size = 32; align = %u; signed = false;\n"
1019 " map = clock.%s.value;\n"
1020 "} := uint32_clock_monotonic_t;\n"
1022 "typealias integer {\n"
1023 " size = 64; align = %u; signed = false;\n"
1024 " map = clock.%s.value;\n"
1025 "} := uint64_clock_monotonic_t;\n\n",
1027 session
->uint32_t_alignment
,
1029 session
->uint64_t_alignment
,
1035 ret
= _lttng_stream_packet_context_declare(session
);
1039 ret
= _lttng_event_header_declare(session
);