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