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>
24 #include <urcu/list.h>
27 * When the receiving thread dies, we need to have a way to make the polling
28 * thread exit eventually. If all FDs hang up (normal case when the
29 * ltt-sessiond stops), we can exit cleanly, but if there is a problem and for
30 * whatever reason some FDs remain open, the consumer should still exit
33 * If the timeout is reached, it means that during this period no events
34 * occurred on the FDs so we need to force an exit. This case should not happen
35 * but it is a safety to ensure we won't block the consumer indefinitely.
37 * The value of 2 seconds is an arbitrary choice.
39 #define LTTNG_KCONSUMERD_POLL_GRACE_PERIOD 2000
41 /* Commands for kconsumerd */
42 enum lttng_kconsumerd_command
{
44 UPDATE_STREAM
, /* pause, delete, active depending on fd state */
45 STOP
, /* inform the kconsumerd to quit when all fd has hang up */
48 /* State of each fd in consumerd */
49 enum lttng_kconsumerd_fd_state
{
55 struct lttng_kconsumerd_fd_list
{
56 struct cds_list_head head
;
60 * Internal representation of the FDs, sessiond_fd is used to identify uniquely
63 struct lttng_kconsumerd_fd
{
64 struct cds_list_head list
;
65 int sessiond_fd
; /* used to identify uniquely a fd with sessiond */
66 int consumerd_fd
; /* fd to consume */
67 int out_fd
; /* output file to write the data */
68 off_t out_fd_offset
; /* write position in the output file descriptor */
69 char path_name
[PATH_MAX
]; /* tracefile name */
70 enum lttng_kconsumerd_fd_state state
;
71 unsigned long max_sb_size
; /* the subbuffer size for this channel */
74 enum lttng_event_output output
; /* splice or mmap */
78 * Kernel consumer local data to the program.
80 struct lttng_kconsumerd_local_data
{
81 /* function to call when data is available on a buffer */
82 int (*on_buffer_ready
)(struct lttng_kconsumerd_fd
*kconsumerd_fd
);
84 * function to call when we receive a new fd, it receives a
85 * newly allocated kconsumerd_fd, depending on the return code
86 * of this function, the new FD will be handled by the
87 * application or the library.
90 * > 0 (success, FD is kept by application)
91 * == 0 (success, FD is left to library)
94 int (*on_recv_fd
)(struct lttng_kconsumerd_fd
*kconsumerd_fd
);
96 * function to call when a FD is getting updated by the session
97 * daemon, this function receives the FD as seen by the session
98 * daemon (sessiond_fd) and the new state, depending on the
99 * return code of this function the update of state for the FD
100 * is handled by the application or the library.
103 * > 0 (success, FD is kept by application)
104 * == 0 (success, FD is left to library)
107 int (*on_update_fd
)(int sessiond_fd
, uint32_t state
);
108 /* socket to communicate errors with sessiond */
109 int kconsumerd_error_socket
;
110 /* socket to exchange commands with sessiond */
111 char *kconsumerd_command_sock_path
;
112 /* communication with splice */
113 int kconsumerd_thread_pipe
[2];
114 /* pipe to wake the poll thread when necessary */
115 int kconsumerd_poll_pipe
[2];
116 /* to let the signal handler wake up the fd receiver thread */
117 int kconsumerd_should_quit
[2];
121 * Initialise the necessary environnement:
122 * - create a new context
123 * - create the poll_pipe
124 * - create the should_quit pipe (for signal handler)
125 * - create the thread pipe (for splice)
127 * Takes the function pointers to the on_buffer_ready, on_recv_fd, and
128 * on_update_fd callbacks.
130 * Returns a pointer to the new context or NULL on error.
132 extern struct lttng_kconsumerd_local_data
*lttng_kconsumerd_create(
133 int (*buffer_ready
)(struct lttng_kconsumerd_fd
*kconsumerd_fd
),
134 int (*recv_fd
)(struct lttng_kconsumerd_fd
*kconsumerd_fd
),
135 int (*update_fd
)(int sessiond_fd
, uint32_t state
));
138 * Close all fds associated with the instance and free the context.
140 extern void lttng_kconsumerd_destroy(struct lttng_kconsumerd_local_data
*ctx
);
143 * Mmap the ring buffer, read it and write the data to the tracefile.
145 * Returns the number of bytes written.
147 extern int lttng_kconsumerd_on_read_subbuffer_mmap(
148 struct lttng_kconsumerd_local_data
*ctx
,
149 struct lttng_kconsumerd_fd
*kconsumerd_fd
, unsigned long len
);
152 * Splice the data from the ring buffer to the tracefile.
154 * Returns the number of bytes spliced.
156 extern int lttng_kconsumerd_on_read_subbuffer_splice(
157 struct lttng_kconsumerd_local_data
*ctx
,
158 struct lttng_kconsumerd_fd
*kconsumerd_fd
, unsigned long len
);
161 * Take a snapshot for a specific fd
163 * Returns 0 on success, < 0 on error
165 int lttng_kconsumerd_take_snapshot(struct lttng_kconsumerd_local_data
*ctx
,
166 struct lttng_kconsumerd_fd
*kconsumerd_fd
);
169 * Get the produced position
171 * Returns 0 on success, < 0 on error
173 int lttng_kconsumerd_get_produced_snapshot(
174 struct lttng_kconsumerd_local_data
*ctx
,
175 struct lttng_kconsumerd_fd
*kconsumerd_fd
,
179 * Send return code to session daemon.
181 * Returns the return code of sendmsg : the number of bytes transmitted or -1
184 extern int lttng_kconsumerd_send_error(
185 struct lttng_kconsumerd_local_data
*ctx
, int cmd
);
188 * Poll on the should_quit pipe and the command socket return -1 on error and
189 * should exit, 0 if data is available on the command socket.
191 extern int lttng_kconsumerd_poll_socket(struct pollfd
*kconsumerd_sockpoll
);
194 * This thread polls the fds in the ltt_fd_list to consume the data and write
195 * it to tracefile if necessary.
197 extern void *lttng_kconsumerd_thread_poll_fds(void *data
);
200 * This thread listens on the consumerd socket and receives the file
201 * descriptors from ltt-sessiond.
203 extern void *lttng_kconsumerd_thread_receive_fds(void *data
);
206 * Called from signal handler to ensure a clean exit.
208 extern void lttng_kconsumerd_should_exit(
209 struct lttng_kconsumerd_local_data
*ctx
);
212 * Cleanup the daemon's socket on exit.
214 extern void lttng_kconsumerd_cleanup(void);
217 * Set the error socket for communication with a session daemon.
219 extern void lttng_kconsumerd_set_error_sock(
220 struct lttng_kconsumerd_local_data
*ctx
, int sock
);
223 * Set the command socket path for communication with a session daemon.
225 extern void lttng_kconsumerd_set_command_sock_path(
226 struct lttng_kconsumerd_local_data
*ctx
, char *sock
);
228 #endif /* _LTTNG_KCONSUMERD_H */