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