| 1 | /* |
| 2 | * Copyright (C) 2011 EfficiOS Inc. |
| 3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-only |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #define _LGPL_SOURCE |
| 11 | #include <inttypes.h> |
| 12 | #include <poll.h> |
| 13 | #include <pthread.h> |
| 14 | #include <signal.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <sys/mman.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include <bin/lttng-consumerd/health-consumerd.hpp> |
| 23 | #include <common/align.hpp> |
| 24 | #include <common/common.hpp> |
| 25 | #include <common/compat/endian.hpp> |
| 26 | #include <common/compat/poll.hpp> |
| 27 | #include <common/consumer/consumer-metadata-cache.hpp> |
| 28 | #include <common/consumer/consumer-stream.hpp> |
| 29 | #include <common/consumer/consumer-testpoint.hpp> |
| 30 | #include <common/consumer/consumer-timer.hpp> |
| 31 | #include <common/consumer/consumer.hpp> |
| 32 | #include <common/dynamic-array.hpp> |
| 33 | #include <common/index/ctf-index.hpp> |
| 34 | #include <common/index/index.hpp> |
| 35 | #include <common/kernel-consumer/kernel-consumer.hpp> |
| 36 | #include <common/kernel-ctl/kernel-ctl.hpp> |
| 37 | #include <common/relayd/relayd.hpp> |
| 38 | #include <common/sessiond-comm/relayd.hpp> |
| 39 | #include <common/sessiond-comm/sessiond-comm.hpp> |
| 40 | #include <common/string-utils/format.hpp> |
| 41 | #include <common/time.hpp> |
| 42 | #include <common/trace-chunk-registry.hpp> |
| 43 | #include <common/trace-chunk.hpp> |
| 44 | #include <common/ust-consumer/ust-consumer.hpp> |
| 45 | #include <common/utils.hpp> |
| 46 | |
| 47 | lttng_consumer_global_data the_consumer_data; |
| 48 | |
| 49 | enum consumer_channel_action { |
| 50 | CONSUMER_CHANNEL_ADD, |
| 51 | CONSUMER_CHANNEL_DEL, |
| 52 | CONSUMER_CHANNEL_QUIT, |
| 53 | }; |
| 54 | |
| 55 | namespace { |
| 56 | struct consumer_channel_msg { |
| 57 | enum consumer_channel_action action; |
| 58 | struct lttng_consumer_channel *chan; /* add */ |
| 59 | uint64_t key; /* del */ |
| 60 | }; |
| 61 | |
| 62 | /* |
| 63 | * Global hash table containing respectively metadata and data streams. The |
| 64 | * stream element in this ht should only be updated by the metadata poll thread |
| 65 | * for the metadata and the data poll thread for the data. |
| 66 | */ |
| 67 | struct lttng_ht *metadata_ht; |
| 68 | struct lttng_ht *data_ht; |
| 69 | } /* namespace */ |
| 70 | |
| 71 | /* Flag used to temporarily pause data consumption from testpoints. */ |
| 72 | int data_consumption_paused; |
| 73 | |
| 74 | /* |
| 75 | * Flag to inform the polling thread to quit when all fd hung up. Updated by |
| 76 | * the consumer_thread_receive_fds when it notices that all fds has hung up. |
| 77 | * Also updated by the signal handler (consumer_should_exit()). Read by the |
| 78 | * polling threads. |
| 79 | */ |
| 80 | int consumer_quit; |
| 81 | |
| 82 | static const char *get_consumer_domain(void) |
| 83 | { |
| 84 | switch (the_consumer_data.type) { |
| 85 | case LTTNG_CONSUMER_KERNEL: |
| 86 | return DEFAULT_KERNEL_TRACE_DIR; |
| 87 | case LTTNG_CONSUMER64_UST: |
| 88 | /* Fall-through. */ |
| 89 | case LTTNG_CONSUMER32_UST: |
| 90 | return DEFAULT_UST_TRACE_DIR; |
| 91 | default: |
| 92 | abort(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Notify a thread lttng pipe to poll back again. This usually means that some |
| 98 | * global state has changed so we just send back the thread in a poll wait |
| 99 | * call. |
| 100 | */ |
| 101 | static void notify_thread_lttng_pipe(struct lttng_pipe *pipe) |
| 102 | { |
| 103 | struct lttng_consumer_stream *null_stream = NULL; |
| 104 | |
| 105 | LTTNG_ASSERT(pipe); |
| 106 | |
| 107 | (void) lttng_pipe_write(pipe, &null_stream, sizeof(null_stream)); |
| 108 | } |
| 109 | |
| 110 | static void notify_health_quit_pipe(int *pipe) |
| 111 | { |
| 112 | ssize_t ret; |
| 113 | |
| 114 | ret = lttng_write(pipe[1], "4", 1); |
| 115 | if (ret < 1) { |
| 116 | PERROR("write consumer health quit"); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static void notify_channel_pipe(struct lttng_consumer_local_data *ctx, |
| 121 | struct lttng_consumer_channel *chan, |
| 122 | uint64_t key, |
| 123 | enum consumer_channel_action action) |
| 124 | { |
| 125 | struct consumer_channel_msg msg; |
| 126 | ssize_t ret; |
| 127 | |
| 128 | memset(&msg, 0, sizeof(msg)); |
| 129 | |
| 130 | msg.action = action; |
| 131 | msg.chan = chan; |
| 132 | msg.key = key; |
| 133 | ret = lttng_write(ctx->consumer_channel_pipe[1], &msg, sizeof(msg)); |
| 134 | if (ret < sizeof(msg)) { |
| 135 | PERROR("notify_channel_pipe write error"); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void notify_thread_del_channel(struct lttng_consumer_local_data *ctx, |
| 140 | uint64_t key) |
| 141 | { |
| 142 | notify_channel_pipe(ctx, NULL, key, CONSUMER_CHANNEL_DEL); |
| 143 | } |
| 144 | |
| 145 | static int read_channel_pipe(struct lttng_consumer_local_data *ctx, |
| 146 | struct lttng_consumer_channel **chan, |
| 147 | uint64_t *key, |
| 148 | enum consumer_channel_action *action) |
| 149 | { |
| 150 | struct consumer_channel_msg msg; |
| 151 | ssize_t ret; |
| 152 | |
| 153 | ret = lttng_read(ctx->consumer_channel_pipe[0], &msg, sizeof(msg)); |
| 154 | if (ret < sizeof(msg)) { |
| 155 | ret = -1; |
| 156 | goto error; |
| 157 | } |
| 158 | *action = msg.action; |
| 159 | *chan = msg.chan; |
| 160 | *key = msg.key; |
| 161 | error: |
| 162 | return (int) ret; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Cleanup the stream list of a channel. Those streams are not yet globally |
| 167 | * visible |
| 168 | */ |
| 169 | static void clean_channel_stream_list(struct lttng_consumer_channel *channel) |
| 170 | { |
| 171 | struct lttng_consumer_stream *stream, *stmp; |
| 172 | |
| 173 | LTTNG_ASSERT(channel); |
| 174 | |
| 175 | /* Delete streams that might have been left in the stream list. */ |
| 176 | cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head, |
| 177 | send_node) { |
| 178 | /* |
| 179 | * Once a stream is added to this list, the buffers were created so we |
| 180 | * have a guarantee that this call will succeed. Setting the monitor |
| 181 | * mode to 0 so we don't lock nor try to delete the stream from the |
| 182 | * global hash table. |
| 183 | */ |
| 184 | stream->monitor = 0; |
| 185 | consumer_stream_destroy(stream, NULL); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Find a stream. The consumer_data.lock must be locked during this |
| 191 | * call. |
| 192 | */ |
| 193 | static struct lttng_consumer_stream *find_stream(uint64_t key, |
| 194 | struct lttng_ht *ht) |
| 195 | { |
| 196 | struct lttng_ht_iter iter; |
| 197 | struct lttng_ht_node_u64 *node; |
| 198 | struct lttng_consumer_stream *stream = NULL; |
| 199 | |
| 200 | LTTNG_ASSERT(ht); |
| 201 | |
| 202 | /* -1ULL keys are lookup failures */ |
| 203 | if (key == (uint64_t) -1ULL) { |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | rcu_read_lock(); |
| 208 | |
| 209 | lttng_ht_lookup(ht, &key, &iter); |
| 210 | node = lttng_ht_iter_get_node_u64(&iter); |
| 211 | if (node != NULL) { |
| 212 | stream = lttng::utils::container_of(node, <tng_consumer_stream::node); |
| 213 | } |
| 214 | |
| 215 | rcu_read_unlock(); |
| 216 | |
| 217 | return stream; |
| 218 | } |
| 219 | |
| 220 | static void steal_stream_key(uint64_t key, struct lttng_ht *ht) |
| 221 | { |
| 222 | struct lttng_consumer_stream *stream; |
| 223 | |
| 224 | rcu_read_lock(); |
| 225 | stream = find_stream(key, ht); |
| 226 | if (stream) { |
| 227 | stream->key = (uint64_t) -1ULL; |
| 228 | /* |
| 229 | * We don't want the lookup to match, but we still need |
| 230 | * to iterate on this stream when iterating over the hash table. Just |
| 231 | * change the node key. |
| 232 | */ |
| 233 | stream->node.key = (uint64_t) -1ULL; |
| 234 | } |
| 235 | rcu_read_unlock(); |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Return a channel object for the given key. |
| 240 | * |
| 241 | * RCU read side lock MUST be acquired before calling this function and |
| 242 | * protects the channel ptr. |
| 243 | */ |
| 244 | struct lttng_consumer_channel *consumer_find_channel(uint64_t key) |
| 245 | { |
| 246 | struct lttng_ht_iter iter; |
| 247 | struct lttng_ht_node_u64 *node; |
| 248 | struct lttng_consumer_channel *channel = NULL; |
| 249 | |
| 250 | ASSERT_RCU_READ_LOCKED(); |
| 251 | |
| 252 | /* -1ULL keys are lookup failures */ |
| 253 | if (key == (uint64_t) -1ULL) { |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | lttng_ht_lookup(the_consumer_data.channel_ht, &key, &iter); |
| 258 | node = lttng_ht_iter_get_node_u64(&iter); |
| 259 | if (node != NULL) { |
| 260 | channel = lttng::utils::container_of(node, <tng_consumer_channel::node); |
| 261 | } |
| 262 | |
| 263 | return channel; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * There is a possibility that the consumer does not have enough time between |
| 268 | * the close of the channel on the session daemon and the cleanup in here thus |
| 269 | * once we have a channel add with an existing key, we know for sure that this |
| 270 | * channel will eventually get cleaned up by all streams being closed. |
| 271 | * |
| 272 | * This function just nullifies the already existing channel key. |
| 273 | */ |
| 274 | static void steal_channel_key(uint64_t key) |
| 275 | { |
| 276 | struct lttng_consumer_channel *channel; |
| 277 | |
| 278 | rcu_read_lock(); |
| 279 | channel = consumer_find_channel(key); |
| 280 | if (channel) { |
| 281 | channel->key = (uint64_t) -1ULL; |
| 282 | /* |
| 283 | * We don't want the lookup to match, but we still need to iterate on |
| 284 | * this channel when iterating over the hash table. Just change the |
| 285 | * node key. |
| 286 | */ |
| 287 | channel->node.key = (uint64_t) -1ULL; |
| 288 | } |
| 289 | rcu_read_unlock(); |
| 290 | } |
| 291 | |
| 292 | static void free_channel_rcu(struct rcu_head *head) |
| 293 | { |
| 294 | struct lttng_ht_node_u64 *node = |
| 295 | lttng::utils::container_of(head, <tng_ht_node_u64::head); |
| 296 | struct lttng_consumer_channel *channel = |
| 297 | lttng::utils::container_of(node, <tng_consumer_channel::node); |
| 298 | |
| 299 | switch (the_consumer_data.type) { |
| 300 | case LTTNG_CONSUMER_KERNEL: |
| 301 | break; |
| 302 | case LTTNG_CONSUMER32_UST: |
| 303 | case LTTNG_CONSUMER64_UST: |
| 304 | lttng_ustconsumer_free_channel(channel); |
| 305 | break; |
| 306 | default: |
| 307 | ERR("Unknown consumer_data type"); |
| 308 | abort(); |
| 309 | } |
| 310 | free(channel); |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * RCU protected relayd socket pair free. |
| 315 | */ |
| 316 | static void free_relayd_rcu(struct rcu_head *head) |
| 317 | { |
| 318 | struct lttng_ht_node_u64 *node = |
| 319 | lttng::utils::container_of(head, <tng_ht_node_u64::head); |
| 320 | struct consumer_relayd_sock_pair *relayd = |
| 321 | lttng::utils::container_of(node, &consumer_relayd_sock_pair::node); |
| 322 | |
| 323 | /* |
| 324 | * Close all sockets. This is done in the call RCU since we don't want the |
| 325 | * socket fds to be reassigned thus potentially creating bad state of the |
| 326 | * relayd object. |
| 327 | * |
| 328 | * We do not have to lock the control socket mutex here since at this stage |
| 329 | * there is no one referencing to this relayd object. |
| 330 | */ |
| 331 | (void) relayd_close(&relayd->control_sock); |
| 332 | (void) relayd_close(&relayd->data_sock); |
| 333 | |
| 334 | pthread_mutex_destroy(&relayd->ctrl_sock_mutex); |
| 335 | free(relayd); |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Destroy and free relayd socket pair object. |
| 340 | */ |
| 341 | void consumer_destroy_relayd(struct consumer_relayd_sock_pair *relayd) |
| 342 | { |
| 343 | int ret; |
| 344 | struct lttng_ht_iter iter; |
| 345 | |
| 346 | if (relayd == NULL) { |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | DBG("Consumer destroy and close relayd socket pair"); |
| 351 | |
| 352 | iter.iter.node = &relayd->node.node; |
| 353 | ret = lttng_ht_del(the_consumer_data.relayd_ht, &iter); |
| 354 | if (ret != 0) { |
| 355 | /* We assume the relayd is being or is destroyed */ |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | /* RCU free() call */ |
| 360 | call_rcu(&relayd->node.head, free_relayd_rcu); |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Remove a channel from the global list protected by a mutex. This function is |
| 365 | * also responsible for freeing its data structures. |
| 366 | */ |
| 367 | void consumer_del_channel(struct lttng_consumer_channel *channel) |
| 368 | { |
| 369 | struct lttng_ht_iter iter; |
| 370 | |
| 371 | DBG("Consumer delete channel key %" PRIu64, channel->key); |
| 372 | |
| 373 | pthread_mutex_lock(&the_consumer_data.lock); |
| 374 | pthread_mutex_lock(&channel->lock); |
| 375 | |
| 376 | /* Destroy streams that might have been left in the stream list. */ |
| 377 | clean_channel_stream_list(channel); |
| 378 | |
| 379 | if (channel->live_timer_enabled == 1) { |
| 380 | consumer_timer_live_stop(channel); |
| 381 | } |
| 382 | if (channel->monitor_timer_enabled == 1) { |
| 383 | consumer_timer_monitor_stop(channel); |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * Send a last buffer statistics sample to the session daemon |
| 388 | * to ensure it tracks the amount of data consumed by this channel. |
| 389 | */ |
| 390 | sample_and_send_channel_buffer_stats(channel); |
| 391 | |
| 392 | switch (the_consumer_data.type) { |
| 393 | case LTTNG_CONSUMER_KERNEL: |
| 394 | break; |
| 395 | case LTTNG_CONSUMER32_UST: |
| 396 | case LTTNG_CONSUMER64_UST: |
| 397 | lttng_ustconsumer_del_channel(channel); |
| 398 | break; |
| 399 | default: |
| 400 | ERR("Unknown consumer_data type"); |
| 401 | abort(); |
| 402 | goto end; |
| 403 | } |
| 404 | |
| 405 | lttng_trace_chunk_put(channel->trace_chunk); |
| 406 | channel->trace_chunk = NULL; |
| 407 | |
| 408 | if (channel->is_published) { |
| 409 | int ret; |
| 410 | |
| 411 | rcu_read_lock(); |
| 412 | iter.iter.node = &channel->node.node; |
| 413 | ret = lttng_ht_del(the_consumer_data.channel_ht, &iter); |
| 414 | LTTNG_ASSERT(!ret); |
| 415 | |
| 416 | iter.iter.node = &channel->channels_by_session_id_ht_node.node; |
| 417 | ret = lttng_ht_del(the_consumer_data.channels_by_session_id_ht, |
| 418 | &iter); |
| 419 | LTTNG_ASSERT(!ret); |
| 420 | rcu_read_unlock(); |
| 421 | } |
| 422 | |
| 423 | channel->is_deleted = true; |
| 424 | call_rcu(&channel->node.head, free_channel_rcu); |
| 425 | end: |
| 426 | pthread_mutex_unlock(&channel->lock); |
| 427 | pthread_mutex_unlock(&the_consumer_data.lock); |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Iterate over the relayd hash table and destroy each element. Finally, |
| 432 | * destroy the whole hash table. |
| 433 | */ |
| 434 | static void cleanup_relayd_ht(void) |
| 435 | { |
| 436 | struct lttng_ht_iter iter; |
| 437 | struct consumer_relayd_sock_pair *relayd; |
| 438 | |
| 439 | rcu_read_lock(); |
| 440 | |
| 441 | cds_lfht_for_each_entry(the_consumer_data.relayd_ht->ht, &iter.iter, |
| 442 | relayd, node.node) { |
| 443 | consumer_destroy_relayd(relayd); |
| 444 | } |
| 445 | |
| 446 | rcu_read_unlock(); |
| 447 | |
| 448 | lttng_ht_destroy(the_consumer_data.relayd_ht); |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Update the end point status of all streams having the given network sequence |
| 453 | * index (relayd index). |
| 454 | * |
| 455 | * It's atomically set without having the stream mutex locked which is fine |
| 456 | * because we handle the write/read race with a pipe wakeup for each thread. |
| 457 | */ |
| 458 | static void update_endpoint_status_by_netidx(uint64_t net_seq_idx, |
| 459 | enum consumer_endpoint_status status) |
| 460 | { |
| 461 | struct lttng_ht_iter iter; |
| 462 | struct lttng_consumer_stream *stream; |
| 463 | |
| 464 | DBG("Consumer set delete flag on stream by idx %" PRIu64, net_seq_idx); |
| 465 | |
| 466 | rcu_read_lock(); |
| 467 | |
| 468 | /* Let's begin with metadata */ |
| 469 | cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) { |
| 470 | if (stream->net_seq_idx == net_seq_idx) { |
| 471 | uatomic_set(&stream->endpoint_status, status); |
| 472 | DBG("Delete flag set to metadata stream %d", stream->wait_fd); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /* Follow up by the data streams */ |
| 477 | cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) { |
| 478 | if (stream->net_seq_idx == net_seq_idx) { |
| 479 | uatomic_set(&stream->endpoint_status, status); |
| 480 | DBG("Delete flag set to data stream %d", stream->wait_fd); |
| 481 | } |
| 482 | } |
| 483 | rcu_read_unlock(); |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Cleanup a relayd object by flagging every associated streams for deletion, |
| 488 | * destroying the object meaning removing it from the relayd hash table, |
| 489 | * closing the sockets and freeing the memory in a RCU call. |
| 490 | * |
| 491 | * If a local data context is available, notify the threads that the streams' |
| 492 | * state have changed. |
| 493 | */ |
| 494 | void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair *relayd) |
| 495 | { |
| 496 | uint64_t netidx; |
| 497 | |
| 498 | LTTNG_ASSERT(relayd); |
| 499 | |
| 500 | DBG("Cleaning up relayd object ID %" PRIu64, relayd->net_seq_idx); |
| 501 | |
| 502 | /* Save the net sequence index before destroying the object */ |
| 503 | netidx = relayd->net_seq_idx; |
| 504 | |
| 505 | /* |
| 506 | * Delete the relayd from the relayd hash table, close the sockets and free |
| 507 | * the object in a RCU call. |
| 508 | */ |
| 509 | consumer_destroy_relayd(relayd); |
| 510 | |
| 511 | /* Set inactive endpoint to all streams */ |
| 512 | update_endpoint_status_by_netidx(netidx, CONSUMER_ENDPOINT_INACTIVE); |
| 513 | |
| 514 | /* |
| 515 | * With a local data context, notify the threads that the streams' state |
| 516 | * have changed. The write() action on the pipe acts as an "implicit" |
| 517 | * memory barrier ordering the updates of the end point status from the |
| 518 | * read of this status which happens AFTER receiving this notify. |
| 519 | */ |
| 520 | notify_thread_lttng_pipe(relayd->ctx->consumer_data_pipe); |
| 521 | notify_thread_lttng_pipe(relayd->ctx->consumer_metadata_pipe); |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * Flag a relayd socket pair for destruction. Destroy it if the refcount |
| 526 | * reaches zero. |
| 527 | * |
| 528 | * RCU read side lock MUST be aquired before calling this function. |
| 529 | */ |
| 530 | void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd) |
| 531 | { |
| 532 | LTTNG_ASSERT(relayd); |
| 533 | ASSERT_RCU_READ_LOCKED(); |
| 534 | |
| 535 | /* Set destroy flag for this object */ |
| 536 | uatomic_set(&relayd->destroy_flag, 1); |
| 537 | |
| 538 | /* Destroy the relayd if refcount is 0 */ |
| 539 | if (uatomic_read(&relayd->refcount) == 0) { |
| 540 | consumer_destroy_relayd(relayd); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * Completly destroy stream from every visiable data structure and the given |
| 546 | * hash table if one. |
| 547 | * |
| 548 | * One this call returns, the stream object is not longer usable nor visible. |
| 549 | */ |
| 550 | void consumer_del_stream(struct lttng_consumer_stream *stream, |
| 551 | struct lttng_ht *ht) |
| 552 | { |
| 553 | consumer_stream_destroy(stream, ht); |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * XXX naming of del vs destroy is all mixed up. |
| 558 | */ |
| 559 | void consumer_del_stream_for_data(struct lttng_consumer_stream *stream) |
| 560 | { |
| 561 | consumer_stream_destroy(stream, data_ht); |
| 562 | } |
| 563 | |
| 564 | void consumer_del_stream_for_metadata(struct lttng_consumer_stream *stream) |
| 565 | { |
| 566 | consumer_stream_destroy(stream, metadata_ht); |
| 567 | } |
| 568 | |
| 569 | void consumer_stream_update_channel_attributes( |
| 570 | struct lttng_consumer_stream *stream, |
| 571 | struct lttng_consumer_channel *channel) |
| 572 | { |
| 573 | stream->channel_read_only_attributes.tracefile_size = |
| 574 | channel->tracefile_size; |
| 575 | } |
| 576 | |
| 577 | /* |
| 578 | * Add a stream to the global list protected by a mutex. |
| 579 | */ |
| 580 | void consumer_add_data_stream(struct lttng_consumer_stream *stream) |
| 581 | { |
| 582 | struct lttng_ht *ht = data_ht; |
| 583 | |
| 584 | LTTNG_ASSERT(stream); |
| 585 | LTTNG_ASSERT(ht); |
| 586 | |
| 587 | DBG3("Adding consumer stream %" PRIu64, stream->key); |
| 588 | |
| 589 | pthread_mutex_lock(&the_consumer_data.lock); |
| 590 | pthread_mutex_lock(&stream->chan->lock); |
| 591 | pthread_mutex_lock(&stream->chan->timer_lock); |
| 592 | pthread_mutex_lock(&stream->lock); |
| 593 | rcu_read_lock(); |
| 594 | |
| 595 | /* Steal stream identifier to avoid having streams with the same key */ |
| 596 | steal_stream_key(stream->key, ht); |
| 597 | |
| 598 | lttng_ht_add_unique_u64(ht, &stream->node); |
| 599 | |
| 600 | lttng_ht_add_u64(the_consumer_data.stream_per_chan_id_ht, |
| 601 | &stream->node_channel_id); |
| 602 | |
| 603 | /* |
| 604 | * Add stream to the stream_list_ht of the consumer data. No need to steal |
| 605 | * the key since the HT does not use it and we allow to add redundant keys |
| 606 | * into this table. |
| 607 | */ |
| 608 | lttng_ht_add_u64(the_consumer_data.stream_list_ht, |
| 609 | &stream->node_session_id); |
| 610 | |
| 611 | /* |
| 612 | * When nb_init_stream_left reaches 0, we don't need to trigger any action |
| 613 | * in terms of destroying the associated channel, because the action that |
| 614 | * causes the count to become 0 also causes a stream to be added. The |
| 615 | * channel deletion will thus be triggered by the following removal of this |
| 616 | * stream. |
| 617 | */ |
| 618 | if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) { |
| 619 | /* Increment refcount before decrementing nb_init_stream_left */ |
| 620 | cmm_smp_wmb(); |
| 621 | uatomic_dec(&stream->chan->nb_init_stream_left); |
| 622 | } |
| 623 | |
| 624 | /* Update consumer data once the node is inserted. */ |
| 625 | the_consumer_data.stream_count++; |
| 626 | the_consumer_data.need_update = 1; |
| 627 | |
| 628 | rcu_read_unlock(); |
| 629 | pthread_mutex_unlock(&stream->lock); |
| 630 | pthread_mutex_unlock(&stream->chan->timer_lock); |
| 631 | pthread_mutex_unlock(&stream->chan->lock); |
| 632 | pthread_mutex_unlock(&the_consumer_data.lock); |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * Add relayd socket to global consumer data hashtable. RCU read side lock MUST |
| 637 | * be acquired before calling this. |
| 638 | */ |
| 639 | static int add_relayd(struct consumer_relayd_sock_pair *relayd) |
| 640 | { |
| 641 | int ret = 0; |
| 642 | struct lttng_ht_node_u64 *node; |
| 643 | struct lttng_ht_iter iter; |
| 644 | |
| 645 | LTTNG_ASSERT(relayd); |
| 646 | ASSERT_RCU_READ_LOCKED(); |
| 647 | |
| 648 | lttng_ht_lookup(the_consumer_data.relayd_ht, &relayd->net_seq_idx, |
| 649 | &iter); |
| 650 | node = lttng_ht_iter_get_node_u64(&iter); |
| 651 | if (node != NULL) { |
| 652 | goto end; |
| 653 | } |
| 654 | lttng_ht_add_unique_u64(the_consumer_data.relayd_ht, &relayd->node); |
| 655 | |
| 656 | end: |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * Allocate and return a consumer relayd socket. |
| 662 | */ |
| 663 | static struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair( |
| 664 | uint64_t net_seq_idx) |
| 665 | { |
| 666 | struct consumer_relayd_sock_pair *obj = NULL; |
| 667 | |
| 668 | /* net sequence index of -1 is a failure */ |
| 669 | if (net_seq_idx == (uint64_t) -1ULL) { |
| 670 | goto error; |
| 671 | } |
| 672 | |
| 673 | obj = zmalloc<consumer_relayd_sock_pair>(); |
| 674 | if (obj == NULL) { |
| 675 | PERROR("zmalloc relayd sock"); |
| 676 | goto error; |
| 677 | } |
| 678 | |
| 679 | obj->net_seq_idx = net_seq_idx; |
| 680 | obj->refcount = 0; |
| 681 | obj->destroy_flag = 0; |
| 682 | obj->control_sock.sock.fd = -1; |
| 683 | obj->data_sock.sock.fd = -1; |
| 684 | lttng_ht_node_init_u64(&obj->node, obj->net_seq_idx); |
| 685 | pthread_mutex_init(&obj->ctrl_sock_mutex, NULL); |
| 686 | |
| 687 | error: |
| 688 | return obj; |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * Find a relayd socket pair in the global consumer data. |
| 693 | * |
| 694 | * Return the object if found else NULL. |
| 695 | * RCU read-side lock must be held across this call and while using the |
| 696 | * returned object. |
| 697 | */ |
| 698 | struct consumer_relayd_sock_pair *consumer_find_relayd(uint64_t key) |
| 699 | { |
| 700 | struct lttng_ht_iter iter; |
| 701 | struct lttng_ht_node_u64 *node; |
| 702 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 703 | |
| 704 | ASSERT_RCU_READ_LOCKED(); |
| 705 | |
| 706 | /* Negative keys are lookup failures */ |
| 707 | if (key == (uint64_t) -1ULL) { |
| 708 | goto error; |
| 709 | } |
| 710 | |
| 711 | lttng_ht_lookup(the_consumer_data.relayd_ht, &key, &iter); |
| 712 | node = lttng_ht_iter_get_node_u64(&iter); |
| 713 | if (node != NULL) { |
| 714 | relayd = lttng::utils::container_of(node, &consumer_relayd_sock_pair::node); |
| 715 | } |
| 716 | |
| 717 | error: |
| 718 | return relayd; |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | * Find a relayd and send the stream |
| 723 | * |
| 724 | * Returns 0 on success, < 0 on error |
| 725 | */ |
| 726 | int consumer_send_relayd_stream(struct lttng_consumer_stream *stream, |
| 727 | char *path) |
| 728 | { |
| 729 | int ret = 0; |
| 730 | struct consumer_relayd_sock_pair *relayd; |
| 731 | |
| 732 | LTTNG_ASSERT(stream); |
| 733 | LTTNG_ASSERT(stream->net_seq_idx != -1ULL); |
| 734 | LTTNG_ASSERT(path); |
| 735 | |
| 736 | /* The stream is not metadata. Get relayd reference if exists. */ |
| 737 | rcu_read_lock(); |
| 738 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 739 | if (relayd != NULL) { |
| 740 | /* Add stream on the relayd */ |
| 741 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 742 | ret = relayd_add_stream(&relayd->control_sock, stream->name, |
| 743 | get_consumer_domain(), path, &stream->relayd_stream_id, |
| 744 | stream->chan->tracefile_size, |
| 745 | stream->chan->tracefile_count, |
| 746 | stream->trace_chunk); |
| 747 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 748 | if (ret < 0) { |
| 749 | ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
| 750 | lttng_consumer_cleanup_relayd(relayd); |
| 751 | goto end; |
| 752 | } |
| 753 | |
| 754 | uatomic_inc(&relayd->refcount); |
| 755 | stream->sent_to_relayd = 1; |
| 756 | } else { |
| 757 | ERR("Stream %" PRIu64 " relayd ID %" PRIu64 " unknown. Can't send it.", |
| 758 | stream->key, stream->net_seq_idx); |
| 759 | ret = -1; |
| 760 | goto end; |
| 761 | } |
| 762 | |
| 763 | DBG("Stream %s with key %" PRIu64 " sent to relayd id %" PRIu64, |
| 764 | stream->name, stream->key, stream->net_seq_idx); |
| 765 | |
| 766 | end: |
| 767 | rcu_read_unlock(); |
| 768 | return ret; |
| 769 | } |
| 770 | |
| 771 | /* |
| 772 | * Find a relayd and send the streams sent message |
| 773 | * |
| 774 | * Returns 0 on success, < 0 on error |
| 775 | */ |
| 776 | int consumer_send_relayd_streams_sent(uint64_t net_seq_idx) |
| 777 | { |
| 778 | int ret = 0; |
| 779 | struct consumer_relayd_sock_pair *relayd; |
| 780 | |
| 781 | LTTNG_ASSERT(net_seq_idx != -1ULL); |
| 782 | |
| 783 | /* The stream is not metadata. Get relayd reference if exists. */ |
| 784 | rcu_read_lock(); |
| 785 | relayd = consumer_find_relayd(net_seq_idx); |
| 786 | if (relayd != NULL) { |
| 787 | /* Add stream on the relayd */ |
| 788 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 789 | ret = relayd_streams_sent(&relayd->control_sock); |
| 790 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 791 | if (ret < 0) { |
| 792 | ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
| 793 | lttng_consumer_cleanup_relayd(relayd); |
| 794 | goto end; |
| 795 | } |
| 796 | } else { |
| 797 | ERR("Relayd ID %" PRIu64 " unknown. Can't send streams_sent.", |
| 798 | net_seq_idx); |
| 799 | ret = -1; |
| 800 | goto end; |
| 801 | } |
| 802 | |
| 803 | ret = 0; |
| 804 | DBG("All streams sent relayd id %" PRIu64, net_seq_idx); |
| 805 | |
| 806 | end: |
| 807 | rcu_read_unlock(); |
| 808 | return ret; |
| 809 | } |
| 810 | |
| 811 | /* |
| 812 | * Find a relayd and close the stream |
| 813 | */ |
| 814 | void close_relayd_stream(struct lttng_consumer_stream *stream) |
| 815 | { |
| 816 | struct consumer_relayd_sock_pair *relayd; |
| 817 | |
| 818 | /* The stream is not metadata. Get relayd reference if exists. */ |
| 819 | rcu_read_lock(); |
| 820 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 821 | if (relayd) { |
| 822 | consumer_stream_relayd_close(stream, relayd); |
| 823 | } |
| 824 | rcu_read_unlock(); |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * Handle stream for relayd transmission if the stream applies for network |
| 829 | * streaming where the net sequence index is set. |
| 830 | * |
| 831 | * Return destination file descriptor or negative value on error. |
| 832 | */ |
| 833 | static int write_relayd_stream_header(struct lttng_consumer_stream *stream, |
| 834 | size_t data_size, unsigned long padding, |
| 835 | struct consumer_relayd_sock_pair *relayd) |
| 836 | { |
| 837 | int outfd = -1, ret; |
| 838 | struct lttcomm_relayd_data_hdr data_hdr; |
| 839 | |
| 840 | /* Safety net */ |
| 841 | LTTNG_ASSERT(stream); |
| 842 | LTTNG_ASSERT(relayd); |
| 843 | |
| 844 | /* Reset data header */ |
| 845 | memset(&data_hdr, 0, sizeof(data_hdr)); |
| 846 | |
| 847 | if (stream->metadata_flag) { |
| 848 | /* Caller MUST acquire the relayd control socket lock */ |
| 849 | ret = relayd_send_metadata(&relayd->control_sock, data_size); |
| 850 | if (ret < 0) { |
| 851 | goto error; |
| 852 | } |
| 853 | |
| 854 | /* Metadata are always sent on the control socket. */ |
| 855 | outfd = relayd->control_sock.sock.fd; |
| 856 | } else { |
| 857 | /* Set header with stream information */ |
| 858 | data_hdr.stream_id = htobe64(stream->relayd_stream_id); |
| 859 | data_hdr.data_size = htobe32(data_size); |
| 860 | data_hdr.padding_size = htobe32(padding); |
| 861 | |
| 862 | /* |
| 863 | * Note that net_seq_num below is assigned with the *current* value of |
| 864 | * next_net_seq_num and only after that the next_net_seq_num will be |
| 865 | * increment. This is why when issuing a command on the relayd using |
| 866 | * this next value, 1 should always be substracted in order to compare |
| 867 | * the last seen sequence number on the relayd side to the last sent. |
| 868 | */ |
| 869 | data_hdr.net_seq_num = htobe64(stream->next_net_seq_num); |
| 870 | /* Other fields are zeroed previously */ |
| 871 | |
| 872 | ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr, |
| 873 | sizeof(data_hdr)); |
| 874 | if (ret < 0) { |
| 875 | goto error; |
| 876 | } |
| 877 | |
| 878 | ++stream->next_net_seq_num; |
| 879 | |
| 880 | /* Set to go on data socket */ |
| 881 | outfd = relayd->data_sock.sock.fd; |
| 882 | } |
| 883 | |
| 884 | error: |
| 885 | return outfd; |
| 886 | } |
| 887 | |
| 888 | /* |
| 889 | * Write a character on the metadata poll pipe to wake the metadata thread. |
| 890 | * Returns 0 on success, -1 on error. |
| 891 | */ |
| 892 | int consumer_metadata_wakeup_pipe(const struct lttng_consumer_channel *channel) |
| 893 | { |
| 894 | int ret = 0; |
| 895 | |
| 896 | DBG("Waking up metadata poll thread (writing to pipe): channel name = '%s'", |
| 897 | channel->name); |
| 898 | if (channel->monitor && channel->metadata_stream) { |
| 899 | const char dummy = 'c'; |
| 900 | const ssize_t write_ret = lttng_write( |
| 901 | channel->metadata_stream->ust_metadata_poll_pipe[1], |
| 902 | &dummy, 1); |
| 903 | |
| 904 | if (write_ret < 1) { |
| 905 | if (errno == EWOULDBLOCK) { |
| 906 | /* |
| 907 | * This is fine, the metadata poll thread |
| 908 | * is having a hard time keeping-up, but |
| 909 | * it will eventually wake-up and consume |
| 910 | * the available data. |
| 911 | */ |
| 912 | ret = 0; |
| 913 | } else { |
| 914 | PERROR("Failed to write to UST metadata pipe while attempting to wake-up the metadata poll thread"); |
| 915 | ret = -1; |
| 916 | goto end; |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | end: |
| 922 | return ret; |
| 923 | } |
| 924 | |
| 925 | /* |
| 926 | * Trigger a dump of the metadata content. Following/during the succesful |
| 927 | * completion of this call, the metadata poll thread will start receiving |
| 928 | * metadata packets to consume. |
| 929 | * |
| 930 | * The caller must hold the channel and stream locks. |
| 931 | */ |
| 932 | static |
| 933 | int consumer_metadata_stream_dump(struct lttng_consumer_stream *stream) |
| 934 | { |
| 935 | int ret; |
| 936 | |
| 937 | ASSERT_LOCKED(stream->chan->lock); |
| 938 | ASSERT_LOCKED(stream->lock); |
| 939 | LTTNG_ASSERT(stream->metadata_flag); |
| 940 | LTTNG_ASSERT(stream->chan->trace_chunk); |
| 941 | |
| 942 | switch (the_consumer_data.type) { |
| 943 | case LTTNG_CONSUMER_KERNEL: |
| 944 | /* |
| 945 | * Reset the position of what has been read from the |
| 946 | * metadata cache to 0 so we can dump it again. |
| 947 | */ |
| 948 | ret = kernctl_metadata_cache_dump(stream->wait_fd); |
| 949 | break; |
| 950 | case LTTNG_CONSUMER32_UST: |
| 951 | case LTTNG_CONSUMER64_UST: |
| 952 | /* |
| 953 | * Reset the position pushed from the metadata cache so it |
| 954 | * will write from the beginning on the next push. |
| 955 | */ |
| 956 | stream->ust_metadata_pushed = 0; |
| 957 | ret = consumer_metadata_wakeup_pipe(stream->chan); |
| 958 | break; |
| 959 | default: |
| 960 | ERR("Unknown consumer_data type"); |
| 961 | abort(); |
| 962 | } |
| 963 | if (ret < 0) { |
| 964 | ERR("Failed to dump the metadata cache"); |
| 965 | } |
| 966 | return ret; |
| 967 | } |
| 968 | |
| 969 | static |
| 970 | int lttng_consumer_channel_set_trace_chunk( |
| 971 | struct lttng_consumer_channel *channel, |
| 972 | struct lttng_trace_chunk *new_trace_chunk) |
| 973 | { |
| 974 | pthread_mutex_lock(&channel->lock); |
| 975 | if (channel->is_deleted) { |
| 976 | /* |
| 977 | * The channel has been logically deleted and should no longer |
| 978 | * be used. It has released its reference to its current trace |
| 979 | * chunk and should not acquire a new one. |
| 980 | * |
| 981 | * Return success as there is nothing for the caller to do. |
| 982 | */ |
| 983 | goto end; |
| 984 | } |
| 985 | |
| 986 | /* |
| 987 | * The acquisition of the reference cannot fail (barring |
| 988 | * a severe internal error) since a reference to the published |
| 989 | * chunk is already held by the caller. |
| 990 | */ |
| 991 | if (new_trace_chunk) { |
| 992 | const bool acquired_reference = lttng_trace_chunk_get( |
| 993 | new_trace_chunk); |
| 994 | |
| 995 | LTTNG_ASSERT(acquired_reference); |
| 996 | } |
| 997 | |
| 998 | lttng_trace_chunk_put(channel->trace_chunk); |
| 999 | channel->trace_chunk = new_trace_chunk; |
| 1000 | end: |
| 1001 | pthread_mutex_unlock(&channel->lock); |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
| 1005 | /* |
| 1006 | * Allocate and return a new lttng_consumer_channel object using the given key |
| 1007 | * to initialize the hash table node. |
| 1008 | * |
| 1009 | * On error, return NULL. |
| 1010 | */ |
| 1011 | struct lttng_consumer_channel *consumer_allocate_channel(uint64_t key, |
| 1012 | uint64_t session_id, |
| 1013 | const uint64_t *chunk_id, |
| 1014 | const char *pathname, |
| 1015 | const char *name, |
| 1016 | uint64_t relayd_id, |
| 1017 | enum lttng_event_output output, |
| 1018 | uint64_t tracefile_size, |
| 1019 | uint64_t tracefile_count, |
| 1020 | uint64_t session_id_per_pid, |
| 1021 | unsigned int monitor, |
| 1022 | unsigned int live_timer_interval, |
| 1023 | bool is_in_live_session, |
| 1024 | const char *root_shm_path, |
| 1025 | const char *shm_path) |
| 1026 | { |
| 1027 | struct lttng_consumer_channel *channel = NULL; |
| 1028 | struct lttng_trace_chunk *trace_chunk = NULL; |
| 1029 | |
| 1030 | if (chunk_id) { |
| 1031 | trace_chunk = lttng_trace_chunk_registry_find_chunk( |
| 1032 | the_consumer_data.chunk_registry, session_id, |
| 1033 | *chunk_id); |
| 1034 | if (!trace_chunk) { |
| 1035 | ERR("Failed to find trace chunk reference during creation of channel"); |
| 1036 | goto end; |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | channel = zmalloc<lttng_consumer_channel>(); |
| 1041 | if (channel == NULL) { |
| 1042 | PERROR("malloc struct lttng_consumer_channel"); |
| 1043 | goto end; |
| 1044 | } |
| 1045 | |
| 1046 | channel->key = key; |
| 1047 | channel->refcount = 0; |
| 1048 | channel->session_id = session_id; |
| 1049 | channel->session_id_per_pid = session_id_per_pid; |
| 1050 | channel->relayd_id = relayd_id; |
| 1051 | channel->tracefile_size = tracefile_size; |
| 1052 | channel->tracefile_count = tracefile_count; |
| 1053 | channel->monitor = monitor; |
| 1054 | channel->live_timer_interval = live_timer_interval; |
| 1055 | channel->is_live = is_in_live_session; |
| 1056 | pthread_mutex_init(&channel->lock, NULL); |
| 1057 | pthread_mutex_init(&channel->timer_lock, NULL); |
| 1058 | |
| 1059 | switch (output) { |
| 1060 | case LTTNG_EVENT_SPLICE: |
| 1061 | channel->output = CONSUMER_CHANNEL_SPLICE; |
| 1062 | break; |
| 1063 | case LTTNG_EVENT_MMAP: |
| 1064 | channel->output = CONSUMER_CHANNEL_MMAP; |
| 1065 | break; |
| 1066 | default: |
| 1067 | abort(); |
| 1068 | free(channel); |
| 1069 | channel = NULL; |
| 1070 | goto end; |
| 1071 | } |
| 1072 | |
| 1073 | /* |
| 1074 | * In monitor mode, the streams associated with the channel will be put in |
| 1075 | * a special list ONLY owned by this channel. So, the refcount is set to 1 |
| 1076 | * here meaning that the channel itself has streams that are referenced. |
| 1077 | * |
| 1078 | * On a channel deletion, once the channel is no longer visible, the |
| 1079 | * refcount is decremented and checked for a zero value to delete it. With |
| 1080 | * streams in no monitor mode, it will now be safe to destroy the channel. |
| 1081 | */ |
| 1082 | if (!channel->monitor) { |
| 1083 | channel->refcount = 1; |
| 1084 | } |
| 1085 | |
| 1086 | strncpy(channel->pathname, pathname, sizeof(channel->pathname)); |
| 1087 | channel->pathname[sizeof(channel->pathname) - 1] = '\0'; |
| 1088 | |
| 1089 | strncpy(channel->name, name, sizeof(channel->name)); |
| 1090 | channel->name[sizeof(channel->name) - 1] = '\0'; |
| 1091 | |
| 1092 | if (root_shm_path) { |
| 1093 | strncpy(channel->root_shm_path, root_shm_path, sizeof(channel->root_shm_path)); |
| 1094 | channel->root_shm_path[sizeof(channel->root_shm_path) - 1] = '\0'; |
| 1095 | } |
| 1096 | if (shm_path) { |
| 1097 | strncpy(channel->shm_path, shm_path, sizeof(channel->shm_path)); |
| 1098 | channel->shm_path[sizeof(channel->shm_path) - 1] = '\0'; |
| 1099 | } |
| 1100 | |
| 1101 | lttng_ht_node_init_u64(&channel->node, channel->key); |
| 1102 | lttng_ht_node_init_u64(&channel->channels_by_session_id_ht_node, |
| 1103 | channel->session_id); |
| 1104 | |
| 1105 | channel->wait_fd = -1; |
| 1106 | CDS_INIT_LIST_HEAD(&channel->streams.head); |
| 1107 | |
| 1108 | if (trace_chunk) { |
| 1109 | int ret = lttng_consumer_channel_set_trace_chunk(channel, |
| 1110 | trace_chunk); |
| 1111 | if (ret) { |
| 1112 | goto error; |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | DBG("Allocated channel (key %" PRIu64 ")", channel->key); |
| 1117 | |
| 1118 | end: |
| 1119 | lttng_trace_chunk_put(trace_chunk); |
| 1120 | return channel; |
| 1121 | error: |
| 1122 | consumer_del_channel(channel); |
| 1123 | channel = NULL; |
| 1124 | goto end; |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * Add a channel to the global list protected by a mutex. |
| 1129 | * |
| 1130 | * Always return 0 indicating success. |
| 1131 | */ |
| 1132 | int consumer_add_channel(struct lttng_consumer_channel *channel, |
| 1133 | struct lttng_consumer_local_data *ctx) |
| 1134 | { |
| 1135 | pthread_mutex_lock(&the_consumer_data.lock); |
| 1136 | pthread_mutex_lock(&channel->lock); |
| 1137 | pthread_mutex_lock(&channel->timer_lock); |
| 1138 | |
| 1139 | /* |
| 1140 | * This gives us a guarantee that the channel we are about to add to the |
| 1141 | * channel hash table will be unique. See this function comment on the why |
| 1142 | * we need to steel the channel key at this stage. |
| 1143 | */ |
| 1144 | steal_channel_key(channel->key); |
| 1145 | |
| 1146 | rcu_read_lock(); |
| 1147 | lttng_ht_add_unique_u64(the_consumer_data.channel_ht, &channel->node); |
| 1148 | lttng_ht_add_u64(the_consumer_data.channels_by_session_id_ht, |
| 1149 | &channel->channels_by_session_id_ht_node); |
| 1150 | rcu_read_unlock(); |
| 1151 | channel->is_published = true; |
| 1152 | |
| 1153 | pthread_mutex_unlock(&channel->timer_lock); |
| 1154 | pthread_mutex_unlock(&channel->lock); |
| 1155 | pthread_mutex_unlock(&the_consumer_data.lock); |
| 1156 | |
| 1157 | if (channel->wait_fd != -1 && channel->type == CONSUMER_CHANNEL_TYPE_DATA) { |
| 1158 | notify_channel_pipe(ctx, channel, -1, CONSUMER_CHANNEL_ADD); |
| 1159 | } |
| 1160 | |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
| 1164 | /* |
| 1165 | * Allocate the pollfd structure and the local view of the out fds to avoid |
| 1166 | * doing a lookup in the linked list and concurrency issues when writing is |
| 1167 | * needed. Called with consumer_data.lock held. |
| 1168 | * |
| 1169 | * Returns the number of fds in the structures. |
| 1170 | */ |
| 1171 | static int update_poll_array(struct lttng_consumer_local_data *ctx, |
| 1172 | struct pollfd **pollfd, struct lttng_consumer_stream **local_stream, |
| 1173 | struct lttng_ht *ht, int *nb_inactive_fd) |
| 1174 | { |
| 1175 | int i = 0; |
| 1176 | struct lttng_ht_iter iter; |
| 1177 | struct lttng_consumer_stream *stream; |
| 1178 | |
| 1179 | LTTNG_ASSERT(ctx); |
| 1180 | LTTNG_ASSERT(ht); |
| 1181 | LTTNG_ASSERT(pollfd); |
| 1182 | LTTNG_ASSERT(local_stream); |
| 1183 | |
| 1184 | DBG("Updating poll fd array"); |
| 1185 | *nb_inactive_fd = 0; |
| 1186 | rcu_read_lock(); |
| 1187 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { |
| 1188 | /* |
| 1189 | * Only active streams with an active end point can be added to the |
| 1190 | * poll set and local stream storage of the thread. |
| 1191 | * |
| 1192 | * There is a potential race here for endpoint_status to be updated |
| 1193 | * just after the check. However, this is OK since the stream(s) will |
| 1194 | * be deleted once the thread is notified that the end point state has |
| 1195 | * changed where this function will be called back again. |
| 1196 | * |
| 1197 | * We track the number of inactive FDs because they still need to be |
| 1198 | * closed by the polling thread after a wakeup on the data_pipe or |
| 1199 | * metadata_pipe. |
| 1200 | */ |
| 1201 | if (stream->endpoint_status == CONSUMER_ENDPOINT_INACTIVE) { |
| 1202 | (*nb_inactive_fd)++; |
| 1203 | continue; |
| 1204 | } |
| 1205 | /* |
| 1206 | * This clobbers way too much the debug output. Uncomment that if you |
| 1207 | * need it for debugging purposes. |
| 1208 | */ |
| 1209 | (*pollfd)[i].fd = stream->wait_fd; |
| 1210 | (*pollfd)[i].events = POLLIN | POLLPRI; |
| 1211 | local_stream[i] = stream; |
| 1212 | i++; |
| 1213 | } |
| 1214 | rcu_read_unlock(); |
| 1215 | |
| 1216 | /* |
| 1217 | * Insert the consumer_data_pipe at the end of the array and don't |
| 1218 | * increment i so nb_fd is the number of real FD. |
| 1219 | */ |
| 1220 | (*pollfd)[i].fd = lttng_pipe_get_readfd(ctx->consumer_data_pipe); |
| 1221 | (*pollfd)[i].events = POLLIN | POLLPRI; |
| 1222 | |
| 1223 | (*pollfd)[i + 1].fd = lttng_pipe_get_readfd(ctx->consumer_wakeup_pipe); |
| 1224 | (*pollfd)[i + 1].events = POLLIN | POLLPRI; |
| 1225 | return i; |
| 1226 | } |
| 1227 | |
| 1228 | /* |
| 1229 | * Poll on the should_quit pipe and the command socket return -1 on |
| 1230 | * error, 1 if should exit, 0 if data is available on the command socket |
| 1231 | */ |
| 1232 | int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll) |
| 1233 | { |
| 1234 | int num_rdy; |
| 1235 | |
| 1236 | restart: |
| 1237 | num_rdy = poll(consumer_sockpoll, 2, -1); |
| 1238 | if (num_rdy == -1) { |
| 1239 | /* |
| 1240 | * Restart interrupted system call. |
| 1241 | */ |
| 1242 | if (errno == EINTR) { |
| 1243 | goto restart; |
| 1244 | } |
| 1245 | PERROR("Poll error"); |
| 1246 | return -1; |
| 1247 | } |
| 1248 | if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) { |
| 1249 | DBG("consumer_should_quit wake up"); |
| 1250 | return 1; |
| 1251 | } |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | * Set the error socket. |
| 1257 | */ |
| 1258 | void lttng_consumer_set_error_sock(struct lttng_consumer_local_data *ctx, |
| 1259 | int sock) |
| 1260 | { |
| 1261 | ctx->consumer_error_socket = sock; |
| 1262 | } |
| 1263 | |
| 1264 | /* |
| 1265 | * Set the command socket path. |
| 1266 | */ |
| 1267 | void lttng_consumer_set_command_sock_path( |
| 1268 | struct lttng_consumer_local_data *ctx, char *sock) |
| 1269 | { |
| 1270 | ctx->consumer_command_sock_path = sock; |
| 1271 | } |
| 1272 | |
| 1273 | /* |
| 1274 | * Send return code to the session daemon. |
| 1275 | * If the socket is not defined, we return 0, it is not a fatal error |
| 1276 | */ |
| 1277 | int lttng_consumer_send_error(struct lttng_consumer_local_data *ctx, int cmd) |
| 1278 | { |
| 1279 | if (ctx->consumer_error_socket > 0) { |
| 1280 | return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd, |
| 1281 | sizeof(enum lttcomm_sessiond_command)); |
| 1282 | } |
| 1283 | |
| 1284 | return 0; |
| 1285 | } |
| 1286 | |
| 1287 | /* |
| 1288 | * Close all the tracefiles and stream fds and MUST be called when all |
| 1289 | * instances are destroyed i.e. when all threads were joined and are ended. |
| 1290 | */ |
| 1291 | void lttng_consumer_cleanup(void) |
| 1292 | { |
| 1293 | struct lttng_ht_iter iter; |
| 1294 | struct lttng_consumer_channel *channel; |
| 1295 | unsigned int trace_chunks_left; |
| 1296 | |
| 1297 | rcu_read_lock(); |
| 1298 | |
| 1299 | cds_lfht_for_each_entry(the_consumer_data.channel_ht->ht, &iter.iter, |
| 1300 | channel, node.node) { |
| 1301 | consumer_del_channel(channel); |
| 1302 | } |
| 1303 | |
| 1304 | rcu_read_unlock(); |
| 1305 | |
| 1306 | lttng_ht_destroy(the_consumer_data.channel_ht); |
| 1307 | lttng_ht_destroy(the_consumer_data.channels_by_session_id_ht); |
| 1308 | |
| 1309 | cleanup_relayd_ht(); |
| 1310 | |
| 1311 | lttng_ht_destroy(the_consumer_data.stream_per_chan_id_ht); |
| 1312 | |
| 1313 | /* |
| 1314 | * This HT contains streams that are freed by either the metadata thread or |
| 1315 | * the data thread so we do *nothing* on the hash table and simply destroy |
| 1316 | * it. |
| 1317 | */ |
| 1318 | lttng_ht_destroy(the_consumer_data.stream_list_ht); |
| 1319 | |
| 1320 | /* |
| 1321 | * Trace chunks in the registry may still exist if the session |
| 1322 | * daemon has encountered an internal error and could not |
| 1323 | * tear down its sessions and/or trace chunks properly. |
| 1324 | * |
| 1325 | * Release the session daemon's implicit reference to any remaining |
| 1326 | * trace chunk and print an error if any trace chunk was found. Note |
| 1327 | * that there are _no_ legitimate cases for trace chunks to be left, |
| 1328 | * it is a leak. However, it can happen following a crash of the |
| 1329 | * session daemon and not emptying the registry would cause an assertion |
| 1330 | * to hit. |
| 1331 | */ |
| 1332 | trace_chunks_left = lttng_trace_chunk_registry_put_each_chunk( |
| 1333 | the_consumer_data.chunk_registry); |
| 1334 | if (trace_chunks_left) { |
| 1335 | ERR("%u trace chunks are leaked by lttng-consumerd. " |
| 1336 | "This can be caused by an internal error of the session daemon.", |
| 1337 | trace_chunks_left); |
| 1338 | } |
| 1339 | /* Run all callbacks freeing each chunk. */ |
| 1340 | rcu_barrier(); |
| 1341 | lttng_trace_chunk_registry_destroy(the_consumer_data.chunk_registry); |
| 1342 | } |
| 1343 | |
| 1344 | /* |
| 1345 | * Called from signal handler. |
| 1346 | */ |
| 1347 | void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx) |
| 1348 | { |
| 1349 | ssize_t ret; |
| 1350 | |
| 1351 | CMM_STORE_SHARED(consumer_quit, 1); |
| 1352 | ret = lttng_write(ctx->consumer_should_quit[1], "4", 1); |
| 1353 | if (ret < 1) { |
| 1354 | PERROR("write consumer quit"); |
| 1355 | } |
| 1356 | |
| 1357 | DBG("Consumer flag that it should quit"); |
| 1358 | } |
| 1359 | |
| 1360 | |
| 1361 | /* |
| 1362 | * Flush pending writes to trace output disk file. |
| 1363 | */ |
| 1364 | static |
| 1365 | void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream, |
| 1366 | off_t orig_offset) |
| 1367 | { |
| 1368 | int ret; |
| 1369 | int outfd = stream->out_fd; |
| 1370 | |
| 1371 | /* |
| 1372 | * This does a blocking write-and-wait on any page that belongs to the |
| 1373 | * subbuffer prior to the one we just wrote. |
| 1374 | * Don't care about error values, as these are just hints and ways to |
| 1375 | * limit the amount of page cache used. |
| 1376 | */ |
| 1377 | if (orig_offset < stream->max_sb_size) { |
| 1378 | return; |
| 1379 | } |
| 1380 | lttng_sync_file_range(outfd, orig_offset - stream->max_sb_size, |
| 1381 | stream->max_sb_size, |
| 1382 | SYNC_FILE_RANGE_WAIT_BEFORE |
| 1383 | | SYNC_FILE_RANGE_WRITE |
| 1384 | | SYNC_FILE_RANGE_WAIT_AFTER); |
| 1385 | /* |
| 1386 | * Give hints to the kernel about how we access the file: |
| 1387 | * POSIX_FADV_DONTNEED : we won't re-access data in a near future after |
| 1388 | * we write it. |
| 1389 | * |
| 1390 | * We need to call fadvise again after the file grows because the |
| 1391 | * kernel does not seem to apply fadvise to non-existing parts of the |
| 1392 | * file. |
| 1393 | * |
| 1394 | * Call fadvise _after_ having waited for the page writeback to |
| 1395 | * complete because the dirty page writeback semantic is not well |
| 1396 | * defined. So it can be expected to lead to lower throughput in |
| 1397 | * streaming. |
| 1398 | */ |
| 1399 | ret = posix_fadvise(outfd, orig_offset - stream->max_sb_size, |
| 1400 | stream->max_sb_size, POSIX_FADV_DONTNEED); |
| 1401 | if (ret && ret != -ENOSYS) { |
| 1402 | errno = ret; |
| 1403 | PERROR("posix_fadvise on fd %i", outfd); |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | /* |
| 1408 | * Initialise the necessary environnement : |
| 1409 | * - create a new context |
| 1410 | * - create the poll_pipe |
| 1411 | * - create the should_quit pipe (for signal handler) |
| 1412 | * - create the thread pipe (for splice) |
| 1413 | * |
| 1414 | * Takes a function pointer as argument, this function is called when data is |
| 1415 | * available on a buffer. This function is responsible to do the |
| 1416 | * kernctl_get_next_subbuf, read the data with mmap or splice depending on the |
| 1417 | * buffer configuration and then kernctl_put_next_subbuf at the end. |
| 1418 | * |
| 1419 | * Returns a pointer to the new context or NULL on error. |
| 1420 | */ |
| 1421 | struct lttng_consumer_local_data *lttng_consumer_create( |
| 1422 | enum lttng_consumer_type type, |
| 1423 | ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream, |
| 1424 | struct lttng_consumer_local_data *ctx, bool locked_by_caller), |
| 1425 | int (*recv_channel)(struct lttng_consumer_channel *channel), |
| 1426 | int (*recv_stream)(struct lttng_consumer_stream *stream), |
| 1427 | int (*update_stream)(uint64_t stream_key, uint32_t state)) |
| 1428 | { |
| 1429 | int ret; |
| 1430 | struct lttng_consumer_local_data *ctx; |
| 1431 | |
| 1432 | LTTNG_ASSERT(the_consumer_data.type == LTTNG_CONSUMER_UNKNOWN || |
| 1433 | the_consumer_data.type == type); |
| 1434 | the_consumer_data.type = type; |
| 1435 | |
| 1436 | ctx = zmalloc<lttng_consumer_local_data>(); |
| 1437 | if (ctx == NULL) { |
| 1438 | PERROR("allocating context"); |
| 1439 | goto error; |
| 1440 | } |
| 1441 | |
| 1442 | ctx->consumer_error_socket = -1; |
| 1443 | ctx->consumer_metadata_socket = -1; |
| 1444 | pthread_mutex_init(&ctx->metadata_socket_lock, NULL); |
| 1445 | /* assign the callbacks */ |
| 1446 | ctx->on_buffer_ready = buffer_ready; |
| 1447 | ctx->on_recv_channel = recv_channel; |
| 1448 | ctx->on_recv_stream = recv_stream; |
| 1449 | ctx->on_update_stream = update_stream; |
| 1450 | |
| 1451 | ctx->consumer_data_pipe = lttng_pipe_open(0); |
| 1452 | if (!ctx->consumer_data_pipe) { |
| 1453 | goto error_poll_pipe; |
| 1454 | } |
| 1455 | |
| 1456 | ctx->consumer_wakeup_pipe = lttng_pipe_open(0); |
| 1457 | if (!ctx->consumer_wakeup_pipe) { |
| 1458 | goto error_wakeup_pipe; |
| 1459 | } |
| 1460 | |
| 1461 | ret = pipe(ctx->consumer_should_quit); |
| 1462 | if (ret < 0) { |
| 1463 | PERROR("Error creating recv pipe"); |
| 1464 | goto error_quit_pipe; |
| 1465 | } |
| 1466 | |
| 1467 | ret = pipe(ctx->consumer_channel_pipe); |
| 1468 | if (ret < 0) { |
| 1469 | PERROR("Error creating channel pipe"); |
| 1470 | goto error_channel_pipe; |
| 1471 | } |
| 1472 | |
| 1473 | ctx->consumer_metadata_pipe = lttng_pipe_open(0); |
| 1474 | if (!ctx->consumer_metadata_pipe) { |
| 1475 | goto error_metadata_pipe; |
| 1476 | } |
| 1477 | |
| 1478 | ctx->channel_monitor_pipe = -1; |
| 1479 | |
| 1480 | return ctx; |
| 1481 | |
| 1482 | error_metadata_pipe: |
| 1483 | utils_close_pipe(ctx->consumer_channel_pipe); |
| 1484 | error_channel_pipe: |
| 1485 | utils_close_pipe(ctx->consumer_should_quit); |
| 1486 | error_quit_pipe: |
| 1487 | lttng_pipe_destroy(ctx->consumer_wakeup_pipe); |
| 1488 | error_wakeup_pipe: |
| 1489 | lttng_pipe_destroy(ctx->consumer_data_pipe); |
| 1490 | error_poll_pipe: |
| 1491 | free(ctx); |
| 1492 | error: |
| 1493 | return NULL; |
| 1494 | } |
| 1495 | |
| 1496 | /* |
| 1497 | * Iterate over all streams of the hashtable and free them properly. |
| 1498 | */ |
| 1499 | static void destroy_data_stream_ht(struct lttng_ht *ht) |
| 1500 | { |
| 1501 | struct lttng_ht_iter iter; |
| 1502 | struct lttng_consumer_stream *stream; |
| 1503 | |
| 1504 | if (ht == NULL) { |
| 1505 | return; |
| 1506 | } |
| 1507 | |
| 1508 | rcu_read_lock(); |
| 1509 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { |
| 1510 | /* |
| 1511 | * Ignore return value since we are currently cleaning up so any error |
| 1512 | * can't be handled. |
| 1513 | */ |
| 1514 | (void) consumer_del_stream(stream, ht); |
| 1515 | } |
| 1516 | rcu_read_unlock(); |
| 1517 | |
| 1518 | lttng_ht_destroy(ht); |
| 1519 | } |
| 1520 | |
| 1521 | /* |
| 1522 | * Iterate over all streams of the metadata hashtable and free them |
| 1523 | * properly. |
| 1524 | */ |
| 1525 | static void destroy_metadata_stream_ht(struct lttng_ht *ht) |
| 1526 | { |
| 1527 | struct lttng_ht_iter iter; |
| 1528 | struct lttng_consumer_stream *stream; |
| 1529 | |
| 1530 | if (ht == NULL) { |
| 1531 | return; |
| 1532 | } |
| 1533 | |
| 1534 | rcu_read_lock(); |
| 1535 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { |
| 1536 | /* |
| 1537 | * Ignore return value since we are currently cleaning up so any error |
| 1538 | * can't be handled. |
| 1539 | */ |
| 1540 | (void) consumer_del_metadata_stream(stream, ht); |
| 1541 | } |
| 1542 | rcu_read_unlock(); |
| 1543 | |
| 1544 | lttng_ht_destroy(ht); |
| 1545 | } |
| 1546 | |
| 1547 | /* |
| 1548 | * Close all fds associated with the instance and free the context. |
| 1549 | */ |
| 1550 | void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx) |
| 1551 | { |
| 1552 | int ret; |
| 1553 | |
| 1554 | DBG("Consumer destroying it. Closing everything."); |
| 1555 | |
| 1556 | if (!ctx) { |
| 1557 | return; |
| 1558 | } |
| 1559 | |
| 1560 | destroy_data_stream_ht(data_ht); |
| 1561 | destroy_metadata_stream_ht(metadata_ht); |
| 1562 | |
| 1563 | ret = close(ctx->consumer_error_socket); |
| 1564 | if (ret) { |
| 1565 | PERROR("close"); |
| 1566 | } |
| 1567 | ret = close(ctx->consumer_metadata_socket); |
| 1568 | if (ret) { |
| 1569 | PERROR("close"); |
| 1570 | } |
| 1571 | utils_close_pipe(ctx->consumer_channel_pipe); |
| 1572 | lttng_pipe_destroy(ctx->consumer_data_pipe); |
| 1573 | lttng_pipe_destroy(ctx->consumer_metadata_pipe); |
| 1574 | lttng_pipe_destroy(ctx->consumer_wakeup_pipe); |
| 1575 | utils_close_pipe(ctx->consumer_should_quit); |
| 1576 | |
| 1577 | unlink(ctx->consumer_command_sock_path); |
| 1578 | free(ctx); |
| 1579 | } |
| 1580 | |
| 1581 | /* |
| 1582 | * Write the metadata stream id on the specified file descriptor. |
| 1583 | */ |
| 1584 | static int write_relayd_metadata_id(int fd, |
| 1585 | struct lttng_consumer_stream *stream, |
| 1586 | unsigned long padding) |
| 1587 | { |
| 1588 | ssize_t ret; |
| 1589 | struct lttcomm_relayd_metadata_payload hdr; |
| 1590 | |
| 1591 | hdr.stream_id = htobe64(stream->relayd_stream_id); |
| 1592 | hdr.padding_size = htobe32(padding); |
| 1593 | ret = lttng_write(fd, (void *) &hdr, sizeof(hdr)); |
| 1594 | if (ret < sizeof(hdr)) { |
| 1595 | /* |
| 1596 | * This error means that the fd's end is closed so ignore the PERROR |
| 1597 | * not to clubber the error output since this can happen in a normal |
| 1598 | * code path. |
| 1599 | */ |
| 1600 | if (errno != EPIPE) { |
| 1601 | PERROR("write metadata stream id"); |
| 1602 | } |
| 1603 | DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno); |
| 1604 | /* |
| 1605 | * Set ret to a negative value because if ret != sizeof(hdr), we don't |
| 1606 | * handle writting the missing part so report that as an error and |
| 1607 | * don't lie to the caller. |
| 1608 | */ |
| 1609 | ret = -1; |
| 1610 | goto end; |
| 1611 | } |
| 1612 | DBG("Metadata stream id %" PRIu64 " with padding %lu written before data", |
| 1613 | stream->relayd_stream_id, padding); |
| 1614 | |
| 1615 | end: |
| 1616 | return (int) ret; |
| 1617 | } |
| 1618 | |
| 1619 | /* |
| 1620 | * Mmap the ring buffer, read it and write the data to the tracefile. This is a |
| 1621 | * core function for writing trace buffers to either the local filesystem or |
| 1622 | * the network. |
| 1623 | * |
| 1624 | * It must be called with the stream and the channel lock held. |
| 1625 | * |
| 1626 | * Careful review MUST be put if any changes occur! |
| 1627 | * |
| 1628 | * Returns the number of bytes written |
| 1629 | */ |
| 1630 | ssize_t lttng_consumer_on_read_subbuffer_mmap( |
| 1631 | struct lttng_consumer_stream *stream, |
| 1632 | const struct lttng_buffer_view *buffer, |
| 1633 | unsigned long padding) |
| 1634 | { |
| 1635 | ssize_t ret = 0; |
| 1636 | off_t orig_offset = stream->out_fd_offset; |
| 1637 | /* Default is on the disk */ |
| 1638 | int outfd = stream->out_fd; |
| 1639 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 1640 | unsigned int relayd_hang_up = 0; |
| 1641 | const size_t subbuf_content_size = buffer->size - padding; |
| 1642 | size_t write_len; |
| 1643 | |
| 1644 | /* RCU lock for the relayd pointer */ |
| 1645 | rcu_read_lock(); |
| 1646 | LTTNG_ASSERT(stream->net_seq_idx != (uint64_t) -1ULL || |
| 1647 | stream->trace_chunk); |
| 1648 | |
| 1649 | /* Flag that the current stream if set for network streaming. */ |
| 1650 | if (stream->net_seq_idx != (uint64_t) -1ULL) { |
| 1651 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 1652 | if (relayd == NULL) { |
| 1653 | ret = -EPIPE; |
| 1654 | goto end; |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | /* Handle stream on the relayd if the output is on the network */ |
| 1659 | if (relayd) { |
| 1660 | unsigned long netlen = subbuf_content_size; |
| 1661 | |
| 1662 | /* |
| 1663 | * Lock the control socket for the complete duration of the function |
| 1664 | * since from this point on we will use the socket. |
| 1665 | */ |
| 1666 | if (stream->metadata_flag) { |
| 1667 | /* Metadata requires the control socket. */ |
| 1668 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 1669 | if (stream->reset_metadata_flag) { |
| 1670 | ret = relayd_reset_metadata(&relayd->control_sock, |
| 1671 | stream->relayd_stream_id, |
| 1672 | stream->metadata_version); |
| 1673 | if (ret < 0) { |
| 1674 | relayd_hang_up = 1; |
| 1675 | goto write_error; |
| 1676 | } |
| 1677 | stream->reset_metadata_flag = 0; |
| 1678 | } |
| 1679 | netlen += sizeof(struct lttcomm_relayd_metadata_payload); |
| 1680 | } |
| 1681 | |
| 1682 | ret = write_relayd_stream_header(stream, netlen, padding, relayd); |
| 1683 | if (ret < 0) { |
| 1684 | relayd_hang_up = 1; |
| 1685 | goto write_error; |
| 1686 | } |
| 1687 | /* Use the returned socket. */ |
| 1688 | outfd = ret; |
| 1689 | |
| 1690 | /* Write metadata stream id before payload */ |
| 1691 | if (stream->metadata_flag) { |
| 1692 | ret = write_relayd_metadata_id(outfd, stream, padding); |
| 1693 | if (ret < 0) { |
| 1694 | relayd_hang_up = 1; |
| 1695 | goto write_error; |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | write_len = subbuf_content_size; |
| 1700 | } else { |
| 1701 | /* No streaming; we have to write the full padding. */ |
| 1702 | if (stream->metadata_flag && stream->reset_metadata_flag) { |
| 1703 | ret = utils_truncate_stream_file(stream->out_fd, 0); |
| 1704 | if (ret < 0) { |
| 1705 | ERR("Reset metadata file"); |
| 1706 | goto end; |
| 1707 | } |
| 1708 | stream->reset_metadata_flag = 0; |
| 1709 | } |
| 1710 | |
| 1711 | /* |
| 1712 | * Check if we need to change the tracefile before writing the packet. |
| 1713 | */ |
| 1714 | if (stream->chan->tracefile_size > 0 && |
| 1715 | (stream->tracefile_size_current + buffer->size) > |
| 1716 | stream->chan->tracefile_size) { |
| 1717 | ret = consumer_stream_rotate_output_files(stream); |
| 1718 | if (ret) { |
| 1719 | goto end; |
| 1720 | } |
| 1721 | outfd = stream->out_fd; |
| 1722 | orig_offset = 0; |
| 1723 | } |
| 1724 | stream->tracefile_size_current += buffer->size; |
| 1725 | write_len = buffer->size; |
| 1726 | } |
| 1727 | |
| 1728 | /* |
| 1729 | * This call guarantee that len or less is returned. It's impossible to |
| 1730 | * receive a ret value that is bigger than len. |
| 1731 | */ |
| 1732 | ret = lttng_write(outfd, buffer->data, write_len); |
| 1733 | DBG("Consumer mmap write() ret %zd (len %zu)", ret, write_len); |
| 1734 | if (ret < 0 || ((size_t) ret != write_len)) { |
| 1735 | /* |
| 1736 | * Report error to caller if nothing was written else at least send the |
| 1737 | * amount written. |
| 1738 | */ |
| 1739 | if (ret < 0) { |
| 1740 | ret = -errno; |
| 1741 | } |
| 1742 | relayd_hang_up = 1; |
| 1743 | |
| 1744 | /* Socket operation failed. We consider the relayd dead */ |
| 1745 | if (errno == EPIPE) { |
| 1746 | /* |
| 1747 | * This is possible if the fd is closed on the other side |
| 1748 | * (outfd) or any write problem. It can be verbose a bit for a |
| 1749 | * normal execution if for instance the relayd is stopped |
| 1750 | * abruptly. This can happen so set this to a DBG statement. |
| 1751 | */ |
| 1752 | DBG("Consumer mmap write detected relayd hang up"); |
| 1753 | } else { |
| 1754 | /* Unhandled error, print it and stop function right now. */ |
| 1755 | PERROR("Error in write mmap (ret %zd != write_len %zu)", ret, |
| 1756 | write_len); |
| 1757 | } |
| 1758 | goto write_error; |
| 1759 | } |
| 1760 | stream->output_written += ret; |
| 1761 | |
| 1762 | /* This call is useless on a socket so better save a syscall. */ |
| 1763 | if (!relayd) { |
| 1764 | /* This won't block, but will start writeout asynchronously */ |
| 1765 | lttng_sync_file_range(outfd, stream->out_fd_offset, write_len, |
| 1766 | SYNC_FILE_RANGE_WRITE); |
| 1767 | stream->out_fd_offset += write_len; |
| 1768 | lttng_consumer_sync_trace_file(stream, orig_offset); |
| 1769 | } |
| 1770 | |
| 1771 | write_error: |
| 1772 | /* |
| 1773 | * This is a special case that the relayd has closed its socket. Let's |
| 1774 | * cleanup the relayd object and all associated streams. |
| 1775 | */ |
| 1776 | if (relayd && relayd_hang_up) { |
| 1777 | ERR("Relayd hangup. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
| 1778 | lttng_consumer_cleanup_relayd(relayd); |
| 1779 | } |
| 1780 | |
| 1781 | end: |
| 1782 | /* Unlock only if ctrl socket used */ |
| 1783 | if (relayd && stream->metadata_flag) { |
| 1784 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 1785 | } |
| 1786 | |
| 1787 | rcu_read_unlock(); |
| 1788 | return ret; |
| 1789 | } |
| 1790 | |
| 1791 | /* |
| 1792 | * Splice the data from the ring buffer to the tracefile. |
| 1793 | * |
| 1794 | * It must be called with the stream lock held. |
| 1795 | * |
| 1796 | * Returns the number of bytes spliced. |
| 1797 | */ |
| 1798 | ssize_t lttng_consumer_on_read_subbuffer_splice( |
| 1799 | struct lttng_consumer_local_data *ctx, |
| 1800 | struct lttng_consumer_stream *stream, unsigned long len, |
| 1801 | unsigned long padding) |
| 1802 | { |
| 1803 | ssize_t ret = 0, written = 0, ret_splice = 0; |
| 1804 | loff_t offset = 0; |
| 1805 | off_t orig_offset = stream->out_fd_offset; |
| 1806 | int fd = stream->wait_fd; |
| 1807 | /* Default is on the disk */ |
| 1808 | int outfd = stream->out_fd; |
| 1809 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 1810 | int *splice_pipe; |
| 1811 | unsigned int relayd_hang_up = 0; |
| 1812 | |
| 1813 | switch (the_consumer_data.type) { |
| 1814 | case LTTNG_CONSUMER_KERNEL: |
| 1815 | break; |
| 1816 | case LTTNG_CONSUMER32_UST: |
| 1817 | case LTTNG_CONSUMER64_UST: |
| 1818 | /* Not supported for user space tracing */ |
| 1819 | return -ENOSYS; |
| 1820 | default: |
| 1821 | ERR("Unknown consumer_data type"); |
| 1822 | abort(); |
| 1823 | } |
| 1824 | |
| 1825 | /* RCU lock for the relayd pointer */ |
| 1826 | rcu_read_lock(); |
| 1827 | |
| 1828 | /* Flag that the current stream if set for network streaming. */ |
| 1829 | if (stream->net_seq_idx != (uint64_t) -1ULL) { |
| 1830 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 1831 | if (relayd == NULL) { |
| 1832 | written = -ret; |
| 1833 | goto end; |
| 1834 | } |
| 1835 | } |
| 1836 | splice_pipe = stream->splice_pipe; |
| 1837 | |
| 1838 | /* Write metadata stream id before payload */ |
| 1839 | if (relayd) { |
| 1840 | unsigned long total_len = len; |
| 1841 | |
| 1842 | if (stream->metadata_flag) { |
| 1843 | /* |
| 1844 | * Lock the control socket for the complete duration of the function |
| 1845 | * since from this point on we will use the socket. |
| 1846 | */ |
| 1847 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 1848 | |
| 1849 | if (stream->reset_metadata_flag) { |
| 1850 | ret = relayd_reset_metadata(&relayd->control_sock, |
| 1851 | stream->relayd_stream_id, |
| 1852 | stream->metadata_version); |
| 1853 | if (ret < 0) { |
| 1854 | relayd_hang_up = 1; |
| 1855 | goto write_error; |
| 1856 | } |
| 1857 | stream->reset_metadata_flag = 0; |
| 1858 | } |
| 1859 | ret = write_relayd_metadata_id(splice_pipe[1], stream, |
| 1860 | padding); |
| 1861 | if (ret < 0) { |
| 1862 | written = ret; |
| 1863 | relayd_hang_up = 1; |
| 1864 | goto write_error; |
| 1865 | } |
| 1866 | |
| 1867 | total_len += sizeof(struct lttcomm_relayd_metadata_payload); |
| 1868 | } |
| 1869 | |
| 1870 | ret = write_relayd_stream_header(stream, total_len, padding, relayd); |
| 1871 | if (ret < 0) { |
| 1872 | written = ret; |
| 1873 | relayd_hang_up = 1; |
| 1874 | goto write_error; |
| 1875 | } |
| 1876 | /* Use the returned socket. */ |
| 1877 | outfd = ret; |
| 1878 | } else { |
| 1879 | /* No streaming, we have to set the len with the full padding */ |
| 1880 | len += padding; |
| 1881 | |
| 1882 | if (stream->metadata_flag && stream->reset_metadata_flag) { |
| 1883 | ret = utils_truncate_stream_file(stream->out_fd, 0); |
| 1884 | if (ret < 0) { |
| 1885 | ERR("Reset metadata file"); |
| 1886 | goto end; |
| 1887 | } |
| 1888 | stream->reset_metadata_flag = 0; |
| 1889 | } |
| 1890 | /* |
| 1891 | * Check if we need to change the tracefile before writing the packet. |
| 1892 | */ |
| 1893 | if (stream->chan->tracefile_size > 0 && |
| 1894 | (stream->tracefile_size_current + len) > |
| 1895 | stream->chan->tracefile_size) { |
| 1896 | ret = consumer_stream_rotate_output_files(stream); |
| 1897 | if (ret < 0) { |
| 1898 | written = ret; |
| 1899 | goto end; |
| 1900 | } |
| 1901 | outfd = stream->out_fd; |
| 1902 | orig_offset = 0; |
| 1903 | } |
| 1904 | stream->tracefile_size_current += len; |
| 1905 | } |
| 1906 | |
| 1907 | while (len > 0) { |
| 1908 | DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)", |
| 1909 | (unsigned long)offset, len, fd, splice_pipe[1]); |
| 1910 | ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len, |
| 1911 | SPLICE_F_MOVE | SPLICE_F_MORE); |
| 1912 | DBG("splice chan to pipe, ret %zd", ret_splice); |
| 1913 | if (ret_splice < 0) { |
| 1914 | ret = errno; |
| 1915 | written = -ret; |
| 1916 | PERROR("Error in relay splice"); |
| 1917 | goto splice_error; |
| 1918 | } |
| 1919 | |
| 1920 | /* Handle stream on the relayd if the output is on the network */ |
| 1921 | if (relayd && stream->metadata_flag) { |
| 1922 | size_t metadata_payload_size = |
| 1923 | sizeof(struct lttcomm_relayd_metadata_payload); |
| 1924 | |
| 1925 | /* Update counter to fit the spliced data */ |
| 1926 | ret_splice += metadata_payload_size; |
| 1927 | len += metadata_payload_size; |
| 1928 | /* |
| 1929 | * We do this so the return value can match the len passed as |
| 1930 | * argument to this function. |
| 1931 | */ |
| 1932 | written -= metadata_payload_size; |
| 1933 | } |
| 1934 | |
| 1935 | /* Splice data out */ |
| 1936 | ret_splice = splice(splice_pipe[0], NULL, outfd, NULL, |
| 1937 | ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE); |
| 1938 | DBG("Consumer splice pipe to file (out_fd: %d), ret %zd", |
| 1939 | outfd, ret_splice); |
| 1940 | if (ret_splice < 0) { |
| 1941 | ret = errno; |
| 1942 | written = -ret; |
| 1943 | relayd_hang_up = 1; |
| 1944 | goto write_error; |
| 1945 | } else if (ret_splice > len) { |
| 1946 | /* |
| 1947 | * We don't expect this code path to be executed but you never know |
| 1948 | * so this is an extra protection agains a buggy splice(). |
| 1949 | */ |
| 1950 | ret = errno; |
| 1951 | written += ret_splice; |
| 1952 | PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice, |
| 1953 | len); |
| 1954 | goto splice_error; |
| 1955 | } else { |
| 1956 | /* All good, update current len and continue. */ |
| 1957 | len -= ret_splice; |
| 1958 | } |
| 1959 | |
| 1960 | /* This call is useless on a socket so better save a syscall. */ |
| 1961 | if (!relayd) { |
| 1962 | /* This won't block, but will start writeout asynchronously */ |
| 1963 | lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice, |
| 1964 | SYNC_FILE_RANGE_WRITE); |
| 1965 | stream->out_fd_offset += ret_splice; |
| 1966 | } |
| 1967 | stream->output_written += ret_splice; |
| 1968 | written += ret_splice; |
| 1969 | } |
| 1970 | if (!relayd) { |
| 1971 | lttng_consumer_sync_trace_file(stream, orig_offset); |
| 1972 | } |
| 1973 | goto end; |
| 1974 | |
| 1975 | write_error: |
| 1976 | /* |
| 1977 | * This is a special case that the relayd has closed its socket. Let's |
| 1978 | * cleanup the relayd object and all associated streams. |
| 1979 | */ |
| 1980 | if (relayd && relayd_hang_up) { |
| 1981 | ERR("Relayd hangup. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
| 1982 | lttng_consumer_cleanup_relayd(relayd); |
| 1983 | /* Skip splice error so the consumer does not fail */ |
| 1984 | goto end; |
| 1985 | } |
| 1986 | |
| 1987 | splice_error: |
| 1988 | /* send the appropriate error description to sessiond */ |
| 1989 | switch (ret) { |
| 1990 | case EINVAL: |
| 1991 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL); |
| 1992 | break; |
| 1993 | case ENOMEM: |
| 1994 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM); |
| 1995 | break; |
| 1996 | case ESPIPE: |
| 1997 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE); |
| 1998 | break; |
| 1999 | } |
| 2000 | |
| 2001 | end: |
| 2002 | if (relayd && stream->metadata_flag) { |
| 2003 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 2004 | } |
| 2005 | |
| 2006 | rcu_read_unlock(); |
| 2007 | return written; |
| 2008 | } |
| 2009 | |
| 2010 | /* |
| 2011 | * Sample the snapshot positions for a specific fd |
| 2012 | * |
| 2013 | * Returns 0 on success, < 0 on error |
| 2014 | */ |
| 2015 | int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream *stream) |
| 2016 | { |
| 2017 | switch (the_consumer_data.type) { |
| 2018 | case LTTNG_CONSUMER_KERNEL: |
| 2019 | return lttng_kconsumer_sample_snapshot_positions(stream); |
| 2020 | case LTTNG_CONSUMER32_UST: |
| 2021 | case LTTNG_CONSUMER64_UST: |
| 2022 | return lttng_ustconsumer_sample_snapshot_positions(stream); |
| 2023 | default: |
| 2024 | ERR("Unknown consumer_data type"); |
| 2025 | abort(); |
| 2026 | return -ENOSYS; |
| 2027 | } |
| 2028 | } |
| 2029 | /* |
| 2030 | * Take a snapshot for a specific fd |
| 2031 | * |
| 2032 | * Returns 0 on success, < 0 on error |
| 2033 | */ |
| 2034 | int lttng_consumer_take_snapshot(struct lttng_consumer_stream *stream) |
| 2035 | { |
| 2036 | switch (the_consumer_data.type) { |
| 2037 | case LTTNG_CONSUMER_KERNEL: |
| 2038 | return lttng_kconsumer_take_snapshot(stream); |
| 2039 | case LTTNG_CONSUMER32_UST: |
| 2040 | case LTTNG_CONSUMER64_UST: |
| 2041 | return lttng_ustconsumer_take_snapshot(stream); |
| 2042 | default: |
| 2043 | ERR("Unknown consumer_data type"); |
| 2044 | abort(); |
| 2045 | return -ENOSYS; |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | /* |
| 2050 | * Get the produced position |
| 2051 | * |
| 2052 | * Returns 0 on success, < 0 on error |
| 2053 | */ |
| 2054 | int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream *stream, |
| 2055 | unsigned long *pos) |
| 2056 | { |
| 2057 | switch (the_consumer_data.type) { |
| 2058 | case LTTNG_CONSUMER_KERNEL: |
| 2059 | return lttng_kconsumer_get_produced_snapshot(stream, pos); |
| 2060 | case LTTNG_CONSUMER32_UST: |
| 2061 | case LTTNG_CONSUMER64_UST: |
| 2062 | return lttng_ustconsumer_get_produced_snapshot(stream, pos); |
| 2063 | default: |
| 2064 | ERR("Unknown consumer_data type"); |
| 2065 | abort(); |
| 2066 | return -ENOSYS; |
| 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | /* |
| 2071 | * Get the consumed position (free-running counter position in bytes). |
| 2072 | * |
| 2073 | * Returns 0 on success, < 0 on error |
| 2074 | */ |
| 2075 | int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream *stream, |
| 2076 | unsigned long *pos) |
| 2077 | { |
| 2078 | switch (the_consumer_data.type) { |
| 2079 | case LTTNG_CONSUMER_KERNEL: |
| 2080 | return lttng_kconsumer_get_consumed_snapshot(stream, pos); |
| 2081 | case LTTNG_CONSUMER32_UST: |
| 2082 | case LTTNG_CONSUMER64_UST: |
| 2083 | return lttng_ustconsumer_get_consumed_snapshot(stream, pos); |
| 2084 | default: |
| 2085 | ERR("Unknown consumer_data type"); |
| 2086 | abort(); |
| 2087 | return -ENOSYS; |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
| 2092 | int sock, struct pollfd *consumer_sockpoll) |
| 2093 | { |
| 2094 | switch (the_consumer_data.type) { |
| 2095 | case LTTNG_CONSUMER_KERNEL: |
| 2096 | return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll); |
| 2097 | case LTTNG_CONSUMER32_UST: |
| 2098 | case LTTNG_CONSUMER64_UST: |
| 2099 | return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll); |
| 2100 | default: |
| 2101 | ERR("Unknown consumer_data type"); |
| 2102 | abort(); |
| 2103 | return -ENOSYS; |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | static |
| 2108 | void lttng_consumer_close_all_metadata(void) |
| 2109 | { |
| 2110 | switch (the_consumer_data.type) { |
| 2111 | case LTTNG_CONSUMER_KERNEL: |
| 2112 | /* |
| 2113 | * The Kernel consumer has a different metadata scheme so we don't |
| 2114 | * close anything because the stream will be closed by the session |
| 2115 | * daemon. |
| 2116 | */ |
| 2117 | break; |
| 2118 | case LTTNG_CONSUMER32_UST: |
| 2119 | case LTTNG_CONSUMER64_UST: |
| 2120 | /* |
| 2121 | * Close all metadata streams. The metadata hash table is passed and |
| 2122 | * this call iterates over it by closing all wakeup fd. This is safe |
| 2123 | * because at this point we are sure that the metadata producer is |
| 2124 | * either dead or blocked. |
| 2125 | */ |
| 2126 | lttng_ustconsumer_close_all_metadata(metadata_ht); |
| 2127 | break; |
| 2128 | default: |
| 2129 | ERR("Unknown consumer_data type"); |
| 2130 | abort(); |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | /* |
| 2135 | * Clean up a metadata stream and free its memory. |
| 2136 | */ |
| 2137 | void consumer_del_metadata_stream(struct lttng_consumer_stream *stream, |
| 2138 | struct lttng_ht *ht) |
| 2139 | { |
| 2140 | struct lttng_consumer_channel *channel = NULL; |
| 2141 | bool free_channel = false; |
| 2142 | |
| 2143 | LTTNG_ASSERT(stream); |
| 2144 | /* |
| 2145 | * This call should NEVER receive regular stream. It must always be |
| 2146 | * metadata stream and this is crucial for data structure synchronization. |
| 2147 | */ |
| 2148 | LTTNG_ASSERT(stream->metadata_flag); |
| 2149 | |
| 2150 | DBG3("Consumer delete metadata stream %d", stream->wait_fd); |
| 2151 | |
| 2152 | pthread_mutex_lock(&the_consumer_data.lock); |
| 2153 | /* |
| 2154 | * Note that this assumes that a stream's channel is never changed and |
| 2155 | * that the stream's lock doesn't need to be taken to sample its |
| 2156 | * channel. |
| 2157 | */ |
| 2158 | channel = stream->chan; |
| 2159 | pthread_mutex_lock(&channel->lock); |
| 2160 | pthread_mutex_lock(&stream->lock); |
| 2161 | if (channel->metadata_cache) { |
| 2162 | /* Only applicable to userspace consumers. */ |
| 2163 | pthread_mutex_lock(&channel->metadata_cache->lock); |
| 2164 | } |
| 2165 | |
| 2166 | /* Remove any reference to that stream. */ |
| 2167 | consumer_stream_delete(stream, ht); |
| 2168 | |
| 2169 | /* Close down everything including the relayd if one. */ |
| 2170 | consumer_stream_close_output(stream); |
| 2171 | /* Destroy tracer buffers of the stream. */ |
| 2172 | consumer_stream_destroy_buffers(stream); |
| 2173 | |
| 2174 | /* Atomically decrement channel refcount since other threads can use it. */ |
| 2175 | if (!uatomic_sub_return(&channel->refcount, 1) |
| 2176 | && !uatomic_read(&channel->nb_init_stream_left)) { |
| 2177 | /* Go for channel deletion! */ |
| 2178 | free_channel = true; |
| 2179 | } |
| 2180 | stream->chan = NULL; |
| 2181 | |
| 2182 | /* |
| 2183 | * Nullify the stream reference so it is not used after deletion. The |
| 2184 | * channel lock MUST be acquired before being able to check for a NULL |
| 2185 | * pointer value. |
| 2186 | */ |
| 2187 | channel->metadata_stream = NULL; |
| 2188 | |
| 2189 | if (channel->metadata_cache) { |
| 2190 | pthread_mutex_unlock(&channel->metadata_cache->lock); |
| 2191 | } |
| 2192 | pthread_mutex_unlock(&stream->lock); |
| 2193 | pthread_mutex_unlock(&channel->lock); |
| 2194 | pthread_mutex_unlock(&the_consumer_data.lock); |
| 2195 | |
| 2196 | if (free_channel) { |
| 2197 | consumer_del_channel(channel); |
| 2198 | } |
| 2199 | |
| 2200 | lttng_trace_chunk_put(stream->trace_chunk); |
| 2201 | stream->trace_chunk = NULL; |
| 2202 | consumer_stream_free(stream); |
| 2203 | } |
| 2204 | |
| 2205 | /* |
| 2206 | * Action done with the metadata stream when adding it to the consumer internal |
| 2207 | * data structures to handle it. |
| 2208 | */ |
| 2209 | void consumer_add_metadata_stream(struct lttng_consumer_stream *stream) |
| 2210 | { |
| 2211 | struct lttng_ht *ht = metadata_ht; |
| 2212 | struct lttng_ht_iter iter; |
| 2213 | struct lttng_ht_node_u64 *node; |
| 2214 | |
| 2215 | LTTNG_ASSERT(stream); |
| 2216 | LTTNG_ASSERT(ht); |
| 2217 | |
| 2218 | DBG3("Adding metadata stream %" PRIu64 " to hash table", stream->key); |
| 2219 | |
| 2220 | pthread_mutex_lock(&the_consumer_data.lock); |
| 2221 | pthread_mutex_lock(&stream->chan->lock); |
| 2222 | pthread_mutex_lock(&stream->chan->timer_lock); |
| 2223 | pthread_mutex_lock(&stream->lock); |
| 2224 | |
| 2225 | /* |
| 2226 | * From here, refcounts are updated so be _careful_ when returning an error |
| 2227 | * after this point. |
| 2228 | */ |
| 2229 | |
| 2230 | rcu_read_lock(); |
| 2231 | |
| 2232 | /* |
| 2233 | * Lookup the stream just to make sure it does not exist in our internal |
| 2234 | * state. This should NEVER happen. |
| 2235 | */ |
| 2236 | lttng_ht_lookup(ht, &stream->key, &iter); |
| 2237 | node = lttng_ht_iter_get_node_u64(&iter); |
| 2238 | LTTNG_ASSERT(!node); |
| 2239 | |
| 2240 | /* |
| 2241 | * When nb_init_stream_left reaches 0, we don't need to trigger any action |
| 2242 | * in terms of destroying the associated channel, because the action that |
| 2243 | * causes the count to become 0 also causes a stream to be added. The |
| 2244 | * channel deletion will thus be triggered by the following removal of this |
| 2245 | * stream. |
| 2246 | */ |
| 2247 | if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) { |
| 2248 | /* Increment refcount before decrementing nb_init_stream_left */ |
| 2249 | cmm_smp_wmb(); |
| 2250 | uatomic_dec(&stream->chan->nb_init_stream_left); |
| 2251 | } |
| 2252 | |
| 2253 | lttng_ht_add_unique_u64(ht, &stream->node); |
| 2254 | |
| 2255 | lttng_ht_add_u64(the_consumer_data.stream_per_chan_id_ht, |
| 2256 | &stream->node_channel_id); |
| 2257 | |
| 2258 | /* |
| 2259 | * Add stream to the stream_list_ht of the consumer data. No need to steal |
| 2260 | * the key since the HT does not use it and we allow to add redundant keys |
| 2261 | * into this table. |
| 2262 | */ |
| 2263 | lttng_ht_add_u64(the_consumer_data.stream_list_ht, |
| 2264 | &stream->node_session_id); |
| 2265 | |
| 2266 | rcu_read_unlock(); |
| 2267 | |
| 2268 | pthread_mutex_unlock(&stream->lock); |
| 2269 | pthread_mutex_unlock(&stream->chan->lock); |
| 2270 | pthread_mutex_unlock(&stream->chan->timer_lock); |
| 2271 | pthread_mutex_unlock(&the_consumer_data.lock); |
| 2272 | } |
| 2273 | |
| 2274 | /* |
| 2275 | * Delete data stream that are flagged for deletion (endpoint_status). |
| 2276 | */ |
| 2277 | static void validate_endpoint_status_data_stream(void) |
| 2278 | { |
| 2279 | struct lttng_ht_iter iter; |
| 2280 | struct lttng_consumer_stream *stream; |
| 2281 | |
| 2282 | DBG("Consumer delete flagged data stream"); |
| 2283 | |
| 2284 | rcu_read_lock(); |
| 2285 | cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) { |
| 2286 | /* Validate delete flag of the stream */ |
| 2287 | if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) { |
| 2288 | continue; |
| 2289 | } |
| 2290 | /* Delete it right now */ |
| 2291 | consumer_del_stream(stream, data_ht); |
| 2292 | } |
| 2293 | rcu_read_unlock(); |
| 2294 | } |
| 2295 | |
| 2296 | /* |
| 2297 | * Delete metadata stream that are flagged for deletion (endpoint_status). |
| 2298 | */ |
| 2299 | static void validate_endpoint_status_metadata_stream( |
| 2300 | struct lttng_poll_event *pollset) |
| 2301 | { |
| 2302 | struct lttng_ht_iter iter; |
| 2303 | struct lttng_consumer_stream *stream; |
| 2304 | |
| 2305 | DBG("Consumer delete flagged metadata stream"); |
| 2306 | |
| 2307 | LTTNG_ASSERT(pollset); |
| 2308 | |
| 2309 | rcu_read_lock(); |
| 2310 | cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) { |
| 2311 | /* Validate delete flag of the stream */ |
| 2312 | if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) { |
| 2313 | continue; |
| 2314 | } |
| 2315 | /* |
| 2316 | * Remove from pollset so the metadata thread can continue without |
| 2317 | * blocking on a deleted stream. |
| 2318 | */ |
| 2319 | lttng_poll_del(pollset, stream->wait_fd); |
| 2320 | |
| 2321 | /* Delete it right now */ |
| 2322 | consumer_del_metadata_stream(stream, metadata_ht); |
| 2323 | } |
| 2324 | rcu_read_unlock(); |
| 2325 | } |
| 2326 | |
| 2327 | /* |
| 2328 | * Thread polls on metadata file descriptor and write them on disk or on the |
| 2329 | * network. |
| 2330 | */ |
| 2331 | void *consumer_thread_metadata_poll(void *data) |
| 2332 | { |
| 2333 | int ret, i, pollfd, err = -1; |
| 2334 | uint32_t revents, nb_fd; |
| 2335 | struct lttng_consumer_stream *stream = NULL; |
| 2336 | struct lttng_ht_iter iter; |
| 2337 | struct lttng_ht_node_u64 *node; |
| 2338 | struct lttng_poll_event events; |
| 2339 | struct lttng_consumer_local_data *ctx = (lttng_consumer_local_data *) data; |
| 2340 | ssize_t len; |
| 2341 | |
| 2342 | rcu_register_thread(); |
| 2343 | |
| 2344 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_METADATA); |
| 2345 | |
| 2346 | if (testpoint(consumerd_thread_metadata)) { |
| 2347 | goto error_testpoint; |
| 2348 | } |
| 2349 | |
| 2350 | health_code_update(); |
| 2351 | |
| 2352 | DBG("Thread metadata poll started"); |
| 2353 | |
| 2354 | /* Size is set to 1 for the consumer_metadata pipe */ |
| 2355 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); |
| 2356 | if (ret < 0) { |
| 2357 | ERR("Poll set creation failed"); |
| 2358 | goto end_poll; |
| 2359 | } |
| 2360 | |
| 2361 | ret = lttng_poll_add(&events, |
| 2362 | lttng_pipe_get_readfd(ctx->consumer_metadata_pipe), LPOLLIN); |
| 2363 | if (ret < 0) { |
| 2364 | goto end; |
| 2365 | } |
| 2366 | |
| 2367 | /* Main loop */ |
| 2368 | DBG("Metadata main loop started"); |
| 2369 | |
| 2370 | while (1) { |
| 2371 | restart: |
| 2372 | health_code_update(); |
| 2373 | health_poll_entry(); |
| 2374 | DBG("Metadata poll wait"); |
| 2375 | ret = lttng_poll_wait(&events, -1); |
| 2376 | DBG("Metadata poll return from wait with %d fd(s)", |
| 2377 | LTTNG_POLL_GETNB(&events)); |
| 2378 | health_poll_exit(); |
| 2379 | DBG("Metadata event caught in thread"); |
| 2380 | if (ret < 0) { |
| 2381 | if (errno == EINTR) { |
| 2382 | ERR("Poll EINTR caught"); |
| 2383 | goto restart; |
| 2384 | } |
| 2385 | if (LTTNG_POLL_GETNB(&events) == 0) { |
| 2386 | err = 0; /* All is OK */ |
| 2387 | } |
| 2388 | goto end; |
| 2389 | } |
| 2390 | |
| 2391 | nb_fd = ret; |
| 2392 | |
| 2393 | /* From here, the event is a metadata wait fd */ |
| 2394 | for (i = 0; i < nb_fd; i++) { |
| 2395 | health_code_update(); |
| 2396 | |
| 2397 | revents = LTTNG_POLL_GETEV(&events, i); |
| 2398 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 2399 | |
| 2400 | if (pollfd == lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)) { |
| 2401 | if (revents & LPOLLIN) { |
| 2402 | ssize_t pipe_len; |
| 2403 | |
| 2404 | pipe_len = lttng_pipe_read(ctx->consumer_metadata_pipe, |
| 2405 | &stream, sizeof(stream)); |
| 2406 | if (pipe_len < sizeof(stream)) { |
| 2407 | if (pipe_len < 0) { |
| 2408 | PERROR("read metadata stream"); |
| 2409 | } |
| 2410 | /* |
| 2411 | * Remove the pipe from the poll set and continue the loop |
| 2412 | * since their might be data to consume. |
| 2413 | */ |
| 2414 | lttng_poll_del(&events, |
| 2415 | lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)); |
| 2416 | lttng_pipe_read_close(ctx->consumer_metadata_pipe); |
| 2417 | continue; |
| 2418 | } |
| 2419 | |
| 2420 | /* A NULL stream means that the state has changed. */ |
| 2421 | if (stream == NULL) { |
| 2422 | /* Check for deleted streams. */ |
| 2423 | validate_endpoint_status_metadata_stream(&events); |
| 2424 | goto restart; |
| 2425 | } |
| 2426 | |
| 2427 | DBG("Adding metadata stream %d to poll set", |
| 2428 | stream->wait_fd); |
| 2429 | |
| 2430 | /* Add metadata stream to the global poll events list */ |
| 2431 | lttng_poll_add(&events, stream->wait_fd, |
| 2432 | LPOLLIN | LPOLLPRI | LPOLLHUP); |
| 2433 | } else if (revents & (LPOLLERR | LPOLLHUP)) { |
| 2434 | DBG("Metadata thread pipe hung up"); |
| 2435 | /* |
| 2436 | * Remove the pipe from the poll set and continue the loop |
| 2437 | * since their might be data to consume. |
| 2438 | */ |
| 2439 | lttng_poll_del(&events, |
| 2440 | lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)); |
| 2441 | lttng_pipe_read_close(ctx->consumer_metadata_pipe); |
| 2442 | continue; |
| 2443 | } else { |
| 2444 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 2445 | goto end; |
| 2446 | } |
| 2447 | |
| 2448 | /* Handle other stream */ |
| 2449 | continue; |
| 2450 | } |
| 2451 | |
| 2452 | rcu_read_lock(); |
| 2453 | { |
| 2454 | uint64_t tmp_id = (uint64_t) pollfd; |
| 2455 | |
| 2456 | lttng_ht_lookup(metadata_ht, &tmp_id, &iter); |
| 2457 | } |
| 2458 | node = lttng_ht_iter_get_node_u64(&iter); |
| 2459 | LTTNG_ASSERT(node); |
| 2460 | |
| 2461 | stream = caa_container_of(node, struct lttng_consumer_stream, |
| 2462 | node); |
| 2463 | |
| 2464 | if (revents & (LPOLLIN | LPOLLPRI)) { |
| 2465 | /* Get the data out of the metadata file descriptor */ |
| 2466 | DBG("Metadata available on fd %d", pollfd); |
| 2467 | LTTNG_ASSERT(stream->wait_fd == pollfd); |
| 2468 | |
| 2469 | do { |
| 2470 | health_code_update(); |
| 2471 | |
| 2472 | len = ctx->on_buffer_ready(stream, ctx, false); |
| 2473 | /* |
| 2474 | * We don't check the return value here since if we get |
| 2475 | * a negative len, it means an error occurred thus we |
| 2476 | * simply remove it from the poll set and free the |
| 2477 | * stream. |
| 2478 | */ |
| 2479 | } while (len > 0); |
| 2480 | |
| 2481 | /* It's ok to have an unavailable sub-buffer */ |
| 2482 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { |
| 2483 | /* Clean up stream from consumer and free it. */ |
| 2484 | lttng_poll_del(&events, stream->wait_fd); |
| 2485 | consumer_del_metadata_stream(stream, metadata_ht); |
| 2486 | } |
| 2487 | } else if (revents & (LPOLLERR | LPOLLHUP)) { |
| 2488 | DBG("Metadata fd %d is hup|err.", pollfd); |
| 2489 | if (!stream->hangup_flush_done && |
| 2490 | (the_consumer_data.type == LTTNG_CONSUMER32_UST || |
| 2491 | the_consumer_data.type == |
| 2492 | LTTNG_CONSUMER64_UST)) { |
| 2493 | DBG("Attempting to flush and consume the UST buffers"); |
| 2494 | lttng_ustconsumer_on_stream_hangup(stream); |
| 2495 | |
| 2496 | /* We just flushed the stream now read it. */ |
| 2497 | do { |
| 2498 | health_code_update(); |
| 2499 | |
| 2500 | len = ctx->on_buffer_ready(stream, ctx, false); |
| 2501 | /* |
| 2502 | * We don't check the return value here since if we get |
| 2503 | * a negative len, it means an error occurred thus we |
| 2504 | * simply remove it from the poll set and free the |
| 2505 | * stream. |
| 2506 | */ |
| 2507 | } while (len > 0); |
| 2508 | } |
| 2509 | |
| 2510 | lttng_poll_del(&events, stream->wait_fd); |
| 2511 | /* |
| 2512 | * This call update the channel states, closes file descriptors |
| 2513 | * and securely free the stream. |
| 2514 | */ |
| 2515 | consumer_del_metadata_stream(stream, metadata_ht); |
| 2516 | } else { |
| 2517 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 2518 | rcu_read_unlock(); |
| 2519 | goto end; |
| 2520 | } |
| 2521 | /* Release RCU lock for the stream looked up */ |
| 2522 | rcu_read_unlock(); |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | /* All is OK */ |
| 2527 | err = 0; |
| 2528 | end: |
| 2529 | DBG("Metadata poll thread exiting"); |
| 2530 | |
| 2531 | lttng_poll_clean(&events); |
| 2532 | end_poll: |
| 2533 | error_testpoint: |
| 2534 | if (err) { |
| 2535 | health_error(); |
| 2536 | ERR("Health error occurred in %s", __func__); |
| 2537 | } |
| 2538 | health_unregister(health_consumerd); |
| 2539 | rcu_unregister_thread(); |
| 2540 | return NULL; |
| 2541 | } |
| 2542 | |
| 2543 | /* |
| 2544 | * This thread polls the fds in the set to consume the data and write |
| 2545 | * it to tracefile if necessary. |
| 2546 | */ |
| 2547 | void *consumer_thread_data_poll(void *data) |
| 2548 | { |
| 2549 | int num_rdy, num_hup, high_prio, ret, i, err = -1; |
| 2550 | struct pollfd *pollfd = NULL; |
| 2551 | /* local view of the streams */ |
| 2552 | struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL; |
| 2553 | /* local view of consumer_data.fds_count */ |
| 2554 | int nb_fd = 0; |
| 2555 | /* 2 for the consumer_data_pipe and wake up pipe */ |
| 2556 | const int nb_pipes_fd = 2; |
| 2557 | /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */ |
| 2558 | int nb_inactive_fd = 0; |
| 2559 | struct lttng_consumer_local_data *ctx = (lttng_consumer_local_data *) data; |
| 2560 | ssize_t len; |
| 2561 | |
| 2562 | rcu_register_thread(); |
| 2563 | |
| 2564 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_DATA); |
| 2565 | |
| 2566 | if (testpoint(consumerd_thread_data)) { |
| 2567 | goto error_testpoint; |
| 2568 | } |
| 2569 | |
| 2570 | health_code_update(); |
| 2571 | |
| 2572 | local_stream = zmalloc<lttng_consumer_stream *>(); |
| 2573 | if (local_stream == NULL) { |
| 2574 | PERROR("local_stream malloc"); |
| 2575 | goto end; |
| 2576 | } |
| 2577 | |
| 2578 | while (1) { |
| 2579 | health_code_update(); |
| 2580 | |
| 2581 | high_prio = 0; |
| 2582 | num_hup = 0; |
| 2583 | |
| 2584 | /* |
| 2585 | * the fds set has been updated, we need to update our |
| 2586 | * local array as well |
| 2587 | */ |
| 2588 | pthread_mutex_lock(&the_consumer_data.lock); |
| 2589 | if (the_consumer_data.need_update) { |
| 2590 | free(pollfd); |
| 2591 | pollfd = NULL; |
| 2592 | |
| 2593 | free(local_stream); |
| 2594 | local_stream = NULL; |
| 2595 | |
| 2596 | /* Allocate for all fds */ |
| 2597 | pollfd = calloc<struct pollfd>(the_consumer_data.stream_count + nb_pipes_fd); |
| 2598 | if (pollfd == NULL) { |
| 2599 | PERROR("pollfd malloc"); |
| 2600 | pthread_mutex_unlock(&the_consumer_data.lock); |
| 2601 | goto end; |
| 2602 | } |
| 2603 | |
| 2604 | local_stream = calloc<lttng_consumer_stream *>(the_consumer_data.stream_count + nb_pipes_fd); |
| 2605 | if (local_stream == NULL) { |
| 2606 | PERROR("local_stream malloc"); |
| 2607 | pthread_mutex_unlock(&the_consumer_data.lock); |
| 2608 | goto end; |
| 2609 | } |
| 2610 | ret = update_poll_array(ctx, &pollfd, local_stream, |
| 2611 | data_ht, &nb_inactive_fd); |
| 2612 | if (ret < 0) { |
| 2613 | ERR("Error in allocating pollfd or local_outfds"); |
| 2614 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
| 2615 | pthread_mutex_unlock(&the_consumer_data.lock); |
| 2616 | goto end; |
| 2617 | } |
| 2618 | nb_fd = ret; |
| 2619 | the_consumer_data.need_update = 0; |
| 2620 | } |
| 2621 | pthread_mutex_unlock(&the_consumer_data.lock); |
| 2622 | |
| 2623 | /* No FDs and consumer_quit, consumer_cleanup the thread */ |
| 2624 | if (nb_fd == 0 && nb_inactive_fd == 0 && |
| 2625 | CMM_LOAD_SHARED(consumer_quit) == 1) { |
| 2626 | err = 0; /* All is OK */ |
| 2627 | goto end; |
| 2628 | } |
| 2629 | /* poll on the array of fds */ |
| 2630 | restart: |
| 2631 | DBG("polling on %d fd", nb_fd + nb_pipes_fd); |
| 2632 | if (testpoint(consumerd_thread_data_poll)) { |
| 2633 | goto end; |
| 2634 | } |
| 2635 | health_poll_entry(); |
| 2636 | num_rdy = poll(pollfd, nb_fd + nb_pipes_fd, -1); |
| 2637 | health_poll_exit(); |
| 2638 | DBG("poll num_rdy : %d", num_rdy); |
| 2639 | if (num_rdy == -1) { |
| 2640 | /* |
| 2641 | * Restart interrupted system call. |
| 2642 | */ |
| 2643 | if (errno == EINTR) { |
| 2644 | goto restart; |
| 2645 | } |
| 2646 | PERROR("Poll error"); |
| 2647 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
| 2648 | goto end; |
| 2649 | } else if (num_rdy == 0) { |
| 2650 | DBG("Polling thread timed out"); |
| 2651 | goto end; |
| 2652 | } |
| 2653 | |
| 2654 | if (caa_unlikely(data_consumption_paused)) { |
| 2655 | DBG("Data consumption paused, sleeping..."); |
| 2656 | sleep(1); |
| 2657 | goto restart; |
| 2658 | } |
| 2659 | |
| 2660 | /* |
| 2661 | * If the consumer_data_pipe triggered poll go directly to the |
| 2662 | * beginning of the loop to update the array. We want to prioritize |
| 2663 | * array update over low-priority reads. |
| 2664 | */ |
| 2665 | if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) { |
| 2666 | ssize_t pipe_readlen; |
| 2667 | |
| 2668 | DBG("consumer_data_pipe wake up"); |
| 2669 | pipe_readlen = lttng_pipe_read(ctx->consumer_data_pipe, |
| 2670 | &new_stream, sizeof(new_stream)); |
| 2671 | if (pipe_readlen < sizeof(new_stream)) { |
| 2672 | PERROR("Consumer data pipe"); |
| 2673 | /* Continue so we can at least handle the current stream(s). */ |
| 2674 | continue; |
| 2675 | } |
| 2676 | |
| 2677 | /* |
| 2678 | * If the stream is NULL, just ignore it. It's also possible that |
| 2679 | * the sessiond poll thread changed the consumer_quit state and is |
| 2680 | * waking us up to test it. |
| 2681 | */ |
| 2682 | if (new_stream == NULL) { |
| 2683 | validate_endpoint_status_data_stream(); |
| 2684 | continue; |
| 2685 | } |
| 2686 | |
| 2687 | /* Continue to update the local streams and handle prio ones */ |
| 2688 | continue; |
| 2689 | } |
| 2690 | |
| 2691 | /* Handle wakeup pipe. */ |
| 2692 | if (pollfd[nb_fd + 1].revents & (POLLIN | POLLPRI)) { |
| 2693 | char dummy; |
| 2694 | ssize_t pipe_readlen; |
| 2695 | |
| 2696 | pipe_readlen = lttng_pipe_read(ctx->consumer_wakeup_pipe, &dummy, |
| 2697 | sizeof(dummy)); |
| 2698 | if (pipe_readlen < 0) { |
| 2699 | PERROR("Consumer data wakeup pipe"); |
| 2700 | } |
| 2701 | /* We've been awakened to handle stream(s). */ |
| 2702 | ctx->has_wakeup = 0; |
| 2703 | } |
| 2704 | |
| 2705 | /* Take care of high priority channels first. */ |
| 2706 | for (i = 0; i < nb_fd; i++) { |
| 2707 | health_code_update(); |
| 2708 | |
| 2709 | if (local_stream[i] == NULL) { |
| 2710 | continue; |
| 2711 | } |
| 2712 | if (pollfd[i].revents & POLLPRI) { |
| 2713 | DBG("Urgent read on fd %d", pollfd[i].fd); |
| 2714 | high_prio = 1; |
| 2715 | len = ctx->on_buffer_ready(local_stream[i], ctx, false); |
| 2716 | /* it's ok to have an unavailable sub-buffer */ |
| 2717 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { |
| 2718 | /* Clean the stream and free it. */ |
| 2719 | consumer_del_stream(local_stream[i], data_ht); |
| 2720 | local_stream[i] = NULL; |
| 2721 | } else if (len > 0) { |
| 2722 | local_stream[i]->has_data_left_to_be_read_before_teardown = 1; |
| 2723 | } |
| 2724 | } |
| 2725 | } |
| 2726 | |
| 2727 | /* |
| 2728 | * If we read high prio channel in this loop, try again |
| 2729 | * for more high prio data. |
| 2730 | */ |
| 2731 | if (high_prio) { |
| 2732 | continue; |
| 2733 | } |
| 2734 | |
| 2735 | /* Take care of low priority channels. */ |
| 2736 | for (i = 0; i < nb_fd; i++) { |
| 2737 | health_code_update(); |
| 2738 | |
| 2739 | if (local_stream[i] == NULL) { |
| 2740 | continue; |
| 2741 | } |
| 2742 | if ((pollfd[i].revents & POLLIN) || |
| 2743 | local_stream[i]->hangup_flush_done || |
| 2744 | local_stream[i]->has_data) { |
| 2745 | DBG("Normal read on fd %d", pollfd[i].fd); |
| 2746 | len = ctx->on_buffer_ready(local_stream[i], ctx, false); |
| 2747 | /* it's ok to have an unavailable sub-buffer */ |
| 2748 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { |
| 2749 | /* Clean the stream and free it. */ |
| 2750 | consumer_del_stream(local_stream[i], data_ht); |
| 2751 | local_stream[i] = NULL; |
| 2752 | } else if (len > 0) { |
| 2753 | local_stream[i]->has_data_left_to_be_read_before_teardown = 1; |
| 2754 | } |
| 2755 | } |
| 2756 | } |
| 2757 | |
| 2758 | /* Handle hangup and errors */ |
| 2759 | for (i = 0; i < nb_fd; i++) { |
| 2760 | health_code_update(); |
| 2761 | |
| 2762 | if (local_stream[i] == NULL) { |
| 2763 | continue; |
| 2764 | } |
| 2765 | if (!local_stream[i]->hangup_flush_done |
| 2766 | && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL)) |
| 2767 | && (the_consumer_data.type == LTTNG_CONSUMER32_UST |
| 2768 | || the_consumer_data.type == LTTNG_CONSUMER64_UST)) { |
| 2769 | DBG("fd %d is hup|err|nval. Attempting flush and read.", |
| 2770 | pollfd[i].fd); |
| 2771 | lttng_ustconsumer_on_stream_hangup(local_stream[i]); |
| 2772 | /* Attempt read again, for the data we just flushed. */ |
| 2773 | local_stream[i]->has_data_left_to_be_read_before_teardown = 1; |
| 2774 | } |
| 2775 | /* |
| 2776 | * When a stream's pipe dies (hup/err/nval), an "inactive producer" flush is |
| 2777 | * performed. This type of flush ensures that a new packet is produced no |
| 2778 | * matter the consumed/produced positions are. |
| 2779 | * |
| 2780 | * This, in turn, causes the next pass to see that data available for the |
| 2781 | * stream. When we come back here, we can be assured that all available |
| 2782 | * data has been consumed and we can finally destroy the stream. |
| 2783 | * |
| 2784 | * If the poll flag is HUP/ERR/NVAL and we have |
| 2785 | * read no data in this pass, we can remove the |
| 2786 | * stream from its hash table. |
| 2787 | */ |
| 2788 | if ((pollfd[i].revents & POLLHUP)) { |
| 2789 | DBG("Polling fd %d tells it has hung up.", pollfd[i].fd); |
| 2790 | if (!local_stream[i]->has_data_left_to_be_read_before_teardown) { |
| 2791 | consumer_del_stream(local_stream[i], data_ht); |
| 2792 | local_stream[i] = NULL; |
| 2793 | num_hup++; |
| 2794 | } |
| 2795 | } else if (pollfd[i].revents & POLLERR) { |
| 2796 | ERR("Error returned in polling fd %d.", pollfd[i].fd); |
| 2797 | if (!local_stream[i]->has_data_left_to_be_read_before_teardown) { |
| 2798 | consumer_del_stream(local_stream[i], data_ht); |
| 2799 | local_stream[i] = NULL; |
| 2800 | num_hup++; |
| 2801 | } |
| 2802 | } else if (pollfd[i].revents & POLLNVAL) { |
| 2803 | ERR("Polling fd %d tells fd is not open.", pollfd[i].fd); |
| 2804 | if (!local_stream[i]->has_data_left_to_be_read_before_teardown) { |
| 2805 | consumer_del_stream(local_stream[i], data_ht); |
| 2806 | local_stream[i] = NULL; |
| 2807 | num_hup++; |
| 2808 | } |
| 2809 | } |
| 2810 | if (local_stream[i] != NULL) { |
| 2811 | local_stream[i]->has_data_left_to_be_read_before_teardown = 0; |
| 2812 | } |
| 2813 | } |
| 2814 | } |
| 2815 | /* All is OK */ |
| 2816 | err = 0; |
| 2817 | end: |
| 2818 | DBG("polling thread exiting"); |
| 2819 | free(pollfd); |
| 2820 | free(local_stream); |
| 2821 | |
| 2822 | /* |
| 2823 | * Close the write side of the pipe so epoll_wait() in |
| 2824 | * consumer_thread_metadata_poll can catch it. The thread is monitoring the |
| 2825 | * read side of the pipe. If we close them both, epoll_wait strangely does |
| 2826 | * not return and could create a endless wait period if the pipe is the |
| 2827 | * only tracked fd in the poll set. The thread will take care of closing |
| 2828 | * the read side. |
| 2829 | */ |
| 2830 | (void) lttng_pipe_write_close(ctx->consumer_metadata_pipe); |
| 2831 | |
| 2832 | error_testpoint: |
| 2833 | if (err) { |
| 2834 | health_error(); |
| 2835 | ERR("Health error occurred in %s", __func__); |
| 2836 | } |
| 2837 | health_unregister(health_consumerd); |
| 2838 | |
| 2839 | rcu_unregister_thread(); |
| 2840 | return NULL; |
| 2841 | } |
| 2842 | |
| 2843 | /* |
| 2844 | * Close wake-up end of each stream belonging to the channel. This will |
| 2845 | * allow the poll() on the stream read-side to detect when the |
| 2846 | * write-side (application) finally closes them. |
| 2847 | */ |
| 2848 | static |
| 2849 | void consumer_close_channel_streams(struct lttng_consumer_channel *channel) |
| 2850 | { |
| 2851 | struct lttng_ht *ht; |
| 2852 | struct lttng_consumer_stream *stream; |
| 2853 | struct lttng_ht_iter iter; |
| 2854 | |
| 2855 | ht = the_consumer_data.stream_per_chan_id_ht; |
| 2856 | |
| 2857 | rcu_read_lock(); |
| 2858 | cds_lfht_for_each_entry_duplicate(ht->ht, |
| 2859 | ht->hash_fct(&channel->key, lttng_ht_seed), |
| 2860 | ht->match_fct, &channel->key, |
| 2861 | &iter.iter, stream, node_channel_id.node) { |
| 2862 | /* |
| 2863 | * Protect against teardown with mutex. |
| 2864 | */ |
| 2865 | pthread_mutex_lock(&stream->lock); |
| 2866 | if (cds_lfht_is_node_deleted(&stream->node.node)) { |
| 2867 | goto next; |
| 2868 | } |
| 2869 | switch (the_consumer_data.type) { |
| 2870 | case LTTNG_CONSUMER_KERNEL: |
| 2871 | break; |
| 2872 | case LTTNG_CONSUMER32_UST: |
| 2873 | case LTTNG_CONSUMER64_UST: |
| 2874 | if (stream->metadata_flag) { |
| 2875 | /* Safe and protected by the stream lock. */ |
| 2876 | lttng_ustconsumer_close_metadata(stream->chan); |
| 2877 | } else { |
| 2878 | /* |
| 2879 | * Note: a mutex is taken internally within |
| 2880 | * liblttng-ust-ctl to protect timer wakeup_fd |
| 2881 | * use from concurrent close. |
| 2882 | */ |
| 2883 | lttng_ustconsumer_close_stream_wakeup(stream); |
| 2884 | } |
| 2885 | break; |
| 2886 | default: |
| 2887 | ERR("Unknown consumer_data type"); |
| 2888 | abort(); |
| 2889 | } |
| 2890 | next: |
| 2891 | pthread_mutex_unlock(&stream->lock); |
| 2892 | } |
| 2893 | rcu_read_unlock(); |
| 2894 | } |
| 2895 | |
| 2896 | static void destroy_channel_ht(struct lttng_ht *ht) |
| 2897 | { |
| 2898 | struct lttng_ht_iter iter; |
| 2899 | struct lttng_consumer_channel *channel; |
| 2900 | int ret; |
| 2901 | |
| 2902 | if (ht == NULL) { |
| 2903 | return; |
| 2904 | } |
| 2905 | |
| 2906 | rcu_read_lock(); |
| 2907 | cds_lfht_for_each_entry(ht->ht, &iter.iter, channel, wait_fd_node.node) { |
| 2908 | ret = lttng_ht_del(ht, &iter); |
| 2909 | LTTNG_ASSERT(ret != 0); |
| 2910 | } |
| 2911 | rcu_read_unlock(); |
| 2912 | |
| 2913 | lttng_ht_destroy(ht); |
| 2914 | } |
| 2915 | |
| 2916 | /* |
| 2917 | * This thread polls the channel fds to detect when they are being |
| 2918 | * closed. It closes all related streams if the channel is detected as |
| 2919 | * closed. It is currently only used as a shim layer for UST because the |
| 2920 | * consumerd needs to keep the per-stream wakeup end of pipes open for |
| 2921 | * periodical flush. |
| 2922 | */ |
| 2923 | void *consumer_thread_channel_poll(void *data) |
| 2924 | { |
| 2925 | int ret, i, pollfd, err = -1; |
| 2926 | uint32_t revents, nb_fd; |
| 2927 | struct lttng_consumer_channel *chan = NULL; |
| 2928 | struct lttng_ht_iter iter; |
| 2929 | struct lttng_ht_node_u64 *node; |
| 2930 | struct lttng_poll_event events; |
| 2931 | struct lttng_consumer_local_data *ctx = (lttng_consumer_local_data *) data; |
| 2932 | struct lttng_ht *channel_ht; |
| 2933 | |
| 2934 | rcu_register_thread(); |
| 2935 | |
| 2936 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_CHANNEL); |
| 2937 | |
| 2938 | if (testpoint(consumerd_thread_channel)) { |
| 2939 | goto error_testpoint; |
| 2940 | } |
| 2941 | |
| 2942 | health_code_update(); |
| 2943 | |
| 2944 | channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 2945 | if (!channel_ht) { |
| 2946 | /* ENOMEM at this point. Better to bail out. */ |
| 2947 | goto end_ht; |
| 2948 | } |
| 2949 | |
| 2950 | DBG("Thread channel poll started"); |
| 2951 | |
| 2952 | /* Size is set to 1 for the consumer_channel pipe */ |
| 2953 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); |
| 2954 | if (ret < 0) { |
| 2955 | ERR("Poll set creation failed"); |
| 2956 | goto end_poll; |
| 2957 | } |
| 2958 | |
| 2959 | ret = lttng_poll_add(&events, ctx->consumer_channel_pipe[0], LPOLLIN); |
| 2960 | if (ret < 0) { |
| 2961 | goto end; |
| 2962 | } |
| 2963 | |
| 2964 | /* Main loop */ |
| 2965 | DBG("Channel main loop started"); |
| 2966 | |
| 2967 | while (1) { |
| 2968 | restart: |
| 2969 | health_code_update(); |
| 2970 | DBG("Channel poll wait"); |
| 2971 | health_poll_entry(); |
| 2972 | ret = lttng_poll_wait(&events, -1); |
| 2973 | DBG("Channel poll return from wait with %d fd(s)", |
| 2974 | LTTNG_POLL_GETNB(&events)); |
| 2975 | health_poll_exit(); |
| 2976 | DBG("Channel event caught in thread"); |
| 2977 | if (ret < 0) { |
| 2978 | if (errno == EINTR) { |
| 2979 | ERR("Poll EINTR caught"); |
| 2980 | goto restart; |
| 2981 | } |
| 2982 | if (LTTNG_POLL_GETNB(&events) == 0) { |
| 2983 | err = 0; /* All is OK */ |
| 2984 | } |
| 2985 | goto end; |
| 2986 | } |
| 2987 | |
| 2988 | nb_fd = ret; |
| 2989 | |
| 2990 | /* From here, the event is a channel wait fd */ |
| 2991 | for (i = 0; i < nb_fd; i++) { |
| 2992 | health_code_update(); |
| 2993 | |
| 2994 | revents = LTTNG_POLL_GETEV(&events, i); |
| 2995 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 2996 | |
| 2997 | if (pollfd == ctx->consumer_channel_pipe[0]) { |
| 2998 | if (revents & LPOLLIN) { |
| 2999 | enum consumer_channel_action action; |
| 3000 | uint64_t key; |
| 3001 | |
| 3002 | ret = read_channel_pipe(ctx, &chan, &key, &action); |
| 3003 | if (ret <= 0) { |
| 3004 | if (ret < 0) { |
| 3005 | ERR("Error reading channel pipe"); |
| 3006 | } |
| 3007 | lttng_poll_del(&events, ctx->consumer_channel_pipe[0]); |
| 3008 | continue; |
| 3009 | } |
| 3010 | |
| 3011 | switch (action) { |
| 3012 | case CONSUMER_CHANNEL_ADD: |
| 3013 | DBG("Adding channel %d to poll set", |
| 3014 | chan->wait_fd); |
| 3015 | |
| 3016 | lttng_ht_node_init_u64(&chan->wait_fd_node, |
| 3017 | chan->wait_fd); |
| 3018 | rcu_read_lock(); |
| 3019 | lttng_ht_add_unique_u64(channel_ht, |
| 3020 | &chan->wait_fd_node); |
| 3021 | rcu_read_unlock(); |
| 3022 | /* Add channel to the global poll events list */ |
| 3023 | lttng_poll_add(&events, chan->wait_fd, |
| 3024 | LPOLLERR | LPOLLHUP); |
| 3025 | break; |
| 3026 | case CONSUMER_CHANNEL_DEL: |
| 3027 | { |
| 3028 | /* |
| 3029 | * This command should never be called if the channel |
| 3030 | * has streams monitored by either the data or metadata |
| 3031 | * thread. The consumer only notify this thread with a |
| 3032 | * channel del. command if it receives a destroy |
| 3033 | * channel command from the session daemon that send it |
| 3034 | * if a command prior to the GET_CHANNEL failed. |
| 3035 | */ |
| 3036 | |
| 3037 | rcu_read_lock(); |
| 3038 | chan = consumer_find_channel(key); |
| 3039 | if (!chan) { |
| 3040 | rcu_read_unlock(); |
| 3041 | ERR("UST consumer get channel key %" PRIu64 " not found for del channel", key); |
| 3042 | break; |
| 3043 | } |
| 3044 | lttng_poll_del(&events, chan->wait_fd); |
| 3045 | iter.iter.node = &chan->wait_fd_node.node; |
| 3046 | ret = lttng_ht_del(channel_ht, &iter); |
| 3047 | LTTNG_ASSERT(ret == 0); |
| 3048 | |
| 3049 | switch (the_consumer_data.type) { |
| 3050 | case LTTNG_CONSUMER_KERNEL: |
| 3051 | break; |
| 3052 | case LTTNG_CONSUMER32_UST: |
| 3053 | case LTTNG_CONSUMER64_UST: |
| 3054 | health_code_update(); |
| 3055 | /* Destroy streams that might have been left in the stream list. */ |
| 3056 | clean_channel_stream_list(chan); |
| 3057 | break; |
| 3058 | default: |
| 3059 | ERR("Unknown consumer_data type"); |
| 3060 | abort(); |
| 3061 | } |
| 3062 | |
| 3063 | /* |
| 3064 | * Release our own refcount. Force channel deletion even if |
| 3065 | * streams were not initialized. |
| 3066 | */ |
| 3067 | if (!uatomic_sub_return(&chan->refcount, 1)) { |
| 3068 | consumer_del_channel(chan); |
| 3069 | } |
| 3070 | rcu_read_unlock(); |
| 3071 | goto restart; |
| 3072 | } |
| 3073 | case CONSUMER_CHANNEL_QUIT: |
| 3074 | /* |
| 3075 | * Remove the pipe from the poll set and continue the loop |
| 3076 | * since their might be data to consume. |
| 3077 | */ |
| 3078 | lttng_poll_del(&events, ctx->consumer_channel_pipe[0]); |
| 3079 | continue; |
| 3080 | default: |
| 3081 | ERR("Unknown action"); |
| 3082 | break; |
| 3083 | } |
| 3084 | } else if (revents & (LPOLLERR | LPOLLHUP)) { |
| 3085 | DBG("Channel thread pipe hung up"); |
| 3086 | /* |
| 3087 | * Remove the pipe from the poll set and continue the loop |
| 3088 | * since their might be data to consume. |
| 3089 | */ |
| 3090 | lttng_poll_del(&events, ctx->consumer_channel_pipe[0]); |
| 3091 | continue; |
| 3092 | } else { |
| 3093 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 3094 | goto end; |
| 3095 | } |
| 3096 | |
| 3097 | /* Handle other stream */ |
| 3098 | continue; |
| 3099 | } |
| 3100 | |
| 3101 | rcu_read_lock(); |
| 3102 | { |
| 3103 | uint64_t tmp_id = (uint64_t) pollfd; |
| 3104 | |
| 3105 | lttng_ht_lookup(channel_ht, &tmp_id, &iter); |
| 3106 | } |
| 3107 | node = lttng_ht_iter_get_node_u64(&iter); |
| 3108 | LTTNG_ASSERT(node); |
| 3109 | |
| 3110 | chan = caa_container_of(node, struct lttng_consumer_channel, |
| 3111 | wait_fd_node); |
| 3112 | |
| 3113 | /* Check for error event */ |
| 3114 | if (revents & (LPOLLERR | LPOLLHUP)) { |
| 3115 | DBG("Channel fd %d is hup|err.", pollfd); |
| 3116 | |
| 3117 | lttng_poll_del(&events, chan->wait_fd); |
| 3118 | ret = lttng_ht_del(channel_ht, &iter); |
| 3119 | LTTNG_ASSERT(ret == 0); |
| 3120 | |
| 3121 | /* |
| 3122 | * This will close the wait fd for each stream associated to |
| 3123 | * this channel AND monitored by the data/metadata thread thus |
| 3124 | * will be clean by the right thread. |
| 3125 | */ |
| 3126 | consumer_close_channel_streams(chan); |
| 3127 | |
| 3128 | /* Release our own refcount */ |
| 3129 | if (!uatomic_sub_return(&chan->refcount, 1) |
| 3130 | && !uatomic_read(&chan->nb_init_stream_left)) { |
| 3131 | consumer_del_channel(chan); |
| 3132 | } |
| 3133 | } else { |
| 3134 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 3135 | rcu_read_unlock(); |
| 3136 | goto end; |
| 3137 | } |
| 3138 | |
| 3139 | /* Release RCU lock for the channel looked up */ |
| 3140 | rcu_read_unlock(); |
| 3141 | } |
| 3142 | } |
| 3143 | |
| 3144 | /* All is OK */ |
| 3145 | err = 0; |
| 3146 | end: |
| 3147 | lttng_poll_clean(&events); |
| 3148 | end_poll: |
| 3149 | destroy_channel_ht(channel_ht); |
| 3150 | end_ht: |
| 3151 | error_testpoint: |
| 3152 | DBG("Channel poll thread exiting"); |
| 3153 | if (err) { |
| 3154 | health_error(); |
| 3155 | ERR("Health error occurred in %s", __func__); |
| 3156 | } |
| 3157 | health_unregister(health_consumerd); |
| 3158 | rcu_unregister_thread(); |
| 3159 | return NULL; |
| 3160 | } |
| 3161 | |
| 3162 | static int set_metadata_socket(struct lttng_consumer_local_data *ctx, |
| 3163 | struct pollfd *sockpoll, int client_socket) |
| 3164 | { |
| 3165 | int ret; |
| 3166 | |
| 3167 | LTTNG_ASSERT(ctx); |
| 3168 | LTTNG_ASSERT(sockpoll); |
| 3169 | |
| 3170 | ret = lttng_consumer_poll_socket(sockpoll); |
| 3171 | if (ret) { |
| 3172 | goto error; |
| 3173 | } |
| 3174 | DBG("Metadata connection on client_socket"); |
| 3175 | |
| 3176 | /* Blocking call, waiting for transmission */ |
| 3177 | ctx->consumer_metadata_socket = lttcomm_accept_unix_sock(client_socket); |
| 3178 | if (ctx->consumer_metadata_socket < 0) { |
| 3179 | WARN("On accept metadata"); |
| 3180 | ret = -1; |
| 3181 | goto error; |
| 3182 | } |
| 3183 | ret = 0; |
| 3184 | |
| 3185 | error: |
| 3186 | return ret; |
| 3187 | } |
| 3188 | |
| 3189 | /* |
| 3190 | * This thread listens on the consumerd socket and receives the file |
| 3191 | * descriptors from the session daemon. |
| 3192 | */ |
| 3193 | void *consumer_thread_sessiond_poll(void *data) |
| 3194 | { |
| 3195 | int sock = -1, client_socket, ret, err = -1; |
| 3196 | /* |
| 3197 | * structure to poll for incoming data on communication socket avoids |
| 3198 | * making blocking sockets. |
| 3199 | */ |
| 3200 | struct pollfd consumer_sockpoll[2]; |
| 3201 | struct lttng_consumer_local_data *ctx = (lttng_consumer_local_data *) data; |
| 3202 | |
| 3203 | rcu_register_thread(); |
| 3204 | |
| 3205 | health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_SESSIOND); |
| 3206 | |
| 3207 | if (testpoint(consumerd_thread_sessiond)) { |
| 3208 | goto error_testpoint; |
| 3209 | } |
| 3210 | |
| 3211 | health_code_update(); |
| 3212 | |
| 3213 | DBG("Creating command socket %s", ctx->consumer_command_sock_path); |
| 3214 | unlink(ctx->consumer_command_sock_path); |
| 3215 | client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path); |
| 3216 | if (client_socket < 0) { |
| 3217 | ERR("Cannot create command socket"); |
| 3218 | goto end; |
| 3219 | } |
| 3220 | |
| 3221 | ret = lttcomm_listen_unix_sock(client_socket); |
| 3222 | if (ret < 0) { |
| 3223 | goto end; |
| 3224 | } |
| 3225 | |
| 3226 | DBG("Sending ready command to lttng-sessiond"); |
| 3227 | ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY); |
| 3228 | /* return < 0 on error, but == 0 is not fatal */ |
| 3229 | if (ret < 0) { |
| 3230 | ERR("Error sending ready command to lttng-sessiond"); |
| 3231 | goto end; |
| 3232 | } |
| 3233 | |
| 3234 | /* prepare the FDs to poll : to client socket and the should_quit pipe */ |
| 3235 | consumer_sockpoll[0].fd = ctx->consumer_should_quit[0]; |
| 3236 | consumer_sockpoll[0].events = POLLIN | POLLPRI; |
| 3237 | consumer_sockpoll[1].fd = client_socket; |
| 3238 | consumer_sockpoll[1].events = POLLIN | POLLPRI; |
| 3239 | |
| 3240 | ret = lttng_consumer_poll_socket(consumer_sockpoll); |
| 3241 | if (ret) { |
| 3242 | if (ret > 0) { |
| 3243 | /* should exit */ |
| 3244 | err = 0; |
| 3245 | } |
| 3246 | goto end; |
| 3247 | } |
| 3248 | DBG("Connection on client_socket"); |
| 3249 | |
| 3250 | /* Blocking call, waiting for transmission */ |
| 3251 | sock = lttcomm_accept_unix_sock(client_socket); |
| 3252 | if (sock < 0) { |
| 3253 | WARN("On accept"); |
| 3254 | goto end; |
| 3255 | } |
| 3256 | |
| 3257 | /* |
| 3258 | * Setup metadata socket which is the second socket connection on the |
| 3259 | * command unix socket. |
| 3260 | */ |
| 3261 | ret = set_metadata_socket(ctx, consumer_sockpoll, client_socket); |
| 3262 | if (ret) { |
| 3263 | if (ret > 0) { |
| 3264 | /* should exit */ |
| 3265 | err = 0; |
| 3266 | } |
| 3267 | goto end; |
| 3268 | } |
| 3269 | |
| 3270 | /* This socket is not useful anymore. */ |
| 3271 | ret = close(client_socket); |
| 3272 | if (ret < 0) { |
| 3273 | PERROR("close client_socket"); |
| 3274 | } |
| 3275 | client_socket = -1; |
| 3276 | |
| 3277 | /* update the polling structure to poll on the established socket */ |
| 3278 | consumer_sockpoll[1].fd = sock; |
| 3279 | consumer_sockpoll[1].events = POLLIN | POLLPRI; |
| 3280 | |
| 3281 | while (1) { |
| 3282 | health_code_update(); |
| 3283 | |
| 3284 | health_poll_entry(); |
| 3285 | ret = lttng_consumer_poll_socket(consumer_sockpoll); |
| 3286 | health_poll_exit(); |
| 3287 | if (ret) { |
| 3288 | if (ret > 0) { |
| 3289 | /* should exit */ |
| 3290 | err = 0; |
| 3291 | } |
| 3292 | goto end; |
| 3293 | } |
| 3294 | DBG("Incoming command on sock"); |
| 3295 | ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll); |
| 3296 | if (ret <= 0) { |
| 3297 | /* |
| 3298 | * This could simply be a session daemon quitting. Don't output |
| 3299 | * ERR() here. |
| 3300 | */ |
| 3301 | DBG("Communication interrupted on command socket"); |
| 3302 | err = 0; |
| 3303 | goto end; |
| 3304 | } |
| 3305 | if (CMM_LOAD_SHARED(consumer_quit)) { |
| 3306 | DBG("consumer_thread_receive_fds received quit from signal"); |
| 3307 | err = 0; /* All is OK */ |
| 3308 | goto end; |
| 3309 | } |
| 3310 | DBG("Received command on sock"); |
| 3311 | } |
| 3312 | /* All is OK */ |
| 3313 | err = 0; |
| 3314 | |
| 3315 | end: |
| 3316 | DBG("Consumer thread sessiond poll exiting"); |
| 3317 | |
| 3318 | /* |
| 3319 | * Close metadata streams since the producer is the session daemon which |
| 3320 | * just died. |
| 3321 | * |
| 3322 | * NOTE: for now, this only applies to the UST tracer. |
| 3323 | */ |
| 3324 | lttng_consumer_close_all_metadata(); |
| 3325 | |
| 3326 | /* |
| 3327 | * when all fds have hung up, the polling thread |
| 3328 | * can exit cleanly |
| 3329 | */ |
| 3330 | CMM_STORE_SHARED(consumer_quit, 1); |
| 3331 | |
| 3332 | /* |
| 3333 | * Notify the data poll thread to poll back again and test the |
| 3334 | * consumer_quit state that we just set so to quit gracefully. |
| 3335 | */ |
| 3336 | notify_thread_lttng_pipe(ctx->consumer_data_pipe); |
| 3337 | |
| 3338 | notify_channel_pipe(ctx, NULL, -1, CONSUMER_CHANNEL_QUIT); |
| 3339 | |
| 3340 | notify_health_quit_pipe(health_quit_pipe); |
| 3341 | |
| 3342 | /* Cleaning up possibly open sockets. */ |
| 3343 | if (sock >= 0) { |
| 3344 | ret = close(sock); |
| 3345 | if (ret < 0) { |
| 3346 | PERROR("close sock sessiond poll"); |
| 3347 | } |
| 3348 | } |
| 3349 | if (client_socket >= 0) { |
| 3350 | ret = close(client_socket); |
| 3351 | if (ret < 0) { |
| 3352 | PERROR("close client_socket sessiond poll"); |
| 3353 | } |
| 3354 | } |
| 3355 | |
| 3356 | error_testpoint: |
| 3357 | if (err) { |
| 3358 | health_error(); |
| 3359 | ERR("Health error occurred in %s", __func__); |
| 3360 | } |
| 3361 | health_unregister(health_consumerd); |
| 3362 | |
| 3363 | rcu_unregister_thread(); |
| 3364 | return NULL; |
| 3365 | } |
| 3366 | |
| 3367 | static int post_consume(struct lttng_consumer_stream *stream, |
| 3368 | const struct stream_subbuffer *subbuffer, |
| 3369 | struct lttng_consumer_local_data *ctx) |
| 3370 | { |
| 3371 | size_t i; |
| 3372 | int ret = 0; |
| 3373 | const size_t count = lttng_dynamic_array_get_count( |
| 3374 | &stream->read_subbuffer_ops.post_consume_cbs); |
| 3375 | |
| 3376 | for (i = 0; i < count; i++) { |
| 3377 | const post_consume_cb op = *(post_consume_cb *) lttng_dynamic_array_get_element( |
| 3378 | &stream->read_subbuffer_ops.post_consume_cbs, |
| 3379 | i); |
| 3380 | |
| 3381 | ret = op(stream, subbuffer, ctx); |
| 3382 | if (ret) { |
| 3383 | goto end; |
| 3384 | } |
| 3385 | } |
| 3386 | end: |
| 3387 | return ret; |
| 3388 | } |
| 3389 | |
| 3390 | ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream, |
| 3391 | struct lttng_consumer_local_data *ctx, |
| 3392 | bool locked_by_caller) |
| 3393 | { |
| 3394 | ssize_t ret, written_bytes = 0; |
| 3395 | int rotation_ret; |
| 3396 | struct stream_subbuffer subbuffer = {}; |
| 3397 | enum get_next_subbuffer_status get_next_status; |
| 3398 | |
| 3399 | if (!locked_by_caller) { |
| 3400 | stream->read_subbuffer_ops.lock(stream); |
| 3401 | } else { |
| 3402 | stream->read_subbuffer_ops.assert_locked(stream); |
| 3403 | } |
| 3404 | |
| 3405 | if (stream->read_subbuffer_ops.on_wake_up) { |
| 3406 | ret = stream->read_subbuffer_ops.on_wake_up(stream); |
| 3407 | if (ret) { |
| 3408 | goto end; |
| 3409 | } |
| 3410 | } |
| 3411 | |
| 3412 | /* |
| 3413 | * If the stream was flagged to be ready for rotation before we extract |
| 3414 | * the next packet, rotate it now. |
| 3415 | */ |
| 3416 | if (stream->rotate_ready) { |
| 3417 | DBG("Rotate stream before consuming data"); |
| 3418 | ret = lttng_consumer_rotate_stream(stream); |
| 3419 | if (ret < 0) { |
| 3420 | ERR("Stream rotation error before consuming data"); |
| 3421 | goto end; |
| 3422 | } |
| 3423 | } |
| 3424 | |
| 3425 | get_next_status = stream->read_subbuffer_ops.get_next_subbuffer( |
| 3426 | stream, &subbuffer); |
| 3427 | switch (get_next_status) { |
| 3428 | case GET_NEXT_SUBBUFFER_STATUS_OK: |
| 3429 | break; |
| 3430 | case GET_NEXT_SUBBUFFER_STATUS_NO_DATA: |
| 3431 | /* Not an error. */ |
| 3432 | ret = 0; |
| 3433 | goto sleep_stream; |
| 3434 | case GET_NEXT_SUBBUFFER_STATUS_ERROR: |
| 3435 | ret = -1; |
| 3436 | goto end; |
| 3437 | default: |
| 3438 | abort(); |
| 3439 | } |
| 3440 | |
| 3441 | ret = stream->read_subbuffer_ops.pre_consume_subbuffer( |
| 3442 | stream, &subbuffer); |
| 3443 | if (ret) { |
| 3444 | goto error_put_subbuf; |
| 3445 | } |
| 3446 | |
| 3447 | written_bytes = stream->read_subbuffer_ops.consume_subbuffer( |
| 3448 | ctx, stream, &subbuffer); |
| 3449 | if (written_bytes <= 0) { |
| 3450 | ERR("Error consuming subbuffer: (%zd)", written_bytes); |
| 3451 | ret = (int) written_bytes; |
| 3452 | goto error_put_subbuf; |
| 3453 | } |
| 3454 | |
| 3455 | ret = stream->read_subbuffer_ops.put_next_subbuffer(stream, &subbuffer); |
| 3456 | if (ret) { |
| 3457 | goto end; |
| 3458 | } |
| 3459 | |
| 3460 | ret = post_consume(stream, &subbuffer, ctx); |
| 3461 | if (ret) { |
| 3462 | goto end; |
| 3463 | } |
| 3464 | |
| 3465 | /* |
| 3466 | * After extracting the packet, we check if the stream is now ready to |
| 3467 | * be rotated and perform the action immediately. |
| 3468 | * |
| 3469 | * Don't overwrite `ret` as callers expect the number of bytes |
| 3470 | * consumed to be returned on success. |
| 3471 | */ |
| 3472 | rotation_ret = lttng_consumer_stream_is_rotate_ready(stream); |
| 3473 | if (rotation_ret == 1) { |
| 3474 | rotation_ret = lttng_consumer_rotate_stream(stream); |
| 3475 | if (rotation_ret < 0) { |
| 3476 | ret = rotation_ret; |
| 3477 | ERR("Stream rotation error after consuming data"); |
| 3478 | goto end; |
| 3479 | } |
| 3480 | |
| 3481 | } else if (rotation_ret < 0) { |
| 3482 | ret = rotation_ret; |
| 3483 | ERR("Failed to check if stream was ready to rotate after consuming data"); |
| 3484 | goto end; |
| 3485 | } |
| 3486 | |
| 3487 | sleep_stream: |
| 3488 | if (stream->read_subbuffer_ops.on_sleep) { |
| 3489 | stream->read_subbuffer_ops.on_sleep(stream, ctx); |
| 3490 | } |
| 3491 | |
| 3492 | ret = written_bytes; |
| 3493 | end: |
| 3494 | if (!locked_by_caller) { |
| 3495 | stream->read_subbuffer_ops.unlock(stream); |
| 3496 | } |
| 3497 | |
| 3498 | return ret; |
| 3499 | error_put_subbuf: |
| 3500 | (void) stream->read_subbuffer_ops.put_next_subbuffer(stream, &subbuffer); |
| 3501 | goto end; |
| 3502 | } |
| 3503 | |
| 3504 | int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream) |
| 3505 | { |
| 3506 | switch (the_consumer_data.type) { |
| 3507 | case LTTNG_CONSUMER_KERNEL: |
| 3508 | return lttng_kconsumer_on_recv_stream(stream); |
| 3509 | case LTTNG_CONSUMER32_UST: |
| 3510 | case LTTNG_CONSUMER64_UST: |
| 3511 | return lttng_ustconsumer_on_recv_stream(stream); |
| 3512 | default: |
| 3513 | ERR("Unknown consumer_data type"); |
| 3514 | abort(); |
| 3515 | return -ENOSYS; |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | /* |
| 3520 | * Allocate and set consumer data hash tables. |
| 3521 | */ |
| 3522 | int lttng_consumer_init(void) |
| 3523 | { |
| 3524 | the_consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 3525 | if (!the_consumer_data.channel_ht) { |
| 3526 | goto error; |
| 3527 | } |
| 3528 | |
| 3529 | the_consumer_data.channels_by_session_id_ht = |
| 3530 | lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 3531 | if (!the_consumer_data.channels_by_session_id_ht) { |
| 3532 | goto error; |
| 3533 | } |
| 3534 | |
| 3535 | the_consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 3536 | if (!the_consumer_data.relayd_ht) { |
| 3537 | goto error; |
| 3538 | } |
| 3539 | |
| 3540 | the_consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 3541 | if (!the_consumer_data.stream_list_ht) { |
| 3542 | goto error; |
| 3543 | } |
| 3544 | |
| 3545 | the_consumer_data.stream_per_chan_id_ht = |
| 3546 | lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 3547 | if (!the_consumer_data.stream_per_chan_id_ht) { |
| 3548 | goto error; |
| 3549 | } |
| 3550 | |
| 3551 | data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 3552 | if (!data_ht) { |
| 3553 | goto error; |
| 3554 | } |
| 3555 | |
| 3556 | metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 3557 | if (!metadata_ht) { |
| 3558 | goto error; |
| 3559 | } |
| 3560 | |
| 3561 | the_consumer_data.chunk_registry = lttng_trace_chunk_registry_create(); |
| 3562 | if (!the_consumer_data.chunk_registry) { |
| 3563 | goto error; |
| 3564 | } |
| 3565 | |
| 3566 | return 0; |
| 3567 | |
| 3568 | error: |
| 3569 | return -1; |
| 3570 | } |
| 3571 | |
| 3572 | /* |
| 3573 | * Process the ADD_RELAYD command receive by a consumer. |
| 3574 | * |
| 3575 | * This will create a relayd socket pair and add it to the relayd hash table. |
| 3576 | * The caller MUST acquire a RCU read side lock before calling it. |
| 3577 | */ |
| 3578 | void consumer_add_relayd_socket(uint64_t net_seq_idx, |
| 3579 | int sock_type, |
| 3580 | struct lttng_consumer_local_data *ctx, |
| 3581 | int sock, |
| 3582 | struct pollfd *consumer_sockpoll, |
| 3583 | uint64_t sessiond_id, |
| 3584 | uint64_t relayd_session_id, |
| 3585 | uint32_t relayd_version_major, |
| 3586 | uint32_t relayd_version_minor, |
| 3587 | enum lttcomm_sock_proto relayd_socket_protocol) |
| 3588 | { |
| 3589 | int fd = -1, ret = -1, relayd_created = 0; |
| 3590 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
| 3591 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 3592 | |
| 3593 | LTTNG_ASSERT(ctx); |
| 3594 | LTTNG_ASSERT(sock >= 0); |
| 3595 | ASSERT_RCU_READ_LOCKED(); |
| 3596 | |
| 3597 | DBG("Consumer adding relayd socket (idx: %" PRIu64 ")", net_seq_idx); |
| 3598 | |
| 3599 | /* Get relayd reference if exists. */ |
| 3600 | relayd = consumer_find_relayd(net_seq_idx); |
| 3601 | if (relayd == NULL) { |
| 3602 | LTTNG_ASSERT(sock_type == LTTNG_STREAM_CONTROL); |
| 3603 | /* Not found. Allocate one. */ |
| 3604 | relayd = consumer_allocate_relayd_sock_pair(net_seq_idx); |
| 3605 | if (relayd == NULL) { |
| 3606 | ret_code = LTTCOMM_CONSUMERD_ENOMEM; |
| 3607 | goto error; |
| 3608 | } else { |
| 3609 | relayd->sessiond_session_id = sessiond_id; |
| 3610 | relayd_created = 1; |
| 3611 | } |
| 3612 | |
| 3613 | /* |
| 3614 | * This code path MUST continue to the consumer send status message to |
| 3615 | * we can notify the session daemon and continue our work without |
| 3616 | * killing everything. |
| 3617 | */ |
| 3618 | } else { |
| 3619 | /* |
| 3620 | * relayd key should never be found for control socket. |
| 3621 | */ |
| 3622 | LTTNG_ASSERT(sock_type != LTTNG_STREAM_CONTROL); |
| 3623 | } |
| 3624 | |
| 3625 | /* First send a status message before receiving the fds. */ |
| 3626 | ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS); |
| 3627 | if (ret < 0) { |
| 3628 | /* Somehow, the session daemon is not responding anymore. */ |
| 3629 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL); |
| 3630 | goto error_nosignal; |
| 3631 | } |
| 3632 | |
| 3633 | /* Poll on consumer socket. */ |
| 3634 | ret = lttng_consumer_poll_socket(consumer_sockpoll); |
| 3635 | if (ret) { |
| 3636 | /* Needing to exit in the middle of a command: error. */ |
| 3637 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
| 3638 | goto error_nosignal; |
| 3639 | } |
| 3640 | |
| 3641 | /* Get relayd socket from session daemon */ |
| 3642 | ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); |
| 3643 | if (ret != sizeof(fd)) { |
| 3644 | fd = -1; /* Just in case it gets set with an invalid value. */ |
| 3645 | |
| 3646 | /* |
| 3647 | * Failing to receive FDs might indicate a major problem such as |
| 3648 | * reaching a fd limit during the receive where the kernel returns a |
| 3649 | * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we |
| 3650 | * don't take any chances and stop everything. |
| 3651 | * |
| 3652 | * XXX: Feature request #558 will fix that and avoid this possible |
| 3653 | * issue when reaching the fd limit. |
| 3654 | */ |
| 3655 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
| 3656 | ret_code = LTTCOMM_CONSUMERD_ERROR_RECV_FD; |
| 3657 | goto error; |
| 3658 | } |
| 3659 | |
| 3660 | /* Copy socket information and received FD */ |
| 3661 | switch (sock_type) { |
| 3662 | case LTTNG_STREAM_CONTROL: |
| 3663 | /* Copy received lttcomm socket */ |
| 3664 | ret = lttcomm_populate_sock_from_open_socket( |
| 3665 | &relayd->control_sock.sock, fd, |
| 3666 | relayd_socket_protocol); |
| 3667 | |
| 3668 | /* Assign version values. */ |
| 3669 | relayd->control_sock.major = relayd_version_major; |
| 3670 | relayd->control_sock.minor = relayd_version_minor; |
| 3671 | |
| 3672 | relayd->relayd_session_id = relayd_session_id; |
| 3673 | |
| 3674 | break; |
| 3675 | case LTTNG_STREAM_DATA: |
| 3676 | /* Copy received lttcomm socket */ |
| 3677 | ret = lttcomm_populate_sock_from_open_socket( |
| 3678 | &relayd->data_sock.sock, fd, |
| 3679 | relayd_socket_protocol); |
| 3680 | /* Assign version values. */ |
| 3681 | relayd->data_sock.major = relayd_version_major; |
| 3682 | relayd->data_sock.minor = relayd_version_minor; |
| 3683 | break; |
| 3684 | default: |
| 3685 | ERR("Unknown relayd socket type (%d)", sock_type); |
| 3686 | ret_code = LTTCOMM_CONSUMERD_FATAL; |
| 3687 | goto error; |
| 3688 | } |
| 3689 | |
| 3690 | if (ret < 0) { |
| 3691 | ret_code = LTTCOMM_CONSUMERD_FATAL; |
| 3692 | goto error; |
| 3693 | } |
| 3694 | |
| 3695 | DBG("Consumer %s socket created successfully with net idx %" PRIu64 " (fd: %d)", |
| 3696 | sock_type == LTTNG_STREAM_CONTROL ? "control" : "data", |
| 3697 | relayd->net_seq_idx, fd); |
| 3698 | /* |
| 3699 | * We gave the ownership of the fd to the relayd structure. Set the |
| 3700 | * fd to -1 so we don't call close() on it in the error path below. |
| 3701 | */ |
| 3702 | fd = -1; |
| 3703 | |
| 3704 | /* We successfully added the socket. Send status back. */ |
| 3705 | ret = consumer_send_status_msg(sock, ret_code); |
| 3706 | if (ret < 0) { |
| 3707 | /* Somehow, the session daemon is not responding anymore. */ |
| 3708 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL); |
| 3709 | goto error_nosignal; |
| 3710 | } |
| 3711 | |
| 3712 | /* |
| 3713 | * Add relayd socket pair to consumer data hashtable. If object already |
| 3714 | * exists or on error, the function gracefully returns. |
| 3715 | */ |
| 3716 | relayd->ctx = ctx; |
| 3717 | add_relayd(relayd); |
| 3718 | |
| 3719 | /* All good! */ |
| 3720 | return; |
| 3721 | |
| 3722 | error: |
| 3723 | if (consumer_send_status_msg(sock, ret_code) < 0) { |
| 3724 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL); |
| 3725 | } |
| 3726 | |
| 3727 | error_nosignal: |
| 3728 | /* Close received socket if valid. */ |
| 3729 | if (fd >= 0) { |
| 3730 | if (close(fd)) { |
| 3731 | PERROR("close received socket"); |
| 3732 | } |
| 3733 | } |
| 3734 | |
| 3735 | if (relayd_created) { |
| 3736 | free(relayd); |
| 3737 | } |
| 3738 | } |
| 3739 | |
| 3740 | /* |
| 3741 | * Search for a relayd associated to the session id and return the reference. |
| 3742 | * |
| 3743 | * A rcu read side lock MUST be acquire before calling this function and locked |
| 3744 | * until the relayd object is no longer necessary. |
| 3745 | */ |
| 3746 | static struct consumer_relayd_sock_pair *find_relayd_by_session_id(uint64_t id) |
| 3747 | { |
| 3748 | struct lttng_ht_iter iter; |
| 3749 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 3750 | |
| 3751 | ASSERT_RCU_READ_LOCKED(); |
| 3752 | |
| 3753 | /* Iterate over all relayd since they are indexed by net_seq_idx. */ |
| 3754 | cds_lfht_for_each_entry(the_consumer_data.relayd_ht->ht, &iter.iter, |
| 3755 | relayd, node.node) { |
| 3756 | /* |
| 3757 | * Check by sessiond id which is unique here where the relayd session |
| 3758 | * id might not be when having multiple relayd. |
| 3759 | */ |
| 3760 | if (relayd->sessiond_session_id == id) { |
| 3761 | /* Found the relayd. There can be only one per id. */ |
| 3762 | goto found; |
| 3763 | } |
| 3764 | } |
| 3765 | |
| 3766 | return NULL; |
| 3767 | |
| 3768 | found: |
| 3769 | return relayd; |
| 3770 | } |
| 3771 | |
| 3772 | /* |
| 3773 | * Check if for a given session id there is still data needed to be extract |
| 3774 | * from the buffers. |
| 3775 | * |
| 3776 | * Return 1 if data is pending or else 0 meaning ready to be read. |
| 3777 | */ |
| 3778 | int consumer_data_pending(uint64_t id) |
| 3779 | { |
| 3780 | int ret; |
| 3781 | struct lttng_ht_iter iter; |
| 3782 | struct lttng_ht *ht; |
| 3783 | struct lttng_consumer_stream *stream; |
| 3784 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 3785 | int (*data_pending)(struct lttng_consumer_stream *); |
| 3786 | |
| 3787 | DBG("Consumer data pending command on session id %" PRIu64, id); |
| 3788 | |
| 3789 | rcu_read_lock(); |
| 3790 | pthread_mutex_lock(&the_consumer_data.lock); |
| 3791 | |
| 3792 | switch (the_consumer_data.type) { |
| 3793 | case LTTNG_CONSUMER_KERNEL: |
| 3794 | data_pending = lttng_kconsumer_data_pending; |
| 3795 | break; |
| 3796 | case LTTNG_CONSUMER32_UST: |
| 3797 | case LTTNG_CONSUMER64_UST: |
| 3798 | data_pending = lttng_ustconsumer_data_pending; |
| 3799 | break; |
| 3800 | default: |
| 3801 | ERR("Unknown consumer data type"); |
| 3802 | abort(); |
| 3803 | } |
| 3804 | |
| 3805 | /* Ease our life a bit */ |
| 3806 | ht = the_consumer_data.stream_list_ht; |
| 3807 | |
| 3808 | cds_lfht_for_each_entry_duplicate(ht->ht, |
| 3809 | ht->hash_fct(&id, lttng_ht_seed), |
| 3810 | ht->match_fct, &id, |
| 3811 | &iter.iter, stream, node_session_id.node) { |
| 3812 | pthread_mutex_lock(&stream->lock); |
| 3813 | |
| 3814 | /* |
| 3815 | * A removed node from the hash table indicates that the stream has |
| 3816 | * been deleted thus having a guarantee that the buffers are closed |
| 3817 | * on the consumer side. However, data can still be transmitted |
| 3818 | * over the network so don't skip the relayd check. |
| 3819 | */ |
| 3820 | ret = cds_lfht_is_node_deleted(&stream->node.node); |
| 3821 | if (!ret) { |
| 3822 | /* Check the stream if there is data in the buffers. */ |
| 3823 | ret = data_pending(stream); |
| 3824 | if (ret == 1) { |
| 3825 | pthread_mutex_unlock(&stream->lock); |
| 3826 | goto data_pending; |
| 3827 | } |
| 3828 | } |
| 3829 | |
| 3830 | pthread_mutex_unlock(&stream->lock); |
| 3831 | } |
| 3832 | |
| 3833 | relayd = find_relayd_by_session_id(id); |
| 3834 | if (relayd) { |
| 3835 | unsigned int is_data_inflight = 0; |
| 3836 | |
| 3837 | /* Send init command for data pending. */ |
| 3838 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 3839 | ret = relayd_begin_data_pending(&relayd->control_sock, |
| 3840 | relayd->relayd_session_id); |
| 3841 | if (ret < 0) { |
| 3842 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 3843 | /* Communication error thus the relayd so no data pending. */ |
| 3844 | goto data_not_pending; |
| 3845 | } |
| 3846 | |
| 3847 | cds_lfht_for_each_entry_duplicate(ht->ht, |
| 3848 | ht->hash_fct(&id, lttng_ht_seed), |
| 3849 | ht->match_fct, &id, |
| 3850 | &iter.iter, stream, node_session_id.node) { |
| 3851 | if (stream->metadata_flag) { |
| 3852 | ret = relayd_quiescent_control(&relayd->control_sock, |
| 3853 | stream->relayd_stream_id); |
| 3854 | } else { |
| 3855 | ret = relayd_data_pending(&relayd->control_sock, |
| 3856 | stream->relayd_stream_id, |
| 3857 | stream->next_net_seq_num - 1); |
| 3858 | } |
| 3859 | |
| 3860 | if (ret == 1) { |
| 3861 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 3862 | goto data_pending; |
| 3863 | } else if (ret < 0) { |
| 3864 | ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
| 3865 | lttng_consumer_cleanup_relayd(relayd); |
| 3866 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 3867 | goto data_not_pending; |
| 3868 | } |
| 3869 | } |
| 3870 | |
| 3871 | /* Send end command for data pending. */ |
| 3872 | ret = relayd_end_data_pending(&relayd->control_sock, |
| 3873 | relayd->relayd_session_id, &is_data_inflight); |
| 3874 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 3875 | if (ret < 0) { |
| 3876 | ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx); |
| 3877 | lttng_consumer_cleanup_relayd(relayd); |
| 3878 | goto data_not_pending; |
| 3879 | } |
| 3880 | if (is_data_inflight) { |
| 3881 | goto data_pending; |
| 3882 | } |
| 3883 | } |
| 3884 | |
| 3885 | /* |
| 3886 | * Finding _no_ node in the hash table and no inflight data means that the |
| 3887 | * stream(s) have been removed thus data is guaranteed to be available for |
| 3888 | * analysis from the trace files. |
| 3889 | */ |
| 3890 | |
| 3891 | data_not_pending: |
| 3892 | /* Data is available to be read by a viewer. */ |
| 3893 | pthread_mutex_unlock(&the_consumer_data.lock); |
| 3894 | rcu_read_unlock(); |
| 3895 | return 0; |
| 3896 | |
| 3897 | data_pending: |
| 3898 | /* Data is still being extracted from buffers. */ |
| 3899 | pthread_mutex_unlock(&the_consumer_data.lock); |
| 3900 | rcu_read_unlock(); |
| 3901 | return 1; |
| 3902 | } |
| 3903 | |
| 3904 | /* |
| 3905 | * Send a ret code status message to the sessiond daemon. |
| 3906 | * |
| 3907 | * Return the sendmsg() return value. |
| 3908 | */ |
| 3909 | int consumer_send_status_msg(int sock, int ret_code) |
| 3910 | { |
| 3911 | struct lttcomm_consumer_status_msg msg; |
| 3912 | |
| 3913 | memset(&msg, 0, sizeof(msg)); |
| 3914 | msg.ret_code = (lttcomm_return_code) ret_code; |
| 3915 | |
| 3916 | return lttcomm_send_unix_sock(sock, &msg, sizeof(msg)); |
| 3917 | } |
| 3918 | |
| 3919 | /* |
| 3920 | * Send a channel status message to the sessiond daemon. |
| 3921 | * |
| 3922 | * Return the sendmsg() return value. |
| 3923 | */ |
| 3924 | int consumer_send_status_channel(int sock, |
| 3925 | struct lttng_consumer_channel *channel) |
| 3926 | { |
| 3927 | struct lttcomm_consumer_status_channel msg; |
| 3928 | |
| 3929 | LTTNG_ASSERT(sock >= 0); |
| 3930 | |
| 3931 | memset(&msg, 0, sizeof(msg)); |
| 3932 | if (!channel) { |
| 3933 | msg.ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL; |
| 3934 | } else { |
| 3935 | msg.ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
| 3936 | msg.key = channel->key; |
| 3937 | msg.stream_count = channel->streams.count; |
| 3938 | } |
| 3939 | |
| 3940 | return lttcomm_send_unix_sock(sock, &msg, sizeof(msg)); |
| 3941 | } |
| 3942 | |
| 3943 | unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos, |
| 3944 | unsigned long produced_pos, uint64_t nb_packets_per_stream, |
| 3945 | uint64_t max_sb_size) |
| 3946 | { |
| 3947 | unsigned long start_pos; |
| 3948 | |
| 3949 | if (!nb_packets_per_stream) { |
| 3950 | return consumed_pos; /* Grab everything */ |
| 3951 | } |
| 3952 | start_pos = produced_pos - lttng_offset_align_floor(produced_pos, max_sb_size); |
| 3953 | start_pos -= max_sb_size * nb_packets_per_stream; |
| 3954 | if ((long) (start_pos - consumed_pos) < 0) { |
| 3955 | return consumed_pos; /* Grab everything */ |
| 3956 | } |
| 3957 | return start_pos; |
| 3958 | } |
| 3959 | |
| 3960 | /* Stream lock must be held by the caller. */ |
| 3961 | static int sample_stream_positions(struct lttng_consumer_stream *stream, |
| 3962 | unsigned long *produced, unsigned long *consumed) |
| 3963 | { |
| 3964 | int ret; |
| 3965 | |
| 3966 | ASSERT_LOCKED(stream->lock); |
| 3967 | |
| 3968 | ret = lttng_consumer_sample_snapshot_positions(stream); |
| 3969 | if (ret < 0) { |
| 3970 | ERR("Failed to sample snapshot positions"); |
| 3971 | goto end; |
| 3972 | } |
| 3973 | |
| 3974 | ret = lttng_consumer_get_produced_snapshot(stream, produced); |
| 3975 | if (ret < 0) { |
| 3976 | ERR("Failed to sample produced position"); |
| 3977 | goto end; |
| 3978 | } |
| 3979 | |
| 3980 | ret = lttng_consumer_get_consumed_snapshot(stream, consumed); |
| 3981 | if (ret < 0) { |
| 3982 | ERR("Failed to sample consumed position"); |
| 3983 | goto end; |
| 3984 | } |
| 3985 | |
| 3986 | end: |
| 3987 | return ret; |
| 3988 | } |
| 3989 | |
| 3990 | /* |
| 3991 | * Sample the rotate position for all the streams of a channel. If a stream |
| 3992 | * is already at the rotate position (produced == consumed), we flag it as |
| 3993 | * ready for rotation. The rotation of ready streams occurs after we have |
| 3994 | * replied to the session daemon that we have finished sampling the positions. |
| 3995 | * Must be called with RCU read-side lock held to ensure existence of channel. |
| 3996 | * |
| 3997 | * Returns 0 on success, < 0 on error |
| 3998 | */ |
| 3999 | int lttng_consumer_rotate_channel(struct lttng_consumer_channel *channel, |
| 4000 | uint64_t key, uint64_t relayd_id) |
| 4001 | { |
| 4002 | int ret; |
| 4003 | struct lttng_consumer_stream *stream; |
| 4004 | struct lttng_ht_iter iter; |
| 4005 | struct lttng_ht *ht = the_consumer_data.stream_per_chan_id_ht; |
| 4006 | struct lttng_dynamic_array stream_rotation_positions; |
| 4007 | uint64_t next_chunk_id, stream_count = 0; |
| 4008 | enum lttng_trace_chunk_status chunk_status; |
| 4009 | const bool is_local_trace = relayd_id == -1ULL; |
| 4010 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 4011 | bool rotating_to_new_chunk = true; |
| 4012 | /* Array of `struct lttng_consumer_stream *` */ |
| 4013 | struct lttng_dynamic_pointer_array streams_packet_to_open; |
| 4014 | size_t stream_idx; |
| 4015 | |
| 4016 | ASSERT_RCU_READ_LOCKED(); |
| 4017 | |
| 4018 | DBG("Consumer sample rotate position for channel %" PRIu64, key); |
| 4019 | |
| 4020 | lttng_dynamic_array_init(&stream_rotation_positions, |
| 4021 | sizeof(struct relayd_stream_rotation_position), NULL); |
| 4022 | lttng_dynamic_pointer_array_init(&streams_packet_to_open, NULL); |
| 4023 | |
| 4024 | rcu_read_lock(); |
| 4025 | |
| 4026 | pthread_mutex_lock(&channel->lock); |
| 4027 | LTTNG_ASSERT(channel->trace_chunk); |
| 4028 | chunk_status = lttng_trace_chunk_get_id(channel->trace_chunk, |
| 4029 | &next_chunk_id); |
| 4030 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 4031 | ret = -1; |
| 4032 | goto end_unlock_channel; |
| 4033 | } |
| 4034 | |
| 4035 | cds_lfht_for_each_entry_duplicate(ht->ht, |
| 4036 | ht->hash_fct(&channel->key, lttng_ht_seed), |
| 4037 | ht->match_fct, &channel->key, &iter.iter, |
| 4038 | stream, node_channel_id.node) { |
| 4039 | unsigned long produced_pos = 0, consumed_pos = 0; |
| 4040 | |
| 4041 | health_code_update(); |
| 4042 | |
| 4043 | /* |
| 4044 | * Lock stream because we are about to change its state. |
| 4045 | */ |
| 4046 | pthread_mutex_lock(&stream->lock); |
| 4047 | |
| 4048 | if (stream->trace_chunk == stream->chan->trace_chunk) { |
| 4049 | rotating_to_new_chunk = false; |
| 4050 | } |
| 4051 | |
| 4052 | /* |
| 4053 | * Do not flush a packet when rotating from a NULL trace |
| 4054 | * chunk. The stream has no means to output data, and the prior |
| 4055 | * rotation which rotated to NULL performed that side-effect |
| 4056 | * already. No new data can be produced when a stream has no |
| 4057 | * associated trace chunk (e.g. a stop followed by a rotate). |
| 4058 | */ |
| 4059 | if (stream->trace_chunk) { |
| 4060 | bool flush_active; |
| 4061 | |
| 4062 | if (stream->metadata_flag) { |
| 4063 | /* |
| 4064 | * Don't produce an empty metadata packet, |
| 4065 | * simply close the current one. |
| 4066 | * |
| 4067 | * Metadata is regenerated on every trace chunk |
| 4068 | * switch; there is no concern that no data was |
| 4069 | * produced. |
| 4070 | */ |
| 4071 | flush_active = true; |
| 4072 | } else { |
| 4073 | /* |
| 4074 | * Only flush an empty packet if the "packet |
| 4075 | * open" could not be performed on transition |
| 4076 | * to a new trace chunk and no packets were |
| 4077 | * consumed within the chunk's lifetime. |
| 4078 | */ |
| 4079 | if (stream->opened_packet_in_current_trace_chunk) { |
| 4080 | flush_active = true; |
| 4081 | } else { |
| 4082 | /* |
| 4083 | * Stream could have been full at the |
| 4084 | * time of rotation, but then have had |
| 4085 | * no activity at all. |
| 4086 | * |
| 4087 | * It is important to flush a packet |
| 4088 | * to prevent 0-length files from being |
| 4089 | * produced as most viewers choke on |
| 4090 | * them. |
| 4091 | * |
| 4092 | * Unfortunately viewers will not be |
| 4093 | * able to know that tracing was active |
| 4094 | * for this stream during this trace |
| 4095 | * chunk's lifetime. |
| 4096 | */ |
| 4097 | ret = sample_stream_positions(stream, &produced_pos, &consumed_pos); |
| 4098 | if (ret) { |
| 4099 | goto end_unlock_stream; |
| 4100 | } |
| 4101 | |
| 4102 | /* |
| 4103 | * Don't flush an empty packet if data |
| 4104 | * was produced; it will be consumed |
| 4105 | * before the rotation completes. |
| 4106 | */ |
| 4107 | flush_active = produced_pos != consumed_pos; |
| 4108 | if (!flush_active) { |
| 4109 | const char *trace_chunk_name; |
| 4110 | uint64_t trace_chunk_id; |
| 4111 | |
| 4112 | chunk_status = lttng_trace_chunk_get_name( |
| 4113 | stream->trace_chunk, |
| 4114 | &trace_chunk_name, |
| 4115 | NULL); |
| 4116 | if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_NONE) { |
| 4117 | trace_chunk_name = "none"; |
| 4118 | } |
| 4119 | |
| 4120 | /* |
| 4121 | * Consumer trace chunks are |
| 4122 | * never anonymous. |
| 4123 | */ |
| 4124 | chunk_status = lttng_trace_chunk_get_id( |
| 4125 | stream->trace_chunk, |
| 4126 | &trace_chunk_id); |
| 4127 | LTTNG_ASSERT(chunk_status == |
| 4128 | LTTNG_TRACE_CHUNK_STATUS_OK); |
| 4129 | |
| 4130 | DBG("Unable to open packet for stream during trace chunk's lifetime. " |
| 4131 | "Flushing an empty packet to prevent an empty file from being created: " |
| 4132 | "stream id = %" PRIu64 ", trace chunk name = `%s`, trace chunk id = %" PRIu64, |
| 4133 | stream->key, trace_chunk_name, trace_chunk_id); |
| 4134 | } |
| 4135 | } |
| 4136 | } |
| 4137 | |
| 4138 | /* |
| 4139 | * Close the current packet before sampling the |
| 4140 | * ring buffer positions. |
| 4141 | */ |
| 4142 | ret = consumer_stream_flush_buffer(stream, flush_active); |
| 4143 | if (ret < 0) { |
| 4144 | ERR("Failed to flush stream %" PRIu64 " during channel rotation", |
| 4145 | stream->key); |
| 4146 | goto end_unlock_stream; |
| 4147 | } |
| 4148 | } |
| 4149 | |
| 4150 | ret = lttng_consumer_take_snapshot(stream); |
| 4151 | if (ret < 0 && ret != -ENODATA && ret != -EAGAIN) { |
| 4152 | ERR("Failed to sample snapshot position during channel rotation"); |
| 4153 | goto end_unlock_stream; |
| 4154 | } |
| 4155 | if (!ret) { |
| 4156 | ret = lttng_consumer_get_produced_snapshot(stream, |
| 4157 | &produced_pos); |
| 4158 | if (ret < 0) { |
| 4159 | ERR("Failed to sample produced position during channel rotation"); |
| 4160 | goto end_unlock_stream; |
| 4161 | } |
| 4162 | |
| 4163 | ret = lttng_consumer_get_consumed_snapshot(stream, |
| 4164 | &consumed_pos); |
| 4165 | if (ret < 0) { |
| 4166 | ERR("Failed to sample consumed position during channel rotation"); |
| 4167 | goto end_unlock_stream; |
| 4168 | } |
| 4169 | } |
| 4170 | /* |
| 4171 | * Align produced position on the start-of-packet boundary of the first |
| 4172 | * packet going into the next trace chunk. |
| 4173 | */ |
| 4174 | produced_pos = lttng_align_floor(produced_pos, stream->max_sb_size); |
| 4175 | if (consumed_pos == produced_pos) { |
| 4176 | DBG("Set rotate ready for stream %" PRIu64 " produced = %lu consumed = %lu", |
| 4177 | stream->key, produced_pos, consumed_pos); |
| 4178 | stream->rotate_ready = true; |
| 4179 | } else { |
| 4180 | DBG("Different consumed and produced positions " |
| 4181 | "for stream %" PRIu64 " produced = %lu consumed = %lu", |
| 4182 | stream->key, produced_pos, consumed_pos); |
| 4183 | } |
| 4184 | /* |
| 4185 | * The rotation position is based on the packet_seq_num of the |
| 4186 | * packet following the last packet that was consumed for this |
| 4187 | * stream, incremented by the offset between produced and |
| 4188 | * consumed positions. This rotation position is a lower bound |
| 4189 | * (inclusive) at which the next trace chunk starts. Since it |
| 4190 | * is a lower bound, it is OK if the packet_seq_num does not |
| 4191 | * correspond exactly to the same packet identified by the |
| 4192 | * consumed_pos, which can happen in overwrite mode. |
| 4193 | */ |
| 4194 | if (stream->sequence_number_unavailable) { |
| 4195 | /* |
| 4196 | * Rotation should never be performed on a session which |
| 4197 | * interacts with a pre-2.8 lttng-modules, which does |
| 4198 | * not implement packet sequence number. |
| 4199 | */ |
| 4200 | ERR("Failure to rotate stream %" PRIu64 ": sequence number unavailable", |
| 4201 | stream->key); |
| 4202 | ret = -1; |
| 4203 | goto end_unlock_stream; |
| 4204 | } |
| 4205 | stream->rotate_position = stream->last_sequence_number + 1 + |
| 4206 | ((produced_pos - consumed_pos) / stream->max_sb_size); |
| 4207 | DBG("Set rotation position for stream %" PRIu64 " at position %" PRIu64, |
| 4208 | stream->key, stream->rotate_position); |
| 4209 | |
| 4210 | if (!is_local_trace) { |
| 4211 | /* |
| 4212 | * The relay daemon control protocol expects a rotation |
| 4213 | * position as "the sequence number of the first packet |
| 4214 | * _after_ the current trace chunk". |
| 4215 | */ |
| 4216 | const struct relayd_stream_rotation_position position = { |
| 4217 | .stream_id = stream->relayd_stream_id, |
| 4218 | .rotate_at_seq_num = stream->rotate_position, |
| 4219 | }; |
| 4220 | |
| 4221 | ret = lttng_dynamic_array_add_element( |
| 4222 | &stream_rotation_positions, |
| 4223 | &position); |
| 4224 | if (ret) { |
| 4225 | ERR("Failed to allocate stream rotation position"); |
| 4226 | goto end_unlock_stream; |
| 4227 | } |
| 4228 | stream_count++; |
| 4229 | } |
| 4230 | |
| 4231 | stream->opened_packet_in_current_trace_chunk = false; |
| 4232 | |
| 4233 | if (rotating_to_new_chunk && !stream->metadata_flag) { |
| 4234 | /* |
| 4235 | * Attempt to flush an empty packet as close to the |
| 4236 | * rotation point as possible. In the event where a |
| 4237 | * stream remains inactive after the rotation point, |
| 4238 | * this ensures that the new trace chunk has a |
| 4239 | * beginning timestamp set at the begining of the |
| 4240 | * trace chunk instead of only creating an empty |
| 4241 | * packet when the trace chunk is stopped. |
| 4242 | * |
| 4243 | * This indicates to the viewers that the stream |
| 4244 | * was being recorded, but more importantly it |
| 4245 | * allows viewers to determine a useable trace |
| 4246 | * intersection. |
| 4247 | * |
| 4248 | * This presents a problem in the case where the |
| 4249 | * ring-buffer is completely full. |
| 4250 | * |
| 4251 | * Consider the following scenario: |
| 4252 | * - The consumption of data is slow (slow network, |
| 4253 | * for instance), |
| 4254 | * - The ring buffer is full, |
| 4255 | * - A rotation is initiated, |
| 4256 | * - The flush below does nothing (no space left to |
| 4257 | * open a new packet), |
| 4258 | * - The other streams rotate very soon, and new |
| 4259 | * data is produced in the new chunk, |
| 4260 | * - This stream completes its rotation long after the |
| 4261 | * rotation was initiated |
| 4262 | * - The session is stopped before any event can be |
| 4263 | * produced in this stream's buffers. |
| 4264 | * |
| 4265 | * The resulting trace chunk will have a single packet |
| 4266 | * temporaly at the end of the trace chunk for this |
| 4267 | * stream making the stream intersection more narrow |
| 4268 | * than it should be. |
| 4269 | * |
| 4270 | * To work-around this, an empty flush is performed |
| 4271 | * after the first consumption of a packet during a |
| 4272 | * rotation if open_packet fails. The idea is that |
| 4273 | * consuming a packet frees enough space to switch |
| 4274 | * packets in this scenario and allows the tracer to |
| 4275 | * "stamp" the beginning of the new trace chunk at the |
| 4276 | * earliest possible point. |
| 4277 | * |
| 4278 | * The packet open is performed after the channel |
| 4279 | * rotation to ensure that no attempt to open a packet |
| 4280 | * is performed in a stream that has no active trace |
| 4281 | * chunk. |
| 4282 | */ |
| 4283 | ret = lttng_dynamic_pointer_array_add_pointer( |
| 4284 | &streams_packet_to_open, stream); |
| 4285 | if (ret) { |
| 4286 | PERROR("Failed to add a stream pointer to array of streams in which to open a packet"); |
| 4287 | ret = -1; |
| 4288 | goto end_unlock_stream; |
| 4289 | } |
| 4290 | } |
| 4291 | |
| 4292 | pthread_mutex_unlock(&stream->lock); |
| 4293 | } |
| 4294 | stream = NULL; |
| 4295 | |
| 4296 | if (!is_local_trace) { |
| 4297 | relayd = consumer_find_relayd(relayd_id); |
| 4298 | if (!relayd) { |
| 4299 | ERR("Failed to find relayd %" PRIu64, relayd_id); |
| 4300 | ret = -1; |
| 4301 | goto end_unlock_channel; |
| 4302 | } |
| 4303 | |
| 4304 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 4305 | ret = relayd_rotate_streams(&relayd->control_sock, stream_count, |
| 4306 | rotating_to_new_chunk ? &next_chunk_id : NULL, |
| 4307 | (const struct relayd_stream_rotation_position *) |
| 4308 | stream_rotation_positions.buffer |
| 4309 | .data); |
| 4310 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 4311 | if (ret < 0) { |
| 4312 | ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64, |
| 4313 | relayd->net_seq_idx); |
| 4314 | lttng_consumer_cleanup_relayd(relayd); |
| 4315 | goto end_unlock_channel; |
| 4316 | } |
| 4317 | } |
| 4318 | |
| 4319 | for (stream_idx = 0; |
| 4320 | stream_idx < lttng_dynamic_pointer_array_get_count( |
| 4321 | &streams_packet_to_open); |
| 4322 | stream_idx++) { |
| 4323 | enum consumer_stream_open_packet_status status; |
| 4324 | |
| 4325 | stream = (lttng_consumer_stream *) lttng_dynamic_pointer_array_get_pointer( |
| 4326 | &streams_packet_to_open, stream_idx); |
| 4327 | |
| 4328 | pthread_mutex_lock(&stream->lock); |
| 4329 | status = consumer_stream_open_packet(stream); |
| 4330 | pthread_mutex_unlock(&stream->lock); |
| 4331 | switch (status) { |
| 4332 | case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED: |
| 4333 | DBG("Opened a packet after a rotation: stream id = %" PRIu64 |
| 4334 | ", channel name = %s, session id = %" PRIu64, |
| 4335 | stream->key, stream->chan->name, |
| 4336 | stream->chan->session_id); |
| 4337 | break; |
| 4338 | case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE: |
| 4339 | /* |
| 4340 | * Can't open a packet as there is no space left |
| 4341 | * in the buffer. A new packet will be opened |
| 4342 | * once one has been consumed. |
| 4343 | */ |
| 4344 | DBG("No space left to open a packet after a rotation: stream id = %" PRIu64 |
| 4345 | ", channel name = %s, session id = %" PRIu64, |
| 4346 | stream->key, stream->chan->name, |
| 4347 | stream->chan->session_id); |
| 4348 | break; |
| 4349 | case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR: |
| 4350 | /* Logged by callee. */ |
| 4351 | ret = -1; |
| 4352 | goto end_unlock_channel; |
| 4353 | default: |
| 4354 | abort(); |
| 4355 | } |
| 4356 | } |
| 4357 | |
| 4358 | pthread_mutex_unlock(&channel->lock); |
| 4359 | ret = 0; |
| 4360 | goto end; |
| 4361 | |
| 4362 | end_unlock_stream: |
| 4363 | pthread_mutex_unlock(&stream->lock); |
| 4364 | end_unlock_channel: |
| 4365 | pthread_mutex_unlock(&channel->lock); |
| 4366 | end: |
| 4367 | rcu_read_unlock(); |
| 4368 | lttng_dynamic_array_reset(&stream_rotation_positions); |
| 4369 | lttng_dynamic_pointer_array_reset(&streams_packet_to_open); |
| 4370 | return ret; |
| 4371 | } |
| 4372 | |
| 4373 | static |
| 4374 | int consumer_clear_buffer(struct lttng_consumer_stream *stream) |
| 4375 | { |
| 4376 | int ret = 0; |
| 4377 | unsigned long consumed_pos_before, consumed_pos_after; |
| 4378 | |
| 4379 | ret = lttng_consumer_sample_snapshot_positions(stream); |
| 4380 | if (ret < 0) { |
| 4381 | ERR("Taking snapshot positions"); |
| 4382 | goto end; |
| 4383 | } |
| 4384 | |
| 4385 | ret = lttng_consumer_get_consumed_snapshot(stream, &consumed_pos_before); |
| 4386 | if (ret < 0) { |
| 4387 | ERR("Consumed snapshot position"); |
| 4388 | goto end; |
| 4389 | } |
| 4390 | |
| 4391 | switch (the_consumer_data.type) { |
| 4392 | case LTTNG_CONSUMER_KERNEL: |
| 4393 | ret = kernctl_buffer_clear(stream->wait_fd); |
| 4394 | if (ret < 0) { |
| 4395 | ERR("Failed to clear kernel stream (ret = %d)", ret); |
| 4396 | goto end; |
| 4397 | } |
| 4398 | break; |
| 4399 | case LTTNG_CONSUMER32_UST: |
| 4400 | case LTTNG_CONSUMER64_UST: |
| 4401 | ret = lttng_ustconsumer_clear_buffer(stream); |
| 4402 | if (ret < 0) { |
| 4403 | ERR("Failed to clear ust stream (ret = %d)", ret); |
| 4404 | goto end; |
| 4405 | } |
| 4406 | break; |
| 4407 | default: |
| 4408 | ERR("Unknown consumer_data type"); |
| 4409 | abort(); |
| 4410 | } |
| 4411 | |
| 4412 | ret = lttng_consumer_sample_snapshot_positions(stream); |
| 4413 | if (ret < 0) { |
| 4414 | ERR("Taking snapshot positions"); |
| 4415 | goto end; |
| 4416 | } |
| 4417 | ret = lttng_consumer_get_consumed_snapshot(stream, &consumed_pos_after); |
| 4418 | if (ret < 0) { |
| 4419 | ERR("Consumed snapshot position"); |
| 4420 | goto end; |
| 4421 | } |
| 4422 | DBG("clear: before: %lu after: %lu", consumed_pos_before, consumed_pos_after); |
| 4423 | end: |
| 4424 | return ret; |
| 4425 | } |
| 4426 | |
| 4427 | static |
| 4428 | int consumer_clear_stream(struct lttng_consumer_stream *stream) |
| 4429 | { |
| 4430 | int ret; |
| 4431 | |
| 4432 | ret = consumer_stream_flush_buffer(stream, 1); |
| 4433 | if (ret < 0) { |
| 4434 | ERR("Failed to flush stream %" PRIu64 " during channel clear", |
| 4435 | stream->key); |
| 4436 | ret = LTTCOMM_CONSUMERD_FATAL; |
| 4437 | goto error; |
| 4438 | } |
| 4439 | |
| 4440 | ret = consumer_clear_buffer(stream); |
| 4441 | if (ret < 0) { |
| 4442 | ERR("Failed to clear stream %" PRIu64 " during channel clear", |
| 4443 | stream->key); |
| 4444 | ret = LTTCOMM_CONSUMERD_FATAL; |
| 4445 | goto error; |
| 4446 | } |
| 4447 | |
| 4448 | ret = LTTCOMM_CONSUMERD_SUCCESS; |
| 4449 | error: |
| 4450 | return ret; |
| 4451 | } |
| 4452 | |
| 4453 | static |
| 4454 | int consumer_clear_unmonitored_channel(struct lttng_consumer_channel *channel) |
| 4455 | { |
| 4456 | int ret; |
| 4457 | struct lttng_consumer_stream *stream; |
| 4458 | |
| 4459 | rcu_read_lock(); |
| 4460 | pthread_mutex_lock(&channel->lock); |
| 4461 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { |
| 4462 | health_code_update(); |
| 4463 | pthread_mutex_lock(&stream->lock); |
| 4464 | ret = consumer_clear_stream(stream); |
| 4465 | if (ret) { |
| 4466 | goto error_unlock; |
| 4467 | } |
| 4468 | pthread_mutex_unlock(&stream->lock); |
| 4469 | } |
| 4470 | pthread_mutex_unlock(&channel->lock); |
| 4471 | rcu_read_unlock(); |
| 4472 | return 0; |
| 4473 | |
| 4474 | error_unlock: |
| 4475 | pthread_mutex_unlock(&stream->lock); |
| 4476 | pthread_mutex_unlock(&channel->lock); |
| 4477 | rcu_read_unlock(); |
| 4478 | return ret; |
| 4479 | } |
| 4480 | |
| 4481 | /* |
| 4482 | * Check if a stream is ready to be rotated after extracting it. |
| 4483 | * |
| 4484 | * Return 1 if it is ready for rotation, 0 if it is not, a negative value on |
| 4485 | * error. Stream lock must be held. |
| 4486 | */ |
| 4487 | int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream *stream) |
| 4488 | { |
| 4489 | DBG("Check is rotate ready for stream %" PRIu64 |
| 4490 | " ready %u rotate_position %" PRIu64 |
| 4491 | " last_sequence_number %" PRIu64, |
| 4492 | stream->key, stream->rotate_ready, |
| 4493 | stream->rotate_position, stream->last_sequence_number); |
| 4494 | if (stream->rotate_ready) { |
| 4495 | return 1; |
| 4496 | } |
| 4497 | |
| 4498 | /* |
| 4499 | * If packet seq num is unavailable, it means we are interacting |
| 4500 | * with a pre-2.8 lttng-modules which does not implement the |
| 4501 | * sequence number. Rotation should never be used by sessiond in this |
| 4502 | * scenario. |
| 4503 | */ |
| 4504 | if (stream->sequence_number_unavailable) { |
| 4505 | ERR("Internal error: rotation used on stream %" PRIu64 |
| 4506 | " with unavailable sequence number", |
| 4507 | stream->key); |
| 4508 | return -1; |
| 4509 | } |
| 4510 | |
| 4511 | if (stream->rotate_position == -1ULL || |
| 4512 | stream->last_sequence_number == -1ULL) { |
| 4513 | return 0; |
| 4514 | } |
| 4515 | |
| 4516 | /* |
| 4517 | * Rotate position not reached yet. The stream rotate position is |
| 4518 | * the position of the next packet belonging to the next trace chunk, |
| 4519 | * but consumerd considers rotation ready when reaching the last |
| 4520 | * packet of the current chunk, hence the "rotate_position - 1". |
| 4521 | */ |
| 4522 | |
| 4523 | DBG("Check is rotate ready for stream %" PRIu64 |
| 4524 | " last_sequence_number %" PRIu64 |
| 4525 | " rotate_position %" PRIu64, |
| 4526 | stream->key, stream->last_sequence_number, |
| 4527 | stream->rotate_position); |
| 4528 | if (stream->last_sequence_number >= stream->rotate_position - 1) { |
| 4529 | return 1; |
| 4530 | } |
| 4531 | |
| 4532 | return 0; |
| 4533 | } |
| 4534 | |
| 4535 | /* |
| 4536 | * Reset the state for a stream after a rotation occurred. |
| 4537 | */ |
| 4538 | void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream *stream) |
| 4539 | { |
| 4540 | DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64, |
| 4541 | stream->key); |
| 4542 | stream->rotate_position = -1ULL; |
| 4543 | stream->rotate_ready = false; |
| 4544 | } |
| 4545 | |
| 4546 | /* |
| 4547 | * Perform the rotation a local stream file. |
| 4548 | */ |
| 4549 | static |
| 4550 | int rotate_local_stream(struct lttng_consumer_stream *stream) |
| 4551 | { |
| 4552 | int ret = 0; |
| 4553 | |
| 4554 | DBG("Rotate local stream: stream key %" PRIu64 ", channel key %" PRIu64, |
| 4555 | stream->key, |
| 4556 | stream->chan->key); |
| 4557 | stream->tracefile_size_current = 0; |
| 4558 | stream->tracefile_count_current = 0; |
| 4559 | |
| 4560 | if (stream->out_fd >= 0) { |
| 4561 | ret = close(stream->out_fd); |
| 4562 | if (ret) { |
| 4563 | PERROR("Failed to close stream out_fd of channel \"%s\"", |
| 4564 | stream->chan->name); |
| 4565 | } |
| 4566 | stream->out_fd = -1; |
| 4567 | } |
| 4568 | |
| 4569 | if (stream->index_file) { |
| 4570 | lttng_index_file_put(stream->index_file); |
| 4571 | stream->index_file = NULL; |
| 4572 | } |
| 4573 | |
| 4574 | if (!stream->trace_chunk) { |
| 4575 | goto end; |
| 4576 | } |
| 4577 | |
| 4578 | ret = consumer_stream_create_output_files(stream, true); |
| 4579 | end: |
| 4580 | return ret; |
| 4581 | } |
| 4582 | |
| 4583 | /* |
| 4584 | * Performs the stream rotation for the rotate session feature if needed. |
| 4585 | * It must be called with the channel and stream locks held. |
| 4586 | * |
| 4587 | * Return 0 on success, a negative number of error. |
| 4588 | */ |
| 4589 | int lttng_consumer_rotate_stream(struct lttng_consumer_stream *stream) |
| 4590 | { |
| 4591 | int ret; |
| 4592 | |
| 4593 | DBG("Consumer rotate stream %" PRIu64, stream->key); |
| 4594 | |
| 4595 | /* |
| 4596 | * Update the stream's 'current' chunk to the session's (channel) |
| 4597 | * now-current chunk. |
| 4598 | */ |
| 4599 | lttng_trace_chunk_put(stream->trace_chunk); |
| 4600 | if (stream->chan->trace_chunk == stream->trace_chunk) { |
| 4601 | /* |
| 4602 | * A channel can be rotated and not have a "next" chunk |
| 4603 | * to transition to. In that case, the channel's "current chunk" |
| 4604 | * has not been closed yet, but it has not been updated to |
| 4605 | * a "next" trace chunk either. Hence, the stream, like its |
| 4606 | * parent channel, becomes part of no chunk and can't output |
| 4607 | * anything until a new trace chunk is created. |
| 4608 | */ |
| 4609 | stream->trace_chunk = NULL; |
| 4610 | } else if (stream->chan->trace_chunk && |
| 4611 | !lttng_trace_chunk_get(stream->chan->trace_chunk)) { |
| 4612 | ERR("Failed to acquire a reference to channel's trace chunk during stream rotation"); |
| 4613 | ret = -1; |
| 4614 | goto error; |
| 4615 | } else { |
| 4616 | /* |
| 4617 | * Update the stream's trace chunk to its parent channel's |
| 4618 | * current trace chunk. |
| 4619 | */ |
| 4620 | stream->trace_chunk = stream->chan->trace_chunk; |
| 4621 | } |
| 4622 | |
| 4623 | if (stream->net_seq_idx == (uint64_t) -1ULL) { |
| 4624 | ret = rotate_local_stream(stream); |
| 4625 | if (ret < 0) { |
| 4626 | ERR("Failed to rotate stream, ret = %i", ret); |
| 4627 | goto error; |
| 4628 | } |
| 4629 | } |
| 4630 | |
| 4631 | if (stream->metadata_flag && stream->trace_chunk) { |
| 4632 | /* |
| 4633 | * If the stream has transitioned to a new trace |
| 4634 | * chunk, the metadata should be re-dumped to the |
| 4635 | * newest chunk. |
| 4636 | * |
| 4637 | * However, it is possible for a stream to transition to |
| 4638 | * a "no-chunk" state. This can happen if a rotation |
| 4639 | * occurs on an inactive session. In such cases, the metadata |
| 4640 | * regeneration will happen when the next trace chunk is |
| 4641 | * created. |
| 4642 | */ |
| 4643 | ret = consumer_metadata_stream_dump(stream); |
| 4644 | if (ret) { |
| 4645 | goto error; |
| 4646 | } |
| 4647 | } |
| 4648 | lttng_consumer_reset_stream_rotate_state(stream); |
| 4649 | |
| 4650 | ret = 0; |
| 4651 | |
| 4652 | error: |
| 4653 | return ret; |
| 4654 | } |
| 4655 | |
| 4656 | /* |
| 4657 | * Rotate all the ready streams now. |
| 4658 | * |
| 4659 | * This is especially important for low throughput streams that have already |
| 4660 | * been consumed, we cannot wait for their next packet to perform the |
| 4661 | * rotation. |
| 4662 | * Need to be called with RCU read-side lock held to ensure existence of |
| 4663 | * channel. |
| 4664 | * |
| 4665 | * Returns 0 on success, < 0 on error |
| 4666 | */ |
| 4667 | int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel *channel, |
| 4668 | uint64_t key) |
| 4669 | { |
| 4670 | int ret; |
| 4671 | struct lttng_consumer_stream *stream; |
| 4672 | struct lttng_ht_iter iter; |
| 4673 | struct lttng_ht *ht = the_consumer_data.stream_per_chan_id_ht; |
| 4674 | |
| 4675 | ASSERT_RCU_READ_LOCKED(); |
| 4676 | |
| 4677 | rcu_read_lock(); |
| 4678 | |
| 4679 | DBG("Consumer rotate ready streams in channel %" PRIu64, key); |
| 4680 | |
| 4681 | cds_lfht_for_each_entry_duplicate(ht->ht, |
| 4682 | ht->hash_fct(&channel->key, lttng_ht_seed), |
| 4683 | ht->match_fct, &channel->key, &iter.iter, |
| 4684 | stream, node_channel_id.node) { |
| 4685 | health_code_update(); |
| 4686 | |
| 4687 | pthread_mutex_lock(&stream->chan->lock); |
| 4688 | pthread_mutex_lock(&stream->lock); |
| 4689 | |
| 4690 | if (!stream->rotate_ready) { |
| 4691 | pthread_mutex_unlock(&stream->lock); |
| 4692 | pthread_mutex_unlock(&stream->chan->lock); |
| 4693 | continue; |
| 4694 | } |
| 4695 | DBG("Consumer rotate ready stream %" PRIu64, stream->key); |
| 4696 | |
| 4697 | ret = lttng_consumer_rotate_stream(stream); |
| 4698 | pthread_mutex_unlock(&stream->lock); |
| 4699 | pthread_mutex_unlock(&stream->chan->lock); |
| 4700 | if (ret) { |
| 4701 | goto end; |
| 4702 | } |
| 4703 | } |
| 4704 | |
| 4705 | ret = 0; |
| 4706 | |
| 4707 | end: |
| 4708 | rcu_read_unlock(); |
| 4709 | return ret; |
| 4710 | } |
| 4711 | |
| 4712 | enum lttcomm_return_code lttng_consumer_init_command( |
| 4713 | struct lttng_consumer_local_data *ctx, |
| 4714 | const lttng_uuid& sessiond_uuid) |
| 4715 | { |
| 4716 | enum lttcomm_return_code ret; |
| 4717 | char uuid_str[LTTNG_UUID_STR_LEN]; |
| 4718 | |
| 4719 | if (ctx->sessiond_uuid.is_set) { |
| 4720 | ret = LTTCOMM_CONSUMERD_ALREADY_SET; |
| 4721 | goto end; |
| 4722 | } |
| 4723 | |
| 4724 | ctx->sessiond_uuid.is_set = true; |
| 4725 | ctx->sessiond_uuid.value = sessiond_uuid; |
| 4726 | ret = LTTCOMM_CONSUMERD_SUCCESS; |
| 4727 | lttng_uuid_to_str(sessiond_uuid, uuid_str); |
| 4728 | DBG("Received session daemon UUID: %s", uuid_str); |
| 4729 | end: |
| 4730 | return ret; |
| 4731 | } |
| 4732 | |
| 4733 | enum lttcomm_return_code lttng_consumer_create_trace_chunk( |
| 4734 | const uint64_t *relayd_id, uint64_t session_id, |
| 4735 | uint64_t chunk_id, |
| 4736 | time_t chunk_creation_timestamp, |
| 4737 | const char *chunk_override_name, |
| 4738 | const struct lttng_credentials *credentials, |
| 4739 | struct lttng_directory_handle *chunk_directory_handle) |
| 4740 | { |
| 4741 | int ret; |
| 4742 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
| 4743 | struct lttng_trace_chunk *created_chunk = NULL, *published_chunk = NULL; |
| 4744 | enum lttng_trace_chunk_status chunk_status; |
| 4745 | char relayd_id_buffer[MAX_INT_DEC_LEN(*relayd_id)]; |
| 4746 | char creation_timestamp_buffer[ISO8601_STR_LEN]; |
| 4747 | const char *relayd_id_str = "(none)"; |
| 4748 | const char *creation_timestamp_str; |
| 4749 | struct lttng_ht_iter iter; |
| 4750 | struct lttng_consumer_channel *channel; |
| 4751 | |
| 4752 | if (relayd_id) { |
| 4753 | /* Only used for logging purposes. */ |
| 4754 | ret = snprintf(relayd_id_buffer, sizeof(relayd_id_buffer), |
| 4755 | "%" PRIu64, *relayd_id); |
| 4756 | if (ret > 0 && ret < sizeof(relayd_id_buffer)) { |
| 4757 | relayd_id_str = relayd_id_buffer; |
| 4758 | } else { |
| 4759 | relayd_id_str = "(formatting error)"; |
| 4760 | } |
| 4761 | } |
| 4762 | |
| 4763 | /* Local protocol error. */ |
| 4764 | LTTNG_ASSERT(chunk_creation_timestamp); |
| 4765 | ret = time_to_iso8601_str(chunk_creation_timestamp, |
| 4766 | creation_timestamp_buffer, |
| 4767 | sizeof(creation_timestamp_buffer)); |
| 4768 | creation_timestamp_str = !ret ? creation_timestamp_buffer : |
| 4769 | "(formatting error)"; |
| 4770 | |
| 4771 | DBG("Consumer create trace chunk command: relay_id = %s" |
| 4772 | ", session_id = %" PRIu64 ", chunk_id = %" PRIu64 |
| 4773 | ", chunk_override_name = %s" |
| 4774 | ", chunk_creation_timestamp = %s", |
| 4775 | relayd_id_str, session_id, chunk_id, |
| 4776 | chunk_override_name ? : "(none)", |
| 4777 | creation_timestamp_str); |
| 4778 | |
| 4779 | /* |
| 4780 | * The trace chunk registry, as used by the consumer daemon, implicitly |
| 4781 | * owns the trace chunks. This is only needed in the consumer since |
| 4782 | * the consumer has no notion of a session beyond session IDs being |
| 4783 | * used to identify other objects. |
| 4784 | * |
| 4785 | * The lttng_trace_chunk_registry_publish() call below provides a |
| 4786 | * reference which is not released; it implicitly becomes the session |
| 4787 | * daemon's reference to the chunk in the consumer daemon. |
| 4788 | * |
| 4789 | * The lifetime of trace chunks in the consumer daemon is managed by |
| 4790 | * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK |
| 4791 | * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands. |
| 4792 | */ |
| 4793 | created_chunk = lttng_trace_chunk_create(chunk_id, |
| 4794 | chunk_creation_timestamp, NULL); |
| 4795 | if (!created_chunk) { |
| 4796 | ERR("Failed to create trace chunk"); |
| 4797 | ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED; |
| 4798 | goto error; |
| 4799 | } |
| 4800 | |
| 4801 | if (chunk_override_name) { |
| 4802 | chunk_status = lttng_trace_chunk_override_name(created_chunk, |
| 4803 | chunk_override_name); |
| 4804 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 4805 | ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED; |
| 4806 | goto error; |
| 4807 | } |
| 4808 | } |
| 4809 | |
| 4810 | if (chunk_directory_handle) { |
| 4811 | chunk_status = lttng_trace_chunk_set_credentials(created_chunk, |
| 4812 | credentials); |
| 4813 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 4814 | ERR("Failed to set trace chunk credentials"); |
| 4815 | ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED; |
| 4816 | goto error; |
| 4817 | } |
| 4818 | /* |
| 4819 | * The consumer daemon has no ownership of the chunk output |
| 4820 | * directory. |
| 4821 | */ |
| 4822 | chunk_status = lttng_trace_chunk_set_as_user(created_chunk, |
| 4823 | chunk_directory_handle); |
| 4824 | chunk_directory_handle = NULL; |
| 4825 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 4826 | ERR("Failed to set trace chunk's directory handle"); |
| 4827 | ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED; |
| 4828 | goto error; |
| 4829 | } |
| 4830 | } |
| 4831 | |
| 4832 | published_chunk = lttng_trace_chunk_registry_publish_chunk( |
| 4833 | the_consumer_data.chunk_registry, session_id, |
| 4834 | created_chunk); |
| 4835 | lttng_trace_chunk_put(created_chunk); |
| 4836 | created_chunk = NULL; |
| 4837 | if (!published_chunk) { |
| 4838 | ERR("Failed to publish trace chunk"); |
| 4839 | ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED; |
| 4840 | goto error; |
| 4841 | } |
| 4842 | |
| 4843 | rcu_read_lock(); |
| 4844 | cds_lfht_for_each_entry_duplicate( |
| 4845 | the_consumer_data.channels_by_session_id_ht->ht, |
| 4846 | the_consumer_data.channels_by_session_id_ht->hash_fct( |
| 4847 | &session_id, lttng_ht_seed), |
| 4848 | the_consumer_data.channels_by_session_id_ht->match_fct, |
| 4849 | &session_id, &iter.iter, channel, |
| 4850 | channels_by_session_id_ht_node.node) { |
| 4851 | ret = lttng_consumer_channel_set_trace_chunk(channel, |
| 4852 | published_chunk); |
| 4853 | if (ret) { |
| 4854 | /* |
| 4855 | * Roll-back the creation of this chunk. |
| 4856 | * |
| 4857 | * This is important since the session daemon will |
| 4858 | * assume that the creation of this chunk failed and |
| 4859 | * will never ask for it to be closed, resulting |
| 4860 | * in a leak and an inconsistent state for some |
| 4861 | * channels. |
| 4862 | */ |
| 4863 | enum lttcomm_return_code close_ret; |
| 4864 | char path[LTTNG_PATH_MAX]; |
| 4865 | |
| 4866 | DBG("Failed to set new trace chunk on existing channels, rolling back"); |
| 4867 | close_ret = lttng_consumer_close_trace_chunk(relayd_id, |
| 4868 | session_id, chunk_id, |
| 4869 | chunk_creation_timestamp, NULL, |
| 4870 | path); |
| 4871 | if (close_ret != LTTCOMM_CONSUMERD_SUCCESS) { |
| 4872 | ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64 ", chunk_id = %" PRIu64, |
| 4873 | session_id, chunk_id); |
| 4874 | } |
| 4875 | |
| 4876 | ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED; |
| 4877 | break; |
| 4878 | } |
| 4879 | } |
| 4880 | |
| 4881 | if (relayd_id) { |
| 4882 | struct consumer_relayd_sock_pair *relayd; |
| 4883 | |
| 4884 | relayd = consumer_find_relayd(*relayd_id); |
| 4885 | if (relayd) { |
| 4886 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 4887 | ret = relayd_create_trace_chunk( |
| 4888 | &relayd->control_sock, published_chunk); |
| 4889 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 4890 | } else { |
| 4891 | ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64, *relayd_id); |
| 4892 | } |
| 4893 | |
| 4894 | if (!relayd || ret) { |
| 4895 | enum lttcomm_return_code close_ret; |
| 4896 | char path[LTTNG_PATH_MAX]; |
| 4897 | |
| 4898 | close_ret = lttng_consumer_close_trace_chunk(relayd_id, |
| 4899 | session_id, |
| 4900 | chunk_id, |
| 4901 | chunk_creation_timestamp, |
| 4902 | NULL, path); |
| 4903 | if (close_ret != LTTCOMM_CONSUMERD_SUCCESS) { |
| 4904 | ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64 ", chunk_id = %" PRIu64, |
| 4905 | session_id, |
| 4906 | chunk_id); |
| 4907 | } |
| 4908 | |
| 4909 | ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED; |
| 4910 | goto error_unlock; |
| 4911 | } |
| 4912 | } |
| 4913 | error_unlock: |
| 4914 | rcu_read_unlock(); |
| 4915 | error: |
| 4916 | /* Release the reference returned by the "publish" operation. */ |
| 4917 | lttng_trace_chunk_put(published_chunk); |
| 4918 | lttng_trace_chunk_put(created_chunk); |
| 4919 | return ret_code; |
| 4920 | } |
| 4921 | |
| 4922 | enum lttcomm_return_code lttng_consumer_close_trace_chunk( |
| 4923 | const uint64_t *relayd_id, uint64_t session_id, |
| 4924 | uint64_t chunk_id, time_t chunk_close_timestamp, |
| 4925 | const enum lttng_trace_chunk_command_type *close_command, |
| 4926 | char *path) |
| 4927 | { |
| 4928 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
| 4929 | struct lttng_trace_chunk *chunk; |
| 4930 | char relayd_id_buffer[MAX_INT_DEC_LEN(*relayd_id)]; |
| 4931 | const char *relayd_id_str = "(none)"; |
| 4932 | const char *close_command_name = "none"; |
| 4933 | struct lttng_ht_iter iter; |
| 4934 | struct lttng_consumer_channel *channel; |
| 4935 | enum lttng_trace_chunk_status chunk_status; |
| 4936 | |
| 4937 | if (relayd_id) { |
| 4938 | int ret; |
| 4939 | |
| 4940 | /* Only used for logging purposes. */ |
| 4941 | ret = snprintf(relayd_id_buffer, sizeof(relayd_id_buffer), |
| 4942 | "%" PRIu64, *relayd_id); |
| 4943 | if (ret > 0 && ret < sizeof(relayd_id_buffer)) { |
| 4944 | relayd_id_str = relayd_id_buffer; |
| 4945 | } else { |
| 4946 | relayd_id_str = "(formatting error)"; |
| 4947 | } |
| 4948 | } |
| 4949 | if (close_command) { |
| 4950 | close_command_name = lttng_trace_chunk_command_type_get_name( |
| 4951 | *close_command); |
| 4952 | } |
| 4953 | |
| 4954 | DBG("Consumer close trace chunk command: relayd_id = %s" |
| 4955 | ", session_id = %" PRIu64 ", chunk_id = %" PRIu64 |
| 4956 | ", close command = %s", |
| 4957 | relayd_id_str, session_id, chunk_id, |
| 4958 | close_command_name); |
| 4959 | |
| 4960 | chunk = lttng_trace_chunk_registry_find_chunk( |
| 4961 | the_consumer_data.chunk_registry, session_id, chunk_id); |
| 4962 | if (!chunk) { |
| 4963 | ERR("Failed to find chunk: session_id = %" PRIu64 |
| 4964 | ", chunk_id = %" PRIu64, |
| 4965 | session_id, chunk_id); |
| 4966 | ret_code = LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK; |
| 4967 | goto end; |
| 4968 | } |
| 4969 | |
| 4970 | chunk_status = lttng_trace_chunk_set_close_timestamp(chunk, |
| 4971 | chunk_close_timestamp); |
| 4972 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 4973 | ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED; |
| 4974 | goto end; |
| 4975 | } |
| 4976 | |
| 4977 | if (close_command) { |
| 4978 | chunk_status = lttng_trace_chunk_set_close_command( |
| 4979 | chunk, *close_command); |
| 4980 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 4981 | ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED; |
| 4982 | goto end; |
| 4983 | } |
| 4984 | } |
| 4985 | |
| 4986 | /* |
| 4987 | * chunk is now invalid to access as we no longer hold a reference to |
| 4988 | * it; it is only kept around to compare it (by address) to the |
| 4989 | * current chunk found in the session's channels. |
| 4990 | */ |
| 4991 | rcu_read_lock(); |
| 4992 | cds_lfht_for_each_entry(the_consumer_data.channel_ht->ht, &iter.iter, |
| 4993 | channel, node.node) { |
| 4994 | int ret; |
| 4995 | |
| 4996 | /* |
| 4997 | * Only change the channel's chunk to NULL if it still |
| 4998 | * references the chunk being closed. The channel may |
| 4999 | * reference a newer channel in the case of a session |
| 5000 | * rotation. When a session rotation occurs, the "next" |
| 5001 | * chunk is created before the "current" chunk is closed. |
| 5002 | */ |
| 5003 | if (channel->trace_chunk != chunk) { |
| 5004 | continue; |
| 5005 | } |
| 5006 | ret = lttng_consumer_channel_set_trace_chunk(channel, NULL); |
| 5007 | if (ret) { |
| 5008 | /* |
| 5009 | * Attempt to close the chunk on as many channels as |
| 5010 | * possible. |
| 5011 | */ |
| 5012 | ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED; |
| 5013 | } |
| 5014 | } |
| 5015 | |
| 5016 | if (relayd_id) { |
| 5017 | int ret; |
| 5018 | struct consumer_relayd_sock_pair *relayd; |
| 5019 | |
| 5020 | relayd = consumer_find_relayd(*relayd_id); |
| 5021 | if (relayd) { |
| 5022 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 5023 | ret = relayd_close_trace_chunk( |
| 5024 | &relayd->control_sock, chunk, |
| 5025 | path); |
| 5026 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 5027 | } else { |
| 5028 | ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64, |
| 5029 | *relayd_id); |
| 5030 | } |
| 5031 | |
| 5032 | if (!relayd || ret) { |
| 5033 | ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED; |
| 5034 | goto error_unlock; |
| 5035 | } |
| 5036 | } |
| 5037 | error_unlock: |
| 5038 | rcu_read_unlock(); |
| 5039 | end: |
| 5040 | /* |
| 5041 | * Release the reference returned by the "find" operation and |
| 5042 | * the session daemon's implicit reference to the chunk. |
| 5043 | */ |
| 5044 | lttng_trace_chunk_put(chunk); |
| 5045 | lttng_trace_chunk_put(chunk); |
| 5046 | |
| 5047 | return ret_code; |
| 5048 | } |
| 5049 | |
| 5050 | enum lttcomm_return_code lttng_consumer_trace_chunk_exists( |
| 5051 | const uint64_t *relayd_id, uint64_t session_id, |
| 5052 | uint64_t chunk_id) |
| 5053 | { |
| 5054 | int ret; |
| 5055 | enum lttcomm_return_code ret_code; |
| 5056 | char relayd_id_buffer[MAX_INT_DEC_LEN(*relayd_id)]; |
| 5057 | const char *relayd_id_str = "(none)"; |
| 5058 | const bool is_local_trace = !relayd_id; |
| 5059 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 5060 | bool chunk_exists_local, chunk_exists_remote; |
| 5061 | |
| 5062 | if (relayd_id) { |
| 5063 | /* Only used for logging purposes. */ |
| 5064 | ret = snprintf(relayd_id_buffer, sizeof(relayd_id_buffer), |
| 5065 | "%" PRIu64, *relayd_id); |
| 5066 | if (ret > 0 && ret < sizeof(relayd_id_buffer)) { |
| 5067 | relayd_id_str = relayd_id_buffer; |
| 5068 | } else { |
| 5069 | relayd_id_str = "(formatting error)"; |
| 5070 | } |
| 5071 | } |
| 5072 | |
| 5073 | DBG("Consumer trace chunk exists command: relayd_id = %s" |
| 5074 | ", chunk_id = %" PRIu64, relayd_id_str, |
| 5075 | chunk_id); |
| 5076 | ret = lttng_trace_chunk_registry_chunk_exists( |
| 5077 | the_consumer_data.chunk_registry, session_id, chunk_id, |
| 5078 | &chunk_exists_local); |
| 5079 | if (ret) { |
| 5080 | /* Internal error. */ |
| 5081 | ERR("Failed to query the existence of a trace chunk"); |
| 5082 | ret_code = LTTCOMM_CONSUMERD_FATAL; |
| 5083 | goto end; |
| 5084 | } |
| 5085 | DBG("Trace chunk %s locally", |
| 5086 | chunk_exists_local ? "exists" : "does not exist"); |
| 5087 | if (chunk_exists_local) { |
| 5088 | ret_code = LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL; |
| 5089 | goto end; |
| 5090 | } else if (is_local_trace) { |
| 5091 | ret_code = LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK; |
| 5092 | goto end; |
| 5093 | } |
| 5094 | |
| 5095 | rcu_read_lock(); |
| 5096 | relayd = consumer_find_relayd(*relayd_id); |
| 5097 | if (!relayd) { |
| 5098 | ERR("Failed to find relayd %" PRIu64, *relayd_id); |
| 5099 | ret_code = LTTCOMM_CONSUMERD_INVALID_PARAMETERS; |
| 5100 | goto end_rcu_unlock; |
| 5101 | } |
| 5102 | DBG("Looking up existence of trace chunk on relay daemon"); |
| 5103 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 5104 | ret = relayd_trace_chunk_exists(&relayd->control_sock, chunk_id, |
| 5105 | &chunk_exists_remote); |
| 5106 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 5107 | if (ret < 0) { |
| 5108 | ERR("Failed to look-up the existence of trace chunk on relay daemon"); |
| 5109 | ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL; |
| 5110 | goto end_rcu_unlock; |
| 5111 | } |
| 5112 | |
| 5113 | ret_code = chunk_exists_remote ? |
| 5114 | LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE : |
| 5115 | LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK; |
| 5116 | DBG("Trace chunk %s on relay daemon", |
| 5117 | chunk_exists_remote ? "exists" : "does not exist"); |
| 5118 | |
| 5119 | end_rcu_unlock: |
| 5120 | rcu_read_unlock(); |
| 5121 | end: |
| 5122 | return ret_code; |
| 5123 | } |
| 5124 | |
| 5125 | static |
| 5126 | int consumer_clear_monitored_channel(struct lttng_consumer_channel *channel) |
| 5127 | { |
| 5128 | struct lttng_ht *ht; |
| 5129 | struct lttng_consumer_stream *stream; |
| 5130 | struct lttng_ht_iter iter; |
| 5131 | int ret; |
| 5132 | |
| 5133 | ht = the_consumer_data.stream_per_chan_id_ht; |
| 5134 | |
| 5135 | rcu_read_lock(); |
| 5136 | cds_lfht_for_each_entry_duplicate(ht->ht, |
| 5137 | ht->hash_fct(&channel->key, lttng_ht_seed), |
| 5138 | ht->match_fct, &channel->key, |
| 5139 | &iter.iter, stream, node_channel_id.node) { |
| 5140 | /* |
| 5141 | * Protect against teardown with mutex. |
| 5142 | */ |
| 5143 | pthread_mutex_lock(&stream->lock); |
| 5144 | if (cds_lfht_is_node_deleted(&stream->node.node)) { |
| 5145 | goto next; |
| 5146 | } |
| 5147 | ret = consumer_clear_stream(stream); |
| 5148 | if (ret) { |
| 5149 | goto error_unlock; |
| 5150 | } |
| 5151 | next: |
| 5152 | pthread_mutex_unlock(&stream->lock); |
| 5153 | } |
| 5154 | rcu_read_unlock(); |
| 5155 | return LTTCOMM_CONSUMERD_SUCCESS; |
| 5156 | |
| 5157 | error_unlock: |
| 5158 | pthread_mutex_unlock(&stream->lock); |
| 5159 | rcu_read_unlock(); |
| 5160 | return ret; |
| 5161 | } |
| 5162 | |
| 5163 | int lttng_consumer_clear_channel(struct lttng_consumer_channel *channel) |
| 5164 | { |
| 5165 | int ret; |
| 5166 | |
| 5167 | DBG("Consumer clear channel %" PRIu64, channel->key); |
| 5168 | |
| 5169 | if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) { |
| 5170 | /* |
| 5171 | * Nothing to do for the metadata channel/stream. |
| 5172 | * Snapshot mechanism already take care of the metadata |
| 5173 | * handling/generation, and monitored channels only need to |
| 5174 | * have their data stream cleared.. |
| 5175 | */ |
| 5176 | ret = LTTCOMM_CONSUMERD_SUCCESS; |
| 5177 | goto end; |
| 5178 | } |
| 5179 | |
| 5180 | if (!channel->monitor) { |
| 5181 | ret = consumer_clear_unmonitored_channel(channel); |
| 5182 | } else { |
| 5183 | ret = consumer_clear_monitored_channel(channel); |
| 5184 | } |
| 5185 | end: |
| 5186 | return ret; |
| 5187 | } |
| 5188 | |
| 5189 | enum lttcomm_return_code lttng_consumer_open_channel_packets( |
| 5190 | struct lttng_consumer_channel *channel) |
| 5191 | { |
| 5192 | struct lttng_consumer_stream *stream; |
| 5193 | enum lttcomm_return_code ret = LTTCOMM_CONSUMERD_SUCCESS; |
| 5194 | |
| 5195 | if (channel->metadata_stream) { |
| 5196 | ERR("Open channel packets command attempted on a metadata channel"); |
| 5197 | ret = LTTCOMM_CONSUMERD_INVALID_PARAMETERS; |
| 5198 | goto end; |
| 5199 | } |
| 5200 | |
| 5201 | rcu_read_lock(); |
| 5202 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { |
| 5203 | enum consumer_stream_open_packet_status status; |
| 5204 | |
| 5205 | pthread_mutex_lock(&stream->lock); |
| 5206 | if (cds_lfht_is_node_deleted(&stream->node.node)) { |
| 5207 | goto next; |
| 5208 | } |
| 5209 | |
| 5210 | status = consumer_stream_open_packet(stream); |
| 5211 | switch (status) { |
| 5212 | case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED: |
| 5213 | DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64 |
| 5214 | ", channel name = %s, session id = %" PRIu64, |
| 5215 | stream->key, stream->chan->name, |
| 5216 | stream->chan->session_id); |
| 5217 | stream->opened_packet_in_current_trace_chunk = true; |
| 5218 | break; |
| 5219 | case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE: |
| 5220 | DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64 |
| 5221 | ", channel name = %s, session id = %" PRIu64, |
| 5222 | stream->key, stream->chan->name, |
| 5223 | stream->chan->session_id); |
| 5224 | break; |
| 5225 | case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR: |
| 5226 | /* |
| 5227 | * Only unexpected internal errors can lead to this |
| 5228 | * failing. Report an unknown error. |
| 5229 | */ |
| 5230 | ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64 |
| 5231 | ", channel id = %" PRIu64 |
| 5232 | ", channel name = %s" |
| 5233 | ", session id = %" PRIu64, |
| 5234 | stream->key, channel->key, |
| 5235 | channel->name, channel->session_id); |
| 5236 | ret = LTTCOMM_CONSUMERD_UNKNOWN_ERROR; |
| 5237 | goto error_unlock; |
| 5238 | default: |
| 5239 | abort(); |
| 5240 | } |
| 5241 | |
| 5242 | next: |
| 5243 | pthread_mutex_unlock(&stream->lock); |
| 5244 | } |
| 5245 | |
| 5246 | end_rcu_unlock: |
| 5247 | rcu_read_unlock(); |
| 5248 | end: |
| 5249 | return ret; |
| 5250 | |
| 5251 | error_unlock: |
| 5252 | pthread_mutex_unlock(&stream->lock); |
| 5253 | goto end_rcu_unlock; |
| 5254 | } |
| 5255 | |
| 5256 | void lttng_consumer_sigbus_handle(void *addr) |
| 5257 | { |
| 5258 | lttng_ustconsumer_sigbus_handle(addr); |
| 5259 | } |