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