Fix: disable all ust events
[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 ret = lttng_metadata_printf(session,
375 " fields := struct {\n"
376 );
377 if (ret)
378 goto end;
379
380 ret = _lttng_fields_metadata_statedump(session, event);
381 if (ret)
382 goto end;
383
384 ret = lttng_metadata_printf(session,
385 " };\n"
386 "};\n\n");
387 if (ret)
388 goto end;
389 event->metadata_dumped = 1;
390
391 end:
392 return ret;
393 }
394
395 /*
396 * Should be called with session registry mutex held.
397 */
398 int ust_metadata_channel_statedump(struct ust_registry_session *session,
399 struct ust_registry_channel *chan)
400 {
401 int ret = 0;
402
403 /* Don't dump metadata events */
404 if (chan->chan_id == -1U)
405 return 0;
406
407 if (!chan->header_type)
408 return -EINVAL;
409
410 ret = lttng_metadata_printf(session,
411 "stream {\n"
412 " id = %u;\n"
413 " event.header := %s;\n"
414 " packet.context := struct packet_context;\n",
415 chan->chan_id,
416 chan->header_type == USTCTL_CHANNEL_HEADER_COMPACT ?
417 "struct event_header_compact" :
418 "struct event_header_large");
419 if (ret)
420 goto end;
421
422 if (chan->ctx_fields) {
423 ret = lttng_metadata_printf(session,
424 " event.context := struct {\n");
425 if (ret)
426 goto end;
427 }
428 ret = _lttng_context_metadata_statedump(session,
429 chan->nr_ctx_fields,
430 chan->ctx_fields);
431 if (ret)
432 goto end;
433 if (chan->ctx_fields) {
434 ret = lttng_metadata_printf(session,
435 " };\n");
436 if (ret)
437 goto end;
438 }
439
440 ret = lttng_metadata_printf(session,
441 "};\n\n");
442 /* Flag success of metadata dump. */
443 chan->metadata_dumped = 1;
444
445 end:
446 return ret;
447 }
448
449 static
450 int _lttng_stream_packet_context_declare(struct ust_registry_session *session)
451 {
452 return lttng_metadata_printf(session,
453 "struct packet_context {\n"
454 " uint64_clock_monotonic_t timestamp_begin;\n"
455 " uint64_clock_monotonic_t timestamp_end;\n"
456 " uint64_t content_size;\n"
457 " uint64_t packet_size;\n"
458 " unsigned long events_discarded;\n"
459 " uint32_t cpu_id;\n"
460 "};\n\n"
461 );
462 }
463
464 /*
465 * Compact header:
466 * id: range: 0 - 30.
467 * id 31 is reserved to indicate an extended header.
468 *
469 * Large header:
470 * id: range: 0 - 65534.
471 * id 65535 is reserved to indicate an extended header.
472 */
473 static
474 int _lttng_event_header_declare(struct ust_registry_session *session)
475 {
476 return lttng_metadata_printf(session,
477 "struct event_header_compact {\n"
478 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
479 " variant <id> {\n"
480 " struct {\n"
481 " uint27_clock_monotonic_t timestamp;\n"
482 " } compact;\n"
483 " struct {\n"
484 " uint32_t id;\n"
485 " uint64_clock_monotonic_t timestamp;\n"
486 " } extended;\n"
487 " } v;\n"
488 "} align(%u);\n"
489 "\n"
490 "struct event_header_large {\n"
491 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
492 " variant <id> {\n"
493 " struct {\n"
494 " uint32_clock_monotonic_t timestamp;\n"
495 " } compact;\n"
496 " struct {\n"
497 " uint32_t id;\n"
498 " uint64_clock_monotonic_t timestamp;\n"
499 " } extended;\n"
500 " } v;\n"
501 "} align(%u);\n\n",
502 session->uint32_t_alignment,
503 session->uint16_t_alignment
504 );
505 }
506
507 static
508 int measure_single_clock_offset(struct offset_sample *sample)
509 {
510 uint64_t offset, monotonic[2], measure_delta, realtime;
511 uint64_t tcf = trace_clock_freq();
512 struct timespec rts = { 0, 0 };
513 int ret;
514
515 monotonic[0] = trace_clock_read64();
516 ret = clock_gettime(CLOCK_REALTIME, &rts);
517 if (ret < 0) {
518 return ret;
519 }
520 monotonic[1] = trace_clock_read64();
521 measure_delta = monotonic[1] - monotonic[0];
522 if (measure_delta > sample->measure_delta) {
523 /*
524 * Discard value if it took longer to read than the best
525 * sample so far.
526 */
527 return 0;
528 }
529 offset = (monotonic[0] + monotonic[1]) >> 1;
530 realtime = (uint64_t) rts.tv_sec * tcf;
531 if (tcf == NSEC_PER_SEC) {
532 realtime += rts.tv_nsec;
533 } else {
534 realtime += (uint64_t) rts.tv_nsec * tcf / NSEC_PER_SEC;
535 }
536 offset = realtime - offset;
537 sample->offset = offset;
538 sample->measure_delta = measure_delta;
539 return 0;
540 }
541
542 /*
543 * Approximation of NTP time of day to clock monotonic correlation,
544 * taken at start of trace. Keep the measurement that took the less time
545 * to complete, thus removing imprecision caused by preemption.
546 */
547 static
548 uint64_t measure_clock_offset(void)
549 {
550 int i;
551 struct offset_sample offset_best_sample = {
552 .offset = 0,
553 .measure_delta = UINT64_MAX,
554 };
555
556 for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) {
557 if (measure_single_clock_offset(&offset_best_sample)) {
558 return 0;
559 }
560 }
561 return offset_best_sample.offset;
562 }
563
564 /*
565 * Should be called with session registry mutex held.
566 */
567 int ust_metadata_session_statedump(struct ust_registry_session *session,
568 struct ust_app *app,
569 uint32_t major,
570 uint32_t minor)
571 {
572 unsigned char *uuid_c;
573 char uuid_s[UUID_STR_LEN],
574 clock_uuid_s[UUID_STR_LEN];
575 int ret = 0;
576 char hostname[HOST_NAME_MAX];
577
578 assert(session);
579
580 uuid_c = session->uuid;
581
582 snprintf(uuid_s, sizeof(uuid_s),
583 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
584 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
585 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
586 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
587 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
588
589 /* For crash ABI */
590 ret = lttng_metadata_printf(session,
591 "/* CTF %u.%u */\n\n",
592 CTF_SPEC_MAJOR,
593 CTF_SPEC_MINOR);
594 if (ret) {
595 goto end;
596 }
597
598 ret = lttng_metadata_printf(session,
599 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
600 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
601 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
602 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
603 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
604 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
605 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
606 "\n"
607 "trace {\n"
608 " major = %u;\n"
609 " minor = %u;\n"
610 " uuid = \"%s\";\n"
611 " byte_order = %s;\n"
612 " packet.header := struct {\n"
613 " uint32_t magic;\n"
614 " uint8_t uuid[16];\n"
615 " uint32_t stream_id;\n"
616 " };\n"
617 "};\n\n",
618 session->uint8_t_alignment,
619 session->uint16_t_alignment,
620 session->uint32_t_alignment,
621 session->uint64_t_alignment,
622 session->bits_per_long,
623 session->long_alignment,
624 CTF_SPEC_MAJOR,
625 CTF_SPEC_MINOR,
626 uuid_s,
627 session->byte_order == BIG_ENDIAN ? "be" : "le"
628 );
629 if (ret)
630 goto end;
631
632 /* ignore error, just use empty string if error. */
633 hostname[0] = '\0';
634 ret = gethostname(hostname, sizeof(hostname));
635 if (ret && errno == ENAMETOOLONG)
636 hostname[HOST_NAME_MAX - 1] = '\0';
637 ret = lttng_metadata_printf(session,
638 "env {\n"
639 " hostname = \"%s\";\n"
640 " domain = \"ust\";\n"
641 " tracer_name = \"lttng-ust\";\n"
642 " tracer_major = %u;\n"
643 " tracer_minor = %u;\n",
644 hostname,
645 major,
646 minor
647 );
648 if (ret)
649 goto end;
650
651 /*
652 * If per-application registry, we can output extra information
653 * about the application.
654 */
655 if (app) {
656 ret = lttng_metadata_printf(session,
657 " tracer_patchlevel = %u;\n"
658 " vpid = %d;\n"
659 " procname = \"%s\";\n",
660 app->version.patchlevel,
661 (int) app->pid,
662 app->name
663 );
664 if (ret)
665 goto end;
666 }
667
668 ret = lttng_metadata_printf(session,
669 "};\n\n"
670 );
671 if (ret)
672 goto end;
673
674
675 ret = lttng_metadata_printf(session,
676 "clock {\n"
677 " name = \"%s\";\n",
678 trace_clock_name()
679 );
680 if (ret)
681 goto end;
682
683 if (!trace_clock_uuid(clock_uuid_s)) {
684 ret = lttng_metadata_printf(session,
685 " uuid = \"%s\";\n",
686 clock_uuid_s
687 );
688 if (ret)
689 goto end;
690 }
691
692 ret = lttng_metadata_printf(session,
693 " description = \"%s\";\n"
694 " freq = %" PRIu64 "; /* Frequency, in Hz */\n"
695 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
696 " offset = %" PRIu64 ";\n"
697 "};\n\n",
698 trace_clock_description(),
699 trace_clock_freq(),
700 measure_clock_offset()
701 );
702 if (ret)
703 goto end;
704
705 ret = lttng_metadata_printf(session,
706 "typealias integer {\n"
707 " size = 27; align = 1; signed = false;\n"
708 " map = clock.%s.value;\n"
709 "} := uint27_clock_monotonic_t;\n"
710 "\n"
711 "typealias integer {\n"
712 " size = 32; align = %u; signed = false;\n"
713 " map = clock.%s.value;\n"
714 "} := uint32_clock_monotonic_t;\n"
715 "\n"
716 "typealias integer {\n"
717 " size = 64; align = %u; signed = false;\n"
718 " map = clock.%s.value;\n"
719 "} := uint64_clock_monotonic_t;\n\n",
720 trace_clock_name(),
721 session->uint32_t_alignment,
722 trace_clock_name(),
723 session->uint64_t_alignment,
724 trace_clock_name()
725 );
726 if (ret)
727 goto end;
728
729 ret = _lttng_stream_packet_context_declare(session);
730 if (ret)
731 goto end;
732
733 ret = _lttng_event_header_declare(session);
734 if (ret)
735 goto end;
736
737 end:
738 return ret;
739 }
This page took 0.044635 seconds and 4 git commands to generate.