2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/consumer/consumer.h>
12 #include <common/hashtable/hashtable.h>
13 #include <lttng/lttng.h>
19 struct snapshot_output
;
22 enum consumer_dst_type
{
27 enum consumer_trace_chunk_exists_status
{
28 CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL
,
29 CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE
,
30 CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
,
33 struct consumer_socket
{
35 * File descriptor. This is just a reference to the consumer data meaning
36 * that every access must be locked and checked for a possible invalid
42 * To use this socket (send/recv), this lock MUST be acquired.
44 pthread_mutex_t
*lock
;
47 * Indicates if the socket was registered by a third part
48 * (REGISTER_CONSUMER) or is the spawn consumer of the session daemon.
49 * During the destroy phase of a consumer output, we close the socket if
50 * this flag is set to 1 since we don't need the fd anymore.
52 unsigned int registered
;
54 /* Flag if network sockets were sent to the consumer. */
55 unsigned int control_sock_sent
;
56 unsigned int data_sock_sent
;
58 struct lttng_ht_node_ulong node
;
60 enum lttng_consumer_type type
;
63 struct consumer_data
{
64 enum lttng_consumer_type type
;
66 /* Mutex to control consumerd pid assignation */
67 pthread_mutex_t pid_mutex
;
71 /* These two sockets uses the cmd_unix_sock_path. */
74 * Write-end of the channel monitoring pipe to be passed to the
77 int channel_monitor_pipe
;
79 * The metadata socket object is handled differently and only created
80 * locally in this object thus it's the only reference available in the
81 * session daemon. For that reason, a variable for the fd is required and
82 * the metadata socket fd points to it.
85 struct consumer_socket metadata_sock
;
87 /* consumer error and command Unix socket path */
88 const char *err_unix_sock_path
;
89 const char *cmd_unix_sock_path
;
92 * This lock has two purposes. It protects any change to the consumer
93 * socket and make sure only one thread uses this object for read/write
102 struct consumer_net
{
104 * Indicate if URI type is set. Those flags should only be set when the
105 * created URI is done AND valid.
111 * The following two URIs MUST have the same destination address for
112 * network streaming to work. Network hop are not yet supported.
115 /* Control path for network streaming. */
116 struct lttng_uri control
;
118 /* Data path for network streaming. */
119 struct lttng_uri data
;
121 /* <hostname>/<session-name> */
122 char base_dir
[PATH_MAX
];
126 * Consumer output object describing where and how to send data.
128 struct consumer_output
{
129 struct urcu_ref ref
; /* Refcount */
131 /* If the consumer is enabled meaning that should be used */
132 unsigned int enabled
;
133 enum consumer_dst_type type
;
136 * The net_seq_index is the index of the network stream on the consumer
137 * side. It tells the consumer which streams goes to which relayd with this
138 * index. The relayd sockets are index with it on the consumer side.
140 uint64_t net_seq_index
;
141 /* Store the relay protocol in use if the session is remote. */
142 uint32_t relay_major_version
;
143 uint32_t relay_minor_version
;
145 /* True if relayd supports the clear feature. */
146 bool relay_allows_clear
;
149 * Subdirectory path name used for both local and network
150 * consumer ("kernel", "ust", or empty).
152 char domain_subdir
[max(sizeof(DEFAULT_KERNEL_TRACE_DIR
),
153 sizeof(DEFAULT_UST_TRACE_DIR
))];
156 * Hashtable of consumer_socket index by the file descriptor value. For
157 * multiarch consumer support, we can have more than one consumer (ex:
160 struct lttng_ht
*socks
;
162 /* Tell if this output is used for snapshot. */
163 unsigned int snapshot
:1;
166 char session_root_path
[LTTNG_PATH_MAX
];
167 struct consumer_net net
;
171 * Sub-directory below the session_root_path where the next chunk of
172 * trace will be stored (\0 before the first session rotation).
174 char chunk_path
[LTTNG_PATH_MAX
];
177 struct consumer_socket
*consumer_find_socket(int key
,
178 const struct consumer_output
*consumer
);
179 struct consumer_socket
*consumer_find_socket_by_bitness(int bits
,
180 const struct consumer_output
*consumer
);
181 struct consumer_socket
*consumer_allocate_socket(int *fd
);
182 void consumer_add_socket(struct consumer_socket
*sock
,
183 struct consumer_output
*consumer
);
184 void consumer_del_socket(struct consumer_socket
*sock
,
185 struct consumer_output
*consumer
);
186 void consumer_destroy_socket(struct consumer_socket
*sock
);
187 int consumer_copy_sockets(struct consumer_output
*dst
,
188 struct consumer_output
*src
);
189 void consumer_destroy_output_sockets(struct consumer_output
*obj
);
190 int consumer_socket_send(struct consumer_socket
*socket
, const void *msg
,
192 int consumer_socket_recv(struct consumer_socket
*socket
, void *msg
,
195 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
);
196 struct consumer_output
*consumer_copy_output(struct consumer_output
*obj
);
197 void consumer_output_get(struct consumer_output
*obj
);
198 void consumer_output_put(struct consumer_output
*obj
);
199 int consumer_set_network_uri(const struct ltt_session
*session
,
200 struct consumer_output
*obj
,
201 struct lttng_uri
*uri
);
202 int consumer_send_fds(struct consumer_socket
*sock
, const int *fds
,
204 int consumer_send_msg(struct consumer_socket
*sock
,
205 const struct lttcomm_consumer_msg
*msg
);
206 int consumer_send_stream(struct consumer_socket
*sock
,
207 struct consumer_output
*dst
, struct lttcomm_consumer_msg
*msg
,
208 const int *fds
, size_t nb_fd
);
209 int consumer_send_channel(struct consumer_socket
*sock
,
210 struct lttcomm_consumer_msg
*msg
);
211 int consumer_send_relayd_socket(struct consumer_socket
*consumer_sock
,
212 struct lttcomm_relayd_sock
*rsock
, struct consumer_output
*consumer
,
213 enum lttng_stream_type type
, uint64_t session_id
,
214 const char *session_name
, const char *hostname
,
215 const char *base_path
, int session_live_timer
,
216 const uint64_t *current_chunk_id
, time_t session_creation_time
,
217 bool session_name_contains_creation_time
);
218 int consumer_send_channel_monitor_pipe(struct consumer_socket
*consumer_sock
,
220 int consumer_send_destroy_relayd(struct consumer_socket
*sock
,
221 struct consumer_output
*consumer
);
222 int consumer_recv_status_reply(struct consumer_socket
*sock
);
223 int consumer_recv_status_channel(struct consumer_socket
*sock
,
224 uint64_t *key
, unsigned int *stream_count
);
225 void consumer_output_send_destroy_relayd(struct consumer_output
*consumer
);
226 int consumer_create_socket(struct consumer_data
*data
,
227 struct consumer_output
*output
);
229 void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
230 uint64_t subbuf_size
,
233 unsigned int switch_timer_interval
,
234 unsigned int read_timer_interval
,
235 unsigned int live_timer_interval
,
236 bool is_in_live_session
,
237 unsigned int monitor_timer_interval
,
241 const char *pathname
,
247 uint64_t tracefile_size
,
248 uint64_t tracefile_count
,
249 uint64_t session_id_per_pid
,
250 unsigned int monitor
,
251 uint32_t ust_app_uid
,
252 int64_t blocking_timeout
,
253 const char *root_shm_path
,
254 const char *shm_path
,
255 struct lttng_trace_chunk
*trace_chunk
,
256 const struct lttng_credentials
*buffer_credentials
);
257 void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
258 uint64_t channel_key
,
261 void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg
*msg
,
262 enum lttng_consumer_command cmd
,
263 uint64_t channel_key
, uint64_t net_seq_idx
);
264 void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
265 uint64_t channel_key
,
267 const char *pathname
,
272 unsigned int nb_init_streams
,
273 enum lttng_event_output output
,
275 uint64_t tracefile_size
,
276 uint64_t tracefile_count
,
277 unsigned int monitor
,
278 unsigned int live_timer_interval
,
279 bool is_in_live_session
,
280 unsigned int monitor_timer_interval
,
281 struct lttng_trace_chunk
*trace_chunk
);
282 int consumer_is_data_pending(uint64_t session_id
,
283 struct consumer_output
*consumer
);
284 int consumer_close_metadata(struct consumer_socket
*socket
,
285 uint64_t metadata_key
);
286 int consumer_setup_metadata(struct consumer_socket
*socket
,
287 uint64_t metadata_key
);
288 int consumer_push_metadata(struct consumer_socket
*socket
,
289 uint64_t metadata_key
, char *metadata_str
, size_t len
,
290 size_t target_offset
, uint64_t version
);
291 int consumer_flush_channel(struct consumer_socket
*socket
, uint64_t key
);
292 int consumer_clear_quiescent_channel(struct consumer_socket
*socket
, uint64_t key
);
293 int consumer_get_discarded_events(uint64_t session_id
, uint64_t channel_key
,
294 struct consumer_output
*consumer
, uint64_t *discarded
);
295 int consumer_get_lost_packets(uint64_t session_id
, uint64_t channel_key
,
296 struct consumer_output
*consumer
, uint64_t *lost
);
298 /* Snapshot command. */
299 enum lttng_error_code
consumer_snapshot_channel(struct consumer_socket
*socket
,
300 uint64_t key
, const struct consumer_output
*output
, int metadata
,
301 uid_t uid
, gid_t gid
, const char *channel_path
, int wait
,
302 uint64_t nb_packets_per_stream
);
304 /* Rotation commands. */
305 int consumer_rotate_channel(struct consumer_socket
*socket
, uint64_t key
,
306 uid_t uid
, gid_t gid
, struct consumer_output
*output
,
307 bool is_metadata_channel
);
308 int consumer_init(struct consumer_socket
*socket
,
309 const lttng_uuid sessiond_uuid
);
311 int consumer_create_trace_chunk(struct consumer_socket
*socket
,
312 uint64_t relayd_id
, uint64_t session_id
,
313 struct lttng_trace_chunk
*chunk
,
314 const char *domain_subdir
);
315 int consumer_close_trace_chunk(struct consumer_socket
*socket
,
316 uint64_t relayd_id
, uint64_t session_id
,
317 struct lttng_trace_chunk
*chunk
,
318 char *closed_trace_chunk_path
);
319 int consumer_trace_chunk_exists(struct consumer_socket
*socket
,
320 uint64_t relayd_id
, uint64_t session_id
,
321 struct lttng_trace_chunk
*chunk
,
322 enum consumer_trace_chunk_exists_status
*result
);
323 int consumer_open_channel_packets(struct consumer_socket
*socket
, uint64_t key
);
325 char *setup_channel_trace_path(struct consumer_output
*consumer
,
326 const char *session_path
, size_t *consumer_path_offset
);
329 int consumer_clear_channel(struct consumer_socket
*socket
, uint64_t key
);
331 #endif /* _CONSUMER_H */