2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2012 - David Goulet <dgoulet@efficios.com>
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.
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
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.
20 #ifndef _LTTNG_CONSUMER_H
21 #define _LTTNG_CONSUMER_H
27 #include <lttng/lttng.h>
29 #include <common/hashtable/hashtable.h>
30 #include <common/compat/fcntl.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
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
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
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.
44 * The value of 2 seconds is an arbitrary choice.
46 #define LTTNG_CONSUMER_POLL_TIMEOUT 2000
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 */
56 LTTNG_CONSUMER_ADD_RELAYD_SOCKET
,
59 /* State of each fd in consumer */
60 enum lttng_consumer_stream_state
{
61 LTTNG_CONSUMER_ACTIVE_STREAM
,
62 LTTNG_CONSUMER_PAUSE_STREAM
,
63 LTTNG_CONSUMER_DELETE_STREAM
,
66 enum lttng_consumer_type
{
67 LTTNG_CONSUMER_UNKNOWN
= 0,
68 LTTNG_CONSUMER_KERNEL
,
73 struct lttng_consumer_channel
{
74 struct lttng_ht_node_ulong node
;
76 uint64_t max_sb_size
; /* the subbuffer size for this channel */
77 int refcount
; /* Number of streams referencing this channel */
83 struct lttng_ust_shm_handle
*handle
;
89 /* Forward declaration for UST. */
90 struct lttng_ust_lib_ring_buffer
;
93 * Internal representation of the streams, sessiond_key is used to identify
96 struct lttng_consumer_stream
{
97 struct lttng_ht_node_ulong node
;
98 struct lttng_ht_node_ulong waitfd_node
;
99 struct lttng_consumer_channel
*chan
; /* associated channel */
101 * key is the key used by the session daemon to refer to the
102 * object in the consumer daemon.
107 int out_fd
; /* output file to write the data */
108 off_t out_fd_offset
; /* write position in the output file descriptor */
109 char path_name
[PATH_MAX
]; /* tracefile name */
110 enum lttng_consumer_stream_state state
;
114 enum lttng_event_output output
; /* splice or mmap */
118 struct lttng_ust_lib_ring_buffer
*buf
;
121 int hangup_flush_done
;
122 /* UID/GID of the user owning the session to which stream belongs */
125 /* Network sequence number. Indicating on which relayd socket it goes. */
127 /* Identify if the stream is the metadata */
128 unsigned int metadata_flag
;
129 /* Used when the stream is set for network streaming */
130 uint64_t relayd_stream_id
;
134 * Internal representation of a relayd socket pair.
136 struct consumer_relayd_sock_pair
{
137 /* Network sequence number. */
139 /* Number of stream associated with this relayd */
140 unsigned int refcount
;
141 pthread_mutex_t ctrl_sock_mutex
;
142 /* Sockets information */
143 struct lttcomm_sock control_sock
;
144 struct lttcomm_sock data_sock
;
145 struct lttng_ht_node_ulong node
;
149 * UST consumer local data to the program. One or more instance per
152 struct lttng_consumer_local_data
{
154 * Function to call when data is available on a buffer.
155 * Returns the number of bytes read, or negative error value.
157 ssize_t (*on_buffer_ready
)(struct lttng_consumer_stream
*stream
,
158 struct lttng_consumer_local_data
*ctx
);
160 * function to call when we receive a new channel, it receives a
161 * newly allocated channel, depending on the return code of this
162 * function, the new channel will be handled by the application
166 * > 0 (success, FD is kept by application)
167 * == 0 (success, FD is left to library)
170 int (*on_recv_channel
)(struct lttng_consumer_channel
*channel
);
172 * function to call when we receive a new stream, it receives a
173 * newly allocated stream, depending on the return code of this
174 * function, the new stream will be handled by the application
178 * > 0 (success, FD is kept by application)
179 * == 0 (success, FD is left to library)
182 int (*on_recv_stream
)(struct lttng_consumer_stream
*stream
);
184 * function to call when a stream is getting updated by the session
185 * daemon, this function receives the sessiond key and the new
186 * state, depending on the return code of this function the
187 * update of state for the stream is handled by the application
191 * > 0 (success, FD is kept by application)
192 * == 0 (success, FD is left to library)
195 int (*on_update_stream
)(int sessiond_key
, uint32_t state
);
196 /* socket to communicate errors with sessiond */
197 int consumer_error_socket
;
198 /* socket to exchange commands with sessiond */
199 char *consumer_command_sock_path
;
200 /* communication with splice */
201 int consumer_thread_pipe
[2];
202 /* pipe to wake the poll thread when necessary */
203 int consumer_poll_pipe
[2];
204 /* to let the signal handler wake up the fd receiver thread */
205 int consumer_should_quit
[2];
209 * Library-level data. One instance per process.
211 struct lttng_consumer_global_data
{
213 * At this time, this lock is used to ensure coherence between the count
214 * and number of element in the hash table. It's also a protection for
215 * concurrent read/write between threads.
217 * XXX: We need to see if this lock is still needed with the lockless RCU
220 pthread_mutex_t lock
;
223 * Number of streams in the hash table. Protected by consumer_data.lock.
227 * Hash tables of streams and channels. Protected by consumer_data.lock.
229 struct lttng_ht
*stream_ht
;
230 struct lttng_ht
*channel_ht
;
232 * Flag specifying if the local array of FDs needs update in the
233 * poll function. Protected by consumer_data.lock.
235 unsigned int need_update
;
236 enum lttng_consumer_type type
;
239 * Relayd socket(s) hashtable indexed by network sequence number. Each
240 * stream has an index which associate the right relayd socket to use.
242 struct lttng_ht
*relayd_ht
;
246 * Init consumer data structures.
248 extern void lttng_consumer_init(void);
251 * Set the error socket for communication with a session daemon.
253 extern void lttng_consumer_set_error_sock(
254 struct lttng_consumer_local_data
*ctx
, int sock
);
257 * Set the command socket path for communication with a session daemon.
259 extern void lttng_consumer_set_command_sock_path(
260 struct lttng_consumer_local_data
*ctx
, char *sock
);
263 * Send return code to session daemon.
265 * Returns the return code of sendmsg : the number of bytes transmitted or -1
268 extern int lttng_consumer_send_error(
269 struct lttng_consumer_local_data
*ctx
, int cmd
);
272 * Called from signal handler to ensure a clean exit.
274 extern void lttng_consumer_should_exit(
275 struct lttng_consumer_local_data
*ctx
);
278 * Cleanup the daemon's socket on exit.
280 extern void lttng_consumer_cleanup(void);
283 * Flush pending writes to trace output disk file.
285 extern void lttng_consumer_sync_trace_file(
286 struct lttng_consumer_stream
*stream
, off_t orig_offset
);
289 * Poll on the should_quit pipe and the command socket return -1 on error and
290 * should exit, 0 if data is available on the command socket
292 extern int lttng_consumer_poll_socket(struct pollfd
*kconsumer_sockpoll
);
294 extern int consumer_update_poll_array(
295 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
296 struct lttng_consumer_stream
**local_consumer_streams
,
297 struct lttng_ht
*metadata_ht
);
299 extern struct lttng_consumer_stream
*consumer_allocate_stream(
300 int channel_key
, int stream_key
,
301 int shm_fd
, int wait_fd
,
302 enum lttng_consumer_stream_state state
,
304 enum lttng_event_output output
,
305 const char *path_name
,
310 extern int consumer_add_stream(struct lttng_consumer_stream
*stream
);
311 extern void consumer_del_stream(struct lttng_consumer_stream
*stream
);
312 extern void consumer_change_stream_state(int stream_key
,
313 enum lttng_consumer_stream_state state
);
314 extern void consumer_del_channel(struct lttng_consumer_channel
*channel
);
315 extern struct lttng_consumer_channel
*consumer_allocate_channel(
317 int shm_fd
, int wait_fd
,
319 uint64_t max_sb_size
);
320 int consumer_add_channel(struct lttng_consumer_channel
*channel
);
322 /* lttng-relayd consumer command */
323 int consumer_add_relayd(struct consumer_relayd_sock_pair
*relayd
);
324 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
326 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
);
327 int consumer_handle_stream_before_relayd(struct lttng_consumer_stream
*stream
,
330 extern struct lttng_consumer_local_data
*lttng_consumer_create(
331 enum lttng_consumer_type type
,
332 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
333 struct lttng_consumer_local_data
*ctx
),
334 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
335 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
336 int (*update_stream
)(int sessiond_key
, uint32_t state
));
337 extern void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
);
338 extern ssize_t
lttng_consumer_on_read_subbuffer_mmap(
339 struct lttng_consumer_local_data
*ctx
,
340 struct lttng_consumer_stream
*stream
, unsigned long len
);
341 extern ssize_t
lttng_consumer_on_read_subbuffer_splice(
342 struct lttng_consumer_local_data
*ctx
,
343 struct lttng_consumer_stream
*stream
, unsigned long len
);
344 extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
345 struct lttng_consumer_stream
*stream
);
346 extern int lttng_consumer_get_produced_snapshot(
347 struct lttng_consumer_local_data
*ctx
,
348 struct lttng_consumer_stream
*stream
,
350 extern void *lttng_consumer_thread_poll_fds(void *data
);
351 extern void *lttng_consumer_thread_receive_fds(void *data
);
352 extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
353 int sock
, struct pollfd
*consumer_sockpoll
);
355 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
356 struct lttng_consumer_local_data
*ctx
);
357 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
);
359 #endif /* _LTTNG_CONSUMER_H */