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