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 * We have exclusive access to our metadata buffer (protected by the
125 * ust_lock), so we can do racy operations such as looking for
126 * remaining space left in packet and write, since mutual exclusion
127 * protects us from concurrent writes.
130 int lttng_metadata_printf(struct ust_registry_session
*session
,
131 const char *fmt
, ...)
140 ret
= vasprintf(&str
, fmt
, ap
);
146 offset
= metadata_reserve(session
, len
);
151 memcpy(&session
->metadata
[offset
], str
, len
);
152 DBG3("Append to metadata: \"%s\"", str
);
161 int _lttng_field_statedump(struct ust_registry_session
*session
,
162 const struct ustctl_field
*field
)
165 const char *bo_be
= " byte_order = be;";
166 const char *bo_le
= " byte_order = le;";
167 const char *bo_native
= "";
168 const char *bo_reverse
;
170 if (session
->byte_order
== BIG_ENDIAN
)
175 switch (field
->type
.atype
) {
176 case ustctl_atype_integer
:
177 ret
= lttng_metadata_printf(session
,
178 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
179 field
->type
.u
.basic
.integer
.size
,
180 field
->type
.u
.basic
.integer
.alignment
,
181 field
->type
.u
.basic
.integer
.signedness
,
182 (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_none
)
184 : (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
187 field
->type
.u
.basic
.integer
.base
,
188 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
191 case ustctl_atype_float
:
192 ret
= lttng_metadata_printf(session
,
193 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
194 field
->type
.u
.basic
._float
.exp_dig
,
195 field
->type
.u
.basic
._float
.mant_dig
,
196 field
->type
.u
.basic
._float
.alignment
,
197 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
200 case ustctl_atype_enum
:
202 case ustctl_atype_array
:
204 const struct ustctl_basic_type
*elem_type
;
206 elem_type
= &field
->type
.u
.array
.elem_type
;
207 ret
= lttng_metadata_printf(session
,
208 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
209 elem_type
->u
.basic
.integer
.size
,
210 elem_type
->u
.basic
.integer
.alignment
,
211 elem_type
->u
.basic
.integer
.signedness
,
212 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
214 : (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
217 elem_type
->u
.basic
.integer
.base
,
218 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
219 field
->name
, field
->type
.u
.array
.length
);
222 case ustctl_atype_sequence
:
224 const struct ustctl_basic_type
*elem_type
;
225 const struct ustctl_basic_type
*length_type
;
227 elem_type
= &field
->type
.u
.sequence
.elem_type
;
228 length_type
= &field
->type
.u
.sequence
.length_type
;
229 ret
= lttng_metadata_printf(session
,
230 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
231 length_type
->u
.basic
.integer
.size
,
232 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
233 length_type
->u
.basic
.integer
.signedness
,
234 (length_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
236 : ((length_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
239 length_type
->u
.basic
.integer
.base
,
240 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
245 ret
= lttng_metadata_printf(session
,
246 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
247 elem_type
->u
.basic
.integer
.size
,
248 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
249 elem_type
->u
.basic
.integer
.signedness
,
250 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
252 : ((elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
255 elem_type
->u
.basic
.integer
.base
,
256 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
262 case ustctl_atype_string
:
263 /* Default encoding is UTF8 */
264 ret
= lttng_metadata_printf(session
,
266 field
->type
.u
.basic
.string
.encoding
== ustctl_encode_ASCII
?
267 " { encoding = ASCII; }" : "",
277 int _lttng_context_metadata_statedump(struct ust_registry_session
*session
,
278 size_t nr_ctx_fields
,
279 struct ustctl_field
*ctx
)
286 for (i
= 0; i
< nr_ctx_fields
; i
++) {
287 const struct ustctl_field
*field
= &ctx
[i
];
289 ret
= _lttng_field_statedump(session
, field
);
297 int _lttng_fields_metadata_statedump(struct ust_registry_session
*session
,
298 struct ust_registry_event
*event
)
303 for (i
= 0; i
< event
->nr_fields
; i
++) {
304 const struct ustctl_field
*field
= &event
->fields
[i
];
306 ret
= _lttng_field_statedump(session
, field
);
314 * Should be called with session registry mutex held.
316 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
317 struct ust_registry_channel
*chan
,
318 struct ust_registry_event
*event
)
322 /* Don't dump metadata events */
323 if (chan
->chan_id
== -1U)
326 ret
= lttng_metadata_printf(session
,
330 " stream_id = %u;\n",
337 ret
= lttng_metadata_printf(session
,
343 if (event
->model_emf_uri
) {
344 ret
= lttng_metadata_printf(session
,
345 " model.emf.uri = \"%s\";\n",
346 event
->model_emf_uri
);
351 #if 0 /* context for events not supported */
353 ret
= lttng_metadata_printf(session
,
354 " context := struct {\n");
358 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
362 ret
= lttng_metadata_printf(session
,
368 ret
= lttng_metadata_printf(session
,
369 " fields := struct {\n"
374 ret
= _lttng_fields_metadata_statedump(session
, event
);
378 ret
= lttng_metadata_printf(session
,
383 event
->metadata_dumped
= 1;
390 * Should be called with session registry mutex held.
392 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
393 struct ust_registry_channel
*chan
)
397 /* Don't dump metadata events */
398 if (chan
->chan_id
== -1U)
401 if (!chan
->header_type
)
404 ret
= lttng_metadata_printf(session
,
407 " event.header := %s;\n"
408 " packet.context := struct packet_context;\n",
410 chan
->header_type
== USTCTL_CHANNEL_HEADER_COMPACT
?
411 "struct event_header_compact" :
412 "struct event_header_large");
416 if (chan
->ctx_fields
) {
417 ret
= lttng_metadata_printf(session
,
418 " event.context := struct {\n");
422 ret
= _lttng_context_metadata_statedump(session
,
427 if (chan
->ctx_fields
) {
428 ret
= lttng_metadata_printf(session
,
434 ret
= lttng_metadata_printf(session
,
436 /* Flag success of metadata dump. */
437 chan
->metadata_dumped
= 1;
444 int _lttng_stream_packet_context_declare(struct ust_registry_session
*session
)
446 return lttng_metadata_printf(session
,
447 "struct packet_context {\n"
448 " uint64_clock_monotonic_t timestamp_begin;\n"
449 " uint64_clock_monotonic_t timestamp_end;\n"
450 " uint64_t content_size;\n"
451 " uint64_t packet_size;\n"
452 " unsigned long events_discarded;\n"
453 " uint32_t cpu_id;\n"
461 * id 31 is reserved to indicate an extended header.
464 * id: range: 0 - 65534.
465 * id 65535 is reserved to indicate an extended header.
468 int _lttng_event_header_declare(struct ust_registry_session
*session
)
470 return lttng_metadata_printf(session
,
471 "struct event_header_compact {\n"
472 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
475 " uint27_clock_monotonic_t timestamp;\n"
479 " uint64_clock_monotonic_t timestamp;\n"
484 "struct event_header_large {\n"
485 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
488 " uint32_clock_monotonic_t timestamp;\n"
492 " uint64_clock_monotonic_t timestamp;\n"
496 session
->uint32_t_alignment
,
497 session
->uint16_t_alignment
502 int measure_single_clock_offset(struct offset_sample
*sample
)
504 uint64_t offset
, monotonic
[2], measure_delta
, realtime
;
505 struct timespec rts
= { 0, 0 };
508 monotonic
[0] = trace_clock_read64();
509 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
513 monotonic
[1] = trace_clock_read64();
514 measure_delta
= monotonic
[1] - monotonic
[0];
515 if (measure_delta
> sample
->measure_delta
) {
517 * Discard value if it took longer to read than the best
522 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
523 realtime
= (uint64_t) rts
.tv_sec
* 1000000000ULL;
524 realtime
+= rts
.tv_nsec
;
525 offset
= realtime
- offset
;
526 sample
->offset
= offset
;
527 sample
->measure_delta
= measure_delta
;
532 * Approximation of NTP time of day to clock monotonic correlation,
533 * taken at start of trace. Keep the measurement that took the less time
534 * to complete, thus removing imprecision caused by preemption.
537 uint64_t measure_clock_offset(void)
540 struct offset_sample offset_best_sample
= {
542 .measure_delta
= UINT64_MAX
,
545 for (i
= 0; i
< NR_CLOCK_OFFSET_SAMPLES
; i
++) {
546 if (measure_single_clock_offset(&offset_best_sample
)) {
550 return offset_best_sample
.offset
;
554 * Should be called with session registry mutex held.
556 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
561 unsigned char *uuid_c
;
562 char uuid_s
[UUID_STR_LEN
],
563 clock_uuid_s
[UUID_STR_LEN
];
565 char hostname
[HOST_NAME_MAX
];
569 uuid_c
= session
->uuid
;
571 snprintf(uuid_s
, sizeof(uuid_s
),
572 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
573 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
574 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
575 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
576 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
578 ret
= lttng_metadata_printf(session
,
579 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
580 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
581 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
582 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
583 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
584 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
585 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
591 " byte_order = %s;\n"
592 " packet.header := struct {\n"
594 " uint8_t uuid[16];\n"
595 " uint32_t stream_id;\n"
598 session
->uint8_t_alignment
,
599 session
->uint16_t_alignment
,
600 session
->uint32_t_alignment
,
601 session
->uint64_t_alignment
,
602 session
->bits_per_long
,
603 session
->long_alignment
,
607 session
->byte_order
== BIG_ENDIAN
? "be" : "le"
612 /* ignore error, just use empty string if error. */
614 ret
= gethostname(hostname
, sizeof(hostname
));
615 if (ret
&& errno
== ENAMETOOLONG
)
616 hostname
[HOST_NAME_MAX
- 1] = '\0';
617 ret
= lttng_metadata_printf(session
,
619 " hostname = \"%s\";\n"
620 " domain = \"ust\";\n"
621 " tracer_name = \"lttng-ust\";\n"
622 " tracer_major = %u;\n"
623 " tracer_minor = %u;\n",
632 * If per-application registry, we can output extra information
633 * about the application.
636 ret
= lttng_metadata_printf(session
,
637 " tracer_patchlevel = %u;\n"
639 " procname = \"%s\";\n",
640 app
->version
.patchlevel
,
648 ret
= lttng_metadata_printf(session
,
655 ret
= lttng_metadata_printf(session
,
663 if (!trace_clock_uuid(clock_uuid_s
)) {
664 ret
= lttng_metadata_printf(session
,
672 ret
= lttng_metadata_printf(session
,
673 " description = \"Monotonic Clock\";\n"
674 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
675 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
676 " offset = %" PRIu64
";\n"
679 measure_clock_offset()
684 ret
= lttng_metadata_printf(session
,
685 "typealias integer {\n"
686 " size = 27; align = 1; signed = false;\n"
687 " map = clock.monotonic.value;\n"
688 "} := uint27_clock_monotonic_t;\n"
690 "typealias integer {\n"
691 " size = 32; align = %u; signed = false;\n"
692 " map = clock.monotonic.value;\n"
693 "} := uint32_clock_monotonic_t;\n"
695 "typealias integer {\n"
696 " size = 64; align = %u; signed = false;\n"
697 " map = clock.monotonic.value;\n"
698 "} := uint64_clock_monotonic_t;\n\n",
699 session
->uint32_t_alignment
,
700 session
->uint64_t_alignment
705 ret
= _lttng_stream_packet_context_declare(session
);
709 ret
= _lttng_event_header_declare(session
);