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 NR_CLOCK_OFFSET_SAMPLES 10
42 struct offset_sample
{
43 uint64_t offset
; /* correlation offset */
44 uint64_t measure_delta
; /* lower is better */
48 int fls(unsigned int x
)
54 if (!(x
& 0xFFFF0000U
)) {
58 if (!(x
& 0xFF000000U
)) {
62 if (!(x
& 0xF0000000U
)) {
66 if (!(x
& 0xC0000000U
)) {
70 if (!(x
& 0x80000000U
)) {
78 int get_count_order(unsigned int count
)
82 order
= fls(count
) - 1;
83 if (count
& (count
- 1))
89 * Returns offset where to write in metadata array, or negative error value on error.
92 ssize_t
metadata_reserve(struct ust_registry_session
*session
, size_t len
)
94 size_t new_len
= session
->metadata_len
+ len
;
95 size_t new_alloc_len
= new_len
;
96 size_t old_alloc_len
= session
->metadata_alloc_len
;
99 if (new_alloc_len
> (UINT32_MAX
>> 1))
101 if ((old_alloc_len
<< 1) > (UINT32_MAX
>> 1))
104 if (new_alloc_len
> old_alloc_len
) {
108 max_t(size_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
109 newptr
= realloc(session
->metadata
, new_alloc_len
);
112 session
->metadata
= newptr
;
113 /* We zero directly the memory from start of allocation. */
114 memset(&session
->metadata
[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
115 session
->metadata_alloc_len
= new_alloc_len
;
117 ret
= session
->metadata_len
;
118 session
->metadata_len
+= len
;
123 * We have exclusive access to our metadata buffer (protected by the
124 * ust_lock), so we can do racy operations such as looking for
125 * remaining space left in packet and write, since mutual exclusion
126 * protects us from concurrent writes.
129 int lttng_metadata_printf(struct ust_registry_session
*session
,
130 const char *fmt
, ...)
139 ret
= vasprintf(&str
, fmt
, ap
);
145 offset
= metadata_reserve(session
, len
);
150 memcpy(&session
->metadata
[offset
], str
, len
);
151 DBG3("Append to metadata: \"%s\"", str
);
160 int _lttng_field_statedump(struct ust_registry_session
*session
,
161 const struct ustctl_field
*field
)
164 const char *bo_be
= " byte_order = be;";
165 const char *bo_le
= " byte_order = le;";
166 const char *bo_native
= "";
167 const char *bo_reverse
;
169 if (session
->byte_order
== BIG_ENDIAN
)
174 switch (field
->type
.atype
) {
175 case ustctl_atype_integer
:
176 ret
= lttng_metadata_printf(session
,
177 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
178 field
->type
.u
.basic
.integer
.size
,
179 field
->type
.u
.basic
.integer
.alignment
,
180 field
->type
.u
.basic
.integer
.signedness
,
181 (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_none
)
183 : (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
186 field
->type
.u
.basic
.integer
.base
,
187 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
190 case ustctl_atype_float
:
191 ret
= lttng_metadata_printf(session
,
192 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
193 field
->type
.u
.basic
._float
.exp_dig
,
194 field
->type
.u
.basic
._float
.mant_dig
,
195 field
->type
.u
.basic
._float
.alignment
,
196 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
199 case ustctl_atype_enum
:
201 case ustctl_atype_array
:
203 const struct ustctl_basic_type
*elem_type
;
205 elem_type
= &field
->type
.u
.array
.elem_type
;
206 ret
= lttng_metadata_printf(session
,
207 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
208 elem_type
->u
.basic
.integer
.size
,
209 elem_type
->u
.basic
.integer
.alignment
,
210 elem_type
->u
.basic
.integer
.signedness
,
211 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
213 : (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
216 elem_type
->u
.basic
.integer
.base
,
217 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
218 field
->name
, field
->type
.u
.array
.length
);
221 case ustctl_atype_sequence
:
223 const struct ustctl_basic_type
*elem_type
;
224 const struct ustctl_basic_type
*length_type
;
226 elem_type
= &field
->type
.u
.sequence
.elem_type
;
227 length_type
= &field
->type
.u
.sequence
.length_type
;
228 ret
= lttng_metadata_printf(session
,
229 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
230 length_type
->u
.basic
.integer
.size
,
231 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
232 length_type
->u
.basic
.integer
.signedness
,
233 (length_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
235 : ((length_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
238 length_type
->u
.basic
.integer
.base
,
239 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
244 ret
= lttng_metadata_printf(session
,
245 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
246 elem_type
->u
.basic
.integer
.size
,
247 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
248 elem_type
->u
.basic
.integer
.signedness
,
249 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
251 : ((elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
254 elem_type
->u
.basic
.integer
.base
,
255 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
261 case ustctl_atype_string
:
262 /* Default encoding is UTF8 */
263 ret
= lttng_metadata_printf(session
,
265 field
->type
.u
.basic
.string
.encoding
== ustctl_encode_ASCII
?
266 " { encoding = ASCII; }" : "",
276 int _lttng_context_metadata_statedump(struct ust_registry_session
*session
,
277 size_t nr_ctx_fields
,
278 struct ustctl_field
*ctx
)
285 for (i
= 0; i
< nr_ctx_fields
; i
++) {
286 const struct ustctl_field
*field
= &ctx
[i
];
288 ret
= _lttng_field_statedump(session
, field
);
296 int _lttng_fields_metadata_statedump(struct ust_registry_session
*session
,
297 struct ust_registry_event
*event
)
302 for (i
= 0; i
< event
->nr_fields
; i
++) {
303 const struct ustctl_field
*field
= &event
->fields
[i
];
305 ret
= _lttng_field_statedump(session
, field
);
313 * Should be called with session registry mutex held.
315 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
316 struct ust_registry_channel
*chan
,
317 struct ust_registry_event
*event
)
321 /* Don't dump metadata events */
322 if (chan
->chan_id
== -1U)
325 ret
= lttng_metadata_printf(session
,
329 " stream_id = %u;\n",
336 ret
= lttng_metadata_printf(session
,
342 if (event
->model_emf_uri
) {
343 ret
= lttng_metadata_printf(session
,
344 " model.emf.uri = \"%s\";\n",
345 event
->model_emf_uri
);
350 #if 0 /* context for events not supported */
352 ret
= lttng_metadata_printf(session
,
353 " context := struct {\n");
357 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
361 ret
= lttng_metadata_printf(session
,
367 ret
= lttng_metadata_printf(session
,
368 " fields := struct {\n"
373 ret
= _lttng_fields_metadata_statedump(session
, event
);
377 ret
= lttng_metadata_printf(session
,
382 event
->metadata_dumped
= 1;
389 * Should be called with session registry mutex held.
391 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
392 struct ust_registry_channel
*chan
)
396 /* Don't dump metadata events */
397 if (chan
->chan_id
== -1U)
400 if (!chan
->header_type
)
403 ret
= lttng_metadata_printf(session
,
406 " event.header := %s;\n"
407 " packet.context := struct packet_context;\n",
409 chan
->header_type
== USTCTL_CHANNEL_HEADER_COMPACT
?
410 "struct event_header_compact" :
411 "struct event_header_large");
415 if (chan
->ctx_fields
) {
416 ret
= lttng_metadata_printf(session
,
417 " event.context := struct {\n");
421 ret
= _lttng_context_metadata_statedump(session
,
426 if (chan
->ctx_fields
) {
427 ret
= lttng_metadata_printf(session
,
433 ret
= lttng_metadata_printf(session
,
435 /* Flag success of metadata dump. */
436 chan
->metadata_dumped
= 1;
443 int _lttng_stream_packet_context_declare(struct ust_registry_session
*session
)
445 return lttng_metadata_printf(session
,
446 "struct packet_context {\n"
447 " uint64_clock_monotonic_t timestamp_begin;\n"
448 " uint64_clock_monotonic_t timestamp_end;\n"
449 " uint64_t content_size;\n"
450 " uint64_t packet_size;\n"
451 " unsigned long events_discarded;\n"
452 " uint32_t cpu_id;\n"
460 * id 31 is reserved to indicate an extended header.
463 * id: range: 0 - 65534.
464 * id 65535 is reserved to indicate an extended header.
467 int _lttng_event_header_declare(struct ust_registry_session
*session
)
469 return lttng_metadata_printf(session
,
470 "struct event_header_compact {\n"
471 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
474 " uint27_clock_monotonic_t timestamp;\n"
478 " uint64_clock_monotonic_t timestamp;\n"
483 "struct event_header_large {\n"
484 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
487 " uint32_clock_monotonic_t timestamp;\n"
491 " uint64_clock_monotonic_t timestamp;\n"
495 session
->uint32_t_alignment
,
496 session
->uint16_t_alignment
501 int measure_single_clock_offset(struct offset_sample
*sample
)
503 uint64_t offset
, monotonic
[2], measure_delta
, realtime
;
504 struct timespec rts
= { 0, 0 };
507 monotonic
[0] = trace_clock_read64();
508 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
512 monotonic
[1] = trace_clock_read64();
513 measure_delta
= monotonic
[1] - monotonic
[0];
514 if (measure_delta
> sample
->measure_delta
) {
516 * Discard value if it took longer to read than the best
521 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
522 realtime
= (uint64_t) rts
.tv_sec
* 1000000000ULL;
523 realtime
+= rts
.tv_nsec
;
524 offset
= realtime
- offset
;
525 sample
->offset
= offset
;
526 sample
->measure_delta
= measure_delta
;
531 * Approximation of NTP time of day to clock monotonic correlation,
532 * taken at start of trace. Keep the measurement that took the less time
533 * to complete, thus removing imprecision caused by preemption.
536 uint64_t measure_clock_offset(void)
539 struct offset_sample offset_best_sample
= {
541 .measure_delta
= UINT64_MAX
,
544 for (i
= 0; i
< NR_CLOCK_OFFSET_SAMPLES
; i
++) {
545 if (measure_single_clock_offset(&offset_best_sample
)) {
549 return offset_best_sample
.offset
;
553 * Should be called with session registry mutex held.
555 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
560 unsigned char *uuid_c
;
561 char uuid_s
[UUID_STR_LEN
],
562 clock_uuid_s
[UUID_STR_LEN
];
564 char hostname
[HOST_NAME_MAX
];
568 uuid_c
= session
->uuid
;
570 snprintf(uuid_s
, sizeof(uuid_s
),
571 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
572 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
573 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
574 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
575 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
577 ret
= lttng_metadata_printf(session
,
578 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
579 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
580 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
581 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
582 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
583 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
584 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
590 " byte_order = %s;\n"
591 " packet.header := struct {\n"
593 " uint8_t uuid[16];\n"
594 " uint32_t stream_id;\n"
597 session
->uint8_t_alignment
,
598 session
->uint16_t_alignment
,
599 session
->uint32_t_alignment
,
600 session
->uint64_t_alignment
,
601 session
->bits_per_long
,
602 session
->long_alignment
,
606 session
->byte_order
== BIG_ENDIAN
? "be" : "le"
611 /* ignore error, just use empty string if error. */
613 ret
= gethostname(hostname
, sizeof(hostname
));
614 if (ret
&& errno
== ENAMETOOLONG
)
615 hostname
[HOST_NAME_MAX
- 1] = '\0';
616 ret
= lttng_metadata_printf(session
,
618 " hostname = \"%s\";\n"
619 " domain = \"ust\";\n"
620 " tracer_name = \"lttng-ust\";\n"
621 " tracer_major = %u;\n"
622 " tracer_minor = %u;\n",
631 * If per-application registry, we can output extra information
632 * about the application.
635 ret
= lttng_metadata_printf(session
,
636 " tracer_patchlevel = %u;\n"
638 " procname = \"%s\";\n",
639 app
->version
.patchlevel
,
647 ret
= lttng_metadata_printf(session
,
654 ret
= lttng_metadata_printf(session
,
662 if (!trace_clock_uuid(clock_uuid_s
)) {
663 ret
= lttng_metadata_printf(session
,
671 ret
= lttng_metadata_printf(session
,
672 " description = \"Monotonic Clock\";\n"
673 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
674 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
675 " offset = %" PRIu64
";\n"
678 measure_clock_offset()
683 ret
= lttng_metadata_printf(session
,
684 "typealias integer {\n"
685 " size = 27; align = 1; signed = false;\n"
686 " map = clock.monotonic.value;\n"
687 "} := uint27_clock_monotonic_t;\n"
689 "typealias integer {\n"
690 " size = 32; align = %u; signed = false;\n"
691 " map = clock.monotonic.value;\n"
692 "} := uint32_clock_monotonic_t;\n"
694 "typealias integer {\n"
695 " size = 64; align = %u; signed = false;\n"
696 " map = clock.monotonic.value;\n"
697 "} := uint64_clock_monotonic_t;\n\n",
698 session
->uint32_t_alignment
,
699 session
->uint64_t_alignment
704 ret
= _lttng_stream_packet_context_declare(session
);
708 ret
= _lttng_event_header_declare(session
);