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 LIB_CONSUMER_H
21 #define LIB_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>
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 */
41 LTTNG_CONSUMER_ADD_RELAYD_SOCKET
,
42 /* Inform the consumer to kill a specific relayd connection */
43 LTTNG_CONSUMER_DESTROY_RELAYD
,
44 /* Return to the sessiond if there is data pending for a session */
45 LTTNG_CONSUMER_DATA_PENDING
,
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
,
55 enum lttng_consumer_type
{
56 LTTNG_CONSUMER_UNKNOWN
= 0,
57 LTTNG_CONSUMER_KERNEL
,
62 enum consumer_endpoint_status
{
63 CONSUMER_ENDPOINT_ACTIVE
,
64 CONSUMER_ENDPOINT_INACTIVE
,
67 struct lttng_consumer_channel
{
68 struct lttng_ht_node_ulong node
;
70 uint64_t max_sb_size
; /* the subbuffer size for this channel */
71 int refcount
; /* Number of streams referencing this channel */
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.
76 unsigned int nb_init_streams
;
83 struct lttng_ust_shm_handle
*handle
;
88 /* Forward declaration for UST. */
89 struct lttng_ust_lib_ring_buffer
;
92 * Internal representation of the streams, sessiond_key is used to identify
95 struct lttng_consumer_stream
{
96 /* HT node used by the data_ht and metadata_ht */
97 struct lttng_ht_node_ulong node
;
98 /* HT node used in consumer_data.stream_list_ht */
99 struct lttng_ht_node_ulong node_session_id
;
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
;
122 int hangup_flush_done
;
123 /* UID/GID of the user owning the session to which stream belongs */
126 /* Network sequence number. Indicating on which relayd socket it goes. */
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
;
133 * When sending a stream packet to a relayd, this number is used to track
134 * the packet sent by the consumer and seen by the relayd. When sending the
135 * data header to the relayd, this number is sent and if the transmission
136 * was successful, it is incremented.
138 * Even if the full data is not fully transmitted it won't matter since
139 * only two possible error can happen after that where either the relayd
140 * died or a read error is detected on the stream making this value useless
143 * This value SHOULD be read/updated atomically or with the lock acquired.
145 uint64_t next_net_seq_num
;
147 * Lock to use the stream FDs since they are used between threads.
149 * This is nested INSIDE the consumer_data lock.
150 * This is nested OUTSIDE consumer_relayd_sock_pair lock.
152 pthread_mutex_t lock
;
153 /* Tracing session id */
156 * Indicates if the stream end point is still active or not (network
157 * streaming or local file system). The thread "owning" the stream is
158 * handling this status and can be notified of a state change through the
159 * consumer data appropriate pipe.
161 enum consumer_endpoint_status endpoint_status
;
165 * Internal representation of a relayd socket pair.
167 struct consumer_relayd_sock_pair
{
168 /* Network sequence number. */
170 /* Number of stream associated with this relayd */
171 unsigned int refcount
;
174 * This flag indicates whether or not we should destroy this object. The
175 * destruction should ONLY occurs when this flag is set and the refcount is
178 unsigned int destroy_flag
;
181 * Mutex protecting the control socket to avoid out of order packets
182 * between threads sending data to the relayd. Since metadata data is sent
183 * over that socket, at least two sendmsg() are needed (header + data)
184 * creating a race for packets to overlap between threads using it.
186 * This is nested INSIDE the consumer_data lock.
187 * This is nested INSIDE the stream lock.
189 pthread_mutex_t ctrl_sock_mutex
;
191 /* Control socket. Command and metadata are passed over it */
192 struct lttcomm_sock control_sock
;
195 * We don't need a mutex at this point since we only splice or write single
196 * large chunk of data with a header appended at the begining. Moreover,
197 * this socket is for now only used in a single thread.
199 struct lttcomm_sock data_sock
;
200 struct lttng_ht_node_ulong node
;
202 /* Session id on both sides for the sockets. */
203 uint64_t relayd_session_id
;
204 uint64_t sessiond_session_id
;
208 * UST consumer local data to the program. One or more instance per
211 struct lttng_consumer_local_data
{
213 * Function to call when data is available on a buffer.
214 * Returns the number of bytes read, or negative error value.
216 ssize_t (*on_buffer_ready
)(struct lttng_consumer_stream
*stream
,
217 struct lttng_consumer_local_data
*ctx
);
219 * function to call when we receive a new channel, it receives a
220 * newly allocated channel, depending on the return code of this
221 * function, the new channel will be handled by the application
225 * > 0 (success, FD is kept by application)
226 * == 0 (success, FD is left to library)
229 int (*on_recv_channel
)(struct lttng_consumer_channel
*channel
);
231 * function to call when we receive a new stream, it receives a
232 * newly allocated stream, depending on the return code of this
233 * function, the new stream will be handled by the application
237 * > 0 (success, FD is kept by application)
238 * == 0 (success, FD is left to library)
241 int (*on_recv_stream
)(struct lttng_consumer_stream
*stream
);
243 * function to call when a stream is getting updated by the session
244 * daemon, this function receives the sessiond key and the new
245 * state, depending on the return code of this function the
246 * update of state for the stream is handled by the application
250 * > 0 (success, FD is kept by application)
251 * == 0 (success, FD is left to library)
254 int (*on_update_stream
)(int sessiond_key
, uint32_t state
);
255 /* socket to communicate errors with sessiond */
256 int consumer_error_socket
;
257 /* socket to exchange commands with sessiond */
258 char *consumer_command_sock_path
;
259 /* communication with splice */
260 int consumer_thread_pipe
[2];
261 int consumer_splice_metadata_pipe
[2];
262 /* Data stream poll thread pipe. To transfer data stream to the thread */
263 int consumer_data_pipe
[2];
264 /* to let the signal handler wake up the fd receiver thread */
265 int consumer_should_quit
[2];
266 /* Metadata poll thread pipe. Transfer metadata stream to it */
267 int consumer_metadata_pipe
[2];
271 * Library-level data. One instance per process.
273 struct lttng_consumer_global_data
{
275 * At this time, this lock is used to ensure coherence between the count
276 * and number of element in the hash table. It's also a protection for
277 * concurrent read/write between threads.
279 * This is nested OUTSIDE the stream lock.
280 * This is nested OUTSIDE the consumer_relayd_sock_pair lock.
282 pthread_mutex_t lock
;
285 * Number of streams in the data stream hash table declared outside.
286 * Protected by consumer_data.lock.
290 /* Channel hash table protected by consumer_data.lock. */
291 struct lttng_ht
*channel_ht
;
293 * Flag specifying if the local array of FDs needs update in the
294 * poll function. Protected by consumer_data.lock.
296 unsigned int need_update
;
297 enum lttng_consumer_type type
;
300 * Relayd socket(s) hashtable indexed by network sequence number. Each
301 * stream has an index which associate the right relayd socket to use.
303 struct lttng_ht
*relayd_ht
;
306 * This hash table contains all streams (metadata and data) indexed by
307 * session id. In other words, the ht is indexed by session id and each
308 * bucket contains the list of associated streams.
310 * This HT uses the "node_session_id" of the consumer stream.
312 struct lttng_ht
*stream_list_ht
;
316 * Init consumer data structures.
318 extern void lttng_consumer_init(void);
321 * Set the error socket for communication with a session daemon.
323 extern void lttng_consumer_set_error_sock(
324 struct lttng_consumer_local_data
*ctx
, int sock
);
327 * Set the command socket path for communication with a session daemon.
329 extern void lttng_consumer_set_command_sock_path(
330 struct lttng_consumer_local_data
*ctx
, char *sock
);
333 * Send return code to session daemon.
335 * Returns the return code of sendmsg : the number of bytes transmitted or -1
338 extern int lttng_consumer_send_error(
339 struct lttng_consumer_local_data
*ctx
, int cmd
);
342 * Called from signal handler to ensure a clean exit.
344 extern void lttng_consumer_should_exit(
345 struct lttng_consumer_local_data
*ctx
);
348 * Cleanup the daemon's socket on exit.
350 extern void lttng_consumer_cleanup(void);
353 * Flush pending writes to trace output disk file.
355 extern void lttng_consumer_sync_trace_file(
356 struct lttng_consumer_stream
*stream
, off_t orig_offset
);
359 * Poll on the should_quit pipe and the command socket return -1 on error and
360 * should exit, 0 if data is available on the command socket
362 extern int lttng_consumer_poll_socket(struct pollfd
*kconsumer_sockpoll
);
364 extern struct lttng_consumer_stream
*consumer_allocate_stream(
365 int channel_key
, int stream_key
,
366 int shm_fd
, int wait_fd
,
367 enum lttng_consumer_stream_state state
,
369 enum lttng_event_output output
,
370 const char *path_name
,
377 extern void consumer_del_stream(struct lttng_consumer_stream
*stream
,
378 struct lttng_ht
*ht
);
379 extern void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
380 struct lttng_ht
*ht
);
381 extern void consumer_del_channel(struct lttng_consumer_channel
*channel
);
382 extern struct lttng_consumer_channel
*consumer_allocate_channel(
384 int shm_fd
, int wait_fd
,
386 uint64_t max_sb_size
,
387 unsigned int nb_init_streams
);
388 int consumer_add_channel(struct lttng_consumer_channel
*channel
);
390 /* lttng-relayd consumer command */
391 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
393 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
);
394 int consumer_handle_stream_before_relayd(struct lttng_consumer_stream
*stream
,
396 void consumer_steal_stream_key(int key
, struct lttng_ht
*ht
);
398 extern struct lttng_consumer_local_data
*lttng_consumer_create(
399 enum lttng_consumer_type type
,
400 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
401 struct lttng_consumer_local_data
*ctx
),
402 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
403 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
404 int (*update_stream
)(int sessiond_key
, uint32_t state
));
405 extern void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
);
406 extern ssize_t
lttng_consumer_on_read_subbuffer_mmap(
407 struct lttng_consumer_local_data
*ctx
,
408 struct lttng_consumer_stream
*stream
, unsigned long len
,
409 unsigned long padding
);
410 extern ssize_t
lttng_consumer_on_read_subbuffer_splice(
411 struct lttng_consumer_local_data
*ctx
,
412 struct lttng_consumer_stream
*stream
, unsigned long len
,
413 unsigned long padding
);
414 extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
415 struct lttng_consumer_stream
*stream
);
416 extern int lttng_consumer_get_produced_snapshot(
417 struct lttng_consumer_local_data
*ctx
,
418 struct lttng_consumer_stream
*stream
,
420 extern void *consumer_thread_metadata_poll(void *data
);
421 extern void *consumer_thread_data_poll(void *data
);
422 extern void *consumer_thread_sessiond_poll(void *data
);
423 extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
424 int sock
, struct pollfd
*consumer_sockpoll
);
426 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
427 struct lttng_consumer_local_data
*ctx
);
428 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
);
429 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
430 struct lttng_consumer_local_data
*ctx
, int sock
,
431 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
,
432 unsigned int sessiond_id
);
433 void consumer_flag_relayd_for_destroy(
434 struct consumer_relayd_sock_pair
*relayd
);
435 int consumer_data_pending(uint64_t id
);
436 int consumer_send_status_msg(int sock
, int ret_code
);
438 #endif /* LIB_CONSUMER_H */