Fix: bad file descriptor on close in health check
[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>
30
990570ed 31#include <common/common.h>
10a8a223 32#include <common/kernel-ctl/kernel-ctl.h>
00e2e675 33#include <common/sessiond-comm/relayd.h>
10a8a223
DG
34#include <common/sessiond-comm/sessiond-comm.h>
35#include <common/kernel-consumer/kernel-consumer.h>
00e2e675 36#include <common/relayd/relayd.h>
10a8a223
DG
37#include <common/ust-consumer/ust-consumer.h>
38
39#include "consumer.h"
3bd1e081
MD
40
41struct lttng_consumer_global_data consumer_data = {
3bd1e081
MD
42 .stream_count = 0,
43 .need_update = 1,
44 .type = LTTNG_CONSUMER_UNKNOWN,
45};
46
47/* timeout parameter, to control the polling thread grace period. */
48int consumer_poll_timeout = -1;
49
50/*
51 * Flag to inform the polling thread to quit when all fd hung up. Updated by
52 * the consumer_thread_receive_fds when it notices that all fds has hung up.
53 * Also updated by the signal handler (consumer_should_exit()). Read by the
54 * polling threads.
55 */
56volatile int consumer_quit = 0;
57
58/*
59 * Find a stream. The consumer_data.lock must be locked during this
60 * call.
61 */
62static struct lttng_consumer_stream *consumer_find_stream(int key)
63{
e4421fec
DG
64 struct lttng_ht_iter iter;
65 struct lttng_ht_node_ulong *node;
66 struct lttng_consumer_stream *stream = NULL;
3bd1e081 67
7ad0a0cb
MD
68 /* Negative keys are lookup failures */
69 if (key < 0)
70 return NULL;
e4421fec 71
6065ceec
DG
72 rcu_read_lock();
73
e4421fec
DG
74 lttng_ht_lookup(consumer_data.stream_ht, (void *)((unsigned long) key),
75 &iter);
76 node = lttng_ht_iter_get_node_ulong(&iter);
77 if (node != NULL) {
78 stream = caa_container_of(node, struct lttng_consumer_stream, node);
3bd1e081 79 }
e4421fec 80
6065ceec
DG
81 rcu_read_unlock();
82
e4421fec 83 return stream;
3bd1e081
MD
84}
85
7ad0a0cb
MD
86static void consumer_steal_stream_key(int key)
87{
88 struct lttng_consumer_stream *stream;
89
04253271 90 rcu_read_lock();
7ad0a0cb 91 stream = consumer_find_stream(key);
04253271 92 if (stream) {
7ad0a0cb 93 stream->key = -1;
04253271
MD
94 /*
95 * We don't want the lookup to match, but we still need
96 * to iterate on this stream when iterating over the hash table. Just
97 * change the node key.
98 */
99 stream->node.key = -1;
100 }
101 rcu_read_unlock();
7ad0a0cb
MD
102}
103
3bd1e081
MD
104static struct lttng_consumer_channel *consumer_find_channel(int key)
105{
e4421fec
DG
106 struct lttng_ht_iter iter;
107 struct lttng_ht_node_ulong *node;
108 struct lttng_consumer_channel *channel = NULL;
3bd1e081 109
7ad0a0cb
MD
110 /* Negative keys are lookup failures */
111 if (key < 0)
112 return NULL;
e4421fec 113
6065ceec
DG
114 rcu_read_lock();
115
e4421fec
DG
116 lttng_ht_lookup(consumer_data.channel_ht, (void *)((unsigned long) key),
117 &iter);
118 node = lttng_ht_iter_get_node_ulong(&iter);
119 if (node != NULL) {
120 channel = caa_container_of(node, struct lttng_consumer_channel, node);
3bd1e081 121 }
e4421fec 122
6065ceec
DG
123 rcu_read_unlock();
124
e4421fec 125 return channel;
3bd1e081
MD
126}
127
7ad0a0cb
MD
128static void consumer_steal_channel_key(int key)
129{
130 struct lttng_consumer_channel *channel;
131
04253271 132 rcu_read_lock();
7ad0a0cb 133 channel = consumer_find_channel(key);
04253271 134 if (channel) {
7ad0a0cb 135 channel->key = -1;
04253271
MD
136 /*
137 * We don't want the lookup to match, but we still need
138 * to iterate on this channel when iterating over the hash table. Just
139 * change the node key.
140 */
141 channel->node.key = -1;
142 }
143 rcu_read_unlock();
7ad0a0cb
MD
144}
145
702b1ea4
MD
146static
147void consumer_free_stream(struct rcu_head *head)
148{
149 struct lttng_ht_node_ulong *node =
150 caa_container_of(head, struct lttng_ht_node_ulong, head);
151 struct lttng_consumer_stream *stream =
152 caa_container_of(node, struct lttng_consumer_stream, node);
153
154 free(stream);
155}
156
00e2e675
DG
157/*
158 * RCU protected relayd socket pair free.
159 */
160static void consumer_rcu_free_relayd(struct rcu_head *head)
161{
162 struct lttng_ht_node_ulong *node =
163 caa_container_of(head, struct lttng_ht_node_ulong, head);
164 struct consumer_relayd_sock_pair *relayd =
165 caa_container_of(node, struct consumer_relayd_sock_pair, node);
166
167 free(relayd);
168}
169
170/*
171 * Destroy and free relayd socket pair object.
172 *
173 * This function MUST be called with the consumer_data lock acquired.
174 */
175void consumer_destroy_relayd(struct consumer_relayd_sock_pair *relayd)
176{
177 int ret;
178 struct lttng_ht_iter iter;
179
173af62f
DG
180 if (relayd == NULL) {
181 return;
182 }
183
00e2e675
DG
184 DBG("Consumer destroy and close relayd socket pair");
185
186 iter.iter.node = &relayd->node.node;
187 ret = lttng_ht_del(consumer_data.relayd_ht, &iter);
173af62f
DG
188 if (ret != 0) {
189 /* We assume the relayd was already destroyed */
190 return;
191 }
00e2e675
DG
192
193 /* Close all sockets */
194 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
195 (void) relayd_close(&relayd->control_sock);
196 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
197 (void) relayd_close(&relayd->data_sock);
198
199 /* RCU free() call */
200 call_rcu(&relayd->node.head, consumer_rcu_free_relayd);
201}
202
3bd1e081
MD
203/*
204 * Remove a stream from the global list protected by a mutex. This
205 * function is also responsible for freeing its data structures.
206 */
207void consumer_del_stream(struct lttng_consumer_stream *stream)
208{
209 int ret;
e4421fec 210 struct lttng_ht_iter iter;
3bd1e081 211 struct lttng_consumer_channel *free_chan = NULL;
00e2e675
DG
212 struct consumer_relayd_sock_pair *relayd;
213
214 assert(stream);
3bd1e081
MD
215
216 pthread_mutex_lock(&consumer_data.lock);
217
218 switch (consumer_data.type) {
219 case LTTNG_CONSUMER_KERNEL:
220 if (stream->mmap_base != NULL) {
221 ret = munmap(stream->mmap_base, stream->mmap_len);
222 if (ret != 0) {
223 perror("munmap");
224 }
225 }
226 break;
7753dea8
MD
227 case LTTNG_CONSUMER32_UST:
228 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
229 lttng_ustconsumer_del_stream(stream);
230 break;
231 default:
232 ERR("Unknown consumer_data type");
233 assert(0);
234 goto end;
235 }
236
6065ceec 237 rcu_read_lock();
04253271
MD
238 iter.iter.node = &stream->node.node;
239 ret = lttng_ht_del(consumer_data.stream_ht, &iter);
240 assert(!ret);
e4421fec 241
6065ceec
DG
242 rcu_read_unlock();
243
3bd1e081
MD
244 if (consumer_data.stream_count <= 0) {
245 goto end;
246 }
247 consumer_data.stream_count--;
248 if (!stream) {
249 goto end;
250 }
251 if (stream->out_fd >= 0) {
4c462e79
MD
252 ret = close(stream->out_fd);
253 if (ret) {
254 PERROR("close");
255 }
3bd1e081 256 }
b5c5fc29 257 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
4c462e79
MD
258 ret = close(stream->wait_fd);
259 if (ret) {
260 PERROR("close");
261 }
3bd1e081 262 }
2c1dd183 263 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
4c462e79
MD
264 ret = close(stream->shm_fd);
265 if (ret) {
266 PERROR("close");
267 }
3bd1e081 268 }
00e2e675
DG
269
270 /* Check and cleanup relayd */
b0b335c8 271 rcu_read_lock();
00e2e675
DG
272 relayd = consumer_find_relayd(stream->net_seq_idx);
273 if (relayd != NULL) {
b0b335c8
MD
274 uatomic_dec(&relayd->refcount);
275 assert(uatomic_read(&relayd->refcount) >= 0);
173af62f
DG
276
277 ret = relayd_send_close_stream(&relayd->control_sock,
278 stream->relayd_stream_id,
279 stream->next_net_seq_num - 1);
280 if (ret < 0) {
281 ERR("Unable to close stream on the relayd. Continuing");
282 /* Continue here. There is nothing we can do for the relayd.*/
283 }
284
285 /* Both conditions are met, we destroy the relayd. */
286 if (uatomic_read(&relayd->refcount) == 0 &&
287 uatomic_read(&relayd->destroy_flag)) {
00e2e675
DG
288 consumer_destroy_relayd(relayd);
289 }
00e2e675 290 }
b0b335c8 291 rcu_read_unlock();
00e2e675
DG
292
293 if (!--stream->chan->refcount) {
3bd1e081 294 free_chan = stream->chan;
00e2e675
DG
295 }
296
702b1ea4
MD
297
298 call_rcu(&stream->node.head, consumer_free_stream);
3bd1e081
MD
299end:
300 consumer_data.need_update = 1;
301 pthread_mutex_unlock(&consumer_data.lock);
302
303 if (free_chan)
304 consumer_del_channel(free_chan);
305}
306
307struct lttng_consumer_stream *consumer_allocate_stream(
308 int channel_key, int stream_key,
309 int shm_fd, int wait_fd,
310 enum lttng_consumer_stream_state state,
311 uint64_t mmap_len,
312 enum lttng_event_output output,
6df2e2c9
MD
313 const char *path_name,
314 uid_t uid,
00e2e675
DG
315 gid_t gid,
316 int net_index,
317 int metadata_flag)
3bd1e081
MD
318{
319 struct lttng_consumer_stream *stream;
320 int ret;
321
effcf122 322 stream = zmalloc(sizeof(*stream));
3bd1e081
MD
323 if (stream == NULL) {
324 perror("malloc struct lttng_consumer_stream");
325 goto end;
326 }
327 stream->chan = consumer_find_channel(channel_key);
328 if (!stream->chan) {
329 perror("Unable to find channel key");
330 goto end;
331 }
332 stream->chan->refcount++;
333 stream->key = stream_key;
334 stream->shm_fd = shm_fd;
335 stream->wait_fd = wait_fd;
336 stream->out_fd = -1;
337 stream->out_fd_offset = 0;
338 stream->state = state;
339 stream->mmap_len = mmap_len;
340 stream->mmap_base = NULL;
341 stream->output = output;
6df2e2c9
MD
342 stream->uid = uid;
343 stream->gid = gid;
00e2e675
DG
344 stream->net_seq_idx = net_index;
345 stream->metadata_flag = metadata_flag;
346 strncpy(stream->path_name, path_name, sizeof(stream->path_name));
347 stream->path_name[sizeof(stream->path_name) - 1] = '\0';
e4421fec 348 lttng_ht_node_init_ulong(&stream->node, stream->key);
00e2e675 349 lttng_ht_node_init_ulong(&stream->waitfd_node, stream->wait_fd);
3bd1e081
MD
350
351 switch (consumer_data.type) {
352 case LTTNG_CONSUMER_KERNEL:
353 break;
7753dea8
MD
354 case LTTNG_CONSUMER32_UST:
355 case LTTNG_CONSUMER64_UST:
5af2f756 356 stream->cpu = stream->chan->cpucount++;
3bd1e081
MD
357 ret = lttng_ustconsumer_allocate_stream(stream);
358 if (ret) {
359 free(stream);
360 return NULL;
361 }
362 break;
363 default:
364 ERR("Unknown consumer_data type");
365 assert(0);
366 goto end;
367 }
00e2e675 368 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d, net_seq_idx %d)",
3bd1e081
MD
369 stream->path_name, stream->key,
370 stream->shm_fd,
371 stream->wait_fd,
372 (unsigned long long) stream->mmap_len,
00e2e675
DG
373 stream->out_fd,
374 stream->net_seq_idx);
3bd1e081
MD
375end:
376 return stream;
377}
378
379/*
380 * Add a stream to the global list protected by a mutex.
381 */
382int consumer_add_stream(struct lttng_consumer_stream *stream)
383{
384 int ret = 0;
c77fc10a
DG
385 struct lttng_ht_node_ulong *node;
386 struct lttng_ht_iter iter;
00e2e675 387 struct consumer_relayd_sock_pair *relayd;
3bd1e081
MD
388
389 pthread_mutex_lock(&consumer_data.lock);
7ad0a0cb
MD
390 /* Steal stream identifier, for UST */
391 consumer_steal_stream_key(stream->key);
c77fc10a 392
b0b335c8 393 rcu_read_lock();
c77fc10a
DG
394 lttng_ht_lookup(consumer_data.stream_ht,
395 (void *)((unsigned long) stream->key), &iter);
396 node = lttng_ht_iter_get_node_ulong(&iter);
397 if (node != NULL) {
398 rcu_read_unlock();
399 /* Stream already exist. Ignore the insertion */
400 goto end;
401 }
402
04253271 403 lttng_ht_add_unique_ulong(consumer_data.stream_ht, &stream->node);
00e2e675
DG
404
405 /* Check and cleanup relayd */
406 relayd = consumer_find_relayd(stream->net_seq_idx);
407 if (relayd != NULL) {
b0b335c8 408 uatomic_inc(&relayd->refcount);
00e2e675 409 }
b0b335c8 410 rcu_read_unlock();
00e2e675
DG
411
412 /* Update consumer data */
3bd1e081
MD
413 consumer_data.stream_count++;
414 consumer_data.need_update = 1;
415
416 switch (consumer_data.type) {
417 case LTTNG_CONSUMER_KERNEL:
418 break;
7753dea8
MD
419 case LTTNG_CONSUMER32_UST:
420 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
421 /* Streams are in CPU number order (we rely on this) */
422 stream->cpu = stream->chan->nr_streams++;
423 break;
424 default:
425 ERR("Unknown consumer_data type");
426 assert(0);
427 goto end;
428 }
429
430end:
431 pthread_mutex_unlock(&consumer_data.lock);
702b1ea4 432
3bd1e081
MD
433 return ret;
434}
435
00e2e675
DG
436/*
437 * Add relayd socket to global consumer data hashtable.
438 */
439int consumer_add_relayd(struct consumer_relayd_sock_pair *relayd)
440{
441 int ret = 0;
442 struct lttng_ht_node_ulong *node;
443 struct lttng_ht_iter iter;
444
445 if (relayd == NULL) {
446 ret = -1;
447 goto end;
448 }
449
450 rcu_read_lock();
451
452 lttng_ht_lookup(consumer_data.relayd_ht,
453 (void *)((unsigned long) relayd->net_seq_idx), &iter);
454 node = lttng_ht_iter_get_node_ulong(&iter);
455 if (node != NULL) {
456 rcu_read_unlock();
457 /* Relayd already exist. Ignore the insertion */
458 goto end;
459 }
460 lttng_ht_add_unique_ulong(consumer_data.relayd_ht, &relayd->node);
461
462 rcu_read_unlock();
463
464end:
465 return ret;
466}
467
468/*
469 * Allocate and return a consumer relayd socket.
470 */
471struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
472 int net_seq_idx)
473{
474 struct consumer_relayd_sock_pair *obj = NULL;
475
476 /* Negative net sequence index is a failure */
477 if (net_seq_idx < 0) {
478 goto error;
479 }
480
481 obj = zmalloc(sizeof(struct consumer_relayd_sock_pair));
482 if (obj == NULL) {
483 PERROR("zmalloc relayd sock");
484 goto error;
485 }
486
487 obj->net_seq_idx = net_seq_idx;
488 obj->refcount = 0;
173af62f 489 obj->destroy_flag = 0;
00e2e675
DG
490 lttng_ht_node_init_ulong(&obj->node, obj->net_seq_idx);
491 pthread_mutex_init(&obj->ctrl_sock_mutex, NULL);
492
493error:
494 return obj;
495}
496
497/*
498 * Find a relayd socket pair in the global consumer data.
499 *
500 * Return the object if found else NULL.
b0b335c8
MD
501 * RCU read-side lock must be held across this call and while using the
502 * returned object.
00e2e675
DG
503 */
504struct consumer_relayd_sock_pair *consumer_find_relayd(int key)
505{
506 struct lttng_ht_iter iter;
507 struct lttng_ht_node_ulong *node;
508 struct consumer_relayd_sock_pair *relayd = NULL;
509
510 /* Negative keys are lookup failures */
511 if (key < 0) {
512 goto error;
513 }
514
00e2e675
DG
515 lttng_ht_lookup(consumer_data.relayd_ht, (void *)((unsigned long) key),
516 &iter);
517 node = lttng_ht_iter_get_node_ulong(&iter);
518 if (node != NULL) {
519 relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node);
520 }
521
00e2e675
DG
522error:
523 return relayd;
524}
525
526/*
527 * Handle stream for relayd transmission if the stream applies for network
528 * streaming where the net sequence index is set.
529 *
530 * Return destination file descriptor or negative value on error.
531 */
6197aea7
DG
532static int write_relayd_stream_header(struct lttng_consumer_stream *stream,
533 size_t data_size, struct consumer_relayd_sock_pair *relayd)
00e2e675
DG
534{
535 int outfd = -1, ret;
00e2e675
DG
536 struct lttcomm_relayd_data_hdr data_hdr;
537
538 /* Safety net */
539 assert(stream);
6197aea7 540 assert(relayd);
00e2e675
DG
541
542 /* Reset data header */
543 memset(&data_hdr, 0, sizeof(data_hdr));
544
00e2e675
DG
545 if (stream->metadata_flag) {
546 /* Caller MUST acquire the relayd control socket lock */
547 ret = relayd_send_metadata(&relayd->control_sock, data_size);
548 if (ret < 0) {
549 goto error;
550 }
551
552 /* Metadata are always sent on the control socket. */
553 outfd = relayd->control_sock.fd;
554 } else {
555 /* Set header with stream information */
556 data_hdr.stream_id = htobe64(stream->relayd_stream_id);
557 data_hdr.data_size = htobe32(data_size);
173af62f 558 data_hdr.net_seq_num = htobe64(stream->next_net_seq_num++);
00e2e675
DG
559 /* Other fields are zeroed previously */
560
561 ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr,
562 sizeof(data_hdr));
563 if (ret < 0) {
564 goto error;
565 }
566
567 /* Set to go on data socket */
568 outfd = relayd->data_sock.fd;
569 }
570
571error:
572 return outfd;
573}
574
3bd1e081
MD
575/*
576 * Update a stream according to what we just received.
577 */
578void consumer_change_stream_state(int stream_key,
579 enum lttng_consumer_stream_state state)
580{
581 struct lttng_consumer_stream *stream;
582
583 pthread_mutex_lock(&consumer_data.lock);
584 stream = consumer_find_stream(stream_key);
585 if (stream) {
586 stream->state = state;
587 }
588 consumer_data.need_update = 1;
589 pthread_mutex_unlock(&consumer_data.lock);
590}
591
702b1ea4
MD
592static
593void consumer_free_channel(struct rcu_head *head)
594{
595 struct lttng_ht_node_ulong *node =
596 caa_container_of(head, struct lttng_ht_node_ulong, head);
597 struct lttng_consumer_channel *channel =
598 caa_container_of(node, struct lttng_consumer_channel, node);
599
600 free(channel);
601}
602
3bd1e081
MD
603/*
604 * Remove a channel from the global list protected by a mutex. This
605 * function is also responsible for freeing its data structures.
606 */
607void consumer_del_channel(struct lttng_consumer_channel *channel)
608{
609 int ret;
e4421fec 610 struct lttng_ht_iter iter;
3bd1e081
MD
611
612 pthread_mutex_lock(&consumer_data.lock);
613
614 switch (consumer_data.type) {
615 case LTTNG_CONSUMER_KERNEL:
616 break;
7753dea8
MD
617 case LTTNG_CONSUMER32_UST:
618 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
619 lttng_ustconsumer_del_channel(channel);
620 break;
621 default:
622 ERR("Unknown consumer_data type");
623 assert(0);
624 goto end;
625 }
626
6065ceec 627 rcu_read_lock();
04253271
MD
628 iter.iter.node = &channel->node.node;
629 ret = lttng_ht_del(consumer_data.channel_ht, &iter);
630 assert(!ret);
6065ceec
DG
631 rcu_read_unlock();
632
3bd1e081
MD
633 if (channel->mmap_base != NULL) {
634 ret = munmap(channel->mmap_base, channel->mmap_len);
635 if (ret != 0) {
636 perror("munmap");
637 }
638 }
b5c5fc29 639 if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) {
4c462e79
MD
640 ret = close(channel->wait_fd);
641 if (ret) {
642 PERROR("close");
643 }
3bd1e081 644 }
2c1dd183 645 if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) {
4c462e79
MD
646 ret = close(channel->shm_fd);
647 if (ret) {
648 PERROR("close");
649 }
3bd1e081 650 }
702b1ea4
MD
651
652 call_rcu(&channel->node.head, consumer_free_channel);
3bd1e081
MD
653end:
654 pthread_mutex_unlock(&consumer_data.lock);
655}
656
657struct lttng_consumer_channel *consumer_allocate_channel(
658 int channel_key,
659 int shm_fd, int wait_fd,
660 uint64_t mmap_len,
661 uint64_t max_sb_size)
662{
663 struct lttng_consumer_channel *channel;
664 int ret;
665
276b26d1 666 channel = zmalloc(sizeof(*channel));
3bd1e081
MD
667 if (channel == NULL) {
668 perror("malloc struct lttng_consumer_channel");
669 goto end;
670 }
671 channel->key = channel_key;
672 channel->shm_fd = shm_fd;
673 channel->wait_fd = wait_fd;
674 channel->mmap_len = mmap_len;
675 channel->max_sb_size = max_sb_size;
676 channel->refcount = 0;
677 channel->nr_streams = 0;
e4421fec 678 lttng_ht_node_init_ulong(&channel->node, channel->key);
3bd1e081
MD
679
680 switch (consumer_data.type) {
681 case LTTNG_CONSUMER_KERNEL:
682 channel->mmap_base = NULL;
683 channel->mmap_len = 0;
684 break;
7753dea8
MD
685 case LTTNG_CONSUMER32_UST:
686 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
687 ret = lttng_ustconsumer_allocate_channel(channel);
688 if (ret) {
689 free(channel);
690 return NULL;
691 }
692 break;
693 default:
694 ERR("Unknown consumer_data type");
695 assert(0);
696 goto end;
697 }
698 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
00e2e675 699 channel->key, channel->shm_fd, channel->wait_fd,
3bd1e081
MD
700 (unsigned long long) channel->mmap_len,
701 (unsigned long long) channel->max_sb_size);
702end:
703 return channel;
704}
705
706/*
707 * Add a channel to the global list protected by a mutex.
708 */
709int consumer_add_channel(struct lttng_consumer_channel *channel)
710{
c77fc10a
DG
711 struct lttng_ht_node_ulong *node;
712 struct lttng_ht_iter iter;
713
3bd1e081 714 pthread_mutex_lock(&consumer_data.lock);
7ad0a0cb
MD
715 /* Steal channel identifier, for UST */
716 consumer_steal_channel_key(channel->key);
6065ceec 717 rcu_read_lock();
c77fc10a
DG
718
719 lttng_ht_lookup(consumer_data.channel_ht,
720 (void *)((unsigned long) channel->key), &iter);
721 node = lttng_ht_iter_get_node_ulong(&iter);
722 if (node != NULL) {
723 /* Channel already exist. Ignore the insertion */
724 goto end;
725 }
726
04253271 727 lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node);
c77fc10a
DG
728
729end:
6065ceec 730 rcu_read_unlock();
3bd1e081 731 pthread_mutex_unlock(&consumer_data.lock);
702b1ea4 732
7ad0a0cb 733 return 0;
3bd1e081
MD
734}
735
736/*
737 * Allocate the pollfd structure and the local view of the out fds to avoid
738 * doing a lookup in the linked list and concurrency issues when writing is
739 * needed. Called with consumer_data.lock held.
740 *
741 * Returns the number of fds in the structures.
742 */
743int consumer_update_poll_array(
744 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
00e2e675
DG
745 struct lttng_consumer_stream **local_stream,
746 struct lttng_ht *metadata_ht)
3bd1e081 747{
3bd1e081 748 int i = 0;
e4421fec
DG
749 struct lttng_ht_iter iter;
750 struct lttng_consumer_stream *stream;
3bd1e081
MD
751
752 DBG("Updating poll fd array");
481d6c57 753 rcu_read_lock();
e4421fec
DG
754 cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, stream,
755 node.node) {
756 if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM) {
3bd1e081
MD
757 continue;
758 }
e4421fec
DG
759 DBG("Active FD %d", stream->wait_fd);
760 (*pollfd)[i].fd = stream->wait_fd;
3bd1e081 761 (*pollfd)[i].events = POLLIN | POLLPRI;
00e2e675
DG
762 if (stream->metadata_flag && metadata_ht) {
763 lttng_ht_add_unique_ulong(metadata_ht, &stream->waitfd_node);
764 DBG("Active FD added to metadata hash table");
765 }
e4421fec 766 local_stream[i] = stream;
3bd1e081
MD
767 i++;
768 }
481d6c57 769 rcu_read_unlock();
3bd1e081
MD
770
771 /*
772 * Insert the consumer_poll_pipe at the end of the array and don't
773 * increment i so nb_fd is the number of real FD.
774 */
775 (*pollfd)[i].fd = ctx->consumer_poll_pipe[0];
509bb1cf 776 (*pollfd)[i].events = POLLIN | POLLPRI;
3bd1e081
MD
777 return i;
778}
779
780/*
781 * Poll on the should_quit pipe and the command socket return -1 on error and
782 * should exit, 0 if data is available on the command socket
783 */
784int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
785{
786 int num_rdy;
787
88f2b785 788restart:
3bd1e081
MD
789 num_rdy = poll(consumer_sockpoll, 2, -1);
790 if (num_rdy == -1) {
88f2b785
MD
791 /*
792 * Restart interrupted system call.
793 */
794 if (errno == EINTR) {
795 goto restart;
796 }
3bd1e081
MD
797 perror("Poll error");
798 goto exit;
799 }
509bb1cf 800 if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) {
3bd1e081
MD
801 DBG("consumer_should_quit wake up");
802 goto exit;
803 }
804 return 0;
805
806exit:
807 return -1;
808}
809
810/*
811 * Set the error socket.
812 */
813void lttng_consumer_set_error_sock(
814 struct lttng_consumer_local_data *ctx, int sock)
815{
816 ctx->consumer_error_socket = sock;
817}
818
819/*
820 * Set the command socket path.
821 */
3bd1e081
MD
822void lttng_consumer_set_command_sock_path(
823 struct lttng_consumer_local_data *ctx, char *sock)
824{
825 ctx->consumer_command_sock_path = sock;
826}
827
828/*
829 * Send return code to the session daemon.
830 * If the socket is not defined, we return 0, it is not a fatal error
831 */
832int lttng_consumer_send_error(
833 struct lttng_consumer_local_data *ctx, int cmd)
834{
835 if (ctx->consumer_error_socket > 0) {
836 return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
837 sizeof(enum lttcomm_sessiond_command));
838 }
839
840 return 0;
841}
842
843/*
844 * Close all the tracefiles and stream fds, should be called when all instances
845 * are destroyed.
846 */
847void lttng_consumer_cleanup(void)
848{
e4421fec 849 struct lttng_ht_iter iter;
6065ceec
DG
850 struct lttng_ht_node_ulong *node;
851
852 rcu_read_lock();
3bd1e081
MD
853
854 /*
6065ceec
DG
855 * close all outfd. Called when there are no more threads running (after
856 * joining on the threads), no need to protect list iteration with mutex.
3bd1e081 857 */
6065ceec
DG
858 cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, node,
859 node) {
702b1ea4
MD
860 struct lttng_consumer_stream *stream =
861 caa_container_of(node, struct lttng_consumer_stream, node);
862 consumer_del_stream(stream);
3bd1e081 863 }
e4421fec 864
6065ceec
DG
865 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node,
866 node) {
702b1ea4
MD
867 struct lttng_consumer_channel *channel =
868 caa_container_of(node, struct lttng_consumer_channel, node);
869 consumer_del_channel(channel);
3bd1e081 870 }
6065ceec
DG
871
872 rcu_read_unlock();
d6ce1df2
MD
873
874 lttng_ht_destroy(consumer_data.stream_ht);
875 lttng_ht_destroy(consumer_data.channel_ht);
3bd1e081
MD
876}
877
878/*
879 * Called from signal handler.
880 */
881void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
882{
883 int ret;
884 consumer_quit = 1;
6f94560a
MD
885 do {
886 ret = write(ctx->consumer_should_quit[1], "4", 1);
887 } while (ret < 0 && errno == EINTR);
3bd1e081
MD
888 if (ret < 0) {
889 perror("write consumer quit");
890 }
891}
892
00e2e675
DG
893void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
894 off_t orig_offset)
3bd1e081
MD
895{
896 int outfd = stream->out_fd;
897
898 /*
899 * This does a blocking write-and-wait on any page that belongs to the
900 * subbuffer prior to the one we just wrote.
901 * Don't care about error values, as these are just hints and ways to
902 * limit the amount of page cache used.
903 */
904 if (orig_offset < stream->chan->max_sb_size) {
905 return;
906 }
b9182dd9 907 lttng_sync_file_range(outfd, orig_offset - stream->chan->max_sb_size,
3bd1e081
MD
908 stream->chan->max_sb_size,
909 SYNC_FILE_RANGE_WAIT_BEFORE
910 | SYNC_FILE_RANGE_WRITE
911 | SYNC_FILE_RANGE_WAIT_AFTER);
912 /*
913 * Give hints to the kernel about how we access the file:
914 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
915 * we write it.
916 *
917 * We need to call fadvise again after the file grows because the
918 * kernel does not seem to apply fadvise to non-existing parts of the
919 * file.
920 *
921 * Call fadvise _after_ having waited for the page writeback to
922 * complete because the dirty page writeback semantic is not well
923 * defined. So it can be expected to lead to lower throughput in
924 * streaming.
925 */
926 posix_fadvise(outfd, orig_offset - stream->chan->max_sb_size,
927 stream->chan->max_sb_size, POSIX_FADV_DONTNEED);
928}
929
930/*
931 * Initialise the necessary environnement :
932 * - create a new context
933 * - create the poll_pipe
934 * - create the should_quit pipe (for signal handler)
935 * - create the thread pipe (for splice)
936 *
937 * Takes a function pointer as argument, this function is called when data is
938 * available on a buffer. This function is responsible to do the
939 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
940 * buffer configuration and then kernctl_put_next_subbuf at the end.
941 *
942 * Returns a pointer to the new context or NULL on error.
943 */
944struct lttng_consumer_local_data *lttng_consumer_create(
945 enum lttng_consumer_type type,
4078b776 946 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
d41f73b7 947 struct lttng_consumer_local_data *ctx),
3bd1e081
MD
948 int (*recv_channel)(struct lttng_consumer_channel *channel),
949 int (*recv_stream)(struct lttng_consumer_stream *stream),
950 int (*update_stream)(int stream_key, uint32_t state))
951{
952 int ret, i;
953 struct lttng_consumer_local_data *ctx;
954
955 assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
956 consumer_data.type == type);
957 consumer_data.type = type;
958
effcf122 959 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
3bd1e081
MD
960 if (ctx == NULL) {
961 perror("allocating context");
962 goto error;
963 }
964
965 ctx->consumer_error_socket = -1;
966 /* assign the callbacks */
967 ctx->on_buffer_ready = buffer_ready;
968 ctx->on_recv_channel = recv_channel;
969 ctx->on_recv_stream = recv_stream;
970 ctx->on_update_stream = update_stream;
971
972 ret = pipe(ctx->consumer_poll_pipe);
973 if (ret < 0) {
974 perror("Error creating poll pipe");
975 goto error_poll_pipe;
976 }
977
04fdd819
MD
978 /* set read end of the pipe to non-blocking */
979 ret = fcntl(ctx->consumer_poll_pipe[0], F_SETFL, O_NONBLOCK);
980 if (ret < 0) {
981 perror("fcntl O_NONBLOCK");
982 goto error_poll_fcntl;
983 }
984
985 /* set write end of the pipe to non-blocking */
986 ret = fcntl(ctx->consumer_poll_pipe[1], F_SETFL, O_NONBLOCK);
987 if (ret < 0) {
988 perror("fcntl O_NONBLOCK");
989 goto error_poll_fcntl;
990 }
991
3bd1e081
MD
992 ret = pipe(ctx->consumer_should_quit);
993 if (ret < 0) {
994 perror("Error creating recv pipe");
995 goto error_quit_pipe;
996 }
997
998 ret = pipe(ctx->consumer_thread_pipe);
999 if (ret < 0) {
1000 perror("Error creating thread pipe");
1001 goto error_thread_pipe;
1002 }
1003
1004 return ctx;
1005
1006
1007error_thread_pipe:
1008 for (i = 0; i < 2; i++) {
1009 int err;
1010
1011 err = close(ctx->consumer_should_quit[i]);
4c462e79
MD
1012 if (err) {
1013 PERROR("close");
1014 }
3bd1e081 1015 }
04fdd819 1016error_poll_fcntl:
3bd1e081
MD
1017error_quit_pipe:
1018 for (i = 0; i < 2; i++) {
1019 int err;
1020
1021 err = close(ctx->consumer_poll_pipe[i]);
4c462e79
MD
1022 if (err) {
1023 PERROR("close");
1024 }
3bd1e081
MD
1025 }
1026error_poll_pipe:
1027 free(ctx);
1028error:
1029 return NULL;
1030}
1031
1032/*
1033 * Close all fds associated with the instance and free the context.
1034 */
1035void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
1036{
4c462e79
MD
1037 int ret;
1038
1039 ret = close(ctx->consumer_error_socket);
1040 if (ret) {
1041 PERROR("close");
1042 }
1043 ret = close(ctx->consumer_thread_pipe[0]);
1044 if (ret) {
1045 PERROR("close");
1046 }
1047 ret = close(ctx->consumer_thread_pipe[1]);
1048 if (ret) {
1049 PERROR("close");
1050 }
1051 ret = close(ctx->consumer_poll_pipe[0]);
1052 if (ret) {
1053 PERROR("close");
1054 }
1055 ret = close(ctx->consumer_poll_pipe[1]);
1056 if (ret) {
1057 PERROR("close");
1058 }
1059 ret = close(ctx->consumer_should_quit[0]);
1060 if (ret) {
1061 PERROR("close");
1062 }
1063 ret = close(ctx->consumer_should_quit[1]);
1064 if (ret) {
1065 PERROR("close");
1066 }
3bd1e081
MD
1067 unlink(ctx->consumer_command_sock_path);
1068 free(ctx);
1069}
1070
6197aea7
DG
1071/*
1072 * Write the metadata stream id on the specified file descriptor.
1073 */
1074static int write_relayd_metadata_id(int fd,
1075 struct lttng_consumer_stream *stream,
1076 struct consumer_relayd_sock_pair *relayd)
1077{
1078 int ret;
1079 uint64_t metadata_id;
1080
1081 metadata_id = htobe64(stream->relayd_stream_id);
1082 do {
1083 ret = write(fd, (void *) &metadata_id,
1084 sizeof(stream->relayd_stream_id));
1085 } while (ret < 0 && errno == EINTR);
1086 if (ret < 0) {
1087 PERROR("write metadata stream id");
1088 goto end;
1089 }
1090 DBG("Metadata stream id %zu written before data",
1091 stream->relayd_stream_id);
1092
1093end:
1094 return ret;
1095}
1096
3bd1e081 1097/*
09e26845
DG
1098 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1099 * core function for writing trace buffers to either the local filesystem or
1100 * the network.
1101 *
1102 * Careful review MUST be put if any changes occur!
3bd1e081
MD
1103 *
1104 * Returns the number of bytes written
1105 */
4078b776 1106ssize_t lttng_consumer_on_read_subbuffer_mmap(
3bd1e081
MD
1107 struct lttng_consumer_local_data *ctx,
1108 struct lttng_consumer_stream *stream, unsigned long len)
1109{
f02e1e8a
DG
1110 unsigned long mmap_offset;
1111 ssize_t ret = 0, written = 0;
1112 off_t orig_offset = stream->out_fd_offset;
1113 /* Default is on the disk */
1114 int outfd = stream->out_fd;
f02e1e8a
DG
1115 struct consumer_relayd_sock_pair *relayd = NULL;
1116
1117 /* RCU lock for the relayd pointer */
1118 rcu_read_lock();
1119
1120 /* Flag that the current stream if set for network streaming. */
1121 if (stream->net_seq_idx != -1) {
1122 relayd = consumer_find_relayd(stream->net_seq_idx);
1123 if (relayd == NULL) {
1124 goto end;
1125 }
1126 }
1127
1128 /* get the offset inside the fd to mmap */
3bd1e081
MD
1129 switch (consumer_data.type) {
1130 case LTTNG_CONSUMER_KERNEL:
f02e1e8a
DG
1131 ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset);
1132 break;
7753dea8
MD
1133 case LTTNG_CONSUMER32_UST:
1134 case LTTNG_CONSUMER64_UST:
f02e1e8a
DG
1135 ret = lttng_ustctl_get_mmap_read_offset(stream->chan->handle,
1136 stream->buf, &mmap_offset);
1137 break;
3bd1e081
MD
1138 default:
1139 ERR("Unknown consumer_data type");
1140 assert(0);
1141 }
f02e1e8a
DG
1142 if (ret != 0) {
1143 errno = -ret;
1144 PERROR("tracer ctl get_mmap_read_offset");
1145 written = ret;
1146 goto end;
1147 }
b9182dd9 1148
f02e1e8a
DG
1149 /* Handle stream on the relayd if the output is on the network */
1150 if (relayd) {
1151 unsigned long netlen = len;
1152
1153 /*
1154 * Lock the control socket for the complete duration of the function
1155 * since from this point on we will use the socket.
1156 */
1157 if (stream->metadata_flag) {
1158 /* Metadata requires the control socket. */
1159 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1160 netlen += sizeof(stream->relayd_stream_id);
1161 }
1162
6197aea7 1163 ret = write_relayd_stream_header(stream, netlen, relayd);
f02e1e8a
DG
1164 if (ret >= 0) {
1165 /* Use the returned socket. */
1166 outfd = ret;
1167
1168 /* Write metadata stream id before payload */
1169 if (stream->metadata_flag) {
6197aea7 1170 ret = write_relayd_metadata_id(outfd, stream, relayd);
f02e1e8a 1171 if (ret < 0) {
f02e1e8a
DG
1172 written = ret;
1173 goto end;
1174 }
f02e1e8a
DG
1175 }
1176 }
1177 /* Else, use the default set before which is the filesystem. */
1178 }
1179
1180 while (len > 0) {
1181 do {
1182 ret = write(outfd, stream->mmap_base + mmap_offset, len);
1183 } while (ret < 0 && errno == EINTR);
1184 if (ret < 0) {
1185 PERROR("Error in file write");
1186 if (written == 0) {
1187 written = ret;
1188 }
1189 goto end;
1190 } else if (ret > len) {
1191 PERROR("Error in file write (ret %ld > len %lu)", ret, len);
1192 written += ret;
1193 goto end;
1194 } else {
1195 len -= ret;
1196 mmap_offset += ret;
1197 }
1198 DBG("Consumer mmap write() ret %ld (len %lu)", ret, len);
1199
1200 /* This call is useless on a socket so better save a syscall. */
1201 if (!relayd) {
1202 /* This won't block, but will start writeout asynchronously */
1203 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
1204 SYNC_FILE_RANGE_WRITE);
1205 stream->out_fd_offset += ret;
1206 }
1207 written += ret;
1208 }
1209 lttng_consumer_sync_trace_file(stream, orig_offset);
1210
1211end:
1212 /* Unlock only if ctrl socket used */
1213 if (relayd && stream->metadata_flag) {
1214 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1215 }
1216
1217 rcu_read_unlock();
1218 return written;
3bd1e081
MD
1219}
1220
1221/*
1222 * Splice the data from the ring buffer to the tracefile.
1223 *
1224 * Returns the number of bytes spliced.
1225 */
4078b776 1226ssize_t lttng_consumer_on_read_subbuffer_splice(
3bd1e081
MD
1227 struct lttng_consumer_local_data *ctx,
1228 struct lttng_consumer_stream *stream, unsigned long len)
1229{
f02e1e8a
DG
1230 ssize_t ret = 0, written = 0, ret_splice = 0;
1231 loff_t offset = 0;
1232 off_t orig_offset = stream->out_fd_offset;
1233 int fd = stream->wait_fd;
1234 /* Default is on the disk */
1235 int outfd = stream->out_fd;
f02e1e8a
DG
1236 struct consumer_relayd_sock_pair *relayd = NULL;
1237
3bd1e081
MD
1238 switch (consumer_data.type) {
1239 case LTTNG_CONSUMER_KERNEL:
f02e1e8a 1240 break;
7753dea8
MD
1241 case LTTNG_CONSUMER32_UST:
1242 case LTTNG_CONSUMER64_UST:
f02e1e8a 1243 /* Not supported for user space tracing */
3bd1e081
MD
1244 return -ENOSYS;
1245 default:
1246 ERR("Unknown consumer_data type");
1247 assert(0);
3bd1e081
MD
1248 }
1249
f02e1e8a
DG
1250 /* RCU lock for the relayd pointer */
1251 rcu_read_lock();
1252
1253 /* Flag that the current stream if set for network streaming. */
1254 if (stream->net_seq_idx != -1) {
1255 relayd = consumer_find_relayd(stream->net_seq_idx);
1256 if (relayd == NULL) {
1257 goto end;
1258 }
1259 }
1260
1261 /* Write metadata stream id before payload */
1262 if (stream->metadata_flag && relayd) {
1263 /*
1264 * Lock the control socket for the complete duration of the function
1265 * since from this point on we will use the socket.
1266 */
1267 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1268
6197aea7
DG
1269 ret = write_relayd_metadata_id(ctx->consumer_thread_pipe[1],
1270 stream, relayd);
f02e1e8a 1271 if (ret < 0) {
f02e1e8a
DG
1272 written = ret;
1273 goto end;
1274 }
f02e1e8a
DG
1275 }
1276
1277 while (len > 0) {
1278 DBG("splice chan to pipe offset %lu of len %lu (fd : %d)",
1279 (unsigned long)offset, len, fd);
1280 ret_splice = splice(fd, &offset, ctx->consumer_thread_pipe[1], NULL, len,
1281 SPLICE_F_MOVE | SPLICE_F_MORE);
1282 DBG("splice chan to pipe, ret %zd", ret_splice);
1283 if (ret_splice < 0) {
1284 PERROR("Error in relay splice");
1285 if (written == 0) {
1286 written = ret_splice;
1287 }
1288 ret = errno;
1289 goto splice_error;
1290 }
1291
1292 /* Handle stream on the relayd if the output is on the network */
1293 if (relayd) {
1294 if (stream->metadata_flag) {
1295 /* Update counter to fit the spliced data */
1296 ret_splice += sizeof(stream->relayd_stream_id);
1297 len += sizeof(stream->relayd_stream_id);
1298 /*
1299 * We do this so the return value can match the len passed as
1300 * argument to this function.
1301 */
1302 written -= sizeof(stream->relayd_stream_id);
1303 }
1304
6197aea7 1305 ret = write_relayd_stream_header(stream, ret_splice, relayd);
f02e1e8a
DG
1306 if (ret >= 0) {
1307 /* Use the returned socket. */
1308 outfd = ret;
1309 } else {
6197aea7
DG
1310 ERR("Remote relayd disconnected. Stopping");
1311 goto end;
f02e1e8a
DG
1312 }
1313 }
1314
1315 /* Splice data out */
1316 ret_splice = splice(ctx->consumer_thread_pipe[0], NULL, outfd, NULL,
1317 ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE);
1318 DBG("Kernel consumer splice pipe to file, ret %zd", ret_splice);
1319 if (ret_splice < 0) {
1320 PERROR("Error in file splice");
1321 if (written == 0) {
1322 written = ret_splice;
1323 }
1324 ret = errno;
1325 goto splice_error;
1326 } else if (ret_splice > len) {
1327 errno = EINVAL;
1328 PERROR("Wrote more data than requested %zd (len: %lu)",
1329 ret_splice, len);
1330 written += ret_splice;
1331 ret = errno;
1332 goto splice_error;
1333 }
1334 len -= ret_splice;
1335
1336 /* This call is useless on a socket so better save a syscall. */
1337 if (!relayd) {
1338 /* This won't block, but will start writeout asynchronously */
1339 lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice,
1340 SYNC_FILE_RANGE_WRITE);
1341 stream->out_fd_offset += ret_splice;
1342 }
1343 written += ret_splice;
1344 }
1345 lttng_consumer_sync_trace_file(stream, orig_offset);
1346
1347 ret = ret_splice;
1348
1349 goto end;
1350
1351splice_error:
1352 /* send the appropriate error description to sessiond */
1353 switch (ret) {
1354 case EBADF:
1355 lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_EBADF);
1356 break;
1357 case EINVAL:
1358 lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_EINVAL);
1359 break;
1360 case ENOMEM:
1361 lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_ENOMEM);
1362 break;
1363 case ESPIPE:
1364 lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_ESPIPE);
1365 break;
1366 }
1367
1368end:
1369 if (relayd && stream->metadata_flag) {
1370 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1371 }
1372
1373 rcu_read_unlock();
1374 return written;
3bd1e081
MD
1375}
1376
1377/*
1378 * Take a snapshot for a specific fd
1379 *
1380 * Returns 0 on success, < 0 on error
1381 */
1382int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
1383 struct lttng_consumer_stream *stream)
1384{
1385 switch (consumer_data.type) {
1386 case LTTNG_CONSUMER_KERNEL:
1387 return lttng_kconsumer_take_snapshot(ctx, stream);
7753dea8
MD
1388 case LTTNG_CONSUMER32_UST:
1389 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1390 return lttng_ustconsumer_take_snapshot(ctx, stream);
1391 default:
1392 ERR("Unknown consumer_data type");
1393 assert(0);
1394 return -ENOSYS;
1395 }
1396
1397}
1398
1399/*
1400 * Get the produced position
1401 *
1402 * Returns 0 on success, < 0 on error
1403 */
1404int lttng_consumer_get_produced_snapshot(
1405 struct lttng_consumer_local_data *ctx,
1406 struct lttng_consumer_stream *stream,
1407 unsigned long *pos)
1408{
1409 switch (consumer_data.type) {
1410 case LTTNG_CONSUMER_KERNEL:
1411 return lttng_kconsumer_get_produced_snapshot(ctx, stream, pos);
7753dea8
MD
1412 case LTTNG_CONSUMER32_UST:
1413 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1414 return lttng_ustconsumer_get_produced_snapshot(ctx, stream, pos);
1415 default:
1416 ERR("Unknown consumer_data type");
1417 assert(0);
1418 return -ENOSYS;
1419 }
1420}
1421
1422int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1423 int sock, struct pollfd *consumer_sockpoll)
1424{
1425 switch (consumer_data.type) {
1426 case LTTNG_CONSUMER_KERNEL:
1427 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
7753dea8
MD
1428 case LTTNG_CONSUMER32_UST:
1429 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1430 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1431 default:
1432 ERR("Unknown consumer_data type");
1433 assert(0);
1434 return -ENOSYS;
1435 }
1436}
1437
1438/*
e4421fec 1439 * This thread polls the fds in the set to consume the data and write
3bd1e081
MD
1440 * it to tracefile if necessary.
1441 */
1442void *lttng_consumer_thread_poll_fds(void *data)
1443{
1444 int num_rdy, num_hup, high_prio, ret, i;
1445 struct pollfd *pollfd = NULL;
1446 /* local view of the streams */
1447 struct lttng_consumer_stream **local_stream = NULL;
1448 /* local view of consumer_data.fds_count */
1449 int nb_fd = 0;
3bd1e081 1450 struct lttng_consumer_local_data *ctx = data;
00e2e675
DG
1451 struct lttng_ht *metadata_ht;
1452 struct lttng_ht_iter iter;
1453 struct lttng_ht_node_ulong *node;
1454 struct lttng_consumer_stream *metadata_stream;
1455 ssize_t len;
1456
1457 metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3bd1e081 1458
e7b994a3
DG
1459 rcu_register_thread();
1460
effcf122 1461 local_stream = zmalloc(sizeof(struct lttng_consumer_stream));
3bd1e081
MD
1462
1463 while (1) {
1464 high_prio = 0;
1465 num_hup = 0;
1466
1467 /*
e4421fec 1468 * the fds set has been updated, we need to update our
3bd1e081
MD
1469 * local array as well
1470 */
1471 pthread_mutex_lock(&consumer_data.lock);
1472 if (consumer_data.need_update) {
1473 if (pollfd != NULL) {
1474 free(pollfd);
1475 pollfd = NULL;
1476 }
1477 if (local_stream != NULL) {
1478 free(local_stream);
1479 local_stream = NULL;
1480 }
1481
1482 /* allocate for all fds + 1 for the consumer_poll_pipe */
effcf122 1483 pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
3bd1e081
MD
1484 if (pollfd == NULL) {
1485 perror("pollfd malloc");
1486 pthread_mutex_unlock(&consumer_data.lock);
1487 goto end;
1488 }
1489
1490 /* allocate for all fds + 1 for the consumer_poll_pipe */
effcf122 1491 local_stream = zmalloc((consumer_data.stream_count + 1) *
3bd1e081
MD
1492 sizeof(struct lttng_consumer_stream));
1493 if (local_stream == NULL) {
1494 perror("local_stream malloc");
1495 pthread_mutex_unlock(&consumer_data.lock);
1496 goto end;
1497 }
00e2e675
DG
1498 ret = consumer_update_poll_array(ctx, &pollfd, local_stream,
1499 metadata_ht);
3bd1e081
MD
1500 if (ret < 0) {
1501 ERR("Error in allocating pollfd or local_outfds");
1502 lttng_consumer_send_error(ctx, CONSUMERD_POLL_ERROR);
1503 pthread_mutex_unlock(&consumer_data.lock);
1504 goto end;
1505 }
1506 nb_fd = ret;
1507 consumer_data.need_update = 0;
1508 }
1509 pthread_mutex_unlock(&consumer_data.lock);
1510
4078b776
MD
1511 /* No FDs and consumer_quit, consumer_cleanup the thread */
1512 if (nb_fd == 0 && consumer_quit == 1) {
1513 goto end;
1514 }
3bd1e081 1515 /* poll on the array of fds */
88f2b785 1516 restart:
3bd1e081
MD
1517 DBG("polling on %d fd", nb_fd + 1);
1518 num_rdy = poll(pollfd, nb_fd + 1, consumer_poll_timeout);
1519 DBG("poll num_rdy : %d", num_rdy);
1520 if (num_rdy == -1) {
88f2b785
MD
1521 /*
1522 * Restart interrupted system call.
1523 */
1524 if (errno == EINTR) {
1525 goto restart;
1526 }
3bd1e081
MD
1527 perror("Poll error");
1528 lttng_consumer_send_error(ctx, CONSUMERD_POLL_ERROR);
1529 goto end;
1530 } else if (num_rdy == 0) {
1531 DBG("Polling thread timed out");
1532 goto end;
1533 }
1534
3bd1e081 1535 /*
00e2e675
DG
1536 * If the consumer_poll_pipe triggered poll go directly to the
1537 * beginning of the loop to update the array. We want to prioritize
1538 * array update over low-priority reads.
3bd1e081 1539 */
509bb1cf 1540 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
04fdd819
MD
1541 size_t pipe_readlen;
1542 char tmp;
1543
3bd1e081 1544 DBG("consumer_poll_pipe wake up");
04fdd819
MD
1545 /* Consume 1 byte of pipe data */
1546 do {
1547 pipe_readlen = read(ctx->consumer_poll_pipe[0], &tmp, 1);
1548 } while (pipe_readlen == -1 && errno == EINTR);
3bd1e081
MD
1549 continue;
1550 }
1551
1552 /* Take care of high priority channels first. */
1553 for (i = 0; i < nb_fd; i++) {
00e2e675
DG
1554 /* Lookup for metadata which is the highest priority */
1555 lttng_ht_lookup(metadata_ht,
1556 (void *)((unsigned long) pollfd[i].fd), &iter);
1557 node = lttng_ht_iter_get_node_ulong(&iter);
1558 if (node != NULL &&
1559 (pollfd[i].revents & (POLLIN | POLLPRI))) {
1560 DBG("Urgent metadata read on fd %d", pollfd[i].fd);
1561 metadata_stream = caa_container_of(node,
1562 struct lttng_consumer_stream, waitfd_node);
1563 high_prio = 1;
1564 len = ctx->on_buffer_ready(metadata_stream, ctx);
1565 /* it's ok to have an unavailable sub-buffer */
1566 if (len < 0 && len != -EAGAIN) {
1567 goto end;
1568 } else if (len > 0) {
1569 metadata_stream->data_read = 1;
1570 }
1571 } else if (pollfd[i].revents & POLLPRI) {
d41f73b7
MD
1572 DBG("Urgent read on fd %d", pollfd[i].fd);
1573 high_prio = 1;
4078b776 1574 len = ctx->on_buffer_ready(local_stream[i], ctx);
d41f73b7 1575 /* it's ok to have an unavailable sub-buffer */
4078b776
MD
1576 if (len < 0 && len != -EAGAIN) {
1577 goto end;
1578 } else if (len > 0) {
1579 local_stream[i]->data_read = 1;
d41f73b7 1580 }
3bd1e081
MD
1581 }
1582 }
1583
4078b776
MD
1584 /*
1585 * If we read high prio channel in this loop, try again
1586 * for more high prio data.
1587 */
1588 if (high_prio) {
3bd1e081
MD
1589 continue;
1590 }
1591
1592 /* Take care of low priority channels. */
4078b776
MD
1593 for (i = 0; i < nb_fd; i++) {
1594 if ((pollfd[i].revents & POLLIN) ||
1595 local_stream[i]->hangup_flush_done) {
4078b776
MD
1596 DBG("Normal read on fd %d", pollfd[i].fd);
1597 len = ctx->on_buffer_ready(local_stream[i], ctx);
1598 /* it's ok to have an unavailable sub-buffer */
1599 if (len < 0 && len != -EAGAIN) {
1600 goto end;
1601 } else if (len > 0) {
1602 local_stream[i]->data_read = 1;
1603 }
1604 }
1605 }
1606
1607 /* Handle hangup and errors */
1608 for (i = 0; i < nb_fd; i++) {
1609 if (!local_stream[i]->hangup_flush_done
1610 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
1611 && (consumer_data.type == LTTNG_CONSUMER32_UST
1612 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
1613 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1614 pollfd[i].fd);
1615 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
1616 /* Attempt read again, for the data we just flushed. */
1617 local_stream[i]->data_read = 1;
1618 }
1619 /*
1620 * If the poll flag is HUP/ERR/NVAL and we have
1621 * read no data in this pass, we can remove the
1622 * stream from its hash table.
1623 */
1624 if ((pollfd[i].revents & POLLHUP)) {
1625 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
1626 if (!local_stream[i]->data_read) {
00e2e675
DG
1627 if (local_stream[i]->metadata_flag) {
1628 iter.iter.node = &local_stream[i]->waitfd_node.node;
1629 ret = lttng_ht_del(metadata_ht, &iter);
1630 assert(!ret);
1631 }
702b1ea4 1632 consumer_del_stream(local_stream[i]);
4078b776
MD
1633 num_hup++;
1634 }
1635 } else if (pollfd[i].revents & POLLERR) {
1636 ERR("Error returned in polling fd %d.", pollfd[i].fd);
1637 if (!local_stream[i]->data_read) {
00e2e675
DG
1638 if (local_stream[i]->metadata_flag) {
1639 iter.iter.node = &local_stream[i]->waitfd_node.node;
1640 ret = lttng_ht_del(metadata_ht, &iter);
1641 assert(!ret);
1642 }
702b1ea4 1643 consumer_del_stream(local_stream[i]);
4078b776
MD
1644 num_hup++;
1645 }
1646 } else if (pollfd[i].revents & POLLNVAL) {
1647 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
1648 if (!local_stream[i]->data_read) {
00e2e675
DG
1649 if (local_stream[i]->metadata_flag) {
1650 iter.iter.node = &local_stream[i]->waitfd_node.node;
1651 ret = lttng_ht_del(metadata_ht, &iter);
1652 assert(!ret);
1653 }
702b1ea4 1654 consumer_del_stream(local_stream[i]);
4078b776 1655 num_hup++;
3bd1e081
MD
1656 }
1657 }
4078b776 1658 local_stream[i]->data_read = 0;
3bd1e081
MD
1659 }
1660 }
1661end:
1662 DBG("polling thread exiting");
1663 if (pollfd != NULL) {
1664 free(pollfd);
1665 pollfd = NULL;
1666 }
1667 if (local_stream != NULL) {
1668 free(local_stream);
1669 local_stream = NULL;
1670 }
e7b994a3 1671 rcu_unregister_thread();
3bd1e081
MD
1672 return NULL;
1673}
1674
1675/*
1676 * This thread listens on the consumerd socket and receives the file
1677 * descriptors from the session daemon.
1678 */
1679void *lttng_consumer_thread_receive_fds(void *data)
1680{
1681 int sock, client_socket, ret;
1682 /*
1683 * structure to poll for incoming data on communication socket avoids
1684 * making blocking sockets.
1685 */
1686 struct pollfd consumer_sockpoll[2];
1687 struct lttng_consumer_local_data *ctx = data;
1688
e7b994a3
DG
1689 rcu_register_thread();
1690
3bd1e081
MD
1691 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
1692 unlink(ctx->consumer_command_sock_path);
1693 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
1694 if (client_socket < 0) {
1695 ERR("Cannot create command socket");
1696 goto end;
1697 }
1698
1699 ret = lttcomm_listen_unix_sock(client_socket);
1700 if (ret < 0) {
1701 goto end;
1702 }
1703
32258573 1704 DBG("Sending ready command to lttng-sessiond");
3bd1e081
MD
1705 ret = lttng_consumer_send_error(ctx, CONSUMERD_COMMAND_SOCK_READY);
1706 /* return < 0 on error, but == 0 is not fatal */
1707 if (ret < 0) {
32258573 1708 ERR("Error sending ready command to lttng-sessiond");
3bd1e081
MD
1709 goto end;
1710 }
1711
1712 ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
1713 if (ret < 0) {
1714 perror("fcntl O_NONBLOCK");
1715 goto end;
1716 }
1717
1718 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1719 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
1720 consumer_sockpoll[0].events = POLLIN | POLLPRI;
1721 consumer_sockpoll[1].fd = client_socket;
1722 consumer_sockpoll[1].events = POLLIN | POLLPRI;
1723
1724 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
1725 goto end;
1726 }
1727 DBG("Connection on client_socket");
1728
1729 /* Blocking call, waiting for transmission */
1730 sock = lttcomm_accept_unix_sock(client_socket);
1731 if (sock <= 0) {
1732 WARN("On accept");
1733 goto end;
1734 }
1735 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
1736 if (ret < 0) {
1737 perror("fcntl O_NONBLOCK");
1738 goto end;
1739 }
1740
1741 /* update the polling structure to poll on the established socket */
1742 consumer_sockpoll[1].fd = sock;
1743 consumer_sockpoll[1].events = POLLIN | POLLPRI;
1744
1745 while (1) {
1746 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
1747 goto end;
1748 }
1749 DBG("Incoming command on sock");
1750 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
1751 if (ret == -ENOENT) {
1752 DBG("Received STOP command");
1753 goto end;
1754 }
1755 if (ret < 0) {
1756 ERR("Communication interrupted on command socket");
1757 goto end;
1758 }
1759 if (consumer_quit) {
1760 DBG("consumer_thread_receive_fds received quit from signal");
1761 goto end;
1762 }
1763 DBG("received fds on sock");
1764 }
1765end:
1766 DBG("consumer_thread_receive_fds exiting");
1767
1768 /*
1769 * when all fds have hung up, the polling thread
1770 * can exit cleanly
1771 */
1772 consumer_quit = 1;
1773
1774 /*
1775 * 2s of grace period, if no polling events occur during
1776 * this period, the polling thread will exit even if there
1777 * are still open FDs (should not happen, but safety mechanism).
1778 */
1779 consumer_poll_timeout = LTTNG_CONSUMER_POLL_TIMEOUT;
1780
04fdd819
MD
1781 /*
1782 * Wake-up the other end by writing a null byte in the pipe
1783 * (non-blocking). Important note: Because writing into the
1784 * pipe is non-blocking (and therefore we allow dropping wakeup
1785 * data, as long as there is wakeup data present in the pipe
1786 * buffer to wake up the other end), the other end should
1787 * perform the following sequence for waiting:
1788 * 1) empty the pipe (reads).
1789 * 2) perform update operation.
1790 * 3) wait on the pipe (poll).
1791 */
1792 do {
1793 ret = write(ctx->consumer_poll_pipe[1], "", 1);
6f94560a 1794 } while (ret < 0 && errno == EINTR);
e7b994a3 1795 rcu_unregister_thread();
3bd1e081
MD
1796 return NULL;
1797}
d41f73b7 1798
4078b776 1799ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
1800 struct lttng_consumer_local_data *ctx)
1801{
1802 switch (consumer_data.type) {
1803 case LTTNG_CONSUMER_KERNEL:
1804 return lttng_kconsumer_read_subbuffer(stream, ctx);
7753dea8
MD
1805 case LTTNG_CONSUMER32_UST:
1806 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
1807 return lttng_ustconsumer_read_subbuffer(stream, ctx);
1808 default:
1809 ERR("Unknown consumer_data type");
1810 assert(0);
1811 return -ENOSYS;
1812 }
1813}
1814
1815int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
1816{
1817 switch (consumer_data.type) {
1818 case LTTNG_CONSUMER_KERNEL:
1819 return lttng_kconsumer_on_recv_stream(stream);
7753dea8
MD
1820 case LTTNG_CONSUMER32_UST:
1821 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
1822 return lttng_ustconsumer_on_recv_stream(stream);
1823 default:
1824 ERR("Unknown consumer_data type");
1825 assert(0);
1826 return -ENOSYS;
1827 }
1828}
e4421fec
DG
1829
1830/*
1831 * Allocate and set consumer data hash tables.
1832 */
1833void lttng_consumer_init(void)
1834{
1835 consumer_data.stream_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1836 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
00e2e675 1837 consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
e4421fec 1838}
This page took 0.115785 seconds and 5 git commands to generate.