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 | ||
19abc2c6 PP |
29 | #define BT_LOG_TAG "STREAM" |
30 | #include <babeltrace/lib-logging-internal.h> | |
31 | ||
ac0c6bdd PP |
32 | #include <babeltrace/ctf-ir/clock-class.h> |
33 | #include <babeltrace/ctf-writer/clock.h> | |
34 | #include <babeltrace/ctf-writer/clock-internal.h> | |
273b65be | 35 | #include <babeltrace/ctf-writer/event.h> |
adc315b8 | 36 | #include <babeltrace/ctf-ir/event-internal.h> |
2e33ac5a PP |
37 | #include <babeltrace/ctf-ir/field-types-internal.h> |
38 | #include <babeltrace/ctf-ir/fields-internal.h> | |
3f043b05 JG |
39 | #include <babeltrace/ctf-ir/stream.h> |
40 | #include <babeltrace/ctf-ir/stream-internal.h> | |
adc315b8 | 41 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
5acf2ae6 | 42 | #include <babeltrace/ctf-ir/trace.h> |
319fd969 PP |
43 | #include <babeltrace/ctf-ir/trace-internal.h> |
44 | #include <babeltrace/ctf-writer/writer-internal.h> | |
3230ee6b | 45 | #include <babeltrace/graph/component-internal.h> |
83509119 | 46 | #include <babeltrace/ref.h> |
273b65be | 47 | #include <babeltrace/ctf-writer/functor-internal.h> |
3d9990ac PP |
48 | #include <babeltrace/compiler-internal.h> |
49 | #include <babeltrace/align-internal.h> | |
dc3fffef | 50 | #include <inttypes.h> |
95076212 | 51 | #include <unistd.h> |
273b65be JG |
52 | |
53 | static | |
50842bdc | 54 | void bt_stream_destroy(struct bt_object *obj); |
273b65be | 55 | static |
50842bdc | 56 | int try_set_structure_field_integer(struct bt_field *, char *, uint64_t); |
273b65be | 57 | |
ac0c6bdd | 58 | static |
50842bdc | 59 | int set_integer_field_value(struct bt_field* field, uint64_t value) |
ac0c6bdd PP |
60 | { |
61 | int ret = 0; | |
50842bdc | 62 | struct bt_field_type *field_type = NULL; |
ac0c6bdd PP |
63 | |
64 | if (!field) { | |
19abc2c6 | 65 | BT_LOGW_STR("Invalid parameter: field is NULL."); |
ac0c6bdd PP |
66 | ret = -1; |
67 | goto end; | |
68 | } | |
69 | ||
50842bdc | 70 | field_type = bt_field_get_type(field); |
ac0c6bdd PP |
71 | assert(field_type); |
72 | ||
50842bdc PP |
73 | if (bt_field_type_get_type_id(field_type) != |
74 | BT_FIELD_TYPE_ID_INTEGER) { | |
ac0c6bdd | 75 | /* Not an integer and the value is unset, error. */ |
19abc2c6 PP |
76 | BT_LOGW("Invalid parameter: field's type is not an integer field type: " |
77 | "field-addr=%p, ft-addr=%p, ft-id=%s", | |
78 | field, field_type, | |
50842bdc | 79 | bt_field_type_id_string(field_type->id)); |
ac0c6bdd PP |
80 | ret = -1; |
81 | goto end; | |
82 | } | |
83 | ||
50842bdc PP |
84 | if (bt_field_type_integer_is_signed(field_type)) { |
85 | ret = bt_field_signed_integer_set_value(field, (int64_t) value); | |
ac0c6bdd PP |
86 | if (ret) { |
87 | /* Value is out of range, error. */ | |
19abc2c6 PP |
88 | BT_LOGW("Cannot set signed integer field's value: " |
89 | "addr=%p, value=%" PRId64, | |
90 | field, (int64_t) value); | |
ac0c6bdd PP |
91 | goto end; |
92 | } | |
93 | } else { | |
50842bdc | 94 | ret = bt_field_unsigned_integer_set_value(field, value); |
ac0c6bdd PP |
95 | if (ret) { |
96 | /* Value is out of range, error. */ | |
19abc2c6 PP |
97 | BT_LOGW("Cannot set unsigned integer field's value: " |
98 | "addr=%p, value=%" PRIu64, | |
99 | field, value); | |
ac0c6bdd PP |
100 | goto end; |
101 | } | |
102 | } | |
103 | end: | |
104 | bt_put(field_type); | |
105 | return ret; | |
106 | } | |
107 | ||
d246b111 | 108 | static |
50842bdc | 109 | int set_packet_header_magic(struct bt_stream *stream) |
d246b111 JG |
110 | { |
111 | int ret = 0; | |
50842bdc | 112 | struct bt_field *magic_field = bt_field_structure_get_field_by_name( |
d246b111 | 113 | stream->packet_header, "magic"); |
19abc2c6 PP |
114 | const uint32_t magic_value = 0xc1fc1fc1; |
115 | ||
116 | assert(stream); | |
d246b111 JG |
117 | |
118 | if (!magic_field) { | |
119 | /* No magic field found. Not an error, skip. */ | |
19abc2c6 PP |
120 | BT_LOGV("No field named `magic` in packet header: skipping: " |
121 | "stream-addr=%p, stream-name=\"%s\"", | |
50842bdc | 122 | stream, bt_stream_get_name(stream)); |
d246b111 JG |
123 | goto end; |
124 | } | |
125 | ||
50842bdc | 126 | ret = bt_field_unsigned_integer_set_value(magic_field, |
b3376dd9 | 127 | (uint64_t) magic_value); |
d246b111 | 128 | |
d246b111 | 129 | if (ret) { |
b3376dd9 PP |
130 | BT_LOGW("Cannot set packet header field's `magic` integer field's value: " |
131 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
50842bdc | 132 | stream, bt_stream_get_name(stream), |
19abc2c6 PP |
133 | magic_field, (uint64_t) magic_value); |
134 | } else { | |
135 | BT_LOGV("Set packet header field's `magic` field's value: " | |
b3376dd9 | 136 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, |
50842bdc | 137 | stream, bt_stream_get_name(stream), |
19abc2c6 | 138 | magic_field, (uint64_t) magic_value); |
d246b111 JG |
139 | } |
140 | end: | |
83509119 | 141 | bt_put(magic_field); |
d246b111 JG |
142 | return ret; |
143 | } | |
144 | ||
145 | static | |
50842bdc | 146 | int set_packet_header_uuid(struct bt_stream *stream) |
d246b111 | 147 | { |
19abc2c6 PP |
148 | int ret = 0; |
149 | int64_t i; | |
50842bdc PP |
150 | struct bt_trace *trace = NULL; |
151 | struct bt_field *uuid_field = bt_field_structure_get_field_by_name( | |
d246b111 JG |
152 | stream->packet_header, "uuid"); |
153 | ||
19abc2c6 PP |
154 | assert(stream); |
155 | ||
d246b111 JG |
156 | if (!uuid_field) { |
157 | /* No uuid field found. Not an error, skip. */ | |
19abc2c6 PP |
158 | BT_LOGV("No field named `uuid` in packet header: skipping: " |
159 | "stream-addr=%p, stream-name=\"%s\"", | |
50842bdc | 160 | stream, bt_stream_get_name(stream)); |
d246b111 JG |
161 | goto end; |
162 | } | |
163 | ||
50842bdc | 164 | trace = (struct bt_trace *) bt_object_get_parent(stream); |
d246b111 | 165 | for (i = 0; i < 16; i++) { |
50842bdc PP |
166 | struct bt_field *uuid_element = |
167 | bt_field_array_get_field(uuid_field, i); | |
d246b111 | 168 | |
50842bdc | 169 | ret = bt_field_unsigned_integer_set_value( |
b3376dd9 | 170 | uuid_element, (uint64_t) trace->uuid[i]); |
83509119 | 171 | bt_put(uuid_element); |
d246b111 | 172 | if (ret) { |
19abc2c6 PP |
173 | BT_LOGW("Cannot set integer field's value (for `uuid` packet header field): " |
174 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, " | |
b3376dd9 | 175 | "value=%" PRIu64 ", index=%" PRId64, |
50842bdc | 176 | stream, bt_stream_get_name(stream), |
19abc2c6 | 177 | uuid_element, (uint64_t) trace->uuid[i], i); |
d246b111 JG |
178 | goto end; |
179 | } | |
180 | } | |
181 | ||
19abc2c6 PP |
182 | BT_LOGV("Set packet header field's `uuid` field's value: " |
183 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p", | |
50842bdc | 184 | stream, bt_stream_get_name(stream), uuid_field); |
19abc2c6 | 185 | |
d246b111 | 186 | end: |
83509119 | 187 | bt_put(uuid_field); |
e6a8e8e4 | 188 | BT_PUT(trace); |
d246b111 JG |
189 | return ret; |
190 | } | |
191 | static | |
50842bdc | 192 | int set_packet_header_stream_id(struct bt_stream *stream) |
d246b111 JG |
193 | { |
194 | int ret = 0; | |
195 | uint32_t stream_id; | |
50842bdc | 196 | struct bt_field *stream_id_field = bt_field_structure_get_field_by_name( |
d246b111 JG |
197 | stream->packet_header, "stream_id"); |
198 | ||
199 | if (!stream_id_field) { | |
200 | /* No stream_id field found. Not an error, skip. */ | |
19abc2c6 PP |
201 | BT_LOGV("No field named `stream_id` in packet header: skipping: " |
202 | "stream-addr=%p, stream-name=\"%s\"", | |
50842bdc | 203 | stream, bt_stream_get_name(stream)); |
d246b111 JG |
204 | goto end; |
205 | } | |
206 | ||
b3376dd9 | 207 | stream_id = stream->stream_class->id; |
50842bdc | 208 | ret = bt_field_unsigned_integer_set_value(stream_id_field, |
b3376dd9 PP |
209 | (uint64_t) stream_id); |
210 | if (ret) { | |
211 | BT_LOGW("Cannot set packet header field's `stream_id` integer field's value: " | |
212 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
50842bdc | 213 | stream, bt_stream_get_name(stream), |
b3376dd9 PP |
214 | stream_id_field, (uint64_t) stream_id); |
215 | } else { | |
216 | BT_LOGV("Set packet header field's `stream_id` field's value: " | |
217 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
50842bdc | 218 | stream, bt_stream_get_name(stream), |
b3376dd9 PP |
219 | stream_id_field, (uint64_t) stream_id); |
220 | } | |
221 | ||
222 | end: | |
223 | bt_put(stream_id_field); | |
224 | return ret; | |
225 | } | |
226 | ||
227 | static | |
50842bdc | 228 | int auto_populate_packet_header(struct bt_stream *stream) |
b3376dd9 PP |
229 | { |
230 | int ret = 0; | |
231 | ||
232 | if (!stream->packet_header) { | |
233 | goto end; | |
234 | } | |
235 | ||
236 | ret = set_packet_header_magic(stream); | |
237 | if (ret) { | |
238 | BT_LOGW("Cannot set packet header's magic number field: " | |
239 | "stream-addr=%p, stream-name=\"%s\"", | |
50842bdc | 240 | stream, bt_stream_get_name(stream)); |
b3376dd9 PP |
241 | goto end; |
242 | } | |
243 | ||
244 | ret = set_packet_header_uuid(stream); | |
245 | if (ret) { | |
246 | BT_LOGW("Cannot set packet header's UUID field: " | |
19abc2c6 | 247 | "stream-addr=%p, stream-name=\"%s\"", |
50842bdc | 248 | stream, bt_stream_get_name(stream)); |
d246b111 JG |
249 | goto end; |
250 | } | |
251 | ||
b3376dd9 PP |
252 | ret = set_packet_header_stream_id(stream); |
253 | if (ret) { | |
254 | BT_LOGW("Cannot set packet header's stream class ID field: " | |
255 | "stream-addr=%p, stream-name=\"%s\"", | |
50842bdc | 256 | stream, bt_stream_get_name(stream)); |
d246b111 JG |
257 | goto end; |
258 | } | |
259 | ||
b3376dd9 PP |
260 | BT_LOGV("Automatically populated stream's packet header's known fields: " |
261 | "stream-addr=%p, stream-name=\"%s\"", | |
50842bdc | 262 | stream, bt_stream_get_name(stream)); |
b3376dd9 PP |
263 | |
264 | end: | |
265 | return ret; | |
266 | } | |
267 | ||
268 | static | |
50842bdc | 269 | int set_packet_context_packet_size(struct bt_stream *stream) |
b3376dd9 PP |
270 | { |
271 | int ret = 0; | |
50842bdc | 272 | struct bt_field *field = bt_field_structure_get_field_by_name( |
b3376dd9 PP |
273 | stream->packet_context, "packet_size"); |
274 | ||
275 | assert(stream); | |
276 | ||
277 | if (!field) { | |
278 | /* No packet size field found. Not an error, skip. */ | |
279 | BT_LOGV("No field named `packet_size` in packet context: skipping: " | |
280 | "stream-addr=%p, stream-name=\"%s\"", | |
50842bdc | 281 | stream, bt_stream_get_name(stream)); |
b3376dd9 PP |
282 | goto end; |
283 | } | |
284 | ||
50842bdc | 285 | ret = bt_field_unsigned_integer_set_value(field, |
b3376dd9 | 286 | stream->pos.packet_size); |
d246b111 | 287 | if (ret) { |
b3376dd9 PP |
288 | BT_LOGW("Cannot set packet context field's `packet_size` integer field's value: " |
289 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
50842bdc | 290 | stream, bt_stream_get_name(stream), |
b3376dd9 | 291 | field, stream->pos.packet_size); |
d246b111 | 292 | } else { |
b3376dd9 PP |
293 | BT_LOGV("Set packet context field's `packet_size` field's value: " |
294 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
50842bdc | 295 | stream, bt_stream_get_name(stream), |
b3376dd9 | 296 | field, stream->pos.packet_size); |
d246b111 | 297 | } |
19abc2c6 | 298 | |
b3376dd9 PP |
299 | end: |
300 | bt_put(field); | |
301 | return ret; | |
302 | } | |
303 | ||
304 | static | |
50842bdc | 305 | int set_packet_context_content_size(struct bt_stream *stream) |
b3376dd9 PP |
306 | { |
307 | int ret = 0; | |
50842bdc | 308 | struct bt_field *field = bt_field_structure_get_field_by_name( |
b3376dd9 PP |
309 | stream->packet_context, "content_size"); |
310 | ||
311 | assert(stream); | |
312 | ||
313 | if (!field) { | |
314 | /* No content size field found. Not an error, skip. */ | |
315 | BT_LOGV("No field named `content_size` in packet context: skipping: " | |
316 | "stream-addr=%p, stream-name=\"%s\"", | |
50842bdc | 317 | stream, bt_stream_get_name(stream)); |
b3376dd9 PP |
318 | goto end; |
319 | } | |
320 | ||
50842bdc | 321 | ret = bt_field_unsigned_integer_set_value(field, |
b3376dd9 | 322 | stream->pos.offset); |
19abc2c6 | 323 | if (ret) { |
b3376dd9 PP |
324 | BT_LOGW("Cannot set packet context field's `content_size` integer field's value: " |
325 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64, | |
50842bdc | 326 | stream, bt_stream_get_name(stream), |
b3376dd9 | 327 | field, stream->pos.offset); |
19abc2c6 | 328 | } else { |
b3376dd9 PP |
329 | BT_LOGV("Set packet context field's `content_size` field's value: " |
330 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64, | |
50842bdc | 331 | stream, bt_stream_get_name(stream), |
b3376dd9 | 332 | field, stream->pos.offset); |
19abc2c6 PP |
333 | } |
334 | ||
d246b111 | 335 | end: |
b3376dd9 | 336 | bt_put(field); |
d246b111 JG |
337 | return ret; |
338 | } | |
339 | ||
340 | static | |
50842bdc | 341 | int set_packet_context_events_discarded(struct bt_stream *stream) |
d246b111 | 342 | { |
b3376dd9 | 343 | int ret = 0; |
50842bdc | 344 | struct bt_field *field = bt_field_structure_get_field_by_name( |
b3376dd9 | 345 | stream->packet_context, "events_discarded"); |
d246b111 | 346 | |
b3376dd9 PP |
347 | assert(stream); |
348 | ||
349 | if (!field) { | |
350 | /* No discarded events count field found. Not an error, skip. */ | |
351 | BT_LOGV("No field named `events_discarded` in packet context: skipping: " | |
352 | "stream-addr=%p, stream-name=\"%s\"", | |
50842bdc | 353 | stream, bt_stream_get_name(stream)); |
b3376dd9 PP |
354 | goto end; |
355 | } | |
356 | ||
59a09b0e PP |
357 | /* |
358 | * If the field is set by the user, make sure that the value is | |
359 | * greater than or equal to the stream's current count of | |
360 | * discarded events. We do not allow wrapping here. If it's | |
361 | * valid, update the stream's current count. | |
362 | */ | |
50842bdc | 363 | if (bt_field_is_set(field)) { |
59a09b0e PP |
364 | uint64_t user_val; |
365 | ||
50842bdc | 366 | ret = bt_field_unsigned_integer_get_value(field, |
59a09b0e PP |
367 | &user_val); |
368 | if (ret) { | |
369 | BT_LOGW("Cannot get packet context `events_discarded` field's unsigned value: " | |
370 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p", | |
50842bdc | 371 | stream, bt_stream_get_name(stream), field); |
59a09b0e PP |
372 | goto end; |
373 | } | |
374 | ||
375 | if (user_val < stream->discarded_events) { | |
376 | BT_LOGW("Invalid packet context `events_discarded` field's unsigned value: " | |
377 | "value is lesser than the stream's current discarded events count: " | |
378 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, " | |
379 | "value=%" PRIu64 ", " | |
380 | "stream-discarded-events-count=%" PRIu64, | |
50842bdc | 381 | stream, bt_stream_get_name(stream), field, |
59a09b0e PP |
382 | user_val, stream->discarded_events); |
383 | goto end; | |
384 | } | |
385 | ||
386 | stream->discarded_events = user_val; | |
b3376dd9 | 387 | } else { |
50842bdc | 388 | ret = bt_field_unsigned_integer_set_value(field, |
59a09b0e PP |
389 | stream->discarded_events); |
390 | if (ret) { | |
391 | BT_LOGW("Cannot set packet context field's `events_discarded` integer field's value: " | |
392 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
50842bdc | 393 | stream, bt_stream_get_name(stream), |
59a09b0e PP |
394 | field, stream->discarded_events); |
395 | } else { | |
396 | BT_LOGV("Set packet context field's `events_discarded` field's value: " | |
397 | "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64, | |
50842bdc | 398 | stream, bt_stream_get_name(stream), |
59a09b0e PP |
399 | field, stream->discarded_events); |
400 | } | |
b3376dd9 PP |
401 | } |
402 | ||
403 | end: | |
404 | bt_put(field); | |
405 | return ret; | |
406 | } | |
407 | ||
408 | static | |
2a03740b PP |
409 | void update_clock_value(uint64_t *val, uint64_t new_val, |
410 | unsigned int new_val_size) | |
b3376dd9 | 411 | { |
2a03740b PP |
412 | const uint64_t pow2 = 1ULL << new_val_size; |
413 | const uint64_t mask = pow2 - 1; | |
414 | uint64_t val_masked; | |
b3376dd9 | 415 | |
2a03740b PP |
416 | #ifdef BT_LOG_ENABLED_VERBOSE |
417 | uint64_t old_val = *val; | |
418 | #endif | |
b3376dd9 | 419 | |
2a03740b PP |
420 | if (new_val_size == 64) { |
421 | *val = new_val; | |
b3376dd9 PP |
422 | goto end; |
423 | } | |
424 | ||
2a03740b PP |
425 | val_masked = *val & mask; |
426 | ||
427 | if (new_val < val_masked) { | |
428 | /* Wrapped once */ | |
429 | new_val |= pow2; | |
b3376dd9 PP |
430 | } |
431 | ||
2a03740b PP |
432 | *val &= ~mask; |
433 | *val |= new_val; | |
434 | ||
435 | end: | |
436 | BT_LOGV("Updated clock value: old-val=%" PRIu64 ", new-val=%" PRIu64, | |
437 | old_val, *val); | |
438 | return; | |
439 | } | |
440 | ||
441 | static | |
442 | int visit_field_update_clock_value(struct bt_field *field, uint64_t *val) | |
443 | { | |
444 | int ret = 0; | |
445 | ||
446 | if (!field) { | |
b3376dd9 PP |
447 | goto end; |
448 | } | |
449 | ||
2a03740b PP |
450 | switch (bt_field_get_type_id(field)) { |
451 | case BT_FIELD_TYPE_ID_INTEGER: | |
452 | { | |
453 | struct bt_clock_class *cc = | |
454 | bt_field_type_integer_get_mapped_clock_class( | |
455 | field->type); | |
456 | int val_size; | |
457 | uint64_t uval; | |
458 | ||
459 | if (!cc) { | |
460 | goto end; | |
461 | } | |
462 | ||
463 | bt_put(cc); | |
464 | val_size = bt_field_type_integer_get_size(field->type); | |
465 | assert(val_size >= 1); | |
466 | ||
467 | if (bt_field_type_integer_is_signed(field->type)) { | |
468 | int64_t ival; | |
469 | ||
470 | ret = bt_field_signed_integer_get_value(field, &ival); | |
471 | uval = (uint64_t) ival; | |
472 | } else { | |
473 | ret = bt_field_unsigned_integer_get_value(field, &uval); | |
474 | } | |
475 | ||
476 | if (ret) { | |
477 | /* Not set */ | |
478 | goto end; | |
479 | } | |
480 | ||
481 | update_clock_value(val, uval, val_size); | |
482 | break; | |
b3376dd9 | 483 | } |
2a03740b PP |
484 | case BT_FIELD_TYPE_ID_ENUM: |
485 | { | |
486 | struct bt_field *int_field = | |
487 | bt_field_enumeration_get_container(field); | |
b3376dd9 | 488 | |
2a03740b PP |
489 | assert(int_field); |
490 | ret = visit_field_update_clock_value(int_field, val); | |
491 | bt_put(int_field); | |
492 | break; | |
b3376dd9 | 493 | } |
2a03740b PP |
494 | case BT_FIELD_TYPE_ID_ARRAY: |
495 | { | |
496 | uint64_t i; | |
497 | int64_t len = bt_field_type_array_get_length(field->type); | |
b3376dd9 | 498 | |
2a03740b PP |
499 | assert(len >= 0); |
500 | ||
501 | for (i = 0; i < len; i++) { | |
502 | struct bt_field *elem_field = | |
503 | bt_field_array_get_field(field, i); | |
504 | ||
505 | assert(elem_field); | |
506 | ret = visit_field_update_clock_value(elem_field, val); | |
507 | bt_put(elem_field); | |
508 | if (ret) { | |
509 | goto end; | |
510 | } | |
511 | } | |
512 | break; | |
513 | } | |
514 | case BT_FIELD_TYPE_ID_SEQUENCE: | |
515 | { | |
516 | uint64_t i; | |
517 | int64_t len = bt_field_sequence_get_int_length(field); | |
518 | ||
519 | if (len < 0) { | |
520 | ret = -1; | |
521 | goto end; | |
522 | } | |
523 | ||
524 | for (i = 0; i < len; i++) { | |
525 | struct bt_field *elem_field = | |
526 | bt_field_sequence_get_field(field, i); | |
527 | ||
528 | assert(elem_field); | |
529 | ret = visit_field_update_clock_value(elem_field, val); | |
530 | bt_put(elem_field); | |
531 | if (ret) { | |
532 | goto end; | |
533 | } | |
534 | } | |
535 | break; | |
536 | } | |
537 | case BT_FIELD_TYPE_ID_STRUCT: | |
538 | { | |
539 | uint64_t i; | |
540 | int64_t len = bt_field_type_structure_get_field_count( | |
541 | field->type); | |
542 | ||
543 | assert(len >= 0); | |
544 | ||
545 | for (i = 0; i < len; i++) { | |
546 | struct bt_field *member_field = | |
547 | bt_field_structure_get_field_by_index(field, i); | |
548 | ||
549 | assert(member_field); | |
550 | ret = visit_field_update_clock_value(member_field, val); | |
551 | bt_put(member_field); | |
552 | if (ret) { | |
553 | goto end; | |
554 | } | |
555 | } | |
556 | break; | |
557 | } | |
558 | case BT_FIELD_TYPE_ID_VARIANT: | |
559 | { | |
560 | struct bt_field *cur_field = | |
561 | bt_field_variant_get_current_field(field); | |
562 | ||
563 | if (!cur_field) { | |
564 | ret = -1; | |
565 | goto end; | |
566 | } | |
567 | ||
568 | ret = visit_field_update_clock_value(cur_field, val); | |
569 | bt_put(cur_field); | |
570 | break; | |
571 | } | |
572 | default: | |
573 | break; | |
b3376dd9 PP |
574 | } |
575 | ||
576 | end: | |
b3376dd9 PP |
577 | return ret; |
578 | } | |
579 | ||
2a03740b | 580 | int visit_event_update_clock_value(struct bt_event *event, uint64_t *val) |
b3376dd9 PP |
581 | { |
582 | int ret = 0; | |
2a03740b | 583 | struct bt_field *field; |
b3376dd9 | 584 | |
2a03740b PP |
585 | field = bt_event_get_header(event); |
586 | ret = visit_field_update_clock_value(field, val); | |
587 | bt_put(field); | |
588 | if (ret) { | |
589 | BT_LOGW_STR("Cannot automatically update clock value in " | |
590 | "event's header."); | |
d246b111 JG |
591 | goto end; |
592 | } | |
593 | ||
2a03740b PP |
594 | field = bt_event_get_stream_event_context(event); |
595 | ret = visit_field_update_clock_value(field, val); | |
596 | bt_put(field); | |
597 | if (ret) { | |
598 | BT_LOGW_STR("Cannot automatically update clock value in " | |
599 | "event's stream event context."); | |
b3376dd9 PP |
600 | goto end; |
601 | } | |
602 | ||
2a03740b PP |
603 | field = bt_event_get_event_context(event); |
604 | ret = visit_field_update_clock_value(field, val); | |
605 | bt_put(field); | |
606 | if (ret) { | |
607 | BT_LOGW_STR("Cannot automatically update clock value in " | |
608 | "event's context."); | |
b3376dd9 PP |
609 | goto end; |
610 | } | |
611 | ||
2a03740b PP |
612 | field = bt_event_get_event_payload(event); |
613 | ret = visit_field_update_clock_value(field, val); | |
614 | bt_put(field); | |
d246b111 | 615 | if (ret) { |
2a03740b PP |
616 | BT_LOGW_STR("Cannot automatically update clock value in " |
617 | "event's payload."); | |
618 | goto end; | |
b3376dd9 PP |
619 | } |
620 | ||
621 | end: | |
b3376dd9 PP |
622 | return ret; |
623 | } | |
624 | ||
625 | static | |
2a03740b | 626 | int set_packet_context_timestamps(struct bt_stream *stream) |
b3376dd9 PP |
627 | { |
628 | int ret = 0; | |
2a03740b PP |
629 | uint64_t val; |
630 | uint64_t cur_clock_value; | |
631 | uint64_t init_clock_value = 0; | |
632 | struct bt_field *ts_begin_field = bt_field_structure_get_field_by_name( | |
633 | stream->packet_context, "timestamp_begin"); | |
634 | struct bt_field *ts_end_field = bt_field_structure_get_field_by_name( | |
635 | stream->packet_context, "timestamp_end"); | |
636 | uint64_t i; | |
637 | int64_t len; | |
638 | ||
639 | if (ts_begin_field && bt_field_is_set(ts_begin_field)) { | |
640 | /* Use provided `timestamp_begin` value as starting value */ | |
641 | ret = bt_field_unsigned_integer_get_value(ts_begin_field, &val); | |
642 | assert(ret == 0); | |
643 | init_clock_value = val; | |
644 | } else if (stream->last_ts_end != -1ULL) { | |
645 | /* Use last packet's ending timestamp as starting value */ | |
646 | init_clock_value = stream->last_ts_end; | |
647 | } | |
648 | ||
649 | cur_clock_value = init_clock_value; | |
650 | ||
651 | if (stream->last_ts_end != -1ULL && | |
652 | cur_clock_value < stream->last_ts_end) { | |
653 | BT_LOGW("Packet's initial timestamp is less than previous " | |
654 | "packet's final timestamp: " | |
655 | "stream-addr=%p, stream-name=\"%s\", " | |
656 | "cur-packet-ts-begin=%" PRIu64 ", " | |
657 | "prev-packet-ts-end=%" PRIu64, | |
658 | stream, bt_stream_get_name(stream), | |
659 | cur_clock_value, stream->last_ts_end); | |
660 | ret = -1; | |
d246b111 JG |
661 | goto end; |
662 | } | |
663 | ||
2a03740b PP |
664 | /* |
665 | * Visit all the packet context fields, followed by all the | |
666 | * fields of all the events, in order, updating our current | |
667 | * clock value as we visit. | |
668 | * | |
669 | * While visiting the packet context fields, do not consider | |
670 | * `timestamp_begin` and `timestamp_end` because this function's | |
671 | * purpose is to set them anyway. Also do not consider | |
672 | * `packet_size`, `content_size`, `events_discarded`, and | |
673 | * `packet_seq_num` if they are not set because those are | |
674 | * autopopulating fields. | |
675 | */ | |
676 | len = bt_field_type_structure_get_field_count( | |
677 | stream->packet_context->type); | |
678 | assert(len >= 0); | |
b3376dd9 | 679 | |
2a03740b PP |
680 | for (i = 0; i < len; i++) { |
681 | const char *member_name; | |
682 | struct bt_field *member_field; | |
b3376dd9 | 683 | |
2a03740b PP |
684 | ret = bt_field_type_structure_get_field_by_index( |
685 | stream->packet_context->type, &member_name, NULL, i); | |
686 | assert(ret == 0); | |
b3376dd9 | 687 | |
2a03740b PP |
688 | if (strcmp(member_name, "timestamp_begin") == 0 || |
689 | strcmp(member_name, "timestamp_end") == 0) { | |
690 | continue; | |
691 | } | |
692 | ||
693 | member_field = bt_field_structure_get_field_by_index( | |
694 | stream->packet_context, i); | |
695 | assert(member_field); | |
696 | ||
697 | if (strcmp(member_name, "packet_size") == 0 && | |
698 | !bt_field_is_set(member_field)) { | |
699 | bt_put(member_field); | |
700 | continue; | |
701 | } | |
702 | ||
703 | if (strcmp(member_name, "content_size") == 0 && | |
704 | !bt_field_is_set(member_field)) { | |
705 | bt_put(member_field); | |
706 | continue; | |
707 | } | |
708 | ||
709 | if (strcmp(member_name, "events_discarded") == 0 && | |
710 | !bt_field_is_set(member_field)) { | |
711 | bt_put(member_field); | |
712 | continue; | |
713 | } | |
714 | ||
715 | if (strcmp(member_name, "packet_seq_num") == 0 && | |
716 | !bt_field_is_set(member_field)) { | |
717 | bt_put(member_field); | |
718 | continue; | |
719 | } | |
720 | ||
721 | ret = visit_field_update_clock_value(member_field, | |
722 | &cur_clock_value); | |
723 | bt_put(member_field); | |
724 | if (ret) { | |
725 | BT_LOGW("Cannot automatically update clock value " | |
726 | "in stream's packet context: " | |
727 | "stream-addr=%p, stream-name=\"%s\", " | |
728 | "field-name=\"%s\"", | |
729 | stream, bt_stream_get_name(stream), | |
730 | member_name); | |
731 | goto end; | |
732 | } | |
b3376dd9 PP |
733 | } |
734 | ||
2a03740b PP |
735 | for (i = 0; i < stream->events->len; i++) { |
736 | struct bt_event *event = g_ptr_array_index(stream->events, i); | |
737 | ||
738 | assert(event); | |
739 | ret = visit_event_update_clock_value(event, &cur_clock_value); | |
740 | if (ret) { | |
741 | BT_LOGW("Cannot automatically update clock value " | |
742 | "in stream's packet context: " | |
743 | "stream-addr=%p, stream-name=\"%s\", " | |
744 | "index=%" PRIu64 ", event-addr=%p, " | |
745 | "event-class-id=%" PRId64 ", " | |
746 | "event-class-name=\"%s\"", | |
747 | stream, bt_stream_get_name(stream), | |
748 | i, event, | |
749 | bt_event_class_get_id(event->event_class), | |
750 | bt_event_class_get_name(event->event_class)); | |
751 | goto end; | |
752 | } | |
753 | } | |
754 | ||
755 | /* | |
756 | * Everything is visited, thus the current clock value | |
757 | * corresponds to the ending timestamp. Validate this value | |
758 | * against the provided value of `timestamp_end`, if any, | |
759 | * otherwise set it. | |
760 | */ | |
761 | if (ts_end_field && bt_field_is_set(ts_end_field)) { | |
762 | ret = bt_field_unsigned_integer_get_value(ts_end_field, &val); | |
763 | assert(ret == 0); | |
764 | ||
765 | if (val < cur_clock_value) { | |
766 | BT_LOGW("Packet's final timestamp is less than " | |
767 | "computed packet's final timestamp: " | |
768 | "stream-addr=%p, stream-name=\"%s\", " | |
769 | "cur-packet-ts-end=%" PRIu64 ", " | |
770 | "computed-packet-ts-end=%" PRIu64, | |
771 | stream, bt_stream_get_name(stream), | |
772 | val, cur_clock_value); | |
773 | ret = -1; | |
774 | goto end; | |
775 | } | |
776 | ||
777 | stream->last_ts_end = val; | |
778 | } | |
779 | ||
780 | if (ts_end_field && !bt_field_is_set(ts_end_field)) { | |
781 | ret = set_integer_field_value(ts_end_field, cur_clock_value); | |
782 | assert(ret == 0); | |
783 | stream->last_ts_end = cur_clock_value; | |
784 | } | |
785 | ||
786 | if (!ts_end_field) { | |
787 | stream->last_ts_end = cur_clock_value; | |
788 | } | |
789 | ||
790 | /* Set `timestamp_begin` field to initial clock value */ | |
791 | if (ts_begin_field && !bt_field_is_set(ts_begin_field)) { | |
792 | ret = set_integer_field_value(ts_begin_field, init_clock_value); | |
793 | assert(ret == 0); | |
794 | } | |
b3376dd9 PP |
795 | |
796 | end: | |
2a03740b PP |
797 | bt_put(ts_begin_field); |
798 | bt_put(ts_end_field); | |
b3376dd9 PP |
799 | return ret; |
800 | } | |
801 | ||
802 | static | |
2a03740b | 803 | int auto_populate_packet_context(struct bt_stream *stream, bool set_ts) |
b3376dd9 PP |
804 | { |
805 | int ret = 0; | |
806 | ||
807 | if (!stream->packet_context) { | |
808 | goto end; | |
809 | } | |
810 | ||
811 | ret = set_packet_context_packet_size(stream); | |
d246b111 | 812 | if (ret) { |
b3376dd9 PP |
813 | BT_LOGW("Cannot set packet context's packet size field: " |
814 | "stream-addr=%p, stream-name=\"%s\"", | |
50842bdc | 815 | stream, bt_stream_get_name(stream)); |
b3376dd9 PP |
816 | goto end; |
817 | } | |
818 | ||
819 | ret = set_packet_context_content_size(stream); | |
820 | if (ret) { | |
821 | BT_LOGW("Cannot set packet context's content size field: " | |
19abc2c6 | 822 | "stream-addr=%p, stream-name=\"%s\"", |
50842bdc | 823 | stream, bt_stream_get_name(stream)); |
d246b111 JG |
824 | goto end; |
825 | } | |
19abc2c6 | 826 | |
2a03740b PP |
827 | if (set_ts) { |
828 | ret = set_packet_context_timestamps(stream); | |
829 | if (ret) { | |
830 | BT_LOGW("Cannot set packet context's timestamp fields: " | |
831 | "stream-addr=%p, stream-name=\"%s\"", | |
832 | stream, bt_stream_get_name(stream)); | |
833 | goto end; | |
834 | } | |
b3376dd9 PP |
835 | } |
836 | ||
837 | ret = set_packet_context_events_discarded(stream); | |
838 | if (ret) { | |
839 | BT_LOGW("Cannot set packet context's discarded events count field: " | |
840 | "stream-addr=%p, stream-name=\"%s\"", | |
50842bdc | 841 | stream, bt_stream_get_name(stream)); |
b3376dd9 PP |
842 | goto end; |
843 | } | |
844 | ||
845 | BT_LOGV("Automatically populated stream's packet context's known fields: " | |
19abc2c6 | 846 | "stream-addr=%p, stream-name=\"%s\"", |
50842bdc | 847 | stream, bt_stream_get_name(stream)); |
19abc2c6 | 848 | |
d246b111 JG |
849 | end: |
850 | return ret; | |
851 | } | |
852 | ||
123fbdec | 853 | static |
50842bdc | 854 | void release_event(struct bt_event *event) |
123fbdec | 855 | { |
e6a8e8e4 JG |
856 | if (bt_object_get_ref_count(event)) { |
857 | /* | |
858 | * The event is being orphaned, but it must guarantee the | |
859 | * existence of its event class for the duration of its | |
860 | * lifetime. | |
861 | */ | |
862 | bt_get(event->event_class); | |
863 | BT_PUT(event->base.parent); | |
864 | } else { | |
865 | bt_object_release(event); | |
866 | } | |
123fbdec JG |
867 | } |
868 | ||
319fd969 PP |
869 | static |
870 | int create_stream_file(struct bt_ctf_writer *writer, | |
50842bdc | 871 | struct bt_stream *stream) |
319fd969 PP |
872 | { |
873 | int fd; | |
ab22d18f PP |
874 | GString *filename = g_string_new(NULL); |
875 | int64_t stream_class_id; | |
876 | char *file_path = NULL; | |
319fd969 | 877 | |
19abc2c6 PP |
878 | BT_LOGD("Creating stream file: writer-addr=%p, stream-addr=%p, " |
879 | "stream-name=\"%s\", stream-class-addr=%p, stream-class-name=\"%s\"", | |
50842bdc | 880 | writer, stream, bt_stream_get_name(stream), |
19abc2c6 PP |
881 | stream->stream_class, stream->stream_class->name->str); |
882 | ||
ab22d18f PP |
883 | if (stream->name && stream->name->len > 0) { |
884 | /* Use stream name's base name as prefix */ | |
885 | gchar *basename = g_path_get_basename(stream->name->str); | |
886 | ||
887 | assert(basename); | |
888 | ||
889 | if (strcmp(basename, G_DIR_SEPARATOR_S) == 0) { | |
890 | g_string_assign(filename, "stream"); | |
891 | } else { | |
892 | g_string_assign(filename, basename); | |
893 | } | |
894 | ||
895 | g_free(basename); | |
896 | goto append_ids; | |
897 | } | |
898 | ||
899 | if (stream->stream_class->name && | |
900 | stream->stream_class->name->len > 0) { | |
901 | /* Use stream class name's base name as prefix */ | |
902 | gchar *basename = | |
903 | g_path_get_basename(stream->stream_class->name->str); | |
904 | ||
905 | assert(basename); | |
906 | ||
907 | if (strcmp(basename, G_DIR_SEPARATOR_S) == 0) { | |
908 | g_string_assign(filename, "stream"); | |
909 | } else { | |
910 | g_string_assign(filename, basename); | |
319fd969 PP |
911 | } |
912 | ||
ab22d18f PP |
913 | g_free(basename); |
914 | goto append_ids; | |
319fd969 PP |
915 | } |
916 | ||
ab22d18f PP |
917 | /* Default to using `stream-` as prefix */ |
918 | g_string_assign(filename, "stream"); | |
919 | ||
920 | append_ids: | |
50842bdc | 921 | stream_class_id = bt_stream_class_get_id(stream->stream_class); |
ab22d18f PP |
922 | assert(stream_class_id >= 0); |
923 | assert(stream->id >= 0); | |
924 | g_string_append_printf(filename, "-%" PRId64 "-%" PRId64, | |
925 | stream_class_id, stream->id); | |
926 | ||
ebd04048 MJ |
927 | file_path = g_build_filename(writer->path->str, filename->str, NULL); |
928 | if (file_path == NULL) { | |
929 | fd = -1; | |
930 | goto end; | |
931 | } | |
ab22d18f | 932 | |
ebd04048 | 933 | fd = open(file_path, |
319fd969 PP |
934 | O_RDWR | O_CREAT | O_TRUNC, |
935 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
ebd04048 | 936 | g_free(file_path); |
19abc2c6 | 937 | if (fd < 0) { |
c606e638 PP |
938 | BT_LOGW_ERRNO("Failed to open stream file for writing", |
939 | ": file_path=\"%s\", filename=\"%s\", ret=%d", | |
940 | file_path, filename->str, fd); | |
19abc2c6 PP |
941 | goto end; |
942 | } | |
943 | ||
944 | BT_LOGD("Created stream file for writing: " | |
ebd04048 | 945 | "stream-addr=%p, stream-name=\"%s\", " |
50842bdc | 946 | "filename=\"%s\", fd=%d", stream, bt_stream_get_name(stream), |
ebd04048 | 947 | filename->str, fd); |
19abc2c6 PP |
948 | |
949 | end: | |
319fd969 PP |
950 | g_string_free(filename, TRUE); |
951 | return fd; | |
952 | } | |
953 | ||
954 | static | |
50842bdc | 955 | void set_stream_fd(struct bt_stream *stream, int fd) |
319fd969 | 956 | { |
50842bdc | 957 | (void) bt_stream_pos_init(&stream->pos, fd, O_RDWR); |
319fd969 | 958 | stream->pos.fd = fd; |
319fd969 PP |
959 | } |
960 | ||
3230ee6b PP |
961 | static |
962 | void component_destroy_listener(struct bt_component *component, void *data) | |
963 | { | |
50842bdc | 964 | struct bt_stream *stream = data; |
3230ee6b | 965 | |
19abc2c6 PP |
966 | BT_LOGD("Component is being destroyed, stream is notified: " |
967 | "comp-addr=%p, stream-addr=%p", component, stream); | |
3230ee6b PP |
968 | g_hash_table_remove(stream->comp_cur_port, component); |
969 | } | |
970 | ||
cfe11c58 | 971 | static |
50842bdc PP |
972 | struct bt_stream *bt_stream_create_with_id_no_check( |
973 | struct bt_stream_class *stream_class, | |
cfe11c58 | 974 | const char *name, uint64_t id) |
273b65be | 975 | { |
12c8a1a3 | 976 | int ret; |
50842bdc PP |
977 | struct bt_stream *stream = NULL; |
978 | struct bt_trace *trace = NULL; | |
319fd969 | 979 | struct bt_ctf_writer *writer = NULL; |
273b65be | 980 | |
319fd969 | 981 | if (!stream_class) { |
19abc2c6 | 982 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
b71d7298 | 983 | goto error; |
319fd969 PP |
984 | } |
985 | ||
19abc2c6 | 986 | BT_LOGD("Creating stream object: stream-class-addr=%p, " |
cfe11c58 PP |
987 | "stream-class-name=\"%s\", stream-name=\"%s\", " |
988 | "stream-id=%" PRIu64, | |
50842bdc | 989 | stream_class, bt_stream_class_get_name(stream_class), |
cfe11c58 | 990 | name, id); |
50842bdc | 991 | trace = bt_stream_class_borrow_trace(stream_class); |
319fd969 | 992 | if (!trace) { |
19abc2c6 | 993 | BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: " |
b3376dd9 PP |
994 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
995 | "stream-name=\"%s\"", | |
50842bdc | 996 | stream_class, bt_stream_class_get_name(stream_class), |
19abc2c6 | 997 | name); |
b71d7298 | 998 | goto error; |
273b65be JG |
999 | } |
1000 | ||
50842bdc | 1001 | if (bt_trace_is_static(trace)) { |
5acf2ae6 PP |
1002 | /* |
1003 | * A static trace has the property that all its stream | |
1004 | * classes, clock classes, and streams are definitive: | |
1005 | * no more can be added, and each object is also frozen. | |
1006 | */ | |
19abc2c6 | 1007 | BT_LOGW("Invalid parameter: cannot create stream from a stream class which is part of a static trace: " |
b3376dd9 PP |
1008 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
1009 | "stream-name=\"%s\", trace-addr=%p", | |
50842bdc | 1010 | stream_class, bt_stream_class_get_name(stream_class), |
b3376dd9 | 1011 | name, trace); |
5acf2ae6 PP |
1012 | goto error; |
1013 | } | |
1014 | ||
cfe11c58 PP |
1015 | if (id != -1ULL) { |
1016 | /* | |
1017 | * Validate that the given ID is unique amongst all the | |
1018 | * existing trace's streams created from the same stream | |
1019 | * class. | |
1020 | */ | |
1021 | size_t i; | |
1022 | ||
1023 | for (i = 0; i < trace->streams->len; i++) { | |
50842bdc | 1024 | struct bt_stream *trace_stream = |
cfe11c58 PP |
1025 | g_ptr_array_index(trace->streams, i); |
1026 | ||
1027 | if (trace_stream->stream_class != stream_class) { | |
1028 | continue; | |
1029 | } | |
1030 | ||
1031 | if (trace_stream->id == id) { | |
1032 | BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID."); | |
1033 | goto error; | |
1034 | } | |
1035 | } | |
1036 | } | |
1037 | ||
50842bdc | 1038 | stream = g_new0(struct bt_stream, 1); |
273b65be | 1039 | if (!stream) { |
19abc2c6 | 1040 | BT_LOGE_STR("Failed to allocate one stream."); |
b71d7298 | 1041 | goto error; |
273b65be JG |
1042 | } |
1043 | ||
50842bdc | 1044 | bt_object_init(stream, bt_stream_destroy); |
e6a8e8e4 JG |
1045 | /* |
1046 | * Acquire reference to parent since stream will become publicly | |
1047 | * reachable; it needs its parent to remain valid. | |
1048 | */ | |
1049 | bt_object_set_parent(stream, trace); | |
273b65be | 1050 | stream->stream_class = stream_class; |
319fd969 | 1051 | stream->pos.fd = -1; |
cfe11c58 | 1052 | stream->id = (int64_t) id; |
d246b111 | 1053 | |
3230ee6b | 1054 | stream->destroy_listeners = g_array_new(FALSE, TRUE, |
50842bdc | 1055 | sizeof(struct bt_stream_destroy_listener)); |
3230ee6b | 1056 | if (!stream->destroy_listeners) { |
19abc2c6 | 1057 | BT_LOGE_STR("Failed to allocate a GArray."); |
3230ee6b PP |
1058 | goto error; |
1059 | } | |
1060 | ||
b71d7298 PP |
1061 | if (name) { |
1062 | stream->name = g_string_new(name); | |
1063 | if (!stream->name) { | |
19abc2c6 | 1064 | BT_LOGE_STR("Failed to allocate a GString."); |
b71d7298 PP |
1065 | goto error; |
1066 | } | |
1067 | } | |
1068 | ||
19abc2c6 PP |
1069 | BT_LOGD("Set stream's trace parent: trace-addr=%p", trace); |
1070 | ||
319fd969 PP |
1071 | if (trace->is_created_by_writer) { |
1072 | int fd; | |
2a03740b | 1073 | |
cfe11c58 PP |
1074 | writer = (struct bt_ctf_writer *) bt_object_get_parent(trace); |
1075 | stream->id = (int64_t) stream_class->next_stream_id++; | |
2a03740b | 1076 | stream->last_ts_end = -1ULL; |
319fd969 | 1077 | |
19abc2c6 PP |
1078 | BT_LOGD("Stream object belongs to a writer's trace: " |
1079 | "writer-addr=%p", writer); | |
319fd969 | 1080 | assert(writer); |
b3376dd9 | 1081 | |
98edd02c | 1082 | if (stream_class->packet_context_type) { |
19abc2c6 PP |
1083 | BT_LOGD("Creating stream's packet context field: " |
1084 | "ft-addr=%p", stream_class->packet_context_type); | |
50842bdc | 1085 | stream->packet_context = bt_field_create( |
98edd02c JG |
1086 | stream_class->packet_context_type); |
1087 | if (!stream->packet_context) { | |
19abc2c6 | 1088 | BT_LOGW_STR("Cannot create stream's packet context field."); |
98edd02c JG |
1089 | goto error; |
1090 | } | |
319fd969 | 1091 | |
98edd02c | 1092 | /* Initialize events_discarded */ |
af9296f3 | 1093 | ret = try_set_structure_field_integer( |
19abc2c6 | 1094 | stream->packet_context, "events_discarded", 0); |
7ee636df | 1095 | if (ret < 0) { |
19abc2c6 PP |
1096 | BT_LOGW("Cannot set `events_discarded` field in packet context: " |
1097 | "ret=%d, packet-context-field-addr=%p", | |
1098 | ret, stream->packet_context); | |
98edd02c JG |
1099 | goto error; |
1100 | } | |
319fd969 PP |
1101 | } |
1102 | ||
1103 | stream->events = g_ptr_array_new_with_free_func( | |
1104 | (GDestroyNotify) release_event); | |
1105 | if (!stream->events) { | |
19abc2c6 | 1106 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
319fd969 PP |
1107 | goto error; |
1108 | } | |
1109 | ||
b3376dd9 PP |
1110 | if (trace->packet_header_type) { |
1111 | BT_LOGD("Creating stream's packet header field: " | |
1112 | "ft-addr=%p", trace->packet_header_type); | |
1113 | stream->packet_header = | |
50842bdc | 1114 | bt_field_create(trace->packet_header_type); |
b3376dd9 PP |
1115 | if (!stream->packet_header) { |
1116 | BT_LOGW_STR("Cannot create stream's packet header field."); | |
1117 | goto error; | |
1118 | } | |
319fd969 PP |
1119 | } |
1120 | ||
1121 | /* | |
1122 | * Attempt to populate the default trace packet header fields | |
1123 | * (magic, uuid and stream_id). This will _not_ fail shall the | |
1124 | * fields not be found or be of an incompatible type; they will | |
1125 | * simply not be populated automatically. The user will have to | |
1126 | * make sure to set the trace packet header fields himself | |
1127 | * before flushing. | |
1128 | */ | |
b3376dd9 | 1129 | ret = auto_populate_packet_header(stream); |
319fd969 | 1130 | if (ret) { |
b3376dd9 | 1131 | BT_LOGW_STR("Cannot automatically populate the stream's packet header."); |
319fd969 PP |
1132 | goto error; |
1133 | } | |
1134 | ||
1135 | /* Create file associated with this stream */ | |
1136 | fd = create_stream_file(writer, stream); | |
1137 | if (fd < 0) { | |
19abc2c6 | 1138 | BT_LOGW_STR("Cannot create stream file."); |
319fd969 PP |
1139 | goto error; |
1140 | } | |
1141 | ||
19abc2c6 | 1142 | set_stream_fd(stream, fd); |
319fd969 PP |
1143 | |
1144 | /* Freeze the writer */ | |
19abc2c6 | 1145 | BT_LOGD_STR("Freezing stream's CTF writer."); |
319fd969 PP |
1146 | bt_ctf_writer_freeze(writer); |
1147 | } else { | |
1148 | /* Non-writer stream indicated by a negative FD */ | |
19abc2c6 | 1149 | set_stream_fd(stream, -1); |
3230ee6b PP |
1150 | stream->comp_cur_port = g_hash_table_new(g_direct_hash, |
1151 | g_direct_equal); | |
1152 | if (!stream->comp_cur_port) { | |
19abc2c6 | 1153 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
3230ee6b PP |
1154 | goto error; |
1155 | } | |
d246b111 | 1156 | } |
319fd969 PP |
1157 | |
1158 | /* Add this stream to the trace's streams */ | |
1159 | g_ptr_array_add(trace->streams, stream); | |
19abc2c6 | 1160 | BT_LOGD("Created stream object: addr=%p", stream); |
cfe11c58 PP |
1161 | goto end; |
1162 | ||
83509119 JG |
1163 | error: |
1164 | BT_PUT(stream); | |
cfe11c58 PP |
1165 | |
1166 | end: | |
1167 | bt_put(writer); | |
1168 | return stream; | |
1169 | } | |
1170 | ||
50842bdc PP |
1171 | struct bt_stream *bt_stream_create_with_id( |
1172 | struct bt_stream_class *stream_class, | |
cfe11c58 PP |
1173 | const char *name, uint64_t id_param) |
1174 | { | |
50842bdc PP |
1175 | struct bt_trace *trace; |
1176 | struct bt_stream *stream = NULL; | |
cfe11c58 PP |
1177 | int64_t id = (int64_t) id_param; |
1178 | ||
1179 | if (!stream_class) { | |
1180 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
1181 | goto end; | |
1182 | } | |
1183 | ||
1184 | if (id < 0) { | |
1185 | BT_LOGW("Invalid parameter: invalid stream's ID: " | |
1186 | "name=\"%s\", id=%" PRIu64, | |
1187 | name, id_param); | |
1188 | goto end; | |
1189 | } | |
1190 | ||
50842bdc | 1191 | trace = bt_stream_class_borrow_trace(stream_class); |
cfe11c58 PP |
1192 | if (!trace) { |
1193 | BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: " | |
1194 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
1195 | "stream-name=\"%s\", stream-id=%" PRIu64, | |
50842bdc | 1196 | stream_class, bt_stream_class_get_name(stream_class), |
cfe11c58 PP |
1197 | name, id_param); |
1198 | goto end; | |
1199 | } | |
1200 | ||
1201 | if (trace->is_created_by_writer) { | |
50842bdc | 1202 | BT_LOGW("Invalid parameter: cannot create a CTF writer stream with this function; use bt_stream_create(): " |
cfe11c58 PP |
1203 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
1204 | "stream-name=\"%s\", stream-id=%" PRIu64, | |
50842bdc | 1205 | stream_class, bt_stream_class_get_name(stream_class), |
cfe11c58 PP |
1206 | name, id_param); |
1207 | goto end; | |
1208 | } | |
1209 | ||
50842bdc | 1210 | stream = bt_stream_create_with_id_no_check(stream_class, |
cfe11c58 PP |
1211 | name, id_param); |
1212 | ||
1213 | end: | |
83509119 | 1214 | return stream; |
273b65be JG |
1215 | } |
1216 | ||
50842bdc PP |
1217 | struct bt_stream *bt_stream_create( |
1218 | struct bt_stream_class *stream_class, | |
cfe11c58 PP |
1219 | const char *name) |
1220 | { | |
50842bdc | 1221 | return bt_stream_create_with_id_no_check(stream_class, |
cfe11c58 PP |
1222 | name, -1ULL); |
1223 | } | |
1224 | ||
50842bdc PP |
1225 | struct bt_stream_class *bt_stream_get_class( |
1226 | struct bt_stream *stream) | |
3baf0856 | 1227 | { |
50842bdc | 1228 | struct bt_stream_class *stream_class = NULL; |
3baf0856 JG |
1229 | |
1230 | if (!stream) { | |
19abc2c6 | 1231 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
3baf0856 JG |
1232 | goto end; |
1233 | } | |
1234 | ||
1235 | stream_class = stream->stream_class; | |
83509119 | 1236 | bt_get(stream_class); |
3baf0856 JG |
1237 | end: |
1238 | return stream_class; | |
1239 | } | |
1240 | ||
50842bdc PP |
1241 | int bt_stream_get_discarded_events_count( |
1242 | struct bt_stream *stream, uint64_t *count) | |
a78a2e25 | 1243 | { |
37949e9e | 1244 | int ret = 0; |
a78a2e25 | 1245 | |
19abc2c6 PP |
1246 | if (!stream) { |
1247 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
37949e9e | 1248 | ret = -1; |
19abc2c6 PP |
1249 | goto end; |
1250 | } | |
1251 | ||
1252 | if (!count) { | |
1253 | BT_LOGW_STR("Invalid parameter: count is NULL."); | |
37949e9e | 1254 | ret = -1; |
19abc2c6 PP |
1255 | goto end; |
1256 | } | |
1257 | ||
b3376dd9 PP |
1258 | if (stream->pos.fd < 0) { |
1259 | BT_LOGW("Invalid parameter: stream is not a CTF writer stream: " | |
19abc2c6 | 1260 | "stream-addr=%p, stream-name=\"%s\"", |
50842bdc | 1261 | stream, bt_stream_get_name(stream)); |
37949e9e | 1262 | ret = -1; |
19abc2c6 PP |
1263 | goto end; |
1264 | } | |
1265 | ||
b3376dd9 PP |
1266 | *count = (uint64_t) stream->discarded_events; |
1267 | ||
1268 | end: | |
1269 | return ret; | |
1270 | } | |
1271 | ||
1272 | static | |
50842bdc | 1273 | int set_packet_context_events_discarded_field(struct bt_stream *stream, |
b3376dd9 PP |
1274 | uint64_t count) |
1275 | { | |
1276 | int ret = 0; | |
50842bdc | 1277 | struct bt_field *events_discarded_field = NULL; |
b3376dd9 PP |
1278 | |
1279 | if (!stream->packet_context) { | |
a78a2e25 JG |
1280 | goto end; |
1281 | } | |
1282 | ||
50842bdc | 1283 | events_discarded_field = bt_field_structure_get_field_by_name( |
12c8a1a3 JG |
1284 | stream->packet_context, "events_discarded"); |
1285 | if (!events_discarded_field) { | |
12c8a1a3 JG |
1286 | goto end; |
1287 | } | |
1288 | ||
50842bdc | 1289 | ret = bt_field_unsigned_integer_set_value( |
b3376dd9 PP |
1290 | events_discarded_field, count); |
1291 | if (ret) { | |
1292 | BT_LOGW("Cannot set packet context's `events_discarded` field: " | |
1293 | "field-addr=%p, value=%" PRIu64, | |
12c8a1a3 | 1294 | events_discarded_field, count); |
b3376dd9 | 1295 | goto end; |
12c8a1a3 | 1296 | } |
b3376dd9 | 1297 | |
a78a2e25 | 1298 | end: |
83509119 | 1299 | bt_put(events_discarded_field); |
a78a2e25 JG |
1300 | return ret; |
1301 | } | |
1302 | ||
50842bdc | 1303 | void bt_stream_append_discarded_events(struct bt_stream *stream, |
273b65be JG |
1304 | uint64_t event_count) |
1305 | { | |
12c8a1a3 | 1306 | int ret; |
12c8a1a3 | 1307 | uint64_t new_count; |
50842bdc | 1308 | struct bt_field *events_discarded_field = NULL; |
12c8a1a3 | 1309 | |
19abc2c6 PP |
1310 | if (!stream) { |
1311 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1312 | goto end; | |
1313 | } | |
1314 | ||
1315 | BT_LOGV("Appending discarded events to stream: " | |
1316 | "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64, | |
50842bdc | 1317 | stream, bt_stream_get_name(stream), event_count); |
19abc2c6 PP |
1318 | |
1319 | if (!stream->packet_context) { | |
1320 | BT_LOGW_STR("Invalid parameter: stream has no packet context field."); | |
1321 | goto end; | |
1322 | } | |
1323 | ||
1324 | if (stream->pos.fd < 0) { | |
1325 | BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream."); | |
12c8a1a3 JG |
1326 | goto end; |
1327 | } | |
1328 | ||
50842bdc | 1329 | events_discarded_field = bt_field_structure_get_field_by_name( |
12c8a1a3 JG |
1330 | stream->packet_context, "events_discarded"); |
1331 | if (!events_discarded_field) { | |
b3376dd9 | 1332 | BT_LOGW_STR("No field named `events_discarded` in stream's packet context."); |
12c8a1a3 JG |
1333 | goto end; |
1334 | } | |
1335 | ||
b3376dd9 PP |
1336 | new_count = stream->discarded_events + event_count; |
1337 | if (new_count < stream->discarded_events) { | |
1338 | BT_LOGW("New discarded events count is less than the stream's current discarded events count: " | |
1339 | "cur-count=%" PRIu64 ", new-count=%" PRIu64, | |
1340 | stream->discarded_events, new_count); | |
1341 | goto end; | |
1342 | } | |
1343 | ||
1344 | ret = set_packet_context_events_discarded_field(stream, new_count); | |
1345 | if (ret) { | |
1346 | /* set_packet_context_events_discarded_field() logs errors */ | |
1347 | goto end; | |
12c8a1a3 JG |
1348 | } |
1349 | ||
b3376dd9 | 1350 | stream->discarded_events = new_count; |
19abc2c6 PP |
1351 | BT_LOGV("Appended discarded events to stream: " |
1352 | "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64, | |
50842bdc | 1353 | stream, bt_stream_get_name(stream), event_count); |
19abc2c6 | 1354 | |
12c8a1a3 | 1355 | end: |
83509119 | 1356 | bt_put(events_discarded_field); |
273b65be JG |
1357 | } |
1358 | ||
50842bdc PP |
1359 | static int auto_populate_event_header(struct bt_stream *stream, |
1360 | struct bt_event *event) | |
ac0c6bdd PP |
1361 | { |
1362 | int ret = 0; | |
50842bdc PP |
1363 | struct bt_field *id_field = NULL, *timestamp_field = NULL; |
1364 | struct bt_clock_class *mapped_clock_class = NULL; | |
fa2061de | 1365 | int64_t event_class_id; |
ac0c6bdd | 1366 | |
19abc2c6 PP |
1367 | assert(event); |
1368 | ||
06b019a7 JD |
1369 | if (!event->event_header) { |
1370 | goto end; | |
1371 | } | |
1372 | ||
19abc2c6 PP |
1373 | if (event->frozen) { |
1374 | BT_LOGW_STR("Cannot populate event header field: event is frozen."); | |
ac0c6bdd PP |
1375 | ret = -1; |
1376 | goto end; | |
1377 | } | |
1378 | ||
b3376dd9 | 1379 | BT_LOGV("Automatically populating event's header field: " |
19abc2c6 | 1380 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p", |
50842bdc | 1381 | stream, bt_stream_get_name(stream), event); |
19abc2c6 | 1382 | |
50842bdc PP |
1383 | id_field = bt_field_structure_get_field_by_name(event->event_header, "id"); |
1384 | event_class_id = bt_event_class_get_id(event->event_class); | |
2a03740b PP |
1385 | assert(event_class_id >= 0); |
1386 | ||
50842bdc | 1387 | if (id_field && bt_field_type_is_integer(id_field->type)) { |
19abc2c6 | 1388 | ret = set_integer_field_value(id_field, event_class_id); |
ac0c6bdd | 1389 | if (ret) { |
19abc2c6 PP |
1390 | BT_LOGW("Cannot set event header's `id` field's value: " |
1391 | "addr=%p, value=%" PRIu64, id_field, | |
1392 | event_class_id); | |
ac0c6bdd PP |
1393 | goto end; |
1394 | } | |
1395 | } | |
1396 | ||
1397 | /* | |
1398 | * The conditions to automatically set the timestamp are: | |
1399 | * | |
1400 | * 1. The event header field "timestamp" exists and is an | |
1401 | * integer field. | |
1402 | * 2. This stream's class has a registered clock (set with | |
50842bdc | 1403 | * bt_stream_class_set_clock()). |
2a03740b | 1404 | * 3. The "timestamp" field is not set. |
ac0c6bdd | 1405 | */ |
2a03740b PP |
1406 | timestamp_field = bt_field_structure_get_field_by_name( |
1407 | event->event_header, "timestamp"); | |
b3376dd9 | 1408 | if (timestamp_field && stream->stream_class->clock && |
2a03740b PP |
1409 | bt_field_type_is_integer(timestamp_field->type) && |
1410 | !bt_field_is_set(timestamp_field)) { | |
50842bdc | 1411 | struct bt_clock_class *stream_class_clock_class = |
ac0c6bdd | 1412 | stream->stream_class->clock->clock_class; |
ac0c6bdd | 1413 | |
ac0c6bdd | 1414 | mapped_clock_class = |
50842bdc | 1415 | bt_field_type_integer_get_mapped_clock_class( |
b3376dd9 | 1416 | timestamp_field->type); |
2a03740b | 1417 | if (mapped_clock_class) { |
ac0c6bdd PP |
1418 | uint64_t timestamp; |
1419 | ||
2a03740b | 1420 | assert(mapped_clock_class == stream_class_clock_class); |
ac0c6bdd PP |
1421 | ret = bt_ctf_clock_get_value( |
1422 | stream->stream_class->clock, | |
1423 | ×tamp); | |
19abc2c6 | 1424 | assert(ret == 0); |
ac0c6bdd PP |
1425 | ret = set_integer_field_value(timestamp_field, |
1426 | timestamp); | |
1427 | if (ret) { | |
19abc2c6 PP |
1428 | BT_LOGW("Cannot set event header's `timestamp` field's value: " |
1429 | "addr=%p, value=%" PRIu64, | |
1430 | timestamp_field, timestamp); | |
ac0c6bdd PP |
1431 | goto end; |
1432 | } | |
1433 | } | |
1434 | } | |
1435 | ||
b3376dd9 | 1436 | BT_LOGV("Automatically populated event's header field: " |
19abc2c6 | 1437 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p", |
50842bdc | 1438 | stream, bt_stream_get_name(stream), event); |
19abc2c6 | 1439 | |
ac0c6bdd PP |
1440 | end: |
1441 | bt_put(id_field); | |
1442 | bt_put(timestamp_field); | |
1443 | bt_put(mapped_clock_class); | |
1444 | return ret; | |
1445 | } | |
1446 | ||
50842bdc PP |
1447 | int bt_stream_append_event(struct bt_stream *stream, |
1448 | struct bt_event *event) | |
273b65be JG |
1449 | { |
1450 | int ret = 0; | |
273b65be | 1451 | |
19abc2c6 PP |
1452 | if (!stream) { |
1453 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1454 | ret = -1; | |
1455 | goto end; | |
1456 | } | |
1457 | ||
1458 | if (!event) { | |
1459 | BT_LOGW_STR("Invalid parameter: event is NULL."); | |
1460 | ret = -1; | |
1461 | goto end; | |
1462 | } | |
1463 | ||
1464 | if (stream->pos.fd < 0) { | |
1465 | BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream."); | |
273b65be JG |
1466 | ret = -1; |
1467 | goto end; | |
1468 | } | |
1469 | ||
19abc2c6 PP |
1470 | BT_LOGV("Appending event to stream: " |
1471 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p, " | |
1472 | "event-class-name=\"%s\", event-class-id=%" PRId64, | |
50842bdc PP |
1473 | stream, bt_stream_get_name(stream), event, |
1474 | bt_event_class_get_name(bt_event_borrow_event_class(event)), | |
1475 | bt_event_class_get_id(bt_event_borrow_event_class(event))); | |
19abc2c6 | 1476 | |
fa29ba83 PP |
1477 | /* |
1478 | * The event is not supposed to have a parent stream at this | |
1479 | * point. The only other way an event can have a parent stream | |
1480 | * is if it was assigned when setting a packet to the event, | |
1481 | * in which case the packet's stream is not a writer stream, | |
1482 | * and thus the user is trying to append an event which belongs | |
1483 | * to another stream. | |
1484 | */ | |
1485 | if (event->base.parent) { | |
1486 | ret = -1; | |
1487 | goto end; | |
1488 | } | |
1489 | ||
e6a8e8e4 | 1490 | bt_object_set_parent(event, stream); |
19abc2c6 | 1491 | BT_LOGV_STR("Automatically populating the header of the event to append."); |
ac0c6bdd | 1492 | ret = auto_populate_event_header(stream, event); |
662e778c | 1493 | if (ret) { |
19abc2c6 | 1494 | /* auto_populate_event_header() reports errors */ |
37f30168 | 1495 | goto error; |
662e778c JG |
1496 | } |
1497 | ||
5fd2e9fd | 1498 | /* Make sure the various scopes of the event are set */ |
19abc2c6 | 1499 | BT_LOGV_STR("Validating event to append."); |
50842bdc | 1500 | ret = bt_event_validate(event); |
273b65be | 1501 | if (ret) { |
37f30168 | 1502 | goto error; |
273b65be JG |
1503 | } |
1504 | ||
0d688c15 | 1505 | /* Save the new event and freeze it */ |
19abc2c6 | 1506 | BT_LOGV_STR("Freezing the event to append."); |
50842bdc | 1507 | bt_event_freeze(event); |
273b65be | 1508 | g_ptr_array_add(stream->events, event); |
5fd2e9fd | 1509 | |
e6a8e8e4 JG |
1510 | /* |
1511 | * Event had to hold a reference to its event class as long as it wasn't | |
1512 | * part of the same trace hierarchy. From now on, the event and its | |
1513 | * class share the same lifetime guarantees and the reference is no | |
1514 | * longer needed. | |
1515 | */ | |
19abc2c6 | 1516 | BT_LOGV_STR("Putting the event's class."); |
e6a8e8e4 | 1517 | bt_put(event->event_class); |
19abc2c6 PP |
1518 | BT_LOGV("Appended event to stream: " |
1519 | "stream-addr=%p, stream-name=\"%s\", event-addr=%p, " | |
1520 | "event-class-name=\"%s\", event-class-id=%" PRId64, | |
50842bdc PP |
1521 | stream, bt_stream_get_name(stream), event, |
1522 | bt_event_class_get_name(bt_event_borrow_event_class(event)), | |
1523 | bt_event_class_get_id(bt_event_borrow_event_class(event))); | |
37f30168 | 1524 | |
273b65be | 1525 | end: |
37f30168 PP |
1526 | return ret; |
1527 | ||
1528 | error: | |
1529 | /* | |
1530 | * Orphan the event; we were not successful in associating it to | |
1531 | * a stream. | |
1532 | */ | |
1533 | bt_object_set_parent(event, NULL); | |
1534 | ||
273b65be JG |
1535 | return ret; |
1536 | } | |
1537 | ||
50842bdc | 1538 | struct bt_field *bt_stream_get_packet_context(struct bt_stream *stream) |
12c8a1a3 | 1539 | { |
50842bdc | 1540 | struct bt_field *packet_context = NULL; |
12c8a1a3 | 1541 | |
19abc2c6 PP |
1542 | if (!stream) { |
1543 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1544 | goto end; | |
1545 | } | |
1546 | ||
1547 | if (stream->pos.fd < 0) { | |
1548 | BT_LOGW("Invalid parameter: stream is not a CTF writer stream: " | |
1549 | "stream-addr=%p, stream-name=\"%s\"", stream, | |
50842bdc | 1550 | bt_stream_get_name(stream)); |
12c8a1a3 JG |
1551 | goto end; |
1552 | } | |
1553 | ||
1554 | packet_context = stream->packet_context; | |
12c8a1a3 | 1555 | if (packet_context) { |
83509119 | 1556 | bt_get(packet_context); |
12c8a1a3 | 1557 | } |
34629a55 | 1558 | end: |
12c8a1a3 JG |
1559 | return packet_context; |
1560 | } | |
1561 | ||
50842bdc PP |
1562 | int bt_stream_set_packet_context(struct bt_stream *stream, |
1563 | struct bt_field *field) | |
12c8a1a3 JG |
1564 | { |
1565 | int ret = 0; | |
50842bdc | 1566 | struct bt_field_type *field_type; |
12c8a1a3 | 1567 | |
19abc2c6 PP |
1568 | if (!stream) { |
1569 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1570 | ret = -1; | |
1571 | goto end; | |
1572 | } | |
1573 | ||
1574 | if (stream->pos.fd < 0) { | |
1575 | BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream."); | |
12c8a1a3 JG |
1576 | ret = -1; |
1577 | goto end; | |
1578 | } | |
1579 | ||
50842bdc PP |
1580 | field_type = bt_field_get_type(field); |
1581 | if (bt_field_type_compare(field_type, | |
09840de5 | 1582 | stream->stream_class->packet_context_type)) { |
19abc2c6 PP |
1583 | BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: " |
1584 | "stream-addr=%p, stream-name=\"%s\", " | |
1585 | "packet-context-field-addr=%p, " | |
1586 | "packet-context-ft-addr=%p", | |
50842bdc | 1587 | stream, bt_stream_get_name(stream), |
19abc2c6 | 1588 | field, field_type); |
12c8a1a3 JG |
1589 | ret = -1; |
1590 | goto end; | |
1591 | } | |
1592 | ||
83509119 | 1593 | bt_put(field_type); |
83509119 | 1594 | bt_put(stream->packet_context); |
835b2d10 | 1595 | stream->packet_context = bt_get(field); |
19abc2c6 PP |
1596 | BT_LOGV("Set stream's packet context field: " |
1597 | "stream-addr=%p, stream-name=\"%s\", " | |
1598 | "packet-context-field-addr=%p", | |
50842bdc | 1599 | stream, bt_stream_get_name(stream), field); |
12c8a1a3 JG |
1600 | end: |
1601 | return ret; | |
1602 | } | |
1603 | ||
50842bdc | 1604 | struct bt_field *bt_stream_get_packet_header(struct bt_stream *stream) |
263a7df5 | 1605 | { |
50842bdc | 1606 | struct bt_field *packet_header = NULL; |
263a7df5 | 1607 | |
19abc2c6 PP |
1608 | if (!stream) { |
1609 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1610 | goto end; | |
1611 | } | |
1612 | ||
1613 | if (stream->pos.fd < 0) { | |
1614 | BT_LOGW("Invalid parameter: stream is not a CTF writer stream: " | |
1615 | "stream-addr=%p, stream-name=\"%s\"", stream, | |
50842bdc | 1616 | bt_stream_get_name(stream)); |
263a7df5 JG |
1617 | goto end; |
1618 | } | |
1619 | ||
1620 | packet_header = stream->packet_header; | |
1621 | if (packet_header) { | |
83509119 | 1622 | bt_get(packet_header); |
263a7df5 JG |
1623 | } |
1624 | end: | |
1625 | return packet_header; | |
1626 | } | |
1627 | ||
50842bdc PP |
1628 | int bt_stream_set_packet_header(struct bt_stream *stream, |
1629 | struct bt_field *field) | |
263a7df5 JG |
1630 | { |
1631 | int ret = 0; | |
50842bdc PP |
1632 | struct bt_trace *trace = NULL; |
1633 | struct bt_field_type *field_type = NULL; | |
263a7df5 | 1634 | |
19abc2c6 PP |
1635 | if (!stream) { |
1636 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1637 | ret = -1; | |
1638 | goto end; | |
1639 | } | |
1640 | ||
1641 | if (stream->pos.fd < 0) { | |
1642 | BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream."); | |
263a7df5 JG |
1643 | ret = -1; |
1644 | goto end; | |
1645 | } | |
1646 | ||
50842bdc | 1647 | trace = (struct bt_trace *) bt_object_get_parent(stream); |
b3376dd9 PP |
1648 | |
1649 | if (!field) { | |
1650 | if (trace->packet_header_type) { | |
1651 | BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: " | |
1652 | "stream-addr=%p, stream-name=\"%s\", " | |
1653 | "packet-header-field-addr=%p, " | |
1654 | "expected-ft-addr=%p", | |
50842bdc | 1655 | stream, bt_stream_get_name(stream), |
b3376dd9 PP |
1656 | field, trace->packet_header_type); |
1657 | ret = -1; | |
1658 | goto end; | |
1659 | } | |
1660 | ||
1661 | goto skip_validation; | |
1662 | } | |
1663 | ||
50842bdc | 1664 | field_type = bt_field_get_type(field); |
b3376dd9 PP |
1665 | assert(field_type); |
1666 | ||
50842bdc | 1667 | if (bt_field_type_compare(field_type, trace->packet_header_type)) { |
19abc2c6 PP |
1668 | BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: " |
1669 | "stream-addr=%p, stream-name=\"%s\", " | |
1670 | "packet-header-field-addr=%p, " | |
1671 | "packet-header-ft-addr=%p", | |
50842bdc | 1672 | stream, bt_stream_get_name(stream), |
19abc2c6 | 1673 | field, field_type); |
263a7df5 JG |
1674 | ret = -1; |
1675 | goto end; | |
1676 | } | |
1677 | ||
b3376dd9 | 1678 | skip_validation: |
83509119 | 1679 | bt_put(stream->packet_header); |
835b2d10 | 1680 | stream->packet_header = bt_get(field); |
19abc2c6 PP |
1681 | BT_LOGV("Set stream's packet header field: " |
1682 | "stream-addr=%p, stream-name=\"%s\", " | |
1683 | "packet-header-field-addr=%p", | |
50842bdc | 1684 | stream, bt_stream_get_name(stream), field); |
263a7df5 | 1685 | end: |
e6a8e8e4 | 1686 | BT_PUT(trace); |
83509119 | 1687 | bt_put(field_type); |
263a7df5 JG |
1688 | return ret; |
1689 | } | |
1690 | ||
c9af50d1 | 1691 | static |
50842bdc | 1692 | void reset_structure_field(struct bt_field *structure, const char *name) |
c9af50d1 | 1693 | { |
50842bdc | 1694 | struct bt_field *member; |
c9af50d1 | 1695 | |
50842bdc | 1696 | member = bt_field_structure_get_field_by_name(structure, name); |
db1af8cc | 1697 | if (member) { |
50842bdc | 1698 | (void) bt_field_reset(member); |
db1af8cc JD |
1699 | bt_put(member); |
1700 | } | |
c9af50d1 JG |
1701 | } |
1702 | ||
50842bdc | 1703 | int bt_stream_flush(struct bt_stream *stream) |
273b65be JG |
1704 | { |
1705 | int ret = 0; | |
1706 | size_t i; | |
50842bdc PP |
1707 | struct bt_stream_pos packet_context_pos; |
1708 | struct bt_trace *trace; | |
1709 | enum bt_byte_order native_byte_order; | |
1c1d572f | 1710 | bool has_packet_size = false; |
273b65be | 1711 | |
19abc2c6 PP |
1712 | if (!stream) { |
1713 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
1714 | ret = -1; | |
f0ea9318 | 1715 | goto end_no_stream; |
19abc2c6 PP |
1716 | } |
1717 | ||
1718 | if (stream->pos.fd < 0) { | |
1719 | BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream."); | |
273b65be JG |
1720 | ret = -1; |
1721 | goto end; | |
1722 | } | |
1723 | ||
1c1d572f | 1724 | if (stream->packet_context) { |
50842bdc | 1725 | struct bt_field *packet_size_field; |
b3376dd9 | 1726 | |
50842bdc | 1727 | packet_size_field = bt_field_structure_get_field_by_name( |
1c1d572f JD |
1728 | stream->packet_context, "packet_size"); |
1729 | has_packet_size = (packet_size_field != NULL); | |
1730 | bt_put(packet_size_field); | |
1731 | } | |
1732 | ||
1733 | if (stream->flushed_packet_count == 1) { | |
b3376dd9 PP |
1734 | if (!stream->packet_context) { |
1735 | BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once."); | |
1736 | ret = -1; | |
1737 | goto end; | |
1738 | } | |
1739 | ||
1c1d572f | 1740 | if (!has_packet_size) { |
b3376dd9 PP |
1741 | BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once."); |
1742 | ret = -1; | |
1743 | goto end; | |
1744 | } | |
664f50c8 JG |
1745 | } |
1746 | ||
19abc2c6 PP |
1747 | BT_LOGV("Flushing stream's current packet: stream-addr=%p, " |
1748 | "stream-name=\"%s\", packet-index=%u", stream, | |
50842bdc PP |
1749 | bt_stream_get_name(stream), stream->flushed_packet_count); |
1750 | trace = bt_stream_class_borrow_trace(stream->stream_class); | |
dc3fffef | 1751 | assert(trace); |
50842bdc | 1752 | native_byte_order = bt_trace_get_native_byte_order(trace); |
d246b111 | 1753 | |
b3376dd9 | 1754 | ret = auto_populate_packet_header(stream); |
d246b111 | 1755 | if (ret) { |
b3376dd9 PP |
1756 | BT_LOGW_STR("Cannot automatically populate the stream's packet header field."); |
1757 | ret = -1; | |
d246b111 | 1758 | goto end; |
273b65be JG |
1759 | } |
1760 | ||
2a03740b | 1761 | ret = auto_populate_packet_context(stream, true); |
b3376dd9 PP |
1762 | if (ret) { |
1763 | BT_LOGW_STR("Cannot automatically populate the stream's packet context field."); | |
1764 | ret = -1; | |
1765 | goto end; | |
1766 | } | |
98edd02c | 1767 | |
b3376dd9 PP |
1768 | /* mmap the next packet */ |
1769 | BT_LOGV("Seeking to the next packet: pos-offset=%" PRId64, | |
1770 | stream->pos.offset); | |
50842bdc | 1771 | bt_stream_pos_packet_seek(&stream->pos, 0, SEEK_CUR); |
b3376dd9 | 1772 | assert(stream->pos.packet_size % 8 == 0); |
273b65be | 1773 | |
b3376dd9 PP |
1774 | if (stream->packet_header) { |
1775 | BT_LOGV_STR("Serializing packet header field."); | |
50842bdc | 1776 | ret = bt_field_serialize(stream->packet_header, &stream->pos, |
b3376dd9 PP |
1777 | native_byte_order); |
1778 | if (ret) { | |
1779 | BT_LOGW("Cannot serialize stream's packet header field: " | |
1780 | "field-addr=%p", stream->packet_header); | |
98edd02c JG |
1781 | goto end; |
1782 | } | |
b3376dd9 | 1783 | } |
12c8a1a3 | 1784 | |
b3376dd9 | 1785 | if (stream->packet_context) { |
10cb7b41 JG |
1786 | /* Write packet context */ |
1787 | memcpy(&packet_context_pos, &stream->pos, | |
dc3fffef | 1788 | sizeof(packet_context_pos)); |
19abc2c6 | 1789 | BT_LOGV_STR("Serializing packet context field."); |
50842bdc | 1790 | ret = bt_field_serialize(stream->packet_context, |
dc3fffef | 1791 | &stream->pos, native_byte_order); |
98edd02c | 1792 | if (ret) { |
b3376dd9 | 1793 | BT_LOGW("Cannot serialize stream's packet context field: " |
19abc2c6 | 1794 | "field-addr=%p", stream->packet_context); |
98edd02c JG |
1795 | goto end; |
1796 | } | |
12c8a1a3 JG |
1797 | } |
1798 | ||
19abc2c6 PP |
1799 | BT_LOGV("Serializing events: count=%u", stream->events->len); |
1800 | ||
273b65be | 1801 | for (i = 0; i < stream->events->len; i++) { |
50842bdc | 1802 | struct bt_event *event = g_ptr_array_index( |
273b65be | 1803 | stream->events, i); |
50842bdc PP |
1804 | struct bt_event_class *event_class = |
1805 | bt_event_borrow_event_class(event); | |
19abc2c6 | 1806 | |
b3376dd9 | 1807 | BT_LOGV("Serializing event: index=%zu, event-addr=%p, " |
19abc2c6 PP |
1808 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " |
1809 | "pos-offset=%" PRId64 ", packet-size=%" PRIu64, | |
50842bdc PP |
1810 | i, event, bt_event_class_get_name(event_class), |
1811 | bt_event_class_get_id(event_class), | |
19abc2c6 | 1812 | stream->pos.offset, stream->pos.packet_size); |
273b65be | 1813 | |
273b65be | 1814 | /* Write event header */ |
06b019a7 JD |
1815 | if (event->event_header) { |
1816 | BT_LOGV_STR("Serializing event's header field."); | |
50842bdc | 1817 | ret = bt_field_serialize(event->event_header, |
06b019a7 JD |
1818 | &stream->pos, native_byte_order); |
1819 | if (ret) { | |
1820 | BT_LOGW("Cannot serialize event's header field: " | |
1821 | "field-addr=%p", event->event_header); | |
1822 | goto end; | |
1823 | } | |
273b65be JG |
1824 | } |
1825 | ||
8bfa3f9c | 1826 | /* Write stream event context */ |
5fd2e9fd | 1827 | if (event->stream_event_context) { |
19abc2c6 | 1828 | BT_LOGV_STR("Serializing event's stream event context field."); |
50842bdc | 1829 | ret = bt_field_serialize( |
dc3fffef PP |
1830 | event->stream_event_context, &stream->pos, |
1831 | native_byte_order); | |
8bfa3f9c | 1832 | if (ret) { |
b3376dd9 | 1833 | BT_LOGW("Cannot serialize event's stream event context field: " |
19abc2c6 | 1834 | "field-addr=%p", event->stream_event_context); |
8bfa3f9c JG |
1835 | goto end; |
1836 | } | |
1837 | } | |
1838 | ||
273b65be | 1839 | /* Write event content */ |
50842bdc | 1840 | ret = bt_event_serialize(event, &stream->pos, |
dc3fffef | 1841 | native_byte_order); |
273b65be | 1842 | if (ret) { |
50842bdc | 1843 | /* bt_event_serialize() logs errors */ |
273b65be JG |
1844 | goto end; |
1845 | } | |
1846 | } | |
1847 | ||
1c1d572f JD |
1848 | if (!has_packet_size && stream->pos.offset % 8 != 0) { |
1849 | BT_LOGW("Stream's packet context field type has no `packet_size` field, " | |
1850 | "but current content size is not a multiple of 8 bits: " | |
1851 | "content-size=%" PRId64 ", " | |
1852 | "packet-size=%" PRIu64, | |
1853 | stream->pos.offset, | |
1854 | stream->pos.packet_size); | |
1855 | ret = -1; | |
1856 | goto end; | |
1857 | } | |
1858 | ||
b3376dd9 | 1859 | assert(stream->pos.packet_size % 8 == 0); |
0686ef94 | 1860 | |
1c1d572f JD |
1861 | /* |
1862 | * Remove extra padding bytes. | |
1863 | */ | |
1864 | stream->pos.packet_size = (stream->pos.offset + 7) & ~7; | |
1865 | ||
98edd02c JG |
1866 | if (stream->packet_context) { |
1867 | /* | |
b3376dd9 PP |
1868 | * The whole packet is serialized at this point. Make sure that, |
1869 | * if `packet_size` is missing, the current content size is | |
1870 | * equal to the current packet size. | |
98edd02c | 1871 | */ |
50842bdc | 1872 | struct bt_field *field = bt_field_structure_get_field_by_name( |
b3376dd9 PP |
1873 | stream->packet_context, "content_size"); |
1874 | ||
1875 | bt_put(field); | |
1876 | if (!field) { | |
1877 | if (stream->pos.offset != stream->pos.packet_size) { | |
1878 | BT_LOGW("Stream's packet context's `content_size` field is missing, " | |
1879 | "but current packet's content size is not equal to its packet size: " | |
1880 | "content-size=%" PRId64 ", " | |
1881 | "packet-size=%" PRIu64, | |
1882 | stream->pos.offset, | |
1883 | stream->pos.packet_size); | |
1884 | ret = -1; | |
c9af50d1 JG |
1885 | goto end; |
1886 | } | |
98edd02c | 1887 | } |
273b65be | 1888 | |
b3376dd9 PP |
1889 | /* |
1890 | * Overwrite the packet context now that the stream | |
1891 | * position's packet and content sizes have the correct | |
1892 | * values. | |
1893 | * | |
1894 | * Copy base_mma as the packet may have been remapped | |
1895 | * (e.g. when a packet is resized). | |
1896 | */ | |
1897 | packet_context_pos.base_mma = stream->pos.base_mma; | |
2a03740b | 1898 | ret = auto_populate_packet_context(stream, false); |
b3376dd9 PP |
1899 | if (ret) { |
1900 | BT_LOGW_STR("Cannot automatically populate the stream's packet context field."); | |
1901 | ret = -1; | |
1902 | goto end; | |
98edd02c | 1903 | } |
273b65be | 1904 | |
19abc2c6 | 1905 | BT_LOGV("Rewriting (serializing) packet context field."); |
50842bdc | 1906 | ret = bt_field_serialize(stream->packet_context, |
dc3fffef | 1907 | &packet_context_pos, native_byte_order); |
98edd02c | 1908 | if (ret) { |
b3376dd9 | 1909 | BT_LOGW("Cannot serialize stream's packet context field: " |
19abc2c6 | 1910 | "field-addr=%p", stream->packet_context); |
98edd02c JG |
1911 | goto end; |
1912 | } | |
273b65be JG |
1913 | } |
1914 | ||
1915 | g_ptr_array_set_size(stream->events, 0); | |
1916 | stream->flushed_packet_count++; | |
b3376dd9 | 1917 | stream->size += stream->pos.packet_size / CHAR_BIT; |
1c1d572f | 1918 | |
273b65be | 1919 | end: |
c9af50d1 | 1920 | /* Reset automatically-set fields. */ |
1c1d572f JD |
1921 | if (stream->packet_context) { |
1922 | reset_structure_field(stream->packet_context, "timestamp_begin"); | |
1923 | reset_structure_field(stream->packet_context, "timestamp_end"); | |
1924 | reset_structure_field(stream->packet_context, "packet_size"); | |
1925 | reset_structure_field(stream->packet_context, "content_size"); | |
1926 | reset_structure_field(stream->packet_context, "events_discarded"); | |
1927 | } | |
cd7d8fb7 JG |
1928 | |
1929 | if (ret < 0) { | |
1930 | /* | |
1931 | * We failed to write the packet. Its size is therefore set to 0 | |
1932 | * to ensure the next mapping is done in the same place rather | |
1933 | * than advancing by "stream->pos.packet_size", which would | |
1934 | * leave a corrupted packet in the trace. | |
1935 | */ | |
1936 | stream->pos.packet_size = 0; | |
19abc2c6 | 1937 | } else { |
b3376dd9 PP |
1938 | BT_LOGV("Flushed stream's current packet: content-size=%" PRId64 ", " |
1939 | "packet-size=%" PRIu64, | |
1940 | stream->pos.offset, stream->pos.packet_size); | |
cd7d8fb7 | 1941 | } |
f0ea9318 MD |
1942 | |
1943 | end_no_stream: | |
273b65be JG |
1944 | return ret; |
1945 | } | |
1946 | ||
19abc2c6 | 1947 | /* Pre-2.0 CTF writer backward compatibility */ |
50842bdc | 1948 | void bt_ctf_stream_get(struct bt_stream *stream) |
273b65be | 1949 | { |
83509119 | 1950 | bt_get(stream); |
273b65be JG |
1951 | } |
1952 | ||
19abc2c6 | 1953 | /* Pre-2.0 CTF writer backward compatibility */ |
50842bdc | 1954 | void bt_ctf_stream_put(struct bt_stream *stream) |
273b65be | 1955 | { |
83509119 | 1956 | bt_put(stream); |
273b65be JG |
1957 | } |
1958 | ||
1959 | static | |
50842bdc | 1960 | void bt_stream_destroy(struct bt_object *obj) |
273b65be | 1961 | { |
50842bdc | 1962 | struct bt_stream *stream; |
3230ee6b | 1963 | int i; |
273b65be | 1964 | |
50842bdc | 1965 | stream = container_of(obj, struct bt_stream, base); |
19abc2c6 | 1966 | BT_LOGD("Destroying stream object: addr=%p, name=\"%s\"", |
50842bdc | 1967 | stream, bt_stream_get_name(stream)); |
3230ee6b PP |
1968 | |
1969 | /* Call destroy listeners in reverse registration order */ | |
1970 | for (i = stream->destroy_listeners->len - 1; i >= 0; i--) { | |
50842bdc | 1971 | struct bt_stream_destroy_listener *listener = |
3230ee6b | 1972 | &g_array_index(stream->destroy_listeners, |
50842bdc | 1973 | struct bt_stream_destroy_listener, i); |
3230ee6b | 1974 | |
19abc2c6 PP |
1975 | BT_LOGD("Calling destroy listener: func=%p, data=%p, index=%d", |
1976 | listener->func, listener->data, i); | |
3230ee6b PP |
1977 | listener->func(stream, listener->data); |
1978 | } | |
1979 | ||
50842bdc | 1980 | (void) bt_stream_pos_fini(&stream->pos); |
0686ef94 JG |
1981 | if (stream->pos.fd >= 0) { |
1982 | int ret; | |
1983 | ||
1984 | /* | |
1985 | * Truncate the file's size to the minimum required to fit the | |
1986 | * last packet as we might have grown it too much on the last | |
1987 | * mmap. | |
1988 | */ | |
1989 | do { | |
1990 | ret = ftruncate(stream->pos.fd, stream->size); | |
1991 | } while (ret == -1 && errno == EINTR); | |
1992 | if (ret) { | |
c606e638 PP |
1993 | BT_LOGE_ERRNO("Failed to truncate stream file", |
1994 | ": ret=%d, size=%" PRIu64, | |
1995 | ret, (uint64_t) stream->size); | |
0686ef94 JG |
1996 | } |
1997 | ||
1998 | if (close(stream->pos.fd)) { | |
c606e638 PP |
1999 | BT_LOGE_ERRNO("Failed to close stream file", |
2000 | ": ret=%d", ret); | |
0686ef94 | 2001 | } |
9f56e450 | 2002 | } |
12c8a1a3 | 2003 | |
12c8a1a3 | 2004 | if (stream->events) { |
19abc2c6 | 2005 | BT_LOGD_STR("Putting events."); |
12c8a1a3 JG |
2006 | g_ptr_array_free(stream->events, TRUE); |
2007 | } | |
b71d7298 PP |
2008 | |
2009 | if (stream->name) { | |
2010 | g_string_free(stream->name, TRUE); | |
2011 | } | |
41ac640a | 2012 | |
3230ee6b PP |
2013 | if (stream->comp_cur_port) { |
2014 | GHashTableIter ht_iter; | |
2015 | gpointer comp_gptr, port_gptr; | |
2016 | ||
2017 | /* | |
2018 | * Since we're destroying the stream, remove the destroy | |
2019 | * listeners that it registered for each component in | |
2020 | * its component-port mapping hash table. Otherwise they | |
2021 | * would be called and the stream would be accessed once | |
2022 | * it's freed or another stream would be accessed. | |
2023 | */ | |
2024 | g_hash_table_iter_init(&ht_iter, stream->comp_cur_port); | |
2025 | ||
2026 | while (g_hash_table_iter_next(&ht_iter, &comp_gptr, &port_gptr)) { | |
2027 | assert(comp_gptr); | |
2028 | bt_component_remove_destroy_listener((void *) comp_gptr, | |
2029 | component_destroy_listener, stream); | |
2030 | } | |
2031 | ||
2032 | g_hash_table_destroy(stream->comp_cur_port); | |
2033 | } | |
2034 | ||
2035 | if (stream->destroy_listeners) { | |
2036 | g_array_free(stream->destroy_listeners, TRUE); | |
2037 | } | |
2038 | ||
19abc2c6 | 2039 | BT_LOGD_STR("Putting packet header field."); |
83509119 | 2040 | bt_put(stream->packet_header); |
19abc2c6 | 2041 | BT_LOGD_STR("Putting packet context field."); |
83509119 | 2042 | bt_put(stream->packet_context); |
273b65be JG |
2043 | g_free(stream); |
2044 | } | |
2045 | ||
273b65be | 2046 | static |
50842bdc | 2047 | int _set_structure_field_integer(struct bt_field *structure, char *name, |
c55a9f58 | 2048 | uint64_t value, bt_bool force) |
273b65be JG |
2049 | { |
2050 | int ret = 0; | |
50842bdc PP |
2051 | struct bt_field_type *field_type = NULL; |
2052 | struct bt_field *integer; | |
12c8a1a3 | 2053 | |
19abc2c6 PP |
2054 | assert(structure); |
2055 | assert(name); | |
273b65be | 2056 | |
50842bdc | 2057 | integer = bt_field_structure_get_field_by_name(structure, name); |
12c8a1a3 JG |
2058 | if (!integer) { |
2059 | /* Field not found, not an error. */ | |
19abc2c6 PP |
2060 | BT_LOGV("Field not found: struct-field-addr=%p, " |
2061 | "name=\"%s\", force=%d", structure, name, force); | |
12c8a1a3 JG |
2062 | goto end; |
2063 | } | |
2064 | ||
2065 | /* Make sure the payload has not already been set. */ | |
50842bdc | 2066 | if (!force && bt_field_is_set(integer)) { |
12c8a1a3 | 2067 | /* Payload already set, not an error */ |
19abc2c6 PP |
2068 | BT_LOGV("Field's payload is already set: struct-field-addr=%p, " |
2069 | "name=\"%s\", force=%d", structure, name, force); | |
12c8a1a3 JG |
2070 | goto end; |
2071 | } | |
2072 | ||
50842bdc | 2073 | field_type = bt_field_get_type(integer); |
d7b1ea66 | 2074 | assert(field_type); |
50842bdc | 2075 | if (bt_field_type_get_type_id(field_type) != BT_FIELD_TYPE_ID_INTEGER) { |
d7b1ea66 JG |
2076 | /* |
2077 | * The user most likely meant for us to populate this field | |
2078 | * automatically. However, we can only do this if the field | |
2079 | * is an integer. Return an error. | |
2080 | */ | |
19abc2c6 PP |
2081 | BT_LOGW("Invalid parameter: field's type is not an integer field type: " |
2082 | "field-addr=%p, ft-addr=%p, ft-id=%s", | |
2083 | integer, field_type, | |
50842bdc | 2084 | bt_field_type_id_string(field_type->id)); |
d7b1ea66 JG |
2085 | ret = -1; |
2086 | goto end; | |
2087 | } | |
2088 | ||
50842bdc PP |
2089 | if (bt_field_type_integer_is_signed(field_type)) { |
2090 | ret = bt_field_signed_integer_set_value(integer, | |
d7b1ea66 JG |
2091 | (int64_t) value); |
2092 | } else { | |
50842bdc | 2093 | ret = bt_field_unsigned_integer_set_value(integer, value); |
d7b1ea66 | 2094 | } |
af9296f3 | 2095 | ret = !ret ? 1 : ret; |
273b65be | 2096 | end: |
83509119 JG |
2097 | bt_put(integer); |
2098 | bt_put(field_type); | |
273b65be JG |
2099 | return ret; |
2100 | } | |
b71d7298 | 2101 | |
af9296f3 JG |
2102 | /* |
2103 | * Returns the following codes: | |
2104 | * 1 if the field was found and set, | |
2105 | * 0 if nothing was done (field not found, or was already set), | |
2106 | * <0 if an error was encoutered | |
2107 | */ | |
2108 | static | |
50842bdc | 2109 | int try_set_structure_field_integer(struct bt_field *structure, char *name, |
af9296f3 JG |
2110 | uint64_t value) |
2111 | { | |
c55a9f58 | 2112 | return _set_structure_field_integer(structure, name, value, BT_FALSE); |
af9296f3 JG |
2113 | } |
2114 | ||
50842bdc | 2115 | const char *bt_stream_get_name(struct bt_stream *stream) |
b71d7298 PP |
2116 | { |
2117 | const char *name = NULL; | |
2118 | ||
2119 | if (!stream) { | |
19abc2c6 | 2120 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
b71d7298 PP |
2121 | goto end; |
2122 | } | |
2123 | ||
2124 | name = stream->name ? stream->name->str : NULL; | |
2125 | ||
2126 | end: | |
2127 | return name; | |
2128 | } | |
98a4cbef | 2129 | |
50842bdc | 2130 | int bt_stream_is_writer(struct bt_stream *stream) |
98a4cbef PP |
2131 | { |
2132 | int ret = -1; | |
2133 | ||
2134 | if (!stream) { | |
19abc2c6 | 2135 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
98a4cbef PP |
2136 | goto end; |
2137 | } | |
2138 | ||
2139 | ret = (stream->pos.fd >= 0); | |
2140 | ||
2141 | end: | |
2142 | return ret; | |
2143 | } | |
3230ee6b PP |
2144 | |
2145 | BT_HIDDEN | |
50842bdc | 2146 | void bt_stream_map_component_to_port(struct bt_stream *stream, |
3230ee6b PP |
2147 | struct bt_component *comp, |
2148 | struct bt_port *port) | |
2149 | { | |
2150 | assert(stream); | |
2151 | assert(comp); | |
2152 | assert(port); | |
2153 | assert(stream->comp_cur_port); | |
2154 | ||
2155 | /* | |
2156 | * Do not take a reference to the component here because we | |
2157 | * don't want the component to exist as long as this stream | |
2158 | * exists. Instead, keep a weak reference, but add a destroy | |
2159 | * listener so that we remove this hash table entry when we know | |
2160 | * the component is destroyed. | |
2161 | */ | |
a2136eca PP |
2162 | BT_LOGV("Adding component's destroy listener for stream: " |
2163 | "stream-addr=%p, stream-name=\"%s\", comp-addr=%p, " | |
2164 | "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"", | |
50842bdc | 2165 | stream, bt_stream_get_name(stream), |
19abc2c6 PP |
2166 | comp, bt_component_get_name(comp), port, |
2167 | bt_port_get_name(port)); | |
a2136eca PP |
2168 | bt_component_add_destroy_listener(comp, component_destroy_listener, |
2169 | stream); | |
2170 | g_hash_table_insert(stream->comp_cur_port, comp, port); | |
2171 | BT_LOGV_STR("Mapped component to port for stream."); | |
3230ee6b PP |
2172 | } |
2173 | ||
2174 | BT_HIDDEN | |
50842bdc | 2175 | struct bt_port *bt_stream_port_for_component(struct bt_stream *stream, |
3230ee6b PP |
2176 | struct bt_component *comp) |
2177 | { | |
2178 | assert(stream); | |
2179 | assert(comp); | |
2180 | assert(stream->comp_cur_port); | |
2181 | return g_hash_table_lookup(stream->comp_cur_port, comp); | |
2182 | } | |
2183 | ||
2184 | BT_HIDDEN | |
50842bdc PP |
2185 | void bt_stream_add_destroy_listener(struct bt_stream *stream, |
2186 | bt_stream_destroy_listener_func func, void *data) | |
3230ee6b | 2187 | { |
50842bdc | 2188 | struct bt_stream_destroy_listener listener; |
3230ee6b PP |
2189 | |
2190 | assert(stream); | |
2191 | assert(func); | |
2192 | listener.func = func; | |
2193 | listener.data = data; | |
2194 | g_array_append_val(stream->destroy_listeners, listener); | |
19abc2c6 PP |
2195 | BT_LOGV("Added stream destroy listener: stream-addr=%p, " |
2196 | "stream-name=\"%s\", func=%p, data=%p", | |
50842bdc | 2197 | stream, bt_stream_get_name(stream), func, data); |
3230ee6b PP |
2198 | } |
2199 | ||
2200 | BT_HIDDEN | |
50842bdc PP |
2201 | void bt_stream_remove_destroy_listener(struct bt_stream *stream, |
2202 | bt_stream_destroy_listener_func func, void *data) | |
3230ee6b PP |
2203 | { |
2204 | size_t i; | |
2205 | ||
2206 | assert(stream); | |
2207 | assert(func); | |
2208 | ||
2209 | for (i = 0; i < stream->destroy_listeners->len; i++) { | |
50842bdc | 2210 | struct bt_stream_destroy_listener *listener = |
3230ee6b | 2211 | &g_array_index(stream->destroy_listeners, |
50842bdc | 2212 | struct bt_stream_destroy_listener, i); |
3230ee6b PP |
2213 | |
2214 | if (listener->func == func && listener->data == data) { | |
2215 | g_array_remove_index(stream->destroy_listeners, i); | |
2216 | i--; | |
19abc2c6 PP |
2217 | BT_LOGV("Removed stream destroy listener: stream-addr=%p, " |
2218 | "stream-name=\"%s\", func=%p, data=%p", | |
50842bdc | 2219 | stream, bt_stream_get_name(stream), |
19abc2c6 | 2220 | func, data); |
3230ee6b PP |
2221 | } |
2222 | } | |
2223 | } | |
cfe11c58 | 2224 | |
50842bdc | 2225 | int64_t bt_stream_get_id(struct bt_stream *stream) |
cfe11c58 PP |
2226 | { |
2227 | int64_t ret; | |
2228 | ||
2229 | if (!stream) { | |
2230 | BT_LOGW_STR("Invalid parameter: stream is NULL."); | |
2231 | ret = (int64_t) -1; | |
2232 | goto end; | |
2233 | } | |
2234 | ||
2235 | ret = stream->id; | |
2236 | if (ret < 0) { | |
2237 | BT_LOGV("Stream's ID is not set: addr=%p, name=\"%s\"", | |
50842bdc | 2238 | stream, bt_stream_get_name(stream)); |
cfe11c58 PP |
2239 | } |
2240 | ||
2241 | end: | |
2242 | return ret; | |
2243 | } |