Fix: missing type definitions in mi-lttng-3.0.xsd
[lttng-tools.git] / src / bin / lttng-sessiond / ust-metadata.c
CommitLineData
d0b96690
DG
1/*
2 * ust-metadata.c
3 *
4 * LTTng-UST metadata generation
5 *
6 * Copyright (C) 2010-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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.
11 *
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.
16 *
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.
20 */
21
6c1c0768 22#define _LGPL_SOURCE
d0b96690
DG
23#include <stdint.h>
24#include <string.h>
25#include <stdarg.h>
26#include <stdio.h>
27#include <limits.h>
28#include <unistd.h>
29#include <inttypes.h>
30#include <common/common.h>
395d6b02 31#include <common/time.h>
d0b96690
DG
32
33#include "ust-registry.h"
34#include "ust-clock.h"
35#include "ust-app.h"
36
37#ifndef max_t
38#define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
39#endif
40
8c645bb0
MD
41#define NR_CLOCK_OFFSET_SAMPLES 10
42
43struct offset_sample {
c2636b57 44 int64_t offset; /* correlation offset */
8c645bb0
MD
45 uint64_t measure_delta; /* lower is better */
46};
47
da860cab
MD
48static
49int _lttng_field_statedump(struct ust_registry_session *session,
50 const struct ustctl_field *fields, size_t nr_fields,
51 size_t *iter_field, size_t nesting);
52
d0b96690
DG
53static inline
54int fls(unsigned int x)
55{
56 int r = 32;
57
58 if (!x)
59 return 0;
60 if (!(x & 0xFFFF0000U)) {
61 x <<= 16;
62 r -= 16;
63 }
64 if (!(x & 0xFF000000U)) {
65 x <<= 8;
66 r -= 8;
67 }
68 if (!(x & 0xF0000000U)) {
69 x <<= 4;
70 r -= 4;
71 }
72 if (!(x & 0xC0000000U)) {
73 x <<= 2;
74 r -= 2;
75 }
76 if (!(x & 0x80000000U)) {
d0b96690
DG
77 r -= 1;
78 }
79 return r;
80}
81
82static inline
83int get_count_order(unsigned int count)
84{
85 int order;
86
87 order = fls(count) - 1;
88 if (count & (count - 1))
89 order++;
90 return order;
91}
92
93/*
94 * Returns offset where to write in metadata array, or negative error value on error.
95 */
96static
97ssize_t metadata_reserve(struct ust_registry_session *session, size_t len)
98{
99 size_t new_len = session->metadata_len + len;
100 size_t new_alloc_len = new_len;
101 size_t old_alloc_len = session->metadata_alloc_len;
102 ssize_t ret;
103
104 if (new_alloc_len > (UINT32_MAX >> 1))
105 return -EINVAL;
106 if ((old_alloc_len << 1) > (UINT32_MAX >> 1))
107 return -EINVAL;
108
109 if (new_alloc_len > old_alloc_len) {
110 char *newptr;
111
112 new_alloc_len =
113 max_t(size_t, 1U << get_count_order(new_alloc_len), old_alloc_len << 1);
114 newptr = realloc(session->metadata, new_alloc_len);
115 if (!newptr)
116 return -ENOMEM;
117 session->metadata = newptr;
118 /* We zero directly the memory from start of allocation. */
119 memset(&session->metadata[old_alloc_len], 0, new_alloc_len - old_alloc_len);
120 session->metadata_alloc_len = new_alloc_len;
121 }
122 ret = session->metadata_len;
123 session->metadata_len += len;
124 return ret;
125}
126
d7ba1388
MD
127static
128int metadata_file_append(struct ust_registry_session *session,
129 const char *str, size_t len)
130{
131 ssize_t written;
132
133 if (session->metadata_fd < 0) {
134 return 0;
135 }
136 /* Write to metadata file */
137 written = lttng_write(session->metadata_fd, str, len);
138 if (written != len) {
139 return -1;
140 }
141 return 0;
142}
143
d0b96690
DG
144/*
145 * We have exclusive access to our metadata buffer (protected by the
146 * ust_lock), so we can do racy operations such as looking for
147 * remaining space left in packet and write, since mutual exclusion
148 * protects us from concurrent writes.
149 */
150static
151int lttng_metadata_printf(struct ust_registry_session *session,
152 const char *fmt, ...)
153{
154 char *str = NULL;
155 size_t len;
156 va_list ap;
157 ssize_t offset;
158 int ret;
159
160 va_start(ap, fmt);
161 ret = vasprintf(&str, fmt, ap);
162 va_end(ap);
163 if (ret < 0)
164 return -ENOMEM;
165
166 len = strlen(str);
167 offset = metadata_reserve(session, len);
168 if (offset < 0) {
169 ret = offset;
170 goto end;
171 }
172 memcpy(&session->metadata[offset], str, len);
d7ba1388
MD
173 ret = metadata_file_append(session, str, len);
174 if (ret) {
175 PERROR("Error appending to metadata file");
176 goto end;
177 }
d0b96690
DG
178 DBG3("Append to metadata: \"%s\"", str);
179 ret = 0;
180
181end:
182 free(str);
183 return ret;
184}
185
da860cab
MD
186static
187int print_tabs(struct ust_registry_session *session, size_t nesting)
188{
189 size_t i;
190
191 for (i = 0; i < nesting; i++) {
192 int ret;
193
194 ret = lttng_metadata_printf(session, " ");
195 if (ret) {
196 return ret;
197 }
198 }
199 return 0;
200}
201
a1f68b22
MD
202static
203void sanitize_ctf_identifier(char *out, const char *in)
204{
205 size_t i;
206
207 for (i = 0; i < LTTNG_UST_SYM_NAME_LEN; i++) {
208 switch (in[i]) {
209 case '.':
210 case '$':
211 case ':':
212 out[i] = '_';
213 break;
214 default:
215 out[i] = in[i];
216 }
217 }
218}
219
10b56aef
MD
220/* Called with session registry mutex held. */
221static
222int ust_metadata_enum_statedump(struct ust_registry_session *session,
223 const char *enum_name,
224 uint64_t enum_id,
225 const struct ustctl_integer_type *container_type,
da860cab 226 const char *field_name, size_t *iter_field, size_t nesting)
10b56aef
MD
227{
228 struct ust_registry_enum *reg_enum;
229 const struct ustctl_enum_entry *entries;
230 size_t nr_entries;
231 int ret = 0;
232 size_t i;
a1f68b22 233 char identifier[LTTNG_UST_SYM_NAME_LEN];
10b56aef
MD
234
235 rcu_read_lock();
236 reg_enum = ust_registry_lookup_enum_by_id(session, enum_name, enum_id);
237 rcu_read_unlock();
238 /* reg_enum can still be used because session registry mutex is held. */
239 if (!reg_enum) {
240 ret = -ENOENT;
241 goto end;
242 }
243 entries = reg_enum->entries;
244 nr_entries = reg_enum->nr_entries;
245
da860cab
MD
246 ret = print_tabs(session, nesting);
247 if (ret) {
248 goto end;
249 }
10b56aef 250 ret = lttng_metadata_printf(session,
da860cab 251 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n",
10b56aef
MD
252 container_type->size,
253 container_type->alignment,
254 container_type->signedness,
255 (container_type->encoding == ustctl_encode_none)
256 ? "none"
257 : (container_type->encoding == ustctl_encode_UTF8)
258 ? "UTF8"
259 : "ASCII",
260 container_type->base);
261 if (ret) {
262 goto end;
263 }
1ddb0e8a 264 nesting++;
10b56aef
MD
265 /* Dump all entries */
266 for (i = 0; i < nr_entries; i++) {
267 const struct ustctl_enum_entry *entry = &entries[i];
268 int j, len;
269
a1f68b22
MD
270 ret = print_tabs(session, nesting);
271 if (ret) {
272 goto end;
273 }
10b56aef 274 ret = lttng_metadata_printf(session,
a1f68b22 275 "\"");
10b56aef
MD
276 if (ret) {
277 goto end;
278 }
279 len = strlen(entry->string);
280 /* Escape the character '"' */
281 for (j = 0; j < len; j++) {
282 char c = entry->string[j];
283
284 switch (c) {
285 case '"':
286 ret = lttng_metadata_printf(session,
287 "\\\"");
288 break;
289 case '\\':
290 ret = lttng_metadata_printf(session,
291 "\\\\");
292 break;
293 default:
294 ret = lttng_metadata_printf(session,
295 "%c", c);
296 break;
297 }
298 if (ret) {
299 goto end;
300 }
301 }
e85e3723 302 ret = lttng_metadata_printf(session, "\"");
10b56aef
MD
303 if (ret) {
304 goto end;
305 }
3b016e58 306
9d27cec7
PP
307 if (entry->u.extra.options &
308 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO) {
e85e3723
PP
309 ret = lttng_metadata_printf(session, ",\n");
310 if (ret) {
311 goto end;
312 }
10b56aef
MD
313 } else {
314 ret = lttng_metadata_printf(session,
e85e3723
PP
315 " = ");
316 if (ret) {
317 goto end;
318 }
3b016e58 319
e85e3723 320 if (entry->start.signedness) {
3b016e58 321 ret = lttng_metadata_printf(session,
e85e3723 322 "%lld", (long long) entry->start.value);
3b016e58
MD
323 } else {
324 ret = lttng_metadata_printf(session,
e85e3723
PP
325 "%llu", entry->start.value);
326 }
327 if (ret) {
328 goto end;
329 }
330
331 if (entry->start.signedness == entry->end.signedness &&
332 entry->start.value ==
333 entry->end.value) {
334 ret = lttng_metadata_printf(session, ",\n");
335 } else {
336 if (entry->end.signedness) {
337 ret = lttng_metadata_printf(session,
338 " ... %lld,\n",
339 (long long) entry->end.value);
340 } else {
341 ret = lttng_metadata_printf(session,
342 " ... %llu,\n",
343 entry->end.value);
344 }
345 }
346 if (ret) {
347 goto end;
3b016e58 348 }
10b56aef
MD
349 }
350 }
1ddb0e8a 351 nesting--;
a1f68b22
MD
352 sanitize_ctf_identifier(identifier, field_name);
353 ret = print_tabs(session, nesting);
354 if (ret) {
355 goto end;
356 }
357 ret = lttng_metadata_printf(session, "} _%s;\n",
358 identifier);
da860cab
MD
359end:
360 (*iter_field)++;
361 return ret;
362}
363
da860cab
MD
364static
365int _lttng_variant_statedump(struct ust_registry_session *session,
366 const struct ustctl_field *fields, size_t nr_fields,
367 size_t *iter_field, size_t nesting)
368{
369 const struct ustctl_field *variant = &fields[*iter_field];
370 uint32_t nr_choices, i;
371 int ret;
a1f68b22 372 char identifier[LTTNG_UST_SYM_NAME_LEN];
da860cab
MD
373
374 if (variant->type.atype != ustctl_atype_variant) {
375 ret = -EINVAL;
376 goto end;
377 }
378 nr_choices = variant->type.u.variant.nr_choices;
379 (*iter_field)++;
a1f68b22
MD
380 sanitize_ctf_identifier(identifier, variant->type.u.variant.tag_name);
381 ret = print_tabs(session, nesting);
382 if (ret) {
383 goto end;
384 }
da860cab 385 ret = lttng_metadata_printf(session,
a1f68b22
MD
386 "variant <_%s> {\n",
387 identifier);
da860cab
MD
388 if (ret) {
389 goto end;
390 }
391
392 for (i = 0; i < nr_choices; i++) {
393 if (*iter_field >= nr_fields) {
394 ret = -EOVERFLOW;
395 goto end;
396 }
397 ret = _lttng_field_statedump(session,
398 fields, nr_fields,
399 iter_field, nesting + 1);
dc6403f3
JG
400 if (ret) {
401 goto end;
402 }
da860cab 403 }
a1f68b22
MD
404 sanitize_ctf_identifier(identifier, variant->name);
405 ret = print_tabs(session, nesting);
850c5d4a
JR
406 if (ret) {
407 goto end;
408 }
da860cab 409 ret = lttng_metadata_printf(session,
a1f68b22
MD
410 "} _%s;\n",
411 identifier);
da860cab
MD
412 if (ret) {
413 goto end;
414 }
10b56aef
MD
415end:
416 return ret;
417}
418
d0b96690
DG
419static
420int _lttng_field_statedump(struct ust_registry_session *session,
da860cab
MD
421 const struct ustctl_field *fields, size_t nr_fields,
422 size_t *iter_field, size_t nesting)
d0b96690
DG
423{
424 int ret = 0;
425 const char *bo_be = " byte_order = be;";
426 const char *bo_le = " byte_order = le;";
427 const char *bo_native = "";
428 const char *bo_reverse;
da860cab 429 const struct ustctl_field *field;
d0b96690 430
da860cab
MD
431 if (*iter_field >= nr_fields) {
432 ret = -EOVERFLOW;
433 goto end;
434 }
435 field = &fields[*iter_field];
436
437 if (session->byte_order == BIG_ENDIAN) {
d0b96690 438 bo_reverse = bo_le;
da860cab 439 } else {
d0b96690 440 bo_reverse = bo_be;
da860cab 441 }
d0b96690
DG
442
443 switch (field->type.atype) {
444 case ustctl_atype_integer:
da860cab
MD
445 ret = print_tabs(session, nesting);
446 if (ret) {
447 goto end;
448 }
d0b96690 449 ret = lttng_metadata_printf(session,
da860cab 450 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
d0b96690
DG
451 field->type.u.basic.integer.size,
452 field->type.u.basic.integer.alignment,
453 field->type.u.basic.integer.signedness,
454 (field->type.u.basic.integer.encoding == ustctl_encode_none)
455 ? "none"
456 : (field->type.u.basic.integer.encoding == ustctl_encode_UTF8)
457 ? "UTF8"
458 : "ASCII",
459 field->type.u.basic.integer.base,
460 field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
461 field->name);
da860cab 462 (*iter_field)++;
d0b96690 463 break;
10b56aef
MD
464 case ustctl_atype_enum:
465 ret = ust_metadata_enum_statedump(session,
466 field->type.u.basic.enumeration.name,
467 field->type.u.basic.enumeration.id,
468 &field->type.u.basic.enumeration.container_type,
da860cab 469 field->name, iter_field, nesting);
10b56aef 470 break;
d0b96690 471 case ustctl_atype_float:
da860cab
MD
472 ret = print_tabs(session, nesting);
473 if (ret) {
474 goto end;
475 }
d0b96690 476 ret = lttng_metadata_printf(session,
da860cab 477 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
d0b96690
DG
478 field->type.u.basic._float.exp_dig,
479 field->type.u.basic._float.mant_dig,
480 field->type.u.basic._float.alignment,
481 field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
482 field->name);
da860cab 483 (*iter_field)++;
d0b96690 484 break;
d0b96690
DG
485 case ustctl_atype_array:
486 {
487 const struct ustctl_basic_type *elem_type;
488
da860cab
MD
489 ret = print_tabs(session, nesting);
490 if (ret) {
491 goto end;
492 }
d0b96690
DG
493 elem_type = &field->type.u.array.elem_type;
494 ret = lttng_metadata_printf(session,
da860cab 495 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
d0b96690
DG
496 elem_type->u.basic.integer.size,
497 elem_type->u.basic.integer.alignment,
498 elem_type->u.basic.integer.signedness,
499 (elem_type->u.basic.integer.encoding == ustctl_encode_none)
500 ? "none"
501 : (elem_type->u.basic.integer.encoding == ustctl_encode_UTF8)
502 ? "UTF8"
503 : "ASCII",
504 elem_type->u.basic.integer.base,
505 elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
506 field->name, field->type.u.array.length);
da860cab 507 (*iter_field)++;
d0b96690
DG
508 break;
509 }
510 case ustctl_atype_sequence:
511 {
512 const struct ustctl_basic_type *elem_type;
513 const struct ustctl_basic_type *length_type;
514
515 elem_type = &field->type.u.sequence.elem_type;
516 length_type = &field->type.u.sequence.length_type;
da860cab
MD
517 ret = print_tabs(session, nesting);
518 if (ret) {
519 goto end;
520 }
d0b96690 521 ret = lttng_metadata_printf(session,
da860cab 522 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
d0b96690
DG
523 length_type->u.basic.integer.size,
524 (unsigned int) length_type->u.basic.integer.alignment,
525 length_type->u.basic.integer.signedness,
526 (length_type->u.basic.integer.encoding == ustctl_encode_none)
527 ? "none"
528 : ((length_type->u.basic.integer.encoding == ustctl_encode_UTF8)
529 ? "UTF8"
530 : "ASCII"),
531 length_type->u.basic.integer.base,
532 length_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
533 field->name);
da860cab
MD
534 if (ret) {
535 goto end;
536 }
d0b96690 537
da860cab
MD
538 ret = print_tabs(session, nesting);
539 if (ret) {
540 goto end;
541 }
d0b96690 542 ret = lttng_metadata_printf(session,
da860cab 543 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
d0b96690
DG
544 elem_type->u.basic.integer.size,
545 (unsigned int) elem_type->u.basic.integer.alignment,
546 elem_type->u.basic.integer.signedness,
547 (elem_type->u.basic.integer.encoding == ustctl_encode_none)
548 ? "none"
549 : ((elem_type->u.basic.integer.encoding == ustctl_encode_UTF8)
550 ? "UTF8"
551 : "ASCII"),
552 elem_type->u.basic.integer.base,
553 elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
554 field->name,
555 field->name);
da860cab 556 (*iter_field)++;
d0b96690
DG
557 break;
558 }
559
560 case ustctl_atype_string:
561 /* Default encoding is UTF8 */
da860cab
MD
562 ret = print_tabs(session, nesting);
563 if (ret) {
564 goto end;
565 }
d0b96690 566 ret = lttng_metadata_printf(session,
da860cab 567 "string%s _%s;\n",
d0b96690
DG
568 field->type.u.basic.string.encoding == ustctl_encode_ASCII ?
569 " { encoding = ASCII; }" : "",
570 field->name);
da860cab
MD
571 (*iter_field)++;
572 break;
573 case ustctl_atype_variant:
574 ret = _lttng_variant_statedump(session, fields, nr_fields, iter_field, nesting);
575 if (ret) {
576 goto end;
577 }
578 break;
579 case ustctl_atype_struct:
580 ret = print_tabs(session, nesting);
581 if (ret) {
582 goto end;
583 }
584 ret = lttng_metadata_printf(session,
585 "struct {} _%s;\n",
586 field->name);
587 (*iter_field)++;
d0b96690
DG
588 break;
589 default:
da860cab 590 ret = -EINVAL;
d0b96690 591 }
da860cab 592end:
d0b96690
DG
593 return ret;
594}
595
596static
597int _lttng_context_metadata_statedump(struct ust_registry_session *session,
598 size_t nr_ctx_fields,
599 struct ustctl_field *ctx)
600{
601 int ret = 0;
da860cab 602 size_t i = 0;
d0b96690
DG
603
604 if (!ctx)
605 return 0;
da860cab
MD
606 for (;;) {
607 if (i >= nr_ctx_fields) {
608 break;
609 }
610 ret = _lttng_field_statedump(session, ctx,
611 nr_ctx_fields, &i, 2);
612 if (ret) {
613 break;
614 }
d0b96690
DG
615 }
616 return ret;
617}
618
619static
620int _lttng_fields_metadata_statedump(struct ust_registry_session *session,
621 struct ust_registry_event *event)
622{
623 int ret = 0;
da860cab 624 size_t i = 0;
d0b96690 625
da860cab
MD
626 for (;;) {
627 if (i >= event->nr_fields) {
628 break;
629 }
630 ret = _lttng_field_statedump(session, event->fields,
631 event->nr_fields, &i, 2);
632 if (ret) {
633 break;
634 }
d0b96690
DG
635 }
636 return ret;
637}
638
639/*
640 * Should be called with session registry mutex held.
641 */
642int ust_metadata_event_statedump(struct ust_registry_session *session,
643 struct ust_registry_channel *chan,
644 struct ust_registry_event *event)
645{
646 int ret = 0;
647
648 /* Don't dump metadata events */
649 if (chan->chan_id == -1U)
650 return 0;
651
652 ret = lttng_metadata_printf(session,
653 "event {\n"
654 " name = \"%s\";\n"
655 " id = %u;\n"
656 " stream_id = %u;\n",
657 event->name,
658 event->id,
659 chan->chan_id);
660 if (ret)
661 goto end;
662
663 ret = lttng_metadata_printf(session,
664 " loglevel = %d;\n",
2106efa0 665 event->loglevel_value);
d0b96690
DG
666 if (ret)
667 goto end;
668
669 if (event->model_emf_uri) {
670 ret = lttng_metadata_printf(session,
671 " model.emf.uri = \"%s\";\n",
672 event->model_emf_uri);
673 if (ret)
674 goto end;
675 }
676
d0b96690
DG
677 ret = lttng_metadata_printf(session,
678 " fields := struct {\n"
679 );
680 if (ret)
681 goto end;
682
683 ret = _lttng_fields_metadata_statedump(session, event);
684 if (ret)
685 goto end;
686
687 ret = lttng_metadata_printf(session,
688 " };\n"
689 "};\n\n");
690 if (ret)
691 goto end;
7972aab2 692 event->metadata_dumped = 1;
d0b96690
DG
693
694end:
695 return ret;
696}
697
698/*
699 * Should be called with session registry mutex held.
700 */
701int ust_metadata_channel_statedump(struct ust_registry_session *session,
702 struct ust_registry_channel *chan)
703{
704 int ret = 0;
705
706 /* Don't dump metadata events */
707 if (chan->chan_id == -1U)
708 return 0;
709
710 if (!chan->header_type)
711 return -EINVAL;
712
713 ret = lttng_metadata_printf(session,
714 "stream {\n"
715 " id = %u;\n"
716 " event.header := %s;\n"
717 " packet.context := struct packet_context;\n",
718 chan->chan_id,
719 chan->header_type == USTCTL_CHANNEL_HEADER_COMPACT ?
720 "struct event_header_compact" :
721 "struct event_header_large");
722 if (ret)
723 goto end;
724
725 if (chan->ctx_fields) {
726 ret = lttng_metadata_printf(session,
727 " event.context := struct {\n");
728 if (ret)
729 goto end;
730 }
731 ret = _lttng_context_metadata_statedump(session,
732 chan->nr_ctx_fields,
733 chan->ctx_fields);
734 if (ret)
735 goto end;
736 if (chan->ctx_fields) {
737 ret = lttng_metadata_printf(session,
738 " };\n");
739 if (ret)
740 goto end;
741 }
742
743 ret = lttng_metadata_printf(session,
744 "};\n\n");
7972aab2
DG
745 /* Flag success of metadata dump. */
746 chan->metadata_dumped = 1;
d0b96690
DG
747
748end:
749 return ret;
750}
751
752static
753int _lttng_stream_packet_context_declare(struct ust_registry_session *session)
754{
755 return lttng_metadata_printf(session,
756 "struct packet_context {\n"
757 " uint64_clock_monotonic_t timestamp_begin;\n"
758 " uint64_clock_monotonic_t timestamp_end;\n"
759 " uint64_t content_size;\n"
760 " uint64_t packet_size;\n"
0793bcc6 761 " uint64_t packet_seq_num;\n"
d0b96690
DG
762 " unsigned long events_discarded;\n"
763 " uint32_t cpu_id;\n"
764 "};\n\n"
765 );
766}
767
768/*
769 * Compact header:
770 * id: range: 0 - 30.
771 * id 31 is reserved to indicate an extended header.
772 *
773 * Large header:
774 * id: range: 0 - 65534.
775 * id 65535 is reserved to indicate an extended header.
776 */
777static
778int _lttng_event_header_declare(struct ust_registry_session *session)
779{
780 return lttng_metadata_printf(session,
781 "struct event_header_compact {\n"
782 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
783 " variant <id> {\n"
784 " struct {\n"
785 " uint27_clock_monotonic_t timestamp;\n"
786 " } compact;\n"
787 " struct {\n"
788 " uint32_t id;\n"
789 " uint64_clock_monotonic_t timestamp;\n"
790 " } extended;\n"
791 " } v;\n"
792 "} align(%u);\n"
793 "\n"
794 "struct event_header_large {\n"
795 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
796 " variant <id> {\n"
797 " struct {\n"
798 " uint32_clock_monotonic_t timestamp;\n"
799 " } compact;\n"
800 " struct {\n"
801 " uint32_t id;\n"
802 " uint64_clock_monotonic_t timestamp;\n"
803 " } extended;\n"
804 " } v;\n"
805 "} align(%u);\n\n",
806 session->uint32_t_alignment,
807 session->uint16_t_alignment
808 );
809}
810
dc113ec7
MD
811/*
812 * The offset between monotonic and realtime clock can be negative if
813 * the system sets the REALTIME clock to 0 after boot.
dc113ec7 814 */
d0b96690 815static
8c645bb0 816int measure_single_clock_offset(struct offset_sample *sample)
d0b96690 817{
dc113ec7 818 uint64_t monotonic_avg, monotonic[2], measure_delta, realtime;
fc0bb9fa 819 uint64_t tcf = trace_clock_freq();
d0b96690
DG
820 struct timespec rts = { 0, 0 };
821 int ret;
822
823 monotonic[0] = trace_clock_read64();
389fbf04 824 ret = lttng_clock_gettime(CLOCK_REALTIME, &rts);
8c645bb0
MD
825 if (ret < 0) {
826 return ret;
827 }
d0b96690 828 monotonic[1] = trace_clock_read64();
8c645bb0
MD
829 measure_delta = monotonic[1] - monotonic[0];
830 if (measure_delta > sample->measure_delta) {
831 /*
832 * Discard value if it took longer to read than the best
833 * sample so far.
834 */
835 return 0;
836 }
dc113ec7 837 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
6e1b0543
MD
838 realtime = (uint64_t) rts.tv_sec * tcf;
839 if (tcf == NSEC_PER_SEC) {
840 realtime += rts.tv_nsec;
841 } else {
842 realtime += (uint64_t) rts.tv_nsec * tcf / NSEC_PER_SEC;
fc0bb9fa 843 }
c2636b57 844 sample->offset = (int64_t) realtime - monotonic_avg;
8c645bb0
MD
845 sample->measure_delta = measure_delta;
846 return 0;
d0b96690
DG
847}
848
8c645bb0
MD
849/*
850 * Approximation of NTP time of day to clock monotonic correlation,
851 * taken at start of trace. Keep the measurement that took the less time
852 * to complete, thus removing imprecision caused by preemption.
c2636b57 853 * May return a negative offset.
8c645bb0
MD
854 */
855static
c2636b57 856int64_t measure_clock_offset(void)
8c645bb0
MD
857{
858 int i;
859 struct offset_sample offset_best_sample = {
860 .offset = 0,
861 .measure_delta = UINT64_MAX,
862 };
863
864 for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) {
865 if (measure_single_clock_offset(&offset_best_sample)) {
866 return 0;
867 }
868 }
869 return offset_best_sample.offset;
870}
d0b96690
DG
871
872/*
873 * Should be called with session registry mutex held.
874 */
875int ust_metadata_session_statedump(struct ust_registry_session *session,
af6142cf
MD
876 struct ust_app *app,
877 uint32_t major,
878 uint32_t minor)
d0b96690
DG
879{
880 unsigned char *uuid_c;
881 char uuid_s[UUID_STR_LEN],
882 clock_uuid_s[UUID_STR_LEN];
883 int ret = 0;
884 char hostname[HOST_NAME_MAX];
885
7972aab2 886 assert(session);
7972aab2 887
d0b96690
DG
888 uuid_c = session->uuid;
889
890 snprintf(uuid_s, sizeof(uuid_s),
891 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
892 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
893 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
894 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
895 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
896
d7ba1388
MD
897 /* For crash ABI */
898 ret = lttng_metadata_printf(session,
899 "/* CTF %u.%u */\n\n",
900 CTF_SPEC_MAJOR,
901 CTF_SPEC_MINOR);
902 if (ret) {
903 goto end;
904 }
905
d0b96690
DG
906 ret = lttng_metadata_printf(session,
907 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
908 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
909 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
910 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
911 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
912 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
913 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
914 "\n"
915 "trace {\n"
916 " major = %u;\n"
917 " minor = %u;\n"
918 " uuid = \"%s\";\n"
919 " byte_order = %s;\n"
920 " packet.header := struct {\n"
921 " uint32_t magic;\n"
922 " uint8_t uuid[16];\n"
923 " uint32_t stream_id;\n"
0793bcc6 924 " uint64_t stream_instance_id;\n"
d0b96690
DG
925 " };\n"
926 "};\n\n",
927 session->uint8_t_alignment,
928 session->uint16_t_alignment,
929 session->uint32_t_alignment,
930 session->uint64_t_alignment,
931 session->bits_per_long,
932 session->long_alignment,
933 CTF_SPEC_MAJOR,
934 CTF_SPEC_MINOR,
935 uuid_s,
936 session->byte_order == BIG_ENDIAN ? "be" : "le"
937 );
938 if (ret)
939 goto end;
940
941 /* ignore error, just use empty string if error. */
942 hostname[0] = '\0';
943 ret = gethostname(hostname, sizeof(hostname));
944 if (ret && errno == ENAMETOOLONG)
945 hostname[HOST_NAME_MAX - 1] = '\0';
946 ret = lttng_metadata_printf(session,
947 "env {\n"
948 " hostname = \"%s\";\n"
949 " domain = \"ust\";\n"
950 " tracer_name = \"lttng-ust\";\n"
951 " tracer_major = %u;\n"
af6142cf 952 " tracer_minor = %u;\n",
d0b96690 953 hostname,
af6142cf
MD
954 major,
955 minor
d0b96690
DG
956 );
957 if (ret)
958 goto end;
959
960 /*
961 * If per-application registry, we can output extra information
962 * about the application.
963 */
964 if (app) {
965 ret = lttng_metadata_printf(session,
af6142cf 966 " tracer_patchlevel = %u;\n"
d0b96690 967 " vpid = %d;\n"
d88aee68 968 " procname = \"%s\";\n",
af6142cf 969 app->version.patchlevel,
d0b96690
DG
970 (int) app->pid,
971 app->name
972 );
973 if (ret)
974 goto end;
975 }
976
977 ret = lttng_metadata_printf(session,
978 "};\n\n"
979 );
980 if (ret)
981 goto end;
982
983
984 ret = lttng_metadata_printf(session,
985 "clock {\n"
fc0bb9fa
MD
986 " name = \"%s\";\n",
987 trace_clock_name()
d0b96690
DG
988 );
989 if (ret)
990 goto end;
991
992 if (!trace_clock_uuid(clock_uuid_s)) {
993 ret = lttng_metadata_printf(session,
994 " uuid = \"%s\";\n",
995 clock_uuid_s
996 );
997 if (ret)
998 goto end;
999 }
1000
1001 ret = lttng_metadata_printf(session,
fc0bb9fa 1002 " description = \"%s\";\n"
d0b96690
DG
1003 " freq = %" PRIu64 "; /* Frequency, in Hz */\n"
1004 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
c2636b57 1005 " offset = %" PRId64 ";\n"
d0b96690 1006 "};\n\n",
fc0bb9fa 1007 trace_clock_description(),
d0b96690
DG
1008 trace_clock_freq(),
1009 measure_clock_offset()
1010 );
1011 if (ret)
1012 goto end;
1013
1014 ret = lttng_metadata_printf(session,
1015 "typealias integer {\n"
1016 " size = 27; align = 1; signed = false;\n"
fc0bb9fa 1017 " map = clock.%s.value;\n"
d0b96690
DG
1018 "} := uint27_clock_monotonic_t;\n"
1019 "\n"
1020 "typealias integer {\n"
1021 " size = 32; align = %u; signed = false;\n"
fc0bb9fa 1022 " map = clock.%s.value;\n"
d0b96690
DG
1023 "} := uint32_clock_monotonic_t;\n"
1024 "\n"
1025 "typealias integer {\n"
1026 " size = 64; align = %u; signed = false;\n"
fc0bb9fa 1027 " map = clock.%s.value;\n"
d0b96690 1028 "} := uint64_clock_monotonic_t;\n\n",
fc0bb9fa 1029 trace_clock_name(),
d0b96690 1030 session->uint32_t_alignment,
fc0bb9fa
MD
1031 trace_clock_name(),
1032 session->uint64_t_alignment,
1033 trace_clock_name()
d0b96690
DG
1034 );
1035 if (ret)
1036 goto end;
1037
1038 ret = _lttng_stream_packet_context_declare(session);
1039 if (ret)
1040 goto end;
1041
1042 ret = _lttng_event_header_declare(session);
1043 if (ret)
1044 goto end;
1045
1046end:
1047 return ret;
1048}
This page took 0.086172 seconds and 4 git commands to generate.