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 | 32 | |
3bd1e081 MD |
33 | /* Commands for consumer */ |
34 | enum lttng_consumer_command { | |
35 | LTTNG_CONSUMER_ADD_CHANNEL, | |
36 | LTTNG_CONSUMER_ADD_STREAM, | |
37 | /* pause, delete, active depending on fd state */ | |
38 | LTTNG_CONSUMER_UPDATE_STREAM, | |
39 | /* inform the consumer to quit when all fd has hang up */ | |
40 | LTTNG_CONSUMER_STOP, | |
00e2e675 | 41 | LTTNG_CONSUMER_ADD_RELAYD_SOCKET, |
173af62f DG |
42 | /* Inform the consumer to kill a specific relayd connection */ |
43 | LTTNG_CONSUMER_DESTROY_RELAYD, | |
53632229 | 44 | /* Return to the sessiond if there is data pending for a session */ |
6d805429 | 45 | LTTNG_CONSUMER_DATA_PENDING, |
3bd1e081 MD |
46 | }; |
47 | ||
48 | /* State of each fd in consumer */ | |
49 | enum lttng_consumer_stream_state { | |
50 | LTTNG_CONSUMER_ACTIVE_STREAM, | |
51 | LTTNG_CONSUMER_PAUSE_STREAM, | |
52 | LTTNG_CONSUMER_DELETE_STREAM, | |
53 | }; | |
54 | ||
3bd1e081 MD |
55 | enum lttng_consumer_type { |
56 | LTTNG_CONSUMER_UNKNOWN = 0, | |
57 | LTTNG_CONSUMER_KERNEL, | |
7753dea8 MD |
58 | LTTNG_CONSUMER64_UST, |
59 | LTTNG_CONSUMER32_UST, | |
3bd1e081 MD |
60 | }; |
61 | ||
8994307f DG |
62 | enum consumer_endpoint_status { |
63 | CONSUMER_ENDPOINT_ACTIVE, | |
64 | CONSUMER_ENDPOINT_INACTIVE, | |
65 | }; | |
66 | ||
3bd1e081 | 67 | struct lttng_consumer_channel { |
e4421fec | 68 | struct lttng_ht_node_ulong node; |
3bd1e081 MD |
69 | int key; |
70 | uint64_t max_sb_size; /* the subbuffer size for this channel */ | |
71 | int refcount; /* Number of streams referencing this channel */ | |
c30aaa51 MD |
72 | /* |
73 | * The number of streams to receive initially. Used to guarantee that we do | |
74 | * not destroy a channel before receiving all its associated streams. | |
75 | */ | |
76 | unsigned int nb_init_streams; | |
77 | ||
3bd1e081 MD |
78 | /* For UST */ |
79 | int shm_fd; | |
80 | int wait_fd; | |
81 | void *mmap_base; | |
82 | size_t mmap_len; | |
13161846 | 83 | struct lttng_ust_shm_handle *handle; |
b5c5fc29 | 84 | int wait_fd_is_copy; |
5af2f756 | 85 | int cpucount; |
3bd1e081 MD |
86 | }; |
87 | ||
88 | /* Forward declaration for UST. */ | |
13161846 | 89 | struct lttng_ust_lib_ring_buffer; |
3bd1e081 MD |
90 | |
91 | /* | |
92 | * Internal representation of the streams, sessiond_key is used to identify | |
93 | * uniquely a stream. | |
94 | */ | |
95 | struct lttng_consumer_stream { | |
53632229 | 96 | /* HT node used by the data_ht and metadata_ht */ |
e4421fec | 97 | struct lttng_ht_node_ulong node; |
53632229 DG |
98 | /* HT node used in consumer_data.stream_list_ht */ |
99 | struct lttng_ht_node_ulong node_session_id; | |
3bd1e081 MD |
100 | struct lttng_consumer_channel *chan; /* associated channel */ |
101 | /* | |
102 | * key is the key used by the session daemon to refer to the | |
103 | * object in the consumer daemon. | |
104 | */ | |
105 | int key; | |
106 | int shm_fd; | |
107 | int wait_fd; | |
108 | int out_fd; /* output file to write the data */ | |
109 | off_t out_fd_offset; /* write position in the output file descriptor */ | |
110 | char path_name[PATH_MAX]; /* tracefile name */ | |
111 | enum lttng_consumer_stream_state state; | |
112 | size_t shm_len; | |
113 | void *mmap_base; | |
114 | size_t mmap_len; | |
115 | enum lttng_event_output output; /* splice or mmap */ | |
b5c5fc29 MD |
116 | int shm_fd_is_copy; |
117 | int wait_fd_is_copy; | |
3bd1e081 | 118 | /* For UST */ |
13161846 | 119 | struct lttng_ust_lib_ring_buffer *buf; |
3bd1e081 | 120 | int cpu; |
4078b776 | 121 | int data_read; |
d056b477 | 122 | int hangup_flush_done; |
6df2e2c9 MD |
123 | /* UID/GID of the user owning the session to which stream belongs */ |
124 | uid_t uid; | |
125 | gid_t gid; | |
00e2e675 DG |
126 | /* Network sequence number. Indicating on which relayd socket it goes. */ |
127 | int net_seq_idx; | |
128 | /* Identify if the stream is the metadata */ | |
129 | unsigned int metadata_flag; | |
130 | /* Used when the stream is set for network streaming */ | |
131 | uint64_t relayd_stream_id; | |
173af62f DG |
132 | /* Next sequence number to use for trace packet */ |
133 | uint64_t next_net_seq_num; | |
63281d5d | 134 | /* |
74251bb8 DG |
135 | * Lock to use the stream FDs since they are used between threads. |
136 | * | |
137 | * This is nested INSIDE the consumer_data lock. | |
138 | * This is nested OUTSIDE consumer_relayd_sock_pair lock. | |
63281d5d | 139 | */ |
53632229 DG |
140 | pthread_mutex_t lock; |
141 | /* Tracing session id */ | |
142 | uint64_t session_id; | |
8994307f DG |
143 | /* |
144 | * Indicates if the stream end point is still active or not (network | |
145 | * streaming or local file system). The thread "owning" the stream is | |
146 | * handling this status and can be notified of a state change through the | |
147 | * consumer data appropriate pipe. | |
148 | */ | |
149 | enum consumer_endpoint_status endpoint_status; | |
00e2e675 DG |
150 | }; |
151 | ||
152 | /* | |
153 | * Internal representation of a relayd socket pair. | |
154 | */ | |
155 | struct consumer_relayd_sock_pair { | |
156 | /* Network sequence number. */ | |
157 | int net_seq_idx; | |
158 | /* Number of stream associated with this relayd */ | |
159 | unsigned int refcount; | |
173af62f DG |
160 | |
161 | /* | |
162 | * This flag indicates whether or not we should destroy this object. The | |
163 | * destruction should ONLY occurs when this flag is set and the refcount is | |
164 | * set to zero. | |
165 | */ | |
166 | unsigned int destroy_flag; | |
167 | ||
51d7db73 DG |
168 | /* |
169 | * Mutex protecting the control socket to avoid out of order packets | |
170 | * between threads sending data to the relayd. Since metadata data is sent | |
171 | * over that socket, at least two sendmsg() are needed (header + data) | |
172 | * creating a race for packets to overlap between threads using it. | |
74251bb8 DG |
173 | * |
174 | * This is nested INSIDE the consumer_data lock. | |
175 | * This is nested INSIDE the stream lock. | |
51d7db73 | 176 | */ |
00e2e675 | 177 | pthread_mutex_t ctrl_sock_mutex; |
51d7db73 DG |
178 | |
179 | /* Control socket. Command and metadata are passed over it */ | |
00e2e675 | 180 | struct lttcomm_sock control_sock; |
51d7db73 DG |
181 | |
182 | /* | |
183 | * We don't need a mutex at this point since we only splice or write single | |
184 | * large chunk of data with a header appended at the begining. Moreover, | |
185 | * this socket is for now only used in a single thread. | |
186 | */ | |
00e2e675 DG |
187 | struct lttcomm_sock data_sock; |
188 | struct lttng_ht_node_ulong node; | |
c5b6f4f0 DG |
189 | |
190 | /* Session id on the relayd side for the sockets. */ | |
191 | uint64_t session_id; | |
3bd1e081 MD |
192 | }; |
193 | ||
194 | /* | |
195 | * UST consumer local data to the program. One or more instance per | |
196 | * process. | |
197 | */ | |
198 | struct lttng_consumer_local_data { | |
4078b776 MD |
199 | /* |
200 | * Function to call when data is available on a buffer. | |
201 | * Returns the number of bytes read, or negative error value. | |
202 | */ | |
203 | ssize_t (*on_buffer_ready)(struct lttng_consumer_stream *stream, | |
d41f73b7 | 204 | struct lttng_consumer_local_data *ctx); |
3bd1e081 MD |
205 | /* |
206 | * function to call when we receive a new channel, it receives a | |
207 | * newly allocated channel, depending on the return code of this | |
208 | * function, the new channel will be handled by the application | |
209 | * or the library. | |
210 | * | |
211 | * Returns: | |
212 | * > 0 (success, FD is kept by application) | |
213 | * == 0 (success, FD is left to library) | |
214 | * < 0 (error) | |
215 | */ | |
216 | int (*on_recv_channel)(struct lttng_consumer_channel *channel); | |
217 | /* | |
218 | * function to call when we receive a new stream, it receives a | |
219 | * newly allocated stream, depending on the return code of this | |
220 | * function, the new stream will be handled by the application | |
221 | * or the library. | |
222 | * | |
223 | * Returns: | |
224 | * > 0 (success, FD is kept by application) | |
225 | * == 0 (success, FD is left to library) | |
226 | * < 0 (error) | |
227 | */ | |
228 | int (*on_recv_stream)(struct lttng_consumer_stream *stream); | |
229 | /* | |
230 | * function to call when a stream is getting updated by the session | |
231 | * daemon, this function receives the sessiond key and the new | |
232 | * state, depending on the return code of this function the | |
233 | * update of state for the stream is handled by the application | |
234 | * or the library. | |
235 | * | |
236 | * Returns: | |
237 | * > 0 (success, FD is kept by application) | |
238 | * == 0 (success, FD is left to library) | |
239 | * < 0 (error) | |
240 | */ | |
241 | int (*on_update_stream)(int sessiond_key, uint32_t state); | |
242 | /* socket to communicate errors with sessiond */ | |
243 | int consumer_error_socket; | |
244 | /* socket to exchange commands with sessiond */ | |
245 | char *consumer_command_sock_path; | |
246 | /* communication with splice */ | |
247 | int consumer_thread_pipe[2]; | |
fb3a43a9 | 248 | int consumer_splice_metadata_pipe[2]; |
50f8ae69 DG |
249 | /* Data stream poll thread pipe. To transfer data stream to the thread */ |
250 | int consumer_data_pipe[2]; | |
3bd1e081 MD |
251 | /* to let the signal handler wake up the fd receiver thread */ |
252 | int consumer_should_quit[2]; | |
fb3a43a9 DG |
253 | /* Metadata poll thread pipe. Transfer metadata stream to it */ |
254 | int consumer_metadata_pipe[2]; | |
3bd1e081 MD |
255 | }; |
256 | ||
257 | /* | |
258 | * Library-level data. One instance per process. | |
259 | */ | |
260 | struct lttng_consumer_global_data { | |
261 | /* | |
e4421fec DG |
262 | * At this time, this lock is used to ensure coherence between the count |
263 | * and number of element in the hash table. It's also a protection for | |
6065ceec DG |
264 | * concurrent read/write between threads. |
265 | * | |
74251bb8 DG |
266 | * This is nested OUTSIDE the stream lock. |
267 | * This is nested OUTSIDE the consumer_relayd_sock_pair lock. | |
3bd1e081 MD |
268 | */ |
269 | pthread_mutex_t lock; | |
e4421fec | 270 | |
3bd1e081 | 271 | /* |
3cc2f24a DG |
272 | * Number of streams in the data stream hash table declared outside. |
273 | * Protected by consumer_data.lock. | |
3bd1e081 MD |
274 | */ |
275 | int stream_count; | |
3cc2f24a DG |
276 | |
277 | /* Channel hash table protected by consumer_data.lock. */ | |
e4421fec | 278 | struct lttng_ht *channel_ht; |
3bd1e081 MD |
279 | /* |
280 | * Flag specifying if the local array of FDs needs update in the | |
281 | * poll function. Protected by consumer_data.lock. | |
282 | */ | |
283 | unsigned int need_update; | |
284 | enum lttng_consumer_type type; | |
00e2e675 DG |
285 | |
286 | /* | |
287 | * Relayd socket(s) hashtable indexed by network sequence number. Each | |
288 | * stream has an index which associate the right relayd socket to use. | |
289 | */ | |
290 | struct lttng_ht *relayd_ht; | |
53632229 DG |
291 | |
292 | /* | |
293 | * This hash table contains all streams (metadata and data) indexed by | |
294 | * session id. In other words, the ht is indexed by session id and each | |
295 | * bucket contains the list of associated streams. | |
296 | * | |
297 | * This HT uses the "node_session_id" of the consumer stream. | |
298 | */ | |
299 | struct lttng_ht *stream_list_ht; | |
3bd1e081 MD |
300 | }; |
301 | ||
46e6455f DG |
302 | /* |
303 | * Session id mapping structure storred in relayd_session_id_ht. | |
304 | */ | |
305 | struct consumer_relayd_session_id { | |
306 | uint64_t sessiond_id; | |
307 | uint64_t relayd_id; | |
308 | struct lttng_ht_node_ulong node; | |
309 | }; | |
310 | ||
e4421fec DG |
311 | /* |
312 | * Init consumer data structures. | |
313 | */ | |
314 | extern void lttng_consumer_init(void); | |
315 | ||
3bd1e081 MD |
316 | /* |
317 | * Set the error socket for communication with a session daemon. | |
318 | */ | |
319 | extern void lttng_consumer_set_error_sock( | |
320 | struct lttng_consumer_local_data *ctx, int sock); | |
321 | ||
322 | /* | |
323 | * Set the command socket path for communication with a session daemon. | |
324 | */ | |
325 | extern void lttng_consumer_set_command_sock_path( | |
326 | struct lttng_consumer_local_data *ctx, char *sock); | |
327 | ||
328 | /* | |
329 | * Send return code to session daemon. | |
330 | * | |
331 | * Returns the return code of sendmsg : the number of bytes transmitted or -1 | |
332 | * on error. | |
333 | */ | |
334 | extern int lttng_consumer_send_error( | |
335 | struct lttng_consumer_local_data *ctx, int cmd); | |
336 | ||
337 | /* | |
338 | * Called from signal handler to ensure a clean exit. | |
339 | */ | |
340 | extern void lttng_consumer_should_exit( | |
341 | struct lttng_consumer_local_data *ctx); | |
342 | ||
343 | /* | |
344 | * Cleanup the daemon's socket on exit. | |
345 | */ | |
346 | extern void lttng_consumer_cleanup(void); | |
347 | ||
348 | /* | |
349 | * Flush pending writes to trace output disk file. | |
350 | */ | |
351 | extern void lttng_consumer_sync_trace_file( | |
352 | struct lttng_consumer_stream *stream, off_t orig_offset); | |
353 | ||
354 | /* | |
355 | * Poll on the should_quit pipe and the command socket return -1 on error and | |
356 | * should exit, 0 if data is available on the command socket | |
357 | */ | |
358 | extern int lttng_consumer_poll_socket(struct pollfd *kconsumer_sockpoll); | |
359 | ||
3bd1e081 MD |
360 | extern struct lttng_consumer_stream *consumer_allocate_stream( |
361 | int channel_key, int stream_key, | |
362 | int shm_fd, int wait_fd, | |
363 | enum lttng_consumer_stream_state state, | |
364 | uint64_t mmap_len, | |
365 | enum lttng_event_output output, | |
6df2e2c9 MD |
366 | const char *path_name, |
367 | uid_t uid, | |
00e2e675 DG |
368 | gid_t gid, |
369 | int net_index, | |
c80048c6 | 370 | int metadata_flag, |
53632229 | 371 | uint64_t session_id, |
c80048c6 | 372 | int *alloc_ret); |
e316aad5 DG |
373 | extern void consumer_del_stream(struct lttng_consumer_stream *stream, |
374 | struct lttng_ht *ht); | |
375 | extern void consumer_del_metadata_stream(struct lttng_consumer_stream *stream, | |
376 | struct lttng_ht *ht); | |
3bd1e081 MD |
377 | extern void consumer_del_channel(struct lttng_consumer_channel *channel); |
378 | extern struct lttng_consumer_channel *consumer_allocate_channel( | |
379 | int channel_key, | |
380 | int shm_fd, int wait_fd, | |
381 | uint64_t mmap_len, | |
c30aaa51 MD |
382 | uint64_t max_sb_size, |
383 | unsigned int nb_init_streams); | |
3bd1e081 MD |
384 | int consumer_add_channel(struct lttng_consumer_channel *channel); |
385 | ||
00e2e675 | 386 | /* lttng-relayd consumer command */ |
00e2e675 DG |
387 | struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair( |
388 | int net_seq_idx); | |
389 | struct consumer_relayd_sock_pair *consumer_find_relayd(int key); | |
390 | int consumer_handle_stream_before_relayd(struct lttng_consumer_stream *stream, | |
391 | size_t data_size); | |
c869f647 | 392 | void consumer_steal_stream_key(int key, struct lttng_ht *ht); |
00e2e675 | 393 | |
3bd1e081 MD |
394 | extern struct lttng_consumer_local_data *lttng_consumer_create( |
395 | enum lttng_consumer_type type, | |
4078b776 | 396 | ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream, |
d41f73b7 | 397 | struct lttng_consumer_local_data *ctx), |
3bd1e081 MD |
398 | int (*recv_channel)(struct lttng_consumer_channel *channel), |
399 | int (*recv_stream)(struct lttng_consumer_stream *stream), | |
400 | int (*update_stream)(int sessiond_key, uint32_t state)); | |
401 | extern void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx); | |
4078b776 | 402 | extern ssize_t lttng_consumer_on_read_subbuffer_mmap( |
3bd1e081 | 403 | struct lttng_consumer_local_data *ctx, |
1d4dfdef DG |
404 | struct lttng_consumer_stream *stream, unsigned long len, |
405 | unsigned long padding); | |
4078b776 | 406 | extern ssize_t lttng_consumer_on_read_subbuffer_splice( |
3bd1e081 | 407 | struct lttng_consumer_local_data *ctx, |
1d4dfdef DG |
408 | struct lttng_consumer_stream *stream, unsigned long len, |
409 | unsigned long padding); | |
3bd1e081 MD |
410 | extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx, |
411 | struct lttng_consumer_stream *stream); | |
412 | extern int lttng_consumer_get_produced_snapshot( | |
413 | struct lttng_consumer_local_data *ctx, | |
414 | struct lttng_consumer_stream *stream, | |
415 | unsigned long *pos); | |
7d980def DG |
416 | extern void *consumer_thread_metadata_poll(void *data); |
417 | extern void *consumer_thread_data_poll(void *data); | |
418 | extern void *consumer_thread_sessiond_poll(void *data); | |
3bd1e081 MD |
419 | extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
420 | int sock, struct pollfd *consumer_sockpoll); | |
421 | ||
4078b776 | 422 | ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream, |
d41f73b7 MD |
423 | struct lttng_consumer_local_data *ctx); |
424 | int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream); | |
7735ef9e DG |
425 | int consumer_add_relayd_socket(int net_seq_idx, int sock_type, |
426 | struct lttng_consumer_local_data *ctx, int sock, | |
46e6455f DG |
427 | struct pollfd *consumer_sockpoll, struct lttcomm_sock *relayd_sock, |
428 | unsigned int sessiond_id); | |
a6ba4fe1 DG |
429 | void consumer_flag_relayd_for_destroy( |
430 | struct consumer_relayd_sock_pair *relayd); | |
6d805429 | 431 | int consumer_data_pending(uint64_t id); |
f50f23d9 | 432 | int consumer_send_status_msg(int sock, int ret_code); |
d41f73b7 | 433 | |
f02e1e8a | 434 | #endif /* LIB_CONSUMER_H */ |