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