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.
31 #include <common/common.h>
33 #include "ust-registry.h"
34 #include "ust-clock.h"
38 #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
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
);
183 int _lttng_field_statedump(struct ust_registry_session
*session
,
184 const struct ustctl_field
*field
)
187 const char *bo_be
= " byte_order = be;";
188 const char *bo_le
= " byte_order = le;";
189 const char *bo_native
= "";
190 const char *bo_reverse
;
192 if (session
->byte_order
== BIG_ENDIAN
)
197 switch (field
->type
.atype
) {
198 case ustctl_atype_integer
:
199 ret
= lttng_metadata_printf(session
,
200 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
201 field
->type
.u
.basic
.integer
.size
,
202 field
->type
.u
.basic
.integer
.alignment
,
203 field
->type
.u
.basic
.integer
.signedness
,
204 (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_none
)
206 : (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
209 field
->type
.u
.basic
.integer
.base
,
210 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
213 case ustctl_atype_float
:
214 ret
= lttng_metadata_printf(session
,
215 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
216 field
->type
.u
.basic
._float
.exp_dig
,
217 field
->type
.u
.basic
._float
.mant_dig
,
218 field
->type
.u
.basic
._float
.alignment
,
219 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
222 case ustctl_atype_enum
:
224 case ustctl_atype_array
:
226 const struct ustctl_basic_type
*elem_type
;
228 elem_type
= &field
->type
.u
.array
.elem_type
;
229 ret
= lttng_metadata_printf(session
,
230 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
231 elem_type
->u
.basic
.integer
.size
,
232 elem_type
->u
.basic
.integer
.alignment
,
233 elem_type
->u
.basic
.integer
.signedness
,
234 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
236 : (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
239 elem_type
->u
.basic
.integer
.base
,
240 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
241 field
->name
, field
->type
.u
.array
.length
);
244 case ustctl_atype_sequence
:
246 const struct ustctl_basic_type
*elem_type
;
247 const struct ustctl_basic_type
*length_type
;
249 elem_type
= &field
->type
.u
.sequence
.elem_type
;
250 length_type
= &field
->type
.u
.sequence
.length_type
;
251 ret
= lttng_metadata_printf(session
,
252 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
253 length_type
->u
.basic
.integer
.size
,
254 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
255 length_type
->u
.basic
.integer
.signedness
,
256 (length_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
258 : ((length_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
261 length_type
->u
.basic
.integer
.base
,
262 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
267 ret
= lttng_metadata_printf(session
,
268 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
269 elem_type
->u
.basic
.integer
.size
,
270 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
271 elem_type
->u
.basic
.integer
.signedness
,
272 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
274 : ((elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
277 elem_type
->u
.basic
.integer
.base
,
278 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
284 case ustctl_atype_string
:
285 /* Default encoding is UTF8 */
286 ret
= lttng_metadata_printf(session
,
288 field
->type
.u
.basic
.string
.encoding
== ustctl_encode_ASCII
?
289 " { encoding = ASCII; }" : "",
299 int _lttng_context_metadata_statedump(struct ust_registry_session
*session
,
300 size_t nr_ctx_fields
,
301 struct ustctl_field
*ctx
)
308 for (i
= 0; i
< nr_ctx_fields
; i
++) {
309 const struct ustctl_field
*field
= &ctx
[i
];
311 ret
= _lttng_field_statedump(session
, field
);
319 int _lttng_fields_metadata_statedump(struct ust_registry_session
*session
,
320 struct ust_registry_event
*event
)
325 for (i
= 0; i
< event
->nr_fields
; i
++) {
326 const struct ustctl_field
*field
= &event
->fields
[i
];
328 ret
= _lttng_field_statedump(session
, field
);
336 * Should be called with session registry mutex held.
338 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
339 struct ust_registry_channel
*chan
,
340 struct ust_registry_event
*event
)
344 /* Don't dump metadata events */
345 if (chan
->chan_id
== -1U)
348 ret
= lttng_metadata_printf(session
,
352 " stream_id = %u;\n",
359 ret
= lttng_metadata_printf(session
,
365 if (event
->model_emf_uri
) {
366 ret
= lttng_metadata_printf(session
,
367 " model.emf.uri = \"%s\";\n",
368 event
->model_emf_uri
);
373 #if 0 /* context for events not supported */
375 ret
= lttng_metadata_printf(session
,
376 " context := struct {\n");
380 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
384 ret
= lttng_metadata_printf(session
,
390 ret
= lttng_metadata_printf(session
,
391 " fields := struct {\n"
396 ret
= _lttng_fields_metadata_statedump(session
, event
);
400 ret
= lttng_metadata_printf(session
,
405 event
->metadata_dumped
= 1;
412 * Should be called with session registry mutex held.
414 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
415 struct ust_registry_channel
*chan
)
419 /* Don't dump metadata events */
420 if (chan
->chan_id
== -1U)
423 if (!chan
->header_type
)
426 ret
= lttng_metadata_printf(session
,
429 " event.header := %s;\n"
430 " packet.context := struct packet_context;\n",
432 chan
->header_type
== USTCTL_CHANNEL_HEADER_COMPACT
?
433 "struct event_header_compact" :
434 "struct event_header_large");
438 if (chan
->ctx_fields
) {
439 ret
= lttng_metadata_printf(session
,
440 " event.context := struct {\n");
444 ret
= _lttng_context_metadata_statedump(session
,
449 if (chan
->ctx_fields
) {
450 ret
= lttng_metadata_printf(session
,
456 ret
= lttng_metadata_printf(session
,
458 /* Flag success of metadata dump. */
459 chan
->metadata_dumped
= 1;
466 int _lttng_stream_packet_context_declare(struct ust_registry_session
*session
)
468 return lttng_metadata_printf(session
,
469 "struct packet_context {\n"
470 " uint64_clock_monotonic_t timestamp_begin;\n"
471 " uint64_clock_monotonic_t timestamp_end;\n"
472 " uint64_t content_size;\n"
473 " uint64_t packet_size;\n"
474 " unsigned long events_discarded;\n"
475 " uint32_t cpu_id;\n"
483 * id 31 is reserved to indicate an extended header.
486 * id: range: 0 - 65534.
487 * id 65535 is reserved to indicate an extended header.
490 int _lttng_event_header_declare(struct ust_registry_session
*session
)
492 return lttng_metadata_printf(session
,
493 "struct event_header_compact {\n"
494 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
497 " uint27_clock_monotonic_t timestamp;\n"
501 " uint64_clock_monotonic_t timestamp;\n"
506 "struct event_header_large {\n"
507 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
510 " uint32_clock_monotonic_t timestamp;\n"
514 " uint64_clock_monotonic_t timestamp;\n"
518 session
->uint32_t_alignment
,
519 session
->uint16_t_alignment
524 int measure_single_clock_offset(struct offset_sample
*sample
)
526 uint64_t offset
, monotonic
[2], measure_delta
, realtime
;
527 struct timespec rts
= { 0, 0 };
530 monotonic
[0] = trace_clock_read64();
531 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
535 monotonic
[1] = trace_clock_read64();
536 measure_delta
= monotonic
[1] - monotonic
[0];
537 if (measure_delta
> sample
->measure_delta
) {
539 * Discard value if it took longer to read than the best
544 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
545 realtime
= (uint64_t) rts
.tv_sec
* 1000000000ULL;
546 realtime
+= rts
.tv_nsec
;
547 offset
= realtime
- offset
;
548 sample
->offset
= offset
;
549 sample
->measure_delta
= measure_delta
;
554 * Approximation of NTP time of day to clock monotonic correlation,
555 * taken at start of trace. Keep the measurement that took the less time
556 * to complete, thus removing imprecision caused by preemption.
559 uint64_t measure_clock_offset(void)
562 struct offset_sample offset_best_sample
= {
564 .measure_delta
= UINT64_MAX
,
567 for (i
= 0; i
< NR_CLOCK_OFFSET_SAMPLES
; i
++) {
568 if (measure_single_clock_offset(&offset_best_sample
)) {
572 return offset_best_sample
.offset
;
576 * Should be called with session registry mutex held.
578 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
583 unsigned char *uuid_c
;
584 char uuid_s
[UUID_STR_LEN
],
585 clock_uuid_s
[UUID_STR_LEN
];
587 char hostname
[HOST_NAME_MAX
];
591 uuid_c
= session
->uuid
;
593 snprintf(uuid_s
, sizeof(uuid_s
),
594 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
595 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
596 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
597 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
598 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
601 ret
= lttng_metadata_printf(session
,
602 "/* CTF %u.%u */\n\n",
609 ret
= lttng_metadata_printf(session
,
610 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
611 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
612 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
613 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
614 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
615 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
616 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
622 " byte_order = %s;\n"
623 " packet.header := struct {\n"
625 " uint8_t uuid[16];\n"
626 " uint32_t stream_id;\n"
629 session
->uint8_t_alignment
,
630 session
->uint16_t_alignment
,
631 session
->uint32_t_alignment
,
632 session
->uint64_t_alignment
,
633 session
->bits_per_long
,
634 session
->long_alignment
,
638 session
->byte_order
== BIG_ENDIAN
? "be" : "le"
643 /* ignore error, just use empty string if error. */
645 ret
= gethostname(hostname
, sizeof(hostname
));
646 if (ret
&& errno
== ENAMETOOLONG
)
647 hostname
[HOST_NAME_MAX
- 1] = '\0';
648 ret
= lttng_metadata_printf(session
,
650 " hostname = \"%s\";\n"
651 " domain = \"ust\";\n"
652 " tracer_name = \"lttng-ust\";\n"
653 " tracer_major = %u;\n"
654 " tracer_minor = %u;\n",
663 * If per-application registry, we can output extra information
664 * about the application.
667 ret
= lttng_metadata_printf(session
,
668 " tracer_patchlevel = %u;\n"
670 " procname = \"%s\";\n",
671 app
->version
.patchlevel
,
679 ret
= lttng_metadata_printf(session
,
686 ret
= lttng_metadata_printf(session
,
694 if (!trace_clock_uuid(clock_uuid_s
)) {
695 ret
= lttng_metadata_printf(session
,
703 ret
= lttng_metadata_printf(session
,
704 " description = \"Monotonic Clock\";\n"
705 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
706 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
707 " offset = %" PRIu64
";\n"
710 measure_clock_offset()
715 ret
= lttng_metadata_printf(session
,
716 "typealias integer {\n"
717 " size = 27; align = 1; signed = false;\n"
718 " map = clock.monotonic.value;\n"
719 "} := uint27_clock_monotonic_t;\n"
721 "typealias integer {\n"
722 " size = 32; align = %u; signed = false;\n"
723 " map = clock.monotonic.value;\n"
724 "} := uint32_clock_monotonic_t;\n"
726 "typealias integer {\n"
727 " size = 64; align = %u; signed = false;\n"
728 " map = clock.monotonic.value;\n"
729 "} := uint64_clock_monotonic_t;\n\n",
730 session
->uint32_t_alignment
,
731 session
->uint64_t_alignment
736 ret
= _lttng_stream_packet_context_declare(session
);
740 ret
= _lttng_event_header_declare(session
);