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