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
25 #include <urcu/list.h>
26 #include <lttng/lttng.h>
29 * When the receiving thread dies, we need to have a way to make the polling
30 * thread exit eventually. If all FDs hang up (normal case when the
31 * lttng-sessiond stops), we can exit cleanly, but if there is a problem and
32 * for whatever reason some FDs remain open, the consumer should still exit
35 * If the timeout is reached, it means that during this period no events
36 * occurred on the FDs so we need to force an exit. This case should not happen
37 * but it is a safety to ensure we won't block the consumer indefinitely.
39 * The value of 2 seconds is an arbitrary choice.
41 #define LTTNG_CONSUMER_POLL_TIMEOUT 2000
43 /* Commands for consumer */
44 enum lttng_consumer_command
{
45 LTTNG_CONSUMER_ADD_CHANNEL
,
46 LTTNG_CONSUMER_ADD_STREAM
,
47 /* pause, delete, active depending on fd state */
48 LTTNG_CONSUMER_UPDATE_STREAM
,
49 /* inform the consumer to quit when all fd has hang up */
53 /* State of each fd in consumer */
54 enum lttng_consumer_stream_state
{
55 LTTNG_CONSUMER_ACTIVE_STREAM
,
56 LTTNG_CONSUMER_PAUSE_STREAM
,
57 LTTNG_CONSUMER_DELETE_STREAM
,
60 struct lttng_consumer_channel_list
{
61 struct cds_list_head head
;
64 struct lttng_consumer_stream_list
{
65 struct cds_list_head head
;
68 enum lttng_consumer_type
{
69 LTTNG_CONSUMER_UNKNOWN
= 0,
70 LTTNG_CONSUMER_KERNEL
,
74 struct lttng_consumer_channel
{
75 struct cds_list_head list
;
77 uint64_t max_sb_size
; /* the subbuffer size for this channel */
78 int refcount
; /* Number of streams referencing this channel */
84 struct lttng_ust_shm_handle
*handle
;
91 /* Forward declaration for UST. */
92 struct lttng_ust_lib_ring_buffer
;
95 * Internal representation of the streams, sessiond_key is used to identify
98 struct lttng_consumer_stream
{
99 struct cds_list_head list
;
100 struct lttng_consumer_channel
*chan
; /* associated channel */
102 * key is the key used by the session daemon to refer to the
103 * object in the consumer daemon.
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
;
115 enum lttng_event_output output
; /* splice or mmap */
119 struct lttng_ust_lib_ring_buffer
*buf
;
124 * UST consumer local data to the program. One or more instance per
127 struct lttng_consumer_local_data
{
128 /* function to call when data is available on a buffer */
129 int (*on_buffer_ready
)(struct lttng_consumer_stream
*stream
,
130 struct lttng_consumer_local_data
*ctx
);
132 * function to call when we receive a new channel, it receives a
133 * newly allocated channel, depending on the return code of this
134 * function, the new channel will be handled by the application
138 * > 0 (success, FD is kept by application)
139 * == 0 (success, FD is left to library)
142 int (*on_recv_channel
)(struct lttng_consumer_channel
*channel
);
144 * function to call when we receive a new stream, it receives a
145 * newly allocated stream, depending on the return code of this
146 * function, the new stream will be handled by the application
150 * > 0 (success, FD is kept by application)
151 * == 0 (success, FD is left to library)
154 int (*on_recv_stream
)(struct lttng_consumer_stream
*stream
);
156 * function to call when a stream is getting updated by the session
157 * daemon, this function receives the sessiond key and the new
158 * state, depending on the return code of this function the
159 * update of state for the stream is handled by the application
163 * > 0 (success, FD is kept by application)
164 * == 0 (success, FD is left to library)
167 int (*on_update_stream
)(int sessiond_key
, uint32_t state
);
168 /* socket to communicate errors with sessiond */
169 int consumer_error_socket
;
170 /* socket to exchange commands with sessiond */
171 char *consumer_command_sock_path
;
172 /* communication with splice */
173 int consumer_thread_pipe
[2];
174 /* pipe to wake the poll thread when necessary */
175 int consumer_poll_pipe
[2];
176 /* to let the signal handler wake up the fd receiver thread */
177 int consumer_should_quit
[2];
181 * Library-level data. One instance per process.
183 struct lttng_consumer_global_data
{
185 * consumer_data.lock protects consumer_data.fd_list,
186 * consumer_data.stream_count, and consumer_data.need_update. It
187 * ensures the count matches the number of items in the fd_list.
188 * It ensures the list updates *always* trigger an fd_array
189 * update (therefore need to make list update vs
190 * consumer_data.need_update flag update atomic, and also flag
191 * read, fd array and flag clear atomic).
193 pthread_mutex_t lock
;
195 * Number of streams in the list below. Protected by
196 * consumer_data.lock.
200 * Lists of streams and channels. Protected by consumer_data.lock.
202 struct lttng_consumer_stream_list stream_list
;
203 struct lttng_consumer_channel_list channel_list
;
205 * Flag specifying if the local array of FDs needs update in the
206 * poll function. Protected by consumer_data.lock.
208 unsigned int need_update
;
209 enum lttng_consumer_type type
;
213 * Set the error socket for communication with a session daemon.
215 extern void lttng_consumer_set_error_sock(
216 struct lttng_consumer_local_data
*ctx
, int sock
);
219 * Set the command socket path for communication with a session daemon.
221 extern void lttng_consumer_set_command_sock_path(
222 struct lttng_consumer_local_data
*ctx
, char *sock
);
225 * Send return code to session daemon.
227 * Returns the return code of sendmsg : the number of bytes transmitted or -1
230 extern int lttng_consumer_send_error(
231 struct lttng_consumer_local_data
*ctx
, int cmd
);
234 * Called from signal handler to ensure a clean exit.
236 extern void lttng_consumer_should_exit(
237 struct lttng_consumer_local_data
*ctx
);
240 * Cleanup the daemon's socket on exit.
242 extern void lttng_consumer_cleanup(void);
245 * Flush pending writes to trace output disk file.
247 extern void lttng_consumer_sync_trace_file(
248 struct lttng_consumer_stream
*stream
, off_t orig_offset
);
251 * Poll on the should_quit pipe and the command socket return -1 on error and
252 * should exit, 0 if data is available on the command socket
254 extern int lttng_consumer_poll_socket(struct pollfd
*kconsumer_sockpoll
);
256 extern int consumer_update_poll_array(
257 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
258 struct lttng_consumer_stream
**local_consumer_streams
);
260 extern struct lttng_consumer_stream
*consumer_allocate_stream(
261 int channel_key
, int stream_key
,
262 int shm_fd
, int wait_fd
,
263 enum lttng_consumer_stream_state state
,
265 enum lttng_event_output output
,
266 const char *path_name
);
267 extern int consumer_add_stream(struct lttng_consumer_stream
*stream
);
268 extern void consumer_del_stream(struct lttng_consumer_stream
*stream
);
269 extern void consumer_change_stream_state(int stream_key
,
270 enum lttng_consumer_stream_state state
);
271 extern void consumer_del_channel(struct lttng_consumer_channel
*channel
);
272 extern struct lttng_consumer_channel
*consumer_allocate_channel(
274 int shm_fd
, int wait_fd
,
276 uint64_t max_sb_size
);
277 int consumer_add_channel(struct lttng_consumer_channel
*channel
);
279 extern struct lttng_consumer_local_data
*lttng_consumer_create(
280 enum lttng_consumer_type type
,
281 int (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
282 struct lttng_consumer_local_data
*ctx
),
283 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
284 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
285 int (*update_stream
)(int sessiond_key
, uint32_t state
));
286 extern void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
);
287 extern int lttng_consumer_on_read_subbuffer_mmap(
288 struct lttng_consumer_local_data
*ctx
,
289 struct lttng_consumer_stream
*stream
, unsigned long len
);
290 extern int lttng_consumer_on_read_subbuffer_splice(
291 struct lttng_consumer_local_data
*ctx
,
292 struct lttng_consumer_stream
*stream
, unsigned long len
);
293 extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
294 struct lttng_consumer_stream
*stream
);
295 extern int lttng_consumer_get_produced_snapshot(
296 struct lttng_consumer_local_data
*ctx
,
297 struct lttng_consumer_stream
*stream
,
299 extern void *lttng_consumer_thread_poll_fds(void *data
);
300 extern void *lttng_consumer_thread_receive_fds(void *data
);
301 extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
302 int sock
, struct pollfd
*consumer_sockpoll
);
304 int lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
305 struct lttng_consumer_local_data
*ctx
);
306 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
);
308 #endif /* _LTTNG_CONSUMER_H */