2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
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.
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.
20 #ifndef _LTTNG_CONSUMER_H
21 #define _LTTNG_CONSUMER_H
27 #include <lttng/lttng.h>
29 #include "src/common/hashtable/hashtable.h"
32 * When the receiving thread dies, we need to have a way to make the polling
33 * thread exit eventually. If all FDs hang up (normal case when the
34 * lttng-sessiond stops), we can exit cleanly, but if there is a problem and
35 * for whatever reason some FDs remain open, the consumer should still exit
38 * If the timeout is reached, it means that during this period no events
39 * occurred on the FDs so we need to force an exit. This case should not happen
40 * but it is a safety to ensure we won't block the consumer indefinitely.
42 * The value of 2 seconds is an arbitrary choice.
44 #define LTTNG_CONSUMER_POLL_TIMEOUT 2000
46 /* Commands for consumer */
47 enum lttng_consumer_command
{
48 LTTNG_CONSUMER_ADD_CHANNEL
,
49 LTTNG_CONSUMER_ADD_STREAM
,
50 /* pause, delete, active depending on fd state */
51 LTTNG_CONSUMER_UPDATE_STREAM
,
52 /* inform the consumer to quit when all fd has hang up */
56 /* State of each fd in consumer */
57 enum lttng_consumer_stream_state
{
58 LTTNG_CONSUMER_ACTIVE_STREAM
,
59 LTTNG_CONSUMER_PAUSE_STREAM
,
60 LTTNG_CONSUMER_DELETE_STREAM
,
63 enum lttng_consumer_type
{
64 LTTNG_CONSUMER_UNKNOWN
= 0,
65 LTTNG_CONSUMER_KERNEL
,
70 struct lttng_consumer_channel
{
71 struct lttng_ht_node_ulong node
;
73 uint64_t max_sb_size
; /* the subbuffer size for this channel */
74 int refcount
; /* Number of streams referencing this channel */
80 struct lttng_ust_shm_handle
*handle
;
86 /* Forward declaration for UST. */
87 struct lttng_ust_lib_ring_buffer
;
90 * Internal representation of the streams, sessiond_key is used to identify
93 struct lttng_consumer_stream
{
94 struct lttng_ht_node_ulong node
;
95 struct lttng_consumer_channel
*chan
; /* associated channel */
97 * key is the key used by the session daemon to refer to the
98 * object in the consumer daemon.
103 int out_fd
; /* output file to write the data */
104 off_t out_fd_offset
; /* write position in the output file descriptor */
105 char path_name
[PATH_MAX
]; /* tracefile name */
106 enum lttng_consumer_stream_state state
;
110 enum lttng_event_output output
; /* splice or mmap */
114 struct lttng_ust_lib_ring_buffer
*buf
;
116 int hangup_flush_done
;
117 /* UID/GID of the user owning the session to which stream belongs */
123 * UST consumer local data to the program. One or more instance per
126 struct lttng_consumer_local_data
{
127 /* function to call when data is available on a buffer */
128 int (*on_buffer_ready
)(struct lttng_consumer_stream
*stream
,
129 struct lttng_consumer_local_data
*ctx
);
131 * function to call when we receive a new channel, it receives a
132 * newly allocated channel, depending on the return code of this
133 * function, the new channel will be handled by the application
137 * > 0 (success, FD is kept by application)
138 * == 0 (success, FD is left to library)
141 int (*on_recv_channel
)(struct lttng_consumer_channel
*channel
);
143 * function to call when we receive a new stream, it receives a
144 * newly allocated stream, depending on the return code of this
145 * function, the new stream will be handled by the application
149 * > 0 (success, FD is kept by application)
150 * == 0 (success, FD is left to library)
153 int (*on_recv_stream
)(struct lttng_consumer_stream
*stream
);
155 * function to call when a stream is getting updated by the session
156 * daemon, this function receives the sessiond key and the new
157 * state, depending on the return code of this function the
158 * update of state for the stream is handled by the application
162 * > 0 (success, FD is kept by application)
163 * == 0 (success, FD is left to library)
166 int (*on_update_stream
)(int sessiond_key
, uint32_t state
);
167 /* socket to communicate errors with sessiond */
168 int consumer_error_socket
;
169 /* socket to exchange commands with sessiond */
170 char *consumer_command_sock_path
;
171 /* communication with splice */
172 int consumer_thread_pipe
[2];
173 /* pipe to wake the poll thread when necessary */
174 int consumer_poll_pipe
[2];
175 /* to let the signal handler wake up the fd receiver thread */
176 int consumer_should_quit
[2];
180 * Library-level data. One instance per process.
182 struct lttng_consumer_global_data
{
185 * At this time, this lock is used to ensure coherence between the count
186 * and number of element in the hash table. It's also a protection for
187 * concurrent read/write between threads.
189 * XXX: We need to see if this lock is still needed with the lockless RCU
192 pthread_mutex_t lock
;
195 * Number of streams in the hash table. Protected by consumer_data.lock.
199 * Hash tables of streams and channels. Protected by consumer_data.lock.
201 struct lttng_ht
*stream_ht
;
202 struct lttng_ht
*channel_ht
;
204 * Flag specifying if the local array of FDs needs update in the
205 * poll function. Protected by consumer_data.lock.
207 unsigned int need_update
;
208 enum lttng_consumer_type type
;
212 * Init consumer data structures.
214 extern void lttng_consumer_init(void);
217 * Set the error socket for communication with a session daemon.
219 extern void lttng_consumer_set_error_sock(
220 struct lttng_consumer_local_data
*ctx
, int sock
);
223 * Set the command socket path for communication with a session daemon.
225 extern void lttng_consumer_set_command_sock_path(
226 struct lttng_consumer_local_data
*ctx
, char *sock
);
229 * Send return code to session daemon.
231 * Returns the return code of sendmsg : the number of bytes transmitted or -1
234 extern int lttng_consumer_send_error(
235 struct lttng_consumer_local_data
*ctx
, int cmd
);
238 * Called from signal handler to ensure a clean exit.
240 extern void lttng_consumer_should_exit(
241 struct lttng_consumer_local_data
*ctx
);
244 * Cleanup the daemon's socket on exit.
246 extern void lttng_consumer_cleanup(void);
249 * Flush pending writes to trace output disk file.
251 extern void lttng_consumer_sync_trace_file(
252 struct lttng_consumer_stream
*stream
, off_t orig_offset
);
255 * Poll on the should_quit pipe and the command socket return -1 on error and
256 * should exit, 0 if data is available on the command socket
258 extern int lttng_consumer_poll_socket(struct pollfd
*kconsumer_sockpoll
);
260 extern int consumer_update_poll_array(
261 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
262 struct lttng_consumer_stream
**local_consumer_streams
);
264 extern struct lttng_consumer_stream
*consumer_allocate_stream(
265 int channel_key
, int stream_key
,
266 int shm_fd
, int wait_fd
,
267 enum lttng_consumer_stream_state state
,
269 enum lttng_event_output output
,
270 const char *path_name
,
273 extern int consumer_add_stream(struct lttng_consumer_stream
*stream
);
274 extern void consumer_del_stream(struct lttng_consumer_stream
*stream
);
275 extern void consumer_change_stream_state(int stream_key
,
276 enum lttng_consumer_stream_state state
);
277 extern void consumer_del_channel(struct lttng_consumer_channel
*channel
);
278 extern struct lttng_consumer_channel
*consumer_allocate_channel(
280 int shm_fd
, int wait_fd
,
282 uint64_t max_sb_size
);
283 int consumer_add_channel(struct lttng_consumer_channel
*channel
);
285 extern struct lttng_consumer_local_data
*lttng_consumer_create(
286 enum lttng_consumer_type type
,
287 int (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
288 struct lttng_consumer_local_data
*ctx
),
289 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
290 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
291 int (*update_stream
)(int sessiond_key
, uint32_t state
));
292 extern void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
);
293 extern int lttng_consumer_on_read_subbuffer_mmap(
294 struct lttng_consumer_local_data
*ctx
,
295 struct lttng_consumer_stream
*stream
, unsigned long len
);
296 extern int lttng_consumer_on_read_subbuffer_splice(
297 struct lttng_consumer_local_data
*ctx
,
298 struct lttng_consumer_stream
*stream
, unsigned long len
);
299 extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
300 struct lttng_consumer_stream
*stream
);
301 extern int lttng_consumer_get_produced_snapshot(
302 struct lttng_consumer_local_data
*ctx
,
303 struct lttng_consumer_stream
*stream
,
305 extern void *lttng_consumer_thread_poll_fds(void *data
);
306 extern void *lttng_consumer_thread_receive_fds(void *data
);
307 extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
308 int sock
, struct pollfd
*consumer_sockpoll
);
310 int lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
311 struct lttng_consumer_local_data
*ctx
);
312 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
);
314 #endif /* _LTTNG_CONSUMER_H */