Propagate trace format all the way to the consumer
[lttng-tools.git] / src / bin / lttng-sessiond / consumer.hpp
1 /*
2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #ifndef _CONSUMER_H
9 #define _CONSUMER_H
10
11 #include <algorithm>
12 #include <common/consumer/consumer.hpp>
13 #include <common/hashtable/hashtable.hpp>
14 #include <lttng/lttng.h>
15 #include <lttng/trace-format-descriptor-internal.hpp>
16 #include <urcu/ref.h>
17
18 #include "snapshot.hpp"
19
20 struct snapshot;
21 struct snapshot_output;
22 struct ltt_session;
23
24 /*
25 * Needed until we use C++14, where std::max is constexpr.
26 *
27 * Use a static_assert so we remember to remove it when we upgrade to a newer
28 * C++.
29 */
30 static_assert(__cplusplus == 201103L, "");
31 template <typename T>
32 constexpr T max_constexpr(T l, T r)
33 {
34 return l > r ? l : r;
35 }
36
37 enum consumer_dst_type {
38 CONSUMER_DST_LOCAL,
39 CONSUMER_DST_NET,
40 };
41
42 enum consumer_trace_chunk_exists_status {
43 CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL,
44 CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE,
45 CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK,
46 };
47
48 struct consumer_socket {
49 /*
50 * File descriptor. This is just a reference to the consumer data meaning
51 * that every access must be locked and checked for a possible invalid
52 * value.
53 */
54 int *fd_ptr;
55
56 /*
57 * To use this socket (send/recv), this lock MUST be acquired.
58 */
59 pthread_mutex_t *lock;
60
61 /*
62 * Indicates if the socket was registered by a third part
63 * (REGISTER_CONSUMER) or is the spawn consumer of the session daemon.
64 * During the destroy phase of a consumer output, we close the socket if
65 * this flag is set to 1 since we don't need the fd anymore.
66 */
67 unsigned int registered;
68
69 /* Flag if network sockets were sent to the consumer. */
70 unsigned int control_sock_sent;
71 unsigned int data_sock_sent;
72
73 struct lttng_ht_node_ulong node;
74
75 enum lttng_consumer_type type;
76 };
77
78 struct consumer_data {
79 consumer_data (lttng_consumer_type type_)
80 : type(type_)
81 {}
82
83 enum lttng_consumer_type type;
84
85 /* Mutex to control consumerd pid assignation */
86 pthread_mutex_t pid_mutex = PTHREAD_MUTEX_INITIALIZER;
87 pid_t pid = 0;
88
89 int err_sock = -1;
90 /* These two sockets uses the cmd_unix_sock_path. */
91 int cmd_sock = -1;
92 /*
93 * Write-end of the channel monitoring pipe to be passed to the
94 * consumer.
95 */
96 int channel_monitor_pipe = -1;
97 /*
98 * The metadata socket object is handled differently and only created
99 * locally in this object thus it's the only reference available in the
100 * session daemon. For that reason, a variable for the fd is required and
101 * the metadata socket fd points to it.
102 */
103 int metadata_fd = 0;
104 struct consumer_socket metadata_sock {};
105
106 /* consumer error and command Unix socket path */
107 const char *err_unix_sock_path = nullptr;
108 const char *cmd_unix_sock_path = nullptr;
109
110 /*
111 * This lock has two purposes. It protects any change to the consumer
112 * socket and make sure only one thread uses this object for read/write
113 * operations.
114 */
115 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
116 };
117
118 /*
119 * Network URIs
120 */
121 struct consumer_net {
122 /*
123 * Indicate if URI type is set. Those flags should only be set when the
124 * created URI is done AND valid.
125 */
126 int control_isset;
127 int data_isset;
128
129 /*
130 * The following two URIs MUST have the same destination address for
131 * network streaming to work. Network hop are not yet supported.
132 */
133
134 /* Control path for network streaming. */
135 struct lttng_uri control;
136
137 /* Data path for network streaming. */
138 struct lttng_uri data;
139
140 /* <hostname>/<session-name> */
141 char base_dir[PATH_MAX];
142 };
143
144 /*
145 * Consumer output object describing where and how to send data.
146 */
147 struct consumer_output {
148 struct urcu_ref ref; /* Refcount */
149
150 /* If the consumer is enabled meaning that should be used */
151 unsigned int enabled;
152 enum consumer_dst_type type;
153
154 /*
155 * The net_seq_index is the index of the network stream on the consumer
156 * side. It tells the consumer which streams goes to which relayd with this
157 * index. The relayd sockets are index with it on the consumer side.
158 */
159 uint64_t net_seq_index;
160 /* Store the relay protocol in use if the session is remote. */
161 uint32_t relay_major_version;
162 uint32_t relay_minor_version;
163
164 /* True if relayd supports the clear feature. */
165 bool relay_allows_clear;
166
167 /*
168 * Subdirectory path name used for both local and network
169 * consumer ("kernel", "ust", or empty).
170 */
171 char domain_subdir[
172 max_constexpr(sizeof(DEFAULT_KERNEL_TRACE_DIR),
173 sizeof(DEFAULT_UST_TRACE_DIR))];
174
175 /*
176 * Hashtable of consumer_socket index by the file descriptor value. For
177 * multiarch consumer support, we can have more than one consumer (ex:
178 * 32 and 64 bit).
179 */
180 struct lttng_ht *socks;
181
182 /* Tell if this output is used for snapshot. */
183 unsigned int snapshot:1;
184
185 union {
186 char session_root_path[LTTNG_PATH_MAX];
187 struct consumer_net net;
188 } dst;
189
190 /*
191 * Sub-directory below the session_root_path where the next chunk of
192 * trace will be stored (\0 before the first session rotation).
193 */
194 char chunk_path[LTTNG_PATH_MAX];
195 };
196
197 struct consumer_socket *consumer_find_socket(int key,
198 const struct consumer_output *consumer);
199 struct consumer_socket *consumer_find_socket_by_bitness(int bits,
200 const struct consumer_output *consumer);
201 struct consumer_socket *consumer_allocate_socket(int *fd);
202 void consumer_add_socket(struct consumer_socket *sock,
203 struct consumer_output *consumer);
204 void consumer_del_socket(struct consumer_socket *sock,
205 struct consumer_output *consumer);
206 void consumer_destroy_socket(struct consumer_socket *sock);
207 int consumer_copy_sockets(struct consumer_output *dst,
208 struct consumer_output *src);
209 void consumer_destroy_output_sockets(struct consumer_output *obj);
210 int consumer_socket_send(struct consumer_socket *socket, const void *msg,
211 size_t len);
212 int consumer_socket_recv(struct consumer_socket *socket, void *msg,
213 size_t len);
214
215 struct consumer_output *consumer_create_output(enum consumer_dst_type type);
216 struct consumer_output *consumer_copy_output(struct consumer_output *obj);
217 void consumer_output_get(struct consumer_output *obj);
218 void consumer_output_put(struct consumer_output *obj);
219 int consumer_set_network_uri(const struct ltt_session *session,
220 struct consumer_output *obj,
221 struct lttng_uri *uri);
222 int consumer_send_fds(struct consumer_socket *sock, const int *fds,
223 size_t nb_fd);
224 int consumer_send_msg(struct consumer_socket *sock,
225 const struct lttcomm_consumer_msg *msg);
226 int consumer_send_stream(struct consumer_socket *sock,
227 struct consumer_output *dst, struct lttcomm_consumer_msg *msg,
228 const int *fds, size_t nb_fd);
229 int consumer_send_channel(struct consumer_socket *sock,
230 struct lttcomm_consumer_msg *msg);
231 int consumer_send_relayd_socket(struct consumer_socket *consumer_sock,
232 struct lttcomm_relayd_sock *rsock, struct consumer_output *consumer,
233 enum lttng_stream_type type, uint64_t session_id,
234 const char *session_name, const char *hostname,
235 const char *base_path, int session_live_timer,
236 const uint64_t *current_chunk_id, time_t session_creation_time,
237 bool session_name_contains_creation_time);
238 int consumer_send_channel_monitor_pipe(struct consumer_socket *consumer_sock,
239 int pipe);
240 int consumer_send_destroy_relayd(struct consumer_socket *sock,
241 struct consumer_output *consumer);
242 int consumer_recv_status_reply(struct consumer_socket *sock);
243 int consumer_recv_status_channel(struct consumer_socket *sock,
244 uint64_t *key, unsigned int *stream_count);
245 void consumer_output_send_destroy_relayd(struct consumer_output *consumer);
246 int consumer_create_socket(struct consumer_data *data,
247 struct consumer_output *output);
248
249 void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg *msg,
250 uint64_t subbuf_size,
251 uint64_t num_subbuf,
252 int overwrite,
253 unsigned int switch_timer_interval,
254 unsigned int read_timer_interval,
255 unsigned int live_timer_interval,
256 bool is_in_live_session,
257 unsigned int monitor_timer_interval,
258 int output,
259 int type,
260 uint64_t session_id,
261 const char *pathname,
262 const char *name,
263 uint64_t relayd_id,
264 uint64_t key,
265 const lttng_uuid& uuid,
266 uint32_t chan_id,
267 uint64_t tracefile_size,
268 uint64_t tracefile_count,
269 uint64_t session_id_per_pid,
270 unsigned int monitor,
271 uint32_t ust_app_uid,
272 int64_t blocking_timeout,
273 const char *root_shm_path,
274 const char *shm_path,
275 struct lttng_trace_chunk *trace_chunk,
276 const struct lttng_credentials *buffer_credentials,
277 const lttng::trace_format_descriptor& trace_format);
278 void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg *msg,
279 uint64_t channel_key,
280 uint64_t stream_key,
281 int32_t cpu);
282 void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg *msg,
283 enum lttng_consumer_command cmd,
284 uint64_t channel_key, uint64_t net_seq_idx);
285 void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg *msg,
286 uint64_t channel_key,
287 uint64_t session_id,
288 const char *pathname,
289 uint64_t relayd_id,
290 const char *name,
291 unsigned int nb_init_streams,
292 enum lttng_event_output output,
293 int type,
294 uint64_t tracefile_size,
295 uint64_t tracefile_count,
296 unsigned int monitor,
297 unsigned int live_timer_interval,
298 bool is_in_live_session,
299 unsigned int monitor_timer_interval,
300 struct lttng_trace_chunk *trace_chunk,
301 const lttng::trace_format_descriptor& trace_format);
302 int consumer_is_data_pending(uint64_t session_id,
303 struct consumer_output *consumer);
304 int consumer_close_metadata(struct consumer_socket *socket,
305 uint64_t metadata_key);
306 int consumer_setup_metadata(struct consumer_socket *socket,
307 uint64_t metadata_key);
308 int consumer_push_metadata(struct consumer_socket *socket,
309 uint64_t metadata_key, char *metadata_str, size_t len,
310 size_t target_offset, uint64_t version);
311 int consumer_flush_channel(struct consumer_socket *socket, uint64_t key);
312 int consumer_clear_quiescent_channel(struct consumer_socket *socket, uint64_t key);
313 int consumer_get_discarded_events(uint64_t session_id, uint64_t channel_key,
314 struct consumer_output *consumer, uint64_t *discarded);
315 int consumer_get_lost_packets(uint64_t session_id, uint64_t channel_key,
316 struct consumer_output *consumer, uint64_t *lost);
317
318 /* Snapshot command. */
319 enum lttng_error_code consumer_snapshot_channel(struct consumer_socket *socket,
320 uint64_t key, const struct consumer_output *output, int metadata,
321 const char *channel_path, uint64_t nb_packets_per_stream);
322
323 /* Rotation commands. */
324 int consumer_rotate_channel(struct consumer_socket *socket, uint64_t key,
325 struct consumer_output *output, bool is_metadata_channel);
326 int consumer_init(struct consumer_socket *socket,
327 const lttng_uuid& sessiond_uuid);
328
329 int consumer_create_trace_chunk(struct consumer_socket *socket,
330 uint64_t relayd_id, uint64_t session_id,
331 struct lttng_trace_chunk *chunk,
332 const char *domain_subdir);
333 int consumer_close_trace_chunk(struct consumer_socket *socket,
334 uint64_t relayd_id, uint64_t session_id,
335 struct lttng_trace_chunk *chunk,
336 char *closed_trace_chunk_path);
337 int consumer_trace_chunk_exists(struct consumer_socket *socket,
338 uint64_t relayd_id, uint64_t session_id,
339 struct lttng_trace_chunk *chunk,
340 enum consumer_trace_chunk_exists_status *result);
341 int consumer_open_channel_packets(struct consumer_socket *socket, uint64_t key);
342
343 char *setup_channel_trace_path(struct consumer_output *consumer,
344 const char *session_path, size_t *consumer_path_offset);
345
346 /* Clear command */
347 int consumer_clear_channel(struct consumer_socket *socket, uint64_t key);
348
349 #endif /* _CONSUMER_H */
This page took 0.038043 seconds and 5 git commands to generate.