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