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