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
);
263 perror("Error in relay splice");
267 ret
= splice(thread_pipe
[0], NULL
, outfd
, NULL
, ret
,
268 SPLICE_F_MOVE
| SPLICE_F_MORE
);
269 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
);
316 * Consume data on a file descriptor and write it on a trace file
318 static int read_subbuffer(struct ltt_kconsumerd_fd
*kconsumerd_fd
)
323 int infd
= kconsumerd_fd
->consumerd_fd
;
325 DBG("In read_subbuffer");
326 /* Get the next subbuffer */
327 err
= kernctl_get_next_subbuf(infd
);
330 perror("Reserving sub buffer failed (everything is normal, "
331 "it is due to concurrency)");
335 /* read the whole subbuffer */
336 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
339 perror("Getting sub-buffer len failed.");
343 /* splice the subbuffer to the tracefile */
344 ret
= on_read_subbuffer(kconsumerd_fd
, len
);
347 * display the error but continue processing to try
348 * to release the subbuffer
350 ERR("Error splicing to tracefile");
353 err
= kernctl_put_next_subbuf(infd
);
356 if (errno
== EFAULT
) {
357 perror("Error in unreserving sub buffer\n");
358 } else if (errno
== EIO
) {
359 /* Should never happen with newer LTTng versions */
360 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
372 * Update a fd according to what we just received
374 static void change_fd_state(int sessiond_fd
,
375 enum lttcomm_kconsumerd_fd_state state
)
377 struct ltt_kconsumerd_fd
*iter
;
378 cds_list_for_each_entry(iter
, &kconsumerd_fd_list
.head
, list
) {
379 if (iter
->sessiond_fd
== sessiond_fd
) {
389 * Receives an array of file descriptors and the associated
390 * structures describing each fd (path name).
391 * Returns the size of received data
393 static int consumerd_recv_fd(int sfd
, int size
,
394 enum lttcomm_consumerd_command cmd_type
)
399 struct cmsghdr
*cmsg
;
401 char tmp
[CMSG_SPACE(size
)];
402 struct lttcomm_kconsumerd_msg
*buf
;
403 /* the number of fds we are about to receive */
404 nb_fd
= size
/sizeof(struct lttcomm_kconsumerd_msg
);
408 memset(&msg
, 0, sizeof(msg
));
410 /* Prepare to receive the structures */
411 iov
[0].iov_base
= buf
;
412 iov
[0].iov_len
= size
;
416 msg
.msg_control
= tmp
;
417 msg
.msg_controllen
= sizeof(tmp
);
419 DBG("Waiting to receive fds");
420 if ((ret
= recvmsg(sfd
, &msg
, 0)) < 0) {
424 ERR("Received only %d, expected %d", ret
, size
);
425 send_error(KCONSUMERD_ERROR_RECV_FD
);
429 cmsg
= CMSG_FIRSTHDR(&msg
);
431 ERR("Invalid control message header");
433 send_error(KCONSUMERD_ERROR_RECV_FD
);
437 /* if we received fds */
438 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
439 DBG("Receive : expecting %d fds", nb_fd
);
440 for (i
= 0; i
< nb_fd
; i
++) {
442 case LTTCOMM_ADD_STREAM
:
443 DBG("add_fd %s (%d)", buf
[i
].path_name
, ((int *)CMSG_DATA(cmsg
))[i
]);
444 ret
= add_fd(&buf
[i
], ((int *)CMSG_DATA(cmsg
))[i
]);
446 send_error(KCONSUMERD_OUTFD_ERROR
);
450 case LTTCOMM_UPDATE_STREAM
:
451 change_fd_state(buf
[i
].fd
, buf
[i
].state
);
457 /* flag to tell the polling thread to update its fd array */
459 send_error(KCONSUMERD_SUCCESS_RECV_FD
);
461 ERR("Didn't received any fd");
462 send_error(KCONSUMERD_ERROR_RECV_FD
);
478 * This thread listens on the consumerd socket and
479 * receives the file descriptors from ltt-sessiond
481 static void *thread_receive_fds(void *data
)
483 int sock
, client_socket
, ret
;
484 struct lttcomm_kconsumerd_header tmp
;
486 DBG("Creating command socket %s", command_sock_path
);
487 unlink(command_sock_path
);
488 client_socket
= lttcomm_create_unix_sock(command_sock_path
);
489 if (client_socket
< 0) {
490 ERR("Cannot create command socket");
494 ret
= lttcomm_listen_unix_sock(client_socket
);
499 DBG("Sending ready command to ltt-sessiond");
500 ret
= send_error(KCONSUMERD_COMMAND_SOCK_READY
);
502 ERR("Error sending ready command to ltt-sessiond");
507 /* Blocking call, waiting for transmission */
508 sock
= lttcomm_accept_unix_sock(client_socket
);
513 /* We first get the number of fd we are about to receive */
514 ret
= lttcomm_recv_unix_sock(sock
, &tmp
,
515 sizeof(struct lttcomm_kconsumerd_header
));
517 ERR("Receiving the lttcomm_kconsumerd_header");
520 ret
= consumerd_recv_fd(sock
, tmp
.payload_size
, tmp
.cmd_type
);
533 * Allocate the pollfd structure and the local view of the out fds
534 * to avoid doing a lookup in the linked list and concurrency issues
535 * when writing is needed.
536 * Returns the number of fds in the structures
538 static int update_poll_array(struct pollfd
**pollfd
,
539 struct ltt_kconsumerd_fd
**local_kconsumerd_fd
)
541 struct ltt_kconsumerd_fd
*iter
;
544 if (*pollfd
!= NULL
) {
549 if (*local_kconsumerd_fd
!= NULL
) {
550 free(*local_kconsumerd_fd
);
551 *local_kconsumerd_fd
= NULL
;
554 *pollfd
= malloc(fds_count
* sizeof(struct pollfd
));
555 if (*pollfd
== NULL
) {
556 perror("pollfd malloc");
560 *local_kconsumerd_fd
= malloc(fds_count
* sizeof(struct ltt_kconsumerd_fd
));
561 if (*local_kconsumerd_fd
== NULL
) {
562 perror("local_kconsumerd_fd malloc");
566 DBG("Updating poll fd array");
567 pthread_mutex_lock(&kconsumerd_lock_fds
);
569 cds_list_for_each_entry(iter
, &kconsumerd_fd_list
.head
, list
) {
570 DBG("Inside for each");
571 if (iter
->state
== ACTIVE_FD
) {
572 DBG("Active FD %d", iter
->consumerd_fd
);
573 pollfd
[i
]->fd
= iter
->consumerd_fd
;
574 pollfd
[i
]->events
= POLLIN
| POLLPRI
;
575 local_kconsumerd_fd
[i
] = iter
;
577 } else if (iter
->state
== DELETE_FD
) {
582 pthread_mutex_unlock(&kconsumerd_lock_fds
);
592 * This thread polls the fds in the ltt_fd_list to consume the data
593 * and write it to tracefile if necessary.
595 static void *thread_poll_fds(void *data
)
597 int num_rdy
, num_hup
, high_prio
, ret
, i
;
598 struct pollfd
*pollfd
= NULL
;
599 /* local view of the fds */
600 struct ltt_kconsumerd_fd
*local_kconsumerd_fd
= NULL
;
601 /* local view of fds_count */
604 ret
= pipe(thread_pipe
);
606 perror("Error creating pipe");
615 * the ltt_fd_list has been updated, we need to update our
616 * local array as well
618 if (update_fd_array
) {
619 ret
= update_poll_array(&pollfd
, &local_kconsumerd_fd
);
621 ERR("Error in allocating pollfd or local_outfds");
622 send_error(KCONSUMERD_POLL_ERROR
);
628 /* poll on the array of fds */
629 DBG("polling on %d fd", nb_fd
);
630 num_rdy
= poll(pollfd
, nb_fd
, POLL_TIMEOUT
);
631 DBG("poll num_rdy : %d", num_rdy
);
633 perror("Poll error");
634 send_error(KCONSUMERD_POLL_ERROR
);
638 /* Take care of high priority channels first. */
639 for (i
= 0; i
< nb_fd
; i
++) {
640 switch(pollfd
[i
].revents
) {
642 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
644 send_error(KCONSUMERD_POLL_ERROR
);
647 ERR("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
651 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
652 send_error(KCONSUMERD_POLL_NVAL
);
656 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
658 ret
= read_subbuffer(&local_kconsumerd_fd
[i
]);
659 /* it's ok to have an unavailable sub-buffer (FIXME : is it ?) */
667 /* If every buffer FD has hung up, we end the read loop here */
668 if (nb_fd
> 0 && num_hup
== nb_fd
) {
669 DBG("every buffer FD has hung up\n");
670 send_error(KCONSUMERD_POLL_HUP
);
674 /* Take care of low priority channels. */
676 for (i
= 0; i
< nb_fd
; i
++) {
677 switch(pollfd
[i
].revents
) {
679 DBG("Normal read on fd %d", pollfd
[i
].fd
);
680 ret
= read_subbuffer(&local_kconsumerd_fd
[i
]);
681 /* it's ok to have an unavailable subbuffer (FIXME : is it ?) */
691 if (pollfd
!= NULL
) {
695 if (local_kconsumerd_fd
!= NULL
) {
696 free(local_kconsumerd_fd
);
697 local_kconsumerd_fd
= NULL
;
703 * usage function on stderr
705 static void usage(void)
707 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
708 fprintf(stderr
, " -h, --help "
709 "Display this usage.\n");
710 fprintf(stderr
, " -c, --kconsumerd-cmd-sock PATH "
711 "Specify path for the command socket\n");
712 fprintf(stderr
, " -e, --kconsumerd-err-sock PATH "
713 "Specify path for the error socket\n");
714 fprintf(stderr
, " -d, --daemonize "
715 "Start as a daemon.\n");
716 fprintf(stderr
, " -q, --quiet "
717 "No output at all.\n");
718 fprintf(stderr
, " -v, --verbose "
719 "Verbose mode. Activate DBG() macro.\n");
720 fprintf(stderr
, " -V, --version "
721 "Show version number.\n");
725 * daemon argument parsing
727 static void parse_args(int argc
, char **argv
)
731 static struct option long_options
[] = {
732 { "kconsumerd-cmd-sock", 1, 0, 'c' },
733 { "kconsumerd-err-sock", 1, 0, 'e' },
734 { "daemonize", 0, 0, 'd' },
735 { "help", 0, 0, 'h' },
736 { "quiet", 0, 0, 'q' },
737 { "verbose", 0, 0, 'v' },
738 { "version", 0, 0, 'V' },
743 int option_index
= 0;
744 c
= getopt_long(argc
, argv
, "dhqvV" "c:e:", long_options
, &option_index
);
751 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
753 fprintf(stderr
, " with arg %s\n", optarg
);
757 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
760 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
775 fprintf(stdout
, "%s\n", VERSION
);
788 int main(int argc
, char **argv
)
794 /* Parse arguments */
796 parse_args(argc
, argv
);
807 if (strlen(command_sock_path
) == 0) {
808 snprintf(command_sock_path
, PATH_MAX
,
809 KCONSUMERD_CMD_SOCK_PATH
);
811 if (strlen(error_sock_path
) == 0) {
812 snprintf(error_sock_path
, PATH_MAX
,
813 KCONSUMERD_ERR_SOCK_PATH
);
816 if (set_signal_handler() < 0) {
820 /* Connect to the socket created by ltt-sessiond to report errors */
821 DBG("Connecting to error socket %s", error_sock_path
);
822 error_socket
= lttcomm_connect_unix_sock(error_sock_path
);
823 /* not a fatal error, but all communication with ltt-sessiond will fail */
824 if (error_socket
< 0) {
825 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
828 /* Create the thread to manage the receive of fd */
829 ret
= pthread_create(&threads
[0], NULL
, thread_receive_fds
, (void *) NULL
);
831 perror("pthread_create");
835 /* Create thread to manage the polling/writing of traces */
836 ret
= pthread_create(&threads
[1], NULL
, thread_poll_fds
, (void *) NULL
);
838 perror("pthread_create");
842 for (i
= 0; i
< 2; i
++) {
843 ret
= pthread_join(threads
[i
], &status
);
845 perror("pthread_join");
850 send_error(KCONSUMERD_EXIT_SUCCESS
);
855 send_error(KCONSUMERD_EXIT_FAILURE
);