2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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; either version 2
8 * of the License, or (at your option) any later version.
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.
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.
32 #include <sys/socket.h>
34 #include <sys/types.h>
35 #include <urcu/list.h>
40 #include "libkernelctl.h"
41 #include "liblttsessiondcomm.h"
42 #include "kconsumerd.h"
44 /* Init the list of FDs */
45 static struct ltt_kconsumerd_fd_list kconsumerd_fd_list
= {
46 .head
= CDS_LIST_HEAD_INIT(kconsumerd_fd_list
.head
),
49 /* Number of element for the list below. */
50 static unsigned int fds_count
;
52 /* If the local array of FDs needs update in the poll function */
53 static unsigned int update_fd_array
= 1;
55 /* lock the fd array and structures */
56 static pthread_mutex_t kconsumerd_lock_fds
;
58 /* the two threads (receive fd and poll) */
59 static pthread_t threads
[2];
61 /* communication with splice */
62 static int thread_pipe
[2];
64 /* pipe to wake the poll thread when necessary */
65 static int poll_pipe
[2];
67 /* socket to communicate errors with sessiond */
68 static int error_socket
= -1;
70 /* to count the number of time the user pressed ctrl+c */
71 static int sigintcount
= 0;
73 /* Argument variables */
76 static int opt_daemon
;
77 static const char *progname
;
78 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
79 static char error_sock_path
[PATH_MAX
]; /* Global error path */
84 * Remove a fd from the global list protected by a mutex
86 static void del_fd(struct ltt_kconsumerd_fd
*lcf
)
88 DBG("Removing %d", lcf
->consumerd_fd
);
89 pthread_mutex_lock(&kconsumerd_lock_fds
);
90 cds_list_del(&lcf
->list
);
93 DBG("Removed ltt_kconsumerd_fd");
96 close(lcf
->consumerd_fd
);
101 pthread_mutex_unlock(&kconsumerd_lock_fds
);
107 * Cleanup the daemon's socket on exit
109 static void cleanup()
111 struct ltt_kconsumerd_fd
*iter
;
113 /* remove the socket file */
114 unlink(command_sock_path
);
116 /* unblock the threads */
117 WARN("Terminating the threads before exiting");
118 pthread_cancel(threads
[0]);
119 pthread_cancel(threads
[1]);
121 /* close all outfd */
122 cds_list_for_each_entry(iter
, &kconsumerd_fd_list
.head
, list
) {
130 * send return code to ltt-sessiond
132 static int send_error(enum lttcomm_return_code cmd
)
134 if (error_socket
> 0) {
135 return lttcomm_send_unix_sock(error_socket
, &cmd
,
136 sizeof(enum lttcomm_sessiond_command
));
145 * Add a fd to the global list protected by a mutex
147 static int add_fd(struct lttcomm_kconsumerd_msg
*buf
, int consumerd_fd
)
149 struct ltt_kconsumerd_fd
*tmp_fd
;
152 tmp_fd
= malloc(sizeof(struct ltt_kconsumerd_fd
));
153 tmp_fd
->sessiond_fd
= buf
->fd
;
154 tmp_fd
->consumerd_fd
= consumerd_fd
;
155 tmp_fd
->state
= buf
->state
;
156 tmp_fd
->max_sb_size
= buf
->max_sb_size
;
157 strncpy(tmp_fd
->path_name
, buf
->path_name
, PATH_MAX
);
159 /* Opening the tracefile in write mode */
160 DBG("Opening %s for writing", tmp_fd
->path_name
);
161 ret
= open(tmp_fd
->path_name
,
162 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
164 ERR("Opening %s", tmp_fd
->path_name
);
168 tmp_fd
->out_fd
= ret
;
169 tmp_fd
->out_fd_offset
= 0;
171 DBG("Adding %s (%d, %d, %d)", tmp_fd
->path_name
,
172 tmp_fd
->sessiond_fd
, tmp_fd
->consumerd_fd
, tmp_fd
->out_fd
);
174 pthread_mutex_lock(&kconsumerd_lock_fds
);
175 cds_list_add(&tmp_fd
->list
, &kconsumerd_fd_list
.head
);
177 pthread_mutex_unlock(&kconsumerd_lock_fds
);
187 * Signal handler for the daemon
189 static void sighandler(int sig
)
191 if (sig
== SIGINT
&& sigintcount
++ == 0) {
192 DBG("ignoring first SIGINT");
204 * Setup signal handler for :
205 * SIGINT, SIGTERM, SIGPIPE
207 static int set_signal_handler(void)
213 if ((ret
= sigemptyset(&sigset
)) < 0) {
214 perror("sigemptyset");
218 sa
.sa_handler
= sighandler
;
221 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
226 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
231 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
242 * Splice the data from the ring buffer to the tracefile.
243 * Returns the number of bytes spliced
245 static int on_read_subbuffer(struct ltt_kconsumerd_fd
*kconsumerd_fd
,
250 off_t orig_offset
= kconsumerd_fd
->out_fd_offset
;
251 int fd
= kconsumerd_fd
->consumerd_fd
;
252 int outfd
= kconsumerd_fd
->out_fd
;
255 DBG("splice chan to pipe offset %lu (fd : %d)",
256 (unsigned long)offset
, fd
);
257 ret
= splice(fd
, &offset
, thread_pipe
[1], NULL
, len
,
258 SPLICE_F_MOVE
| SPLICE_F_MORE
);
259 DBG("splice chan to pipe ret %ld", ret
);
262 perror("Error in relay splice");
266 ret
= splice(thread_pipe
[0], NULL
, outfd
, NULL
, ret
,
267 SPLICE_F_MOVE
| SPLICE_F_MORE
);
268 DBG("splice pipe to file %ld", ret
);
271 perror("Error in file splice");
277 /* This won't block, but will start writeout asynchronously */
278 sync_file_range(outfd
, kconsumerd_fd
->out_fd_offset
, ret
,
279 SYNC_FILE_RANGE_WRITE
);
280 kconsumerd_fd
->out_fd_offset
+= ret
;
284 * This does a blocking write-and-wait on any page that belongs to the
285 * subbuffer prior to the one we just wrote.
286 * Don't care about error values, as these are just hints and ways to
287 * limit the amount of page cache used.
289 if (orig_offset
>= kconsumerd_fd
->max_sb_size
) {
290 sync_file_range(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
291 kconsumerd_fd
->max_sb_size
,
292 SYNC_FILE_RANGE_WAIT_BEFORE
293 | SYNC_FILE_RANGE_WRITE
294 | SYNC_FILE_RANGE_WAIT_AFTER
);
296 * Give hints to the kernel about how we access the file:
297 * POSIX_FADV_DONTNEED : we won't re-access data in a near
298 * future after we write it.
299 * We need to call fadvise again after the file grows because
300 * the kernel does not seem to apply fadvise to non-existing
302 * Call fadvise _after_ having waited for the page writeback to
303 * complete because the dirty page writeback semantic is not
304 * well defined. So it can be expected to lead to lower
305 * throughput in streaming.
307 posix_fadvise(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
308 kconsumerd_fd
->max_sb_size
, POSIX_FADV_DONTNEED
);
313 /* send the appropriate error description to sessiond */
316 send_error(KCONSUMERD_SPLICE_EBADF
);
319 send_error(KCONSUMERD_SPLICE_EINVAL
);
322 send_error(KCONSUMERD_SPLICE_ENOMEM
);
325 send_error(KCONSUMERD_SPLICE_ESPIPE
);
336 * Consume data on a file descriptor and write it on a trace file
338 static int read_subbuffer(struct ltt_kconsumerd_fd
*kconsumerd_fd
)
343 int infd
= kconsumerd_fd
->consumerd_fd
;
345 DBG("In read_subbuffer (infd : %d)", infd
);
346 /* Get the next subbuffer */
347 err
= kernctl_get_next_subbuf(infd
);
350 perror("Reserving sub buffer failed (everything is normal, "
351 "it is due to concurrency)");
355 /* read the whole subbuffer */
356 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
359 perror("Getting sub-buffer len failed.");
363 /* splice the subbuffer to the tracefile */
364 ret
= on_read_subbuffer(kconsumerd_fd
, len
);
367 * display the error but continue processing to try
368 * to release the subbuffer
370 ERR("Error splicing to tracefile");
373 err
= kernctl_put_next_subbuf(infd
);
376 if (errno
== EFAULT
) {
377 perror("Error in unreserving sub buffer\n");
378 } else if (errno
== EIO
) {
379 /* Should never happen with newer LTTng versions */
380 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
392 * Update a fd according to what we just received
394 static void change_fd_state(int sessiond_fd
,
395 enum kconsumerd_fd_state state
)
397 struct ltt_kconsumerd_fd
*iter
;
398 cds_list_for_each_entry(iter
, &kconsumerd_fd_list
.head
, list
) {
399 if (iter
->sessiond_fd
== sessiond_fd
) {
409 * Receives an array of file descriptors and the associated
410 * structures describing each fd (path name).
411 * Returns the size of received data
413 static int consumerd_recv_fd(int sfd
, int size
,
414 enum kconsumerd_command cmd_type
)
419 struct cmsghdr
*cmsg
;
421 char tmp
[CMSG_SPACE(size
)];
422 struct lttcomm_kconsumerd_msg
*buf
;
423 /* the number of fds we are about to receive */
424 nb_fd
= size
/sizeof(struct lttcomm_kconsumerd_msg
);
428 memset(&msg
, 0, sizeof(msg
));
430 /* Prepare to receive the structures */
431 iov
[0].iov_base
= buf
;
432 iov
[0].iov_len
= size
;
436 msg
.msg_control
= tmp
;
437 msg
.msg_controllen
= sizeof(tmp
);
439 DBG("Waiting to receive fds");
440 if ((ret
= recvmsg(sfd
, &msg
, 0)) < 0) {
444 ERR("Received only %d, expected %d", ret
, size
);
445 send_error(KCONSUMERD_ERROR_RECV_FD
);
449 cmsg
= CMSG_FIRSTHDR(&msg
);
451 ERR("Invalid control message header");
453 send_error(KCONSUMERD_ERROR_RECV_FD
);
457 /* if we received fds */
458 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
459 DBG("Receive : expecting %d fds", nb_fd
);
460 for (i
= 0; i
< nb_fd
; i
++) {
463 DBG("add_fd %s (%d)", buf
[i
].path_name
, ((int *)CMSG_DATA(cmsg
))[i
]);
464 ret
= add_fd(&buf
[i
], ((int *)CMSG_DATA(cmsg
))[i
]);
466 send_error(KCONSUMERD_OUTFD_ERROR
);
471 change_fd_state(buf
[i
].fd
, buf
[i
].state
);
477 /* flag to tell the polling thread to update its fd array */
479 /* signal the poll thread */
480 tmp2
= write(poll_pipe
[1], "4", 1);
482 ERR("Didn't received any fd");
483 send_error(KCONSUMERD_ERROR_RECV_FD
);
499 * This thread listens on the consumerd socket and
500 * receives the file descriptors from ltt-sessiond
502 static void *thread_receive_fds(void *data
)
504 int sock
, client_socket
, ret
;
505 struct lttcomm_kconsumerd_header tmp
;
507 DBG("Creating command socket %s", command_sock_path
);
508 unlink(command_sock_path
);
509 client_socket
= lttcomm_create_unix_sock(command_sock_path
);
510 if (client_socket
< 0) {
511 ERR("Cannot create command socket");
515 ret
= lttcomm_listen_unix_sock(client_socket
);
520 DBG("Sending ready command to ltt-sessiond");
521 ret
= send_error(KCONSUMERD_COMMAND_SOCK_READY
);
523 ERR("Error sending ready command to ltt-sessiond");
527 /* Blocking call, waiting for transmission */
528 sock
= lttcomm_accept_unix_sock(client_socket
);
534 /* We first get the number of fd we are about to receive */
535 ret
= lttcomm_recv_unix_sock(sock
, &tmp
,
536 sizeof(struct lttcomm_kconsumerd_header
));
538 ERR("Receiving the lttcomm_kconsumerd_header, exiting");
541 ret
= consumerd_recv_fd(sock
, tmp
.payload_size
, tmp
.cmd_type
);
543 ERR("Receiving the FD, exiting");
555 * Allocate the pollfd structure and the local view of the out fds
556 * to avoid doing a lookup in the linked list and concurrency issues
557 * when writing is needed.
558 * Returns the number of fds in the structures
560 static int update_poll_array(struct pollfd
**pollfd
,
561 struct ltt_kconsumerd_fd
**local_kconsumerd_fd
)
563 struct ltt_kconsumerd_fd
*iter
;
567 DBG("Updating poll fd array");
568 pthread_mutex_lock(&kconsumerd_lock_fds
);
570 cds_list_for_each_entry(iter
, &kconsumerd_fd_list
.head
, list
) {
571 DBG("Inside for each");
572 if (iter
->state
== ACTIVE_FD
) {
573 DBG("Active FD %d", iter
->consumerd_fd
);
574 (*pollfd
)[i
].fd
= iter
->consumerd_fd
;
575 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
576 local_kconsumerd_fd
[i
] = iter
;
578 } else if (iter
->state
== DELETE_FD
) {
583 * insert the poll_pipe at the end of the array and don't increment i
584 * so nb_fd is the number of real FD
586 (*pollfd
)[i
].fd
= poll_pipe
[0];
587 (*pollfd
)[i
].events
= POLLIN
;
590 pthread_mutex_unlock(&kconsumerd_lock_fds
);
598 * This thread polls the fds in the ltt_fd_list to consume the data
599 * and write it to tracefile if necessary.
601 static void *thread_poll_fds(void *data
)
603 int num_rdy
, num_hup
, high_prio
, ret
, i
;
604 struct pollfd
*pollfd
= NULL
;
605 /* local view of the fds */
606 struct ltt_kconsumerd_fd
**local_kconsumerd_fd
= NULL
;
607 /* local view of fds_count */
612 ret
= pipe(thread_pipe
);
614 perror("Error creating pipe");
618 local_kconsumerd_fd
= malloc(sizeof(struct ltt_kconsumerd_fd
));
625 * the ltt_fd_list has been updated, we need to update our
626 * local array as well
628 if (update_fd_array
== 1) {
629 if (pollfd
!= NULL
) {
633 if (local_kconsumerd_fd
!= NULL
) {
634 free(local_kconsumerd_fd
);
635 local_kconsumerd_fd
= NULL
;
637 /* allocate for all fds + 1 for the poll_pipe */
638 pollfd
= malloc((fds_count
+ 1) * sizeof(struct pollfd
));
639 if (pollfd
== NULL
) {
640 perror("pollfd malloc");
643 /* allocate for all fds + 1 for the poll_pipe */
644 local_kconsumerd_fd
= malloc((fds_count
+ 1) * sizeof(struct ltt_kconsumerd_fd
));
645 if (local_kconsumerd_fd
== NULL
) {
646 perror("local_kconsumerd_fd malloc");
650 ret
= update_poll_array(&pollfd
, local_kconsumerd_fd
);
652 ERR("Error in allocating pollfd or local_outfds");
653 send_error(KCONSUMERD_POLL_ERROR
);
659 /* poll on the array of fds */
660 DBG("polling on %d fd", nb_fd
+ 1);
661 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
662 DBG("poll num_rdy : %d", num_rdy
);
664 perror("Poll error");
665 send_error(KCONSUMERD_POLL_ERROR
);
670 * if only the poll_pipe triggered poll to return just return to the
671 * beginning of the loop to update the array
673 if (num_rdy
== 1 && pollfd
[nb_fd
].revents
== POLLIN
) {
674 DBG("poll_pipe wake up");
675 tmp2
= read(poll_pipe
[0], &tmp
, 1);
679 /* Take care of high priority channels first. */
680 for (i
= 0; i
< nb_fd
; i
++) {
681 switch(pollfd
[i
].revents
) {
683 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
685 send_error(KCONSUMERD_POLL_ERROR
);
688 ERR("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
692 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
693 send_error(KCONSUMERD_POLL_NVAL
);
697 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
699 ret
= read_subbuffer(local_kconsumerd_fd
[i
]);
700 /* it's ok to have an unavailable sub-buffer (FIXME : is it ?) */
708 /* If every buffer FD has hung up, we end the read loop here */
709 if (nb_fd
> 0 && num_hup
== nb_fd
) {
710 DBG("every buffer FD has hung up\n");
711 send_error(KCONSUMERD_POLL_HUP
);
715 /* Take care of low priority channels. */
716 if (high_prio
== 0) {
717 for (i
= 0; i
< nb_fd
; i
++) {
718 if (pollfd
[i
].revents
== POLLIN
) {
719 DBG("Normal read on fd %d", pollfd
[i
].fd
);
720 ret
= read_subbuffer(local_kconsumerd_fd
[i
]);
721 /* it's ok to have an unavailable subbuffer (FIXME : is it ?) */
730 if (pollfd
!= NULL
) {
734 if (local_kconsumerd_fd
!= NULL
) {
735 free(local_kconsumerd_fd
);
736 local_kconsumerd_fd
= NULL
;
743 * usage function on stderr
745 static void usage(void)
747 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
748 fprintf(stderr
, " -h, --help "
749 "Display this usage.\n");
750 fprintf(stderr
, " -c, --kconsumerd-cmd-sock PATH "
751 "Specify path for the command socket\n");
752 fprintf(stderr
, " -e, --kconsumerd-err-sock PATH "
753 "Specify path for the error socket\n");
754 fprintf(stderr
, " -d, --daemonize "
755 "Start as a daemon.\n");
756 fprintf(stderr
, " -q, --quiet "
757 "No output at all.\n");
758 fprintf(stderr
, " -v, --verbose "
759 "Verbose mode. Activate DBG() macro.\n");
760 fprintf(stderr
, " -V, --version "
761 "Show version number.\n");
765 * daemon argument parsing
767 static void parse_args(int argc
, char **argv
)
771 static struct option long_options
[] = {
772 { "kconsumerd-cmd-sock", 1, 0, 'c' },
773 { "kconsumerd-err-sock", 1, 0, 'e' },
774 { "daemonize", 0, 0, 'd' },
775 { "help", 0, 0, 'h' },
776 { "quiet", 0, 0, 'q' },
777 { "verbose", 0, 0, 'v' },
778 { "version", 0, 0, 'V' },
783 int option_index
= 0;
784 c
= getopt_long(argc
, argv
, "dhqvV" "c:e:", long_options
, &option_index
);
791 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
793 fprintf(stderr
, " with arg %s\n", optarg
);
797 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
800 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
815 fprintf(stdout
, "%s\n", VERSION
);
828 int main(int argc
, char **argv
)
834 /* Parse arguments */
836 parse_args(argc
, argv
);
847 if (strlen(command_sock_path
) == 0) {
848 snprintf(command_sock_path
, PATH_MAX
,
849 KCONSUMERD_CMD_SOCK_PATH
);
851 if (strlen(error_sock_path
) == 0) {
852 snprintf(error_sock_path
, PATH_MAX
,
853 KCONSUMERD_ERR_SOCK_PATH
);
856 if (set_signal_handler() < 0) {
860 /* create the pipe to wake to polling thread when needed */
861 ret
= pipe(poll_pipe
);
863 perror("Error creating poll pipe");
867 /* Connect to the socket created by ltt-sessiond to report errors */
868 DBG("Connecting to error socket %s", error_sock_path
);
869 error_socket
= lttcomm_connect_unix_sock(error_sock_path
);
870 /* not a fatal error, but all communication with ltt-sessiond will fail */
871 if (error_socket
< 0) {
872 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
875 /* Create the thread to manage the receive of fd */
876 ret
= pthread_create(&threads
[0], NULL
, thread_receive_fds
, (void *) NULL
);
878 perror("pthread_create");
882 /* Create thread to manage the polling/writing of traces */
883 ret
= pthread_create(&threads
[1], NULL
, thread_poll_fds
, (void *) NULL
);
885 perror("pthread_create");
889 for (i
= 0; i
< 2; i
++) {
890 ret
= pthread_join(threads
[i
], &status
);
892 perror("pthread_join");
897 send_error(KCONSUMERD_EXIT_SUCCESS
);
902 send_error(KCONSUMERD_EXIT_FAILURE
);