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