2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #ifndef _LIBLTTKCONSUMERD_H
20 #define _LIBLTTKCONSUMERD_H
22 #include <lttng-sessiond-comm.h>
23 #include "lttng-kconsumerd.h"
26 * When the receiving thread dies, we need to have a way to make
27 * the polling thread exit eventually.
28 * If all FDs hang up (normal case when the ltt-sessiond stops),
29 * we can exit cleanly, but if there is a problem and for whatever
30 * reason some FDs remain open, the consumer should still exit eventually.
32 * If the timeout is reached, it means that during this period
33 * no events occurred on the FDs so we need to force an exit.
34 * This case should not happen but it is a safety to ensure we won't block
35 * the consumer indefinitely.
37 * The value of 2 seconds is an arbitrary choice.
39 #define KCONSUMERD_POLL_GRACE_PERIOD 2000
41 struct kconsumerd_fd_list
{
42 struct cds_list_head head
;
46 * Internal representation of the FDs,
47 * sessiond_fd is used to identify uniquely a fd
49 struct kconsumerd_fd
{
50 struct cds_list_head list
;
51 int sessiond_fd
; /* used to identify uniquely a fd with sessiond */
52 int consumerd_fd
; /* fd to consume */
53 int out_fd
; /* output file to write the data */
54 off_t out_fd_offset
; /* write position in the output file descriptor */
55 char path_name
[PATH_MAX
]; /* tracefile name */
56 enum kconsumerd_fd_state state
;
57 unsigned long max_sb_size
; /* the subbuffer size for this channel */
60 struct kconsumerd_local_data
{
61 /* function to call when data is available on a buffer */
62 int (*on_buffer_ready
)(struct kconsumerd_fd
*kconsumerd_fd
);
63 /* socket to communicate errors with sessiond */
64 int kconsumerd_error_socket
;
65 /* socket to exchange commands with sessiond */
66 char *kconsumerd_command_sock_path
;
67 /* communication with splice */
68 int kconsumerd_thread_pipe
[2];
69 /* pipe to wake the poll thread when necessary */
70 int kconsumerd_poll_pipe
[2];
71 /* to let the signal handler wake up the fd receiver thread */
72 int kconsumerd_should_quit
[2];
77 * initialise the necessary environnement :
78 * - create a new context
79 * - create the poll_pipe
80 * - create the should_quit pipe (for signal handler)
81 * - create the thread pipe (for splice)
82 * Takes a function pointer as argument, this function is called when data is
83 * available on a buffer. This function is responsible to do the
84 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
85 * buffer configuration and then kernctl_put_next_subbuf at the end.
86 * Returns a pointer to the new context or NULL on error.
88 struct kconsumerd_local_data
*kconsumerd_create(
89 int (*buffer_ready
)(struct kconsumerd_fd
*kconsumerd_fd
));
93 * Close all fds associated with the instance and free the context
95 void kconsumerd_destroy(struct kconsumerd_local_data
*ctx
);
98 * kconsumerd_on_read_subbuffer_mmap
99 * mmap the ring buffer, read it and write the data to the tracefile.
100 * Returns the number of bytes written
102 int kconsumerd_on_read_subbuffer_mmap(struct kconsumerd_local_data
*ctx
,
103 struct kconsumerd_fd
*kconsumerd_fd
, unsigned long len
);
106 * kconsumerd_on_read_subbuffer
108 * Splice the data from the ring buffer to the tracefile.
109 * Returns the number of bytes spliced
111 int kconsumerd_on_read_subbuffer_splice(struct kconsumerd_local_data
*ctx
,
112 struct kconsumerd_fd
*kconsumerd_fd
, unsigned long len
);
115 * kconsumerd_send_error
116 * send return code to ltt-sessiond
117 * returns the return code of sendmsg : the number of bytes transmitted
120 int kconsumerd_send_error(struct kconsumerd_local_data
*ctx
,
121 enum lttcomm_return_code cmd
);
124 * kconsumerd_poll_socket
125 * Poll on the should_quit pipe and the command socket
126 * return -1 on error and should exit, 0 if data is
127 * available on the command socket
129 int kconsumerd_poll_socket(struct pollfd
*kconsumerd_sockpoll
);
132 * kconsumerd_thread_poll_fds
133 * This thread polls the fds in the ltt_fd_list to consume the data
134 * and write it to tracefile if necessary.
136 void *kconsumerd_thread_poll_fds(void *data
);
139 * kconsumerd_thread_receive_fds
140 * This thread listens on the consumerd socket and
141 * receives the file descriptors from ltt-sessiond
143 void *kconsumerd_thread_receive_fds(void *data
);
146 * kconsumerd_should_exit
147 * Called from signal handler to ensure a clean exit
149 void kconsumerd_should_exit(struct kconsumerd_local_data
*ctx
);
153 * Cleanup the daemon's socket on exit
155 void kconsumerd_cleanup(void);
158 * kconsumerd_set_error_socket
159 * Set the error socket for communication with a session daemon
161 void kconsumerd_set_error_socket(struct kconsumerd_local_data
*ctx
, int sock
);
164 * kconsumerd_set_command_socket_path
165 * Set the command socket path for communication with a session daemon
167 void kconsumerd_set_command_socket_path(struct kconsumerd_local_data
*ctx
, char *sock
);
169 #endif /* _LIBLTTKCONSUMERD_H */