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>
32 #include "ust-registry.h"
33 #include "ust-clock.h"
37 #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
40 #define NSEC_PER_SEC 1000000000ULL
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
, " ");
203 /* Called with session registry mutex held. */
205 int ust_metadata_enum_statedump(struct ust_registry_session
*session
,
206 const char *enum_name
,
208 const struct ustctl_integer_type
*container_type
,
209 const char *field_name
, size_t *iter_field
, size_t nesting
)
211 struct ust_registry_enum
*reg_enum
;
212 const struct ustctl_enum_entry
*entries
;
218 reg_enum
= ust_registry_lookup_enum_by_id(session
, enum_name
, enum_id
);
220 /* reg_enum can still be used because session registry mutex is held. */
225 entries
= reg_enum
->entries
;
226 nr_entries
= reg_enum
->nr_entries
;
228 ret
= print_tabs(session
, nesting
);
232 ret
= lttng_metadata_printf(session
,
233 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n",
234 container_type
->size
,
235 container_type
->alignment
,
236 container_type
->signedness
,
237 (container_type
->encoding
== ustctl_encode_none
)
239 : (container_type
->encoding
== ustctl_encode_UTF8
)
242 container_type
->base
);
246 /* Dump all entries */
247 for (i
= 0; i
< nr_entries
; i
++) {
248 const struct ustctl_enum_entry
*entry
= &entries
[i
];
251 ret
= lttng_metadata_printf(session
,
256 len
= strlen(entry
->string
);
257 /* Escape the character '"' */
258 for (j
= 0; j
< len
; j
++) {
259 char c
= entry
->string
[j
];
263 ret
= lttng_metadata_printf(session
,
267 ret
= lttng_metadata_printf(session
,
271 ret
= lttng_metadata_printf(session
,
279 ret
= lttng_metadata_printf(session
,
284 if (entry
->start
== entry
->end
) {
285 ret
= lttng_metadata_printf(session
,
289 ret
= lttng_metadata_printf(session
,
291 entry
->start
, entry
->end
);
297 ret
= lttng_metadata_printf(session
, " } _%s;\n",
306 int _lttng_variant_statedump(struct ust_registry_session
*session
,
307 const struct ustctl_field
*fields
, size_t nr_fields
,
308 size_t *iter_field
, size_t nesting
)
310 const struct ustctl_field
*variant
= &fields
[*iter_field
];
311 uint32_t nr_choices
, i
;
314 if (variant
->type
.atype
!= ustctl_atype_variant
) {
318 nr_choices
= variant
->type
.u
.variant
.nr_choices
;
320 ret
= lttng_metadata_printf(session
,
321 " variant <_%s> {\n",
322 variant
->type
.u
.variant
.tag_name
);
327 for (i
= 0; i
< nr_choices
; i
++) {
328 if (*iter_field
>= nr_fields
) {
332 ret
= _lttng_field_statedump(session
,
334 iter_field
, nesting
+ 1);
336 ret
= lttng_metadata_printf(session
,
347 int _lttng_field_statedump(struct ust_registry_session
*session
,
348 const struct ustctl_field
*fields
, size_t nr_fields
,
349 size_t *iter_field
, size_t nesting
)
352 const char *bo_be
= " byte_order = be;";
353 const char *bo_le
= " byte_order = le;";
354 const char *bo_native
= "";
355 const char *bo_reverse
;
356 const struct ustctl_field
*field
;
358 if (*iter_field
>= nr_fields
) {
362 field
= &fields
[*iter_field
];
364 if (session
->byte_order
== BIG_ENDIAN
) {
370 switch (field
->type
.atype
) {
371 case ustctl_atype_integer
:
372 ret
= print_tabs(session
, nesting
);
376 ret
= lttng_metadata_printf(session
,
377 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
378 field
->type
.u
.basic
.integer
.size
,
379 field
->type
.u
.basic
.integer
.alignment
,
380 field
->type
.u
.basic
.integer
.signedness
,
381 (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_none
)
383 : (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
386 field
->type
.u
.basic
.integer
.base
,
387 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
391 case ustctl_atype_enum
:
392 ret
= ust_metadata_enum_statedump(session
,
393 field
->type
.u
.basic
.enumeration
.name
,
394 field
->type
.u
.basic
.enumeration
.id
,
395 &field
->type
.u
.basic
.enumeration
.container_type
,
396 field
->name
, iter_field
, nesting
);
398 case ustctl_atype_float
:
399 ret
= print_tabs(session
, nesting
);
403 ret
= lttng_metadata_printf(session
,
404 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
405 field
->type
.u
.basic
._float
.exp_dig
,
406 field
->type
.u
.basic
._float
.mant_dig
,
407 field
->type
.u
.basic
._float
.alignment
,
408 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
412 case ustctl_atype_array
:
414 const struct ustctl_basic_type
*elem_type
;
416 ret
= print_tabs(session
, nesting
);
420 elem_type
= &field
->type
.u
.array
.elem_type
;
421 ret
= lttng_metadata_printf(session
,
422 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
423 elem_type
->u
.basic
.integer
.size
,
424 elem_type
->u
.basic
.integer
.alignment
,
425 elem_type
->u
.basic
.integer
.signedness
,
426 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
428 : (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
431 elem_type
->u
.basic
.integer
.base
,
432 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
433 field
->name
, field
->type
.u
.array
.length
);
437 case ustctl_atype_sequence
:
439 const struct ustctl_basic_type
*elem_type
;
440 const struct ustctl_basic_type
*length_type
;
442 elem_type
= &field
->type
.u
.sequence
.elem_type
;
443 length_type
= &field
->type
.u
.sequence
.length_type
;
444 ret
= print_tabs(session
, nesting
);
448 ret
= lttng_metadata_printf(session
,
449 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
450 length_type
->u
.basic
.integer
.size
,
451 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
452 length_type
->u
.basic
.integer
.signedness
,
453 (length_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
455 : ((length_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
458 length_type
->u
.basic
.integer
.base
,
459 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
465 ret
= print_tabs(session
, nesting
);
469 ret
= lttng_metadata_printf(session
,
470 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
471 elem_type
->u
.basic
.integer
.size
,
472 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
473 elem_type
->u
.basic
.integer
.signedness
,
474 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
476 : ((elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
479 elem_type
->u
.basic
.integer
.base
,
480 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
487 case ustctl_atype_string
:
488 /* Default encoding is UTF8 */
489 ret
= print_tabs(session
, nesting
);
493 ret
= lttng_metadata_printf(session
,
495 field
->type
.u
.basic
.string
.encoding
== ustctl_encode_ASCII
?
496 " { encoding = ASCII; }" : "",
500 case ustctl_atype_variant
:
501 ret
= _lttng_variant_statedump(session
, fields
, nr_fields
, iter_field
, nesting
);
506 case ustctl_atype_struct
:
507 ret
= print_tabs(session
, nesting
);
511 ret
= lttng_metadata_printf(session
,
524 int _lttng_context_metadata_statedump(struct ust_registry_session
*session
,
525 size_t nr_ctx_fields
,
526 struct ustctl_field
*ctx
)
534 if (i
>= nr_ctx_fields
) {
537 ret
= _lttng_field_statedump(session
, ctx
,
538 nr_ctx_fields
, &i
, 2);
547 int _lttng_fields_metadata_statedump(struct ust_registry_session
*session
,
548 struct ust_registry_event
*event
)
554 if (i
>= event
->nr_fields
) {
557 ret
= _lttng_field_statedump(session
, event
->fields
,
558 event
->nr_fields
, &i
, 2);
567 * Should be called with session registry mutex held.
569 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
570 struct ust_registry_channel
*chan
,
571 struct ust_registry_event
*event
)
575 /* Don't dump metadata events */
576 if (chan
->chan_id
== -1U)
579 ret
= lttng_metadata_printf(session
,
583 " stream_id = %u;\n",
590 ret
= lttng_metadata_printf(session
,
592 event
->loglevel_value
);
596 if (event
->model_emf_uri
) {
597 ret
= lttng_metadata_printf(session
,
598 " model.emf.uri = \"%s\";\n",
599 event
->model_emf_uri
);
604 ret
= lttng_metadata_printf(session
,
605 " fields := struct {\n"
610 ret
= _lttng_fields_metadata_statedump(session
, event
);
614 ret
= lttng_metadata_printf(session
,
619 event
->metadata_dumped
= 1;
626 * Should be called with session registry mutex held.
628 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
629 struct ust_registry_channel
*chan
)
633 /* Don't dump metadata events */
634 if (chan
->chan_id
== -1U)
637 if (!chan
->header_type
)
640 ret
= lttng_metadata_printf(session
,
643 " event.header := %s;\n"
644 " packet.context := struct packet_context;\n",
646 chan
->header_type
== USTCTL_CHANNEL_HEADER_COMPACT
?
647 "struct event_header_compact" :
648 "struct event_header_large");
652 if (chan
->ctx_fields
) {
653 ret
= lttng_metadata_printf(session
,
654 " event.context := struct {\n");
658 ret
= _lttng_context_metadata_statedump(session
,
663 if (chan
->ctx_fields
) {
664 ret
= lttng_metadata_printf(session
,
670 ret
= lttng_metadata_printf(session
,
672 /* Flag success of metadata dump. */
673 chan
->metadata_dumped
= 1;
680 int _lttng_stream_packet_context_declare(struct ust_registry_session
*session
)
682 return lttng_metadata_printf(session
,
683 "struct packet_context {\n"
684 " uint64_clock_monotonic_t timestamp_begin;\n"
685 " uint64_clock_monotonic_t timestamp_end;\n"
686 " uint64_t content_size;\n"
687 " uint64_t packet_size;\n"
688 " unsigned long events_discarded;\n"
689 " uint32_t cpu_id;\n"
697 * id 31 is reserved to indicate an extended header.
700 * id: range: 0 - 65534.
701 * id 65535 is reserved to indicate an extended header.
704 int _lttng_event_header_declare(struct ust_registry_session
*session
)
706 return lttng_metadata_printf(session
,
707 "struct event_header_compact {\n"
708 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
711 " uint27_clock_monotonic_t timestamp;\n"
715 " uint64_clock_monotonic_t timestamp;\n"
720 "struct event_header_large {\n"
721 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
724 " uint32_clock_monotonic_t timestamp;\n"
728 " uint64_clock_monotonic_t timestamp;\n"
732 session
->uint32_t_alignment
,
733 session
->uint16_t_alignment
738 * The offset between monotonic and realtime clock can be negative if
739 * the system sets the REALTIME clock to 0 after boot.
742 int measure_single_clock_offset(struct offset_sample
*sample
)
744 uint64_t monotonic_avg
, monotonic
[2], measure_delta
, realtime
;
745 uint64_t tcf
= trace_clock_freq();
746 struct timespec rts
= { 0, 0 };
749 monotonic
[0] = trace_clock_read64();
750 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
754 monotonic
[1] = trace_clock_read64();
755 measure_delta
= monotonic
[1] - monotonic
[0];
756 if (measure_delta
> sample
->measure_delta
) {
758 * Discard value if it took longer to read than the best
763 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
764 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
765 if (tcf
== NSEC_PER_SEC
) {
766 realtime
+= rts
.tv_nsec
;
768 realtime
+= (uint64_t) rts
.tv_nsec
* tcf
/ NSEC_PER_SEC
;
770 sample
->offset
= (int64_t) realtime
- monotonic_avg
;
771 sample
->measure_delta
= measure_delta
;
776 * Approximation of NTP time of day to clock monotonic correlation,
777 * taken at start of trace. Keep the measurement that took the less time
778 * to complete, thus removing imprecision caused by preemption.
779 * May return a negative offset.
782 int64_t measure_clock_offset(void)
785 struct offset_sample offset_best_sample
= {
787 .measure_delta
= UINT64_MAX
,
790 for (i
= 0; i
< NR_CLOCK_OFFSET_SAMPLES
; i
++) {
791 if (measure_single_clock_offset(&offset_best_sample
)) {
795 return offset_best_sample
.offset
;
799 * Should be called with session registry mutex held.
801 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
806 unsigned char *uuid_c
;
807 char uuid_s
[UUID_STR_LEN
],
808 clock_uuid_s
[UUID_STR_LEN
];
810 char hostname
[HOST_NAME_MAX
];
814 uuid_c
= session
->uuid
;
816 snprintf(uuid_s
, sizeof(uuid_s
),
817 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
818 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
819 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
820 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
821 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
824 ret
= lttng_metadata_printf(session
,
825 "/* CTF %u.%u */\n\n",
832 ret
= lttng_metadata_printf(session
,
833 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
834 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
835 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
836 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
837 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
838 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
839 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
845 " byte_order = %s;\n"
846 " packet.header := struct {\n"
848 " uint8_t uuid[16];\n"
849 " uint32_t stream_id;\n"
852 session
->uint8_t_alignment
,
853 session
->uint16_t_alignment
,
854 session
->uint32_t_alignment
,
855 session
->uint64_t_alignment
,
856 session
->bits_per_long
,
857 session
->long_alignment
,
861 session
->byte_order
== BIG_ENDIAN
? "be" : "le"
866 /* ignore error, just use empty string if error. */
868 ret
= gethostname(hostname
, sizeof(hostname
));
869 if (ret
&& errno
== ENAMETOOLONG
)
870 hostname
[HOST_NAME_MAX
- 1] = '\0';
871 ret
= lttng_metadata_printf(session
,
873 " hostname = \"%s\";\n"
874 " domain = \"ust\";\n"
875 " tracer_name = \"lttng-ust\";\n"
876 " tracer_major = %u;\n"
877 " tracer_minor = %u;\n",
886 * If per-application registry, we can output extra information
887 * about the application.
890 ret
= lttng_metadata_printf(session
,
891 " tracer_patchlevel = %u;\n"
893 " procname = \"%s\";\n",
894 app
->version
.patchlevel
,
902 ret
= lttng_metadata_printf(session
,
909 ret
= lttng_metadata_printf(session
,
917 if (!trace_clock_uuid(clock_uuid_s
)) {
918 ret
= lttng_metadata_printf(session
,
926 ret
= lttng_metadata_printf(session
,
927 " description = \"%s\";\n"
928 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
929 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
930 " offset = %" PRId64
";\n"
932 trace_clock_description(),
934 measure_clock_offset()
939 ret
= lttng_metadata_printf(session
,
940 "typealias integer {\n"
941 " size = 27; align = 1; signed = false;\n"
942 " map = clock.%s.value;\n"
943 "} := uint27_clock_monotonic_t;\n"
945 "typealias integer {\n"
946 " size = 32; align = %u; signed = false;\n"
947 " map = clock.%s.value;\n"
948 "} := uint32_clock_monotonic_t;\n"
950 "typealias integer {\n"
951 " size = 64; align = %u; signed = false;\n"
952 " map = clock.%s.value;\n"
953 "} := uint64_clock_monotonic_t;\n\n",
955 session
->uint32_t_alignment
,
957 session
->uint64_t_alignment
,
963 ret
= _lttng_stream_packet_context_declare(session
);
967 ret
= _lttng_event_header_declare(session
);