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