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