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