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