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
, " ");
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
);
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
->start
== entry
->end
) {
308 ret
= lttng_metadata_printf(session
,
312 ret
= lttng_metadata_printf(session
,
314 entry
->start
, entry
->end
);
320 sanitize_ctf_identifier(identifier
, field_name
);
321 ret
= print_tabs(session
, nesting
);
325 ret
= lttng_metadata_printf(session
, "} _%s;\n",
333 int _lttng_variant_statedump(struct ust_registry_session
*session
,
334 const struct ustctl_field
*fields
, size_t nr_fields
,
335 size_t *iter_field
, size_t nesting
)
337 const struct ustctl_field
*variant
= &fields
[*iter_field
];
338 uint32_t nr_choices
, i
;
340 char identifier
[LTTNG_UST_SYM_NAME_LEN
];
342 if (variant
->type
.atype
!= ustctl_atype_variant
) {
346 nr_choices
= variant
->type
.u
.variant
.nr_choices
;
348 sanitize_ctf_identifier(identifier
, variant
->type
.u
.variant
.tag_name
);
349 ret
= print_tabs(session
, nesting
);
353 ret
= lttng_metadata_printf(session
,
360 for (i
= 0; i
< nr_choices
; i
++) {
361 if (*iter_field
>= nr_fields
) {
365 ret
= _lttng_field_statedump(session
,
367 iter_field
, nesting
+ 1);
369 sanitize_ctf_identifier(identifier
, variant
->name
);
370 ret
= print_tabs(session
, nesting
);
371 ret
= lttng_metadata_printf(session
,
382 int _lttng_field_statedump(struct ust_registry_session
*session
,
383 const struct ustctl_field
*fields
, size_t nr_fields
,
384 size_t *iter_field
, size_t nesting
)
387 const char *bo_be
= " byte_order = be;";
388 const char *bo_le
= " byte_order = le;";
389 const char *bo_native
= "";
390 const char *bo_reverse
;
391 const struct ustctl_field
*field
;
393 if (*iter_field
>= nr_fields
) {
397 field
= &fields
[*iter_field
];
399 if (session
->byte_order
== BIG_ENDIAN
) {
405 switch (field
->type
.atype
) {
406 case ustctl_atype_integer
:
407 ret
= print_tabs(session
, nesting
);
411 ret
= lttng_metadata_printf(session
,
412 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
413 field
->type
.u
.basic
.integer
.size
,
414 field
->type
.u
.basic
.integer
.alignment
,
415 field
->type
.u
.basic
.integer
.signedness
,
416 (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_none
)
418 : (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
421 field
->type
.u
.basic
.integer
.base
,
422 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
426 case ustctl_atype_enum
:
427 ret
= ust_metadata_enum_statedump(session
,
428 field
->type
.u
.basic
.enumeration
.name
,
429 field
->type
.u
.basic
.enumeration
.id
,
430 &field
->type
.u
.basic
.enumeration
.container_type
,
431 field
->name
, iter_field
, nesting
);
433 case ustctl_atype_float
:
434 ret
= print_tabs(session
, nesting
);
438 ret
= lttng_metadata_printf(session
,
439 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
440 field
->type
.u
.basic
._float
.exp_dig
,
441 field
->type
.u
.basic
._float
.mant_dig
,
442 field
->type
.u
.basic
._float
.alignment
,
443 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
447 case ustctl_atype_array
:
449 const struct ustctl_basic_type
*elem_type
;
451 ret
= print_tabs(session
, nesting
);
455 elem_type
= &field
->type
.u
.array
.elem_type
;
456 ret
= lttng_metadata_printf(session
,
457 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
458 elem_type
->u
.basic
.integer
.size
,
459 elem_type
->u
.basic
.integer
.alignment
,
460 elem_type
->u
.basic
.integer
.signedness
,
461 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
463 : (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
466 elem_type
->u
.basic
.integer
.base
,
467 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
468 field
->name
, field
->type
.u
.array
.length
);
472 case ustctl_atype_sequence
:
474 const struct ustctl_basic_type
*elem_type
;
475 const struct ustctl_basic_type
*length_type
;
477 elem_type
= &field
->type
.u
.sequence
.elem_type
;
478 length_type
= &field
->type
.u
.sequence
.length_type
;
479 ret
= print_tabs(session
, nesting
);
483 ret
= lttng_metadata_printf(session
,
484 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
485 length_type
->u
.basic
.integer
.size
,
486 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
487 length_type
->u
.basic
.integer
.signedness
,
488 (length_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
490 : ((length_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
493 length_type
->u
.basic
.integer
.base
,
494 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
500 ret
= print_tabs(session
, nesting
);
504 ret
= lttng_metadata_printf(session
,
505 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
506 elem_type
->u
.basic
.integer
.size
,
507 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
508 elem_type
->u
.basic
.integer
.signedness
,
509 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
511 : ((elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
514 elem_type
->u
.basic
.integer
.base
,
515 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
522 case ustctl_atype_string
:
523 /* Default encoding is UTF8 */
524 ret
= print_tabs(session
, nesting
);
528 ret
= lttng_metadata_printf(session
,
530 field
->type
.u
.basic
.string
.encoding
== ustctl_encode_ASCII
?
531 " { encoding = ASCII; }" : "",
535 case ustctl_atype_variant
:
536 ret
= _lttng_variant_statedump(session
, fields
, nr_fields
, iter_field
, nesting
);
541 case ustctl_atype_struct
:
542 ret
= print_tabs(session
, nesting
);
546 ret
= lttng_metadata_printf(session
,
559 int _lttng_context_metadata_statedump(struct ust_registry_session
*session
,
560 size_t nr_ctx_fields
,
561 struct ustctl_field
*ctx
)
569 if (i
>= nr_ctx_fields
) {
572 ret
= _lttng_field_statedump(session
, ctx
,
573 nr_ctx_fields
, &i
, 2);
582 int _lttng_fields_metadata_statedump(struct ust_registry_session
*session
,
583 struct ust_registry_event
*event
)
589 if (i
>= event
->nr_fields
) {
592 ret
= _lttng_field_statedump(session
, event
->fields
,
593 event
->nr_fields
, &i
, 2);
602 * Should be called with session registry mutex held.
604 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
605 struct ust_registry_channel
*chan
,
606 struct ust_registry_event
*event
)
610 /* Don't dump metadata events */
611 if (chan
->chan_id
== -1U)
614 ret
= lttng_metadata_printf(session
,
618 " stream_id = %u;\n",
625 ret
= lttng_metadata_printf(session
,
627 event
->loglevel_value
);
631 if (event
->model_emf_uri
) {
632 ret
= lttng_metadata_printf(session
,
633 " model.emf.uri = \"%s\";\n",
634 event
->model_emf_uri
);
639 ret
= lttng_metadata_printf(session
,
640 " fields := struct {\n"
645 ret
= _lttng_fields_metadata_statedump(session
, event
);
649 ret
= lttng_metadata_printf(session
,
654 event
->metadata_dumped
= 1;
661 * Should be called with session registry mutex held.
663 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
664 struct ust_registry_channel
*chan
)
668 /* Don't dump metadata events */
669 if (chan
->chan_id
== -1U)
672 if (!chan
->header_type
)
675 ret
= lttng_metadata_printf(session
,
678 " event.header := %s;\n"
679 " packet.context := struct packet_context;\n",
681 chan
->header_type
== USTCTL_CHANNEL_HEADER_COMPACT
?
682 "struct event_header_compact" :
683 "struct event_header_large");
687 if (chan
->ctx_fields
) {
688 ret
= lttng_metadata_printf(session
,
689 " event.context := struct {\n");
693 ret
= _lttng_context_metadata_statedump(session
,
698 if (chan
->ctx_fields
) {
699 ret
= lttng_metadata_printf(session
,
705 ret
= lttng_metadata_printf(session
,
707 /* Flag success of metadata dump. */
708 chan
->metadata_dumped
= 1;
715 int _lttng_stream_packet_context_declare(struct ust_registry_session
*session
)
717 return lttng_metadata_printf(session
,
718 "struct packet_context {\n"
719 " uint64_clock_monotonic_t timestamp_begin;\n"
720 " uint64_clock_monotonic_t timestamp_end;\n"
721 " uint64_t content_size;\n"
722 " uint64_t packet_size;\n"
723 " unsigned long events_discarded;\n"
724 " uint32_t cpu_id;\n"
732 * id 31 is reserved to indicate an extended header.
735 * id: range: 0 - 65534.
736 * id 65535 is reserved to indicate an extended header.
739 int _lttng_event_header_declare(struct ust_registry_session
*session
)
741 return lttng_metadata_printf(session
,
742 "struct event_header_compact {\n"
743 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
746 " uint27_clock_monotonic_t timestamp;\n"
750 " uint64_clock_monotonic_t timestamp;\n"
755 "struct event_header_large {\n"
756 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
759 " uint32_clock_monotonic_t timestamp;\n"
763 " uint64_clock_monotonic_t timestamp;\n"
767 session
->uint32_t_alignment
,
768 session
->uint16_t_alignment
773 * The offset between monotonic and realtime clock can be negative if
774 * the system sets the REALTIME clock to 0 after boot.
777 int measure_single_clock_offset(struct offset_sample
*sample
)
779 uint64_t monotonic_avg
, monotonic
[2], measure_delta
, realtime
;
780 uint64_t tcf
= trace_clock_freq();
781 struct timespec rts
= { 0, 0 };
784 monotonic
[0] = trace_clock_read64();
785 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
789 monotonic
[1] = trace_clock_read64();
790 measure_delta
= monotonic
[1] - monotonic
[0];
791 if (measure_delta
> sample
->measure_delta
) {
793 * Discard value if it took longer to read than the best
798 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
799 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
800 if (tcf
== NSEC_PER_SEC
) {
801 realtime
+= rts
.tv_nsec
;
803 realtime
+= (uint64_t) rts
.tv_nsec
* tcf
/ NSEC_PER_SEC
;
805 sample
->offset
= (int64_t) realtime
- monotonic_avg
;
806 sample
->measure_delta
= measure_delta
;
811 * Approximation of NTP time of day to clock monotonic correlation,
812 * taken at start of trace. Keep the measurement that took the less time
813 * to complete, thus removing imprecision caused by preemption.
814 * May return a negative offset.
817 int64_t measure_clock_offset(void)
820 struct offset_sample offset_best_sample
= {
822 .measure_delta
= UINT64_MAX
,
825 for (i
= 0; i
< NR_CLOCK_OFFSET_SAMPLES
; i
++) {
826 if (measure_single_clock_offset(&offset_best_sample
)) {
830 return offset_best_sample
.offset
;
834 * Should be called with session registry mutex held.
836 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
841 unsigned char *uuid_c
;
842 char uuid_s
[UUID_STR_LEN
],
843 clock_uuid_s
[UUID_STR_LEN
];
845 char hostname
[HOST_NAME_MAX
];
849 uuid_c
= session
->uuid
;
851 snprintf(uuid_s
, sizeof(uuid_s
),
852 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
853 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
854 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
855 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
856 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
859 ret
= lttng_metadata_printf(session
,
860 "/* CTF %u.%u */\n\n",
867 ret
= lttng_metadata_printf(session
,
868 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
869 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
870 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
871 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
872 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
873 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
874 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
880 " byte_order = %s;\n"
881 " packet.header := struct {\n"
883 " uint8_t uuid[16];\n"
884 " uint32_t stream_id;\n"
887 session
->uint8_t_alignment
,
888 session
->uint16_t_alignment
,
889 session
->uint32_t_alignment
,
890 session
->uint64_t_alignment
,
891 session
->bits_per_long
,
892 session
->long_alignment
,
896 session
->byte_order
== BIG_ENDIAN
? "be" : "le"
901 /* ignore error, just use empty string if error. */
903 ret
= gethostname(hostname
, sizeof(hostname
));
904 if (ret
&& errno
== ENAMETOOLONG
)
905 hostname
[HOST_NAME_MAX
- 1] = '\0';
906 ret
= lttng_metadata_printf(session
,
908 " hostname = \"%s\";\n"
909 " domain = \"ust\";\n"
910 " tracer_name = \"lttng-ust\";\n"
911 " tracer_major = %u;\n"
912 " tracer_minor = %u;\n",
921 * If per-application registry, we can output extra information
922 * about the application.
925 ret
= lttng_metadata_printf(session
,
926 " tracer_patchlevel = %u;\n"
928 " procname = \"%s\";\n",
929 app
->version
.patchlevel
,
937 ret
= lttng_metadata_printf(session
,
944 ret
= lttng_metadata_printf(session
,
952 if (!trace_clock_uuid(clock_uuid_s
)) {
953 ret
= lttng_metadata_printf(session
,
961 ret
= lttng_metadata_printf(session
,
962 " description = \"%s\";\n"
963 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
964 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
965 " offset = %" PRId64
";\n"
967 trace_clock_description(),
969 measure_clock_offset()
974 ret
= lttng_metadata_printf(session
,
975 "typealias integer {\n"
976 " size = 27; align = 1; signed = false;\n"
977 " map = clock.%s.value;\n"
978 "} := uint27_clock_monotonic_t;\n"
980 "typealias integer {\n"
981 " size = 32; align = %u; signed = false;\n"
982 " map = clock.%s.value;\n"
983 "} := uint32_clock_monotonic_t;\n"
985 "typealias integer {\n"
986 " size = 64; align = %u; signed = false;\n"
987 " map = clock.%s.value;\n"
988 "} := uint64_clock_monotonic_t;\n\n",
990 session
->uint32_t_alignment
,
992 session
->uint64_t_alignment
,
998 ret
= _lttng_stream_packet_context_declare(session
);
1002 ret
= _lttng_event_header_declare(session
);