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 * Remove a fd from the global list protected by a mutex
80 static void del_fd(struct ltt_kconsumerd_fd
*lcf
)
82 pthread_mutex_lock(&kconsumerd_lock_fds
);
83 cds_list_del(&lcf
->list
);
86 DBG("Removed ltt_kconsumerd_fd");
89 close(lcf
->consumerd_fd
);
94 pthread_mutex_unlock(&kconsumerd_lock_fds
);
100 * Cleanup the daemon's socket on exit
102 static void cleanup()
104 struct ltt_kconsumerd_fd
*iter
;
107 /* remove the socket file */
108 unlink(command_sock_path
);
110 /* unblock the threads */
111 WARN("Terminating the threads before exiting");
112 pthread_cancel(threads
[0]);
113 pthread_cancel(threads
[1]);
115 /* close all outfd */
116 cds_list_for_each_entry(iter
, &kconsumerd_fd_list
.head
, list
) {
123 * send return code to ltt-sessiond
125 static int send_error(enum lttcomm_return_code cmd
)
127 if (error_socket
> 0) {
128 return lttcomm_send_unix_sock(error_socket
, &cmd
,
129 sizeof(enum lttcomm_sessiond_command
));
138 * Add a fd to the global list protected by a mutex
140 static int add_fd(struct lttcomm_kconsumerd_msg
*buf
, int consumerd_fd
)
142 struct ltt_kconsumerd_fd
*tmp_fd
;
145 tmp_fd
= malloc(sizeof(struct ltt_kconsumerd_fd
));
146 tmp_fd
->sessiond_fd
= buf
->fd
;
147 tmp_fd
->consumerd_fd
= consumerd_fd
;
148 tmp_fd
->state
= buf
->state
;
149 tmp_fd
->max_sb_size
= buf
->max_sb_size
;
150 strncpy(tmp_fd
->path_name
, buf
->path_name
, PATH_MAX
);
152 /* Opening the tracefile in write mode */
153 DBG("Opening %s for writing", tmp_fd
->path_name
);
154 ret
= open(tmp_fd
->path_name
,
155 O_WRONLY
|O_CREAT
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
157 ERR("Opening %s", tmp_fd
->path_name
);
161 tmp_fd
->out_fd
= ret
;
162 tmp_fd
->out_fd_offset
= 0;
164 DBG("Adding %s (%d, %d, %d)", tmp_fd
->path_name
,
165 tmp_fd
->sessiond_fd
, tmp_fd
->consumerd_fd
, tmp_fd
->out_fd
);
167 pthread_mutex_lock(&kconsumerd_lock_fds
);
168 cds_list_add(&tmp_fd
->list
, &kconsumerd_fd_list
.head
);
170 pthread_mutex_unlock(&kconsumerd_lock_fds
);
180 * Signal handler for the daemon
182 static void sighandler(int sig
)
192 * Setup signal handler for :
193 * SIGINT, SIGTERM, SIGPIPE
195 static int set_signal_handler(void)
201 if ((ret
= sigemptyset(&sigset
)) < 0) {
202 perror("sigemptyset");
206 sa
.sa_handler
= sighandler
;
209 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
214 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
219 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
230 * Splice the data from the ring buffer to the tracefile.
231 * Returns the number of bytes spliced
233 static int on_read_subbuffer(struct ltt_kconsumerd_fd
*kconsumerd_fd
,
238 off_t orig_offset
= kconsumerd_fd
->out_fd_offset
;
239 int fd
= kconsumerd_fd
->consumerd_fd
;
240 int outfd
= kconsumerd_fd
->out_fd
;
243 DBG("splice chan to pipe offset %lu (fd : %d)",
244 (unsigned long)offset
, fd
);
245 ret
= splice(fd
, &offset
, thread_pipe
[1], NULL
, len
,
246 SPLICE_F_MOVE
| SPLICE_F_MORE
);
247 DBG("splice chan to pipe ret %ld", ret
);
250 perror("Error in relay splice");
254 ret
= splice(thread_pipe
[0], NULL
, outfd
, NULL
, ret
,
255 SPLICE_F_MOVE
| SPLICE_F_MORE
);
256 DBG("splice pipe to file %ld", ret
);
259 perror("Error in file splice");
265 /* This won't block, but will start writeout asynchronously */
266 sync_file_range(outfd
, kconsumerd_fd
->out_fd_offset
, ret
,
267 SYNC_FILE_RANGE_WRITE
);
268 kconsumerd_fd
->out_fd_offset
+= ret
;
272 * This does a blocking write-and-wait on any page that belongs to the
273 * subbuffer prior to the one we just wrote.
274 * Don't care about error values, as these are just hints and ways to
275 * limit the amount of page cache used.
277 if (orig_offset
>= kconsumerd_fd
->max_sb_size
) {
278 sync_file_range(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
279 kconsumerd_fd
->max_sb_size
,
280 SYNC_FILE_RANGE_WAIT_BEFORE
281 | SYNC_FILE_RANGE_WRITE
282 | SYNC_FILE_RANGE_WAIT_AFTER
);
284 * Give hints to the kernel about how we access the file:
285 * POSIX_FADV_DONTNEED : we won't re-access data in a near
286 * future after we write it.
287 * We need to call fadvise again after the file grows because
288 * the kernel does not seem to apply fadvise to non-existing
290 * Call fadvise _after_ having waited for the page writeback to
291 * complete because the dirty page writeback semantic is not
292 * well defined. So it can be expected to lead to lower
293 * throughput in streaming.
295 posix_fadvise(outfd
, orig_offset
- kconsumerd_fd
->max_sb_size
,
296 kconsumerd_fd
->max_sb_size
, POSIX_FADV_DONTNEED
);
301 /* send the appropriate error description to sessiond */
304 send_error(KCONSUMERD_SPLICE_EBADF
);
307 send_error(KCONSUMERD_SPLICE_EINVAL
);
310 send_error(KCONSUMERD_SPLICE_ENOMEM
);
313 send_error(KCONSUMERD_SPLICE_ESPIPE
);
324 * Consume data on a file descriptor and write it on a trace file
326 static int read_subbuffer(struct ltt_kconsumerd_fd
*kconsumerd_fd
)
331 int infd
= kconsumerd_fd
->consumerd_fd
;
333 DBG("In read_subbuffer");
334 /* Get the next subbuffer */
335 err
= kernctl_get_next_subbuf(infd
);
338 perror("Reserving sub buffer failed (everything is normal, "
339 "it is due to concurrency)");
343 /* read the whole subbuffer */
344 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
347 perror("Getting sub-buffer len failed.");
351 /* splice the subbuffer to the tracefile */
352 ret
= on_read_subbuffer(kconsumerd_fd
, len
);
355 * display the error but continue processing to try
356 * to release the subbuffer
358 ERR("Error splicing to tracefile");
361 err
= kernctl_put_next_subbuf(infd
);
364 if (errno
== EFAULT
) {
365 perror("Error in unreserving sub buffer\n");
366 } else if (errno
== EIO
) {
367 /* Should never happen with newer LTTng versions */
368 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
380 * Update a fd according to what we just received
382 static void change_fd_state(int sessiond_fd
,
383 enum lttcomm_kconsumerd_fd_state state
)
385 struct ltt_kconsumerd_fd
*iter
;
386 cds_list_for_each_entry(iter
, &kconsumerd_fd_list
.head
, list
) {
387 if (iter
->sessiond_fd
== sessiond_fd
) {
397 * Receives an array of file descriptors and the associated
398 * structures describing each fd (path name).
399 * Returns the size of received data
401 static int consumerd_recv_fd(int sfd
, int size
,
402 enum lttcomm_consumerd_command cmd_type
)
407 struct cmsghdr
*cmsg
;
409 char tmp
[CMSG_SPACE(size
)];
410 struct lttcomm_kconsumerd_msg
*buf
;
411 /* the number of fds we are about to receive */
412 nb_fd
= size
/sizeof(struct lttcomm_kconsumerd_msg
);
416 memset(&msg
, 0, sizeof(msg
));
418 /* Prepare to receive the structures */
419 iov
[0].iov_base
= buf
;
420 iov
[0].iov_len
= size
;
424 msg
.msg_control
= tmp
;
425 msg
.msg_controllen
= sizeof(tmp
);
427 DBG("Waiting to receive fds");
428 if ((ret
= recvmsg(sfd
, &msg
, 0)) < 0) {
432 ERR("Received only %d, expected %d", ret
, size
);
433 send_error(KCONSUMERD_ERROR_RECV_FD
);
437 cmsg
= CMSG_FIRSTHDR(&msg
);
439 ERR("Invalid control message header");
441 send_error(KCONSUMERD_ERROR_RECV_FD
);
445 /* if we received fds */
446 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
447 DBG("Receive : expecting %d fds", nb_fd
);
448 for (i
= 0; i
< nb_fd
; i
++) {
450 case LTTCOMM_ADD_STREAM
:
451 DBG("add_fd %s (%d)", buf
[i
].path_name
, ((int *)CMSG_DATA(cmsg
))[i
]);
452 ret
= add_fd(&buf
[i
], ((int *)CMSG_DATA(cmsg
))[i
]);
454 send_error(KCONSUMERD_OUTFD_ERROR
);
458 case LTTCOMM_UPDATE_STREAM
:
459 change_fd_state(buf
[i
].fd
, buf
[i
].state
);
465 /* flag to tell the polling thread to update its fd array */
467 send_error(KCONSUMERD_SUCCESS_RECV_FD
);
469 ERR("Didn't received any fd");
470 send_error(KCONSUMERD_ERROR_RECV_FD
);
486 * This thread listens on the consumerd socket and
487 * receives the file descriptors from ltt-sessiond
489 static void *thread_receive_fds(void *data
)
491 int sock
, client_socket
, ret
;
492 struct lttcomm_kconsumerd_header tmp
;
494 DBG("Creating command socket %s", command_sock_path
);
495 unlink(command_sock_path
);
496 client_socket
= lttcomm_create_unix_sock(command_sock_path
);
497 if (client_socket
< 0) {
498 ERR("Cannot create command socket");
502 ret
= lttcomm_listen_unix_sock(client_socket
);
507 DBG("Sending ready command to ltt-sessiond");
508 ret
= send_error(KCONSUMERD_COMMAND_SOCK_READY
);
510 ERR("Error sending ready command to ltt-sessiond");
514 /* Blocking call, waiting for transmission */
515 sock
= lttcomm_accept_unix_sock(client_socket
);
517 WARN("On accept, retrying");
522 /* We first get the number of fd we are about to receive */
523 ret
= lttcomm_recv_unix_sock(sock
, &tmp
,
524 sizeof(struct lttcomm_kconsumerd_header
));
526 ERR("Receiving the lttcomm_kconsumerd_header, exiting");
529 ret
= consumerd_recv_fd(sock
, tmp
.payload_size
, tmp
.cmd_type
);
531 ERR("Receiving the FD, exiting");
544 * Allocate the pollfd structure and the local view of the out fds
545 * to avoid doing a lookup in the linked list and concurrency issues
546 * when writing is needed.
547 * Returns the number of fds in the structures
549 static int update_poll_array(struct pollfd
**pollfd
,
550 struct ltt_kconsumerd_fd
**local_kconsumerd_fd
)
552 struct ltt_kconsumerd_fd
*iter
;
555 if (*pollfd
!= NULL
) {
560 if (*local_kconsumerd_fd
!= NULL
) {
561 free(*local_kconsumerd_fd
);
562 *local_kconsumerd_fd
= NULL
;
565 *pollfd
= malloc(fds_count
* sizeof(struct pollfd
));
566 if (*pollfd
== NULL
) {
567 perror("pollfd malloc");
571 *local_kconsumerd_fd
= malloc(fds_count
* sizeof(struct ltt_kconsumerd_fd
));
572 if (*local_kconsumerd_fd
== NULL
) {
573 perror("local_kconsumerd_fd malloc");
577 DBG("Updating poll fd array");
578 pthread_mutex_lock(&kconsumerd_lock_fds
);
580 cds_list_for_each_entry(iter
, &kconsumerd_fd_list
.head
, list
) {
581 DBG("Inside for each");
582 if (iter
->state
== ACTIVE_FD
) {
583 DBG("Active FD %d", iter
->consumerd_fd
);
584 pollfd
[i
]->fd
= iter
->consumerd_fd
;
585 pollfd
[i
]->events
= POLLIN
| POLLPRI
;
586 local_kconsumerd_fd
[i
] = iter
;
588 } else if (iter
->state
== DELETE_FD
) {
593 pthread_mutex_unlock(&kconsumerd_lock_fds
);
603 * This thread polls the fds in the ltt_fd_list to consume the data
604 * and write it to tracefile if necessary.
606 static void *thread_poll_fds(void *data
)
608 int num_rdy
, num_hup
, high_prio
, ret
, i
;
609 struct pollfd
*pollfd
= NULL
;
610 /* local view of the fds */
611 struct ltt_kconsumerd_fd
*local_kconsumerd_fd
= NULL
;
612 /* local view of fds_count */
615 ret
= pipe(thread_pipe
);
617 perror("Error creating pipe");
626 * the ltt_fd_list has been updated, we need to update our
627 * local array as well
629 if (update_fd_array
) {
630 ret
= update_poll_array(&pollfd
, &local_kconsumerd_fd
);
632 ERR("Error in allocating pollfd or local_outfds");
633 send_error(KCONSUMERD_POLL_ERROR
);
639 /* poll on the array of fds */
640 DBG("polling on %d fd", nb_fd
);
641 num_rdy
= poll(pollfd
, nb_fd
, POLL_TIMEOUT
);
642 DBG("poll num_rdy : %d", num_rdy
);
644 perror("Poll error");
645 send_error(KCONSUMERD_POLL_ERROR
);
649 /* Take care of high priority channels first. */
650 for (i
= 0; i
< nb_fd
; i
++) {
651 switch(pollfd
[i
].revents
) {
653 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
655 send_error(KCONSUMERD_POLL_ERROR
);
658 ERR("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
662 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
663 send_error(KCONSUMERD_POLL_NVAL
);
667 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
669 ret
= read_subbuffer(&local_kconsumerd_fd
[i
]);
670 /* it's ok to have an unavailable sub-buffer (FIXME : is it ?) */
678 /* If every buffer FD has hung up, we end the read loop here */
679 if (nb_fd
> 0 && num_hup
== nb_fd
) {
680 DBG("every buffer FD has hung up\n");
681 send_error(KCONSUMERD_POLL_HUP
);
685 /* Take care of low priority channels. */
687 for (i
= 0; i
< nb_fd
; i
++) {
688 switch(pollfd
[i
].revents
) {
690 DBG("Normal read on fd %d", pollfd
[i
].fd
);
691 ret
= read_subbuffer(&local_kconsumerd_fd
[i
]);
692 /* it's ok to have an unavailable subbuffer (FIXME : is it ?) */
702 if (pollfd
!= NULL
) {
706 if (local_kconsumerd_fd
!= NULL
) {
707 free(local_kconsumerd_fd
);
708 local_kconsumerd_fd
= NULL
;
715 * usage function on stderr
717 static void usage(void)
719 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
720 fprintf(stderr
, " -h, --help "
721 "Display this usage.\n");
722 fprintf(stderr
, " -c, --kconsumerd-cmd-sock PATH "
723 "Specify path for the command socket\n");
724 fprintf(stderr
, " -e, --kconsumerd-err-sock PATH "
725 "Specify path for the error socket\n");
726 fprintf(stderr
, " -d, --daemonize "
727 "Start as a daemon.\n");
728 fprintf(stderr
, " -q, --quiet "
729 "No output at all.\n");
730 fprintf(stderr
, " -v, --verbose "
731 "Verbose mode. Activate DBG() macro.\n");
732 fprintf(stderr
, " -V, --version "
733 "Show version number.\n");
737 * daemon argument parsing
739 static void parse_args(int argc
, char **argv
)
743 static struct option long_options
[] = {
744 { "kconsumerd-cmd-sock", 1, 0, 'c' },
745 { "kconsumerd-err-sock", 1, 0, 'e' },
746 { "daemonize", 0, 0, 'd' },
747 { "help", 0, 0, 'h' },
748 { "quiet", 0, 0, 'q' },
749 { "verbose", 0, 0, 'v' },
750 { "version", 0, 0, 'V' },
755 int option_index
= 0;
756 c
= getopt_long(argc
, argv
, "dhqvV" "c:e:", long_options
, &option_index
);
763 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
765 fprintf(stderr
, " with arg %s\n", optarg
);
769 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
772 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
787 fprintf(stdout
, "%s\n", VERSION
);
800 int main(int argc
, char **argv
)
806 /* Parse arguments */
808 parse_args(argc
, argv
);
819 if (strlen(command_sock_path
) == 0) {
820 snprintf(command_sock_path
, PATH_MAX
,
821 KCONSUMERD_CMD_SOCK_PATH
);
823 if (strlen(error_sock_path
) == 0) {
824 snprintf(error_sock_path
, PATH_MAX
,
825 KCONSUMERD_ERR_SOCK_PATH
);
828 if (set_signal_handler() < 0) {
832 /* Connect to the socket created by ltt-sessiond to report errors */
833 DBG("Connecting to error socket %s", error_sock_path
);
834 error_socket
= lttcomm_connect_unix_sock(error_sock_path
);
835 /* not a fatal error, but all communication with ltt-sessiond will fail */
836 if (error_socket
< 0) {
837 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
840 /* Create the thread to manage the receive of fd */
841 ret
= pthread_create(&threads
[0], NULL
, thread_receive_fds
, (void *) NULL
);
843 perror("pthread_create");
847 /* Create thread to manage the polling/writing of traces */
848 ret
= pthread_create(&threads
[1], NULL
, thread_poll_fds
, (void *) NULL
);
850 perror("pthread_create");
854 for (i
= 0; i
< 2; i
++) {
855 ret
= pthread_join(threads
[i
], &status
);
857 perror("pthread_join");
862 send_error(KCONSUMERD_EXIT_SUCCESS
);
867 send_error(KCONSUMERD_EXIT_FAILURE
);