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