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 | ||
0d688c15 PP |
498 | /* Save the new event and freeze it */ |
499 | bt_ctf_event_freeze(event); | |
273b65be | 500 | g_ptr_array_add(stream->events, event); |
5fd2e9fd | 501 | |
e6a8e8e4 JG |
502 | /* |
503 | * Event had to hold a reference to its event class as long as it wasn't | |
504 | * part of the same trace hierarchy. From now on, the event and its | |
505 | * class share the same lifetime guarantees and the reference is no | |
506 | * longer needed. | |
507 | */ | |
508 | bt_put(event->event_class); | |
273b65be | 509 | end: |
123fbdec | 510 | if (ret) { |
e6a8e8e4 JG |
511 | /* |
512 | * Orphan the event; we were not succesful in associating it to | |
513 | * a stream. | |
514 | */ | |
515 | bt_object_set_parent(event, NULL); | |
123fbdec | 516 | } |
273b65be JG |
517 | return ret; |
518 | } | |
519 | ||
12c8a1a3 JG |
520 | struct bt_ctf_field *bt_ctf_stream_get_packet_context( |
521 | struct bt_ctf_stream *stream) | |
522 | { | |
523 | struct bt_ctf_field *packet_context = NULL; | |
524 | ||
525 | if (!stream) { | |
526 | goto end; | |
527 | } | |
528 | ||
529 | packet_context = stream->packet_context; | |
12c8a1a3 | 530 | if (packet_context) { |
83509119 | 531 | bt_get(packet_context); |
12c8a1a3 | 532 | } |
34629a55 | 533 | end: |
12c8a1a3 JG |
534 | return packet_context; |
535 | } | |
536 | ||
537 | int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream, | |
538 | struct bt_ctf_field *field) | |
539 | { | |
540 | int ret = 0; | |
541 | struct bt_ctf_field_type *field_type; | |
542 | ||
543 | if (!stream || !field) { | |
544 | ret = -1; | |
545 | goto end; | |
546 | } | |
547 | ||
548 | field_type = bt_ctf_field_get_type(field); | |
09840de5 PP |
549 | if (bt_ctf_field_type_compare(field_type, |
550 | stream->stream_class->packet_context_type)) { | |
12c8a1a3 JG |
551 | ret = -1; |
552 | goto end; | |
553 | } | |
554 | ||
83509119 JG |
555 | bt_put(field_type); |
556 | bt_get(field); | |
557 | bt_put(stream->packet_context); | |
12c8a1a3 JG |
558 | stream->packet_context = field; |
559 | end: | |
560 | return ret; | |
561 | } | |
562 | ||
263a7df5 JG |
563 | struct bt_ctf_field *bt_ctf_stream_get_packet_header( |
564 | struct bt_ctf_stream *stream) | |
565 | { | |
566 | struct bt_ctf_field *packet_header = NULL; | |
567 | ||
568 | if (!stream) { | |
569 | goto end; | |
570 | } | |
571 | ||
572 | packet_header = stream->packet_header; | |
573 | if (packet_header) { | |
83509119 | 574 | bt_get(packet_header); |
263a7df5 JG |
575 | } |
576 | end: | |
577 | return packet_header; | |
578 | } | |
579 | ||
580 | int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream, | |
581 | struct bt_ctf_field *field) | |
582 | { | |
583 | int ret = 0; | |
e6a8e8e4 | 584 | struct bt_ctf_trace *trace = NULL; |
263a7df5 JG |
585 | struct bt_ctf_field_type *field_type = NULL; |
586 | ||
587 | if (!stream || !field) { | |
588 | ret = -1; | |
589 | goto end; | |
590 | } | |
591 | ||
e6a8e8e4 | 592 | trace = (struct bt_ctf_trace *) bt_object_get_parent(stream); |
263a7df5 | 593 | field_type = bt_ctf_field_get_type(field); |
09840de5 | 594 | if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) { |
263a7df5 JG |
595 | ret = -1; |
596 | goto end; | |
597 | } | |
598 | ||
83509119 JG |
599 | bt_get(field); |
600 | bt_put(stream->packet_header); | |
263a7df5 JG |
601 | stream->packet_header = field; |
602 | end: | |
e6a8e8e4 | 603 | BT_PUT(trace); |
83509119 | 604 | bt_put(field_type); |
263a7df5 JG |
605 | return ret; |
606 | } | |
607 | ||
9a220c32 JG |
608 | static |
609 | int get_event_header_timestamp(struct bt_ctf_field *event_header, uint64_t *timestamp) | |
610 | { | |
611 | int ret = 0; | |
612 | struct bt_ctf_field *timestamp_field = NULL; | |
613 | struct bt_ctf_field_type *timestamp_field_type = NULL; | |
614 | ||
615 | timestamp_field = bt_ctf_field_structure_get_field(event_header, | |
616 | "timestamp"); | |
617 | if (!timestamp_field) { | |
618 | ret = -1; | |
619 | goto end; | |
620 | } | |
621 | ||
622 | timestamp_field_type = bt_ctf_field_get_type(timestamp_field); | |
623 | assert(timestamp_field_type); | |
624 | if (bt_ctf_field_type_get_type_id(timestamp_field_type) != | |
9a19a512 | 625 | BT_CTF_TYPE_ID_INTEGER) { |
9a220c32 JG |
626 | ret = -1; |
627 | goto end; | |
628 | } | |
629 | ||
630 | if (bt_ctf_field_type_integer_get_signed(timestamp_field_type)) { | |
631 | int64_t val; | |
632 | ||
633 | ret = bt_ctf_field_signed_integer_get_value(timestamp_field, | |
634 | &val); | |
635 | if (ret) { | |
636 | goto end; | |
637 | } | |
638 | *timestamp = (uint64_t) val; | |
639 | } else { | |
640 | ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field, | |
641 | timestamp); | |
642 | if (ret) { | |
643 | goto end; | |
644 | } | |
645 | } | |
646 | end: | |
83509119 JG |
647 | bt_put(timestamp_field); |
648 | bt_put(timestamp_field_type); | |
9a220c32 JG |
649 | return ret; |
650 | } | |
651 | ||
273b65be JG |
652 | int bt_ctf_stream_flush(struct bt_ctf_stream *stream) |
653 | { | |
654 | int ret = 0; | |
655 | size_t i; | |
12c8a1a3 | 656 | uint64_t timestamp_begin, timestamp_end, events_discarded; |
273b65be JG |
657 | struct bt_ctf_field *integer = NULL; |
658 | struct ctf_stream_pos packet_context_pos; | |
659 | ||
bc37ae52 JG |
660 | if (!stream || stream->pos.fd < 0) { |
661 | /* | |
662 | * Stream does not have an associated fd. It is, | |
663 | * therefore, not a stream being used to write events. | |
664 | */ | |
273b65be JG |
665 | ret = -1; |
666 | goto end; | |
667 | } | |
668 | ||
669 | if (!stream->events->len) { | |
670 | goto end; | |
671 | } | |
672 | ||
d246b111 JG |
673 | ret = bt_ctf_field_validate(stream->packet_header); |
674 | if (ret) { | |
675 | goto end; | |
676 | } | |
677 | ||
28362f2b JG |
678 | /* mmap the next packet */ |
679 | ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR); | |
d246b111 JG |
680 | |
681 | ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos); | |
682 | if (ret) { | |
683 | goto end; | |
273b65be JG |
684 | } |
685 | ||
12c8a1a3 | 686 | /* Set the default context attributes if present and unset. */ |
9a220c32 JG |
687 | if (!get_event_header_timestamp( |
688 | ((struct bt_ctf_event *) g_ptr_array_index( | |
689 | stream->events, 0))->event_header, ×tamp_begin)) { | |
690 | ret = set_structure_field_integer(stream->packet_context, | |
691 | "timestamp_begin", timestamp_begin); | |
692 | if (ret) { | |
693 | goto end; | |
694 | } | |
273b65be JG |
695 | } |
696 | ||
9a220c32 JG |
697 | if (!get_event_header_timestamp( |
698 | ((struct bt_ctf_event *) g_ptr_array_index( | |
699 | stream->events, stream->events->len - 1))->event_header, | |
700 | ×tamp_end)) { | |
273b65be | 701 | |
9a220c32 JG |
702 | ret = set_structure_field_integer(stream->packet_context, |
703 | "timestamp_end", timestamp_end); | |
704 | if (ret) { | |
705 | goto end; | |
706 | } | |
707 | } | |
12c8a1a3 | 708 | ret = set_structure_field_integer(stream->packet_context, |
273b65be JG |
709 | "content_size", UINT64_MAX); |
710 | if (ret) { | |
711 | goto end; | |
712 | } | |
713 | ||
12c8a1a3 | 714 | ret = set_structure_field_integer(stream->packet_context, |
273b65be JG |
715 | "packet_size", UINT64_MAX); |
716 | if (ret) { | |
717 | goto end; | |
718 | } | |
719 | ||
720 | /* Write packet context */ | |
721 | memcpy(&packet_context_pos, &stream->pos, | |
722 | sizeof(struct ctf_stream_pos)); | |
12c8a1a3 | 723 | ret = bt_ctf_field_serialize(stream->packet_context, |
273b65be JG |
724 | &stream->pos); |
725 | if (ret) { | |
726 | goto end; | |
727 | } | |
728 | ||
12c8a1a3 JG |
729 | ret = bt_ctf_stream_get_discarded_events_count(stream, |
730 | &events_discarded); | |
731 | if (ret) { | |
732 | goto end; | |
733 | } | |
734 | ||
735 | /* Unset the packet context's fields. */ | |
736 | ret = bt_ctf_field_reset(stream->packet_context); | |
737 | if (ret) { | |
738 | goto end; | |
739 | } | |
740 | ||
741 | /* Set the previous number of discarded events. */ | |
742 | ret = set_structure_field_integer(stream->packet_context, | |
743 | "events_discarded", events_discarded); | |
744 | if (ret) { | |
745 | goto end; | |
746 | } | |
747 | ||
273b65be JG |
748 | for (i = 0; i < stream->events->len; i++) { |
749 | struct bt_ctf_event *event = g_ptr_array_index( | |
750 | stream->events, i); | |
273b65be | 751 | |
662e778c | 752 | ret = bt_ctf_field_reset(event->event_header); |
12c8a1a3 JG |
753 | if (ret) { |
754 | goto end; | |
755 | } | |
756 | ||
273b65be | 757 | /* Write event header */ |
662e778c | 758 | ret = bt_ctf_field_serialize(event->event_header, |
273b65be JG |
759 | &stream->pos); |
760 | if (ret) { | |
761 | goto end; | |
762 | } | |
763 | ||
8bfa3f9c | 764 | /* Write stream event context */ |
5fd2e9fd | 765 | if (event->stream_event_context) { |
8bfa3f9c | 766 | ret = bt_ctf_field_serialize( |
5fd2e9fd | 767 | event->stream_event_context, &stream->pos); |
8bfa3f9c JG |
768 | if (ret) { |
769 | goto end; | |
770 | } | |
771 | } | |
772 | ||
273b65be JG |
773 | /* Write event content */ |
774 | ret = bt_ctf_event_serialize(event, &stream->pos); | |
775 | if (ret) { | |
776 | goto end; | |
777 | } | |
778 | } | |
779 | ||
780 | /* | |
781 | * Update the packet total size and content size and overwrite the | |
782 | * packet context. | |
3a092c05 JG |
783 | * Copy base_mma as the packet may have been remapped (e.g. when a |
784 | * packet is resized). | |
273b65be | 785 | */ |
3a092c05 | 786 | packet_context_pos.base_mma = stream->pos.base_mma; |
12c8a1a3 | 787 | ret = set_structure_field_integer(stream->packet_context, |
273b65be JG |
788 | "content_size", stream->pos.offset); |
789 | if (ret) { | |
790 | goto end; | |
791 | } | |
792 | ||
12c8a1a3 | 793 | ret = set_structure_field_integer(stream->packet_context, |
273b65be JG |
794 | "packet_size", stream->pos.packet_size); |
795 | if (ret) { | |
796 | goto end; | |
797 | } | |
798 | ||
12c8a1a3 | 799 | ret = bt_ctf_field_serialize(stream->packet_context, |
273b65be JG |
800 | &packet_context_pos); |
801 | if (ret) { | |
802 | goto end; | |
803 | } | |
804 | ||
805 | g_ptr_array_set_size(stream->events, 0); | |
806 | stream->flushed_packet_count++; | |
807 | end: | |
83509119 | 808 | bt_put(integer); |
273b65be JG |
809 | return ret; |
810 | } | |
811 | ||
812 | void bt_ctf_stream_get(struct bt_ctf_stream *stream) | |
813 | { | |
83509119 | 814 | bt_get(stream); |
273b65be JG |
815 | } |
816 | ||
817 | void bt_ctf_stream_put(struct bt_ctf_stream *stream) | |
818 | { | |
83509119 | 819 | bt_put(stream); |
273b65be JG |
820 | } |
821 | ||
822 | static | |
83509119 | 823 | void bt_ctf_stream_destroy(struct bt_object *obj) |
273b65be JG |
824 | { |
825 | struct bt_ctf_stream *stream; | |
273b65be | 826 | |
83509119 | 827 | stream = container_of(obj, struct bt_ctf_stream, base); |
273b65be | 828 | ctf_fini_pos(&stream->pos); |
0e45d308 | 829 | if (stream->pos.fd >= 0 && close(stream->pos.fd)) { |
9f56e450 JG |
830 | perror("close"); |
831 | } | |
12c8a1a3 | 832 | |
12c8a1a3 JG |
833 | if (stream->events) { |
834 | g_ptr_array_free(stream->events, TRUE); | |
835 | } | |
83509119 JG |
836 | bt_put(stream->packet_header); |
837 | bt_put(stream->packet_context); | |
838 | bt_put(stream->event_context); | |
273b65be JG |
839 | g_free(stream); |
840 | } | |
841 | ||
273b65be JG |
842 | static |
843 | int set_structure_field_integer(struct bt_ctf_field *structure, char *name, | |
844 | uint64_t value) | |
845 | { | |
846 | int ret = 0; | |
d7b1ea66 | 847 | struct bt_ctf_field_type *field_type = NULL; |
273b65be JG |
848 | struct bt_ctf_field *integer = |
849 | bt_ctf_field_structure_get_field(structure, name); | |
12c8a1a3 JG |
850 | |
851 | if (!structure || !name) { | |
273b65be JG |
852 | ret = -1; |
853 | goto end; | |
854 | } | |
855 | ||
12c8a1a3 JG |
856 | if (!integer) { |
857 | /* Field not found, not an error. */ | |
858 | goto end; | |
859 | } | |
860 | ||
861 | /* Make sure the payload has not already been set. */ | |
862 | if (!bt_ctf_field_validate(integer)) { | |
863 | /* Payload already set, not an error */ | |
864 | goto end; | |
865 | } | |
866 | ||
d7b1ea66 JG |
867 | field_type = bt_ctf_field_get_type(integer); |
868 | /* Something is serioulsly wrong */ | |
869 | assert(field_type); | |
9a19a512 | 870 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_TYPE_ID_INTEGER) { |
d7b1ea66 JG |
871 | /* |
872 | * The user most likely meant for us to populate this field | |
873 | * automatically. However, we can only do this if the field | |
874 | * is an integer. Return an error. | |
875 | */ | |
876 | ret = -1; | |
877 | goto end; | |
878 | } | |
879 | ||
880 | if (bt_ctf_field_type_integer_get_signed(field_type)) { | |
881 | ret = bt_ctf_field_signed_integer_set_value(integer, | |
882 | (int64_t) value); | |
883 | } else { | |
884 | ret = bt_ctf_field_unsigned_integer_set_value(integer, value); | |
885 | } | |
273b65be | 886 | end: |
83509119 JG |
887 | bt_put(integer); |
888 | bt_put(field_type); | |
273b65be JG |
889 | return ret; |
890 | } |