Commit | Line | Data |
---|---|---|
6533b585 DG |
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> | |
5eb91c98 | 24 | #include <poll.h> |
1e307fab | 25 | #include <urcu/list.h> |
6533b585 DG |
26 | |
27 | /* | |
28 | * When the receiving thread dies, we need to have a way to make the polling | |
29 | * thread exit eventually. If all FDs hang up (normal case when the | |
30 | * ltt-sessiond stops), we can exit cleanly, but if there is a problem and for | |
31 | * whatever reason some FDs remain open, the consumer should still exit | |
32 | * eventually. | |
33 | * | |
34 | * If the timeout is reached, it means that during this period no events | |
35 | * occurred on the FDs so we need to force an exit. This case should not happen | |
36 | * but it is a safety to ensure we won't block the consumer indefinitely. | |
37 | * | |
38 | * The value of 2 seconds is an arbitrary choice. | |
39 | */ | |
40 | #define LTTNG_KCONSUMERD_POLL_GRACE_PERIOD 2000 | |
41 | ||
42 | /* Commands for kconsumerd */ | |
43 | enum lttng_kconsumerd_command { | |
44 | ADD_STREAM, | |
45 | UPDATE_STREAM, /* pause, delete, active depending on fd state */ | |
46 | STOP, /* inform the kconsumerd to quit when all fd has hang up */ | |
47 | }; | |
48 | ||
49 | /* State of each fd in consumerd */ | |
50 | enum lttng_kconsumerd_fd_state { | |
51 | ACTIVE_FD, | |
52 | PAUSE_FD, | |
53 | DELETE_FD, | |
54 | }; | |
55 | ||
56 | struct lttng_kconsumerd_fd_list { | |
57 | struct cds_list_head head; | |
58 | }; | |
59 | ||
60 | /* | |
61 | * Internal representation of the FDs, sessiond_fd is used to identify uniquely | |
62 | * a fd | |
63 | */ | |
64 | struct lttng_kconsumerd_fd { | |
65 | struct cds_list_head list; | |
66 | int sessiond_fd; /* used to identify uniquely a fd with sessiond */ | |
67 | int consumerd_fd; /* fd to consume */ | |
68 | int out_fd; /* output file to write the data */ | |
69 | off_t out_fd_offset; /* write position in the output file descriptor */ | |
70 | char path_name[PATH_MAX]; /* tracefile name */ | |
71 | enum lttng_kconsumerd_fd_state state; | |
72 | unsigned long max_sb_size; /* the subbuffer size for this channel */ | |
73 | void *mmap_base; | |
74 | size_t mmap_len; | |
75 | enum lttng_event_output output; /* splice or mmap */ | |
76 | }; | |
77 | ||
78 | /* | |
79 | * Kernel consumer local data to the program. | |
80 | */ | |
81 | struct lttng_kconsumerd_local_data { | |
82 | /* function to call when data is available on a buffer */ | |
83 | int (*on_buffer_ready)(struct lttng_kconsumerd_fd *kconsumerd_fd); | |
5348b470 JD |
84 | /* |
85 | * function to call when we receive a new fd, it receives a | |
86 | * newly allocated kconsumerd_fd, depending on the return code | |
87 | * of this function, the new FD will be handled by the | |
88 | * application or the library. | |
89 | * | |
90 | * Returns: | |
91 | * > 0 (success, FD is kept by application) | |
92 | * == 0 (success, FD is left to library) | |
93 | * < 0 (error) | |
94 | */ | |
95 | int (*on_recv_fd)(struct lttng_kconsumerd_fd *kconsumerd_fd); | |
96 | /* | |
97 | * function to call when a FD is getting updated by the session | |
98 | * daemon, this function receives the FD as seen by the session | |
99 | * daemon (sessiond_fd) and the new state, depending on the | |
100 | * return code of this function the update of state for the FD | |
101 | * is handled by the application or the library. | |
102 | * | |
103 | * Returns: | |
104 | * > 0 (success, FD is kept by application) | |
105 | * == 0 (success, FD is left to library) | |
106 | * < 0 (error) | |
107 | */ | |
108 | int (*on_update_fd)(int sessiond_fd, uint32_t state); | |
6533b585 DG |
109 | /* socket to communicate errors with sessiond */ |
110 | int kconsumerd_error_socket; | |
111 | /* socket to exchange commands with sessiond */ | |
112 | char *kconsumerd_command_sock_path; | |
113 | /* communication with splice */ | |
114 | int kconsumerd_thread_pipe[2]; | |
115 | /* pipe to wake the poll thread when necessary */ | |
116 | int kconsumerd_poll_pipe[2]; | |
117 | /* to let the signal handler wake up the fd receiver thread */ | |
118 | int kconsumerd_should_quit[2]; | |
119 | }; | |
120 | ||
121 | /* | |
122 | * Initialise the necessary environnement: | |
123 | * - create a new context | |
124 | * - create the poll_pipe | |
125 | * - create the should_quit pipe (for signal handler) | |
126 | * - create the thread pipe (for splice) | |
127 | * | |
5348b470 JD |
128 | * Takes the function pointers to the on_buffer_ready, on_recv_fd, and |
129 | * on_update_fd callbacks. | |
6533b585 DG |
130 | * |
131 | * Returns a pointer to the new context or NULL on error. | |
132 | */ | |
133 | extern struct lttng_kconsumerd_local_data *lttng_kconsumerd_create( | |
5348b470 JD |
134 | int (*buffer_ready)(struct lttng_kconsumerd_fd *kconsumerd_fd), |
135 | int (*recv_fd)(struct lttng_kconsumerd_fd *kconsumerd_fd), | |
136 | int (*update_fd)(int sessiond_fd, uint32_t state)); | |
6533b585 DG |
137 | |
138 | /* | |
139 | * Close all fds associated with the instance and free the context. | |
140 | */ | |
141 | extern void lttng_kconsumerd_destroy(struct lttng_kconsumerd_local_data *ctx); | |
142 | ||
143 | /* | |
144 | * Mmap the ring buffer, read it and write the data to the tracefile. | |
145 | * | |
146 | * Returns the number of bytes written. | |
147 | */ | |
148 | extern int lttng_kconsumerd_on_read_subbuffer_mmap( | |
149 | struct lttng_kconsumerd_local_data *ctx, | |
150 | struct lttng_kconsumerd_fd *kconsumerd_fd, unsigned long len); | |
151 | ||
152 | /* | |
153 | * Splice the data from the ring buffer to the tracefile. | |
154 | * | |
155 | * Returns the number of bytes spliced. | |
156 | */ | |
157 | extern int lttng_kconsumerd_on_read_subbuffer_splice( | |
158 | struct lttng_kconsumerd_local_data *ctx, | |
159 | struct lttng_kconsumerd_fd *kconsumerd_fd, unsigned long len); | |
160 | ||
92ab9ab6 JD |
161 | /* |
162 | * Take a snapshot for a specific fd | |
163 | * | |
164 | * Returns 0 on success, < 0 on error | |
165 | */ | |
166 | int lttng_kconsumerd_take_snapshot(struct lttng_kconsumerd_local_data *ctx, | |
167 | struct lttng_kconsumerd_fd *kconsumerd_fd); | |
168 | ||
169 | /* | |
170 | * Get the produced position | |
171 | * | |
172 | * Returns 0 on success, < 0 on error | |
173 | */ | |
174 | int lttng_kconsumerd_get_produced_snapshot( | |
175 | struct lttng_kconsumerd_local_data *ctx, | |
176 | struct lttng_kconsumerd_fd *kconsumerd_fd, | |
177 | unsigned long *pos); | |
178 | ||
6533b585 DG |
179 | /* |
180 | * Send return code to session daemon. | |
181 | * | |
182 | * Returns the return code of sendmsg : the number of bytes transmitted or -1 | |
183 | * on error. | |
184 | */ | |
185 | extern int lttng_kconsumerd_send_error( | |
186 | struct lttng_kconsumerd_local_data *ctx, int cmd); | |
187 | ||
188 | /* | |
189 | * Poll on the should_quit pipe and the command socket return -1 on error and | |
190 | * should exit, 0 if data is available on the command socket. | |
191 | */ | |
192 | extern int lttng_kconsumerd_poll_socket(struct pollfd *kconsumerd_sockpoll); | |
193 | ||
194 | /* | |
195 | * This thread polls the fds in the ltt_fd_list to consume the data and write | |
196 | * it to tracefile if necessary. | |
197 | */ | |
198 | extern void *lttng_kconsumerd_thread_poll_fds(void *data); | |
199 | ||
200 | /* | |
201 | * This thread listens on the consumerd socket and receives the file | |
202 | * descriptors from ltt-sessiond. | |
203 | */ | |
204 | extern void *lttng_kconsumerd_thread_receive_fds(void *data); | |
205 | ||
206 | /* | |
207 | * Called from signal handler to ensure a clean exit. | |
208 | */ | |
209 | extern void lttng_kconsumerd_should_exit( | |
210 | struct lttng_kconsumerd_local_data *ctx); | |
211 | ||
212 | /* | |
213 | * Cleanup the daemon's socket on exit. | |
214 | */ | |
215 | extern void lttng_kconsumerd_cleanup(void); | |
216 | ||
217 | /* | |
218 | * Set the error socket for communication with a session daemon. | |
219 | */ | |
220 | extern void lttng_kconsumerd_set_error_sock( | |
221 | struct lttng_kconsumerd_local_data *ctx, int sock); | |
222 | ||
223 | /* | |
224 | * Set the command socket path for communication with a session daemon. | |
225 | */ | |
226 | extern void lttng_kconsumerd_set_command_sock_path( | |
227 | struct lttng_kconsumerd_local_data *ctx, char *sock); | |
228 | ||
229 | #endif /* _LTTNG_KCONSUMERD_H */ |