Commit | Line | Data |
---|---|---|
3bd1e081 MD |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
00e2e675 DG |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
4 | * 2012 - David Goulet <dgoulet@efficios.com> | |
3bd1e081 | 5 | * |
d14d33bf AM |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License, version 2 only, | |
8 | * as published by the Free Software Foundation. | |
3bd1e081 | 9 | * |
d14d33bf AM |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
3bd1e081 | 14 | * |
d14d33bf AM |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
3bd1e081 MD |
18 | */ |
19 | ||
f02e1e8a DG |
20 | #ifndef LIB_CONSUMER_H |
21 | #define LIB_CONSUMER_H | |
3bd1e081 MD |
22 | |
23 | #include <limits.h> | |
24 | #include <poll.h> | |
6df2e2c9 | 25 | #include <unistd.h> |
e4421fec | 26 | |
3bd1e081 | 27 | #include <lttng/lttng.h> |
10a8a223 | 28 | |
b9182dd9 DG |
29 | #include <common/hashtable/hashtable.h> |
30 | #include <common/compat/fcntl.h> | |
00e2e675 | 31 | #include <common/sessiond-comm/sessiond-comm.h> |
3bd1e081 MD |
32 | |
33 | /* | |
34 | * When the receiving thread dies, we need to have a way to make the polling | |
35 | * thread exit eventually. If all FDs hang up (normal case when the | |
32258573 DG |
36 | * lttng-sessiond stops), we can exit cleanly, but if there is a problem and |
37 | * for whatever reason some FDs remain open, the consumer should still exit | |
3bd1e081 MD |
38 | * eventually. |
39 | * | |
40 | * If the timeout is reached, it means that during this period no events | |
41 | * occurred on the FDs so we need to force an exit. This case should not happen | |
42 | * but it is a safety to ensure we won't block the consumer indefinitely. | |
43 | * | |
44 | * The value of 2 seconds is an arbitrary choice. | |
45 | */ | |
46 | #define LTTNG_CONSUMER_POLL_TIMEOUT 2000 | |
47 | ||
48 | /* Commands for consumer */ | |
49 | enum lttng_consumer_command { | |
50 | LTTNG_CONSUMER_ADD_CHANNEL, | |
51 | LTTNG_CONSUMER_ADD_STREAM, | |
52 | /* pause, delete, active depending on fd state */ | |
53 | LTTNG_CONSUMER_UPDATE_STREAM, | |
54 | /* inform the consumer to quit when all fd has hang up */ | |
55 | LTTNG_CONSUMER_STOP, | |
00e2e675 | 56 | LTTNG_CONSUMER_ADD_RELAYD_SOCKET, |
173af62f DG |
57 | /* Inform the consumer to kill a specific relayd connection */ |
58 | LTTNG_CONSUMER_DESTROY_RELAYD, | |
3bd1e081 MD |
59 | }; |
60 | ||
61 | /* State of each fd in consumer */ | |
62 | enum lttng_consumer_stream_state { | |
63 | LTTNG_CONSUMER_ACTIVE_STREAM, | |
64 | LTTNG_CONSUMER_PAUSE_STREAM, | |
65 | LTTNG_CONSUMER_DELETE_STREAM, | |
66 | }; | |
67 | ||
3bd1e081 MD |
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 { | |
e4421fec | 76 | struct lttng_ht_node_ulong node; |
3bd1e081 MD |
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 | 87 | int wait_fd_is_copy; |
5af2f756 | 88 | int cpucount; |
3bd1e081 MD |
89 | }; |
90 | ||
91 | /* Forward declaration for UST. */ | |
13161846 | 92 | struct lttng_ust_lib_ring_buffer; |
3bd1e081 MD |
93 | |
94 | /* | |
95 | * Internal representation of the streams, sessiond_key is used to identify | |
96 | * uniquely a stream. | |
97 | */ | |
98 | struct lttng_consumer_stream { | |
e4421fec | 99 | struct lttng_ht_node_ulong node; |
00e2e675 | 100 | struct lttng_ht_node_ulong waitfd_node; |
3bd1e081 MD |
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; |
4078b776 | 122 | int data_read; |
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; | |
00e2e675 DG |
127 | /* Network sequence number. Indicating on which relayd socket it goes. */ |
128 | int net_seq_idx; | |
129 | /* Identify if the stream is the metadata */ | |
130 | unsigned int metadata_flag; | |
131 | /* Used when the stream is set for network streaming */ | |
132 | uint64_t relayd_stream_id; | |
173af62f DG |
133 | /* Next sequence number to use for trace packet */ |
134 | uint64_t next_net_seq_num; | |
00e2e675 DG |
135 | }; |
136 | ||
137 | /* | |
138 | * Internal representation of a relayd socket pair. | |
139 | */ | |
140 | struct consumer_relayd_sock_pair { | |
141 | /* Network sequence number. */ | |
142 | int net_seq_idx; | |
143 | /* Number of stream associated with this relayd */ | |
144 | unsigned int refcount; | |
173af62f DG |
145 | |
146 | /* | |
147 | * This flag indicates whether or not we should destroy this object. The | |
148 | * destruction should ONLY occurs when this flag is set and the refcount is | |
149 | * set to zero. | |
150 | */ | |
151 | unsigned int destroy_flag; | |
152 | ||
51d7db73 DG |
153 | /* |
154 | * Mutex protecting the control socket to avoid out of order packets | |
155 | * between threads sending data to the relayd. Since metadata data is sent | |
156 | * over that socket, at least two sendmsg() are needed (header + data) | |
157 | * creating a race for packets to overlap between threads using it. | |
158 | */ | |
00e2e675 | 159 | pthread_mutex_t ctrl_sock_mutex; |
51d7db73 DG |
160 | |
161 | /* Control socket. Command and metadata are passed over it */ | |
00e2e675 | 162 | struct lttcomm_sock control_sock; |
51d7db73 DG |
163 | |
164 | /* | |
165 | * We don't need a mutex at this point since we only splice or write single | |
166 | * large chunk of data with a header appended at the begining. Moreover, | |
167 | * this socket is for now only used in a single thread. | |
168 | */ | |
00e2e675 DG |
169 | struct lttcomm_sock data_sock; |
170 | struct lttng_ht_node_ulong node; | |
3bd1e081 MD |
171 | }; |
172 | ||
173 | /* | |
174 | * UST consumer local data to the program. One or more instance per | |
175 | * process. | |
176 | */ | |
177 | struct lttng_consumer_local_data { | |
4078b776 MD |
178 | /* |
179 | * Function to call when data is available on a buffer. | |
180 | * Returns the number of bytes read, or negative error value. | |
181 | */ | |
182 | ssize_t (*on_buffer_ready)(struct lttng_consumer_stream *stream, | |
d41f73b7 | 183 | struct lttng_consumer_local_data *ctx); |
3bd1e081 MD |
184 | /* |
185 | * function to call when we receive a new channel, it receives a | |
186 | * newly allocated channel, depending on the return code of this | |
187 | * function, the new channel will be handled by the application | |
188 | * or the library. | |
189 | * | |
190 | * Returns: | |
191 | * > 0 (success, FD is kept by application) | |
192 | * == 0 (success, FD is left to library) | |
193 | * < 0 (error) | |
194 | */ | |
195 | int (*on_recv_channel)(struct lttng_consumer_channel *channel); | |
196 | /* | |
197 | * function to call when we receive a new stream, it receives a | |
198 | * newly allocated stream, depending on the return code of this | |
199 | * function, the new stream will be handled by the application | |
200 | * or the library. | |
201 | * | |
202 | * Returns: | |
203 | * > 0 (success, FD is kept by application) | |
204 | * == 0 (success, FD is left to library) | |
205 | * < 0 (error) | |
206 | */ | |
207 | int (*on_recv_stream)(struct lttng_consumer_stream *stream); | |
208 | /* | |
209 | * function to call when a stream is getting updated by the session | |
210 | * daemon, this function receives the sessiond key and the new | |
211 | * state, depending on the return code of this function the | |
212 | * update of state for the stream is handled by the application | |
213 | * or the library. | |
214 | * | |
215 | * Returns: | |
216 | * > 0 (success, FD is kept by application) | |
217 | * == 0 (success, FD is left to library) | |
218 | * < 0 (error) | |
219 | */ | |
220 | int (*on_update_stream)(int sessiond_key, uint32_t state); | |
221 | /* socket to communicate errors with sessiond */ | |
222 | int consumer_error_socket; | |
223 | /* socket to exchange commands with sessiond */ | |
224 | char *consumer_command_sock_path; | |
225 | /* communication with splice */ | |
226 | int consumer_thread_pipe[2]; | |
227 | /* pipe to wake the poll thread when necessary */ | |
228 | int consumer_poll_pipe[2]; | |
229 | /* to let the signal handler wake up the fd receiver thread */ | |
230 | int consumer_should_quit[2]; | |
231 | }; | |
232 | ||
233 | /* | |
234 | * Library-level data. One instance per process. | |
235 | */ | |
236 | struct lttng_consumer_global_data { | |
237 | /* | |
e4421fec DG |
238 | * At this time, this lock is used to ensure coherence between the count |
239 | * and number of element in the hash table. It's also a protection for | |
6065ceec DG |
240 | * concurrent read/write between threads. |
241 | * | |
242 | * XXX: We need to see if this lock is still needed with the lockless RCU | |
243 | * hash tables. | |
3bd1e081 MD |
244 | */ |
245 | pthread_mutex_t lock; | |
e4421fec | 246 | |
3bd1e081 | 247 | /* |
e4421fec | 248 | * Number of streams in the hash table. Protected by consumer_data.lock. |
3bd1e081 MD |
249 | */ |
250 | int stream_count; | |
251 | /* | |
e4421fec | 252 | * Hash tables of streams and channels. Protected by consumer_data.lock. |
3bd1e081 | 253 | */ |
e4421fec DG |
254 | struct lttng_ht *stream_ht; |
255 | struct lttng_ht *channel_ht; | |
3bd1e081 MD |
256 | /* |
257 | * Flag specifying if the local array of FDs needs update in the | |
258 | * poll function. Protected by consumer_data.lock. | |
259 | */ | |
260 | unsigned int need_update; | |
261 | enum lttng_consumer_type type; | |
00e2e675 DG |
262 | |
263 | /* | |
264 | * Relayd socket(s) hashtable indexed by network sequence number. Each | |
265 | * stream has an index which associate the right relayd socket to use. | |
266 | */ | |
267 | struct lttng_ht *relayd_ht; | |
3bd1e081 MD |
268 | }; |
269 | ||
e4421fec DG |
270 | /* |
271 | * Init consumer data structures. | |
272 | */ | |
273 | extern void lttng_consumer_init(void); | |
274 | ||
3bd1e081 MD |
275 | /* |
276 | * Set the error socket for communication with a session daemon. | |
277 | */ | |
278 | extern void lttng_consumer_set_error_sock( | |
279 | struct lttng_consumer_local_data *ctx, int sock); | |
280 | ||
281 | /* | |
282 | * Set the command socket path for communication with a session daemon. | |
283 | */ | |
284 | extern void lttng_consumer_set_command_sock_path( | |
285 | struct lttng_consumer_local_data *ctx, char *sock); | |
286 | ||
287 | /* | |
288 | * Send return code to session daemon. | |
289 | * | |
290 | * Returns the return code of sendmsg : the number of bytes transmitted or -1 | |
291 | * on error. | |
292 | */ | |
293 | extern int lttng_consumer_send_error( | |
294 | struct lttng_consumer_local_data *ctx, int cmd); | |
295 | ||
296 | /* | |
297 | * Called from signal handler to ensure a clean exit. | |
298 | */ | |
299 | extern void lttng_consumer_should_exit( | |
300 | struct lttng_consumer_local_data *ctx); | |
301 | ||
302 | /* | |
303 | * Cleanup the daemon's socket on exit. | |
304 | */ | |
305 | extern void lttng_consumer_cleanup(void); | |
306 | ||
307 | /* | |
308 | * Flush pending writes to trace output disk file. | |
309 | */ | |
310 | extern void lttng_consumer_sync_trace_file( | |
311 | struct lttng_consumer_stream *stream, off_t orig_offset); | |
312 | ||
313 | /* | |
314 | * Poll on the should_quit pipe and the command socket return -1 on error and | |
315 | * should exit, 0 if data is available on the command socket | |
316 | */ | |
317 | extern int lttng_consumer_poll_socket(struct pollfd *kconsumer_sockpoll); | |
318 | ||
319 | extern int consumer_update_poll_array( | |
320 | struct lttng_consumer_local_data *ctx, struct pollfd **pollfd, | |
00e2e675 DG |
321 | struct lttng_consumer_stream **local_consumer_streams, |
322 | struct lttng_ht *metadata_ht); | |
3bd1e081 MD |
323 | |
324 | extern struct lttng_consumer_stream *consumer_allocate_stream( | |
325 | int channel_key, int stream_key, | |
326 | int shm_fd, int wait_fd, | |
327 | enum lttng_consumer_stream_state state, | |
328 | uint64_t mmap_len, | |
329 | enum lttng_event_output output, | |
6df2e2c9 MD |
330 | const char *path_name, |
331 | uid_t uid, | |
00e2e675 DG |
332 | gid_t gid, |
333 | int net_index, | |
334 | int metadata_flag); | |
3bd1e081 MD |
335 | extern int consumer_add_stream(struct lttng_consumer_stream *stream); |
336 | extern void consumer_del_stream(struct lttng_consumer_stream *stream); | |
337 | extern void consumer_change_stream_state(int stream_key, | |
338 | enum lttng_consumer_stream_state state); | |
339 | extern void consumer_del_channel(struct lttng_consumer_channel *channel); | |
340 | extern struct lttng_consumer_channel *consumer_allocate_channel( | |
341 | int channel_key, | |
342 | int shm_fd, int wait_fd, | |
343 | uint64_t mmap_len, | |
344 | uint64_t max_sb_size); | |
345 | int consumer_add_channel(struct lttng_consumer_channel *channel); | |
346 | ||
00e2e675 DG |
347 | /* lttng-relayd consumer command */ |
348 | int consumer_add_relayd(struct consumer_relayd_sock_pair *relayd); | |
349 | struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair( | |
350 | int net_seq_idx); | |
351 | struct consumer_relayd_sock_pair *consumer_find_relayd(int key); | |
352 | int consumer_handle_stream_before_relayd(struct lttng_consumer_stream *stream, | |
353 | size_t data_size); | |
173af62f | 354 | void consumer_destroy_relayd(struct consumer_relayd_sock_pair *relayd); |
00e2e675 | 355 | |
3bd1e081 MD |
356 | extern struct lttng_consumer_local_data *lttng_consumer_create( |
357 | enum lttng_consumer_type type, | |
4078b776 | 358 | ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream, |
d41f73b7 | 359 | struct lttng_consumer_local_data *ctx), |
3bd1e081 MD |
360 | int (*recv_channel)(struct lttng_consumer_channel *channel), |
361 | int (*recv_stream)(struct lttng_consumer_stream *stream), | |
362 | int (*update_stream)(int sessiond_key, uint32_t state)); | |
363 | extern void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx); | |
4078b776 | 364 | extern ssize_t lttng_consumer_on_read_subbuffer_mmap( |
3bd1e081 MD |
365 | struct lttng_consumer_local_data *ctx, |
366 | struct lttng_consumer_stream *stream, unsigned long len); | |
4078b776 | 367 | extern ssize_t lttng_consumer_on_read_subbuffer_splice( |
3bd1e081 MD |
368 | struct lttng_consumer_local_data *ctx, |
369 | struct lttng_consumer_stream *stream, unsigned long len); | |
370 | extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx, | |
371 | struct lttng_consumer_stream *stream); | |
372 | extern int lttng_consumer_get_produced_snapshot( | |
373 | struct lttng_consumer_local_data *ctx, | |
374 | struct lttng_consumer_stream *stream, | |
375 | unsigned long *pos); | |
376 | extern void *lttng_consumer_thread_poll_fds(void *data); | |
377 | extern void *lttng_consumer_thread_receive_fds(void *data); | |
378 | extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx, | |
379 | int sock, struct pollfd *consumer_sockpoll); | |
380 | ||
4078b776 | 381 | ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream, |
d41f73b7 MD |
382 | struct lttng_consumer_local_data *ctx); |
383 | int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream); | |
384 | ||
f02e1e8a | 385 | #endif /* LIB_CONSUMER_H */ |