| 1 | /* |
| 2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * 2012 - David Goulet <dgoulet@efficios.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License, version 2 only, |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #define _GNU_SOURCE |
| 21 | #include <assert.h> |
| 22 | #include <poll.h> |
| 23 | #include <pthread.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/mman.h> |
| 27 | #include <sys/socket.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <unistd.h> |
| 30 | #include <inttypes.h> |
| 31 | #include <signal.h> |
| 32 | |
| 33 | #include <common/common.h> |
| 34 | #include <common/utils.h> |
| 35 | #include <common/compat/poll.h> |
| 36 | #include <common/kernel-ctl/kernel-ctl.h> |
| 37 | #include <common/sessiond-comm/relayd.h> |
| 38 | #include <common/sessiond-comm/sessiond-comm.h> |
| 39 | #include <common/kernel-consumer/kernel-consumer.h> |
| 40 | #include <common/relayd/relayd.h> |
| 41 | #include <common/ust-consumer/ust-consumer.h> |
| 42 | |
| 43 | #include "consumer.h" |
| 44 | |
| 45 | struct lttng_consumer_global_data consumer_data = { |
| 46 | .stream_count = 0, |
| 47 | .need_update = 1, |
| 48 | .type = LTTNG_CONSUMER_UNKNOWN, |
| 49 | }; |
| 50 | |
| 51 | enum consumer_channel_action { |
| 52 | CONSUMER_CHANNEL_ADD, |
| 53 | CONSUMER_CHANNEL_DEL, |
| 54 | CONSUMER_CHANNEL_QUIT, |
| 55 | }; |
| 56 | |
| 57 | struct consumer_channel_msg { |
| 58 | enum consumer_channel_action action; |
| 59 | struct lttng_consumer_channel *chan; /* add */ |
| 60 | uint64_t key; /* del */ |
| 61 | }; |
| 62 | |
| 63 | /* |
| 64 | * Flag to inform the polling thread to quit when all fd hung up. Updated by |
| 65 | * the consumer_thread_receive_fds when it notices that all fds has hung up. |
| 66 | * Also updated by the signal handler (consumer_should_exit()). Read by the |
| 67 | * polling threads. |
| 68 | */ |
| 69 | volatile int consumer_quit; |
| 70 | |
| 71 | /* |
| 72 | * Global hash table containing respectively metadata and data streams. The |
| 73 | * stream element in this ht should only be updated by the metadata poll thread |
| 74 | * for the metadata and the data poll thread for the data. |
| 75 | */ |
| 76 | static struct lttng_ht *metadata_ht; |
| 77 | static struct lttng_ht *data_ht; |
| 78 | |
| 79 | /* |
| 80 | * Notify a thread lttng pipe to poll back again. This usually means that some |
| 81 | * global state has changed so we just send back the thread in a poll wait |
| 82 | * call. |
| 83 | */ |
| 84 | static void notify_thread_lttng_pipe(struct lttng_pipe *pipe) |
| 85 | { |
| 86 | struct lttng_consumer_stream *null_stream = NULL; |
| 87 | |
| 88 | assert(pipe); |
| 89 | |
| 90 | (void) lttng_pipe_write(pipe, &null_stream, sizeof(null_stream)); |
| 91 | } |
| 92 | |
| 93 | static void notify_channel_pipe(struct lttng_consumer_local_data *ctx, |
| 94 | struct lttng_consumer_channel *chan, |
| 95 | uint64_t key, |
| 96 | enum consumer_channel_action action) |
| 97 | { |
| 98 | struct consumer_channel_msg msg; |
| 99 | int ret; |
| 100 | |
| 101 | memset(&msg, 0, sizeof(msg)); |
| 102 | |
| 103 | msg.action = action; |
| 104 | msg.chan = chan; |
| 105 | msg.key = key; |
| 106 | do { |
| 107 | ret = write(ctx->consumer_channel_pipe[1], &msg, sizeof(msg)); |
| 108 | } while (ret < 0 && errno == EINTR); |
| 109 | } |
| 110 | |
| 111 | void notify_thread_del_channel(struct lttng_consumer_local_data *ctx, |
| 112 | uint64_t key) |
| 113 | { |
| 114 | notify_channel_pipe(ctx, NULL, key, CONSUMER_CHANNEL_DEL); |
| 115 | } |
| 116 | |
| 117 | static int read_channel_pipe(struct lttng_consumer_local_data *ctx, |
| 118 | struct lttng_consumer_channel **chan, |
| 119 | uint64_t *key, |
| 120 | enum consumer_channel_action *action) |
| 121 | { |
| 122 | struct consumer_channel_msg msg; |
| 123 | int ret; |
| 124 | |
| 125 | do { |
| 126 | ret = read(ctx->consumer_channel_pipe[0], &msg, sizeof(msg)); |
| 127 | } while (ret < 0 && errno == EINTR); |
| 128 | if (ret > 0) { |
| 129 | *action = msg.action; |
| 130 | *chan = msg.chan; |
| 131 | *key = msg.key; |
| 132 | } |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Find a stream. The consumer_data.lock must be locked during this |
| 138 | * call. |
| 139 | */ |
| 140 | static struct lttng_consumer_stream *find_stream(uint64_t key, |
| 141 | struct lttng_ht *ht) |
| 142 | { |
| 143 | struct lttng_ht_iter iter; |
| 144 | struct lttng_ht_node_u64 *node; |
| 145 | struct lttng_consumer_stream *stream = NULL; |
| 146 | |
| 147 | assert(ht); |
| 148 | |
| 149 | /* -1ULL keys are lookup failures */ |
| 150 | if (key == (uint64_t) -1ULL) { |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | rcu_read_lock(); |
| 155 | |
| 156 | lttng_ht_lookup(ht, &key, &iter); |
| 157 | node = lttng_ht_iter_get_node_u64(&iter); |
| 158 | if (node != NULL) { |
| 159 | stream = caa_container_of(node, struct lttng_consumer_stream, node); |
| 160 | } |
| 161 | |
| 162 | rcu_read_unlock(); |
| 163 | |
| 164 | return stream; |
| 165 | } |
| 166 | |
| 167 | static void steal_stream_key(uint64_t key, struct lttng_ht *ht) |
| 168 | { |
| 169 | struct lttng_consumer_stream *stream; |
| 170 | |
| 171 | rcu_read_lock(); |
| 172 | stream = find_stream(key, ht); |
| 173 | if (stream) { |
| 174 | stream->key = (uint64_t) -1ULL; |
| 175 | /* |
| 176 | * We don't want the lookup to match, but we still need |
| 177 | * to iterate on this stream when iterating over the hash table. Just |
| 178 | * change the node key. |
| 179 | */ |
| 180 | stream->node.key = (uint64_t) -1ULL; |
| 181 | } |
| 182 | rcu_read_unlock(); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Return a channel object for the given key. |
| 187 | * |
| 188 | * RCU read side lock MUST be acquired before calling this function and |
| 189 | * protects the channel ptr. |
| 190 | */ |
| 191 | struct lttng_consumer_channel *consumer_find_channel(uint64_t key) |
| 192 | { |
| 193 | struct lttng_ht_iter iter; |
| 194 | struct lttng_ht_node_u64 *node; |
| 195 | struct lttng_consumer_channel *channel = NULL; |
| 196 | |
| 197 | /* -1ULL keys are lookup failures */ |
| 198 | if (key == (uint64_t) -1ULL) { |
| 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | lttng_ht_lookup(consumer_data.channel_ht, &key, &iter); |
| 203 | node = lttng_ht_iter_get_node_u64(&iter); |
| 204 | if (node != NULL) { |
| 205 | channel = caa_container_of(node, struct lttng_consumer_channel, node); |
| 206 | } |
| 207 | |
| 208 | return channel; |
| 209 | } |
| 210 | |
| 211 | static void free_stream_rcu(struct rcu_head *head) |
| 212 | { |
| 213 | struct lttng_ht_node_u64 *node = |
| 214 | caa_container_of(head, struct lttng_ht_node_u64, head); |
| 215 | struct lttng_consumer_stream *stream = |
| 216 | caa_container_of(node, struct lttng_consumer_stream, node); |
| 217 | |
| 218 | free(stream); |
| 219 | } |
| 220 | |
| 221 | static void free_channel_rcu(struct rcu_head *head) |
| 222 | { |
| 223 | struct lttng_ht_node_u64 *node = |
| 224 | caa_container_of(head, struct lttng_ht_node_u64, head); |
| 225 | struct lttng_consumer_channel *channel = |
| 226 | caa_container_of(node, struct lttng_consumer_channel, node); |
| 227 | |
| 228 | free(channel); |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * RCU protected relayd socket pair free. |
| 233 | */ |
| 234 | static void free_relayd_rcu(struct rcu_head *head) |
| 235 | { |
| 236 | struct lttng_ht_node_u64 *node = |
| 237 | caa_container_of(head, struct lttng_ht_node_u64, head); |
| 238 | struct consumer_relayd_sock_pair *relayd = |
| 239 | caa_container_of(node, struct consumer_relayd_sock_pair, node); |
| 240 | |
| 241 | /* |
| 242 | * Close all sockets. This is done in the call RCU since we don't want the |
| 243 | * socket fds to be reassigned thus potentially creating bad state of the |
| 244 | * relayd object. |
| 245 | * |
| 246 | * We do not have to lock the control socket mutex here since at this stage |
| 247 | * there is no one referencing to this relayd object. |
| 248 | */ |
| 249 | (void) relayd_close(&relayd->control_sock); |
| 250 | (void) relayd_close(&relayd->data_sock); |
| 251 | |
| 252 | free(relayd); |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Destroy and free relayd socket pair object. |
| 257 | * |
| 258 | * This function MUST be called with the consumer_data lock acquired. |
| 259 | */ |
| 260 | static void destroy_relayd(struct consumer_relayd_sock_pair *relayd) |
| 261 | { |
| 262 | int ret; |
| 263 | struct lttng_ht_iter iter; |
| 264 | |
| 265 | if (relayd == NULL) { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | DBG("Consumer destroy and close relayd socket pair"); |
| 270 | |
| 271 | iter.iter.node = &relayd->node.node; |
| 272 | ret = lttng_ht_del(consumer_data.relayd_ht, &iter); |
| 273 | if (ret != 0) { |
| 274 | /* We assume the relayd is being or is destroyed */ |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | /* RCU free() call */ |
| 279 | call_rcu(&relayd->node.head, free_relayd_rcu); |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Remove a channel from the global list protected by a mutex. This function is |
| 284 | * also responsible for freeing its data structures. |
| 285 | */ |
| 286 | void consumer_del_channel(struct lttng_consumer_channel *channel) |
| 287 | { |
| 288 | int ret; |
| 289 | struct lttng_ht_iter iter; |
| 290 | struct lttng_consumer_stream *stream, *stmp; |
| 291 | |
| 292 | DBG("Consumer delete channel key %" PRIu64, channel->key); |
| 293 | |
| 294 | pthread_mutex_lock(&consumer_data.lock); |
| 295 | |
| 296 | switch (consumer_data.type) { |
| 297 | case LTTNG_CONSUMER_KERNEL: |
| 298 | break; |
| 299 | case LTTNG_CONSUMER32_UST: |
| 300 | case LTTNG_CONSUMER64_UST: |
| 301 | /* Delete streams that might have been left in the stream list. */ |
| 302 | cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head, |
| 303 | send_node) { |
| 304 | cds_list_del(&stream->send_node); |
| 305 | lttng_ustconsumer_del_stream(stream); |
| 306 | free(stream); |
| 307 | } |
| 308 | lttng_ustconsumer_del_channel(channel); |
| 309 | break; |
| 310 | default: |
| 311 | ERR("Unknown consumer_data type"); |
| 312 | assert(0); |
| 313 | goto end; |
| 314 | } |
| 315 | |
| 316 | rcu_read_lock(); |
| 317 | iter.iter.node = &channel->node.node; |
| 318 | ret = lttng_ht_del(consumer_data.channel_ht, &iter); |
| 319 | assert(!ret); |
| 320 | rcu_read_unlock(); |
| 321 | |
| 322 | call_rcu(&channel->node.head, free_channel_rcu); |
| 323 | end: |
| 324 | pthread_mutex_unlock(&consumer_data.lock); |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Iterate over the relayd hash table and destroy each element. Finally, |
| 329 | * destroy the whole hash table. |
| 330 | */ |
| 331 | static void cleanup_relayd_ht(void) |
| 332 | { |
| 333 | struct lttng_ht_iter iter; |
| 334 | struct consumer_relayd_sock_pair *relayd; |
| 335 | |
| 336 | rcu_read_lock(); |
| 337 | |
| 338 | cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd, |
| 339 | node.node) { |
| 340 | destroy_relayd(relayd); |
| 341 | } |
| 342 | |
| 343 | rcu_read_unlock(); |
| 344 | |
| 345 | lttng_ht_destroy(consumer_data.relayd_ht); |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Update the end point status of all streams having the given network sequence |
| 350 | * index (relayd index). |
| 351 | * |
| 352 | * It's atomically set without having the stream mutex locked which is fine |
| 353 | * because we handle the write/read race with a pipe wakeup for each thread. |
| 354 | */ |
| 355 | static void update_endpoint_status_by_netidx(uint64_t net_seq_idx, |
| 356 | enum consumer_endpoint_status status) |
| 357 | { |
| 358 | struct lttng_ht_iter iter; |
| 359 | struct lttng_consumer_stream *stream; |
| 360 | |
| 361 | DBG("Consumer set delete flag on stream by idx %" PRIu64, net_seq_idx); |
| 362 | |
| 363 | rcu_read_lock(); |
| 364 | |
| 365 | /* Let's begin with metadata */ |
| 366 | cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) { |
| 367 | if (stream->net_seq_idx == net_seq_idx) { |
| 368 | uatomic_set(&stream->endpoint_status, status); |
| 369 | DBG("Delete flag set to metadata stream %d", stream->wait_fd); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /* Follow up by the data streams */ |
| 374 | cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) { |
| 375 | if (stream->net_seq_idx == net_seq_idx) { |
| 376 | uatomic_set(&stream->endpoint_status, status); |
| 377 | DBG("Delete flag set to data stream %d", stream->wait_fd); |
| 378 | } |
| 379 | } |
| 380 | rcu_read_unlock(); |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Cleanup a relayd object by flagging every associated streams for deletion, |
| 385 | * destroying the object meaning removing it from the relayd hash table, |
| 386 | * closing the sockets and freeing the memory in a RCU call. |
| 387 | * |
| 388 | * If a local data context is available, notify the threads that the streams' |
| 389 | * state have changed. |
| 390 | */ |
| 391 | static void cleanup_relayd(struct consumer_relayd_sock_pair *relayd, |
| 392 | struct lttng_consumer_local_data *ctx) |
| 393 | { |
| 394 | uint64_t netidx; |
| 395 | |
| 396 | assert(relayd); |
| 397 | |
| 398 | DBG("Cleaning up relayd sockets"); |
| 399 | |
| 400 | /* Save the net sequence index before destroying the object */ |
| 401 | netidx = relayd->net_seq_idx; |
| 402 | |
| 403 | /* |
| 404 | * Delete the relayd from the relayd hash table, close the sockets and free |
| 405 | * the object in a RCU call. |
| 406 | */ |
| 407 | destroy_relayd(relayd); |
| 408 | |
| 409 | /* Set inactive endpoint to all streams */ |
| 410 | update_endpoint_status_by_netidx(netidx, CONSUMER_ENDPOINT_INACTIVE); |
| 411 | |
| 412 | /* |
| 413 | * With a local data context, notify the threads that the streams' state |
| 414 | * have changed. The write() action on the pipe acts as an "implicit" |
| 415 | * memory barrier ordering the updates of the end point status from the |
| 416 | * read of this status which happens AFTER receiving this notify. |
| 417 | */ |
| 418 | if (ctx) { |
| 419 | notify_thread_lttng_pipe(ctx->consumer_data_pipe); |
| 420 | notify_thread_lttng_pipe(ctx->consumer_metadata_pipe); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Flag a relayd socket pair for destruction. Destroy it if the refcount |
| 426 | * reaches zero. |
| 427 | * |
| 428 | * RCU read side lock MUST be aquired before calling this function. |
| 429 | */ |
| 430 | void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd) |
| 431 | { |
| 432 | assert(relayd); |
| 433 | |
| 434 | /* Set destroy flag for this object */ |
| 435 | uatomic_set(&relayd->destroy_flag, 1); |
| 436 | |
| 437 | /* Destroy the relayd if refcount is 0 */ |
| 438 | if (uatomic_read(&relayd->refcount) == 0) { |
| 439 | destroy_relayd(relayd); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * Remove a stream from the global list protected by a mutex. This |
| 445 | * function is also responsible for freeing its data structures. |
| 446 | */ |
| 447 | void consumer_del_stream(struct lttng_consumer_stream *stream, |
| 448 | struct lttng_ht *ht) |
| 449 | { |
| 450 | int ret; |
| 451 | struct lttng_ht_iter iter; |
| 452 | struct lttng_consumer_channel *free_chan = NULL; |
| 453 | struct consumer_relayd_sock_pair *relayd; |
| 454 | |
| 455 | assert(stream); |
| 456 | |
| 457 | DBG("Consumer del stream %d", stream->wait_fd); |
| 458 | |
| 459 | if (ht == NULL) { |
| 460 | /* Means the stream was allocated but not successfully added */ |
| 461 | goto free_stream_rcu; |
| 462 | } |
| 463 | |
| 464 | pthread_mutex_lock(&consumer_data.lock); |
| 465 | pthread_mutex_lock(&stream->lock); |
| 466 | |
| 467 | switch (consumer_data.type) { |
| 468 | case LTTNG_CONSUMER_KERNEL: |
| 469 | if (stream->mmap_base != NULL) { |
| 470 | ret = munmap(stream->mmap_base, stream->mmap_len); |
| 471 | if (ret != 0) { |
| 472 | PERROR("munmap"); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (stream->wait_fd >= 0) { |
| 477 | ret = close(stream->wait_fd); |
| 478 | if (ret) { |
| 479 | PERROR("close"); |
| 480 | } |
| 481 | } |
| 482 | break; |
| 483 | case LTTNG_CONSUMER32_UST: |
| 484 | case LTTNG_CONSUMER64_UST: |
| 485 | lttng_ustconsumer_del_stream(stream); |
| 486 | break; |
| 487 | default: |
| 488 | ERR("Unknown consumer_data type"); |
| 489 | assert(0); |
| 490 | goto end; |
| 491 | } |
| 492 | |
| 493 | rcu_read_lock(); |
| 494 | iter.iter.node = &stream->node.node; |
| 495 | ret = lttng_ht_del(ht, &iter); |
| 496 | assert(!ret); |
| 497 | |
| 498 | iter.iter.node = &stream->node_channel_id.node; |
| 499 | ret = lttng_ht_del(consumer_data.stream_per_chan_id_ht, &iter); |
| 500 | assert(!ret); |
| 501 | |
| 502 | iter.iter.node = &stream->node_session_id.node; |
| 503 | ret = lttng_ht_del(consumer_data.stream_list_ht, &iter); |
| 504 | assert(!ret); |
| 505 | rcu_read_unlock(); |
| 506 | |
| 507 | assert(consumer_data.stream_count > 0); |
| 508 | consumer_data.stream_count--; |
| 509 | |
| 510 | if (stream->out_fd >= 0) { |
| 511 | ret = close(stream->out_fd); |
| 512 | if (ret) { |
| 513 | PERROR("close"); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /* Check and cleanup relayd */ |
| 518 | rcu_read_lock(); |
| 519 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 520 | if (relayd != NULL) { |
| 521 | uatomic_dec(&relayd->refcount); |
| 522 | assert(uatomic_read(&relayd->refcount) >= 0); |
| 523 | |
| 524 | /* Closing streams requires to lock the control socket. */ |
| 525 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 526 | ret = relayd_send_close_stream(&relayd->control_sock, |
| 527 | stream->relayd_stream_id, |
| 528 | stream->next_net_seq_num - 1); |
| 529 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 530 | if (ret < 0) { |
| 531 | DBG("Unable to close stream on the relayd. Continuing"); |
| 532 | /* |
| 533 | * Continue here. There is nothing we can do for the relayd. |
| 534 | * Chances are that the relayd has closed the socket so we just |
| 535 | * continue cleaning up. |
| 536 | */ |
| 537 | } |
| 538 | |
| 539 | /* Both conditions are met, we destroy the relayd. */ |
| 540 | if (uatomic_read(&relayd->refcount) == 0 && |
| 541 | uatomic_read(&relayd->destroy_flag)) { |
| 542 | destroy_relayd(relayd); |
| 543 | } |
| 544 | } |
| 545 | rcu_read_unlock(); |
| 546 | |
| 547 | if (!uatomic_sub_return(&stream->chan->refcount, 1) |
| 548 | && !uatomic_read(&stream->chan->nb_init_stream_left)) { |
| 549 | free_chan = stream->chan; |
| 550 | } |
| 551 | |
| 552 | end: |
| 553 | consumer_data.need_update = 1; |
| 554 | pthread_mutex_unlock(&stream->lock); |
| 555 | pthread_mutex_unlock(&consumer_data.lock); |
| 556 | |
| 557 | if (free_chan) { |
| 558 | consumer_del_channel(free_chan); |
| 559 | } |
| 560 | |
| 561 | free_stream_rcu: |
| 562 | call_rcu(&stream->node.head, free_stream_rcu); |
| 563 | } |
| 564 | |
| 565 | struct lttng_consumer_stream *consumer_allocate_stream(uint64_t channel_key, |
| 566 | uint64_t stream_key, |
| 567 | enum lttng_consumer_stream_state state, |
| 568 | const char *channel_name, |
| 569 | uid_t uid, |
| 570 | gid_t gid, |
| 571 | uint64_t relayd_id, |
| 572 | uint64_t session_id, |
| 573 | int cpu, |
| 574 | int *alloc_ret, |
| 575 | enum consumer_channel_type type) |
| 576 | { |
| 577 | int ret; |
| 578 | struct lttng_consumer_stream *stream; |
| 579 | |
| 580 | stream = zmalloc(sizeof(*stream)); |
| 581 | if (stream == NULL) { |
| 582 | PERROR("malloc struct lttng_consumer_stream"); |
| 583 | ret = -ENOMEM; |
| 584 | goto end; |
| 585 | } |
| 586 | |
| 587 | rcu_read_lock(); |
| 588 | |
| 589 | stream->key = stream_key; |
| 590 | stream->out_fd = -1; |
| 591 | stream->out_fd_offset = 0; |
| 592 | stream->state = state; |
| 593 | stream->uid = uid; |
| 594 | stream->gid = gid; |
| 595 | stream->net_seq_idx = relayd_id; |
| 596 | stream->session_id = session_id; |
| 597 | pthread_mutex_init(&stream->lock, NULL); |
| 598 | |
| 599 | /* If channel is the metadata, flag this stream as metadata. */ |
| 600 | if (type == CONSUMER_CHANNEL_TYPE_METADATA) { |
| 601 | stream->metadata_flag = 1; |
| 602 | /* Metadata is flat out. */ |
| 603 | strncpy(stream->name, DEFAULT_METADATA_NAME, sizeof(stream->name)); |
| 604 | } else { |
| 605 | /* Format stream name to <channel_name>_<cpu_number> */ |
| 606 | ret = snprintf(stream->name, sizeof(stream->name), "%s_%d", |
| 607 | channel_name, cpu); |
| 608 | if (ret < 0) { |
| 609 | PERROR("snprintf stream name"); |
| 610 | goto error; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | /* Key is always the wait_fd for streams. */ |
| 615 | lttng_ht_node_init_u64(&stream->node, stream->key); |
| 616 | |
| 617 | /* Init node per channel id key */ |
| 618 | lttng_ht_node_init_u64(&stream->node_channel_id, channel_key); |
| 619 | |
| 620 | /* Init session id node with the stream session id */ |
| 621 | lttng_ht_node_init_u64(&stream->node_session_id, stream->session_id); |
| 622 | |
| 623 | DBG3("Allocated stream %s (key %" PRIu64 ", chan_key %" PRIu64 " relayd_id %" PRIu64 ", session_id %" PRIu64, |
| 624 | stream->name, stream->key, channel_key, stream->net_seq_idx, stream->session_id); |
| 625 | |
| 626 | rcu_read_unlock(); |
| 627 | return stream; |
| 628 | |
| 629 | error: |
| 630 | rcu_read_unlock(); |
| 631 | free(stream); |
| 632 | end: |
| 633 | if (alloc_ret) { |
| 634 | *alloc_ret = ret; |
| 635 | } |
| 636 | return NULL; |
| 637 | } |
| 638 | |
| 639 | /* |
| 640 | * Add a stream to the global list protected by a mutex. |
| 641 | */ |
| 642 | static int add_stream(struct lttng_consumer_stream *stream, |
| 643 | struct lttng_ht *ht) |
| 644 | { |
| 645 | int ret = 0; |
| 646 | struct consumer_relayd_sock_pair *relayd; |
| 647 | |
| 648 | assert(stream); |
| 649 | assert(ht); |
| 650 | |
| 651 | DBG3("Adding consumer stream %" PRIu64, stream->key); |
| 652 | |
| 653 | pthread_mutex_lock(&consumer_data.lock); |
| 654 | pthread_mutex_lock(&stream->lock); |
| 655 | rcu_read_lock(); |
| 656 | |
| 657 | /* Steal stream identifier to avoid having streams with the same key */ |
| 658 | steal_stream_key(stream->key, ht); |
| 659 | |
| 660 | lttng_ht_add_unique_u64(ht, &stream->node); |
| 661 | |
| 662 | lttng_ht_add_u64(consumer_data.stream_per_chan_id_ht, |
| 663 | &stream->node_channel_id); |
| 664 | |
| 665 | /* |
| 666 | * Add stream to the stream_list_ht of the consumer data. No need to steal |
| 667 | * the key since the HT does not use it and we allow to add redundant keys |
| 668 | * into this table. |
| 669 | */ |
| 670 | lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id); |
| 671 | |
| 672 | /* Check and cleanup relayd */ |
| 673 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 674 | if (relayd != NULL) { |
| 675 | uatomic_inc(&relayd->refcount); |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * When nb_init_stream_left reaches 0, we don't need to trigger any action |
| 680 | * in terms of destroying the associated channel, because the action that |
| 681 | * causes the count to become 0 also causes a stream to be added. The |
| 682 | * channel deletion will thus be triggered by the following removal of this |
| 683 | * stream. |
| 684 | */ |
| 685 | if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) { |
| 686 | /* Increment refcount before decrementing nb_init_stream_left */ |
| 687 | cmm_smp_wmb(); |
| 688 | uatomic_dec(&stream->chan->nb_init_stream_left); |
| 689 | } |
| 690 | |
| 691 | /* Update consumer data once the node is inserted. */ |
| 692 | consumer_data.stream_count++; |
| 693 | consumer_data.need_update = 1; |
| 694 | |
| 695 | rcu_read_unlock(); |
| 696 | pthread_mutex_unlock(&stream->lock); |
| 697 | pthread_mutex_unlock(&consumer_data.lock); |
| 698 | |
| 699 | return ret; |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Add relayd socket to global consumer data hashtable. RCU read side lock MUST |
| 704 | * be acquired before calling this. |
| 705 | */ |
| 706 | static int add_relayd(struct consumer_relayd_sock_pair *relayd) |
| 707 | { |
| 708 | int ret = 0; |
| 709 | struct lttng_ht_node_u64 *node; |
| 710 | struct lttng_ht_iter iter; |
| 711 | |
| 712 | assert(relayd); |
| 713 | |
| 714 | lttng_ht_lookup(consumer_data.relayd_ht, |
| 715 | &relayd->net_seq_idx, &iter); |
| 716 | node = lttng_ht_iter_get_node_u64(&iter); |
| 717 | if (node != NULL) { |
| 718 | goto end; |
| 719 | } |
| 720 | lttng_ht_add_unique_u64(consumer_data.relayd_ht, &relayd->node); |
| 721 | |
| 722 | end: |
| 723 | return ret; |
| 724 | } |
| 725 | |
| 726 | /* |
| 727 | * Allocate and return a consumer relayd socket. |
| 728 | */ |
| 729 | struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair( |
| 730 | uint64_t net_seq_idx) |
| 731 | { |
| 732 | struct consumer_relayd_sock_pair *obj = NULL; |
| 733 | |
| 734 | /* net sequence index of -1 is a failure */ |
| 735 | if (net_seq_idx == (uint64_t) -1ULL) { |
| 736 | goto error; |
| 737 | } |
| 738 | |
| 739 | obj = zmalloc(sizeof(struct consumer_relayd_sock_pair)); |
| 740 | if (obj == NULL) { |
| 741 | PERROR("zmalloc relayd sock"); |
| 742 | goto error; |
| 743 | } |
| 744 | |
| 745 | obj->net_seq_idx = net_seq_idx; |
| 746 | obj->refcount = 0; |
| 747 | obj->destroy_flag = 0; |
| 748 | obj->control_sock.sock.fd = -1; |
| 749 | obj->data_sock.sock.fd = -1; |
| 750 | lttng_ht_node_init_u64(&obj->node, obj->net_seq_idx); |
| 751 | pthread_mutex_init(&obj->ctrl_sock_mutex, NULL); |
| 752 | |
| 753 | error: |
| 754 | return obj; |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | * Find a relayd socket pair in the global consumer data. |
| 759 | * |
| 760 | * Return the object if found else NULL. |
| 761 | * RCU read-side lock must be held across this call and while using the |
| 762 | * returned object. |
| 763 | */ |
| 764 | struct consumer_relayd_sock_pair *consumer_find_relayd(uint64_t key) |
| 765 | { |
| 766 | struct lttng_ht_iter iter; |
| 767 | struct lttng_ht_node_u64 *node; |
| 768 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 769 | |
| 770 | /* Negative keys are lookup failures */ |
| 771 | if (key == (uint64_t) -1ULL) { |
| 772 | goto error; |
| 773 | } |
| 774 | |
| 775 | lttng_ht_lookup(consumer_data.relayd_ht, &key, |
| 776 | &iter); |
| 777 | node = lttng_ht_iter_get_node_u64(&iter); |
| 778 | if (node != NULL) { |
| 779 | relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node); |
| 780 | } |
| 781 | |
| 782 | error: |
| 783 | return relayd; |
| 784 | } |
| 785 | |
| 786 | /* |
| 787 | * Handle stream for relayd transmission if the stream applies for network |
| 788 | * streaming where the net sequence index is set. |
| 789 | * |
| 790 | * Return destination file descriptor or negative value on error. |
| 791 | */ |
| 792 | static int write_relayd_stream_header(struct lttng_consumer_stream *stream, |
| 793 | size_t data_size, unsigned long padding, |
| 794 | struct consumer_relayd_sock_pair *relayd) |
| 795 | { |
| 796 | int outfd = -1, ret; |
| 797 | struct lttcomm_relayd_data_hdr data_hdr; |
| 798 | |
| 799 | /* Safety net */ |
| 800 | assert(stream); |
| 801 | assert(relayd); |
| 802 | |
| 803 | /* Reset data header */ |
| 804 | memset(&data_hdr, 0, sizeof(data_hdr)); |
| 805 | |
| 806 | if (stream->metadata_flag) { |
| 807 | /* Caller MUST acquire the relayd control socket lock */ |
| 808 | ret = relayd_send_metadata(&relayd->control_sock, data_size); |
| 809 | if (ret < 0) { |
| 810 | goto error; |
| 811 | } |
| 812 | |
| 813 | /* Metadata are always sent on the control socket. */ |
| 814 | outfd = relayd->control_sock.sock.fd; |
| 815 | } else { |
| 816 | /* Set header with stream information */ |
| 817 | data_hdr.stream_id = htobe64(stream->relayd_stream_id); |
| 818 | data_hdr.data_size = htobe32(data_size); |
| 819 | data_hdr.padding_size = htobe32(padding); |
| 820 | /* |
| 821 | * Note that net_seq_num below is assigned with the *current* value of |
| 822 | * next_net_seq_num and only after that the next_net_seq_num will be |
| 823 | * increment. This is why when issuing a command on the relayd using |
| 824 | * this next value, 1 should always be substracted in order to compare |
| 825 | * the last seen sequence number on the relayd side to the last sent. |
| 826 | */ |
| 827 | data_hdr.net_seq_num = htobe64(stream->next_net_seq_num); |
| 828 | /* Other fields are zeroed previously */ |
| 829 | |
| 830 | ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr, |
| 831 | sizeof(data_hdr)); |
| 832 | if (ret < 0) { |
| 833 | goto error; |
| 834 | } |
| 835 | |
| 836 | ++stream->next_net_seq_num; |
| 837 | |
| 838 | /* Set to go on data socket */ |
| 839 | outfd = relayd->data_sock.sock.fd; |
| 840 | } |
| 841 | |
| 842 | error: |
| 843 | return outfd; |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * Allocate and return a new lttng_consumer_channel object using the given key |
| 848 | * to initialize the hash table node. |
| 849 | * |
| 850 | * On error, return NULL. |
| 851 | */ |
| 852 | struct lttng_consumer_channel *consumer_allocate_channel(uint64_t key, |
| 853 | uint64_t session_id, |
| 854 | const char *pathname, |
| 855 | const char *name, |
| 856 | uid_t uid, |
| 857 | gid_t gid, |
| 858 | uint64_t relayd_id, |
| 859 | enum lttng_event_output output, |
| 860 | uint64_t tracefile_size, |
| 861 | uint64_t tracefile_count, |
| 862 | uint64_t session_id_per_pid) |
| 863 | { |
| 864 | struct lttng_consumer_channel *channel; |
| 865 | |
| 866 | channel = zmalloc(sizeof(*channel)); |
| 867 | if (channel == NULL) { |
| 868 | PERROR("malloc struct lttng_consumer_channel"); |
| 869 | goto end; |
| 870 | } |
| 871 | |
| 872 | channel->key = key; |
| 873 | channel->refcount = 0; |
| 874 | channel->session_id = session_id; |
| 875 | channel->session_id_per_pid = session_id_per_pid; |
| 876 | channel->uid = uid; |
| 877 | channel->gid = gid; |
| 878 | channel->relayd_id = relayd_id; |
| 879 | channel->output = output; |
| 880 | channel->tracefile_size = tracefile_size; |
| 881 | channel->tracefile_count = tracefile_count; |
| 882 | |
| 883 | strncpy(channel->pathname, pathname, sizeof(channel->pathname)); |
| 884 | channel->pathname[sizeof(channel->pathname) - 1] = '\0'; |
| 885 | |
| 886 | strncpy(channel->name, name, sizeof(channel->name)); |
| 887 | channel->name[sizeof(channel->name) - 1] = '\0'; |
| 888 | |
| 889 | lttng_ht_node_init_u64(&channel->node, channel->key); |
| 890 | |
| 891 | channel->wait_fd = -1; |
| 892 | |
| 893 | CDS_INIT_LIST_HEAD(&channel->streams.head); |
| 894 | |
| 895 | DBG("Allocated channel (key %" PRIu64 ")", channel->key) |
| 896 | |
| 897 | end: |
| 898 | return channel; |
| 899 | } |
| 900 | |
| 901 | /* |
| 902 | * Add a channel to the global list protected by a mutex. |
| 903 | * |
| 904 | * On success 0 is returned else a negative value. |
| 905 | */ |
| 906 | int consumer_add_channel(struct lttng_consumer_channel *channel, |
| 907 | struct lttng_consumer_local_data *ctx) |
| 908 | { |
| 909 | int ret = 0; |
| 910 | struct lttng_ht_node_u64 *node; |
| 911 | struct lttng_ht_iter iter; |
| 912 | |
| 913 | pthread_mutex_lock(&consumer_data.lock); |
| 914 | rcu_read_lock(); |
| 915 | |
| 916 | lttng_ht_lookup(consumer_data.channel_ht, &channel->key, &iter); |
| 917 | node = lttng_ht_iter_get_node_u64(&iter); |
| 918 | if (node != NULL) { |
| 919 | /* Channel already exist. Ignore the insertion */ |
| 920 | ERR("Consumer add channel key %" PRIu64 " already exists!", |
| 921 | channel->key); |
| 922 | ret = -EEXIST; |
| 923 | goto end; |
| 924 | } |
| 925 | |
| 926 | lttng_ht_add_unique_u64(consumer_data.channel_ht, &channel->node); |
| 927 | |
| 928 | end: |
| 929 | rcu_read_unlock(); |
| 930 | pthread_mutex_unlock(&consumer_data.lock); |
| 931 | |
| 932 | if (!ret && channel->wait_fd != -1 && |
| 933 | channel->metadata_stream == NULL) { |
| 934 | notify_channel_pipe(ctx, channel, -1, CONSUMER_CHANNEL_ADD); |
| 935 | } |
| 936 | return ret; |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * Allocate the pollfd structure and the local view of the out fds to avoid |
| 941 | * doing a lookup in the linked list and concurrency issues when writing is |
| 942 | * needed. Called with consumer_data.lock held. |
| 943 | * |
| 944 | * Returns the number of fds in the structures. |
| 945 | */ |
| 946 | static int update_poll_array(struct lttng_consumer_local_data *ctx, |
| 947 | struct pollfd **pollfd, struct lttng_consumer_stream **local_stream, |
| 948 | struct lttng_ht *ht) |
| 949 | { |
| 950 | int i = 0; |
| 951 | struct lttng_ht_iter iter; |
| 952 | struct lttng_consumer_stream *stream; |
| 953 | |
| 954 | assert(ctx); |
| 955 | assert(ht); |
| 956 | assert(pollfd); |
| 957 | assert(local_stream); |
| 958 | |
| 959 | DBG("Updating poll fd array"); |
| 960 | rcu_read_lock(); |
| 961 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { |
| 962 | /* |
| 963 | * Only active streams with an active end point can be added to the |
| 964 | * poll set and local stream storage of the thread. |
| 965 | * |
| 966 | * There is a potential race here for endpoint_status to be updated |
| 967 | * just after the check. However, this is OK since the stream(s) will |
| 968 | * be deleted once the thread is notified that the end point state has |
| 969 | * changed where this function will be called back again. |
| 970 | */ |
| 971 | if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM || |
| 972 | stream->endpoint_status == CONSUMER_ENDPOINT_INACTIVE) { |
| 973 | continue; |
| 974 | } |
| 975 | /* |
| 976 | * This clobbers way too much the debug output. Uncomment that if you |
| 977 | * need it for debugging purposes. |
| 978 | * |
| 979 | * DBG("Active FD %d", stream->wait_fd); |
| 980 | */ |
| 981 | (*pollfd)[i].fd = stream->wait_fd; |
| 982 | (*pollfd)[i].events = POLLIN | POLLPRI; |
| 983 | local_stream[i] = stream; |
| 984 | i++; |
| 985 | } |
| 986 | rcu_read_unlock(); |
| 987 | |
| 988 | /* |
| 989 | * Insert the consumer_data_pipe at the end of the array and don't |
| 990 | * increment i so nb_fd is the number of real FD. |
| 991 | */ |
| 992 | (*pollfd)[i].fd = lttng_pipe_get_readfd(ctx->consumer_data_pipe); |
| 993 | (*pollfd)[i].events = POLLIN | POLLPRI; |
| 994 | return i; |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * Poll on the should_quit pipe and the command socket return -1 on error and |
| 999 | * should exit, 0 if data is available on the command socket |
| 1000 | */ |
| 1001 | int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll) |
| 1002 | { |
| 1003 | int num_rdy; |
| 1004 | |
| 1005 | restart: |
| 1006 | num_rdy = poll(consumer_sockpoll, 2, -1); |
| 1007 | if (num_rdy == -1) { |
| 1008 | /* |
| 1009 | * Restart interrupted system call. |
| 1010 | */ |
| 1011 | if (errno == EINTR) { |
| 1012 | goto restart; |
| 1013 | } |
| 1014 | PERROR("Poll error"); |
| 1015 | goto exit; |
| 1016 | } |
| 1017 | if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) { |
| 1018 | DBG("consumer_should_quit wake up"); |
| 1019 | goto exit; |
| 1020 | } |
| 1021 | return 0; |
| 1022 | |
| 1023 | exit: |
| 1024 | return -1; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * Set the error socket. |
| 1029 | */ |
| 1030 | void lttng_consumer_set_error_sock(struct lttng_consumer_local_data *ctx, |
| 1031 | int sock) |
| 1032 | { |
| 1033 | ctx->consumer_error_socket = sock; |
| 1034 | } |
| 1035 | |
| 1036 | /* |
| 1037 | * Set the command socket path. |
| 1038 | */ |
| 1039 | void lttng_consumer_set_command_sock_path( |
| 1040 | struct lttng_consumer_local_data *ctx, char *sock) |
| 1041 | { |
| 1042 | ctx->consumer_command_sock_path = sock; |
| 1043 | } |
| 1044 | |
| 1045 | /* |
| 1046 | * Send return code to the session daemon. |
| 1047 | * If the socket is not defined, we return 0, it is not a fatal error |
| 1048 | */ |
| 1049 | int lttng_consumer_send_error(struct lttng_consumer_local_data *ctx, int cmd) |
| 1050 | { |
| 1051 | if (ctx->consumer_error_socket > 0) { |
| 1052 | return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd, |
| 1053 | sizeof(enum lttcomm_sessiond_command)); |
| 1054 | } |
| 1055 | |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | * Close all the tracefiles and stream fds and MUST be called when all |
| 1061 | * instances are destroyed i.e. when all threads were joined and are ended. |
| 1062 | */ |
| 1063 | void lttng_consumer_cleanup(void) |
| 1064 | { |
| 1065 | struct lttng_ht_iter iter; |
| 1066 | struct lttng_consumer_channel *channel; |
| 1067 | |
| 1068 | rcu_read_lock(); |
| 1069 | |
| 1070 | cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, channel, |
| 1071 | node.node) { |
| 1072 | consumer_del_channel(channel); |
| 1073 | } |
| 1074 | |
| 1075 | rcu_read_unlock(); |
| 1076 | |
| 1077 | lttng_ht_destroy(consumer_data.channel_ht); |
| 1078 | |
| 1079 | cleanup_relayd_ht(); |
| 1080 | |
| 1081 | lttng_ht_destroy(consumer_data.stream_per_chan_id_ht); |
| 1082 | |
| 1083 | /* |
| 1084 | * This HT contains streams that are freed by either the metadata thread or |
| 1085 | * the data thread so we do *nothing* on the hash table and simply destroy |
| 1086 | * it. |
| 1087 | */ |
| 1088 | lttng_ht_destroy(consumer_data.stream_list_ht); |
| 1089 | } |
| 1090 | |
| 1091 | /* |
| 1092 | * Called from signal handler. |
| 1093 | */ |
| 1094 | void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx) |
| 1095 | { |
| 1096 | int ret; |
| 1097 | consumer_quit = 1; |
| 1098 | do { |
| 1099 | ret = write(ctx->consumer_should_quit[1], "4", 1); |
| 1100 | } while (ret < 0 && errno == EINTR); |
| 1101 | if (ret < 0 || ret != 1) { |
| 1102 | PERROR("write consumer quit"); |
| 1103 | } |
| 1104 | |
| 1105 | DBG("Consumer flag that it should quit"); |
| 1106 | } |
| 1107 | |
| 1108 | void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream, |
| 1109 | off_t orig_offset) |
| 1110 | { |
| 1111 | int outfd = stream->out_fd; |
| 1112 | |
| 1113 | /* |
| 1114 | * This does a blocking write-and-wait on any page that belongs to the |
| 1115 | * subbuffer prior to the one we just wrote. |
| 1116 | * Don't care about error values, as these are just hints and ways to |
| 1117 | * limit the amount of page cache used. |
| 1118 | */ |
| 1119 | if (orig_offset < stream->max_sb_size) { |
| 1120 | return; |
| 1121 | } |
| 1122 | lttng_sync_file_range(outfd, orig_offset - stream->max_sb_size, |
| 1123 | stream->max_sb_size, |
| 1124 | SYNC_FILE_RANGE_WAIT_BEFORE |
| 1125 | | SYNC_FILE_RANGE_WRITE |
| 1126 | | SYNC_FILE_RANGE_WAIT_AFTER); |
| 1127 | /* |
| 1128 | * Give hints to the kernel about how we access the file: |
| 1129 | * POSIX_FADV_DONTNEED : we won't re-access data in a near future after |
| 1130 | * we write it. |
| 1131 | * |
| 1132 | * We need to call fadvise again after the file grows because the |
| 1133 | * kernel does not seem to apply fadvise to non-existing parts of the |
| 1134 | * file. |
| 1135 | * |
| 1136 | * Call fadvise _after_ having waited for the page writeback to |
| 1137 | * complete because the dirty page writeback semantic is not well |
| 1138 | * defined. So it can be expected to lead to lower throughput in |
| 1139 | * streaming. |
| 1140 | */ |
| 1141 | posix_fadvise(outfd, orig_offset - stream->max_sb_size, |
| 1142 | stream->max_sb_size, POSIX_FADV_DONTNEED); |
| 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | * Initialise the necessary environnement : |
| 1147 | * - create a new context |
| 1148 | * - create the poll_pipe |
| 1149 | * - create the should_quit pipe (for signal handler) |
| 1150 | * - create the thread pipe (for splice) |
| 1151 | * |
| 1152 | * Takes a function pointer as argument, this function is called when data is |
| 1153 | * available on a buffer. This function is responsible to do the |
| 1154 | * kernctl_get_next_subbuf, read the data with mmap or splice depending on the |
| 1155 | * buffer configuration and then kernctl_put_next_subbuf at the end. |
| 1156 | * |
| 1157 | * Returns a pointer to the new context or NULL on error. |
| 1158 | */ |
| 1159 | struct lttng_consumer_local_data *lttng_consumer_create( |
| 1160 | enum lttng_consumer_type type, |
| 1161 | ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream, |
| 1162 | struct lttng_consumer_local_data *ctx), |
| 1163 | int (*recv_channel)(struct lttng_consumer_channel *channel), |
| 1164 | int (*recv_stream)(struct lttng_consumer_stream *stream), |
| 1165 | int (*update_stream)(uint64_t stream_key, uint32_t state)) |
| 1166 | { |
| 1167 | int ret; |
| 1168 | struct lttng_consumer_local_data *ctx; |
| 1169 | |
| 1170 | assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN || |
| 1171 | consumer_data.type == type); |
| 1172 | consumer_data.type = type; |
| 1173 | |
| 1174 | ctx = zmalloc(sizeof(struct lttng_consumer_local_data)); |
| 1175 | if (ctx == NULL) { |
| 1176 | PERROR("allocating context"); |
| 1177 | goto error; |
| 1178 | } |
| 1179 | |
| 1180 | ctx->consumer_error_socket = -1; |
| 1181 | ctx->consumer_metadata_socket = -1; |
| 1182 | /* assign the callbacks */ |
| 1183 | ctx->on_buffer_ready = buffer_ready; |
| 1184 | ctx->on_recv_channel = recv_channel; |
| 1185 | ctx->on_recv_stream = recv_stream; |
| 1186 | ctx->on_update_stream = update_stream; |
| 1187 | |
| 1188 | ctx->consumer_data_pipe = lttng_pipe_open(0); |
| 1189 | if (!ctx->consumer_data_pipe) { |
| 1190 | goto error_poll_pipe; |
| 1191 | } |
| 1192 | |
| 1193 | ret = pipe(ctx->consumer_should_quit); |
| 1194 | if (ret < 0) { |
| 1195 | PERROR("Error creating recv pipe"); |
| 1196 | goto error_quit_pipe; |
| 1197 | } |
| 1198 | |
| 1199 | ret = pipe(ctx->consumer_thread_pipe); |
| 1200 | if (ret < 0) { |
| 1201 | PERROR("Error creating thread pipe"); |
| 1202 | goto error_thread_pipe; |
| 1203 | } |
| 1204 | |
| 1205 | ret = pipe(ctx->consumer_channel_pipe); |
| 1206 | if (ret < 0) { |
| 1207 | PERROR("Error creating channel pipe"); |
| 1208 | goto error_channel_pipe; |
| 1209 | } |
| 1210 | |
| 1211 | ctx->consumer_metadata_pipe = lttng_pipe_open(0); |
| 1212 | if (!ctx->consumer_metadata_pipe) { |
| 1213 | goto error_metadata_pipe; |
| 1214 | } |
| 1215 | |
| 1216 | ret = utils_create_pipe(ctx->consumer_splice_metadata_pipe); |
| 1217 | if (ret < 0) { |
| 1218 | goto error_splice_pipe; |
| 1219 | } |
| 1220 | |
| 1221 | return ctx; |
| 1222 | |
| 1223 | error_splice_pipe: |
| 1224 | lttng_pipe_destroy(ctx->consumer_metadata_pipe); |
| 1225 | error_metadata_pipe: |
| 1226 | utils_close_pipe(ctx->consumer_channel_pipe); |
| 1227 | error_channel_pipe: |
| 1228 | utils_close_pipe(ctx->consumer_thread_pipe); |
| 1229 | error_thread_pipe: |
| 1230 | utils_close_pipe(ctx->consumer_should_quit); |
| 1231 | error_quit_pipe: |
| 1232 | lttng_pipe_destroy(ctx->consumer_data_pipe); |
| 1233 | error_poll_pipe: |
| 1234 | free(ctx); |
| 1235 | error: |
| 1236 | return NULL; |
| 1237 | } |
| 1238 | |
| 1239 | /* |
| 1240 | * Close all fds associated with the instance and free the context. |
| 1241 | */ |
| 1242 | void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx) |
| 1243 | { |
| 1244 | int ret; |
| 1245 | |
| 1246 | DBG("Consumer destroying it. Closing everything."); |
| 1247 | |
| 1248 | ret = close(ctx->consumer_error_socket); |
| 1249 | if (ret) { |
| 1250 | PERROR("close"); |
| 1251 | } |
| 1252 | ret = close(ctx->consumer_metadata_socket); |
| 1253 | if (ret) { |
| 1254 | PERROR("close"); |
| 1255 | } |
| 1256 | utils_close_pipe(ctx->consumer_thread_pipe); |
| 1257 | utils_close_pipe(ctx->consumer_channel_pipe); |
| 1258 | lttng_pipe_destroy(ctx->consumer_data_pipe); |
| 1259 | lttng_pipe_destroy(ctx->consumer_metadata_pipe); |
| 1260 | utils_close_pipe(ctx->consumer_should_quit); |
| 1261 | utils_close_pipe(ctx->consumer_splice_metadata_pipe); |
| 1262 | |
| 1263 | unlink(ctx->consumer_command_sock_path); |
| 1264 | free(ctx); |
| 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * Write the metadata stream id on the specified file descriptor. |
| 1269 | */ |
| 1270 | static int write_relayd_metadata_id(int fd, |
| 1271 | struct lttng_consumer_stream *stream, |
| 1272 | struct consumer_relayd_sock_pair *relayd, unsigned long padding) |
| 1273 | { |
| 1274 | int ret; |
| 1275 | struct lttcomm_relayd_metadata_payload hdr; |
| 1276 | |
| 1277 | hdr.stream_id = htobe64(stream->relayd_stream_id); |
| 1278 | hdr.padding_size = htobe32(padding); |
| 1279 | do { |
| 1280 | ret = write(fd, (void *) &hdr, sizeof(hdr)); |
| 1281 | } while (ret < 0 && errno == EINTR); |
| 1282 | if (ret < 0 || ret != sizeof(hdr)) { |
| 1283 | /* |
| 1284 | * This error means that the fd's end is closed so ignore the perror |
| 1285 | * not to clubber the error output since this can happen in a normal |
| 1286 | * code path. |
| 1287 | */ |
| 1288 | if (errno != EPIPE) { |
| 1289 | PERROR("write metadata stream id"); |
| 1290 | } |
| 1291 | DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno); |
| 1292 | /* |
| 1293 | * Set ret to a negative value because if ret != sizeof(hdr), we don't |
| 1294 | * handle writting the missing part so report that as an error and |
| 1295 | * don't lie to the caller. |
| 1296 | */ |
| 1297 | ret = -1; |
| 1298 | goto end; |
| 1299 | } |
| 1300 | DBG("Metadata stream id %" PRIu64 " with padding %lu written before data", |
| 1301 | stream->relayd_stream_id, padding); |
| 1302 | |
| 1303 | end: |
| 1304 | return ret; |
| 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | * Mmap the ring buffer, read it and write the data to the tracefile. This is a |
| 1309 | * core function for writing trace buffers to either the local filesystem or |
| 1310 | * the network. |
| 1311 | * |
| 1312 | * It must be called with the stream lock held. |
| 1313 | * |
| 1314 | * Careful review MUST be put if any changes occur! |
| 1315 | * |
| 1316 | * Returns the number of bytes written |
| 1317 | */ |
| 1318 | ssize_t lttng_consumer_on_read_subbuffer_mmap( |
| 1319 | struct lttng_consumer_local_data *ctx, |
| 1320 | struct lttng_consumer_stream *stream, unsigned long len, |
| 1321 | unsigned long padding) |
| 1322 | { |
| 1323 | unsigned long mmap_offset; |
| 1324 | void *mmap_base; |
| 1325 | ssize_t ret = 0, written = 0; |
| 1326 | off_t orig_offset = stream->out_fd_offset; |
| 1327 | /* Default is on the disk */ |
| 1328 | int outfd = stream->out_fd; |
| 1329 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 1330 | unsigned int relayd_hang_up = 0; |
| 1331 | |
| 1332 | /* RCU lock for the relayd pointer */ |
| 1333 | rcu_read_lock(); |
| 1334 | |
| 1335 | /* Flag that the current stream if set for network streaming. */ |
| 1336 | if (stream->net_seq_idx != (uint64_t) -1ULL) { |
| 1337 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 1338 | if (relayd == NULL) { |
| 1339 | goto end; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | /* get the offset inside the fd to mmap */ |
| 1344 | switch (consumer_data.type) { |
| 1345 | case LTTNG_CONSUMER_KERNEL: |
| 1346 | mmap_base = stream->mmap_base; |
| 1347 | ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset); |
| 1348 | break; |
| 1349 | case LTTNG_CONSUMER32_UST: |
| 1350 | case LTTNG_CONSUMER64_UST: |
| 1351 | mmap_base = lttng_ustctl_get_mmap_base(stream); |
| 1352 | if (!mmap_base) { |
| 1353 | ERR("read mmap get mmap base for stream %s", stream->name); |
| 1354 | written = -1; |
| 1355 | goto end; |
| 1356 | } |
| 1357 | ret = lttng_ustctl_get_mmap_read_offset(stream, &mmap_offset); |
| 1358 | |
| 1359 | break; |
| 1360 | default: |
| 1361 | ERR("Unknown consumer_data type"); |
| 1362 | assert(0); |
| 1363 | } |
| 1364 | if (ret != 0) { |
| 1365 | errno = -ret; |
| 1366 | PERROR("tracer ctl get_mmap_read_offset"); |
| 1367 | written = ret; |
| 1368 | goto end; |
| 1369 | } |
| 1370 | |
| 1371 | /* Handle stream on the relayd if the output is on the network */ |
| 1372 | if (relayd) { |
| 1373 | unsigned long netlen = len; |
| 1374 | |
| 1375 | /* |
| 1376 | * Lock the control socket for the complete duration of the function |
| 1377 | * since from this point on we will use the socket. |
| 1378 | */ |
| 1379 | if (stream->metadata_flag) { |
| 1380 | /* Metadata requires the control socket. */ |
| 1381 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 1382 | netlen += sizeof(struct lttcomm_relayd_metadata_payload); |
| 1383 | } |
| 1384 | |
| 1385 | ret = write_relayd_stream_header(stream, netlen, padding, relayd); |
| 1386 | if (ret >= 0) { |
| 1387 | /* Use the returned socket. */ |
| 1388 | outfd = ret; |
| 1389 | |
| 1390 | /* Write metadata stream id before payload */ |
| 1391 | if (stream->metadata_flag) { |
| 1392 | ret = write_relayd_metadata_id(outfd, stream, relayd, padding); |
| 1393 | if (ret < 0) { |
| 1394 | written = ret; |
| 1395 | /* Socket operation failed. We consider the relayd dead */ |
| 1396 | if (ret == -EPIPE || ret == -EINVAL) { |
| 1397 | relayd_hang_up = 1; |
| 1398 | goto write_error; |
| 1399 | } |
| 1400 | goto end; |
| 1401 | } |
| 1402 | } |
| 1403 | } else { |
| 1404 | /* Socket operation failed. We consider the relayd dead */ |
| 1405 | if (ret == -EPIPE || ret == -EINVAL) { |
| 1406 | relayd_hang_up = 1; |
| 1407 | goto write_error; |
| 1408 | } |
| 1409 | /* Else, use the default set before which is the filesystem. */ |
| 1410 | } |
| 1411 | } else { |
| 1412 | /* No streaming, we have to set the len with the full padding */ |
| 1413 | len += padding; |
| 1414 | |
| 1415 | /* |
| 1416 | * Check if we need to change the tracefile before writing the packet. |
| 1417 | */ |
| 1418 | if (stream->chan->tracefile_size > 0 && |
| 1419 | (stream->tracefile_size_current + len) > |
| 1420 | stream->chan->tracefile_size) { |
| 1421 | ret = utils_rotate_stream_file(stream->chan->pathname, |
| 1422 | stream->name, stream->chan->tracefile_size, |
| 1423 | stream->chan->tracefile_count, stream->uid, stream->gid, |
| 1424 | stream->out_fd, &(stream->tracefile_count_current)); |
| 1425 | if (ret < 0) { |
| 1426 | ERR("Rotating output file"); |
| 1427 | goto end; |
| 1428 | } |
| 1429 | outfd = stream->out_fd = ret; |
| 1430 | /* Reset current size because we just perform a rotation. */ |
| 1431 | stream->tracefile_size_current = 0; |
| 1432 | } |
| 1433 | stream->tracefile_size_current += len; |
| 1434 | } |
| 1435 | |
| 1436 | while (len > 0) { |
| 1437 | do { |
| 1438 | ret = write(outfd, mmap_base + mmap_offset, len); |
| 1439 | } while (ret < 0 && errno == EINTR); |
| 1440 | DBG("Consumer mmap write() ret %zd (len %lu)", ret, len); |
| 1441 | if (ret < 0) { |
| 1442 | /* |
| 1443 | * This is possible if the fd is closed on the other side (outfd) |
| 1444 | * or any write problem. It can be verbose a bit for a normal |
| 1445 | * execution if for instance the relayd is stopped abruptly. This |
| 1446 | * can happen so set this to a DBG statement. |
| 1447 | */ |
| 1448 | DBG("Error in file write mmap"); |
| 1449 | if (written == 0) { |
| 1450 | written = ret; |
| 1451 | } |
| 1452 | /* Socket operation failed. We consider the relayd dead */ |
| 1453 | if (errno == EPIPE || errno == EINVAL) { |
| 1454 | relayd_hang_up = 1; |
| 1455 | goto write_error; |
| 1456 | } |
| 1457 | goto end; |
| 1458 | } else if (ret > len) { |
| 1459 | PERROR("Error in file write (ret %zd > len %lu)", ret, len); |
| 1460 | written += ret; |
| 1461 | goto end; |
| 1462 | } else { |
| 1463 | len -= ret; |
| 1464 | mmap_offset += ret; |
| 1465 | } |
| 1466 | |
| 1467 | /* This call is useless on a socket so better save a syscall. */ |
| 1468 | if (!relayd) { |
| 1469 | /* This won't block, but will start writeout asynchronously */ |
| 1470 | lttng_sync_file_range(outfd, stream->out_fd_offset, ret, |
| 1471 | SYNC_FILE_RANGE_WRITE); |
| 1472 | stream->out_fd_offset += ret; |
| 1473 | } |
| 1474 | written += ret; |
| 1475 | } |
| 1476 | lttng_consumer_sync_trace_file(stream, orig_offset); |
| 1477 | |
| 1478 | write_error: |
| 1479 | /* |
| 1480 | * This is a special case that the relayd has closed its socket. Let's |
| 1481 | * cleanup the relayd object and all associated streams. |
| 1482 | */ |
| 1483 | if (relayd && relayd_hang_up) { |
| 1484 | cleanup_relayd(relayd, ctx); |
| 1485 | } |
| 1486 | |
| 1487 | end: |
| 1488 | /* Unlock only if ctrl socket used */ |
| 1489 | if (relayd && stream->metadata_flag) { |
| 1490 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 1491 | } |
| 1492 | |
| 1493 | rcu_read_unlock(); |
| 1494 | return written; |
| 1495 | } |
| 1496 | |
| 1497 | /* |
| 1498 | * Splice the data from the ring buffer to the tracefile. |
| 1499 | * |
| 1500 | * It must be called with the stream lock held. |
| 1501 | * |
| 1502 | * Returns the number of bytes spliced. |
| 1503 | */ |
| 1504 | ssize_t lttng_consumer_on_read_subbuffer_splice( |
| 1505 | struct lttng_consumer_local_data *ctx, |
| 1506 | struct lttng_consumer_stream *stream, unsigned long len, |
| 1507 | unsigned long padding) |
| 1508 | { |
| 1509 | ssize_t ret = 0, written = 0, ret_splice = 0; |
| 1510 | loff_t offset = 0; |
| 1511 | off_t orig_offset = stream->out_fd_offset; |
| 1512 | int fd = stream->wait_fd; |
| 1513 | /* Default is on the disk */ |
| 1514 | int outfd = stream->out_fd; |
| 1515 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 1516 | int *splice_pipe; |
| 1517 | unsigned int relayd_hang_up = 0; |
| 1518 | |
| 1519 | switch (consumer_data.type) { |
| 1520 | case LTTNG_CONSUMER_KERNEL: |
| 1521 | break; |
| 1522 | case LTTNG_CONSUMER32_UST: |
| 1523 | case LTTNG_CONSUMER64_UST: |
| 1524 | /* Not supported for user space tracing */ |
| 1525 | return -ENOSYS; |
| 1526 | default: |
| 1527 | ERR("Unknown consumer_data type"); |
| 1528 | assert(0); |
| 1529 | } |
| 1530 | |
| 1531 | /* RCU lock for the relayd pointer */ |
| 1532 | rcu_read_lock(); |
| 1533 | |
| 1534 | /* Flag that the current stream if set for network streaming. */ |
| 1535 | if (stream->net_seq_idx != (uint64_t) -1ULL) { |
| 1536 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 1537 | if (relayd == NULL) { |
| 1538 | goto end; |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | /* |
| 1543 | * Choose right pipe for splice. Metadata and trace data are handled by |
| 1544 | * different threads hence the use of two pipes in order not to race or |
| 1545 | * corrupt the written data. |
| 1546 | */ |
| 1547 | if (stream->metadata_flag) { |
| 1548 | splice_pipe = ctx->consumer_splice_metadata_pipe; |
| 1549 | } else { |
| 1550 | splice_pipe = ctx->consumer_thread_pipe; |
| 1551 | } |
| 1552 | |
| 1553 | /* Write metadata stream id before payload */ |
| 1554 | if (relayd) { |
| 1555 | int total_len = len; |
| 1556 | |
| 1557 | if (stream->metadata_flag) { |
| 1558 | /* |
| 1559 | * Lock the control socket for the complete duration of the function |
| 1560 | * since from this point on we will use the socket. |
| 1561 | */ |
| 1562 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 1563 | |
| 1564 | ret = write_relayd_metadata_id(splice_pipe[1], stream, relayd, |
| 1565 | padding); |
| 1566 | if (ret < 0) { |
| 1567 | written = ret; |
| 1568 | /* Socket operation failed. We consider the relayd dead */ |
| 1569 | if (ret == -EBADF) { |
| 1570 | WARN("Remote relayd disconnected. Stopping"); |
| 1571 | relayd_hang_up = 1; |
| 1572 | goto write_error; |
| 1573 | } |
| 1574 | goto end; |
| 1575 | } |
| 1576 | |
| 1577 | total_len += sizeof(struct lttcomm_relayd_metadata_payload); |
| 1578 | } |
| 1579 | |
| 1580 | ret = write_relayd_stream_header(stream, total_len, padding, relayd); |
| 1581 | if (ret >= 0) { |
| 1582 | /* Use the returned socket. */ |
| 1583 | outfd = ret; |
| 1584 | } else { |
| 1585 | /* Socket operation failed. We consider the relayd dead */ |
| 1586 | if (ret == -EBADF) { |
| 1587 | WARN("Remote relayd disconnected. Stopping"); |
| 1588 | relayd_hang_up = 1; |
| 1589 | goto write_error; |
| 1590 | } |
| 1591 | goto end; |
| 1592 | } |
| 1593 | } else { |
| 1594 | /* No streaming, we have to set the len with the full padding */ |
| 1595 | len += padding; |
| 1596 | |
| 1597 | /* |
| 1598 | * Check if we need to change the tracefile before writing the packet. |
| 1599 | */ |
| 1600 | if (stream->chan->tracefile_size > 0 && |
| 1601 | (stream->tracefile_size_current + len) > |
| 1602 | stream->chan->tracefile_size) { |
| 1603 | ret = utils_rotate_stream_file(stream->chan->pathname, |
| 1604 | stream->name, stream->chan->tracefile_size, |
| 1605 | stream->chan->tracefile_count, stream->uid, stream->gid, |
| 1606 | stream->out_fd, &(stream->tracefile_count_current)); |
| 1607 | if (ret < 0) { |
| 1608 | ERR("Rotating output file"); |
| 1609 | goto end; |
| 1610 | } |
| 1611 | outfd = stream->out_fd = ret; |
| 1612 | /* Reset current size because we just perform a rotation. */ |
| 1613 | stream->tracefile_size_current = 0; |
| 1614 | } |
| 1615 | stream->tracefile_size_current += len; |
| 1616 | } |
| 1617 | |
| 1618 | while (len > 0) { |
| 1619 | DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)", |
| 1620 | (unsigned long)offset, len, fd, splice_pipe[1]); |
| 1621 | ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len, |
| 1622 | SPLICE_F_MOVE | SPLICE_F_MORE); |
| 1623 | DBG("splice chan to pipe, ret %zd", ret_splice); |
| 1624 | if (ret_splice < 0) { |
| 1625 | PERROR("Error in relay splice"); |
| 1626 | if (written == 0) { |
| 1627 | written = ret_splice; |
| 1628 | } |
| 1629 | ret = errno; |
| 1630 | goto splice_error; |
| 1631 | } |
| 1632 | |
| 1633 | /* Handle stream on the relayd if the output is on the network */ |
| 1634 | if (relayd) { |
| 1635 | if (stream->metadata_flag) { |
| 1636 | size_t metadata_payload_size = |
| 1637 | sizeof(struct lttcomm_relayd_metadata_payload); |
| 1638 | |
| 1639 | /* Update counter to fit the spliced data */ |
| 1640 | ret_splice += metadata_payload_size; |
| 1641 | len += metadata_payload_size; |
| 1642 | /* |
| 1643 | * We do this so the return value can match the len passed as |
| 1644 | * argument to this function. |
| 1645 | */ |
| 1646 | written -= metadata_payload_size; |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | /* Splice data out */ |
| 1651 | ret_splice = splice(splice_pipe[0], NULL, outfd, NULL, |
| 1652 | ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE); |
| 1653 | DBG("Consumer splice pipe to file, ret %zd", ret_splice); |
| 1654 | if (ret_splice < 0) { |
| 1655 | PERROR("Error in file splice"); |
| 1656 | if (written == 0) { |
| 1657 | written = ret_splice; |
| 1658 | } |
| 1659 | /* Socket operation failed. We consider the relayd dead */ |
| 1660 | if (errno == EBADF || errno == EPIPE) { |
| 1661 | WARN("Remote relayd disconnected. Stopping"); |
| 1662 | relayd_hang_up = 1; |
| 1663 | goto write_error; |
| 1664 | } |
| 1665 | ret = errno; |
| 1666 | goto splice_error; |
| 1667 | } else if (ret_splice > len) { |
| 1668 | errno = EINVAL; |
| 1669 | PERROR("Wrote more data than requested %zd (len: %lu)", |
| 1670 | ret_splice, len); |
| 1671 | written += ret_splice; |
| 1672 | ret = errno; |
| 1673 | goto splice_error; |
| 1674 | } |
| 1675 | len -= ret_splice; |
| 1676 | |
| 1677 | /* This call is useless on a socket so better save a syscall. */ |
| 1678 | if (!relayd) { |
| 1679 | /* This won't block, but will start writeout asynchronously */ |
| 1680 | lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice, |
| 1681 | SYNC_FILE_RANGE_WRITE); |
| 1682 | stream->out_fd_offset += ret_splice; |
| 1683 | } |
| 1684 | written += ret_splice; |
| 1685 | } |
| 1686 | lttng_consumer_sync_trace_file(stream, orig_offset); |
| 1687 | |
| 1688 | ret = ret_splice; |
| 1689 | |
| 1690 | goto end; |
| 1691 | |
| 1692 | write_error: |
| 1693 | /* |
| 1694 | * This is a special case that the relayd has closed its socket. Let's |
| 1695 | * cleanup the relayd object and all associated streams. |
| 1696 | */ |
| 1697 | if (relayd && relayd_hang_up) { |
| 1698 | cleanup_relayd(relayd, ctx); |
| 1699 | /* Skip splice error so the consumer does not fail */ |
| 1700 | goto end; |
| 1701 | } |
| 1702 | |
| 1703 | splice_error: |
| 1704 | /* send the appropriate error description to sessiond */ |
| 1705 | switch (ret) { |
| 1706 | case EINVAL: |
| 1707 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL); |
| 1708 | break; |
| 1709 | case ENOMEM: |
| 1710 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM); |
| 1711 | break; |
| 1712 | case ESPIPE: |
| 1713 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE); |
| 1714 | break; |
| 1715 | } |
| 1716 | |
| 1717 | end: |
| 1718 | if (relayd && stream->metadata_flag) { |
| 1719 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 1720 | } |
| 1721 | |
| 1722 | rcu_read_unlock(); |
| 1723 | return written; |
| 1724 | } |
| 1725 | |
| 1726 | /* |
| 1727 | * Take a snapshot for a specific fd |
| 1728 | * |
| 1729 | * Returns 0 on success, < 0 on error |
| 1730 | */ |
| 1731 | int lttng_consumer_take_snapshot(struct lttng_consumer_stream *stream) |
| 1732 | { |
| 1733 | switch (consumer_data.type) { |
| 1734 | case LTTNG_CONSUMER_KERNEL: |
| 1735 | return lttng_kconsumer_take_snapshot(stream); |
| 1736 | case LTTNG_CONSUMER32_UST: |
| 1737 | case LTTNG_CONSUMER64_UST: |
| 1738 | return lttng_ustconsumer_take_snapshot(stream); |
| 1739 | default: |
| 1740 | ERR("Unknown consumer_data type"); |
| 1741 | assert(0); |
| 1742 | return -ENOSYS; |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | /* |
| 1747 | * Get the produced position |
| 1748 | * |
| 1749 | * Returns 0 on success, < 0 on error |
| 1750 | */ |
| 1751 | int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream *stream, |
| 1752 | unsigned long *pos) |
| 1753 | { |
| 1754 | switch (consumer_data.type) { |
| 1755 | case LTTNG_CONSUMER_KERNEL: |
| 1756 | return lttng_kconsumer_get_produced_snapshot(stream, pos); |
| 1757 | case LTTNG_CONSUMER32_UST: |
| 1758 | case LTTNG_CONSUMER64_UST: |
| 1759 | return lttng_ustconsumer_get_produced_snapshot(stream, pos); |
| 1760 | default: |
| 1761 | ERR("Unknown consumer_data type"); |
| 1762 | assert(0); |
| 1763 | return -ENOSYS; |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
| 1768 | int sock, struct pollfd *consumer_sockpoll) |
| 1769 | { |
| 1770 | switch (consumer_data.type) { |
| 1771 | case LTTNG_CONSUMER_KERNEL: |
| 1772 | return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll); |
| 1773 | case LTTNG_CONSUMER32_UST: |
| 1774 | case LTTNG_CONSUMER64_UST: |
| 1775 | return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll); |
| 1776 | default: |
| 1777 | ERR("Unknown consumer_data type"); |
| 1778 | assert(0); |
| 1779 | return -ENOSYS; |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | /* |
| 1784 | * Iterate over all streams of the hashtable and free them properly. |
| 1785 | * |
| 1786 | * WARNING: *MUST* be used with data stream only. |
| 1787 | */ |
| 1788 | static void destroy_data_stream_ht(struct lttng_ht *ht) |
| 1789 | { |
| 1790 | struct lttng_ht_iter iter; |
| 1791 | struct lttng_consumer_stream *stream; |
| 1792 | |
| 1793 | if (ht == NULL) { |
| 1794 | return; |
| 1795 | } |
| 1796 | |
| 1797 | rcu_read_lock(); |
| 1798 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { |
| 1799 | /* |
| 1800 | * Ignore return value since we are currently cleaning up so any error |
| 1801 | * can't be handled. |
| 1802 | */ |
| 1803 | (void) consumer_del_stream(stream, ht); |
| 1804 | } |
| 1805 | rcu_read_unlock(); |
| 1806 | |
| 1807 | lttng_ht_destroy(ht); |
| 1808 | } |
| 1809 | |
| 1810 | /* |
| 1811 | * Iterate over all streams of the hashtable and free them properly. |
| 1812 | * |
| 1813 | * XXX: Should not be only for metadata stream or else use an other name. |
| 1814 | */ |
| 1815 | static void destroy_stream_ht(struct lttng_ht *ht) |
| 1816 | { |
| 1817 | struct lttng_ht_iter iter; |
| 1818 | struct lttng_consumer_stream *stream; |
| 1819 | |
| 1820 | if (ht == NULL) { |
| 1821 | return; |
| 1822 | } |
| 1823 | |
| 1824 | rcu_read_lock(); |
| 1825 | cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { |
| 1826 | /* |
| 1827 | * Ignore return value since we are currently cleaning up so any error |
| 1828 | * can't be handled. |
| 1829 | */ |
| 1830 | (void) consumer_del_metadata_stream(stream, ht); |
| 1831 | } |
| 1832 | rcu_read_unlock(); |
| 1833 | |
| 1834 | lttng_ht_destroy(ht); |
| 1835 | } |
| 1836 | |
| 1837 | void lttng_consumer_close_metadata(void) |
| 1838 | { |
| 1839 | switch (consumer_data.type) { |
| 1840 | case LTTNG_CONSUMER_KERNEL: |
| 1841 | /* |
| 1842 | * The Kernel consumer has a different metadata scheme so we don't |
| 1843 | * close anything because the stream will be closed by the session |
| 1844 | * daemon. |
| 1845 | */ |
| 1846 | break; |
| 1847 | case LTTNG_CONSUMER32_UST: |
| 1848 | case LTTNG_CONSUMER64_UST: |
| 1849 | /* |
| 1850 | * Close all metadata streams. The metadata hash table is passed and |
| 1851 | * this call iterates over it by closing all wakeup fd. This is safe |
| 1852 | * because at this point we are sure that the metadata producer is |
| 1853 | * either dead or blocked. |
| 1854 | */ |
| 1855 | lttng_ustconsumer_close_metadata(metadata_ht); |
| 1856 | break; |
| 1857 | default: |
| 1858 | ERR("Unknown consumer_data type"); |
| 1859 | assert(0); |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | /* |
| 1864 | * Clean up a metadata stream and free its memory. |
| 1865 | */ |
| 1866 | void consumer_del_metadata_stream(struct lttng_consumer_stream *stream, |
| 1867 | struct lttng_ht *ht) |
| 1868 | { |
| 1869 | int ret; |
| 1870 | struct lttng_ht_iter iter; |
| 1871 | struct lttng_consumer_channel *free_chan = NULL; |
| 1872 | struct consumer_relayd_sock_pair *relayd; |
| 1873 | |
| 1874 | assert(stream); |
| 1875 | /* |
| 1876 | * This call should NEVER receive regular stream. It must always be |
| 1877 | * metadata stream and this is crucial for data structure synchronization. |
| 1878 | */ |
| 1879 | assert(stream->metadata_flag); |
| 1880 | |
| 1881 | DBG3("Consumer delete metadata stream %d", stream->wait_fd); |
| 1882 | |
| 1883 | if (ht == NULL) { |
| 1884 | /* Means the stream was allocated but not successfully added */ |
| 1885 | goto free_stream_rcu; |
| 1886 | } |
| 1887 | |
| 1888 | pthread_mutex_lock(&consumer_data.lock); |
| 1889 | pthread_mutex_lock(&stream->lock); |
| 1890 | |
| 1891 | switch (consumer_data.type) { |
| 1892 | case LTTNG_CONSUMER_KERNEL: |
| 1893 | if (stream->mmap_base != NULL) { |
| 1894 | ret = munmap(stream->mmap_base, stream->mmap_len); |
| 1895 | if (ret != 0) { |
| 1896 | PERROR("munmap metadata stream"); |
| 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | if (stream->wait_fd >= 0) { |
| 1901 | ret = close(stream->wait_fd); |
| 1902 | if (ret < 0) { |
| 1903 | PERROR("close kernel metadata wait_fd"); |
| 1904 | } |
| 1905 | } |
| 1906 | break; |
| 1907 | case LTTNG_CONSUMER32_UST: |
| 1908 | case LTTNG_CONSUMER64_UST: |
| 1909 | lttng_ustconsumer_del_stream(stream); |
| 1910 | break; |
| 1911 | default: |
| 1912 | ERR("Unknown consumer_data type"); |
| 1913 | assert(0); |
| 1914 | goto end; |
| 1915 | } |
| 1916 | |
| 1917 | rcu_read_lock(); |
| 1918 | iter.iter.node = &stream->node.node; |
| 1919 | ret = lttng_ht_del(ht, &iter); |
| 1920 | assert(!ret); |
| 1921 | |
| 1922 | iter.iter.node = &stream->node_channel_id.node; |
| 1923 | ret = lttng_ht_del(consumer_data.stream_per_chan_id_ht, &iter); |
| 1924 | assert(!ret); |
| 1925 | |
| 1926 | iter.iter.node = &stream->node_session_id.node; |
| 1927 | ret = lttng_ht_del(consumer_data.stream_list_ht, &iter); |
| 1928 | assert(!ret); |
| 1929 | rcu_read_unlock(); |
| 1930 | |
| 1931 | if (stream->out_fd >= 0) { |
| 1932 | ret = close(stream->out_fd); |
| 1933 | if (ret) { |
| 1934 | PERROR("close"); |
| 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | /* Check and cleanup relayd */ |
| 1939 | rcu_read_lock(); |
| 1940 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 1941 | if (relayd != NULL) { |
| 1942 | uatomic_dec(&relayd->refcount); |
| 1943 | assert(uatomic_read(&relayd->refcount) >= 0); |
| 1944 | |
| 1945 | /* Closing streams requires to lock the control socket. */ |
| 1946 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 1947 | ret = relayd_send_close_stream(&relayd->control_sock, |
| 1948 | stream->relayd_stream_id, stream->next_net_seq_num - 1); |
| 1949 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 1950 | if (ret < 0) { |
| 1951 | DBG("Unable to close stream on the relayd. Continuing"); |
| 1952 | /* |
| 1953 | * Continue here. There is nothing we can do for the relayd. |
| 1954 | * Chances are that the relayd has closed the socket so we just |
| 1955 | * continue cleaning up. |
| 1956 | */ |
| 1957 | } |
| 1958 | |
| 1959 | /* Both conditions are met, we destroy the relayd. */ |
| 1960 | if (uatomic_read(&relayd->refcount) == 0 && |
| 1961 | uatomic_read(&relayd->destroy_flag)) { |
| 1962 | destroy_relayd(relayd); |
| 1963 | } |
| 1964 | } |
| 1965 | rcu_read_unlock(); |
| 1966 | |
| 1967 | /* Atomically decrement channel refcount since other threads can use it. */ |
| 1968 | if (!uatomic_sub_return(&stream->chan->refcount, 1) |
| 1969 | && !uatomic_read(&stream->chan->nb_init_stream_left)) { |
| 1970 | /* Go for channel deletion! */ |
| 1971 | free_chan = stream->chan; |
| 1972 | } |
| 1973 | |
| 1974 | end: |
| 1975 | /* |
| 1976 | * Nullify the stream reference so it is not used after deletion. The |
| 1977 | * consumer data lock MUST be acquired before being able to check for a |
| 1978 | * NULL pointer value. |
| 1979 | */ |
| 1980 | stream->chan->metadata_stream = NULL; |
| 1981 | |
| 1982 | pthread_mutex_unlock(&stream->lock); |
| 1983 | pthread_mutex_unlock(&consumer_data.lock); |
| 1984 | |
| 1985 | if (free_chan) { |
| 1986 | consumer_del_channel(free_chan); |
| 1987 | } |
| 1988 | |
| 1989 | free_stream_rcu: |
| 1990 | call_rcu(&stream->node.head, free_stream_rcu); |
| 1991 | } |
| 1992 | |
| 1993 | /* |
| 1994 | * Action done with the metadata stream when adding it to the consumer internal |
| 1995 | * data structures to handle it. |
| 1996 | */ |
| 1997 | static int add_metadata_stream(struct lttng_consumer_stream *stream, |
| 1998 | struct lttng_ht *ht) |
| 1999 | { |
| 2000 | int ret = 0; |
| 2001 | struct consumer_relayd_sock_pair *relayd; |
| 2002 | struct lttng_ht_iter iter; |
| 2003 | struct lttng_ht_node_u64 *node; |
| 2004 | |
| 2005 | assert(stream); |
| 2006 | assert(ht); |
| 2007 | |
| 2008 | DBG3("Adding metadata stream %" PRIu64 " to hash table", stream->key); |
| 2009 | |
| 2010 | pthread_mutex_lock(&consumer_data.lock); |
| 2011 | pthread_mutex_lock(&stream->lock); |
| 2012 | |
| 2013 | /* |
| 2014 | * From here, refcounts are updated so be _careful_ when returning an error |
| 2015 | * after this point. |
| 2016 | */ |
| 2017 | |
| 2018 | rcu_read_lock(); |
| 2019 | |
| 2020 | /* |
| 2021 | * Lookup the stream just to make sure it does not exist in our internal |
| 2022 | * state. This should NEVER happen. |
| 2023 | */ |
| 2024 | lttng_ht_lookup(ht, &stream->key, &iter); |
| 2025 | node = lttng_ht_iter_get_node_u64(&iter); |
| 2026 | assert(!node); |
| 2027 | |
| 2028 | /* Find relayd and, if one is found, increment refcount. */ |
| 2029 | relayd = consumer_find_relayd(stream->net_seq_idx); |
| 2030 | if (relayd != NULL) { |
| 2031 | uatomic_inc(&relayd->refcount); |
| 2032 | } |
| 2033 | |
| 2034 | /* |
| 2035 | * When nb_init_stream_left reaches 0, we don't need to trigger any action |
| 2036 | * in terms of destroying the associated channel, because the action that |
| 2037 | * causes the count to become 0 also causes a stream to be added. The |
| 2038 | * channel deletion will thus be triggered by the following removal of this |
| 2039 | * stream. |
| 2040 | */ |
| 2041 | if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) { |
| 2042 | /* Increment refcount before decrementing nb_init_stream_left */ |
| 2043 | cmm_smp_wmb(); |
| 2044 | uatomic_dec(&stream->chan->nb_init_stream_left); |
| 2045 | } |
| 2046 | |
| 2047 | lttng_ht_add_unique_u64(ht, &stream->node); |
| 2048 | |
| 2049 | lttng_ht_add_unique_u64(consumer_data.stream_per_chan_id_ht, |
| 2050 | &stream->node_channel_id); |
| 2051 | |
| 2052 | /* |
| 2053 | * Add stream to the stream_list_ht of the consumer data. No need to steal |
| 2054 | * the key since the HT does not use it and we allow to add redundant keys |
| 2055 | * into this table. |
| 2056 | */ |
| 2057 | lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id); |
| 2058 | |
| 2059 | rcu_read_unlock(); |
| 2060 | |
| 2061 | pthread_mutex_unlock(&stream->lock); |
| 2062 | pthread_mutex_unlock(&consumer_data.lock); |
| 2063 | return ret; |
| 2064 | } |
| 2065 | |
| 2066 | /* |
| 2067 | * Delete data stream that are flagged for deletion (endpoint_status). |
| 2068 | */ |
| 2069 | static void validate_endpoint_status_data_stream(void) |
| 2070 | { |
| 2071 | struct lttng_ht_iter iter; |
| 2072 | struct lttng_consumer_stream *stream; |
| 2073 | |
| 2074 | DBG("Consumer delete flagged data stream"); |
| 2075 | |
| 2076 | rcu_read_lock(); |
| 2077 | cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) { |
| 2078 | /* Validate delete flag of the stream */ |
| 2079 | if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) { |
| 2080 | continue; |
| 2081 | } |
| 2082 | /* Delete it right now */ |
| 2083 | consumer_del_stream(stream, data_ht); |
| 2084 | } |
| 2085 | rcu_read_unlock(); |
| 2086 | } |
| 2087 | |
| 2088 | /* |
| 2089 | * Delete metadata stream that are flagged for deletion (endpoint_status). |
| 2090 | */ |
| 2091 | static void validate_endpoint_status_metadata_stream( |
| 2092 | struct lttng_poll_event *pollset) |
| 2093 | { |
| 2094 | struct lttng_ht_iter iter; |
| 2095 | struct lttng_consumer_stream *stream; |
| 2096 | |
| 2097 | DBG("Consumer delete flagged metadata stream"); |
| 2098 | |
| 2099 | assert(pollset); |
| 2100 | |
| 2101 | rcu_read_lock(); |
| 2102 | cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) { |
| 2103 | /* Validate delete flag of the stream */ |
| 2104 | if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) { |
| 2105 | continue; |
| 2106 | } |
| 2107 | /* |
| 2108 | * Remove from pollset so the metadata thread can continue without |
| 2109 | * blocking on a deleted stream. |
| 2110 | */ |
| 2111 | lttng_poll_del(pollset, stream->wait_fd); |
| 2112 | |
| 2113 | /* Delete it right now */ |
| 2114 | consumer_del_metadata_stream(stream, metadata_ht); |
| 2115 | } |
| 2116 | rcu_read_unlock(); |
| 2117 | } |
| 2118 | |
| 2119 | /* |
| 2120 | * Thread polls on metadata file descriptor and write them on disk or on the |
| 2121 | * network. |
| 2122 | */ |
| 2123 | void *consumer_thread_metadata_poll(void *data) |
| 2124 | { |
| 2125 | int ret, i, pollfd; |
| 2126 | uint32_t revents, nb_fd; |
| 2127 | struct lttng_consumer_stream *stream = NULL; |
| 2128 | struct lttng_ht_iter iter; |
| 2129 | struct lttng_ht_node_u64 *node; |
| 2130 | struct lttng_poll_event events; |
| 2131 | struct lttng_consumer_local_data *ctx = data; |
| 2132 | ssize_t len; |
| 2133 | |
| 2134 | rcu_register_thread(); |
| 2135 | |
| 2136 | metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 2137 | if (!metadata_ht) { |
| 2138 | /* ENOMEM at this point. Better to bail out. */ |
| 2139 | goto end_ht; |
| 2140 | } |
| 2141 | |
| 2142 | DBG("Thread metadata poll started"); |
| 2143 | |
| 2144 | /* Size is set to 1 for the consumer_metadata pipe */ |
| 2145 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); |
| 2146 | if (ret < 0) { |
| 2147 | ERR("Poll set creation failed"); |
| 2148 | goto end_poll; |
| 2149 | } |
| 2150 | |
| 2151 | ret = lttng_poll_add(&events, |
| 2152 | lttng_pipe_get_readfd(ctx->consumer_metadata_pipe), LPOLLIN); |
| 2153 | if (ret < 0) { |
| 2154 | goto end; |
| 2155 | } |
| 2156 | |
| 2157 | /* Main loop */ |
| 2158 | DBG("Metadata main loop started"); |
| 2159 | |
| 2160 | while (1) { |
| 2161 | /* Only the metadata pipe is set */ |
| 2162 | if (LTTNG_POLL_GETNB(&events) == 0 && consumer_quit == 1) { |
| 2163 | goto end; |
| 2164 | } |
| 2165 | |
| 2166 | restart: |
| 2167 | DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events)); |
| 2168 | ret = lttng_poll_wait(&events, -1); |
| 2169 | DBG("Metadata event catched in thread"); |
| 2170 | if (ret < 0) { |
| 2171 | if (errno == EINTR) { |
| 2172 | ERR("Poll EINTR catched"); |
| 2173 | goto restart; |
| 2174 | } |
| 2175 | goto error; |
| 2176 | } |
| 2177 | |
| 2178 | nb_fd = ret; |
| 2179 | |
| 2180 | /* From here, the event is a metadata wait fd */ |
| 2181 | for (i = 0; i < nb_fd; i++) { |
| 2182 | revents = LTTNG_POLL_GETEV(&events, i); |
| 2183 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 2184 | |
| 2185 | /* Just don't waste time if no returned events for the fd */ |
| 2186 | if (!revents) { |
| 2187 | continue; |
| 2188 | } |
| 2189 | |
| 2190 | if (pollfd == lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)) { |
| 2191 | if (revents & (LPOLLERR | LPOLLHUP )) { |
| 2192 | DBG("Metadata thread pipe hung up"); |
| 2193 | /* |
| 2194 | * Remove the pipe from the poll set and continue the loop |
| 2195 | * since their might be data to consume. |
| 2196 | */ |
| 2197 | lttng_poll_del(&events, |
| 2198 | lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)); |
| 2199 | lttng_pipe_read_close(ctx->consumer_metadata_pipe); |
| 2200 | continue; |
| 2201 | } else if (revents & LPOLLIN) { |
| 2202 | ssize_t pipe_len; |
| 2203 | |
| 2204 | pipe_len = lttng_pipe_read(ctx->consumer_metadata_pipe, |
| 2205 | &stream, sizeof(stream)); |
| 2206 | if (pipe_len < 0) { |
| 2207 | ERR("read metadata stream, ret: %ld", pipe_len); |
| 2208 | /* |
| 2209 | * Continue here to handle the rest of the streams. |
| 2210 | */ |
| 2211 | continue; |
| 2212 | } |
| 2213 | |
| 2214 | /* A NULL stream means that the state has changed. */ |
| 2215 | if (stream == NULL) { |
| 2216 | /* Check for deleted streams. */ |
| 2217 | validate_endpoint_status_metadata_stream(&events); |
| 2218 | goto restart; |
| 2219 | } |
| 2220 | |
| 2221 | DBG("Adding metadata stream %d to poll set", |
| 2222 | stream->wait_fd); |
| 2223 | |
| 2224 | ret = add_metadata_stream(stream, metadata_ht); |
| 2225 | if (ret) { |
| 2226 | ERR("Unable to add metadata stream"); |
| 2227 | /* Stream was not setup properly. Continuing. */ |
| 2228 | consumer_del_metadata_stream(stream, NULL); |
| 2229 | continue; |
| 2230 | } |
| 2231 | |
| 2232 | /* Add metadata stream to the global poll events list */ |
| 2233 | lttng_poll_add(&events, stream->wait_fd, |
| 2234 | LPOLLIN | LPOLLPRI); |
| 2235 | } |
| 2236 | |
| 2237 | /* Handle other stream */ |
| 2238 | continue; |
| 2239 | } |
| 2240 | |
| 2241 | rcu_read_lock(); |
| 2242 | { |
| 2243 | uint64_t tmp_id = (uint64_t) pollfd; |
| 2244 | |
| 2245 | lttng_ht_lookup(metadata_ht, &tmp_id, &iter); |
| 2246 | } |
| 2247 | node = lttng_ht_iter_get_node_u64(&iter); |
| 2248 | assert(node); |
| 2249 | |
| 2250 | stream = caa_container_of(node, struct lttng_consumer_stream, |
| 2251 | node); |
| 2252 | |
| 2253 | /* Check for error event */ |
| 2254 | if (revents & (LPOLLERR | LPOLLHUP)) { |
| 2255 | DBG("Metadata fd %d is hup|err.", pollfd); |
| 2256 | if (!stream->hangup_flush_done |
| 2257 | && (consumer_data.type == LTTNG_CONSUMER32_UST |
| 2258 | || consumer_data.type == LTTNG_CONSUMER64_UST)) { |
| 2259 | DBG("Attempting to flush and consume the UST buffers"); |
| 2260 | lttng_ustconsumer_on_stream_hangup(stream); |
| 2261 | |
| 2262 | /* We just flushed the stream now read it. */ |
| 2263 | do { |
| 2264 | len = ctx->on_buffer_ready(stream, ctx); |
| 2265 | /* |
| 2266 | * We don't check the return value here since if we get |
| 2267 | * a negative len, it means an error occured thus we |
| 2268 | * simply remove it from the poll set and free the |
| 2269 | * stream. |
| 2270 | */ |
| 2271 | } while (len > 0); |
| 2272 | } |
| 2273 | |
| 2274 | lttng_poll_del(&events, stream->wait_fd); |
| 2275 | /* |
| 2276 | * This call update the channel states, closes file descriptors |
| 2277 | * and securely free the stream. |
| 2278 | */ |
| 2279 | consumer_del_metadata_stream(stream, metadata_ht); |
| 2280 | } else if (revents & (LPOLLIN | LPOLLPRI)) { |
| 2281 | /* Get the data out of the metadata file descriptor */ |
| 2282 | DBG("Metadata available on fd %d", pollfd); |
| 2283 | assert(stream->wait_fd == pollfd); |
| 2284 | |
| 2285 | len = ctx->on_buffer_ready(stream, ctx); |
| 2286 | /* It's ok to have an unavailable sub-buffer */ |
| 2287 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { |
| 2288 | /* Clean up stream from consumer and free it. */ |
| 2289 | lttng_poll_del(&events, stream->wait_fd); |
| 2290 | consumer_del_metadata_stream(stream, metadata_ht); |
| 2291 | } else if (len > 0) { |
| 2292 | stream->data_read = 1; |
| 2293 | } |
| 2294 | } |
| 2295 | |
| 2296 | /* Release RCU lock for the stream looked up */ |
| 2297 | rcu_read_unlock(); |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | error: |
| 2302 | end: |
| 2303 | DBG("Metadata poll thread exiting"); |
| 2304 | |
| 2305 | lttng_poll_clean(&events); |
| 2306 | end_poll: |
| 2307 | destroy_stream_ht(metadata_ht); |
| 2308 | end_ht: |
| 2309 | rcu_unregister_thread(); |
| 2310 | return NULL; |
| 2311 | } |
| 2312 | |
| 2313 | /* |
| 2314 | * This thread polls the fds in the set to consume the data and write |
| 2315 | * it to tracefile if necessary. |
| 2316 | */ |
| 2317 | void *consumer_thread_data_poll(void *data) |
| 2318 | { |
| 2319 | int num_rdy, num_hup, high_prio, ret, i; |
| 2320 | struct pollfd *pollfd = NULL; |
| 2321 | /* local view of the streams */ |
| 2322 | struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL; |
| 2323 | /* local view of consumer_data.fds_count */ |
| 2324 | int nb_fd = 0; |
| 2325 | struct lttng_consumer_local_data *ctx = data; |
| 2326 | ssize_t len; |
| 2327 | |
| 2328 | rcu_register_thread(); |
| 2329 | |
| 2330 | data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 2331 | if (data_ht == NULL) { |
| 2332 | /* ENOMEM at this point. Better to bail out. */ |
| 2333 | goto end; |
| 2334 | } |
| 2335 | |
| 2336 | local_stream = zmalloc(sizeof(struct lttng_consumer_stream)); |
| 2337 | |
| 2338 | while (1) { |
| 2339 | high_prio = 0; |
| 2340 | num_hup = 0; |
| 2341 | |
| 2342 | /* |
| 2343 | * the fds set has been updated, we need to update our |
| 2344 | * local array as well |
| 2345 | */ |
| 2346 | pthread_mutex_lock(&consumer_data.lock); |
| 2347 | if (consumer_data.need_update) { |
| 2348 | free(pollfd); |
| 2349 | pollfd = NULL; |
| 2350 | |
| 2351 | free(local_stream); |
| 2352 | local_stream = NULL; |
| 2353 | |
| 2354 | /* allocate for all fds + 1 for the consumer_data_pipe */ |
| 2355 | pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd)); |
| 2356 | if (pollfd == NULL) { |
| 2357 | PERROR("pollfd malloc"); |
| 2358 | pthread_mutex_unlock(&consumer_data.lock); |
| 2359 | goto end; |
| 2360 | } |
| 2361 | |
| 2362 | /* allocate for all fds + 1 for the consumer_data_pipe */ |
| 2363 | local_stream = zmalloc((consumer_data.stream_count + 1) * |
| 2364 | sizeof(struct lttng_consumer_stream *)); |
| 2365 | if (local_stream == NULL) { |
| 2366 | PERROR("local_stream malloc"); |
| 2367 | pthread_mutex_unlock(&consumer_data.lock); |
| 2368 | goto end; |
| 2369 | } |
| 2370 | ret = update_poll_array(ctx, &pollfd, local_stream, |
| 2371 | data_ht); |
| 2372 | if (ret < 0) { |
| 2373 | ERR("Error in allocating pollfd or local_outfds"); |
| 2374 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
| 2375 | pthread_mutex_unlock(&consumer_data.lock); |
| 2376 | goto end; |
| 2377 | } |
| 2378 | nb_fd = ret; |
| 2379 | consumer_data.need_update = 0; |
| 2380 | } |
| 2381 | pthread_mutex_unlock(&consumer_data.lock); |
| 2382 | |
| 2383 | /* No FDs and consumer_quit, consumer_cleanup the thread */ |
| 2384 | if (nb_fd == 0 && consumer_quit == 1) { |
| 2385 | goto end; |
| 2386 | } |
| 2387 | /* poll on the array of fds */ |
| 2388 | restart: |
| 2389 | DBG("polling on %d fd", nb_fd + 1); |
| 2390 | num_rdy = poll(pollfd, nb_fd + 1, -1); |
| 2391 | DBG("poll num_rdy : %d", num_rdy); |
| 2392 | if (num_rdy == -1) { |
| 2393 | /* |
| 2394 | * Restart interrupted system call. |
| 2395 | */ |
| 2396 | if (errno == EINTR) { |
| 2397 | goto restart; |
| 2398 | } |
| 2399 | PERROR("Poll error"); |
| 2400 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
| 2401 | goto end; |
| 2402 | } else if (num_rdy == 0) { |
| 2403 | DBG("Polling thread timed out"); |
| 2404 | goto end; |
| 2405 | } |
| 2406 | |
| 2407 | /* |
| 2408 | * If the consumer_data_pipe triggered poll go directly to the |
| 2409 | * beginning of the loop to update the array. We want to prioritize |
| 2410 | * array update over low-priority reads. |
| 2411 | */ |
| 2412 | if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) { |
| 2413 | ssize_t pipe_readlen; |
| 2414 | |
| 2415 | DBG("consumer_data_pipe wake up"); |
| 2416 | pipe_readlen = lttng_pipe_read(ctx->consumer_data_pipe, |
| 2417 | &new_stream, sizeof(new_stream)); |
| 2418 | if (pipe_readlen < 0) { |
| 2419 | ERR("Consumer data pipe ret %ld", pipe_readlen); |
| 2420 | /* Continue so we can at least handle the current stream(s). */ |
| 2421 | continue; |
| 2422 | } |
| 2423 | |
| 2424 | /* |
| 2425 | * If the stream is NULL, just ignore it. It's also possible that |
| 2426 | * the sessiond poll thread changed the consumer_quit state and is |
| 2427 | * waking us up to test it. |
| 2428 | */ |
| 2429 | if (new_stream == NULL) { |
| 2430 | validate_endpoint_status_data_stream(); |
| 2431 | continue; |
| 2432 | } |
| 2433 | |
| 2434 | ret = add_stream(new_stream, data_ht); |
| 2435 | if (ret) { |
| 2436 | ERR("Consumer add stream %" PRIu64 " failed. Continuing", |
| 2437 | new_stream->key); |
| 2438 | /* |
| 2439 | * At this point, if the add_stream fails, it is not in the |
| 2440 | * hash table thus passing the NULL value here. |
| 2441 | */ |
| 2442 | consumer_del_stream(new_stream, NULL); |
| 2443 | } |
| 2444 | |
| 2445 | /* Continue to update the local streams and handle prio ones */ |
| 2446 | continue; |
| 2447 | } |
| 2448 | |
| 2449 | /* Take care of high priority channels first. */ |
| 2450 | for (i = 0; i < nb_fd; i++) { |
| 2451 | if (local_stream[i] == NULL) { |
| 2452 | continue; |
| 2453 | } |
| 2454 | if (pollfd[i].revents & POLLPRI) { |
| 2455 | DBG("Urgent read on fd %d", pollfd[i].fd); |
| 2456 | high_prio = 1; |
| 2457 | len = ctx->on_buffer_ready(local_stream[i], ctx); |
| 2458 | /* it's ok to have an unavailable sub-buffer */ |
| 2459 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { |
| 2460 | /* Clean the stream and free it. */ |
| 2461 | consumer_del_stream(local_stream[i], data_ht); |
| 2462 | local_stream[i] = NULL; |
| 2463 | } else if (len > 0) { |
| 2464 | local_stream[i]->data_read = 1; |
| 2465 | } |
| 2466 | } |
| 2467 | } |
| 2468 | |
| 2469 | /* |
| 2470 | * If we read high prio channel in this loop, try again |
| 2471 | * for more high prio data. |
| 2472 | */ |
| 2473 | if (high_prio) { |
| 2474 | continue; |
| 2475 | } |
| 2476 | |
| 2477 | /* Take care of low priority channels. */ |
| 2478 | for (i = 0; i < nb_fd; i++) { |
| 2479 | if (local_stream[i] == NULL) { |
| 2480 | continue; |
| 2481 | } |
| 2482 | if ((pollfd[i].revents & POLLIN) || |
| 2483 | local_stream[i]->hangup_flush_done) { |
| 2484 | DBG("Normal read on fd %d", pollfd[i].fd); |
| 2485 | len = ctx->on_buffer_ready(local_stream[i], ctx); |
| 2486 | /* it's ok to have an unavailable sub-buffer */ |
| 2487 | if (len < 0 && len != -EAGAIN && len != -ENODATA) { |
| 2488 | /* Clean the stream and free it. */ |
| 2489 | consumer_del_stream(local_stream[i], data_ht); |
| 2490 | local_stream[i] = NULL; |
| 2491 | } else if (len > 0) { |
| 2492 | local_stream[i]->data_read = 1; |
| 2493 | } |
| 2494 | } |
| 2495 | } |
| 2496 | |
| 2497 | /* Handle hangup and errors */ |
| 2498 | for (i = 0; i < nb_fd; i++) { |
| 2499 | if (local_stream[i] == NULL) { |
| 2500 | continue; |
| 2501 | } |
| 2502 | if (!local_stream[i]->hangup_flush_done |
| 2503 | && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL)) |
| 2504 | && (consumer_data.type == LTTNG_CONSUMER32_UST |
| 2505 | || consumer_data.type == LTTNG_CONSUMER64_UST)) { |
| 2506 | DBG("fd %d is hup|err|nval. Attempting flush and read.", |
| 2507 | pollfd[i].fd); |
| 2508 | lttng_ustconsumer_on_stream_hangup(local_stream[i]); |
| 2509 | /* Attempt read again, for the data we just flushed. */ |
| 2510 | local_stream[i]->data_read = 1; |
| 2511 | } |
| 2512 | /* |
| 2513 | * If the poll flag is HUP/ERR/NVAL and we have |
| 2514 | * read no data in this pass, we can remove the |
| 2515 | * stream from its hash table. |
| 2516 | */ |
| 2517 | if ((pollfd[i].revents & POLLHUP)) { |
| 2518 | DBG("Polling fd %d tells it has hung up.", pollfd[i].fd); |
| 2519 | if (!local_stream[i]->data_read) { |
| 2520 | consumer_del_stream(local_stream[i], data_ht); |
| 2521 | local_stream[i] = NULL; |
| 2522 | num_hup++; |
| 2523 | } |
| 2524 | } else if (pollfd[i].revents & POLLERR) { |
| 2525 | ERR("Error returned in polling fd %d.", pollfd[i].fd); |
| 2526 | if (!local_stream[i]->data_read) { |
| 2527 | consumer_del_stream(local_stream[i], data_ht); |
| 2528 | local_stream[i] = NULL; |
| 2529 | num_hup++; |
| 2530 | } |
| 2531 | } else if (pollfd[i].revents & POLLNVAL) { |
| 2532 | ERR("Polling fd %d tells fd is not open.", pollfd[i].fd); |
| 2533 | if (!local_stream[i]->data_read) { |
| 2534 | consumer_del_stream(local_stream[i], data_ht); |
| 2535 | local_stream[i] = NULL; |
| 2536 | num_hup++; |
| 2537 | } |
| 2538 | } |
| 2539 | if (local_stream[i] != NULL) { |
| 2540 | local_stream[i]->data_read = 0; |
| 2541 | } |
| 2542 | } |
| 2543 | } |
| 2544 | end: |
| 2545 | DBG("polling thread exiting"); |
| 2546 | free(pollfd); |
| 2547 | free(local_stream); |
| 2548 | |
| 2549 | /* |
| 2550 | * Close the write side of the pipe so epoll_wait() in |
| 2551 | * consumer_thread_metadata_poll can catch it. The thread is monitoring the |
| 2552 | * read side of the pipe. If we close them both, epoll_wait strangely does |
| 2553 | * not return and could create a endless wait period if the pipe is the |
| 2554 | * only tracked fd in the poll set. The thread will take care of closing |
| 2555 | * the read side. |
| 2556 | */ |
| 2557 | (void) lttng_pipe_write_close(ctx->consumer_metadata_pipe); |
| 2558 | |
| 2559 | destroy_data_stream_ht(data_ht); |
| 2560 | |
| 2561 | rcu_unregister_thread(); |
| 2562 | return NULL; |
| 2563 | } |
| 2564 | |
| 2565 | /* |
| 2566 | * Close wake-up end of each stream belonging to the channel. This will |
| 2567 | * allow the poll() on the stream read-side to detect when the |
| 2568 | * write-side (application) finally closes them. |
| 2569 | */ |
| 2570 | static |
| 2571 | void consumer_close_channel_streams(struct lttng_consumer_channel *channel) |
| 2572 | { |
| 2573 | struct lttng_ht *ht; |
| 2574 | struct lttng_consumer_stream *stream; |
| 2575 | struct lttng_ht_iter iter; |
| 2576 | |
| 2577 | ht = consumer_data.stream_per_chan_id_ht; |
| 2578 | |
| 2579 | rcu_read_lock(); |
| 2580 | cds_lfht_for_each_entry_duplicate(ht->ht, |
| 2581 | ht->hash_fct(&channel->key, lttng_ht_seed), |
| 2582 | ht->match_fct, &channel->key, |
| 2583 | &iter.iter, stream, node_channel_id.node) { |
| 2584 | /* |
| 2585 | * Protect against teardown with mutex. |
| 2586 | */ |
| 2587 | pthread_mutex_lock(&stream->lock); |
| 2588 | if (cds_lfht_is_node_deleted(&stream->node.node)) { |
| 2589 | goto next; |
| 2590 | } |
| 2591 | switch (consumer_data.type) { |
| 2592 | case LTTNG_CONSUMER_KERNEL: |
| 2593 | break; |
| 2594 | case LTTNG_CONSUMER32_UST: |
| 2595 | case LTTNG_CONSUMER64_UST: |
| 2596 | /* |
| 2597 | * Note: a mutex is taken internally within |
| 2598 | * liblttng-ust-ctl to protect timer wakeup_fd |
| 2599 | * use from concurrent close. |
| 2600 | */ |
| 2601 | lttng_ustconsumer_close_stream_wakeup(stream); |
| 2602 | break; |
| 2603 | default: |
| 2604 | ERR("Unknown consumer_data type"); |
| 2605 | assert(0); |
| 2606 | } |
| 2607 | next: |
| 2608 | pthread_mutex_unlock(&stream->lock); |
| 2609 | } |
| 2610 | rcu_read_unlock(); |
| 2611 | } |
| 2612 | |
| 2613 | static void destroy_channel_ht(struct lttng_ht *ht) |
| 2614 | { |
| 2615 | struct lttng_ht_iter iter; |
| 2616 | struct lttng_consumer_channel *channel; |
| 2617 | int ret; |
| 2618 | |
| 2619 | if (ht == NULL) { |
| 2620 | return; |
| 2621 | } |
| 2622 | |
| 2623 | rcu_read_lock(); |
| 2624 | cds_lfht_for_each_entry(ht->ht, &iter.iter, channel, wait_fd_node.node) { |
| 2625 | ret = lttng_ht_del(ht, &iter); |
| 2626 | assert(ret != 0); |
| 2627 | } |
| 2628 | rcu_read_unlock(); |
| 2629 | |
| 2630 | lttng_ht_destroy(ht); |
| 2631 | } |
| 2632 | |
| 2633 | /* |
| 2634 | * This thread polls the channel fds to detect when they are being |
| 2635 | * closed. It closes all related streams if the channel is detected as |
| 2636 | * closed. It is currently only used as a shim layer for UST because the |
| 2637 | * consumerd needs to keep the per-stream wakeup end of pipes open for |
| 2638 | * periodical flush. |
| 2639 | */ |
| 2640 | void *consumer_thread_channel_poll(void *data) |
| 2641 | { |
| 2642 | int ret, i, pollfd; |
| 2643 | uint32_t revents, nb_fd; |
| 2644 | struct lttng_consumer_channel *chan = NULL; |
| 2645 | struct lttng_ht_iter iter; |
| 2646 | struct lttng_ht_node_u64 *node; |
| 2647 | struct lttng_poll_event events; |
| 2648 | struct lttng_consumer_local_data *ctx = data; |
| 2649 | struct lttng_ht *channel_ht; |
| 2650 | |
| 2651 | rcu_register_thread(); |
| 2652 | |
| 2653 | channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 2654 | if (!channel_ht) { |
| 2655 | /* ENOMEM at this point. Better to bail out. */ |
| 2656 | goto end_ht; |
| 2657 | } |
| 2658 | |
| 2659 | DBG("Thread channel poll started"); |
| 2660 | |
| 2661 | /* Size is set to 1 for the consumer_channel pipe */ |
| 2662 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); |
| 2663 | if (ret < 0) { |
| 2664 | ERR("Poll set creation failed"); |
| 2665 | goto end_poll; |
| 2666 | } |
| 2667 | |
| 2668 | ret = lttng_poll_add(&events, ctx->consumer_channel_pipe[0], LPOLLIN); |
| 2669 | if (ret < 0) { |
| 2670 | goto end; |
| 2671 | } |
| 2672 | |
| 2673 | /* Main loop */ |
| 2674 | DBG("Channel main loop started"); |
| 2675 | |
| 2676 | while (1) { |
| 2677 | /* Only the channel pipe is set */ |
| 2678 | if (LTTNG_POLL_GETNB(&events) == 0 && consumer_quit == 1) { |
| 2679 | goto end; |
| 2680 | } |
| 2681 | |
| 2682 | restart: |
| 2683 | DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events)); |
| 2684 | ret = lttng_poll_wait(&events, -1); |
| 2685 | DBG("Channel event catched in thread"); |
| 2686 | if (ret < 0) { |
| 2687 | if (errno == EINTR) { |
| 2688 | ERR("Poll EINTR catched"); |
| 2689 | goto restart; |
| 2690 | } |
| 2691 | goto end; |
| 2692 | } |
| 2693 | |
| 2694 | nb_fd = ret; |
| 2695 | |
| 2696 | /* From here, the event is a channel wait fd */ |
| 2697 | for (i = 0; i < nb_fd; i++) { |
| 2698 | revents = LTTNG_POLL_GETEV(&events, i); |
| 2699 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 2700 | |
| 2701 | /* Just don't waste time if no returned events for the fd */ |
| 2702 | if (!revents) { |
| 2703 | continue; |
| 2704 | } |
| 2705 | if (pollfd == ctx->consumer_channel_pipe[0]) { |
| 2706 | if (revents & (LPOLLERR | LPOLLHUP)) { |
| 2707 | DBG("Channel thread pipe hung up"); |
| 2708 | /* |
| 2709 | * Remove the pipe from the poll set and continue the loop |
| 2710 | * since their might be data to consume. |
| 2711 | */ |
| 2712 | lttng_poll_del(&events, ctx->consumer_channel_pipe[0]); |
| 2713 | continue; |
| 2714 | } else if (revents & LPOLLIN) { |
| 2715 | enum consumer_channel_action action; |
| 2716 | uint64_t key; |
| 2717 | |
| 2718 | ret = read_channel_pipe(ctx, &chan, &key, &action); |
| 2719 | if (ret <= 0) { |
| 2720 | ERR("Error reading channel pipe"); |
| 2721 | continue; |
| 2722 | } |
| 2723 | |
| 2724 | switch (action) { |
| 2725 | case CONSUMER_CHANNEL_ADD: |
| 2726 | DBG("Adding channel %d to poll set", |
| 2727 | chan->wait_fd); |
| 2728 | |
| 2729 | lttng_ht_node_init_u64(&chan->wait_fd_node, |
| 2730 | chan->wait_fd); |
| 2731 | rcu_read_lock(); |
| 2732 | lttng_ht_add_unique_u64(channel_ht, |
| 2733 | &chan->wait_fd_node); |
| 2734 | rcu_read_unlock(); |
| 2735 | /* Add channel to the global poll events list */ |
| 2736 | lttng_poll_add(&events, chan->wait_fd, |
| 2737 | LPOLLIN | LPOLLPRI); |
| 2738 | break; |
| 2739 | case CONSUMER_CHANNEL_DEL: |
| 2740 | { |
| 2741 | struct lttng_consumer_stream *stream, *stmp; |
| 2742 | |
| 2743 | rcu_read_lock(); |
| 2744 | chan = consumer_find_channel(key); |
| 2745 | if (!chan) { |
| 2746 | rcu_read_unlock(); |
| 2747 | ERR("UST consumer get channel key %" PRIu64 " not found for del channel", key); |
| 2748 | break; |
| 2749 | } |
| 2750 | lttng_poll_del(&events, chan->wait_fd); |
| 2751 | iter.iter.node = &chan->wait_fd_node.node; |
| 2752 | ret = lttng_ht_del(channel_ht, &iter); |
| 2753 | assert(ret == 0); |
| 2754 | consumer_close_channel_streams(chan); |
| 2755 | |
| 2756 | switch (consumer_data.type) { |
| 2757 | case LTTNG_CONSUMER_KERNEL: |
| 2758 | break; |
| 2759 | case LTTNG_CONSUMER32_UST: |
| 2760 | case LTTNG_CONSUMER64_UST: |
| 2761 | /* Delete streams that might have been left in the stream list. */ |
| 2762 | cds_list_for_each_entry_safe(stream, stmp, &chan->streams.head, |
| 2763 | send_node) { |
| 2764 | cds_list_del(&stream->send_node); |
| 2765 | lttng_ustconsumer_del_stream(stream); |
| 2766 | uatomic_sub(&stream->chan->refcount, 1); |
| 2767 | assert(&chan->refcount); |
| 2768 | free(stream); |
| 2769 | } |
| 2770 | break; |
| 2771 | default: |
| 2772 | ERR("Unknown consumer_data type"); |
| 2773 | assert(0); |
| 2774 | } |
| 2775 | |
| 2776 | /* |
| 2777 | * Release our own refcount. Force channel deletion even if |
| 2778 | * streams were not initialized. |
| 2779 | */ |
| 2780 | if (!uatomic_sub_return(&chan->refcount, 1)) { |
| 2781 | consumer_del_channel(chan); |
| 2782 | } |
| 2783 | rcu_read_unlock(); |
| 2784 | goto restart; |
| 2785 | } |
| 2786 | case CONSUMER_CHANNEL_QUIT: |
| 2787 | /* |
| 2788 | * Remove the pipe from the poll set and continue the loop |
| 2789 | * since their might be data to consume. |
| 2790 | */ |
| 2791 | lttng_poll_del(&events, ctx->consumer_channel_pipe[0]); |
| 2792 | continue; |
| 2793 | default: |
| 2794 | ERR("Unknown action"); |
| 2795 | break; |
| 2796 | } |
| 2797 | } |
| 2798 | |
| 2799 | /* Handle other stream */ |
| 2800 | continue; |
| 2801 | } |
| 2802 | |
| 2803 | rcu_read_lock(); |
| 2804 | { |
| 2805 | uint64_t tmp_id = (uint64_t) pollfd; |
| 2806 | |
| 2807 | lttng_ht_lookup(channel_ht, &tmp_id, &iter); |
| 2808 | } |
| 2809 | node = lttng_ht_iter_get_node_u64(&iter); |
| 2810 | assert(node); |
| 2811 | |
| 2812 | chan = caa_container_of(node, struct lttng_consumer_channel, |
| 2813 | wait_fd_node); |
| 2814 | |
| 2815 | /* Check for error event */ |
| 2816 | if (revents & (LPOLLERR | LPOLLHUP)) { |
| 2817 | DBG("Channel fd %d is hup|err.", pollfd); |
| 2818 | |
| 2819 | lttng_poll_del(&events, chan->wait_fd); |
| 2820 | ret = lttng_ht_del(channel_ht, &iter); |
| 2821 | assert(ret == 0); |
| 2822 | assert(cds_list_empty(&chan->streams.head)); |
| 2823 | consumer_close_channel_streams(chan); |
| 2824 | |
| 2825 | /* Release our own refcount */ |
| 2826 | if (!uatomic_sub_return(&chan->refcount, 1) |
| 2827 | && !uatomic_read(&chan->nb_init_stream_left)) { |
| 2828 | consumer_del_channel(chan); |
| 2829 | } |
| 2830 | } |
| 2831 | |
| 2832 | /* Release RCU lock for the channel looked up */ |
| 2833 | rcu_read_unlock(); |
| 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | end: |
| 2838 | lttng_poll_clean(&events); |
| 2839 | end_poll: |
| 2840 | destroy_channel_ht(channel_ht); |
| 2841 | end_ht: |
| 2842 | DBG("Channel poll thread exiting"); |
| 2843 | rcu_unregister_thread(); |
| 2844 | return NULL; |
| 2845 | } |
| 2846 | |
| 2847 | static int set_metadata_socket(struct lttng_consumer_local_data *ctx, |
| 2848 | struct pollfd *sockpoll, int client_socket) |
| 2849 | { |
| 2850 | int ret; |
| 2851 | |
| 2852 | assert(ctx); |
| 2853 | assert(sockpoll); |
| 2854 | |
| 2855 | if (lttng_consumer_poll_socket(sockpoll) < 0) { |
| 2856 | ret = -1; |
| 2857 | goto error; |
| 2858 | } |
| 2859 | DBG("Metadata connection on client_socket"); |
| 2860 | |
| 2861 | /* Blocking call, waiting for transmission */ |
| 2862 | ctx->consumer_metadata_socket = lttcomm_accept_unix_sock(client_socket); |
| 2863 | if (ctx->consumer_metadata_socket < 0) { |
| 2864 | WARN("On accept metadata"); |
| 2865 | ret = -1; |
| 2866 | goto error; |
| 2867 | } |
| 2868 | ret = 0; |
| 2869 | |
| 2870 | error: |
| 2871 | return ret; |
| 2872 | } |
| 2873 | |
| 2874 | /* |
| 2875 | * This thread listens on the consumerd socket and receives the file |
| 2876 | * descriptors from the session daemon. |
| 2877 | */ |
| 2878 | void *consumer_thread_sessiond_poll(void *data) |
| 2879 | { |
| 2880 | int sock = -1, client_socket, ret; |
| 2881 | /* |
| 2882 | * structure to poll for incoming data on communication socket avoids |
| 2883 | * making blocking sockets. |
| 2884 | */ |
| 2885 | struct pollfd consumer_sockpoll[2]; |
| 2886 | struct lttng_consumer_local_data *ctx = data; |
| 2887 | |
| 2888 | rcu_register_thread(); |
| 2889 | |
| 2890 | DBG("Creating command socket %s", ctx->consumer_command_sock_path); |
| 2891 | unlink(ctx->consumer_command_sock_path); |
| 2892 | client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path); |
| 2893 | if (client_socket < 0) { |
| 2894 | ERR("Cannot create command socket"); |
| 2895 | goto end; |
| 2896 | } |
| 2897 | |
| 2898 | ret = lttcomm_listen_unix_sock(client_socket); |
| 2899 | if (ret < 0) { |
| 2900 | goto end; |
| 2901 | } |
| 2902 | |
| 2903 | DBG("Sending ready command to lttng-sessiond"); |
| 2904 | ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY); |
| 2905 | /* return < 0 on error, but == 0 is not fatal */ |
| 2906 | if (ret < 0) { |
| 2907 | ERR("Error sending ready command to lttng-sessiond"); |
| 2908 | goto end; |
| 2909 | } |
| 2910 | |
| 2911 | /* prepare the FDs to poll : to client socket and the should_quit pipe */ |
| 2912 | consumer_sockpoll[0].fd = ctx->consumer_should_quit[0]; |
| 2913 | consumer_sockpoll[0].events = POLLIN | POLLPRI; |
| 2914 | consumer_sockpoll[1].fd = client_socket; |
| 2915 | consumer_sockpoll[1].events = POLLIN | POLLPRI; |
| 2916 | |
| 2917 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { |
| 2918 | goto end; |
| 2919 | } |
| 2920 | DBG("Connection on client_socket"); |
| 2921 | |
| 2922 | /* Blocking call, waiting for transmission */ |
| 2923 | sock = lttcomm_accept_unix_sock(client_socket); |
| 2924 | if (sock < 0) { |
| 2925 | WARN("On accept"); |
| 2926 | goto end; |
| 2927 | } |
| 2928 | |
| 2929 | /* |
| 2930 | * Setup metadata socket which is the second socket connection on the |
| 2931 | * command unix socket. |
| 2932 | */ |
| 2933 | ret = set_metadata_socket(ctx, consumer_sockpoll, client_socket); |
| 2934 | if (ret < 0) { |
| 2935 | goto end; |
| 2936 | } |
| 2937 | |
| 2938 | /* This socket is not useful anymore. */ |
| 2939 | ret = close(client_socket); |
| 2940 | if (ret < 0) { |
| 2941 | PERROR("close client_socket"); |
| 2942 | } |
| 2943 | client_socket = -1; |
| 2944 | |
| 2945 | /* update the polling structure to poll on the established socket */ |
| 2946 | consumer_sockpoll[1].fd = sock; |
| 2947 | consumer_sockpoll[1].events = POLLIN | POLLPRI; |
| 2948 | |
| 2949 | while (1) { |
| 2950 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { |
| 2951 | goto end; |
| 2952 | } |
| 2953 | DBG("Incoming command on sock"); |
| 2954 | ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll); |
| 2955 | if (ret == -ENOENT) { |
| 2956 | DBG("Received STOP command"); |
| 2957 | goto end; |
| 2958 | } |
| 2959 | if (ret <= 0) { |
| 2960 | /* |
| 2961 | * This could simply be a session daemon quitting. Don't output |
| 2962 | * ERR() here. |
| 2963 | */ |
| 2964 | DBG("Communication interrupted on command socket"); |
| 2965 | goto end; |
| 2966 | } |
| 2967 | if (consumer_quit) { |
| 2968 | DBG("consumer_thread_receive_fds received quit from signal"); |
| 2969 | goto end; |
| 2970 | } |
| 2971 | DBG("received command on sock"); |
| 2972 | } |
| 2973 | end: |
| 2974 | DBG("Consumer thread sessiond poll exiting"); |
| 2975 | |
| 2976 | /* |
| 2977 | * Close metadata streams since the producer is the session daemon which |
| 2978 | * just died. |
| 2979 | * |
| 2980 | * NOTE: for now, this only applies to the UST tracer. |
| 2981 | */ |
| 2982 | lttng_consumer_close_metadata(); |
| 2983 | |
| 2984 | /* |
| 2985 | * when all fds have hung up, the polling thread |
| 2986 | * can exit cleanly |
| 2987 | */ |
| 2988 | consumer_quit = 1; |
| 2989 | |
| 2990 | /* |
| 2991 | * Notify the data poll thread to poll back again and test the |
| 2992 | * consumer_quit state that we just set so to quit gracefully. |
| 2993 | */ |
| 2994 | notify_thread_lttng_pipe(ctx->consumer_data_pipe); |
| 2995 | |
| 2996 | notify_channel_pipe(ctx, NULL, -1, CONSUMER_CHANNEL_QUIT); |
| 2997 | |
| 2998 | /* Cleaning up possibly open sockets. */ |
| 2999 | if (sock >= 0) { |
| 3000 | ret = close(sock); |
| 3001 | if (ret < 0) { |
| 3002 | PERROR("close sock sessiond poll"); |
| 3003 | } |
| 3004 | } |
| 3005 | if (client_socket >= 0) { |
| 3006 | ret = close(client_socket); |
| 3007 | if (ret < 0) { |
| 3008 | PERROR("close client_socket sessiond poll"); |
| 3009 | } |
| 3010 | } |
| 3011 | |
| 3012 | rcu_unregister_thread(); |
| 3013 | return NULL; |
| 3014 | } |
| 3015 | |
| 3016 | ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream, |
| 3017 | struct lttng_consumer_local_data *ctx) |
| 3018 | { |
| 3019 | ssize_t ret; |
| 3020 | |
| 3021 | pthread_mutex_lock(&stream->lock); |
| 3022 | |
| 3023 | switch (consumer_data.type) { |
| 3024 | case LTTNG_CONSUMER_KERNEL: |
| 3025 | ret = lttng_kconsumer_read_subbuffer(stream, ctx); |
| 3026 | break; |
| 3027 | case LTTNG_CONSUMER32_UST: |
| 3028 | case LTTNG_CONSUMER64_UST: |
| 3029 | ret = lttng_ustconsumer_read_subbuffer(stream, ctx); |
| 3030 | break; |
| 3031 | default: |
| 3032 | ERR("Unknown consumer_data type"); |
| 3033 | assert(0); |
| 3034 | ret = -ENOSYS; |
| 3035 | break; |
| 3036 | } |
| 3037 | |
| 3038 | pthread_mutex_unlock(&stream->lock); |
| 3039 | return ret; |
| 3040 | } |
| 3041 | |
| 3042 | int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream) |
| 3043 | { |
| 3044 | switch (consumer_data.type) { |
| 3045 | case LTTNG_CONSUMER_KERNEL: |
| 3046 | return lttng_kconsumer_on_recv_stream(stream); |
| 3047 | case LTTNG_CONSUMER32_UST: |
| 3048 | case LTTNG_CONSUMER64_UST: |
| 3049 | return lttng_ustconsumer_on_recv_stream(stream); |
| 3050 | default: |
| 3051 | ERR("Unknown consumer_data type"); |
| 3052 | assert(0); |
| 3053 | return -ENOSYS; |
| 3054 | } |
| 3055 | } |
| 3056 | |
| 3057 | /* |
| 3058 | * Allocate and set consumer data hash tables. |
| 3059 | */ |
| 3060 | void lttng_consumer_init(void) |
| 3061 | { |
| 3062 | consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 3063 | consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 3064 | consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 3065 | consumer_data.stream_per_chan_id_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 3066 | } |
| 3067 | |
| 3068 | /* |
| 3069 | * Process the ADD_RELAYD command receive by a consumer. |
| 3070 | * |
| 3071 | * This will create a relayd socket pair and add it to the relayd hash table. |
| 3072 | * The caller MUST acquire a RCU read side lock before calling it. |
| 3073 | */ |
| 3074 | int consumer_add_relayd_socket(uint64_t net_seq_idx, int sock_type, |
| 3075 | struct lttng_consumer_local_data *ctx, int sock, |
| 3076 | struct pollfd *consumer_sockpoll, |
| 3077 | struct lttcomm_relayd_sock *relayd_sock, uint64_t sessiond_id) |
| 3078 | { |
| 3079 | int fd = -1, ret = -1, relayd_created = 0; |
| 3080 | enum lttng_error_code ret_code = LTTNG_OK; |
| 3081 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 3082 | |
| 3083 | assert(ctx); |
| 3084 | assert(relayd_sock); |
| 3085 | |
| 3086 | DBG("Consumer adding relayd socket (idx: %" PRIu64 ")", net_seq_idx); |
| 3087 | |
| 3088 | /* Get relayd reference if exists. */ |
| 3089 | relayd = consumer_find_relayd(net_seq_idx); |
| 3090 | if (relayd == NULL) { |
| 3091 | assert(sock_type == LTTNG_STREAM_CONTROL); |
| 3092 | /* Not found. Allocate one. */ |
| 3093 | relayd = consumer_allocate_relayd_sock_pair(net_seq_idx); |
| 3094 | if (relayd == NULL) { |
| 3095 | ret_code = LTTCOMM_CONSUMERD_ENOMEM; |
| 3096 | ret = -ENOMEM; |
| 3097 | } else { |
| 3098 | relayd->sessiond_session_id = sessiond_id; |
| 3099 | relayd_created = 1; |
| 3100 | } |
| 3101 | |
| 3102 | /* |
| 3103 | * This code path MUST continue to the consumer send status message to |
| 3104 | * we can notify the session daemon and continue our work without |
| 3105 | * killing everything. |
| 3106 | */ |
| 3107 | } else { |
| 3108 | /* |
| 3109 | * relayd key should never be found for control socket. |
| 3110 | */ |
| 3111 | assert(sock_type != LTTNG_STREAM_CONTROL); |
| 3112 | } |
| 3113 | |
| 3114 | /* First send a status message before receiving the fds. */ |
| 3115 | ret = consumer_send_status_msg(sock, ret_code); |
| 3116 | if (ret < 0 || ret_code != LTTNG_OK) { |
| 3117 | /* Somehow, the session daemon is not responding anymore. */ |
| 3118 | goto error; |
| 3119 | } |
| 3120 | |
| 3121 | /* Poll on consumer socket. */ |
| 3122 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { |
| 3123 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR); |
| 3124 | ret = -EINTR; |
| 3125 | goto error; |
| 3126 | } |
| 3127 | |
| 3128 | /* Get relayd socket from session daemon */ |
| 3129 | ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); |
| 3130 | if (ret != sizeof(fd)) { |
| 3131 | ret_code = LTTCOMM_CONSUMERD_ERROR_RECV_FD; |
| 3132 | ret = -1; |
| 3133 | fd = -1; /* Just in case it gets set with an invalid value. */ |
| 3134 | |
| 3135 | /* |
| 3136 | * Failing to receive FDs might indicate a major problem such as |
| 3137 | * reaching a fd limit during the receive where the kernel returns a |
| 3138 | * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we |
| 3139 | * don't take any chances and stop everything. |
| 3140 | * |
| 3141 | * XXX: Feature request #558 will fix that and avoid this possible |
| 3142 | * issue when reaching the fd limit. |
| 3143 | */ |
| 3144 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
| 3145 | |
| 3146 | /* |
| 3147 | * This code path MUST continue to the consumer send status message so |
| 3148 | * we can send the error to the thread expecting a reply. The above |
| 3149 | * call will make everything stop. |
| 3150 | */ |
| 3151 | } |
| 3152 | |
| 3153 | /* We have the fds without error. Send status back. */ |
| 3154 | ret = consumer_send_status_msg(sock, ret_code); |
| 3155 | if (ret < 0 || ret_code != LTTNG_OK) { |
| 3156 | /* Somehow, the session daemon is not responding anymore. */ |
| 3157 | goto error; |
| 3158 | } |
| 3159 | |
| 3160 | /* Copy socket information and received FD */ |
| 3161 | switch (sock_type) { |
| 3162 | case LTTNG_STREAM_CONTROL: |
| 3163 | /* Copy received lttcomm socket */ |
| 3164 | lttcomm_copy_sock(&relayd->control_sock.sock, &relayd_sock->sock); |
| 3165 | ret = lttcomm_create_sock(&relayd->control_sock.sock); |
| 3166 | /* Handle create_sock error. */ |
| 3167 | if (ret < 0) { |
| 3168 | goto error; |
| 3169 | } |
| 3170 | /* |
| 3171 | * Close the socket created internally by |
| 3172 | * lttcomm_create_sock, so we can replace it by the one |
| 3173 | * received from sessiond. |
| 3174 | */ |
| 3175 | if (close(relayd->control_sock.sock.fd)) { |
| 3176 | PERROR("close"); |
| 3177 | } |
| 3178 | |
| 3179 | /* Assign new file descriptor */ |
| 3180 | relayd->control_sock.sock.fd = fd; |
| 3181 | fd = -1; /* For error path */ |
| 3182 | /* Assign version values. */ |
| 3183 | relayd->control_sock.major = relayd_sock->major; |
| 3184 | relayd->control_sock.minor = relayd_sock->minor; |
| 3185 | |
| 3186 | /* |
| 3187 | * Create a session on the relayd and store the returned id. Lock the |
| 3188 | * control socket mutex if the relayd was NOT created before. |
| 3189 | */ |
| 3190 | if (!relayd_created) { |
| 3191 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 3192 | } |
| 3193 | ret = relayd_create_session(&relayd->control_sock, |
| 3194 | &relayd->relayd_session_id); |
| 3195 | if (!relayd_created) { |
| 3196 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 3197 | } |
| 3198 | if (ret < 0) { |
| 3199 | /* |
| 3200 | * Close all sockets of a relayd object. It will be freed if it was |
| 3201 | * created at the error code path or else it will be garbage |
| 3202 | * collect. |
| 3203 | */ |
| 3204 | (void) relayd_close(&relayd->control_sock); |
| 3205 | (void) relayd_close(&relayd->data_sock); |
| 3206 | goto error; |
| 3207 | } |
| 3208 | |
| 3209 | break; |
| 3210 | case LTTNG_STREAM_DATA: |
| 3211 | /* Copy received lttcomm socket */ |
| 3212 | lttcomm_copy_sock(&relayd->data_sock.sock, &relayd_sock->sock); |
| 3213 | ret = lttcomm_create_sock(&relayd->data_sock.sock); |
| 3214 | /* Handle create_sock error. */ |
| 3215 | if (ret < 0) { |
| 3216 | goto error; |
| 3217 | } |
| 3218 | /* |
| 3219 | * Close the socket created internally by |
| 3220 | * lttcomm_create_sock, so we can replace it by the one |
| 3221 | * received from sessiond. |
| 3222 | */ |
| 3223 | if (close(relayd->data_sock.sock.fd)) { |
| 3224 | PERROR("close"); |
| 3225 | } |
| 3226 | |
| 3227 | /* Assign new file descriptor */ |
| 3228 | relayd->data_sock.sock.fd = fd; |
| 3229 | fd = -1; /* for eventual error paths */ |
| 3230 | /* Assign version values. */ |
| 3231 | relayd->data_sock.major = relayd_sock->major; |
| 3232 | relayd->data_sock.minor = relayd_sock->minor; |
| 3233 | break; |
| 3234 | default: |
| 3235 | ERR("Unknown relayd socket type (%d)", sock_type); |
| 3236 | ret = -1; |
| 3237 | goto error; |
| 3238 | } |
| 3239 | |
| 3240 | DBG("Consumer %s socket created successfully with net idx %" PRIu64 " (fd: %d)", |
| 3241 | sock_type == LTTNG_STREAM_CONTROL ? "control" : "data", |
| 3242 | relayd->net_seq_idx, fd); |
| 3243 | |
| 3244 | /* |
| 3245 | * Add relayd socket pair to consumer data hashtable. If object already |
| 3246 | * exists or on error, the function gracefully returns. |
| 3247 | */ |
| 3248 | add_relayd(relayd); |
| 3249 | |
| 3250 | /* All good! */ |
| 3251 | return 0; |
| 3252 | |
| 3253 | error: |
| 3254 | /* Close received socket if valid. */ |
| 3255 | if (fd >= 0) { |
| 3256 | if (close(fd)) { |
| 3257 | PERROR("close received socket"); |
| 3258 | } |
| 3259 | } |
| 3260 | |
| 3261 | if (relayd_created) { |
| 3262 | free(relayd); |
| 3263 | } |
| 3264 | |
| 3265 | return ret; |
| 3266 | } |
| 3267 | |
| 3268 | /* |
| 3269 | * Try to lock the stream mutex. |
| 3270 | * |
| 3271 | * On success, 1 is returned else 0 indicating that the mutex is NOT lock. |
| 3272 | */ |
| 3273 | static int stream_try_lock(struct lttng_consumer_stream *stream) |
| 3274 | { |
| 3275 | int ret; |
| 3276 | |
| 3277 | assert(stream); |
| 3278 | |
| 3279 | /* |
| 3280 | * Try to lock the stream mutex. On failure, we know that the stream is |
| 3281 | * being used else where hence there is data still being extracted. |
| 3282 | */ |
| 3283 | ret = pthread_mutex_trylock(&stream->lock); |
| 3284 | if (ret) { |
| 3285 | /* For both EBUSY and EINVAL error, the mutex is NOT locked. */ |
| 3286 | ret = 0; |
| 3287 | goto end; |
| 3288 | } |
| 3289 | |
| 3290 | ret = 1; |
| 3291 | |
| 3292 | end: |
| 3293 | return ret; |
| 3294 | } |
| 3295 | |
| 3296 | /* |
| 3297 | * Search for a relayd associated to the session id and return the reference. |
| 3298 | * |
| 3299 | * A rcu read side lock MUST be acquire before calling this function and locked |
| 3300 | * until the relayd object is no longer necessary. |
| 3301 | */ |
| 3302 | static struct consumer_relayd_sock_pair *find_relayd_by_session_id(uint64_t id) |
| 3303 | { |
| 3304 | struct lttng_ht_iter iter; |
| 3305 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 3306 | |
| 3307 | /* Iterate over all relayd since they are indexed by net_seq_idx. */ |
| 3308 | cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd, |
| 3309 | node.node) { |
| 3310 | /* |
| 3311 | * Check by sessiond id which is unique here where the relayd session |
| 3312 | * id might not be when having multiple relayd. |
| 3313 | */ |
| 3314 | if (relayd->sessiond_session_id == id) { |
| 3315 | /* Found the relayd. There can be only one per id. */ |
| 3316 | goto found; |
| 3317 | } |
| 3318 | } |
| 3319 | |
| 3320 | return NULL; |
| 3321 | |
| 3322 | found: |
| 3323 | return relayd; |
| 3324 | } |
| 3325 | |
| 3326 | /* |
| 3327 | * Check if for a given session id there is still data needed to be extract |
| 3328 | * from the buffers. |
| 3329 | * |
| 3330 | * Return 1 if data is pending or else 0 meaning ready to be read. |
| 3331 | */ |
| 3332 | int consumer_data_pending(uint64_t id) |
| 3333 | { |
| 3334 | int ret; |
| 3335 | struct lttng_ht_iter iter; |
| 3336 | struct lttng_ht *ht; |
| 3337 | struct lttng_consumer_stream *stream; |
| 3338 | struct consumer_relayd_sock_pair *relayd = NULL; |
| 3339 | int (*data_pending)(struct lttng_consumer_stream *); |
| 3340 | |
| 3341 | DBG("Consumer data pending command on session id %" PRIu64, id); |
| 3342 | |
| 3343 | rcu_read_lock(); |
| 3344 | pthread_mutex_lock(&consumer_data.lock); |
| 3345 | |
| 3346 | switch (consumer_data.type) { |
| 3347 | case LTTNG_CONSUMER_KERNEL: |
| 3348 | data_pending = lttng_kconsumer_data_pending; |
| 3349 | break; |
| 3350 | case LTTNG_CONSUMER32_UST: |
| 3351 | case LTTNG_CONSUMER64_UST: |
| 3352 | data_pending = lttng_ustconsumer_data_pending; |
| 3353 | break; |
| 3354 | default: |
| 3355 | ERR("Unknown consumer data type"); |
| 3356 | assert(0); |
| 3357 | } |
| 3358 | |
| 3359 | /* Ease our life a bit */ |
| 3360 | ht = consumer_data.stream_list_ht; |
| 3361 | |
| 3362 | relayd = find_relayd_by_session_id(id); |
| 3363 | if (relayd) { |
| 3364 | /* Send init command for data pending. */ |
| 3365 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 3366 | ret = relayd_begin_data_pending(&relayd->control_sock, |
| 3367 | relayd->relayd_session_id); |
| 3368 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 3369 | if (ret < 0) { |
| 3370 | /* Communication error thus the relayd so no data pending. */ |
| 3371 | goto data_not_pending; |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | cds_lfht_for_each_entry_duplicate(ht->ht, |
| 3376 | ht->hash_fct(&id, lttng_ht_seed), |
| 3377 | ht->match_fct, &id, |
| 3378 | &iter.iter, stream, node_session_id.node) { |
| 3379 | /* If this call fails, the stream is being used hence data pending. */ |
| 3380 | ret = stream_try_lock(stream); |
| 3381 | if (!ret) { |
| 3382 | goto data_pending; |
| 3383 | } |
| 3384 | |
| 3385 | /* |
| 3386 | * A removed node from the hash table indicates that the stream has |
| 3387 | * been deleted thus having a guarantee that the buffers are closed |
| 3388 | * on the consumer side. However, data can still be transmitted |
| 3389 | * over the network so don't skip the relayd check. |
| 3390 | */ |
| 3391 | ret = cds_lfht_is_node_deleted(&stream->node.node); |
| 3392 | if (!ret) { |
| 3393 | /* Check the stream if there is data in the buffers. */ |
| 3394 | ret = data_pending(stream); |
| 3395 | if (ret == 1) { |
| 3396 | pthread_mutex_unlock(&stream->lock); |
| 3397 | goto data_pending; |
| 3398 | } |
| 3399 | } |
| 3400 | |
| 3401 | /* Relayd check */ |
| 3402 | if (relayd) { |
| 3403 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 3404 | if (stream->metadata_flag) { |
| 3405 | ret = relayd_quiescent_control(&relayd->control_sock, |
| 3406 | stream->relayd_stream_id); |
| 3407 | } else { |
| 3408 | ret = relayd_data_pending(&relayd->control_sock, |
| 3409 | stream->relayd_stream_id, |
| 3410 | stream->next_net_seq_num - 1); |
| 3411 | } |
| 3412 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 3413 | if (ret == 1) { |
| 3414 | pthread_mutex_unlock(&stream->lock); |
| 3415 | goto data_pending; |
| 3416 | } |
| 3417 | } |
| 3418 | pthread_mutex_unlock(&stream->lock); |
| 3419 | } |
| 3420 | |
| 3421 | if (relayd) { |
| 3422 | unsigned int is_data_inflight = 0; |
| 3423 | |
| 3424 | /* Send init command for data pending. */ |
| 3425 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); |
| 3426 | ret = relayd_end_data_pending(&relayd->control_sock, |
| 3427 | relayd->relayd_session_id, &is_data_inflight); |
| 3428 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
| 3429 | if (ret < 0) { |
| 3430 | goto data_not_pending; |
| 3431 | } |
| 3432 | if (is_data_inflight) { |
| 3433 | goto data_pending; |
| 3434 | } |
| 3435 | } |
| 3436 | |
| 3437 | /* |
| 3438 | * Finding _no_ node in the hash table and no inflight data means that the |
| 3439 | * stream(s) have been removed thus data is guaranteed to be available for |
| 3440 | * analysis from the trace files. |
| 3441 | */ |
| 3442 | |
| 3443 | data_not_pending: |
| 3444 | /* Data is available to be read by a viewer. */ |
| 3445 | pthread_mutex_unlock(&consumer_data.lock); |
| 3446 | rcu_read_unlock(); |
| 3447 | return 0; |
| 3448 | |
| 3449 | data_pending: |
| 3450 | /* Data is still being extracted from buffers. */ |
| 3451 | pthread_mutex_unlock(&consumer_data.lock); |
| 3452 | rcu_read_unlock(); |
| 3453 | return 1; |
| 3454 | } |
| 3455 | |
| 3456 | /* |
| 3457 | * Send a ret code status message to the sessiond daemon. |
| 3458 | * |
| 3459 | * Return the sendmsg() return value. |
| 3460 | */ |
| 3461 | int consumer_send_status_msg(int sock, int ret_code) |
| 3462 | { |
| 3463 | struct lttcomm_consumer_status_msg msg; |
| 3464 | |
| 3465 | msg.ret_code = ret_code; |
| 3466 | |
| 3467 | return lttcomm_send_unix_sock(sock, &msg, sizeof(msg)); |
| 3468 | } |
| 3469 | |
| 3470 | /* |
| 3471 | * Send a channel status message to the sessiond daemon. |
| 3472 | * |
| 3473 | * Return the sendmsg() return value. |
| 3474 | */ |
| 3475 | int consumer_send_status_channel(int sock, |
| 3476 | struct lttng_consumer_channel *channel) |
| 3477 | { |
| 3478 | struct lttcomm_consumer_status_channel msg; |
| 3479 | |
| 3480 | assert(sock >= 0); |
| 3481 | |
| 3482 | if (!channel) { |
| 3483 | msg.ret_code = -LTTNG_ERR_UST_CHAN_FAIL; |
| 3484 | } else { |
| 3485 | msg.ret_code = LTTNG_OK; |
| 3486 | msg.key = channel->key; |
| 3487 | msg.stream_count = channel->streams.count; |
| 3488 | } |
| 3489 | |
| 3490 | return lttcomm_send_unix_sock(sock, &msg, sizeof(msg)); |
| 3491 | } |