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