| 1 | /* |
| 2 | * stream.c |
| 3 | * |
| 4 | * Babeltrace CTF Writer |
| 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/event-types-internal.h> |
| 34 | #include <babeltrace/ctf-ir/event-fields-internal.h> |
| 35 | #include <babeltrace/ctf-ir/stream.h> |
| 36 | #include <babeltrace/ctf-ir/stream-internal.h> |
| 37 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
| 38 | #include <babeltrace/ctf-writer/functor-internal.h> |
| 39 | #include <babeltrace/compiler.h> |
| 40 | #include <babeltrace/align.h> |
| 41 | |
| 42 | static |
| 43 | void bt_ctf_stream_destroy(struct bt_ctf_ref *ref); |
| 44 | static |
| 45 | int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t); |
| 46 | |
| 47 | BT_HIDDEN |
| 48 | struct bt_ctf_stream *bt_ctf_stream_create( |
| 49 | struct bt_ctf_stream_class *stream_class) |
| 50 | { |
| 51 | struct bt_ctf_stream *stream = NULL; |
| 52 | |
| 53 | if (!stream_class) { |
| 54 | goto end; |
| 55 | } |
| 56 | |
| 57 | stream = g_new0(struct bt_ctf_stream, 1); |
| 58 | if (!stream) { |
| 59 | goto end; |
| 60 | } |
| 61 | |
| 62 | bt_ctf_ref_init(&stream->ref_count); |
| 63 | stream->pos.fd = -1; |
| 64 | stream->id = stream_class->next_stream_id++; |
| 65 | stream->stream_class = stream_class; |
| 66 | bt_ctf_stream_class_get(stream_class); |
| 67 | bt_ctf_stream_class_freeze(stream_class); |
| 68 | stream->events = g_ptr_array_new_with_free_func( |
| 69 | (GDestroyNotify)bt_ctf_event_put); |
| 70 | end: |
| 71 | return stream; |
| 72 | } |
| 73 | |
| 74 | BT_HIDDEN |
| 75 | int bt_ctf_stream_set_flush_callback(struct bt_ctf_stream *stream, |
| 76 | flush_func callback, void *data) |
| 77 | { |
| 78 | int ret = stream ? 0 : -1; |
| 79 | |
| 80 | if (!stream) { |
| 81 | goto end; |
| 82 | } |
| 83 | |
| 84 | stream->flush.func = callback; |
| 85 | stream->flush.data = data; |
| 86 | end: |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | BT_HIDDEN |
| 91 | int bt_ctf_stream_set_fd(struct bt_ctf_stream *stream, int fd) |
| 92 | { |
| 93 | int ret = 0; |
| 94 | |
| 95 | if (stream->pos.fd != -1) { |
| 96 | ret = -1; |
| 97 | goto end; |
| 98 | } |
| 99 | |
| 100 | ctf_init_pos(&stream->pos, NULL, fd, O_RDWR); |
| 101 | stream->pos.fd = fd; |
| 102 | end: |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | int bt_ctf_stream_get_discarded_events_count( |
| 107 | struct bt_ctf_stream *stream, uint64_t *count) |
| 108 | { |
| 109 | int64_t ret = 0; |
| 110 | |
| 111 | if (!stream || !count) { |
| 112 | ret = -1; |
| 113 | goto end; |
| 114 | } |
| 115 | |
| 116 | *count = stream->events_discarded; |
| 117 | end: |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, |
| 122 | uint64_t event_count) |
| 123 | { |
| 124 | if (!stream) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | stream->events_discarded += event_count; |
| 129 | } |
| 130 | |
| 131 | int bt_ctf_stream_append_event(struct bt_ctf_stream *stream, |
| 132 | struct bt_ctf_event *event) |
| 133 | { |
| 134 | int ret = 0; |
| 135 | uint64_t timestamp; |
| 136 | |
| 137 | if (!stream || !event) { |
| 138 | ret = -1; |
| 139 | goto end; |
| 140 | } |
| 141 | |
| 142 | ret = bt_ctf_event_validate(event); |
| 143 | if (ret) { |
| 144 | goto end; |
| 145 | } |
| 146 | |
| 147 | timestamp = bt_ctf_clock_get_time(stream->stream_class->clock); |
| 148 | ret = bt_ctf_event_set_timestamp(event, timestamp); |
| 149 | if (ret) { |
| 150 | goto end; |
| 151 | } |
| 152 | |
| 153 | bt_ctf_event_get(event); |
| 154 | g_ptr_array_add(stream->events, event); |
| 155 | end: |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | int bt_ctf_stream_flush(struct bt_ctf_stream *stream) |
| 160 | { |
| 161 | int ret = 0; |
| 162 | size_t i; |
| 163 | uint64_t timestamp_begin, timestamp_end; |
| 164 | struct bt_ctf_stream_class *stream_class; |
| 165 | struct bt_ctf_field *integer = NULL; |
| 166 | struct ctf_stream_pos packet_context_pos; |
| 167 | |
| 168 | if (!stream) { |
| 169 | ret = -1; |
| 170 | goto end; |
| 171 | } |
| 172 | |
| 173 | if (!stream->events->len) { |
| 174 | goto end; |
| 175 | } |
| 176 | |
| 177 | if (stream->flush.func) { |
| 178 | stream->flush.func(stream, stream->flush.data); |
| 179 | } |
| 180 | |
| 181 | stream_class = stream->stream_class; |
| 182 | timestamp_begin = ((struct bt_ctf_event *) g_ptr_array_index( |
| 183 | stream->events, 0))->timestamp; |
| 184 | timestamp_end = ((struct bt_ctf_event *) g_ptr_array_index( |
| 185 | stream->events, stream->events->len - 1))->timestamp; |
| 186 | ret = set_structure_field_integer(stream_class->packet_context, |
| 187 | "timestamp_begin", timestamp_begin); |
| 188 | if (ret) { |
| 189 | goto end; |
| 190 | } |
| 191 | |
| 192 | ret = set_structure_field_integer(stream_class->packet_context, |
| 193 | "timestamp_end", timestamp_end); |
| 194 | if (ret) { |
| 195 | goto end; |
| 196 | } |
| 197 | |
| 198 | ret = set_structure_field_integer(stream_class->packet_context, |
| 199 | "events_discarded", stream->events_discarded); |
| 200 | if (ret) { |
| 201 | goto end; |
| 202 | } |
| 203 | |
| 204 | ret = set_structure_field_integer(stream_class->packet_context, |
| 205 | "content_size", UINT64_MAX); |
| 206 | if (ret) { |
| 207 | goto end; |
| 208 | } |
| 209 | |
| 210 | ret = set_structure_field_integer(stream_class->packet_context, |
| 211 | "packet_size", UINT64_MAX); |
| 212 | if (ret) { |
| 213 | goto end; |
| 214 | } |
| 215 | |
| 216 | /* Write packet context */ |
| 217 | memcpy(&packet_context_pos, &stream->pos, |
| 218 | sizeof(struct ctf_stream_pos)); |
| 219 | ret = bt_ctf_field_serialize(stream_class->packet_context, |
| 220 | &stream->pos); |
| 221 | if (ret) { |
| 222 | goto end; |
| 223 | } |
| 224 | |
| 225 | for (i = 0; i < stream->events->len; i++) { |
| 226 | struct bt_ctf_event *event = g_ptr_array_index( |
| 227 | stream->events, i); |
| 228 | uint32_t event_id = bt_ctf_event_class_get_id( |
| 229 | event->event_class); |
| 230 | uint64_t timestamp = bt_ctf_event_get_timestamp(event); |
| 231 | |
| 232 | ret = set_structure_field_integer(stream_class->event_header, |
| 233 | "id", event_id); |
| 234 | if (ret) { |
| 235 | goto end; |
| 236 | } |
| 237 | ret = set_structure_field_integer(stream_class->event_header, |
| 238 | "timestamp", timestamp); |
| 239 | if (ret) { |
| 240 | goto end; |
| 241 | } |
| 242 | |
| 243 | /* Write event header */ |
| 244 | ret = bt_ctf_field_serialize(stream_class->event_header, |
| 245 | &stream->pos); |
| 246 | if (ret) { |
| 247 | goto end; |
| 248 | } |
| 249 | |
| 250 | /* Write event content */ |
| 251 | ret = bt_ctf_event_serialize(event, &stream->pos); |
| 252 | if (ret) { |
| 253 | goto end; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Update the packet total size and content size and overwrite the |
| 259 | * packet context. |
| 260 | * Copy base_mma as the packet may have been remapped (e.g. when a |
| 261 | * packet is resized). |
| 262 | */ |
| 263 | packet_context_pos.base_mma = stream->pos.base_mma; |
| 264 | ret = set_structure_field_integer(stream_class->packet_context, |
| 265 | "content_size", stream->pos.offset); |
| 266 | if (ret) { |
| 267 | goto end; |
| 268 | } |
| 269 | |
| 270 | ret = set_structure_field_integer(stream_class->packet_context, |
| 271 | "packet_size", stream->pos.packet_size); |
| 272 | if (ret) { |
| 273 | goto end; |
| 274 | } |
| 275 | |
| 276 | ret = bt_ctf_field_serialize(stream_class->packet_context, |
| 277 | &packet_context_pos); |
| 278 | if (ret) { |
| 279 | goto end; |
| 280 | } |
| 281 | |
| 282 | g_ptr_array_set_size(stream->events, 0); |
| 283 | stream->flushed_packet_count++; |
| 284 | end: |
| 285 | bt_ctf_field_put(integer); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | void bt_ctf_stream_get(struct bt_ctf_stream *stream) |
| 290 | { |
| 291 | if (!stream) { |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | bt_ctf_ref_get(&stream->ref_count); |
| 296 | } |
| 297 | |
| 298 | void bt_ctf_stream_put(struct bt_ctf_stream *stream) |
| 299 | { |
| 300 | if (!stream) { |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | bt_ctf_ref_put(&stream->ref_count, bt_ctf_stream_destroy); |
| 305 | } |
| 306 | |
| 307 | static |
| 308 | void bt_ctf_stream_destroy(struct bt_ctf_ref *ref) |
| 309 | { |
| 310 | struct bt_ctf_stream *stream; |
| 311 | |
| 312 | if (!ref) { |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | stream = container_of(ref, struct bt_ctf_stream, ref_count); |
| 317 | ctf_fini_pos(&stream->pos); |
| 318 | if (close(stream->pos.fd)) { |
| 319 | perror("close"); |
| 320 | } |
| 321 | bt_ctf_stream_class_put(stream->stream_class); |
| 322 | g_ptr_array_free(stream->events, TRUE); |
| 323 | g_free(stream); |
| 324 | } |
| 325 | |
| 326 | static |
| 327 | int set_structure_field_integer(struct bt_ctf_field *structure, char *name, |
| 328 | uint64_t value) |
| 329 | { |
| 330 | int ret = 0; |
| 331 | |
| 332 | struct bt_ctf_field *integer = |
| 333 | bt_ctf_field_structure_get_field(structure, name); |
| 334 | if (!integer) { |
| 335 | ret = -1; |
| 336 | goto end; |
| 337 | } |
| 338 | |
| 339 | ret = bt_ctf_field_unsigned_integer_set_value(integer, value); |
| 340 | end: |
| 341 | bt_ctf_field_put(integer); |
| 342 | return ret; |
| 343 | } |