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