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 /* socket to communicate errors with sessiond */
65 static int error_socket
= -1;
67 /* Argument variables */
70 static int opt_daemon
;
71 static const char *progname
;
72 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
73 static char error_sock_path
[PATH_MAX
]; /* Global error path */
78 * Cleanup the daemon's socket on exit
82 unlink(command_sock_path
);
87 * send return code to ltt-sessiond
89 static int send_error(enum lttcomm_return_code cmd
)
91 if (error_socket
> 0) {
92 return lttcomm_send_unix_sock(error_socket
, &cmd
,
93 sizeof(enum lttcomm_sessiond_command
));
100 * cleanup_kconsumerd_fd
102 * Close the FDs and frees a ltt_kconsumerd_fd struct
104 static void cleanup_kconsumerd_fd(struct ltt_kconsumerd_fd
*lcf
)
108 close(lcf
->consumerd_fd
);
117 * Add a fd to the global list protected by a mutex
119 static int add_fd(struct lttcomm_kconsumerd_msg
*buf
, int consumerd_fd
)
121 struct ltt_kconsumerd_fd
*tmp_fd
;
124 tmp_fd
= malloc(sizeof(struct ltt_kconsumerd_fd
));
125 tmp_fd
->sessiond_fd
= buf
->fd
;
126 tmp_fd
->consumerd_fd
= consumerd_fd
;
127 tmp_fd
->state
= buf
->state
;
128 tmp_fd
->max_sb_size
= buf
->max_sb_size
;
129 strncpy(tmp_fd
->path_name
, buf
->path_name
, PATH_MAX
);
131 /* Opening the tracefile in write mode */
132 DBG("Opening %s for writing", tmp_fd
->path_name
);
133 ret
= open(tmp_fd
->path_name
,
134 O_WRONLY
|O_CREAT
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
136 ERR("Opening %s", tmp_fd
->path_name
);
140 tmp_fd
->out_fd
= ret
;
141 tmp_fd
->out_fd_offset
= 0;
143 DBG("Adding %s (%d, %d, %d)", tmp_fd
->path_name
,
144 tmp_fd
->sessiond_fd
, tmp_fd
->consumerd_fd
, tmp_fd
->out_fd
);
146 pthread_mutex_lock(&kconsumerd_lock_fds
);
147 cds_list_add(&tmp_fd
->list
, &kconsumerd_fd_list
.head
);
149 pthread_mutex_unlock(&kconsumerd_lock_fds
);
158 * Remove a fd from the global list protected by a mutex
160 static void del_fd(struct ltt_kconsumerd_fd
*lcf
)
162 pthread_mutex_lock(&kconsumerd_lock_fds
);
163 cds_list_del(&lcf
->list
);
166 DBG("Removed ltt_kconsumerd_fd");
167 cleanup_kconsumerd_fd(lcf
);
169 pthread_mutex_unlock(&kconsumerd_lock_fds
);
175 * Close all fds in the previous fd_list
176 * Must be used with kconsumerd_lock_fds lock held
178 static void close_outfds()
180 struct ltt_kconsumerd_fd
*iter
;
181 cds_list_for_each_entry(iter
, &kconsumerd_fd_list
.head
, list
) {
189 * Signal handler for the daemon
191 static void sighandler(int sig
)
193 /* unblock the threads */
194 pthread_cancel(threads
[0]);
195 pthread_cancel(threads
[1]);
206 * Setup signal handler for :
207 * SIGINT, SIGTERM, SIGPIPE
209 static int set_signal_handler(void)
215 if ((ret
= sigemptyset(&sigset
)) < 0) {
216 perror("sigemptyset");
220 sa
.sa_handler
= sighandler
;
223 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
228 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
233 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
244 * Splice the data from the ring buffer to the tracefile.
245 * Returns the number of bytes spliced
247 static int on_read_subbuffer(struct ltt_kconsumerd_fd
*kconsumerd_fd
,
252 off_t orig_offset
= kconsumerd_fd
->out_fd_offset
;
253 int fd
= kconsumerd_fd
->consumerd_fd
;
254 int outfd
= kconsumerd_fd
->out_fd
;
257 DBG("splice chan to pipe offset %lu (fd : %d)",
258 (unsigned long)offset
, fd
);
259 ret
= splice(fd
, &offset
, thread_pipe
[1], NULL
, len
,
260 SPLICE_F_MOVE
| SPLICE_F_MORE
);
261 DBG("splice chan to pipe ret %ld", ret
);
264 perror("Error in relay splice");
268 ret
= splice(thread_pipe
[0], NULL
, outfd
, NULL
, ret
,
269 SPLICE_F_MOVE
| SPLICE_F_MORE
);
270 DBG("splice pipe to file %ld", ret
);
273 perror("Error in file splice");
279 /* This won't block, but will start writeout asynchronously */
280 sync_file_range(outfd
, kconsumerd_fd
->out_fd_offset
, ret
,
281 SYNC_FILE_RANGE_WRITE
);
282 kconsumerd_fd
->out_fd_offset
+= ret
;
286 * This does a blocking write-and-wait on any page that belongs to the
287 * subbuffer prior to the one we just wrote.
288 * Don't care about error values, as these are just hints and ways to
289 * limit the amount of page cache used.
291 if (orig_offset
>= kconsumerd_fd
->max_sb_size
) {
292 sync_file_range(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
293 kconsumerd_fd
->max_sb_size
,
294 SYNC_FILE_RANGE_WAIT_BEFORE
295 | SYNC_FILE_RANGE_WRITE
296 | SYNC_FILE_RANGE_WAIT_AFTER
);
298 * Give hints to the kernel about how we access the file:
299 * POSIX_FADV_DONTNEED : we won't re-access data in a near
300 * future after we write it.
301 * We need to call fadvise again after the file grows because
302 * the kernel does not seem to apply fadvise to non-existing
304 * Call fadvise _after_ having waited for the page writeback to
305 * complete because the dirty page writeback semantic is not
306 * well defined. So it can be expected to lead to lower
307 * throughput in streaming.
309 posix_fadvise(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
310 kconsumerd_fd
->max_sb_size
, POSIX_FADV_DONTNEED
);
315 /* send the appropriate error description to sessiond */
318 send_error(KCONSUMERD_SPLICE_EBADF
);
321 send_error(KCONSUMERD_SPLICE_EINVAL
);
324 send_error(KCONSUMERD_SPLICE_ENOMEM
);
327 send_error(KCONSUMERD_SPLICE_ESPIPE
);
338 * Consume data on a file descriptor and write it on a trace file
340 static int read_subbuffer(struct ltt_kconsumerd_fd
*kconsumerd_fd
)
345 int infd
= kconsumerd_fd
->consumerd_fd
;
347 DBG("In read_subbuffer");
348 /* Get the next subbuffer */
349 err
= kernctl_get_next_subbuf(infd
);
352 perror("Reserving sub buffer failed (everything is normal, "
353 "it is due to concurrency)");
357 /* read the whole subbuffer */
358 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
361 perror("Getting sub-buffer len failed.");
365 /* splice the subbuffer to the tracefile */
366 ret
= on_read_subbuffer(kconsumerd_fd
, len
);
369 * display the error but continue processing to try
370 * to release the subbuffer
372 ERR("Error splicing to tracefile");
375 err
= kernctl_put_next_subbuf(infd
);
378 if (errno
== EFAULT
) {
379 perror("Error in unreserving sub buffer\n");
380 } else if (errno
== EIO
) {
381 /* Should never happen with newer LTTng versions */
382 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
394 * Update a fd according to what we just received
396 static void change_fd_state(int sessiond_fd
,
397 enum lttcomm_kconsumerd_fd_state state
)
399 struct ltt_kconsumerd_fd
*iter
;
400 cds_list_for_each_entry(iter
, &kconsumerd_fd_list
.head
, list
) {
401 if (iter
->sessiond_fd
== sessiond_fd
) {
411 * Receives an array of file descriptors and the associated
412 * structures describing each fd (path name).
413 * Returns the size of received data
415 static int consumerd_recv_fd(int sfd
, int size
,
416 enum lttcomm_consumerd_command cmd_type
)
421 struct cmsghdr
*cmsg
;
423 char tmp
[CMSG_SPACE(size
)];
424 struct lttcomm_kconsumerd_msg
*buf
;
425 /* the number of fds we are about to receive */
426 nb_fd
= size
/sizeof(struct lttcomm_kconsumerd_msg
);
430 memset(&msg
, 0, sizeof(msg
));
432 /* Prepare to receive the structures */
433 iov
[0].iov_base
= buf
;
434 iov
[0].iov_len
= size
;
438 msg
.msg_control
= tmp
;
439 msg
.msg_controllen
= sizeof(tmp
);
441 DBG("Waiting to receive fds");
442 if ((ret
= recvmsg(sfd
, &msg
, 0)) < 0) {
446 ERR("Received only %d, expected %d", ret
, size
);
447 send_error(KCONSUMERD_ERROR_RECV_FD
);
451 cmsg
= CMSG_FIRSTHDR(&msg
);
453 ERR("Invalid control message header");
455 send_error(KCONSUMERD_ERROR_RECV_FD
);
459 /* if we received fds */
460 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
461 DBG("Receive : expecting %d fds", nb_fd
);
462 for (i
= 0; i
< nb_fd
; i
++) {
464 case LTTCOMM_ADD_STREAM
:
465 DBG("add_fd %s (%d)", buf
[i
].path_name
, ((int *)CMSG_DATA(cmsg
))[i
]);
466 ret
= add_fd(&buf
[i
], ((int *)CMSG_DATA(cmsg
))[i
]);
468 send_error(KCONSUMERD_OUTFD_ERROR
);
472 case LTTCOMM_UPDATE_STREAM
:
473 change_fd_state(buf
[i
].fd
, buf
[i
].state
);
479 /* flag to tell the polling thread to update its fd array */
481 send_error(KCONSUMERD_SUCCESS_RECV_FD
);
483 ERR("Didn't received any fd");
484 send_error(KCONSUMERD_ERROR_RECV_FD
);
500 * This thread listens on the consumerd socket and
501 * receives the file descriptors from ltt-sessiond
503 static void *thread_receive_fds(void *data
)
505 int sock
, client_socket
, ret
;
506 struct lttcomm_kconsumerd_header tmp
;
508 DBG("Creating command socket %s", command_sock_path
);
509 unlink(command_sock_path
);
510 client_socket
= lttcomm_create_unix_sock(command_sock_path
);
511 if (client_socket
< 0) {
512 ERR("Cannot create command socket");
516 ret
= lttcomm_listen_unix_sock(client_socket
);
521 DBG("Sending ready command to ltt-sessiond");
522 ret
= send_error(KCONSUMERD_COMMAND_SOCK_READY
);
524 ERR("Error sending ready command to ltt-sessiond");
529 /* Blocking call, waiting for transmission */
530 sock
= lttcomm_accept_unix_sock(client_socket
);
535 /* We first get the number of fd we are about to receive */
536 ret
= lttcomm_recv_unix_sock(sock
, &tmp
,
537 sizeof(struct lttcomm_kconsumerd_header
));
539 ERR("Receiving the lttcomm_kconsumerd_header");
542 ret
= consumerd_recv_fd(sock
, tmp
.payload_size
, tmp
.cmd_type
);
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
;
566 if (*pollfd
!= NULL
) {
571 if (*local_kconsumerd_fd
!= NULL
) {
572 free(*local_kconsumerd_fd
);
573 *local_kconsumerd_fd
= NULL
;
576 *pollfd
= malloc(fds_count
* sizeof(struct pollfd
));
577 if (*pollfd
== NULL
) {
578 perror("pollfd malloc");
582 *local_kconsumerd_fd
= malloc(fds_count
* sizeof(struct ltt_kconsumerd_fd
));
583 if (*local_kconsumerd_fd
== NULL
) {
584 perror("local_kconsumerd_fd malloc");
588 DBG("Updating poll fd array");
589 pthread_mutex_lock(&kconsumerd_lock_fds
);
591 cds_list_for_each_entry(iter
, &kconsumerd_fd_list
.head
, list
) {
592 DBG("Inside for each");
593 if (iter
->state
== ACTIVE_FD
) {
594 DBG("Active FD %d", iter
->consumerd_fd
);
595 pollfd
[i
]->fd
= iter
->consumerd_fd
;
596 pollfd
[i
]->events
= POLLIN
| POLLPRI
;
597 local_kconsumerd_fd
[i
] = iter
;
599 } else if (iter
->state
== DELETE_FD
) {
604 pthread_mutex_unlock(&kconsumerd_lock_fds
);
614 * This thread polls the fds in the ltt_fd_list to consume the data
615 * and write it to tracefile if necessary.
617 static void *thread_poll_fds(void *data
)
619 int num_rdy
, num_hup
, high_prio
, ret
, i
;
620 struct pollfd
*pollfd
= NULL
;
621 /* local view of the fds */
622 struct ltt_kconsumerd_fd
*local_kconsumerd_fd
= NULL
;
623 /* local view of fds_count */
626 ret
= pipe(thread_pipe
);
628 perror("Error creating pipe");
637 * the ltt_fd_list has been updated, we need to update our
638 * local array as well
640 if (update_fd_array
) {
641 ret
= update_poll_array(&pollfd
, &local_kconsumerd_fd
);
643 ERR("Error in allocating pollfd or local_outfds");
644 send_error(KCONSUMERD_POLL_ERROR
);
650 /* poll on the array of fds */
651 DBG("polling on %d fd", nb_fd
);
652 num_rdy
= poll(pollfd
, nb_fd
, POLL_TIMEOUT
);
653 DBG("poll num_rdy : %d", num_rdy
);
655 perror("Poll error");
656 send_error(KCONSUMERD_POLL_ERROR
);
660 /* Take care of high priority channels first. */
661 for (i
= 0; i
< nb_fd
; i
++) {
662 switch(pollfd
[i
].revents
) {
664 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
666 send_error(KCONSUMERD_POLL_ERROR
);
669 ERR("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
673 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
674 send_error(KCONSUMERD_POLL_NVAL
);
678 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
680 ret
= read_subbuffer(&local_kconsumerd_fd
[i
]);
681 /* it's ok to have an unavailable sub-buffer (FIXME : is it ?) */
689 /* If every buffer FD has hung up, we end the read loop here */
690 if (nb_fd
> 0 && num_hup
== nb_fd
) {
691 DBG("every buffer FD has hung up\n");
692 send_error(KCONSUMERD_POLL_HUP
);
696 /* Take care of low priority channels. */
698 for (i
= 0; i
< nb_fd
; i
++) {
699 switch(pollfd
[i
].revents
) {
701 DBG("Normal read on fd %d", pollfd
[i
].fd
);
702 ret
= read_subbuffer(&local_kconsumerd_fd
[i
]);
703 /* it's ok to have an unavailable subbuffer (FIXME : is it ?) */
713 if (pollfd
!= NULL
) {
717 if (local_kconsumerd_fd
!= NULL
) {
718 free(local_kconsumerd_fd
);
719 local_kconsumerd_fd
= NULL
;
725 * usage function on stderr
727 static void usage(void)
729 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
730 fprintf(stderr
, " -h, --help "
731 "Display this usage.\n");
732 fprintf(stderr
, " -c, --kconsumerd-cmd-sock PATH "
733 "Specify path for the command socket\n");
734 fprintf(stderr
, " -e, --kconsumerd-err-sock PATH "
735 "Specify path for the error socket\n");
736 fprintf(stderr
, " -d, --daemonize "
737 "Start as a daemon.\n");
738 fprintf(stderr
, " -q, --quiet "
739 "No output at all.\n");
740 fprintf(stderr
, " -v, --verbose "
741 "Verbose mode. Activate DBG() macro.\n");
742 fprintf(stderr
, " -V, --version "
743 "Show version number.\n");
747 * daemon argument parsing
749 static void parse_args(int argc
, char **argv
)
753 static struct option long_options
[] = {
754 { "kconsumerd-cmd-sock", 1, 0, 'c' },
755 { "kconsumerd-err-sock", 1, 0, 'e' },
756 { "daemonize", 0, 0, 'd' },
757 { "help", 0, 0, 'h' },
758 { "quiet", 0, 0, 'q' },
759 { "verbose", 0, 0, 'v' },
760 { "version", 0, 0, 'V' },
765 int option_index
= 0;
766 c
= getopt_long(argc
, argv
, "dhqvV" "c:e:", long_options
, &option_index
);
773 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
775 fprintf(stderr
, " with arg %s\n", optarg
);
779 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
782 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
797 fprintf(stdout
, "%s\n", VERSION
);
810 int main(int argc
, char **argv
)
816 /* Parse arguments */
818 parse_args(argc
, argv
);
829 if (strlen(command_sock_path
) == 0) {
830 snprintf(command_sock_path
, PATH_MAX
,
831 KCONSUMERD_CMD_SOCK_PATH
);
833 if (strlen(error_sock_path
) == 0) {
834 snprintf(error_sock_path
, PATH_MAX
,
835 KCONSUMERD_ERR_SOCK_PATH
);
838 if (set_signal_handler() < 0) {
842 /* Connect to the socket created by ltt-sessiond to report errors */
843 DBG("Connecting to error socket %s", error_sock_path
);
844 error_socket
= lttcomm_connect_unix_sock(error_sock_path
);
845 /* not a fatal error, but all communication with ltt-sessiond will fail */
846 if (error_socket
< 0) {
847 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
850 /* Create the thread to manage the receive of fd */
851 ret
= pthread_create(&threads
[0], NULL
, thread_receive_fds
, (void *) NULL
);
853 perror("pthread_create");
857 /* Create thread to manage the polling/writing of traces */
858 ret
= pthread_create(&threads
[1], NULL
, thread_poll_fds
, (void *) NULL
);
860 perror("pthread_create");
864 for (i
= 0; i
< 2; i
++) {
865 ret
= pthread_join(threads
[i
], &status
);
867 perror("pthread_join");
872 send_error(KCONSUMERD_EXIT_SUCCESS
);
877 send_error(KCONSUMERD_EXIT_FAILURE
);