Move add data stream to the data thread
[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>
3bd1e081 31
990570ed 32#include <common/common.h>
fb3a43a9
DG
33#include <common/utils.h>
34#include <common/compat/poll.h>
10a8a223 35#include <common/kernel-ctl/kernel-ctl.h>
00e2e675 36#include <common/sessiond-comm/relayd.h>
10a8a223
DG
37#include <common/sessiond-comm/sessiond-comm.h>
38#include <common/kernel-consumer/kernel-consumer.h>
00e2e675 39#include <common/relayd/relayd.h>
10a8a223
DG
40#include <common/ust-consumer/ust-consumer.h>
41
42#include "consumer.h"
3bd1e081
MD
43
44struct lttng_consumer_global_data consumer_data = {
3bd1e081
MD
45 .stream_count = 0,
46 .need_update = 1,
47 .type = LTTNG_CONSUMER_UNKNOWN,
48};
49
50/* timeout parameter, to control the polling thread grace period. */
51int consumer_poll_timeout = -1;
52
53/*
54 * Flag to inform the polling thread to quit when all fd hung up. Updated by
55 * the consumer_thread_receive_fds when it notices that all fds has hung up.
56 * Also updated by the signal handler (consumer_should_exit()). Read by the
57 * polling threads.
58 */
59volatile int consumer_quit = 0;
60
61/*
62 * Find a stream. The consumer_data.lock must be locked during this
63 * call.
64 */
8389e4f8
DG
65static struct lttng_consumer_stream *consumer_find_stream(int key,
66 struct lttng_ht *ht)
3bd1e081 67{
e4421fec
DG
68 struct lttng_ht_iter iter;
69 struct lttng_ht_node_ulong *node;
70 struct lttng_consumer_stream *stream = NULL;
3bd1e081 71
8389e4f8
DG
72 assert(ht);
73
7ad0a0cb 74 /* Negative keys are lookup failures */
7a57cf92 75 if (key < 0) {
7ad0a0cb 76 return NULL;
7a57cf92 77 }
e4421fec 78
6065ceec
DG
79 rcu_read_lock();
80
8389e4f8 81 lttng_ht_lookup(ht, (void *)((unsigned long) key), &iter);
e4421fec
DG
82 node = lttng_ht_iter_get_node_ulong(&iter);
83 if (node != NULL) {
84 stream = caa_container_of(node, struct lttng_consumer_stream, node);
3bd1e081 85 }
e4421fec 86
6065ceec
DG
87 rcu_read_unlock();
88
e4421fec 89 return stream;
3bd1e081
MD
90}
91
c869f647 92void consumer_steal_stream_key(int key, struct lttng_ht *ht)
7ad0a0cb
MD
93{
94 struct lttng_consumer_stream *stream;
95
04253271 96 rcu_read_lock();
8389e4f8 97 stream = consumer_find_stream(key, ht);
04253271 98 if (stream) {
7ad0a0cb 99 stream->key = -1;
04253271
MD
100 /*
101 * We don't want the lookup to match, but we still need
102 * to iterate on this stream when iterating over the hash table. Just
103 * change the node key.
104 */
105 stream->node.key = -1;
106 }
107 rcu_read_unlock();
7ad0a0cb
MD
108}
109
3bd1e081
MD
110static struct lttng_consumer_channel *consumer_find_channel(int key)
111{
e4421fec
DG
112 struct lttng_ht_iter iter;
113 struct lttng_ht_node_ulong *node;
114 struct lttng_consumer_channel *channel = NULL;
3bd1e081 115
7ad0a0cb 116 /* Negative keys are lookup failures */
7a57cf92 117 if (key < 0) {
7ad0a0cb 118 return NULL;
7a57cf92 119 }
e4421fec 120
6065ceec
DG
121 rcu_read_lock();
122
e4421fec
DG
123 lttng_ht_lookup(consumer_data.channel_ht, (void *)((unsigned long) key),
124 &iter);
125 node = lttng_ht_iter_get_node_ulong(&iter);
126 if (node != NULL) {
127 channel = caa_container_of(node, struct lttng_consumer_channel, node);
3bd1e081 128 }
e4421fec 129
6065ceec
DG
130 rcu_read_unlock();
131
e4421fec 132 return channel;
3bd1e081
MD
133}
134
7ad0a0cb
MD
135static void consumer_steal_channel_key(int key)
136{
137 struct lttng_consumer_channel *channel;
138
04253271 139 rcu_read_lock();
7ad0a0cb 140 channel = consumer_find_channel(key);
04253271 141 if (channel) {
7ad0a0cb 142 channel->key = -1;
04253271
MD
143 /*
144 * We don't want the lookup to match, but we still need
145 * to iterate on this channel when iterating over the hash table. Just
146 * change the node key.
147 */
148 channel->node.key = -1;
149 }
150 rcu_read_unlock();
7ad0a0cb
MD
151}
152
702b1ea4
MD
153static
154void consumer_free_stream(struct rcu_head *head)
155{
156 struct lttng_ht_node_ulong *node =
157 caa_container_of(head, struct lttng_ht_node_ulong, head);
158 struct lttng_consumer_stream *stream =
159 caa_container_of(node, struct lttng_consumer_stream, node);
160
161 free(stream);
162}
163
e316aad5
DG
164static
165void consumer_free_metadata_stream(struct rcu_head *head)
166{
167 struct lttng_ht_node_ulong *node =
168 caa_container_of(head, struct lttng_ht_node_ulong, head);
169 struct lttng_consumer_stream *stream =
170 caa_container_of(node, struct lttng_consumer_stream, waitfd_node);
171
172 free(stream);
173}
174
00e2e675
DG
175/*
176 * RCU protected relayd socket pair free.
177 */
178static void consumer_rcu_free_relayd(struct rcu_head *head)
179{
180 struct lttng_ht_node_ulong *node =
181 caa_container_of(head, struct lttng_ht_node_ulong, head);
182 struct consumer_relayd_sock_pair *relayd =
183 caa_container_of(node, struct consumer_relayd_sock_pair, node);
184
185 free(relayd);
186}
187
188/*
189 * Destroy and free relayd socket pair object.
190 *
191 * This function MUST be called with the consumer_data lock acquired.
192 */
d09e1200 193static void destroy_relayd(struct consumer_relayd_sock_pair *relayd)
00e2e675
DG
194{
195 int ret;
196 struct lttng_ht_iter iter;
197
173af62f
DG
198 if (relayd == NULL) {
199 return;
200 }
201
00e2e675
DG
202 DBG("Consumer destroy and close relayd socket pair");
203
204 iter.iter.node = &relayd->node.node;
205 ret = lttng_ht_del(consumer_data.relayd_ht, &iter);
173af62f
DG
206 if (ret != 0) {
207 /* We assume the relayd was already destroyed */
208 return;
209 }
00e2e675
DG
210
211 /* Close all sockets */
212 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
213 (void) relayd_close(&relayd->control_sock);
214 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
215 (void) relayd_close(&relayd->data_sock);
216
217 /* RCU free() call */
218 call_rcu(&relayd->node.head, consumer_rcu_free_relayd);
219}
220
a6ba4fe1
DG
221/*
222 * Flag a relayd socket pair for destruction. Destroy it if the refcount
223 * reaches zero.
224 *
225 * RCU read side lock MUST be aquired before calling this function.
226 */
227void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd)
228{
229 assert(relayd);
230
231 /* Set destroy flag for this object */
232 uatomic_set(&relayd->destroy_flag, 1);
233
234 /* Destroy the relayd if refcount is 0 */
235 if (uatomic_read(&relayd->refcount) == 0) {
d09e1200 236 destroy_relayd(relayd);
a6ba4fe1
DG
237 }
238}
239
3bd1e081
MD
240/*
241 * Remove a stream from the global list protected by a mutex. This
242 * function is also responsible for freeing its data structures.
243 */
e316aad5
DG
244void consumer_del_stream(struct lttng_consumer_stream *stream,
245 struct lttng_ht *ht)
3bd1e081
MD
246{
247 int ret;
e4421fec 248 struct lttng_ht_iter iter;
3bd1e081 249 struct lttng_consumer_channel *free_chan = NULL;
00e2e675
DG
250 struct consumer_relayd_sock_pair *relayd;
251
252 assert(stream);
3bd1e081 253
e316aad5
DG
254 if (ht == NULL) {
255 /* Means the stream was allocated but not successfully added */
256 goto free_stream;
257 }
258
3bd1e081
MD
259 pthread_mutex_lock(&consumer_data.lock);
260
261 switch (consumer_data.type) {
262 case LTTNG_CONSUMER_KERNEL:
263 if (stream->mmap_base != NULL) {
264 ret = munmap(stream->mmap_base, stream->mmap_len);
265 if (ret != 0) {
7a57cf92 266 PERROR("munmap");
3bd1e081
MD
267 }
268 }
269 break;
7753dea8
MD
270 case LTTNG_CONSUMER32_UST:
271 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
272 lttng_ustconsumer_del_stream(stream);
273 break;
274 default:
275 ERR("Unknown consumer_data type");
276 assert(0);
277 goto end;
278 }
279
6065ceec 280 rcu_read_lock();
04253271 281 iter.iter.node = &stream->node.node;
e316aad5 282 ret = lttng_ht_del(ht, &iter);
04253271 283 assert(!ret);
e4421fec 284
6065ceec
DG
285 rcu_read_unlock();
286
3bd1e081
MD
287 if (consumer_data.stream_count <= 0) {
288 goto end;
289 }
290 consumer_data.stream_count--;
291 if (!stream) {
292 goto end;
293 }
294 if (stream->out_fd >= 0) {
4c462e79
MD
295 ret = close(stream->out_fd);
296 if (ret) {
297 PERROR("close");
298 }
3bd1e081 299 }
b5c5fc29 300 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
4c462e79
MD
301 ret = close(stream->wait_fd);
302 if (ret) {
303 PERROR("close");
304 }
3bd1e081 305 }
2c1dd183 306 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
4c462e79
MD
307 ret = close(stream->shm_fd);
308 if (ret) {
309 PERROR("close");
310 }
3bd1e081 311 }
00e2e675
DG
312
313 /* Check and cleanup relayd */
b0b335c8 314 rcu_read_lock();
00e2e675
DG
315 relayd = consumer_find_relayd(stream->net_seq_idx);
316 if (relayd != NULL) {
b0b335c8
MD
317 uatomic_dec(&relayd->refcount);
318 assert(uatomic_read(&relayd->refcount) >= 0);
173af62f 319
3f8e211f
DG
320 /* Closing streams requires to lock the control socket. */
321 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
173af62f
DG
322 ret = relayd_send_close_stream(&relayd->control_sock,
323 stream->relayd_stream_id,
324 stream->next_net_seq_num - 1);
3f8e211f 325 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
173af62f 326 if (ret < 0) {
a4b92340
DG
327 DBG("Unable to close stream on the relayd. Continuing");
328 /*
329 * Continue here. There is nothing we can do for the relayd.
330 * Chances are that the relayd has closed the socket so we just
331 * continue cleaning up.
332 */
173af62f
DG
333 }
334
335 /* Both conditions are met, we destroy the relayd. */
336 if (uatomic_read(&relayd->refcount) == 0 &&
337 uatomic_read(&relayd->destroy_flag)) {
d09e1200 338 destroy_relayd(relayd);
00e2e675 339 }
00e2e675 340 }
b0b335c8 341 rcu_read_unlock();
00e2e675 342
c30aaa51
MD
343 uatomic_dec(&stream->chan->refcount);
344 if (!uatomic_read(&stream->chan->refcount)
345 && !uatomic_read(&stream->chan->nb_init_streams)) {
3bd1e081 346 free_chan = stream->chan;
00e2e675
DG
347 }
348
3bd1e081
MD
349end:
350 consumer_data.need_update = 1;
351 pthread_mutex_unlock(&consumer_data.lock);
352
c30aaa51 353 if (free_chan) {
3bd1e081 354 consumer_del_channel(free_chan);
c30aaa51 355 }
e316aad5
DG
356
357free_stream:
358 call_rcu(&stream->node.head, consumer_free_stream);
3bd1e081
MD
359}
360
361struct lttng_consumer_stream *consumer_allocate_stream(
362 int channel_key, int stream_key,
363 int shm_fd, int wait_fd,
364 enum lttng_consumer_stream_state state,
365 uint64_t mmap_len,
366 enum lttng_event_output output,
6df2e2c9
MD
367 const char *path_name,
368 uid_t uid,
00e2e675
DG
369 gid_t gid,
370 int net_index,
c80048c6
MD
371 int metadata_flag,
372 int *alloc_ret)
3bd1e081
MD
373{
374 struct lttng_consumer_stream *stream;
3bd1e081 375
effcf122 376 stream = zmalloc(sizeof(*stream));
3bd1e081 377 if (stream == NULL) {
7a57cf92 378 PERROR("malloc struct lttng_consumer_stream");
c80048c6 379 *alloc_ret = -ENOMEM;
7a57cf92 380 goto end;
3bd1e081 381 }
7a57cf92
DG
382
383 /*
384 * Get stream's channel reference. Needed when adding the stream to the
385 * global hash table.
386 */
3bd1e081
MD
387 stream->chan = consumer_find_channel(channel_key);
388 if (!stream->chan) {
c80048c6 389 *alloc_ret = -ENOENT;
7a57cf92 390 ERR("Unable to find channel for stream %d", stream_key);
c80048c6 391 goto error;
3bd1e081 392 }
e316aad5 393
3bd1e081
MD
394 stream->key = stream_key;
395 stream->shm_fd = shm_fd;
396 stream->wait_fd = wait_fd;
397 stream->out_fd = -1;
398 stream->out_fd_offset = 0;
399 stream->state = state;
400 stream->mmap_len = mmap_len;
401 stream->mmap_base = NULL;
402 stream->output = output;
6df2e2c9
MD
403 stream->uid = uid;
404 stream->gid = gid;
00e2e675
DG
405 stream->net_seq_idx = net_index;
406 stream->metadata_flag = metadata_flag;
407 strncpy(stream->path_name, path_name, sizeof(stream->path_name));
408 stream->path_name[sizeof(stream->path_name) - 1] = '\0';
00e2e675 409 lttng_ht_node_init_ulong(&stream->waitfd_node, stream->wait_fd);
e316aad5 410 lttng_ht_node_init_ulong(&stream->node, stream->key);
c30aaa51 411
c869f647
DG
412 /*
413 * The cpu number is needed before using any ustctl_* actions. Ignored for
414 * the kernel so the value does not matter.
415 */
416 pthread_mutex_lock(&consumer_data.lock);
417 stream->cpu = stream->chan->cpucount++;
418 pthread_mutex_unlock(&consumer_data.lock);
419
c30aaa51
MD
420 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
421 " out_fd %d, net_seq_idx %d)", stream->path_name, stream->key,
422 stream->shm_fd, stream->wait_fd,
423 (unsigned long long) stream->mmap_len, stream->out_fd,
00e2e675 424 stream->net_seq_idx);
3bd1e081 425 return stream;
c80048c6
MD
426
427error:
428 free(stream);
7a57cf92 429end:
c80048c6 430 return NULL;
3bd1e081
MD
431}
432
433/*
434 * Add a stream to the global list protected by a mutex.
435 */
436int consumer_add_stream(struct lttng_consumer_stream *stream)
437{
438 int ret = 0;
00e2e675 439 struct consumer_relayd_sock_pair *relayd;
3bd1e081 440
e316aad5 441 assert(stream);
c77fc10a 442
e316aad5
DG
443 DBG3("Adding consumer stream %d", stream->key);
444
445 pthread_mutex_lock(&consumer_data.lock);
b0b335c8 446 rcu_read_lock();
e316aad5 447
04253271 448 lttng_ht_add_unique_ulong(consumer_data.stream_ht, &stream->node);
00e2e675
DG
449
450 /* Check and cleanup relayd */
451 relayd = consumer_find_relayd(stream->net_seq_idx);
452 if (relayd != NULL) {
b0b335c8 453 uatomic_inc(&relayd->refcount);
00e2e675
DG
454 }
455
e316aad5
DG
456 /* Update channel refcount once added without error(s). */
457 uatomic_inc(&stream->chan->refcount);
458
459 /*
460 * When nb_init_streams reaches 0, we don't need to trigger any action in
461 * terms of destroying the associated channel, because the action that
462 * causes the count to become 0 also causes a stream to be added. The
463 * channel deletion will thus be triggered by the following removal of this
464 * stream.
465 */
466 if (uatomic_read(&stream->chan->nb_init_streams) > 0) {
467 uatomic_dec(&stream->chan->nb_init_streams);
468 }
469
470 /* Update consumer data once the node is inserted. */
3bd1e081
MD
471 consumer_data.stream_count++;
472 consumer_data.need_update = 1;
473
e316aad5 474 rcu_read_unlock();
3bd1e081 475 pthread_mutex_unlock(&consumer_data.lock);
702b1ea4 476
3bd1e081
MD
477 return ret;
478}
479
00e2e675 480/*
3f8e211f
DG
481 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
482 * be acquired before calling this.
00e2e675 483 */
d09e1200 484static int add_relayd(struct consumer_relayd_sock_pair *relayd)
00e2e675
DG
485{
486 int ret = 0;
487 struct lttng_ht_node_ulong *node;
488 struct lttng_ht_iter iter;
489
490 if (relayd == NULL) {
491 ret = -1;
492 goto end;
493 }
494
00e2e675
DG
495 lttng_ht_lookup(consumer_data.relayd_ht,
496 (void *)((unsigned long) relayd->net_seq_idx), &iter);
497 node = lttng_ht_iter_get_node_ulong(&iter);
498 if (node != NULL) {
00e2e675
DG
499 /* Relayd already exist. Ignore the insertion */
500 goto end;
501 }
502 lttng_ht_add_unique_ulong(consumer_data.relayd_ht, &relayd->node);
503
00e2e675
DG
504end:
505 return ret;
506}
507
508/*
509 * Allocate and return a consumer relayd socket.
510 */
511struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
512 int net_seq_idx)
513{
514 struct consumer_relayd_sock_pair *obj = NULL;
515
516 /* Negative net sequence index is a failure */
517 if (net_seq_idx < 0) {
518 goto error;
519 }
520
521 obj = zmalloc(sizeof(struct consumer_relayd_sock_pair));
522 if (obj == NULL) {
523 PERROR("zmalloc relayd sock");
524 goto error;
525 }
526
527 obj->net_seq_idx = net_seq_idx;
528 obj->refcount = 0;
173af62f 529 obj->destroy_flag = 0;
00e2e675
DG
530 lttng_ht_node_init_ulong(&obj->node, obj->net_seq_idx);
531 pthread_mutex_init(&obj->ctrl_sock_mutex, NULL);
532
533error:
534 return obj;
535}
536
537/*
538 * Find a relayd socket pair in the global consumer data.
539 *
540 * Return the object if found else NULL.
b0b335c8
MD
541 * RCU read-side lock must be held across this call and while using the
542 * returned object.
00e2e675
DG
543 */
544struct consumer_relayd_sock_pair *consumer_find_relayd(int key)
545{
546 struct lttng_ht_iter iter;
547 struct lttng_ht_node_ulong *node;
548 struct consumer_relayd_sock_pair *relayd = NULL;
549
550 /* Negative keys are lookup failures */
551 if (key < 0) {
552 goto error;
553 }
554
00e2e675
DG
555 lttng_ht_lookup(consumer_data.relayd_ht, (void *)((unsigned long) key),
556 &iter);
557 node = lttng_ht_iter_get_node_ulong(&iter);
558 if (node != NULL) {
559 relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node);
560 }
561
00e2e675
DG
562error:
563 return relayd;
564}
565
566/*
567 * Handle stream for relayd transmission if the stream applies for network
568 * streaming where the net sequence index is set.
569 *
570 * Return destination file descriptor or negative value on error.
571 */
6197aea7 572static int write_relayd_stream_header(struct lttng_consumer_stream *stream,
1d4dfdef
DG
573 size_t data_size, unsigned long padding,
574 struct consumer_relayd_sock_pair *relayd)
00e2e675
DG
575{
576 int outfd = -1, ret;
00e2e675
DG
577 struct lttcomm_relayd_data_hdr data_hdr;
578
579 /* Safety net */
580 assert(stream);
6197aea7 581 assert(relayd);
00e2e675
DG
582
583 /* Reset data header */
584 memset(&data_hdr, 0, sizeof(data_hdr));
585
00e2e675
DG
586 if (stream->metadata_flag) {
587 /* Caller MUST acquire the relayd control socket lock */
588 ret = relayd_send_metadata(&relayd->control_sock, data_size);
589 if (ret < 0) {
590 goto error;
591 }
592
593 /* Metadata are always sent on the control socket. */
594 outfd = relayd->control_sock.fd;
595 } else {
596 /* Set header with stream information */
597 data_hdr.stream_id = htobe64(stream->relayd_stream_id);
598 data_hdr.data_size = htobe32(data_size);
1d4dfdef 599 data_hdr.padding_size = htobe32(padding);
173af62f 600 data_hdr.net_seq_num = htobe64(stream->next_net_seq_num++);
00e2e675
DG
601 /* Other fields are zeroed previously */
602
603 ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr,
604 sizeof(data_hdr));
605 if (ret < 0) {
606 goto error;
607 }
608
609 /* Set to go on data socket */
610 outfd = relayd->data_sock.fd;
611 }
612
613error:
614 return outfd;
615}
616
3bd1e081
MD
617/*
618 * Update a stream according to what we just received.
619 */
620void consumer_change_stream_state(int stream_key,
621 enum lttng_consumer_stream_state state)
622{
623 struct lttng_consumer_stream *stream;
624
625 pthread_mutex_lock(&consumer_data.lock);
8389e4f8 626 stream = consumer_find_stream(stream_key, consumer_data.stream_ht);
3bd1e081
MD
627 if (stream) {
628 stream->state = state;
629 }
630 consumer_data.need_update = 1;
631 pthread_mutex_unlock(&consumer_data.lock);
632}
633
702b1ea4
MD
634static
635void consumer_free_channel(struct rcu_head *head)
636{
637 struct lttng_ht_node_ulong *node =
638 caa_container_of(head, struct lttng_ht_node_ulong, head);
639 struct lttng_consumer_channel *channel =
640 caa_container_of(node, struct lttng_consumer_channel, node);
641
642 free(channel);
643}
644
3bd1e081
MD
645/*
646 * Remove a channel from the global list protected by a mutex. This
647 * function is also responsible for freeing its data structures.
648 */
649void consumer_del_channel(struct lttng_consumer_channel *channel)
650{
651 int ret;
e4421fec 652 struct lttng_ht_iter iter;
3bd1e081
MD
653
654 pthread_mutex_lock(&consumer_data.lock);
655
656 switch (consumer_data.type) {
657 case LTTNG_CONSUMER_KERNEL:
658 break;
7753dea8
MD
659 case LTTNG_CONSUMER32_UST:
660 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
661 lttng_ustconsumer_del_channel(channel);
662 break;
663 default:
664 ERR("Unknown consumer_data type");
665 assert(0);
666 goto end;
667 }
668
6065ceec 669 rcu_read_lock();
04253271
MD
670 iter.iter.node = &channel->node.node;
671 ret = lttng_ht_del(consumer_data.channel_ht, &iter);
672 assert(!ret);
6065ceec
DG
673 rcu_read_unlock();
674
3bd1e081
MD
675 if (channel->mmap_base != NULL) {
676 ret = munmap(channel->mmap_base, channel->mmap_len);
677 if (ret != 0) {
7a57cf92 678 PERROR("munmap");
3bd1e081
MD
679 }
680 }
b5c5fc29 681 if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) {
4c462e79
MD
682 ret = close(channel->wait_fd);
683 if (ret) {
684 PERROR("close");
685 }
3bd1e081 686 }
2c1dd183 687 if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) {
4c462e79
MD
688 ret = close(channel->shm_fd);
689 if (ret) {
690 PERROR("close");
691 }
3bd1e081 692 }
702b1ea4
MD
693
694 call_rcu(&channel->node.head, consumer_free_channel);
3bd1e081
MD
695end:
696 pthread_mutex_unlock(&consumer_data.lock);
697}
698
699struct lttng_consumer_channel *consumer_allocate_channel(
700 int channel_key,
701 int shm_fd, int wait_fd,
702 uint64_t mmap_len,
c30aaa51
MD
703 uint64_t max_sb_size,
704 unsigned int nb_init_streams)
3bd1e081
MD
705{
706 struct lttng_consumer_channel *channel;
707 int ret;
708
276b26d1 709 channel = zmalloc(sizeof(*channel));
3bd1e081 710 if (channel == NULL) {
7a57cf92 711 PERROR("malloc struct lttng_consumer_channel");
3bd1e081
MD
712 goto end;
713 }
714 channel->key = channel_key;
715 channel->shm_fd = shm_fd;
716 channel->wait_fd = wait_fd;
717 channel->mmap_len = mmap_len;
718 channel->max_sb_size = max_sb_size;
719 channel->refcount = 0;
c30aaa51 720 channel->nb_init_streams = nb_init_streams;
e4421fec 721 lttng_ht_node_init_ulong(&channel->node, channel->key);
3bd1e081
MD
722
723 switch (consumer_data.type) {
724 case LTTNG_CONSUMER_KERNEL:
725 channel->mmap_base = NULL;
726 channel->mmap_len = 0;
727 break;
7753dea8
MD
728 case LTTNG_CONSUMER32_UST:
729 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
730 ret = lttng_ustconsumer_allocate_channel(channel);
731 if (ret) {
732 free(channel);
733 return NULL;
734 }
735 break;
736 default:
737 ERR("Unknown consumer_data type");
738 assert(0);
739 goto end;
740 }
741 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
00e2e675 742 channel->key, channel->shm_fd, channel->wait_fd,
3bd1e081
MD
743 (unsigned long long) channel->mmap_len,
744 (unsigned long long) channel->max_sb_size);
745end:
746 return channel;
747}
748
749/*
750 * Add a channel to the global list protected by a mutex.
751 */
752int consumer_add_channel(struct lttng_consumer_channel *channel)
753{
c77fc10a
DG
754 struct lttng_ht_node_ulong *node;
755 struct lttng_ht_iter iter;
756
3bd1e081 757 pthread_mutex_lock(&consumer_data.lock);
7ad0a0cb
MD
758 /* Steal channel identifier, for UST */
759 consumer_steal_channel_key(channel->key);
6065ceec 760 rcu_read_lock();
c77fc10a
DG
761
762 lttng_ht_lookup(consumer_data.channel_ht,
763 (void *)((unsigned long) channel->key), &iter);
764 node = lttng_ht_iter_get_node_ulong(&iter);
765 if (node != NULL) {
766 /* Channel already exist. Ignore the insertion */
767 goto end;
768 }
769
04253271 770 lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node);
c77fc10a
DG
771
772end:
6065ceec 773 rcu_read_unlock();
3bd1e081 774 pthread_mutex_unlock(&consumer_data.lock);
702b1ea4 775
7ad0a0cb 776 return 0;
3bd1e081
MD
777}
778
779/*
780 * Allocate the pollfd structure and the local view of the out fds to avoid
781 * doing a lookup in the linked list and concurrency issues when writing is
782 * needed. Called with consumer_data.lock held.
783 *
784 * Returns the number of fds in the structures.
785 */
786int consumer_update_poll_array(
787 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
fb3a43a9 788 struct lttng_consumer_stream **local_stream)
3bd1e081 789{
3bd1e081 790 int i = 0;
e4421fec
DG
791 struct lttng_ht_iter iter;
792 struct lttng_consumer_stream *stream;
3bd1e081
MD
793
794 DBG("Updating poll fd array");
481d6c57 795 rcu_read_lock();
e4421fec
DG
796 cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, stream,
797 node.node) {
798 if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM) {
3bd1e081
MD
799 continue;
800 }
e4421fec
DG
801 DBG("Active FD %d", stream->wait_fd);
802 (*pollfd)[i].fd = stream->wait_fd;
3bd1e081 803 (*pollfd)[i].events = POLLIN | POLLPRI;
e4421fec 804 local_stream[i] = stream;
3bd1e081
MD
805 i++;
806 }
481d6c57 807 rcu_read_unlock();
3bd1e081
MD
808
809 /*
810 * Insert the consumer_poll_pipe at the end of the array and don't
811 * increment i so nb_fd is the number of real FD.
812 */
813 (*pollfd)[i].fd = ctx->consumer_poll_pipe[0];
509bb1cf 814 (*pollfd)[i].events = POLLIN | POLLPRI;
3bd1e081
MD
815 return i;
816}
817
818/*
819 * Poll on the should_quit pipe and the command socket return -1 on error and
820 * should exit, 0 if data is available on the command socket
821 */
822int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
823{
824 int num_rdy;
825
88f2b785 826restart:
3bd1e081
MD
827 num_rdy = poll(consumer_sockpoll, 2, -1);
828 if (num_rdy == -1) {
88f2b785
MD
829 /*
830 * Restart interrupted system call.
831 */
832 if (errno == EINTR) {
833 goto restart;
834 }
7a57cf92 835 PERROR("Poll error");
3bd1e081
MD
836 goto exit;
837 }
509bb1cf 838 if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) {
3bd1e081
MD
839 DBG("consumer_should_quit wake up");
840 goto exit;
841 }
842 return 0;
843
844exit:
845 return -1;
846}
847
848/*
849 * Set the error socket.
850 */
851void lttng_consumer_set_error_sock(
852 struct lttng_consumer_local_data *ctx, int sock)
853{
854 ctx->consumer_error_socket = sock;
855}
856
857/*
858 * Set the command socket path.
859 */
3bd1e081
MD
860void lttng_consumer_set_command_sock_path(
861 struct lttng_consumer_local_data *ctx, char *sock)
862{
863 ctx->consumer_command_sock_path = sock;
864}
865
866/*
867 * Send return code to the session daemon.
868 * If the socket is not defined, we return 0, it is not a fatal error
869 */
870int lttng_consumer_send_error(
871 struct lttng_consumer_local_data *ctx, int cmd)
872{
873 if (ctx->consumer_error_socket > 0) {
874 return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
875 sizeof(enum lttcomm_sessiond_command));
876 }
877
878 return 0;
879}
880
881/*
882 * Close all the tracefiles and stream fds, should be called when all instances
883 * are destroyed.
884 */
885void lttng_consumer_cleanup(void)
886{
e4421fec 887 struct lttng_ht_iter iter;
6065ceec
DG
888 struct lttng_ht_node_ulong *node;
889
890 rcu_read_lock();
3bd1e081
MD
891
892 /*
6065ceec
DG
893 * close all outfd. Called when there are no more threads running (after
894 * joining on the threads), no need to protect list iteration with mutex.
3bd1e081 895 */
6065ceec
DG
896 cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, node,
897 node) {
702b1ea4
MD
898 struct lttng_consumer_stream *stream =
899 caa_container_of(node, struct lttng_consumer_stream, node);
e316aad5 900 consumer_del_stream(stream, consumer_data.stream_ht);
3bd1e081 901 }
e4421fec 902
6065ceec
DG
903 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node,
904 node) {
702b1ea4
MD
905 struct lttng_consumer_channel *channel =
906 caa_container_of(node, struct lttng_consumer_channel, node);
907 consumer_del_channel(channel);
3bd1e081 908 }
6065ceec
DG
909
910 rcu_read_unlock();
d6ce1df2
MD
911
912 lttng_ht_destroy(consumer_data.stream_ht);
913 lttng_ht_destroy(consumer_data.channel_ht);
3bd1e081
MD
914}
915
916/*
917 * Called from signal handler.
918 */
919void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
920{
921 int ret;
922 consumer_quit = 1;
6f94560a
MD
923 do {
924 ret = write(ctx->consumer_should_quit[1], "4", 1);
925 } while (ret < 0 && errno == EINTR);
3bd1e081 926 if (ret < 0) {
7a57cf92 927 PERROR("write consumer quit");
3bd1e081
MD
928 }
929}
930
00e2e675
DG
931void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
932 off_t orig_offset)
3bd1e081
MD
933{
934 int outfd = stream->out_fd;
935
936 /*
937 * This does a blocking write-and-wait on any page that belongs to the
938 * subbuffer prior to the one we just wrote.
939 * Don't care about error values, as these are just hints and ways to
940 * limit the amount of page cache used.
941 */
942 if (orig_offset < stream->chan->max_sb_size) {
943 return;
944 }
b9182dd9 945 lttng_sync_file_range(outfd, orig_offset - stream->chan->max_sb_size,
3bd1e081
MD
946 stream->chan->max_sb_size,
947 SYNC_FILE_RANGE_WAIT_BEFORE
948 | SYNC_FILE_RANGE_WRITE
949 | SYNC_FILE_RANGE_WAIT_AFTER);
950 /*
951 * Give hints to the kernel about how we access the file:
952 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
953 * we write it.
954 *
955 * We need to call fadvise again after the file grows because the
956 * kernel does not seem to apply fadvise to non-existing parts of the
957 * file.
958 *
959 * Call fadvise _after_ having waited for the page writeback to
960 * complete because the dirty page writeback semantic is not well
961 * defined. So it can be expected to lead to lower throughput in
962 * streaming.
963 */
964 posix_fadvise(outfd, orig_offset - stream->chan->max_sb_size,
965 stream->chan->max_sb_size, POSIX_FADV_DONTNEED);
966}
967
968/*
969 * Initialise the necessary environnement :
970 * - create a new context
971 * - create the poll_pipe
972 * - create the should_quit pipe (for signal handler)
973 * - create the thread pipe (for splice)
974 *
975 * Takes a function pointer as argument, this function is called when data is
976 * available on a buffer. This function is responsible to do the
977 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
978 * buffer configuration and then kernctl_put_next_subbuf at the end.
979 *
980 * Returns a pointer to the new context or NULL on error.
981 */
982struct lttng_consumer_local_data *lttng_consumer_create(
983 enum lttng_consumer_type type,
4078b776 984 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
d41f73b7 985 struct lttng_consumer_local_data *ctx),
3bd1e081
MD
986 int (*recv_channel)(struct lttng_consumer_channel *channel),
987 int (*recv_stream)(struct lttng_consumer_stream *stream),
988 int (*update_stream)(int stream_key, uint32_t state))
989{
990 int ret, i;
991 struct lttng_consumer_local_data *ctx;
992
993 assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
994 consumer_data.type == type);
995 consumer_data.type = type;
996
effcf122 997 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
3bd1e081 998 if (ctx == NULL) {
7a57cf92 999 PERROR("allocating context");
3bd1e081
MD
1000 goto error;
1001 }
1002
1003 ctx->consumer_error_socket = -1;
1004 /* assign the callbacks */
1005 ctx->on_buffer_ready = buffer_ready;
1006 ctx->on_recv_channel = recv_channel;
1007 ctx->on_recv_stream = recv_stream;
1008 ctx->on_update_stream = update_stream;
1009
1010 ret = pipe(ctx->consumer_poll_pipe);
1011 if (ret < 0) {
7a57cf92 1012 PERROR("Error creating poll pipe");
3bd1e081
MD
1013 goto error_poll_pipe;
1014 }
1015
04fdd819
MD
1016 /* set read end of the pipe to non-blocking */
1017 ret = fcntl(ctx->consumer_poll_pipe[0], F_SETFL, O_NONBLOCK);
1018 if (ret < 0) {
7a57cf92 1019 PERROR("fcntl O_NONBLOCK");
04fdd819
MD
1020 goto error_poll_fcntl;
1021 }
1022
1023 /* set write end of the pipe to non-blocking */
1024 ret = fcntl(ctx->consumer_poll_pipe[1], F_SETFL, O_NONBLOCK);
1025 if (ret < 0) {
7a57cf92 1026 PERROR("fcntl O_NONBLOCK");
04fdd819
MD
1027 goto error_poll_fcntl;
1028 }
1029
3bd1e081
MD
1030 ret = pipe(ctx->consumer_should_quit);
1031 if (ret < 0) {
7a57cf92 1032 PERROR("Error creating recv pipe");
3bd1e081
MD
1033 goto error_quit_pipe;
1034 }
1035
1036 ret = pipe(ctx->consumer_thread_pipe);
1037 if (ret < 0) {
7a57cf92 1038 PERROR("Error creating thread pipe");
3bd1e081
MD
1039 goto error_thread_pipe;
1040 }
1041
fb3a43a9
DG
1042 ret = utils_create_pipe(ctx->consumer_metadata_pipe);
1043 if (ret < 0) {
1044 goto error_metadata_pipe;
1045 }
3bd1e081 1046
fb3a43a9
DG
1047 ret = utils_create_pipe(ctx->consumer_splice_metadata_pipe);
1048 if (ret < 0) {
1049 goto error_splice_pipe;
1050 }
1051
1052 return ctx;
3bd1e081 1053
fb3a43a9
DG
1054error_splice_pipe:
1055 utils_close_pipe(ctx->consumer_metadata_pipe);
1056error_metadata_pipe:
1057 utils_close_pipe(ctx->consumer_thread_pipe);
3bd1e081
MD
1058error_thread_pipe:
1059 for (i = 0; i < 2; i++) {
1060 int err;
1061
1062 err = close(ctx->consumer_should_quit[i]);
4c462e79
MD
1063 if (err) {
1064 PERROR("close");
1065 }
3bd1e081 1066 }
04fdd819 1067error_poll_fcntl:
3bd1e081
MD
1068error_quit_pipe:
1069 for (i = 0; i < 2; i++) {
1070 int err;
1071
1072 err = close(ctx->consumer_poll_pipe[i]);
4c462e79
MD
1073 if (err) {
1074 PERROR("close");
1075 }
3bd1e081
MD
1076 }
1077error_poll_pipe:
1078 free(ctx);
1079error:
1080 return NULL;
1081}
1082
1083/*
1084 * Close all fds associated with the instance and free the context.
1085 */
1086void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
1087{
4c462e79
MD
1088 int ret;
1089
1090 ret = close(ctx->consumer_error_socket);
1091 if (ret) {
1092 PERROR("close");
1093 }
1094 ret = close(ctx->consumer_thread_pipe[0]);
1095 if (ret) {
1096 PERROR("close");
1097 }
1098 ret = close(ctx->consumer_thread_pipe[1]);
1099 if (ret) {
1100 PERROR("close");
1101 }
1102 ret = close(ctx->consumer_poll_pipe[0]);
1103 if (ret) {
1104 PERROR("close");
1105 }
1106 ret = close(ctx->consumer_poll_pipe[1]);
1107 if (ret) {
1108 PERROR("close");
1109 }
1110 ret = close(ctx->consumer_should_quit[0]);
1111 if (ret) {
1112 PERROR("close");
1113 }
1114 ret = close(ctx->consumer_should_quit[1]);
1115 if (ret) {
1116 PERROR("close");
1117 }
fb3a43a9
DG
1118 utils_close_pipe(ctx->consumer_splice_metadata_pipe);
1119
3bd1e081
MD
1120 unlink(ctx->consumer_command_sock_path);
1121 free(ctx);
1122}
1123
6197aea7
DG
1124/*
1125 * Write the metadata stream id on the specified file descriptor.
1126 */
1127static int write_relayd_metadata_id(int fd,
1128 struct lttng_consumer_stream *stream,
1d4dfdef
DG
1129 struct consumer_relayd_sock_pair *relayd,
1130 unsigned long padding)
6197aea7
DG
1131{
1132 int ret;
1d4dfdef 1133 struct lttcomm_relayd_metadata_payload hdr;
6197aea7 1134
1d4dfdef
DG
1135 hdr.stream_id = htobe64(stream->relayd_stream_id);
1136 hdr.padding_size = htobe32(padding);
6197aea7 1137 do {
1d4dfdef 1138 ret = write(fd, (void *) &hdr, sizeof(hdr));
6197aea7
DG
1139 } while (ret < 0 && errno == EINTR);
1140 if (ret < 0) {
1141 PERROR("write metadata stream id");
1142 goto end;
1143 }
1d4dfdef
DG
1144 DBG("Metadata stream id %" PRIu64 " with padding %lu written before data",
1145 stream->relayd_stream_id, padding);
6197aea7
DG
1146
1147end:
1148 return ret;
1149}
1150
3bd1e081 1151/*
09e26845
DG
1152 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1153 * core function for writing trace buffers to either the local filesystem or
1154 * the network.
1155 *
1156 * Careful review MUST be put if any changes occur!
3bd1e081
MD
1157 *
1158 * Returns the number of bytes written
1159 */
4078b776 1160ssize_t lttng_consumer_on_read_subbuffer_mmap(
3bd1e081 1161 struct lttng_consumer_local_data *ctx,
1d4dfdef
DG
1162 struct lttng_consumer_stream *stream, unsigned long len,
1163 unsigned long padding)
3bd1e081 1164{
f02e1e8a
DG
1165 unsigned long mmap_offset;
1166 ssize_t ret = 0, written = 0;
1167 off_t orig_offset = stream->out_fd_offset;
1168 /* Default is on the disk */
1169 int outfd = stream->out_fd;
f02e1e8a
DG
1170 struct consumer_relayd_sock_pair *relayd = NULL;
1171
1172 /* RCU lock for the relayd pointer */
1173 rcu_read_lock();
1174
1175 /* Flag that the current stream if set for network streaming. */
1176 if (stream->net_seq_idx != -1) {
1177 relayd = consumer_find_relayd(stream->net_seq_idx);
1178 if (relayd == NULL) {
1179 goto end;
1180 }
1181 }
1182
1183 /* get the offset inside the fd to mmap */
3bd1e081
MD
1184 switch (consumer_data.type) {
1185 case LTTNG_CONSUMER_KERNEL:
f02e1e8a
DG
1186 ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset);
1187 break;
7753dea8
MD
1188 case LTTNG_CONSUMER32_UST:
1189 case LTTNG_CONSUMER64_UST:
f02e1e8a
DG
1190 ret = lttng_ustctl_get_mmap_read_offset(stream->chan->handle,
1191 stream->buf, &mmap_offset);
1192 break;
3bd1e081
MD
1193 default:
1194 ERR("Unknown consumer_data type");
1195 assert(0);
1196 }
f02e1e8a
DG
1197 if (ret != 0) {
1198 errno = -ret;
1199 PERROR("tracer ctl get_mmap_read_offset");
1200 written = ret;
1201 goto end;
1202 }
b9182dd9 1203
f02e1e8a
DG
1204 /* Handle stream on the relayd if the output is on the network */
1205 if (relayd) {
1206 unsigned long netlen = len;
1207
1208 /*
1209 * Lock the control socket for the complete duration of the function
1210 * since from this point on we will use the socket.
1211 */
1212 if (stream->metadata_flag) {
1213 /* Metadata requires the control socket. */
1214 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1d4dfdef 1215 netlen += sizeof(struct lttcomm_relayd_metadata_payload);
f02e1e8a
DG
1216 }
1217
1d4dfdef 1218 ret = write_relayd_stream_header(stream, netlen, padding, relayd);
f02e1e8a
DG
1219 if (ret >= 0) {
1220 /* Use the returned socket. */
1221 outfd = ret;
1222
1223 /* Write metadata stream id before payload */
1224 if (stream->metadata_flag) {
1d4dfdef 1225 ret = write_relayd_metadata_id(outfd, stream, relayd, padding);
f02e1e8a 1226 if (ret < 0) {
f02e1e8a
DG
1227 written = ret;
1228 goto end;
1229 }
f02e1e8a
DG
1230 }
1231 }
1232 /* Else, use the default set before which is the filesystem. */
1d4dfdef
DG
1233 } else {
1234 /* No streaming, we have to set the len with the full padding */
1235 len += padding;
f02e1e8a
DG
1236 }
1237
1238 while (len > 0) {
1239 do {
1240 ret = write(outfd, stream->mmap_base + mmap_offset, len);
1241 } while (ret < 0 && errno == EINTR);
1d4dfdef 1242 DBG("Consumer mmap write() ret %zd (len %lu)", ret, len);
f02e1e8a
DG
1243 if (ret < 0) {
1244 PERROR("Error in file write");
1245 if (written == 0) {
1246 written = ret;
1247 }
1248 goto end;
1249 } else if (ret > len) {
77c7c900 1250 PERROR("Error in file write (ret %zd > len %lu)", ret, len);
f02e1e8a
DG
1251 written += ret;
1252 goto end;
1253 } else {
1254 len -= ret;
1255 mmap_offset += ret;
1256 }
f02e1e8a
DG
1257
1258 /* This call is useless on a socket so better save a syscall. */
1259 if (!relayd) {
1260 /* This won't block, but will start writeout asynchronously */
1261 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
1262 SYNC_FILE_RANGE_WRITE);
1263 stream->out_fd_offset += ret;
1264 }
1265 written += ret;
1266 }
1267 lttng_consumer_sync_trace_file(stream, orig_offset);
1268
1269end:
1270 /* Unlock only if ctrl socket used */
1271 if (relayd && stream->metadata_flag) {
1272 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1273 }
1274
1275 rcu_read_unlock();
1276 return written;
3bd1e081
MD
1277}
1278
1279/*
1280 * Splice the data from the ring buffer to the tracefile.
1281 *
1282 * Returns the number of bytes spliced.
1283 */
4078b776 1284ssize_t lttng_consumer_on_read_subbuffer_splice(
3bd1e081 1285 struct lttng_consumer_local_data *ctx,
1d4dfdef
DG
1286 struct lttng_consumer_stream *stream, unsigned long len,
1287 unsigned long padding)
3bd1e081 1288{
f02e1e8a
DG
1289 ssize_t ret = 0, written = 0, ret_splice = 0;
1290 loff_t offset = 0;
1291 off_t orig_offset = stream->out_fd_offset;
1292 int fd = stream->wait_fd;
1293 /* Default is on the disk */
1294 int outfd = stream->out_fd;
f02e1e8a 1295 struct consumer_relayd_sock_pair *relayd = NULL;
fb3a43a9 1296 int *splice_pipe;
f02e1e8a 1297
3bd1e081
MD
1298 switch (consumer_data.type) {
1299 case LTTNG_CONSUMER_KERNEL:
f02e1e8a 1300 break;
7753dea8
MD
1301 case LTTNG_CONSUMER32_UST:
1302 case LTTNG_CONSUMER64_UST:
f02e1e8a 1303 /* Not supported for user space tracing */
3bd1e081
MD
1304 return -ENOSYS;
1305 default:
1306 ERR("Unknown consumer_data type");
1307 assert(0);
3bd1e081
MD
1308 }
1309
f02e1e8a
DG
1310 /* RCU lock for the relayd pointer */
1311 rcu_read_lock();
1312
1313 /* Flag that the current stream if set for network streaming. */
1314 if (stream->net_seq_idx != -1) {
1315 relayd = consumer_find_relayd(stream->net_seq_idx);
1316 if (relayd == NULL) {
1317 goto end;
1318 }
1319 }
1320
fb3a43a9
DG
1321 /*
1322 * Choose right pipe for splice. Metadata and trace data are handled by
1323 * different threads hence the use of two pipes in order not to race or
1324 * corrupt the written data.
1325 */
1326 if (stream->metadata_flag) {
1327 splice_pipe = ctx->consumer_splice_metadata_pipe;
1328 } else {
1329 splice_pipe = ctx->consumer_thread_pipe;
1330 }
1331
f02e1e8a 1332 /* Write metadata stream id before payload */
1d4dfdef
DG
1333 if (relayd) {
1334 int total_len = len;
f02e1e8a 1335
1d4dfdef
DG
1336 if (stream->metadata_flag) {
1337 /*
1338 * Lock the control socket for the complete duration of the function
1339 * since from this point on we will use the socket.
1340 */
1341 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1342
1343 ret = write_relayd_metadata_id(splice_pipe[1], stream, relayd,
1344 padding);
1345 if (ret < 0) {
1346 written = ret;
1347 goto end;
1348 }
1349
1350 total_len += sizeof(struct lttcomm_relayd_metadata_payload);
1351 }
1352
1353 ret = write_relayd_stream_header(stream, total_len, padding, relayd);
1354 if (ret >= 0) {
1355 /* Use the returned socket. */
1356 outfd = ret;
1357 } else {
1358 ERR("Remote relayd disconnected. Stopping");
f02e1e8a
DG
1359 goto end;
1360 }
1d4dfdef
DG
1361 } else {
1362 /* No streaming, we have to set the len with the full padding */
1363 len += padding;
f02e1e8a
DG
1364 }
1365
1366 while (len > 0) {
1d4dfdef
DG
1367 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1368 (unsigned long)offset, len, fd, splice_pipe[1]);
fb3a43a9 1369 ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len,
f02e1e8a
DG
1370 SPLICE_F_MOVE | SPLICE_F_MORE);
1371 DBG("splice chan to pipe, ret %zd", ret_splice);
1372 if (ret_splice < 0) {
1373 PERROR("Error in relay splice");
1374 if (written == 0) {
1375 written = ret_splice;
1376 }
1377 ret = errno;
1378 goto splice_error;
1379 }
1380
1381 /* Handle stream on the relayd if the output is on the network */
1382 if (relayd) {
1383 if (stream->metadata_flag) {
1d4dfdef
DG
1384 size_t metadata_payload_size =
1385 sizeof(struct lttcomm_relayd_metadata_payload);
1386
f02e1e8a 1387 /* Update counter to fit the spliced data */
1d4dfdef
DG
1388 ret_splice += metadata_payload_size;
1389 len += metadata_payload_size;
f02e1e8a
DG
1390 /*
1391 * We do this so the return value can match the len passed as
1392 * argument to this function.
1393 */
1d4dfdef 1394 written -= metadata_payload_size;
f02e1e8a
DG
1395 }
1396 }
1397
1398 /* Splice data out */
fb3a43a9 1399 ret_splice = splice(splice_pipe[0], NULL, outfd, NULL,
f02e1e8a 1400 ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE);
1d4dfdef 1401 DBG("Consumer splice pipe to file, ret %zd", ret_splice);
f02e1e8a
DG
1402 if (ret_splice < 0) {
1403 PERROR("Error in file splice");
1404 if (written == 0) {
1405 written = ret_splice;
1406 }
1407 ret = errno;
1408 goto splice_error;
1409 } else if (ret_splice > len) {
1410 errno = EINVAL;
1411 PERROR("Wrote more data than requested %zd (len: %lu)",
1412 ret_splice, len);
1413 written += ret_splice;
1414 ret = errno;
1415 goto splice_error;
1416 }
1417 len -= ret_splice;
1418
1419 /* This call is useless on a socket so better save a syscall. */
1420 if (!relayd) {
1421 /* This won't block, but will start writeout asynchronously */
1422 lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice,
1423 SYNC_FILE_RANGE_WRITE);
1424 stream->out_fd_offset += ret_splice;
1425 }
1426 written += ret_splice;
1427 }
1428 lttng_consumer_sync_trace_file(stream, orig_offset);
1429
1430 ret = ret_splice;
1431
1432 goto end;
1433
1434splice_error:
1435 /* send the appropriate error description to sessiond */
1436 switch (ret) {
1437 case EBADF:
f73fabfd 1438 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EBADF);
f02e1e8a
DG
1439 break;
1440 case EINVAL:
f73fabfd 1441 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL);
f02e1e8a
DG
1442 break;
1443 case ENOMEM:
f73fabfd 1444 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM);
f02e1e8a
DG
1445 break;
1446 case ESPIPE:
f73fabfd 1447 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE);
f02e1e8a
DG
1448 break;
1449 }
1450
1451end:
1452 if (relayd && stream->metadata_flag) {
1453 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1454 }
1455
1456 rcu_read_unlock();
1457 return written;
3bd1e081
MD
1458}
1459
1460/*
1461 * Take a snapshot for a specific fd
1462 *
1463 * Returns 0 on success, < 0 on error
1464 */
1465int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
1466 struct lttng_consumer_stream *stream)
1467{
1468 switch (consumer_data.type) {
1469 case LTTNG_CONSUMER_KERNEL:
1470 return lttng_kconsumer_take_snapshot(ctx, stream);
7753dea8
MD
1471 case LTTNG_CONSUMER32_UST:
1472 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1473 return lttng_ustconsumer_take_snapshot(ctx, stream);
1474 default:
1475 ERR("Unknown consumer_data type");
1476 assert(0);
1477 return -ENOSYS;
1478 }
1479
1480}
1481
1482/*
1483 * Get the produced position
1484 *
1485 * Returns 0 on success, < 0 on error
1486 */
1487int lttng_consumer_get_produced_snapshot(
1488 struct lttng_consumer_local_data *ctx,
1489 struct lttng_consumer_stream *stream,
1490 unsigned long *pos)
1491{
1492 switch (consumer_data.type) {
1493 case LTTNG_CONSUMER_KERNEL:
1494 return lttng_kconsumer_get_produced_snapshot(ctx, stream, pos);
7753dea8
MD
1495 case LTTNG_CONSUMER32_UST:
1496 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1497 return lttng_ustconsumer_get_produced_snapshot(ctx, stream, pos);
1498 default:
1499 ERR("Unknown consumer_data type");
1500 assert(0);
1501 return -ENOSYS;
1502 }
1503}
1504
1505int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1506 int sock, struct pollfd *consumer_sockpoll)
1507{
1508 switch (consumer_data.type) {
1509 case LTTNG_CONSUMER_KERNEL:
1510 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
7753dea8
MD
1511 case LTTNG_CONSUMER32_UST:
1512 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1513 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1514 default:
1515 ERR("Unknown consumer_data type");
1516 assert(0);
1517 return -ENOSYS;
1518 }
1519}
1520
fb3a43a9 1521/*
f724d81e 1522 * Iterate over all streams of the hashtable and free them properly.
e316aad5
DG
1523 *
1524 * XXX: Should not be only for metadata stream or else use an other name.
fb3a43a9
DG
1525 */
1526static void destroy_stream_ht(struct lttng_ht *ht)
1527{
1528 int ret;
1529 struct lttng_ht_iter iter;
1530 struct lttng_consumer_stream *stream;
1531
1532 if (ht == NULL) {
1533 return;
1534 }
1535
d09e1200 1536 rcu_read_lock();
e316aad5 1537 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, waitfd_node.node) {
fb3a43a9
DG
1538 ret = lttng_ht_del(ht, &iter);
1539 assert(!ret);
1540
e316aad5 1541 call_rcu(&stream->waitfd_node.head, consumer_free_metadata_stream);
fb3a43a9 1542 }
d09e1200 1543 rcu_read_unlock();
fb3a43a9
DG
1544
1545 lttng_ht_destroy(ht);
1546}
1547
1548/*
1549 * Clean up a metadata stream and free its memory.
1550 */
e316aad5
DG
1551void consumer_del_metadata_stream(struct lttng_consumer_stream *stream,
1552 struct lttng_ht *ht)
fb3a43a9
DG
1553{
1554 int ret;
e316aad5
DG
1555 struct lttng_ht_iter iter;
1556 struct lttng_consumer_channel *free_chan = NULL;
fb3a43a9
DG
1557 struct consumer_relayd_sock_pair *relayd;
1558
1559 assert(stream);
1560 /*
1561 * This call should NEVER receive regular stream. It must always be
1562 * metadata stream and this is crucial for data structure synchronization.
1563 */
1564 assert(stream->metadata_flag);
1565
e316aad5
DG
1566 DBG3("Consumer delete metadata stream %d", stream->wait_fd);
1567
1568 if (ht == NULL) {
1569 /* Means the stream was allocated but not successfully added */
1570 goto free_stream;
1571 }
1572
fb3a43a9
DG
1573 pthread_mutex_lock(&consumer_data.lock);
1574 switch (consumer_data.type) {
1575 case LTTNG_CONSUMER_KERNEL:
1576 if (stream->mmap_base != NULL) {
1577 ret = munmap(stream->mmap_base, stream->mmap_len);
1578 if (ret != 0) {
1579 PERROR("munmap metadata stream");
1580 }
1581 }
1582 break;
1583 case LTTNG_CONSUMER32_UST:
1584 case LTTNG_CONSUMER64_UST:
1585 lttng_ustconsumer_del_stream(stream);
1586 break;
1587 default:
1588 ERR("Unknown consumer_data type");
1589 assert(0);
e316aad5 1590 goto end;
fb3a43a9 1591 }
fb3a43a9 1592
c869f647
DG
1593 rcu_read_lock();
1594 iter.iter.node = &stream->waitfd_node.node;
1595 ret = lttng_ht_del(ht, &iter);
1596 assert(!ret);
1597 rcu_read_unlock();
1598
fb3a43a9
DG
1599 if (stream->out_fd >= 0) {
1600 ret = close(stream->out_fd);
1601 if (ret) {
1602 PERROR("close");
1603 }
1604 }
1605
1606 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
1607 ret = close(stream->wait_fd);
1608 if (ret) {
1609 PERROR("close");
1610 }
1611 }
1612
1613 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
1614 ret = close(stream->shm_fd);
1615 if (ret) {
1616 PERROR("close");
1617 }
1618 }
1619
1620 /* Check and cleanup relayd */
1621 rcu_read_lock();
1622 relayd = consumer_find_relayd(stream->net_seq_idx);
1623 if (relayd != NULL) {
1624 uatomic_dec(&relayd->refcount);
1625 assert(uatomic_read(&relayd->refcount) >= 0);
1626
1627 /* Closing streams requires to lock the control socket. */
1628 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1629 ret = relayd_send_close_stream(&relayd->control_sock,
1630 stream->relayd_stream_id, stream->next_net_seq_num - 1);
1631 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1632 if (ret < 0) {
1633 DBG("Unable to close stream on the relayd. Continuing");
1634 /*
1635 * Continue here. There is nothing we can do for the relayd.
1636 * Chances are that the relayd has closed the socket so we just
1637 * continue cleaning up.
1638 */
1639 }
1640
1641 /* Both conditions are met, we destroy the relayd. */
1642 if (uatomic_read(&relayd->refcount) == 0 &&
1643 uatomic_read(&relayd->destroy_flag)) {
d09e1200 1644 destroy_relayd(relayd);
fb3a43a9
DG
1645 }
1646 }
1647 rcu_read_unlock();
1648
1649 /* Atomically decrement channel refcount since other threads can use it. */
1650 uatomic_dec(&stream->chan->refcount);
c30aaa51
MD
1651 if (!uatomic_read(&stream->chan->refcount)
1652 && !uatomic_read(&stream->chan->nb_init_streams)) {
1653 /* Go for channel deletion! */
e316aad5 1654 free_chan = stream->chan;
fb3a43a9
DG
1655 }
1656
e316aad5
DG
1657end:
1658 pthread_mutex_unlock(&consumer_data.lock);
1659
1660 if (free_chan) {
1661 consumer_del_channel(free_chan);
1662 }
1663
1664free_stream:
1665 call_rcu(&stream->waitfd_node.head, consumer_free_metadata_stream);
fb3a43a9
DG
1666}
1667
1668/*
1669 * Action done with the metadata stream when adding it to the consumer internal
1670 * data structures to handle it.
1671 */
e316aad5
DG
1672static int consumer_add_metadata_stream(struct lttng_consumer_stream *stream,
1673 struct lttng_ht *ht)
fb3a43a9 1674{
e316aad5 1675 int ret = 0;
fb3a43a9
DG
1676 struct consumer_relayd_sock_pair *relayd;
1677
e316aad5
DG
1678 assert(stream);
1679 assert(ht);
1680
1681 DBG3("Adding metadata stream %d to hash table", stream->wait_fd);
1682
1683 pthread_mutex_lock(&consumer_data.lock);
1684
e316aad5
DG
1685 /*
1686 * From here, refcounts are updated so be _careful_ when returning an error
1687 * after this point.
1688 */
1689
fb3a43a9 1690 rcu_read_lock();
e316aad5 1691 /* Find relayd and, if one is found, increment refcount. */
fb3a43a9
DG
1692 relayd = consumer_find_relayd(stream->net_seq_idx);
1693 if (relayd != NULL) {
1694 uatomic_inc(&relayd->refcount);
1695 }
e316aad5
DG
1696
1697 /* Update channel refcount once added without error(s). */
1698 uatomic_inc(&stream->chan->refcount);
1699
1700 /*
1701 * When nb_init_streams reaches 0, we don't need to trigger any action in
1702 * terms of destroying the associated channel, because the action that
1703 * causes the count to become 0 also causes a stream to be added. The
1704 * channel deletion will thus be triggered by the following removal of this
1705 * stream.
1706 */
1707 if (uatomic_read(&stream->chan->nb_init_streams) > 0) {
1708 uatomic_dec(&stream->chan->nb_init_streams);
1709 }
1710
1711 lttng_ht_add_unique_ulong(ht, &stream->waitfd_node);
fb3a43a9 1712 rcu_read_unlock();
e316aad5 1713
e316aad5
DG
1714 pthread_mutex_unlock(&consumer_data.lock);
1715 return ret;
fb3a43a9
DG
1716}
1717
1718/*
1719 * Thread polls on metadata file descriptor and write them on disk or on the
1720 * network.
1721 */
7d980def 1722void *consumer_thread_metadata_poll(void *data)
fb3a43a9
DG
1723{
1724 int ret, i, pollfd;
1725 uint32_t revents, nb_fd;
e316aad5 1726 struct lttng_consumer_stream *stream = NULL;
fb3a43a9
DG
1727 struct lttng_ht_iter iter;
1728 struct lttng_ht_node_ulong *node;
1729 struct lttng_ht *metadata_ht = NULL;
1730 struct lttng_poll_event events;
1731 struct lttng_consumer_local_data *ctx = data;
1732 ssize_t len;
1733
1734 rcu_register_thread();
1735
1736 DBG("Thread metadata poll started");
1737
1738 metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1739 if (metadata_ht == NULL) {
1740 goto end;
1741 }
1742
1743 /* Size is set to 1 for the consumer_metadata pipe */
1744 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
1745 if (ret < 0) {
1746 ERR("Poll set creation failed");
1747 goto end;
1748 }
1749
1750 ret = lttng_poll_add(&events, ctx->consumer_metadata_pipe[0], LPOLLIN);
1751 if (ret < 0) {
1752 goto end;
1753 }
1754
1755 /* Main loop */
1756 DBG("Metadata main loop started");
1757
1758 while (1) {
1759 lttng_poll_reset(&events);
1760
1761 nb_fd = LTTNG_POLL_GETNB(&events);
1762
1763 /* Only the metadata pipe is set */
1764 if (nb_fd == 0 && consumer_quit == 1) {
1765 goto end;
1766 }
1767
1768restart:
1769 DBG("Metadata poll wait with %d fd(s)", nb_fd);
1770 ret = lttng_poll_wait(&events, -1);
1771 DBG("Metadata event catched in thread");
1772 if (ret < 0) {
1773 if (errno == EINTR) {
e316aad5 1774 ERR("Poll EINTR catched");
fb3a43a9
DG
1775 goto restart;
1776 }
1777 goto error;
1778 }
1779
e316aad5 1780 /* From here, the event is a metadata wait fd */
fb3a43a9
DG
1781 for (i = 0; i < nb_fd; i++) {
1782 revents = LTTNG_POLL_GETEV(&events, i);
1783 pollfd = LTTNG_POLL_GETFD(&events, i);
1784
e316aad5
DG
1785 /* Just don't waste time if no returned events for the fd */
1786 if (!revents) {
1787 continue;
1788 }
1789
fb3a43a9 1790 if (pollfd == ctx->consumer_metadata_pipe[0]) {
4adabd61 1791 if (revents & (LPOLLERR | LPOLLHUP )) {
fb3a43a9
DG
1792 DBG("Metadata thread pipe hung up");
1793 /*
1794 * Remove the pipe from the poll set and continue the loop
1795 * since their might be data to consume.
1796 */
1797 lttng_poll_del(&events, ctx->consumer_metadata_pipe[0]);
1798 close(ctx->consumer_metadata_pipe[0]);
1799 continue;
1800 } else if (revents & LPOLLIN) {
fb3a43a9 1801 do {
633d0084
DG
1802 /* Get the stream pointer received */
1803 ret = read(pollfd, &stream, sizeof(stream));
fb3a43a9 1804 } while (ret < 0 && errno == EINTR);
633d0084
DG
1805 if (ret < 0 ||
1806 ret < sizeof(struct lttng_consumer_stream *)) {
fb3a43a9 1807 PERROR("read metadata stream");
fb3a43a9
DG
1808 /*
1809 * Let's continue here and hope we can still work
1810 * without stopping the consumer. XXX: Should we?
1811 */
1812 continue;
1813 }
1814
1815 DBG("Adding metadata stream %d to poll set",
1816 stream->wait_fd);
1817
e316aad5
DG
1818 ret = consumer_add_metadata_stream(stream, metadata_ht);
1819 if (ret) {
1820 ERR("Unable to add metadata stream");
1821 /* Stream was not setup properly. Continuing. */
1822 consumer_del_metadata_stream(stream, NULL);
1823 continue;
1824 }
fb3a43a9
DG
1825
1826 /* Add metadata stream to the global poll events list */
1827 lttng_poll_add(&events, stream->wait_fd,
1828 LPOLLIN | LPOLLPRI);
fb3a43a9
DG
1829 }
1830
e316aad5 1831 /* Handle other stream */
fb3a43a9
DG
1832 continue;
1833 }
1834
d09e1200 1835 rcu_read_lock();
fb3a43a9
DG
1836 lttng_ht_lookup(metadata_ht, (void *)((unsigned long) pollfd),
1837 &iter);
1838 node = lttng_ht_iter_get_node_ulong(&iter);
e316aad5 1839 assert(node);
fb3a43a9
DG
1840
1841 stream = caa_container_of(node, struct lttng_consumer_stream,
1842 waitfd_node);
1843
e316aad5 1844 /* Check for error event */
4adabd61 1845 if (revents & (LPOLLERR | LPOLLHUP)) {
e316aad5 1846 DBG("Metadata fd %d is hup|err.", pollfd);
fb3a43a9
DG
1847 if (!stream->hangup_flush_done
1848 && (consumer_data.type == LTTNG_CONSUMER32_UST
1849 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
1850 DBG("Attempting to flush and consume the UST buffers");
1851 lttng_ustconsumer_on_stream_hangup(stream);
1852
1853 /* We just flushed the stream now read it. */
4bb94b75
DG
1854 do {
1855 len = ctx->on_buffer_ready(stream, ctx);
1856 /*
1857 * We don't check the return value here since if we get
1858 * a negative len, it means an error occured thus we
1859 * simply remove it from the poll set and free the
1860 * stream.
1861 */
1862 } while (len > 0);
fb3a43a9
DG
1863 }
1864
fb3a43a9 1865 lttng_poll_del(&events, stream->wait_fd);
e316aad5
DG
1866 /*
1867 * This call update the channel states, closes file descriptors
1868 * and securely free the stream.
1869 */
1870 consumer_del_metadata_stream(stream, metadata_ht);
1871 } else if (revents & (LPOLLIN | LPOLLPRI)) {
1872 /* Get the data out of the metadata file descriptor */
1873 DBG("Metadata available on fd %d", pollfd);
1874 assert(stream->wait_fd == pollfd);
1875
1876 len = ctx->on_buffer_ready(stream, ctx);
1877 /* It's ok to have an unavailable sub-buffer */
b64403e3 1878 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
e316aad5
DG
1879 rcu_read_unlock();
1880 goto end;
1881 } else if (len > 0) {
1882 stream->data_read = 1;
1883 }
fb3a43a9 1884 }
e316aad5
DG
1885
1886 /* Release RCU lock for the stream looked up */
d09e1200 1887 rcu_read_unlock();
fb3a43a9
DG
1888 }
1889 }
1890
1891error:
1892end:
1893 DBG("Metadata poll thread exiting");
1894 lttng_poll_clean(&events);
1895
1896 if (metadata_ht) {
1897 destroy_stream_ht(metadata_ht);
1898 }
1899
1900 rcu_unregister_thread();
1901 return NULL;
1902}
1903
3bd1e081 1904/*
e4421fec 1905 * This thread polls the fds in the set to consume the data and write
3bd1e081
MD
1906 * it to tracefile if necessary.
1907 */
7d980def 1908void *consumer_thread_data_poll(void *data)
3bd1e081
MD
1909{
1910 int num_rdy, num_hup, high_prio, ret, i;
1911 struct pollfd *pollfd = NULL;
1912 /* local view of the streams */
c869f647 1913 struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL;
3bd1e081
MD
1914 /* local view of consumer_data.fds_count */
1915 int nb_fd = 0;
3bd1e081 1916 struct lttng_consumer_local_data *ctx = data;
00e2e675 1917 ssize_t len;
3bd1e081 1918
e7b994a3
DG
1919 rcu_register_thread();
1920
effcf122 1921 local_stream = zmalloc(sizeof(struct lttng_consumer_stream));
3bd1e081
MD
1922
1923 while (1) {
1924 high_prio = 0;
1925 num_hup = 0;
1926
1927 /*
e4421fec 1928 * the fds set has been updated, we need to update our
3bd1e081
MD
1929 * local array as well
1930 */
1931 pthread_mutex_lock(&consumer_data.lock);
1932 if (consumer_data.need_update) {
1933 if (pollfd != NULL) {
1934 free(pollfd);
1935 pollfd = NULL;
1936 }
1937 if (local_stream != NULL) {
1938 free(local_stream);
1939 local_stream = NULL;
1940 }
1941
1942 /* allocate for all fds + 1 for the consumer_poll_pipe */
effcf122 1943 pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
3bd1e081 1944 if (pollfd == NULL) {
7a57cf92 1945 PERROR("pollfd malloc");
3bd1e081
MD
1946 pthread_mutex_unlock(&consumer_data.lock);
1947 goto end;
1948 }
1949
1950 /* allocate for all fds + 1 for the consumer_poll_pipe */
effcf122 1951 local_stream = zmalloc((consumer_data.stream_count + 1) *
3bd1e081
MD
1952 sizeof(struct lttng_consumer_stream));
1953 if (local_stream == NULL) {
7a57cf92 1954 PERROR("local_stream malloc");
3bd1e081
MD
1955 pthread_mutex_unlock(&consumer_data.lock);
1956 goto end;
1957 }
fb3a43a9 1958 ret = consumer_update_poll_array(ctx, &pollfd, local_stream);
3bd1e081
MD
1959 if (ret < 0) {
1960 ERR("Error in allocating pollfd or local_outfds");
f73fabfd 1961 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3bd1e081
MD
1962 pthread_mutex_unlock(&consumer_data.lock);
1963 goto end;
1964 }
1965 nb_fd = ret;
1966 consumer_data.need_update = 0;
1967 }
1968 pthread_mutex_unlock(&consumer_data.lock);
1969
4078b776
MD
1970 /* No FDs and consumer_quit, consumer_cleanup the thread */
1971 if (nb_fd == 0 && consumer_quit == 1) {
1972 goto end;
1973 }
3bd1e081 1974 /* poll on the array of fds */
88f2b785 1975 restart:
3bd1e081
MD
1976 DBG("polling on %d fd", nb_fd + 1);
1977 num_rdy = poll(pollfd, nb_fd + 1, consumer_poll_timeout);
1978 DBG("poll num_rdy : %d", num_rdy);
1979 if (num_rdy == -1) {
88f2b785
MD
1980 /*
1981 * Restart interrupted system call.
1982 */
1983 if (errno == EINTR) {
1984 goto restart;
1985 }
7a57cf92 1986 PERROR("Poll error");
f73fabfd 1987 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3bd1e081
MD
1988 goto end;
1989 } else if (num_rdy == 0) {
1990 DBG("Polling thread timed out");
1991 goto end;
1992 }
1993
3bd1e081 1994 /*
00e2e675
DG
1995 * If the consumer_poll_pipe triggered poll go directly to the
1996 * beginning of the loop to update the array. We want to prioritize
1997 * array update over low-priority reads.
3bd1e081 1998 */
509bb1cf 1999 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
04fdd819 2000 size_t pipe_readlen;
04fdd819 2001
3bd1e081 2002 DBG("consumer_poll_pipe wake up");
04fdd819
MD
2003 /* Consume 1 byte of pipe data */
2004 do {
c869f647
DG
2005 pipe_readlen = read(ctx->consumer_poll_pipe[0], &new_stream,
2006 sizeof(new_stream));
04fdd819 2007 } while (pipe_readlen == -1 && errno == EINTR);
c869f647
DG
2008
2009 /*
2010 * If the stream is NULL, just ignore it. It's also possible that
2011 * the sessiond poll thread changed the consumer_quit state and is
2012 * waking us up to test it.
2013 */
2014 if (new_stream == NULL) {
2015 continue;
2016 }
2017
2018 ret = consumer_add_stream(new_stream);
2019 if (ret) {
2020 ERR("Consumer add stream %d failed. Continuing",
2021 new_stream->key);
2022 /*
2023 * At this point, if the add_stream fails, it is not in the
2024 * hash table thus passing the NULL value here.
2025 */
2026 consumer_del_stream(new_stream, NULL);
2027 }
2028
2029 /* Continue to update the local streams and handle prio ones */
3bd1e081
MD
2030 continue;
2031 }
2032
2033 /* Take care of high priority channels first. */
2034 for (i = 0; i < nb_fd; i++) {
fb3a43a9 2035 if (pollfd[i].revents & POLLPRI) {
d41f73b7
MD
2036 DBG("Urgent read on fd %d", pollfd[i].fd);
2037 high_prio = 1;
4078b776 2038 len = ctx->on_buffer_ready(local_stream[i], ctx);
d41f73b7 2039 /* it's ok to have an unavailable sub-buffer */
b64403e3 2040 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
4078b776
MD
2041 goto end;
2042 } else if (len > 0) {
2043 local_stream[i]->data_read = 1;
d41f73b7 2044 }
3bd1e081
MD
2045 }
2046 }
2047
4078b776
MD
2048 /*
2049 * If we read high prio channel in this loop, try again
2050 * for more high prio data.
2051 */
2052 if (high_prio) {
3bd1e081
MD
2053 continue;
2054 }
2055
2056 /* Take care of low priority channels. */
4078b776
MD
2057 for (i = 0; i < nb_fd; i++) {
2058 if ((pollfd[i].revents & POLLIN) ||
2059 local_stream[i]->hangup_flush_done) {
4078b776
MD
2060 DBG("Normal read on fd %d", pollfd[i].fd);
2061 len = ctx->on_buffer_ready(local_stream[i], ctx);
2062 /* it's ok to have an unavailable sub-buffer */
b64403e3 2063 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
4078b776
MD
2064 goto end;
2065 } else if (len > 0) {
2066 local_stream[i]->data_read = 1;
2067 }
2068 }
2069 }
2070
2071 /* Handle hangup and errors */
2072 for (i = 0; i < nb_fd; i++) {
2073 if (!local_stream[i]->hangup_flush_done
2074 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
2075 && (consumer_data.type == LTTNG_CONSUMER32_UST
2076 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2077 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2078 pollfd[i].fd);
2079 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
2080 /* Attempt read again, for the data we just flushed. */
2081 local_stream[i]->data_read = 1;
2082 }
2083 /*
2084 * If the poll flag is HUP/ERR/NVAL and we have
2085 * read no data in this pass, we can remove the
2086 * stream from its hash table.
2087 */
2088 if ((pollfd[i].revents & POLLHUP)) {
2089 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
2090 if (!local_stream[i]->data_read) {
e316aad5
DG
2091 consumer_del_stream(local_stream[i],
2092 consumer_data.stream_ht);
4078b776
MD
2093 num_hup++;
2094 }
2095 } else if (pollfd[i].revents & POLLERR) {
2096 ERR("Error returned in polling fd %d.", pollfd[i].fd);
2097 if (!local_stream[i]->data_read) {
e316aad5
DG
2098 consumer_del_stream(local_stream[i],
2099 consumer_data.stream_ht);
4078b776
MD
2100 num_hup++;
2101 }
2102 } else if (pollfd[i].revents & POLLNVAL) {
2103 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
2104 if (!local_stream[i]->data_read) {
e316aad5
DG
2105 consumer_del_stream(local_stream[i],
2106 consumer_data.stream_ht);
4078b776 2107 num_hup++;
3bd1e081
MD
2108 }
2109 }
4078b776 2110 local_stream[i]->data_read = 0;
3bd1e081
MD
2111 }
2112 }
2113end:
2114 DBG("polling thread exiting");
2115 if (pollfd != NULL) {
2116 free(pollfd);
2117 pollfd = NULL;
2118 }
2119 if (local_stream != NULL) {
2120 free(local_stream);
2121 local_stream = NULL;
2122 }
fb3a43a9
DG
2123
2124 /*
2125 * Close the write side of the pipe so epoll_wait() in
7d980def
DG
2126 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2127 * read side of the pipe. If we close them both, epoll_wait strangely does
2128 * not return and could create a endless wait period if the pipe is the
2129 * only tracked fd in the poll set. The thread will take care of closing
2130 * the read side.
fb3a43a9
DG
2131 */
2132 close(ctx->consumer_metadata_pipe[1]);
fb3a43a9 2133
e7b994a3 2134 rcu_unregister_thread();
3bd1e081
MD
2135 return NULL;
2136}
2137
2138/*
2139 * This thread listens on the consumerd socket and receives the file
2140 * descriptors from the session daemon.
2141 */
7d980def 2142void *consumer_thread_sessiond_poll(void *data)
3bd1e081
MD
2143{
2144 int sock, client_socket, ret;
2145 /*
2146 * structure to poll for incoming data on communication socket avoids
2147 * making blocking sockets.
2148 */
2149 struct pollfd consumer_sockpoll[2];
2150 struct lttng_consumer_local_data *ctx = data;
2151
e7b994a3
DG
2152 rcu_register_thread();
2153
3bd1e081
MD
2154 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
2155 unlink(ctx->consumer_command_sock_path);
2156 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
2157 if (client_socket < 0) {
2158 ERR("Cannot create command socket");
2159 goto end;
2160 }
2161
2162 ret = lttcomm_listen_unix_sock(client_socket);
2163 if (ret < 0) {
2164 goto end;
2165 }
2166
32258573 2167 DBG("Sending ready command to lttng-sessiond");
f73fabfd 2168 ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY);
3bd1e081
MD
2169 /* return < 0 on error, but == 0 is not fatal */
2170 if (ret < 0) {
32258573 2171 ERR("Error sending ready command to lttng-sessiond");
3bd1e081
MD
2172 goto end;
2173 }
2174
2175 ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
2176 if (ret < 0) {
7a57cf92 2177 PERROR("fcntl O_NONBLOCK");
3bd1e081
MD
2178 goto end;
2179 }
2180
2181 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2182 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
2183 consumer_sockpoll[0].events = POLLIN | POLLPRI;
2184 consumer_sockpoll[1].fd = client_socket;
2185 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2186
2187 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2188 goto end;
2189 }
2190 DBG("Connection on client_socket");
2191
2192 /* Blocking call, waiting for transmission */
2193 sock = lttcomm_accept_unix_sock(client_socket);
2194 if (sock <= 0) {
2195 WARN("On accept");
2196 goto end;
2197 }
2198 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
2199 if (ret < 0) {
7a57cf92 2200 PERROR("fcntl O_NONBLOCK");
3bd1e081
MD
2201 goto end;
2202 }
2203
2204 /* update the polling structure to poll on the established socket */
2205 consumer_sockpoll[1].fd = sock;
2206 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2207
2208 while (1) {
2209 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2210 goto end;
2211 }
2212 DBG("Incoming command on sock");
2213 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
2214 if (ret == -ENOENT) {
2215 DBG("Received STOP command");
2216 goto end;
2217 }
4cbc1a04
DG
2218 if (ret <= 0) {
2219 /*
2220 * This could simply be a session daemon quitting. Don't output
2221 * ERR() here.
2222 */
2223 DBG("Communication interrupted on command socket");
3bd1e081
MD
2224 goto end;
2225 }
2226 if (consumer_quit) {
2227 DBG("consumer_thread_receive_fds received quit from signal");
2228 goto end;
2229 }
2230 DBG("received fds on sock");
2231 }
2232end:
2233 DBG("consumer_thread_receive_fds exiting");
2234
2235 /*
2236 * when all fds have hung up, the polling thread
2237 * can exit cleanly
2238 */
2239 consumer_quit = 1;
2240
2241 /*
2242 * 2s of grace period, if no polling events occur during
2243 * this period, the polling thread will exit even if there
2244 * are still open FDs (should not happen, but safety mechanism).
2245 */
2246 consumer_poll_timeout = LTTNG_CONSUMER_POLL_TIMEOUT;
2247
04fdd819 2248 /*
c869f647
DG
2249 * Notify the data poll thread to poll back again and test the
2250 * consumer_quit state to quit gracefully.
04fdd819
MD
2251 */
2252 do {
c869f647
DG
2253 struct lttng_consumer_stream *null_stream = NULL;
2254
2255 ret = write(ctx->consumer_poll_pipe[1], &null_stream,
2256 sizeof(null_stream));
6f94560a 2257 } while (ret < 0 && errno == EINTR);
c869f647 2258
e7b994a3 2259 rcu_unregister_thread();
3bd1e081
MD
2260 return NULL;
2261}
d41f73b7 2262
4078b776 2263ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
2264 struct lttng_consumer_local_data *ctx)
2265{
2266 switch (consumer_data.type) {
2267 case LTTNG_CONSUMER_KERNEL:
2268 return lttng_kconsumer_read_subbuffer(stream, ctx);
7753dea8
MD
2269 case LTTNG_CONSUMER32_UST:
2270 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
2271 return lttng_ustconsumer_read_subbuffer(stream, ctx);
2272 default:
2273 ERR("Unknown consumer_data type");
2274 assert(0);
2275 return -ENOSYS;
2276 }
2277}
2278
2279int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
2280{
2281 switch (consumer_data.type) {
2282 case LTTNG_CONSUMER_KERNEL:
2283 return lttng_kconsumer_on_recv_stream(stream);
7753dea8
MD
2284 case LTTNG_CONSUMER32_UST:
2285 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
2286 return lttng_ustconsumer_on_recv_stream(stream);
2287 default:
2288 ERR("Unknown consumer_data type");
2289 assert(0);
2290 return -ENOSYS;
2291 }
2292}
e4421fec
DG
2293
2294/*
2295 * Allocate and set consumer data hash tables.
2296 */
2297void lttng_consumer_init(void)
2298{
2299 consumer_data.stream_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2300 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
00e2e675 2301 consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
e4421fec 2302}
7735ef9e
DG
2303
2304/*
2305 * Process the ADD_RELAYD command receive by a consumer.
2306 *
2307 * This will create a relayd socket pair and add it to the relayd hash table.
2308 * The caller MUST acquire a RCU read side lock before calling it.
2309 */
2310int consumer_add_relayd_socket(int net_seq_idx, int sock_type,
2311 struct lttng_consumer_local_data *ctx, int sock,
2312 struct pollfd *consumer_sockpoll, struct lttcomm_sock *relayd_sock)
2313{
2314 int fd, ret = -1;
2315 struct consumer_relayd_sock_pair *relayd;
2316
2317 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx);
2318
2319 /* Get relayd reference if exists. */
2320 relayd = consumer_find_relayd(net_seq_idx);
2321 if (relayd == NULL) {
2322 /* Not found. Allocate one. */
2323 relayd = consumer_allocate_relayd_sock_pair(net_seq_idx);
2324 if (relayd == NULL) {
f73fabfd 2325 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
7735ef9e
DG
2326 goto error;
2327 }
2328 }
2329
2330 /* Poll on consumer socket. */
2331 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2332 ret = -EINTR;
2333 goto error;
2334 }
2335
2336 /* Get relayd socket from session daemon */
2337 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
2338 if (ret != sizeof(fd)) {
f73fabfd 2339 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
7735ef9e
DG
2340 ret = -1;
2341 goto error;
2342 }
2343
2344 /* Copy socket information and received FD */
2345 switch (sock_type) {
2346 case LTTNG_STREAM_CONTROL:
2347 /* Copy received lttcomm socket */
2348 lttcomm_copy_sock(&relayd->control_sock, relayd_sock);
2349 ret = lttcomm_create_sock(&relayd->control_sock);
2350 if (ret < 0) {
2351 goto error;
2352 }
2353
2354 /* Close the created socket fd which is useless */
2355 close(relayd->control_sock.fd);
2356
2357 /* Assign new file descriptor */
2358 relayd->control_sock.fd = fd;
2359 break;
2360 case LTTNG_STREAM_DATA:
2361 /* Copy received lttcomm socket */
2362 lttcomm_copy_sock(&relayd->data_sock, relayd_sock);
2363 ret = lttcomm_create_sock(&relayd->data_sock);
2364 if (ret < 0) {
2365 goto error;
2366 }
2367
2368 /* Close the created socket fd which is useless */
2369 close(relayd->data_sock.fd);
2370
2371 /* Assign new file descriptor */
2372 relayd->data_sock.fd = fd;
2373 break;
2374 default:
2375 ERR("Unknown relayd socket type (%d)", sock_type);
2376 goto error;
2377 }
2378
2379 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2380 sock_type == LTTNG_STREAM_CONTROL ? "control" : "data",
2381 relayd->net_seq_idx, fd);
2382
2383 /*
2384 * Add relayd socket pair to consumer data hashtable. If object already
2385 * exists or on error, the function gracefully returns.
2386 */
d09e1200 2387 add_relayd(relayd);
7735ef9e
DG
2388
2389 /* All good! */
2390 ret = 0;
2391
2392error:
2393 return ret;
2394}
This page took 0.145017 seconds and 5 git commands to generate.