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