2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/consumer/consumer.hpp>
12 #include <common/hashtable/hashtable.hpp>
13 #include <lttng/lttng.h>
17 #include "snapshot.hpp"
20 struct snapshot_output;
24 * Needed until we use C++14, where std::max is constexpr.
26 * Use a static_assert so we remember to remove it when we upgrade to a newer
29 static_assert(__cplusplus == 201103L, "");
31 constexpr T max_constexpr(T l, T r)
36 enum consumer_dst_type {
41 enum consumer_trace_chunk_exists_status {
42 CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL,
43 CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE,
44 CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK,
47 struct consumer_socket {
49 * File descriptor. This is just a reference to the consumer data meaning
50 * that every access must be locked and checked for a possible invalid
56 * To use this socket (send/recv), this lock MUST be acquired.
58 pthread_mutex_t *lock;
61 * Indicates if the socket was registered by a third part
62 * (REGISTER_CONSUMER) or is the spawn consumer of the session daemon.
63 * During the destroy phase of a consumer output, we close the socket if
64 * this flag is set to 1 since we don't need the fd anymore.
66 unsigned int registered;
68 /* Flag if network sockets were sent to the consumer. */
69 unsigned int control_sock_sent;
70 unsigned int data_sock_sent;
72 struct lttng_ht_node_ulong node;
74 enum lttng_consumer_type type;
77 struct consumer_data {
78 consumer_data (lttng_consumer_type type_)
82 enum lttng_consumer_type type;
84 /* Mutex to control consumerd pid assignation */
85 pthread_mutex_t pid_mutex = PTHREAD_MUTEX_INITIALIZER;
89 /* These two sockets uses the cmd_unix_sock_path. */
92 * Write-end of the channel monitoring pipe to be passed to the
95 int channel_monitor_pipe = -1;
97 * The metadata socket object is handled differently and only created
98 * locally in this object thus it's the only reference available in the
99 * session daemon. For that reason, a variable for the fd is required and
100 * the metadata socket fd points to it.
103 struct consumer_socket metadata_sock {};
105 /* consumer error and command Unix socket path */
106 const char *err_unix_sock_path = nullptr;
107 const char *cmd_unix_sock_path = nullptr;
110 * This lock has two purposes. It protects any change to the consumer
111 * socket and make sure only one thread uses this object for read/write
114 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
120 struct consumer_net {
122 * Indicate if URI type is set. Those flags should only be set when the
123 * created URI is done AND valid.
129 * The following two URIs MUST have the same destination address for
130 * network streaming to work. Network hop are not yet supported.
133 /* Control path for network streaming. */
134 struct lttng_uri control;
136 /* Data path for network streaming. */
137 struct lttng_uri data;
139 /* <hostname>/<session-name> */
140 char base_dir[PATH_MAX];
144 * Consumer output object describing where and how to send data.
146 struct consumer_output {
147 struct urcu_ref ref; /* Refcount */
149 /* If the consumer is enabled meaning that should be used */
150 unsigned int enabled;
151 enum consumer_dst_type type;
154 * The net_seq_index is the index of the network stream on the consumer
155 * side. It tells the consumer which streams goes to which relayd with this
156 * index. The relayd sockets are index with it on the consumer side.
158 uint64_t net_seq_index;
159 /* Store the relay protocol in use if the session is remote. */
160 uint32_t relay_major_version;
161 uint32_t relay_minor_version;
163 /* True if relayd supports the clear feature. */
164 bool relay_allows_clear;
167 * Subdirectory path name used for both local and network
168 * consumer ("kernel", "ust", or empty).
171 max_constexpr(sizeof(DEFAULT_KERNEL_TRACE_DIR),
172 sizeof(DEFAULT_UST_TRACE_DIR))];
175 * Hashtable of consumer_socket index by the file descriptor value. For
176 * multiarch consumer support, we can have more than one consumer (ex:
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 const struct consumer_output *consumer);
198 struct consumer_socket *consumer_find_socket_by_bitness(int bits,
199 const 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, const 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(const struct ltt_session *session,
219 struct consumer_output *obj,
220 struct lttng_uri *uri);
221 int consumer_send_fds(struct consumer_socket *sock, const int *fds,
223 int consumer_send_msg(struct consumer_socket *sock,
224 const struct lttcomm_consumer_msg *msg);
225 int consumer_send_stream(struct consumer_socket *sock,
226 struct consumer_output *dst, struct lttcomm_consumer_msg *msg,
227 const int *fds, size_t nb_fd);
228 int consumer_send_channel(struct consumer_socket *sock,
229 struct lttcomm_consumer_msg *msg);
230 int consumer_send_relayd_socket(struct consumer_socket *consumer_sock,
231 struct lttcomm_relayd_sock *rsock, struct consumer_output *consumer,
232 enum lttng_stream_type type, uint64_t session_id,
233 const char *session_name, const char *hostname,
234 const char *base_path, int session_live_timer,
235 const uint64_t *current_chunk_id, time_t session_creation_time,
236 bool session_name_contains_creation_time);
237 int consumer_send_channel_monitor_pipe(struct consumer_socket *consumer_sock,
239 int consumer_send_destroy_relayd(struct consumer_socket *sock,
240 struct consumer_output *consumer);
241 int consumer_recv_status_reply(struct consumer_socket *sock);
242 int consumer_recv_status_channel(struct consumer_socket *sock,
243 uint64_t *key, unsigned int *stream_count);
244 void consumer_output_send_destroy_relayd(struct consumer_output *consumer);
245 int consumer_create_socket(struct consumer_data *data,
246 struct consumer_output *output);
248 void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg *msg,
249 uint64_t subbuf_size,
252 unsigned int switch_timer_interval,
253 unsigned int read_timer_interval,
254 unsigned int live_timer_interval,
255 bool is_in_live_session,
256 unsigned int monitor_timer_interval,
260 const char *pathname,
266 uint64_t tracefile_size,
267 uint64_t tracefile_count,
268 uint64_t session_id_per_pid,
269 unsigned int monitor,
270 uint32_t ust_app_uid,
271 int64_t blocking_timeout,
272 const char *root_shm_path,
273 const char *shm_path,
274 struct lttng_trace_chunk *trace_chunk,
275 const struct lttng_credentials *buffer_credentials);
276 void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg *msg,
277 uint64_t channel_key,
280 void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg *msg,
281 enum lttng_consumer_command cmd,
282 uint64_t channel_key, uint64_t net_seq_idx);
283 void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg *msg,
284 uint64_t channel_key,
286 const char *pathname,
289 unsigned int nb_init_streams,
290 enum lttng_event_output output,
292 uint64_t tracefile_size,
293 uint64_t tracefile_count,
294 unsigned int monitor,
295 unsigned int live_timer_interval,
296 bool is_in_live_session,
297 unsigned int monitor_timer_interval,
298 struct lttng_trace_chunk *trace_chunk);
299 int consumer_is_data_pending(uint64_t session_id,
300 struct consumer_output *consumer);
301 int consumer_close_metadata(struct consumer_socket *socket,
302 uint64_t metadata_key);
303 int consumer_setup_metadata(struct consumer_socket *socket,
304 uint64_t metadata_key);
305 int consumer_push_metadata(struct consumer_socket *socket,
306 uint64_t metadata_key, char *metadata_str, size_t len,
307 size_t target_offset, uint64_t version);
308 int consumer_flush_channel(struct consumer_socket *socket, uint64_t key);
309 int consumer_clear_quiescent_channel(struct consumer_socket *socket, uint64_t key);
310 int consumer_get_discarded_events(uint64_t session_id, uint64_t channel_key,
311 struct consumer_output *consumer, uint64_t *discarded);
312 int consumer_get_lost_packets(uint64_t session_id, uint64_t channel_key,
313 struct consumer_output *consumer, uint64_t *lost);
315 /* Snapshot command. */
316 enum lttng_error_code consumer_snapshot_channel(struct consumer_socket *socket,
317 uint64_t key, const struct consumer_output *output, int metadata,
318 const char *channel_path, uint64_t nb_packets_per_stream);
320 /* Rotation commands. */
321 int consumer_rotate_channel(struct consumer_socket *socket, uint64_t key,
322 struct consumer_output *output, bool is_metadata_channel);
323 int consumer_init(struct consumer_socket *socket,
324 const lttng_uuid sessiond_uuid);
326 int consumer_create_trace_chunk(struct consumer_socket *socket,
327 uint64_t relayd_id, uint64_t session_id,
328 struct lttng_trace_chunk *chunk,
329 const char *domain_subdir);
330 int consumer_close_trace_chunk(struct consumer_socket *socket,
331 uint64_t relayd_id, uint64_t session_id,
332 struct lttng_trace_chunk *chunk,
333 char *closed_trace_chunk_path);
334 int consumer_trace_chunk_exists(struct consumer_socket *socket,
335 uint64_t relayd_id, uint64_t session_id,
336 struct lttng_trace_chunk *chunk,
337 enum consumer_trace_chunk_exists_status *result);
338 int consumer_open_channel_packets(struct consumer_socket *socket, uint64_t key);
340 char *setup_channel_trace_path(struct consumer_output *consumer,
341 const char *session_path, size_t *consumer_path_offset);
344 int consumer_clear_channel(struct consumer_socket *socket, uint64_t key);
346 #endif /* _CONSUMER_H */