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