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
);
83 * function to call when we receive a new fd, it receives a
84 * newly allocated kconsumerd_fd, depending on the return code
85 * of this function, the new FD will be handled by the
86 * application or the library.
89 * > 0 (success, FD is kept by application)
90 * == 0 (success, FD is left to library)
93 int (*on_recv_fd
)(struct lttng_kconsumerd_fd
*kconsumerd_fd
);
95 * function to call when a FD is getting updated by the session
96 * daemon, this function receives the FD as seen by the session
97 * daemon (sessiond_fd) and the new state, depending on the
98 * return code of this function the update of state for the FD
99 * is handled by the application or the library.
102 * > 0 (success, FD is kept by application)
103 * == 0 (success, FD is left to library)
106 int (*on_update_fd
)(int sessiond_fd
, uint32_t state
);
107 /* socket to communicate errors with sessiond */
108 int kconsumerd_error_socket
;
109 /* socket to exchange commands with sessiond */
110 char *kconsumerd_command_sock_path
;
111 /* communication with splice */
112 int kconsumerd_thread_pipe
[2];
113 /* pipe to wake the poll thread when necessary */
114 int kconsumerd_poll_pipe
[2];
115 /* to let the signal handler wake up the fd receiver thread */
116 int kconsumerd_should_quit
[2];
120 * Initialise the necessary environnement:
121 * - create a new context
122 * - create the poll_pipe
123 * - create the should_quit pipe (for signal handler)
124 * - create the thread pipe (for splice)
126 * Takes the function pointers to the on_buffer_ready, on_recv_fd, and
127 * on_update_fd callbacks.
129 * Returns a pointer to the new context or NULL on error.
131 extern struct lttng_kconsumerd_local_data
*lttng_kconsumerd_create(
132 int (*buffer_ready
)(struct lttng_kconsumerd_fd
*kconsumerd_fd
),
133 int (*recv_fd
)(struct lttng_kconsumerd_fd
*kconsumerd_fd
),
134 int (*update_fd
)(int sessiond_fd
, uint32_t state
));
137 * Close all fds associated with the instance and free the context.
139 extern void lttng_kconsumerd_destroy(struct lttng_kconsumerd_local_data
*ctx
);
142 * Mmap the ring buffer, read it and write the data to the tracefile.
144 * Returns the number of bytes written.
146 extern int lttng_kconsumerd_on_read_subbuffer_mmap(
147 struct lttng_kconsumerd_local_data
*ctx
,
148 struct lttng_kconsumerd_fd
*kconsumerd_fd
, unsigned long len
);
151 * Splice the data from the ring buffer to the tracefile.
153 * Returns the number of bytes spliced.
155 extern int lttng_kconsumerd_on_read_subbuffer_splice(
156 struct lttng_kconsumerd_local_data
*ctx
,
157 struct lttng_kconsumerd_fd
*kconsumerd_fd
, unsigned long len
);
160 * Take a snapshot for a specific fd
162 * Returns 0 on success, < 0 on error
164 int lttng_kconsumerd_take_snapshot(struct lttng_kconsumerd_local_data
*ctx
,
165 struct lttng_kconsumerd_fd
*kconsumerd_fd
);
168 * Get the produced position
170 * Returns 0 on success, < 0 on error
172 int lttng_kconsumerd_get_produced_snapshot(
173 struct lttng_kconsumerd_local_data
*ctx
,
174 struct lttng_kconsumerd_fd
*kconsumerd_fd
,
178 * Send return code to session daemon.
180 * Returns the return code of sendmsg : the number of bytes transmitted or -1
183 extern int lttng_kconsumerd_send_error(
184 struct lttng_kconsumerd_local_data
*ctx
, int cmd
);
187 * Poll on the should_quit pipe and the command socket return -1 on error and
188 * should exit, 0 if data is available on the command socket.
190 extern int lttng_kconsumerd_poll_socket(struct pollfd
*kconsumerd_sockpoll
);
193 * This thread polls the fds in the ltt_fd_list to consume the data and write
194 * it to tracefile if necessary.
196 extern void *lttng_kconsumerd_thread_poll_fds(void *data
);
199 * This thread listens on the consumerd socket and receives the file
200 * descriptors from ltt-sessiond.
202 extern void *lttng_kconsumerd_thread_receive_fds(void *data
);
205 * Called from signal handler to ensure a clean exit.
207 extern void lttng_kconsumerd_should_exit(
208 struct lttng_kconsumerd_local_data
*ctx
);
211 * Cleanup the daemon's socket on exit.
213 extern void lttng_kconsumerd_cleanup(void);
216 * Set the error socket for communication with a session daemon.
218 extern void lttng_kconsumerd_set_error_sock(
219 struct lttng_kconsumerd_local_data
*ctx
, int sock
);
222 * Set the command socket path for communication with a session daemon.
224 extern void lttng_kconsumerd_set_command_sock_path(
225 struct lttng_kconsumerd_local_data
*ctx
, char *sock
);
227 #endif /* _LTTNG_KCONSUMERD_H */