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