Fix: consumerd: type confusion in lttng_consumer_send_error
[lttng-tools.git] / src / common / consumer / consumer.c
CommitLineData
3bd1e081 1/*
90c106c6 2 * Copyright (C) 2011 EfficiOS Inc.
ab5be9fa
MJ
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3bd1e081 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
3bd1e081 7 *
3bd1e081
MD
8 */
9
6f9449c2 10#include "common/index/ctf-index.h"
fe7bf564 11#include <stdint.h>
6c1c0768 12#define _LGPL_SOURCE
3bd1e081 13#include <assert.h>
3bd1e081
MD
14#include <poll.h>
15#include <pthread.h>
16#include <stdlib.h>
17#include <string.h>
18#include <sys/mman.h>
19#include <sys/socket.h>
20#include <sys/types.h>
21#include <unistd.h>
77c7c900 22#include <inttypes.h>
331744e3 23#include <signal.h>
3bd1e081 24
51a9e1c7 25#include <bin/lttng-consumerd/health-consumerd.h>
990570ed 26#include <common/common.h>
fb3a43a9 27#include <common/utils.h>
d2956687 28#include <common/time.h>
fb3a43a9 29#include <common/compat/poll.h>
f263b7fd 30#include <common/compat/endian.h>
309167d2 31#include <common/index/index.h>
10a8a223 32#include <common/kernel-ctl/kernel-ctl.h>
00e2e675 33#include <common/sessiond-comm/relayd.h>
10a8a223
DG
34#include <common/sessiond-comm/sessiond-comm.h>
35#include <common/kernel-consumer/kernel-consumer.h>
00e2e675 36#include <common/relayd/relayd.h>
10a8a223 37#include <common/ust-consumer/ust-consumer.h>
c8fea79c
JR
38#include <common/consumer/consumer-timer.h>
39#include <common/consumer/consumer.h>
40#include <common/consumer/consumer-stream.h>
41#include <common/consumer/consumer-testpoint.h>
42#include <common/align.h>
5feafd41 43#include <common/consumer/consumer-metadata-cache.h>
d2956687
JG
44#include <common/trace-chunk.h>
45#include <common/trace-chunk-registry.h>
46#include <common/string-utils/format.h>
c35f9726 47#include <common/dynamic-array.h>
3bd1e081 48
fa29bfbf
SM
49struct lttng_consumer_global_data the_consumer_data = {
50 .stream_count = 0,
51 .need_update = 1,
52 .type = LTTNG_CONSUMER_UNKNOWN,
3bd1e081
MD
53};
54
d8ef542d
MD
55enum consumer_channel_action {
56 CONSUMER_CHANNEL_ADD,
a0cbdd2e 57 CONSUMER_CHANNEL_DEL,
d8ef542d
MD
58 CONSUMER_CHANNEL_QUIT,
59};
60
61struct consumer_channel_msg {
62 enum consumer_channel_action action;
a0cbdd2e
MD
63 struct lttng_consumer_channel *chan; /* add */
64 uint64_t key; /* del */
d8ef542d
MD
65};
66
80957876 67/* Flag used to temporarily pause data consumption from testpoints. */
cf0bcb51
JG
68int data_consumption_paused;
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 */
10211f5c 76int 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
5da88b0f
MD
86static const char *get_consumer_domain(void)
87{
fa29bfbf 88 switch (the_consumer_data.type) {
5da88b0f
MD
89 case LTTNG_CONSUMER_KERNEL:
90 return DEFAULT_KERNEL_TRACE_DIR;
91 case LTTNG_CONSUMER64_UST:
92 /* Fall-through. */
93 case LTTNG_CONSUMER32_UST:
94 return DEFAULT_UST_TRACE_DIR;
95 default:
96 abort();
97 }
98}
99
acdb9057
DG
100/*
101 * Notify a thread lttng pipe to poll back again. This usually means that some
102 * global state has changed so we just send back the thread in a poll wait
103 * call.
104 */
105static void notify_thread_lttng_pipe(struct lttng_pipe *pipe)
106{
107 struct lttng_consumer_stream *null_stream = NULL;
108
109 assert(pipe);
110
111 (void) lttng_pipe_write(pipe, &null_stream, sizeof(null_stream));
112}
113
5c635c72
MD
114static void notify_health_quit_pipe(int *pipe)
115{
6cd525e8 116 ssize_t ret;
5c635c72 117
6cd525e8
MD
118 ret = lttng_write(pipe[1], "4", 1);
119 if (ret < 1) {
5c635c72
MD
120 PERROR("write consumer health quit");
121 }
122}
123
d8ef542d
MD
124static void notify_channel_pipe(struct lttng_consumer_local_data *ctx,
125 struct lttng_consumer_channel *chan,
a0cbdd2e 126 uint64_t key,
d8ef542d
MD
127 enum consumer_channel_action action)
128{
129 struct consumer_channel_msg msg;
6cd525e8 130 ssize_t ret;
d8ef542d 131
e56251fc
DG
132 memset(&msg, 0, sizeof(msg));
133
d8ef542d
MD
134 msg.action = action;
135 msg.chan = chan;
f21dae48 136 msg.key = key;
6cd525e8
MD
137 ret = lttng_write(ctx->consumer_channel_pipe[1], &msg, sizeof(msg));
138 if (ret < sizeof(msg)) {
139 PERROR("notify_channel_pipe write error");
140 }
d8ef542d
MD
141}
142
a0cbdd2e
MD
143void notify_thread_del_channel(struct lttng_consumer_local_data *ctx,
144 uint64_t key)
145{
146 notify_channel_pipe(ctx, NULL, key, CONSUMER_CHANNEL_DEL);
147}
148
d8ef542d
MD
149static int read_channel_pipe(struct lttng_consumer_local_data *ctx,
150 struct lttng_consumer_channel **chan,
a0cbdd2e 151 uint64_t *key,
d8ef542d
MD
152 enum consumer_channel_action *action)
153{
154 struct consumer_channel_msg msg;
6cd525e8 155 ssize_t ret;
d8ef542d 156
6cd525e8
MD
157 ret = lttng_read(ctx->consumer_channel_pipe[0], &msg, sizeof(msg));
158 if (ret < sizeof(msg)) {
159 ret = -1;
160 goto error;
d8ef542d 161 }
6cd525e8
MD
162 *action = msg.action;
163 *chan = msg.chan;
164 *key = msg.key;
165error:
166 return (int) ret;
d8ef542d
MD
167}
168
212d67a2
DG
169/*
170 * Cleanup the stream list of a channel. Those streams are not yet globally
171 * visible
172 */
173static void clean_channel_stream_list(struct lttng_consumer_channel *channel)
174{
175 struct lttng_consumer_stream *stream, *stmp;
176
177 assert(channel);
178
179 /* Delete streams that might have been left in the stream list. */
180 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
181 send_node) {
212d67a2
DG
182 /*
183 * Once a stream is added to this list, the buffers were created so we
184 * have a guarantee that this call will succeed. Setting the monitor
185 * mode to 0 so we don't lock nor try to delete the stream from the
186 * global hash table.
187 */
188 stream->monitor = 0;
189 consumer_stream_destroy(stream, NULL);
190 }
191}
192
3bd1e081
MD
193/*
194 * Find a stream. The consumer_data.lock must be locked during this
195 * call.
196 */
d88aee68 197static struct lttng_consumer_stream *find_stream(uint64_t key,
8389e4f8 198 struct lttng_ht *ht)
3bd1e081 199{
e4421fec 200 struct lttng_ht_iter iter;
d88aee68 201 struct lttng_ht_node_u64 *node;
e4421fec 202 struct lttng_consumer_stream *stream = NULL;
3bd1e081 203
8389e4f8
DG
204 assert(ht);
205
d88aee68
DG
206 /* -1ULL keys are lookup failures */
207 if (key == (uint64_t) -1ULL) {
7ad0a0cb 208 return NULL;
7a57cf92 209 }
e4421fec 210
6065ceec
DG
211 rcu_read_lock();
212
d88aee68
DG
213 lttng_ht_lookup(ht, &key, &iter);
214 node = lttng_ht_iter_get_node_u64(&iter);
e4421fec
DG
215 if (node != NULL) {
216 stream = caa_container_of(node, struct lttng_consumer_stream, node);
3bd1e081 217 }
e4421fec 218
6065ceec
DG
219 rcu_read_unlock();
220
e4421fec 221 return stream;
3bd1e081
MD
222}
223
da009f2c 224static void steal_stream_key(uint64_t key, struct lttng_ht *ht)
7ad0a0cb
MD
225{
226 struct lttng_consumer_stream *stream;
227
04253271 228 rcu_read_lock();
ffe60014 229 stream = find_stream(key, ht);
04253271 230 if (stream) {
da009f2c 231 stream->key = (uint64_t) -1ULL;
04253271
MD
232 /*
233 * We don't want the lookup to match, but we still need
234 * to iterate on this stream when iterating over the hash table. Just
235 * change the node key.
236 */
da009f2c 237 stream->node.key = (uint64_t) -1ULL;
04253271
MD
238 }
239 rcu_read_unlock();
7ad0a0cb
MD
240}
241
d56db448
DG
242/*
243 * Return a channel object for the given key.
244 *
245 * RCU read side lock MUST be acquired before calling this function and
246 * protects the channel ptr.
247 */
d88aee68 248struct lttng_consumer_channel *consumer_find_channel(uint64_t key)
3bd1e081 249{
e4421fec 250 struct lttng_ht_iter iter;
d88aee68 251 struct lttng_ht_node_u64 *node;
e4421fec 252 struct lttng_consumer_channel *channel = NULL;
3bd1e081 253
d88aee68
DG
254 /* -1ULL keys are lookup failures */
255 if (key == (uint64_t) -1ULL) {
7ad0a0cb 256 return NULL;
7a57cf92 257 }
e4421fec 258
fa29bfbf 259 lttng_ht_lookup(the_consumer_data.channel_ht, &key, &iter);
d88aee68 260 node = lttng_ht_iter_get_node_u64(&iter);
e4421fec
DG
261 if (node != NULL) {
262 channel = caa_container_of(node, struct lttng_consumer_channel, node);
3bd1e081 263 }
e4421fec
DG
264
265 return channel;
3bd1e081
MD
266}
267
b5a6470f
DG
268/*
269 * There is a possibility that the consumer does not have enough time between
270 * the close of the channel on the session daemon and the cleanup in here thus
271 * once we have a channel add with an existing key, we know for sure that this
272 * channel will eventually get cleaned up by all streams being closed.
273 *
274 * This function just nullifies the already existing channel key.
275 */
276static void steal_channel_key(uint64_t key)
277{
278 struct lttng_consumer_channel *channel;
279
280 rcu_read_lock();
281 channel = consumer_find_channel(key);
282 if (channel) {
283 channel->key = (uint64_t) -1ULL;
284 /*
285 * We don't want the lookup to match, but we still need to iterate on
286 * this channel when iterating over the hash table. Just change the
287 * node key.
288 */
289 channel->node.key = (uint64_t) -1ULL;
290 }
291 rcu_read_unlock();
292}
293
ffe60014 294static void free_channel_rcu(struct rcu_head *head)
702b1ea4 295{
d88aee68
DG
296 struct lttng_ht_node_u64 *node =
297 caa_container_of(head, struct lttng_ht_node_u64, head);
ffe60014
DG
298 struct lttng_consumer_channel *channel =
299 caa_container_of(node, struct lttng_consumer_channel, node);
702b1ea4 300
fa29bfbf 301 switch (the_consumer_data.type) {
b83e03c4
MD
302 case LTTNG_CONSUMER_KERNEL:
303 break;
304 case LTTNG_CONSUMER32_UST:
305 case LTTNG_CONSUMER64_UST:
306 lttng_ustconsumer_free_channel(channel);
307 break;
308 default:
309 ERR("Unknown consumer_data type");
310 abort();
311 }
ffe60014 312 free(channel);
702b1ea4
MD
313}
314
00e2e675
DG
315/*
316 * RCU protected relayd socket pair free.
317 */
ffe60014 318static void free_relayd_rcu(struct rcu_head *head)
00e2e675 319{
d88aee68
DG
320 struct lttng_ht_node_u64 *node =
321 caa_container_of(head, struct lttng_ht_node_u64, head);
00e2e675
DG
322 struct consumer_relayd_sock_pair *relayd =
323 caa_container_of(node, struct consumer_relayd_sock_pair, node);
324
8994307f
DG
325 /*
326 * Close all sockets. This is done in the call RCU since we don't want the
327 * socket fds to be reassigned thus potentially creating bad state of the
328 * relayd object.
329 *
330 * We do not have to lock the control socket mutex here since at this stage
331 * there is no one referencing to this relayd object.
332 */
333 (void) relayd_close(&relayd->control_sock);
334 (void) relayd_close(&relayd->data_sock);
335
3a84e2f3 336 pthread_mutex_destroy(&relayd->ctrl_sock_mutex);
00e2e675
DG
337 free(relayd);
338}
339
340/*
341 * Destroy and free relayd socket pair object.
00e2e675 342 */
51230d70 343void consumer_destroy_relayd(struct consumer_relayd_sock_pair *relayd)
00e2e675
DG
344{
345 int ret;
346 struct lttng_ht_iter iter;
347
173af62f
DG
348 if (relayd == NULL) {
349 return;
350 }
351
00e2e675
DG
352 DBG("Consumer destroy and close relayd socket pair");
353
354 iter.iter.node = &relayd->node.node;
fa29bfbf 355 ret = lttng_ht_del(the_consumer_data.relayd_ht, &iter);
173af62f 356 if (ret != 0) {
8994307f 357 /* We assume the relayd is being or is destroyed */
173af62f
DG
358 return;
359 }
00e2e675 360
00e2e675 361 /* RCU free() call */
ffe60014
DG
362 call_rcu(&relayd->node.head, free_relayd_rcu);
363}
364
365/*
366 * Remove a channel from the global list protected by a mutex. This function is
367 * also responsible for freeing its data structures.
368 */
369void consumer_del_channel(struct lttng_consumer_channel *channel)
370{
ffe60014
DG
371 struct lttng_ht_iter iter;
372
d88aee68 373 DBG("Consumer delete channel key %" PRIu64, channel->key);
ffe60014 374
fa29bfbf 375 pthread_mutex_lock(&the_consumer_data.lock);
a9838785 376 pthread_mutex_lock(&channel->lock);
ffe60014 377
212d67a2
DG
378 /* Destroy streams that might have been left in the stream list. */
379 clean_channel_stream_list(channel);
51e762e5 380
d3e2ba59
JD
381 if (channel->live_timer_enabled == 1) {
382 consumer_timer_live_stop(channel);
383 }
e9404c27
JG
384 if (channel->monitor_timer_enabled == 1) {
385 consumer_timer_monitor_stop(channel);
386 }
d3e2ba59 387
fa29bfbf 388 switch (the_consumer_data.type) {
ffe60014
DG
389 case LTTNG_CONSUMER_KERNEL:
390 break;
391 case LTTNG_CONSUMER32_UST:
392 case LTTNG_CONSUMER64_UST:
393 lttng_ustconsumer_del_channel(channel);
394 break;
395 default:
396 ERR("Unknown consumer_data type");
397 assert(0);
398 goto end;
399 }
400
d2956687
JG
401 lttng_trace_chunk_put(channel->trace_chunk);
402 channel->trace_chunk = NULL;
5c3892a6 403
d2956687
JG
404 if (channel->is_published) {
405 int ret;
406
407 rcu_read_lock();
408 iter.iter.node = &channel->node.node;
fa29bfbf 409 ret = lttng_ht_del(the_consumer_data.channel_ht, &iter);
d2956687 410 assert(!ret);
ffe60014 411
d2956687 412 iter.iter.node = &channel->channels_by_session_id_ht_node.node;
fa29bfbf 413 ret = lttng_ht_del(the_consumer_data.channels_by_session_id_ht,
d2956687
JG
414 &iter);
415 assert(!ret);
416 rcu_read_unlock();
417 }
418
b6921a17
JG
419 channel->is_deleted = true;
420 call_rcu(&channel->node.head, free_channel_rcu);
ffe60014 421end:
a9838785 422 pthread_mutex_unlock(&channel->lock);
fa29bfbf 423 pthread_mutex_unlock(&the_consumer_data.lock);
00e2e675
DG
424}
425
228b5bf7
DG
426/*
427 * Iterate over the relayd hash table and destroy each element. Finally,
428 * destroy the whole hash table.
429 */
430static void cleanup_relayd_ht(void)
431{
432 struct lttng_ht_iter iter;
433 struct consumer_relayd_sock_pair *relayd;
434
435 rcu_read_lock();
436
fa29bfbf
SM
437 cds_lfht_for_each_entry(the_consumer_data.relayd_ht->ht, &iter.iter,
438 relayd, node.node) {
51230d70 439 consumer_destroy_relayd(relayd);
228b5bf7
DG
440 }
441
228b5bf7 442 rcu_read_unlock();
36b588ed 443
fa29bfbf 444 lttng_ht_destroy(the_consumer_data.relayd_ht);
228b5bf7
DG
445}
446
8994307f
DG
447/*
448 * Update the end point status of all streams having the given network sequence
449 * index (relayd index).
450 *
451 * It's atomically set without having the stream mutex locked which is fine
452 * because we handle the write/read race with a pipe wakeup for each thread.
453 */
da009f2c 454static void update_endpoint_status_by_netidx(uint64_t net_seq_idx,
8994307f
DG
455 enum consumer_endpoint_status status)
456{
457 struct lttng_ht_iter iter;
458 struct lttng_consumer_stream *stream;
459
da009f2c 460 DBG("Consumer set delete flag on stream by idx %" PRIu64, net_seq_idx);
8994307f
DG
461
462 rcu_read_lock();
463
464 /* Let's begin with metadata */
465 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
466 if (stream->net_seq_idx == net_seq_idx) {
467 uatomic_set(&stream->endpoint_status, status);
95671f53
JG
468 lttng_wait_queue_wake_all(&stream->chan->metadata_pushed_wait_queue);
469
8994307f
DG
470 DBG("Delete flag set to metadata stream %d", stream->wait_fd);
471 }
472 }
473
474 /* Follow up by the data streams */
475 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
476 if (stream->net_seq_idx == net_seq_idx) {
477 uatomic_set(&stream->endpoint_status, status);
478 DBG("Delete flag set to data stream %d", stream->wait_fd);
479 }
480 }
481 rcu_read_unlock();
482}
483
484/*
485 * Cleanup a relayd object by flagging every associated streams for deletion,
486 * destroying the object meaning removing it from the relayd hash table,
487 * closing the sockets and freeing the memory in a RCU call.
488 *
489 * If a local data context is available, notify the threads that the streams'
490 * state have changed.
491 */
9276e5c8 492void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair *relayd)
8994307f 493{
da009f2c 494 uint64_t netidx;
8994307f
DG
495
496 assert(relayd);
497
9276e5c8 498 DBG("Cleaning up relayd object ID %"PRIu64, relayd->net_seq_idx);
9617607b 499
8994307f
DG
500 /* Save the net sequence index before destroying the object */
501 netidx = relayd->net_seq_idx;
502
503 /*
504 * Delete the relayd from the relayd hash table, close the sockets and free
505 * the object in a RCU call.
506 */
51230d70 507 consumer_destroy_relayd(relayd);
8994307f
DG
508
509 /* Set inactive endpoint to all streams */
510 update_endpoint_status_by_netidx(netidx, CONSUMER_ENDPOINT_INACTIVE);
511
512 /*
513 * With a local data context, notify the threads that the streams' state
514 * have changed. The write() action on the pipe acts as an "implicit"
515 * memory barrier ordering the updates of the end point status from the
516 * read of this status which happens AFTER receiving this notify.
517 */
9276e5c8
JR
518 notify_thread_lttng_pipe(relayd->ctx->consumer_data_pipe);
519 notify_thread_lttng_pipe(relayd->ctx->consumer_metadata_pipe);
8994307f
DG
520}
521
a6ba4fe1
DG
522/*
523 * Flag a relayd socket pair for destruction. Destroy it if the refcount
524 * reaches zero.
525 *
526 * RCU read side lock MUST be aquired before calling this function.
527 */
528void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd)
529{
530 assert(relayd);
531
532 /* Set destroy flag for this object */
533 uatomic_set(&relayd->destroy_flag, 1);
534
535 /* Destroy the relayd if refcount is 0 */
536 if (uatomic_read(&relayd->refcount) == 0) {
51230d70 537 consumer_destroy_relayd(relayd);
a6ba4fe1
DG
538 }
539}
540
3bd1e081 541/*
1d1a276c
DG
542 * Completly destroy stream from every visiable data structure and the given
543 * hash table if one.
544 *
545 * One this call returns, the stream object is not longer usable nor visible.
3bd1e081 546 */
e316aad5
DG
547void consumer_del_stream(struct lttng_consumer_stream *stream,
548 struct lttng_ht *ht)
3bd1e081 549{
1d1a276c 550 consumer_stream_destroy(stream, ht);
3bd1e081
MD
551}
552
5ab66908
MD
553/*
554 * XXX naming of del vs destroy is all mixed up.
555 */
556void consumer_del_stream_for_data(struct lttng_consumer_stream *stream)
557{
558 consumer_stream_destroy(stream, data_ht);
559}
560
561void consumer_del_stream_for_metadata(struct lttng_consumer_stream *stream)
562{
563 consumer_stream_destroy(stream, metadata_ht);
564}
565
d9a2e16e
JD
566void consumer_stream_update_channel_attributes(
567 struct lttng_consumer_stream *stream,
568 struct lttng_consumer_channel *channel)
569{
570 stream->channel_read_only_attributes.tracefile_size =
571 channel->tracefile_size;
d9a2e16e
JD
572}
573
3bd1e081
MD
574/*
575 * Add a stream to the global list protected by a mutex.
576 */
66d583dc 577void consumer_add_data_stream(struct lttng_consumer_stream *stream)
3bd1e081 578{
5ab66908 579 struct lttng_ht *ht = data_ht;
3bd1e081 580
e316aad5 581 assert(stream);
43c34bc3 582 assert(ht);
c77fc10a 583
d88aee68 584 DBG3("Adding consumer stream %" PRIu64, stream->key);
e316aad5 585
fa29bfbf 586 pthread_mutex_lock(&the_consumer_data.lock);
a9838785 587 pthread_mutex_lock(&stream->chan->lock);
ec6ea7d0 588 pthread_mutex_lock(&stream->chan->timer_lock);
2e818a6a 589 pthread_mutex_lock(&stream->lock);
b0b335c8 590 rcu_read_lock();
e316aad5 591
43c34bc3 592 /* Steal stream identifier to avoid having streams with the same key */
ffe60014 593 steal_stream_key(stream->key, ht);
43c34bc3 594
d88aee68 595 lttng_ht_add_unique_u64(ht, &stream->node);
00e2e675 596
fa29bfbf 597 lttng_ht_add_u64(the_consumer_data.stream_per_chan_id_ht,
d8ef542d
MD
598 &stream->node_channel_id);
599
ca22feea
DG
600 /*
601 * Add stream to the stream_list_ht of the consumer data. No need to steal
602 * the key since the HT does not use it and we allow to add redundant keys
603 * into this table.
604 */
fa29bfbf
SM
605 lttng_ht_add_u64(the_consumer_data.stream_list_ht,
606 &stream->node_session_id);
ca22feea 607
e316aad5 608 /*
ffe60014
DG
609 * When nb_init_stream_left reaches 0, we don't need to trigger any action
610 * in terms of destroying the associated channel, because the action that
e316aad5
DG
611 * causes the count to become 0 also causes a stream to be added. The
612 * channel deletion will thus be triggered by the following removal of this
613 * stream.
614 */
ffe60014 615 if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) {
f2ad556d
MD
616 /* Increment refcount before decrementing nb_init_stream_left */
617 cmm_smp_wmb();
ffe60014 618 uatomic_dec(&stream->chan->nb_init_stream_left);
e316aad5
DG
619 }
620
621 /* Update consumer data once the node is inserted. */
fa29bfbf
SM
622 the_consumer_data.stream_count++;
623 the_consumer_data.need_update = 1;
3bd1e081 624
e316aad5 625 rcu_read_unlock();
2e818a6a 626 pthread_mutex_unlock(&stream->lock);
ec6ea7d0 627 pthread_mutex_unlock(&stream->chan->timer_lock);
a9838785 628 pthread_mutex_unlock(&stream->chan->lock);
fa29bfbf 629 pthread_mutex_unlock(&the_consumer_data.lock);
3bd1e081
MD
630}
631
00e2e675 632/*
3f8e211f
DG
633 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
634 * be acquired before calling this.
00e2e675 635 */
d09e1200 636static int add_relayd(struct consumer_relayd_sock_pair *relayd)
00e2e675
DG
637{
638 int ret = 0;
d88aee68 639 struct lttng_ht_node_u64 *node;
00e2e675
DG
640 struct lttng_ht_iter iter;
641
ffe60014 642 assert(relayd);
00e2e675 643
fa29bfbf
SM
644 lttng_ht_lookup(the_consumer_data.relayd_ht, &relayd->net_seq_idx,
645 &iter);
d88aee68 646 node = lttng_ht_iter_get_node_u64(&iter);
00e2e675 647 if (node != NULL) {
00e2e675
DG
648 goto end;
649 }
fa29bfbf 650 lttng_ht_add_unique_u64(the_consumer_data.relayd_ht, &relayd->node);
00e2e675 651
00e2e675
DG
652end:
653 return ret;
654}
655
656/*
657 * Allocate and return a consumer relayd socket.
658 */
027a694f 659static struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
da009f2c 660 uint64_t net_seq_idx)
00e2e675
DG
661{
662 struct consumer_relayd_sock_pair *obj = NULL;
663
da009f2c
MD
664 /* net sequence index of -1 is a failure */
665 if (net_seq_idx == (uint64_t) -1ULL) {
00e2e675
DG
666 goto error;
667 }
668
669 obj = zmalloc(sizeof(struct consumer_relayd_sock_pair));
670 if (obj == NULL) {
671 PERROR("zmalloc relayd sock");
672 goto error;
673 }
674
675 obj->net_seq_idx = net_seq_idx;
676 obj->refcount = 0;
173af62f 677 obj->destroy_flag = 0;
f96e4545
MD
678 obj->control_sock.sock.fd = -1;
679 obj->data_sock.sock.fd = -1;
d88aee68 680 lttng_ht_node_init_u64(&obj->node, obj->net_seq_idx);
00e2e675
DG
681 pthread_mutex_init(&obj->ctrl_sock_mutex, NULL);
682
683error:
684 return obj;
685}
686
687/*
688 * Find a relayd socket pair in the global consumer data.
689 *
690 * Return the object if found else NULL.
b0b335c8
MD
691 * RCU read-side lock must be held across this call and while using the
692 * returned object.
00e2e675 693 */
d88aee68 694struct consumer_relayd_sock_pair *consumer_find_relayd(uint64_t key)
00e2e675
DG
695{
696 struct lttng_ht_iter iter;
d88aee68 697 struct lttng_ht_node_u64 *node;
00e2e675
DG
698 struct consumer_relayd_sock_pair *relayd = NULL;
699
700 /* Negative keys are lookup failures */
d88aee68 701 if (key == (uint64_t) -1ULL) {
00e2e675
DG
702 goto error;
703 }
704
fa29bfbf 705 lttng_ht_lookup(the_consumer_data.relayd_ht, &key, &iter);
d88aee68 706 node = lttng_ht_iter_get_node_u64(&iter);
00e2e675
DG
707 if (node != NULL) {
708 relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node);
709 }
710
00e2e675
DG
711error:
712 return relayd;
713}
714
10a50311
JD
715/*
716 * Find a relayd and send the stream
717 *
718 * Returns 0 on success, < 0 on error
719 */
720int consumer_send_relayd_stream(struct lttng_consumer_stream *stream,
721 char *path)
722{
723 int ret = 0;
724 struct consumer_relayd_sock_pair *relayd;
725
726 assert(stream);
727 assert(stream->net_seq_idx != -1ULL);
728 assert(path);
729
730 /* The stream is not metadata. Get relayd reference if exists. */
731 rcu_read_lock();
732 relayd = consumer_find_relayd(stream->net_seq_idx);
733 if (relayd != NULL) {
734 /* Add stream on the relayd */
735 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
736 ret = relayd_add_stream(&relayd->control_sock, stream->name,
5da88b0f 737 get_consumer_domain(), path, &stream->relayd_stream_id,
d2956687
JG
738 stream->chan->tracefile_size,
739 stream->chan->tracefile_count,
740 stream->trace_chunk);
10a50311
JD
741 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
742 if (ret < 0) {
9276e5c8
JR
743 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx);
744 lttng_consumer_cleanup_relayd(relayd);
10a50311
JD
745 goto end;
746 }
1c20f0e2 747
10a50311 748 uatomic_inc(&relayd->refcount);
d01178b6 749 stream->sent_to_relayd = 1;
10a50311
JD
750 } else {
751 ERR("Stream %" PRIu64 " relayd ID %" PRIu64 " unknown. Can't send it.",
752 stream->key, stream->net_seq_idx);
753 ret = -1;
754 goto end;
755 }
756
757 DBG("Stream %s with key %" PRIu64 " sent to relayd id %" PRIu64,
758 stream->name, stream->key, stream->net_seq_idx);
759
760end:
761 rcu_read_unlock();
762 return ret;
763}
764
a4baae1b
JD
765/*
766 * Find a relayd and send the streams sent message
767 *
768 * Returns 0 on success, < 0 on error
769 */
770int consumer_send_relayd_streams_sent(uint64_t net_seq_idx)
771{
772 int ret = 0;
773 struct consumer_relayd_sock_pair *relayd;
774
775 assert(net_seq_idx != -1ULL);
776
777 /* The stream is not metadata. Get relayd reference if exists. */
778 rcu_read_lock();
779 relayd = consumer_find_relayd(net_seq_idx);
780 if (relayd != NULL) {
781 /* Add stream on the relayd */
782 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
783 ret = relayd_streams_sent(&relayd->control_sock);
784 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
785 if (ret < 0) {
9276e5c8
JR
786 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx);
787 lttng_consumer_cleanup_relayd(relayd);
a4baae1b
JD
788 goto end;
789 }
790 } else {
791 ERR("Relayd ID %" PRIu64 " unknown. Can't send streams_sent.",
792 net_seq_idx);
793 ret = -1;
794 goto end;
795 }
796
797 ret = 0;
798 DBG("All streams sent relayd id %" PRIu64, net_seq_idx);
799
800end:
801 rcu_read_unlock();
802 return ret;
803}
804
10a50311
JD
805/*
806 * Find a relayd and close the stream
807 */
808void close_relayd_stream(struct lttng_consumer_stream *stream)
809{
810 struct consumer_relayd_sock_pair *relayd;
811
812 /* The stream is not metadata. Get relayd reference if exists. */
813 rcu_read_lock();
814 relayd = consumer_find_relayd(stream->net_seq_idx);
815 if (relayd) {
816 consumer_stream_relayd_close(stream, relayd);
817 }
818 rcu_read_unlock();
819}
820
00e2e675
DG
821/*
822 * Handle stream for relayd transmission if the stream applies for network
823 * streaming where the net sequence index is set.
824 *
825 * Return destination file descriptor or negative value on error.
826 */
6197aea7 827static int write_relayd_stream_header(struct lttng_consumer_stream *stream,
1d4dfdef
DG
828 size_t data_size, unsigned long padding,
829 struct consumer_relayd_sock_pair *relayd)
00e2e675
DG
830{
831 int outfd = -1, ret;
00e2e675
DG
832 struct lttcomm_relayd_data_hdr data_hdr;
833
834 /* Safety net */
835 assert(stream);
6197aea7 836 assert(relayd);
00e2e675
DG
837
838 /* Reset data header */
839 memset(&data_hdr, 0, sizeof(data_hdr));
840
00e2e675
DG
841 if (stream->metadata_flag) {
842 /* Caller MUST acquire the relayd control socket lock */
843 ret = relayd_send_metadata(&relayd->control_sock, data_size);
844 if (ret < 0) {
845 goto error;
846 }
847
848 /* Metadata are always sent on the control socket. */
6151a90f 849 outfd = relayd->control_sock.sock.fd;
00e2e675
DG
850 } else {
851 /* Set header with stream information */
852 data_hdr.stream_id = htobe64(stream->relayd_stream_id);
853 data_hdr.data_size = htobe32(data_size);
1d4dfdef 854 data_hdr.padding_size = htobe32(padding);
c35f9726 855
39df6d9f
DG
856 /*
857 * Note that net_seq_num below is assigned with the *current* value of
858 * next_net_seq_num and only after that the next_net_seq_num will be
859 * increment. This is why when issuing a command on the relayd using
860 * this next value, 1 should always be substracted in order to compare
861 * the last seen sequence number on the relayd side to the last sent.
862 */
3604f373 863 data_hdr.net_seq_num = htobe64(stream->next_net_seq_num);
00e2e675
DG
864 /* Other fields are zeroed previously */
865
866 ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr,
867 sizeof(data_hdr));
868 if (ret < 0) {
869 goto error;
870 }
871
3604f373
DG
872 ++stream->next_net_seq_num;
873
00e2e675 874 /* Set to go on data socket */
6151a90f 875 outfd = relayd->data_sock.sock.fd;
00e2e675
DG
876 }
877
878error:
879 return outfd;
880}
881
b1316da1
JG
882/*
883 * Write a character on the metadata poll pipe to wake the metadata thread.
884 * Returns 0 on success, -1 on error.
885 */
886int consumer_metadata_wakeup_pipe(const struct lttng_consumer_channel *channel)
887{
888 int ret = 0;
889
890 DBG("Waking up metadata poll thread (writing to pipe): channel name = '%s'",
891 channel->name);
892 if (channel->monitor && channel->metadata_stream) {
893 const char dummy = 'c';
894 const ssize_t write_ret = lttng_write(
895 channel->metadata_stream->ust_metadata_poll_pipe[1],
896 &dummy, 1);
897
898 if (write_ret < 1) {
899 if (errno == EWOULDBLOCK) {
900 /*
901 * This is fine, the metadata poll thread
902 * is having a hard time keeping-up, but
903 * it will eventually wake-up and consume
904 * the available data.
905 */
906 ret = 0;
907 } else {
908 PERROR("Failed to write to UST metadata pipe while attempting to wake-up the metadata poll thread");
909 ret = -1;
910 goto end;
911 }
912 }
913 }
914
915end:
916 return ret;
917}
918
d2956687
JG
919/*
920 * Trigger a dump of the metadata content. Following/during the succesful
921 * completion of this call, the metadata poll thread will start receiving
922 * metadata packets to consume.
923 *
924 * The caller must hold the channel and stream locks.
925 */
926static
927int consumer_metadata_stream_dump(struct lttng_consumer_stream *stream)
928{
929 int ret;
930
931 ASSERT_LOCKED(stream->chan->lock);
932 ASSERT_LOCKED(stream->lock);
933 assert(stream->metadata_flag);
934 assert(stream->chan->trace_chunk);
935
fa29bfbf 936 switch (the_consumer_data.type) {
d2956687
JG
937 case LTTNG_CONSUMER_KERNEL:
938 /*
939 * Reset the position of what has been read from the
940 * metadata cache to 0 so we can dump it again.
941 */
942 ret = kernctl_metadata_cache_dump(stream->wait_fd);
943 break;
944 case LTTNG_CONSUMER32_UST:
945 case LTTNG_CONSUMER64_UST:
946 /*
947 * Reset the position pushed from the metadata cache so it
948 * will write from the beginning on the next push.
949 */
950 stream->ust_metadata_pushed = 0;
951 ret = consumer_metadata_wakeup_pipe(stream->chan);
952 break;
953 default:
954 ERR("Unknown consumer_data type");
955 abort();
956 }
957 if (ret < 0) {
958 ERR("Failed to dump the metadata cache");
959 }
960 return ret;
961}
962
963static
964int lttng_consumer_channel_set_trace_chunk(
965 struct lttng_consumer_channel *channel,
966 struct lttng_trace_chunk *new_trace_chunk)
967{
d2956687 968 pthread_mutex_lock(&channel->lock);
b6921a17
JG
969 if (channel->is_deleted) {
970 /*
971 * The channel has been logically deleted and should no longer
972 * be used. It has released its reference to its current trace
973 * chunk and should not acquire a new one.
974 *
975 * Return success as there is nothing for the caller to do.
976 */
977 goto end;
978 }
d2956687
JG
979
980 /*
981 * The acquisition of the reference cannot fail (barring
982 * a severe internal error) since a reference to the published
983 * chunk is already held by the caller.
984 */
985 if (new_trace_chunk) {
986 const bool acquired_reference = lttng_trace_chunk_get(
987 new_trace_chunk);
988
989 assert(acquired_reference);
990 }
991
992 lttng_trace_chunk_put(channel->trace_chunk);
993 channel->trace_chunk = new_trace_chunk;
d2956687
JG
994end:
995 pthread_mutex_unlock(&channel->lock);
ce1aa6fe 996 return 0;
d2956687
JG
997}
998
3bd1e081 999/*
ffe60014
DG
1000 * Allocate and return a new lttng_consumer_channel object using the given key
1001 * to initialize the hash table node.
1002 *
1003 * On error, return NULL.
3bd1e081 1004 */
886224ff 1005struct lttng_consumer_channel *consumer_allocate_channel(uint64_t key,
ffe60014 1006 uint64_t session_id,
d2956687 1007 const uint64_t *chunk_id,
ffe60014
DG
1008 const char *pathname,
1009 const char *name,
57a269f2 1010 uint64_t relayd_id,
1624d5b7
JD
1011 enum lttng_event_output output,
1012 uint64_t tracefile_size,
2bba9e53 1013 uint64_t tracefile_count,
1950109e 1014 uint64_t session_id_per_pid,
ecc48a90 1015 unsigned int monitor,
d7ba1388 1016 unsigned int live_timer_interval,
a2814ea7 1017 bool is_in_live_session,
3d071855 1018 const char *root_shm_path,
d7ba1388 1019 const char *shm_path)
3bd1e081 1020{
d2956687
JG
1021 struct lttng_consumer_channel *channel = NULL;
1022 struct lttng_trace_chunk *trace_chunk = NULL;
1023
1024 if (chunk_id) {
1025 trace_chunk = lttng_trace_chunk_registry_find_chunk(
fa29bfbf 1026 the_consumer_data.chunk_registry, session_id,
d2956687
JG
1027 *chunk_id);
1028 if (!trace_chunk) {
1029 ERR("Failed to find trace chunk reference during creation of channel");
1030 goto end;
1031 }
1032 }
3bd1e081 1033
276b26d1 1034 channel = zmalloc(sizeof(*channel));
3bd1e081 1035 if (channel == NULL) {
7a57cf92 1036 PERROR("malloc struct lttng_consumer_channel");
3bd1e081
MD
1037 goto end;
1038 }
ffe60014
DG
1039
1040 channel->key = key;
3bd1e081 1041 channel->refcount = 0;
ffe60014 1042 channel->session_id = session_id;
1950109e 1043 channel->session_id_per_pid = session_id_per_pid;
ffe60014 1044 channel->relayd_id = relayd_id;
1624d5b7
JD
1045 channel->tracefile_size = tracefile_size;
1046 channel->tracefile_count = tracefile_count;
2bba9e53 1047 channel->monitor = monitor;
ecc48a90 1048 channel->live_timer_interval = live_timer_interval;
a2814ea7 1049 channel->is_live = is_in_live_session;
a9838785 1050 pthread_mutex_init(&channel->lock, NULL);
ec6ea7d0 1051 pthread_mutex_init(&channel->timer_lock, NULL);
95671f53 1052 lttng_wait_queue_init(&channel->metadata_pushed_wait_queue);
ffe60014 1053
0c759fc9
DG
1054 switch (output) {
1055 case LTTNG_EVENT_SPLICE:
1056 channel->output = CONSUMER_CHANNEL_SPLICE;
1057 break;
1058 case LTTNG_EVENT_MMAP:
1059 channel->output = CONSUMER_CHANNEL_MMAP;
1060 break;
1061 default:
1062 assert(0);
1063 free(channel);
1064 channel = NULL;
1065 goto end;
1066 }
1067
07b86b52
JD
1068 /*
1069 * In monitor mode, the streams associated with the channel will be put in
1070 * a special list ONLY owned by this channel. So, the refcount is set to 1
1071 * here meaning that the channel itself has streams that are referenced.
1072 *
1073 * On a channel deletion, once the channel is no longer visible, the
1074 * refcount is decremented and checked for a zero value to delete it. With
1075 * streams in no monitor mode, it will now be safe to destroy the channel.
1076 */
1077 if (!channel->monitor) {
1078 channel->refcount = 1;
1079 }
1080
ffe60014
DG
1081 strncpy(channel->pathname, pathname, sizeof(channel->pathname));
1082 channel->pathname[sizeof(channel->pathname) - 1] = '\0';
1083
1084 strncpy(channel->name, name, sizeof(channel->name));
1085 channel->name[sizeof(channel->name) - 1] = '\0';
1086
3d071855
MD
1087 if (root_shm_path) {
1088 strncpy(channel->root_shm_path, root_shm_path, sizeof(channel->root_shm_path));
1089 channel->root_shm_path[sizeof(channel->root_shm_path) - 1] = '\0';
1090 }
d7ba1388
MD
1091 if (shm_path) {
1092 strncpy(channel->shm_path, shm_path, sizeof(channel->shm_path));
1093 channel->shm_path[sizeof(channel->shm_path) - 1] = '\0';
1094 }
1095
d88aee68 1096 lttng_ht_node_init_u64(&channel->node, channel->key);
5c3892a6
JG
1097 lttng_ht_node_init_u64(&channel->channels_by_session_id_ht_node,
1098 channel->session_id);
d8ef542d
MD
1099
1100 channel->wait_fd = -1;
ffe60014
DG
1101 CDS_INIT_LIST_HEAD(&channel->streams.head);
1102
d2956687
JG
1103 if (trace_chunk) {
1104 int ret = lttng_consumer_channel_set_trace_chunk(channel,
1105 trace_chunk);
1106 if (ret) {
1107 goto error;
1108 }
1109 }
1110
62a7b8ed 1111 DBG("Allocated channel (key %" PRIu64 ")", channel->key);
3bd1e081 1112
3bd1e081 1113end:
d2956687 1114 lttng_trace_chunk_put(trace_chunk);
3bd1e081 1115 return channel;
d2956687
JG
1116error:
1117 consumer_del_channel(channel);
1118 channel = NULL;
1119 goto end;
3bd1e081
MD
1120}
1121
1122/*
1123 * Add a channel to the global list protected by a mutex.
821fffb2 1124 *
b5a6470f 1125 * Always return 0 indicating success.
3bd1e081 1126 */
d8ef542d
MD
1127int consumer_add_channel(struct lttng_consumer_channel *channel,
1128 struct lttng_consumer_local_data *ctx)
3bd1e081 1129{
fa29bfbf 1130 pthread_mutex_lock(&the_consumer_data.lock);
a9838785 1131 pthread_mutex_lock(&channel->lock);
ec6ea7d0 1132 pthread_mutex_lock(&channel->timer_lock);
c77fc10a 1133
b5a6470f
DG
1134 /*
1135 * This gives us a guarantee that the channel we are about to add to the
1136 * channel hash table will be unique. See this function comment on the why
1137 * we need to steel the channel key at this stage.
1138 */
1139 steal_channel_key(channel->key);
c77fc10a 1140
b5a6470f 1141 rcu_read_lock();
fa29bfbf
SM
1142 lttng_ht_add_unique_u64(the_consumer_data.channel_ht, &channel->node);
1143 lttng_ht_add_u64(the_consumer_data.channels_by_session_id_ht,
5c3892a6 1144 &channel->channels_by_session_id_ht_node);
6065ceec 1145 rcu_read_unlock();
d2956687 1146 channel->is_published = true;
b5a6470f 1147
ec6ea7d0 1148 pthread_mutex_unlock(&channel->timer_lock);
a9838785 1149 pthread_mutex_unlock(&channel->lock);
fa29bfbf 1150 pthread_mutex_unlock(&the_consumer_data.lock);
702b1ea4 1151
b5a6470f 1152 if (channel->wait_fd != -1 && channel->type == CONSUMER_CHANNEL_TYPE_DATA) {
a0cbdd2e 1153 notify_channel_pipe(ctx, channel, -1, CONSUMER_CHANNEL_ADD);
d8ef542d 1154 }
b5a6470f
DG
1155
1156 return 0;
3bd1e081
MD
1157}
1158
1159/*
1160 * Allocate the pollfd structure and the local view of the out fds to avoid
1161 * doing a lookup in the linked list and concurrency issues when writing is
1162 * needed. Called with consumer_data.lock held.
1163 *
1164 * Returns the number of fds in the structures.
1165 */
ffe60014
DG
1166static int update_poll_array(struct lttng_consumer_local_data *ctx,
1167 struct pollfd **pollfd, struct lttng_consumer_stream **local_stream,
9a2fcf78 1168 struct lttng_ht *ht, int *nb_inactive_fd)
3bd1e081 1169{
3bd1e081 1170 int i = 0;
e4421fec
DG
1171 struct lttng_ht_iter iter;
1172 struct lttng_consumer_stream *stream;
3bd1e081 1173
ffe60014
DG
1174 assert(ctx);
1175 assert(ht);
1176 assert(pollfd);
1177 assert(local_stream);
1178
3bd1e081 1179 DBG("Updating poll fd array");
9a2fcf78 1180 *nb_inactive_fd = 0;
481d6c57 1181 rcu_read_lock();
43c34bc3 1182 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
8994307f
DG
1183 /*
1184 * Only active streams with an active end point can be added to the
1185 * poll set and local stream storage of the thread.
1186 *
1187 * There is a potential race here for endpoint_status to be updated
1188 * just after the check. However, this is OK since the stream(s) will
1189 * be deleted once the thread is notified that the end point state has
1190 * changed where this function will be called back again.
9a2fcf78
JD
1191 *
1192 * We track the number of inactive FDs because they still need to be
1193 * closed by the polling thread after a wakeup on the data_pipe or
1194 * metadata_pipe.
8994307f 1195 */
d2956687 1196 if (stream->endpoint_status == CONSUMER_ENDPOINT_INACTIVE) {
9a2fcf78 1197 (*nb_inactive_fd)++;
3bd1e081
MD
1198 continue;
1199 }
7972aab2
DG
1200 /*
1201 * This clobbers way too much the debug output. Uncomment that if you
1202 * need it for debugging purposes.
7972aab2 1203 */
e4421fec 1204 (*pollfd)[i].fd = stream->wait_fd;
3bd1e081 1205 (*pollfd)[i].events = POLLIN | POLLPRI;
e4421fec 1206 local_stream[i] = stream;
3bd1e081
MD
1207 i++;
1208 }
481d6c57 1209 rcu_read_unlock();
3bd1e081
MD
1210
1211 /*
50f8ae69 1212 * Insert the consumer_data_pipe at the end of the array and don't
3bd1e081
MD
1213 * increment i so nb_fd is the number of real FD.
1214 */
acdb9057 1215 (*pollfd)[i].fd = lttng_pipe_get_readfd(ctx->consumer_data_pipe);
509bb1cf 1216 (*pollfd)[i].events = POLLIN | POLLPRI;
02b3d176
DG
1217
1218 (*pollfd)[i + 1].fd = lttng_pipe_get_readfd(ctx->consumer_wakeup_pipe);
1219 (*pollfd)[i + 1].events = POLLIN | POLLPRI;
3bd1e081
MD
1220 return i;
1221}
1222
1223/*
84382d49
MD
1224 * Poll on the should_quit pipe and the command socket return -1 on
1225 * error, 1 if should exit, 0 if data is available on the command socket
3bd1e081
MD
1226 */
1227int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
1228{
1229 int num_rdy;
1230
88f2b785 1231restart:
3bd1e081
MD
1232 num_rdy = poll(consumer_sockpoll, 2, -1);
1233 if (num_rdy == -1) {
88f2b785
MD
1234 /*
1235 * Restart interrupted system call.
1236 */
1237 if (errno == EINTR) {
1238 goto restart;
1239 }
7a57cf92 1240 PERROR("Poll error");
84382d49 1241 return -1;
3bd1e081 1242 }
509bb1cf 1243 if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) {
3bd1e081 1244 DBG("consumer_should_quit wake up");
84382d49 1245 return 1;
3bd1e081
MD
1246 }
1247 return 0;
3bd1e081
MD
1248}
1249
1250/*
1251 * Set the error socket.
1252 */
ffe60014
DG
1253void lttng_consumer_set_error_sock(struct lttng_consumer_local_data *ctx,
1254 int sock)
3bd1e081
MD
1255{
1256 ctx->consumer_error_socket = sock;
1257}
1258
1259/*
1260 * Set the command socket path.
1261 */
3bd1e081
MD
1262void lttng_consumer_set_command_sock_path(
1263 struct lttng_consumer_local_data *ctx, char *sock)
1264{
1265 ctx->consumer_command_sock_path = sock;
1266}
1267
1268/*
1269 * Send return code to the session daemon.
1270 * If the socket is not defined, we return 0, it is not a fatal error
1271 */
a73d9496
JG
1272int lttng_consumer_send_error(struct lttng_consumer_local_data *ctx,
1273 enum lttcomm_return_code error_code)
3bd1e081
MD
1274{
1275 if (ctx->consumer_error_socket > 0) {
a73d9496
JG
1276 const int32_t comm_code = (int32_t) error_code;
1277
1278 return lttcomm_send_unix_sock(
1279 ctx->consumer_error_socket, &comm_code, sizeof(comm_code));
3bd1e081
MD
1280 }
1281
1282 return 0;
1283}
1284
1285/*
228b5bf7
DG
1286 * Close all the tracefiles and stream fds and MUST be called when all
1287 * instances are destroyed i.e. when all threads were joined and are ended.
3bd1e081
MD
1288 */
1289void lttng_consumer_cleanup(void)
1290{
e4421fec 1291 struct lttng_ht_iter iter;
ffe60014 1292 struct lttng_consumer_channel *channel;
e10aec8f 1293 unsigned int trace_chunks_left;
6065ceec
DG
1294
1295 rcu_read_lock();
3bd1e081 1296
fa29bfbf
SM
1297 cds_lfht_for_each_entry(the_consumer_data.channel_ht->ht, &iter.iter,
1298 channel, node.node) {
702b1ea4 1299 consumer_del_channel(channel);
3bd1e081 1300 }
6065ceec
DG
1301
1302 rcu_read_unlock();
d6ce1df2 1303
fa29bfbf
SM
1304 lttng_ht_destroy(the_consumer_data.channel_ht);
1305 lttng_ht_destroy(the_consumer_data.channels_by_session_id_ht);
228b5bf7
DG
1306
1307 cleanup_relayd_ht();
1308
fa29bfbf 1309 lttng_ht_destroy(the_consumer_data.stream_per_chan_id_ht);
d8ef542d 1310
228b5bf7
DG
1311 /*
1312 * This HT contains streams that are freed by either the metadata thread or
1313 * the data thread so we do *nothing* on the hash table and simply destroy
1314 * it.
1315 */
fa29bfbf 1316 lttng_ht_destroy(the_consumer_data.stream_list_ht);
28cc88f3 1317
e10aec8f
MD
1318 /*
1319 * Trace chunks in the registry may still exist if the session
1320 * daemon has encountered an internal error and could not
1321 * tear down its sessions and/or trace chunks properly.
1322 *
1323 * Release the session daemon's implicit reference to any remaining
1324 * trace chunk and print an error if any trace chunk was found. Note
1325 * that there are _no_ legitimate cases for trace chunks to be left,
1326 * it is a leak. However, it can happen following a crash of the
1327 * session daemon and not emptying the registry would cause an assertion
1328 * to hit.
1329 */
1330 trace_chunks_left = lttng_trace_chunk_registry_put_each_chunk(
fa29bfbf 1331 the_consumer_data.chunk_registry);
e10aec8f
MD
1332 if (trace_chunks_left) {
1333 ERR("%u trace chunks are leaked by lttng-consumerd. "
1334 "This can be caused by an internal error of the session daemon.",
1335 trace_chunks_left);
1336 }
1337 /* Run all callbacks freeing each chunk. */
1338 rcu_barrier();
fa29bfbf 1339 lttng_trace_chunk_registry_destroy(the_consumer_data.chunk_registry);
3bd1e081
MD
1340}
1341
1342/*
1343 * Called from signal handler.
1344 */
1345void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
1346{
6cd525e8
MD
1347 ssize_t ret;
1348
10211f5c 1349 CMM_STORE_SHARED(consumer_quit, 1);
6cd525e8
MD
1350 ret = lttng_write(ctx->consumer_should_quit[1], "4", 1);
1351 if (ret < 1) {
7a57cf92 1352 PERROR("write consumer quit");
3bd1e081 1353 }
ab1027f4
DG
1354
1355 DBG("Consumer flag that it should quit");
3bd1e081
MD
1356}
1357
5199ffc4
JG
1358
1359/*
1360 * Flush pending writes to trace output disk file.
1361 */
1362static
00e2e675
DG
1363void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
1364 off_t orig_offset)
3bd1e081 1365{
c7a78aab 1366 int ret;
3bd1e081
MD
1367 int outfd = stream->out_fd;
1368
1369 /*
1370 * This does a blocking write-and-wait on any page that belongs to the
1371 * subbuffer prior to the one we just wrote.
1372 * Don't care about error values, as these are just hints and ways to
1373 * limit the amount of page cache used.
1374 */
ffe60014 1375 if (orig_offset < stream->max_sb_size) {
3bd1e081
MD
1376 return;
1377 }
ffe60014
DG
1378 lttng_sync_file_range(outfd, orig_offset - stream->max_sb_size,
1379 stream->max_sb_size,
3bd1e081
MD
1380 SYNC_FILE_RANGE_WAIT_BEFORE
1381 | SYNC_FILE_RANGE_WRITE
1382 | SYNC_FILE_RANGE_WAIT_AFTER);
1383 /*
1384 * Give hints to the kernel about how we access the file:
1385 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1386 * we write it.
1387 *
1388 * We need to call fadvise again after the file grows because the
1389 * kernel does not seem to apply fadvise to non-existing parts of the
1390 * file.
1391 *
1392 * Call fadvise _after_ having waited for the page writeback to
1393 * complete because the dirty page writeback semantic is not well
1394 * defined. So it can be expected to lead to lower throughput in
1395 * streaming.
1396 */
c7a78aab 1397 ret = posix_fadvise(outfd, orig_offset - stream->max_sb_size,
ffe60014 1398 stream->max_sb_size, POSIX_FADV_DONTNEED);
a0d0e127 1399 if (ret && ret != -ENOSYS) {
a74a5f4a
JG
1400 errno = ret;
1401 PERROR("posix_fadvise on fd %i", outfd);
c7a78aab 1402 }
3bd1e081
MD
1403}
1404
1405/*
1406 * Initialise the necessary environnement :
1407 * - create a new context
1408 * - create the poll_pipe
1409 * - create the should_quit pipe (for signal handler)
1410 * - create the thread pipe (for splice)
1411 *
1412 * Takes a function pointer as argument, this function is called when data is
1413 * available on a buffer. This function is responsible to do the
1414 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1415 * buffer configuration and then kernctl_put_next_subbuf at the end.
1416 *
1417 * Returns a pointer to the new context or NULL on error.
1418 */
1419struct lttng_consumer_local_data *lttng_consumer_create(
1420 enum lttng_consumer_type type,
4078b776 1421 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
6f9449c2 1422 struct lttng_consumer_local_data *ctx, bool locked_by_caller),
3bd1e081
MD
1423 int (*recv_channel)(struct lttng_consumer_channel *channel),
1424 int (*recv_stream)(struct lttng_consumer_stream *stream),
30319bcb 1425 int (*update_stream)(uint64_t stream_key, uint32_t state))
3bd1e081 1426{
d8ef542d 1427 int ret;
3bd1e081
MD
1428 struct lttng_consumer_local_data *ctx;
1429
fa29bfbf
SM
1430 assert(the_consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
1431 the_consumer_data.type == type);
1432 the_consumer_data.type = type;
3bd1e081 1433
effcf122 1434 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
3bd1e081 1435 if (ctx == NULL) {
7a57cf92 1436 PERROR("allocating context");
3bd1e081
MD
1437 goto error;
1438 }
1439
1440 ctx->consumer_error_socket = -1;
331744e3 1441 ctx->consumer_metadata_socket = -1;
75d83e50 1442 pthread_mutex_init(&ctx->metadata_socket_lock, NULL);
3bd1e081
MD
1443 /* assign the callbacks */
1444 ctx->on_buffer_ready = buffer_ready;
1445 ctx->on_recv_channel = recv_channel;
1446 ctx->on_recv_stream = recv_stream;
1447 ctx->on_update_stream = update_stream;
1448
acdb9057
DG
1449 ctx->consumer_data_pipe = lttng_pipe_open(0);
1450 if (!ctx->consumer_data_pipe) {
3bd1e081
MD
1451 goto error_poll_pipe;
1452 }
1453
02b3d176
DG
1454 ctx->consumer_wakeup_pipe = lttng_pipe_open(0);
1455 if (!ctx->consumer_wakeup_pipe) {
1456 goto error_wakeup_pipe;
1457 }
1458
3bd1e081
MD
1459 ret = pipe(ctx->consumer_should_quit);
1460 if (ret < 0) {
7a57cf92 1461 PERROR("Error creating recv pipe");
3bd1e081
MD
1462 goto error_quit_pipe;
1463 }
1464
d8ef542d
MD
1465 ret = pipe(ctx->consumer_channel_pipe);
1466 if (ret < 0) {
1467 PERROR("Error creating channel pipe");
1468 goto error_channel_pipe;
1469 }
1470
13886d2d
DG
1471 ctx->consumer_metadata_pipe = lttng_pipe_open(0);
1472 if (!ctx->consumer_metadata_pipe) {
fb3a43a9
DG
1473 goto error_metadata_pipe;
1474 }
3bd1e081 1475
e9404c27
JG
1476 ctx->channel_monitor_pipe = -1;
1477
fb3a43a9 1478 return ctx;
3bd1e081 1479
fb3a43a9 1480error_metadata_pipe:
d8ef542d
MD
1481 utils_close_pipe(ctx->consumer_channel_pipe);
1482error_channel_pipe:
d8ef542d 1483 utils_close_pipe(ctx->consumer_should_quit);
3bd1e081 1484error_quit_pipe:
02b3d176
DG
1485 lttng_pipe_destroy(ctx->consumer_wakeup_pipe);
1486error_wakeup_pipe:
acdb9057 1487 lttng_pipe_destroy(ctx->consumer_data_pipe);
3bd1e081
MD
1488error_poll_pipe:
1489 free(ctx);
1490error:
1491 return NULL;
1492}
1493
282dadbc
MD
1494/*
1495 * Iterate over all streams of the hashtable and free them properly.
1496 */
1497static void destroy_data_stream_ht(struct lttng_ht *ht)
1498{
1499 struct lttng_ht_iter iter;
1500 struct lttng_consumer_stream *stream;
1501
1502 if (ht == NULL) {
1503 return;
1504 }
1505
1506 rcu_read_lock();
1507 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1508 /*
1509 * Ignore return value since we are currently cleaning up so any error
1510 * can't be handled.
1511 */
1512 (void) consumer_del_stream(stream, ht);
1513 }
1514 rcu_read_unlock();
1515
1516 lttng_ht_destroy(ht);
1517}
1518
1519/*
1520 * Iterate over all streams of the metadata hashtable and free them
1521 * properly.
1522 */
1523static void destroy_metadata_stream_ht(struct lttng_ht *ht)
1524{
1525 struct lttng_ht_iter iter;
1526 struct lttng_consumer_stream *stream;
1527
1528 if (ht == NULL) {
1529 return;
1530 }
1531
1532 rcu_read_lock();
1533 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1534 /*
1535 * Ignore return value since we are currently cleaning up so any error
1536 * can't be handled.
1537 */
1538 (void) consumer_del_metadata_stream(stream, ht);
1539 }
1540 rcu_read_unlock();
1541
1542 lttng_ht_destroy(ht);
1543}
1544
3bd1e081
MD
1545/*
1546 * Close all fds associated with the instance and free the context.
1547 */
1548void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
1549{
4c462e79
MD
1550 int ret;
1551
ab1027f4
DG
1552 DBG("Consumer destroying it. Closing everything.");
1553
4f2e75b9
DG
1554 if (!ctx) {
1555 return;
1556 }
1557
282dadbc
MD
1558 destroy_data_stream_ht(data_ht);
1559 destroy_metadata_stream_ht(metadata_ht);
1560
4c462e79
MD
1561 ret = close(ctx->consumer_error_socket);
1562 if (ret) {
1563 PERROR("close");
1564 }
331744e3
JD
1565 ret = close(ctx->consumer_metadata_socket);
1566 if (ret) {
1567 PERROR("close");
1568 }
d8ef542d 1569 utils_close_pipe(ctx->consumer_channel_pipe);
acdb9057 1570 lttng_pipe_destroy(ctx->consumer_data_pipe);
13886d2d 1571 lttng_pipe_destroy(ctx->consumer_metadata_pipe);
02b3d176 1572 lttng_pipe_destroy(ctx->consumer_wakeup_pipe);
d8ef542d 1573 utils_close_pipe(ctx->consumer_should_quit);
fb3a43a9 1574
3bd1e081
MD
1575 unlink(ctx->consumer_command_sock_path);
1576 free(ctx);
1577}
1578
6197aea7
DG
1579/*
1580 * Write the metadata stream id on the specified file descriptor.
1581 */
1582static int write_relayd_metadata_id(int fd,
1583 struct lttng_consumer_stream *stream,
239f61af 1584 unsigned long padding)
6197aea7 1585{
6cd525e8 1586 ssize_t ret;
1d4dfdef 1587 struct lttcomm_relayd_metadata_payload hdr;
6197aea7 1588
1d4dfdef
DG
1589 hdr.stream_id = htobe64(stream->relayd_stream_id);
1590 hdr.padding_size = htobe32(padding);
6cd525e8
MD
1591 ret = lttng_write(fd, (void *) &hdr, sizeof(hdr));
1592 if (ret < sizeof(hdr)) {
d7b75ec8 1593 /*
6f04ed72 1594 * This error means that the fd's end is closed so ignore the PERROR
d7b75ec8
DG
1595 * not to clubber the error output since this can happen in a normal
1596 * code path.
1597 */
1598 if (errno != EPIPE) {
1599 PERROR("write metadata stream id");
1600 }
1601 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno);
534d2592
DG
1602 /*
1603 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1604 * handle writting the missing part so report that as an error and
1605 * don't lie to the caller.
1606 */
1607 ret = -1;
6197aea7
DG
1608 goto end;
1609 }
1d4dfdef
DG
1610 DBG("Metadata stream id %" PRIu64 " with padding %lu written before data",
1611 stream->relayd_stream_id, padding);
6197aea7
DG
1612
1613end:
6cd525e8 1614 return (int) ret;
6197aea7
DG
1615}
1616
3bd1e081 1617/*
09e26845
DG
1618 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1619 * core function for writing trace buffers to either the local filesystem or
1620 * the network.
1621 *
d2956687 1622 * It must be called with the stream and the channel lock held.
79d4ffb7 1623 *
09e26845 1624 * Careful review MUST be put if any changes occur!
3bd1e081
MD
1625 *
1626 * Returns the number of bytes written
1627 */
4078b776 1628ssize_t lttng_consumer_on_read_subbuffer_mmap(
128708c3 1629 struct lttng_consumer_stream *stream,
fd424d99 1630 const struct lttng_buffer_view *buffer,
6f9449c2 1631 unsigned long padding)
3bd1e081 1632{
994ab360 1633 ssize_t ret = 0;
f02e1e8a
DG
1634 off_t orig_offset = stream->out_fd_offset;
1635 /* Default is on the disk */
1636 int outfd = stream->out_fd;
f02e1e8a 1637 struct consumer_relayd_sock_pair *relayd = NULL;
8994307f 1638 unsigned int relayd_hang_up = 0;
fd424d99
JG
1639 const size_t subbuf_content_size = buffer->size - padding;
1640 size_t write_len;
f02e1e8a
DG
1641
1642 /* RCU lock for the relayd pointer */
1643 rcu_read_lock();
7fd975c5 1644 assert(stream->net_seq_idx != (uint64_t) -1ULL ||
948411cd 1645 stream->trace_chunk);
d2956687 1646
f02e1e8a 1647 /* Flag that the current stream if set for network streaming. */
da009f2c 1648 if (stream->net_seq_idx != (uint64_t) -1ULL) {
f02e1e8a
DG
1649 relayd = consumer_find_relayd(stream->net_seq_idx);
1650 if (relayd == NULL) {
56591bac 1651 ret = -EPIPE;
f02e1e8a
DG
1652 goto end;
1653 }
1654 }
1655
f02e1e8a
DG
1656 /* Handle stream on the relayd if the output is on the network */
1657 if (relayd) {
fd424d99 1658 unsigned long netlen = subbuf_content_size;
f02e1e8a
DG
1659
1660 /*
1661 * Lock the control socket for the complete duration of the function
1662 * since from this point on we will use the socket.
1663 */
1664 if (stream->metadata_flag) {
1665 /* Metadata requires the control socket. */
1666 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
93ec662e
JD
1667 if (stream->reset_metadata_flag) {
1668 ret = relayd_reset_metadata(&relayd->control_sock,
1669 stream->relayd_stream_id,
1670 stream->metadata_version);
1671 if (ret < 0) {
1672 relayd_hang_up = 1;
1673 goto write_error;
1674 }
1675 stream->reset_metadata_flag = 0;
1676 }
1d4dfdef 1677 netlen += sizeof(struct lttcomm_relayd_metadata_payload);
f02e1e8a
DG
1678 }
1679
1d4dfdef 1680 ret = write_relayd_stream_header(stream, netlen, padding, relayd);
994ab360
DG
1681 if (ret < 0) {
1682 relayd_hang_up = 1;
1683 goto write_error;
1684 }
1685 /* Use the returned socket. */
1686 outfd = ret;
f02e1e8a 1687
994ab360
DG
1688 /* Write metadata stream id before payload */
1689 if (stream->metadata_flag) {
239f61af 1690 ret = write_relayd_metadata_id(outfd, stream, padding);
994ab360 1691 if (ret < 0) {
8994307f
DG
1692 relayd_hang_up = 1;
1693 goto write_error;
1694 }
f02e1e8a 1695 }
1624d5b7 1696
fd424d99
JG
1697 write_len = subbuf_content_size;
1698 } else {
1699 /* No streaming; we have to write the full padding. */
93ec662e
JD
1700 if (stream->metadata_flag && stream->reset_metadata_flag) {
1701 ret = utils_truncate_stream_file(stream->out_fd, 0);
1702 if (ret < 0) {
1703 ERR("Reset metadata file");
1704 goto end;
1705 }
1706 stream->reset_metadata_flag = 0;
1707 }
1708
1624d5b7
JD
1709 /*
1710 * Check if we need to change the tracefile before writing the packet.
1711 */
1712 if (stream->chan->tracefile_size > 0 &&
fd424d99 1713 (stream->tracefile_size_current + buffer->size) >
1624d5b7 1714 stream->chan->tracefile_size) {
d2956687
JG
1715 ret = consumer_stream_rotate_output_files(stream);
1716 if (ret) {
1624d5b7
JD
1717 goto end;
1718 }
309167d2 1719 outfd = stream->out_fd;
a1ae300f 1720 orig_offset = 0;
1624d5b7 1721 }
fd424d99 1722 stream->tracefile_size_current += buffer->size;
fd424d99 1723 write_len = buffer->size;
f02e1e8a
DG
1724 }
1725
d02b8372
DG
1726 /*
1727 * This call guarantee that len or less is returned. It's impossible to
1728 * receive a ret value that is bigger than len.
1729 */
fd424d99 1730 ret = lttng_write(outfd, buffer->data, write_len);
e2d1190b 1731 DBG("Consumer mmap write() ret %zd (len %zu)", ret, write_len);
fd424d99 1732 if (ret < 0 || ((size_t) ret != write_len)) {
d02b8372
DG
1733 /*
1734 * Report error to caller if nothing was written else at least send the
1735 * amount written.
1736 */
1737 if (ret < 0) {
994ab360 1738 ret = -errno;
f02e1e8a 1739 }
994ab360 1740 relayd_hang_up = 1;
f02e1e8a 1741
d02b8372 1742 /* Socket operation failed. We consider the relayd dead */
fcf0f774 1743 if (errno == EPIPE) {
d02b8372
DG
1744 /*
1745 * This is possible if the fd is closed on the other side
1746 * (outfd) or any write problem. It can be verbose a bit for a
1747 * normal execution if for instance the relayd is stopped
1748 * abruptly. This can happen so set this to a DBG statement.
1749 */
1750 DBG("Consumer mmap write detected relayd hang up");
994ab360
DG
1751 } else {
1752 /* Unhandled error, print it and stop function right now. */
fd424d99
JG
1753 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret,
1754 write_len);
f02e1e8a 1755 }
994ab360 1756 goto write_error;
d02b8372
DG
1757 }
1758 stream->output_written += ret;
d02b8372
DG
1759
1760 /* This call is useless on a socket so better save a syscall. */
1761 if (!relayd) {
1762 /* This won't block, but will start writeout asynchronously */
fd424d99 1763 lttng_sync_file_range(outfd, stream->out_fd_offset, write_len,
d02b8372 1764 SYNC_FILE_RANGE_WRITE);
fd424d99 1765 stream->out_fd_offset += write_len;
f5dbe415 1766 lttng_consumer_sync_trace_file(stream, orig_offset);
f02e1e8a 1767 }
f02e1e8a 1768
8994307f
DG
1769write_error:
1770 /*
1771 * This is a special case that the relayd has closed its socket. Let's
1772 * cleanup the relayd object and all associated streams.
1773 */
1774 if (relayd && relayd_hang_up) {
9276e5c8
JR
1775 ERR("Relayd hangup. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx);
1776 lttng_consumer_cleanup_relayd(relayd);
8994307f
DG
1777 }
1778
f02e1e8a
DG
1779end:
1780 /* Unlock only if ctrl socket used */
1781 if (relayd && stream->metadata_flag) {
1782 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1783 }
1784
1785 rcu_read_unlock();
994ab360 1786 return ret;
3bd1e081
MD
1787}
1788
1789/*
1790 * Splice the data from the ring buffer to the tracefile.
1791 *
79d4ffb7
DG
1792 * It must be called with the stream lock held.
1793 *
3bd1e081
MD
1794 * Returns the number of bytes spliced.
1795 */
4078b776 1796ssize_t lttng_consumer_on_read_subbuffer_splice(
3bd1e081 1797 struct lttng_consumer_local_data *ctx,
1d4dfdef 1798 struct lttng_consumer_stream *stream, unsigned long len,
6f9449c2 1799 unsigned long padding)
3bd1e081 1800{
f02e1e8a
DG
1801 ssize_t ret = 0, written = 0, ret_splice = 0;
1802 loff_t offset = 0;
1803 off_t orig_offset = stream->out_fd_offset;
1804 int fd = stream->wait_fd;
1805 /* Default is on the disk */
1806 int outfd = stream->out_fd;
f02e1e8a 1807 struct consumer_relayd_sock_pair *relayd = NULL;
fb3a43a9 1808 int *splice_pipe;
8994307f 1809 unsigned int relayd_hang_up = 0;
f02e1e8a 1810
fa29bfbf 1811 switch (the_consumer_data.type) {
3bd1e081 1812 case LTTNG_CONSUMER_KERNEL:
f02e1e8a 1813 break;
7753dea8
MD
1814 case LTTNG_CONSUMER32_UST:
1815 case LTTNG_CONSUMER64_UST:
f02e1e8a 1816 /* Not supported for user space tracing */
3bd1e081
MD
1817 return -ENOSYS;
1818 default:
1819 ERR("Unknown consumer_data type");
1820 assert(0);
3bd1e081
MD
1821 }
1822
f02e1e8a
DG
1823 /* RCU lock for the relayd pointer */
1824 rcu_read_lock();
1825
1826 /* Flag that the current stream if set for network streaming. */
da009f2c 1827 if (stream->net_seq_idx != (uint64_t) -1ULL) {
f02e1e8a
DG
1828 relayd = consumer_find_relayd(stream->net_seq_idx);
1829 if (relayd == NULL) {
ad0b0d23 1830 written = -ret;
f02e1e8a
DG
1831 goto end;
1832 }
1833 }
a2361a61 1834 splice_pipe = stream->splice_pipe;
fb3a43a9 1835
f02e1e8a 1836 /* Write metadata stream id before payload */
1d4dfdef 1837 if (relayd) {
ad0b0d23 1838 unsigned long total_len = len;
f02e1e8a 1839
1d4dfdef
DG
1840 if (stream->metadata_flag) {
1841 /*
1842 * Lock the control socket for the complete duration of the function
1843 * since from this point on we will use the socket.
1844 */
1845 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1846
93ec662e
JD
1847 if (stream->reset_metadata_flag) {
1848 ret = relayd_reset_metadata(&relayd->control_sock,
1849 stream->relayd_stream_id,
1850 stream->metadata_version);
1851 if (ret < 0) {
1852 relayd_hang_up = 1;
1853 goto write_error;
1854 }
1855 stream->reset_metadata_flag = 0;
1856 }
239f61af 1857 ret = write_relayd_metadata_id(splice_pipe[1], stream,
1d4dfdef
DG
1858 padding);
1859 if (ret < 0) {
1860 written = ret;
ad0b0d23
DG
1861 relayd_hang_up = 1;
1862 goto write_error;
1d4dfdef
DG
1863 }
1864
1865 total_len += sizeof(struct lttcomm_relayd_metadata_payload);
1866 }
1867
1868 ret = write_relayd_stream_header(stream, total_len, padding, relayd);
ad0b0d23
DG
1869 if (ret < 0) {
1870 written = ret;
1871 relayd_hang_up = 1;
1872 goto write_error;
f02e1e8a 1873 }
ad0b0d23
DG
1874 /* Use the returned socket. */
1875 outfd = ret;
1d4dfdef
DG
1876 } else {
1877 /* No streaming, we have to set the len with the full padding */
1878 len += padding;
1624d5b7 1879
93ec662e
JD
1880 if (stream->metadata_flag && stream->reset_metadata_flag) {
1881 ret = utils_truncate_stream_file(stream->out_fd, 0);
1882 if (ret < 0) {
1883 ERR("Reset metadata file");
1884 goto end;
1885 }
1886 stream->reset_metadata_flag = 0;
1887 }
1624d5b7
JD
1888 /*
1889 * Check if we need to change the tracefile before writing the packet.
1890 */
1891 if (stream->chan->tracefile_size > 0 &&
1892 (stream->tracefile_size_current + len) >
1893 stream->chan->tracefile_size) {
d2956687 1894 ret = consumer_stream_rotate_output_files(stream);
1624d5b7 1895 if (ret < 0) {
ad0b0d23 1896 written = ret;
1624d5b7
JD
1897 goto end;
1898 }
309167d2 1899 outfd = stream->out_fd;
a1ae300f 1900 orig_offset = 0;
1624d5b7
JD
1901 }
1902 stream->tracefile_size_current += len;
f02e1e8a
DG
1903 }
1904
1905 while (len > 0) {
1d4dfdef
DG
1906 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1907 (unsigned long)offset, len, fd, splice_pipe[1]);
fb3a43a9 1908 ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len,
f02e1e8a
DG
1909 SPLICE_F_MOVE | SPLICE_F_MORE);
1910 DBG("splice chan to pipe, ret %zd", ret_splice);
1911 if (ret_splice < 0) {
d02b8372 1912 ret = errno;
ad0b0d23 1913 written = -ret;
d02b8372 1914 PERROR("Error in relay splice");
f02e1e8a
DG
1915 goto splice_error;
1916 }
1917
1918 /* Handle stream on the relayd if the output is on the network */
ad0b0d23
DG
1919 if (relayd && stream->metadata_flag) {
1920 size_t metadata_payload_size =
1921 sizeof(struct lttcomm_relayd_metadata_payload);
1922
1923 /* Update counter to fit the spliced data */
1924 ret_splice += metadata_payload_size;
1925 len += metadata_payload_size;
1926 /*
1927 * We do this so the return value can match the len passed as
1928 * argument to this function.
1929 */
1930 written -= metadata_payload_size;
f02e1e8a
DG
1931 }
1932
1933 /* Splice data out */
fb3a43a9 1934 ret_splice = splice(splice_pipe[0], NULL, outfd, NULL,
f02e1e8a 1935 ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE);
a2361a61
JD
1936 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1937 outfd, ret_splice);
f02e1e8a 1938 if (ret_splice < 0) {
d02b8372 1939 ret = errno;
ad0b0d23
DG
1940 written = -ret;
1941 relayd_hang_up = 1;
1942 goto write_error;
f02e1e8a 1943 } else if (ret_splice > len) {
d02b8372
DG
1944 /*
1945 * We don't expect this code path to be executed but you never know
1946 * so this is an extra protection agains a buggy splice().
1947 */
f02e1e8a 1948 ret = errno;
ad0b0d23 1949 written += ret_splice;
d02b8372
DG
1950 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice,
1951 len);
f02e1e8a 1952 goto splice_error;
d02b8372
DG
1953 } else {
1954 /* All good, update current len and continue. */
1955 len -= ret_splice;
f02e1e8a 1956 }
f02e1e8a
DG
1957
1958 /* This call is useless on a socket so better save a syscall. */
1959 if (!relayd) {
1960 /* This won't block, but will start writeout asynchronously */
1961 lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice,
1962 SYNC_FILE_RANGE_WRITE);
1963 stream->out_fd_offset += ret_splice;
1964 }
e5d1a9b3 1965 stream->output_written += ret_splice;
f02e1e8a
DG
1966 written += ret_splice;
1967 }
f5dbe415
JG
1968 if (!relayd) {
1969 lttng_consumer_sync_trace_file(stream, orig_offset);
1970 }
f02e1e8a
DG
1971 goto end;
1972
8994307f
DG
1973write_error:
1974 /*
1975 * This is a special case that the relayd has closed its socket. Let's
1976 * cleanup the relayd object and all associated streams.
1977 */
1978 if (relayd && relayd_hang_up) {
9276e5c8
JR
1979 ERR("Relayd hangup. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx);
1980 lttng_consumer_cleanup_relayd(relayd);
8994307f
DG
1981 /* Skip splice error so the consumer does not fail */
1982 goto end;
1983 }
1984
f02e1e8a
DG
1985splice_error:
1986 /* send the appropriate error description to sessiond */
1987 switch (ret) {
f02e1e8a 1988 case EINVAL:
f73fabfd 1989 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL);
f02e1e8a
DG
1990 break;
1991 case ENOMEM:
f73fabfd 1992 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM);
f02e1e8a
DG
1993 break;
1994 case ESPIPE:
f73fabfd 1995 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE);
f02e1e8a
DG
1996 break;
1997 }
1998
1999end:
2000 if (relayd && stream->metadata_flag) {
2001 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
2002 }
2003
2004 rcu_read_unlock();
2005 return written;
3bd1e081
MD
2006}
2007
15055ce5
JD
2008/*
2009 * Sample the snapshot positions for a specific fd
2010 *
2011 * Returns 0 on success, < 0 on error
2012 */
2013int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream *stream)
2014{
fa29bfbf 2015 switch (the_consumer_data.type) {
15055ce5
JD
2016 case LTTNG_CONSUMER_KERNEL:
2017 return lttng_kconsumer_sample_snapshot_positions(stream);
2018 case LTTNG_CONSUMER32_UST:
2019 case LTTNG_CONSUMER64_UST:
2020 return lttng_ustconsumer_sample_snapshot_positions(stream);
2021 default:
2022 ERR("Unknown consumer_data type");
2023 assert(0);
2024 return -ENOSYS;
2025 }
2026}
3bd1e081
MD
2027/*
2028 * Take a snapshot for a specific fd
2029 *
2030 * Returns 0 on success, < 0 on error
2031 */
ffe60014 2032int lttng_consumer_take_snapshot(struct lttng_consumer_stream *stream)
3bd1e081 2033{
fa29bfbf 2034 switch (the_consumer_data.type) {
3bd1e081 2035 case LTTNG_CONSUMER_KERNEL:
ffe60014 2036 return lttng_kconsumer_take_snapshot(stream);
7753dea8
MD
2037 case LTTNG_CONSUMER32_UST:
2038 case LTTNG_CONSUMER64_UST:
ffe60014 2039 return lttng_ustconsumer_take_snapshot(stream);
3bd1e081
MD
2040 default:
2041 ERR("Unknown consumer_data type");
2042 assert(0);
2043 return -ENOSYS;
2044 }
3bd1e081
MD
2045}
2046
2047/*
2048 * Get the produced position
2049 *
2050 * Returns 0 on success, < 0 on error
2051 */
ffe60014 2052int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
3bd1e081
MD
2053 unsigned long *pos)
2054{
fa29bfbf 2055 switch (the_consumer_data.type) {
3bd1e081 2056 case LTTNG_CONSUMER_KERNEL:
ffe60014 2057 return lttng_kconsumer_get_produced_snapshot(stream, pos);
7753dea8
MD
2058 case LTTNG_CONSUMER32_UST:
2059 case LTTNG_CONSUMER64_UST:
ffe60014 2060 return lttng_ustconsumer_get_produced_snapshot(stream, pos);
3bd1e081
MD
2061 default:
2062 ERR("Unknown consumer_data type");
2063 assert(0);
2064 return -ENOSYS;
2065 }
2066}
2067
15055ce5
JD
2068/*
2069 * Get the consumed position (free-running counter position in bytes).
2070 *
2071 * Returns 0 on success, < 0 on error
2072 */
2073int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
2074 unsigned long *pos)
2075{
fa29bfbf 2076 switch (the_consumer_data.type) {
15055ce5
JD
2077 case LTTNG_CONSUMER_KERNEL:
2078 return lttng_kconsumer_get_consumed_snapshot(stream, pos);
2079 case LTTNG_CONSUMER32_UST:
2080 case LTTNG_CONSUMER64_UST:
2081 return lttng_ustconsumer_get_consumed_snapshot(stream, pos);
2082 default:
2083 ERR("Unknown consumer_data type");
2084 assert(0);
2085 return -ENOSYS;
2086 }
2087}
2088
3bd1e081
MD
2089int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
2090 int sock, struct pollfd *consumer_sockpoll)
2091{
fa29bfbf 2092 switch (the_consumer_data.type) {
3bd1e081
MD
2093 case LTTNG_CONSUMER_KERNEL:
2094 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
7753dea8
MD
2095 case LTTNG_CONSUMER32_UST:
2096 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
2097 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
2098 default:
2099 ERR("Unknown consumer_data type");
2100 assert(0);
2101 return -ENOSYS;
2102 }
2103}
2104
1f8d1c14 2105static
6d574024 2106void lttng_consumer_close_all_metadata(void)
d88aee68 2107{
fa29bfbf 2108 switch (the_consumer_data.type) {
d88aee68
DG
2109 case LTTNG_CONSUMER_KERNEL:
2110 /*
2111 * The Kernel consumer has a different metadata scheme so we don't
2112 * close anything because the stream will be closed by the session
2113 * daemon.
2114 */
2115 break;
2116 case LTTNG_CONSUMER32_UST:
2117 case LTTNG_CONSUMER64_UST:
2118 /*
2119 * Close all metadata streams. The metadata hash table is passed and
2120 * this call iterates over it by closing all wakeup fd. This is safe
2121 * because at this point we are sure that the metadata producer is
2122 * either dead or blocked.
2123 */
6d574024 2124 lttng_ustconsumer_close_all_metadata(metadata_ht);
d88aee68
DG
2125 break;
2126 default:
2127 ERR("Unknown consumer_data type");
2128 assert(0);
2129 }
2130}
2131
fb3a43a9
DG
2132/*
2133 * Clean up a metadata stream and free its memory.
2134 */
e316aad5
DG
2135void consumer_del_metadata_stream(struct lttng_consumer_stream *stream,
2136 struct lttng_ht *ht)
fb3a43a9 2137{
a6ef8ee6
JG
2138 struct lttng_consumer_channel *channel = NULL;
2139 bool free_channel = false;
fb3a43a9
DG
2140
2141 assert(stream);
2142 /*
2143 * This call should NEVER receive regular stream. It must always be
2144 * metadata stream and this is crucial for data structure synchronization.
2145 */
2146 assert(stream->metadata_flag);
2147
e316aad5
DG
2148 DBG3("Consumer delete metadata stream %d", stream->wait_fd);
2149
fa29bfbf 2150 pthread_mutex_lock(&the_consumer_data.lock);
a6ef8ee6
JG
2151 /*
2152 * Note that this assumes that a stream's channel is never changed and
2153 * that the stream's lock doesn't need to be taken to sample its
2154 * channel.
2155 */
2156 channel = stream->chan;
2157 pthread_mutex_lock(&channel->lock);
3dad2c0f 2158 pthread_mutex_lock(&stream->lock);
a6ef8ee6 2159 if (channel->metadata_cache) {
081424af 2160 /* Only applicable to userspace consumers. */
a6ef8ee6 2161 pthread_mutex_lock(&channel->metadata_cache->lock);
081424af 2162 }
8994307f 2163
6d574024
DG
2164 /* Remove any reference to that stream. */
2165 consumer_stream_delete(stream, ht);
ca22feea 2166
6d574024
DG
2167 /* Close down everything including the relayd if one. */
2168 consumer_stream_close(stream);
2169 /* Destroy tracer buffers of the stream. */
2170 consumer_stream_destroy_buffers(stream);
fb3a43a9
DG
2171
2172 /* Atomically decrement channel refcount since other threads can use it. */
a6ef8ee6
JG
2173 if (!uatomic_sub_return(&channel->refcount, 1)
2174 && !uatomic_read(&channel->nb_init_stream_left)) {
c30aaa51 2175 /* Go for channel deletion! */
a6ef8ee6 2176 free_channel = true;
fb3a43a9 2177 }
a6ef8ee6 2178 stream->chan = NULL;
fb3a43a9 2179
73811ecc
DG
2180 /*
2181 * Nullify the stream reference so it is not used after deletion. The
6d574024
DG
2182 * channel lock MUST be acquired before being able to check for a NULL
2183 * pointer value.
73811ecc 2184 */
a6ef8ee6 2185 channel->metadata_stream = NULL;
95671f53 2186 lttng_wait_queue_wake_all(&channel->metadata_pushed_wait_queue);
73811ecc 2187
a6ef8ee6
JG
2188 if (channel->metadata_cache) {
2189 pthread_mutex_unlock(&channel->metadata_cache->lock);
081424af 2190 }
3dad2c0f 2191 pthread_mutex_unlock(&stream->lock);
a6ef8ee6 2192 pthread_mutex_unlock(&channel->lock);
fa29bfbf 2193 pthread_mutex_unlock(&the_consumer_data.lock);
e316aad5 2194
a6ef8ee6
JG
2195 if (free_channel) {
2196 consumer_del_channel(channel);
e316aad5
DG
2197 }
2198
d2956687
JG
2199 lttng_trace_chunk_put(stream->trace_chunk);
2200 stream->trace_chunk = NULL;
6d574024 2201 consumer_stream_free(stream);
fb3a43a9
DG
2202}
2203
2204/*
2205 * Action done with the metadata stream when adding it to the consumer internal
2206 * data structures to handle it.
2207 */
66d583dc 2208void consumer_add_metadata_stream(struct lttng_consumer_stream *stream)
fb3a43a9 2209{
5ab66908 2210 struct lttng_ht *ht = metadata_ht;
76082088 2211 struct lttng_ht_iter iter;
d88aee68 2212 struct lttng_ht_node_u64 *node;
fb3a43a9 2213
e316aad5
DG
2214 assert(stream);
2215 assert(ht);
2216
d88aee68 2217 DBG3("Adding metadata stream %" PRIu64 " to hash table", stream->key);
e316aad5 2218
fa29bfbf 2219 pthread_mutex_lock(&the_consumer_data.lock);
a9838785 2220 pthread_mutex_lock(&stream->chan->lock);
ec6ea7d0 2221 pthread_mutex_lock(&stream->chan->timer_lock);
2e818a6a 2222 pthread_mutex_lock(&stream->lock);
e316aad5 2223
e316aad5
DG
2224 /*
2225 * From here, refcounts are updated so be _careful_ when returning an error
2226 * after this point.
2227 */
2228
fb3a43a9 2229 rcu_read_lock();
76082088
DG
2230
2231 /*
2232 * Lookup the stream just to make sure it does not exist in our internal
2233 * state. This should NEVER happen.
2234 */
d88aee68
DG
2235 lttng_ht_lookup(ht, &stream->key, &iter);
2236 node = lttng_ht_iter_get_node_u64(&iter);
76082088
DG
2237 assert(!node);
2238
e316aad5 2239 /*
ffe60014
DG
2240 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2241 * in terms of destroying the associated channel, because the action that
e316aad5
DG
2242 * causes the count to become 0 also causes a stream to be added. The
2243 * channel deletion will thus be triggered by the following removal of this
2244 * stream.
2245 */
ffe60014 2246 if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) {
f2ad556d
MD
2247 /* Increment refcount before decrementing nb_init_stream_left */
2248 cmm_smp_wmb();
ffe60014 2249 uatomic_dec(&stream->chan->nb_init_stream_left);
e316aad5
DG
2250 }
2251
d88aee68 2252 lttng_ht_add_unique_u64(ht, &stream->node);
ca22feea 2253
fa29bfbf
SM
2254 lttng_ht_add_u64(the_consumer_data.stream_per_chan_id_ht,
2255 &stream->node_channel_id);
d8ef542d 2256
ca22feea
DG
2257 /*
2258 * Add stream to the stream_list_ht of the consumer data. No need to steal
2259 * the key since the HT does not use it and we allow to add redundant keys
2260 * into this table.
2261 */
fa29bfbf
SM
2262 lttng_ht_add_u64(the_consumer_data.stream_list_ht,
2263 &stream->node_session_id);
ca22feea 2264
fb3a43a9 2265 rcu_read_unlock();
e316aad5 2266
2e818a6a 2267 pthread_mutex_unlock(&stream->lock);
a9838785 2268 pthread_mutex_unlock(&stream->chan->lock);
ec6ea7d0 2269 pthread_mutex_unlock(&stream->chan->timer_lock);
fa29bfbf 2270 pthread_mutex_unlock(&the_consumer_data.lock);
fb3a43a9
DG
2271}
2272
8994307f
DG
2273/*
2274 * Delete data stream that are flagged for deletion (endpoint_status).
2275 */
2276static void validate_endpoint_status_data_stream(void)
2277{
2278 struct lttng_ht_iter iter;
2279 struct lttng_consumer_stream *stream;
2280
2281 DBG("Consumer delete flagged data stream");
2282
2283 rcu_read_lock();
2284 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
2285 /* Validate delete flag of the stream */
79d4ffb7 2286 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
8994307f
DG
2287 continue;
2288 }
2289 /* Delete it right now */
2290 consumer_del_stream(stream, data_ht);
2291 }
2292 rcu_read_unlock();
2293}
2294
2295/*
2296 * Delete metadata stream that are flagged for deletion (endpoint_status).
2297 */
2298static void validate_endpoint_status_metadata_stream(
2299 struct lttng_poll_event *pollset)
2300{
2301 struct lttng_ht_iter iter;
2302 struct lttng_consumer_stream *stream;
2303
2304 DBG("Consumer delete flagged metadata stream");
2305
2306 assert(pollset);
2307
2308 rcu_read_lock();
2309 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
2310 /* Validate delete flag of the stream */
79d4ffb7 2311 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
8994307f
DG
2312 continue;
2313 }
2314 /*
2315 * Remove from pollset so the metadata thread can continue without
2316 * blocking on a deleted stream.
2317 */
2318 lttng_poll_del(pollset, stream->wait_fd);
2319
2320 /* Delete it right now */
2321 consumer_del_metadata_stream(stream, metadata_ht);
2322 }
2323 rcu_read_unlock();
2324}
2325
fb3a43a9
DG
2326/*
2327 * Thread polls on metadata file descriptor and write them on disk or on the
2328 * network.
2329 */
7d980def 2330void *consumer_thread_metadata_poll(void *data)
fb3a43a9 2331{
1fc79fb4 2332 int ret, i, pollfd, err = -1;
fb3a43a9 2333 uint32_t revents, nb_fd;
e316aad5 2334 struct lttng_consumer_stream *stream = NULL;
fb3a43a9 2335 struct lttng_ht_iter iter;
d88aee68 2336 struct lttng_ht_node_u64 *node;
fb3a43a9
DG
2337 struct lttng_poll_event events;
2338 struct lttng_consumer_local_data *ctx = data;
2339 ssize_t len;
2340
2341 rcu_register_thread();
2342
1fc79fb4
MD
2343 health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_METADATA);
2344
2d57de81
MD
2345 if (testpoint(consumerd_thread_metadata)) {
2346 goto error_testpoint;
2347 }
2348
9ce5646a
MD
2349 health_code_update();
2350
fb3a43a9
DG
2351 DBG("Thread metadata poll started");
2352
fb3a43a9
DG
2353 /* Size is set to 1 for the consumer_metadata pipe */
2354 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2355 if (ret < 0) {
2356 ERR("Poll set creation failed");
d8ef542d 2357 goto end_poll;
fb3a43a9
DG
2358 }
2359
13886d2d
DG
2360 ret = lttng_poll_add(&events,
2361 lttng_pipe_get_readfd(ctx->consumer_metadata_pipe), LPOLLIN);
fb3a43a9
DG
2362 if (ret < 0) {
2363 goto end;
2364 }
2365
2366 /* Main loop */
2367 DBG("Metadata main loop started");
2368
2369 while (1) {
fb3a43a9 2370restart:
7fa2082e 2371 health_code_update();
9ce5646a 2372 health_poll_entry();
7fa2082e 2373 DBG("Metadata poll wait");
fb3a43a9 2374 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
2375 DBG("Metadata poll return from wait with %d fd(s)",
2376 LTTNG_POLL_GETNB(&events));
9ce5646a 2377 health_poll_exit();
40063ead 2378 DBG("Metadata event caught in thread");
fb3a43a9
DG
2379 if (ret < 0) {
2380 if (errno == EINTR) {
40063ead 2381 ERR("Poll EINTR caught");
fb3a43a9
DG
2382 goto restart;
2383 }
d9607cd7
MD
2384 if (LTTNG_POLL_GETNB(&events) == 0) {
2385 err = 0; /* All is OK */
2386 }
2387 goto end;
fb3a43a9
DG
2388 }
2389
0d9c5d77
DG
2390 nb_fd = ret;
2391
e316aad5 2392 /* From here, the event is a metadata wait fd */
fb3a43a9 2393 for (i = 0; i < nb_fd; i++) {
9ce5646a
MD
2394 health_code_update();
2395
fb3a43a9
DG
2396 revents = LTTNG_POLL_GETEV(&events, i);
2397 pollfd = LTTNG_POLL_GETFD(&events, i);
2398
13886d2d 2399 if (pollfd == lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)) {
03e43155 2400 if (revents & LPOLLIN) {
13886d2d
DG
2401 ssize_t pipe_len;
2402
2403 pipe_len = lttng_pipe_read(ctx->consumer_metadata_pipe,
2404 &stream, sizeof(stream));
6cd525e8 2405 if (pipe_len < sizeof(stream)) {
03e43155
MD
2406 if (pipe_len < 0) {
2407 PERROR("read metadata stream");
2408 }
fb3a43a9 2409 /*
03e43155
MD
2410 * Remove the pipe from the poll set and continue the loop
2411 * since their might be data to consume.
fb3a43a9 2412 */
03e43155
MD
2413 lttng_poll_del(&events,
2414 lttng_pipe_get_readfd(ctx->consumer_metadata_pipe));
2415 lttng_pipe_read_close(ctx->consumer_metadata_pipe);
fb3a43a9
DG
2416 continue;
2417 }
2418
8994307f
DG
2419 /* A NULL stream means that the state has changed. */
2420 if (stream == NULL) {
2421 /* Check for deleted streams. */
2422 validate_endpoint_status_metadata_stream(&events);
3714380f 2423 goto restart;
8994307f
DG
2424 }
2425
fb3a43a9
DG
2426 DBG("Adding metadata stream %d to poll set",
2427 stream->wait_fd);
2428
fb3a43a9
DG
2429 /* Add metadata stream to the global poll events list */
2430 lttng_poll_add(&events, stream->wait_fd,
6d574024 2431 LPOLLIN | LPOLLPRI | LPOLLHUP);
03e43155
MD
2432 } else if (revents & (LPOLLERR | LPOLLHUP)) {
2433 DBG("Metadata thread pipe hung up");
2434 /*
2435 * Remove the pipe from the poll set and continue the loop
2436 * since their might be data to consume.
2437 */
2438 lttng_poll_del(&events,
2439 lttng_pipe_get_readfd(ctx->consumer_metadata_pipe));
2440 lttng_pipe_read_close(ctx->consumer_metadata_pipe);
2441 continue;
2442 } else {
2443 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2444 goto end;
fb3a43a9
DG
2445 }
2446
e316aad5 2447 /* Handle other stream */
fb3a43a9
DG
2448 continue;
2449 }
2450
d09e1200 2451 rcu_read_lock();
d88aee68
DG
2452 {
2453 uint64_t tmp_id = (uint64_t) pollfd;
2454
2455 lttng_ht_lookup(metadata_ht, &tmp_id, &iter);
2456 }
2457 node = lttng_ht_iter_get_node_u64(&iter);
e316aad5 2458 assert(node);
fb3a43a9
DG
2459
2460 stream = caa_container_of(node, struct lttng_consumer_stream,
58b1f425 2461 node);
fb3a43a9 2462
03e43155
MD
2463 if (revents & (LPOLLIN | LPOLLPRI)) {
2464 /* Get the data out of the metadata file descriptor */
2465 DBG("Metadata available on fd %d", pollfd);
2466 assert(stream->wait_fd == pollfd);
2467
2468 do {
2469 health_code_update();
2470
6f9449c2 2471 len = ctx->on_buffer_ready(stream, ctx, false);
03e43155
MD
2472 /*
2473 * We don't check the return value here since if we get
83f4233d 2474 * a negative len, it means an error occurred thus we
03e43155
MD
2475 * simply remove it from the poll set and free the
2476 * stream.
2477 */
2478 } while (len > 0);
2479
2480 /* It's ok to have an unavailable sub-buffer */
2481 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2482 /* Clean up stream from consumer and free it. */
2483 lttng_poll_del(&events, stream->wait_fd);
2484 consumer_del_metadata_stream(stream, metadata_ht);
2485 }
2486 } else if (revents & (LPOLLERR | LPOLLHUP)) {
e316aad5 2487 DBG("Metadata fd %d is hup|err.", pollfd);
fa29bfbf
SM
2488 if (!stream->hangup_flush_done &&
2489 (the_consumer_data.type == LTTNG_CONSUMER32_UST ||
2490 the_consumer_data.type ==
2491 LTTNG_CONSUMER64_UST)) {
fb3a43a9
DG
2492 DBG("Attempting to flush and consume the UST buffers");
2493 lttng_ustconsumer_on_stream_hangup(stream);
2494
2495 /* We just flushed the stream now read it. */
4bb94b75 2496 do {
9ce5646a
MD
2497 health_code_update();
2498
6f9449c2 2499 len = ctx->on_buffer_ready(stream, ctx, false);
4bb94b75
DG
2500 /*
2501 * We don't check the return value here since if we get
83f4233d 2502 * a negative len, it means an error occurred thus we
4bb94b75
DG
2503 * simply remove it from the poll set and free the
2504 * stream.
2505 */
2506 } while (len > 0);
fb3a43a9
DG
2507 }
2508
fb3a43a9 2509 lttng_poll_del(&events, stream->wait_fd);
e316aad5
DG
2510 /*
2511 * This call update the channel states, closes file descriptors
2512 * and securely free the stream.
2513 */
2514 consumer_del_metadata_stream(stream, metadata_ht);
03e43155
MD
2515 } else {
2516 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
6f2f1a70 2517 rcu_read_unlock();
03e43155 2518 goto end;
fb3a43a9 2519 }
e316aad5 2520 /* Release RCU lock for the stream looked up */
d09e1200 2521 rcu_read_unlock();
fb3a43a9
DG
2522 }
2523 }
2524
1fc79fb4
MD
2525 /* All is OK */
2526 err = 0;
fb3a43a9
DG
2527end:
2528 DBG("Metadata poll thread exiting");
fb3a43a9 2529
d8ef542d
MD
2530 lttng_poll_clean(&events);
2531end_poll:
2d57de81 2532error_testpoint:
1fc79fb4
MD
2533 if (err) {
2534 health_error();
2535 ERR("Health error occurred in %s", __func__);
2536 }
2537 health_unregister(health_consumerd);
fb3a43a9
DG
2538 rcu_unregister_thread();
2539 return NULL;
2540}
2541
3bd1e081 2542/*
e4421fec 2543 * This thread polls the fds in the set to consume the data and write
3bd1e081
MD
2544 * it to tracefile if necessary.
2545 */
7d980def 2546void *consumer_thread_data_poll(void *data)
3bd1e081 2547{
1fc79fb4 2548 int num_rdy, num_hup, high_prio, ret, i, err = -1;
3bd1e081
MD
2549 struct pollfd *pollfd = NULL;
2550 /* local view of the streams */
c869f647 2551 struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL;
3bd1e081 2552 /* local view of consumer_data.fds_count */
8bdcc002
JG
2553 int nb_fd = 0;
2554 /* 2 for the consumer_data_pipe and wake up pipe */
2555 const int nb_pipes_fd = 2;
9a2fcf78
JD
2556 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2557 int nb_inactive_fd = 0;
3bd1e081 2558 struct lttng_consumer_local_data *ctx = data;
00e2e675 2559 ssize_t len;
3bd1e081 2560
e7b994a3
DG
2561 rcu_register_thread();
2562
1fc79fb4
MD
2563 health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_DATA);
2564
2d57de81
MD
2565 if (testpoint(consumerd_thread_data)) {
2566 goto error_testpoint;
2567 }
2568
9ce5646a
MD
2569 health_code_update();
2570
4df6c8cb
MD
2571 local_stream = zmalloc(sizeof(struct lttng_consumer_stream *));
2572 if (local_stream == NULL) {
2573 PERROR("local_stream malloc");
2574 goto end;
2575 }
3bd1e081
MD
2576
2577 while (1) {
9ce5646a
MD
2578 health_code_update();
2579
3bd1e081
MD
2580 high_prio = 0;
2581 num_hup = 0;
2582
2583 /*
e4421fec 2584 * the fds set has been updated, we need to update our
3bd1e081
MD
2585 * local array as well
2586 */
fa29bfbf
SM
2587 pthread_mutex_lock(&the_consumer_data.lock);
2588 if (the_consumer_data.need_update) {
0e428499
DG
2589 free(pollfd);
2590 pollfd = NULL;
2591
2592 free(local_stream);
2593 local_stream = NULL;
3bd1e081 2594
8bdcc002 2595 /* Allocate for all fds */
fa29bfbf
SM
2596 pollfd = zmalloc((the_consumer_data.stream_count +
2597 nb_pipes_fd) *
2598 sizeof(struct pollfd));
3bd1e081 2599 if (pollfd == NULL) {
7a57cf92 2600 PERROR("pollfd malloc");
fa29bfbf 2601 pthread_mutex_unlock(&the_consumer_data.lock);
3bd1e081
MD
2602 goto end;
2603 }
2604
fa29bfbf
SM
2605 local_stream = zmalloc((the_consumer_data.stream_count +
2606 nb_pipes_fd) *
747f8642 2607 sizeof(struct lttng_consumer_stream *));
3bd1e081 2608 if (local_stream == NULL) {
7a57cf92 2609 PERROR("local_stream malloc");
fa29bfbf 2610 pthread_mutex_unlock(&the_consumer_data.lock);
3bd1e081
MD
2611 goto end;
2612 }
ffe60014 2613 ret = update_poll_array(ctx, &pollfd, local_stream,
9a2fcf78 2614 data_ht, &nb_inactive_fd);
3bd1e081
MD
2615 if (ret < 0) {
2616 ERR("Error in allocating pollfd or local_outfds");
f73fabfd 2617 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
fa29bfbf 2618 pthread_mutex_unlock(&the_consumer_data.lock);
3bd1e081
MD
2619 goto end;
2620 }
2621 nb_fd = ret;
fa29bfbf 2622 the_consumer_data.need_update = 0;
3bd1e081 2623 }
fa29bfbf 2624 pthread_mutex_unlock(&the_consumer_data.lock);
3bd1e081 2625
4078b776 2626 /* No FDs and consumer_quit, consumer_cleanup the thread */
9a2fcf78
JD
2627 if (nb_fd == 0 && nb_inactive_fd == 0 &&
2628 CMM_LOAD_SHARED(consumer_quit) == 1) {
1fc79fb4 2629 err = 0; /* All is OK */
4078b776
MD
2630 goto end;
2631 }
3bd1e081 2632 /* poll on the array of fds */
88f2b785 2633 restart:
261de637 2634 DBG("polling on %d fd", nb_fd + nb_pipes_fd);
cf0bcb51
JG
2635 if (testpoint(consumerd_thread_data_poll)) {
2636 goto end;
2637 }
9ce5646a 2638 health_poll_entry();
261de637 2639 num_rdy = poll(pollfd, nb_fd + nb_pipes_fd, -1);
9ce5646a 2640 health_poll_exit();
3bd1e081
MD
2641 DBG("poll num_rdy : %d", num_rdy);
2642 if (num_rdy == -1) {
88f2b785
MD
2643 /*
2644 * Restart interrupted system call.
2645 */
2646 if (errno == EINTR) {
2647 goto restart;
2648 }
7a57cf92 2649 PERROR("Poll error");
f73fabfd 2650 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3bd1e081
MD
2651 goto end;
2652 } else if (num_rdy == 0) {
2653 DBG("Polling thread timed out");
2654 goto end;
2655 }
2656
80957876
JG
2657 if (caa_unlikely(data_consumption_paused)) {
2658 DBG("Data consumption paused, sleeping...");
2659 sleep(1);
2660 goto restart;
2661 }
2662
3bd1e081 2663 /*
50f8ae69 2664 * If the consumer_data_pipe triggered poll go directly to the
00e2e675
DG
2665 * beginning of the loop to update the array. We want to prioritize
2666 * array update over low-priority reads.
3bd1e081 2667 */
509bb1cf 2668 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
ab30f567 2669 ssize_t pipe_readlen;
04fdd819 2670
50f8ae69 2671 DBG("consumer_data_pipe wake up");
acdb9057
DG
2672 pipe_readlen = lttng_pipe_read(ctx->consumer_data_pipe,
2673 &new_stream, sizeof(new_stream));
6cd525e8
MD
2674 if (pipe_readlen < sizeof(new_stream)) {
2675 PERROR("Consumer data pipe");
23f5f35d
DG
2676 /* Continue so we can at least handle the current stream(s). */
2677 continue;
2678 }
c869f647
DG
2679
2680 /*
2681 * If the stream is NULL, just ignore it. It's also possible that
2682 * the sessiond poll thread changed the consumer_quit state and is
2683 * waking us up to test it.
2684 */
2685 if (new_stream == NULL) {
8994307f 2686 validate_endpoint_status_data_stream();
c869f647
DG
2687 continue;
2688 }
2689
c869f647 2690 /* Continue to update the local streams and handle prio ones */
3bd1e081
MD
2691 continue;
2692 }
2693
02b3d176
DG
2694 /* Handle wakeup pipe. */
2695 if (pollfd[nb_fd + 1].revents & (POLLIN | POLLPRI)) {
2696 char dummy;
2697 ssize_t pipe_readlen;
2698
2699 pipe_readlen = lttng_pipe_read(ctx->consumer_wakeup_pipe, &dummy,
2700 sizeof(dummy));
2701 if (pipe_readlen < 0) {
2702 PERROR("Consumer data wakeup pipe");
2703 }
2704 /* We've been awakened to handle stream(s). */
2705 ctx->has_wakeup = 0;
2706 }
2707
3bd1e081
MD
2708 /* Take care of high priority channels first. */
2709 for (i = 0; i < nb_fd; i++) {
9ce5646a
MD
2710 health_code_update();
2711
9617607b
DG
2712 if (local_stream[i] == NULL) {
2713 continue;
2714 }
fb3a43a9 2715 if (pollfd[i].revents & POLLPRI) {
d41f73b7
MD
2716 DBG("Urgent read on fd %d", pollfd[i].fd);
2717 high_prio = 1;
6f9449c2 2718 len = ctx->on_buffer_ready(local_stream[i], ctx, false);
d41f73b7 2719 /* it's ok to have an unavailable sub-buffer */
b64403e3 2720 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
ab1027f4
DG
2721 /* Clean the stream and free it. */
2722 consumer_del_stream(local_stream[i], data_ht);
9617607b 2723 local_stream[i] = NULL;
4078b776
MD
2724 } else if (len > 0) {
2725 local_stream[i]->data_read = 1;
d41f73b7 2726 }
3bd1e081
MD
2727 }
2728 }
2729
4078b776
MD
2730 /*
2731 * If we read high prio channel in this loop, try again
2732 * for more high prio data.
2733 */
2734 if (high_prio) {
3bd1e081
MD
2735 continue;
2736 }
2737
2738 /* Take care of low priority channels. */
4078b776 2739 for (i = 0; i < nb_fd; i++) {
9ce5646a
MD
2740 health_code_update();
2741
9617607b
DG
2742 if (local_stream[i] == NULL) {
2743 continue;
2744 }
4078b776 2745 if ((pollfd[i].revents & POLLIN) ||
02b3d176
DG
2746 local_stream[i]->hangup_flush_done ||
2747 local_stream[i]->has_data) {
4078b776 2748 DBG("Normal read on fd %d", pollfd[i].fd);
6f9449c2 2749 len = ctx->on_buffer_ready(local_stream[i], ctx, false);
4078b776 2750 /* it's ok to have an unavailable sub-buffer */
b64403e3 2751 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
ab1027f4
DG
2752 /* Clean the stream and free it. */
2753 consumer_del_stream(local_stream[i], data_ht);
9617607b 2754 local_stream[i] = NULL;
4078b776
MD
2755 } else if (len > 0) {
2756 local_stream[i]->data_read = 1;
2757 }
2758 }
2759 }
2760
2761 /* Handle hangup and errors */
2762 for (i = 0; i < nb_fd; i++) {
9ce5646a
MD
2763 health_code_update();
2764
9617607b
DG
2765 if (local_stream[i] == NULL) {
2766 continue;
2767 }
4078b776
MD
2768 if (!local_stream[i]->hangup_flush_done
2769 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
fa29bfbf
SM
2770 && (the_consumer_data.type == LTTNG_CONSUMER32_UST
2771 || the_consumer_data.type == LTTNG_CONSUMER64_UST)) {
4078b776 2772 DBG("fd %d is hup|err|nval. Attempting flush and read.",
9617607b 2773 pollfd[i].fd);
4078b776
MD
2774 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
2775 /* Attempt read again, for the data we just flushed. */
2776 local_stream[i]->data_read = 1;
2777 }
2778 /*
2779 * If the poll flag is HUP/ERR/NVAL and we have
2780 * read no data in this pass, we can remove the
2781 * stream from its hash table.
2782 */
2783 if ((pollfd[i].revents & POLLHUP)) {
2784 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
2785 if (!local_stream[i]->data_read) {
43c34bc3 2786 consumer_del_stream(local_stream[i], data_ht);
9617607b 2787 local_stream[i] = NULL;
4078b776
MD
2788 num_hup++;
2789 }
2790 } else if (pollfd[i].revents & POLLERR) {
2791 ERR("Error returned in polling fd %d.", pollfd[i].fd);
2792 if (!local_stream[i]->data_read) {
43c34bc3 2793 consumer_del_stream(local_stream[i], data_ht);
9617607b 2794 local_stream[i] = NULL;
4078b776
MD
2795 num_hup++;
2796 }
2797 } else if (pollfd[i].revents & POLLNVAL) {
2798 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
2799 if (!local_stream[i]->data_read) {
43c34bc3 2800 consumer_del_stream(local_stream[i], data_ht);
9617607b 2801 local_stream[i] = NULL;
4078b776 2802 num_hup++;
3bd1e081
MD
2803 }
2804 }
9617607b
DG
2805 if (local_stream[i] != NULL) {
2806 local_stream[i]->data_read = 0;
2807 }
3bd1e081
MD
2808 }
2809 }
1fc79fb4
MD
2810 /* All is OK */
2811 err = 0;
3bd1e081
MD
2812end:
2813 DBG("polling thread exiting");
0e428499
DG
2814 free(pollfd);
2815 free(local_stream);
fb3a43a9
DG
2816
2817 /*
2818 * Close the write side of the pipe so epoll_wait() in
7d980def
DG
2819 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2820 * read side of the pipe. If we close them both, epoll_wait strangely does
2821 * not return and could create a endless wait period if the pipe is the
2822 * only tracked fd in the poll set. The thread will take care of closing
2823 * the read side.
fb3a43a9 2824 */
13886d2d 2825 (void) lttng_pipe_write_close(ctx->consumer_metadata_pipe);
fb3a43a9 2826
2d57de81 2827error_testpoint:
1fc79fb4
MD
2828 if (err) {
2829 health_error();
2830 ERR("Health error occurred in %s", __func__);
2831 }
2832 health_unregister(health_consumerd);
2833
e7b994a3 2834 rcu_unregister_thread();
3bd1e081
MD
2835 return NULL;
2836}
2837
d8ef542d
MD
2838/*
2839 * Close wake-up end of each stream belonging to the channel. This will
2840 * allow the poll() on the stream read-side to detect when the
2841 * write-side (application) finally closes them.
2842 */
2843static
2844void consumer_close_channel_streams(struct lttng_consumer_channel *channel)
2845{
2846 struct lttng_ht *ht;
2847 struct lttng_consumer_stream *stream;
2848 struct lttng_ht_iter iter;
2849
fa29bfbf 2850 ht = the_consumer_data.stream_per_chan_id_ht;
d8ef542d
MD
2851
2852 rcu_read_lock();
2853 cds_lfht_for_each_entry_duplicate(ht->ht,
2854 ht->hash_fct(&channel->key, lttng_ht_seed),
2855 ht->match_fct, &channel->key,
2856 &iter.iter, stream, node_channel_id.node) {
f2ad556d
MD
2857 /*
2858 * Protect against teardown with mutex.
2859 */
2860 pthread_mutex_lock(&stream->lock);
2861 if (cds_lfht_is_node_deleted(&stream->node.node)) {
2862 goto next;
2863 }
fa29bfbf 2864 switch (the_consumer_data.type) {
d8ef542d
MD
2865 case LTTNG_CONSUMER_KERNEL:
2866 break;
2867 case LTTNG_CONSUMER32_UST:
2868 case LTTNG_CONSUMER64_UST:
b4a650f3
DG
2869 if (stream->metadata_flag) {
2870 /* Safe and protected by the stream lock. */
2871 lttng_ustconsumer_close_metadata(stream->chan);
2872 } else {
2873 /*
2874 * Note: a mutex is taken internally within
2875 * liblttng-ust-ctl to protect timer wakeup_fd
2876 * use from concurrent close.
2877 */
2878 lttng_ustconsumer_close_stream_wakeup(stream);
2879 }
d8ef542d
MD
2880 break;
2881 default:
2882 ERR("Unknown consumer_data type");
2883 assert(0);
2884 }
f2ad556d
MD
2885 next:
2886 pthread_mutex_unlock(&stream->lock);
d8ef542d
MD
2887 }
2888 rcu_read_unlock();
2889}
2890
2891static void destroy_channel_ht(struct lttng_ht *ht)
2892{
2893 struct lttng_ht_iter iter;
2894 struct lttng_consumer_channel *channel;
2895 int ret;
2896
2897 if (ht == NULL) {
2898 return;
2899 }
2900
2901 rcu_read_lock();
2902 cds_lfht_for_each_entry(ht->ht, &iter.iter, channel, wait_fd_node.node) {
2903 ret = lttng_ht_del(ht, &iter);
2904 assert(ret != 0);
2905 }
2906 rcu_read_unlock();
2907
2908 lttng_ht_destroy(ht);
2909}
2910
2911/*
2912 * This thread polls the channel fds to detect when they are being
2913 * closed. It closes all related streams if the channel is detected as
2914 * closed. It is currently only used as a shim layer for UST because the
2915 * consumerd needs to keep the per-stream wakeup end of pipes open for
2916 * periodical flush.
2917 */
2918void *consumer_thread_channel_poll(void *data)
2919{
1fc79fb4 2920 int ret, i, pollfd, err = -1;
d8ef542d
MD
2921 uint32_t revents, nb_fd;
2922 struct lttng_consumer_channel *chan = NULL;
2923 struct lttng_ht_iter iter;
2924 struct lttng_ht_node_u64 *node;
2925 struct lttng_poll_event events;
2926 struct lttng_consumer_local_data *ctx = data;
2927 struct lttng_ht *channel_ht;
2928
2929 rcu_register_thread();
2930
1fc79fb4
MD
2931 health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_CHANNEL);
2932
2d57de81
MD
2933 if (testpoint(consumerd_thread_channel)) {
2934 goto error_testpoint;
2935 }
2936
9ce5646a
MD
2937 health_code_update();
2938
d8ef542d
MD
2939 channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2940 if (!channel_ht) {
2941 /* ENOMEM at this point. Better to bail out. */
2942 goto end_ht;
2943 }
2944
2945 DBG("Thread channel poll started");
2946
2947 /* Size is set to 1 for the consumer_channel pipe */
2948 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2949 if (ret < 0) {
2950 ERR("Poll set creation failed");
2951 goto end_poll;
2952 }
2953
2954 ret = lttng_poll_add(&events, ctx->consumer_channel_pipe[0], LPOLLIN);
2955 if (ret < 0) {
2956 goto end;
2957 }
2958
2959 /* Main loop */
2960 DBG("Channel main loop started");
2961
2962 while (1) {
d8ef542d 2963restart:
7fa2082e
MD
2964 health_code_update();
2965 DBG("Channel poll wait");
9ce5646a 2966 health_poll_entry();
d8ef542d 2967 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
2968 DBG("Channel poll return from wait with %d fd(s)",
2969 LTTNG_POLL_GETNB(&events));
9ce5646a 2970 health_poll_exit();
40063ead 2971 DBG("Channel event caught in thread");
d8ef542d
MD
2972 if (ret < 0) {
2973 if (errno == EINTR) {
40063ead 2974 ERR("Poll EINTR caught");
d8ef542d
MD
2975 goto restart;
2976 }
d9607cd7
MD
2977 if (LTTNG_POLL_GETNB(&events) == 0) {
2978 err = 0; /* All is OK */
2979 }
d8ef542d
MD
2980 goto end;
2981 }
2982
2983 nb_fd = ret;
2984
2985 /* From here, the event is a channel wait fd */
2986 for (i = 0; i < nb_fd; i++) {
9ce5646a
MD
2987 health_code_update();
2988
d8ef542d
MD
2989 revents = LTTNG_POLL_GETEV(&events, i);
2990 pollfd = LTTNG_POLL_GETFD(&events, i);
2991
d8ef542d 2992 if (pollfd == ctx->consumer_channel_pipe[0]) {
03e43155 2993 if (revents & LPOLLIN) {
d8ef542d 2994 enum consumer_channel_action action;
a0cbdd2e 2995 uint64_t key;
d8ef542d 2996
a0cbdd2e 2997 ret = read_channel_pipe(ctx, &chan, &key, &action);
d8ef542d 2998 if (ret <= 0) {
03e43155
MD
2999 if (ret < 0) {
3000 ERR("Error reading channel pipe");
3001 }
3002 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
d8ef542d
MD
3003 continue;
3004 }
3005
3006 switch (action) {
3007 case CONSUMER_CHANNEL_ADD:
3008 DBG("Adding channel %d to poll set",
3009 chan->wait_fd);
3010
3011 lttng_ht_node_init_u64(&chan->wait_fd_node,
3012 chan->wait_fd);
c7260a81 3013 rcu_read_lock();
d8ef542d
MD
3014 lttng_ht_add_unique_u64(channel_ht,
3015 &chan->wait_fd_node);
c7260a81 3016 rcu_read_unlock();
d8ef542d
MD
3017 /* Add channel to the global poll events list */
3018 lttng_poll_add(&events, chan->wait_fd,
03e43155 3019 LPOLLERR | LPOLLHUP);
d8ef542d 3020 break;
a0cbdd2e
MD
3021 case CONSUMER_CHANNEL_DEL:
3022 {
b4a650f3
DG
3023 /*
3024 * This command should never be called if the channel
3025 * has streams monitored by either the data or metadata
3026 * thread. The consumer only notify this thread with a
3027 * channel del. command if it receives a destroy
3028 * channel command from the session daemon that send it
3029 * if a command prior to the GET_CHANNEL failed.
3030 */
3031
c7260a81 3032 rcu_read_lock();
a0cbdd2e
MD
3033 chan = consumer_find_channel(key);
3034 if (!chan) {
c7260a81 3035 rcu_read_unlock();
a0cbdd2e
MD
3036 ERR("UST consumer get channel key %" PRIu64 " not found for del channel", key);
3037 break;
3038 }
3039 lttng_poll_del(&events, chan->wait_fd);
f623cc0b 3040 iter.iter.node = &chan->wait_fd_node.node;
a0cbdd2e
MD
3041 ret = lttng_ht_del(channel_ht, &iter);
3042 assert(ret == 0);
a0cbdd2e 3043
fa29bfbf 3044 switch (the_consumer_data.type) {
f2a444f1
DG
3045 case LTTNG_CONSUMER_KERNEL:
3046 break;
3047 case LTTNG_CONSUMER32_UST:
3048 case LTTNG_CONSUMER64_UST:
212d67a2
DG
3049 health_code_update();
3050 /* Destroy streams that might have been left in the stream list. */
3051 clean_channel_stream_list(chan);
f2a444f1
DG
3052 break;
3053 default:
3054 ERR("Unknown consumer_data type");
3055 assert(0);
3056 }
3057
a0cbdd2e
MD
3058 /*
3059 * Release our own refcount. Force channel deletion even if
3060 * streams were not initialized.
3061 */
3062 if (!uatomic_sub_return(&chan->refcount, 1)) {
3063 consumer_del_channel(chan);
3064 }
c7260a81 3065 rcu_read_unlock();
a0cbdd2e
MD
3066 goto restart;
3067 }
d8ef542d
MD
3068 case CONSUMER_CHANNEL_QUIT:
3069 /*
3070 * Remove the pipe from the poll set and continue the loop
3071 * since their might be data to consume.
3072 */
3073 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
3074 continue;
3075 default:
3076 ERR("Unknown action");
3077 break;
3078 }
03e43155
MD
3079 } else if (revents & (LPOLLERR | LPOLLHUP)) {
3080 DBG("Channel thread pipe hung up");
3081 /*
3082 * Remove the pipe from the poll set and continue the loop
3083 * since their might be data to consume.
3084 */
3085 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
3086 continue;
3087 } else {
3088 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
3089 goto end;
d8ef542d
MD
3090 }
3091
3092 /* Handle other stream */
3093 continue;
3094 }
3095
3096 rcu_read_lock();
3097 {
3098 uint64_t tmp_id = (uint64_t) pollfd;
3099
3100 lttng_ht_lookup(channel_ht, &tmp_id, &iter);
3101 }
3102 node = lttng_ht_iter_get_node_u64(&iter);
3103 assert(node);
3104
3105 chan = caa_container_of(node, struct lttng_consumer_channel,
3106 wait_fd_node);
3107
3108 /* Check for error event */
3109 if (revents & (LPOLLERR | LPOLLHUP)) {
3110 DBG("Channel fd %d is hup|err.", pollfd);
3111
3112 lttng_poll_del(&events, chan->wait_fd);
3113 ret = lttng_ht_del(channel_ht, &iter);
3114 assert(ret == 0);
b4a650f3
DG
3115
3116 /*
3117 * This will close the wait fd for each stream associated to
3118 * this channel AND monitored by the data/metadata thread thus
3119 * will be clean by the right thread.
3120 */
d8ef542d 3121 consumer_close_channel_streams(chan);
f2ad556d
MD
3122
3123 /* Release our own refcount */
3124 if (!uatomic_sub_return(&chan->refcount, 1)
3125 && !uatomic_read(&chan->nb_init_stream_left)) {
3126 consumer_del_channel(chan);
3127 }
03e43155
MD
3128 } else {
3129 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
3130 rcu_read_unlock();
3131 goto end;
d8ef542d
MD
3132 }
3133
3134 /* Release RCU lock for the channel looked up */
3135 rcu_read_unlock();
3136 }
3137 }
3138
1fc79fb4
MD
3139 /* All is OK */
3140 err = 0;
d8ef542d
MD
3141end:
3142 lttng_poll_clean(&events);
3143end_poll:
3144 destroy_channel_ht(channel_ht);
3145end_ht:
2d57de81 3146error_testpoint:
d8ef542d 3147 DBG("Channel poll thread exiting");
1fc79fb4
MD
3148 if (err) {
3149 health_error();
3150 ERR("Health error occurred in %s", __func__);
3151 }
3152 health_unregister(health_consumerd);
d8ef542d
MD
3153 rcu_unregister_thread();
3154 return NULL;
3155}
3156
331744e3
JD
3157static int set_metadata_socket(struct lttng_consumer_local_data *ctx,
3158 struct pollfd *sockpoll, int client_socket)
3159{
3160 int ret;
3161
3162 assert(ctx);
3163 assert(sockpoll);
3164
84382d49
MD
3165 ret = lttng_consumer_poll_socket(sockpoll);
3166 if (ret) {
331744e3
JD
3167 goto error;
3168 }
3169 DBG("Metadata connection on client_socket");
3170
3171 /* Blocking call, waiting for transmission */
3172 ctx->consumer_metadata_socket = lttcomm_accept_unix_sock(client_socket);
3173 if (ctx->consumer_metadata_socket < 0) {
3174 WARN("On accept metadata");
3175 ret = -1;
3176 goto error;
3177 }
3178 ret = 0;
3179
3180error:
3181 return ret;
3182}
3183
3bd1e081
MD
3184/*
3185 * This thread listens on the consumerd socket and receives the file
3186 * descriptors from the session daemon.
3187 */
7d980def 3188void *consumer_thread_sessiond_poll(void *data)
3bd1e081 3189{
1fc79fb4 3190 int sock = -1, client_socket, ret, err = -1;
3bd1e081
MD
3191 /*
3192 * structure to poll for incoming data on communication socket avoids
3193 * making blocking sockets.
3194 */
3195 struct pollfd consumer_sockpoll[2];
3196 struct lttng_consumer_local_data *ctx = data;
3197
e7b994a3
DG
3198 rcu_register_thread();
3199
1fc79fb4
MD
3200 health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_SESSIOND);
3201
2d57de81
MD
3202 if (testpoint(consumerd_thread_sessiond)) {
3203 goto error_testpoint;
3204 }
3205
9ce5646a
MD
3206 health_code_update();
3207
3bd1e081
MD
3208 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
3209 unlink(ctx->consumer_command_sock_path);
3210 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
3211 if (client_socket < 0) {
3212 ERR("Cannot create command socket");
3213 goto end;
3214 }
3215
3216 ret = lttcomm_listen_unix_sock(client_socket);
3217 if (ret < 0) {
3218 goto end;
3219 }
3220
32258573 3221 DBG("Sending ready command to lttng-sessiond");
f73fabfd 3222 ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY);
3bd1e081
MD
3223 /* return < 0 on error, but == 0 is not fatal */
3224 if (ret < 0) {
32258573 3225 ERR("Error sending ready command to lttng-sessiond");
3bd1e081
MD
3226 goto end;
3227 }
3228
3bd1e081
MD
3229 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3230 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
3231 consumer_sockpoll[0].events = POLLIN | POLLPRI;
3232 consumer_sockpoll[1].fd = client_socket;
3233 consumer_sockpoll[1].events = POLLIN | POLLPRI;
3234
84382d49
MD
3235 ret = lttng_consumer_poll_socket(consumer_sockpoll);
3236 if (ret) {
3237 if (ret > 0) {
3238 /* should exit */
3239 err = 0;
3240 }
3bd1e081
MD
3241 goto end;
3242 }
3243 DBG("Connection on client_socket");
3244
3245 /* Blocking call, waiting for transmission */
3246 sock = lttcomm_accept_unix_sock(client_socket);
534d2592 3247 if (sock < 0) {
3bd1e081
MD
3248 WARN("On accept");
3249 goto end;
3250 }
3bd1e081 3251
331744e3
JD
3252 /*
3253 * Setup metadata socket which is the second socket connection on the
3254 * command unix socket.
3255 */
3256 ret = set_metadata_socket(ctx, consumer_sockpoll, client_socket);
84382d49
MD
3257 if (ret) {
3258 if (ret > 0) {
3259 /* should exit */
3260 err = 0;
3261 }
331744e3
JD
3262 goto end;
3263 }
3264
d96f09c6
DG
3265 /* This socket is not useful anymore. */
3266 ret = close(client_socket);
3267 if (ret < 0) {
3268 PERROR("close client_socket");
3269 }
3270 client_socket = -1;
3271
3bd1e081
MD
3272 /* update the polling structure to poll on the established socket */
3273 consumer_sockpoll[1].fd = sock;
3274 consumer_sockpoll[1].events = POLLIN | POLLPRI;
3275
3276 while (1) {
9ce5646a
MD
3277 health_code_update();
3278
3279 health_poll_entry();
3280 ret = lttng_consumer_poll_socket(consumer_sockpoll);
3281 health_poll_exit();
84382d49
MD
3282 if (ret) {
3283 if (ret > 0) {
3284 /* should exit */
3285 err = 0;
3286 }
3bd1e081
MD
3287 goto end;
3288 }
3289 DBG("Incoming command on sock");
3290 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
4cbc1a04
DG
3291 if (ret <= 0) {
3292 /*
3293 * This could simply be a session daemon quitting. Don't output
3294 * ERR() here.
3295 */
3296 DBG("Communication interrupted on command socket");
41ba6035 3297 err = 0;
3bd1e081
MD
3298 goto end;
3299 }
10211f5c 3300 if (CMM_LOAD_SHARED(consumer_quit)) {
3bd1e081 3301 DBG("consumer_thread_receive_fds received quit from signal");
1fc79fb4 3302 err = 0; /* All is OK */
3bd1e081
MD
3303 goto end;
3304 }
a1ed855a 3305 DBG("Received command on sock");
3bd1e081 3306 }
1fc79fb4
MD
3307 /* All is OK */
3308 err = 0;
3309
3bd1e081 3310end:
ffe60014 3311 DBG("Consumer thread sessiond poll exiting");
3bd1e081 3312
d88aee68
DG
3313 /*
3314 * Close metadata streams since the producer is the session daemon which
3315 * just died.
3316 *
3317 * NOTE: for now, this only applies to the UST tracer.
3318 */
6d574024 3319 lttng_consumer_close_all_metadata();
d88aee68 3320
3bd1e081
MD
3321 /*
3322 * when all fds have hung up, the polling thread
3323 * can exit cleanly
3324 */
10211f5c 3325 CMM_STORE_SHARED(consumer_quit, 1);
3bd1e081 3326
04fdd819 3327 /*
c869f647 3328 * Notify the data poll thread to poll back again and test the
8994307f 3329 * consumer_quit state that we just set so to quit gracefully.
04fdd819 3330 */
acdb9057 3331 notify_thread_lttng_pipe(ctx->consumer_data_pipe);
c869f647 3332
a0cbdd2e 3333 notify_channel_pipe(ctx, NULL, -1, CONSUMER_CHANNEL_QUIT);
d8ef542d 3334
5c635c72
MD
3335 notify_health_quit_pipe(health_quit_pipe);
3336
d96f09c6
DG
3337 /* Cleaning up possibly open sockets. */
3338 if (sock >= 0) {
3339 ret = close(sock);
3340 if (ret < 0) {
3341 PERROR("close sock sessiond poll");
3342 }
3343 }
3344 if (client_socket >= 0) {
38476d24 3345 ret = close(client_socket);
d96f09c6
DG
3346 if (ret < 0) {
3347 PERROR("close client_socket sessiond poll");
3348 }
3349 }
3350
2d57de81 3351error_testpoint:
1fc79fb4
MD
3352 if (err) {
3353 health_error();
3354 ERR("Health error occurred in %s", __func__);
3355 }
3356 health_unregister(health_consumerd);
3357
e7b994a3 3358 rcu_unregister_thread();
3bd1e081
MD
3359 return NULL;
3360}
d41f73b7 3361
503fefca
JG
3362static int post_consume(struct lttng_consumer_stream *stream,
3363 const struct stream_subbuffer *subbuffer,
3364 struct lttng_consumer_local_data *ctx)
f96af312 3365{
503fefca 3366 size_t i;
f96af312 3367 int ret = 0;
503fefca
JG
3368 const size_t count = lttng_dynamic_array_get_count(
3369 &stream->read_subbuffer_ops.post_consume_cbs);
f96af312 3370
503fefca
JG
3371 for (i = 0; i < count; i++) {
3372 const post_consume_cb op = *(post_consume_cb *) lttng_dynamic_array_get_element(
3373 &stream->read_subbuffer_ops.post_consume_cbs,
3374 i);
3375
3376 ret = op(stream, subbuffer, ctx);
3377 if (ret) {
3378 goto end;
f96af312 3379 }
f96af312 3380 }
f96af312
JG
3381end:
3382 return ret;
3383}
3384
4078b776 3385ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
6f9449c2
JG
3386 struct lttng_consumer_local_data *ctx,
3387 bool locked_by_caller)
d41f73b7 3388{
12bddd1d 3389 ssize_t ret, written_bytes = 0;
23d56598 3390 int rotation_ret;
6f9449c2 3391 struct stream_subbuffer subbuffer = {};
823a3926 3392 enum get_next_subbuffer_status get_next_status;
74251bb8 3393
6f9449c2
JG
3394 if (!locked_by_caller) {
3395 stream->read_subbuffer_ops.lock(stream);
a48ac75a
JR
3396 } else {
3397 stream->read_subbuffer_ops.assert_locked(stream);
6f9449c2
JG
3398 }
3399
3400 if (stream->read_subbuffer_ops.on_wake_up) {
3401 ret = stream->read_subbuffer_ops.on_wake_up(stream);
3402 if (ret) {
3403 goto end;
3404 }
94d49140 3405 }
74251bb8 3406
23d56598
JG
3407 /*
3408 * If the stream was flagged to be ready for rotation before we extract
3409 * the next packet, rotate it now.
3410 */
3411 if (stream->rotate_ready) {
3412 DBG("Rotate stream before consuming data");
3413 ret = lttng_consumer_rotate_stream(ctx, stream);
3414 if (ret < 0) {
3415 ERR("Stream rotation error before consuming data");
3416 goto end;
3417 }
3418 }
3419
823a3926
JG
3420 get_next_status = stream->read_subbuffer_ops.get_next_subbuffer(
3421 stream, &subbuffer);
3422 switch (get_next_status) {
3423 case GET_NEXT_SUBBUFFER_STATUS_OK:
3424 break;
3425 case GET_NEXT_SUBBUFFER_STATUS_NO_DATA:
3426 /* Not an error. */
3427 ret = 0;
3428 goto sleep_stream;
3429 case GET_NEXT_SUBBUFFER_STATUS_ERROR:
3430 ret = -1;
6f9449c2 3431 goto end;
823a3926
JG
3432 default:
3433 abort();
d41f73b7 3434 }
74251bb8 3435
6f9449c2
JG
3436 ret = stream->read_subbuffer_ops.pre_consume_subbuffer(
3437 stream, &subbuffer);
3438 if (ret) {
3439 goto error_put_subbuf;
3440 }
3441
3442 written_bytes = stream->read_subbuffer_ops.consume_subbuffer(
3443 ctx, stream, &subbuffer);
514775d9
FD
3444 if (written_bytes <= 0) {
3445 ERR("Error consuming subbuffer: (%zd)", written_bytes);
3446 ret = (int) written_bytes;
3447 goto error_put_subbuf;
6f9449c2
JG
3448 }
3449
3450 ret = stream->read_subbuffer_ops.put_next_subbuffer(stream, &subbuffer);
3451 if (ret) {
23d56598
JG
3452 goto end;
3453 }
3454
503fefca
JG
3455 ret = post_consume(stream, &subbuffer, ctx);
3456 if (ret) {
3457 goto end;
6f9449c2
JG
3458 }
3459
23d56598
JG
3460 /*
3461 * After extracting the packet, we check if the stream is now ready to
3462 * be rotated and perform the action immediately.
3463 *
3464 * Don't overwrite `ret` as callers expect the number of bytes
3465 * consumed to be returned on success.
3466 */
3467 rotation_ret = lttng_consumer_stream_is_rotate_ready(stream);
3468 if (rotation_ret == 1) {
3469 rotation_ret = lttng_consumer_rotate_stream(ctx, stream);
3470 if (rotation_ret < 0) {
3471 ret = rotation_ret;
3472 ERR("Stream rotation error after consuming data");
3473 goto end;
3474 }
503fefca 3475
23d56598
JG
3476 } else if (rotation_ret < 0) {
3477 ret = rotation_ret;
3478 ERR("Failed to check if stream was ready to rotate after consuming data");
3479 goto end;
3480 }
3481
82e72193 3482sleep_stream:
6f9449c2
JG
3483 if (stream->read_subbuffer_ops.on_sleep) {
3484 stream->read_subbuffer_ops.on_sleep(stream, ctx);
3485 }
3486
3487 ret = written_bytes;
23d56598 3488end:
6f9449c2
JG
3489 if (!locked_by_caller) {
3490 stream->read_subbuffer_ops.unlock(stream);
94d49140 3491 }
6f9449c2 3492
74251bb8 3493 return ret;
6f9449c2
JG
3494error_put_subbuf:
3495 (void) stream->read_subbuffer_ops.put_next_subbuffer(stream, &subbuffer);
3496 goto end;
d41f73b7
MD
3497}
3498
3499int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
3500{
fa29bfbf 3501 switch (the_consumer_data.type) {
d41f73b7
MD
3502 case LTTNG_CONSUMER_KERNEL:
3503 return lttng_kconsumer_on_recv_stream(stream);
7753dea8
MD
3504 case LTTNG_CONSUMER32_UST:
3505 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
3506 return lttng_ustconsumer_on_recv_stream(stream);
3507 default:
3508 ERR("Unknown consumer_data type");
3509 assert(0);
3510 return -ENOSYS;
3511 }
3512}
e4421fec
DG
3513
3514/*
3515 * Allocate and set consumer data hash tables.
3516 */
282dadbc 3517int lttng_consumer_init(void)
e4421fec 3518{
fa29bfbf
SM
3519 the_consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3520 if (!the_consumer_data.channel_ht) {
282dadbc
MD
3521 goto error;
3522 }
3523
fa29bfbf 3524 the_consumer_data.channels_by_session_id_ht =
5c3892a6 3525 lttng_ht_new(0, LTTNG_HT_TYPE_U64);
fa29bfbf 3526 if (!the_consumer_data.channels_by_session_id_ht) {
5c3892a6
JG
3527 goto error;
3528 }
3529
fa29bfbf
SM
3530 the_consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3531 if (!the_consumer_data.relayd_ht) {
282dadbc
MD
3532 goto error;
3533 }
3534
fa29bfbf
SM
3535 the_consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3536 if (!the_consumer_data.stream_list_ht) {
282dadbc
MD
3537 goto error;
3538 }
3539
fa29bfbf
SM
3540 the_consumer_data.stream_per_chan_id_ht =
3541 lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3542 if (!the_consumer_data.stream_per_chan_id_ht) {
282dadbc
MD
3543 goto error;
3544 }
3545
3546 data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3547 if (!data_ht) {
3548 goto error;
3549 }
3550
3551 metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3552 if (!metadata_ht) {
3553 goto error;
3554 }
3555
fa29bfbf
SM
3556 the_consumer_data.chunk_registry = lttng_trace_chunk_registry_create();
3557 if (!the_consumer_data.chunk_registry) {
28cc88f3
JG
3558 goto error;
3559 }
3560
282dadbc
MD
3561 return 0;
3562
3563error:
3564 return -1;
e4421fec 3565}
7735ef9e
DG
3566
3567/*
3568 * Process the ADD_RELAYD command receive by a consumer.
3569 *
3570 * This will create a relayd socket pair and add it to the relayd hash table.
3571 * The caller MUST acquire a RCU read side lock before calling it.
3572 */
fe7bf564
JR
3573void consumer_add_relayd_socket(uint64_t net_seq_idx,
3574 int sock_type,
3575 struct lttng_consumer_local_data *ctx,
3576 int sock,
6151a90f 3577 struct pollfd *consumer_sockpoll,
fe7bf564
JR
3578 uint64_t sessiond_id,
3579 uint64_t relayd_session_id,
3580 uint32_t relayd_version_major,
3581 uint32_t relayd_version_minor,
3582 enum lttcomm_sock_proto relayd_socket_protocol)
7735ef9e 3583{
cd2b09ed 3584 int fd = -1, ret = -1, relayd_created = 0;
0c759fc9 3585 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
d4298c99 3586 struct consumer_relayd_sock_pair *relayd = NULL;
7735ef9e 3587
6151a90f 3588 assert(ctx);
6151a90f 3589
da009f2c 3590 DBG("Consumer adding relayd socket (idx: %" PRIu64 ")", net_seq_idx);
7735ef9e
DG
3591
3592 /* Get relayd reference if exists. */
3593 relayd = consumer_find_relayd(net_seq_idx);
3594 if (relayd == NULL) {
da009f2c 3595 assert(sock_type == LTTNG_STREAM_CONTROL);
7735ef9e
DG
3596 /* Not found. Allocate one. */
3597 relayd = consumer_allocate_relayd_sock_pair(net_seq_idx);
3598 if (relayd == NULL) {
618a6a28
MD
3599 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3600 goto error;
0d08d75e 3601 } else {
30319bcb 3602 relayd->sessiond_session_id = sessiond_id;
0d08d75e 3603 relayd_created = 1;
7735ef9e 3604 }
0d08d75e
DG
3605
3606 /*
3607 * This code path MUST continue to the consumer send status message to
3608 * we can notify the session daemon and continue our work without
3609 * killing everything.
3610 */
da009f2c
MD
3611 } else {
3612 /*
3613 * relayd key should never be found for control socket.
3614 */
3615 assert(sock_type != LTTNG_STREAM_CONTROL);
0d08d75e
DG
3616 }
3617
3618 /* First send a status message before receiving the fds. */
0c759fc9 3619 ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS);
618a6a28 3620 if (ret < 0) {
0d08d75e 3621 /* Somehow, the session daemon is not responding anymore. */
618a6a28
MD
3622 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3623 goto error_nosignal;
7735ef9e
DG
3624 }
3625
3626 /* Poll on consumer socket. */
84382d49
MD
3627 ret = lttng_consumer_poll_socket(consumer_sockpoll);
3628 if (ret) {
3629 /* Needing to exit in the middle of a command: error. */
0d08d75e 3630 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
618a6a28 3631 goto error_nosignal;
7735ef9e
DG
3632 }
3633
3634 /* Get relayd socket from session daemon */
3635 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
3636 if (ret != sizeof(fd)) {
4028eeb9 3637 fd = -1; /* Just in case it gets set with an invalid value. */
0d08d75e
DG
3638
3639 /*
3640 * Failing to receive FDs might indicate a major problem such as
3641 * reaching a fd limit during the receive where the kernel returns a
3642 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3643 * don't take any chances and stop everything.
3644 *
3645 * XXX: Feature request #558 will fix that and avoid this possible
3646 * issue when reaching the fd limit.
3647 */
3648 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
618a6a28 3649 ret_code = LTTCOMM_CONSUMERD_ERROR_RECV_FD;
f50f23d9
DG
3650 goto error;
3651 }
3652
7735ef9e
DG
3653 /* Copy socket information and received FD */
3654 switch (sock_type) {
3655 case LTTNG_STREAM_CONTROL:
3656 /* Copy received lttcomm socket */
fe7bf564
JR
3657 ret = lttcomm_populate_sock_from_open_socket(
3658 &relayd->control_sock.sock, fd,
3659 relayd_socket_protocol);
7735ef9e 3660
6151a90f 3661 /* Assign version values. */
fe7bf564
JR
3662 relayd->control_sock.major = relayd_version_major;
3663 relayd->control_sock.minor = relayd_version_minor;
c5b6f4f0 3664
d3e2ba59 3665 relayd->relayd_session_id = relayd_session_id;
c5b6f4f0 3666
7735ef9e
DG
3667 break;
3668 case LTTNG_STREAM_DATA:
3669 /* Copy received lttcomm socket */
fe7bf564
JR
3670 ret = lttcomm_populate_sock_from_open_socket(
3671 &relayd->data_sock.sock, fd,
3672 relayd_socket_protocol);
6151a90f 3673 /* Assign version values. */
fe7bf564
JR
3674 relayd->data_sock.major = relayd_version_major;
3675 relayd->data_sock.minor = relayd_version_minor;
7735ef9e
DG
3676 break;
3677 default:
3678 ERR("Unknown relayd socket type (%d)", sock_type);
618a6a28 3679 ret_code = LTTCOMM_CONSUMERD_FATAL;
7735ef9e
DG
3680 goto error;
3681 }
3682
fe7bf564
JR
3683 if (ret < 0) {
3684 ret_code = LTTCOMM_CONSUMERD_FATAL;
3685 goto error;
3686 }
3687
d88aee68 3688 DBG("Consumer %s socket created successfully with net idx %" PRIu64 " (fd: %d)",
7735ef9e
DG
3689 sock_type == LTTNG_STREAM_CONTROL ? "control" : "data",
3690 relayd->net_seq_idx, fd);
39d9954c
FD
3691 /*
3692 * We gave the ownership of the fd to the relayd structure. Set the
3693 * fd to -1 so we don't call close() on it in the error path below.
3694 */
3695 fd = -1;
7735ef9e 3696
618a6a28
MD
3697 /* We successfully added the socket. Send status back. */
3698 ret = consumer_send_status_msg(sock, ret_code);
3699 if (ret < 0) {
3700 /* Somehow, the session daemon is not responding anymore. */
3701 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3702 goto error_nosignal;
3703 }
3704
7735ef9e
DG
3705 /*
3706 * Add relayd socket pair to consumer data hashtable. If object already
3707 * exists or on error, the function gracefully returns.
3708 */
9276e5c8 3709 relayd->ctx = ctx;
d09e1200 3710 add_relayd(relayd);
7735ef9e
DG
3711
3712 /* All good! */
2527bf85 3713 return;
7735ef9e
DG
3714
3715error:
618a6a28
MD
3716 if (consumer_send_status_msg(sock, ret_code) < 0) {
3717 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3718 }
3719
3720error_nosignal:
4028eeb9
DG
3721 /* Close received socket if valid. */
3722 if (fd >= 0) {
3723 if (close(fd)) {
3724 PERROR("close received socket");
3725 }
3726 }
cd2b09ed
DG
3727
3728 if (relayd_created) {
cd2b09ed
DG
3729 free(relayd);
3730 }
7735ef9e 3731}
ca22feea 3732
f7079f67
DG
3733/*
3734 * Search for a relayd associated to the session id and return the reference.
3735 *
3736 * A rcu read side lock MUST be acquire before calling this function and locked
3737 * until the relayd object is no longer necessary.
3738 */
3739static struct consumer_relayd_sock_pair *find_relayd_by_session_id(uint64_t id)
3740{
3741 struct lttng_ht_iter iter;
f7079f67 3742 struct consumer_relayd_sock_pair *relayd = NULL;
f7079f67
DG
3743
3744 /* Iterate over all relayd since they are indexed by net_seq_idx. */
fa29bfbf
SM
3745 cds_lfht_for_each_entry(the_consumer_data.relayd_ht->ht, &iter.iter,
3746 relayd, node.node) {
18261bd1
DG
3747 /*
3748 * Check by sessiond id which is unique here where the relayd session
3749 * id might not be when having multiple relayd.
3750 */
3751 if (relayd->sessiond_session_id == id) {
f7079f67 3752 /* Found the relayd. There can be only one per id. */
18261bd1 3753 goto found;
f7079f67
DG
3754 }
3755 }
3756
18261bd1
DG
3757 return NULL;
3758
3759found:
f7079f67
DG
3760 return relayd;
3761}
3762
ca22feea
DG
3763/*
3764 * Check if for a given session id there is still data needed to be extract
3765 * from the buffers.
3766 *
6d805429 3767 * Return 1 if data is pending or else 0 meaning ready to be read.
ca22feea 3768 */
6d805429 3769int consumer_data_pending(uint64_t id)
ca22feea
DG
3770{
3771 int ret;
3772 struct lttng_ht_iter iter;
3773 struct lttng_ht *ht;
3774 struct lttng_consumer_stream *stream;
f7079f67 3775 struct consumer_relayd_sock_pair *relayd = NULL;
6d805429 3776 int (*data_pending)(struct lttng_consumer_stream *);
ca22feea 3777
6d805429 3778 DBG("Consumer data pending command on session id %" PRIu64, id);
ca22feea 3779
6f6eda74 3780 rcu_read_lock();
fa29bfbf 3781 pthread_mutex_lock(&the_consumer_data.lock);
ca22feea 3782
fa29bfbf 3783 switch (the_consumer_data.type) {
ca22feea 3784 case LTTNG_CONSUMER_KERNEL:
6d805429 3785 data_pending = lttng_kconsumer_data_pending;
ca22feea
DG
3786 break;
3787 case LTTNG_CONSUMER32_UST:
3788 case LTTNG_CONSUMER64_UST:
6d805429 3789 data_pending = lttng_ustconsumer_data_pending;
ca22feea
DG
3790 break;
3791 default:
3792 ERR("Unknown consumer data type");
3793 assert(0);
3794 }
3795
3796 /* Ease our life a bit */
fa29bfbf 3797 ht = the_consumer_data.stream_list_ht;
ca22feea 3798
c8f59ee5 3799 cds_lfht_for_each_entry_duplicate(ht->ht,
d88aee68
DG
3800 ht->hash_fct(&id, lttng_ht_seed),
3801 ht->match_fct, &id,
ca22feea 3802 &iter.iter, stream, node_session_id.node) {
bb586a6e 3803 pthread_mutex_lock(&stream->lock);
ca22feea 3804
4e9a4686
DG
3805 /*
3806 * A removed node from the hash table indicates that the stream has
3807 * been deleted thus having a guarantee that the buffers are closed
3808 * on the consumer side. However, data can still be transmitted
3809 * over the network so don't skip the relayd check.
3810 */
3811 ret = cds_lfht_is_node_deleted(&stream->node.node);
3812 if (!ret) {
3813 /* Check the stream if there is data in the buffers. */
6d805429
DG
3814 ret = data_pending(stream);
3815 if (ret == 1) {
4e9a4686 3816 pthread_mutex_unlock(&stream->lock);
f7079f67 3817 goto data_pending;
4e9a4686
DG
3818 }
3819 }
3820
d9f0c7c7
JR
3821 pthread_mutex_unlock(&stream->lock);
3822 }
3823
3824 relayd = find_relayd_by_session_id(id);
3825 if (relayd) {
3826 unsigned int is_data_inflight = 0;
3827
3828 /* Send init command for data pending. */
3829 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3830 ret = relayd_begin_data_pending(&relayd->control_sock,
3831 relayd->relayd_session_id);
3832 if (ret < 0) {
3833 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3834 /* Communication error thus the relayd so no data pending. */
3835 goto data_not_pending;
3836 }
3837
3838 cds_lfht_for_each_entry_duplicate(ht->ht,
3839 ht->hash_fct(&id, lttng_ht_seed),
3840 ht->match_fct, &id,
3841 &iter.iter, stream, node_session_id.node) {
c8f59ee5 3842 if (stream->metadata_flag) {
ad7051c0
DG
3843 ret = relayd_quiescent_control(&relayd->control_sock,
3844 stream->relayd_stream_id);
c8f59ee5 3845 } else {
6d805429 3846 ret = relayd_data_pending(&relayd->control_sock,
39df6d9f
DG
3847 stream->relayd_stream_id,
3848 stream->next_net_seq_num - 1);
c8f59ee5 3849 }
d9f0c7c7
JR
3850
3851 if (ret == 1) {
3852 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3853 goto data_pending;
3854 } else if (ret < 0) {
9276e5c8
JR
3855 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx);
3856 lttng_consumer_cleanup_relayd(relayd);
3857 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
9276e5c8
JR
3858 goto data_not_pending;
3859 }
c8f59ee5 3860 }
f7079f67 3861
d9f0c7c7 3862 /* Send end command for data pending. */
f7079f67
DG
3863 ret = relayd_end_data_pending(&relayd->control_sock,
3864 relayd->relayd_session_id, &is_data_inflight);
3865 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
bdd88757 3866 if (ret < 0) {
9276e5c8
JR
3867 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64".", relayd->net_seq_idx);
3868 lttng_consumer_cleanup_relayd(relayd);
f7079f67
DG
3869 goto data_not_pending;
3870 }
bdd88757
DG
3871 if (is_data_inflight) {
3872 goto data_pending;
3873 }
f7079f67
DG
3874 }
3875
ca22feea 3876 /*
f7079f67
DG
3877 * Finding _no_ node in the hash table and no inflight data means that the
3878 * stream(s) have been removed thus data is guaranteed to be available for
3879 * analysis from the trace files.
ca22feea
DG
3880 */
3881
f7079f67 3882data_not_pending:
ca22feea 3883 /* Data is available to be read by a viewer. */
fa29bfbf 3884 pthread_mutex_unlock(&the_consumer_data.lock);
c8f59ee5 3885 rcu_read_unlock();
6d805429 3886 return 0;
ca22feea 3887
f7079f67 3888data_pending:
ca22feea 3889 /* Data is still being extracted from buffers. */
fa29bfbf 3890 pthread_mutex_unlock(&the_consumer_data.lock);
c8f59ee5 3891 rcu_read_unlock();
6d805429 3892 return 1;
ca22feea 3893}
f50f23d9
DG
3894
3895/*
3896 * Send a ret code status message to the sessiond daemon.
3897 *
3898 * Return the sendmsg() return value.
3899 */
3900int consumer_send_status_msg(int sock, int ret_code)
3901{
3902 struct lttcomm_consumer_status_msg msg;
3903
53efb85a 3904 memset(&msg, 0, sizeof(msg));
f50f23d9
DG
3905 msg.ret_code = ret_code;
3906
3907 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3908}
ffe60014
DG
3909
3910/*
3911 * Send a channel status message to the sessiond daemon.
3912 *
3913 * Return the sendmsg() return value.
3914 */
3915int consumer_send_status_channel(int sock,
3916 struct lttng_consumer_channel *channel)
3917{
3918 struct lttcomm_consumer_status_channel msg;
3919
3920 assert(sock >= 0);
3921
53efb85a 3922 memset(&msg, 0, sizeof(msg));
ffe60014 3923 if (!channel) {
0c759fc9 3924 msg.ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL;
ffe60014 3925 } else {
0c759fc9 3926 msg.ret_code = LTTCOMM_CONSUMERD_SUCCESS;
ffe60014
DG
3927 msg.key = channel->key;
3928 msg.stream_count = channel->streams.count;
3929 }
3930
3931 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3932}
5c786ded 3933
d07ceecd
MD
3934unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos,
3935 unsigned long produced_pos, uint64_t nb_packets_per_stream,
3936 uint64_t max_sb_size)
5c786ded 3937{
d07ceecd 3938 unsigned long start_pos;
5c786ded 3939
d07ceecd
MD
3940 if (!nb_packets_per_stream) {
3941 return consumed_pos; /* Grab everything */
3942 }
3943 start_pos = produced_pos - offset_align_floor(produced_pos, max_sb_size);
3944 start_pos -= max_sb_size * nb_packets_per_stream;
3945 if ((long) (start_pos - consumed_pos) < 0) {
3946 return consumed_pos; /* Grab everything */
3947 }
3948 return start_pos;
5c786ded 3949}
a1ae2ea5 3950
c1dcb8bb
JG
3951/* Stream lock must be held by the caller. */
3952static int sample_stream_positions(struct lttng_consumer_stream *stream,
3953 unsigned long *produced, unsigned long *consumed)
3954{
3955 int ret;
3956
3957 ASSERT_LOCKED(stream->lock);
3958
3959 ret = lttng_consumer_sample_snapshot_positions(stream);
3960 if (ret < 0) {
3961 ERR("Failed to sample snapshot positions");
3962 goto end;
3963 }
3964
3965 ret = lttng_consumer_get_produced_snapshot(stream, produced);
3966 if (ret < 0) {
3967 ERR("Failed to sample produced position");
3968 goto end;
3969 }
3970
3971 ret = lttng_consumer_get_consumed_snapshot(stream, consumed);
3972 if (ret < 0) {
3973 ERR("Failed to sample consumed position");
3974 goto end;
3975 }
3976
3977end:
3978 return ret;
3979}
3980
b99a8d42
JD
3981/*
3982 * Sample the rotate position for all the streams of a channel. If a stream
3983 * is already at the rotate position (produced == consumed), we flag it as
3984 * ready for rotation. The rotation of ready streams occurs after we have
3985 * replied to the session daemon that we have finished sampling the positions.
92b7a7f8 3986 * Must be called with RCU read-side lock held to ensure existence of channel.
b99a8d42
JD
3987 *
3988 * Returns 0 on success, < 0 on error
3989 */
92b7a7f8 3990int lttng_consumer_rotate_channel(struct lttng_consumer_channel *channel,
d2956687 3991 uint64_t key, uint64_t relayd_id, uint32_t metadata,
b99a8d42
JD
3992 struct lttng_consumer_local_data *ctx)
3993{
3994 int ret;
b99a8d42
JD
3995 struct lttng_consumer_stream *stream;
3996 struct lttng_ht_iter iter;
fa29bfbf 3997 struct lttng_ht *ht = the_consumer_data.stream_per_chan_id_ht;
c35f9726
JG
3998 struct lttng_dynamic_array stream_rotation_positions;
3999 uint64_t next_chunk_id, stream_count = 0;
4000 enum lttng_trace_chunk_status chunk_status;
4001 const bool is_local_trace = relayd_id == -1ULL;
4002 struct consumer_relayd_sock_pair *relayd = NULL;
4003 bool rotating_to_new_chunk = true;
b32703d6
JG
4004 /* Array of `struct lttng_consumer_stream *` */
4005 struct lttng_dynamic_pointer_array streams_packet_to_open;
4006 size_t stream_idx;
b99a8d42
JD
4007
4008 DBG("Consumer sample rotate position for channel %" PRIu64, key);
4009
c35f9726
JG
4010 lttng_dynamic_array_init(&stream_rotation_positions,
4011 sizeof(struct relayd_stream_rotation_position), NULL);
b32703d6 4012 lttng_dynamic_pointer_array_init(&streams_packet_to_open, NULL);
c35f9726 4013
b99a8d42
JD
4014 rcu_read_lock();
4015
b99a8d42 4016 pthread_mutex_lock(&channel->lock);
c35f9726
JG
4017 assert(channel->trace_chunk);
4018 chunk_status = lttng_trace_chunk_get_id(channel->trace_chunk,
4019 &next_chunk_id);
4020 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
4021 ret = -1;
4022 goto end_unlock_channel;
4023 }
b99a8d42
JD
4024
4025 cds_lfht_for_each_entry_duplicate(ht->ht,
4026 ht->hash_fct(&channel->key, lttng_ht_seed),
4027 ht->match_fct, &channel->key, &iter.iter,
4028 stream, node_channel_id.node) {
a40a503f 4029 unsigned long produced_pos = 0, consumed_pos = 0;
b99a8d42
JD
4030
4031 health_code_update();
4032
4033 /*
4034 * Lock stream because we are about to change its state.
4035 */
4036 pthread_mutex_lock(&stream->lock);
4037
c35f9726
JG
4038 if (stream->trace_chunk == stream->chan->trace_chunk) {
4039 rotating_to_new_chunk = false;
4040 }
4041
a40a503f 4042 /*
c1dcb8bb 4043 * Do not flush a packet when rotating from a NULL trace
a9dde553 4044 * chunk. The stream has no means to output data, and the prior
c1dcb8bb
JG
4045 * rotation which rotated to NULL performed that side-effect
4046 * already. No new data can be produced when a stream has no
4047 * associated trace chunk (e.g. a stop followed by a rotate).
a40a503f 4048 */
a9dde553 4049 if (stream->trace_chunk) {
c1dcb8bb
JG
4050 bool flush_active;
4051
4052 if (stream->metadata_flag) {
4053 /*
4054 * Don't produce an empty metadata packet,
4055 * simply close the current one.
4056 *
4057 * Metadata is regenerated on every trace chunk
4058 * switch; there is no concern that no data was
4059 * produced.
4060 */
4061 flush_active = true;
4062 } else {
4063 /*
4064 * Only flush an empty packet if the "packet
4065 * open" could not be performed on transition
4066 * to a new trace chunk and no packets were
4067 * consumed within the chunk's lifetime.
4068 */
4069 if (stream->opened_packet_in_current_trace_chunk) {
4070 flush_active = true;
4071 } else {
4072 /*
4073 * Stream could have been full at the
4074 * time of rotation, but then have had
4075 * no activity at all.
4076 *
4077 * It is important to flush a packet
4078 * to prevent 0-length files from being
4079 * produced as most viewers choke on
4080 * them.
4081 *
4082 * Unfortunately viewers will not be
4083 * able to know that tracing was active
4084 * for this stream during this trace
4085 * chunk's lifetime.
4086 */
4087 ret = sample_stream_positions(stream, &produced_pos, &consumed_pos);
4088 if (ret) {
4089 goto end_unlock_stream;
4090 }
4091
4092 /*
4093 * Don't flush an empty packet if data
4094 * was produced; it will be consumed
4095 * before the rotation completes.
4096 */
4097 flush_active = produced_pos != consumed_pos;
4098 if (!flush_active) {
c1dcb8bb
JG
4099 const char *trace_chunk_name;
4100 uint64_t trace_chunk_id;
4101
4102 chunk_status = lttng_trace_chunk_get_name(
4103 stream->trace_chunk,
4104 &trace_chunk_name,
4105 NULL);
4106 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_NONE) {
4107 trace_chunk_name = "none";
4108 }
4109
4110 /*
4111 * Consumer trace chunks are
4112 * never anonymous.
4113 */
4114 chunk_status = lttng_trace_chunk_get_id(
4115 stream->trace_chunk,
4116 &trace_chunk_id);
4117 assert(chunk_status ==
4118 LTTNG_TRACE_CHUNK_STATUS_OK);
4119
4120 DBG("Unable to open packet for stream during trace chunk's lifetime. "
4121 "Flushing an empty packet to prevent an empty file from being created: "
4122 "stream id = %" PRIu64 ", trace chunk name = `%s`, trace chunk id = %" PRIu64,
4123 stream->key, trace_chunk_name, trace_chunk_id);
4124 }
4125 }
4126 }
4127
a9dde553 4128 /*
c1dcb8bb
JG
4129 * Close the current packet before sampling the
4130 * ring buffer positions.
a9dde553 4131 */
c1dcb8bb 4132 ret = consumer_stream_flush_buffer(stream, flush_active);
a9dde553
MD
4133 if (ret < 0) {
4134 ERR("Failed to flush stream %" PRIu64 " during channel rotation",
4135 stream->key);
4136 goto end_unlock_stream;
4137 }
b99a8d42
JD
4138 }
4139
a40a503f
MD
4140 ret = lttng_consumer_take_snapshot(stream);
4141 if (ret < 0 && ret != -ENODATA && ret != -EAGAIN) {
4142 ERR("Failed to sample snapshot position during channel rotation");
b99a8d42
JD
4143 goto end_unlock_stream;
4144 }
a40a503f
MD
4145 if (!ret) {
4146 ret = lttng_consumer_get_produced_snapshot(stream,
4147 &produced_pos);
4148 if (ret < 0) {
4149 ERR("Failed to sample produced position during channel rotation");
4150 goto end_unlock_stream;
4151 }
b99a8d42 4152
a40a503f
MD
4153 ret = lttng_consumer_get_consumed_snapshot(stream,
4154 &consumed_pos);
4155 if (ret < 0) {
4156 ERR("Failed to sample consumed position during channel rotation");
4157 goto end_unlock_stream;
4158 }
4159 }
4160 /*
4161 * Align produced position on the start-of-packet boundary of the first
4162 * packet going into the next trace chunk.
4163 */
4164 produced_pos = ALIGN_FLOOR(produced_pos, stream->max_sb_size);
4165 if (consumed_pos == produced_pos) {
f8528c7a
MD
4166 DBG("Set rotate ready for stream %" PRIu64 " produced = %lu consumed = %lu",
4167 stream->key, produced_pos, consumed_pos);
b99a8d42 4168 stream->rotate_ready = true;
f8528c7a
MD
4169 } else {
4170 DBG("Different consumed and produced positions "
4171 "for stream %" PRIu64 " produced = %lu consumed = %lu",
4172 stream->key, produced_pos, consumed_pos);
b99a8d42 4173 }
633d0182 4174 /*
a40a503f
MD
4175 * The rotation position is based on the packet_seq_num of the
4176 * packet following the last packet that was consumed for this
4177 * stream, incremented by the offset between produced and
4178 * consumed positions. This rotation position is a lower bound
4179 * (inclusive) at which the next trace chunk starts. Since it
4180 * is a lower bound, it is OK if the packet_seq_num does not
4181 * correspond exactly to the same packet identified by the
4182 * consumed_pos, which can happen in overwrite mode.
633d0182 4183 */
a40a503f
MD
4184 if (stream->sequence_number_unavailable) {
4185 /*
4186 * Rotation should never be performed on a session which
4187 * interacts with a pre-2.8 lttng-modules, which does
4188 * not implement packet sequence number.
4189 */
4190 ERR("Failure to rotate stream %" PRIu64 ": sequence number unavailable",
b99a8d42 4191 stream->key);
a40a503f 4192 ret = -1;
b99a8d42
JD
4193 goto end_unlock_stream;
4194 }
a40a503f
MD
4195 stream->rotate_position = stream->last_sequence_number + 1 +
4196 ((produced_pos - consumed_pos) / stream->max_sb_size);
f8528c7a
MD
4197 DBG("Set rotation position for stream %" PRIu64 " at position %" PRIu64,
4198 stream->key, stream->rotate_position);
b99a8d42 4199
c35f9726 4200 if (!is_local_trace) {
633d0182
JG
4201 /*
4202 * The relay daemon control protocol expects a rotation
4203 * position as "the sequence number of the first packet
a40a503f 4204 * _after_ the current trace chunk".
633d0182 4205 */
c35f9726
JG
4206 const struct relayd_stream_rotation_position position = {
4207 .stream_id = stream->relayd_stream_id,
a40a503f 4208 .rotate_at_seq_num = stream->rotate_position,
c35f9726
JG
4209 };
4210
4211 ret = lttng_dynamic_array_add_element(
4212 &stream_rotation_positions,
4213 &position);
4214 if (ret) {
4215 ERR("Failed to allocate stream rotation position");
4216 goto end_unlock_stream;
4217 }
4218 stream_count++;
4219 }
f96af312
JG
4220
4221 stream->opened_packet_in_current_trace_chunk = false;
4222
4223 if (rotating_to_new_chunk && !stream->metadata_flag) {
4224 /*
4225 * Attempt to flush an empty packet as close to the
4226 * rotation point as possible. In the event where a
4227 * stream remains inactive after the rotation point,
4228 * this ensures that the new trace chunk has a
4229 * beginning timestamp set at the begining of the
4230 * trace chunk instead of only creating an empty
4231 * packet when the trace chunk is stopped.
4232 *
4233 * This indicates to the viewers that the stream
4234 * was being recorded, but more importantly it
4235 * allows viewers to determine a useable trace
4236 * intersection.
4237 *
4238 * This presents a problem in the case where the
4239 * ring-buffer is completely full.
4240 *
4241 * Consider the following scenario:
4242 * - The consumption of data is slow (slow network,
4243 * for instance),
4244 * - The ring buffer is full,
4245 * - A rotation is initiated,
4246 * - The flush below does nothing (no space left to
4247 * open a new packet),
4248 * - The other streams rotate very soon, and new
4249 * data is produced in the new chunk,
4250 * - This stream completes its rotation long after the
4251 * rotation was initiated
4252 * - The session is stopped before any event can be
4253 * produced in this stream's buffers.
4254 *
4255 * The resulting trace chunk will have a single packet
4256 * temporaly at the end of the trace chunk for this
4257 * stream making the stream intersection more narrow
4258 * than it should be.
4259 *
4260 * To work-around this, an empty flush is performed
4261 * after the first consumption of a packet during a
4262 * rotation if open_packet fails. The idea is that
4263 * consuming a packet frees enough space to switch
4264 * packets in this scenario and allows the tracer to
4265 * "stamp" the beginning of the new trace chunk at the
4266 * earliest possible point.
b32703d6
JG
4267 *
4268 * The packet open is performed after the channel
4269 * rotation to ensure that no attempt to open a packet
4270 * is performed in a stream that has no active trace
4271 * chunk.
f96af312 4272 */
b32703d6
JG
4273 ret = lttng_dynamic_pointer_array_add_pointer(
4274 &streams_packet_to_open, stream);
4275 if (ret) {
4276 PERROR("Failed to add a stream pointer to array of streams in which to open a packet");
f96af312
JG
4277 ret = -1;
4278 goto end_unlock_stream;
f96af312
JG
4279 }
4280 }
4281
b99a8d42
JD
4282 pthread_mutex_unlock(&stream->lock);
4283 }
c35f9726 4284 stream = NULL;
b99a8d42 4285
b32703d6
JG
4286 if (!is_local_trace) {
4287 relayd = consumer_find_relayd(relayd_id);
4288 if (!relayd) {
4289 ERR("Failed to find relayd %" PRIu64, relayd_id);
4290 ret = -1;
4291 goto end_unlock_channel;
4292 }
c35f9726 4293
b32703d6
JG
4294 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
4295 ret = relayd_rotate_streams(&relayd->control_sock, stream_count,
4296 rotating_to_new_chunk ? &next_chunk_id : NULL,
4297 (const struct relayd_stream_rotation_position *)
4298 stream_rotation_positions.buffer
4299 .data);
4300 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
4301 if (ret < 0) {
4302 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64,
4303 relayd->net_seq_idx);
4304 lttng_consumer_cleanup_relayd(relayd);
4305 goto end_unlock_channel;
4306 }
c35f9726
JG
4307 }
4308
b32703d6
JG
4309 for (stream_idx = 0;
4310 stream_idx < lttng_dynamic_pointer_array_get_count(
4311 &streams_packet_to_open);
4312 stream_idx++) {
4313 enum consumer_stream_open_packet_status status;
4314
4315 stream = lttng_dynamic_pointer_array_get_pointer(
4316 &streams_packet_to_open, stream_idx);
4317
4318 pthread_mutex_lock(&stream->lock);
4319 status = consumer_stream_open_packet(stream);
4320 pthread_mutex_unlock(&stream->lock);
4321 switch (status) {
4322 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED:
4323 DBG("Opened a packet after a rotation: stream id = %" PRIu64
4324 ", channel name = %s, session id = %" PRIu64,
4325 stream->key, stream->chan->name,
4326 stream->chan->session_id);
4327 break;
4328 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE:
4329 /*
4330 * Can't open a packet as there is no space left
4331 * in the buffer. A new packet will be opened
4332 * once one has been consumed.
4333 */
4334 DBG("No space left to open a packet after a rotation: stream id = %" PRIu64
4335 ", channel name = %s, session id = %" PRIu64,
4336 stream->key, stream->chan->name,
4337 stream->chan->session_id);
4338 break;
4339 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR:
4340 /* Logged by callee. */
4341 ret = -1;
7a86c13d 4342 goto end_unlock_channel;
b32703d6
JG
4343 default:
4344 abort();
4345 }
c35f9726
JG
4346 }
4347
b32703d6 4348 pthread_mutex_unlock(&channel->lock);
b99a8d42
JD
4349 ret = 0;
4350 goto end;
4351
4352end_unlock_stream:
4353 pthread_mutex_unlock(&stream->lock);
c35f9726 4354end_unlock_channel:
b99a8d42
JD
4355 pthread_mutex_unlock(&channel->lock);
4356end:
4357 rcu_read_unlock();
c35f9726 4358 lttng_dynamic_array_reset(&stream_rotation_positions);
b32703d6 4359 lttng_dynamic_pointer_array_reset(&streams_packet_to_open);
b99a8d42
JD
4360 return ret;
4361}
4362
5f3aff8b
MD
4363static
4364int consumer_clear_buffer(struct lttng_consumer_stream *stream)
4365{
4366 int ret = 0;
4367 unsigned long consumed_pos_before, consumed_pos_after;
4368
4369 ret = lttng_consumer_sample_snapshot_positions(stream);
4370 if (ret < 0) {
4371 ERR("Taking snapshot positions");
4372 goto end;
4373 }
4374
4375 ret = lttng_consumer_get_consumed_snapshot(stream, &consumed_pos_before);
4376 if (ret < 0) {
4377 ERR("Consumed snapshot position");
4378 goto end;
4379 }
4380
fa29bfbf 4381 switch (the_consumer_data.type) {
5f3aff8b
MD
4382 case LTTNG_CONSUMER_KERNEL:
4383 ret = kernctl_buffer_clear(stream->wait_fd);
4384 if (ret < 0) {
96393977 4385 ERR("Failed to clear kernel stream (ret = %d)", ret);
5f3aff8b
MD
4386 goto end;
4387 }
4388 break;
4389 case LTTNG_CONSUMER32_UST:
4390 case LTTNG_CONSUMER64_UST:
d85707b0
MD
4391 ret = lttng_ustconsumer_clear_buffer(stream);
4392 if (ret < 0) {
4393 ERR("Failed to clear ust stream (ret = %d)", ret);
4394 goto end;
4395 }
5f3aff8b
MD
4396 break;
4397 default:
4398 ERR("Unknown consumer_data type");
4399 abort();
4400 }
4401
4402 ret = lttng_consumer_sample_snapshot_positions(stream);
4403 if (ret < 0) {
4404 ERR("Taking snapshot positions");
4405 goto end;
4406 }
4407 ret = lttng_consumer_get_consumed_snapshot(stream, &consumed_pos_after);
4408 if (ret < 0) {
4409 ERR("Consumed snapshot position");
4410 goto end;
4411 }
4412 DBG("clear: before: %lu after: %lu", consumed_pos_before, consumed_pos_after);
4413end:
4414 return ret;
4415}
4416
4417static
4418int consumer_clear_stream(struct lttng_consumer_stream *stream)
4419{
4420 int ret;
4421
503fefca 4422 ret = consumer_stream_flush_buffer(stream, 1);
5f3aff8b
MD
4423 if (ret < 0) {
4424 ERR("Failed to flush stream %" PRIu64 " during channel clear",
4425 stream->key);
4426 ret = LTTCOMM_CONSUMERD_FATAL;
4427 goto error;
4428 }
4429
4430 ret = consumer_clear_buffer(stream);
4431 if (ret < 0) {
4432 ERR("Failed to clear stream %" PRIu64 " during channel clear",
4433 stream->key);
4434 ret = LTTCOMM_CONSUMERD_FATAL;
4435 goto error;
4436 }
4437
4438 ret = LTTCOMM_CONSUMERD_SUCCESS;
4439error:
4440 return ret;
4441}
4442
4443static
4444int consumer_clear_unmonitored_channel(struct lttng_consumer_channel *channel)
4445{
4446 int ret;
4447 struct lttng_consumer_stream *stream;
4448
4449 rcu_read_lock();
4450 pthread_mutex_lock(&channel->lock);
4451 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
4452 health_code_update();
4453 pthread_mutex_lock(&stream->lock);
4454 ret = consumer_clear_stream(stream);
4455 if (ret) {
4456 goto error_unlock;
4457 }
4458 pthread_mutex_unlock(&stream->lock);
4459 }
4460 pthread_mutex_unlock(&channel->lock);
4461 rcu_read_unlock();
4462 return 0;
4463
4464error_unlock:
4465 pthread_mutex_unlock(&stream->lock);
4466 pthread_mutex_unlock(&channel->lock);
4467 rcu_read_unlock();
5f3aff8b
MD
4468 return ret;
4469}
4470
02d02e31
JD
4471/*
4472 * Check if a stream is ready to be rotated after extracting it.
4473 *
4474 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4475 * error. Stream lock must be held.
4476 */
4477int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream *stream)
4478{
f8528c7a
MD
4479 DBG("Check is rotate ready for stream %" PRIu64
4480 " ready %u rotate_position %" PRIu64
4481 " last_sequence_number %" PRIu64,
4482 stream->key, stream->rotate_ready,
4483 stream->rotate_position, stream->last_sequence_number);
02d02e31 4484 if (stream->rotate_ready) {
a40a503f 4485 return 1;
02d02e31
JD
4486 }
4487
4488 /*
a40a503f
MD
4489 * If packet seq num is unavailable, it means we are interacting
4490 * with a pre-2.8 lttng-modules which does not implement the
4491 * sequence number. Rotation should never be used by sessiond in this
4492 * scenario.
02d02e31 4493 */
a40a503f
MD
4494 if (stream->sequence_number_unavailable) {
4495 ERR("Internal error: rotation used on stream %" PRIu64
4496 " with unavailable sequence number",
4497 stream->key);
4498 return -1;
02d02e31
JD
4499 }
4500
a40a503f
MD
4501 if (stream->rotate_position == -1ULL ||
4502 stream->last_sequence_number == -1ULL) {
4503 return 0;
02d02e31
JD
4504 }
4505
a40a503f
MD
4506 /*
4507 * Rotate position not reached yet. The stream rotate position is
4508 * the position of the next packet belonging to the next trace chunk,
4509 * but consumerd considers rotation ready when reaching the last
4510 * packet of the current chunk, hence the "rotate_position - 1".
4511 */
f8528c7a
MD
4512
4513 DBG("Check is rotate ready for stream %" PRIu64
4514 " last_sequence_number %" PRIu64
4515 " rotate_position %" PRIu64,
4516 stream->key, stream->last_sequence_number,
4517 stream->rotate_position);
a40a503f
MD
4518 if (stream->last_sequence_number >= stream->rotate_position - 1) {
4519 return 1;
02d02e31 4520 }
02d02e31 4521
a40a503f 4522 return 0;
02d02e31
JD
4523}
4524
d73bf3d7
JD
4525/*
4526 * Reset the state for a stream after a rotation occurred.
4527 */
4528void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream *stream)
4529{
f8528c7a
MD
4530 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64,
4531 stream->key);
a40a503f 4532 stream->rotate_position = -1ULL;
d73bf3d7
JD
4533 stream->rotate_ready = false;
4534}
4535
4536/*
4537 * Perform the rotation a local stream file.
4538 */
d2956687 4539static
d73bf3d7
JD
4540int rotate_local_stream(struct lttng_consumer_local_data *ctx,
4541 struct lttng_consumer_stream *stream)
4542{
d2956687 4543 int ret = 0;
d73bf3d7 4544
d2956687 4545 DBG("Rotate local stream: stream key %" PRIu64 ", channel key %" PRIu64,
d73bf3d7 4546 stream->key,
d2956687 4547 stream->chan->key);
d73bf3d7 4548 stream->tracefile_size_current = 0;
d2956687 4549 stream->tracefile_count_current = 0;
d73bf3d7 4550
d2956687
JG
4551 if (stream->out_fd >= 0) {
4552 ret = close(stream->out_fd);
4553 if (ret) {
4554 PERROR("Failed to close stream out_fd of channel \"%s\"",
4555 stream->chan->name);
4556 }
4557 stream->out_fd = -1;
4558 }
d73bf3d7 4559
d2956687 4560 if (stream->index_file) {
d73bf3d7 4561 lttng_index_file_put(stream->index_file);
d2956687 4562 stream->index_file = NULL;
d73bf3d7
JD
4563 }
4564
d2956687
JG
4565 if (!stream->trace_chunk) {
4566 goto end;
4567 }
d73bf3d7 4568
d2956687 4569 ret = consumer_stream_create_output_files(stream, true);
d73bf3d7
JD
4570end:
4571 return ret;
d73bf3d7
JD
4572}
4573
d73bf3d7
JD
4574/*
4575 * Performs the stream rotation for the rotate session feature if needed.
d2956687 4576 * It must be called with the channel and stream locks held.
d73bf3d7
JD
4577 *
4578 * Return 0 on success, a negative number of error.
4579 */
4580int lttng_consumer_rotate_stream(struct lttng_consumer_local_data *ctx,
d2956687 4581 struct lttng_consumer_stream *stream)
d73bf3d7
JD
4582{
4583 int ret;
4584
4585 DBG("Consumer rotate stream %" PRIu64, stream->key);
4586
d2956687
JG
4587 /*
4588 * Update the stream's 'current' chunk to the session's (channel)
4589 * now-current chunk.
4590 */
4591 lttng_trace_chunk_put(stream->trace_chunk);
4592 if (stream->chan->trace_chunk == stream->trace_chunk) {
4593 /*
4594 * A channel can be rotated and not have a "next" chunk
4595 * to transition to. In that case, the channel's "current chunk"
4596 * has not been closed yet, but it has not been updated to
4597 * a "next" trace chunk either. Hence, the stream, like its
4598 * parent channel, becomes part of no chunk and can't output
4599 * anything until a new trace chunk is created.
4600 */
4601 stream->trace_chunk = NULL;
4602 } else if (stream->chan->trace_chunk &&
4603 !lttng_trace_chunk_get(stream->chan->trace_chunk)) {
4604 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4605 ret = -1;
4606 goto error;
4607 } else {
4608 /*
4609 * Update the stream's trace chunk to its parent channel's
4610 * current trace chunk.
4611 */
4612 stream->trace_chunk = stream->chan->trace_chunk;
4613 }
4614
c35f9726 4615 if (stream->net_seq_idx == (uint64_t) -1ULL) {
d73bf3d7 4616 ret = rotate_local_stream(ctx, stream);
c35f9726
JG
4617 if (ret < 0) {
4618 ERR("Failed to rotate stream, ret = %i", ret);
4619 goto error;
4620 }
d73bf3d7
JD
4621 }
4622
d2956687
JG
4623 if (stream->metadata_flag && stream->trace_chunk) {
4624 /*
4625 * If the stream has transitioned to a new trace
4626 * chunk, the metadata should be re-dumped to the
4627 * newest chunk.
4628 *
4629 * However, it is possible for a stream to transition to
4630 * a "no-chunk" state. This can happen if a rotation
4631 * occurs on an inactive session. In such cases, the metadata
4632 * regeneration will happen when the next trace chunk is
4633 * created.
4634 */
4635 ret = consumer_metadata_stream_dump(stream);
4636 if (ret) {
4637 goto error;
d73bf3d7
JD
4638 }
4639 }
4640 lttng_consumer_reset_stream_rotate_state(stream);
4641
4642 ret = 0;
4643
4644error:
4645 return ret;
4646}
4647
b99a8d42
JD
4648/*
4649 * Rotate all the ready streams now.
4650 *
4651 * This is especially important for low throughput streams that have already
4652 * been consumed, we cannot wait for their next packet to perform the
4653 * rotation.
92b7a7f8
MD
4654 * Need to be called with RCU read-side lock held to ensure existence of
4655 * channel.
b99a8d42
JD
4656 *
4657 * Returns 0 on success, < 0 on error
4658 */
92b7a7f8
MD
4659int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel *channel,
4660 uint64_t key, struct lttng_consumer_local_data *ctx)
b99a8d42
JD
4661{
4662 int ret;
b99a8d42
JD
4663 struct lttng_consumer_stream *stream;
4664 struct lttng_ht_iter iter;
fa29bfbf 4665 struct lttng_ht *ht = the_consumer_data.stream_per_chan_id_ht;
b99a8d42
JD
4666
4667 rcu_read_lock();
4668
4669 DBG("Consumer rotate ready streams in channel %" PRIu64, key);
4670
b99a8d42
JD
4671 cds_lfht_for_each_entry_duplicate(ht->ht,
4672 ht->hash_fct(&channel->key, lttng_ht_seed),
4673 ht->match_fct, &channel->key, &iter.iter,
4674 stream, node_channel_id.node) {
4675 health_code_update();
4676
d2956687 4677 pthread_mutex_lock(&stream->chan->lock);
b99a8d42
JD
4678 pthread_mutex_lock(&stream->lock);
4679
4680 if (!stream->rotate_ready) {
4681 pthread_mutex_unlock(&stream->lock);
d2956687 4682 pthread_mutex_unlock(&stream->chan->lock);
b99a8d42
JD
4683 continue;
4684 }
4685 DBG("Consumer rotate ready stream %" PRIu64, stream->key);
4686
d2956687 4687 ret = lttng_consumer_rotate_stream(ctx, stream);
b99a8d42 4688 pthread_mutex_unlock(&stream->lock);
d2956687 4689 pthread_mutex_unlock(&stream->chan->lock);
b99a8d42
JD
4690 if (ret) {
4691 goto end;
4692 }
4693 }
4694
4695 ret = 0;
4696
4697end:
4698 rcu_read_unlock();
4699 return ret;
4700}
4701
d2956687
JG
4702enum lttcomm_return_code lttng_consumer_init_command(
4703 struct lttng_consumer_local_data *ctx,
4704 const lttng_uuid sessiond_uuid)
00fb02ac 4705{
d2956687 4706 enum lttcomm_return_code ret;
c70636a7 4707 char uuid_str[LTTNG_UUID_STR_LEN];
00fb02ac 4708
d2956687
JG
4709 if (ctx->sessiond_uuid.is_set) {
4710 ret = LTTCOMM_CONSUMERD_ALREADY_SET;
00fb02ac
JD
4711 goto end;
4712 }
4713
d2956687
JG
4714 ctx->sessiond_uuid.is_set = true;
4715 memcpy(ctx->sessiond_uuid.value, sessiond_uuid, sizeof(lttng_uuid));
4716 ret = LTTCOMM_CONSUMERD_SUCCESS;
4717 lttng_uuid_to_str(sessiond_uuid, uuid_str);
4718 DBG("Received session daemon UUID: %s", uuid_str);
00fb02ac
JD
4719end:
4720 return ret;
4721}
4722
d2956687
JG
4723enum lttcomm_return_code lttng_consumer_create_trace_chunk(
4724 const uint64_t *relayd_id, uint64_t session_id,
4725 uint64_t chunk_id,
4726 time_t chunk_creation_timestamp,
4727 const char *chunk_override_name,
4728 const struct lttng_credentials *credentials,
4729 struct lttng_directory_handle *chunk_directory_handle)
00fb02ac
JD
4730{
4731 int ret;
d2956687 4732 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
7ea24db3 4733 struct lttng_trace_chunk *created_chunk = NULL, *published_chunk = NULL;
d2956687
JG
4734 enum lttng_trace_chunk_status chunk_status;
4735 char relayd_id_buffer[MAX_INT_DEC_LEN(*relayd_id)];
4736 char creation_timestamp_buffer[ISO8601_STR_LEN];
4737 const char *relayd_id_str = "(none)";
4738 const char *creation_timestamp_str;
4739 struct lttng_ht_iter iter;
4740 struct lttng_consumer_channel *channel;
92816cc3 4741
d2956687
JG
4742 if (relayd_id) {
4743 /* Only used for logging purposes. */
4744 ret = snprintf(relayd_id_buffer, sizeof(relayd_id_buffer),
4745 "%" PRIu64, *relayd_id);
4746 if (ret > 0 && ret < sizeof(relayd_id_buffer)) {
4747 relayd_id_str = relayd_id_buffer;
4748 } else {
4749 relayd_id_str = "(formatting error)";
4750 }
d01ef216 4751 }
d2956687 4752
d01ef216 4753 /* Local protocol error. */
d2956687
JG
4754 assert(chunk_creation_timestamp);
4755 ret = time_to_iso8601_str(chunk_creation_timestamp,
4756 creation_timestamp_buffer,
4757 sizeof(creation_timestamp_buffer));
4758 creation_timestamp_str = !ret ? creation_timestamp_buffer :
4759 "(formatting error)";
4760
4761 DBG("Consumer create trace chunk command: relay_id = %s"
4762 ", session_id = %" PRIu64 ", chunk_id = %" PRIu64
4763 ", chunk_override_name = %s"
4764 ", chunk_creation_timestamp = %s",
4765 relayd_id_str, session_id, chunk_id,
4766 chunk_override_name ? : "(none)",
4767 creation_timestamp_str);
92816cc3
JG
4768
4769 /*
d2956687
JG
4770 * The trace chunk registry, as used by the consumer daemon, implicitly
4771 * owns the trace chunks. This is only needed in the consumer since
4772 * the consumer has no notion of a session beyond session IDs being
4773 * used to identify other objects.
4774 *
4775 * The lttng_trace_chunk_registry_publish() call below provides a
4776 * reference which is not released; it implicitly becomes the session
4777 * daemon's reference to the chunk in the consumer daemon.
4778 *
4779 * The lifetime of trace chunks in the consumer daemon is managed by
4780 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4781 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
92816cc3 4782 */
d2956687 4783 created_chunk = lttng_trace_chunk_create(chunk_id,
a7ceb342 4784 chunk_creation_timestamp, NULL);
d2956687
JG
4785 if (!created_chunk) {
4786 ERR("Failed to create trace chunk");
4787 ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED;
7ea24db3 4788 goto error;
d2956687 4789 }
92816cc3 4790
d2956687
JG
4791 if (chunk_override_name) {
4792 chunk_status = lttng_trace_chunk_override_name(created_chunk,
4793 chunk_override_name);
4794 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
4795 ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED;
7ea24db3 4796 goto error;
92816cc3
JG
4797 }
4798 }
4799
d2956687
JG
4800 if (chunk_directory_handle) {
4801 chunk_status = lttng_trace_chunk_set_credentials(created_chunk,
4802 credentials);
4803 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
4804 ERR("Failed to set trace chunk credentials");
4805 ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED;
7ea24db3 4806 goto error;
d2956687
JG
4807 }
4808 /*
4809 * The consumer daemon has no ownership of the chunk output
4810 * directory.
4811 */
4812 chunk_status = lttng_trace_chunk_set_as_user(created_chunk,
4813 chunk_directory_handle);
cbf53d23 4814 chunk_directory_handle = NULL;
d2956687
JG
4815 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
4816 ERR("Failed to set trace chunk's directory handle");
4817 ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED;
7ea24db3 4818 goto error;
92816cc3
JG
4819 }
4820 }
4821
d2956687 4822 published_chunk = lttng_trace_chunk_registry_publish_chunk(
fa29bfbf 4823 the_consumer_data.chunk_registry, session_id,
d2956687
JG
4824 created_chunk);
4825 lttng_trace_chunk_put(created_chunk);
4826 created_chunk = NULL;
4827 if (!published_chunk) {
4828 ERR("Failed to publish trace chunk");
4829 ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED;
7ea24db3 4830 goto error;
d88744a4
JD
4831 }
4832
d2956687 4833 rcu_read_lock();
fa29bfbf
SM
4834 cds_lfht_for_each_entry_duplicate(
4835 the_consumer_data.channels_by_session_id_ht->ht,
4836 the_consumer_data.channels_by_session_id_ht->hash_fct(
d2956687 4837 &session_id, lttng_ht_seed),
fa29bfbf 4838 the_consumer_data.channels_by_session_id_ht->match_fct,
d2956687
JG
4839 &session_id, &iter.iter, channel,
4840 channels_by_session_id_ht_node.node) {
4841 ret = lttng_consumer_channel_set_trace_chunk(channel,
4842 published_chunk);
4843 if (ret) {
4844 /*
4845 * Roll-back the creation of this chunk.
4846 *
4847 * This is important since the session daemon will
4848 * assume that the creation of this chunk failed and
4849 * will never ask for it to be closed, resulting
4850 * in a leak and an inconsistent state for some
4851 * channels.
4852 */
4853 enum lttcomm_return_code close_ret;
ecd1a12f 4854 char path[LTTNG_PATH_MAX];
d2956687
JG
4855
4856 DBG("Failed to set new trace chunk on existing channels, rolling back");
4857 close_ret = lttng_consumer_close_trace_chunk(relayd_id,
4858 session_id, chunk_id,
ecd1a12f
MD
4859 chunk_creation_timestamp, NULL,
4860 path);
d2956687
JG
4861 if (close_ret != LTTCOMM_CONSUMERD_SUCCESS) {
4862 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64 ", chunk_id = %" PRIu64,
4863 session_id, chunk_id);
4864 }
a1ae2ea5 4865
d2956687
JG
4866 ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED;
4867 break;
4868 }
a1ae2ea5
JD
4869 }
4870
e5add6d0
JG
4871 if (relayd_id) {
4872 struct consumer_relayd_sock_pair *relayd;
4873
4874 relayd = consumer_find_relayd(*relayd_id);
4875 if (relayd) {
4876 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
4877 ret = relayd_create_trace_chunk(
4878 &relayd->control_sock, published_chunk);
4879 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
4880 } else {
4881 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64, *relayd_id);
4882 }
4883
4884 if (!relayd || ret) {
4885 enum lttcomm_return_code close_ret;
ecd1a12f 4886 char path[LTTNG_PATH_MAX];
e5add6d0
JG
4887
4888 close_ret = lttng_consumer_close_trace_chunk(relayd_id,
4889 session_id,
4890 chunk_id,
bbc4768c 4891 chunk_creation_timestamp,
ecd1a12f 4892 NULL, path);
e5add6d0
JG
4893 if (close_ret != LTTCOMM_CONSUMERD_SUCCESS) {
4894 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64 ", chunk_id = %" PRIu64,
4895 session_id,
4896 chunk_id);
4897 }
4898
4899 ret_code = LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED;
7ea24db3 4900 goto error_unlock;
e5add6d0
JG
4901 }
4902 }
7ea24db3 4903error_unlock:
e5add6d0 4904 rcu_read_unlock();
7ea24db3 4905error:
d2956687
JG
4906 /* Release the reference returned by the "publish" operation. */
4907 lttng_trace_chunk_put(published_chunk);
9bb5f1f8 4908 lttng_trace_chunk_put(created_chunk);
d2956687 4909 return ret_code;
a1ae2ea5
JD
4910}
4911
d2956687
JG
4912enum lttcomm_return_code lttng_consumer_close_trace_chunk(
4913 const uint64_t *relayd_id, uint64_t session_id,
bbc4768c 4914 uint64_t chunk_id, time_t chunk_close_timestamp,
ecd1a12f
MD
4915 const enum lttng_trace_chunk_command_type *close_command,
4916 char *path)
a1ae2ea5 4917{
d2956687
JG
4918 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
4919 struct lttng_trace_chunk *chunk;
4920 char relayd_id_buffer[MAX_INT_DEC_LEN(*relayd_id)];
4921 const char *relayd_id_str = "(none)";
bbc4768c 4922 const char *close_command_name = "none";
d2956687
JG
4923 struct lttng_ht_iter iter;
4924 struct lttng_consumer_channel *channel;
4925 enum lttng_trace_chunk_status chunk_status;
a1ae2ea5 4926
d2956687
JG
4927 if (relayd_id) {
4928 int ret;
4929
4930 /* Only used for logging purposes. */
4931 ret = snprintf(relayd_id_buffer, sizeof(relayd_id_buffer),
4932 "%" PRIu64, *relayd_id);
4933 if (ret > 0 && ret < sizeof(relayd_id_buffer)) {
4934 relayd_id_str = relayd_id_buffer;
4935 } else {
4936 relayd_id_str = "(formatting error)";
4937 }
bbc4768c
JG
4938 }
4939 if (close_command) {
4940 close_command_name = lttng_trace_chunk_command_type_get_name(
4941 *close_command);
4942 }
d2956687
JG
4943
4944 DBG("Consumer close trace chunk command: relayd_id = %s"
bbc4768c
JG
4945 ", session_id = %" PRIu64 ", chunk_id = %" PRIu64
4946 ", close command = %s",
4947 relayd_id_str, session_id, chunk_id,
4948 close_command_name);
4949
d2956687 4950 chunk = lttng_trace_chunk_registry_find_chunk(
fa29bfbf 4951 the_consumer_data.chunk_registry, session_id, chunk_id);
bbc4768c 4952 if (!chunk) {
d2956687
JG
4953 ERR("Failed to find chunk: session_id = %" PRIu64
4954 ", chunk_id = %" PRIu64,
4955 session_id, chunk_id);
4956 ret_code = LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK;
a1ae2ea5
JD
4957 goto end;
4958 }
4959
d2956687
JG
4960 chunk_status = lttng_trace_chunk_set_close_timestamp(chunk,
4961 chunk_close_timestamp);
4962 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
4963 ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED;
4964 goto end;
45f1d9a1 4965 }
bbc4768c
JG
4966
4967 if (close_command) {
4968 chunk_status = lttng_trace_chunk_set_close_command(
4969 chunk, *close_command);
4970 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
4971 ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED;
4972 goto end;
4973 }
4974 }
a1ae2ea5 4975
d2956687
JG
4976 /*
4977 * chunk is now invalid to access as we no longer hold a reference to
4978 * it; it is only kept around to compare it (by address) to the
4979 * current chunk found in the session's channels.
4980 */
4981 rcu_read_lock();
fa29bfbf 4982 cds_lfht_for_each_entry(the_consumer_data.channel_ht->ht, &iter.iter,
d2956687
JG
4983 channel, node.node) {
4984 int ret;
a1ae2ea5 4985
d2956687
JG
4986 /*
4987 * Only change the channel's chunk to NULL if it still
4988 * references the chunk being closed. The channel may
4989 * reference a newer channel in the case of a session
4990 * rotation. When a session rotation occurs, the "next"
4991 * chunk is created before the "current" chunk is closed.
4992 */
4993 if (channel->trace_chunk != chunk) {
4994 continue;
4995 }
4996 ret = lttng_consumer_channel_set_trace_chunk(channel, NULL);
4997 if (ret) {
4998 /*
4999 * Attempt to close the chunk on as many channels as
5000 * possible.
5001 */
5002 ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED;
5003 }
a1ae2ea5 5004 }
bbc4768c
JG
5005
5006 if (relayd_id) {
5007 int ret;
5008 struct consumer_relayd_sock_pair *relayd;
5009
5010 relayd = consumer_find_relayd(*relayd_id);
5011 if (relayd) {
5012 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
5013 ret = relayd_close_trace_chunk(
ecd1a12f
MD
5014 &relayd->control_sock, chunk,
5015 path);
bbc4768c
JG
5016 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
5017 } else {
5018 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64,
5019 *relayd_id);
5020 }
5021
5022 if (!relayd || ret) {
5023 ret_code = LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED;
5024 goto error_unlock;
5025 }
5026 }
5027error_unlock:
d2956687
JG
5028 rcu_read_unlock();
5029end:
bbc4768c
JG
5030 /*
5031 * Release the reference returned by the "find" operation and
5032 * the session daemon's implicit reference to the chunk.
5033 */
5034 lttng_trace_chunk_put(chunk);
5035 lttng_trace_chunk_put(chunk);
5036
d2956687 5037 return ret_code;
a1ae2ea5 5038}
3654ed19 5039
d2956687
JG
5040enum lttcomm_return_code lttng_consumer_trace_chunk_exists(
5041 const uint64_t *relayd_id, uint64_t session_id,
5042 uint64_t chunk_id)
3654ed19 5043{
c35f9726 5044 int ret;
d2956687 5045 enum lttcomm_return_code ret_code;
d2956687
JG
5046 char relayd_id_buffer[MAX_INT_DEC_LEN(*relayd_id)];
5047 const char *relayd_id_str = "(none)";
c35f9726
JG
5048 const bool is_local_trace = !relayd_id;
5049 struct consumer_relayd_sock_pair *relayd = NULL;
6b584c2e 5050 bool chunk_exists_local, chunk_exists_remote;
d2956687
JG
5051
5052 if (relayd_id) {
d2956687
JG
5053 /* Only used for logging purposes. */
5054 ret = snprintf(relayd_id_buffer, sizeof(relayd_id_buffer),
5055 "%" PRIu64, *relayd_id);
5056 if (ret > 0 && ret < sizeof(relayd_id_buffer)) {
5057 relayd_id_str = relayd_id_buffer;
5058 } else {
5059 relayd_id_str = "(formatting error)";
5060 }
d01ef216 5061 }
d2956687
JG
5062
5063 DBG("Consumer trace chunk exists command: relayd_id = %s"
d2956687 5064 ", chunk_id = %" PRIu64, relayd_id_str,
c35f9726 5065 chunk_id);
6b584c2e 5066 ret = lttng_trace_chunk_registry_chunk_exists(
fa29bfbf
SM
5067 the_consumer_data.chunk_registry, session_id, chunk_id,
5068 &chunk_exists_local);
6b584c2e
JG
5069 if (ret) {
5070 /* Internal error. */
5071 ERR("Failed to query the existence of a trace chunk");
5072 ret_code = LTTCOMM_CONSUMERD_FATAL;
13e3b280 5073 goto end;
6b584c2e
JG
5074 }
5075 DBG("Trace chunk %s locally",
5076 chunk_exists_local ? "exists" : "does not exist");
5077 if (chunk_exists_local) {
c35f9726 5078 ret_code = LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL;
c35f9726
JG
5079 goto end;
5080 } else if (is_local_trace) {
5081 ret_code = LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK;
5082 goto end;
5083 }
5084
5085 rcu_read_lock();
5086 relayd = consumer_find_relayd(*relayd_id);
5087 if (!relayd) {
5088 ERR("Failed to find relayd %" PRIu64, *relayd_id);
5089 ret_code = LTTCOMM_CONSUMERD_INVALID_PARAMETERS;
5090 goto end_rcu_unlock;
5091 }
5092 DBG("Looking up existence of trace chunk on relay daemon");
5093 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
5094 ret = relayd_trace_chunk_exists(&relayd->control_sock, chunk_id,
5095 &chunk_exists_remote);
5096 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
5097 if (ret < 0) {
5098 ERR("Failed to look-up the existence of trace chunk on relay daemon");
5099 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
5100 goto end_rcu_unlock;
5101 }
5102
5103 ret_code = chunk_exists_remote ?
5104 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE :
d2956687 5105 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK;
c35f9726
JG
5106 DBG("Trace chunk %s on relay daemon",
5107 chunk_exists_remote ? "exists" : "does not exist");
d2956687 5108
c35f9726
JG
5109end_rcu_unlock:
5110 rcu_read_unlock();
5111end:
d2956687 5112 return ret_code;
3654ed19 5113}
5f3aff8b
MD
5114
5115static
5116int consumer_clear_monitored_channel(struct lttng_consumer_channel *channel)
5117{
5118 struct lttng_ht *ht;
5119 struct lttng_consumer_stream *stream;
5120 struct lttng_ht_iter iter;
5121 int ret;
5122
fa29bfbf 5123 ht = the_consumer_data.stream_per_chan_id_ht;
5f3aff8b
MD
5124
5125 rcu_read_lock();
5126 cds_lfht_for_each_entry_duplicate(ht->ht,
5127 ht->hash_fct(&channel->key, lttng_ht_seed),
5128 ht->match_fct, &channel->key,
5129 &iter.iter, stream, node_channel_id.node) {
5130 /*
5131 * Protect against teardown with mutex.
5132 */
5133 pthread_mutex_lock(&stream->lock);
5134 if (cds_lfht_is_node_deleted(&stream->node.node)) {
5135 goto next;
5136 }
5137 ret = consumer_clear_stream(stream);
5138 if (ret) {
5139 goto error_unlock;
5140 }
5141 next:
5142 pthread_mutex_unlock(&stream->lock);
5143 }
5144 rcu_read_unlock();
5145 return LTTCOMM_CONSUMERD_SUCCESS;
5146
5147error_unlock:
5148 pthread_mutex_unlock(&stream->lock);
5149 rcu_read_unlock();
5150 return ret;
5151}
5152
5153int lttng_consumer_clear_channel(struct lttng_consumer_channel *channel)
5154{
5155 int ret;
5156
5157 DBG("Consumer clear channel %" PRIu64, channel->key);
5158
5159 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) {
5160 /*
5161 * Nothing to do for the metadata channel/stream.
5162 * Snapshot mechanism already take care of the metadata
5163 * handling/generation, and monitored channels only need to
5164 * have their data stream cleared..
5165 */
5166 ret = LTTCOMM_CONSUMERD_SUCCESS;
5167 goto end;
5168 }
5169
5170 if (!channel->monitor) {
5171 ret = consumer_clear_unmonitored_channel(channel);
5172 } else {
5173 ret = consumer_clear_monitored_channel(channel);
5174 }
5175end:
5176 return ret;
5177}
04ed9e10
JG
5178
5179enum lttcomm_return_code lttng_consumer_open_channel_packets(
5180 struct lttng_consumer_channel *channel)
5181{
5182 struct lttng_consumer_stream *stream;
5183 enum lttcomm_return_code ret = LTTCOMM_CONSUMERD_SUCCESS;
5184
5185 if (channel->metadata_stream) {
5186 ERR("Open channel packets command attempted on a metadata channel");
5187 ret = LTTCOMM_CONSUMERD_INVALID_PARAMETERS;
5188 goto end;
5189 }
5190
5191 rcu_read_lock();
5192 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
503fefca 5193 enum consumer_stream_open_packet_status status;
04ed9e10
JG
5194
5195 pthread_mutex_lock(&stream->lock);
5196 if (cds_lfht_is_node_deleted(&stream->node.node)) {
5197 goto next;
5198 }
5199
503fefca 5200 status = consumer_stream_open_packet(stream);
04ed9e10 5201 switch (status) {
503fefca 5202 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED:
04ed9e10
JG
5203 DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64
5204 ", channel name = %s, session id = %" PRIu64,
5205 stream->key, stream->chan->name,
5206 stream->chan->session_id);
5207 stream->opened_packet_in_current_trace_chunk = true;
5208 break;
503fefca 5209 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE:
04ed9e10
JG
5210 DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64
5211 ", channel name = %s, session id = %" PRIu64,
5212 stream->key, stream->chan->name,
5213 stream->chan->session_id);
5214 break;
503fefca 5215 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR:
04ed9e10
JG
5216 /*
5217 * Only unexpected internal errors can lead to this
5218 * failing. Report an unknown error.
5219 */
5220 ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64
5221 ", channel id = %" PRIu64
5222 ", channel name = %s"
5223 ", session id = %" PRIu64,
5224 stream->key, channel->key,
5225 channel->name, channel->session_id);
5226 ret = LTTCOMM_CONSUMERD_UNKNOWN_ERROR;
5227 goto error_unlock;
5228 default:
5229 abort();
5230 }
5231
5232 next:
5233 pthread_mutex_unlock(&stream->lock);
5234 }
5235
5236end_rcu_unlock:
5237 rcu_read_unlock();
5238end:
5239 return ret;
5240
5241error_unlock:
5242 pthread_mutex_unlock(&stream->lock);
5243 goto end_rcu_unlock;
5244}
d85707b0
MD
5245
5246void lttng_consumer_sigbus_handle(void *addr)
5247{
5248 lttng_ustconsumer_sigbus_handle(addr);
5249}
This page took 0.417625 seconds and 4 git commands to generate.