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 NSEC_PER_SEC 1000000000ULL
42 #define NR_CLOCK_OFFSET_SAMPLES 10
44 struct offset_sample
{
45 uint64_t offset
; /* correlation offset */
46 uint64_t measure_delta
; /* lower is better */
50 int fls(unsigned int x
)
56 if (!(x
& 0xFFFF0000U
)) {
60 if (!(x
& 0xFF000000U
)) {
64 if (!(x
& 0xF0000000U
)) {
68 if (!(x
& 0xC0000000U
)) {
72 if (!(x
& 0x80000000U
)) {
80 int get_count_order(unsigned int count
)
84 order
= fls(count
) - 1;
85 if (count
& (count
- 1))
91 * Returns offset where to write in metadata array, or negative error value on error.
94 ssize_t
metadata_reserve(struct ust_registry_session
*session
, size_t len
)
96 size_t new_len
= session
->metadata_len
+ len
;
97 size_t new_alloc_len
= new_len
;
98 size_t old_alloc_len
= session
->metadata_alloc_len
;
101 if (new_alloc_len
> (UINT32_MAX
>> 1))
103 if ((old_alloc_len
<< 1) > (UINT32_MAX
>> 1))
106 if (new_alloc_len
> old_alloc_len
) {
110 max_t(size_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
111 newptr
= realloc(session
->metadata
, new_alloc_len
);
114 session
->metadata
= newptr
;
115 /* We zero directly the memory from start of allocation. */
116 memset(&session
->metadata
[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
117 session
->metadata_alloc_len
= new_alloc_len
;
119 ret
= session
->metadata_len
;
120 session
->metadata_len
+= len
;
125 int metadata_file_append(struct ust_registry_session
*session
,
126 const char *str
, size_t len
)
130 if (session
->metadata_fd
< 0) {
133 /* Write to metadata file */
134 written
= lttng_write(session
->metadata_fd
, str
, len
);
135 if (written
!= len
) {
142 * We have exclusive access to our metadata buffer (protected by the
143 * ust_lock), so we can do racy operations such as looking for
144 * remaining space left in packet and write, since mutual exclusion
145 * protects us from concurrent writes.
148 int lttng_metadata_printf(struct ust_registry_session
*session
,
149 const char *fmt
, ...)
158 ret
= vasprintf(&str
, fmt
, ap
);
164 offset
= metadata_reserve(session
, len
);
169 memcpy(&session
->metadata
[offset
], str
, len
);
170 ret
= metadata_file_append(session
, str
, len
);
172 PERROR("Error appending to metadata file");
175 DBG3("Append to metadata: \"%s\"", str
);
184 int _lttng_field_statedump(struct ust_registry_session
*session
,
185 const struct ustctl_field
*field
)
188 const char *bo_be
= " byte_order = be;";
189 const char *bo_le
= " byte_order = le;";
190 const char *bo_native
= "";
191 const char *bo_reverse
;
193 if (session
->byte_order
== BIG_ENDIAN
)
198 switch (field
->type
.atype
) {
199 case ustctl_atype_integer
:
200 ret
= lttng_metadata_printf(session
,
201 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
202 field
->type
.u
.basic
.integer
.size
,
203 field
->type
.u
.basic
.integer
.alignment
,
204 field
->type
.u
.basic
.integer
.signedness
,
205 (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_none
)
207 : (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
210 field
->type
.u
.basic
.integer
.base
,
211 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
214 case ustctl_atype_float
:
215 ret
= lttng_metadata_printf(session
,
216 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
217 field
->type
.u
.basic
._float
.exp_dig
,
218 field
->type
.u
.basic
._float
.mant_dig
,
219 field
->type
.u
.basic
._float
.alignment
,
220 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
223 case ustctl_atype_enum
:
225 case ustctl_atype_array
:
227 const struct ustctl_basic_type
*elem_type
;
229 elem_type
= &field
->type
.u
.array
.elem_type
;
230 ret
= lttng_metadata_printf(session
,
231 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
232 elem_type
->u
.basic
.integer
.size
,
233 elem_type
->u
.basic
.integer
.alignment
,
234 elem_type
->u
.basic
.integer
.signedness
,
235 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
237 : (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
240 elem_type
->u
.basic
.integer
.base
,
241 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
242 field
->name
, field
->type
.u
.array
.length
);
245 case ustctl_atype_sequence
:
247 const struct ustctl_basic_type
*elem_type
;
248 const struct ustctl_basic_type
*length_type
;
250 elem_type
= &field
->type
.u
.sequence
.elem_type
;
251 length_type
= &field
->type
.u
.sequence
.length_type
;
252 ret
= lttng_metadata_printf(session
,
253 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
254 length_type
->u
.basic
.integer
.size
,
255 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
256 length_type
->u
.basic
.integer
.signedness
,
257 (length_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
259 : ((length_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
262 length_type
->u
.basic
.integer
.base
,
263 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
268 ret
= lttng_metadata_printf(session
,
269 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
270 elem_type
->u
.basic
.integer
.size
,
271 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
272 elem_type
->u
.basic
.integer
.signedness
,
273 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
275 : ((elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
278 elem_type
->u
.basic
.integer
.base
,
279 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
285 case ustctl_atype_string
:
286 /* Default encoding is UTF8 */
287 ret
= lttng_metadata_printf(session
,
289 field
->type
.u
.basic
.string
.encoding
== ustctl_encode_ASCII
?
290 " { encoding = ASCII; }" : "",
300 int _lttng_context_metadata_statedump(struct ust_registry_session
*session
,
301 size_t nr_ctx_fields
,
302 struct ustctl_field
*ctx
)
309 for (i
= 0; i
< nr_ctx_fields
; i
++) {
310 const struct ustctl_field
*field
= &ctx
[i
];
312 ret
= _lttng_field_statedump(session
, field
);
320 int _lttng_fields_metadata_statedump(struct ust_registry_session
*session
,
321 struct ust_registry_event
*event
)
326 for (i
= 0; i
< event
->nr_fields
; i
++) {
327 const struct ustctl_field
*field
= &event
->fields
[i
];
329 ret
= _lttng_field_statedump(session
, field
);
337 * Should be called with session registry mutex held.
339 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
340 struct ust_registry_channel
*chan
,
341 struct ust_registry_event
*event
)
345 /* Don't dump metadata events */
346 if (chan
->chan_id
== -1U)
349 ret
= lttng_metadata_printf(session
,
353 " stream_id = %u;\n",
360 ret
= lttng_metadata_printf(session
,
362 event
->loglevel_value
);
366 if (event
->model_emf_uri
) {
367 ret
= lttng_metadata_printf(session
,
368 " model.emf.uri = \"%s\";\n",
369 event
->model_emf_uri
);
374 ret
= lttng_metadata_printf(session
,
375 " fields := struct {\n"
380 ret
= _lttng_fields_metadata_statedump(session
, event
);
384 ret
= lttng_metadata_printf(session
,
389 event
->metadata_dumped
= 1;
396 * Should be called with session registry mutex held.
398 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
399 struct ust_registry_channel
*chan
)
403 /* Don't dump metadata events */
404 if (chan
->chan_id
== -1U)
407 if (!chan
->header_type
)
410 ret
= lttng_metadata_printf(session
,
413 " event.header := %s;\n"
414 " packet.context := struct packet_context;\n",
416 chan
->header_type
== USTCTL_CHANNEL_HEADER_COMPACT
?
417 "struct event_header_compact" :
418 "struct event_header_large");
422 if (chan
->ctx_fields
) {
423 ret
= lttng_metadata_printf(session
,
424 " event.context := struct {\n");
428 ret
= _lttng_context_metadata_statedump(session
,
433 if (chan
->ctx_fields
) {
434 ret
= lttng_metadata_printf(session
,
440 ret
= lttng_metadata_printf(session
,
442 /* Flag success of metadata dump. */
443 chan
->metadata_dumped
= 1;
450 int _lttng_stream_packet_context_declare(struct ust_registry_session
*session
)
452 return lttng_metadata_printf(session
,
453 "struct packet_context {\n"
454 " uint64_clock_monotonic_t timestamp_begin;\n"
455 " uint64_clock_monotonic_t timestamp_end;\n"
456 " uint64_t content_size;\n"
457 " uint64_t packet_size;\n"
458 " unsigned long events_discarded;\n"
459 " uint32_t cpu_id;\n"
467 * id 31 is reserved to indicate an extended header.
470 * id: range: 0 - 65534.
471 * id 65535 is reserved to indicate an extended header.
474 int _lttng_event_header_declare(struct ust_registry_session
*session
)
476 return lttng_metadata_printf(session
,
477 "struct event_header_compact {\n"
478 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
481 " uint27_clock_monotonic_t timestamp;\n"
485 " uint64_clock_monotonic_t timestamp;\n"
490 "struct event_header_large {\n"
491 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
494 " uint32_clock_monotonic_t timestamp;\n"
498 " uint64_clock_monotonic_t timestamp;\n"
502 session
->uint32_t_alignment
,
503 session
->uint16_t_alignment
508 int measure_single_clock_offset(struct offset_sample
*sample
)
510 uint64_t offset
, monotonic
[2], measure_delta
, realtime
;
511 uint64_t tcf
= trace_clock_freq();
512 struct timespec rts
= { 0, 0 };
515 monotonic
[0] = trace_clock_read64();
516 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
520 monotonic
[1] = trace_clock_read64();
521 measure_delta
= monotonic
[1] - monotonic
[0];
522 if (measure_delta
> sample
->measure_delta
) {
524 * Discard value if it took longer to read than the best
529 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
530 realtime
= (uint64_t) rts
.tv_sec
* tcf
;
531 if (tcf
== NSEC_PER_SEC
) {
532 realtime
+= rts
.tv_nsec
;
534 realtime
+= (uint64_t) rts
.tv_nsec
* tcf
/ NSEC_PER_SEC
;
536 offset
= realtime
- offset
;
537 sample
->offset
= offset
;
538 sample
->measure_delta
= measure_delta
;
543 * Approximation of NTP time of day to clock monotonic correlation,
544 * taken at start of trace. Keep the measurement that took the less time
545 * to complete, thus removing imprecision caused by preemption.
548 uint64_t measure_clock_offset(void)
551 struct offset_sample offset_best_sample
= {
553 .measure_delta
= UINT64_MAX
,
556 for (i
= 0; i
< NR_CLOCK_OFFSET_SAMPLES
; i
++) {
557 if (measure_single_clock_offset(&offset_best_sample
)) {
561 return offset_best_sample
.offset
;
565 * Should be called with session registry mutex held.
567 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
572 unsigned char *uuid_c
;
573 char uuid_s
[UUID_STR_LEN
],
574 clock_uuid_s
[UUID_STR_LEN
];
576 char hostname
[HOST_NAME_MAX
];
580 uuid_c
= session
->uuid
;
582 snprintf(uuid_s
, sizeof(uuid_s
),
583 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
584 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
585 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
586 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
587 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
590 ret
= lttng_metadata_printf(session
,
591 "/* CTF %u.%u */\n\n",
598 ret
= lttng_metadata_printf(session
,
599 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
600 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
601 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
602 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
603 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
604 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
605 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
611 " byte_order = %s;\n"
612 " packet.header := struct {\n"
614 " uint8_t uuid[16];\n"
615 " uint32_t stream_id;\n"
618 session
->uint8_t_alignment
,
619 session
->uint16_t_alignment
,
620 session
->uint32_t_alignment
,
621 session
->uint64_t_alignment
,
622 session
->bits_per_long
,
623 session
->long_alignment
,
627 session
->byte_order
== BIG_ENDIAN
? "be" : "le"
632 /* ignore error, just use empty string if error. */
634 ret
= gethostname(hostname
, sizeof(hostname
));
635 if (ret
&& errno
== ENAMETOOLONG
)
636 hostname
[HOST_NAME_MAX
- 1] = '\0';
637 ret
= lttng_metadata_printf(session
,
639 " hostname = \"%s\";\n"
640 " domain = \"ust\";\n"
641 " tracer_name = \"lttng-ust\";\n"
642 " tracer_major = %u;\n"
643 " tracer_minor = %u;\n",
652 * If per-application registry, we can output extra information
653 * about the application.
656 ret
= lttng_metadata_printf(session
,
657 " tracer_patchlevel = %u;\n"
659 " procname = \"%s\";\n",
660 app
->version
.patchlevel
,
668 ret
= lttng_metadata_printf(session
,
675 ret
= lttng_metadata_printf(session
,
683 if (!trace_clock_uuid(clock_uuid_s
)) {
684 ret
= lttng_metadata_printf(session
,
692 ret
= lttng_metadata_printf(session
,
693 " description = \"%s\";\n"
694 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
695 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
696 " offset = %" PRIu64
";\n"
698 trace_clock_description(),
700 measure_clock_offset()
705 ret
= lttng_metadata_printf(session
,
706 "typealias integer {\n"
707 " size = 27; align = 1; signed = false;\n"
708 " map = clock.%s.value;\n"
709 "} := uint27_clock_monotonic_t;\n"
711 "typealias integer {\n"
712 " size = 32; align = %u; signed = false;\n"
713 " map = clock.%s.value;\n"
714 "} := uint32_clock_monotonic_t;\n"
716 "typealias integer {\n"
717 " size = 64; align = %u; signed = false;\n"
718 " map = clock.%s.value;\n"
719 "} := uint64_clock_monotonic_t;\n\n",
721 session
->uint32_t_alignment
,
723 session
->uint64_t_alignment
,
729 ret
= _lttng_stream_packet_context_declare(session
);
733 ret
= _lttng_event_header_declare(session
);