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