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