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 | ||
3f043b05 | 29 | #include <babeltrace/ctf-ir/clock.h> |
adc315b8 | 30 | #include <babeltrace/ctf-ir/clock-internal.h> |
273b65be | 31 | #include <babeltrace/ctf-writer/event.h> |
adc315b8 JG |
32 | #include <babeltrace/ctf-ir/event-internal.h> |
33 | #include <babeltrace/ctf-ir/event-types-internal.h> | |
34 | #include <babeltrace/ctf-ir/event-fields-internal.h> | |
3f043b05 JG |
35 | #include <babeltrace/ctf-ir/stream.h> |
36 | #include <babeltrace/ctf-ir/stream-internal.h> | |
adc315b8 | 37 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
273b65be JG |
38 | #include <babeltrace/ctf-writer/functor-internal.h> |
39 | #include <babeltrace/compiler.h> | |
40 | #include <babeltrace/align.h> | |
d246b111 | 41 | #include <babeltrace/ctf/ctf-index.h> |
273b65be JG |
42 | |
43 | static | |
44 | void bt_ctf_stream_destroy(struct bt_ctf_ref *ref); | |
45 | static | |
273b65be JG |
46 | int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t); |
47 | ||
d246b111 JG |
48 | static |
49 | int set_packet_header_magic(struct bt_ctf_stream *stream) | |
50 | { | |
51 | int ret = 0; | |
52 | struct bt_ctf_field_type *magic_field_type = NULL; | |
53 | struct bt_ctf_field *magic_field = bt_ctf_field_structure_get_field( | |
54 | stream->packet_header, "magic"); | |
55 | ||
56 | if (!magic_field) { | |
57 | /* No magic field found. Not an error, skip. */ | |
58 | goto end; | |
59 | } | |
60 | ||
61 | if (!bt_ctf_field_validate(magic_field)) { | |
62 | /* Value already set. Not an error, skip. */ | |
63 | goto end; | |
64 | } | |
65 | ||
66 | magic_field_type = bt_ctf_field_get_type(magic_field); | |
67 | assert(magic_field_type); | |
68 | ||
69 | if (bt_ctf_field_type_get_type_id(magic_field_type) != | |
70 | CTF_TYPE_INTEGER) { | |
71 | /* Magic field is not an integer. Not an error, skip. */ | |
72 | goto end; | |
73 | } | |
74 | ||
75 | if (bt_ctf_field_type_integer_get_size(magic_field_type) != 32) { | |
76 | /* | |
77 | * Magic field is not of the expected size. | |
78 | * Not an error, skip. | |
79 | */ | |
80 | goto end; | |
81 | } | |
82 | ||
83 | ret = bt_ctf_field_type_integer_get_signed(magic_field_type); | |
84 | assert(ret >= 0); | |
85 | if (ret) { | |
86 | ret = bt_ctf_field_signed_integer_set_value(magic_field, | |
87 | (int64_t) 0xC1FC1FC1); | |
88 | } else { | |
89 | ret = bt_ctf_field_unsigned_integer_set_value(magic_field, | |
90 | (uint64_t) 0xC1FC1FC1); | |
91 | } | |
92 | end: | |
93 | if (magic_field) { | |
94 | bt_ctf_field_put(magic_field); | |
95 | } | |
96 | if (magic_field_type) { | |
97 | bt_ctf_field_type_put(magic_field_type); | |
98 | } | |
99 | return ret; | |
100 | } | |
101 | ||
102 | static | |
103 | int set_packet_header_uuid(struct bt_ctf_stream *stream) | |
104 | { | |
105 | int i, ret = 0; | |
106 | struct bt_ctf_field_type *uuid_field_type = NULL; | |
107 | struct bt_ctf_field_type *element_field_type = NULL; | |
108 | struct bt_ctf_field *uuid_field = bt_ctf_field_structure_get_field( | |
109 | stream->packet_header, "uuid"); | |
110 | ||
111 | if (!uuid_field) { | |
112 | /* No uuid field found. Not an error, skip. */ | |
113 | goto end; | |
114 | } | |
115 | ||
116 | if (!bt_ctf_field_validate(uuid_field)) { | |
117 | /* Value already set. Not an error, skip. */ | |
118 | goto end; | |
119 | } | |
120 | ||
121 | uuid_field_type = bt_ctf_field_get_type(uuid_field); | |
122 | assert(uuid_field_type); | |
123 | if (bt_ctf_field_type_get_type_id(uuid_field_type) != | |
124 | CTF_TYPE_ARRAY) { | |
125 | /* UUID field is not an array. Not an error, skip. */ | |
126 | goto end; | |
127 | } | |
128 | ||
129 | if (bt_ctf_field_type_array_get_length(uuid_field_type) != 16) { | |
130 | /* | |
131 | * UUID field is not of the expected size. | |
132 | * Not an error, skip. | |
133 | */ | |
134 | goto end; | |
135 | } | |
136 | ||
137 | element_field_type = bt_ctf_field_type_array_get_element_type( | |
138 | uuid_field_type); | |
139 | assert(element_field_type); | |
140 | if (bt_ctf_field_type_get_type_id(element_field_type) != | |
141 | CTF_TYPE_INTEGER) { | |
142 | /* UUID array elements are not integers. Not an error, skip */ | |
143 | goto end; | |
144 | } | |
145 | ||
146 | for (i = 0; i < 16; i++) { | |
147 | struct bt_ctf_field *uuid_element = | |
148 | bt_ctf_field_array_get_field(uuid_field, i); | |
149 | ||
150 | ret = bt_ctf_field_type_integer_get_signed(element_field_type); | |
151 | assert(ret >= 0); | |
152 | ||
153 | if (ret) { | |
154 | ret = bt_ctf_field_signed_integer_set_value( | |
155 | uuid_element, (int64_t) stream->trace->uuid[i]); | |
156 | } else { | |
157 | ret = bt_ctf_field_unsigned_integer_set_value( | |
158 | uuid_element, | |
159 | (uint64_t) stream->trace->uuid[i]); | |
160 | } | |
161 | bt_ctf_field_put(uuid_element); | |
162 | if (ret) { | |
163 | goto end; | |
164 | } | |
165 | } | |
166 | ||
167 | end: | |
168 | if (uuid_field) { | |
169 | bt_ctf_field_put(uuid_field); | |
170 | } | |
171 | if (uuid_field_type) { | |
172 | bt_ctf_field_type_put(uuid_field_type); | |
173 | } | |
174 | if (element_field_type) { | |
175 | bt_ctf_field_type_put(element_field_type); | |
176 | } | |
177 | return ret; | |
178 | } | |
179 | static | |
180 | int set_packet_header_stream_id(struct bt_ctf_stream *stream) | |
181 | { | |
182 | int ret = 0; | |
183 | uint32_t stream_id; | |
184 | struct bt_ctf_field_type *stream_id_field_type = NULL; | |
185 | struct bt_ctf_field *stream_id_field = bt_ctf_field_structure_get_field( | |
186 | stream->packet_header, "stream_id"); | |
187 | ||
188 | if (!stream_id_field) { | |
189 | /* No stream_id field found. Not an error, skip. */ | |
190 | goto end; | |
191 | } | |
192 | ||
193 | if (!bt_ctf_field_validate(stream_id_field)) { | |
194 | /* Value already set. Not an error, skip. */ | |
195 | goto end; | |
196 | } | |
197 | ||
198 | stream_id_field_type = bt_ctf_field_get_type(stream_id_field); | |
199 | assert(stream_id_field_type); | |
200 | if (bt_ctf_field_type_get_type_id(stream_id_field_type) != | |
201 | CTF_TYPE_INTEGER) { | |
202 | /* stream_id field is not an integer. Not an error, skip. */ | |
203 | goto end; | |
204 | } | |
205 | ||
206 | stream_id = stream->stream_class->id; | |
207 | ret = bt_ctf_field_type_integer_get_signed(stream_id_field_type); | |
208 | assert(ret >= 0); | |
209 | if (ret) { | |
210 | ret = bt_ctf_field_signed_integer_set_value(stream_id_field, | |
211 | (int64_t) stream_id); | |
212 | } else { | |
213 | ret = bt_ctf_field_unsigned_integer_set_value(stream_id_field, | |
214 | (uint64_t) stream_id); | |
215 | } | |
216 | end: | |
217 | if (stream_id_field) { | |
218 | bt_ctf_field_put(stream_id_field); | |
219 | } | |
220 | if (stream_id_field_type) { | |
221 | bt_ctf_field_type_put(stream_id_field_type); | |
222 | } | |
223 | return ret; | |
224 | } | |
225 | ||
226 | static | |
227 | int set_packet_header(struct bt_ctf_stream *stream) | |
228 | { | |
229 | int ret; | |
230 | ||
231 | ret = set_packet_header_magic(stream); | |
232 | if (ret) { | |
233 | goto end; | |
234 | } | |
235 | ||
236 | ret = set_packet_header_uuid(stream); | |
237 | if (ret) { | |
238 | goto end; | |
239 | } | |
240 | ||
241 | ret = set_packet_header_stream_id(stream); | |
242 | if (ret) { | |
243 | goto end; | |
244 | } | |
245 | end: | |
246 | return ret; | |
247 | } | |
248 | ||
123fbdec JG |
249 | static |
250 | void put_event(struct bt_ctf_event *event) | |
251 | { | |
252 | bt_ctf_event_set_stream(event, NULL); | |
253 | bt_ctf_event_put(event); | |
254 | } | |
255 | ||
273b65be JG |
256 | BT_HIDDEN |
257 | struct bt_ctf_stream *bt_ctf_stream_create( | |
d246b111 JG |
258 | struct bt_ctf_stream_class *stream_class, |
259 | struct bt_ctf_trace *trace) | |
273b65be | 260 | { |
12c8a1a3 | 261 | int ret; |
273b65be JG |
262 | struct bt_ctf_stream *stream = NULL; |
263 | ||
d246b111 | 264 | if (!stream_class || !trace) { |
273b65be JG |
265 | goto end; |
266 | } | |
267 | ||
268 | stream = g_new0(struct bt_ctf_stream, 1); | |
269 | if (!stream) { | |
270 | goto end; | |
271 | } | |
272 | ||
a4031f67 | 273 | /* A stream has no ownership of its trace (weak ptr) */ |
d246b111 | 274 | stream->trace = trace; |
273b65be | 275 | bt_ctf_ref_init(&stream->ref_count); |
12c8a1a3 JG |
276 | stream->packet_context = bt_ctf_field_create( |
277 | stream_class->packet_context_type); | |
278 | if (!stream->packet_context) { | |
279 | goto error_destroy; | |
280 | } | |
281 | ||
af181248 JG |
282 | /* |
283 | * A stream class may not have a stream event context defined | |
284 | * in which case this stream will never have a stream_event_context | |
285 | * member since, after a stream's creation, the parent stream class | |
286 | * is "frozen" (immutable). | |
287 | */ | |
288 | if (stream_class->event_context_type) { | |
289 | stream->event_context = bt_ctf_field_create( | |
290 | stream_class->event_context_type); | |
291 | if (!stream->packet_context) { | |
292 | goto error_destroy; | |
293 | } | |
294 | } | |
295 | ||
e19cc57d | 296 | /* Initialize events_discarded */ |
12c8a1a3 JG |
297 | ret = set_structure_field_integer(stream->packet_context, |
298 | "events_discarded", 0); | |
299 | if (ret) { | |
300 | goto error_destroy; | |
301 | } | |
302 | ||
273b65be JG |
303 | stream->pos.fd = -1; |
304 | stream->id = stream_class->next_stream_id++; | |
305 | stream->stream_class = stream_class; | |
306 | bt_ctf_stream_class_get(stream_class); | |
273b65be | 307 | stream->events = g_ptr_array_new_with_free_func( |
123fbdec | 308 | (GDestroyNotify) put_event); |
8bfa3f9c JG |
309 | if (!stream->events) { |
310 | goto error_destroy; | |
311 | } | |
312 | if (stream_class->event_context_type) { | |
313 | stream->event_contexts = g_ptr_array_new_with_free_func( | |
314 | (GDestroyNotify) bt_ctf_field_put); | |
315 | if (!stream->event_contexts) { | |
316 | goto error_destroy; | |
317 | } | |
318 | } | |
d246b111 JG |
319 | |
320 | /* A trace is not allowed to have a NULL packet header */ | |
321 | assert(trace->packet_header_type); | |
322 | stream->packet_header = bt_ctf_field_create(trace->packet_header_type); | |
92351460 JG |
323 | if (!stream->packet_header) { |
324 | goto error_destroy; | |
325 | } | |
d246b111 JG |
326 | /* |
327 | * Attempt to populate the default trace packet header fields | |
328 | * (magic, uuid and stream_id). This will _not_ fail shall the | |
329 | * fields not be found or be of an incompatible type; they will | |
330 | * simply not be populated automatically. The user will have to | |
331 | * make sure to set the trace packet header fields himself before | |
332 | * flushing. | |
333 | */ | |
334 | ret = set_packet_header(stream); | |
335 | if (ret) { | |
336 | goto error_destroy; | |
337 | } | |
273b65be JG |
338 | end: |
339 | return stream; | |
12c8a1a3 JG |
340 | error_destroy: |
341 | bt_ctf_stream_destroy(&stream->ref_count); | |
342 | return NULL; | |
273b65be JG |
343 | } |
344 | ||
273b65be JG |
345 | BT_HIDDEN |
346 | int bt_ctf_stream_set_fd(struct bt_ctf_stream *stream, int fd) | |
347 | { | |
348 | int ret = 0; | |
349 | ||
350 | if (stream->pos.fd != -1) { | |
351 | ret = -1; | |
352 | goto end; | |
353 | } | |
354 | ||
355 | ctf_init_pos(&stream->pos, NULL, fd, O_RDWR); | |
356 | stream->pos.fd = fd; | |
357 | end: | |
358 | return ret; | |
359 | } | |
360 | ||
3baf0856 JG |
361 | struct bt_ctf_stream_class *bt_ctf_stream_get_class( |
362 | struct bt_ctf_stream *stream) | |
363 | { | |
364 | struct bt_ctf_stream_class *stream_class = NULL; | |
365 | ||
366 | if (!stream) { | |
367 | goto end; | |
368 | } | |
369 | ||
370 | stream_class = stream->stream_class; | |
371 | bt_ctf_stream_class_get(stream_class); | |
372 | end: | |
373 | return stream_class; | |
374 | } | |
375 | ||
a78a2e25 JG |
376 | int bt_ctf_stream_get_discarded_events_count( |
377 | struct bt_ctf_stream *stream, uint64_t *count) | |
378 | { | |
379 | int64_t ret = 0; | |
12c8a1a3 JG |
380 | int field_signed; |
381 | struct bt_ctf_field *events_discarded_field = NULL; | |
382 | struct bt_ctf_field_type *events_discarded_field_type = NULL; | |
a78a2e25 | 383 | |
12c8a1a3 | 384 | if (!stream || !count || !stream->packet_context) { |
a78a2e25 JG |
385 | ret = -1; |
386 | goto end; | |
387 | } | |
388 | ||
12c8a1a3 JG |
389 | events_discarded_field = bt_ctf_field_structure_get_field( |
390 | stream->packet_context, "events_discarded"); | |
391 | if (!events_discarded_field) { | |
392 | ret = -1; | |
393 | goto end; | |
394 | } | |
395 | ||
396 | events_discarded_field_type = bt_ctf_field_get_type( | |
397 | events_discarded_field); | |
398 | if (!events_discarded_field_type) { | |
399 | ret = -1; | |
400 | goto end; | |
401 | } | |
402 | ||
403 | field_signed = bt_ctf_field_type_integer_get_signed( | |
404 | events_discarded_field_type); | |
405 | if (field_signed < 0) { | |
406 | ret = field_signed; | |
407 | goto end; | |
408 | } | |
409 | ||
410 | if (field_signed) { | |
411 | int64_t signed_count; | |
412 | ||
413 | ret = bt_ctf_field_signed_integer_get_value( | |
414 | events_discarded_field, &signed_count); | |
415 | if (ret) { | |
416 | goto end; | |
417 | } | |
418 | if (signed_count < 0) { | |
419 | /* Invalid value */ | |
420 | ret = -1; | |
421 | goto end; | |
422 | } | |
423 | *count = (uint64_t) signed_count; | |
424 | } else { | |
425 | ret = bt_ctf_field_unsigned_integer_get_value( | |
426 | events_discarded_field, count); | |
427 | if (ret) { | |
428 | goto end; | |
429 | } | |
430 | } | |
a78a2e25 | 431 | end: |
12c8a1a3 JG |
432 | if (events_discarded_field) { |
433 | bt_ctf_field_put(events_discarded_field); | |
434 | } | |
435 | if (events_discarded_field_type) { | |
436 | bt_ctf_field_type_put(events_discarded_field_type); | |
437 | } | |
a78a2e25 JG |
438 | return ret; |
439 | } | |
440 | ||
273b65be JG |
441 | void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, |
442 | uint64_t event_count) | |
443 | { | |
12c8a1a3 JG |
444 | int ret; |
445 | int field_signed; | |
446 | uint64_t previous_count; | |
447 | uint64_t new_count; | |
448 | struct bt_ctf_field *events_discarded_field = NULL; | |
449 | struct bt_ctf_field_type *events_discarded_field_type = NULL; | |
450 | ||
451 | if (!stream || !stream->packet_context) { | |
452 | goto end; | |
453 | } | |
454 | ||
455 | ret = bt_ctf_stream_get_discarded_events_count(stream, | |
456 | &previous_count); | |
457 | if (ret) { | |
458 | goto end; | |
459 | } | |
460 | ||
461 | events_discarded_field = bt_ctf_field_structure_get_field( | |
462 | stream->packet_context, "events_discarded"); | |
463 | if (!events_discarded_field) { | |
464 | goto end; | |
465 | } | |
466 | ||
467 | events_discarded_field_type = bt_ctf_field_get_type( | |
468 | events_discarded_field); | |
469 | if (!events_discarded_field_type) { | |
470 | goto end; | |
471 | } | |
472 | ||
473 | field_signed = bt_ctf_field_type_integer_get_signed( | |
474 | events_discarded_field_type); | |
475 | if (field_signed < 0) { | |
476 | goto end; | |
273b65be JG |
477 | } |
478 | ||
12c8a1a3 JG |
479 | new_count = previous_count + event_count; |
480 | if (field_signed) { | |
481 | ret = bt_ctf_field_signed_integer_set_value( | |
482 | events_discarded_field, (int64_t) new_count); | |
483 | if (ret) { | |
484 | goto end; | |
485 | } | |
486 | } else { | |
487 | ret = bt_ctf_field_unsigned_integer_set_value( | |
488 | events_discarded_field, new_count); | |
489 | if (ret) { | |
490 | goto end; | |
491 | } | |
492 | } | |
493 | ||
494 | end: | |
495 | if (events_discarded_field) { | |
496 | bt_ctf_field_put(events_discarded_field); | |
497 | } | |
498 | if (events_discarded_field_type) { | |
499 | bt_ctf_field_type_put(events_discarded_field_type); | |
500 | } | |
273b65be JG |
501 | } |
502 | ||
503 | int bt_ctf_stream_append_event(struct bt_ctf_stream *stream, | |
504 | struct bt_ctf_event *event) | |
505 | { | |
506 | int ret = 0; | |
af181248 | 507 | struct bt_ctf_field *event_context_copy = NULL; |
273b65be JG |
508 | |
509 | if (!stream || !event) { | |
510 | ret = -1; | |
511 | goto end; | |
512 | } | |
513 | ||
123fbdec JG |
514 | ret = bt_ctf_event_set_stream(event, stream); |
515 | if (ret) { | |
516 | /* Event was already associated to a stream */ | |
517 | ret = -1; | |
518 | goto end; | |
519 | } | |
520 | ||
662e778c JG |
521 | ret = bt_ctf_event_populate_event_header(event); |
522 | if (ret) { | |
523 | goto end; | |
524 | } | |
525 | ||
785b5390 | 526 | /* Make sure the event's payload is set */ |
273b65be JG |
527 | ret = bt_ctf_event_validate(event); |
528 | if (ret) { | |
529 | goto end; | |
530 | } | |
531 | ||
8bfa3f9c JG |
532 | /* Sample the current stream event context by copying it */ |
533 | if (stream->event_context) { | |
534 | /* Make sure the event context's payload is set */ | |
535 | ret = bt_ctf_field_validate(stream->event_context); | |
536 | if (ret) { | |
537 | goto end; | |
538 | } | |
539 | ||
540 | event_context_copy = bt_ctf_field_copy(stream->event_context); | |
541 | if (!event_context_copy) { | |
542 | ret = -1; | |
543 | goto end; | |
544 | } | |
545 | } | |
546 | ||
273b65be | 547 | bt_ctf_event_get(event); |
8bfa3f9c | 548 | /* Save the new event along with its associated stream event context */ |
273b65be | 549 | g_ptr_array_add(stream->events, event); |
8bfa3f9c JG |
550 | if (event_context_copy) { |
551 | g_ptr_array_add(stream->event_contexts, event_context_copy); | |
552 | } | |
273b65be | 553 | end: |
123fbdec JG |
554 | if (ret) { |
555 | (void) bt_ctf_event_set_stream(event, NULL); | |
556 | } | |
273b65be JG |
557 | return ret; |
558 | } | |
559 | ||
12c8a1a3 JG |
560 | struct bt_ctf_field *bt_ctf_stream_get_packet_context( |
561 | struct bt_ctf_stream *stream) | |
562 | { | |
563 | struct bt_ctf_field *packet_context = NULL; | |
564 | ||
565 | if (!stream) { | |
566 | goto end; | |
567 | } | |
568 | ||
569 | packet_context = stream->packet_context; | |
12c8a1a3 JG |
570 | if (packet_context) { |
571 | bt_ctf_field_get(packet_context); | |
572 | } | |
34629a55 | 573 | end: |
12c8a1a3 JG |
574 | return packet_context; |
575 | } | |
576 | ||
577 | int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream, | |
578 | struct bt_ctf_field *field) | |
579 | { | |
580 | int ret = 0; | |
581 | struct bt_ctf_field_type *field_type; | |
582 | ||
583 | if (!stream || !field) { | |
584 | ret = -1; | |
585 | goto end; | |
586 | } | |
587 | ||
588 | field_type = bt_ctf_field_get_type(field); | |
589 | if (field_type != stream->stream_class->packet_context_type) { | |
590 | ret = -1; | |
591 | goto end; | |
592 | } | |
593 | ||
594 | bt_ctf_field_type_put(field_type); | |
595 | bt_ctf_field_get(field); | |
596 | bt_ctf_field_put(stream->packet_context); | |
597 | stream->packet_context = field; | |
598 | end: | |
599 | return ret; | |
600 | } | |
601 | ||
8bfa3f9c JG |
602 | struct bt_ctf_field *bt_ctf_stream_get_event_context( |
603 | struct bt_ctf_stream *stream) | |
604 | { | |
605 | struct bt_ctf_field *event_context = NULL; | |
606 | ||
607 | if (!stream) { | |
608 | goto end; | |
609 | } | |
610 | ||
611 | event_context = stream->event_context; | |
612 | if (event_context) { | |
613 | bt_ctf_field_get(event_context); | |
614 | } | |
615 | end: | |
616 | return event_context; | |
617 | } | |
618 | ||
619 | int bt_ctf_stream_set_event_context(struct bt_ctf_stream *stream, | |
620 | struct bt_ctf_field *field) | |
621 | { | |
622 | int ret = 0; | |
623 | struct bt_ctf_field_type *field_type = NULL; | |
624 | ||
625 | if (!stream || !field) { | |
626 | ret = -1; | |
627 | goto end; | |
628 | } | |
629 | ||
630 | field_type = bt_ctf_field_get_type(field); | |
631 | if (field_type != stream->stream_class->event_context_type) { | |
632 | ret = -1; | |
633 | goto end; | |
634 | } | |
635 | ||
636 | bt_ctf_field_get(field); | |
637 | bt_ctf_field_put(stream->event_context); | |
638 | stream->event_context = field; | |
639 | end: | |
640 | if (field_type) { | |
641 | bt_ctf_field_type_put(field_type); | |
642 | } | |
643 | return ret; | |
644 | } | |
645 | ||
263a7df5 JG |
646 | struct bt_ctf_field *bt_ctf_stream_get_packet_header( |
647 | struct bt_ctf_stream *stream) | |
648 | { | |
649 | struct bt_ctf_field *packet_header = NULL; | |
650 | ||
651 | if (!stream) { | |
652 | goto end; | |
653 | } | |
654 | ||
655 | packet_header = stream->packet_header; | |
656 | if (packet_header) { | |
657 | bt_ctf_field_get(packet_header); | |
658 | } | |
659 | end: | |
660 | return packet_header; | |
661 | } | |
662 | ||
663 | int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream, | |
664 | struct bt_ctf_field *field) | |
665 | { | |
666 | int ret = 0; | |
667 | struct bt_ctf_field_type *field_type = NULL; | |
668 | ||
669 | if (!stream || !field) { | |
670 | ret = -1; | |
671 | goto end; | |
672 | } | |
673 | ||
674 | field_type = bt_ctf_field_get_type(field); | |
675 | if (field_type != stream->trace->packet_header_type) { | |
676 | ret = -1; | |
677 | goto end; | |
678 | } | |
679 | ||
680 | bt_ctf_field_get(field); | |
681 | bt_ctf_field_put(stream->packet_header); | |
682 | stream->packet_header = field; | |
683 | end: | |
684 | if (field_type) { | |
685 | bt_ctf_field_type_put(field_type); | |
686 | } | |
687 | return ret; | |
688 | } | |
689 | ||
9a220c32 JG |
690 | static |
691 | int get_event_header_timestamp(struct bt_ctf_field *event_header, uint64_t *timestamp) | |
692 | { | |
693 | int ret = 0; | |
694 | struct bt_ctf_field *timestamp_field = NULL; | |
695 | struct bt_ctf_field_type *timestamp_field_type = NULL; | |
696 | ||
697 | timestamp_field = bt_ctf_field_structure_get_field(event_header, | |
698 | "timestamp"); | |
699 | if (!timestamp_field) { | |
700 | ret = -1; | |
701 | goto end; | |
702 | } | |
703 | ||
704 | timestamp_field_type = bt_ctf_field_get_type(timestamp_field); | |
705 | assert(timestamp_field_type); | |
706 | if (bt_ctf_field_type_get_type_id(timestamp_field_type) != | |
707 | CTF_TYPE_INTEGER) { | |
708 | ret = -1; | |
709 | goto end; | |
710 | } | |
711 | ||
712 | if (bt_ctf_field_type_integer_get_signed(timestamp_field_type)) { | |
713 | int64_t val; | |
714 | ||
715 | ret = bt_ctf_field_signed_integer_get_value(timestamp_field, | |
716 | &val); | |
717 | if (ret) { | |
718 | goto end; | |
719 | } | |
720 | *timestamp = (uint64_t) val; | |
721 | } else { | |
722 | ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field, | |
723 | timestamp); | |
724 | if (ret) { | |
725 | goto end; | |
726 | } | |
727 | } | |
728 | end: | |
729 | bt_ctf_field_put(timestamp_field); | |
730 | bt_ctf_field_type_put(timestamp_field_type); | |
731 | return ret; | |
732 | } | |
733 | ||
273b65be JG |
734 | int bt_ctf_stream_flush(struct bt_ctf_stream *stream) |
735 | { | |
736 | int ret = 0; | |
737 | size_t i; | |
12c8a1a3 | 738 | uint64_t timestamp_begin, timestamp_end, events_discarded; |
273b65be JG |
739 | struct bt_ctf_field *integer = NULL; |
740 | struct ctf_stream_pos packet_context_pos; | |
741 | ||
bc37ae52 JG |
742 | if (!stream || stream->pos.fd < 0) { |
743 | /* | |
744 | * Stream does not have an associated fd. It is, | |
745 | * therefore, not a stream being used to write events. | |
746 | */ | |
273b65be JG |
747 | ret = -1; |
748 | goto end; | |
749 | } | |
750 | ||
751 | if (!stream->events->len) { | |
752 | goto end; | |
753 | } | |
754 | ||
d246b111 JG |
755 | ret = bt_ctf_field_validate(stream->packet_header); |
756 | if (ret) { | |
757 | goto end; | |
758 | } | |
759 | ||
28362f2b JG |
760 | /* mmap the next packet */ |
761 | ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR); | |
d246b111 JG |
762 | |
763 | ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos); | |
764 | if (ret) { | |
765 | goto end; | |
273b65be JG |
766 | } |
767 | ||
12c8a1a3 | 768 | /* Set the default context attributes if present and unset. */ |
9a220c32 JG |
769 | if (!get_event_header_timestamp( |
770 | ((struct bt_ctf_event *) g_ptr_array_index( | |
771 | stream->events, 0))->event_header, ×tamp_begin)) { | |
772 | ret = set_structure_field_integer(stream->packet_context, | |
773 | "timestamp_begin", timestamp_begin); | |
774 | if (ret) { | |
775 | goto end; | |
776 | } | |
273b65be JG |
777 | } |
778 | ||
9a220c32 JG |
779 | if (!get_event_header_timestamp( |
780 | ((struct bt_ctf_event *) g_ptr_array_index( | |
781 | stream->events, stream->events->len - 1))->event_header, | |
782 | ×tamp_end)) { | |
273b65be | 783 | |
9a220c32 JG |
784 | ret = set_structure_field_integer(stream->packet_context, |
785 | "timestamp_end", timestamp_end); | |
786 | if (ret) { | |
787 | goto end; | |
788 | } | |
789 | } | |
12c8a1a3 | 790 | ret = set_structure_field_integer(stream->packet_context, |
273b65be JG |
791 | "content_size", UINT64_MAX); |
792 | if (ret) { | |
793 | goto end; | |
794 | } | |
795 | ||
12c8a1a3 | 796 | ret = set_structure_field_integer(stream->packet_context, |
273b65be JG |
797 | "packet_size", UINT64_MAX); |
798 | if (ret) { | |
799 | goto end; | |
800 | } | |
801 | ||
802 | /* Write packet context */ | |
803 | memcpy(&packet_context_pos, &stream->pos, | |
804 | sizeof(struct ctf_stream_pos)); | |
12c8a1a3 | 805 | ret = bt_ctf_field_serialize(stream->packet_context, |
273b65be JG |
806 | &stream->pos); |
807 | if (ret) { | |
808 | goto end; | |
809 | } | |
810 | ||
12c8a1a3 JG |
811 | ret = bt_ctf_stream_get_discarded_events_count(stream, |
812 | &events_discarded); | |
813 | if (ret) { | |
814 | goto end; | |
815 | } | |
816 | ||
817 | /* Unset the packet context's fields. */ | |
818 | ret = bt_ctf_field_reset(stream->packet_context); | |
819 | if (ret) { | |
820 | goto end; | |
821 | } | |
822 | ||
823 | /* Set the previous number of discarded events. */ | |
824 | ret = set_structure_field_integer(stream->packet_context, | |
825 | "events_discarded", events_discarded); | |
826 | if (ret) { | |
827 | goto end; | |
828 | } | |
829 | ||
273b65be JG |
830 | for (i = 0; i < stream->events->len; i++) { |
831 | struct bt_ctf_event *event = g_ptr_array_index( | |
832 | stream->events, i); | |
273b65be | 833 | |
662e778c | 834 | ret = bt_ctf_field_reset(event->event_header); |
12c8a1a3 JG |
835 | if (ret) { |
836 | goto end; | |
837 | } | |
838 | ||
273b65be | 839 | /* Write event header */ |
662e778c | 840 | ret = bt_ctf_field_serialize(event->event_header, |
273b65be JG |
841 | &stream->pos); |
842 | if (ret) { | |
843 | goto end; | |
844 | } | |
845 | ||
8bfa3f9c JG |
846 | /* Write stream event context */ |
847 | if (stream->event_contexts) { | |
848 | ret = bt_ctf_field_serialize( | |
849 | g_ptr_array_index(stream->event_contexts, i), | |
850 | &stream->pos); | |
851 | if (ret) { | |
852 | goto end; | |
853 | } | |
854 | } | |
855 | ||
273b65be JG |
856 | /* Write event content */ |
857 | ret = bt_ctf_event_serialize(event, &stream->pos); | |
858 | if (ret) { | |
859 | goto end; | |
860 | } | |
861 | } | |
862 | ||
863 | /* | |
864 | * Update the packet total size and content size and overwrite the | |
865 | * packet context. | |
3a092c05 JG |
866 | * Copy base_mma as the packet may have been remapped (e.g. when a |
867 | * packet is resized). | |
273b65be | 868 | */ |
3a092c05 | 869 | packet_context_pos.base_mma = stream->pos.base_mma; |
12c8a1a3 | 870 | ret = set_structure_field_integer(stream->packet_context, |
273b65be JG |
871 | "content_size", stream->pos.offset); |
872 | if (ret) { | |
873 | goto end; | |
874 | } | |
875 | ||
12c8a1a3 | 876 | ret = set_structure_field_integer(stream->packet_context, |
273b65be JG |
877 | "packet_size", stream->pos.packet_size); |
878 | if (ret) { | |
879 | goto end; | |
880 | } | |
881 | ||
12c8a1a3 | 882 | ret = bt_ctf_field_serialize(stream->packet_context, |
273b65be JG |
883 | &packet_context_pos); |
884 | if (ret) { | |
885 | goto end; | |
886 | } | |
887 | ||
888 | g_ptr_array_set_size(stream->events, 0); | |
8bfa3f9c JG |
889 | if (stream->event_contexts) { |
890 | g_ptr_array_set_size(stream->event_contexts, 0); | |
891 | } | |
273b65be JG |
892 | stream->flushed_packet_count++; |
893 | end: | |
894 | bt_ctf_field_put(integer); | |
895 | return ret; | |
896 | } | |
897 | ||
898 | void bt_ctf_stream_get(struct bt_ctf_stream *stream) | |
899 | { | |
900 | if (!stream) { | |
901 | return; | |
902 | } | |
903 | ||
904 | bt_ctf_ref_get(&stream->ref_count); | |
905 | } | |
906 | ||
907 | void bt_ctf_stream_put(struct bt_ctf_stream *stream) | |
908 | { | |
909 | if (!stream) { | |
910 | return; | |
911 | } | |
912 | ||
913 | bt_ctf_ref_put(&stream->ref_count, bt_ctf_stream_destroy); | |
914 | } | |
915 | ||
916 | static | |
917 | void bt_ctf_stream_destroy(struct bt_ctf_ref *ref) | |
918 | { | |
919 | struct bt_ctf_stream *stream; | |
920 | ||
921 | if (!ref) { | |
922 | return; | |
923 | } | |
924 | ||
925 | stream = container_of(ref, struct bt_ctf_stream, ref_count); | |
926 | ctf_fini_pos(&stream->pos); | |
0e45d308 | 927 | if (stream->pos.fd >= 0 && close(stream->pos.fd)) { |
9f56e450 JG |
928 | perror("close"); |
929 | } | |
12c8a1a3 JG |
930 | |
931 | if (stream->stream_class) { | |
932 | bt_ctf_stream_class_put(stream->stream_class); | |
933 | } | |
934 | if (stream->events) { | |
935 | g_ptr_array_free(stream->events, TRUE); | |
936 | } | |
8bfa3f9c JG |
937 | if (stream->event_contexts) { |
938 | g_ptr_array_free(stream->event_contexts, TRUE); | |
939 | } | |
d246b111 JG |
940 | if (stream->packet_header) { |
941 | bt_ctf_field_put(stream->packet_header); | |
942 | } | |
12c8a1a3 JG |
943 | if (stream->packet_context) { |
944 | bt_ctf_field_put(stream->packet_context); | |
945 | } | |
8bfa3f9c JG |
946 | if (stream->event_context) { |
947 | bt_ctf_field_put(stream->event_context); | |
948 | } | |
273b65be JG |
949 | g_free(stream); |
950 | } | |
951 | ||
273b65be JG |
952 | static |
953 | int set_structure_field_integer(struct bt_ctf_field *structure, char *name, | |
954 | uint64_t value) | |
955 | { | |
956 | int ret = 0; | |
d7b1ea66 | 957 | struct bt_ctf_field_type *field_type = NULL; |
273b65be JG |
958 | struct bt_ctf_field *integer = |
959 | bt_ctf_field_structure_get_field(structure, name); | |
12c8a1a3 JG |
960 | |
961 | if (!structure || !name) { | |
273b65be JG |
962 | ret = -1; |
963 | goto end; | |
964 | } | |
965 | ||
12c8a1a3 JG |
966 | if (!integer) { |
967 | /* Field not found, not an error. */ | |
968 | goto end; | |
969 | } | |
970 | ||
971 | /* Make sure the payload has not already been set. */ | |
972 | if (!bt_ctf_field_validate(integer)) { | |
973 | /* Payload already set, not an error */ | |
974 | goto end; | |
975 | } | |
976 | ||
d7b1ea66 JG |
977 | field_type = bt_ctf_field_get_type(integer); |
978 | /* Something is serioulsly wrong */ | |
979 | assert(field_type); | |
980 | if (bt_ctf_field_type_get_type_id(field_type) != CTF_TYPE_INTEGER) { | |
981 | /* | |
982 | * The user most likely meant for us to populate this field | |
983 | * automatically. However, we can only do this if the field | |
984 | * is an integer. Return an error. | |
985 | */ | |
986 | ret = -1; | |
987 | goto end; | |
988 | } | |
989 | ||
990 | if (bt_ctf_field_type_integer_get_signed(field_type)) { | |
991 | ret = bt_ctf_field_signed_integer_set_value(integer, | |
992 | (int64_t) value); | |
993 | } else { | |
994 | ret = bt_ctf_field_unsigned_integer_set_value(integer, value); | |
995 | } | |
273b65be | 996 | end: |
d7b1ea66 JG |
997 | if (integer) { |
998 | bt_ctf_field_put(integer); | |
999 | } | |
1000 | if (field_type) { | |
1001 | bt_ctf_field_type_put(field_type); | |
1002 | } | |
273b65be JG |
1003 | return ret; |
1004 | } |