Fix: Use PERROR all across lttng-tools, never make it quiet
[lttng-tools.git] / src / common / consumer.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
8 * of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#define _GNU_SOURCE
21#include <assert.h>
22#include <fcntl.h>
23#include <poll.h>
24#include <pthread.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/mman.h>
28#include <sys/socket.h>
29#include <sys/types.h>
30#include <unistd.h>
31
32#include <common/common.h>
33#include <common/kernel-ctl/kernel-ctl.h>
34#include <common/sessiond-comm/sessiond-comm.h>
35#include <common/kernel-consumer/kernel-consumer.h>
36#include <common/ust-consumer/ust-consumer.h>
37
38#include "consumer.h"
39
40struct lttng_consumer_global_data consumer_data = {
41 .stream_count = 0,
42 .need_update = 1,
43 .type = LTTNG_CONSUMER_UNKNOWN,
44};
45
46/* timeout parameter, to control the polling thread grace period. */
47int consumer_poll_timeout = -1;
48
49/*
50 * Flag to inform the polling thread to quit when all fd hung up. Updated by
51 * the consumer_thread_receive_fds when it notices that all fds has hung up.
52 * Also updated by the signal handler (consumer_should_exit()). Read by the
53 * polling threads.
54 */
55volatile int consumer_quit = 0;
56
57/*
58 * Find a stream. The consumer_data.lock must be locked during this
59 * call.
60 */
61static struct lttng_consumer_stream *consumer_find_stream(int key)
62{
63 struct lttng_ht_iter iter;
64 struct lttng_ht_node_ulong *node;
65 struct lttng_consumer_stream *stream = NULL;
66
67 /* Negative keys are lookup failures */
68 if (key < 0)
69 return NULL;
70
71 rcu_read_lock();
72
73 lttng_ht_lookup(consumer_data.stream_ht, (void *)((unsigned long) key),
74 &iter);
75 node = lttng_ht_iter_get_node_ulong(&iter);
76 if (node != NULL) {
77 stream = caa_container_of(node, struct lttng_consumer_stream, node);
78 }
79
80 rcu_read_unlock();
81
82 return stream;
83}
84
85static void consumer_steal_stream_key(int key)
86{
87 struct lttng_consumer_stream *stream;
88
89 stream = consumer_find_stream(key);
90 if (stream)
91 stream->key = -1;
92}
93
94static struct lttng_consumer_channel *consumer_find_channel(int key)
95{
96 struct lttng_ht_iter iter;
97 struct lttng_ht_node_ulong *node;
98 struct lttng_consumer_channel *channel = NULL;
99
100 /* Negative keys are lookup failures */
101 if (key < 0)
102 return NULL;
103
104 rcu_read_lock();
105
106 lttng_ht_lookup(consumer_data.channel_ht, (void *)((unsigned long) key),
107 &iter);
108 node = lttng_ht_iter_get_node_ulong(&iter);
109 if (node != NULL) {
110 channel = caa_container_of(node, struct lttng_consumer_channel, node);
111 }
112
113 rcu_read_unlock();
114
115 return channel;
116}
117
118static void consumer_steal_channel_key(int key)
119{
120 struct lttng_consumer_channel *channel;
121
122 channel = consumer_find_channel(key);
123 if (channel)
124 channel->key = -1;
125}
126
127/*
128 * Remove a stream from the global list protected by a mutex. This
129 * function is also responsible for freeing its data structures.
130 */
131void consumer_del_stream(struct lttng_consumer_stream *stream)
132{
133 int ret;
134 struct lttng_ht_iter iter;
135 struct lttng_consumer_channel *free_chan = NULL;
136
137 pthread_mutex_lock(&consumer_data.lock);
138
139 switch (consumer_data.type) {
140 case LTTNG_CONSUMER_KERNEL:
141 if (stream->mmap_base != NULL) {
142 ret = munmap(stream->mmap_base, stream->mmap_len);
143 if (ret != 0) {
144 perror("munmap");
145 }
146 }
147 break;
148 case LTTNG_CONSUMER32_UST:
149 case LTTNG_CONSUMER64_UST:
150 lttng_ustconsumer_del_stream(stream);
151 break;
152 default:
153 ERR("Unknown consumer_data type");
154 assert(0);
155 goto end;
156 }
157
158 rcu_read_lock();
159
160 /* Get stream node from hash table */
161 lttng_ht_lookup(consumer_data.stream_ht,
162 (void *)((unsigned long) stream->key), &iter);
163 /* Remove stream node from hash table */
164 ret = lttng_ht_del(consumer_data.stream_ht, &iter);
165 assert(!ret);
166
167 rcu_read_unlock();
168
169 if (consumer_data.stream_count <= 0) {
170 goto end;
171 }
172 consumer_data.stream_count--;
173 if (!stream) {
174 goto end;
175 }
176 if (stream->out_fd >= 0) {
177 ret = close(stream->out_fd);
178 if (ret) {
179 PERROR("close");
180 }
181 }
182 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
183 ret = close(stream->wait_fd);
184 if (ret) {
185 PERROR("close");
186 }
187 }
188 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
189 ret = close(stream->shm_fd);
190 if (ret) {
191 PERROR("close");
192 }
193 }
194 if (!--stream->chan->refcount)
195 free_chan = stream->chan;
196 free(stream);
197end:
198 consumer_data.need_update = 1;
199 pthread_mutex_unlock(&consumer_data.lock);
200
201 if (free_chan)
202 consumer_del_channel(free_chan);
203}
204
205static void consumer_del_stream_rcu(struct rcu_head *head)
206{
207 struct lttng_ht_node_ulong *node =
208 caa_container_of(head, struct lttng_ht_node_ulong, head);
209 struct lttng_consumer_stream *stream =
210 caa_container_of(node, struct lttng_consumer_stream, node);
211
212 consumer_del_stream(stream);
213}
214
215struct lttng_consumer_stream *consumer_allocate_stream(
216 int channel_key, int stream_key,
217 int shm_fd, int wait_fd,
218 enum lttng_consumer_stream_state state,
219 uint64_t mmap_len,
220 enum lttng_event_output output,
221 const char *path_name,
222 uid_t uid,
223 gid_t gid)
224{
225 struct lttng_consumer_stream *stream;
226 int ret;
227
228 stream = zmalloc(sizeof(*stream));
229 if (stream == NULL) {
230 perror("malloc struct lttng_consumer_stream");
231 goto end;
232 }
233 stream->chan = consumer_find_channel(channel_key);
234 if (!stream->chan) {
235 perror("Unable to find channel key");
236 goto end;
237 }
238 stream->chan->refcount++;
239 stream->key = stream_key;
240 stream->shm_fd = shm_fd;
241 stream->wait_fd = wait_fd;
242 stream->out_fd = -1;
243 stream->out_fd_offset = 0;
244 stream->state = state;
245 stream->mmap_len = mmap_len;
246 stream->mmap_base = NULL;
247 stream->output = output;
248 stream->uid = uid;
249 stream->gid = gid;
250 strncpy(stream->path_name, path_name, PATH_MAX - 1);
251 stream->path_name[PATH_MAX - 1] = '\0';
252 lttng_ht_node_init_ulong(&stream->node, stream->key);
253
254 switch (consumer_data.type) {
255 case LTTNG_CONSUMER_KERNEL:
256 break;
257 case LTTNG_CONSUMER32_UST:
258 case LTTNG_CONSUMER64_UST:
259 stream->cpu = stream->chan->cpucount++;
260 ret = lttng_ustconsumer_allocate_stream(stream);
261 if (ret) {
262 free(stream);
263 return NULL;
264 }
265 break;
266 default:
267 ERR("Unknown consumer_data type");
268 assert(0);
269 goto end;
270 }
271 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d)",
272 stream->path_name, stream->key,
273 stream->shm_fd,
274 stream->wait_fd,
275 (unsigned long long) stream->mmap_len,
276 stream->out_fd);
277end:
278 return stream;
279}
280
281/*
282 * Add a stream to the global list protected by a mutex.
283 */
284int consumer_add_stream(struct lttng_consumer_stream *stream)
285{
286 int ret = 0;
287
288 pthread_mutex_lock(&consumer_data.lock);
289 /* Steal stream identifier, for UST */
290 consumer_steal_stream_key(stream->key);
291 rcu_read_lock();
292 lttng_ht_add_unique_ulong(consumer_data.stream_ht, &stream->node);
293 rcu_read_unlock();
294 consumer_data.stream_count++;
295 consumer_data.need_update = 1;
296
297 switch (consumer_data.type) {
298 case LTTNG_CONSUMER_KERNEL:
299 break;
300 case LTTNG_CONSUMER32_UST:
301 case LTTNG_CONSUMER64_UST:
302 /* Streams are in CPU number order (we rely on this) */
303 stream->cpu = stream->chan->nr_streams++;
304 break;
305 default:
306 ERR("Unknown consumer_data type");
307 assert(0);
308 goto end;
309 }
310
311end:
312 pthread_mutex_unlock(&consumer_data.lock);
313 return ret;
314}
315
316/*
317 * Update a stream according to what we just received.
318 */
319void consumer_change_stream_state(int stream_key,
320 enum lttng_consumer_stream_state state)
321{
322 struct lttng_consumer_stream *stream;
323
324 pthread_mutex_lock(&consumer_data.lock);
325 stream = consumer_find_stream(stream_key);
326 if (stream) {
327 stream->state = state;
328 }
329 consumer_data.need_update = 1;
330 pthread_mutex_unlock(&consumer_data.lock);
331}
332
333/*
334 * Remove a channel from the global list protected by a mutex. This
335 * function is also responsible for freeing its data structures.
336 */
337void consumer_del_channel(struct lttng_consumer_channel *channel)
338{
339 int ret;
340 struct lttng_ht_iter iter;
341
342 pthread_mutex_lock(&consumer_data.lock);
343
344 switch (consumer_data.type) {
345 case LTTNG_CONSUMER_KERNEL:
346 break;
347 case LTTNG_CONSUMER32_UST:
348 case LTTNG_CONSUMER64_UST:
349 lttng_ustconsumer_del_channel(channel);
350 break;
351 default:
352 ERR("Unknown consumer_data type");
353 assert(0);
354 goto end;
355 }
356
357 rcu_read_lock();
358
359 lttng_ht_lookup(consumer_data.channel_ht,
360 (void *)((unsigned long) channel->key), &iter);
361 ret = lttng_ht_del(consumer_data.channel_ht, &iter);
362 assert(!ret);
363
364 rcu_read_unlock();
365
366 if (channel->mmap_base != NULL) {
367 ret = munmap(channel->mmap_base, channel->mmap_len);
368 if (ret != 0) {
369 perror("munmap");
370 }
371 }
372 if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) {
373 ret = close(channel->wait_fd);
374 if (ret) {
375 PERROR("close");
376 }
377 }
378 if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) {
379 ret = close(channel->shm_fd);
380 if (ret) {
381 PERROR("close");
382 }
383 }
384 free(channel);
385end:
386 pthread_mutex_unlock(&consumer_data.lock);
387}
388
389static void consumer_del_channel_rcu(struct rcu_head *head)
390{
391 struct lttng_ht_node_ulong *node =
392 caa_container_of(head, struct lttng_ht_node_ulong, head);
393 struct lttng_consumer_channel *channel=
394 caa_container_of(node, struct lttng_consumer_channel, node);
395
396 consumer_del_channel(channel);
397}
398
399struct lttng_consumer_channel *consumer_allocate_channel(
400 int channel_key,
401 int shm_fd, int wait_fd,
402 uint64_t mmap_len,
403 uint64_t max_sb_size)
404{
405 struct lttng_consumer_channel *channel;
406 int ret;
407
408 channel = zmalloc(sizeof(*channel));
409 if (channel == NULL) {
410 perror("malloc struct lttng_consumer_channel");
411 goto end;
412 }
413 channel->key = channel_key;
414 channel->shm_fd = shm_fd;
415 channel->wait_fd = wait_fd;
416 channel->mmap_len = mmap_len;
417 channel->max_sb_size = max_sb_size;
418 channel->refcount = 0;
419 channel->nr_streams = 0;
420 lttng_ht_node_init_ulong(&channel->node, channel->key);
421
422 switch (consumer_data.type) {
423 case LTTNG_CONSUMER_KERNEL:
424 channel->mmap_base = NULL;
425 channel->mmap_len = 0;
426 break;
427 case LTTNG_CONSUMER32_UST:
428 case LTTNG_CONSUMER64_UST:
429 ret = lttng_ustconsumer_allocate_channel(channel);
430 if (ret) {
431 free(channel);
432 return NULL;
433 }
434 break;
435 default:
436 ERR("Unknown consumer_data type");
437 assert(0);
438 goto end;
439 }
440 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
441 channel->key,
442 channel->shm_fd,
443 channel->wait_fd,
444 (unsigned long long) channel->mmap_len,
445 (unsigned long long) channel->max_sb_size);
446end:
447 return channel;
448}
449
450/*
451 * Add a channel to the global list protected by a mutex.
452 */
453int consumer_add_channel(struct lttng_consumer_channel *channel)
454{
455 pthread_mutex_lock(&consumer_data.lock);
456 /* Steal channel identifier, for UST */
457 consumer_steal_channel_key(channel->key);
458 rcu_read_lock();
459 lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node);
460 rcu_read_unlock();
461 pthread_mutex_unlock(&consumer_data.lock);
462 return 0;
463}
464
465/*
466 * Allocate the pollfd structure and the local view of the out fds to avoid
467 * doing a lookup in the linked list and concurrency issues when writing is
468 * needed. Called with consumer_data.lock held.
469 *
470 * Returns the number of fds in the structures.
471 */
472int consumer_update_poll_array(
473 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
474 struct lttng_consumer_stream **local_stream)
475{
476 int i = 0;
477 struct lttng_ht_iter iter;
478 struct lttng_consumer_stream *stream;
479
480 DBG("Updating poll fd array");
481 cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, stream,
482 node.node) {
483 if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM) {
484 continue;
485 }
486 DBG("Active FD %d", stream->wait_fd);
487 (*pollfd)[i].fd = stream->wait_fd;
488 (*pollfd)[i].events = POLLIN | POLLPRI;
489 local_stream[i] = stream;
490 i++;
491 }
492
493 /*
494 * Insert the consumer_poll_pipe at the end of the array and don't
495 * increment i so nb_fd is the number of real FD.
496 */
497 (*pollfd)[i].fd = ctx->consumer_poll_pipe[0];
498 (*pollfd)[i].events = POLLIN;
499 return i;
500}
501
502/*
503 * Poll on the should_quit pipe and the command socket return -1 on error and
504 * should exit, 0 if data is available on the command socket
505 */
506int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
507{
508 int num_rdy;
509
510restart:
511 num_rdy = poll(consumer_sockpoll, 2, -1);
512 if (num_rdy == -1) {
513 /*
514 * Restart interrupted system call.
515 */
516 if (errno == EINTR) {
517 goto restart;
518 }
519 perror("Poll error");
520 goto exit;
521 }
522 if (consumer_sockpoll[0].revents == POLLIN) {
523 DBG("consumer_should_quit wake up");
524 goto exit;
525 }
526 return 0;
527
528exit:
529 return -1;
530}
531
532/*
533 * Set the error socket.
534 */
535void lttng_consumer_set_error_sock(
536 struct lttng_consumer_local_data *ctx, int sock)
537{
538 ctx->consumer_error_socket = sock;
539}
540
541/*
542 * Set the command socket path.
543 */
544
545void lttng_consumer_set_command_sock_path(
546 struct lttng_consumer_local_data *ctx, char *sock)
547{
548 ctx->consumer_command_sock_path = sock;
549}
550
551/*
552 * Send return code to the session daemon.
553 * If the socket is not defined, we return 0, it is not a fatal error
554 */
555int lttng_consumer_send_error(
556 struct lttng_consumer_local_data *ctx, int cmd)
557{
558 if (ctx->consumer_error_socket > 0) {
559 return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
560 sizeof(enum lttcomm_sessiond_command));
561 }
562
563 return 0;
564}
565
566/*
567 * Close all the tracefiles and stream fds, should be called when all instances
568 * are destroyed.
569 */
570void lttng_consumer_cleanup(void)
571{
572 int ret;
573 struct lttng_ht_iter iter;
574 struct lttng_ht_node_ulong *node;
575
576 rcu_read_lock();
577
578 /*
579 * close all outfd. Called when there are no more threads running (after
580 * joining on the threads), no need to protect list iteration with mutex.
581 */
582 cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, node,
583 node) {
584 ret = lttng_ht_del(consumer_data.stream_ht, &iter);
585 assert(!ret);
586 call_rcu(&node->head, consumer_del_stream_rcu);
587 }
588
589 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node,
590 node) {
591 ret = lttng_ht_del(consumer_data.channel_ht, &iter);
592 assert(!ret);
593 call_rcu(&node->head, consumer_del_channel_rcu);
594 }
595
596 rcu_read_unlock();
597}
598
599/*
600 * Called from signal handler.
601 */
602void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
603{
604 int ret;
605 consumer_quit = 1;
606 ret = write(ctx->consumer_should_quit[1], "4", 1);
607 if (ret < 0) {
608 perror("write consumer quit");
609 }
610}
611
612void lttng_consumer_sync_trace_file(
613 struct lttng_consumer_stream *stream, off_t orig_offset)
614{
615 int outfd = stream->out_fd;
616
617 /*
618 * This does a blocking write-and-wait on any page that belongs to the
619 * subbuffer prior to the one we just wrote.
620 * Don't care about error values, as these are just hints and ways to
621 * limit the amount of page cache used.
622 */
623 if (orig_offset < stream->chan->max_sb_size) {
624 return;
625 }
626 sync_file_range(outfd, orig_offset - stream->chan->max_sb_size,
627 stream->chan->max_sb_size,
628 SYNC_FILE_RANGE_WAIT_BEFORE
629 | SYNC_FILE_RANGE_WRITE
630 | SYNC_FILE_RANGE_WAIT_AFTER);
631 /*
632 * Give hints to the kernel about how we access the file:
633 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
634 * we write it.
635 *
636 * We need to call fadvise again after the file grows because the
637 * kernel does not seem to apply fadvise to non-existing parts of the
638 * file.
639 *
640 * Call fadvise _after_ having waited for the page writeback to
641 * complete because the dirty page writeback semantic is not well
642 * defined. So it can be expected to lead to lower throughput in
643 * streaming.
644 */
645 posix_fadvise(outfd, orig_offset - stream->chan->max_sb_size,
646 stream->chan->max_sb_size, POSIX_FADV_DONTNEED);
647}
648
649/*
650 * Initialise the necessary environnement :
651 * - create a new context
652 * - create the poll_pipe
653 * - create the should_quit pipe (for signal handler)
654 * - create the thread pipe (for splice)
655 *
656 * Takes a function pointer as argument, this function is called when data is
657 * available on a buffer. This function is responsible to do the
658 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
659 * buffer configuration and then kernctl_put_next_subbuf at the end.
660 *
661 * Returns a pointer to the new context or NULL on error.
662 */
663struct lttng_consumer_local_data *lttng_consumer_create(
664 enum lttng_consumer_type type,
665 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
666 struct lttng_consumer_local_data *ctx),
667 int (*recv_channel)(struct lttng_consumer_channel *channel),
668 int (*recv_stream)(struct lttng_consumer_stream *stream),
669 int (*update_stream)(int stream_key, uint32_t state))
670{
671 int ret, i;
672 struct lttng_consumer_local_data *ctx;
673
674 assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
675 consumer_data.type == type);
676 consumer_data.type = type;
677
678 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
679 if (ctx == NULL) {
680 perror("allocating context");
681 goto error;
682 }
683
684 ctx->consumer_error_socket = -1;
685 /* assign the callbacks */
686 ctx->on_buffer_ready = buffer_ready;
687 ctx->on_recv_channel = recv_channel;
688 ctx->on_recv_stream = recv_stream;
689 ctx->on_update_stream = update_stream;
690
691 ret = pipe(ctx->consumer_poll_pipe);
692 if (ret < 0) {
693 perror("Error creating poll pipe");
694 goto error_poll_pipe;
695 }
696
697 ret = pipe(ctx->consumer_should_quit);
698 if (ret < 0) {
699 perror("Error creating recv pipe");
700 goto error_quit_pipe;
701 }
702
703 ret = pipe(ctx->consumer_thread_pipe);
704 if (ret < 0) {
705 perror("Error creating thread pipe");
706 goto error_thread_pipe;
707 }
708
709 return ctx;
710
711
712error_thread_pipe:
713 for (i = 0; i < 2; i++) {
714 int err;
715
716 err = close(ctx->consumer_should_quit[i]);
717 if (err) {
718 PERROR("close");
719 }
720 }
721error_quit_pipe:
722 for (i = 0; i < 2; i++) {
723 int err;
724
725 err = close(ctx->consumer_poll_pipe[i]);
726 if (err) {
727 PERROR("close");
728 }
729 }
730error_poll_pipe:
731 free(ctx);
732error:
733 return NULL;
734}
735
736/*
737 * Close all fds associated with the instance and free the context.
738 */
739void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
740{
741 int ret;
742
743 ret = close(ctx->consumer_error_socket);
744 if (ret) {
745 PERROR("close");
746 }
747 ret = close(ctx->consumer_thread_pipe[0]);
748 if (ret) {
749 PERROR("close");
750 }
751 ret = close(ctx->consumer_thread_pipe[1]);
752 if (ret) {
753 PERROR("close");
754 }
755 ret = close(ctx->consumer_poll_pipe[0]);
756 if (ret) {
757 PERROR("close");
758 }
759 ret = close(ctx->consumer_poll_pipe[1]);
760 if (ret) {
761 PERROR("close");
762 }
763 ret = close(ctx->consumer_should_quit[0]);
764 if (ret) {
765 PERROR("close");
766 }
767 ret = close(ctx->consumer_should_quit[1]);
768 if (ret) {
769 PERROR("close");
770 }
771 unlink(ctx->consumer_command_sock_path);
772 free(ctx);
773}
774
775/*
776 * Mmap the ring buffer, read it and write the data to the tracefile.
777 *
778 * Returns the number of bytes written
779 */
780ssize_t lttng_consumer_on_read_subbuffer_mmap(
781 struct lttng_consumer_local_data *ctx,
782 struct lttng_consumer_stream *stream, unsigned long len)
783{
784 switch (consumer_data.type) {
785 case LTTNG_CONSUMER_KERNEL:
786 return lttng_kconsumer_on_read_subbuffer_mmap(ctx, stream, len);
787 case LTTNG_CONSUMER32_UST:
788 case LTTNG_CONSUMER64_UST:
789 return lttng_ustconsumer_on_read_subbuffer_mmap(ctx, stream, len);
790 default:
791 ERR("Unknown consumer_data type");
792 assert(0);
793 }
794}
795
796/*
797 * Splice the data from the ring buffer to the tracefile.
798 *
799 * Returns the number of bytes spliced.
800 */
801ssize_t lttng_consumer_on_read_subbuffer_splice(
802 struct lttng_consumer_local_data *ctx,
803 struct lttng_consumer_stream *stream, unsigned long len)
804{
805 switch (consumer_data.type) {
806 case LTTNG_CONSUMER_KERNEL:
807 return lttng_kconsumer_on_read_subbuffer_splice(ctx, stream, len);
808 case LTTNG_CONSUMER32_UST:
809 case LTTNG_CONSUMER64_UST:
810 return -ENOSYS;
811 default:
812 ERR("Unknown consumer_data type");
813 assert(0);
814 return -ENOSYS;
815 }
816
817}
818
819/*
820 * Take a snapshot for a specific fd
821 *
822 * Returns 0 on success, < 0 on error
823 */
824int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
825 struct lttng_consumer_stream *stream)
826{
827 switch (consumer_data.type) {
828 case LTTNG_CONSUMER_KERNEL:
829 return lttng_kconsumer_take_snapshot(ctx, stream);
830 case LTTNG_CONSUMER32_UST:
831 case LTTNG_CONSUMER64_UST:
832 return lttng_ustconsumer_take_snapshot(ctx, stream);
833 default:
834 ERR("Unknown consumer_data type");
835 assert(0);
836 return -ENOSYS;
837 }
838
839}
840
841/*
842 * Get the produced position
843 *
844 * Returns 0 on success, < 0 on error
845 */
846int lttng_consumer_get_produced_snapshot(
847 struct lttng_consumer_local_data *ctx,
848 struct lttng_consumer_stream *stream,
849 unsigned long *pos)
850{
851 switch (consumer_data.type) {
852 case LTTNG_CONSUMER_KERNEL:
853 return lttng_kconsumer_get_produced_snapshot(ctx, stream, pos);
854 case LTTNG_CONSUMER32_UST:
855 case LTTNG_CONSUMER64_UST:
856 return lttng_ustconsumer_get_produced_snapshot(ctx, stream, pos);
857 default:
858 ERR("Unknown consumer_data type");
859 assert(0);
860 return -ENOSYS;
861 }
862}
863
864int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
865 int sock, struct pollfd *consumer_sockpoll)
866{
867 switch (consumer_data.type) {
868 case LTTNG_CONSUMER_KERNEL:
869 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
870 case LTTNG_CONSUMER32_UST:
871 case LTTNG_CONSUMER64_UST:
872 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
873 default:
874 ERR("Unknown consumer_data type");
875 assert(0);
876 return -ENOSYS;
877 }
878}
879
880/*
881 * This thread polls the fds in the set to consume the data and write
882 * it to tracefile if necessary.
883 */
884void *lttng_consumer_thread_poll_fds(void *data)
885{
886 int num_rdy, num_hup, high_prio, ret, i;
887 struct pollfd *pollfd = NULL;
888 /* local view of the streams */
889 struct lttng_consumer_stream **local_stream = NULL;
890 /* local view of consumer_data.fds_count */
891 int nb_fd = 0;
892 char tmp;
893 int tmp2;
894 struct lttng_consumer_local_data *ctx = data;
895
896 rcu_register_thread();
897
898 local_stream = zmalloc(sizeof(struct lttng_consumer_stream));
899
900 while (1) {
901 high_prio = 0;
902 num_hup = 0;
903
904 /*
905 * the fds set has been updated, we need to update our
906 * local array as well
907 */
908 pthread_mutex_lock(&consumer_data.lock);
909 if (consumer_data.need_update) {
910 if (pollfd != NULL) {
911 free(pollfd);
912 pollfd = NULL;
913 }
914 if (local_stream != NULL) {
915 free(local_stream);
916 local_stream = NULL;
917 }
918
919 /* allocate for all fds + 1 for the consumer_poll_pipe */
920 pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
921 if (pollfd == NULL) {
922 perror("pollfd malloc");
923 pthread_mutex_unlock(&consumer_data.lock);
924 goto end;
925 }
926
927 /* allocate for all fds + 1 for the consumer_poll_pipe */
928 local_stream = zmalloc((consumer_data.stream_count + 1) *
929 sizeof(struct lttng_consumer_stream));
930 if (local_stream == NULL) {
931 perror("local_stream malloc");
932 pthread_mutex_unlock(&consumer_data.lock);
933 goto end;
934 }
935 ret = consumer_update_poll_array(ctx, &pollfd, local_stream);
936 if (ret < 0) {
937 ERR("Error in allocating pollfd or local_outfds");
938 lttng_consumer_send_error(ctx, CONSUMERD_POLL_ERROR);
939 pthread_mutex_unlock(&consumer_data.lock);
940 goto end;
941 }
942 nb_fd = ret;
943 consumer_data.need_update = 0;
944 }
945 pthread_mutex_unlock(&consumer_data.lock);
946
947 /* No FDs and consumer_quit, consumer_cleanup the thread */
948 if (nb_fd == 0 && consumer_quit == 1) {
949 goto end;
950 }
951 /* poll on the array of fds */
952 restart:
953 DBG("polling on %d fd", nb_fd + 1);
954 num_rdy = poll(pollfd, nb_fd + 1, consumer_poll_timeout);
955 DBG("poll num_rdy : %d", num_rdy);
956 if (num_rdy == -1) {
957 /*
958 * Restart interrupted system call.
959 */
960 if (errno == EINTR) {
961 goto restart;
962 }
963 perror("Poll error");
964 lttng_consumer_send_error(ctx, CONSUMERD_POLL_ERROR);
965 goto end;
966 } else if (num_rdy == 0) {
967 DBG("Polling thread timed out");
968 goto end;
969 }
970
971 /*
972 * If the consumer_poll_pipe triggered poll go
973 * directly to the beginning of the loop to update the
974 * array. We want to prioritize array update over
975 * low-priority reads.
976 */
977 if (pollfd[nb_fd].revents & POLLIN) {
978 DBG("consumer_poll_pipe wake up");
979 tmp2 = read(ctx->consumer_poll_pipe[0], &tmp, 1);
980 if (tmp2 < 0) {
981 perror("read consumer poll");
982 }
983 continue;
984 }
985
986 /* Take care of high priority channels first. */
987 for (i = 0; i < nb_fd; i++) {
988 if (pollfd[i].revents & POLLPRI) {
989 ssize_t len;
990
991 DBG("Urgent read on fd %d", pollfd[i].fd);
992 high_prio = 1;
993 len = ctx->on_buffer_ready(local_stream[i], ctx);
994 /* it's ok to have an unavailable sub-buffer */
995 if (len < 0 && len != -EAGAIN) {
996 goto end;
997 } else if (len > 0) {
998 local_stream[i]->data_read = 1;
999 }
1000 }
1001 }
1002
1003 /*
1004 * If we read high prio channel in this loop, try again
1005 * for more high prio data.
1006 */
1007 if (high_prio) {
1008 continue;
1009 }
1010
1011 /* Take care of low priority channels. */
1012 for (i = 0; i < nb_fd; i++) {
1013 if ((pollfd[i].revents & POLLIN) ||
1014 local_stream[i]->hangup_flush_done) {
1015 ssize_t len;
1016
1017 assert(!(pollfd[i].revents & POLLERR));
1018 assert(!(pollfd[i].revents & POLLNVAL));
1019 DBG("Normal read on fd %d", pollfd[i].fd);
1020 len = ctx->on_buffer_ready(local_stream[i], ctx);
1021 /* it's ok to have an unavailable sub-buffer */
1022 if (len < 0 && len != -EAGAIN) {
1023 goto end;
1024 } else if (len > 0) {
1025 local_stream[i]->data_read = 1;
1026 }
1027 }
1028 }
1029
1030 /* Handle hangup and errors */
1031 for (i = 0; i < nb_fd; i++) {
1032 if (!local_stream[i]->hangup_flush_done
1033 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
1034 && (consumer_data.type == LTTNG_CONSUMER32_UST
1035 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
1036 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1037 pollfd[i].fd);
1038 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
1039 /* Attempt read again, for the data we just flushed. */
1040 local_stream[i]->data_read = 1;
1041 }
1042 /*
1043 * If the poll flag is HUP/ERR/NVAL and we have
1044 * read no data in this pass, we can remove the
1045 * stream from its hash table.
1046 */
1047 if ((pollfd[i].revents & POLLHUP)) {
1048 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
1049 if (!local_stream[i]->data_read) {
1050 rcu_read_lock();
1051 consumer_del_stream_rcu(&local_stream[i]->node.head);
1052 rcu_read_unlock();
1053 num_hup++;
1054 }
1055 } else if (pollfd[i].revents & POLLERR) {
1056 ERR("Error returned in polling fd %d.", pollfd[i].fd);
1057 if (!local_stream[i]->data_read) {
1058 rcu_read_lock();
1059 consumer_del_stream_rcu(&local_stream[i]->node.head);
1060 rcu_read_unlock();
1061 num_hup++;
1062 }
1063 } else if (pollfd[i].revents & POLLNVAL) {
1064 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
1065 if (!local_stream[i]->data_read) {
1066 rcu_read_lock();
1067 consumer_del_stream_rcu(&local_stream[i]->node.head);
1068 rcu_read_unlock();
1069 num_hup++;
1070 }
1071 }
1072 local_stream[i]->data_read = 0;
1073 }
1074 }
1075end:
1076 DBG("polling thread exiting");
1077 if (pollfd != NULL) {
1078 free(pollfd);
1079 pollfd = NULL;
1080 }
1081 if (local_stream != NULL) {
1082 free(local_stream);
1083 local_stream = NULL;
1084 }
1085 rcu_unregister_thread();
1086 return NULL;
1087}
1088
1089/*
1090 * This thread listens on the consumerd socket and receives the file
1091 * descriptors from the session daemon.
1092 */
1093void *lttng_consumer_thread_receive_fds(void *data)
1094{
1095 int sock, client_socket, ret;
1096 /*
1097 * structure to poll for incoming data on communication socket avoids
1098 * making blocking sockets.
1099 */
1100 struct pollfd consumer_sockpoll[2];
1101 struct lttng_consumer_local_data *ctx = data;
1102
1103 rcu_register_thread();
1104
1105 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
1106 unlink(ctx->consumer_command_sock_path);
1107 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
1108 if (client_socket < 0) {
1109 ERR("Cannot create command socket");
1110 goto end;
1111 }
1112
1113 ret = lttcomm_listen_unix_sock(client_socket);
1114 if (ret < 0) {
1115 goto end;
1116 }
1117
1118 DBG("Sending ready command to lttng-sessiond");
1119 ret = lttng_consumer_send_error(ctx, CONSUMERD_COMMAND_SOCK_READY);
1120 /* return < 0 on error, but == 0 is not fatal */
1121 if (ret < 0) {
1122 ERR("Error sending ready command to lttng-sessiond");
1123 goto end;
1124 }
1125
1126 ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
1127 if (ret < 0) {
1128 perror("fcntl O_NONBLOCK");
1129 goto end;
1130 }
1131
1132 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1133 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
1134 consumer_sockpoll[0].events = POLLIN | POLLPRI;
1135 consumer_sockpoll[1].fd = client_socket;
1136 consumer_sockpoll[1].events = POLLIN | POLLPRI;
1137
1138 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
1139 goto end;
1140 }
1141 DBG("Connection on client_socket");
1142
1143 /* Blocking call, waiting for transmission */
1144 sock = lttcomm_accept_unix_sock(client_socket);
1145 if (sock <= 0) {
1146 WARN("On accept");
1147 goto end;
1148 }
1149 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
1150 if (ret < 0) {
1151 perror("fcntl O_NONBLOCK");
1152 goto end;
1153 }
1154
1155 /* update the polling structure to poll on the established socket */
1156 consumer_sockpoll[1].fd = sock;
1157 consumer_sockpoll[1].events = POLLIN | POLLPRI;
1158
1159 while (1) {
1160 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
1161 goto end;
1162 }
1163 DBG("Incoming command on sock");
1164 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
1165 if (ret == -ENOENT) {
1166 DBG("Received STOP command");
1167 goto end;
1168 }
1169 if (ret < 0) {
1170 ERR("Communication interrupted on command socket");
1171 goto end;
1172 }
1173 if (consumer_quit) {
1174 DBG("consumer_thread_receive_fds received quit from signal");
1175 goto end;
1176 }
1177 DBG("received fds on sock");
1178 }
1179end:
1180 DBG("consumer_thread_receive_fds exiting");
1181
1182 /*
1183 * when all fds have hung up, the polling thread
1184 * can exit cleanly
1185 */
1186 consumer_quit = 1;
1187
1188 /*
1189 * 2s of grace period, if no polling events occur during
1190 * this period, the polling thread will exit even if there
1191 * are still open FDs (should not happen, but safety mechanism).
1192 */
1193 consumer_poll_timeout = LTTNG_CONSUMER_POLL_TIMEOUT;
1194
1195 /* wake up the polling thread */
1196 ret = write(ctx->consumer_poll_pipe[1], "4", 1);
1197 if (ret < 0) {
1198 perror("poll pipe write");
1199 }
1200 rcu_unregister_thread();
1201 return NULL;
1202}
1203
1204ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
1205 struct lttng_consumer_local_data *ctx)
1206{
1207 switch (consumer_data.type) {
1208 case LTTNG_CONSUMER_KERNEL:
1209 return lttng_kconsumer_read_subbuffer(stream, ctx);
1210 case LTTNG_CONSUMER32_UST:
1211 case LTTNG_CONSUMER64_UST:
1212 return lttng_ustconsumer_read_subbuffer(stream, ctx);
1213 default:
1214 ERR("Unknown consumer_data type");
1215 assert(0);
1216 return -ENOSYS;
1217 }
1218}
1219
1220int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
1221{
1222 switch (consumer_data.type) {
1223 case LTTNG_CONSUMER_KERNEL:
1224 return lttng_kconsumer_on_recv_stream(stream);
1225 case LTTNG_CONSUMER32_UST:
1226 case LTTNG_CONSUMER64_UST:
1227 return lttng_ustconsumer_on_recv_stream(stream);
1228 default:
1229 ERR("Unknown consumer_data type");
1230 assert(0);
1231 return -ENOSYS;
1232 }
1233}
1234
1235/*
1236 * Allocate and set consumer data hash tables.
1237 */
1238void lttng_consumer_init(void)
1239{
1240 consumer_data.stream_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1241 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1242}
1243
This page took 0.026513 seconds and 4 git commands to generate.