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)))
41 int fls(unsigned int x
)
47 if (!(x
& 0xFFFF0000U
)) {
51 if (!(x
& 0xFF000000U
)) {
55 if (!(x
& 0xF0000000U
)) {
59 if (!(x
& 0xC0000000U
)) {
63 if (!(x
& 0x80000000U
)) {
71 int get_count_order(unsigned int count
)
75 order
= fls(count
) - 1;
76 if (count
& (count
- 1))
82 * Returns offset where to write in metadata array, or negative error value on error.
85 ssize_t
metadata_reserve(struct ust_registry_session
*session
, size_t len
)
87 size_t new_len
= session
->metadata_len
+ len
;
88 size_t new_alloc_len
= new_len
;
89 size_t old_alloc_len
= session
->metadata_alloc_len
;
92 if (new_alloc_len
> (UINT32_MAX
>> 1))
94 if ((old_alloc_len
<< 1) > (UINT32_MAX
>> 1))
97 if (new_alloc_len
> old_alloc_len
) {
101 max_t(size_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
102 newptr
= realloc(session
->metadata
, new_alloc_len
);
105 session
->metadata
= newptr
;
106 /* We zero directly the memory from start of allocation. */
107 memset(&session
->metadata
[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
108 session
->metadata_alloc_len
= new_alloc_len
;
110 ret
= session
->metadata_len
;
111 session
->metadata_len
+= len
;
116 * We have exclusive access to our metadata buffer (protected by the
117 * ust_lock), so we can do racy operations such as looking for
118 * remaining space left in packet and write, since mutual exclusion
119 * protects us from concurrent writes.
122 int lttng_metadata_printf(struct ust_registry_session
*session
,
123 const char *fmt
, ...)
132 ret
= vasprintf(&str
, fmt
, ap
);
138 offset
= metadata_reserve(session
, len
);
143 memcpy(&session
->metadata
[offset
], str
, len
);
144 DBG3("Append to metadata: \"%s\"", str
);
153 int _lttng_field_statedump(struct ust_registry_session
*session
,
154 const struct ustctl_field
*field
)
157 const char *bo_be
= " byte_order = be;";
158 const char *bo_le
= " byte_order = le;";
159 const char *bo_native
= "";
160 const char *bo_reverse
;
162 if (session
->byte_order
== BIG_ENDIAN
)
167 switch (field
->type
.atype
) {
168 case ustctl_atype_integer
:
169 ret
= lttng_metadata_printf(session
,
170 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
171 field
->type
.u
.basic
.integer
.size
,
172 field
->type
.u
.basic
.integer
.alignment
,
173 field
->type
.u
.basic
.integer
.signedness
,
174 (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_none
)
176 : (field
->type
.u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
179 field
->type
.u
.basic
.integer
.base
,
180 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
183 case ustctl_atype_float
:
184 ret
= lttng_metadata_printf(session
,
185 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
186 field
->type
.u
.basic
._float
.exp_dig
,
187 field
->type
.u
.basic
._float
.mant_dig
,
188 field
->type
.u
.basic
._float
.alignment
,
189 field
->type
.u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
192 case ustctl_atype_enum
:
194 case ustctl_atype_array
:
196 const struct ustctl_basic_type
*elem_type
;
198 elem_type
= &field
->type
.u
.array
.elem_type
;
199 ret
= lttng_metadata_printf(session
,
200 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
201 elem_type
->u
.basic
.integer
.size
,
202 elem_type
->u
.basic
.integer
.alignment
,
203 elem_type
->u
.basic
.integer
.signedness
,
204 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
206 : (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
209 elem_type
->u
.basic
.integer
.base
,
210 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
211 field
->name
, field
->type
.u
.array
.length
);
214 case ustctl_atype_sequence
:
216 const struct ustctl_basic_type
*elem_type
;
217 const struct ustctl_basic_type
*length_type
;
219 elem_type
= &field
->type
.u
.sequence
.elem_type
;
220 length_type
= &field
->type
.u
.sequence
.length_type
;
221 ret
= lttng_metadata_printf(session
,
222 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
223 length_type
->u
.basic
.integer
.size
,
224 (unsigned int) length_type
->u
.basic
.integer
.alignment
,
225 length_type
->u
.basic
.integer
.signedness
,
226 (length_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
228 : ((length_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
231 length_type
->u
.basic
.integer
.base
,
232 length_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
237 ret
= lttng_metadata_printf(session
,
238 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
239 elem_type
->u
.basic
.integer
.size
,
240 (unsigned int) elem_type
->u
.basic
.integer
.alignment
,
241 elem_type
->u
.basic
.integer
.signedness
,
242 (elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_none
)
244 : ((elem_type
->u
.basic
.integer
.encoding
== ustctl_encode_UTF8
)
247 elem_type
->u
.basic
.integer
.base
,
248 elem_type
->u
.basic
.integer
.reverse_byte_order
? bo_reverse
: bo_native
,
254 case ustctl_atype_string
:
255 /* Default encoding is UTF8 */
256 ret
= lttng_metadata_printf(session
,
258 field
->type
.u
.basic
.string
.encoding
== ustctl_encode_ASCII
?
259 " { encoding = ASCII; }" : "",
269 int _lttng_context_metadata_statedump(struct ust_registry_session
*session
,
270 size_t nr_ctx_fields
,
271 struct ustctl_field
*ctx
)
278 for (i
= 0; i
< nr_ctx_fields
; i
++) {
279 const struct ustctl_field
*field
= &ctx
[i
];
281 ret
= _lttng_field_statedump(session
, field
);
289 int _lttng_fields_metadata_statedump(struct ust_registry_session
*session
,
290 struct ust_registry_event
*event
)
295 for (i
= 0; i
< event
->nr_fields
; i
++) {
296 const struct ustctl_field
*field
= &event
->fields
[i
];
298 ret
= _lttng_field_statedump(session
, field
);
306 * Should be called with session registry mutex held.
308 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
309 struct ust_registry_channel
*chan
,
310 struct ust_registry_event
*event
)
314 /* Don't dump metadata events */
315 if (chan
->chan_id
== -1U)
318 ret
= lttng_metadata_printf(session
,
322 " stream_id = %u;\n",
329 ret
= lttng_metadata_printf(session
,
335 if (event
->model_emf_uri
) {
336 ret
= lttng_metadata_printf(session
,
337 " model.emf.uri = \"%s\";\n",
338 event
->model_emf_uri
);
343 #if 0 /* context for events not supported */
345 ret
= lttng_metadata_printf(session
,
346 " context := struct {\n");
350 ret
= _lttng_context_metadata_statedump(session
, event
->ctx
);
354 ret
= lttng_metadata_printf(session
,
360 ret
= lttng_metadata_printf(session
,
361 " fields := struct {\n"
366 ret
= _lttng_fields_metadata_statedump(session
, event
);
370 ret
= lttng_metadata_printf(session
,
375 event
->metadata_dumped
= 1;
382 * Should be called with session registry mutex held.
384 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
385 struct ust_registry_channel
*chan
)
389 /* Don't dump metadata events */
390 if (chan
->chan_id
== -1U)
393 if (!chan
->header_type
)
396 ret
= lttng_metadata_printf(session
,
399 " event.header := %s;\n"
400 " packet.context := struct packet_context;\n",
402 chan
->header_type
== USTCTL_CHANNEL_HEADER_COMPACT
?
403 "struct event_header_compact" :
404 "struct event_header_large");
408 if (chan
->ctx_fields
) {
409 ret
= lttng_metadata_printf(session
,
410 " event.context := struct {\n");
414 ret
= _lttng_context_metadata_statedump(session
,
419 if (chan
->ctx_fields
) {
420 ret
= lttng_metadata_printf(session
,
426 ret
= lttng_metadata_printf(session
,
428 /* Flag success of metadata dump. */
429 chan
->metadata_dumped
= 1;
436 int _lttng_stream_packet_context_declare(struct ust_registry_session
*session
)
438 return lttng_metadata_printf(session
,
439 "struct packet_context {\n"
440 " uint64_clock_monotonic_t timestamp_begin;\n"
441 " uint64_clock_monotonic_t timestamp_end;\n"
442 " uint64_t content_size;\n"
443 " uint64_t packet_size;\n"
444 " unsigned long events_discarded;\n"
445 " uint32_t cpu_id;\n"
453 * id 31 is reserved to indicate an extended header.
456 * id: range: 0 - 65534.
457 * id 65535 is reserved to indicate an extended header.
460 int _lttng_event_header_declare(struct ust_registry_session
*session
)
462 return lttng_metadata_printf(session
,
463 "struct event_header_compact {\n"
464 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
467 " uint27_clock_monotonic_t timestamp;\n"
471 " uint64_clock_monotonic_t timestamp;\n"
476 "struct event_header_large {\n"
477 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
480 " uint32_clock_monotonic_t timestamp;\n"
484 " uint64_clock_monotonic_t timestamp;\n"
488 session
->uint32_t_alignment
,
489 session
->uint16_t_alignment
494 * Approximation of NTP time of day to clock monotonic correlation,
495 * taken at start of trace.
496 * Yes, this is only an approximation. Yes, we can (and will) do better
497 * in future versions.
500 uint64_t measure_clock_offset(void)
502 uint64_t offset
, monotonic
[2], realtime
;
503 struct timespec rts
= { 0, 0 };
506 monotonic
[0] = trace_clock_read64();
507 ret
= clock_gettime(CLOCK_REALTIME
, &rts
);
510 monotonic
[1] = trace_clock_read64();
511 offset
= (monotonic
[0] + monotonic
[1]) >> 1;
512 realtime
= (uint64_t) rts
.tv_sec
* 1000000000ULL;
513 realtime
+= rts
.tv_nsec
;
514 offset
= realtime
- offset
;
520 * Should be called with session registry mutex held.
522 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
525 unsigned char *uuid_c
;
526 char uuid_s
[UUID_STR_LEN
],
527 clock_uuid_s
[UUID_STR_LEN
];
529 char hostname
[HOST_NAME_MAX
];
534 uuid_c
= session
->uuid
;
536 snprintf(uuid_s
, sizeof(uuid_s
),
537 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
538 uuid_c
[0], uuid_c
[1], uuid_c
[2], uuid_c
[3],
539 uuid_c
[4], uuid_c
[5], uuid_c
[6], uuid_c
[7],
540 uuid_c
[8], uuid_c
[9], uuid_c
[10], uuid_c
[11],
541 uuid_c
[12], uuid_c
[13], uuid_c
[14], uuid_c
[15]);
543 ret
= lttng_metadata_printf(session
,
544 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
545 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
546 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
547 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
548 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
549 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
550 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
556 " byte_order = %s;\n"
557 " packet.header := struct {\n"
559 " uint8_t uuid[16];\n"
560 " uint32_t stream_id;\n"
563 session
->uint8_t_alignment
,
564 session
->uint16_t_alignment
,
565 session
->uint32_t_alignment
,
566 session
->uint64_t_alignment
,
567 session
->bits_per_long
,
568 session
->long_alignment
,
572 session
->byte_order
== BIG_ENDIAN
? "be" : "le"
577 /* ignore error, just use empty string if error. */
579 ret
= gethostname(hostname
, sizeof(hostname
));
580 if (ret
&& errno
== ENAMETOOLONG
)
581 hostname
[HOST_NAME_MAX
- 1] = '\0';
582 ret
= lttng_metadata_printf(session
,
584 " hostname = \"%s\";\n"
585 " domain = \"ust\";\n"
586 " tracer_name = \"lttng-ust\";\n"
587 " tracer_major = %u;\n"
588 " tracer_minor = %u;\n"
589 " tracer_patchlevel = %u;\n",
593 app
->version
.patchlevel
599 * If per-application registry, we can output extra information
600 * about the application.
603 ret
= lttng_metadata_printf(session
,
605 " procname = \"%s\";\n",
613 ret
= lttng_metadata_printf(session
,
620 ret
= lttng_metadata_printf(session
,
628 if (!trace_clock_uuid(clock_uuid_s
)) {
629 ret
= lttng_metadata_printf(session
,
637 ret
= lttng_metadata_printf(session
,
638 " description = \"Monotonic Clock\";\n"
639 " freq = %" PRIu64
"; /* Frequency, in Hz */\n"
640 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
641 " offset = %" PRIu64
";\n"
644 measure_clock_offset()
649 ret
= lttng_metadata_printf(session
,
650 "typealias integer {\n"
651 " size = 27; align = 1; signed = false;\n"
652 " map = clock.monotonic.value;\n"
653 "} := uint27_clock_monotonic_t;\n"
655 "typealias integer {\n"
656 " size = 32; align = %u; signed = false;\n"
657 " map = clock.monotonic.value;\n"
658 "} := uint32_clock_monotonic_t;\n"
660 "typealias integer {\n"
661 " size = 64; align = %u; signed = false;\n"
662 " map = clock.monotonic.value;\n"
663 "} := uint64_clock_monotonic_t;\n\n",
664 session
->uint32_t_alignment
,
665 session
->uint64_t_alignment
670 ret
= _lttng_stream_packet_context_declare(session
);
674 ret
= _lttng_event_header_declare(session
);