Commit | Line | Data |
---|---|---|
3bd1e081 MD |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
3 | * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU General Public License | |
7 | * as published by the Free Software Foundation; only version 2 | |
8 | * of the License. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | */ | |
19 | ||
20 | #ifndef _LTTNG_CONSUMER_H | |
21 | #define _LTTNG_CONSUMER_H | |
22 | ||
23 | #include <limits.h> | |
24 | #include <poll.h> | |
6df2e2c9 | 25 | #include <unistd.h> |
3bd1e081 MD |
26 | #include <urcu/list.h> |
27 | #include <lttng/lttng.h> | |
28 | ||
29 | /* | |
30 | * When the receiving thread dies, we need to have a way to make the polling | |
31 | * thread exit eventually. If all FDs hang up (normal case when the | |
32258573 DG |
32 | * lttng-sessiond stops), we can exit cleanly, but if there is a problem and |
33 | * for whatever reason some FDs remain open, the consumer should still exit | |
3bd1e081 MD |
34 | * eventually. |
35 | * | |
36 | * If the timeout is reached, it means that during this period no events | |
37 | * occurred on the FDs so we need to force an exit. This case should not happen | |
38 | * but it is a safety to ensure we won't block the consumer indefinitely. | |
39 | * | |
40 | * The value of 2 seconds is an arbitrary choice. | |
41 | */ | |
42 | #define LTTNG_CONSUMER_POLL_TIMEOUT 2000 | |
43 | ||
44 | /* Commands for consumer */ | |
45 | enum lttng_consumer_command { | |
46 | LTTNG_CONSUMER_ADD_CHANNEL, | |
47 | LTTNG_CONSUMER_ADD_STREAM, | |
48 | /* pause, delete, active depending on fd state */ | |
49 | LTTNG_CONSUMER_UPDATE_STREAM, | |
50 | /* inform the consumer to quit when all fd has hang up */ | |
51 | LTTNG_CONSUMER_STOP, | |
52 | }; | |
53 | ||
54 | /* State of each fd in consumer */ | |
55 | enum lttng_consumer_stream_state { | |
56 | LTTNG_CONSUMER_ACTIVE_STREAM, | |
57 | LTTNG_CONSUMER_PAUSE_STREAM, | |
58 | LTTNG_CONSUMER_DELETE_STREAM, | |
59 | }; | |
60 | ||
61 | struct lttng_consumer_channel_list { | |
62 | struct cds_list_head head; | |
63 | }; | |
64 | ||
65 | struct lttng_consumer_stream_list { | |
66 | struct cds_list_head head; | |
67 | }; | |
68 | ||
69 | enum lttng_consumer_type { | |
70 | LTTNG_CONSUMER_UNKNOWN = 0, | |
71 | LTTNG_CONSUMER_KERNEL, | |
7753dea8 MD |
72 | LTTNG_CONSUMER64_UST, |
73 | LTTNG_CONSUMER32_UST, | |
3bd1e081 MD |
74 | }; |
75 | ||
76 | struct lttng_consumer_channel { | |
77 | struct cds_list_head list; | |
78 | int key; | |
79 | uint64_t max_sb_size; /* the subbuffer size for this channel */ | |
80 | int refcount; /* Number of streams referencing this channel */ | |
81 | /* For UST */ | |
82 | int shm_fd; | |
83 | int wait_fd; | |
84 | void *mmap_base; | |
85 | size_t mmap_len; | |
13161846 | 86 | struct lttng_ust_shm_handle *handle; |
3bd1e081 | 87 | int nr_streams; |
b5c5fc29 MD |
88 | int shm_fd_is_copy; |
89 | int wait_fd_is_copy; | |
5af2f756 | 90 | int cpucount; |
3bd1e081 MD |
91 | }; |
92 | ||
93 | /* Forward declaration for UST. */ | |
13161846 | 94 | struct lttng_ust_lib_ring_buffer; |
3bd1e081 MD |
95 | |
96 | /* | |
97 | * Internal representation of the streams, sessiond_key is used to identify | |
98 | * uniquely a stream. | |
99 | */ | |
100 | struct lttng_consumer_stream { | |
101 | struct cds_list_head list; | |
102 | struct lttng_consumer_channel *chan; /* associated channel */ | |
103 | /* | |
104 | * key is the key used by the session daemon to refer to the | |
105 | * object in the consumer daemon. | |
106 | */ | |
107 | int key; | |
108 | int shm_fd; | |
109 | int wait_fd; | |
110 | int out_fd; /* output file to write the data */ | |
111 | off_t out_fd_offset; /* write position in the output file descriptor */ | |
112 | char path_name[PATH_MAX]; /* tracefile name */ | |
113 | enum lttng_consumer_stream_state state; | |
114 | size_t shm_len; | |
115 | void *mmap_base; | |
116 | size_t mmap_len; | |
117 | enum lttng_event_output output; /* splice or mmap */ | |
b5c5fc29 MD |
118 | int shm_fd_is_copy; |
119 | int wait_fd_is_copy; | |
3bd1e081 | 120 | /* For UST */ |
13161846 | 121 | struct lttng_ust_lib_ring_buffer *buf; |
3bd1e081 | 122 | int cpu; |
d056b477 | 123 | int hangup_flush_done; |
6df2e2c9 MD |
124 | /* UID/GID of the user owning the session to which stream belongs */ |
125 | uid_t uid; | |
126 | gid_t gid; | |
3bd1e081 MD |
127 | }; |
128 | ||
129 | /* | |
130 | * UST consumer local data to the program. One or more instance per | |
131 | * process. | |
132 | */ | |
133 | struct lttng_consumer_local_data { | |
134 | /* function to call when data is available on a buffer */ | |
d41f73b7 MD |
135 | int (*on_buffer_ready)(struct lttng_consumer_stream *stream, |
136 | struct lttng_consumer_local_data *ctx); | |
3bd1e081 MD |
137 | /* |
138 | * function to call when we receive a new channel, it receives a | |
139 | * newly allocated channel, depending on the return code of this | |
140 | * function, the new channel will be handled by the application | |
141 | * or the library. | |
142 | * | |
143 | * Returns: | |
144 | * > 0 (success, FD is kept by application) | |
145 | * == 0 (success, FD is left to library) | |
146 | * < 0 (error) | |
147 | */ | |
148 | int (*on_recv_channel)(struct lttng_consumer_channel *channel); | |
149 | /* | |
150 | * function to call when we receive a new stream, it receives a | |
151 | * newly allocated stream, depending on the return code of this | |
152 | * function, the new stream will be handled by the application | |
153 | * or the library. | |
154 | * | |
155 | * Returns: | |
156 | * > 0 (success, FD is kept by application) | |
157 | * == 0 (success, FD is left to library) | |
158 | * < 0 (error) | |
159 | */ | |
160 | int (*on_recv_stream)(struct lttng_consumer_stream *stream); | |
161 | /* | |
162 | * function to call when a stream is getting updated by the session | |
163 | * daemon, this function receives the sessiond key and the new | |
164 | * state, depending on the return code of this function the | |
165 | * update of state for the stream is handled by the application | |
166 | * or the library. | |
167 | * | |
168 | * Returns: | |
169 | * > 0 (success, FD is kept by application) | |
170 | * == 0 (success, FD is left to library) | |
171 | * < 0 (error) | |
172 | */ | |
173 | int (*on_update_stream)(int sessiond_key, uint32_t state); | |
174 | /* socket to communicate errors with sessiond */ | |
175 | int consumer_error_socket; | |
176 | /* socket to exchange commands with sessiond */ | |
177 | char *consumer_command_sock_path; | |
178 | /* communication with splice */ | |
179 | int consumer_thread_pipe[2]; | |
180 | /* pipe to wake the poll thread when necessary */ | |
181 | int consumer_poll_pipe[2]; | |
182 | /* to let the signal handler wake up the fd receiver thread */ | |
183 | int consumer_should_quit[2]; | |
184 | }; | |
185 | ||
186 | /* | |
187 | * Library-level data. One instance per process. | |
188 | */ | |
189 | struct lttng_consumer_global_data { | |
190 | /* | |
191 | * consumer_data.lock protects consumer_data.fd_list, | |
192 | * consumer_data.stream_count, and consumer_data.need_update. It | |
193 | * ensures the count matches the number of items in the fd_list. | |
194 | * It ensures the list updates *always* trigger an fd_array | |
195 | * update (therefore need to make list update vs | |
196 | * consumer_data.need_update flag update atomic, and also flag | |
197 | * read, fd array and flag clear atomic). | |
198 | */ | |
199 | pthread_mutex_t lock; | |
200 | /* | |
201 | * Number of streams in the list below. Protected by | |
202 | * consumer_data.lock. | |
203 | */ | |
204 | int stream_count; | |
205 | /* | |
206 | * Lists of streams and channels. Protected by consumer_data.lock. | |
207 | */ | |
208 | struct lttng_consumer_stream_list stream_list; | |
209 | struct lttng_consumer_channel_list channel_list; | |
210 | /* | |
211 | * Flag specifying if the local array of FDs needs update in the | |
212 | * poll function. Protected by consumer_data.lock. | |
213 | */ | |
214 | unsigned int need_update; | |
215 | enum lttng_consumer_type type; | |
216 | }; | |
217 | ||
218 | /* | |
219 | * Set the error socket for communication with a session daemon. | |
220 | */ | |
221 | extern void lttng_consumer_set_error_sock( | |
222 | struct lttng_consumer_local_data *ctx, int sock); | |
223 | ||
224 | /* | |
225 | * Set the command socket path for communication with a session daemon. | |
226 | */ | |
227 | extern void lttng_consumer_set_command_sock_path( | |
228 | struct lttng_consumer_local_data *ctx, char *sock); | |
229 | ||
230 | /* | |
231 | * Send return code to session daemon. | |
232 | * | |
233 | * Returns the return code of sendmsg : the number of bytes transmitted or -1 | |
234 | * on error. | |
235 | */ | |
236 | extern int lttng_consumer_send_error( | |
237 | struct lttng_consumer_local_data *ctx, int cmd); | |
238 | ||
239 | /* | |
240 | * Called from signal handler to ensure a clean exit. | |
241 | */ | |
242 | extern void lttng_consumer_should_exit( | |
243 | struct lttng_consumer_local_data *ctx); | |
244 | ||
245 | /* | |
246 | * Cleanup the daemon's socket on exit. | |
247 | */ | |
248 | extern void lttng_consumer_cleanup(void); | |
249 | ||
250 | /* | |
251 | * Flush pending writes to trace output disk file. | |
252 | */ | |
253 | extern void lttng_consumer_sync_trace_file( | |
254 | struct lttng_consumer_stream *stream, off_t orig_offset); | |
255 | ||
256 | /* | |
257 | * Poll on the should_quit pipe and the command socket return -1 on error and | |
258 | * should exit, 0 if data is available on the command socket | |
259 | */ | |
260 | extern int lttng_consumer_poll_socket(struct pollfd *kconsumer_sockpoll); | |
261 | ||
262 | extern int consumer_update_poll_array( | |
263 | struct lttng_consumer_local_data *ctx, struct pollfd **pollfd, | |
264 | struct lttng_consumer_stream **local_consumer_streams); | |
265 | ||
266 | extern struct lttng_consumer_stream *consumer_allocate_stream( | |
267 | int channel_key, int stream_key, | |
268 | int shm_fd, int wait_fd, | |
269 | enum lttng_consumer_stream_state state, | |
270 | uint64_t mmap_len, | |
271 | enum lttng_event_output output, | |
6df2e2c9 MD |
272 | const char *path_name, |
273 | uid_t uid, | |
274 | gid_t gid); | |
3bd1e081 MD |
275 | extern int consumer_add_stream(struct lttng_consumer_stream *stream); |
276 | extern void consumer_del_stream(struct lttng_consumer_stream *stream); | |
277 | extern void consumer_change_stream_state(int stream_key, | |
278 | enum lttng_consumer_stream_state state); | |
279 | extern void consumer_del_channel(struct lttng_consumer_channel *channel); | |
280 | extern struct lttng_consumer_channel *consumer_allocate_channel( | |
281 | int channel_key, | |
282 | int shm_fd, int wait_fd, | |
283 | uint64_t mmap_len, | |
284 | uint64_t max_sb_size); | |
285 | int consumer_add_channel(struct lttng_consumer_channel *channel); | |
286 | ||
287 | extern struct lttng_consumer_local_data *lttng_consumer_create( | |
288 | enum lttng_consumer_type type, | |
d41f73b7 MD |
289 | int (*buffer_ready)(struct lttng_consumer_stream *stream, |
290 | struct lttng_consumer_local_data *ctx), | |
3bd1e081 MD |
291 | int (*recv_channel)(struct lttng_consumer_channel *channel), |
292 | int (*recv_stream)(struct lttng_consumer_stream *stream), | |
293 | int (*update_stream)(int sessiond_key, uint32_t state)); | |
294 | extern void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx); | |
295 | extern int lttng_consumer_on_read_subbuffer_mmap( | |
296 | struct lttng_consumer_local_data *ctx, | |
297 | struct lttng_consumer_stream *stream, unsigned long len); | |
298 | extern int lttng_consumer_on_read_subbuffer_splice( | |
299 | struct lttng_consumer_local_data *ctx, | |
300 | struct lttng_consumer_stream *stream, unsigned long len); | |
301 | extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx, | |
302 | struct lttng_consumer_stream *stream); | |
303 | extern int lttng_consumer_get_produced_snapshot( | |
304 | struct lttng_consumer_local_data *ctx, | |
305 | struct lttng_consumer_stream *stream, | |
306 | unsigned long *pos); | |
307 | extern void *lttng_consumer_thread_poll_fds(void *data); | |
308 | extern void *lttng_consumer_thread_receive_fds(void *data); | |
309 | extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx, | |
310 | int sock, struct pollfd *consumer_sockpoll); | |
311 | ||
d41f73b7 MD |
312 | int lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream, |
313 | struct lttng_consumer_local_data *ctx); | |
314 | int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream); | |
315 | ||
3bd1e081 | 316 | #endif /* _LTTNG_CONSUMER_H */ |