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