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