2aa337fa66b9e1d61f250642c355b7099d05ea35
[lttng-tools.git] / include / lttng / lttng-kconsumerd.h
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 *
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
7 * of the License.
8 *
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.
13 *
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.
17 */
18
19 #ifndef _LTTNG_KCONSUMERD_H
20 #define _LTTNG_KCONSUMERD_H
21
22 #include <limits.h>
23 #include <lttng/lttng.h>
24 #include <urcu/list.h>
25
26 /*
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
31 * eventually.
32 *
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.
36 *
37 * The value of 2 seconds is an arbitrary choice.
38 */
39 #define LTTNG_KCONSUMERD_POLL_GRACE_PERIOD 2000
40
41 /* Commands for kconsumerd */
42 enum lttng_kconsumerd_command {
43 ADD_STREAM,
44 UPDATE_STREAM, /* pause, delete, active depending on fd state */
45 STOP, /* inform the kconsumerd to quit when all fd has hang up */
46 };
47
48 /* State of each fd in consumerd */
49 enum lttng_kconsumerd_fd_state {
50 ACTIVE_FD,
51 PAUSE_FD,
52 DELETE_FD,
53 };
54
55 struct lttng_kconsumerd_fd_list {
56 struct cds_list_head head;
57 };
58
59 /*
60 * Internal representation of the FDs, sessiond_fd is used to identify uniquely
61 * a fd
62 */
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 */
72 void *mmap_base;
73 size_t mmap_len;
74 enum lttng_event_output output; /* splice or mmap */
75 };
76
77 /*
78 * Kernel consumer local data to the program.
79 */
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);
83 /*
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.
88 *
89 * Returns:
90 * > 0 (success, FD is kept by application)
91 * == 0 (success, FD is left to library)
92 * < 0 (error)
93 */
94 int (*on_recv_fd)(struct lttng_kconsumerd_fd *kconsumerd_fd);
95 /*
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.
101 *
102 * Returns:
103 * > 0 (success, FD is kept by application)
104 * == 0 (success, FD is left to library)
105 * < 0 (error)
106 */
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];
118 };
119
120 /*
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)
126 *
127 * Takes the function pointers to the on_buffer_ready, on_recv_fd, and
128 * on_update_fd callbacks.
129 *
130 * Returns a pointer to the new context or NULL on error.
131 */
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));
136
137 /*
138 * Close all fds associated with the instance and free the context.
139 */
140 extern void lttng_kconsumerd_destroy(struct lttng_kconsumerd_local_data *ctx);
141
142 /*
143 * Mmap the ring buffer, read it and write the data to the tracefile.
144 *
145 * Returns the number of bytes written.
146 */
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);
150
151 /*
152 * Splice the data from the ring buffer to the tracefile.
153 *
154 * Returns the number of bytes spliced.
155 */
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);
159
160 /*
161 * Take a snapshot for a specific fd
162 *
163 * Returns 0 on success, < 0 on error
164 */
165 int lttng_kconsumerd_take_snapshot(struct lttng_kconsumerd_local_data *ctx,
166 struct lttng_kconsumerd_fd *kconsumerd_fd);
167
168 /*
169 * Get the produced position
170 *
171 * Returns 0 on success, < 0 on error
172 */
173 int lttng_kconsumerd_get_produced_snapshot(
174 struct lttng_kconsumerd_local_data *ctx,
175 struct lttng_kconsumerd_fd *kconsumerd_fd,
176 unsigned long *pos);
177
178 /*
179 * Send return code to session daemon.
180 *
181 * Returns the return code of sendmsg : the number of bytes transmitted or -1
182 * on error.
183 */
184 extern int lttng_kconsumerd_send_error(
185 struct lttng_kconsumerd_local_data *ctx, int cmd);
186
187 /*
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.
190 */
191 extern int lttng_kconsumerd_poll_socket(struct pollfd *kconsumerd_sockpoll);
192
193 /*
194 * This thread polls the fds in the ltt_fd_list to consume the data and write
195 * it to tracefile if necessary.
196 */
197 extern void *lttng_kconsumerd_thread_poll_fds(void *data);
198
199 /*
200 * This thread listens on the consumerd socket and receives the file
201 * descriptors from ltt-sessiond.
202 */
203 extern void *lttng_kconsumerd_thread_receive_fds(void *data);
204
205 /*
206 * Called from signal handler to ensure a clean exit.
207 */
208 extern void lttng_kconsumerd_should_exit(
209 struct lttng_kconsumerd_local_data *ctx);
210
211 /*
212 * Cleanup the daemon's socket on exit.
213 */
214 extern void lttng_kconsumerd_cleanup(void);
215
216 /*
217 * Set the error socket for communication with a session daemon.
218 */
219 extern void lttng_kconsumerd_set_error_sock(
220 struct lttng_kconsumerd_local_data *ctx, int sock);
221
222 /*
223 * Set the command socket path for communication with a session daemon.
224 */
225 extern void lttng_kconsumerd_set_command_sock_path(
226 struct lttng_kconsumerd_local_data *ctx, char *sock);
227
228 #endif /* _LTTNG_KCONSUMERD_H */
This page took 0.033305 seconds and 3 git commands to generate.