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