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