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