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