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