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