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