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