Commit | Line | Data |
---|---|---|
273b65be JG |
1 | /* |
2 | * stream.c | |
3 | * | |
d2dc44b6 | 4 | * Babeltrace CTF IR - Stream |
273b65be | 5 | * |
de9dd397 | 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
7 | * |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
ac0c6bdd PP |
29 | #include <babeltrace/ctf-ir/clock-class.h> |
30 | #include <babeltrace/ctf-writer/clock.h> | |
31 | #include <babeltrace/ctf-writer/clock-internal.h> | |
273b65be | 32 | #include <babeltrace/ctf-writer/event.h> |
adc315b8 | 33 | #include <babeltrace/ctf-ir/event-internal.h> |
2e33ac5a PP |
34 | #include <babeltrace/ctf-ir/field-types-internal.h> |
35 | #include <babeltrace/ctf-ir/fields-internal.h> | |
3f043b05 JG |
36 | #include <babeltrace/ctf-ir/stream.h> |
37 | #include <babeltrace/ctf-ir/stream-internal.h> | |
adc315b8 | 38 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
5acf2ae6 | 39 | #include <babeltrace/ctf-ir/trace.h> |
319fd969 PP |
40 | #include <babeltrace/ctf-ir/trace-internal.h> |
41 | #include <babeltrace/ctf-writer/writer-internal.h> | |
3230ee6b | 42 | #include <babeltrace/graph/component-internal.h> |
83509119 | 43 | #include <babeltrace/ref.h> |
273b65be | 44 | #include <babeltrace/ctf-writer/functor-internal.h> |
3d9990ac PP |
45 | #include <babeltrace/compiler-internal.h> |
46 | #include <babeltrace/align-internal.h> | |
dc3fffef | 47 | #include <inttypes.h> |
273b65be JG |
48 | |
49 | static | |
83509119 | 50 | void bt_ctf_stream_destroy(struct bt_object *obj); |
273b65be | 51 | static |
af9296f3 JG |
52 | int try_set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t); |
53 | static | |
273b65be JG |
54 | int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t); |
55 | ||
ac0c6bdd PP |
56 | static |
57 | int set_integer_field_value(struct bt_ctf_field* field, uint64_t value) | |
58 | { | |
59 | int ret = 0; | |
60 | struct bt_ctf_field_type *field_type = NULL; | |
61 | ||
62 | if (!field) { | |
63 | ret = -1; | |
64 | goto end; | |
65 | } | |
66 | ||
67 | field_type = bt_ctf_field_get_type(field); | |
68 | assert(field_type); | |
69 | ||
70 | if (bt_ctf_field_type_get_type_id(field_type) != | |
1487a16a | 71 | BT_CTF_FIELD_TYPE_ID_INTEGER) { |
ac0c6bdd PP |
72 | /* Not an integer and the value is unset, error. */ |
73 | ret = -1; | |
74 | goto end; | |
75 | } | |
76 | ||
77 | if (bt_ctf_field_type_integer_get_signed(field_type)) { | |
78 | ret = bt_ctf_field_signed_integer_set_value(field, (int64_t) value); | |
79 | if (ret) { | |
80 | /* Value is out of range, error. */ | |
81 | goto end; | |
82 | } | |
83 | } else { | |
84 | ret = bt_ctf_field_unsigned_integer_set_value(field, value); | |
85 | if (ret) { | |
86 | /* Value is out of range, error. */ | |
87 | goto end; | |
88 | } | |
89 | } | |
90 | end: | |
91 | bt_put(field_type); | |
92 | return ret; | |
93 | } | |
94 | ||
d246b111 JG |
95 | static |
96 | int set_packet_header_magic(struct bt_ctf_stream *stream) | |
97 | { | |
98 | int ret = 0; | |
99 | struct bt_ctf_field_type *magic_field_type = NULL; | |
100 | struct bt_ctf_field *magic_field = bt_ctf_field_structure_get_field( | |
101 | stream->packet_header, "magic"); | |
102 | ||
103 | if (!magic_field) { | |
104 | /* No magic field found. Not an error, skip. */ | |
105 | goto end; | |
106 | } | |
107 | ||
fc47320a | 108 | if (bt_ctf_field_is_set(magic_field)) { |
d246b111 JG |
109 | /* Value already set. Not an error, skip. */ |
110 | goto end; | |
111 | } | |
112 | ||
113 | magic_field_type = bt_ctf_field_get_type(magic_field); | |
114 | assert(magic_field_type); | |
115 | ||
116 | if (bt_ctf_field_type_get_type_id(magic_field_type) != | |
1487a16a | 117 | BT_CTF_FIELD_TYPE_ID_INTEGER) { |
d246b111 JG |
118 | /* Magic field is not an integer. Not an error, skip. */ |
119 | goto end; | |
120 | } | |
121 | ||
122 | if (bt_ctf_field_type_integer_get_size(magic_field_type) != 32) { | |
123 | /* | |
124 | * Magic field is not of the expected size. | |
125 | * Not an error, skip. | |
126 | */ | |
127 | goto end; | |
128 | } | |
129 | ||
130 | ret = bt_ctf_field_type_integer_get_signed(magic_field_type); | |
131 | assert(ret >= 0); | |
132 | if (ret) { | |
133 | ret = bt_ctf_field_signed_integer_set_value(magic_field, | |
134 | (int64_t) 0xC1FC1FC1); | |
135 | } else { | |
136 | ret = bt_ctf_field_unsigned_integer_set_value(magic_field, | |
137 | (uint64_t) 0xC1FC1FC1); | |
138 | } | |
139 | end: | |
83509119 JG |
140 | bt_put(magic_field); |
141 | bt_put(magic_field_type); | |
d246b111 JG |
142 | return ret; |
143 | } | |
144 | ||
145 | static | |
146 | int set_packet_header_uuid(struct bt_ctf_stream *stream) | |
147 | { | |
148 | int i, ret = 0; | |
e6a8e8e4 | 149 | struct bt_ctf_trace *trace = NULL; |
d246b111 JG |
150 | struct bt_ctf_field_type *uuid_field_type = NULL; |
151 | struct bt_ctf_field_type *element_field_type = NULL; | |
152 | struct bt_ctf_field *uuid_field = bt_ctf_field_structure_get_field( | |
153 | stream->packet_header, "uuid"); | |
154 | ||
155 | if (!uuid_field) { | |
156 | /* No uuid field found. Not an error, skip. */ | |
157 | goto end; | |
158 | } | |
159 | ||
fc47320a | 160 | if (bt_ctf_field_is_set(uuid_field)) { |
d246b111 JG |
161 | /* Value already set. Not an error, skip. */ |
162 | goto end; | |
163 | } | |
164 | ||
165 | uuid_field_type = bt_ctf_field_get_type(uuid_field); | |
166 | assert(uuid_field_type); | |
167 | if (bt_ctf_field_type_get_type_id(uuid_field_type) != | |
1487a16a | 168 | BT_CTF_FIELD_TYPE_ID_ARRAY) { |
d246b111 JG |
169 | /* UUID field is not an array. Not an error, skip. */ |
170 | goto end; | |
171 | } | |
172 | ||
173 | if (bt_ctf_field_type_array_get_length(uuid_field_type) != 16) { | |
174 | /* | |
175 | * UUID field is not of the expected size. | |
176 | * Not an error, skip. | |
177 | */ | |
178 | goto end; | |
179 | } | |
180 | ||
181 | element_field_type = bt_ctf_field_type_array_get_element_type( | |
182 | uuid_field_type); | |
183 | assert(element_field_type); | |
184 | if (bt_ctf_field_type_get_type_id(element_field_type) != | |
1487a16a | 185 | BT_CTF_FIELD_TYPE_ID_INTEGER) { |
d246b111 JG |
186 | /* UUID array elements are not integers. Not an error, skip */ |
187 | goto end; | |
188 | } | |
189 | ||
e6a8e8e4 | 190 | trace = (struct bt_ctf_trace *) bt_object_get_parent(stream); |
d246b111 JG |
191 | for (i = 0; i < 16; i++) { |
192 | struct bt_ctf_field *uuid_element = | |
193 | bt_ctf_field_array_get_field(uuid_field, i); | |
194 | ||
195 | ret = bt_ctf_field_type_integer_get_signed(element_field_type); | |
196 | assert(ret >= 0); | |
197 | ||
198 | if (ret) { | |
199 | ret = bt_ctf_field_signed_integer_set_value( | |
e6a8e8e4 | 200 | uuid_element, (int64_t) trace->uuid[i]); |
d246b111 JG |
201 | } else { |
202 | ret = bt_ctf_field_unsigned_integer_set_value( | |
203 | uuid_element, | |
e6a8e8e4 | 204 | (uint64_t) trace->uuid[i]); |
d246b111 | 205 | } |
83509119 | 206 | bt_put(uuid_element); |
d246b111 JG |
207 | if (ret) { |
208 | goto end; | |
209 | } | |
210 | } | |
211 | ||
212 | end: | |
83509119 JG |
213 | bt_put(uuid_field); |
214 | bt_put(uuid_field_type); | |
215 | bt_put(element_field_type); | |
e6a8e8e4 | 216 | BT_PUT(trace); |
d246b111 JG |
217 | return ret; |
218 | } | |
219 | static | |
220 | int set_packet_header_stream_id(struct bt_ctf_stream *stream) | |
221 | { | |
222 | int ret = 0; | |
223 | uint32_t stream_id; | |
224 | struct bt_ctf_field_type *stream_id_field_type = NULL; | |
225 | struct bt_ctf_field *stream_id_field = bt_ctf_field_structure_get_field( | |
226 | stream->packet_header, "stream_id"); | |
227 | ||
228 | if (!stream_id_field) { | |
229 | /* No stream_id field found. Not an error, skip. */ | |
230 | goto end; | |
231 | } | |
232 | ||
fc47320a | 233 | if (bt_ctf_field_is_set(stream_id_field)) { |
d246b111 JG |
234 | /* Value already set. Not an error, skip. */ |
235 | goto end; | |
236 | } | |
237 | ||
238 | stream_id_field_type = bt_ctf_field_get_type(stream_id_field); | |
239 | assert(stream_id_field_type); | |
240 | if (bt_ctf_field_type_get_type_id(stream_id_field_type) != | |
1487a16a | 241 | BT_CTF_FIELD_TYPE_ID_INTEGER) { |
d246b111 JG |
242 | /* stream_id field is not an integer. Not an error, skip. */ |
243 | goto end; | |
244 | } | |
245 | ||
246 | stream_id = stream->stream_class->id; | |
247 | ret = bt_ctf_field_type_integer_get_signed(stream_id_field_type); | |
248 | assert(ret >= 0); | |
249 | if (ret) { | |
250 | ret = bt_ctf_field_signed_integer_set_value(stream_id_field, | |
251 | (int64_t) stream_id); | |
252 | } else { | |
253 | ret = bt_ctf_field_unsigned_integer_set_value(stream_id_field, | |
254 | (uint64_t) stream_id); | |
255 | } | |
256 | end: | |
83509119 JG |
257 | bt_put(stream_id_field); |
258 | bt_put(stream_id_field_type); | |
d246b111 JG |
259 | return ret; |
260 | } | |
261 | ||
262 | static | |
263 | int set_packet_header(struct bt_ctf_stream *stream) | |
264 | { | |
265 | int ret; | |
266 | ||
267 | ret = set_packet_header_magic(stream); | |
268 | if (ret) { | |
269 | goto end; | |
270 | } | |
271 | ||
272 | ret = set_packet_header_uuid(stream); | |
273 | if (ret) { | |
274 | goto end; | |
275 | } | |
276 | ||
277 | ret = set_packet_header_stream_id(stream); | |
278 | if (ret) { | |
279 | goto end; | |
280 | } | |
281 | end: | |
282 | return ret; | |
283 | } | |
284 | ||
123fbdec | 285 | static |
e6a8e8e4 | 286 | void release_event(struct bt_ctf_event *event) |
123fbdec | 287 | { |
e6a8e8e4 JG |
288 | if (bt_object_get_ref_count(event)) { |
289 | /* | |
290 | * The event is being orphaned, but it must guarantee the | |
291 | * existence of its event class for the duration of its | |
292 | * lifetime. | |
293 | */ | |
294 | bt_get(event->event_class); | |
295 | BT_PUT(event->base.parent); | |
296 | } else { | |
297 | bt_object_release(event); | |
298 | } | |
123fbdec JG |
299 | } |
300 | ||
319fd969 PP |
301 | static |
302 | int create_stream_file(struct bt_ctf_writer *writer, | |
303 | struct bt_ctf_stream *stream) | |
304 | { | |
305 | int fd; | |
306 | GString *filename = g_string_new(stream->stream_class->name->str); | |
307 | ||
308 | if (stream->stream_class->name->len == 0) { | |
309 | int64_t ret; | |
310 | ||
311 | ret = bt_ctf_stream_class_get_id(stream->stream_class); | |
312 | if (ret < 0) { | |
313 | fd = -1; | |
314 | goto error; | |
315 | } | |
316 | ||
317 | g_string_printf(filename, "stream_%" PRId64, ret); | |
318 | } | |
319 | ||
320 | g_string_append_printf(filename, "_%" PRIu32, stream->id); | |
321 | fd = openat(writer->trace_dir_fd, filename->str, | |
322 | O_RDWR | O_CREAT | O_TRUNC, | |
323 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
324 | error: | |
325 | g_string_free(filename, TRUE); | |
326 | return fd; | |
327 | } | |
328 | ||
329 | static | |
330 | int set_stream_fd(struct bt_ctf_stream *stream, int fd) | |
331 | { | |
332 | int ret = 0; | |
333 | ||
334 | if (stream->pos.fd != -1) { | |
335 | ret = -1; | |
336 | goto end; | |
337 | } | |
338 | ||
dc3fffef | 339 | (void) bt_ctf_stream_pos_init(&stream->pos, fd, O_RDWR); |
319fd969 PP |
340 | stream->pos.fd = fd; |
341 | end: | |
342 | return ret; | |
343 | } | |
344 | ||
3230ee6b PP |
345 | static |
346 | void component_destroy_listener(struct bt_component *component, void *data) | |
347 | { | |
348 | struct bt_ctf_stream *stream = data; | |
349 | ||
350 | g_hash_table_remove(stream->comp_cur_port, component); | |
351 | } | |
352 | ||
273b65be | 353 | struct bt_ctf_stream *bt_ctf_stream_create( |
b71d7298 PP |
354 | struct bt_ctf_stream_class *stream_class, |
355 | const char *name) | |
273b65be | 356 | { |
12c8a1a3 | 357 | int ret; |
273b65be | 358 | struct bt_ctf_stream *stream = NULL; |
319fd969 PP |
359 | struct bt_ctf_trace *trace = NULL; |
360 | struct bt_ctf_writer *writer = NULL; | |
273b65be | 361 | |
319fd969 | 362 | if (!stream_class) { |
b71d7298 | 363 | goto error; |
319fd969 PP |
364 | } |
365 | ||
366 | trace = bt_ctf_stream_class_get_trace(stream_class); | |
367 | if (!trace) { | |
b71d7298 | 368 | goto error; |
273b65be JG |
369 | } |
370 | ||
5acf2ae6 PP |
371 | if (bt_ctf_trace_is_static(trace)) { |
372 | /* | |
373 | * A static trace has the property that all its stream | |
374 | * classes, clock classes, and streams are definitive: | |
375 | * no more can be added, and each object is also frozen. | |
376 | */ | |
377 | goto error; | |
378 | } | |
379 | ||
273b65be JG |
380 | stream = g_new0(struct bt_ctf_stream, 1); |
381 | if (!stream) { | |
b71d7298 | 382 | goto error; |
273b65be JG |
383 | } |
384 | ||
83509119 | 385 | bt_object_init(stream, bt_ctf_stream_destroy); |
e6a8e8e4 JG |
386 | /* |
387 | * Acquire reference to parent since stream will become publicly | |
388 | * reachable; it needs its parent to remain valid. | |
389 | */ | |
390 | bt_object_set_parent(stream, trace); | |
273b65be JG |
391 | stream->id = stream_class->next_stream_id++; |
392 | stream->stream_class = stream_class; | |
319fd969 | 393 | stream->pos.fd = -1; |
d246b111 | 394 | |
3230ee6b PP |
395 | stream->destroy_listeners = g_array_new(FALSE, TRUE, |
396 | sizeof(struct bt_ctf_stream_destroy_listener)); | |
397 | if (!stream->destroy_listeners) { | |
398 | goto error; | |
399 | } | |
400 | ||
b71d7298 PP |
401 | if (name) { |
402 | stream->name = g_string_new(name); | |
403 | if (!stream->name) { | |
404 | goto error; | |
405 | } | |
406 | } | |
407 | ||
319fd969 PP |
408 | if (trace->is_created_by_writer) { |
409 | int fd; | |
410 | writer = (struct bt_ctf_writer *) | |
411 | bt_object_get_parent(trace); | |
412 | ||
413 | assert(writer); | |
98edd02c JG |
414 | if (stream_class->packet_context_type) { |
415 | stream->packet_context = bt_ctf_field_create( | |
416 | stream_class->packet_context_type); | |
417 | if (!stream->packet_context) { | |
418 | goto error; | |
419 | } | |
319fd969 | 420 | |
98edd02c | 421 | /* Initialize events_discarded */ |
af9296f3 JG |
422 | ret = try_set_structure_field_integer( |
423 | stream->packet_context, | |
98edd02c | 424 | "events_discarded", 0); |
af9296f3 | 425 | if (ret != 1) { |
98edd02c JG |
426 | goto error; |
427 | } | |
319fd969 PP |
428 | } |
429 | ||
430 | stream->events = g_ptr_array_new_with_free_func( | |
431 | (GDestroyNotify) release_event); | |
432 | if (!stream->events) { | |
433 | goto error; | |
434 | } | |
435 | ||
436 | /* A trace is not allowed to have a NULL packet header */ | |
437 | assert(trace->packet_header_type); | |
438 | stream->packet_header = | |
439 | bt_ctf_field_create(trace->packet_header_type); | |
440 | if (!stream->packet_header) { | |
441 | goto error; | |
442 | } | |
443 | ||
444 | /* | |
445 | * Attempt to populate the default trace packet header fields | |
446 | * (magic, uuid and stream_id). This will _not_ fail shall the | |
447 | * fields not be found or be of an incompatible type; they will | |
448 | * simply not be populated automatically. The user will have to | |
449 | * make sure to set the trace packet header fields himself | |
450 | * before flushing. | |
451 | */ | |
452 | ret = set_packet_header(stream); | |
453 | if (ret) { | |
454 | goto error; | |
455 | } | |
456 | ||
457 | /* Create file associated with this stream */ | |
458 | fd = create_stream_file(writer, stream); | |
459 | if (fd < 0) { | |
460 | goto error; | |
461 | } | |
462 | ||
463 | ret = set_stream_fd(stream, fd); | |
464 | if (ret) { | |
465 | goto error; | |
466 | } | |
467 | ||
468 | /* Freeze the writer */ | |
469 | bt_ctf_writer_freeze(writer); | |
470 | } else { | |
471 | /* Non-writer stream indicated by a negative FD */ | |
472 | ret = set_stream_fd(stream, -1); | |
473 | if (ret) { | |
474 | goto error; | |
475 | } | |
3230ee6b PP |
476 | |
477 | stream->comp_cur_port = g_hash_table_new(g_direct_hash, | |
478 | g_direct_equal); | |
479 | if (!stream->comp_cur_port) { | |
480 | goto error; | |
481 | } | |
d246b111 | 482 | } |
319fd969 PP |
483 | |
484 | /* Add this stream to the trace's streams */ | |
485 | g_ptr_array_add(trace->streams, stream); | |
486 | ||
319fd969 PP |
487 | BT_PUT(trace); |
488 | BT_PUT(writer); | |
273b65be | 489 | return stream; |
83509119 JG |
490 | error: |
491 | BT_PUT(stream); | |
319fd969 PP |
492 | BT_PUT(trace); |
493 | BT_PUT(writer); | |
83509119 | 494 | return stream; |
273b65be JG |
495 | } |
496 | ||
3baf0856 JG |
497 | struct bt_ctf_stream_class *bt_ctf_stream_get_class( |
498 | struct bt_ctf_stream *stream) | |
499 | { | |
500 | struct bt_ctf_stream_class *stream_class = NULL; | |
501 | ||
502 | if (!stream) { | |
503 | goto end; | |
504 | } | |
505 | ||
506 | stream_class = stream->stream_class; | |
83509119 | 507 | bt_get(stream_class); |
3baf0856 JG |
508 | end: |
509 | return stream_class; | |
510 | } | |
511 | ||
a78a2e25 JG |
512 | int bt_ctf_stream_get_discarded_events_count( |
513 | struct bt_ctf_stream *stream, uint64_t *count) | |
514 | { | |
515 | int64_t ret = 0; | |
12c8a1a3 JG |
516 | int field_signed; |
517 | struct bt_ctf_field *events_discarded_field = NULL; | |
518 | struct bt_ctf_field_type *events_discarded_field_type = NULL; | |
a78a2e25 | 519 | |
1579cde5 PP |
520 | if (!stream || !count || !stream->packet_context || |
521 | stream->pos.fd < 0) { | |
a78a2e25 JG |
522 | ret = -1; |
523 | goto end; | |
524 | } | |
525 | ||
12c8a1a3 JG |
526 | events_discarded_field = bt_ctf_field_structure_get_field( |
527 | stream->packet_context, "events_discarded"); | |
528 | if (!events_discarded_field) { | |
529 | ret = -1; | |
530 | goto end; | |
531 | } | |
532 | ||
533 | events_discarded_field_type = bt_ctf_field_get_type( | |
534 | events_discarded_field); | |
535 | if (!events_discarded_field_type) { | |
536 | ret = -1; | |
537 | goto end; | |
538 | } | |
539 | ||
540 | field_signed = bt_ctf_field_type_integer_get_signed( | |
541 | events_discarded_field_type); | |
542 | if (field_signed < 0) { | |
543 | ret = field_signed; | |
544 | goto end; | |
545 | } | |
546 | ||
547 | if (field_signed) { | |
548 | int64_t signed_count; | |
549 | ||
550 | ret = bt_ctf_field_signed_integer_get_value( | |
551 | events_discarded_field, &signed_count); | |
552 | if (ret) { | |
553 | goto end; | |
554 | } | |
555 | if (signed_count < 0) { | |
556 | /* Invalid value */ | |
557 | ret = -1; | |
558 | goto end; | |
559 | } | |
560 | *count = (uint64_t) signed_count; | |
561 | } else { | |
562 | ret = bt_ctf_field_unsigned_integer_get_value( | |
563 | events_discarded_field, count); | |
564 | if (ret) { | |
565 | goto end; | |
566 | } | |
567 | } | |
a78a2e25 | 568 | end: |
83509119 JG |
569 | bt_put(events_discarded_field); |
570 | bt_put(events_discarded_field_type); | |
a78a2e25 JG |
571 | return ret; |
572 | } | |
573 | ||
273b65be JG |
574 | void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, |
575 | uint64_t event_count) | |
576 | { | |
12c8a1a3 JG |
577 | int ret; |
578 | int field_signed; | |
579 | uint64_t previous_count; | |
580 | uint64_t new_count; | |
581 | struct bt_ctf_field *events_discarded_field = NULL; | |
582 | struct bt_ctf_field_type *events_discarded_field_type = NULL; | |
583 | ||
1579cde5 | 584 | if (!stream || !stream->packet_context || stream->pos.fd < 0) { |
12c8a1a3 JG |
585 | goto end; |
586 | } | |
587 | ||
588 | ret = bt_ctf_stream_get_discarded_events_count(stream, | |
589 | &previous_count); | |
590 | if (ret) { | |
591 | goto end; | |
592 | } | |
593 | ||
594 | events_discarded_field = bt_ctf_field_structure_get_field( | |
595 | stream->packet_context, "events_discarded"); | |
596 | if (!events_discarded_field) { | |
597 | goto end; | |
598 | } | |
599 | ||
600 | events_discarded_field_type = bt_ctf_field_get_type( | |
601 | events_discarded_field); | |
602 | if (!events_discarded_field_type) { | |
603 | goto end; | |
604 | } | |
605 | ||
606 | field_signed = bt_ctf_field_type_integer_get_signed( | |
607 | events_discarded_field_type); | |
608 | if (field_signed < 0) { | |
609 | goto end; | |
273b65be JG |
610 | } |
611 | ||
12c8a1a3 JG |
612 | new_count = previous_count + event_count; |
613 | if (field_signed) { | |
614 | ret = bt_ctf_field_signed_integer_set_value( | |
615 | events_discarded_field, (int64_t) new_count); | |
616 | if (ret) { | |
617 | goto end; | |
618 | } | |
619 | } else { | |
620 | ret = bt_ctf_field_unsigned_integer_set_value( | |
621 | events_discarded_field, new_count); | |
622 | if (ret) { | |
623 | goto end; | |
624 | } | |
625 | } | |
626 | ||
627 | end: | |
83509119 JG |
628 | bt_put(events_discarded_field); |
629 | bt_put(events_discarded_field_type); | |
273b65be JG |
630 | } |
631 | ||
ac0c6bdd PP |
632 | static int auto_populate_event_header(struct bt_ctf_stream *stream, |
633 | struct bt_ctf_event *event) | |
634 | { | |
635 | int ret = 0; | |
636 | struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL; | |
637 | struct bt_ctf_clock_class *mapped_clock_class = NULL; | |
638 | ||
639 | if (!event || event->frozen) { | |
640 | ret = -1; | |
641 | goto end; | |
642 | } | |
643 | ||
644 | /* | |
645 | * The condition to automatically set the ID are: | |
646 | * | |
647 | * 1. The event header field "id" exists and is an integer | |
648 | * field. | |
649 | * 2. The event header field "id" is NOT set. | |
650 | */ | |
651 | id_field = bt_ctf_field_structure_get_field(event->event_header, "id"); | |
652 | if (id_field && !bt_ctf_field_is_set(id_field)) { | |
653 | ret = set_integer_field_value(id_field, | |
654 | (uint64_t) bt_ctf_event_class_get_id( | |
655 | event->event_class)); | |
656 | if (ret) { | |
657 | goto end; | |
658 | } | |
659 | } | |
660 | ||
661 | /* | |
662 | * The conditions to automatically set the timestamp are: | |
663 | * | |
664 | * 1. The event header field "timestamp" exists and is an | |
665 | * integer field. | |
666 | * 2. This stream's class has a registered clock (set with | |
667 | * bt_ctf_stream_class_set_clock()). | |
668 | * 3. The event header field "timestamp" has its type mapped to | |
669 | * a clock class which is also the clock class of this | |
670 | * stream's class's registered clock. | |
671 | * 4. The event header field "timestamp" is NOT set. | |
672 | */ | |
673 | timestamp_field = bt_ctf_field_structure_get_field(event->event_header, | |
674 | "timestamp"); | |
675 | if (timestamp_field && !bt_ctf_field_is_set(timestamp_field) && | |
676 | stream->stream_class->clock) { | |
677 | struct bt_ctf_clock_class *stream_class_clock_class = | |
678 | stream->stream_class->clock->clock_class; | |
679 | struct bt_ctf_field_type *timestamp_field_type = | |
680 | bt_ctf_field_get_type(timestamp_field); | |
681 | ||
682 | assert(timestamp_field_type); | |
683 | mapped_clock_class = | |
684 | bt_ctf_field_type_integer_get_mapped_clock_class( | |
685 | timestamp_field_type); | |
686 | BT_PUT(timestamp_field_type); | |
687 | if (mapped_clock_class == stream_class_clock_class) { | |
688 | uint64_t timestamp; | |
689 | ||
690 | ret = bt_ctf_clock_get_value( | |
691 | stream->stream_class->clock, | |
692 | ×tamp); | |
693 | if (ret) { | |
694 | goto end; | |
695 | } | |
696 | ||
697 | ret = set_integer_field_value(timestamp_field, | |
698 | timestamp); | |
699 | if (ret) { | |
700 | goto end; | |
701 | } | |
702 | } | |
703 | } | |
704 | ||
705 | end: | |
706 | bt_put(id_field); | |
707 | bt_put(timestamp_field); | |
708 | bt_put(mapped_clock_class); | |
709 | return ret; | |
710 | } | |
711 | ||
273b65be JG |
712 | int bt_ctf_stream_append_event(struct bt_ctf_stream *stream, |
713 | struct bt_ctf_event *event) | |
714 | { | |
715 | int ret = 0; | |
273b65be | 716 | |
1579cde5 | 717 | if (!stream || !event || stream->pos.fd < 0) { |
273b65be JG |
718 | ret = -1; |
719 | goto end; | |
720 | } | |
721 | ||
fa29ba83 PP |
722 | /* |
723 | * The event is not supposed to have a parent stream at this | |
724 | * point. The only other way an event can have a parent stream | |
725 | * is if it was assigned when setting a packet to the event, | |
726 | * in which case the packet's stream is not a writer stream, | |
727 | * and thus the user is trying to append an event which belongs | |
728 | * to another stream. | |
729 | */ | |
730 | if (event->base.parent) { | |
731 | ret = -1; | |
732 | goto end; | |
733 | } | |
734 | ||
e6a8e8e4 | 735 | bt_object_set_parent(event, stream); |
ac0c6bdd | 736 | ret = auto_populate_event_header(stream, event); |
662e778c | 737 | if (ret) { |
37f30168 | 738 | goto error; |
662e778c JG |
739 | } |
740 | ||
5fd2e9fd | 741 | /* Make sure the various scopes of the event are set */ |
273b65be JG |
742 | ret = bt_ctf_event_validate(event); |
743 | if (ret) { | |
37f30168 | 744 | goto error; |
273b65be JG |
745 | } |
746 | ||
0d688c15 PP |
747 | /* Save the new event and freeze it */ |
748 | bt_ctf_event_freeze(event); | |
273b65be | 749 | g_ptr_array_add(stream->events, event); |
5fd2e9fd | 750 | |
e6a8e8e4 JG |
751 | /* |
752 | * Event had to hold a reference to its event class as long as it wasn't | |
753 | * part of the same trace hierarchy. From now on, the event and its | |
754 | * class share the same lifetime guarantees and the reference is no | |
755 | * longer needed. | |
756 | */ | |
757 | bt_put(event->event_class); | |
37f30168 | 758 | |
273b65be | 759 | end: |
37f30168 PP |
760 | return ret; |
761 | ||
762 | error: | |
763 | /* | |
764 | * Orphan the event; we were not successful in associating it to | |
765 | * a stream. | |
766 | */ | |
767 | bt_object_set_parent(event, NULL); | |
768 | ||
273b65be JG |
769 | return ret; |
770 | } | |
771 | ||
12c8a1a3 JG |
772 | struct bt_ctf_field *bt_ctf_stream_get_packet_context( |
773 | struct bt_ctf_stream *stream) | |
774 | { | |
775 | struct bt_ctf_field *packet_context = NULL; | |
776 | ||
1579cde5 | 777 | if (!stream || stream->pos.fd < 0) { |
12c8a1a3 JG |
778 | goto end; |
779 | } | |
780 | ||
781 | packet_context = stream->packet_context; | |
12c8a1a3 | 782 | if (packet_context) { |
83509119 | 783 | bt_get(packet_context); |
12c8a1a3 | 784 | } |
34629a55 | 785 | end: |
12c8a1a3 JG |
786 | return packet_context; |
787 | } | |
788 | ||
789 | int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream, | |
790 | struct bt_ctf_field *field) | |
791 | { | |
792 | int ret = 0; | |
793 | struct bt_ctf_field_type *field_type; | |
794 | ||
835b2d10 | 795 | if (!stream || stream->pos.fd < 0) { |
12c8a1a3 JG |
796 | ret = -1; |
797 | goto end; | |
798 | } | |
799 | ||
800 | field_type = bt_ctf_field_get_type(field); | |
09840de5 PP |
801 | if (bt_ctf_field_type_compare(field_type, |
802 | stream->stream_class->packet_context_type)) { | |
12c8a1a3 JG |
803 | ret = -1; |
804 | goto end; | |
805 | } | |
806 | ||
83509119 | 807 | bt_put(field_type); |
83509119 | 808 | bt_put(stream->packet_context); |
835b2d10 | 809 | stream->packet_context = bt_get(field); |
12c8a1a3 JG |
810 | end: |
811 | return ret; | |
812 | } | |
813 | ||
263a7df5 JG |
814 | struct bt_ctf_field *bt_ctf_stream_get_packet_header( |
815 | struct bt_ctf_stream *stream) | |
816 | { | |
817 | struct bt_ctf_field *packet_header = NULL; | |
818 | ||
1579cde5 | 819 | if (!stream || stream->pos.fd < 0) { |
263a7df5 JG |
820 | goto end; |
821 | } | |
822 | ||
823 | packet_header = stream->packet_header; | |
824 | if (packet_header) { | |
83509119 | 825 | bt_get(packet_header); |
263a7df5 JG |
826 | } |
827 | end: | |
828 | return packet_header; | |
829 | } | |
830 | ||
831 | int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream, | |
832 | struct bt_ctf_field *field) | |
833 | { | |
834 | int ret = 0; | |
e6a8e8e4 | 835 | struct bt_ctf_trace *trace = NULL; |
263a7df5 JG |
836 | struct bt_ctf_field_type *field_type = NULL; |
837 | ||
835b2d10 | 838 | if (!stream || stream->pos.fd < 0) { |
263a7df5 JG |
839 | ret = -1; |
840 | goto end; | |
841 | } | |
842 | ||
e6a8e8e4 | 843 | trace = (struct bt_ctf_trace *) bt_object_get_parent(stream); |
263a7df5 | 844 | field_type = bt_ctf_field_get_type(field); |
09840de5 | 845 | if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) { |
263a7df5 JG |
846 | ret = -1; |
847 | goto end; | |
848 | } | |
849 | ||
83509119 | 850 | bt_put(stream->packet_header); |
835b2d10 | 851 | stream->packet_header = bt_get(field); |
263a7df5 | 852 | end: |
e6a8e8e4 | 853 | BT_PUT(trace); |
83509119 | 854 | bt_put(field_type); |
263a7df5 JG |
855 | return ret; |
856 | } | |
857 | ||
9a220c32 JG |
858 | static |
859 | int get_event_header_timestamp(struct bt_ctf_field *event_header, uint64_t *timestamp) | |
860 | { | |
861 | int ret = 0; | |
862 | struct bt_ctf_field *timestamp_field = NULL; | |
863 | struct bt_ctf_field_type *timestamp_field_type = NULL; | |
864 | ||
865 | timestamp_field = bt_ctf_field_structure_get_field(event_header, | |
866 | "timestamp"); | |
867 | if (!timestamp_field) { | |
868 | ret = -1; | |
869 | goto end; | |
870 | } | |
871 | ||
872 | timestamp_field_type = bt_ctf_field_get_type(timestamp_field); | |
873 | assert(timestamp_field_type); | |
874 | if (bt_ctf_field_type_get_type_id(timestamp_field_type) != | |
1487a16a | 875 | BT_CTF_FIELD_TYPE_ID_INTEGER) { |
9a220c32 JG |
876 | ret = -1; |
877 | goto end; | |
878 | } | |
879 | ||
880 | if (bt_ctf_field_type_integer_get_signed(timestamp_field_type)) { | |
881 | int64_t val; | |
882 | ||
883 | ret = bt_ctf_field_signed_integer_get_value(timestamp_field, | |
884 | &val); | |
885 | if (ret) { | |
886 | goto end; | |
887 | } | |
888 | *timestamp = (uint64_t) val; | |
889 | } else { | |
890 | ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field, | |
891 | timestamp); | |
892 | if (ret) { | |
893 | goto end; | |
894 | } | |
895 | } | |
896 | end: | |
83509119 JG |
897 | bt_put(timestamp_field); |
898 | bt_put(timestamp_field_type); | |
9a220c32 JG |
899 | return ret; |
900 | } | |
901 | ||
c9af50d1 JG |
902 | static |
903 | void reset_structure_field(struct bt_ctf_field *structure, const char *name) | |
904 | { | |
905 | struct bt_ctf_field *member; | |
906 | ||
907 | member = bt_ctf_field_structure_get_field(structure, name); | |
908 | assert(member); | |
909 | (void) bt_ctf_field_reset(member); | |
e027ff8c | 910 | bt_put(member); |
c9af50d1 JG |
911 | } |
912 | ||
273b65be JG |
913 | int bt_ctf_stream_flush(struct bt_ctf_stream *stream) |
914 | { | |
915 | int ret = 0; | |
916 | size_t i; | |
c9af50d1 | 917 | uint64_t timestamp_begin, timestamp_end; |
273b65be | 918 | struct bt_ctf_field *integer = NULL; |
dc3fffef PP |
919 | struct bt_ctf_stream_pos packet_context_pos; |
920 | struct bt_ctf_trace *trace; | |
921 | enum bt_ctf_byte_order native_byte_order; | |
98edd02c | 922 | bool empty_packet; |
0686ef94 | 923 | uint64_t packet_size_bits; |
c9af50d1 JG |
924 | struct { |
925 | bool timestamp_begin; | |
926 | bool timestamp_end; | |
927 | bool content_size; | |
928 | bool packet_size; | |
929 | } auto_set_fields = { 0 }; | |
273b65be | 930 | |
bc37ae52 JG |
931 | if (!stream || stream->pos.fd < 0) { |
932 | /* | |
933 | * Stream does not have an associated fd. It is, | |
934 | * therefore, not a stream being used to write events. | |
935 | */ | |
273b65be JG |
936 | ret = -1; |
937 | goto end; | |
938 | } | |
939 | ||
664f50c8 JG |
940 | if (!stream->packet_context && stream->flushed_packet_count > 0) { |
941 | /* | |
942 | * A stream without a packet context, and thus without | |
943 | * content and packet size members, can't have more than | |
944 | * one packet. | |
945 | */ | |
946 | ret = -1; | |
947 | goto end; | |
948 | } | |
949 | ||
dc3fffef PP |
950 | trace = bt_ctf_stream_class_borrow_trace(stream->stream_class); |
951 | assert(trace); | |
952 | native_byte_order = bt_ctf_trace_get_byte_order(trace); | |
98edd02c | 953 | empty_packet = (stream->events->len == 0); |
d246b111 | 954 | |
28362f2b | 955 | /* mmap the next packet */ |
dc3fffef PP |
956 | bt_ctf_stream_pos_packet_seek(&stream->pos, 0, SEEK_CUR); |
957 | ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos, | |
958 | native_byte_order); | |
d246b111 JG |
959 | if (ret) { |
960 | goto end; | |
273b65be JG |
961 | } |
962 | ||
98edd02c JG |
963 | if (stream->packet_context) { |
964 | /* Set the default context attributes if present and unset. */ | |
965 | if (!empty_packet && !get_event_header_timestamp( | |
966 | ((struct bt_ctf_event *) g_ptr_array_index( | |
967 | stream->events, 0))->event_header, ×tamp_begin)) { | |
af9296f3 JG |
968 | ret = try_set_structure_field_integer( |
969 | stream->packet_context, | |
98edd02c | 970 | "timestamp_begin", timestamp_begin); |
af9296f3 | 971 | if (ret < 0) { |
98edd02c JG |
972 | goto end; |
973 | } | |
c9af50d1 | 974 | auto_set_fields.timestamp_begin = ret == 1; |
98edd02c JG |
975 | } |
976 | ||
977 | if (!empty_packet && !get_event_header_timestamp( | |
978 | ((struct bt_ctf_event *) g_ptr_array_index( | |
979 | stream->events, stream->events->len - 1))->event_header, | |
980 | ×tamp_end)) { | |
981 | ||
af9296f3 JG |
982 | ret = try_set_structure_field_integer( |
983 | stream->packet_context, | |
98edd02c | 984 | "timestamp_end", timestamp_end); |
af9296f3 | 985 | if (ret < 0) { |
98edd02c JG |
986 | goto end; |
987 | } | |
c9af50d1 | 988 | auto_set_fields.timestamp_end = ret == 1; |
98edd02c | 989 | } |
af9296f3 | 990 | ret = try_set_structure_field_integer(stream->packet_context, |
98edd02c | 991 | "content_size", UINT64_MAX); |
af9296f3 | 992 | if (ret < 0) { |
9a220c32 JG |
993 | goto end; |
994 | } | |
c9af50d1 | 995 | auto_set_fields.content_size = ret == 1; |
273b65be | 996 | |
af9296f3 | 997 | ret = try_set_structure_field_integer(stream->packet_context, |
98edd02c | 998 | "packet_size", UINT64_MAX); |
af9296f3 | 999 | if (ret < 0) { |
98edd02c JG |
1000 | goto end; |
1001 | } | |
c9af50d1 | 1002 | auto_set_fields.packet_size = ret == 1; |
12c8a1a3 | 1003 | |
10cb7b41 JG |
1004 | /* Write packet context */ |
1005 | memcpy(&packet_context_pos, &stream->pos, | |
dc3fffef | 1006 | sizeof(packet_context_pos)); |
10cb7b41 | 1007 | ret = bt_ctf_field_serialize(stream->packet_context, |
dc3fffef | 1008 | &stream->pos, native_byte_order); |
98edd02c JG |
1009 | if (ret) { |
1010 | goto end; | |
1011 | } | |
12c8a1a3 JG |
1012 | } |
1013 | ||
273b65be JG |
1014 | for (i = 0; i < stream->events->len; i++) { |
1015 | struct bt_ctf_event *event = g_ptr_array_index( | |
1016 | stream->events, i); | |
273b65be | 1017 | |
273b65be | 1018 | /* Write event header */ |
662e778c | 1019 | ret = bt_ctf_field_serialize(event->event_header, |
dc3fffef | 1020 | &stream->pos, native_byte_order); |
273b65be JG |
1021 | if (ret) { |
1022 | goto end; | |
1023 | } | |
1024 | ||
8bfa3f9c | 1025 | /* Write stream event context */ |
5fd2e9fd | 1026 | if (event->stream_event_context) { |
8bfa3f9c | 1027 | ret = bt_ctf_field_serialize( |
dc3fffef PP |
1028 | event->stream_event_context, &stream->pos, |
1029 | native_byte_order); | |
8bfa3f9c JG |
1030 | if (ret) { |
1031 | goto end; | |
1032 | } | |
1033 | } | |
1034 | ||
273b65be | 1035 | /* Write event content */ |
dc3fffef PP |
1036 | ret = bt_ctf_event_serialize(event, &stream->pos, |
1037 | native_byte_order); | |
273b65be JG |
1038 | if (ret) { |
1039 | goto end; | |
1040 | } | |
1041 | } | |
1042 | ||
0686ef94 JG |
1043 | /* Rounded-up in case content_size is not byte-aligned. */ |
1044 | packet_size_bits = (stream->pos.offset + (CHAR_BIT - 1)) & | |
1045 | ~(CHAR_BIT - 1); | |
1046 | stream->pos.packet_size = packet_size_bits; | |
1047 | ||
98edd02c JG |
1048 | if (stream->packet_context) { |
1049 | /* | |
1050 | * Update the packet total size and content size and overwrite | |
1051 | * the packet context. | |
1052 | * Copy base_mma as the packet may have been remapped (e.g. when | |
1053 | * a packet is resized). | |
1054 | */ | |
1055 | packet_context_pos.base_mma = stream->pos.base_mma; | |
c9af50d1 JG |
1056 | if (auto_set_fields.content_size) { |
1057 | ret = set_structure_field_integer( | |
1058 | stream->packet_context, | |
1059 | "content_size", stream->pos.offset); | |
1060 | if (ret < 0) { | |
1061 | goto end; | |
1062 | } | |
98edd02c | 1063 | } |
273b65be | 1064 | |
c9af50d1 JG |
1065 | if (auto_set_fields.packet_size) { |
1066 | ret = set_structure_field_integer(stream->packet_context, | |
1067 | "packet_size", packet_size_bits); | |
1068 | if (ret < 0) { | |
1069 | goto end; | |
1070 | } | |
98edd02c | 1071 | } |
273b65be | 1072 | |
98edd02c | 1073 | ret = bt_ctf_field_serialize(stream->packet_context, |
dc3fffef | 1074 | &packet_context_pos, native_byte_order); |
98edd02c JG |
1075 | if (ret) { |
1076 | goto end; | |
1077 | } | |
273b65be JG |
1078 | } |
1079 | ||
1080 | g_ptr_array_set_size(stream->events, 0); | |
1081 | stream->flushed_packet_count++; | |
0686ef94 | 1082 | stream->size += packet_size_bits / CHAR_BIT; |
273b65be | 1083 | end: |
c9af50d1 JG |
1084 | /* Reset automatically-set fields. */ |
1085 | if (auto_set_fields.timestamp_begin) { | |
1086 | reset_structure_field(stream->packet_context, | |
1087 | "timestamp_begin"); | |
1088 | } | |
1089 | if (auto_set_fields.timestamp_end) { | |
1090 | reset_structure_field(stream->packet_context, | |
1091 | "timestamp_end"); | |
1092 | } | |
1093 | if (auto_set_fields.packet_size) { | |
1094 | reset_structure_field(stream->packet_context, | |
1095 | "packet_size"); | |
1096 | } | |
1097 | if (auto_set_fields.content_size) { | |
1098 | reset_structure_field(stream->packet_context, | |
1099 | "content_size"); | |
1100 | } | |
83509119 | 1101 | bt_put(integer); |
cd7d8fb7 JG |
1102 | |
1103 | if (ret < 0) { | |
1104 | /* | |
1105 | * We failed to write the packet. Its size is therefore set to 0 | |
1106 | * to ensure the next mapping is done in the same place rather | |
1107 | * than advancing by "stream->pos.packet_size", which would | |
1108 | * leave a corrupted packet in the trace. | |
1109 | */ | |
1110 | stream->pos.packet_size = 0; | |
1111 | } | |
273b65be JG |
1112 | return ret; |
1113 | } | |
1114 | ||
1115 | void bt_ctf_stream_get(struct bt_ctf_stream *stream) | |
1116 | { | |
83509119 | 1117 | bt_get(stream); |
273b65be JG |
1118 | } |
1119 | ||
1120 | void bt_ctf_stream_put(struct bt_ctf_stream *stream) | |
1121 | { | |
83509119 | 1122 | bt_put(stream); |
273b65be JG |
1123 | } |
1124 | ||
1125 | static | |
83509119 | 1126 | void bt_ctf_stream_destroy(struct bt_object *obj) |
273b65be JG |
1127 | { |
1128 | struct bt_ctf_stream *stream; | |
3230ee6b | 1129 | int i; |
273b65be | 1130 | |
83509119 | 1131 | stream = container_of(obj, struct bt_ctf_stream, base); |
3230ee6b PP |
1132 | |
1133 | /* Call destroy listeners in reverse registration order */ | |
1134 | for (i = stream->destroy_listeners->len - 1; i >= 0; i--) { | |
1135 | struct bt_ctf_stream_destroy_listener *listener = | |
1136 | &g_array_index(stream->destroy_listeners, | |
1137 | struct bt_ctf_stream_destroy_listener, i); | |
1138 | ||
1139 | listener->func(stream, listener->data); | |
1140 | } | |
1141 | ||
dc3fffef | 1142 | (void) bt_ctf_stream_pos_fini(&stream->pos); |
0686ef94 JG |
1143 | if (stream->pos.fd >= 0) { |
1144 | int ret; | |
1145 | ||
1146 | /* | |
1147 | * Truncate the file's size to the minimum required to fit the | |
1148 | * last packet as we might have grown it too much on the last | |
1149 | * mmap. | |
1150 | */ | |
1151 | do { | |
1152 | ret = ftruncate(stream->pos.fd, stream->size); | |
1153 | } while (ret == -1 && errno == EINTR); | |
1154 | if (ret) { | |
1155 | perror("ftruncate"); | |
1156 | } | |
1157 | ||
1158 | if (close(stream->pos.fd)) { | |
1159 | perror("close"); | |
1160 | } | |
9f56e450 | 1161 | } |
12c8a1a3 | 1162 | |
12c8a1a3 JG |
1163 | if (stream->events) { |
1164 | g_ptr_array_free(stream->events, TRUE); | |
1165 | } | |
b71d7298 PP |
1166 | |
1167 | if (stream->name) { | |
1168 | g_string_free(stream->name, TRUE); | |
1169 | } | |
41ac640a | 1170 | |
3230ee6b PP |
1171 | if (stream->comp_cur_port) { |
1172 | GHashTableIter ht_iter; | |
1173 | gpointer comp_gptr, port_gptr; | |
1174 | ||
1175 | /* | |
1176 | * Since we're destroying the stream, remove the destroy | |
1177 | * listeners that it registered for each component in | |
1178 | * its component-port mapping hash table. Otherwise they | |
1179 | * would be called and the stream would be accessed once | |
1180 | * it's freed or another stream would be accessed. | |
1181 | */ | |
1182 | g_hash_table_iter_init(&ht_iter, stream->comp_cur_port); | |
1183 | ||
1184 | while (g_hash_table_iter_next(&ht_iter, &comp_gptr, &port_gptr)) { | |
1185 | assert(comp_gptr); | |
1186 | bt_component_remove_destroy_listener((void *) comp_gptr, | |
1187 | component_destroy_listener, stream); | |
1188 | } | |
1189 | ||
1190 | g_hash_table_destroy(stream->comp_cur_port); | |
1191 | } | |
1192 | ||
1193 | if (stream->destroy_listeners) { | |
1194 | g_array_free(stream->destroy_listeners, TRUE); | |
1195 | } | |
1196 | ||
83509119 JG |
1197 | bt_put(stream->packet_header); |
1198 | bt_put(stream->packet_context); | |
273b65be JG |
1199 | g_free(stream); |
1200 | } | |
1201 | ||
273b65be | 1202 | static |
af9296f3 JG |
1203 | int _set_structure_field_integer(struct bt_ctf_field *structure, char *name, |
1204 | uint64_t value, bool force) | |
273b65be JG |
1205 | { |
1206 | int ret = 0; | |
d7b1ea66 | 1207 | struct bt_ctf_field_type *field_type = NULL; |
273b65be JG |
1208 | struct bt_ctf_field *integer = |
1209 | bt_ctf_field_structure_get_field(structure, name); | |
12c8a1a3 JG |
1210 | |
1211 | if (!structure || !name) { | |
273b65be JG |
1212 | ret = -1; |
1213 | goto end; | |
1214 | } | |
1215 | ||
12c8a1a3 JG |
1216 | if (!integer) { |
1217 | /* Field not found, not an error. */ | |
1218 | goto end; | |
1219 | } | |
1220 | ||
1221 | /* Make sure the payload has not already been set. */ | |
af9296f3 | 1222 | if (!force && bt_ctf_field_is_set(integer)) { |
12c8a1a3 JG |
1223 | /* Payload already set, not an error */ |
1224 | goto end; | |
1225 | } | |
1226 | ||
d7b1ea66 | 1227 | field_type = bt_ctf_field_get_type(integer); |
d7b1ea66 | 1228 | assert(field_type); |
1487a16a | 1229 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
d7b1ea66 JG |
1230 | /* |
1231 | * The user most likely meant for us to populate this field | |
1232 | * automatically. However, we can only do this if the field | |
1233 | * is an integer. Return an error. | |
1234 | */ | |
1235 | ret = -1; | |
1236 | goto end; | |
1237 | } | |
1238 | ||
1239 | if (bt_ctf_field_type_integer_get_signed(field_type)) { | |
1240 | ret = bt_ctf_field_signed_integer_set_value(integer, | |
1241 | (int64_t) value); | |
1242 | } else { | |
1243 | ret = bt_ctf_field_unsigned_integer_set_value(integer, value); | |
1244 | } | |
af9296f3 | 1245 | ret = !ret ? 1 : ret; |
273b65be | 1246 | end: |
83509119 JG |
1247 | bt_put(integer); |
1248 | bt_put(field_type); | |
273b65be JG |
1249 | return ret; |
1250 | } | |
b71d7298 | 1251 | |
af9296f3 JG |
1252 | static |
1253 | int set_structure_field_integer(struct bt_ctf_field *structure, char *name, | |
1254 | uint64_t value) | |
1255 | { | |
1256 | return _set_structure_field_integer(structure, name, value, true); | |
1257 | } | |
1258 | ||
1259 | /* | |
1260 | * Returns the following codes: | |
1261 | * 1 if the field was found and set, | |
1262 | * 0 if nothing was done (field not found, or was already set), | |
1263 | * <0 if an error was encoutered | |
1264 | */ | |
1265 | static | |
1266 | int try_set_structure_field_integer(struct bt_ctf_field *structure, char *name, | |
1267 | uint64_t value) | |
1268 | { | |
1269 | return _set_structure_field_integer(structure, name, value, false); | |
1270 | } | |
1271 | ||
b71d7298 PP |
1272 | const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream) |
1273 | { | |
1274 | const char *name = NULL; | |
1275 | ||
1276 | if (!stream) { | |
1277 | goto end; | |
1278 | } | |
1279 | ||
1280 | name = stream->name ? stream->name->str : NULL; | |
1281 | ||
1282 | end: | |
1283 | return name; | |
1284 | } | |
98a4cbef PP |
1285 | |
1286 | int bt_ctf_stream_is_writer(struct bt_ctf_stream *stream) | |
1287 | { | |
1288 | int ret = -1; | |
1289 | ||
1290 | if (!stream) { | |
1291 | goto end; | |
1292 | } | |
1293 | ||
1294 | ret = (stream->pos.fd >= 0); | |
1295 | ||
1296 | end: | |
1297 | return ret; | |
1298 | } | |
3230ee6b PP |
1299 | |
1300 | BT_HIDDEN | |
1301 | void bt_ctf_stream_map_component_to_port(struct bt_ctf_stream *stream, | |
1302 | struct bt_component *comp, | |
1303 | struct bt_port *port) | |
1304 | { | |
1305 | assert(stream); | |
1306 | assert(comp); | |
1307 | assert(port); | |
1308 | assert(stream->comp_cur_port); | |
1309 | ||
1310 | /* | |
1311 | * Do not take a reference to the component here because we | |
1312 | * don't want the component to exist as long as this stream | |
1313 | * exists. Instead, keep a weak reference, but add a destroy | |
1314 | * listener so that we remove this hash table entry when we know | |
1315 | * the component is destroyed. | |
1316 | */ | |
1317 | bt_component_add_destroy_listener(comp, component_destroy_listener, | |
1318 | stream); | |
1319 | g_hash_table_insert(stream->comp_cur_port, comp, port); | |
1320 | } | |
1321 | ||
1322 | BT_HIDDEN | |
1323 | struct bt_port *bt_ctf_stream_port_for_component(struct bt_ctf_stream *stream, | |
1324 | struct bt_component *comp) | |
1325 | { | |
1326 | assert(stream); | |
1327 | assert(comp); | |
1328 | assert(stream->comp_cur_port); | |
1329 | return g_hash_table_lookup(stream->comp_cur_port, comp); | |
1330 | } | |
1331 | ||
1332 | BT_HIDDEN | |
1333 | void bt_ctf_stream_add_destroy_listener(struct bt_ctf_stream *stream, | |
1334 | bt_ctf_stream_destroy_listener_func func, void *data) | |
1335 | { | |
1336 | struct bt_ctf_stream_destroy_listener listener; | |
1337 | ||
1338 | assert(stream); | |
1339 | assert(func); | |
1340 | listener.func = func; | |
1341 | listener.data = data; | |
1342 | g_array_append_val(stream->destroy_listeners, listener); | |
1343 | } | |
1344 | ||
1345 | BT_HIDDEN | |
1346 | void bt_ctf_stream_remove_destroy_listener(struct bt_ctf_stream *stream, | |
1347 | bt_ctf_stream_destroy_listener_func func, void *data) | |
1348 | { | |
1349 | size_t i; | |
1350 | ||
1351 | assert(stream); | |
1352 | assert(func); | |
1353 | ||
1354 | for (i = 0; i < stream->destroy_listeners->len; i++) { | |
1355 | struct bt_ctf_stream_destroy_listener *listener = | |
1356 | &g_array_index(stream->destroy_listeners, | |
1357 | struct bt_ctf_stream_destroy_listener, i); | |
1358 | ||
1359 | if (listener->func == func && listener->data == data) { | |
1360 | g_array_remove_index(stream->destroy_listeners, i); | |
1361 | i--; | |
1362 | } | |
1363 | } | |
1364 | } |