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
,
308 if (entry
->start
.signedness
) {
309 ret
= lttng_metadata_printf(session
,
310 "%lld", (long long) entry
->start
.value
);
312 ret
= lttng_metadata_printf(session
,
313 "%llu", entry
->start
.value
);
319 if (entry
->start
.signedness
== entry
->end
.signedness
&&
320 entry
->start
.value
== entry
->end
.value
) {
321 ret
= lttng_metadata_printf(session
,
324 if (entry
->end
.signedness
) {
325 ret
= lttng_metadata_printf(session
,
326 " ... %lld,\n", (long long) entry
->end
.value
);
328 ret
= lttng_metadata_printf(session
,
329 " ... %llu,\n", entry
->end
.value
);
336 sanitize_ctf_identifier(identifier
, field_name
);
337 ret
= print_tabs(session
, nesting
);
341 ret
= lttng_metadata_printf(session
, "} _%s;\n",
349 int _lttng_variant_statedump(struct ust_registry_session
*session
,
350 const struct ustctl_field
*fields
, size_t nr_fields
,
351 size_t *iter_field
, size_t nesting
)
353 const struct ustctl_field
*variant
= &fields
[*iter_field
];
354 uint32_t nr_choices
, i
;
356 char identifier
[LTTNG_UST_SYM_NAME_LEN
];
358 if (variant
->type
.atype
!= ustctl_atype_variant
) {
362 nr_choices
= variant
->type
.u
.variant
.nr_choices
;
364 sanitize_ctf_identifier(identifier
, variant
->type
.u
.variant
.tag_name
);
365 ret
= print_tabs(session
, nesting
);
369 ret
= lttng_metadata_printf(session
,
376 for (i
= 0; i
< nr_choices
; i
++) {
377 if (*iter_field
>= nr_fields
) {
381 ret
= _lttng_field_statedump(session
,
383 iter_field
, nesting
+ 1);
388 sanitize_ctf_identifier(identifier
, variant
->name
);
389 ret
= print_tabs(session
, nesting
);
390 ret
= lttng_metadata_printf(session
,
401 int _lttng_field_statedump(struct ust_registry_session
*session
,
402 const struct ustctl_field
*fields
, size_t nr_fields
,
403 size_t *iter_field
, size_t nesting
)
406 const char *bo_be
= " byte_order = be;";
407 const char *bo_le
= " byte_order = le;";
408 const char *bo_native
= "";
409 const char *bo_reverse
;
410 const struct ustctl_field
*field
;
412 if (*iter_field
>= nr_fields
) {
416 field
= &fields
[*iter_field
];
418 if (session
->byte_order
== BIG_ENDIAN
) {
424 switch (field
->type
.atype
) {
425 case ustctl_atype_integer
:
426 ret
= print_tabs(session
, nesting
);
430 ret
= lttng_metadata_printf(session
,
431 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
432 field
->type
.u
.basic
.integer
.size
,
433 field
->type
.u
.basic
.integer
.alignment
,
434 field
->type
.u
.basic
.integer
.signedness
,
435 (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_none
)
437 : (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
440 field
->type
.u
.basic
.integer
.base
,
441 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
445 case ustctl_atype_enum
:
446 ret
= ust_metadata_enum_statedump(session
,
447 field
->type
.u
.basic
.enumeration
.name
,
448 field
->type
.u
.basic
.enumeration
.id
,
449 &field
->type
.u
.basic
.enumeration
.container_type
,
450 field
->name
, iter_field
, nesting
);
452 case ustctl_atype_float
:
453 ret
= print_tabs(session
, nesting
);
457 ret
= lttng_metadata_printf(session
,
458 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
459 field
->type
.u
.basic
._float
.exp_dig
,
460 field
->type
.u
.basic
._float
.mant_dig
,
461 field
->type
.u
.basic
._float
.alignment
,
462 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
466 case ustctl_atype_array
:
468 const struct ustctl_basic_type
*elem_type
;
470 ret
= print_tabs(session
, nesting
);
474 elem_type
= &field
->type
.u
.array
.elem_type
;
475 ret
= lttng_metadata_printf(session
,
476 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
477 elem_type
->u
.basic
.integer
.size
,
478 elem_type
->u
.basic
.integer
.alignment
,
479 elem_type
->u
.basic
.integer
.signedness
,
480 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
482 : (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
485 elem_type
->u
.basic
.integer
.base
,
486 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
487 field
->name
, field
->type
.u
.array
.length
);
491 case ustctl_atype_sequence
:
493 const struct ustctl_basic_type
*elem_type
;
494 const struct ustctl_basic_type
*length_type
;
496 elem_type
= &field
->type
.u
.sequence
.elem_type
;
497 length_type
= &field
->type
.u
.sequence
.length_type
;
498 ret
= print_tabs(session
, nesting
);
502 ret
= lttng_metadata_printf(session
,
503 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
504 length_type
->u
.basic
.integer
.size
,
505 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
506 length_type
->u
.basic
.integer
.signedness
,
507 (length_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
509 : ((length_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
512 length_type
->u
.basic
.integer
.base
,
513 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
519 ret
= print_tabs(session
, nesting
);
523 ret
= lttng_metadata_printf(session
,
524 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
525 elem_type
->u
.basic
.integer
.size
,
526 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
527 elem_type
->u
.basic
.integer
.signedness
,
528 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
530 : ((elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
533 elem_type
->u
.basic
.integer
.base
,
534 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
541 case ustctl_atype_string
:
542 /* Default encoding is UTF8 */
543 ret
= print_tabs(session
, nesting
);
547 ret
= lttng_metadata_printf(session
,
549 field
->type
.u
.basic
.string
.encoding
== ustctl_encode_ASCII
?
550 " { encoding = ASCII; }" : "",
554 case ustctl_atype_variant
:
555 ret
= _lttng_variant_statedump(session
, fields
, nr_fields
, iter_field
, nesting
);
560 case ustctl_atype_struct
:
561 ret
= print_tabs(session
, nesting
);
565 ret
= lttng_metadata_printf(session
,
578 int _lttng_context_metadata_statedump(struct ust_registry_session
*session
,
579 size_t nr_ctx_fields
,
580 struct ustctl_field
*ctx
)
588 if (i
>= nr_ctx_fields
) {
591 ret
= _lttng_field_statedump(session
, ctx
,
592 nr_ctx_fields
, &i
, 2);
601 int _lttng_fields_metadata_statedump(struct ust_registry_session
*session
,
602 struct ust_registry_event
*event
)
608 if (i
>= event
->nr_fields
) {
611 ret
= _lttng_field_statedump(session
, event
->fields
,
612 event
->nr_fields
, &i
, 2);
621 * Should be called with session registry mutex held.
623 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
624 struct ust_registry_channel
*chan
,
625 struct ust_registry_event
*event
)
629 /* Don't dump metadata events */
630 if (chan
->chan_id
== -1U)
633 ret
= lttng_metadata_printf(session
,
637 " stream_id = %u;\n",
644 ret
= lttng_metadata_printf(session
,
646 event
->loglevel_value
);
650 if (event
->model_emf_uri
) {
651 ret
= lttng_metadata_printf(session
,
652 " model.emf.uri = \"%s\";\n",
653 event
->model_emf_uri
);
658 ret
= lttng_metadata_printf(session
,
659 " fields := struct {\n"
664 ret
= _lttng_fields_metadata_statedump(session
, event
);
668 ret
= lttng_metadata_printf(session
,
673 event
->metadata_dumped
= 1;
680 * Should be called with session registry mutex held.
682 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
683 struct ust_registry_channel
*chan
)
687 /* Don't dump metadata events */
688 if (chan
->chan_id
== -1U)
691 if (!chan
->header_type
)
694 ret
= lttng_metadata_printf(session
,
697 " event.header := %s;\n"
698 " packet.context := struct packet_context;\n",
700 chan
->header_type
== USTCTL_CHANNEL_HEADER_COMPACT
?
701 "struct event_header_compact" :
702 "struct event_header_large");
706 if (chan
->ctx_fields
) {
707 ret
= lttng_metadata_printf(session
,
708 " event.context := struct {\n");
712 ret
= _lttng_context_metadata_statedump(session
,
717 if (chan
->ctx_fields
) {
718 ret
= lttng_metadata_printf(session
,
724 ret
= lttng_metadata_printf(session
,
726 /* Flag success of metadata dump. */
727 chan
->metadata_dumped
= 1;
734 int _lttng_stream_packet_context_declare(struct ust_registry_session
*session
)
736 return lttng_metadata_printf(session
,
737 "struct packet_context {\n"
738 " uint64_clock_monotonic_t timestamp_begin;\n"
739 " uint64_clock_monotonic_t timestamp_end;\n"
740 " uint64_t content_size;\n"
741 " uint64_t packet_size;\n"
742 " uint64_t packet_seq_num;\n"
743 " unsigned long events_discarded;\n"
744 " uint32_t cpu_id;\n"
752 * id 31 is reserved to indicate an extended header.
755 * id: range: 0 - 65534.
756 * id 65535 is reserved to indicate an extended header.
759 int _lttng_event_header_declare(struct ust_registry_session
*session
)
761 return lttng_metadata_printf(session
,
762 "struct event_header_compact {\n"
763 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
766 " uint27_clock_monotonic_t timestamp;\n"
770 " uint64_clock_monotonic_t timestamp;\n"
775 "struct event_header_large {\n"
776 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
779 " uint32_clock_monotonic_t timestamp;\n"
783 " uint64_clock_monotonic_t timestamp;\n"
787 session
->uint32_t_alignment
,
788 session
->uint16_t_alignment
793 * The offset between monotonic and realtime clock can be negative if
794 * the system sets the REALTIME clock to 0 after boot.
797 int measure_single_clock_offset(struct offset_sample
*sample
)
799 uint64_t monotonic_avg
, monotonic
[2], measure_delta
, realtime
;
800 uint64_t tcf
= trace_clock_freq();
801 struct timespec rts
= { 0, 0 };
804 monotonic
[0] = trace_clock_read64();
805 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
809 monotonic
[1] = trace_clock_read64();
810 measure_delta
= monotonic
[1] - monotonic
[0];
811 if (measure_delta
> sample
->measure_delta
) {
813 * Discard value if it took longer to read than the best
818 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
819 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
820 if (tcf
== NSEC_PER_SEC
) {
821 realtime
+= rts
.tv_nsec
;
823 realtime
+= (uint64_t) rts
.tv_nsec
* tcf
/ NSEC_PER_SEC
;
825 sample
->offset
= (int64_t) realtime
- monotonic_avg
;
826 sample
->measure_delta
= measure_delta
;
831 * Approximation of NTP time of day to clock monotonic correlation,
832 * taken at start of trace. Keep the measurement that took the less time
833 * to complete, thus removing imprecision caused by preemption.
834 * May return a negative offset.
837 int64_t measure_clock_offset(void)
840 struct offset_sample offset_best_sample
= {
842 .measure_delta
= UINT64_MAX
,
845 for (i
= 0; i
< NR_CLOCK_OFFSET_SAMPLES
; i
++) {
846 if (measure_single_clock_offset(&offset_best_sample
)) {
850 return offset_best_sample
.offset
;
854 * Should be called with session registry mutex held.
856 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
861 unsigned char *uuid_c
;
862 char uuid_s
[UUID_STR_LEN
],
863 clock_uuid_s
[UUID_STR_LEN
];
865 char hostname
[HOST_NAME_MAX
];
869 uuid_c
= session
->uuid
;
871 snprintf(uuid_s
, sizeof(uuid_s
),
872 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
873 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
874 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
875 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
876 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
879 ret
= lttng_metadata_printf(session
,
880 "/* CTF %u.%u */\n\n",
887 ret
= lttng_metadata_printf(session
,
888 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
889 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
890 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
891 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
892 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
893 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
894 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
900 " byte_order = %s;\n"
901 " packet.header := struct {\n"
903 " uint8_t uuid[16];\n"
904 " uint32_t stream_id;\n"
905 " uint64_t stream_instance_id;\n"
908 session
->uint8_t_alignment
,
909 session
->uint16_t_alignment
,
910 session
->uint32_t_alignment
,
911 session
->uint64_t_alignment
,
912 session
->bits_per_long
,
913 session
->long_alignment
,
917 session
->byte_order
== BIG_ENDIAN
? "be" : "le"
922 /* ignore error, just use empty string if error. */
924 ret
= gethostname(hostname
, sizeof(hostname
));
925 if (ret
&& errno
== ENAMETOOLONG
)
926 hostname
[HOST_NAME_MAX
- 1] = '\0';
927 ret
= lttng_metadata_printf(session
,
929 " hostname = \"%s\";\n"
930 " domain = \"ust\";\n"
931 " tracer_name = \"lttng-ust\";\n"
932 " tracer_major = %u;\n"
933 " tracer_minor = %u;\n",
942 * If per-application registry, we can output extra information
943 * about the application.
946 ret
= lttng_metadata_printf(session
,
947 " tracer_patchlevel = %u;\n"
949 " procname = \"%s\";\n",
950 app
->version
.patchlevel
,
958 ret
= lttng_metadata_printf(session
,
965 ret
= lttng_metadata_printf(session
,
973 if (!trace_clock_uuid(clock_uuid_s
)) {
974 ret
= lttng_metadata_printf(session
,
982 ret
= lttng_metadata_printf(session
,
983 " description = \"%s\";\n"
984 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
985 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
986 " offset = %" PRId64
";\n"
988 trace_clock_description(),
990 measure_clock_offset()
995 ret
= lttng_metadata_printf(session
,
996 "typealias integer {\n"
997 " size = 27; align = 1; signed = false;\n"
998 " map = clock.%s.value;\n"
999 "} := uint27_clock_monotonic_t;\n"
1001 "typealias integer {\n"
1002 " size = 32; align = %u; signed = false;\n"
1003 " map = clock.%s.value;\n"
1004 "} := uint32_clock_monotonic_t;\n"
1006 "typealias integer {\n"
1007 " size = 64; align = %u; signed = false;\n"
1008 " map = clock.%s.value;\n"
1009 "} := uint64_clock_monotonic_t;\n\n",
1011 session
->uint32_t_alignment
,
1013 session
->uint64_t_alignment
,
1019 ret
= _lttng_stream_packet_context_declare(session
);
1023 ret
= _lttng_event_header_declare(session
);