Fix: handle negative clock offset for lttng-ust metadata
[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 _GNU_SOURCE
23 #define _LGPL_SOURCE
24 #include <stdint.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <limits.h>
29 #include <unistd.h>
30 #include <inttypes.h>
31 #include <common/common.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 NSEC_PER_SEC 1000000000ULL
42 #define NR_CLOCK_OFFSET_SAMPLES 10
43
44 struct offset_sample {
45 uint64_t offset; /* correlation offset */
46 uint64_t measure_delta; /* lower is better */
47 };
48
49 static inline
50 int fls(unsigned int x)
51 {
52 int r = 32;
53
54 if (!x)
55 return 0;
56 if (!(x & 0xFFFF0000U)) {
57 x <<= 16;
58 r -= 16;
59 }
60 if (!(x & 0xFF000000U)) {
61 x <<= 8;
62 r -= 8;
63 }
64 if (!(x & 0xF0000000U)) {
65 x <<= 4;
66 r -= 4;
67 }
68 if (!(x & 0xC0000000U)) {
69 x <<= 2;
70 r -= 2;
71 }
72 if (!(x & 0x80000000U)) {
73 x <<= 1;
74 r -= 1;
75 }
76 return r;
77 }
78
79 static inline
80 int get_count_order(unsigned int count)
81 {
82 int order;
83
84 order = fls(count) - 1;
85 if (count & (count - 1))
86 order++;
87 return order;
88 }
89
90 /*
91 * Returns offset where to write in metadata array, or negative error value on error.
92 */
93 static
94 ssize_t metadata_reserve(struct ust_registry_session *session, size_t len)
95 {
96 size_t new_len = session->metadata_len + len;
97 size_t new_alloc_len = new_len;
98 size_t old_alloc_len = session->metadata_alloc_len;
99 ssize_t ret;
100
101 if (new_alloc_len > (UINT32_MAX >> 1))
102 return -EINVAL;
103 if ((old_alloc_len << 1) > (UINT32_MAX >> 1))
104 return -EINVAL;
105
106 if (new_alloc_len > old_alloc_len) {
107 char *newptr;
108
109 new_alloc_len =
110 max_t(size_t, 1U << get_count_order(new_alloc_len), old_alloc_len << 1);
111 newptr = realloc(session->metadata, new_alloc_len);
112 if (!newptr)
113 return -ENOMEM;
114 session->metadata = newptr;
115 /* We zero directly the memory from start of allocation. */
116 memset(&session->metadata[old_alloc_len], 0, new_alloc_len - old_alloc_len);
117 session->metadata_alloc_len = new_alloc_len;
118 }
119 ret = session->metadata_len;
120 session->metadata_len += len;
121 return ret;
122 }
123
124 static
125 int metadata_file_append(struct ust_registry_session *session,
126 const char *str, size_t len)
127 {
128 ssize_t written;
129
130 if (session->metadata_fd < 0) {
131 return 0;
132 }
133 /* Write to metadata file */
134 written = lttng_write(session->metadata_fd, str, len);
135 if (written != len) {
136 return -1;
137 }
138 return 0;
139 }
140
141 /*
142 * We have exclusive access to our metadata buffer (protected by the
143 * ust_lock), so we can do racy operations such as looking for
144 * remaining space left in packet and write, since mutual exclusion
145 * protects us from concurrent writes.
146 */
147 static
148 int lttng_metadata_printf(struct ust_registry_session *session,
149 const char *fmt, ...)
150 {
151 char *str = NULL;
152 size_t len;
153 va_list ap;
154 ssize_t offset;
155 int ret;
156
157 va_start(ap, fmt);
158 ret = vasprintf(&str, fmt, ap);
159 va_end(ap);
160 if (ret < 0)
161 return -ENOMEM;
162
163 len = strlen(str);
164 offset = metadata_reserve(session, len);
165 if (offset < 0) {
166 ret = offset;
167 goto end;
168 }
169 memcpy(&session->metadata[offset], str, len);
170 ret = metadata_file_append(session, str, len);
171 if (ret) {
172 PERROR("Error appending to metadata file");
173 goto end;
174 }
175 DBG3("Append to metadata: \"%s\"", str);
176 ret = 0;
177
178 end:
179 free(str);
180 return ret;
181 }
182
183 static
184 int _lttng_field_statedump(struct ust_registry_session *session,
185 const struct ustctl_field *field)
186 {
187 int ret = 0;
188 const char *bo_be = " byte_order = be;";
189 const char *bo_le = " byte_order = le;";
190 const char *bo_native = "";
191 const char *bo_reverse;
192
193 if (session->byte_order == BIG_ENDIAN)
194 bo_reverse = bo_le;
195 else
196 bo_reverse = bo_be;
197
198 switch (field->type.atype) {
199 case ustctl_atype_integer:
200 ret = lttng_metadata_printf(session,
201 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
202 field->type.u.basic.integer.size,
203 field->type.u.basic.integer.alignment,
204 field->type.u.basic.integer.signedness,
205 (field->type.u.basic.integer.encoding == ustctl_encode_none)
206 ? "none"
207 : (field->type.u.basic.integer.encoding == ustctl_encode_UTF8)
208 ? "UTF8"
209 : "ASCII",
210 field->type.u.basic.integer.base,
211 field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
212 field->name);
213 break;
214 case ustctl_atype_float:
215 ret = lttng_metadata_printf(session,
216 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
217 field->type.u.basic._float.exp_dig,
218 field->type.u.basic._float.mant_dig,
219 field->type.u.basic._float.alignment,
220 field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
221 field->name);
222 break;
223 case ustctl_atype_enum:
224 return -EINVAL;
225 case ustctl_atype_array:
226 {
227 const struct ustctl_basic_type *elem_type;
228
229 elem_type = &field->type.u.array.elem_type;
230 ret = lttng_metadata_printf(session,
231 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
232 elem_type->u.basic.integer.size,
233 elem_type->u.basic.integer.alignment,
234 elem_type->u.basic.integer.signedness,
235 (elem_type->u.basic.integer.encoding == ustctl_encode_none)
236 ? "none"
237 : (elem_type->u.basic.integer.encoding == ustctl_encode_UTF8)
238 ? "UTF8"
239 : "ASCII",
240 elem_type->u.basic.integer.base,
241 elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
242 field->name, field->type.u.array.length);
243 break;
244 }
245 case ustctl_atype_sequence:
246 {
247 const struct ustctl_basic_type *elem_type;
248 const struct ustctl_basic_type *length_type;
249
250 elem_type = &field->type.u.sequence.elem_type;
251 length_type = &field->type.u.sequence.length_type;
252 ret = lttng_metadata_printf(session,
253 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
254 length_type->u.basic.integer.size,
255 (unsigned int) length_type->u.basic.integer.alignment,
256 length_type->u.basic.integer.signedness,
257 (length_type->u.basic.integer.encoding == ustctl_encode_none)
258 ? "none"
259 : ((length_type->u.basic.integer.encoding == ustctl_encode_UTF8)
260 ? "UTF8"
261 : "ASCII"),
262 length_type->u.basic.integer.base,
263 length_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
264 field->name);
265 if (ret)
266 return ret;
267
268 ret = lttng_metadata_printf(session,
269 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
270 elem_type->u.basic.integer.size,
271 (unsigned int) elem_type->u.basic.integer.alignment,
272 elem_type->u.basic.integer.signedness,
273 (elem_type->u.basic.integer.encoding == ustctl_encode_none)
274 ? "none"
275 : ((elem_type->u.basic.integer.encoding == ustctl_encode_UTF8)
276 ? "UTF8"
277 : "ASCII"),
278 elem_type->u.basic.integer.base,
279 elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
280 field->name,
281 field->name);
282 break;
283 }
284
285 case ustctl_atype_string:
286 /* Default encoding is UTF8 */
287 ret = lttng_metadata_printf(session,
288 " string%s _%s;\n",
289 field->type.u.basic.string.encoding == ustctl_encode_ASCII ?
290 " { encoding = ASCII; }" : "",
291 field->name);
292 break;
293 default:
294 return -EINVAL;
295 }
296 return ret;
297 }
298
299 static
300 int _lttng_context_metadata_statedump(struct ust_registry_session *session,
301 size_t nr_ctx_fields,
302 struct ustctl_field *ctx)
303 {
304 int ret = 0;
305 int i;
306
307 if (!ctx)
308 return 0;
309 for (i = 0; i < nr_ctx_fields; i++) {
310 const struct ustctl_field *field = &ctx[i];
311
312 ret = _lttng_field_statedump(session, field);
313 if (ret)
314 return ret;
315 }
316 return ret;
317 }
318
319 static
320 int _lttng_fields_metadata_statedump(struct ust_registry_session *session,
321 struct ust_registry_event *event)
322 {
323 int ret = 0;
324 int i;
325
326 for (i = 0; i < event->nr_fields; i++) {
327 const struct ustctl_field *field = &event->fields[i];
328
329 ret = _lttng_field_statedump(session, field);
330 if (ret)
331 return ret;
332 }
333 return ret;
334 }
335
336 /*
337 * Should be called with session registry mutex held.
338 */
339 int ust_metadata_event_statedump(struct ust_registry_session *session,
340 struct ust_registry_channel *chan,
341 struct ust_registry_event *event)
342 {
343 int ret = 0;
344
345 /* Don't dump metadata events */
346 if (chan->chan_id == -1U)
347 return 0;
348
349 ret = lttng_metadata_printf(session,
350 "event {\n"
351 " name = \"%s\";\n"
352 " id = %u;\n"
353 " stream_id = %u;\n",
354 event->name,
355 event->id,
356 chan->chan_id);
357 if (ret)
358 goto end;
359
360 ret = lttng_metadata_printf(session,
361 " loglevel = %d;\n",
362 event->loglevel_value);
363 if (ret)
364 goto end;
365
366 if (event->model_emf_uri) {
367 ret = lttng_metadata_printf(session,
368 " model.emf.uri = \"%s\";\n",
369 event->model_emf_uri);
370 if (ret)
371 goto end;
372 }
373
374 #if 0 /* context for events not supported */
375 if (event->ctx) {
376 ret = lttng_metadata_printf(session,
377 " context := struct {\n");
378 if (ret)
379 goto end;
380 }
381 ret = _lttng_context_metadata_statedump(session, event->ctx);
382 if (ret)
383 goto end;
384 if (event->ctx) {
385 ret = lttng_metadata_printf(session,
386 " };\n");
387 if (ret)
388 goto end;
389 }
390 #endif
391 ret = lttng_metadata_printf(session,
392 " fields := struct {\n"
393 );
394 if (ret)
395 goto end;
396
397 ret = _lttng_fields_metadata_statedump(session, event);
398 if (ret)
399 goto end;
400
401 ret = lttng_metadata_printf(session,
402 " };\n"
403 "};\n\n");
404 if (ret)
405 goto end;
406 event->metadata_dumped = 1;
407
408 end:
409 return ret;
410 }
411
412 /*
413 * Should be called with session registry mutex held.
414 */
415 int ust_metadata_channel_statedump(struct ust_registry_session *session,
416 struct ust_registry_channel *chan)
417 {
418 int ret = 0;
419
420 /* Don't dump metadata events */
421 if (chan->chan_id == -1U)
422 return 0;
423
424 if (!chan->header_type)
425 return -EINVAL;
426
427 ret = lttng_metadata_printf(session,
428 "stream {\n"
429 " id = %u;\n"
430 " event.header := %s;\n"
431 " packet.context := struct packet_context;\n",
432 chan->chan_id,
433 chan->header_type == USTCTL_CHANNEL_HEADER_COMPACT ?
434 "struct event_header_compact" :
435 "struct event_header_large");
436 if (ret)
437 goto end;
438
439 if (chan->ctx_fields) {
440 ret = lttng_metadata_printf(session,
441 " event.context := struct {\n");
442 if (ret)
443 goto end;
444 }
445 ret = _lttng_context_metadata_statedump(session,
446 chan->nr_ctx_fields,
447 chan->ctx_fields);
448 if (ret)
449 goto end;
450 if (chan->ctx_fields) {
451 ret = lttng_metadata_printf(session,
452 " };\n");
453 if (ret)
454 goto end;
455 }
456
457 ret = lttng_metadata_printf(session,
458 "};\n\n");
459 /* Flag success of metadata dump. */
460 chan->metadata_dumped = 1;
461
462 end:
463 return ret;
464 }
465
466 static
467 int _lttng_stream_packet_context_declare(struct ust_registry_session *session)
468 {
469 return lttng_metadata_printf(session,
470 "struct packet_context {\n"
471 " uint64_clock_monotonic_t timestamp_begin;\n"
472 " uint64_clock_monotonic_t timestamp_end;\n"
473 " uint64_t content_size;\n"
474 " uint64_t packet_size;\n"
475 " unsigned long events_discarded;\n"
476 " uint32_t cpu_id;\n"
477 "};\n\n"
478 );
479 }
480
481 /*
482 * Compact header:
483 * id: range: 0 - 30.
484 * id 31 is reserved to indicate an extended header.
485 *
486 * Large header:
487 * id: range: 0 - 65534.
488 * id 65535 is reserved to indicate an extended header.
489 */
490 static
491 int _lttng_event_header_declare(struct ust_registry_session *session)
492 {
493 return lttng_metadata_printf(session,
494 "struct event_header_compact {\n"
495 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
496 " variant <id> {\n"
497 " struct {\n"
498 " uint27_clock_monotonic_t timestamp;\n"
499 " } compact;\n"
500 " struct {\n"
501 " uint32_t id;\n"
502 " uint64_clock_monotonic_t timestamp;\n"
503 " } extended;\n"
504 " } v;\n"
505 "} align(%u);\n"
506 "\n"
507 "struct event_header_large {\n"
508 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
509 " variant <id> {\n"
510 " struct {\n"
511 " uint32_clock_monotonic_t timestamp;\n"
512 " } compact;\n"
513 " struct {\n"
514 " uint32_t id;\n"
515 " uint64_clock_monotonic_t timestamp;\n"
516 " } extended;\n"
517 " } v;\n"
518 "} align(%u);\n\n",
519 session->uint32_t_alignment,
520 session->uint16_t_alignment
521 );
522 }
523
524 /*
525 * The offset between monotonic and realtime clock can be negative if
526 * the system sets the REALTIME clock to 0 after boot.
527 * Currently handle this by flooring the offset at 0.
528 */
529 static
530 int measure_single_clock_offset(struct offset_sample *sample)
531 {
532 uint64_t monotonic_avg, monotonic[2], measure_delta, realtime;
533 uint64_t tcf = trace_clock_freq();
534 int64_t offset;
535 struct timespec rts = { 0, 0 };
536 int ret;
537
538 monotonic[0] = trace_clock_read64();
539 ret = clock_gettime(CLOCK_REALTIME, &rts);
540 if (ret < 0) {
541 return ret;
542 }
543 monotonic[1] = trace_clock_read64();
544 measure_delta = monotonic[1] - monotonic[0];
545 if (measure_delta > sample->measure_delta) {
546 /*
547 * Discard value if it took longer to read than the best
548 * sample so far.
549 */
550 return 0;
551 }
552 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
553 realtime = (uint64_t) rts.tv_sec * tcf;
554 if (tcf == NSEC_PER_SEC) {
555 realtime += rts.tv_nsec;
556 } else {
557 realtime += (uint64_t) rts.tv_nsec * tcf / NSEC_PER_SEC;
558 }
559 offset = (int64_t) realtime - monotonic_avg;
560 sample->offset = max(offset, 0);
561 sample->measure_delta = measure_delta;
562 return 0;
563 }
564
565 /*
566 * Approximation of NTP time of day to clock monotonic correlation,
567 * taken at start of trace. Keep the measurement that took the less time
568 * to complete, thus removing imprecision caused by preemption.
569 */
570 static
571 uint64_t measure_clock_offset(void)
572 {
573 int i;
574 struct offset_sample offset_best_sample = {
575 .offset = 0,
576 .measure_delta = UINT64_MAX,
577 };
578
579 for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) {
580 if (measure_single_clock_offset(&offset_best_sample)) {
581 return 0;
582 }
583 }
584 return offset_best_sample.offset;
585 }
586
587 /*
588 * Should be called with session registry mutex held.
589 */
590 int ust_metadata_session_statedump(struct ust_registry_session *session,
591 struct ust_app *app,
592 uint32_t major,
593 uint32_t minor)
594 {
595 unsigned char *uuid_c;
596 char uuid_s[UUID_STR_LEN],
597 clock_uuid_s[UUID_STR_LEN];
598 int ret = 0;
599 char hostname[HOST_NAME_MAX];
600
601 assert(session);
602
603 uuid_c = session->uuid;
604
605 snprintf(uuid_s, sizeof(uuid_s),
606 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
607 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
608 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
609 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
610 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
611
612 /* For crash ABI */
613 ret = lttng_metadata_printf(session,
614 "/* CTF %u.%u */\n\n",
615 CTF_SPEC_MAJOR,
616 CTF_SPEC_MINOR);
617 if (ret) {
618 goto end;
619 }
620
621 ret = lttng_metadata_printf(session,
622 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
623 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
624 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
625 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
626 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
627 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
628 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
629 "\n"
630 "trace {\n"
631 " major = %u;\n"
632 " minor = %u;\n"
633 " uuid = \"%s\";\n"
634 " byte_order = %s;\n"
635 " packet.header := struct {\n"
636 " uint32_t magic;\n"
637 " uint8_t uuid[16];\n"
638 " uint32_t stream_id;\n"
639 " };\n"
640 "};\n\n",
641 session->uint8_t_alignment,
642 session->uint16_t_alignment,
643 session->uint32_t_alignment,
644 session->uint64_t_alignment,
645 session->bits_per_long,
646 session->long_alignment,
647 CTF_SPEC_MAJOR,
648 CTF_SPEC_MINOR,
649 uuid_s,
650 session->byte_order == BIG_ENDIAN ? "be" : "le"
651 );
652 if (ret)
653 goto end;
654
655 /* ignore error, just use empty string if error. */
656 hostname[0] = '\0';
657 ret = gethostname(hostname, sizeof(hostname));
658 if (ret && errno == ENAMETOOLONG)
659 hostname[HOST_NAME_MAX - 1] = '\0';
660 ret = lttng_metadata_printf(session,
661 "env {\n"
662 " hostname = \"%s\";\n"
663 " domain = \"ust\";\n"
664 " tracer_name = \"lttng-ust\";\n"
665 " tracer_major = %u;\n"
666 " tracer_minor = %u;\n",
667 hostname,
668 major,
669 minor
670 );
671 if (ret)
672 goto end;
673
674 /*
675 * If per-application registry, we can output extra information
676 * about the application.
677 */
678 if (app) {
679 ret = lttng_metadata_printf(session,
680 " tracer_patchlevel = %u;\n"
681 " vpid = %d;\n"
682 " procname = \"%s\";\n",
683 app->version.patchlevel,
684 (int) app->pid,
685 app->name
686 );
687 if (ret)
688 goto end;
689 }
690
691 ret = lttng_metadata_printf(session,
692 "};\n\n"
693 );
694 if (ret)
695 goto end;
696
697
698 ret = lttng_metadata_printf(session,
699 "clock {\n"
700 " name = \"%s\";\n",
701 trace_clock_name()
702 );
703 if (ret)
704 goto end;
705
706 if (!trace_clock_uuid(clock_uuid_s)) {
707 ret = lttng_metadata_printf(session,
708 " uuid = \"%s\";\n",
709 clock_uuid_s
710 );
711 if (ret)
712 goto end;
713 }
714
715 ret = lttng_metadata_printf(session,
716 " description = \"%s\";\n"
717 " freq = %" PRIu64 "; /* Frequency, in Hz */\n"
718 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
719 " offset = %" PRIu64 ";\n"
720 "};\n\n",
721 trace_clock_description(),
722 trace_clock_freq(),
723 measure_clock_offset()
724 );
725 if (ret)
726 goto end;
727
728 ret = lttng_metadata_printf(session,
729 "typealias integer {\n"
730 " size = 27; align = 1; signed = false;\n"
731 " map = clock.%s.value;\n"
732 "} := uint27_clock_monotonic_t;\n"
733 "\n"
734 "typealias integer {\n"
735 " size = 32; align = %u; signed = false;\n"
736 " map = clock.%s.value;\n"
737 "} := uint32_clock_monotonic_t;\n"
738 "\n"
739 "typealias integer {\n"
740 " size = 64; align = %u; signed = false;\n"
741 " map = clock.%s.value;\n"
742 "} := uint64_clock_monotonic_t;\n\n",
743 trace_clock_name(),
744 session->uint32_t_alignment,
745 trace_clock_name(),
746 session->uint64_t_alignment,
747 trace_clock_name()
748 );
749 if (ret)
750 goto end;
751
752 ret = _lttng_stream_packet_context_declare(session);
753 if (ret)
754 goto end;
755
756 ret = _lttng_event_header_declare(session);
757 if (ret)
758 goto end;
759
760 end:
761 return ret;
762 }
This page took 0.052535 seconds and 4 git commands to generate.