Metadata: add env fields to ease lttng path hierarchy creation for viewer
[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
8de88061
JR
220static
221int print_escaped_ctf_string(struct ust_registry_session *session, const char *string)
222{
223 int ret;
224 size_t i;
225 char cur;
226
227 i = 0;
228 cur = string[i];
229 while (cur != '\0') {
230 switch (cur) {
231 case '\n':
232 ret = lttng_metadata_printf(session, "%s", "\\n");
233 break;
234 case '\\':
235 case '"':
236 ret = lttng_metadata_printf(session, "%c", '\\');
237 if (ret) {
238 goto error;
239 }
240 /* We still print the current char */
241 /* Fallthrough */
242 default:
243 ret = lttng_metadata_printf(session, "%c", cur);
244 break;
245 }
246
247 if (ret) {
248 goto error;
249 }
250
251 cur = string[++i];
252 }
253error:
254 return ret;
255}
256
10b56aef
MD
257/* Called with session registry mutex held. */
258static
259int ust_metadata_enum_statedump(struct ust_registry_session *session,
260 const char *enum_name,
261 uint64_t enum_id,
262 const struct ustctl_integer_type *container_type,
da860cab 263 const char *field_name, size_t *iter_field, size_t nesting)
10b56aef
MD
264{
265 struct ust_registry_enum *reg_enum;
266 const struct ustctl_enum_entry *entries;
267 size_t nr_entries;
268 int ret = 0;
269 size_t i;
a1f68b22 270 char identifier[LTTNG_UST_SYM_NAME_LEN];
10b56aef
MD
271
272 rcu_read_lock();
273 reg_enum = ust_registry_lookup_enum_by_id(session, enum_name, enum_id);
274 rcu_read_unlock();
275 /* reg_enum can still be used because session registry mutex is held. */
276 if (!reg_enum) {
277 ret = -ENOENT;
278 goto end;
279 }
280 entries = reg_enum->entries;
281 nr_entries = reg_enum->nr_entries;
282
da860cab
MD
283 ret = print_tabs(session, nesting);
284 if (ret) {
285 goto end;
286 }
10b56aef 287 ret = lttng_metadata_printf(session,
da860cab 288 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n",
10b56aef
MD
289 container_type->size,
290 container_type->alignment,
291 container_type->signedness,
292 (container_type->encoding == ustctl_encode_none)
293 ? "none"
294 : (container_type->encoding == ustctl_encode_UTF8)
295 ? "UTF8"
296 : "ASCII",
297 container_type->base);
298 if (ret) {
299 goto end;
300 }
1ddb0e8a 301 nesting++;
10b56aef
MD
302 /* Dump all entries */
303 for (i = 0; i < nr_entries; i++) {
304 const struct ustctl_enum_entry *entry = &entries[i];
305 int j, len;
306
a1f68b22
MD
307 ret = print_tabs(session, nesting);
308 if (ret) {
309 goto end;
310 }
10b56aef 311 ret = lttng_metadata_printf(session,
a1f68b22 312 "\"");
10b56aef
MD
313 if (ret) {
314 goto end;
315 }
316 len = strlen(entry->string);
317 /* Escape the character '"' */
318 for (j = 0; j < len; j++) {
319 char c = entry->string[j];
320
321 switch (c) {
322 case '"':
323 ret = lttng_metadata_printf(session,
324 "\\\"");
325 break;
326 case '\\':
327 ret = lttng_metadata_printf(session,
328 "\\\\");
329 break;
330 default:
331 ret = lttng_metadata_printf(session,
332 "%c", c);
333 break;
334 }
335 if (ret) {
336 goto end;
337 }
338 }
e85e3723 339 ret = lttng_metadata_printf(session, "\"");
10b56aef
MD
340 if (ret) {
341 goto end;
342 }
3b016e58 343
9d27cec7
PP
344 if (entry->u.extra.options &
345 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO) {
e85e3723
PP
346 ret = lttng_metadata_printf(session, ",\n");
347 if (ret) {
348 goto end;
349 }
10b56aef
MD
350 } else {
351 ret = lttng_metadata_printf(session,
e85e3723
PP
352 " = ");
353 if (ret) {
354 goto end;
355 }
3b016e58 356
e85e3723 357 if (entry->start.signedness) {
3b016e58 358 ret = lttng_metadata_printf(session,
e85e3723 359 "%lld", (long long) entry->start.value);
3b016e58
MD
360 } else {
361 ret = lttng_metadata_printf(session,
e85e3723
PP
362 "%llu", entry->start.value);
363 }
364 if (ret) {
365 goto end;
366 }
367
368 if (entry->start.signedness == entry->end.signedness &&
369 entry->start.value ==
370 entry->end.value) {
371 ret = lttng_metadata_printf(session, ",\n");
372 } else {
373 if (entry->end.signedness) {
374 ret = lttng_metadata_printf(session,
375 " ... %lld,\n",
376 (long long) entry->end.value);
377 } else {
378 ret = lttng_metadata_printf(session,
379 " ... %llu,\n",
380 entry->end.value);
381 }
382 }
383 if (ret) {
384 goto end;
3b016e58 385 }
10b56aef
MD
386 }
387 }
1ddb0e8a 388 nesting--;
a1f68b22
MD
389 sanitize_ctf_identifier(identifier, field_name);
390 ret = print_tabs(session, nesting);
391 if (ret) {
392 goto end;
393 }
394 ret = lttng_metadata_printf(session, "} _%s;\n",
395 identifier);
da860cab
MD
396end:
397 (*iter_field)++;
398 return ret;
399}
400
da860cab
MD
401static
402int _lttng_variant_statedump(struct ust_registry_session *session,
403 const struct ustctl_field *fields, size_t nr_fields,
404 size_t *iter_field, size_t nesting)
405{
406 const struct ustctl_field *variant = &fields[*iter_field];
407 uint32_t nr_choices, i;
408 int ret;
a1f68b22 409 char identifier[LTTNG_UST_SYM_NAME_LEN];
da860cab
MD
410
411 if (variant->type.atype != ustctl_atype_variant) {
412 ret = -EINVAL;
413 goto end;
414 }
415 nr_choices = variant->type.u.variant.nr_choices;
416 (*iter_field)++;
a1f68b22
MD
417 sanitize_ctf_identifier(identifier, variant->type.u.variant.tag_name);
418 ret = print_tabs(session, nesting);
419 if (ret) {
420 goto end;
421 }
da860cab 422 ret = lttng_metadata_printf(session,
a1f68b22
MD
423 "variant <_%s> {\n",
424 identifier);
da860cab
MD
425 if (ret) {
426 goto end;
427 }
428
429 for (i = 0; i < nr_choices; i++) {
430 if (*iter_field >= nr_fields) {
431 ret = -EOVERFLOW;
432 goto end;
433 }
434 ret = _lttng_field_statedump(session,
435 fields, nr_fields,
436 iter_field, nesting + 1);
dc6403f3
JG
437 if (ret) {
438 goto end;
439 }
da860cab 440 }
a1f68b22
MD
441 sanitize_ctf_identifier(identifier, variant->name);
442 ret = print_tabs(session, nesting);
850c5d4a
JR
443 if (ret) {
444 goto end;
445 }
da860cab 446 ret = lttng_metadata_printf(session,
a1f68b22
MD
447 "} _%s;\n",
448 identifier);
da860cab
MD
449 if (ret) {
450 goto end;
451 }
10b56aef
MD
452end:
453 return ret;
454}
455
d0b96690
DG
456static
457int _lttng_field_statedump(struct ust_registry_session *session,
da860cab
MD
458 const struct ustctl_field *fields, size_t nr_fields,
459 size_t *iter_field, size_t nesting)
d0b96690
DG
460{
461 int ret = 0;
462 const char *bo_be = " byte_order = be;";
463 const char *bo_le = " byte_order = le;";
464 const char *bo_native = "";
465 const char *bo_reverse;
da860cab 466 const struct ustctl_field *field;
d0b96690 467
da860cab
MD
468 if (*iter_field >= nr_fields) {
469 ret = -EOVERFLOW;
470 goto end;
471 }
472 field = &fields[*iter_field];
473
474 if (session->byte_order == BIG_ENDIAN) {
d0b96690 475 bo_reverse = bo_le;
da860cab 476 } else {
d0b96690 477 bo_reverse = bo_be;
da860cab 478 }
d0b96690
DG
479
480 switch (field->type.atype) {
481 case ustctl_atype_integer:
da860cab
MD
482 ret = print_tabs(session, nesting);
483 if (ret) {
484 goto end;
485 }
d0b96690 486 ret = lttng_metadata_printf(session,
da860cab 487 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
d0b96690
DG
488 field->type.u.basic.integer.size,
489 field->type.u.basic.integer.alignment,
490 field->type.u.basic.integer.signedness,
491 (field->type.u.basic.integer.encoding == ustctl_encode_none)
492 ? "none"
493 : (field->type.u.basic.integer.encoding == ustctl_encode_UTF8)
494 ? "UTF8"
495 : "ASCII",
496 field->type.u.basic.integer.base,
497 field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
498 field->name);
da860cab 499 (*iter_field)++;
d0b96690 500 break;
10b56aef
MD
501 case ustctl_atype_enum:
502 ret = ust_metadata_enum_statedump(session,
503 field->type.u.basic.enumeration.name,
504 field->type.u.basic.enumeration.id,
505 &field->type.u.basic.enumeration.container_type,
da860cab 506 field->name, iter_field, nesting);
10b56aef 507 break;
d0b96690 508 case ustctl_atype_float:
da860cab
MD
509 ret = print_tabs(session, nesting);
510 if (ret) {
511 goto end;
512 }
d0b96690 513 ret = lttng_metadata_printf(session,
da860cab 514 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
d0b96690
DG
515 field->type.u.basic._float.exp_dig,
516 field->type.u.basic._float.mant_dig,
517 field->type.u.basic._float.alignment,
518 field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
519 field->name);
da860cab 520 (*iter_field)++;
d0b96690 521 break;
d0b96690
DG
522 case ustctl_atype_array:
523 {
524 const struct ustctl_basic_type *elem_type;
525
da860cab
MD
526 ret = print_tabs(session, nesting);
527 if (ret) {
528 goto end;
529 }
d0b96690
DG
530 elem_type = &field->type.u.array.elem_type;
531 ret = lttng_metadata_printf(session,
da860cab 532 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
d0b96690
DG
533 elem_type->u.basic.integer.size,
534 elem_type->u.basic.integer.alignment,
535 elem_type->u.basic.integer.signedness,
536 (elem_type->u.basic.integer.encoding == ustctl_encode_none)
537 ? "none"
538 : (elem_type->u.basic.integer.encoding == ustctl_encode_UTF8)
539 ? "UTF8"
540 : "ASCII",
541 elem_type->u.basic.integer.base,
542 elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
543 field->name, field->type.u.array.length);
da860cab 544 (*iter_field)++;
d0b96690
DG
545 break;
546 }
547 case ustctl_atype_sequence:
548 {
549 const struct ustctl_basic_type *elem_type;
550 const struct ustctl_basic_type *length_type;
551
552 elem_type = &field->type.u.sequence.elem_type;
553 length_type = &field->type.u.sequence.length_type;
da860cab
MD
554 ret = print_tabs(session, nesting);
555 if (ret) {
556 goto end;
557 }
d0b96690 558 ret = lttng_metadata_printf(session,
da860cab 559 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
d0b96690
DG
560 length_type->u.basic.integer.size,
561 (unsigned int) length_type->u.basic.integer.alignment,
562 length_type->u.basic.integer.signedness,
563 (length_type->u.basic.integer.encoding == ustctl_encode_none)
564 ? "none"
565 : ((length_type->u.basic.integer.encoding == ustctl_encode_UTF8)
566 ? "UTF8"
567 : "ASCII"),
568 length_type->u.basic.integer.base,
569 length_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
570 field->name);
da860cab
MD
571 if (ret) {
572 goto end;
573 }
d0b96690 574
da860cab
MD
575 ret = print_tabs(session, nesting);
576 if (ret) {
577 goto end;
578 }
d0b96690 579 ret = lttng_metadata_printf(session,
da860cab 580 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
d0b96690
DG
581 elem_type->u.basic.integer.size,
582 (unsigned int) elem_type->u.basic.integer.alignment,
583 elem_type->u.basic.integer.signedness,
584 (elem_type->u.basic.integer.encoding == ustctl_encode_none)
585 ? "none"
586 : ((elem_type->u.basic.integer.encoding == ustctl_encode_UTF8)
587 ? "UTF8"
588 : "ASCII"),
589 elem_type->u.basic.integer.base,
590 elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
591 field->name,
592 field->name);
da860cab 593 (*iter_field)++;
d0b96690
DG
594 break;
595 }
596
597 case ustctl_atype_string:
598 /* Default encoding is UTF8 */
da860cab
MD
599 ret = print_tabs(session, nesting);
600 if (ret) {
601 goto end;
602 }
d0b96690 603 ret = lttng_metadata_printf(session,
da860cab 604 "string%s _%s;\n",
d0b96690
DG
605 field->type.u.basic.string.encoding == ustctl_encode_ASCII ?
606 " { encoding = ASCII; }" : "",
607 field->name);
da860cab
MD
608 (*iter_field)++;
609 break;
610 case ustctl_atype_variant:
611 ret = _lttng_variant_statedump(session, fields, nr_fields, iter_field, nesting);
612 if (ret) {
613 goto end;
614 }
615 break;
616 case ustctl_atype_struct:
617 ret = print_tabs(session, nesting);
618 if (ret) {
619 goto end;
620 }
621 ret = lttng_metadata_printf(session,
622 "struct {} _%s;\n",
623 field->name);
624 (*iter_field)++;
d0b96690
DG
625 break;
626 default:
da860cab 627 ret = -EINVAL;
d0b96690 628 }
da860cab 629end:
d0b96690
DG
630 return ret;
631}
632
633static
634int _lttng_context_metadata_statedump(struct ust_registry_session *session,
635 size_t nr_ctx_fields,
636 struct ustctl_field *ctx)
637{
638 int ret = 0;
da860cab 639 size_t i = 0;
d0b96690
DG
640
641 if (!ctx)
642 return 0;
da860cab
MD
643 for (;;) {
644 if (i >= nr_ctx_fields) {
645 break;
646 }
647 ret = _lttng_field_statedump(session, ctx,
648 nr_ctx_fields, &i, 2);
649 if (ret) {
650 break;
651 }
d0b96690
DG
652 }
653 return ret;
654}
655
656static
657int _lttng_fields_metadata_statedump(struct ust_registry_session *session,
658 struct ust_registry_event *event)
659{
660 int ret = 0;
da860cab 661 size_t i = 0;
d0b96690 662
da860cab
MD
663 for (;;) {
664 if (i >= event->nr_fields) {
665 break;
666 }
667 ret = _lttng_field_statedump(session, event->fields,
668 event->nr_fields, &i, 2);
669 if (ret) {
670 break;
671 }
d0b96690
DG
672 }
673 return ret;
674}
675
676/*
677 * Should be called with session registry mutex held.
678 */
679int ust_metadata_event_statedump(struct ust_registry_session *session,
680 struct ust_registry_channel *chan,
681 struct ust_registry_event *event)
682{
683 int ret = 0;
684
685 /* Don't dump metadata events */
686 if (chan->chan_id == -1U)
687 return 0;
688
689 ret = lttng_metadata_printf(session,
690 "event {\n"
691 " name = \"%s\";\n"
692 " id = %u;\n"
693 " stream_id = %u;\n",
694 event->name,
695 event->id,
696 chan->chan_id);
697 if (ret)
698 goto end;
699
700 ret = lttng_metadata_printf(session,
701 " loglevel = %d;\n",
2106efa0 702 event->loglevel_value);
d0b96690
DG
703 if (ret)
704 goto end;
705
706 if (event->model_emf_uri) {
707 ret = lttng_metadata_printf(session,
708 " model.emf.uri = \"%s\";\n",
709 event->model_emf_uri);
710 if (ret)
711 goto end;
712 }
713
d0b96690
DG
714 ret = lttng_metadata_printf(session,
715 " fields := struct {\n"
716 );
717 if (ret)
718 goto end;
719
720 ret = _lttng_fields_metadata_statedump(session, event);
721 if (ret)
722 goto end;
723
724 ret = lttng_metadata_printf(session,
725 " };\n"
726 "};\n\n");
727 if (ret)
728 goto end;
7972aab2 729 event->metadata_dumped = 1;
d0b96690
DG
730
731end:
732 return ret;
733}
734
735/*
736 * Should be called with session registry mutex held.
737 */
738int ust_metadata_channel_statedump(struct ust_registry_session *session,
739 struct ust_registry_channel *chan)
740{
741 int ret = 0;
742
743 /* Don't dump metadata events */
744 if (chan->chan_id == -1U)
745 return 0;
746
747 if (!chan->header_type)
748 return -EINVAL;
749
750 ret = lttng_metadata_printf(session,
751 "stream {\n"
752 " id = %u;\n"
753 " event.header := %s;\n"
754 " packet.context := struct packet_context;\n",
755 chan->chan_id,
756 chan->header_type == USTCTL_CHANNEL_HEADER_COMPACT ?
757 "struct event_header_compact" :
758 "struct event_header_large");
759 if (ret)
760 goto end;
761
762 if (chan->ctx_fields) {
763 ret = lttng_metadata_printf(session,
764 " event.context := struct {\n");
765 if (ret)
766 goto end;
767 }
768 ret = _lttng_context_metadata_statedump(session,
769 chan->nr_ctx_fields,
770 chan->ctx_fields);
771 if (ret)
772 goto end;
773 if (chan->ctx_fields) {
774 ret = lttng_metadata_printf(session,
775 " };\n");
776 if (ret)
777 goto end;
778 }
779
780 ret = lttng_metadata_printf(session,
781 "};\n\n");
7972aab2
DG
782 /* Flag success of metadata dump. */
783 chan->metadata_dumped = 1;
d0b96690
DG
784
785end:
786 return ret;
787}
788
789static
790int _lttng_stream_packet_context_declare(struct ust_registry_session *session)
791{
792 return lttng_metadata_printf(session,
793 "struct packet_context {\n"
794 " uint64_clock_monotonic_t timestamp_begin;\n"
795 " uint64_clock_monotonic_t timestamp_end;\n"
796 " uint64_t content_size;\n"
797 " uint64_t packet_size;\n"
0793bcc6 798 " uint64_t packet_seq_num;\n"
d0b96690
DG
799 " unsigned long events_discarded;\n"
800 " uint32_t cpu_id;\n"
801 "};\n\n"
802 );
803}
804
805/*
806 * Compact header:
807 * id: range: 0 - 30.
808 * id 31 is reserved to indicate an extended header.
809 *
810 * Large header:
811 * id: range: 0 - 65534.
812 * id 65535 is reserved to indicate an extended header.
813 */
814static
815int _lttng_event_header_declare(struct ust_registry_session *session)
816{
817 return lttng_metadata_printf(session,
818 "struct event_header_compact {\n"
819 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
820 " variant <id> {\n"
821 " struct {\n"
822 " uint27_clock_monotonic_t timestamp;\n"
823 " } compact;\n"
824 " struct {\n"
825 " uint32_t id;\n"
826 " uint64_clock_monotonic_t timestamp;\n"
827 " } extended;\n"
828 " } v;\n"
829 "} align(%u);\n"
830 "\n"
831 "struct event_header_large {\n"
832 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
833 " variant <id> {\n"
834 " struct {\n"
835 " uint32_clock_monotonic_t timestamp;\n"
836 " } compact;\n"
837 " struct {\n"
838 " uint32_t id;\n"
839 " uint64_clock_monotonic_t timestamp;\n"
840 " } extended;\n"
841 " } v;\n"
842 "} align(%u);\n\n",
843 session->uint32_t_alignment,
844 session->uint16_t_alignment
845 );
846}
847
dc113ec7
MD
848/*
849 * The offset between monotonic and realtime clock can be negative if
850 * the system sets the REALTIME clock to 0 after boot.
dc113ec7 851 */
d0b96690 852static
8c645bb0 853int measure_single_clock_offset(struct offset_sample *sample)
d0b96690 854{
dc113ec7 855 uint64_t monotonic_avg, monotonic[2], measure_delta, realtime;
fc0bb9fa 856 uint64_t tcf = trace_clock_freq();
d0b96690
DG
857 struct timespec rts = { 0, 0 };
858 int ret;
859
860 monotonic[0] = trace_clock_read64();
389fbf04 861 ret = lttng_clock_gettime(CLOCK_REALTIME, &rts);
8c645bb0
MD
862 if (ret < 0) {
863 return ret;
864 }
d0b96690 865 monotonic[1] = trace_clock_read64();
8c645bb0
MD
866 measure_delta = monotonic[1] - monotonic[0];
867 if (measure_delta > sample->measure_delta) {
868 /*
869 * Discard value if it took longer to read than the best
870 * sample so far.
871 */
872 return 0;
873 }
dc113ec7 874 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
6e1b0543
MD
875 realtime = (uint64_t) rts.tv_sec * tcf;
876 if (tcf == NSEC_PER_SEC) {
877 realtime += rts.tv_nsec;
878 } else {
879 realtime += (uint64_t) rts.tv_nsec * tcf / NSEC_PER_SEC;
fc0bb9fa 880 }
c2636b57 881 sample->offset = (int64_t) realtime - monotonic_avg;
8c645bb0
MD
882 sample->measure_delta = measure_delta;
883 return 0;
d0b96690
DG
884}
885
8c645bb0
MD
886/*
887 * Approximation of NTP time of day to clock monotonic correlation,
888 * taken at start of trace. Keep the measurement that took the less time
889 * to complete, thus removing imprecision caused by preemption.
c2636b57 890 * May return a negative offset.
8c645bb0
MD
891 */
892static
c2636b57 893int64_t measure_clock_offset(void)
8c645bb0
MD
894{
895 int i;
896 struct offset_sample offset_best_sample = {
897 .offset = 0,
898 .measure_delta = UINT64_MAX,
899 };
900
901 for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) {
902 if (measure_single_clock_offset(&offset_best_sample)) {
903 return 0;
904 }
905 }
906 return offset_best_sample.offset;
907}
d0b96690 908
8de88061
JR
909static
910int print_metadata_session_information(struct ust_registry_session *registry)
911{
912 int ret;
913 struct ltt_session *session = NULL;
914 char creation_datetime[ISO8601_STR_LEN];
915
916 rcu_read_lock();
917 session = session_find_by_id(registry->tracing_id);
918 if (!session) {
919 ret = -1;
920 goto error;
921 }
922
923 /* Print the trace name */
924 ret = lttng_metadata_printf(registry, " trace_name = \"");
925 if (ret) {
926 goto error;
927 }
928
929 /*
930 * This is necessary since the creation time is present in the session
931 * name when it is generated.
932 */
933 if (session->has_auto_generated_name) {
934 ret = print_escaped_ctf_string(registry, DEFAULT_SESSION_NAME);
935 } else {
936 ret = print_escaped_ctf_string(registry, session->name);
937 }
938 if (ret) {
939 goto error;
940 }
941
942 ret = lttng_metadata_printf(registry, "\";\n");
943 if (ret) {
944 goto error;
945 }
946
947 /* Prepare creation time */
948 ret = time_to_iso8601_str(session->creation_time, creation_datetime,
949 sizeof(creation_datetime));
950 if (ret) {
951 goto error;
952 }
953
954 /* Output the reste of the information */
955 ret = lttng_metadata_printf(registry,
956 " trace_creation_datetime = \"%s\";\n"
957 " hostname = \"%s\";\n",
958 creation_datetime, session->hostname);
959 if (ret) {
960 goto error;
961 }
962
963error:
964 if (session) {
965 session_put(session);
966 }
967 rcu_read_unlock();
968 return ret;
969}
970
971static
972int print_metadata_app_information(struct ust_registry_session *registry,
973 struct ust_app *app)
974{
975 int ret;
976 char datetime[ISO8601_STR_LEN];
977
978 if (!app) {
979 ret = 0;
980 goto end;
981 }
982
983 ret = time_to_iso8601_str(
984 app->registration_time, datetime, sizeof(datetime));
985 if (ret) {
986 goto end;
987 }
988
989 ret = lttng_metadata_printf(registry,
990 " tracer_patchlevel = %u;\n"
991 " vpid = %d;\n"
992 " procname = \"%s\";\n"
993 " vpid_datetime = \"%s\";\n",
994 app->version.patchlevel, (int) app->pid, app->name,
995 datetime);
996
997end:
998 return ret;
999}
1000
d0b96690
DG
1001/*
1002 * Should be called with session registry mutex held.
1003 */
1004int ust_metadata_session_statedump(struct ust_registry_session *session,
af6142cf
MD
1005 struct ust_app *app,
1006 uint32_t major,
1007 uint32_t minor)
d0b96690 1008{
d0b96690
DG
1009 char uuid_s[UUID_STR_LEN],
1010 clock_uuid_s[UUID_STR_LEN];
1011 int ret = 0;
d0b96690 1012
7972aab2 1013 assert(session);
7972aab2 1014
c8e0c5f5 1015 lttng_uuid_to_str(session->uuid, uuid_s);
d0b96690 1016
d7ba1388
MD
1017 /* For crash ABI */
1018 ret = lttng_metadata_printf(session,
1019 "/* CTF %u.%u */\n\n",
1020 CTF_SPEC_MAJOR,
1021 CTF_SPEC_MINOR);
1022 if (ret) {
1023 goto end;
1024 }
1025
d0b96690
DG
1026 ret = lttng_metadata_printf(session,
1027 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1028 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1029 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1030 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1031 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1032 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1033 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1034 "\n"
1035 "trace {\n"
1036 " major = %u;\n"
1037 " minor = %u;\n"
1038 " uuid = \"%s\";\n"
1039 " byte_order = %s;\n"
1040 " packet.header := struct {\n"
1041 " uint32_t magic;\n"
1042 " uint8_t uuid[16];\n"
1043 " uint32_t stream_id;\n"
0793bcc6 1044 " uint64_t stream_instance_id;\n"
d0b96690
DG
1045 " };\n"
1046 "};\n\n",
1047 session->uint8_t_alignment,
1048 session->uint16_t_alignment,
1049 session->uint32_t_alignment,
1050 session->uint64_t_alignment,
1051 session->bits_per_long,
1052 session->long_alignment,
1053 CTF_SPEC_MAJOR,
1054 CTF_SPEC_MINOR,
1055 uuid_s,
1056 session->byte_order == BIG_ENDIAN ? "be" : "le"
1057 );
1058 if (ret)
1059 goto end;
1060
d0b96690
DG
1061 ret = lttng_metadata_printf(session,
1062 "env {\n"
d0b96690
DG
1063 " domain = \"ust\";\n"
1064 " tracer_name = \"lttng-ust\";\n"
1065 " tracer_major = %u;\n"
8de88061
JR
1066 " tracer_minor = %u;\n"
1067 " tracer_buffering_scheme = \"%s\";\n"
1068 " tracer_buffering_id = %u;\n"
1069 " architecture_bit_width = %u;\n",
af6142cf 1070 major,
8de88061
JR
1071 minor,
1072 app ? "pid" : "uid",
1073 app ? (int) app->pid : (int) session->tracing_uid,
1074 session->bits_per_long);
1075 if (ret) {
1076 goto end;
1077 }
1078
1079 ret = print_metadata_session_information(session);
1080 if (ret) {
d0b96690 1081 goto end;
8de88061 1082 }
d0b96690
DG
1083
1084 /*
1085 * If per-application registry, we can output extra information
1086 * about the application.
1087 */
8de88061
JR
1088 ret = print_metadata_app_information(session, app);
1089 if (ret) {
1090 goto end;
d0b96690
DG
1091 }
1092
1093 ret = lttng_metadata_printf(session,
1094 "};\n\n"
1095 );
1096 if (ret)
1097 goto end;
1098
1099
1100 ret = lttng_metadata_printf(session,
1101 "clock {\n"
fc0bb9fa
MD
1102 " name = \"%s\";\n",
1103 trace_clock_name()
d0b96690
DG
1104 );
1105 if (ret)
1106 goto end;
1107
1108 if (!trace_clock_uuid(clock_uuid_s)) {
1109 ret = lttng_metadata_printf(session,
1110 " uuid = \"%s\";\n",
1111 clock_uuid_s
1112 );
1113 if (ret)
1114 goto end;
1115 }
1116
1117 ret = lttng_metadata_printf(session,
fc0bb9fa 1118 " description = \"%s\";\n"
d0b96690
DG
1119 " freq = %" PRIu64 "; /* Frequency, in Hz */\n"
1120 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
c2636b57 1121 " offset = %" PRId64 ";\n"
d0b96690 1122 "};\n\n",
fc0bb9fa 1123 trace_clock_description(),
d0b96690
DG
1124 trace_clock_freq(),
1125 measure_clock_offset()
1126 );
1127 if (ret)
1128 goto end;
1129
1130 ret = lttng_metadata_printf(session,
1131 "typealias integer {\n"
1132 " size = 27; align = 1; signed = false;\n"
fc0bb9fa 1133 " map = clock.%s.value;\n"
d0b96690
DG
1134 "} := uint27_clock_monotonic_t;\n"
1135 "\n"
1136 "typealias integer {\n"
1137 " size = 32; align = %u; signed = false;\n"
fc0bb9fa 1138 " map = clock.%s.value;\n"
d0b96690
DG
1139 "} := uint32_clock_monotonic_t;\n"
1140 "\n"
1141 "typealias integer {\n"
1142 " size = 64; align = %u; signed = false;\n"
fc0bb9fa 1143 " map = clock.%s.value;\n"
d0b96690 1144 "} := uint64_clock_monotonic_t;\n\n",
fc0bb9fa 1145 trace_clock_name(),
d0b96690 1146 session->uint32_t_alignment,
fc0bb9fa
MD
1147 trace_clock_name(),
1148 session->uint64_t_alignment,
1149 trace_clock_name()
d0b96690
DG
1150 );
1151 if (ret)
1152 goto end;
1153
1154 ret = _lttng_stream_packet_context_declare(session);
1155 if (ret)
1156 goto end;
1157
1158 ret = _lttng_event_header_declare(session);
1159 if (ret)
1160 goto end;
1161
1162end:
1163 return ret;
1164}
This page took 0.09587 seconds and 4 git commands to generate.