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
26 #include <urcu/list.h>
27 #include <lttng/lttng.h>
30 * When the receiving thread dies, we need to have a way to make the polling
31 * thread exit eventually. If all FDs hang up (normal case when the
32 * lttng-sessiond stops), we can exit cleanly, but if there is a problem and
33 * for whatever reason some FDs remain open, the consumer should still exit
36 * If the timeout is reached, it means that during this period no events
37 * occurred on the FDs so we need to force an exit. This case should not happen
38 * but it is a safety to ensure we won't block the consumer indefinitely.
40 * The value of 2 seconds is an arbitrary choice.
42 #define LTTNG_CONSUMER_POLL_TIMEOUT 2000
44 /* Commands for consumer */
45 enum lttng_consumer_command
{
46 LTTNG_CONSUMER_ADD_CHANNEL
,
47 LTTNG_CONSUMER_ADD_STREAM
,
48 /* pause, delete, active depending on fd state */
49 LTTNG_CONSUMER_UPDATE_STREAM
,
50 /* inform the consumer to quit when all fd has hang up */
54 /* State of each fd in consumer */
55 enum lttng_consumer_stream_state
{
56 LTTNG_CONSUMER_ACTIVE_STREAM
,
57 LTTNG_CONSUMER_PAUSE_STREAM
,
58 LTTNG_CONSUMER_DELETE_STREAM
,
61 struct lttng_consumer_channel_list
{
62 struct cds_list_head head
;
65 struct lttng_consumer_stream_list
{
66 struct cds_list_head head
;
69 enum lttng_consumer_type
{
70 LTTNG_CONSUMER_UNKNOWN
= 0,
71 LTTNG_CONSUMER_KERNEL
,
76 struct lttng_consumer_channel
{
77 struct cds_list_head list
;
79 uint64_t max_sb_size
; /* the subbuffer size for this channel */
80 int refcount
; /* Number of streams referencing this channel */
86 struct lttng_ust_shm_handle
*handle
;
93 /* Forward declaration for UST. */
94 struct lttng_ust_lib_ring_buffer
;
97 * Internal representation of the streams, sessiond_key is used to identify
100 struct lttng_consumer_stream
{
101 struct cds_list_head list
;
102 struct lttng_consumer_channel
*chan
; /* associated channel */
104 * key is the key used by the session daemon to refer to the
105 * object in the consumer daemon.
110 int out_fd
; /* output file to write the data */
111 off_t out_fd_offset
; /* write position in the output file descriptor */
112 char path_name
[PATH_MAX
]; /* tracefile name */
113 enum lttng_consumer_stream_state state
;
117 enum lttng_event_output output
; /* splice or mmap */
121 struct lttng_ust_lib_ring_buffer
*buf
;
123 int hangup_flush_done
;
124 /* UID/GID of the user owning the session to which stream belongs */
130 * UST consumer local data to the program. One or more instance per
133 struct lttng_consumer_local_data
{
134 /* function to call when data is available on a buffer */
135 int (*on_buffer_ready
)(struct lttng_consumer_stream
*stream
,
136 struct lttng_consumer_local_data
*ctx
);
138 * function to call when we receive a new channel, it receives a
139 * newly allocated channel, depending on the return code of this
140 * function, the new channel will be handled by the application
144 * > 0 (success, FD is kept by application)
145 * == 0 (success, FD is left to library)
148 int (*on_recv_channel
)(struct lttng_consumer_channel
*channel
);
150 * function to call when we receive a new stream, it receives a
151 * newly allocated stream, depending on the return code of this
152 * function, the new stream will be handled by the application
156 * > 0 (success, FD is kept by application)
157 * == 0 (success, FD is left to library)
160 int (*on_recv_stream
)(struct lttng_consumer_stream
*stream
);
162 * function to call when a stream is getting updated by the session
163 * daemon, this function receives the sessiond key and the new
164 * state, depending on the return code of this function the
165 * update of state for the stream is handled by the application
169 * > 0 (success, FD is kept by application)
170 * == 0 (success, FD is left to library)
173 int (*on_update_stream
)(int sessiond_key
, uint32_t state
);
174 /* socket to communicate errors with sessiond */
175 int consumer_error_socket
;
176 /* socket to exchange commands with sessiond */
177 char *consumer_command_sock_path
;
178 /* communication with splice */
179 int consumer_thread_pipe
[2];
180 /* pipe to wake the poll thread when necessary */
181 int consumer_poll_pipe
[2];
182 /* to let the signal handler wake up the fd receiver thread */
183 int consumer_should_quit
[2];
187 * Library-level data. One instance per process.
189 struct lttng_consumer_global_data
{
191 * consumer_data.lock protects consumer_data.fd_list,
192 * consumer_data.stream_count, and consumer_data.need_update. It
193 * ensures the count matches the number of items in the fd_list.
194 * It ensures the list updates *always* trigger an fd_array
195 * update (therefore need to make list update vs
196 * consumer_data.need_update flag update atomic, and also flag
197 * read, fd array and flag clear atomic).
199 pthread_mutex_t lock
;
201 * Number of streams in the list below. Protected by
202 * consumer_data.lock.
206 * Lists of streams and channels. Protected by consumer_data.lock.
208 struct lttng_consumer_stream_list stream_list
;
209 struct lttng_consumer_channel_list channel_list
;
211 * Flag specifying if the local array of FDs needs update in the
212 * poll function. Protected by consumer_data.lock.
214 unsigned int need_update
;
215 enum lttng_consumer_type type
;
219 * Set the error socket for communication with a session daemon.
221 extern void lttng_consumer_set_error_sock(
222 struct lttng_consumer_local_data
*ctx
, int sock
);
225 * Set the command socket path for communication with a session daemon.
227 extern void lttng_consumer_set_command_sock_path(
228 struct lttng_consumer_local_data
*ctx
, char *sock
);
231 * Send return code to session daemon.
233 * Returns the return code of sendmsg : the number of bytes transmitted or -1
236 extern int lttng_consumer_send_error(
237 struct lttng_consumer_local_data
*ctx
, int cmd
);
240 * Called from signal handler to ensure a clean exit.
242 extern void lttng_consumer_should_exit(
243 struct lttng_consumer_local_data
*ctx
);
246 * Cleanup the daemon's socket on exit.
248 extern void lttng_consumer_cleanup(void);
251 * Flush pending writes to trace output disk file.
253 extern void lttng_consumer_sync_trace_file(
254 struct lttng_consumer_stream
*stream
, off_t orig_offset
);
257 * Poll on the should_quit pipe and the command socket return -1 on error and
258 * should exit, 0 if data is available on the command socket
260 extern int lttng_consumer_poll_socket(struct pollfd
*kconsumer_sockpoll
);
262 extern int consumer_update_poll_array(
263 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
264 struct lttng_consumer_stream
**local_consumer_streams
);
266 extern struct lttng_consumer_stream
*consumer_allocate_stream(
267 int channel_key
, int stream_key
,
268 int shm_fd
, int wait_fd
,
269 enum lttng_consumer_stream_state state
,
271 enum lttng_event_output output
,
272 const char *path_name
,
275 extern int consumer_add_stream(struct lttng_consumer_stream
*stream
);
276 extern void consumer_del_stream(struct lttng_consumer_stream
*stream
);
277 extern void consumer_change_stream_state(int stream_key
,
278 enum lttng_consumer_stream_state state
);
279 extern void consumer_del_channel(struct lttng_consumer_channel
*channel
);
280 extern struct lttng_consumer_channel
*consumer_allocate_channel(
282 int shm_fd
, int wait_fd
,
284 uint64_t max_sb_size
);
285 int consumer_add_channel(struct lttng_consumer_channel
*channel
);
287 extern struct lttng_consumer_local_data
*lttng_consumer_create(
288 enum lttng_consumer_type type
,
289 int (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
290 struct lttng_consumer_local_data
*ctx
),
291 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
292 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
293 int (*update_stream
)(int sessiond_key
, uint32_t state
));
294 extern void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
);
295 extern int lttng_consumer_on_read_subbuffer_mmap(
296 struct lttng_consumer_local_data
*ctx
,
297 struct lttng_consumer_stream
*stream
, unsigned long len
);
298 extern int lttng_consumer_on_read_subbuffer_splice(
299 struct lttng_consumer_local_data
*ctx
,
300 struct lttng_consumer_stream
*stream
, unsigned long len
);
301 extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
302 struct lttng_consumer_stream
*stream
);
303 extern int lttng_consumer_get_produced_snapshot(
304 struct lttng_consumer_local_data
*ctx
,
305 struct lttng_consumer_stream
*stream
,
307 extern void *lttng_consumer_thread_poll_fds(void *data
);
308 extern void *lttng_consumer_thread_receive_fds(void *data
);
309 extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
310 int sock
, struct pollfd
*consumer_sockpoll
);
312 int lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
313 struct lttng_consumer_local_data
*ctx
);
314 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
);
316 #endif /* _LTTNG_CONSUMER_H */