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