Commit | Line | Data |
---|---|---|
1c822dfb JG |
1 | /* |
2 | * stream.c | |
3 | * | |
4 | * Babeltrace CTF IR - Stream | |
5 | * | |
6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
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 | ||
29 | #include <babeltrace/ctf-ir/clock.h> | |
30 | #include <babeltrace/ctf-ir/clock-internal.h> | |
31 | #include <babeltrace/ctf-writer/event.h> | |
32 | #include <babeltrace/ctf-ir/event-internal.h> | |
33 | #include <babeltrace/ctf-ir/field-types-internal.h> | |
34 | #include <babeltrace/ctf-ir/fields-internal.h> | |
1c822dfb JG |
35 | #include <babeltrace/ctf-ir/stream-internal.h> |
36 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
37 | #include <babeltrace/ctf-ir/trace-internal.h> | |
38 | #include <babeltrace/ctf-writer/writer-internal.h> | |
39 | #include <babeltrace/ref.h> | |
40 | #include <babeltrace/ctf-writer/functor-internal.h> | |
41 | #include <babeltrace/compiler.h> | |
42 | #include <babeltrace/align.h> | |
43 | #include <babeltrace/ctf/ctf-index.h> | |
44 | ||
45 | static | |
46 | void bt_ctf_stream_destroy(struct bt_object *obj); | |
47 | static | |
48 | int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t); | |
49 | ||
50 | static | |
51 | int set_packet_header_magic(struct bt_ctf_stream *stream) | |
52 | { | |
53 | int ret = 0; | |
54 | struct bt_ctf_field_type *magic_field_type = NULL; | |
55 | struct bt_ctf_field *magic_field = bt_ctf_field_structure_get_field( | |
56 | stream->packet_header, "magic"); | |
57 | ||
58 | if (!magic_field) { | |
59 | /* No magic field found. Not an error, skip. */ | |
60 | goto end; | |
61 | } | |
62 | ||
63 | if (!bt_ctf_field_validate(magic_field)) { | |
64 | /* Value already set. Not an error, skip. */ | |
65 | goto end; | |
66 | } | |
67 | ||
68 | magic_field_type = bt_ctf_field_get_type(magic_field); | |
69 | assert(magic_field_type); | |
70 | ||
71 | if (bt_ctf_field_type_get_type_id(magic_field_type) != | |
830017b0 | 72 | CTF_TYPE_INTEGER) { |
1c822dfb JG |
73 | /* Magic field is not an integer. Not an error, skip. */ |
74 | goto end; | |
75 | } | |
76 | ||
77 | if (bt_ctf_field_type_integer_get_size(magic_field_type) != 32) { | |
78 | /* | |
79 | * Magic field is not of the expected size. | |
80 | * Not an error, skip. | |
81 | */ | |
82 | goto end; | |
83 | } | |
84 | ||
85 | ret = bt_ctf_field_type_integer_get_signed(magic_field_type); | |
86 | assert(ret >= 0); | |
87 | if (ret) { | |
88 | ret = bt_ctf_field_signed_integer_set_value(magic_field, | |
89 | (int64_t) 0xC1FC1FC1); | |
90 | } else { | |
91 | ret = bt_ctf_field_unsigned_integer_set_value(magic_field, | |
92 | (uint64_t) 0xC1FC1FC1); | |
93 | } | |
94 | end: | |
95 | bt_put(magic_field); | |
96 | bt_put(magic_field_type); | |
97 | return ret; | |
98 | } | |
99 | ||
100 | static | |
101 | int set_packet_header_uuid(struct bt_ctf_stream *stream) | |
102 | { | |
103 | int i, ret = 0; | |
104 | struct bt_ctf_trace *trace = NULL; | |
105 | struct bt_ctf_field_type *uuid_field_type = NULL; | |
106 | struct bt_ctf_field_type *element_field_type = NULL; | |
107 | struct bt_ctf_field *uuid_field = bt_ctf_field_structure_get_field( | |
108 | stream->packet_header, "uuid"); | |
109 | ||
110 | if (!uuid_field) { | |
111 | /* No uuid field found. Not an error, skip. */ | |
112 | goto end; | |
113 | } | |
114 | ||
115 | if (!bt_ctf_field_validate(uuid_field)) { | |
116 | /* Value already set. Not an error, skip. */ | |
117 | goto end; | |
118 | } | |
119 | ||
120 | uuid_field_type = bt_ctf_field_get_type(uuid_field); | |
121 | assert(uuid_field_type); | |
122 | if (bt_ctf_field_type_get_type_id(uuid_field_type) != | |
830017b0 | 123 | CTF_TYPE_ARRAY) { |
1c822dfb JG |
124 | /* UUID field is not an array. Not an error, skip. */ |
125 | goto end; | |
126 | } | |
127 | ||
128 | if (bt_ctf_field_type_array_get_length(uuid_field_type) != 16) { | |
129 | /* | |
130 | * UUID field is not of the expected size. | |
131 | * Not an error, skip. | |
132 | */ | |
133 | goto end; | |
134 | } | |
135 | ||
136 | element_field_type = bt_ctf_field_type_array_get_element_type( | |
137 | uuid_field_type); | |
138 | assert(element_field_type); | |
139 | if (bt_ctf_field_type_get_type_id(element_field_type) != | |
830017b0 | 140 | CTF_TYPE_INTEGER) { |
1c822dfb JG |
141 | /* UUID array elements are not integers. Not an error, skip */ |
142 | goto end; | |
143 | } | |
144 | ||
145 | trace = (struct bt_ctf_trace *) bt_object_get_parent(stream); | |
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) trace->uuid[i]); | |
156 | } else { | |
157 | ret = bt_ctf_field_unsigned_integer_set_value( | |
158 | uuid_element, | |
159 | (uint64_t) trace->uuid[i]); | |
160 | } | |
161 | bt_put(uuid_element); | |
162 | if (ret) { | |
163 | goto end; | |
164 | } | |
165 | } | |
166 | ||
167 | end: | |
168 | bt_put(uuid_field); | |
169 | bt_put(uuid_field_type); | |
170 | bt_put(element_field_type); | |
171 | BT_PUT(trace); | |
172 | return ret; | |
173 | } | |
174 | static | |
175 | int set_packet_header_stream_id(struct bt_ctf_stream *stream) | |
176 | { | |
177 | int ret = 0; | |
178 | uint32_t stream_id; | |
179 | struct bt_ctf_field_type *stream_id_field_type = NULL; | |
180 | struct bt_ctf_field *stream_id_field = bt_ctf_field_structure_get_field( | |
181 | stream->packet_header, "stream_id"); | |
182 | ||
183 | if (!stream_id_field) { | |
184 | /* No stream_id field found. Not an error, skip. */ | |
185 | goto end; | |
186 | } | |
187 | ||
188 | if (!bt_ctf_field_validate(stream_id_field)) { | |
189 | /* Value already set. Not an error, skip. */ | |
190 | goto end; | |
191 | } | |
192 | ||
193 | stream_id_field_type = bt_ctf_field_get_type(stream_id_field); | |
194 | assert(stream_id_field_type); | |
195 | if (bt_ctf_field_type_get_type_id(stream_id_field_type) != | |
830017b0 | 196 | CTF_TYPE_INTEGER) { |
1c822dfb JG |
197 | /* stream_id field is not an integer. Not an error, skip. */ |
198 | goto end; | |
199 | } | |
200 | ||
201 | stream_id = stream->stream_class->id; | |
202 | ret = bt_ctf_field_type_integer_get_signed(stream_id_field_type); | |
203 | assert(ret >= 0); | |
204 | if (ret) { | |
205 | ret = bt_ctf_field_signed_integer_set_value(stream_id_field, | |
206 | (int64_t) stream_id); | |
207 | } else { | |
208 | ret = bt_ctf_field_unsigned_integer_set_value(stream_id_field, | |
209 | (uint64_t) stream_id); | |
210 | } | |
211 | end: | |
212 | bt_put(stream_id_field); | |
213 | bt_put(stream_id_field_type); | |
214 | return ret; | |
215 | } | |
216 | ||
217 | static | |
218 | int set_packet_header(struct bt_ctf_stream *stream) | |
219 | { | |
220 | int ret; | |
221 | ||
222 | ret = set_packet_header_magic(stream); | |
223 | if (ret) { | |
224 | goto end; | |
225 | } | |
226 | ||
227 | ret = set_packet_header_uuid(stream); | |
228 | if (ret) { | |
229 | goto end; | |
230 | } | |
231 | ||
232 | ret = set_packet_header_stream_id(stream); | |
233 | if (ret) { | |
234 | goto end; | |
235 | } | |
236 | end: | |
237 | return ret; | |
238 | } | |
239 | ||
240 | static | |
241 | void release_event(struct bt_ctf_event *event) | |
242 | { | |
243 | if (bt_object_get_ref_count(event)) { | |
244 | /* | |
245 | * The event is being orphaned, but it must guarantee the | |
246 | * existence of its event class for the duration of its | |
247 | * lifetime. | |
248 | */ | |
249 | bt_get(event->event_class); | |
250 | BT_PUT(event->base.parent); | |
251 | } else { | |
252 | bt_object_release(event); | |
253 | } | |
254 | } | |
255 | ||
256 | static | |
257 | int create_stream_file(struct bt_ctf_writer *writer, | |
258 | struct bt_ctf_stream *stream) | |
259 | { | |
260 | int fd; | |
261 | GString *filename = g_string_new(stream->stream_class->name->str); | |
262 | ||
263 | if (stream->stream_class->name->len == 0) { | |
264 | int64_t ret; | |
265 | ||
266 | ret = bt_ctf_stream_class_get_id(stream->stream_class); | |
267 | if (ret < 0) { | |
268 | fd = -1; | |
269 | goto error; | |
270 | } | |
271 | ||
272 | g_string_printf(filename, "stream_%" PRId64, ret); | |
273 | } | |
274 | ||
275 | g_string_append_printf(filename, "_%" PRIu32, stream->id); | |
276 | fd = openat(writer->trace_dir_fd, filename->str, | |
277 | O_RDWR | O_CREAT | O_TRUNC, | |
278 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
279 | error: | |
280 | g_string_free(filename, TRUE); | |
281 | return fd; | |
282 | } | |
283 | ||
284 | static | |
285 | int set_stream_fd(struct bt_ctf_stream *stream, int fd) | |
286 | { | |
287 | int ret = 0; | |
288 | ||
289 | if (stream->pos.fd != -1) { | |
290 | ret = -1; | |
291 | goto end; | |
292 | } | |
293 | ||
294 | ctf_init_pos(&stream->pos, NULL, fd, O_RDWR); | |
295 | stream->pos.fd = fd; | |
296 | end: | |
297 | return ret; | |
298 | } | |
299 | ||
300 | struct bt_ctf_stream *bt_ctf_stream_create( | |
301 | struct bt_ctf_stream_class *stream_class, | |
302 | const char *name) | |
303 | { | |
304 | int ret; | |
305 | struct bt_ctf_stream *stream = NULL; | |
306 | struct bt_ctf_trace *trace = NULL; | |
307 | struct bt_ctf_writer *writer = NULL; | |
308 | ||
309 | if (!stream_class) { | |
310 | goto error; | |
311 | } | |
312 | ||
313 | trace = bt_ctf_stream_class_get_trace(stream_class); | |
314 | if (!trace) { | |
315 | goto error; | |
316 | } | |
317 | ||
318 | stream = g_new0(struct bt_ctf_stream, 1); | |
319 | if (!stream) { | |
320 | goto error; | |
321 | } | |
322 | ||
323 | bt_object_init(stream, bt_ctf_stream_destroy); | |
324 | /* | |
325 | * Acquire reference to parent since stream will become publicly | |
326 | * reachable; it needs its parent to remain valid. | |
327 | */ | |
328 | bt_object_set_parent(stream, trace); | |
329 | stream->id = stream_class->next_stream_id++; | |
330 | stream->stream_class = stream_class; | |
331 | stream->pos.fd = -1; | |
332 | ||
333 | if (name) { | |
334 | stream->name = g_string_new(name); | |
335 | if (!stream->name) { | |
336 | goto error; | |
337 | } | |
338 | } | |
339 | ||
340 | if (trace->is_created_by_writer) { | |
341 | int fd; | |
342 | writer = (struct bt_ctf_writer *) | |
343 | bt_object_get_parent(trace); | |
344 | ||
345 | assert(writer); | |
346 | stream->packet_context = bt_ctf_field_create( | |
347 | stream_class->packet_context_type); | |
348 | if (!stream->packet_context) { | |
349 | goto error; | |
350 | } | |
351 | ||
352 | /* Initialize events_discarded */ | |
353 | ret = set_structure_field_integer(stream->packet_context, | |
354 | "events_discarded", 0); | |
355 | if (ret) { | |
356 | goto error; | |
357 | } | |
358 | ||
359 | stream->events = g_ptr_array_new_with_free_func( | |
360 | (GDestroyNotify) release_event); | |
361 | if (!stream->events) { | |
362 | goto error; | |
363 | } | |
364 | ||
365 | /* A trace is not allowed to have a NULL packet header */ | |
366 | assert(trace->packet_header_type); | |
367 | stream->packet_header = | |
368 | bt_ctf_field_create(trace->packet_header_type); | |
369 | if (!stream->packet_header) { | |
370 | goto error; | |
371 | } | |
372 | ||
373 | /* | |
374 | * Attempt to populate the default trace packet header fields | |
375 | * (magic, uuid and stream_id). This will _not_ fail shall the | |
376 | * fields not be found or be of an incompatible type; they will | |
377 | * simply not be populated automatically. The user will have to | |
378 | * make sure to set the trace packet header fields himself | |
379 | * before flushing. | |
380 | */ | |
381 | ret = set_packet_header(stream); | |
382 | if (ret) { | |
383 | goto error; | |
384 | } | |
385 | ||
386 | /* Create file associated with this stream */ | |
387 | fd = create_stream_file(writer, stream); | |
388 | if (fd < 0) { | |
389 | goto error; | |
390 | } | |
391 | ||
392 | ret = set_stream_fd(stream, fd); | |
393 | if (ret) { | |
394 | goto error; | |
395 | } | |
396 | ||
397 | /* Freeze the writer */ | |
398 | bt_ctf_writer_freeze(writer); | |
399 | } else { | |
400 | /* Non-writer stream indicated by a negative FD */ | |
401 | ret = set_stream_fd(stream, -1); | |
402 | if (ret) { | |
403 | goto error; | |
404 | } | |
405 | ||
406 | stream->clock_values = g_hash_table_new_full(g_direct_hash, | |
407 | g_direct_equal, NULL, g_free); | |
408 | } | |
409 | ||
410 | /* Add this stream to the trace's streams */ | |
411 | g_ptr_array_add(trace->streams, stream); | |
412 | ||
413 | BT_PUT(trace); | |
414 | BT_PUT(writer); | |
415 | return stream; | |
416 | error: | |
417 | BT_PUT(stream); | |
418 | BT_PUT(trace); | |
419 | BT_PUT(writer); | |
420 | return stream; | |
421 | } | |
422 | ||
423 | struct bt_ctf_stream_class *bt_ctf_stream_get_class( | |
424 | struct bt_ctf_stream *stream) | |
425 | { | |
426 | struct bt_ctf_stream_class *stream_class = NULL; | |
427 | ||
428 | if (!stream) { | |
429 | goto end; | |
430 | } | |
431 | ||
432 | stream_class = stream->stream_class; | |
433 | bt_get(stream_class); | |
434 | end: | |
435 | return stream_class; | |
436 | } | |
437 | ||
438 | int bt_ctf_stream_get_discarded_events_count( | |
439 | struct bt_ctf_stream *stream, uint64_t *count) | |
440 | { | |
441 | int64_t ret = 0; | |
442 | int field_signed; | |
443 | struct bt_ctf_field *events_discarded_field = NULL; | |
444 | struct bt_ctf_field_type *events_discarded_field_type = NULL; | |
445 | ||
446 | if (!stream || !count || !stream->packet_context || | |
447 | stream->pos.fd < 0) { | |
448 | ret = -1; | |
449 | goto end; | |
450 | } | |
451 | ||
452 | events_discarded_field = bt_ctf_field_structure_get_field( | |
453 | stream->packet_context, "events_discarded"); | |
454 | if (!events_discarded_field) { | |
455 | ret = -1; | |
456 | goto end; | |
457 | } | |
458 | ||
459 | events_discarded_field_type = bt_ctf_field_get_type( | |
460 | events_discarded_field); | |
461 | if (!events_discarded_field_type) { | |
462 | ret = -1; | |
463 | goto end; | |
464 | } | |
465 | ||
466 | field_signed = bt_ctf_field_type_integer_get_signed( | |
467 | events_discarded_field_type); | |
468 | if (field_signed < 0) { | |
469 | ret = field_signed; | |
470 | goto end; | |
471 | } | |
472 | ||
473 | if (field_signed) { | |
474 | int64_t signed_count; | |
475 | ||
476 | ret = bt_ctf_field_signed_integer_get_value( | |
477 | events_discarded_field, &signed_count); | |
478 | if (ret) { | |
479 | goto end; | |
480 | } | |
481 | if (signed_count < 0) { | |
482 | /* Invalid value */ | |
483 | ret = -1; | |
484 | goto end; | |
485 | } | |
486 | *count = (uint64_t) signed_count; | |
487 | } else { | |
488 | ret = bt_ctf_field_unsigned_integer_get_value( | |
489 | events_discarded_field, count); | |
490 | if (ret) { | |
491 | goto end; | |
492 | } | |
493 | } | |
494 | end: | |
495 | bt_put(events_discarded_field); | |
496 | bt_put(events_discarded_field_type); | |
497 | return ret; | |
498 | } | |
499 | ||
500 | void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, | |
501 | uint64_t event_count) | |
502 | { | |
503 | int ret; | |
504 | int field_signed; | |
505 | uint64_t previous_count; | |
506 | uint64_t new_count; | |
507 | struct bt_ctf_field *events_discarded_field = NULL; | |
508 | struct bt_ctf_field_type *events_discarded_field_type = NULL; | |
509 | ||
510 | if (!stream || !stream->packet_context || stream->pos.fd < 0) { | |
511 | goto end; | |
512 | } | |
513 | ||
514 | ret = bt_ctf_stream_get_discarded_events_count(stream, | |
515 | &previous_count); | |
516 | if (ret) { | |
517 | goto end; | |
518 | } | |
519 | ||
520 | events_discarded_field = bt_ctf_field_structure_get_field( | |
521 | stream->packet_context, "events_discarded"); | |
522 | if (!events_discarded_field) { | |
523 | goto end; | |
524 | } | |
525 | ||
526 | events_discarded_field_type = bt_ctf_field_get_type( | |
527 | events_discarded_field); | |
528 | if (!events_discarded_field_type) { | |
529 | goto end; | |
530 | } | |
531 | ||
532 | field_signed = bt_ctf_field_type_integer_get_signed( | |
533 | events_discarded_field_type); | |
534 | if (field_signed < 0) { | |
535 | goto end; | |
536 | } | |
537 | ||
538 | new_count = previous_count + event_count; | |
539 | if (field_signed) { | |
540 | ret = bt_ctf_field_signed_integer_set_value( | |
541 | events_discarded_field, (int64_t) new_count); | |
542 | if (ret) { | |
543 | goto end; | |
544 | } | |
545 | } else { | |
546 | ret = bt_ctf_field_unsigned_integer_set_value( | |
547 | events_discarded_field, new_count); | |
548 | if (ret) { | |
549 | goto end; | |
550 | } | |
551 | } | |
552 | ||
553 | end: | |
554 | bt_put(events_discarded_field); | |
555 | bt_put(events_discarded_field_type); | |
556 | } | |
557 | ||
558 | int bt_ctf_stream_append_event(struct bt_ctf_stream *stream, | |
559 | struct bt_ctf_event *event) | |
560 | { | |
561 | int ret = 0; | |
562 | ||
563 | if (!stream || !event || stream->pos.fd < 0) { | |
564 | ret = -1; | |
565 | goto end; | |
566 | } | |
567 | ||
568 | /* | |
569 | * The event is not supposed to have a parent stream at this | |
570 | * point. The only other way an event can have a parent stream | |
571 | * is if it was assigned when setting a packet to the event, | |
572 | * in which case the packet's stream is not a writer stream, | |
573 | * and thus the user is trying to append an event which belongs | |
574 | * to another stream. | |
575 | */ | |
576 | if (event->base.parent) { | |
577 | ret = -1; | |
578 | goto end; | |
579 | } | |
580 | ||
581 | bt_object_set_parent(event, stream); | |
582 | ret = bt_ctf_event_populate_event_header(event); | |
583 | if (ret) { | |
584 | goto error; | |
585 | } | |
586 | ||
587 | /* Make sure the various scopes of the event are set */ | |
588 | ret = bt_ctf_event_validate(event); | |
589 | if (ret) { | |
590 | goto error; | |
591 | } | |
592 | ||
593 | /* Save the new event and freeze it */ | |
594 | bt_ctf_event_freeze(event); | |
595 | g_ptr_array_add(stream->events, event); | |
596 | ||
597 | /* | |
598 | * Event had to hold a reference to its event class as long as it wasn't | |
599 | * part of the same trace hierarchy. From now on, the event and its | |
600 | * class share the same lifetime guarantees and the reference is no | |
601 | * longer needed. | |
602 | */ | |
603 | bt_put(event->event_class); | |
604 | ||
605 | end: | |
606 | return ret; | |
607 | ||
608 | error: | |
609 | /* | |
610 | * Orphan the event; we were not successful in associating it to | |
611 | * a stream. | |
612 | */ | |
613 | bt_object_set_parent(event, NULL); | |
614 | ||
615 | return ret; | |
616 | } | |
617 | ||
618 | struct bt_ctf_field *bt_ctf_stream_get_packet_context( | |
619 | struct bt_ctf_stream *stream) | |
620 | { | |
621 | struct bt_ctf_field *packet_context = NULL; | |
622 | ||
623 | if (!stream || stream->pos.fd < 0) { | |
624 | goto end; | |
625 | } | |
626 | ||
627 | packet_context = stream->packet_context; | |
628 | if (packet_context) { | |
629 | bt_get(packet_context); | |
630 | } | |
631 | end: | |
632 | return packet_context; | |
633 | } | |
634 | ||
eea2626f | 635 | BT_HIDDEN |
1c822dfb JG |
636 | int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream, |
637 | struct bt_ctf_field *field) | |
638 | { | |
639 | int ret = 0; | |
640 | struct bt_ctf_field_type *field_type; | |
641 | ||
642 | if (!stream || !field || stream->pos.fd < 0) { | |
643 | ret = -1; | |
644 | goto end; | |
645 | } | |
646 | ||
647 | field_type = bt_ctf_field_get_type(field); | |
648 | if (bt_ctf_field_type_compare(field_type, | |
649 | stream->stream_class->packet_context_type)) { | |
650 | ret = -1; | |
651 | goto end; | |
652 | } | |
653 | ||
654 | bt_put(field_type); | |
655 | bt_get(field); | |
656 | bt_put(stream->packet_context); | |
657 | stream->packet_context = field; | |
658 | end: | |
659 | return ret; | |
660 | } | |
661 | ||
eea2626f | 662 | BT_HIDDEN |
1c822dfb JG |
663 | struct bt_ctf_field *bt_ctf_stream_get_packet_header( |
664 | struct bt_ctf_stream *stream) | |
665 | { | |
666 | struct bt_ctf_field *packet_header = NULL; | |
667 | ||
668 | if (!stream || stream->pos.fd < 0) { | |
669 | goto end; | |
670 | } | |
671 | ||
672 | packet_header = stream->packet_header; | |
673 | if (packet_header) { | |
674 | bt_get(packet_header); | |
675 | } | |
676 | end: | |
677 | return packet_header; | |
678 | } | |
679 | ||
eea2626f | 680 | BT_HIDDEN |
1c822dfb JG |
681 | int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream, |
682 | struct bt_ctf_field *field) | |
683 | { | |
684 | int ret = 0; | |
685 | struct bt_ctf_trace *trace = NULL; | |
686 | struct bt_ctf_field_type *field_type = NULL; | |
687 | ||
688 | if (!stream || !field || stream->pos.fd < 0) { | |
689 | ret = -1; | |
690 | goto end; | |
691 | } | |
692 | ||
693 | trace = (struct bt_ctf_trace *) bt_object_get_parent(stream); | |
694 | field_type = bt_ctf_field_get_type(field); | |
695 | if (bt_ctf_field_type_compare(field_type, trace->packet_header_type)) { | |
696 | ret = -1; | |
697 | goto end; | |
698 | } | |
699 | ||
700 | bt_get(field); | |
701 | bt_put(stream->packet_header); | |
702 | stream->packet_header = field; | |
703 | end: | |
704 | BT_PUT(trace); | |
705 | bt_put(field_type); | |
706 | return ret; | |
707 | } | |
708 | ||
709 | static | |
710 | int get_event_header_timestamp(struct bt_ctf_field *event_header, uint64_t *timestamp) | |
711 | { | |
712 | int ret = 0; | |
713 | struct bt_ctf_field *timestamp_field = NULL; | |
714 | struct bt_ctf_field_type *timestamp_field_type = NULL; | |
715 | ||
716 | timestamp_field = bt_ctf_field_structure_get_field(event_header, | |
717 | "timestamp"); | |
718 | if (!timestamp_field) { | |
719 | ret = -1; | |
720 | goto end; | |
721 | } | |
722 | ||
723 | timestamp_field_type = bt_ctf_field_get_type(timestamp_field); | |
724 | assert(timestamp_field_type); | |
725 | if (bt_ctf_field_type_get_type_id(timestamp_field_type) != | |
830017b0 | 726 | CTF_TYPE_INTEGER) { |
1c822dfb JG |
727 | ret = -1; |
728 | goto end; | |
729 | } | |
730 | ||
731 | if (bt_ctf_field_type_integer_get_signed(timestamp_field_type)) { | |
732 | int64_t val; | |
733 | ||
734 | ret = bt_ctf_field_signed_integer_get_value(timestamp_field, | |
735 | &val); | |
736 | if (ret) { | |
737 | goto end; | |
738 | } | |
739 | *timestamp = (uint64_t) val; | |
740 | } else { | |
741 | ret = bt_ctf_field_unsigned_integer_get_value(timestamp_field, | |
742 | timestamp); | |
743 | if (ret) { | |
744 | goto end; | |
745 | } | |
746 | } | |
747 | end: | |
748 | bt_put(timestamp_field); | |
749 | bt_put(timestamp_field_type); | |
750 | return ret; | |
751 | } | |
752 | ||
753 | int bt_ctf_stream_flush(struct bt_ctf_stream *stream) | |
754 | { | |
755 | int ret = 0; | |
756 | size_t i; | |
757 | uint64_t timestamp_begin, timestamp_end, events_discarded; | |
758 | struct bt_ctf_field *integer = NULL; | |
759 | struct ctf_stream_pos packet_context_pos; | |
760 | ||
761 | if (!stream || stream->pos.fd < 0) { | |
762 | /* | |
763 | * Stream does not have an associated fd. It is, | |
764 | * therefore, not a stream being used to write events. | |
765 | */ | |
766 | ret = -1; | |
767 | goto end; | |
768 | } | |
769 | ||
770 | if (!stream->events->len) { | |
771 | goto end; | |
772 | } | |
773 | ||
774 | ret = bt_ctf_field_validate(stream->packet_header); | |
775 | if (ret) { | |
776 | goto end; | |
777 | } | |
778 | ||
779 | /* mmap the next packet */ | |
780 | ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR); | |
781 | ||
782 | ret = bt_ctf_field_serialize(stream->packet_header, &stream->pos); | |
783 | if (ret) { | |
784 | goto end; | |
785 | } | |
786 | ||
787 | /* Set the default context attributes if present and unset. */ | |
788 | if (!get_event_header_timestamp( | |
789 | ((struct bt_ctf_event *) g_ptr_array_index( | |
790 | stream->events, 0))->event_header, ×tamp_begin)) { | |
791 | ret = set_structure_field_integer(stream->packet_context, | |
792 | "timestamp_begin", timestamp_begin); | |
793 | if (ret) { | |
794 | goto end; | |
795 | } | |
796 | } | |
797 | ||
798 | if (!get_event_header_timestamp( | |
799 | ((struct bt_ctf_event *) g_ptr_array_index( | |
800 | stream->events, stream->events->len - 1))->event_header, | |
801 | ×tamp_end)) { | |
802 | ||
803 | ret = set_structure_field_integer(stream->packet_context, | |
804 | "timestamp_end", timestamp_end); | |
805 | if (ret) { | |
806 | goto end; | |
807 | } | |
808 | } | |
809 | ret = set_structure_field_integer(stream->packet_context, | |
810 | "content_size", UINT64_MAX); | |
811 | if (ret) { | |
812 | goto end; | |
813 | } | |
814 | ||
815 | ret = set_structure_field_integer(stream->packet_context, | |
816 | "packet_size", UINT64_MAX); | |
817 | if (ret) { | |
818 | goto end; | |
819 | } | |
820 | ||
821 | /* Write packet context */ | |
822 | memcpy(&packet_context_pos, &stream->pos, | |
823 | sizeof(struct ctf_stream_pos)); | |
824 | ret = bt_ctf_field_serialize(stream->packet_context, | |
825 | &stream->pos); | |
826 | if (ret) { | |
827 | goto end; | |
828 | } | |
829 | ||
830 | ret = bt_ctf_stream_get_discarded_events_count(stream, | |
831 | &events_discarded); | |
832 | if (ret) { | |
833 | goto end; | |
834 | } | |
835 | ||
836 | /* Unset the packet context's fields. */ | |
837 | ret = bt_ctf_field_reset(stream->packet_context); | |
838 | if (ret) { | |
839 | goto end; | |
840 | } | |
841 | ||
842 | /* Set the previous number of discarded events. */ | |
843 | ret = set_structure_field_integer(stream->packet_context, | |
844 | "events_discarded", events_discarded); | |
845 | if (ret) { | |
846 | goto end; | |
847 | } | |
848 | ||
849 | for (i = 0; i < stream->events->len; i++) { | |
850 | struct bt_ctf_event *event = g_ptr_array_index( | |
851 | stream->events, i); | |
852 | ||
853 | ret = bt_ctf_field_reset(event->event_header); | |
854 | if (ret) { | |
855 | goto end; | |
856 | } | |
857 | ||
858 | /* Write event header */ | |
859 | ret = bt_ctf_field_serialize(event->event_header, | |
860 | &stream->pos); | |
861 | if (ret) { | |
862 | goto end; | |
863 | } | |
864 | ||
865 | /* Write stream event context */ | |
866 | if (event->stream_event_context) { | |
867 | ret = bt_ctf_field_serialize( | |
868 | event->stream_event_context, &stream->pos); | |
869 | if (ret) { | |
870 | goto end; | |
871 | } | |
872 | } | |
873 | ||
874 | /* Write event content */ | |
875 | ret = bt_ctf_event_serialize(event, &stream->pos); | |
876 | if (ret) { | |
877 | goto end; | |
878 | } | |
879 | } | |
880 | ||
881 | /* | |
882 | * Update the packet total size and content size and overwrite the | |
883 | * packet context. | |
884 | * Copy base_mma as the packet may have been remapped (e.g. when a | |
885 | * packet is resized). | |
886 | */ | |
887 | packet_context_pos.base_mma = stream->pos.base_mma; | |
888 | ret = set_structure_field_integer(stream->packet_context, | |
889 | "content_size", stream->pos.offset); | |
890 | if (ret) { | |
891 | goto end; | |
892 | } | |
893 | ||
894 | ret = set_structure_field_integer(stream->packet_context, | |
895 | "packet_size", stream->pos.packet_size); | |
896 | if (ret) { | |
897 | goto end; | |
898 | } | |
899 | ||
900 | ret = bt_ctf_field_serialize(stream->packet_context, | |
901 | &packet_context_pos); | |
902 | if (ret) { | |
903 | goto end; | |
904 | } | |
905 | ||
906 | g_ptr_array_set_size(stream->events, 0); | |
907 | stream->flushed_packet_count++; | |
908 | end: | |
909 | bt_put(integer); | |
910 | return ret; | |
911 | } | |
912 | ||
913 | void bt_ctf_stream_get(struct bt_ctf_stream *stream) | |
914 | { | |
915 | bt_get(stream); | |
916 | } | |
917 | ||
918 | void bt_ctf_stream_put(struct bt_ctf_stream *stream) | |
919 | { | |
920 | bt_put(stream); | |
921 | } | |
922 | ||
923 | static | |
924 | void bt_ctf_stream_destroy(struct bt_object *obj) | |
925 | { | |
926 | struct bt_ctf_stream *stream; | |
927 | ||
928 | stream = container_of(obj, struct bt_ctf_stream, base); | |
929 | ctf_fini_pos(&stream->pos); | |
930 | if (stream->pos.fd >= 0 && close(stream->pos.fd)) { | |
931 | perror("close"); | |
932 | } | |
933 | ||
934 | if (stream->events) { | |
935 | g_ptr_array_free(stream->events, TRUE); | |
936 | } | |
937 | ||
938 | if (stream->name) { | |
939 | g_string_free(stream->name, TRUE); | |
940 | } | |
941 | ||
942 | if (stream->clock_values) { | |
943 | g_hash_table_destroy(stream->clock_values); | |
944 | } | |
945 | ||
946 | bt_put(stream->packet_header); | |
947 | bt_put(stream->packet_context); | |
948 | g_free(stream); | |
949 | } | |
950 | ||
951 | static | |
952 | int set_structure_field_integer(struct bt_ctf_field *structure, char *name, | |
953 | uint64_t value) | |
954 | { | |
955 | int ret = 0; | |
956 | struct bt_ctf_field_type *field_type = NULL; | |
957 | struct bt_ctf_field *integer = | |
958 | bt_ctf_field_structure_get_field(structure, name); | |
959 | ||
960 | if (!structure || !name) { | |
961 | ret = -1; | |
962 | goto end; | |
963 | } | |
964 | ||
965 | if (!integer) { | |
966 | /* Field not found, not an error. */ | |
967 | goto end; | |
968 | } | |
969 | ||
970 | /* Make sure the payload has not already been set. */ | |
971 | if (!bt_ctf_field_validate(integer)) { | |
972 | /* Payload already set, not an error */ | |
973 | goto end; | |
974 | } | |
975 | ||
976 | field_type = bt_ctf_field_get_type(integer); | |
977 | /* Something is serioulsly wrong */ | |
978 | assert(field_type); | |
830017b0 | 979 | if (bt_ctf_field_type_get_type_id(field_type) != CTF_TYPE_INTEGER) { |
1c822dfb JG |
980 | /* |
981 | * The user most likely meant for us to populate this field | |
982 | * automatically. However, we can only do this if the field | |
983 | * is an integer. Return an error. | |
984 | */ | |
985 | ret = -1; | |
986 | goto end; | |
987 | } | |
988 | ||
989 | if (bt_ctf_field_type_integer_get_signed(field_type)) { | |
990 | ret = bt_ctf_field_signed_integer_set_value(integer, | |
991 | (int64_t) value); | |
992 | } else { | |
993 | ret = bt_ctf_field_unsigned_integer_set_value(integer, value); | |
994 | } | |
995 | end: | |
996 | bt_put(integer); | |
997 | bt_put(field_type); | |
998 | return ret; | |
999 | } | |
1000 | ||
eea2626f | 1001 | BT_HIDDEN |
1c822dfb JG |
1002 | const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream) |
1003 | { | |
1004 | const char *name = NULL; | |
1005 | ||
1006 | if (!stream) { | |
1007 | goto end; | |
1008 | } | |
1009 | ||
1010 | name = stream->name ? stream->name->str : NULL; | |
1011 | ||
1012 | end: | |
1013 | return name; | |
1014 | } | |
1015 | ||
1016 | BT_HIDDEN | |
1017 | void bt_ctf_stream_update_clock_value(struct bt_ctf_stream *stream, | |
1018 | struct bt_ctf_field *value_field) | |
1019 | { | |
1020 | struct bt_ctf_field_type *value_type = NULL; | |
1021 | struct bt_ctf_clock *clock = NULL; | |
1022 | uint64_t requested_new_value; | |
1023 | uint64_t requested_new_value_mask; | |
1024 | uint64_t *cur_value; | |
1025 | uint64_t cur_value_masked; | |
1026 | int requested_new_value_size; | |
1027 | int ret; | |
1028 | ||
1029 | assert(stream); | |
1030 | assert(clock); | |
1031 | assert(value_field); | |
1032 | value_type = bt_ctf_field_get_type(value_field); | |
1033 | assert(value_type); | |
1034 | clock = bt_ctf_field_type_integer_get_mapped_clock(value_type); | |
1035 | assert(clock); | |
1036 | requested_new_value_size = | |
1037 | bt_ctf_field_type_integer_get_size(value_type); | |
1038 | assert(requested_new_value_size > 0); | |
1039 | ret = bt_ctf_field_unsigned_integer_get_value(value_field, | |
1040 | &requested_new_value); | |
1041 | assert(!ret); | |
1042 | cur_value = g_hash_table_lookup(stream->clock_values, clock); | |
1043 | ||
1044 | if (!cur_value) { | |
1045 | /* | |
1046 | * Updating the value of a clock which is not registered | |
1047 | * yet, so register it with the new value as its initial | |
1048 | * value. | |
1049 | */ | |
1050 | uint64_t *requested_new_value_ptr = g_new0(uint64_t, 1); | |
1051 | ||
1052 | *requested_new_value_ptr = requested_new_value; | |
1053 | g_hash_table_insert(stream->clock_values, clock, | |
1054 | requested_new_value_ptr); | |
1055 | goto end; | |
1056 | } | |
1057 | ||
1058 | /* | |
1059 | * Special case for a 64-bit new value, which is the limit | |
1060 | * of a clock value as of this version: overwrite the | |
1061 | * current value directly. | |
1062 | */ | |
1063 | if (requested_new_value_size == 64) { | |
1064 | *cur_value = requested_new_value; | |
1065 | goto end; | |
1066 | } | |
1067 | ||
1068 | requested_new_value_mask = (1ULL << requested_new_value_size) - 1; | |
1069 | cur_value_masked = *cur_value & requested_new_value_mask; | |
1070 | ||
1071 | if (requested_new_value < cur_value_masked) { | |
1072 | /* | |
1073 | * It looks like a wrap happened on the number of bits | |
1074 | * of the requested new value. Assume that the clock | |
1075 | * value wrapped only one time. | |
1076 | */ | |
1077 | *cur_value += requested_new_value_mask + 1; | |
1078 | } | |
1079 | ||
1080 | /* Clear the low bits of the current clock value */ | |
1081 | *cur_value &= ~requested_new_value_mask; | |
1082 | ||
1083 | /* Set the low bits of the current clock value */ | |
1084 | *cur_value |= requested_new_value; | |
1085 | ||
1086 | end: | |
1087 | bt_put(clock); | |
1088 | bt_put(value_type); | |
1089 | } |