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