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