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