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