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