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 uint64_t offset
; /* correlation offset */
45 uint64_t measure_delta
; /* lower is better */
49 int fls(unsigned int x
)
55 if (!(x
& 0xFFFF0000U
)) {
59 if (!(x
& 0xFF000000U
)) {
63 if (!(x
& 0xF0000000U
)) {
67 if (!(x
& 0xC0000000U
)) {
71 if (!(x
& 0x80000000U
)) {
79 int get_count_order(unsigned int count
)
83 order
= fls(count
) - 1;
84 if (count
& (count
- 1))
90 * Returns offset where to write in metadata array, or negative error value on error.
93 ssize_t
metadata_reserve(struct ust_registry_session
*session
, size_t len
)
95 size_t new_len
= session
->metadata_len
+ len
;
96 size_t new_alloc_len
= new_len
;
97 size_t old_alloc_len
= session
->metadata_alloc_len
;
100 if (new_alloc_len
> (UINT32_MAX
>> 1))
102 if ((old_alloc_len
<< 1) > (UINT32_MAX
>> 1))
105 if (new_alloc_len
> old_alloc_len
) {
109 max_t(size_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
110 newptr
= realloc(session
->metadata
, new_alloc_len
);
113 session
->metadata
= newptr
;
114 /* We zero directly the memory from start of allocation. */
115 memset(&session
->metadata
[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
116 session
->metadata_alloc_len
= new_alloc_len
;
118 ret
= session
->metadata_len
;
119 session
->metadata_len
+= len
;
124 int metadata_file_append(struct ust_registry_session
*session
,
125 const char *str
, size_t len
)
129 if (session
->metadata_fd
< 0) {
132 /* Write to metadata file */
133 written
= lttng_write(session
->metadata_fd
, str
, len
);
134 if (written
!= len
) {
141 * We have exclusive access to our metadata buffer (protected by the
142 * ust_lock), so we can do racy operations such as looking for
143 * remaining space left in packet and write, since mutual exclusion
144 * protects us from concurrent writes.
147 int lttng_metadata_printf(struct ust_registry_session
*session
,
148 const char *fmt
, ...)
157 ret
= vasprintf(&str
, fmt
, ap
);
163 offset
= metadata_reserve(session
, len
);
168 memcpy(&session
->metadata
[offset
], str
, len
);
169 ret
= metadata_file_append(session
, str
, len
);
171 PERROR("Error appending to metadata file");
174 DBG3("Append to metadata: \"%s\"", str
);
182 /* Called with session registry mutex held. */
184 int ust_metadata_enum_statedump(struct ust_registry_session
*session
,
185 const char *enum_name
,
187 const struct ustctl_integer_type
*container_type
,
188 const char *field_name
)
190 struct ust_registry_enum
*reg_enum
;
191 const struct ustctl_enum_entry
*entries
;
197 reg_enum
= ust_registry_lookup_enum_by_id(session
, enum_name
, enum_id
);
199 /* reg_enum can still be used because session registry mutex is held. */
204 entries
= reg_enum
->entries
;
205 nr_entries
= reg_enum
->nr_entries
;
207 ret
= lttng_metadata_printf(session
,
208 " enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n",
209 container_type
->size
,
210 container_type
->alignment
,
211 container_type
->signedness
,
212 (container_type
->encoding
== ustctl_encode_none
)
214 : (container_type
->encoding
== ustctl_encode_UTF8
)
217 container_type
->base
);
221 /* Dump all entries */
222 for (i
= 0; i
< nr_entries
; i
++) {
223 const struct ustctl_enum_entry
*entry
= &entries
[i
];
226 ret
= lttng_metadata_printf(session
,
231 len
= strlen(entry
->string
);
232 /* Escape the character '"' */
233 for (j
= 0; j
< len
; j
++) {
234 char c
= entry
->string
[j
];
238 ret
= lttng_metadata_printf(session
,
242 ret
= lttng_metadata_printf(session
,
246 ret
= lttng_metadata_printf(session
,
254 ret
= lttng_metadata_printf(session
,
259 if (entry
->start
== entry
->end
) {
260 ret
= lttng_metadata_printf(session
,
264 ret
= lttng_metadata_printf(session
,
266 entry
->start
, entry
->end
);
272 ret
= lttng_metadata_printf(session
, " } _%s;\n",
279 int _lttng_field_statedump(struct ust_registry_session
*session
,
280 const struct ustctl_field
*field
)
283 const char *bo_be
= " byte_order = be;";
284 const char *bo_le
= " byte_order = le;";
285 const char *bo_native
= "";
286 const char *bo_reverse
;
288 if (session
->byte_order
== BIG_ENDIAN
)
293 switch (field
->type
.atype
) {
294 case ustctl_atype_integer
:
295 ret
= lttng_metadata_printf(session
,
296 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
297 field
->type
.u
.basic
.integer
.size
,
298 field
->type
.u
.basic
.integer
.alignment
,
299 field
->type
.u
.basic
.integer
.signedness
,
300 (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_none
)
302 : (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
305 field
->type
.u
.basic
.integer
.base
,
306 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
309 case ustctl_atype_enum
:
310 ret
= ust_metadata_enum_statedump(session
,
311 field
->type
.u
.basic
.enumeration
.name
,
312 field
->type
.u
.basic
.enumeration
.id
,
313 &field
->type
.u
.basic
.enumeration
.container_type
,
316 case ustctl_atype_float
:
317 ret
= lttng_metadata_printf(session
,
318 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
319 field
->type
.u
.basic
._float
.exp_dig
,
320 field
->type
.u
.basic
._float
.mant_dig
,
321 field
->type
.u
.basic
._float
.alignment
,
322 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
325 case ustctl_atype_array
:
327 const struct ustctl_basic_type
*elem_type
;
329 elem_type
= &field
->type
.u
.array
.elem_type
;
330 ret
= lttng_metadata_printf(session
,
331 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
332 elem_type
->u
.basic
.integer
.size
,
333 elem_type
->u
.basic
.integer
.alignment
,
334 elem_type
->u
.basic
.integer
.signedness
,
335 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
337 : (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
340 elem_type
->u
.basic
.integer
.base
,
341 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
342 field
->name
, field
->type
.u
.array
.length
);
345 case ustctl_atype_sequence
:
347 const struct ustctl_basic_type
*elem_type
;
348 const struct ustctl_basic_type
*length_type
;
350 elem_type
= &field
->type
.u
.sequence
.elem_type
;
351 length_type
= &field
->type
.u
.sequence
.length_type
;
352 ret
= lttng_metadata_printf(session
,
353 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
354 length_type
->u
.basic
.integer
.size
,
355 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
356 length_type
->u
.basic
.integer
.signedness
,
357 (length_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
359 : ((length_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
362 length_type
->u
.basic
.integer
.base
,
363 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
368 ret
= lttng_metadata_printf(session
,
369 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
370 elem_type
->u
.basic
.integer
.size
,
371 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
372 elem_type
->u
.basic
.integer
.signedness
,
373 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
375 : ((elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
378 elem_type
->u
.basic
.integer
.base
,
379 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
385 case ustctl_atype_string
:
386 /* Default encoding is UTF8 */
387 ret
= lttng_metadata_printf(session
,
389 field
->type
.u
.basic
.string
.encoding
== ustctl_encode_ASCII
?
390 " { encoding = ASCII; }" : "",
400 int _lttng_context_metadata_statedump(struct ust_registry_session
*session
,
401 size_t nr_ctx_fields
,
402 struct ustctl_field
*ctx
)
409 for (i
= 0; i
< nr_ctx_fields
; i
++) {
410 const struct ustctl_field
*field
= &ctx
[i
];
412 ret
= _lttng_field_statedump(session
, field
);
420 int _lttng_fields_metadata_statedump(struct ust_registry_session
*session
,
421 struct ust_registry_event
*event
)
426 for (i
= 0; i
< event
->nr_fields
; i
++) {
427 const struct ustctl_field
*field
= &event
->fields
[i
];
429 ret
= _lttng_field_statedump(session
, field
);
437 * Should be called with session registry mutex held.
439 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
440 struct ust_registry_channel
*chan
,
441 struct ust_registry_event
*event
)
445 /* Don't dump metadata events */
446 if (chan
->chan_id
== -1U)
449 ret
= lttng_metadata_printf(session
,
453 " stream_id = %u;\n",
460 ret
= lttng_metadata_printf(session
,
462 event
->loglevel_value
);
466 if (event
->model_emf_uri
) {
467 ret
= lttng_metadata_printf(session
,
468 " model.emf.uri = \"%s\";\n",
469 event
->model_emf_uri
);
474 ret
= lttng_metadata_printf(session
,
475 " fields := struct {\n"
480 ret
= _lttng_fields_metadata_statedump(session
, event
);
484 ret
= lttng_metadata_printf(session
,
489 event
->metadata_dumped
= 1;
496 * Should be called with session registry mutex held.
498 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
499 struct ust_registry_channel
*chan
)
503 /* Don't dump metadata events */
504 if (chan
->chan_id
== -1U)
507 if (!chan
->header_type
)
510 ret
= lttng_metadata_printf(session
,
513 " event.header := %s;\n"
514 " packet.context := struct packet_context;\n",
516 chan
->header_type
== USTCTL_CHANNEL_HEADER_COMPACT
?
517 "struct event_header_compact" :
518 "struct event_header_large");
522 if (chan
->ctx_fields
) {
523 ret
= lttng_metadata_printf(session
,
524 " event.context := struct {\n");
528 ret
= _lttng_context_metadata_statedump(session
,
533 if (chan
->ctx_fields
) {
534 ret
= lttng_metadata_printf(session
,
540 ret
= lttng_metadata_printf(session
,
542 /* Flag success of metadata dump. */
543 chan
->metadata_dumped
= 1;
550 int _lttng_stream_packet_context_declare(struct ust_registry_session
*session
)
552 return lttng_metadata_printf(session
,
553 "struct packet_context {\n"
554 " uint64_clock_monotonic_t timestamp_begin;\n"
555 " uint64_clock_monotonic_t timestamp_end;\n"
556 " uint64_t content_size;\n"
557 " uint64_t packet_size;\n"
558 " unsigned long events_discarded;\n"
559 " uint32_t cpu_id;\n"
567 * id 31 is reserved to indicate an extended header.
570 * id: range: 0 - 65534.
571 * id 65535 is reserved to indicate an extended header.
574 int _lttng_event_header_declare(struct ust_registry_session
*session
)
576 return lttng_metadata_printf(session
,
577 "struct event_header_compact {\n"
578 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
581 " uint27_clock_monotonic_t timestamp;\n"
585 " uint64_clock_monotonic_t timestamp;\n"
590 "struct event_header_large {\n"
591 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
594 " uint32_clock_monotonic_t timestamp;\n"
598 " uint64_clock_monotonic_t timestamp;\n"
602 session
->uint32_t_alignment
,
603 session
->uint16_t_alignment
608 int measure_single_clock_offset(struct offset_sample
*sample
)
610 uint64_t offset
, monotonic
[2], measure_delta
, realtime
;
611 uint64_t tcf
= trace_clock_freq();
612 struct timespec rts
= { 0, 0 };
615 monotonic
[0] = trace_clock_read64();
616 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
620 monotonic
[1] = trace_clock_read64();
621 measure_delta
= monotonic
[1] - monotonic
[0];
622 if (measure_delta
> sample
->measure_delta
) {
624 * Discard value if it took longer to read than the best
629 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
630 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
631 if (tcf
== NSEC_PER_SEC
) {
632 realtime
+= rts
.tv_nsec
;
634 realtime
+= (uint64_t) rts
.tv_nsec
* tcf
/ NSEC_PER_SEC
;
636 offset
= realtime
- offset
;
637 sample
->offset
= offset
;
638 sample
->measure_delta
= measure_delta
;
643 * Approximation of NTP time of day to clock monotonic correlation,
644 * taken at start of trace. Keep the measurement that took the less time
645 * to complete, thus removing imprecision caused by preemption.
648 uint64_t measure_clock_offset(void)
651 struct offset_sample offset_best_sample
= {
653 .measure_delta
= UINT64_MAX
,
656 for (i
= 0; i
< NR_CLOCK_OFFSET_SAMPLES
; i
++) {
657 if (measure_single_clock_offset(&offset_best_sample
)) {
661 return offset_best_sample
.offset
;
665 * Should be called with session registry mutex held.
667 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
672 unsigned char *uuid_c
;
673 char uuid_s
[UUID_STR_LEN
],
674 clock_uuid_s
[UUID_STR_LEN
];
676 char hostname
[HOST_NAME_MAX
];
680 uuid_c
= session
->uuid
;
682 snprintf(uuid_s
, sizeof(uuid_s
),
683 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
684 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
685 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
686 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
687 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
690 ret
= lttng_metadata_printf(session
,
691 "/* CTF %u.%u */\n\n",
698 ret
= lttng_metadata_printf(session
,
699 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
700 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
701 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
702 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
703 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
704 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
705 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
711 " byte_order = %s;\n"
712 " packet.header := struct {\n"
714 " uint8_t uuid[16];\n"
715 " uint32_t stream_id;\n"
718 session
->uint8_t_alignment
,
719 session
->uint16_t_alignment
,
720 session
->uint32_t_alignment
,
721 session
->uint64_t_alignment
,
722 session
->bits_per_long
,
723 session
->long_alignment
,
727 session
->byte_order
== BIG_ENDIAN
? "be" : "le"
732 /* ignore error, just use empty string if error. */
734 ret
= gethostname(hostname
, sizeof(hostname
));
735 if (ret
&& errno
== ENAMETOOLONG
)
736 hostname
[HOST_NAME_MAX
- 1] = '\0';
737 ret
= lttng_metadata_printf(session
,
739 " hostname = \"%s\";\n"
740 " domain = \"ust\";\n"
741 " tracer_name = \"lttng-ust\";\n"
742 " tracer_major = %u;\n"
743 " tracer_minor = %u;\n",
752 * If per-application registry, we can output extra information
753 * about the application.
756 ret
= lttng_metadata_printf(session
,
757 " tracer_patchlevel = %u;\n"
759 " procname = \"%s\";\n",
760 app
->version
.patchlevel
,
768 ret
= lttng_metadata_printf(session
,
775 ret
= lttng_metadata_printf(session
,
783 if (!trace_clock_uuid(clock_uuid_s
)) {
784 ret
= lttng_metadata_printf(session
,
792 ret
= lttng_metadata_printf(session
,
793 " description = \"%s\";\n"
794 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
795 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
796 " offset = %" PRIu64
";\n"
798 trace_clock_description(),
800 measure_clock_offset()
805 ret
= lttng_metadata_printf(session
,
806 "typealias integer {\n"
807 " size = 27; align = 1; signed = false;\n"
808 " map = clock.%s.value;\n"
809 "} := uint27_clock_monotonic_t;\n"
811 "typealias integer {\n"
812 " size = 32; align = %u; signed = false;\n"
813 " map = clock.%s.value;\n"
814 "} := uint32_clock_monotonic_t;\n"
816 "typealias integer {\n"
817 " size = 64; align = %u; signed = false;\n"
818 " map = clock.%s.value;\n"
819 "} := uint64_clock_monotonic_t;\n\n",
821 session
->uint32_t_alignment
,
823 session
->uint64_t_alignment
,
829 ret
= _lttng_stream_packet_context_declare(session
);
833 ret
= _lttng_event_header_declare(session
);