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 { | |
c2636b57 | 44 | int64_t offset; /* correlation offset */ |
8c645bb0 MD |
45 | uint64_t measure_delta; /* lower is better */ |
46 | }; | |
47 | ||
da860cab MD |
48 | static |
49 | int _lttng_field_statedump(struct ust_registry_session *session, | |
50 | const struct ustctl_field *fields, size_t nr_fields, | |
51 | size_t *iter_field, size_t nesting); | |
52 | ||
d0b96690 DG |
53 | static inline |
54 | int fls(unsigned int x) | |
55 | { | |
56 | int r = 32; | |
57 | ||
58 | if (!x) | |
59 | return 0; | |
60 | if (!(x & 0xFFFF0000U)) { | |
61 | x <<= 16; | |
62 | r -= 16; | |
63 | } | |
64 | if (!(x & 0xFF000000U)) { | |
65 | x <<= 8; | |
66 | r -= 8; | |
67 | } | |
68 | if (!(x & 0xF0000000U)) { | |
69 | x <<= 4; | |
70 | r -= 4; | |
71 | } | |
72 | if (!(x & 0xC0000000U)) { | |
73 | x <<= 2; | |
74 | r -= 2; | |
75 | } | |
76 | if (!(x & 0x80000000U)) { | |
77 | x <<= 1; | |
78 | r -= 1; | |
79 | } | |
80 | return r; | |
81 | } | |
82 | ||
83 | static inline | |
84 | int get_count_order(unsigned int count) | |
85 | { | |
86 | int order; | |
87 | ||
88 | order = fls(count) - 1; | |
89 | if (count & (count - 1)) | |
90 | order++; | |
91 | return order; | |
92 | } | |
93 | ||
94 | /* | |
95 | * Returns offset where to write in metadata array, or negative error value on error. | |
96 | */ | |
97 | static | |
98 | ssize_t metadata_reserve(struct ust_registry_session *session, size_t len) | |
99 | { | |
100 | size_t new_len = session->metadata_len + len; | |
101 | size_t new_alloc_len = new_len; | |
102 | size_t old_alloc_len = session->metadata_alloc_len; | |
103 | ssize_t ret; | |
104 | ||
105 | if (new_alloc_len > (UINT32_MAX >> 1)) | |
106 | return -EINVAL; | |
107 | if ((old_alloc_len << 1) > (UINT32_MAX >> 1)) | |
108 | return -EINVAL; | |
109 | ||
110 | if (new_alloc_len > old_alloc_len) { | |
111 | char *newptr; | |
112 | ||
113 | new_alloc_len = | |
114 | max_t(size_t, 1U << get_count_order(new_alloc_len), old_alloc_len << 1); | |
115 | newptr = realloc(session->metadata, new_alloc_len); | |
116 | if (!newptr) | |
117 | return -ENOMEM; | |
118 | session->metadata = newptr; | |
119 | /* We zero directly the memory from start of allocation. */ | |
120 | memset(&session->metadata[old_alloc_len], 0, new_alloc_len - old_alloc_len); | |
121 | session->metadata_alloc_len = new_alloc_len; | |
122 | } | |
123 | ret = session->metadata_len; | |
124 | session->metadata_len += len; | |
125 | return ret; | |
126 | } | |
127 | ||
d7ba1388 MD |
128 | static |
129 | int metadata_file_append(struct ust_registry_session *session, | |
130 | const char *str, size_t len) | |
131 | { | |
132 | ssize_t written; | |
133 | ||
134 | if (session->metadata_fd < 0) { | |
135 | return 0; | |
136 | } | |
137 | /* Write to metadata file */ | |
138 | written = lttng_write(session->metadata_fd, str, len); | |
139 | if (written != len) { | |
140 | return -1; | |
141 | } | |
142 | return 0; | |
143 | } | |
144 | ||
d0b96690 DG |
145 | /* |
146 | * We have exclusive access to our metadata buffer (protected by the | |
147 | * ust_lock), so we can do racy operations such as looking for | |
148 | * remaining space left in packet and write, since mutual exclusion | |
149 | * protects us from concurrent writes. | |
150 | */ | |
151 | static | |
152 | int lttng_metadata_printf(struct ust_registry_session *session, | |
153 | const char *fmt, ...) | |
154 | { | |
155 | char *str = NULL; | |
156 | size_t len; | |
157 | va_list ap; | |
158 | ssize_t offset; | |
159 | int ret; | |
160 | ||
161 | va_start(ap, fmt); | |
162 | ret = vasprintf(&str, fmt, ap); | |
163 | va_end(ap); | |
164 | if (ret < 0) | |
165 | return -ENOMEM; | |
166 | ||
167 | len = strlen(str); | |
168 | offset = metadata_reserve(session, len); | |
169 | if (offset < 0) { | |
170 | ret = offset; | |
171 | goto end; | |
172 | } | |
173 | memcpy(&session->metadata[offset], str, len); | |
d7ba1388 MD |
174 | ret = metadata_file_append(session, str, len); |
175 | if (ret) { | |
176 | PERROR("Error appending to metadata file"); | |
177 | goto end; | |
178 | } | |
d0b96690 DG |
179 | DBG3("Append to metadata: \"%s\"", str); |
180 | ret = 0; | |
181 | ||
182 | end: | |
183 | free(str); | |
184 | return ret; | |
185 | } | |
186 | ||
da860cab MD |
187 | static |
188 | int print_tabs(struct ust_registry_session *session, size_t nesting) | |
189 | { | |
190 | size_t i; | |
191 | ||
192 | for (i = 0; i < nesting; i++) { | |
193 | int ret; | |
194 | ||
195 | ret = lttng_metadata_printf(session, " "); | |
196 | if (ret) { | |
197 | return ret; | |
198 | } | |
199 | } | |
200 | return 0; | |
201 | } | |
202 | ||
a1f68b22 MD |
203 | static |
204 | void sanitize_ctf_identifier(char *out, const char *in) | |
205 | { | |
206 | size_t i; | |
207 | ||
208 | for (i = 0; i < LTTNG_UST_SYM_NAME_LEN; i++) { | |
209 | switch (in[i]) { | |
210 | case '.': | |
211 | case '$': | |
212 | case ':': | |
213 | out[i] = '_'; | |
214 | break; | |
215 | default: | |
216 | out[i] = in[i]; | |
217 | } | |
218 | } | |
219 | } | |
220 | ||
10b56aef MD |
221 | /* Called with session registry mutex held. */ |
222 | static | |
223 | int ust_metadata_enum_statedump(struct ust_registry_session *session, | |
224 | const char *enum_name, | |
225 | uint64_t enum_id, | |
226 | const struct ustctl_integer_type *container_type, | |
da860cab | 227 | const char *field_name, size_t *iter_field, size_t nesting) |
10b56aef MD |
228 | { |
229 | struct ust_registry_enum *reg_enum; | |
230 | const struct ustctl_enum_entry *entries; | |
231 | size_t nr_entries; | |
232 | int ret = 0; | |
233 | size_t i; | |
a1f68b22 | 234 | char identifier[LTTNG_UST_SYM_NAME_LEN]; |
10b56aef MD |
235 | |
236 | rcu_read_lock(); | |
237 | reg_enum = ust_registry_lookup_enum_by_id(session, enum_name, enum_id); | |
238 | rcu_read_unlock(); | |
239 | /* reg_enum can still be used because session registry mutex is held. */ | |
240 | if (!reg_enum) { | |
241 | ret = -ENOENT; | |
242 | goto end; | |
243 | } | |
244 | entries = reg_enum->entries; | |
245 | nr_entries = reg_enum->nr_entries; | |
246 | ||
da860cab MD |
247 | ret = print_tabs(session, nesting); |
248 | if (ret) { | |
249 | goto end; | |
250 | } | |
10b56aef | 251 | ret = lttng_metadata_printf(session, |
da860cab | 252 | "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n", |
10b56aef MD |
253 | container_type->size, |
254 | container_type->alignment, | |
255 | container_type->signedness, | |
256 | (container_type->encoding == ustctl_encode_none) | |
257 | ? "none" | |
258 | : (container_type->encoding == ustctl_encode_UTF8) | |
259 | ? "UTF8" | |
260 | : "ASCII", | |
261 | container_type->base); | |
262 | if (ret) { | |
263 | goto end; | |
264 | } | |
265 | /* Dump all entries */ | |
266 | for (i = 0; i < nr_entries; i++) { | |
267 | const struct ustctl_enum_entry *entry = &entries[i]; | |
268 | int j, len; | |
269 | ||
a1f68b22 MD |
270 | ret = print_tabs(session, nesting); |
271 | if (ret) { | |
272 | goto end; | |
273 | } | |
10b56aef | 274 | ret = lttng_metadata_printf(session, |
a1f68b22 | 275 | "\""); |
10b56aef MD |
276 | if (ret) { |
277 | goto end; | |
278 | } | |
279 | len = strlen(entry->string); | |
280 | /* Escape the character '"' */ | |
281 | for (j = 0; j < len; j++) { | |
282 | char c = entry->string[j]; | |
283 | ||
284 | switch (c) { | |
285 | case '"': | |
286 | ret = lttng_metadata_printf(session, | |
287 | "\\\""); | |
288 | break; | |
289 | case '\\': | |
290 | ret = lttng_metadata_printf(session, | |
291 | "\\\\"); | |
292 | break; | |
293 | default: | |
294 | ret = lttng_metadata_printf(session, | |
295 | "%c", c); | |
296 | break; | |
297 | } | |
298 | if (ret) { | |
299 | goto end; | |
300 | } | |
301 | } | |
302 | ret = lttng_metadata_printf(session, | |
303 | "\" = "); | |
304 | if (ret) { | |
305 | goto end; | |
306 | } | |
307 | if (entry->start == entry->end) { | |
308 | ret = lttng_metadata_printf(session, | |
309 | "%d,\n", | |
310 | entry->start); | |
311 | } else { | |
312 | ret = lttng_metadata_printf(session, | |
313 | "%d ... %d,\n", | |
314 | entry->start, entry->end); | |
315 | } | |
316 | if (ret) { | |
317 | goto end; | |
318 | } | |
319 | } | |
a1f68b22 MD |
320 | sanitize_ctf_identifier(identifier, field_name); |
321 | ret = print_tabs(session, nesting); | |
322 | if (ret) { | |
323 | goto end; | |
324 | } | |
325 | ret = lttng_metadata_printf(session, "} _%s;\n", | |
326 | identifier); | |
da860cab MD |
327 | end: |
328 | (*iter_field)++; | |
329 | return ret; | |
330 | } | |
331 | ||
da860cab MD |
332 | static |
333 | int _lttng_variant_statedump(struct ust_registry_session *session, | |
334 | const struct ustctl_field *fields, size_t nr_fields, | |
335 | size_t *iter_field, size_t nesting) | |
336 | { | |
337 | const struct ustctl_field *variant = &fields[*iter_field]; | |
338 | uint32_t nr_choices, i; | |
339 | int ret; | |
a1f68b22 | 340 | char identifier[LTTNG_UST_SYM_NAME_LEN]; |
da860cab MD |
341 | |
342 | if (variant->type.atype != ustctl_atype_variant) { | |
343 | ret = -EINVAL; | |
344 | goto end; | |
345 | } | |
346 | nr_choices = variant->type.u.variant.nr_choices; | |
347 | (*iter_field)++; | |
a1f68b22 MD |
348 | sanitize_ctf_identifier(identifier, variant->type.u.variant.tag_name); |
349 | ret = print_tabs(session, nesting); | |
350 | if (ret) { | |
351 | goto end; | |
352 | } | |
da860cab | 353 | ret = lttng_metadata_printf(session, |
a1f68b22 MD |
354 | "variant <_%s> {\n", |
355 | identifier); | |
da860cab MD |
356 | if (ret) { |
357 | goto end; | |
358 | } | |
359 | ||
360 | for (i = 0; i < nr_choices; i++) { | |
361 | if (*iter_field >= nr_fields) { | |
362 | ret = -EOVERFLOW; | |
363 | goto end; | |
364 | } | |
365 | ret = _lttng_field_statedump(session, | |
366 | fields, nr_fields, | |
367 | iter_field, nesting + 1); | |
dc6403f3 JG |
368 | if (ret) { |
369 | goto end; | |
370 | } | |
da860cab | 371 | } |
a1f68b22 MD |
372 | sanitize_ctf_identifier(identifier, variant->name); |
373 | ret = print_tabs(session, nesting); | |
da860cab | 374 | ret = lttng_metadata_printf(session, |
a1f68b22 MD |
375 | "} _%s;\n", |
376 | identifier); | |
da860cab MD |
377 | if (ret) { |
378 | goto end; | |
379 | } | |
10b56aef MD |
380 | end: |
381 | return ret; | |
382 | } | |
383 | ||
d0b96690 DG |
384 | static |
385 | int _lttng_field_statedump(struct ust_registry_session *session, | |
da860cab MD |
386 | const struct ustctl_field *fields, size_t nr_fields, |
387 | size_t *iter_field, size_t nesting) | |
d0b96690 DG |
388 | { |
389 | int ret = 0; | |
390 | const char *bo_be = " byte_order = be;"; | |
391 | const char *bo_le = " byte_order = le;"; | |
392 | const char *bo_native = ""; | |
393 | const char *bo_reverse; | |
da860cab | 394 | const struct ustctl_field *field; |
d0b96690 | 395 | |
da860cab MD |
396 | if (*iter_field >= nr_fields) { |
397 | ret = -EOVERFLOW; | |
398 | goto end; | |
399 | } | |
400 | field = &fields[*iter_field]; | |
401 | ||
402 | if (session->byte_order == BIG_ENDIAN) { | |
d0b96690 | 403 | bo_reverse = bo_le; |
da860cab | 404 | } else { |
d0b96690 | 405 | bo_reverse = bo_be; |
da860cab | 406 | } |
d0b96690 DG |
407 | |
408 | switch (field->type.atype) { | |
409 | case ustctl_atype_integer: | |
da860cab MD |
410 | ret = print_tabs(session, nesting); |
411 | if (ret) { | |
412 | goto end; | |
413 | } | |
d0b96690 | 414 | ret = lttng_metadata_printf(session, |
da860cab | 415 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n", |
d0b96690 DG |
416 | field->type.u.basic.integer.size, |
417 | field->type.u.basic.integer.alignment, | |
418 | field->type.u.basic.integer.signedness, | |
419 | (field->type.u.basic.integer.encoding == ustctl_encode_none) | |
420 | ? "none" | |
421 | : (field->type.u.basic.integer.encoding == ustctl_encode_UTF8) | |
422 | ? "UTF8" | |
423 | : "ASCII", | |
424 | field->type.u.basic.integer.base, | |
425 | field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
426 | field->name); | |
da860cab | 427 | (*iter_field)++; |
d0b96690 | 428 | break; |
10b56aef MD |
429 | case ustctl_atype_enum: |
430 | ret = ust_metadata_enum_statedump(session, | |
431 | field->type.u.basic.enumeration.name, | |
432 | field->type.u.basic.enumeration.id, | |
433 | &field->type.u.basic.enumeration.container_type, | |
da860cab | 434 | field->name, iter_field, nesting); |
10b56aef | 435 | break; |
d0b96690 | 436 | case ustctl_atype_float: |
da860cab MD |
437 | ret = print_tabs(session, nesting); |
438 | if (ret) { | |
439 | goto end; | |
440 | } | |
d0b96690 | 441 | ret = lttng_metadata_printf(session, |
da860cab | 442 | "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n", |
d0b96690 DG |
443 | field->type.u.basic._float.exp_dig, |
444 | field->type.u.basic._float.mant_dig, | |
445 | field->type.u.basic._float.alignment, | |
446 | field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
447 | field->name); | |
da860cab | 448 | (*iter_field)++; |
d0b96690 | 449 | break; |
d0b96690 DG |
450 | case ustctl_atype_array: |
451 | { | |
452 | const struct ustctl_basic_type *elem_type; | |
453 | ||
da860cab MD |
454 | ret = print_tabs(session, nesting); |
455 | if (ret) { | |
456 | goto end; | |
457 | } | |
d0b96690 DG |
458 | elem_type = &field->type.u.array.elem_type; |
459 | ret = lttng_metadata_printf(session, | |
da860cab | 460 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n", |
d0b96690 DG |
461 | elem_type->u.basic.integer.size, |
462 | elem_type->u.basic.integer.alignment, | |
463 | elem_type->u.basic.integer.signedness, | |
464 | (elem_type->u.basic.integer.encoding == ustctl_encode_none) | |
465 | ? "none" | |
466 | : (elem_type->u.basic.integer.encoding == ustctl_encode_UTF8) | |
467 | ? "UTF8" | |
468 | : "ASCII", | |
469 | elem_type->u.basic.integer.base, | |
470 | elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
471 | field->name, field->type.u.array.length); | |
da860cab | 472 | (*iter_field)++; |
d0b96690 DG |
473 | break; |
474 | } | |
475 | case ustctl_atype_sequence: | |
476 | { | |
477 | const struct ustctl_basic_type *elem_type; | |
478 | const struct ustctl_basic_type *length_type; | |
479 | ||
480 | elem_type = &field->type.u.sequence.elem_type; | |
481 | length_type = &field->type.u.sequence.length_type; | |
da860cab MD |
482 | ret = print_tabs(session, nesting); |
483 | if (ret) { | |
484 | goto end; | |
485 | } | |
d0b96690 | 486 | ret = lttng_metadata_printf(session, |
da860cab | 487 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n", |
d0b96690 DG |
488 | length_type->u.basic.integer.size, |
489 | (unsigned int) length_type->u.basic.integer.alignment, | |
490 | length_type->u.basic.integer.signedness, | |
491 | (length_type->u.basic.integer.encoding == ustctl_encode_none) | |
492 | ? "none" | |
493 | : ((length_type->u.basic.integer.encoding == ustctl_encode_UTF8) | |
494 | ? "UTF8" | |
495 | : "ASCII"), | |
496 | length_type->u.basic.integer.base, | |
497 | length_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
498 | field->name); | |
da860cab MD |
499 | if (ret) { |
500 | goto end; | |
501 | } | |
d0b96690 | 502 | |
da860cab MD |
503 | ret = print_tabs(session, nesting); |
504 | if (ret) { | |
505 | goto end; | |
506 | } | |
d0b96690 | 507 | ret = lttng_metadata_printf(session, |
da860cab | 508 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n", |
d0b96690 DG |
509 | elem_type->u.basic.integer.size, |
510 | (unsigned int) elem_type->u.basic.integer.alignment, | |
511 | elem_type->u.basic.integer.signedness, | |
512 | (elem_type->u.basic.integer.encoding == ustctl_encode_none) | |
513 | ? "none" | |
514 | : ((elem_type->u.basic.integer.encoding == ustctl_encode_UTF8) | |
515 | ? "UTF8" | |
516 | : "ASCII"), | |
517 | elem_type->u.basic.integer.base, | |
518 | elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
519 | field->name, | |
520 | field->name); | |
da860cab | 521 | (*iter_field)++; |
d0b96690 DG |
522 | break; |
523 | } | |
524 | ||
525 | case ustctl_atype_string: | |
526 | /* Default encoding is UTF8 */ | |
da860cab MD |
527 | ret = print_tabs(session, nesting); |
528 | if (ret) { | |
529 | goto end; | |
530 | } | |
d0b96690 | 531 | ret = lttng_metadata_printf(session, |
da860cab | 532 | "string%s _%s;\n", |
d0b96690 DG |
533 | field->type.u.basic.string.encoding == ustctl_encode_ASCII ? |
534 | " { encoding = ASCII; }" : "", | |
535 | field->name); | |
da860cab MD |
536 | (*iter_field)++; |
537 | break; | |
538 | case ustctl_atype_variant: | |
539 | ret = _lttng_variant_statedump(session, fields, nr_fields, iter_field, nesting); | |
540 | if (ret) { | |
541 | goto end; | |
542 | } | |
543 | break; | |
544 | case ustctl_atype_struct: | |
545 | ret = print_tabs(session, nesting); | |
546 | if (ret) { | |
547 | goto end; | |
548 | } | |
549 | ret = lttng_metadata_printf(session, | |
550 | "struct {} _%s;\n", | |
551 | field->name); | |
552 | (*iter_field)++; | |
d0b96690 DG |
553 | break; |
554 | default: | |
da860cab | 555 | ret = -EINVAL; |
d0b96690 | 556 | } |
da860cab | 557 | end: |
d0b96690 DG |
558 | return ret; |
559 | } | |
560 | ||
561 | static | |
562 | int _lttng_context_metadata_statedump(struct ust_registry_session *session, | |
563 | size_t nr_ctx_fields, | |
564 | struct ustctl_field *ctx) | |
565 | { | |
566 | int ret = 0; | |
da860cab | 567 | size_t i = 0; |
d0b96690 DG |
568 | |
569 | if (!ctx) | |
570 | return 0; | |
da860cab MD |
571 | for (;;) { |
572 | if (i >= nr_ctx_fields) { | |
573 | break; | |
574 | } | |
575 | ret = _lttng_field_statedump(session, ctx, | |
576 | nr_ctx_fields, &i, 2); | |
577 | if (ret) { | |
578 | break; | |
579 | } | |
d0b96690 DG |
580 | } |
581 | return ret; | |
582 | } | |
583 | ||
584 | static | |
585 | int _lttng_fields_metadata_statedump(struct ust_registry_session *session, | |
586 | struct ust_registry_event *event) | |
587 | { | |
588 | int ret = 0; | |
da860cab | 589 | size_t i = 0; |
d0b96690 | 590 | |
da860cab MD |
591 | for (;;) { |
592 | if (i >= event->nr_fields) { | |
593 | break; | |
594 | } | |
595 | ret = _lttng_field_statedump(session, event->fields, | |
596 | event->nr_fields, &i, 2); | |
597 | if (ret) { | |
598 | break; | |
599 | } | |
d0b96690 DG |
600 | } |
601 | return ret; | |
602 | } | |
603 | ||
604 | /* | |
605 | * Should be called with session registry mutex held. | |
606 | */ | |
607 | int ust_metadata_event_statedump(struct ust_registry_session *session, | |
608 | struct ust_registry_channel *chan, | |
609 | struct ust_registry_event *event) | |
610 | { | |
611 | int ret = 0; | |
612 | ||
613 | /* Don't dump metadata events */ | |
614 | if (chan->chan_id == -1U) | |
615 | return 0; | |
616 | ||
617 | ret = lttng_metadata_printf(session, | |
618 | "event {\n" | |
619 | " name = \"%s\";\n" | |
620 | " id = %u;\n" | |
621 | " stream_id = %u;\n", | |
622 | event->name, | |
623 | event->id, | |
624 | chan->chan_id); | |
625 | if (ret) | |
626 | goto end; | |
627 | ||
628 | ret = lttng_metadata_printf(session, | |
629 | " loglevel = %d;\n", | |
2106efa0 | 630 | event->loglevel_value); |
d0b96690 DG |
631 | if (ret) |
632 | goto end; | |
633 | ||
634 | if (event->model_emf_uri) { | |
635 | ret = lttng_metadata_printf(session, | |
636 | " model.emf.uri = \"%s\";\n", | |
637 | event->model_emf_uri); | |
638 | if (ret) | |
639 | goto end; | |
640 | } | |
641 | ||
d0b96690 DG |
642 | ret = lttng_metadata_printf(session, |
643 | " fields := struct {\n" | |
644 | ); | |
645 | if (ret) | |
646 | goto end; | |
647 | ||
648 | ret = _lttng_fields_metadata_statedump(session, event); | |
649 | if (ret) | |
650 | goto end; | |
651 | ||
652 | ret = lttng_metadata_printf(session, | |
653 | " };\n" | |
654 | "};\n\n"); | |
655 | if (ret) | |
656 | goto end; | |
7972aab2 | 657 | event->metadata_dumped = 1; |
d0b96690 DG |
658 | |
659 | end: | |
660 | return ret; | |
661 | } | |
662 | ||
663 | /* | |
664 | * Should be called with session registry mutex held. | |
665 | */ | |
666 | int ust_metadata_channel_statedump(struct ust_registry_session *session, | |
667 | struct ust_registry_channel *chan) | |
668 | { | |
669 | int ret = 0; | |
670 | ||
671 | /* Don't dump metadata events */ | |
672 | if (chan->chan_id == -1U) | |
673 | return 0; | |
674 | ||
675 | if (!chan->header_type) | |
676 | return -EINVAL; | |
677 | ||
678 | ret = lttng_metadata_printf(session, | |
679 | "stream {\n" | |
680 | " id = %u;\n" | |
681 | " event.header := %s;\n" | |
682 | " packet.context := struct packet_context;\n", | |
683 | chan->chan_id, | |
684 | chan->header_type == USTCTL_CHANNEL_HEADER_COMPACT ? | |
685 | "struct event_header_compact" : | |
686 | "struct event_header_large"); | |
687 | if (ret) | |
688 | goto end; | |
689 | ||
690 | if (chan->ctx_fields) { | |
691 | ret = lttng_metadata_printf(session, | |
692 | " event.context := struct {\n"); | |
693 | if (ret) | |
694 | goto end; | |
695 | } | |
696 | ret = _lttng_context_metadata_statedump(session, | |
697 | chan->nr_ctx_fields, | |
698 | chan->ctx_fields); | |
699 | if (ret) | |
700 | goto end; | |
701 | if (chan->ctx_fields) { | |
702 | ret = lttng_metadata_printf(session, | |
703 | " };\n"); | |
704 | if (ret) | |
705 | goto end; | |
706 | } | |
707 | ||
708 | ret = lttng_metadata_printf(session, | |
709 | "};\n\n"); | |
7972aab2 DG |
710 | /* Flag success of metadata dump. */ |
711 | chan->metadata_dumped = 1; | |
d0b96690 DG |
712 | |
713 | end: | |
714 | return ret; | |
715 | } | |
716 | ||
717 | static | |
718 | int _lttng_stream_packet_context_declare(struct ust_registry_session *session) | |
719 | { | |
720 | return lttng_metadata_printf(session, | |
721 | "struct packet_context {\n" | |
722 | " uint64_clock_monotonic_t timestamp_begin;\n" | |
723 | " uint64_clock_monotonic_t timestamp_end;\n" | |
724 | " uint64_t content_size;\n" | |
725 | " uint64_t packet_size;\n" | |
0793bcc6 | 726 | " uint64_t packet_seq_num;\n" |
d0b96690 DG |
727 | " unsigned long events_discarded;\n" |
728 | " uint32_t cpu_id;\n" | |
729 | "};\n\n" | |
730 | ); | |
731 | } | |
732 | ||
733 | /* | |
734 | * Compact header: | |
735 | * id: range: 0 - 30. | |
736 | * id 31 is reserved to indicate an extended header. | |
737 | * | |
738 | * Large header: | |
739 | * id: range: 0 - 65534. | |
740 | * id 65535 is reserved to indicate an extended header. | |
741 | */ | |
742 | static | |
743 | int _lttng_event_header_declare(struct ust_registry_session *session) | |
744 | { | |
745 | return lttng_metadata_printf(session, | |
746 | "struct event_header_compact {\n" | |
747 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" | |
748 | " variant <id> {\n" | |
749 | " struct {\n" | |
750 | " uint27_clock_monotonic_t timestamp;\n" | |
751 | " } compact;\n" | |
752 | " struct {\n" | |
753 | " uint32_t id;\n" | |
754 | " uint64_clock_monotonic_t timestamp;\n" | |
755 | " } extended;\n" | |
756 | " } v;\n" | |
757 | "} align(%u);\n" | |
758 | "\n" | |
759 | "struct event_header_large {\n" | |
760 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" | |
761 | " variant <id> {\n" | |
762 | " struct {\n" | |
763 | " uint32_clock_monotonic_t timestamp;\n" | |
764 | " } compact;\n" | |
765 | " struct {\n" | |
766 | " uint32_t id;\n" | |
767 | " uint64_clock_monotonic_t timestamp;\n" | |
768 | " } extended;\n" | |
769 | " } v;\n" | |
770 | "} align(%u);\n\n", | |
771 | session->uint32_t_alignment, | |
772 | session->uint16_t_alignment | |
773 | ); | |
774 | } | |
775 | ||
dc113ec7 MD |
776 | /* |
777 | * The offset between monotonic and realtime clock can be negative if | |
778 | * the system sets the REALTIME clock to 0 after boot. | |
dc113ec7 | 779 | */ |
d0b96690 | 780 | static |
8c645bb0 | 781 | int measure_single_clock_offset(struct offset_sample *sample) |
d0b96690 | 782 | { |
dc113ec7 | 783 | uint64_t monotonic_avg, monotonic[2], measure_delta, realtime; |
fc0bb9fa | 784 | uint64_t tcf = trace_clock_freq(); |
d0b96690 DG |
785 | struct timespec rts = { 0, 0 }; |
786 | int ret; | |
787 | ||
788 | monotonic[0] = trace_clock_read64(); | |
789 | ret = clock_gettime(CLOCK_REALTIME, &rts); | |
8c645bb0 MD |
790 | if (ret < 0) { |
791 | return ret; | |
792 | } | |
d0b96690 | 793 | monotonic[1] = trace_clock_read64(); |
8c645bb0 MD |
794 | measure_delta = monotonic[1] - monotonic[0]; |
795 | if (measure_delta > sample->measure_delta) { | |
796 | /* | |
797 | * Discard value if it took longer to read than the best | |
798 | * sample so far. | |
799 | */ | |
800 | return 0; | |
801 | } | |
dc113ec7 | 802 | monotonic_avg = (monotonic[0] + monotonic[1]) >> 1; |
6e1b0543 MD |
803 | realtime = (uint64_t) rts.tv_sec * tcf; |
804 | if (tcf == NSEC_PER_SEC) { | |
805 | realtime += rts.tv_nsec; | |
806 | } else { | |
807 | realtime += (uint64_t) rts.tv_nsec * tcf / NSEC_PER_SEC; | |
fc0bb9fa | 808 | } |
c2636b57 | 809 | sample->offset = (int64_t) realtime - monotonic_avg; |
8c645bb0 MD |
810 | sample->measure_delta = measure_delta; |
811 | return 0; | |
d0b96690 DG |
812 | } |
813 | ||
8c645bb0 MD |
814 | /* |
815 | * Approximation of NTP time of day to clock monotonic correlation, | |
816 | * taken at start of trace. Keep the measurement that took the less time | |
817 | * to complete, thus removing imprecision caused by preemption. | |
c2636b57 | 818 | * May return a negative offset. |
8c645bb0 MD |
819 | */ |
820 | static | |
c2636b57 | 821 | int64_t measure_clock_offset(void) |
8c645bb0 MD |
822 | { |
823 | int i; | |
824 | struct offset_sample offset_best_sample = { | |
825 | .offset = 0, | |
826 | .measure_delta = UINT64_MAX, | |
827 | }; | |
828 | ||
829 | for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) { | |
830 | if (measure_single_clock_offset(&offset_best_sample)) { | |
831 | return 0; | |
832 | } | |
833 | } | |
834 | return offset_best_sample.offset; | |
835 | } | |
d0b96690 DG |
836 | |
837 | /* | |
838 | * Should be called with session registry mutex held. | |
839 | */ | |
840 | int ust_metadata_session_statedump(struct ust_registry_session *session, | |
af6142cf MD |
841 | struct ust_app *app, |
842 | uint32_t major, | |
843 | uint32_t minor) | |
d0b96690 DG |
844 | { |
845 | unsigned char *uuid_c; | |
846 | char uuid_s[UUID_STR_LEN], | |
847 | clock_uuid_s[UUID_STR_LEN]; | |
848 | int ret = 0; | |
849 | char hostname[HOST_NAME_MAX]; | |
850 | ||
7972aab2 | 851 | assert(session); |
7972aab2 | 852 | |
d0b96690 DG |
853 | uuid_c = session->uuid; |
854 | ||
855 | snprintf(uuid_s, sizeof(uuid_s), | |
856 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", | |
857 | uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3], | |
858 | uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7], | |
859 | uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11], | |
860 | uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]); | |
861 | ||
d7ba1388 MD |
862 | /* For crash ABI */ |
863 | ret = lttng_metadata_printf(session, | |
864 | "/* CTF %u.%u */\n\n", | |
865 | CTF_SPEC_MAJOR, | |
866 | CTF_SPEC_MINOR); | |
867 | if (ret) { | |
868 | goto end; | |
869 | } | |
870 | ||
d0b96690 DG |
871 | ret = lttng_metadata_printf(session, |
872 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" | |
873 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" | |
874 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" | |
875 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" | |
876 | "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n" | |
877 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" | |
878 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" | |
879 | "\n" | |
880 | "trace {\n" | |
881 | " major = %u;\n" | |
882 | " minor = %u;\n" | |
883 | " uuid = \"%s\";\n" | |
884 | " byte_order = %s;\n" | |
885 | " packet.header := struct {\n" | |
886 | " uint32_t magic;\n" | |
887 | " uint8_t uuid[16];\n" | |
888 | " uint32_t stream_id;\n" | |
0793bcc6 | 889 | " uint64_t stream_instance_id;\n" |
d0b96690 DG |
890 | " };\n" |
891 | "};\n\n", | |
892 | session->uint8_t_alignment, | |
893 | session->uint16_t_alignment, | |
894 | session->uint32_t_alignment, | |
895 | session->uint64_t_alignment, | |
896 | session->bits_per_long, | |
897 | session->long_alignment, | |
898 | CTF_SPEC_MAJOR, | |
899 | CTF_SPEC_MINOR, | |
900 | uuid_s, | |
901 | session->byte_order == BIG_ENDIAN ? "be" : "le" | |
902 | ); | |
903 | if (ret) | |
904 | goto end; | |
905 | ||
906 | /* ignore error, just use empty string if error. */ | |
907 | hostname[0] = '\0'; | |
908 | ret = gethostname(hostname, sizeof(hostname)); | |
909 | if (ret && errno == ENAMETOOLONG) | |
910 | hostname[HOST_NAME_MAX - 1] = '\0'; | |
911 | ret = lttng_metadata_printf(session, | |
912 | "env {\n" | |
913 | " hostname = \"%s\";\n" | |
914 | " domain = \"ust\";\n" | |
915 | " tracer_name = \"lttng-ust\";\n" | |
916 | " tracer_major = %u;\n" | |
af6142cf | 917 | " tracer_minor = %u;\n", |
d0b96690 | 918 | hostname, |
af6142cf MD |
919 | major, |
920 | minor | |
d0b96690 DG |
921 | ); |
922 | if (ret) | |
923 | goto end; | |
924 | ||
925 | /* | |
926 | * If per-application registry, we can output extra information | |
927 | * about the application. | |
928 | */ | |
929 | if (app) { | |
930 | ret = lttng_metadata_printf(session, | |
af6142cf | 931 | " tracer_patchlevel = %u;\n" |
d0b96690 | 932 | " vpid = %d;\n" |
d88aee68 | 933 | " procname = \"%s\";\n", |
af6142cf | 934 | app->version.patchlevel, |
d0b96690 DG |
935 | (int) app->pid, |
936 | app->name | |
937 | ); | |
938 | if (ret) | |
939 | goto end; | |
940 | } | |
941 | ||
942 | ret = lttng_metadata_printf(session, | |
943 | "};\n\n" | |
944 | ); | |
945 | if (ret) | |
946 | goto end; | |
947 | ||
948 | ||
949 | ret = lttng_metadata_printf(session, | |
950 | "clock {\n" | |
fc0bb9fa MD |
951 | " name = \"%s\";\n", |
952 | trace_clock_name() | |
d0b96690 DG |
953 | ); |
954 | if (ret) | |
955 | goto end; | |
956 | ||
957 | if (!trace_clock_uuid(clock_uuid_s)) { | |
958 | ret = lttng_metadata_printf(session, | |
959 | " uuid = \"%s\";\n", | |
960 | clock_uuid_s | |
961 | ); | |
962 | if (ret) | |
963 | goto end; | |
964 | } | |
965 | ||
966 | ret = lttng_metadata_printf(session, | |
fc0bb9fa | 967 | " description = \"%s\";\n" |
d0b96690 DG |
968 | " freq = %" PRIu64 "; /* Frequency, in Hz */\n" |
969 | " /* clock value offset from Epoch is: offset * (1/freq) */\n" | |
c2636b57 | 970 | " offset = %" PRId64 ";\n" |
d0b96690 | 971 | "};\n\n", |
fc0bb9fa | 972 | trace_clock_description(), |
d0b96690 DG |
973 | trace_clock_freq(), |
974 | measure_clock_offset() | |
975 | ); | |
976 | if (ret) | |
977 | goto end; | |
978 | ||
979 | ret = lttng_metadata_printf(session, | |
980 | "typealias integer {\n" | |
981 | " size = 27; align = 1; signed = false;\n" | |
fc0bb9fa | 982 | " map = clock.%s.value;\n" |
d0b96690 DG |
983 | "} := uint27_clock_monotonic_t;\n" |
984 | "\n" | |
985 | "typealias integer {\n" | |
986 | " size = 32; align = %u; signed = false;\n" | |
fc0bb9fa | 987 | " map = clock.%s.value;\n" |
d0b96690 DG |
988 | "} := uint32_clock_monotonic_t;\n" |
989 | "\n" | |
990 | "typealias integer {\n" | |
991 | " size = 64; align = %u; signed = false;\n" | |
fc0bb9fa | 992 | " map = clock.%s.value;\n" |
d0b96690 | 993 | "} := uint64_clock_monotonic_t;\n\n", |
fc0bb9fa | 994 | trace_clock_name(), |
d0b96690 | 995 | session->uint32_t_alignment, |
fc0bb9fa MD |
996 | trace_clock_name(), |
997 | session->uint64_t_alignment, | |
998 | trace_clock_name() | |
d0b96690 DG |
999 | ); |
1000 | if (ret) | |
1001 | goto end; | |
1002 | ||
1003 | ret = _lttng_stream_packet_context_declare(session); | |
1004 | if (ret) | |
1005 | goto end; | |
1006 | ||
1007 | ret = _lttng_event_header_declare(session); | |
1008 | if (ret) | |
1009 | goto end; | |
1010 | ||
1011 | end: | |
1012 | return ret; | |
1013 | } |