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
);
395 ret
= lttng_metadata_printf(session
,
406 int _lttng_field_statedump(struct ust_registry_session
*session
,
407 const struct ustctl_field
*fields
, size_t nr_fields
,
408 size_t *iter_field
, size_t nesting
)
411 const char *bo_be
= " byte_order = be;";
412 const char *bo_le
= " byte_order = le;";
413 const char *bo_native
= "";
414 const char *bo_reverse
;
415 const struct ustctl_field
*field
;
417 if (*iter_field
>= nr_fields
) {
421 field
= &fields
[*iter_field
];
423 if (session
->byte_order
== BIG_ENDIAN
) {
429 switch (field
->type
.atype
) {
430 case ustctl_atype_integer
:
431 ret
= print_tabs(session
, nesting
);
435 ret
= lttng_metadata_printf(session
,
436 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
437 field
->type
.u
.basic
.integer
.size
,
438 field
->type
.u
.basic
.integer
.alignment
,
439 field
->type
.u
.basic
.integer
.signedness
,
440 (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_none
)
442 : (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
445 field
->type
.u
.basic
.integer
.base
,
446 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
450 case ustctl_atype_enum
:
451 ret
= ust_metadata_enum_statedump(session
,
452 field
->type
.u
.basic
.enumeration
.name
,
453 field
->type
.u
.basic
.enumeration
.id
,
454 &field
->type
.u
.basic
.enumeration
.container_type
,
455 field
->name
, iter_field
, nesting
);
457 case ustctl_atype_float
:
458 ret
= print_tabs(session
, nesting
);
462 ret
= lttng_metadata_printf(session
,
463 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
464 field
->type
.u
.basic
._float
.exp_dig
,
465 field
->type
.u
.basic
._float
.mant_dig
,
466 field
->type
.u
.basic
._float
.alignment
,
467 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
471 case ustctl_atype_array
:
473 const struct ustctl_basic_type
*elem_type
;
475 ret
= print_tabs(session
, nesting
);
479 elem_type
= &field
->type
.u
.array
.elem_type
;
480 ret
= lttng_metadata_printf(session
,
481 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
482 elem_type
->u
.basic
.integer
.size
,
483 elem_type
->u
.basic
.integer
.alignment
,
484 elem_type
->u
.basic
.integer
.signedness
,
485 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
487 : (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
490 elem_type
->u
.basic
.integer
.base
,
491 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
492 field
->name
, field
->type
.u
.array
.length
);
496 case ustctl_atype_sequence
:
498 const struct ustctl_basic_type
*elem_type
;
499 const struct ustctl_basic_type
*length_type
;
501 elem_type
= &field
->type
.u
.sequence
.elem_type
;
502 length_type
= &field
->type
.u
.sequence
.length_type
;
503 ret
= print_tabs(session
, nesting
);
507 ret
= lttng_metadata_printf(session
,
508 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
509 length_type
->u
.basic
.integer
.size
,
510 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
511 length_type
->u
.basic
.integer
.signedness
,
512 (length_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
514 : ((length_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
517 length_type
->u
.basic
.integer
.base
,
518 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
524 ret
= print_tabs(session
, nesting
);
528 ret
= lttng_metadata_printf(session
,
529 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
530 elem_type
->u
.basic
.integer
.size
,
531 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
532 elem_type
->u
.basic
.integer
.signedness
,
533 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
535 : ((elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
538 elem_type
->u
.basic
.integer
.base
,
539 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
546 case ustctl_atype_string
:
547 /* Default encoding is UTF8 */
548 ret
= print_tabs(session
, nesting
);
552 ret
= lttng_metadata_printf(session
,
554 field
->type
.u
.basic
.string
.encoding
== ustctl_encode_ASCII
?
555 " { encoding = ASCII; }" : "",
559 case ustctl_atype_variant
:
560 ret
= _lttng_variant_statedump(session
, fields
, nr_fields
, iter_field
, nesting
);
565 case ustctl_atype_struct
:
566 ret
= print_tabs(session
, nesting
);
570 ret
= lttng_metadata_printf(session
,
583 int _lttng_context_metadata_statedump(struct ust_registry_session
*session
,
584 size_t nr_ctx_fields
,
585 struct ustctl_field
*ctx
)
593 if (i
>= nr_ctx_fields
) {
596 ret
= _lttng_field_statedump(session
, ctx
,
597 nr_ctx_fields
, &i
, 2);
606 int _lttng_fields_metadata_statedump(struct ust_registry_session
*session
,
607 struct ust_registry_event
*event
)
613 if (i
>= event
->nr_fields
) {
616 ret
= _lttng_field_statedump(session
, event
->fields
,
617 event
->nr_fields
, &i
, 2);
626 * Should be called with session registry mutex held.
628 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
629 struct ust_registry_channel
*chan
,
630 struct ust_registry_event
*event
)
634 /* Don't dump metadata events */
635 if (chan
->chan_id
== -1U)
638 ret
= lttng_metadata_printf(session
,
642 " stream_id = %u;\n",
649 ret
= lttng_metadata_printf(session
,
651 event
->loglevel_value
);
655 if (event
->model_emf_uri
) {
656 ret
= lttng_metadata_printf(session
,
657 " model.emf.uri = \"%s\";\n",
658 event
->model_emf_uri
);
663 ret
= lttng_metadata_printf(session
,
664 " fields := struct {\n"
669 ret
= _lttng_fields_metadata_statedump(session
, event
);
673 ret
= lttng_metadata_printf(session
,
678 event
->metadata_dumped
= 1;
685 * Should be called with session registry mutex held.
687 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
688 struct ust_registry_channel
*chan
)
692 /* Don't dump metadata events */
693 if (chan
->chan_id
== -1U)
696 if (!chan
->header_type
)
699 ret
= lttng_metadata_printf(session
,
702 " event.header := %s;\n"
703 " packet.context := struct packet_context;\n",
705 chan
->header_type
== USTCTL_CHANNEL_HEADER_COMPACT
?
706 "struct event_header_compact" :
707 "struct event_header_large");
711 if (chan
->ctx_fields
) {
712 ret
= lttng_metadata_printf(session
,
713 " event.context := struct {\n");
717 ret
= _lttng_context_metadata_statedump(session
,
722 if (chan
->ctx_fields
) {
723 ret
= lttng_metadata_printf(session
,
729 ret
= lttng_metadata_printf(session
,
731 /* Flag success of metadata dump. */
732 chan
->metadata_dumped
= 1;
739 int _lttng_stream_packet_context_declare(struct ust_registry_session
*session
)
741 return lttng_metadata_printf(session
,
742 "struct packet_context {\n"
743 " uint64_clock_monotonic_t timestamp_begin;\n"
744 " uint64_clock_monotonic_t timestamp_end;\n"
745 " uint64_t content_size;\n"
746 " uint64_t packet_size;\n"
747 " uint64_t packet_seq_num;\n"
748 " unsigned long events_discarded;\n"
749 " uint32_t cpu_id;\n"
757 * id 31 is reserved to indicate an extended header.
760 * id: range: 0 - 65534.
761 * id 65535 is reserved to indicate an extended header.
764 int _lttng_event_header_declare(struct ust_registry_session
*session
)
766 return lttng_metadata_printf(session
,
767 "struct event_header_compact {\n"
768 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
771 " uint27_clock_monotonic_t timestamp;\n"
775 " uint64_clock_monotonic_t timestamp;\n"
780 "struct event_header_large {\n"
781 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
784 " uint32_clock_monotonic_t timestamp;\n"
788 " uint64_clock_monotonic_t timestamp;\n"
792 session
->uint32_t_alignment
,
793 session
->uint16_t_alignment
798 * The offset between monotonic and realtime clock can be negative if
799 * the system sets the REALTIME clock to 0 after boot.
802 int measure_single_clock_offset(struct offset_sample
*sample
)
804 uint64_t monotonic_avg
, monotonic
[2], measure_delta
, realtime
;
805 uint64_t tcf
= trace_clock_freq();
806 struct timespec rts
= { 0, 0 };
809 monotonic
[0] = trace_clock_read64();
810 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
814 monotonic
[1] = trace_clock_read64();
815 measure_delta
= monotonic
[1] - monotonic
[0];
816 if (measure_delta
> sample
->measure_delta
) {
818 * Discard value if it took longer to read than the best
823 monotonic_avg
= (monotonic
[0] + monotonic
[1]) >> 1;
824 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
825 if (tcf
== NSEC_PER_SEC
) {
826 realtime
+= rts
.tv_nsec
;
828 realtime
+= (uint64_t) rts
.tv_nsec
* tcf
/ NSEC_PER_SEC
;
830 sample
->offset
= (int64_t) realtime
- monotonic_avg
;
831 sample
->measure_delta
= measure_delta
;
836 * Approximation of NTP time of day to clock monotonic correlation,
837 * taken at start of trace. Keep the measurement that took the less time
838 * to complete, thus removing imprecision caused by preemption.
839 * May return a negative offset.
842 int64_t measure_clock_offset(void)
845 struct offset_sample offset_best_sample
= {
847 .measure_delta
= UINT64_MAX
,
850 for (i
= 0; i
< NR_CLOCK_OFFSET_SAMPLES
; i
++) {
851 if (measure_single_clock_offset(&offset_best_sample
)) {
855 return offset_best_sample
.offset
;
859 * Should be called with session registry mutex held.
861 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
866 unsigned char *uuid_c
;
867 char uuid_s
[UUID_STR_LEN
],
868 clock_uuid_s
[UUID_STR_LEN
];
870 char hostname
[HOST_NAME_MAX
];
874 uuid_c
= session
->uuid
;
876 snprintf(uuid_s
, sizeof(uuid_s
),
877 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
878 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
879 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
880 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
881 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
884 ret
= lttng_metadata_printf(session
,
885 "/* CTF %u.%u */\n\n",
892 ret
= lttng_metadata_printf(session
,
893 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
894 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
895 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
896 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
897 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
898 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
899 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
905 " byte_order = %s;\n"
906 " packet.header := struct {\n"
908 " uint8_t uuid[16];\n"
909 " uint32_t stream_id;\n"
910 " uint64_t stream_instance_id;\n"
913 session
->uint8_t_alignment
,
914 session
->uint16_t_alignment
,
915 session
->uint32_t_alignment
,
916 session
->uint64_t_alignment
,
917 session
->bits_per_long
,
918 session
->long_alignment
,
922 session
->byte_order
== BIG_ENDIAN
? "be" : "le"
927 /* ignore error, just use empty string if error. */
929 ret
= gethostname(hostname
, sizeof(hostname
));
930 if (ret
&& errno
== ENAMETOOLONG
)
931 hostname
[HOST_NAME_MAX
- 1] = '\0';
932 ret
= lttng_metadata_printf(session
,
934 " hostname = \"%s\";\n"
935 " domain = \"ust\";\n"
936 " tracer_name = \"lttng-ust\";\n"
937 " tracer_major = %u;\n"
938 " tracer_minor = %u;\n",
947 * If per-application registry, we can output extra information
948 * about the application.
951 ret
= lttng_metadata_printf(session
,
952 " tracer_patchlevel = %u;\n"
954 " procname = \"%s\";\n",
955 app
->version
.patchlevel
,
963 ret
= lttng_metadata_printf(session
,
970 ret
= lttng_metadata_printf(session
,
978 if (!trace_clock_uuid(clock_uuid_s
)) {
979 ret
= lttng_metadata_printf(session
,
987 ret
= lttng_metadata_printf(session
,
988 " description = \"%s\";\n"
989 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
990 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
991 " offset = %" PRId64
";\n"
993 trace_clock_description(),
995 measure_clock_offset()
1000 ret
= lttng_metadata_printf(session
,
1001 "typealias integer {\n"
1002 " size = 27; align = 1; signed = false;\n"
1003 " map = clock.%s.value;\n"
1004 "} := uint27_clock_monotonic_t;\n"
1006 "typealias integer {\n"
1007 " size = 32; align = %u; signed = false;\n"
1008 " map = clock.%s.value;\n"
1009 "} := uint32_clock_monotonic_t;\n"
1011 "typealias integer {\n"
1012 " size = 64; align = %u; signed = false;\n"
1013 " map = clock.%s.value;\n"
1014 "} := uint64_clock_monotonic_t;\n\n",
1016 session
->uint32_t_alignment
,
1018 session
->uint64_t_alignment
,
1024 ret
= _lttng_stream_packet_context_declare(session
);
1028 ret
= _lttng_event_header_declare(session
);