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 _LTTNG_KCONSUMERD_H
20 #define _LTTNG_KCONSUMERD_H
23 #include <lttng/lttng.h>
26 * When the receiving thread dies, we need to have a way to make the polling
27 * thread exit eventually. If all FDs hang up (normal case when the
28 * ltt-sessiond stops), we can exit cleanly, but if there is a problem and for
29 * whatever reason some FDs remain open, the consumer should still exit
32 * If the timeout is reached, it means that during this period no events
33 * occurred on the FDs so we need to force an exit. This case should not happen
34 * but it is a safety to ensure we won't block the consumer indefinitely.
36 * The value of 2 seconds is an arbitrary choice.
38 #define LTTNG_KCONSUMERD_POLL_GRACE_PERIOD 2000
40 /* Commands for kconsumerd */
41 enum lttng_kconsumerd_command
{
43 UPDATE_STREAM
, /* pause, delete, active depending on fd state */
44 STOP
, /* inform the kconsumerd to quit when all fd has hang up */
47 /* State of each fd in consumerd */
48 enum lttng_kconsumerd_fd_state
{
54 struct lttng_kconsumerd_fd_list
{
55 struct cds_list_head head
;
59 * Internal representation of the FDs, sessiond_fd is used to identify uniquely
62 struct lttng_kconsumerd_fd
{
63 struct cds_list_head list
;
64 int sessiond_fd
; /* used to identify uniquely a fd with sessiond */
65 int consumerd_fd
; /* fd to consume */
66 int out_fd
; /* output file to write the data */
67 off_t out_fd_offset
; /* write position in the output file descriptor */
68 char path_name
[PATH_MAX
]; /* tracefile name */
69 enum lttng_kconsumerd_fd_state state
;
70 unsigned long max_sb_size
; /* the subbuffer size for this channel */
73 enum lttng_event_output output
; /* splice or mmap */
77 * Kernel consumer local data to the program.
79 struct lttng_kconsumerd_local_data
{
80 /* function to call when data is available on a buffer */
81 int (*on_buffer_ready
)(struct lttng_kconsumerd_fd
*kconsumerd_fd
);
82 /* socket to communicate errors with sessiond */
83 int kconsumerd_error_socket
;
84 /* socket to exchange commands with sessiond */
85 char *kconsumerd_command_sock_path
;
86 /* communication with splice */
87 int kconsumerd_thread_pipe
[2];
88 /* pipe to wake the poll thread when necessary */
89 int kconsumerd_poll_pipe
[2];
90 /* to let the signal handler wake up the fd receiver thread */
91 int kconsumerd_should_quit
[2];
95 * Initialise the necessary environnement:
96 * - create a new context
97 * - create the poll_pipe
98 * - create the should_quit pipe (for signal handler)
99 * - create the thread pipe (for splice)
101 * Takes a function pointer as argument, this function is called when data is
102 * available on a buffer. This function is responsible to do the
103 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
104 * buffer configuration and then kernctl_put_next_subbuf at the end.
106 * Returns a pointer to the new context or NULL on error.
108 extern struct lttng_kconsumerd_local_data
*lttng_kconsumerd_create(
109 int (*buffer_ready
)(struct lttng_kconsumerd_fd
*kconsumerd_fd
));
112 * Close all fds associated with the instance and free the context.
114 extern void lttng_kconsumerd_destroy(struct lttng_kconsumerd_local_data
*ctx
);
117 * Mmap the ring buffer, read it and write the data to the tracefile.
119 * Returns the number of bytes written.
121 extern int lttng_kconsumerd_on_read_subbuffer_mmap(
122 struct lttng_kconsumerd_local_data
*ctx
,
123 struct lttng_kconsumerd_fd
*kconsumerd_fd
, unsigned long len
);
126 * Splice the data from the ring buffer to the tracefile.
128 * Returns the number of bytes spliced.
130 extern int lttng_kconsumerd_on_read_subbuffer_splice(
131 struct lttng_kconsumerd_local_data
*ctx
,
132 struct lttng_kconsumerd_fd
*kconsumerd_fd
, unsigned long len
);
135 * Send return code to session daemon.
137 * Returns the return code of sendmsg : the number of bytes transmitted or -1
140 extern int lttng_kconsumerd_send_error(
141 struct lttng_kconsumerd_local_data
*ctx
, int cmd
);
144 * Poll on the should_quit pipe and the command socket return -1 on error and
145 * should exit, 0 if data is available on the command socket.
147 extern int lttng_kconsumerd_poll_socket(struct pollfd
*kconsumerd_sockpoll
);
150 * This thread polls the fds in the ltt_fd_list to consume the data and write
151 * it to tracefile if necessary.
153 extern void *lttng_kconsumerd_thread_poll_fds(void *data
);
156 * This thread listens on the consumerd socket and receives the file
157 * descriptors from ltt-sessiond.
159 extern void *lttng_kconsumerd_thread_receive_fds(void *data
);
162 * Called from signal handler to ensure a clean exit.
164 extern void lttng_kconsumerd_should_exit(
165 struct lttng_kconsumerd_local_data
*ctx
);
168 * Cleanup the daemon's socket on exit.
170 extern void lttng_kconsumerd_cleanup(void);
173 * Set the error socket for communication with a session daemon.
175 extern void lttng_kconsumerd_set_error_sock(
176 struct lttng_kconsumerd_local_data
*ctx
, int sock
);
179 * Set the command socket path for communication with a session daemon.
181 extern void lttng_kconsumerd_set_command_sock_path(
182 struct lttng_kconsumerd_local_data
*ctx
, char *sock
);
184 #endif /* _LTTNG_KCONSUMERD_H */