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