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