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 | ||
d246b111 | 273 | stream->trace = trace; |
273b65be | 274 | bt_ctf_ref_init(&stream->ref_count); |
12c8a1a3 JG |
275 | stream->packet_context = bt_ctf_field_create( |
276 | stream_class->packet_context_type); | |
277 | if (!stream->packet_context) { | |
278 | goto error_destroy; | |
279 | } | |
280 | ||
af181248 JG |
281 | /* |
282 | * A stream class may not have a stream event context defined | |
283 | * in which case this stream will never have a stream_event_context | |
284 | * member since, after a stream's creation, the parent stream class | |
285 | * is "frozen" (immutable). | |
286 | */ | |
287 | if (stream_class->event_context_type) { | |
288 | stream->event_context = bt_ctf_field_create( | |
289 | stream_class->event_context_type); | |
290 | if (!stream->packet_context) { | |
291 | goto error_destroy; | |
292 | } | |
293 | } | |
294 | ||
e19cc57d | 295 | /* Initialize events_discarded */ |
12c8a1a3 JG |
296 | ret = set_structure_field_integer(stream->packet_context, |
297 | "events_discarded", 0); | |
298 | if (ret) { | |
299 | goto error_destroy; | |
300 | } | |
301 | ||
273b65be JG |
302 | stream->pos.fd = -1; |
303 | stream->id = stream_class->next_stream_id++; | |
304 | stream->stream_class = stream_class; | |
305 | bt_ctf_stream_class_get(stream_class); | |
273b65be | 306 | stream->events = g_ptr_array_new_with_free_func( |
123fbdec | 307 | (GDestroyNotify) put_event); |
8bfa3f9c JG |
308 | if (!stream->events) { |
309 | goto error_destroy; | |
310 | } | |
311 | if (stream_class->event_context_type) { | |
312 | stream->event_contexts = g_ptr_array_new_with_free_func( | |
313 | (GDestroyNotify) bt_ctf_field_put); | |
314 | if (!stream->event_contexts) { | |
315 | goto error_destroy; | |
316 | } | |
317 | } | |
d246b111 JG |
318 | |
319 | /* A trace is not allowed to have a NULL packet header */ | |
320 | assert(trace->packet_header_type); | |
321 | stream->packet_header = bt_ctf_field_create(trace->packet_header_type); | |
92351460 JG |
322 | if (!stream->packet_header) { |
323 | goto error_destroy; | |
324 | } | |
d246b111 JG |
325 | /* |
326 | * Attempt to populate the default trace packet header fields | |
327 | * (magic, uuid and stream_id). This will _not_ fail shall the | |
328 | * fields not be found or be of an incompatible type; they will | |
329 | * simply not be populated automatically. The user will have to | |
330 | * make sure to set the trace packet header fields himself before | |
331 | * flushing. | |
332 | */ | |
333 | ret = set_packet_header(stream); | |
334 | if (ret) { | |
335 | goto error_destroy; | |
336 | } | |
273b65be JG |
337 | end: |
338 | return stream; | |
12c8a1a3 JG |
339 | error_destroy: |
340 | bt_ctf_stream_destroy(&stream->ref_count); | |
341 | return NULL; | |
273b65be JG |
342 | } |
343 | ||
273b65be JG |
344 | BT_HIDDEN |
345 | int bt_ctf_stream_set_fd(struct bt_ctf_stream *stream, int fd) | |
346 | { | |
347 | int ret = 0; | |
348 | ||
349 | if (stream->pos.fd != -1) { | |
350 | ret = -1; | |
351 | goto end; | |
352 | } | |
353 | ||
354 | ctf_init_pos(&stream->pos, NULL, fd, O_RDWR); | |
355 | stream->pos.fd = fd; | |
356 | end: | |
357 | return ret; | |
358 | } | |
359 | ||
3baf0856 JG |
360 | struct bt_ctf_stream_class *bt_ctf_stream_get_class( |
361 | struct bt_ctf_stream *stream) | |
362 | { | |
363 | struct bt_ctf_stream_class *stream_class = NULL; | |
364 | ||
365 | if (!stream) { | |
366 | goto end; | |
367 | } | |
368 | ||
369 | stream_class = stream->stream_class; | |
370 | bt_ctf_stream_class_get(stream_class); | |
371 | end: | |
372 | return stream_class; | |
373 | } | |
374 | ||
a78a2e25 JG |
375 | int bt_ctf_stream_get_discarded_events_count( |
376 | struct bt_ctf_stream *stream, uint64_t *count) | |
377 | { | |
378 | int64_t ret = 0; | |
12c8a1a3 JG |
379 | int field_signed; |
380 | struct bt_ctf_field *events_discarded_field = NULL; | |
381 | struct bt_ctf_field_type *events_discarded_field_type = NULL; | |
a78a2e25 | 382 | |
12c8a1a3 | 383 | if (!stream || !count || !stream->packet_context) { |
a78a2e25 JG |
384 | ret = -1; |
385 | goto end; | |
386 | } | |
387 | ||
12c8a1a3 JG |
388 | events_discarded_field = bt_ctf_field_structure_get_field( |
389 | stream->packet_context, "events_discarded"); | |
390 | if (!events_discarded_field) { | |
391 | ret = -1; | |
392 | goto end; | |
393 | } | |
394 | ||
395 | events_discarded_field_type = bt_ctf_field_get_type( | |
396 | events_discarded_field); | |
397 | if (!events_discarded_field_type) { | |
398 | ret = -1; | |
399 | goto end; | |
400 | } | |
401 | ||
402 | field_signed = bt_ctf_field_type_integer_get_signed( | |
403 | events_discarded_field_type); | |
404 | if (field_signed < 0) { | |
405 | ret = field_signed; | |
406 | goto end; | |
407 | } | |
408 | ||
409 | if (field_signed) { | |
410 | int64_t signed_count; | |
411 | ||
412 | ret = bt_ctf_field_signed_integer_get_value( | |
413 | events_discarded_field, &signed_count); | |
414 | if (ret) { | |
415 | goto end; | |
416 | } | |
417 | if (signed_count < 0) { | |
418 | /* Invalid value */ | |
419 | ret = -1; | |
420 | goto end; | |
421 | } | |
422 | *count = (uint64_t) signed_count; | |
423 | } else { | |
424 | ret = bt_ctf_field_unsigned_integer_get_value( | |
425 | events_discarded_field, count); | |
426 | if (ret) { | |
427 | goto end; | |
428 | } | |
429 | } | |
a78a2e25 | 430 | end: |
12c8a1a3 JG |
431 | if (events_discarded_field) { |
432 | bt_ctf_field_put(events_discarded_field); | |
433 | } | |
434 | if (events_discarded_field_type) { | |
435 | bt_ctf_field_type_put(events_discarded_field_type); | |
436 | } | |
a78a2e25 JG |
437 | return ret; |
438 | } | |
439 | ||
273b65be JG |
440 | void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, |
441 | uint64_t event_count) | |
442 | { | |
12c8a1a3 JG |
443 | int ret; |
444 | int field_signed; | |
445 | uint64_t previous_count; | |
446 | uint64_t new_count; | |
447 | struct bt_ctf_field *events_discarded_field = NULL; | |
448 | struct bt_ctf_field_type *events_discarded_field_type = NULL; | |
449 | ||
450 | if (!stream || !stream->packet_context) { | |
451 | goto end; | |
452 | } | |
453 | ||
454 | ret = bt_ctf_stream_get_discarded_events_count(stream, | |
455 | &previous_count); | |
456 | if (ret) { | |
457 | goto end; | |
458 | } | |
459 | ||
460 | events_discarded_field = bt_ctf_field_structure_get_field( | |
461 | stream->packet_context, "events_discarded"); | |
462 | if (!events_discarded_field) { | |
463 | goto end; | |
464 | } | |
465 | ||
466 | events_discarded_field_type = bt_ctf_field_get_type( | |
467 | events_discarded_field); | |
468 | if (!events_discarded_field_type) { | |
469 | goto end; | |
470 | } | |
471 | ||
472 | field_signed = bt_ctf_field_type_integer_get_signed( | |
473 | events_discarded_field_type); | |
474 | if (field_signed < 0) { | |
475 | goto end; | |
273b65be JG |
476 | } |
477 | ||
12c8a1a3 JG |
478 | new_count = previous_count + event_count; |
479 | if (field_signed) { | |
480 | ret = bt_ctf_field_signed_integer_set_value( | |
481 | events_discarded_field, (int64_t) new_count); | |
482 | if (ret) { | |
483 | goto end; | |
484 | } | |
485 | } else { | |
486 | ret = bt_ctf_field_unsigned_integer_set_value( | |
487 | events_discarded_field, new_count); | |
488 | if (ret) { | |
489 | goto end; | |
490 | } | |
491 | } | |
492 | ||
493 | end: | |
494 | if (events_discarded_field) { | |
495 | bt_ctf_field_put(events_discarded_field); | |
496 | } | |
497 | if (events_discarded_field_type) { | |
498 | bt_ctf_field_type_put(events_discarded_field_type); | |
499 | } | |
273b65be JG |
500 | } |
501 | ||
502 | int bt_ctf_stream_append_event(struct bt_ctf_stream *stream, | |
503 | struct bt_ctf_event *event) | |
504 | { | |
505 | int ret = 0; | |
af181248 | 506 | struct bt_ctf_field *event_context_copy = NULL; |
273b65be JG |
507 | |
508 | if (!stream || !event) { | |
509 | ret = -1; | |
510 | goto end; | |
511 | } | |
512 | ||
123fbdec JG |
513 | ret = bt_ctf_event_set_stream(event, stream); |
514 | if (ret) { | |
515 | /* Event was already associated to a stream */ | |
516 | ret = -1; | |
517 | goto end; | |
518 | } | |
519 | ||
662e778c JG |
520 | ret = bt_ctf_event_populate_event_header(event); |
521 | if (ret) { | |
522 | goto end; | |
523 | } | |
524 | ||
785b5390 | 525 | /* Make sure the event's payload is set */ |
273b65be JG |
526 | ret = bt_ctf_event_validate(event); |
527 | if (ret) { | |
528 | goto end; | |
529 | } | |
530 | ||
8bfa3f9c JG |
531 | /* Sample the current stream event context by copying it */ |
532 | if (stream->event_context) { | |
533 | /* Make sure the event context's payload is set */ | |
534 | ret = bt_ctf_field_validate(stream->event_context); | |
535 | if (ret) { | |
536 | goto end; | |
537 | } | |
538 | ||
539 | event_context_copy = bt_ctf_field_copy(stream->event_context); | |
540 | if (!event_context_copy) { | |
541 | ret = -1; | |
542 | goto end; | |
543 | } | |
544 | } | |
545 | ||
273b65be | 546 | bt_ctf_event_get(event); |
8bfa3f9c | 547 | /* Save the new event along with its associated stream event context */ |
273b65be | 548 | g_ptr_array_add(stream->events, event); |
8bfa3f9c JG |
549 | if (event_context_copy) { |
550 | g_ptr_array_add(stream->event_contexts, event_context_copy); | |
551 | } | |
273b65be | 552 | end: |
123fbdec JG |
553 | if (ret) { |
554 | (void) bt_ctf_event_set_stream(event, NULL); | |
555 | } | |
273b65be JG |
556 | return ret; |
557 | } | |
558 | ||
12c8a1a3 JG |
559 | struct bt_ctf_field *bt_ctf_stream_get_packet_context( |
560 | struct bt_ctf_stream *stream) | |
561 | { | |
562 | struct bt_ctf_field *packet_context = NULL; | |
563 | ||
564 | if (!stream) { | |
565 | goto end; | |
566 | } | |
567 | ||
568 | packet_context = stream->packet_context; | |
12c8a1a3 JG |
569 | if (packet_context) { |
570 | bt_ctf_field_get(packet_context); | |
571 | } | |
34629a55 | 572 | end: |
12c8a1a3 JG |
573 | return packet_context; |
574 | } | |
575 | ||
576 | int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream, | |
577 | struct bt_ctf_field *field) | |
578 | { | |
579 | int ret = 0; | |
580 | struct bt_ctf_field_type *field_type; | |
581 | ||
582 | if (!stream || !field) { | |
583 | ret = -1; | |
584 | goto end; | |
585 | } | |
586 | ||
587 | field_type = bt_ctf_field_get_type(field); | |
588 | if (field_type != stream->stream_class->packet_context_type) { | |
589 | ret = -1; | |
590 | goto end; | |
591 | } | |
592 | ||
593 | bt_ctf_field_type_put(field_type); | |
594 | bt_ctf_field_get(field); | |
595 | bt_ctf_field_put(stream->packet_context); | |
596 | stream->packet_context = field; | |
597 | end: | |
598 | return ret; | |
599 | } | |
600 | ||
8bfa3f9c JG |
601 | struct bt_ctf_field *bt_ctf_stream_get_event_context( |
602 | struct bt_ctf_stream *stream) | |
603 | { | |
604 | struct bt_ctf_field *event_context = NULL; | |
605 | ||
606 | if (!stream) { | |
607 | goto end; | |
608 | } | |
609 | ||
610 | event_context = stream->event_context; | |
611 | if (event_context) { | |
612 | bt_ctf_field_get(event_context); | |
613 | } | |
614 | end: | |
615 | return event_context; | |
616 | } | |
617 | ||
618 | int bt_ctf_stream_set_event_context(struct bt_ctf_stream *stream, | |
619 | struct bt_ctf_field *field) | |
620 | { | |
621 | int ret = 0; | |
622 | struct bt_ctf_field_type *field_type = NULL; | |
623 | ||
624 | if (!stream || !field) { | |
625 | ret = -1; | |
626 | goto end; | |
627 | } | |
628 | ||
629 | field_type = bt_ctf_field_get_type(field); | |
630 | if (field_type != stream->stream_class->event_context_type) { | |
631 | ret = -1; | |
632 | goto end; | |
633 | } | |
634 | ||
635 | bt_ctf_field_get(field); | |
636 | bt_ctf_field_put(stream->event_context); | |
637 | stream->event_context = field; | |
638 | end: | |
639 | if (field_type) { | |
640 | bt_ctf_field_type_put(field_type); | |
641 | } | |
642 | return ret; | |
643 | } | |
644 | ||
263a7df5 JG |
645 | struct bt_ctf_field *bt_ctf_stream_get_packet_header( |
646 | struct bt_ctf_stream *stream) | |
647 | { | |
648 | struct bt_ctf_field *packet_header = NULL; | |
649 | ||
650 | if (!stream) { | |
651 | goto end; | |
652 | } | |
653 | ||
654 | packet_header = stream->packet_header; | |
655 | if (packet_header) { | |
656 | bt_ctf_field_get(packet_header); | |
657 | } | |
658 | end: | |
659 | return packet_header; | |
660 | } | |
661 | ||
662 | int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream, | |
663 | struct bt_ctf_field *field) | |
664 | { | |
665 | int ret = 0; | |
666 | struct bt_ctf_field_type *field_type = NULL; | |
667 | ||
668 | if (!stream || !field) { | |
669 | ret = -1; | |
670 | goto end; | |
671 | } | |
672 | ||
673 | field_type = bt_ctf_field_get_type(field); | |
674 | if (field_type != stream->trace->packet_header_type) { | |
675 | ret = -1; | |
676 | goto end; | |
677 | } | |
678 | ||
679 | bt_ctf_field_get(field); | |
680 | bt_ctf_field_put(stream->packet_header); | |
681 | stream->packet_header = field; | |
682 | end: | |
683 | if (field_type) { | |
684 | bt_ctf_field_type_put(field_type); | |
685 | } | |
686 | return ret; | |
687 | } | |
688 | ||
9a220c32 JG |
689 | static |
690 | int get_event_header_timestamp(struct bt_ctf_field *event_header, uint64_t *timestamp) | |
691 | { | |
692 | int ret = 0; | |
693 | struct bt_ctf_field *timestamp_field = NULL; | |
694 | struct bt_ctf_field_type *timestamp_field_type = NULL; | |
695 | ||
696 | timestamp_field = bt_ctf_field_structure_get_field(event_header, | |
697 | "timestamp"); | |
698 | if (!timestamp_field) { | |
699 | ret = -1; | |
700 | goto end; | |
701 | } | |
702 | ||
703 | timestamp_field_type = bt_ctf_field_get_type(timestamp_field); | |
704 | assert(timestamp_field_type); | |
705 | if (bt_ctf_field_type_get_type_id(timestamp_field_type) != | |
706 | CTF_TYPE_INTEGER) { | |
707 | ret = -1; | |
708 | goto end; | |
709 | } | |
710 | ||
711 | if (bt_ctf_field_type_integer_get_signed(timestamp_field_type)) { | |
712 | int64_t val; | |
713 | ||
714 | ret = bt_ctf_field_signed_integer_get_value(timestamp_field, | |
715 | &val); | |
716 | if (ret) { | |
717 | goto end; | |
718 | } | |
719 | *timestamp = (uint64_t) val; | |
720 | } else { | |
721 | ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field, | |
722 | timestamp); | |
723 | if (ret) { | |
724 | goto end; | |
725 | } | |
726 | } | |
727 | end: | |
728 | bt_ctf_field_put(timestamp_field); | |
729 | bt_ctf_field_type_put(timestamp_field_type); | |
730 | return ret; | |
731 | } | |
732 | ||
273b65be JG |
733 | int bt_ctf_stream_flush(struct bt_ctf_stream *stream) |
734 | { | |
735 | int ret = 0; | |
736 | size_t i; | |
12c8a1a3 | 737 | uint64_t timestamp_begin, timestamp_end, events_discarded; |
273b65be JG |
738 | struct bt_ctf_field *integer = NULL; |
739 | struct ctf_stream_pos packet_context_pos; | |
740 | ||
bc37ae52 JG |
741 | if (!stream || stream->pos.fd < 0) { |
742 | /* | |
743 | * Stream does not have an associated fd. It is, | |
744 | * therefore, not a stream being used to write events. | |
745 | */ | |
273b65be JG |
746 | ret = -1; |
747 | goto end; | |
748 | } | |
749 | ||
750 | if (!stream->events->len) { | |
751 | goto end; | |
752 | } | |
753 | ||
d246b111 JG |
754 | ret = bt_ctf_field_validate(stream->packet_header); |
755 | if (ret) { | |
756 | goto end; | |
757 | } | |
758 | ||
28362f2b JG |
759 | /* mmap the next packet */ |
760 | ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR); | |
d246b111 JG |
761 | |
762 | ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos); | |
763 | if (ret) { | |
764 | goto end; | |
273b65be JG |
765 | } |
766 | ||
12c8a1a3 | 767 | /* Set the default context attributes if present and unset. */ |
9a220c32 JG |
768 | if (!get_event_header_timestamp( |
769 | ((struct bt_ctf_event *) g_ptr_array_index( | |
770 | stream->events, 0))->event_header, ×tamp_begin)) { | |
771 | ret = set_structure_field_integer(stream->packet_context, | |
772 | "timestamp_begin", timestamp_begin); | |
773 | if (ret) { | |
774 | goto end; | |
775 | } | |
273b65be JG |
776 | } |
777 | ||
9a220c32 JG |
778 | if (!get_event_header_timestamp( |
779 | ((struct bt_ctf_event *) g_ptr_array_index( | |
780 | stream->events, stream->events->len - 1))->event_header, | |
781 | ×tamp_end)) { | |
273b65be | 782 | |
9a220c32 JG |
783 | ret = set_structure_field_integer(stream->packet_context, |
784 | "timestamp_end", timestamp_end); | |
785 | if (ret) { | |
786 | goto end; | |
787 | } | |
788 | } | |
12c8a1a3 | 789 | ret = set_structure_field_integer(stream->packet_context, |
273b65be JG |
790 | "content_size", UINT64_MAX); |
791 | if (ret) { | |
792 | goto end; | |
793 | } | |
794 | ||
12c8a1a3 | 795 | ret = set_structure_field_integer(stream->packet_context, |
273b65be JG |
796 | "packet_size", UINT64_MAX); |
797 | if (ret) { | |
798 | goto end; | |
799 | } | |
800 | ||
801 | /* Write packet context */ | |
802 | memcpy(&packet_context_pos, &stream->pos, | |
803 | sizeof(struct ctf_stream_pos)); | |
12c8a1a3 | 804 | ret = bt_ctf_field_serialize(stream->packet_context, |
273b65be JG |
805 | &stream->pos); |
806 | if (ret) { | |
807 | goto end; | |
808 | } | |
809 | ||
12c8a1a3 JG |
810 | ret = bt_ctf_stream_get_discarded_events_count(stream, |
811 | &events_discarded); | |
812 | if (ret) { | |
813 | goto end; | |
814 | } | |
815 | ||
816 | /* Unset the packet context's fields. */ | |
817 | ret = bt_ctf_field_reset(stream->packet_context); | |
818 | if (ret) { | |
819 | goto end; | |
820 | } | |
821 | ||
822 | /* Set the previous number of discarded events. */ | |
823 | ret = set_structure_field_integer(stream->packet_context, | |
824 | "events_discarded", events_discarded); | |
825 | if (ret) { | |
826 | goto end; | |
827 | } | |
828 | ||
273b65be JG |
829 | for (i = 0; i < stream->events->len; i++) { |
830 | struct bt_ctf_event *event = g_ptr_array_index( | |
831 | stream->events, i); | |
273b65be | 832 | |
662e778c | 833 | ret = bt_ctf_field_reset(event->event_header); |
12c8a1a3 JG |
834 | if (ret) { |
835 | goto end; | |
836 | } | |
837 | ||
273b65be | 838 | /* Write event header */ |
662e778c | 839 | ret = bt_ctf_field_serialize(event->event_header, |
273b65be JG |
840 | &stream->pos); |
841 | if (ret) { | |
842 | goto end; | |
843 | } | |
844 | ||
8bfa3f9c JG |
845 | /* Write stream event context */ |
846 | if (stream->event_contexts) { | |
847 | ret = bt_ctf_field_serialize( | |
848 | g_ptr_array_index(stream->event_contexts, i), | |
849 | &stream->pos); | |
850 | if (ret) { | |
851 | goto end; | |
852 | } | |
853 | } | |
854 | ||
273b65be JG |
855 | /* Write event content */ |
856 | ret = bt_ctf_event_serialize(event, &stream->pos); | |
857 | if (ret) { | |
858 | goto end; | |
859 | } | |
860 | } | |
861 | ||
862 | /* | |
863 | * Update the packet total size and content size and overwrite the | |
864 | * packet context. | |
3a092c05 JG |
865 | * Copy base_mma as the packet may have been remapped (e.g. when a |
866 | * packet is resized). | |
273b65be | 867 | */ |
3a092c05 | 868 | packet_context_pos.base_mma = stream->pos.base_mma; |
12c8a1a3 | 869 | ret = set_structure_field_integer(stream->packet_context, |
273b65be JG |
870 | "content_size", stream->pos.offset); |
871 | if (ret) { | |
872 | goto end; | |
873 | } | |
874 | ||
12c8a1a3 | 875 | ret = set_structure_field_integer(stream->packet_context, |
273b65be JG |
876 | "packet_size", stream->pos.packet_size); |
877 | if (ret) { | |
878 | goto end; | |
879 | } | |
880 | ||
12c8a1a3 | 881 | ret = bt_ctf_field_serialize(stream->packet_context, |
273b65be JG |
882 | &packet_context_pos); |
883 | if (ret) { | |
884 | goto end; | |
885 | } | |
886 | ||
887 | g_ptr_array_set_size(stream->events, 0); | |
8bfa3f9c JG |
888 | if (stream->event_contexts) { |
889 | g_ptr_array_set_size(stream->event_contexts, 0); | |
890 | } | |
273b65be JG |
891 | stream->flushed_packet_count++; |
892 | end: | |
893 | bt_ctf_field_put(integer); | |
894 | return ret; | |
895 | } | |
896 | ||
897 | void bt_ctf_stream_get(struct bt_ctf_stream *stream) | |
898 | { | |
899 | if (!stream) { | |
900 | return; | |
901 | } | |
902 | ||
903 | bt_ctf_ref_get(&stream->ref_count); | |
904 | } | |
905 | ||
906 | void bt_ctf_stream_put(struct bt_ctf_stream *stream) | |
907 | { | |
908 | if (!stream) { | |
909 | return; | |
910 | } | |
911 | ||
912 | bt_ctf_ref_put(&stream->ref_count, bt_ctf_stream_destroy); | |
913 | } | |
914 | ||
915 | static | |
916 | void bt_ctf_stream_destroy(struct bt_ctf_ref *ref) | |
917 | { | |
918 | struct bt_ctf_stream *stream; | |
919 | ||
920 | if (!ref) { | |
921 | return; | |
922 | } | |
923 | ||
924 | stream = container_of(ref, struct bt_ctf_stream, ref_count); | |
925 | ctf_fini_pos(&stream->pos); | |
0e45d308 | 926 | if (stream->pos.fd >= 0 && close(stream->pos.fd)) { |
9f56e450 JG |
927 | perror("close"); |
928 | } | |
12c8a1a3 JG |
929 | |
930 | if (stream->stream_class) { | |
931 | bt_ctf_stream_class_put(stream->stream_class); | |
932 | } | |
933 | if (stream->events) { | |
934 | g_ptr_array_free(stream->events, TRUE); | |
935 | } | |
8bfa3f9c JG |
936 | if (stream->event_contexts) { |
937 | g_ptr_array_free(stream->event_contexts, TRUE); | |
938 | } | |
d246b111 JG |
939 | if (stream->packet_header) { |
940 | bt_ctf_field_put(stream->packet_header); | |
941 | } | |
12c8a1a3 JG |
942 | if (stream->packet_context) { |
943 | bt_ctf_field_put(stream->packet_context); | |
944 | } | |
8bfa3f9c JG |
945 | if (stream->event_context) { |
946 | bt_ctf_field_put(stream->event_context); | |
947 | } | |
273b65be JG |
948 | g_free(stream); |
949 | } | |
950 | ||
273b65be JG |
951 | static |
952 | int set_structure_field_integer(struct bt_ctf_field *structure, char *name, | |
953 | uint64_t value) | |
954 | { | |
955 | int ret = 0; | |
d7b1ea66 | 956 | struct bt_ctf_field_type *field_type = NULL; |
273b65be JG |
957 | struct bt_ctf_field *integer = |
958 | bt_ctf_field_structure_get_field(structure, name); | |
12c8a1a3 JG |
959 | |
960 | if (!structure || !name) { | |
273b65be JG |
961 | ret = -1; |
962 | goto end; | |
963 | } | |
964 | ||
12c8a1a3 JG |
965 | if (!integer) { |
966 | /* Field not found, not an error. */ | |
967 | goto end; | |
968 | } | |
969 | ||
970 | /* Make sure the payload has not already been set. */ | |
971 | if (!bt_ctf_field_validate(integer)) { | |
972 | /* Payload already set, not an error */ | |
973 | goto end; | |
974 | } | |
975 | ||
d7b1ea66 JG |
976 | field_type = bt_ctf_field_get_type(integer); |
977 | /* Something is serioulsly wrong */ | |
978 | assert(field_type); | |
979 | if (bt_ctf_field_type_get_type_id(field_type) != CTF_TYPE_INTEGER) { | |
980 | /* | |
981 | * The user most likely meant for us to populate this field | |
982 | * automatically. However, we can only do this if the field | |
983 | * is an integer. Return an error. | |
984 | */ | |
985 | ret = -1; | |
986 | goto end; | |
987 | } | |
988 | ||
989 | if (bt_ctf_field_type_integer_get_signed(field_type)) { | |
990 | ret = bt_ctf_field_signed_integer_set_value(integer, | |
991 | (int64_t) value); | |
992 | } else { | |
993 | ret = bt_ctf_field_unsigned_integer_set_value(integer, value); | |
994 | } | |
273b65be | 995 | end: |
d7b1ea66 JG |
996 | if (integer) { |
997 | bt_ctf_field_put(integer); | |
998 | } | |
999 | if (field_type) { | |
1000 | bt_ctf_field_type_put(field_type); | |
1001 | } | |
273b65be JG |
1002 | return ret; |
1003 | } |