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