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
);
409 ret
= lttng_metadata_printf(session
,
420 int _lttng_field_statedump(struct ust_registry_session
*session
,
421 const struct ustctl_field
*fields
, size_t nr_fields
,
422 size_t *iter_field
, size_t nesting
)
425 const char *bo_be
= " byte_order = be;";
426 const char *bo_le
= " byte_order = le;";
427 const char *bo_native
= "";
428 const char *bo_reverse
;
429 const struct ustctl_field
*field
;
431 if (*iter_field
>= nr_fields
) {
435 field
= &fields
[*iter_field
];
437 if (session
->byte_order
== BIG_ENDIAN
) {
443 switch (field
->type
.atype
) {
444 case ustctl_atype_integer
:
445 ret
= print_tabs(session
, nesting
);
449 ret
= lttng_metadata_printf(session
,
450 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
451 field
->type
.u
.basic
.integer
.size
,
452 field
->type
.u
.basic
.integer
.alignment
,
453 field
->type
.u
.basic
.integer
.signedness
,
454 (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_none
)
456 : (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
459 field
->type
.u
.basic
.integer
.base
,
460 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
464 case ustctl_atype_enum
:
465 ret
= ust_metadata_enum_statedump(session
,
466 field
->type
.u
.basic
.enumeration
.name
,
467 field
->type
.u
.basic
.enumeration
.id
,
468 &field
->type
.u
.basic
.enumeration
.container_type
,
469 field
->name
, iter_field
, nesting
);
471 case ustctl_atype_float
:
472 ret
= print_tabs(session
, nesting
);
476 ret
= lttng_metadata_printf(session
,
477 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
478 field
->type
.u
.basic
._float
.exp_dig
,
479 field
->type
.u
.basic
._float
.mant_dig
,
480 field
->type
.u
.basic
._float
.alignment
,
481 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
485 case ustctl_atype_array
:
487 const struct ustctl_basic_type
*elem_type
;
489 ret
= print_tabs(session
, nesting
);
493 elem_type
= &field
->type
.u
.array
.elem_type
;
494 ret
= lttng_metadata_printf(session
,
495 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
496 elem_type
->u
.basic
.integer
.size
,
497 elem_type
->u
.basic
.integer
.alignment
,
498 elem_type
->u
.basic
.integer
.signedness
,
499 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
501 : (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
504 elem_type
->u
.basic
.integer
.base
,
505 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
506 field
->name
, field
->type
.u
.array
.length
);
510 case ustctl_atype_sequence
:
512 const struct ustctl_basic_type
*elem_type
;
513 const struct ustctl_basic_type
*length_type
;
515 elem_type
= &field
->type
.u
.sequence
.elem_type
;
516 length_type
= &field
->type
.u
.sequence
.length_type
;
517 ret
= print_tabs(session
, nesting
);
521 ret
= lttng_metadata_printf(session
,
522 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
523 length_type
->u
.basic
.integer
.size
,
524 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
525 length_type
->u
.basic
.integer
.signedness
,
526 (length_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
528 : ((length_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
531 length_type
->u
.basic
.integer
.base
,
532 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
538 ret
= print_tabs(session
, nesting
);
542 ret
= lttng_metadata_printf(session
,
543 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
544 elem_type
->u
.basic
.integer
.size
,
545 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
546 elem_type
->u
.basic
.integer
.signedness
,
547 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
549 : ((elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
552 elem_type
->u
.basic
.integer
.base
,
553 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
560 case ustctl_atype_string
:
561 /* Default encoding is UTF8 */
562 ret
= print_tabs(session
, nesting
);
566 ret
= lttng_metadata_printf(session
,
568 field
->type
.u
.basic
.string
.encoding
== ustctl_encode_ASCII
?
569 " { encoding = ASCII; }" : "",
573 case ustctl_atype_variant
:
574 ret
= _lttng_variant_statedump(session
, fields
, nr_fields
, iter_field
, nesting
);
579 case ustctl_atype_struct
:
580 ret
= print_tabs(session
, nesting
);
584 ret
= lttng_metadata_printf(session
,
597 int _lttng_context_metadata_statedump(struct ust_registry_session
*session
,
598 size_t nr_ctx_fields
,
599 struct ustctl_field
*ctx
)
607 if (i
>= nr_ctx_fields
) {
610 ret
= _lttng_field_statedump(session
, ctx
,
611 nr_ctx_fields
, &i
, 2);
620 int _lttng_fields_metadata_statedump(struct ust_registry_session
*session
,
621 struct ust_registry_event
*event
)
627 if (i
>= event
->nr_fields
) {
630 ret
= _lttng_field_statedump(session
, event
->fields
,
631 event
->nr_fields
, &i
, 2);
640 * Should be called with session registry mutex held.
642 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
643 struct ust_registry_channel
*chan
,
644 struct ust_registry_event
*event
)
648 /* Don't dump metadata events */
649 if (chan
->chan_id
== -1U)
652 ret
= lttng_metadata_printf(session
,
656 " stream_id = %u;\n",
663 ret
= lttng_metadata_printf(session
,
665 event
->loglevel_value
);
669 if (event
->model_emf_uri
) {
670 ret
= lttng_metadata_printf(session
,
671 " model.emf.uri = \"%s\";\n",
672 event
->model_emf_uri
);
677 ret
= lttng_metadata_printf(session
,
678 " fields := struct {\n"
683 ret
= _lttng_fields_metadata_statedump(session
, event
);
687 ret
= lttng_metadata_printf(session
,
692 event
->metadata_dumped
= 1;
699 * Should be called with session registry mutex held.
701 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
702 struct ust_registry_channel
*chan
)
706 /* Don't dump metadata events */
707 if (chan
->chan_id
== -1U)
710 if (!chan
->header_type
)
713 ret
= lttng_metadata_printf(session
,
716 " event.header := %s;\n"
717 " packet.context := struct packet_context;\n",
719 chan
->header_type
== USTCTL_CHANNEL_HEADER_COMPACT
?
720 "struct event_header_compact" :
721 "struct event_header_large");
725 if (chan
->ctx_fields
) {
726 ret
= lttng_metadata_printf(session
,
727 " event.context := struct {\n");
731 ret
= _lttng_context_metadata_statedump(session
,
736 if (chan
->ctx_fields
) {
737 ret
= lttng_metadata_printf(session
,
743 ret
= lttng_metadata_printf(session
,
745 /* Flag success of metadata dump. */
746 chan
->metadata_dumped
= 1;
753 int _lttng_stream_packet_context_declare(struct ust_registry_session
*session
)
755 return lttng_metadata_printf(session
,
756 "struct packet_context {\n"
757 " uint64_clock_monotonic_t timestamp_begin;\n"
758 " uint64_clock_monotonic_t timestamp_end;\n"
759 " uint64_t content_size;\n"
760 " uint64_t packet_size;\n"
761 " uint64_t packet_seq_num;\n"
762 " unsigned long events_discarded;\n"
763 " uint32_t cpu_id;\n"
771 * id 31 is reserved to indicate an extended header.
774 * id: range: 0 - 65534.
775 * id 65535 is reserved to indicate an extended header.
778 int _lttng_event_header_declare(struct ust_registry_session
*session
)
780 return lttng_metadata_printf(session
,
781 "struct event_header_compact {\n"
782 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
785 " uint27_clock_monotonic_t timestamp;\n"
789 " uint64_clock_monotonic_t timestamp;\n"
794 "struct event_header_large {\n"
795 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
798 " uint32_clock_monotonic_t timestamp;\n"
802 " uint64_clock_monotonic_t timestamp;\n"
806 session
->uint32_t_alignment
,
807 session
->uint16_t_alignment
812 * The offset between monotonic and realtime clock can be negative if
813 * the system sets the REALTIME clock to 0 after boot.
816 int measure_single_clock_offset(struct offset_sample
*sample
)
818 uint64_t monotonic_avg
, monotonic
[2], measure_delta
, realtime
;
819 uint64_t tcf
= trace_clock_freq();
820 struct timespec rts
= { 0, 0 };
823 monotonic
[0] = trace_clock_read64();
824 ret
= lttng_clock_gettime(CLOCK_REALTIME
, &rts
);
828 monotonic
[1] = trace_clock_read64();
829 measure_delta
= monotonic
[1] - monotonic
[0];
830 if (measure_delta
> sample
->measure_delta
) {
832 * Discard value if it took longer to read than the best
837 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
838 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
839 if (tcf
== NSEC_PER_SEC
) {
840 realtime
+= rts
.tv_nsec
;
842 realtime
+= (uint64_t) rts
.tv_nsec
* tcf
/ NSEC_PER_SEC
;
844 sample
->offset
= (int64_t) realtime
- monotonic_avg
;
845 sample
->measure_delta
= measure_delta
;
850 * Approximation of NTP time of day to clock monotonic correlation,
851 * taken at start of trace. Keep the measurement that took the less time
852 * to complete, thus removing imprecision caused by preemption.
853 * May return a negative offset.
856 int64_t measure_clock_offset(void)
859 struct offset_sample offset_best_sample
= {
861 .measure_delta
= UINT64_MAX
,
864 for (i
= 0; i
< NR_CLOCK_OFFSET_SAMPLES
; i
++) {
865 if (measure_single_clock_offset(&offset_best_sample
)) {
869 return offset_best_sample
.offset
;
873 * Should be called with session registry mutex held.
875 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
880 unsigned char *uuid_c
;
881 char uuid_s
[UUID_STR_LEN
],
882 clock_uuid_s
[UUID_STR_LEN
];
884 char hostname
[HOST_NAME_MAX
];
888 uuid_c
= session
->uuid
;
890 snprintf(uuid_s
, sizeof(uuid_s
),
891 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
892 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
893 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
894 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
895 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
898 ret
= lttng_metadata_printf(session
,
899 "/* CTF %u.%u */\n\n",
906 ret
= lttng_metadata_printf(session
,
907 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
908 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
909 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
910 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
911 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
912 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
913 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
919 " byte_order = %s;\n"
920 " packet.header := struct {\n"
922 " uint8_t uuid[16];\n"
923 " uint32_t stream_id;\n"
924 " uint64_t stream_instance_id;\n"
927 session
->uint8_t_alignment
,
928 session
->uint16_t_alignment
,
929 session
->uint32_t_alignment
,
930 session
->uint64_t_alignment
,
931 session
->bits_per_long
,
932 session
->long_alignment
,
936 session
->byte_order
== BIG_ENDIAN
? "be" : "le"
941 /* ignore error, just use empty string if error. */
943 ret
= gethostname(hostname
, sizeof(hostname
));
944 if (ret
&& errno
== ENAMETOOLONG
)
945 hostname
[HOST_NAME_MAX
- 1] = '\0';
946 ret
= lttng_metadata_printf(session
,
948 " hostname = \"%s\";\n"
949 " domain = \"ust\";\n"
950 " tracer_name = \"lttng-ust\";\n"
951 " tracer_major = %u;\n"
952 " tracer_minor = %u;\n",
961 * If per-application registry, we can output extra information
962 * about the application.
965 ret
= lttng_metadata_printf(session
,
966 " tracer_patchlevel = %u;\n"
968 " procname = \"%s\";\n",
969 app
->version
.patchlevel
,
977 ret
= lttng_metadata_printf(session
,
984 ret
= lttng_metadata_printf(session
,
992 if (!trace_clock_uuid(clock_uuid_s
)) {
993 ret
= lttng_metadata_printf(session
,
1001 ret
= lttng_metadata_printf(session
,
1002 " description = \"%s\";\n"
1003 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
1004 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1005 " offset = %" PRId64
";\n"
1007 trace_clock_description(),
1009 measure_clock_offset()
1014 ret
= lttng_metadata_printf(session
,
1015 "typealias integer {\n"
1016 " size = 27; align = 1; signed = false;\n"
1017 " map = clock.%s.value;\n"
1018 "} := uint27_clock_monotonic_t;\n"
1020 "typealias integer {\n"
1021 " size = 32; align = %u; signed = false;\n"
1022 " map = clock.%s.value;\n"
1023 "} := uint32_clock_monotonic_t;\n"
1025 "typealias integer {\n"
1026 " size = 64; align = %u; signed = false;\n"
1027 " map = clock.%s.value;\n"
1028 "} := uint64_clock_monotonic_t;\n\n",
1030 session
->uint32_t_alignment
,
1032 session
->uint64_t_alignment
,
1038 ret
= _lttng_stream_packet_context_declare(session
);
1042 ret
= _lttng_event_header_declare(session
);