Fix: parse monitor timer parameter as an unsigned 64-bit integer
[lttng-tools.git] / src / bin / lttng-sessiond / ust-metadata.c
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
22 #define _LGPL_SOURCE
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>
31 #include <common/time.h>
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
41 #define NR_CLOCK_OFFSET_SAMPLES 10
42
43 struct offset_sample {
44 int64_t offset; /* correlation offset */
45 uint64_t measure_delta; /* lower is better */
46 };
47
48 static
49 int _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
53 static inline
54 int 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)) {
77 r -= 1;
78 }
79 return r;
80 }
81
82 static inline
83 int 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 */
96 static
97 ssize_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
127 static
128 int 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
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 */
150 static
151 int 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);
173 ret = metadata_file_append(session, str, len);
174 if (ret) {
175 PERROR("Error appending to metadata file");
176 goto end;
177 }
178 DBG3("Append to metadata: \"%s\"", str);
179 ret = 0;
180
181 end:
182 free(str);
183 return ret;
184 }
185
186 static
187 int 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
202 static
203 void 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
220 /* Called with session registry mutex held. */
221 static
222 int 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,
226 const char *field_name, size_t *iter_field, size_t nesting)
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;
233 char identifier[LTTNG_UST_SYM_NAME_LEN];
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
246 ret = print_tabs(session, nesting);
247 if (ret) {
248 goto end;
249 }
250 ret = lttng_metadata_printf(session,
251 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n",
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 }
264 nesting++;
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
270 ret = print_tabs(session, nesting);
271 if (ret) {
272 goto end;
273 }
274 ret = lttng_metadata_printf(session,
275 "\"");
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 }
302 ret = lttng_metadata_printf(session, "\"");
303 if (ret) {
304 goto end;
305 }
306
307 if (entry->u.extra.options &
308 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO) {
309 ret = lttng_metadata_printf(session, ",\n");
310 if (ret) {
311 goto end;
312 }
313 } else {
314 ret = lttng_metadata_printf(session,
315 " = ");
316 if (ret) {
317 goto end;
318 }
319
320 if (entry->start.signedness) {
321 ret = lttng_metadata_printf(session,
322 "%lld", (long long) entry->start.value);
323 } else {
324 ret = lttng_metadata_printf(session,
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;
348 }
349 }
350 }
351 nesting--;
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);
359 end:
360 (*iter_field)++;
361 return ret;
362 }
363
364 static
365 int _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;
372 char identifier[LTTNG_UST_SYM_NAME_LEN];
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)++;
380 sanitize_ctf_identifier(identifier, variant->type.u.variant.tag_name);
381 ret = print_tabs(session, nesting);
382 if (ret) {
383 goto end;
384 }
385 ret = lttng_metadata_printf(session,
386 "variant <_%s> {\n",
387 identifier);
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);
400 if (ret) {
401 goto end;
402 }
403 }
404 sanitize_ctf_identifier(identifier, variant->name);
405 ret = print_tabs(session, nesting);
406 ret = lttng_metadata_printf(session,
407 "} _%s;\n",
408 identifier);
409 if (ret) {
410 goto end;
411 }
412 end:
413 return ret;
414 }
415
416 static
417 int _lttng_field_statedump(struct ust_registry_session *session,
418 const struct ustctl_field *fields, size_t nr_fields,
419 size_t *iter_field, size_t nesting)
420 {
421 int ret = 0;
422 const char *bo_be = " byte_order = be;";
423 const char *bo_le = " byte_order = le;";
424 const char *bo_native = "";
425 const char *bo_reverse;
426 const struct ustctl_field *field;
427
428 if (*iter_field >= nr_fields) {
429 ret = -EOVERFLOW;
430 goto end;
431 }
432 field = &fields[*iter_field];
433
434 if (session->byte_order == BIG_ENDIAN) {
435 bo_reverse = bo_le;
436 } else {
437 bo_reverse = bo_be;
438 }
439
440 switch (field->type.atype) {
441 case ustctl_atype_integer:
442 ret = print_tabs(session, nesting);
443 if (ret) {
444 goto end;
445 }
446 ret = lttng_metadata_printf(session,
447 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
448 field->type.u.basic.integer.size,
449 field->type.u.basic.integer.alignment,
450 field->type.u.basic.integer.signedness,
451 (field->type.u.basic.integer.encoding == ustctl_encode_none)
452 ? "none"
453 : (field->type.u.basic.integer.encoding == ustctl_encode_UTF8)
454 ? "UTF8"
455 : "ASCII",
456 field->type.u.basic.integer.base,
457 field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
458 field->name);
459 (*iter_field)++;
460 break;
461 case ustctl_atype_enum:
462 ret = ust_metadata_enum_statedump(session,
463 field->type.u.basic.enumeration.name,
464 field->type.u.basic.enumeration.id,
465 &field->type.u.basic.enumeration.container_type,
466 field->name, iter_field, nesting);
467 break;
468 case ustctl_atype_float:
469 ret = print_tabs(session, nesting);
470 if (ret) {
471 goto end;
472 }
473 ret = lttng_metadata_printf(session,
474 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
475 field->type.u.basic._float.exp_dig,
476 field->type.u.basic._float.mant_dig,
477 field->type.u.basic._float.alignment,
478 field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
479 field->name);
480 (*iter_field)++;
481 break;
482 case ustctl_atype_array:
483 {
484 const struct ustctl_basic_type *elem_type;
485
486 ret = print_tabs(session, nesting);
487 if (ret) {
488 goto end;
489 }
490 elem_type = &field->type.u.array.elem_type;
491 ret = lttng_metadata_printf(session,
492 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
493 elem_type->u.basic.integer.size,
494 elem_type->u.basic.integer.alignment,
495 elem_type->u.basic.integer.signedness,
496 (elem_type->u.basic.integer.encoding == ustctl_encode_none)
497 ? "none"
498 : (elem_type->u.basic.integer.encoding == ustctl_encode_UTF8)
499 ? "UTF8"
500 : "ASCII",
501 elem_type->u.basic.integer.base,
502 elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
503 field->name, field->type.u.array.length);
504 (*iter_field)++;
505 break;
506 }
507 case ustctl_atype_sequence:
508 {
509 const struct ustctl_basic_type *elem_type;
510 const struct ustctl_basic_type *length_type;
511
512 elem_type = &field->type.u.sequence.elem_type;
513 length_type = &field->type.u.sequence.length_type;
514 ret = print_tabs(session, nesting);
515 if (ret) {
516 goto end;
517 }
518 ret = lttng_metadata_printf(session,
519 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
520 length_type->u.basic.integer.size,
521 (unsigned int) length_type->u.basic.integer.alignment,
522 length_type->u.basic.integer.signedness,
523 (length_type->u.basic.integer.encoding == ustctl_encode_none)
524 ? "none"
525 : ((length_type->u.basic.integer.encoding == ustctl_encode_UTF8)
526 ? "UTF8"
527 : "ASCII"),
528 length_type->u.basic.integer.base,
529 length_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
530 field->name);
531 if (ret) {
532 goto end;
533 }
534
535 ret = print_tabs(session, nesting);
536 if (ret) {
537 goto end;
538 }
539 ret = lttng_metadata_printf(session,
540 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
541 elem_type->u.basic.integer.size,
542 (unsigned int) elem_type->u.basic.integer.alignment,
543 elem_type->u.basic.integer.signedness,
544 (elem_type->u.basic.integer.encoding == ustctl_encode_none)
545 ? "none"
546 : ((elem_type->u.basic.integer.encoding == ustctl_encode_UTF8)
547 ? "UTF8"
548 : "ASCII"),
549 elem_type->u.basic.integer.base,
550 elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
551 field->name,
552 field->name);
553 (*iter_field)++;
554 break;
555 }
556
557 case ustctl_atype_string:
558 /* Default encoding is UTF8 */
559 ret = print_tabs(session, nesting);
560 if (ret) {
561 goto end;
562 }
563 ret = lttng_metadata_printf(session,
564 "string%s _%s;\n",
565 field->type.u.basic.string.encoding == ustctl_encode_ASCII ?
566 " { encoding = ASCII; }" : "",
567 field->name);
568 (*iter_field)++;
569 break;
570 case ustctl_atype_variant:
571 ret = _lttng_variant_statedump(session, fields, nr_fields, iter_field, nesting);
572 if (ret) {
573 goto end;
574 }
575 break;
576 case ustctl_atype_struct:
577 ret = print_tabs(session, nesting);
578 if (ret) {
579 goto end;
580 }
581 ret = lttng_metadata_printf(session,
582 "struct {} _%s;\n",
583 field->name);
584 (*iter_field)++;
585 break;
586 default:
587 ret = -EINVAL;
588 }
589 end:
590 return ret;
591 }
592
593 static
594 int _lttng_context_metadata_statedump(struct ust_registry_session *session,
595 size_t nr_ctx_fields,
596 struct ustctl_field *ctx)
597 {
598 int ret = 0;
599 size_t i = 0;
600
601 if (!ctx)
602 return 0;
603 for (;;) {
604 if (i >= nr_ctx_fields) {
605 break;
606 }
607 ret = _lttng_field_statedump(session, ctx,
608 nr_ctx_fields, &i, 2);
609 if (ret) {
610 break;
611 }
612 }
613 return ret;
614 }
615
616 static
617 int _lttng_fields_metadata_statedump(struct ust_registry_session *session,
618 struct ust_registry_event *event)
619 {
620 int ret = 0;
621 size_t i = 0;
622
623 for (;;) {
624 if (i >= event->nr_fields) {
625 break;
626 }
627 ret = _lttng_field_statedump(session, event->fields,
628 event->nr_fields, &i, 2);
629 if (ret) {
630 break;
631 }
632 }
633 return ret;
634 }
635
636 /*
637 * Should be called with session registry mutex held.
638 */
639 int ust_metadata_event_statedump(struct ust_registry_session *session,
640 struct ust_registry_channel *chan,
641 struct ust_registry_event *event)
642 {
643 int ret = 0;
644
645 /* Don't dump metadata events */
646 if (chan->chan_id == -1U)
647 return 0;
648
649 ret = lttng_metadata_printf(session,
650 "event {\n"
651 " name = \"%s\";\n"
652 " id = %u;\n"
653 " stream_id = %u;\n",
654 event->name,
655 event->id,
656 chan->chan_id);
657 if (ret)
658 goto end;
659
660 ret = lttng_metadata_printf(session,
661 " loglevel = %d;\n",
662 event->loglevel_value);
663 if (ret)
664 goto end;
665
666 if (event->model_emf_uri) {
667 ret = lttng_metadata_printf(session,
668 " model.emf.uri = \"%s\";\n",
669 event->model_emf_uri);
670 if (ret)
671 goto end;
672 }
673
674 ret = lttng_metadata_printf(session,
675 " fields := struct {\n"
676 );
677 if (ret)
678 goto end;
679
680 ret = _lttng_fields_metadata_statedump(session, event);
681 if (ret)
682 goto end;
683
684 ret = lttng_metadata_printf(session,
685 " };\n"
686 "};\n\n");
687 if (ret)
688 goto end;
689 event->metadata_dumped = 1;
690
691 end:
692 return ret;
693 }
694
695 /*
696 * Should be called with session registry mutex held.
697 */
698 int ust_metadata_channel_statedump(struct ust_registry_session *session,
699 struct ust_registry_channel *chan)
700 {
701 int ret = 0;
702
703 /* Don't dump metadata events */
704 if (chan->chan_id == -1U)
705 return 0;
706
707 if (!chan->header_type)
708 return -EINVAL;
709
710 ret = lttng_metadata_printf(session,
711 "stream {\n"
712 " id = %u;\n"
713 " event.header := %s;\n"
714 " packet.context := struct packet_context;\n",
715 chan->chan_id,
716 chan->header_type == USTCTL_CHANNEL_HEADER_COMPACT ?
717 "struct event_header_compact" :
718 "struct event_header_large");
719 if (ret)
720 goto end;
721
722 if (chan->ctx_fields) {
723 ret = lttng_metadata_printf(session,
724 " event.context := struct {\n");
725 if (ret)
726 goto end;
727 }
728 ret = _lttng_context_metadata_statedump(session,
729 chan->nr_ctx_fields,
730 chan->ctx_fields);
731 if (ret)
732 goto end;
733 if (chan->ctx_fields) {
734 ret = lttng_metadata_printf(session,
735 " };\n");
736 if (ret)
737 goto end;
738 }
739
740 ret = lttng_metadata_printf(session,
741 "};\n\n");
742 /* Flag success of metadata dump. */
743 chan->metadata_dumped = 1;
744
745 end:
746 return ret;
747 }
748
749 static
750 int _lttng_stream_packet_context_declare(struct ust_registry_session *session)
751 {
752 return lttng_metadata_printf(session,
753 "struct packet_context {\n"
754 " uint64_clock_monotonic_t timestamp_begin;\n"
755 " uint64_clock_monotonic_t timestamp_end;\n"
756 " uint64_t content_size;\n"
757 " uint64_t packet_size;\n"
758 " uint64_t packet_seq_num;\n"
759 " unsigned long events_discarded;\n"
760 " uint32_t cpu_id;\n"
761 "};\n\n"
762 );
763 }
764
765 /*
766 * Compact header:
767 * id: range: 0 - 30.
768 * id 31 is reserved to indicate an extended header.
769 *
770 * Large header:
771 * id: range: 0 - 65534.
772 * id 65535 is reserved to indicate an extended header.
773 */
774 static
775 int _lttng_event_header_declare(struct ust_registry_session *session)
776 {
777 return lttng_metadata_printf(session,
778 "struct event_header_compact {\n"
779 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
780 " variant <id> {\n"
781 " struct {\n"
782 " uint27_clock_monotonic_t timestamp;\n"
783 " } compact;\n"
784 " struct {\n"
785 " uint32_t id;\n"
786 " uint64_clock_monotonic_t timestamp;\n"
787 " } extended;\n"
788 " } v;\n"
789 "} align(%u);\n"
790 "\n"
791 "struct event_header_large {\n"
792 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
793 " variant <id> {\n"
794 " struct {\n"
795 " uint32_clock_monotonic_t timestamp;\n"
796 " } compact;\n"
797 " struct {\n"
798 " uint32_t id;\n"
799 " uint64_clock_monotonic_t timestamp;\n"
800 " } extended;\n"
801 " } v;\n"
802 "} align(%u);\n\n",
803 session->uint32_t_alignment,
804 session->uint16_t_alignment
805 );
806 }
807
808 /*
809 * The offset between monotonic and realtime clock can be negative if
810 * the system sets the REALTIME clock to 0 after boot.
811 */
812 static
813 int measure_single_clock_offset(struct offset_sample *sample)
814 {
815 uint64_t monotonic_avg, monotonic[2], measure_delta, realtime;
816 uint64_t tcf = trace_clock_freq();
817 struct timespec rts = { 0, 0 };
818 int ret;
819
820 monotonic[0] = trace_clock_read64();
821 ret = lttng_clock_gettime(CLOCK_REALTIME, &rts);
822 if (ret < 0) {
823 return ret;
824 }
825 monotonic[1] = trace_clock_read64();
826 measure_delta = monotonic[1] - monotonic[0];
827 if (measure_delta > sample->measure_delta) {
828 /*
829 * Discard value if it took longer to read than the best
830 * sample so far.
831 */
832 return 0;
833 }
834 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
835 realtime = (uint64_t) rts.tv_sec * tcf;
836 if (tcf == NSEC_PER_SEC) {
837 realtime += rts.tv_nsec;
838 } else {
839 realtime += (uint64_t) rts.tv_nsec * tcf / NSEC_PER_SEC;
840 }
841 sample->offset = (int64_t) realtime - monotonic_avg;
842 sample->measure_delta = measure_delta;
843 return 0;
844 }
845
846 /*
847 * Approximation of NTP time of day to clock monotonic correlation,
848 * taken at start of trace. Keep the measurement that took the less time
849 * to complete, thus removing imprecision caused by preemption.
850 * May return a negative offset.
851 */
852 static
853 int64_t measure_clock_offset(void)
854 {
855 int i;
856 struct offset_sample offset_best_sample = {
857 .offset = 0,
858 .measure_delta = UINT64_MAX,
859 };
860
861 for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) {
862 if (measure_single_clock_offset(&offset_best_sample)) {
863 return 0;
864 }
865 }
866 return offset_best_sample.offset;
867 }
868
869 /*
870 * Should be called with session registry mutex held.
871 */
872 int ust_metadata_session_statedump(struct ust_registry_session *session,
873 struct ust_app *app,
874 uint32_t major,
875 uint32_t minor)
876 {
877 unsigned char *uuid_c;
878 char uuid_s[UUID_STR_LEN],
879 clock_uuid_s[UUID_STR_LEN];
880 int ret = 0;
881 char hostname[HOST_NAME_MAX];
882
883 assert(session);
884
885 uuid_c = session->uuid;
886
887 snprintf(uuid_s, sizeof(uuid_s),
888 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
889 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
890 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
891 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
892 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
893
894 /* For crash ABI */
895 ret = lttng_metadata_printf(session,
896 "/* CTF %u.%u */\n\n",
897 CTF_SPEC_MAJOR,
898 CTF_SPEC_MINOR);
899 if (ret) {
900 goto end;
901 }
902
903 ret = lttng_metadata_printf(session,
904 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
905 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
906 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
907 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
908 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
909 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
910 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
911 "\n"
912 "trace {\n"
913 " major = %u;\n"
914 " minor = %u;\n"
915 " uuid = \"%s\";\n"
916 " byte_order = %s;\n"
917 " packet.header := struct {\n"
918 " uint32_t magic;\n"
919 " uint8_t uuid[16];\n"
920 " uint32_t stream_id;\n"
921 " uint64_t stream_instance_id;\n"
922 " };\n"
923 "};\n\n",
924 session->uint8_t_alignment,
925 session->uint16_t_alignment,
926 session->uint32_t_alignment,
927 session->uint64_t_alignment,
928 session->bits_per_long,
929 session->long_alignment,
930 CTF_SPEC_MAJOR,
931 CTF_SPEC_MINOR,
932 uuid_s,
933 session->byte_order == BIG_ENDIAN ? "be" : "le"
934 );
935 if (ret)
936 goto end;
937
938 /* ignore error, just use empty string if error. */
939 hostname[0] = '\0';
940 ret = gethostname(hostname, sizeof(hostname));
941 if (ret && errno == ENAMETOOLONG)
942 hostname[HOST_NAME_MAX - 1] = '\0';
943 ret = lttng_metadata_printf(session,
944 "env {\n"
945 " hostname = \"%s\";\n"
946 " domain = \"ust\";\n"
947 " tracer_name = \"lttng-ust\";\n"
948 " tracer_major = %u;\n"
949 " tracer_minor = %u;\n",
950 hostname,
951 major,
952 minor
953 );
954 if (ret)
955 goto end;
956
957 /*
958 * If per-application registry, we can output extra information
959 * about the application.
960 */
961 if (app) {
962 ret = lttng_metadata_printf(session,
963 " tracer_patchlevel = %u;\n"
964 " vpid = %d;\n"
965 " procname = \"%s\";\n",
966 app->version.patchlevel,
967 (int) app->pid,
968 app->name
969 );
970 if (ret)
971 goto end;
972 }
973
974 ret = lttng_metadata_printf(session,
975 "};\n\n"
976 );
977 if (ret)
978 goto end;
979
980
981 ret = lttng_metadata_printf(session,
982 "clock {\n"
983 " name = \"%s\";\n",
984 trace_clock_name()
985 );
986 if (ret)
987 goto end;
988
989 if (!trace_clock_uuid(clock_uuid_s)) {
990 ret = lttng_metadata_printf(session,
991 " uuid = \"%s\";\n",
992 clock_uuid_s
993 );
994 if (ret)
995 goto end;
996 }
997
998 ret = lttng_metadata_printf(session,
999 " description = \"%s\";\n"
1000 " freq = %" PRIu64 "; /* Frequency, in Hz */\n"
1001 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1002 " offset = %" PRId64 ";\n"
1003 "};\n\n",
1004 trace_clock_description(),
1005 trace_clock_freq(),
1006 measure_clock_offset()
1007 );
1008 if (ret)
1009 goto end;
1010
1011 ret = lttng_metadata_printf(session,
1012 "typealias integer {\n"
1013 " size = 27; align = 1; signed = false;\n"
1014 " map = clock.%s.value;\n"
1015 "} := uint27_clock_monotonic_t;\n"
1016 "\n"
1017 "typealias integer {\n"
1018 " size = 32; align = %u; signed = false;\n"
1019 " map = clock.%s.value;\n"
1020 "} := uint32_clock_monotonic_t;\n"
1021 "\n"
1022 "typealias integer {\n"
1023 " size = 64; align = %u; signed = false;\n"
1024 " map = clock.%s.value;\n"
1025 "} := uint64_clock_monotonic_t;\n\n",
1026 trace_clock_name(),
1027 session->uint32_t_alignment,
1028 trace_clock_name(),
1029 session->uint64_t_alignment,
1030 trace_clock_name()
1031 );
1032 if (ret)
1033 goto end;
1034
1035 ret = _lttng_stream_packet_context_declare(session);
1036 if (ret)
1037 goto end;
1038
1039 ret = _lttng_event_header_declare(session);
1040 if (ret)
1041 goto end;
1042
1043 end:
1044 return ret;
1045 }
This page took 0.054824 seconds and 5 git commands to generate.