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