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