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 | ||
5e0e07c0 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 | ||
5e0e07c0 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 | ||
e70076b1 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, | |
5e0e07c0 | 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; | |
e70076b1 | 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 | ||
5e0e07c0 MD |
247 | ret = print_tabs(session, nesting); |
248 | if (ret) { | |
249 | goto end; | |
250 | } | |
10b56aef | 251 | ret = lttng_metadata_printf(session, |
5e0e07c0 | 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 | ||
e70076b1 MD |
270 | ret = print_tabs(session, nesting); |
271 | if (ret) { | |
272 | goto end; | |
273 | } | |
10b56aef | 274 | ret = lttng_metadata_printf(session, |
e70076b1 | 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 | } | |
e70076b1 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); | |
5e0e07c0 MD |
327 | end: |
328 | (*iter_field)++; | |
329 | return ret; | |
330 | } | |
331 | ||
5e0e07c0 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; | |
e70076b1 | 340 | char identifier[LTTNG_UST_SYM_NAME_LEN]; |
5e0e07c0 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)++; | |
e70076b1 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 | } | |
5e0e07c0 | 353 | ret = lttng_metadata_printf(session, |
e70076b1 MD |
354 | "variant <_%s> {\n", |
355 | identifier); | |
5e0e07c0 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); | |
368 | } | |
e70076b1 MD |
369 | sanitize_ctf_identifier(identifier, variant->name); |
370 | ret = print_tabs(session, nesting); | |
5e0e07c0 | 371 | ret = lttng_metadata_printf(session, |
e70076b1 MD |
372 | "} _%s;\n", |
373 | identifier); | |
5e0e07c0 MD |
374 | if (ret) { |
375 | goto end; | |
376 | } | |
10b56aef MD |
377 | end: |
378 | return ret; | |
379 | } | |
380 | ||
d0b96690 DG |
381 | static |
382 | int _lttng_field_statedump(struct ust_registry_session *session, | |
5e0e07c0 MD |
383 | const struct ustctl_field *fields, size_t nr_fields, |
384 | size_t *iter_field, size_t nesting) | |
d0b96690 DG |
385 | { |
386 | int ret = 0; | |
387 | const char *bo_be = " byte_order = be;"; | |
388 | const char *bo_le = " byte_order = le;"; | |
389 | const char *bo_native = ""; | |
390 | const char *bo_reverse; | |
5e0e07c0 | 391 | const struct ustctl_field *field; |
d0b96690 | 392 | |
5e0e07c0 MD |
393 | if (*iter_field >= nr_fields) { |
394 | ret = -EOVERFLOW; | |
395 | goto end; | |
396 | } | |
397 | field = &fields[*iter_field]; | |
398 | ||
399 | if (session->byte_order == BIG_ENDIAN) { | |
d0b96690 | 400 | bo_reverse = bo_le; |
5e0e07c0 | 401 | } else { |
d0b96690 | 402 | bo_reverse = bo_be; |
5e0e07c0 | 403 | } |
d0b96690 DG |
404 | |
405 | switch (field->type.atype) { | |
406 | case ustctl_atype_integer: | |
5e0e07c0 MD |
407 | ret = print_tabs(session, nesting); |
408 | if (ret) { | |
409 | goto end; | |
410 | } | |
d0b96690 | 411 | ret = lttng_metadata_printf(session, |
5e0e07c0 | 412 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n", |
d0b96690 DG |
413 | field->type.u.basic.integer.size, |
414 | field->type.u.basic.integer.alignment, | |
415 | field->type.u.basic.integer.signedness, | |
416 | (field->type.u.basic.integer.encoding == ustctl_encode_none) | |
417 | ? "none" | |
418 | : (field->type.u.basic.integer.encoding == ustctl_encode_UTF8) | |
419 | ? "UTF8" | |
420 | : "ASCII", | |
421 | field->type.u.basic.integer.base, | |
422 | field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
423 | field->name); | |
5e0e07c0 | 424 | (*iter_field)++; |
d0b96690 | 425 | break; |
10b56aef MD |
426 | case ustctl_atype_enum: |
427 | ret = ust_metadata_enum_statedump(session, | |
428 | field->type.u.basic.enumeration.name, | |
429 | field->type.u.basic.enumeration.id, | |
430 | &field->type.u.basic.enumeration.container_type, | |
5e0e07c0 | 431 | field->name, iter_field, nesting); |
10b56aef | 432 | break; |
d0b96690 | 433 | case ustctl_atype_float: |
5e0e07c0 MD |
434 | ret = print_tabs(session, nesting); |
435 | if (ret) { | |
436 | goto end; | |
437 | } | |
d0b96690 | 438 | ret = lttng_metadata_printf(session, |
5e0e07c0 | 439 | "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n", |
d0b96690 DG |
440 | field->type.u.basic._float.exp_dig, |
441 | field->type.u.basic._float.mant_dig, | |
442 | field->type.u.basic._float.alignment, | |
443 | field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
444 | field->name); | |
5e0e07c0 | 445 | (*iter_field)++; |
d0b96690 | 446 | break; |
d0b96690 DG |
447 | case ustctl_atype_array: |
448 | { | |
449 | const struct ustctl_basic_type *elem_type; | |
450 | ||
5e0e07c0 MD |
451 | ret = print_tabs(session, nesting); |
452 | if (ret) { | |
453 | goto end; | |
454 | } | |
d0b96690 DG |
455 | elem_type = &field->type.u.array.elem_type; |
456 | ret = lttng_metadata_printf(session, | |
5e0e07c0 | 457 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n", |
d0b96690 DG |
458 | elem_type->u.basic.integer.size, |
459 | elem_type->u.basic.integer.alignment, | |
460 | elem_type->u.basic.integer.signedness, | |
461 | (elem_type->u.basic.integer.encoding == ustctl_encode_none) | |
462 | ? "none" | |
463 | : (elem_type->u.basic.integer.encoding == ustctl_encode_UTF8) | |
464 | ? "UTF8" | |
465 | : "ASCII", | |
466 | elem_type->u.basic.integer.base, | |
467 | elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
468 | field->name, field->type.u.array.length); | |
5e0e07c0 | 469 | (*iter_field)++; |
d0b96690 DG |
470 | break; |
471 | } | |
472 | case ustctl_atype_sequence: | |
473 | { | |
474 | const struct ustctl_basic_type *elem_type; | |
475 | const struct ustctl_basic_type *length_type; | |
476 | ||
477 | elem_type = &field->type.u.sequence.elem_type; | |
478 | length_type = &field->type.u.sequence.length_type; | |
5e0e07c0 MD |
479 | ret = print_tabs(session, nesting); |
480 | if (ret) { | |
481 | goto end; | |
482 | } | |
d0b96690 | 483 | ret = lttng_metadata_printf(session, |
5e0e07c0 | 484 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n", |
d0b96690 DG |
485 | length_type->u.basic.integer.size, |
486 | (unsigned int) length_type->u.basic.integer.alignment, | |
487 | length_type->u.basic.integer.signedness, | |
488 | (length_type->u.basic.integer.encoding == ustctl_encode_none) | |
489 | ? "none" | |
490 | : ((length_type->u.basic.integer.encoding == ustctl_encode_UTF8) | |
491 | ? "UTF8" | |
492 | : "ASCII"), | |
493 | length_type->u.basic.integer.base, | |
494 | length_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
495 | field->name); | |
5e0e07c0 MD |
496 | if (ret) { |
497 | goto end; | |
498 | } | |
d0b96690 | 499 | |
5e0e07c0 MD |
500 | ret = print_tabs(session, nesting); |
501 | if (ret) { | |
502 | goto end; | |
503 | } | |
d0b96690 | 504 | ret = lttng_metadata_printf(session, |
5e0e07c0 | 505 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n", |
d0b96690 DG |
506 | elem_type->u.basic.integer.size, |
507 | (unsigned int) elem_type->u.basic.integer.alignment, | |
508 | elem_type->u.basic.integer.signedness, | |
509 | (elem_type->u.basic.integer.encoding == ustctl_encode_none) | |
510 | ? "none" | |
511 | : ((elem_type->u.basic.integer.encoding == ustctl_encode_UTF8) | |
512 | ? "UTF8" | |
513 | : "ASCII"), | |
514 | elem_type->u.basic.integer.base, | |
515 | elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
516 | field->name, | |
517 | field->name); | |
5e0e07c0 | 518 | (*iter_field)++; |
d0b96690 DG |
519 | break; |
520 | } | |
521 | ||
522 | case ustctl_atype_string: | |
523 | /* Default encoding is UTF8 */ | |
5e0e07c0 MD |
524 | ret = print_tabs(session, nesting); |
525 | if (ret) { | |
526 | goto end; | |
527 | } | |
d0b96690 | 528 | ret = lttng_metadata_printf(session, |
5e0e07c0 | 529 | "string%s _%s;\n", |
d0b96690 DG |
530 | field->type.u.basic.string.encoding == ustctl_encode_ASCII ? |
531 | " { encoding = ASCII; }" : "", | |
532 | field->name); | |
5e0e07c0 MD |
533 | (*iter_field)++; |
534 | break; | |
535 | case ustctl_atype_variant: | |
536 | ret = _lttng_variant_statedump(session, fields, nr_fields, iter_field, nesting); | |
537 | if (ret) { | |
538 | goto end; | |
539 | } | |
540 | break; | |
541 | case ustctl_atype_struct: | |
542 | ret = print_tabs(session, nesting); | |
543 | if (ret) { | |
544 | goto end; | |
545 | } | |
546 | ret = lttng_metadata_printf(session, | |
547 | "struct {} _%s;\n", | |
548 | field->name); | |
549 | (*iter_field)++; | |
d0b96690 DG |
550 | break; |
551 | default: | |
5e0e07c0 | 552 | ret = -EINVAL; |
d0b96690 | 553 | } |
5e0e07c0 | 554 | end: |
d0b96690 DG |
555 | return ret; |
556 | } | |
557 | ||
558 | static | |
559 | int _lttng_context_metadata_statedump(struct ust_registry_session *session, | |
560 | size_t nr_ctx_fields, | |
561 | struct ustctl_field *ctx) | |
562 | { | |
563 | int ret = 0; | |
5e0e07c0 | 564 | size_t i = 0; |
d0b96690 DG |
565 | |
566 | if (!ctx) | |
567 | return 0; | |
5e0e07c0 MD |
568 | for (;;) { |
569 | if (i >= nr_ctx_fields) { | |
570 | break; | |
571 | } | |
572 | ret = _lttng_field_statedump(session, ctx, | |
573 | nr_ctx_fields, &i, 2); | |
574 | if (ret) { | |
575 | break; | |
576 | } | |
d0b96690 DG |
577 | } |
578 | return ret; | |
579 | } | |
580 | ||
581 | static | |
582 | int _lttng_fields_metadata_statedump(struct ust_registry_session *session, | |
583 | struct ust_registry_event *event) | |
584 | { | |
585 | int ret = 0; | |
5e0e07c0 | 586 | size_t i = 0; |
d0b96690 | 587 | |
5e0e07c0 MD |
588 | for (;;) { |
589 | if (i >= event->nr_fields) { | |
590 | break; | |
591 | } | |
592 | ret = _lttng_field_statedump(session, event->fields, | |
593 | event->nr_fields, &i, 2); | |
594 | if (ret) { | |
595 | break; | |
596 | } | |
d0b96690 DG |
597 | } |
598 | return ret; | |
599 | } | |
600 | ||
601 | /* | |
602 | * Should be called with session registry mutex held. | |
603 | */ | |
604 | int ust_metadata_event_statedump(struct ust_registry_session *session, | |
605 | struct ust_registry_channel *chan, | |
606 | struct ust_registry_event *event) | |
607 | { | |
608 | int ret = 0; | |
609 | ||
610 | /* Don't dump metadata events */ | |
611 | if (chan->chan_id == -1U) | |
612 | return 0; | |
613 | ||
614 | ret = lttng_metadata_printf(session, | |
615 | "event {\n" | |
616 | " name = \"%s\";\n" | |
617 | " id = %u;\n" | |
618 | " stream_id = %u;\n", | |
619 | event->name, | |
620 | event->id, | |
621 | chan->chan_id); | |
622 | if (ret) | |
623 | goto end; | |
624 | ||
625 | ret = lttng_metadata_printf(session, | |
626 | " loglevel = %d;\n", | |
2106efa0 | 627 | event->loglevel_value); |
d0b96690 DG |
628 | if (ret) |
629 | goto end; | |
630 | ||
631 | if (event->model_emf_uri) { | |
632 | ret = lttng_metadata_printf(session, | |
633 | " model.emf.uri = \"%s\";\n", | |
634 | event->model_emf_uri); | |
635 | if (ret) | |
636 | goto end; | |
637 | } | |
638 | ||
d0b96690 DG |
639 | ret = lttng_metadata_printf(session, |
640 | " fields := struct {\n" | |
641 | ); | |
642 | if (ret) | |
643 | goto end; | |
644 | ||
645 | ret = _lttng_fields_metadata_statedump(session, event); | |
646 | if (ret) | |
647 | goto end; | |
648 | ||
649 | ret = lttng_metadata_printf(session, | |
650 | " };\n" | |
651 | "};\n\n"); | |
652 | if (ret) | |
653 | goto end; | |
7972aab2 | 654 | event->metadata_dumped = 1; |
d0b96690 DG |
655 | |
656 | end: | |
657 | return ret; | |
658 | } | |
659 | ||
660 | /* | |
661 | * Should be called with session registry mutex held. | |
662 | */ | |
663 | int ust_metadata_channel_statedump(struct ust_registry_session *session, | |
664 | struct ust_registry_channel *chan) | |
665 | { | |
666 | int ret = 0; | |
667 | ||
668 | /* Don't dump metadata events */ | |
669 | if (chan->chan_id == -1U) | |
670 | return 0; | |
671 | ||
672 | if (!chan->header_type) | |
673 | return -EINVAL; | |
674 | ||
675 | ret = lttng_metadata_printf(session, | |
676 | "stream {\n" | |
677 | " id = %u;\n" | |
678 | " event.header := %s;\n" | |
679 | " packet.context := struct packet_context;\n", | |
680 | chan->chan_id, | |
681 | chan->header_type == USTCTL_CHANNEL_HEADER_COMPACT ? | |
682 | "struct event_header_compact" : | |
683 | "struct event_header_large"); | |
684 | if (ret) | |
685 | goto end; | |
686 | ||
687 | if (chan->ctx_fields) { | |
688 | ret = lttng_metadata_printf(session, | |
689 | " event.context := struct {\n"); | |
690 | if (ret) | |
691 | goto end; | |
692 | } | |
693 | ret = _lttng_context_metadata_statedump(session, | |
694 | chan->nr_ctx_fields, | |
695 | chan->ctx_fields); | |
696 | if (ret) | |
697 | goto end; | |
698 | if (chan->ctx_fields) { | |
699 | ret = lttng_metadata_printf(session, | |
700 | " };\n"); | |
701 | if (ret) | |
702 | goto end; | |
703 | } | |
704 | ||
705 | ret = lttng_metadata_printf(session, | |
706 | "};\n\n"); | |
7972aab2 DG |
707 | /* Flag success of metadata dump. */ |
708 | chan->metadata_dumped = 1; | |
d0b96690 DG |
709 | |
710 | end: | |
711 | return ret; | |
712 | } | |
713 | ||
714 | static | |
715 | int _lttng_stream_packet_context_declare(struct ust_registry_session *session) | |
716 | { | |
717 | return lttng_metadata_printf(session, | |
718 | "struct packet_context {\n" | |
719 | " uint64_clock_monotonic_t timestamp_begin;\n" | |
720 | " uint64_clock_monotonic_t timestamp_end;\n" | |
721 | " uint64_t content_size;\n" | |
722 | " uint64_t packet_size;\n" | |
723 | " unsigned long events_discarded;\n" | |
724 | " uint32_t cpu_id;\n" | |
725 | "};\n\n" | |
726 | ); | |
727 | } | |
728 | ||
729 | /* | |
730 | * Compact header: | |
731 | * id: range: 0 - 30. | |
732 | * id 31 is reserved to indicate an extended header. | |
733 | * | |
734 | * Large header: | |
735 | * id: range: 0 - 65534. | |
736 | * id 65535 is reserved to indicate an extended header. | |
737 | */ | |
738 | static | |
739 | int _lttng_event_header_declare(struct ust_registry_session *session) | |
740 | { | |
741 | return lttng_metadata_printf(session, | |
742 | "struct event_header_compact {\n" | |
743 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" | |
744 | " variant <id> {\n" | |
745 | " struct {\n" | |
746 | " uint27_clock_monotonic_t timestamp;\n" | |
747 | " } compact;\n" | |
748 | " struct {\n" | |
749 | " uint32_t id;\n" | |
750 | " uint64_clock_monotonic_t timestamp;\n" | |
751 | " } extended;\n" | |
752 | " } v;\n" | |
753 | "} align(%u);\n" | |
754 | "\n" | |
755 | "struct event_header_large {\n" | |
756 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" | |
757 | " variant <id> {\n" | |
758 | " struct {\n" | |
759 | " uint32_clock_monotonic_t timestamp;\n" | |
760 | " } compact;\n" | |
761 | " struct {\n" | |
762 | " uint32_t id;\n" | |
763 | " uint64_clock_monotonic_t timestamp;\n" | |
764 | " } extended;\n" | |
765 | " } v;\n" | |
766 | "} align(%u);\n\n", | |
767 | session->uint32_t_alignment, | |
768 | session->uint16_t_alignment | |
769 | ); | |
770 | } | |
771 | ||
d0b96690 | 772 | static |
8c645bb0 | 773 | int measure_single_clock_offset(struct offset_sample *sample) |
d0b96690 | 774 | { |
8c645bb0 | 775 | uint64_t offset, monotonic[2], measure_delta, realtime; |
fc0bb9fa | 776 | uint64_t tcf = trace_clock_freq(); |
d0b96690 DG |
777 | struct timespec rts = { 0, 0 }; |
778 | int ret; | |
779 | ||
780 | monotonic[0] = trace_clock_read64(); | |
781 | ret = clock_gettime(CLOCK_REALTIME, &rts); | |
8c645bb0 MD |
782 | if (ret < 0) { |
783 | return ret; | |
784 | } | |
d0b96690 | 785 | monotonic[1] = trace_clock_read64(); |
8c645bb0 MD |
786 | measure_delta = monotonic[1] - monotonic[0]; |
787 | if (measure_delta > sample->measure_delta) { | |
788 | /* | |
789 | * Discard value if it took longer to read than the best | |
790 | * sample so far. | |
791 | */ | |
792 | return 0; | |
793 | } | |
d0b96690 | 794 | offset = (monotonic[0] + monotonic[1]) >> 1; |
6e1b0543 MD |
795 | realtime = (uint64_t) rts.tv_sec * tcf; |
796 | if (tcf == NSEC_PER_SEC) { | |
797 | realtime += rts.tv_nsec; | |
798 | } else { | |
799 | realtime += (uint64_t) rts.tv_nsec * tcf / NSEC_PER_SEC; | |
fc0bb9fa | 800 | } |
d0b96690 | 801 | offset = realtime - offset; |
8c645bb0 MD |
802 | sample->offset = offset; |
803 | sample->measure_delta = measure_delta; | |
804 | return 0; | |
d0b96690 DG |
805 | } |
806 | ||
8c645bb0 MD |
807 | /* |
808 | * Approximation of NTP time of day to clock monotonic correlation, | |
809 | * taken at start of trace. Keep the measurement that took the less time | |
810 | * to complete, thus removing imprecision caused by preemption. | |
811 | */ | |
812 | static | |
813 | uint64_t measure_clock_offset(void) | |
814 | { | |
815 | int i; | |
816 | struct offset_sample offset_best_sample = { | |
817 | .offset = 0, | |
818 | .measure_delta = UINT64_MAX, | |
819 | }; | |
820 | ||
821 | for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) { | |
822 | if (measure_single_clock_offset(&offset_best_sample)) { | |
823 | return 0; | |
824 | } | |
825 | } | |
826 | return offset_best_sample.offset; | |
827 | } | |
d0b96690 DG |
828 | |
829 | /* | |
830 | * Should be called with session registry mutex held. | |
831 | */ | |
832 | int ust_metadata_session_statedump(struct ust_registry_session *session, | |
af6142cf MD |
833 | struct ust_app *app, |
834 | uint32_t major, | |
835 | uint32_t minor) | |
d0b96690 DG |
836 | { |
837 | unsigned char *uuid_c; | |
838 | char uuid_s[UUID_STR_LEN], | |
839 | clock_uuid_s[UUID_STR_LEN]; | |
840 | int ret = 0; | |
841 | char hostname[HOST_NAME_MAX]; | |
842 | ||
7972aab2 | 843 | assert(session); |
7972aab2 | 844 | |
d0b96690 DG |
845 | uuid_c = session->uuid; |
846 | ||
847 | snprintf(uuid_s, sizeof(uuid_s), | |
848 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", | |
849 | uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3], | |
850 | uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7], | |
851 | uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11], | |
852 | uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]); | |
853 | ||
d7ba1388 MD |
854 | /* For crash ABI */ |
855 | ret = lttng_metadata_printf(session, | |
856 | "/* CTF %u.%u */\n\n", | |
857 | CTF_SPEC_MAJOR, | |
858 | CTF_SPEC_MINOR); | |
859 | if (ret) { | |
860 | goto end; | |
861 | } | |
862 | ||
d0b96690 DG |
863 | ret = lttng_metadata_printf(session, |
864 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" | |
865 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" | |
866 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" | |
867 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" | |
868 | "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n" | |
869 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" | |
870 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" | |
871 | "\n" | |
872 | "trace {\n" | |
873 | " major = %u;\n" | |
874 | " minor = %u;\n" | |
875 | " uuid = \"%s\";\n" | |
876 | " byte_order = %s;\n" | |
877 | " packet.header := struct {\n" | |
878 | " uint32_t magic;\n" | |
879 | " uint8_t uuid[16];\n" | |
880 | " uint32_t stream_id;\n" | |
881 | " };\n" | |
882 | "};\n\n", | |
883 | session->uint8_t_alignment, | |
884 | session->uint16_t_alignment, | |
885 | session->uint32_t_alignment, | |
886 | session->uint64_t_alignment, | |
887 | session->bits_per_long, | |
888 | session->long_alignment, | |
889 | CTF_SPEC_MAJOR, | |
890 | CTF_SPEC_MINOR, | |
891 | uuid_s, | |
892 | session->byte_order == BIG_ENDIAN ? "be" : "le" | |
893 | ); | |
894 | if (ret) | |
895 | goto end; | |
896 | ||
897 | /* ignore error, just use empty string if error. */ | |
898 | hostname[0] = '\0'; | |
899 | ret = gethostname(hostname, sizeof(hostname)); | |
900 | if (ret && errno == ENAMETOOLONG) | |
901 | hostname[HOST_NAME_MAX - 1] = '\0'; | |
902 | ret = lttng_metadata_printf(session, | |
903 | "env {\n" | |
904 | " hostname = \"%s\";\n" | |
905 | " domain = \"ust\";\n" | |
906 | " tracer_name = \"lttng-ust\";\n" | |
907 | " tracer_major = %u;\n" | |
af6142cf | 908 | " tracer_minor = %u;\n", |
d0b96690 | 909 | hostname, |
af6142cf MD |
910 | major, |
911 | minor | |
d0b96690 DG |
912 | ); |
913 | if (ret) | |
914 | goto end; | |
915 | ||
916 | /* | |
917 | * If per-application registry, we can output extra information | |
918 | * about the application. | |
919 | */ | |
920 | if (app) { | |
921 | ret = lttng_metadata_printf(session, | |
af6142cf | 922 | " tracer_patchlevel = %u;\n" |
d0b96690 | 923 | " vpid = %d;\n" |
d88aee68 | 924 | " procname = \"%s\";\n", |
af6142cf | 925 | app->version.patchlevel, |
d0b96690 DG |
926 | (int) app->pid, |
927 | app->name | |
928 | ); | |
929 | if (ret) | |
930 | goto end; | |
931 | } | |
932 | ||
933 | ret = lttng_metadata_printf(session, | |
934 | "};\n\n" | |
935 | ); | |
936 | if (ret) | |
937 | goto end; | |
938 | ||
939 | ||
940 | ret = lttng_metadata_printf(session, | |
941 | "clock {\n" | |
fc0bb9fa MD |
942 | " name = \"%s\";\n", |
943 | trace_clock_name() | |
d0b96690 DG |
944 | ); |
945 | if (ret) | |
946 | goto end; | |
947 | ||
948 | if (!trace_clock_uuid(clock_uuid_s)) { | |
949 | ret = lttng_metadata_printf(session, | |
950 | " uuid = \"%s\";\n", | |
951 | clock_uuid_s | |
952 | ); | |
953 | if (ret) | |
954 | goto end; | |
955 | } | |
956 | ||
957 | ret = lttng_metadata_printf(session, | |
fc0bb9fa | 958 | " description = \"%s\";\n" |
d0b96690 DG |
959 | " freq = %" PRIu64 "; /* Frequency, in Hz */\n" |
960 | " /* clock value offset from Epoch is: offset * (1/freq) */\n" | |
961 | " offset = %" PRIu64 ";\n" | |
962 | "};\n\n", | |
fc0bb9fa | 963 | trace_clock_description(), |
d0b96690 DG |
964 | trace_clock_freq(), |
965 | measure_clock_offset() | |
966 | ); | |
967 | if (ret) | |
968 | goto end; | |
969 | ||
970 | ret = lttng_metadata_printf(session, | |
971 | "typealias integer {\n" | |
972 | " size = 27; align = 1; signed = false;\n" | |
fc0bb9fa | 973 | " map = clock.%s.value;\n" |
d0b96690 DG |
974 | "} := uint27_clock_monotonic_t;\n" |
975 | "\n" | |
976 | "typealias integer {\n" | |
977 | " size = 32; align = %u; signed = false;\n" | |
fc0bb9fa | 978 | " map = clock.%s.value;\n" |
d0b96690 DG |
979 | "} := uint32_clock_monotonic_t;\n" |
980 | "\n" | |
981 | "typealias integer {\n" | |
982 | " size = 64; align = %u; signed = false;\n" | |
fc0bb9fa | 983 | " map = clock.%s.value;\n" |
d0b96690 | 984 | "} := uint64_clock_monotonic_t;\n\n", |
fc0bb9fa | 985 | trace_clock_name(), |
d0b96690 | 986 | session->uint32_t_alignment, |
fc0bb9fa MD |
987 | trace_clock_name(), |
988 | session->uint64_t_alignment, | |
989 | trace_clock_name() | |
d0b96690 DG |
990 | ); |
991 | if (ret) | |
992 | goto end; | |
993 | ||
994 | ret = _lttng_stream_packet_context_declare(session); | |
995 | if (ret) | |
996 | goto end; | |
997 | ||
998 | ret = _lttng_event_header_declare(session); | |
999 | if (ret) | |
1000 | goto end; | |
1001 | ||
1002 | end: | |
1003 | return ret; | |
1004 | } |