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