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
);
266 /* Dump all entries */
267 for (i
= 0; i
< nr_entries
; i
++) {
268 const struct ustctl_enum_entry
*entry
= &entries
[i
];
271 ret
= print_tabs(session
, nesting
);
275 ret
= lttng_metadata_printf(session
,
280 len
= strlen(entry
->string
);
281 /* Escape the character '"' */
282 for (j
= 0; j
< len
; j
++) {
283 char c
= entry
->string
[j
];
287 ret
= lttng_metadata_printf(session
,
291 ret
= lttng_metadata_printf(session
,
295 ret
= lttng_metadata_printf(session
,
303 ret
= lttng_metadata_printf(session
,
309 if (entry
->start
.signedness
) {
310 ret
= lttng_metadata_printf(session
,
311 "%lld", (long long) entry
->start
.value
);
313 ret
= lttng_metadata_printf(session
,
314 "%llu", entry
->start
.value
);
320 if (entry
->start
.signedness
== entry
->end
.signedness
&&
321 entry
->start
.value
== entry
->end
.value
) {
322 ret
= lttng_metadata_printf(session
,
325 if (entry
->end
.signedness
) {
326 ret
= lttng_metadata_printf(session
,
327 " ... %lld,\n", (long long) entry
->end
.value
);
329 ret
= lttng_metadata_printf(session
,
330 " ... %llu,\n", entry
->end
.value
);
338 sanitize_ctf_identifier(identifier
, field_name
);
339 ret
= print_tabs(session
, nesting
);
343 ret
= lttng_metadata_printf(session
, "} _%s;\n",
351 int _lttng_variant_statedump(struct ust_registry_session
*session
,
352 const struct ustctl_field
*fields
, size_t nr_fields
,
353 size_t *iter_field
, size_t nesting
)
355 const struct ustctl_field
*variant
= &fields
[*iter_field
];
356 uint32_t nr_choices
, i
;
358 char identifier
[LTTNG_UST_SYM_NAME_LEN
];
360 if (variant
->type
.atype
!= ustctl_atype_variant
) {
364 nr_choices
= variant
->type
.u
.variant
.nr_choices
;
366 sanitize_ctf_identifier(identifier
, variant
->type
.u
.variant
.tag_name
);
367 ret
= print_tabs(session
, nesting
);
371 ret
= lttng_metadata_printf(session
,
378 for (i
= 0; i
< nr_choices
; i
++) {
379 if (*iter_field
>= nr_fields
) {
383 ret
= _lttng_field_statedump(session
,
385 iter_field
, nesting
+ 1);
390 sanitize_ctf_identifier(identifier
, variant
->name
);
391 ret
= print_tabs(session
, nesting
);
392 ret
= lttng_metadata_printf(session
,
403 int _lttng_field_statedump(struct ust_registry_session
*session
,
404 const struct ustctl_field
*fields
, size_t nr_fields
,
405 size_t *iter_field
, size_t nesting
)
408 const char *bo_be
= " byte_order = be;";
409 const char *bo_le
= " byte_order = le;";
410 const char *bo_native
= "";
411 const char *bo_reverse
;
412 const struct ustctl_field
*field
;
414 if (*iter_field
>= nr_fields
) {
418 field
= &fields
[*iter_field
];
420 if (session
->byte_order
== BIG_ENDIAN
) {
426 switch (field
->type
.atype
) {
427 case ustctl_atype_integer
:
428 ret
= print_tabs(session
, nesting
);
432 ret
= lttng_metadata_printf(session
,
433 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
434 field
->type
.u
.basic
.integer
.size
,
435 field
->type
.u
.basic
.integer
.alignment
,
436 field
->type
.u
.basic
.integer
.signedness
,
437 (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_none
)
439 : (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
442 field
->type
.u
.basic
.integer
.base
,
443 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
447 case ustctl_atype_enum
:
448 ret
= ust_metadata_enum_statedump(session
,
449 field
->type
.u
.basic
.enumeration
.name
,
450 field
->type
.u
.basic
.enumeration
.id
,
451 &field
->type
.u
.basic
.enumeration
.container_type
,
452 field
->name
, iter_field
, nesting
);
454 case ustctl_atype_float
:
455 ret
= print_tabs(session
, nesting
);
459 ret
= lttng_metadata_printf(session
,
460 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
461 field
->type
.u
.basic
._float
.exp_dig
,
462 field
->type
.u
.basic
._float
.mant_dig
,
463 field
->type
.u
.basic
._float
.alignment
,
464 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
468 case ustctl_atype_array
:
470 const struct ustctl_basic_type
*elem_type
;
472 ret
= print_tabs(session
, nesting
);
476 elem_type
= &field
->type
.u
.array
.elem_type
;
477 ret
= lttng_metadata_printf(session
,
478 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
479 elem_type
->u
.basic
.integer
.size
,
480 elem_type
->u
.basic
.integer
.alignment
,
481 elem_type
->u
.basic
.integer
.signedness
,
482 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
484 : (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
487 elem_type
->u
.basic
.integer
.base
,
488 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
489 field
->name
, field
->type
.u
.array
.length
);
493 case ustctl_atype_sequence
:
495 const struct ustctl_basic_type
*elem_type
;
496 const struct ustctl_basic_type
*length_type
;
498 elem_type
= &field
->type
.u
.sequence
.elem_type
;
499 length_type
= &field
->type
.u
.sequence
.length_type
;
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_length;\n",
506 length_type
->u
.basic
.integer
.size
,
507 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
508 length_type
->u
.basic
.integer
.signedness
,
509 (length_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
511 : ((length_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
514 length_type
->u
.basic
.integer
.base
,
515 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
521 ret
= print_tabs(session
, nesting
);
525 ret
= lttng_metadata_printf(session
,
526 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
527 elem_type
->u
.basic
.integer
.size
,
528 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
529 elem_type
->u
.basic
.integer
.signedness
,
530 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
532 : ((elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
535 elem_type
->u
.basic
.integer
.base
,
536 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
543 case ustctl_atype_string
:
544 /* Default encoding is UTF8 */
545 ret
= print_tabs(session
, nesting
);
549 ret
= lttng_metadata_printf(session
,
551 field
->type
.u
.basic
.string
.encoding
== ustctl_encode_ASCII
?
552 " { encoding = ASCII; }" : "",
556 case ustctl_atype_variant
:
557 ret
= _lttng_variant_statedump(session
, fields
, nr_fields
, iter_field
, nesting
);
562 case ustctl_atype_struct
:
563 ret
= print_tabs(session
, nesting
);
567 ret
= lttng_metadata_printf(session
,
580 int _lttng_context_metadata_statedump(struct ust_registry_session
*session
,
581 size_t nr_ctx_fields
,
582 struct ustctl_field
*ctx
)
590 if (i
>= nr_ctx_fields
) {
593 ret
= _lttng_field_statedump(session
, ctx
,
594 nr_ctx_fields
, &i
, 2);
603 int _lttng_fields_metadata_statedump(struct ust_registry_session
*session
,
604 struct ust_registry_event
*event
)
610 if (i
>= event
->nr_fields
) {
613 ret
= _lttng_field_statedump(session
, event
->fields
,
614 event
->nr_fields
, &i
, 2);
623 * Should be called with session registry mutex held.
625 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
626 struct ust_registry_channel
*chan
,
627 struct ust_registry_event
*event
)
631 /* Don't dump metadata events */
632 if (chan
->chan_id
== -1U)
635 ret
= lttng_metadata_printf(session
,
639 " stream_id = %u;\n",
646 ret
= lttng_metadata_printf(session
,
648 event
->loglevel_value
);
652 if (event
->model_emf_uri
) {
653 ret
= lttng_metadata_printf(session
,
654 " model.emf.uri = \"%s\";\n",
655 event
->model_emf_uri
);
660 ret
= lttng_metadata_printf(session
,
661 " fields := struct {\n"
666 ret
= _lttng_fields_metadata_statedump(session
, event
);
670 ret
= lttng_metadata_printf(session
,
675 event
->metadata_dumped
= 1;
682 * Should be called with session registry mutex held.
684 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
685 struct ust_registry_channel
*chan
)
689 /* Don't dump metadata events */
690 if (chan
->chan_id
== -1U)
693 if (!chan
->header_type
)
696 ret
= lttng_metadata_printf(session
,
699 " event.header := %s;\n"
700 " packet.context := struct packet_context;\n",
702 chan
->header_type
== USTCTL_CHANNEL_HEADER_COMPACT
?
703 "struct event_header_compact" :
704 "struct event_header_large");
708 if (chan
->ctx_fields
) {
709 ret
= lttng_metadata_printf(session
,
710 " event.context := struct {\n");
714 ret
= _lttng_context_metadata_statedump(session
,
719 if (chan
->ctx_fields
) {
720 ret
= lttng_metadata_printf(session
,
726 ret
= lttng_metadata_printf(session
,
728 /* Flag success of metadata dump. */
729 chan
->metadata_dumped
= 1;
736 int _lttng_stream_packet_context_declare(struct ust_registry_session
*session
)
738 return lttng_metadata_printf(session
,
739 "struct packet_context {\n"
740 " uint64_clock_monotonic_t timestamp_begin;\n"
741 " uint64_clock_monotonic_t timestamp_end;\n"
742 " uint64_t content_size;\n"
743 " uint64_t packet_size;\n"
744 " uint64_t packet_seq_num;\n"
745 " unsigned long events_discarded;\n"
746 " uint32_t cpu_id;\n"
754 * id 31 is reserved to indicate an extended header.
757 * id: range: 0 - 65534.
758 * id 65535 is reserved to indicate an extended header.
761 int _lttng_event_header_declare(struct ust_registry_session
*session
)
763 return lttng_metadata_printf(session
,
764 "struct event_header_compact {\n"
765 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
768 " uint27_clock_monotonic_t timestamp;\n"
772 " uint64_clock_monotonic_t timestamp;\n"
777 "struct event_header_large {\n"
778 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
781 " uint32_clock_monotonic_t timestamp;\n"
785 " uint64_clock_monotonic_t timestamp;\n"
789 session
->uint32_t_alignment
,
790 session
->uint16_t_alignment
795 * The offset between monotonic and realtime clock can be negative if
796 * the system sets the REALTIME clock to 0 after boot.
799 int measure_single_clock_offset(struct offset_sample
*sample
)
801 uint64_t monotonic_avg
, monotonic
[2], measure_delta
, realtime
;
802 uint64_t tcf
= trace_clock_freq();
803 struct timespec rts
= { 0, 0 };
806 monotonic
[0] = trace_clock_read64();
807 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
811 monotonic
[1] = trace_clock_read64();
812 measure_delta
= monotonic
[1] - monotonic
[0];
813 if (measure_delta
> sample
->measure_delta
) {
815 * Discard value if it took longer to read than the best
820 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
821 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
822 if (tcf
== NSEC_PER_SEC
) {
823 realtime
+= rts
.tv_nsec
;
825 realtime
+= (uint64_t) rts
.tv_nsec
* tcf
/ NSEC_PER_SEC
;
827 sample
->offset
= (int64_t) realtime
- monotonic_avg
;
828 sample
->measure_delta
= measure_delta
;
833 * Approximation of NTP time of day to clock monotonic correlation,
834 * taken at start of trace. Keep the measurement that took the less time
835 * to complete, thus removing imprecision caused by preemption.
836 * May return a negative offset.
839 int64_t measure_clock_offset(void)
842 struct offset_sample offset_best_sample
= {
844 .measure_delta
= UINT64_MAX
,
847 for (i
= 0; i
< NR_CLOCK_OFFSET_SAMPLES
; i
++) {
848 if (measure_single_clock_offset(&offset_best_sample
)) {
852 return offset_best_sample
.offset
;
856 * Should be called with session registry mutex held.
858 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
863 unsigned char *uuid_c
;
864 char uuid_s
[UUID_STR_LEN
],
865 clock_uuid_s
[UUID_STR_LEN
];
867 char hostname
[HOST_NAME_MAX
];
871 uuid_c
= session
->uuid
;
873 snprintf(uuid_s
, sizeof(uuid_s
),
874 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
875 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
876 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
877 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
878 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
881 ret
= lttng_metadata_printf(session
,
882 "/* CTF %u.%u */\n\n",
889 ret
= lttng_metadata_printf(session
,
890 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
891 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
892 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
893 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
894 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
895 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
896 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
902 " byte_order = %s;\n"
903 " packet.header := struct {\n"
905 " uint8_t uuid[16];\n"
906 " uint32_t stream_id;\n"
907 " uint64_t stream_instance_id;\n"
910 session
->uint8_t_alignment
,
911 session
->uint16_t_alignment
,
912 session
->uint32_t_alignment
,
913 session
->uint64_t_alignment
,
914 session
->bits_per_long
,
915 session
->long_alignment
,
919 session
->byte_order
== BIG_ENDIAN
? "be" : "le"
924 /* ignore error, just use empty string if error. */
926 ret
= gethostname(hostname
, sizeof(hostname
));
927 if (ret
&& errno
== ENAMETOOLONG
)
928 hostname
[HOST_NAME_MAX
- 1] = '\0';
929 ret
= lttng_metadata_printf(session
,
931 " hostname = \"%s\";\n"
932 " domain = \"ust\";\n"
933 " tracer_name = \"lttng-ust\";\n"
934 " tracer_major = %u;\n"
935 " tracer_minor = %u;\n",
944 * If per-application registry, we can output extra information
945 * about the application.
948 ret
= lttng_metadata_printf(session
,
949 " tracer_patchlevel = %u;\n"
951 " procname = \"%s\";\n",
952 app
->version
.patchlevel
,
960 ret
= lttng_metadata_printf(session
,
967 ret
= lttng_metadata_printf(session
,
975 if (!trace_clock_uuid(clock_uuid_s
)) {
976 ret
= lttng_metadata_printf(session
,
984 ret
= lttng_metadata_printf(session
,
985 " description = \"%s\";\n"
986 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
987 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
988 " offset = %" PRId64
";\n"
990 trace_clock_description(),
992 measure_clock_offset()
997 ret
= lttng_metadata_printf(session
,
998 "typealias integer {\n"
999 " size = 27; align = 1; signed = false;\n"
1000 " map = clock.%s.value;\n"
1001 "} := uint27_clock_monotonic_t;\n"
1003 "typealias integer {\n"
1004 " size = 32; align = %u; signed = false;\n"
1005 " map = clock.%s.value;\n"
1006 "} := uint32_clock_monotonic_t;\n"
1008 "typealias integer {\n"
1009 " size = 64; align = %u; signed = false;\n"
1010 " map = clock.%s.value;\n"
1011 "} := uint64_clock_monotonic_t;\n\n",
1013 session
->uint32_t_alignment
,
1015 session
->uint64_t_alignment
,
1021 ret
= _lttng_stream_packet_context_declare(session
);
1025 ret
= _lttng_event_header_declare(session
);