2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <common/consumer/consumer.h>
22 #include <common/hashtable/hashtable.h>
23 #include <lttng/lttng.h>
29 struct snapshot_output
;
31 enum consumer_dst_type
{
36 struct consumer_socket
{
38 * File descriptor. This is just a reference to the consumer data meaning
39 * that every access must be locked and checked for a possible invalid
45 * To use this socket (send/recv), this lock MUST be acquired.
47 pthread_mutex_t
*lock
;
50 * Indicates if the socket was registered by a third part
51 * (REGISTER_CONSUMER) or is the spawn consumer of the session daemon.
52 * During the destroy phase of a consumer output, we close the socket if
53 * this flag is set to 1 since we don't need the fd anymore.
55 unsigned int registered
;
57 /* Flag if network sockets were sent to the consumer. */
58 unsigned int control_sock_sent
;
59 unsigned int data_sock_sent
;
61 struct lttng_ht_node_ulong node
;
63 enum lttng_consumer_type type
;
66 struct consumer_data
{
67 enum lttng_consumer_type type
;
69 pthread_t thread
; /* Worker thread interacting with the consumer */
71 /* Conditions used by the consumer thread to indicate readiness. */
73 pthread_condattr_t condattr
;
74 pthread_mutex_t cond_mutex
;
77 * This is a flag condition indicating that the consumer thread is ready
78 * and connected to the lttng-consumerd daemon. This flag MUST only be
79 * updated by locking the condition mutex above or before spawning a
82 * A value of 0 means that the thread is NOT ready. A value of 1 means that
83 * the thread consumer did connect successfully to the lttng-consumerd
84 * daemon. A negative value indicates that there is been an error and the
85 * thread has likely quit.
87 int consumer_thread_is_ready
;
89 /* Mutex to control consumerd pid assignation */
90 pthread_mutex_t pid_mutex
;
94 /* These two sockets uses the cmd_unix_sock_path. */
97 * Write-end of the channel monitoring pipe to be passed to the
100 int channel_monitor_pipe
;
102 * The metadata socket object is handled differently and only created
103 * locally in this object thus it's the only reference available in the
104 * session daemon. For that reason, a variable for the fd is required and
105 * the metadata socket fd points to it.
108 struct consumer_socket metadata_sock
;
110 /* consumer error and command Unix socket path */
111 const char *err_unix_sock_path
;
112 const char *cmd_unix_sock_path
;
115 * This lock has two purposes. It protects any change to the consumer
116 * socket and make sure only one thread uses this object for read/write
119 pthread_mutex_t lock
;
125 struct consumer_net
{
127 * Indicate if URI type is set. Those flags should only be set when the
128 * created URI is done AND valid.
134 * The following two URIs MUST have the same destination address for
135 * network streaming to work. Network hop are not yet supported.
138 /* Control path for network streaming. */
139 struct lttng_uri control
;
141 /* Data path for network streaming. */
142 struct lttng_uri data
;
144 /* <hostname>/<session-name> */
145 char base_dir
[PATH_MAX
];
149 * Consumer output object describing where and how to send data.
151 struct consumer_output
{
152 struct urcu_ref ref
; /* Refcount */
154 /* If the consumer is enabled meaning that should be used */
155 unsigned int enabled
;
156 enum consumer_dst_type type
;
159 * The net_seq_index is the index of the network stream on the consumer
160 * side. It tells the consumer which streams goes to which relayd with this
161 * index. The relayd sockets are index with it on the consumer side.
163 uint64_t net_seq_index
;
164 /* Store the relay protocol in use if the session is remote. */
165 uint32_t relay_major_version
;
166 uint32_t relay_minor_version
;
169 * Subdirectory path name used for both local and network
170 * consumer (/kernel or /ust).
172 char subdir
[LTTNG_PATH_MAX
];
175 * Hashtable of consumer_socket index by the file descriptor value. For
176 * multiarch consumer support, we can have more than one consumer (ex: 32
179 struct lttng_ht
*socks
;
181 /* Tell if this output is used for snapshot. */
182 unsigned int snapshot
:1;
185 char session_root_path
[LTTNG_PATH_MAX
];
186 struct consumer_net net
;
190 * Sub-directory below the session_root_path where the next chunk of
191 * trace will be stored (\0 before the first session rotation).
193 char chunk_path
[LTTNG_PATH_MAX
];
196 struct consumer_socket
*consumer_find_socket(int key
,
197 struct consumer_output
*consumer
);
198 struct consumer_socket
*consumer_find_socket_by_bitness(int bits
,
199 struct consumer_output
*consumer
);
200 struct consumer_socket
*consumer_allocate_socket(int *fd
);
201 void consumer_add_socket(struct consumer_socket
*sock
,
202 struct consumer_output
*consumer
);
203 void consumer_del_socket(struct consumer_socket
*sock
,
204 struct consumer_output
*consumer
);
205 void consumer_destroy_socket(struct consumer_socket
*sock
);
206 int consumer_copy_sockets(struct consumer_output
*dst
,
207 struct consumer_output
*src
);
208 void consumer_destroy_output_sockets(struct consumer_output
*obj
);
209 int consumer_socket_send(struct consumer_socket
*socket
, void *msg
,
211 int consumer_socket_recv(struct consumer_socket
*socket
, void *msg
,
214 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
);
215 struct consumer_output
*consumer_copy_output(struct consumer_output
*obj
);
216 void consumer_output_get(struct consumer_output
*obj
);
217 void consumer_output_put(struct consumer_output
*obj
);
218 int consumer_set_network_uri(struct consumer_output
*obj
,
219 struct lttng_uri
*uri
);
220 int consumer_send_fds(struct consumer_socket
*sock
, const int *fds
,
222 int consumer_send_msg(struct consumer_socket
*sock
,
223 struct lttcomm_consumer_msg
*msg
);
224 int consumer_send_stream(struct consumer_socket
*sock
,
225 struct consumer_output
*dst
, struct lttcomm_consumer_msg
*msg
,
226 const int *fds
, size_t nb_fd
);
227 int consumer_send_channel(struct consumer_socket
*sock
,
228 struct lttcomm_consumer_msg
*msg
);
229 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
230 struct lttcomm_relayd_sock
*rsock
, struct consumer_output
*consumer
,
231 enum lttng_stream_type type
, uint64_t session_id
,
232 char *session_name
, char *hostname
, int session_live_timer
);
233 int consumer_send_channel_monitor_pipe(struct consumer_socket
*consumer_sock
,
235 int consumer_send_destroy_relayd(struct consumer_socket
*sock
,
236 struct consumer_output
*consumer
);
237 int consumer_recv_status_reply(struct consumer_socket
*sock
);
238 int consumer_recv_status_channel(struct consumer_socket
*sock
,
239 uint64_t *key
, unsigned int *stream_count
);
240 void consumer_output_send_destroy_relayd(struct consumer_output
*consumer
);
241 int consumer_create_socket(struct consumer_data
*data
,
242 struct consumer_output
*output
);
243 int consumer_set_subdir(struct consumer_output
*consumer
,
244 const char *session_name
);
246 void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
247 uint64_t subbuf_size
,
250 unsigned int switch_timer_interval
,
251 unsigned int read_timer_interval
,
252 unsigned int live_timer_interval
,
253 unsigned int monitor_timer_interval
,
257 const char *pathname
,
265 uint64_t tracefile_size
,
266 uint64_t tracefile_count
,
267 uint64_t session_id_per_pid
,
268 unsigned int monitor
,
269 uint32_t ust_app_uid
,
270 int64_t blocking_timeout
,
271 const char *root_shm_path
,
272 const char *shm_path
,
273 uint64_t trace_archive_id
);
274 void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
275 uint64_t channel_key
,
278 uint64_t trace_archive_id
);
279 void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg
*msg
,
280 enum lttng_consumer_command cmd
,
281 uint64_t channel_key
, uint64_t net_seq_idx
);
282 void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
283 uint64_t channel_key
,
285 const char *pathname
,
290 unsigned int nb_init_streams
,
291 enum lttng_event_output output
,
293 uint64_t tracefile_size
,
294 uint64_t tracefile_count
,
295 unsigned int monitor
,
296 unsigned int live_timer_interval
,
297 unsigned int monitor_timer_interval
);
298 int consumer_is_data_pending(uint64_t session_id
,
299 struct consumer_output
*consumer
);
300 int consumer_close_metadata(struct consumer_socket
*socket
,
301 uint64_t metadata_key
);
302 int consumer_setup_metadata(struct consumer_socket
*socket
,
303 uint64_t metadata_key
);
304 int consumer_push_metadata(struct consumer_socket
*socket
,
305 uint64_t metadata_key
, char *metadata_str
, size_t len
,
306 size_t target_offset
, uint64_t version
);
307 int consumer_flush_channel(struct consumer_socket
*socket
, uint64_t key
);
308 int consumer_clear_quiescent_channel(struct consumer_socket
*socket
, uint64_t key
);
309 int consumer_get_discarded_events(uint64_t session_id
, uint64_t channel_key
,
310 struct consumer_output
*consumer
, uint64_t *discarded
);
311 int consumer_get_lost_packets(uint64_t session_id
, uint64_t channel_key
,
312 struct consumer_output
*consumer
, uint64_t *lost
);
314 /* Snapshot command. */
315 int consumer_snapshot_channel(struct consumer_socket
*socket
, uint64_t key
,
316 struct snapshot_output
*output
, int metadata
,
317 uid_t uid
, gid_t gid
, const char *session_path
, int wait
,
318 uint64_t nb_packets_per_stream
, uint64_t trace_archive_id
);
320 /* Rotation commands. */
321 int consumer_rotate_channel(struct consumer_socket
*socket
, uint64_t key
,
322 uid_t uid
, gid_t gid
, struct consumer_output
*output
,
323 char *domain_path
, bool is_metadata_channel
, uint64_t new_chunk_id
);
324 int consumer_rotate_rename(struct consumer_socket
*socket
, uint64_t session_id
,
325 const struct consumer_output
*output
, const char *old_path
,
326 const char *new_path
, uid_t uid
, gid_t gid
);
327 int consumer_check_rotation_pending_local(struct consumer_socket
*socket
,
328 uint64_t session_id
, uint64_t chunk_id
);
329 int consumer_check_rotation_pending_relay(struct consumer_socket
*socket
,
330 const struct consumer_output
*output
, uint64_t session_id
,
332 int consumer_mkdir(struct consumer_socket
*socket
, uint64_t session_id
,
333 const struct consumer_output
*output
, const char *path
,
334 uid_t uid
, gid_t gid
);
336 #endif /* _CONSUMER_H */