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