Propagate trace format to relayd on session creation
[deliverable/lttng-tools.git] / src / bin / lttng-sessiond / consumer.hpp
CommitLineData
f1e16794 1/*
ab5be9fa 2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
f1e16794 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
f1e16794 5 *
f1e16794
DG
6 */
7
8#ifndef _CONSUMER_H
9#define _CONSUMER_H
10
8696b40b 11#include <algorithm>
c9e313bc
SM
12#include <common/consumer/consumer.hpp>
13#include <common/hashtable/hashtable.hpp>
00e2e675 14#include <lttng/lttng.h>
8696b40b 15#include <lttng/trace-format-descriptor-internal.hpp>
6addfa37 16#include <urcu/ref.h>
00e2e675 17
c9e313bc 18#include "snapshot.hpp"
6dc3064a
DG
19
20struct snapshot;
21struct snapshot_output;
b178f53e 22struct ltt_session;
6dc3064a 23
7966af57
SM
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 */
30static_assert(__cplusplus == 201103L, "");
31template <typename T>
32constexpr T max_constexpr(T l, T r)
33{
34 return l > r ? l : r;
35}
36
00e2e675
DG
37enum consumer_dst_type {
38 CONSUMER_DST_LOCAL,
39 CONSUMER_DST_NET,
40};
f1e16794 41
d2956687
JG
42enum 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
173af62f 48struct consumer_socket {
4ce514c4
DG
49 /*
50 * File descriptor. This is just a reference to the consumer data meaning
9363801e
DG
51 * that every access must be locked and checked for a possible invalid
52 * value.
4ce514c4 53 */
9363801e 54 int *fd_ptr;
4ce514c4 55
173af62f
DG
56 /*
57 * To use this socket (send/recv), this lock MUST be acquired.
58 */
59 pthread_mutex_t *lock;
2f77fc4b
DG
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
ffe60014
DG
69 /* Flag if network sockets were sent to the consumer. */
70 unsigned int control_sock_sent;
71 unsigned int data_sock_sent;
72
173af62f 73 struct lttng_ht_node_ulong node;
6dc3064a
DG
74
75 enum lttng_consumer_type type;
173af62f
DG
76};
77
f1e16794 78struct consumer_data {
7966af57
SM
79 consumer_data (lttng_consumer_type type_)
80 : type(type_)
81 {}
82
f1e16794
DG
83 enum lttng_consumer_type type;
84
f1e16794 85 /* Mutex to control consumerd pid assignation */
7966af57
SM
86 pthread_mutex_t pid_mutex = PTHREAD_MUTEX_INITIALIZER;
87 pid_t pid = 0;
f1e16794 88
7966af57 89 int err_sock = -1;
331744e3 90 /* These two sockets uses the cmd_unix_sock_path. */
7966af57 91 int cmd_sock = -1;
e9404c27
JG
92 /*
93 * Write-end of the channel monitoring pipe to be passed to the
94 * consumer.
95 */
7966af57 96 int channel_monitor_pipe = -1;
4ce514c4
DG
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
9363801e
DG
100 * session daemon. For that reason, a variable for the fd is required and
101 * the metadata socket fd points to it.
4ce514c4 102 */
7966af57
SM
103 int metadata_fd = 0;
104 struct consumer_socket metadata_sock {};
f1e16794
DG
105
106 /* consumer error and command Unix socket path */
7966af57
SM
107 const char *err_unix_sock_path = nullptr;
108 const char *cmd_unix_sock_path = nullptr;
44a5e5eb 109
92db7cdc
DG
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 */
7966af57 115 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
f1e16794
DG
116};
117
00e2e675
DG
118/*
119 * Network URIs
120 */
121struct 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;
2b29c638
JD
139
140 /* <hostname>/<session-name> */
141 char base_dir[PATH_MAX];
00e2e675
DG
142};
143
144/*
145 * Consumer output object describing where and how to send data.
146 */
147struct consumer_output {
6addfa37
MD
148 struct urcu_ref ref; /* Refcount */
149
00e2e675
DG
150 /* If the consumer is enabled meaning that should be used */
151 unsigned int enabled;
152 enum consumer_dst_type type;
173af62f 153
00e2e675
DG
154 /*
155 * The net_seq_index is the index of the network stream on the consumer
3f8e211f
DG
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.
00e2e675 158 */
d88aee68 159 uint64_t net_seq_index;
b31610f2
JD
160 /* Store the relay protocol in use if the session is remote. */
161 uint32_t relay_major_version;
162 uint32_t relay_minor_version;
173af62f 163
eacb7b6f
MD
164 /* True if relayd supports the clear feature. */
165 bool relay_allows_clear;
166
00e2e675 167 /*
366a9222 168 * Subdirectory path name used for both local and network
d2956687 169 * consumer ("kernel", "ust", or empty).
00e2e675 170 */
7966af57
SM
171 char domain_subdir[
172 max_constexpr(sizeof(DEFAULT_KERNEL_TRACE_DIR),
b178f53e 173 sizeof(DEFAULT_UST_TRACE_DIR))];
173af62f
DG
174
175 /*
176 * Hashtable of consumer_socket index by the file descriptor value. For
b178f53e
JG
177 * multiarch consumer support, we can have more than one consumer (ex:
178 * 32 and 64 bit).
173af62f
DG
179 */
180 struct lttng_ht *socks;
181
7d2f7452
DG
182 /* Tell if this output is used for snapshot. */
183 unsigned int snapshot:1;
184
00e2e675 185 union {
61ba6b6d 186 char session_root_path[LTTNG_PATH_MAX];
00e2e675
DG
187 struct consumer_net net;
188 } dst;
366a9222
JD
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 */
61ba6b6d 194 char chunk_path[LTTNG_PATH_MAX];
00e2e675
DG
195};
196
173af62f 197struct consumer_socket *consumer_find_socket(int key,
348a81dc 198 const struct consumer_output *consumer);
7972aab2 199struct consumer_socket *consumer_find_socket_by_bitness(int bits,
348a81dc 200 const struct consumer_output *consumer);
4ce514c4 201struct consumer_socket *consumer_allocate_socket(int *fd);
173af62f
DG
202void consumer_add_socket(struct consumer_socket *sock,
203 struct consumer_output *consumer);
204void consumer_del_socket(struct consumer_socket *sock,
205 struct consumer_output *consumer);
206void consumer_destroy_socket(struct consumer_socket *sock);
6dc3064a
DG
207int consumer_copy_sockets(struct consumer_output *dst,
208 struct consumer_output *src);
af706bb7 209void consumer_destroy_output_sockets(struct consumer_output *obj);
04ed9e10 210int consumer_socket_send(struct consumer_socket *socket, const void *msg,
52898cb1
DG
211 size_t len);
212int consumer_socket_recv(struct consumer_socket *socket, void *msg,
213 size_t len);
173af62f 214
00e2e675
DG
215struct consumer_output *consumer_create_output(enum consumer_dst_type type);
216struct consumer_output *consumer_copy_output(struct consumer_output *obj);
6addfa37
MD
217void consumer_output_get(struct consumer_output *obj);
218void consumer_output_put(struct consumer_output *obj);
b178f53e
JG
219int consumer_set_network_uri(const struct ltt_session *session,
220 struct consumer_output *obj,
00e2e675 221 struct lttng_uri *uri);
ac2f30af
JG
222int consumer_send_fds(struct consumer_socket *sock, const int *fds,
223 size_t nb_fd);
ffe60014 224int consumer_send_msg(struct consumer_socket *sock,
04ed9e10 225 const struct lttcomm_consumer_msg *msg);
f50f23d9
DG
226int consumer_send_stream(struct consumer_socket *sock,
227 struct consumer_output *dst, struct lttcomm_consumer_msg *msg,
ac2f30af 228 const int *fds, size_t nb_fd);
8476ce3a 229int consumer_send_channel(struct consumer_socket *sock, struct lttcomm_consumer_msg *msg);
f50f23d9 230int consumer_send_relayd_socket(struct consumer_socket *consumer_sock,
8476ce3a
JR
231 struct lttcomm_relayd_sock *rsock,
232 struct consumer_output *consumer,
233 enum lttng_stream_type type,
234 const struct ltt_session& session,
235 const char *base_path_override,
236 const uint64_t *current_chunk_id);
237int consumer_send_channel_monitor_pipe(struct consumer_socket *consumer_sock, int pipe);
238int consumer_send_destroy_relayd(struct consumer_socket *sock, struct consumer_output *consumer);
f50f23d9 239int consumer_recv_status_reply(struct consumer_socket *sock);
ffe60014 240int consumer_recv_status_channel(struct consumer_socket *sock,
d88aee68 241 uint64_t *key, unsigned int *stream_count);
2f77fc4b 242void consumer_output_send_destroy_relayd(struct consumer_output *consumer);
a4b92340
DG
243int consumer_create_socket(struct consumer_data *data,
244 struct consumer_output *output);
37278a1e 245
ffe60014
DG
246void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg *msg,
247 uint64_t subbuf_size,
248 uint64_t num_subbuf,
249 int overwrite,
250 unsigned int switch_timer_interval,
251 unsigned int read_timer_interval,
ecc48a90 252 unsigned int live_timer_interval,
a2814ea7 253 bool is_in_live_session,
e9404c27 254 unsigned int monitor_timer_interval,
ffe60014
DG
255 int output,
256 int type,
257 uint64_t session_id,
258 const char *pathname,
259 const char *name,
d88aee68
DG
260 uint64_t relayd_id,
261 uint64_t key,
328c2fe7 262 const lttng_uuid& uuid,
1624d5b7
JD
263 uint32_t chan_id,
264 uint64_t tracefile_size,
2bba9e53 265 uint64_t tracefile_count,
1950109e 266 uint64_t session_id_per_pid,
567eb353 267 unsigned int monitor,
d7ba1388 268 uint32_t ust_app_uid,
491d1539 269 int64_t blocking_timeout,
3d071855 270 const char *root_shm_path,
e098433c 271 const char *shm_path,
1b0bebcb 272 struct lttng_trace_chunk *trace_chunk,
8696b40b
JR
273 const struct lttng_credentials *buffer_credentials,
274 const lttng::trace_format_descriptor& trace_format);
e098433c 275void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg *msg,
d88aee68
DG
276 uint64_t channel_key,
277 uint64_t stream_key,
d2956687 278 int32_t cpu);
a4baae1b
JD
279void 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);
638e7b4e 282void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg *msg,
d88aee68 283 uint64_t channel_key,
ffe60014
DG
284 uint64_t session_id,
285 const char *pathname,
d88aee68 286 uint64_t relayd_id,
c30aaa51 287 const char *name,
ffe60014
DG
288 unsigned int nb_init_streams,
289 enum lttng_event_output output,
1624d5b7
JD
290 int type,
291 uint64_t tracefile_size,
2bba9e53 292 uint64_t tracefile_count,
ecc48a90 293 unsigned int monitor,
e9404c27 294 unsigned int live_timer_interval,
a2814ea7 295 bool is_in_live_session,
d2956687 296 unsigned int monitor_timer_interval,
8696b40b
JR
297 struct lttng_trace_chunk *trace_chunk,
298 const lttng::trace_format_descriptor& trace_format);
d88aee68 299int consumer_is_data_pending(uint64_t session_id,
806e2684 300 struct consumer_output *consumer);
7972aab2
DG
301int consumer_close_metadata(struct consumer_socket *socket,
302 uint64_t metadata_key);
303int consumer_setup_metadata(struct consumer_socket *socket,
304 uint64_t metadata_key);
305int consumer_push_metadata(struct consumer_socket *socket,
306 uint64_t metadata_key, char *metadata_str, size_t len,
93ec662e 307 size_t target_offset, uint64_t version);
7972aab2 308int consumer_flush_channel(struct consumer_socket *socket, uint64_t key);
0dd01979 309int consumer_clear_quiescent_channel(struct consumer_socket *socket, uint64_t key);
fb83fe64
JD
310int consumer_get_discarded_events(uint64_t session_id, uint64_t channel_key,
311 struct consumer_output *consumer, uint64_t *discarded);
312int consumer_get_lost_packets(uint64_t session_id, uint64_t channel_key,
313 struct consumer_output *consumer, uint64_t *lost);
00e2e675 314
6dc3064a 315/* Snapshot command. */
9a654598 316enum lttng_error_code consumer_snapshot_channel(struct consumer_socket *socket,
348a81dc 317 uint64_t key, const struct consumer_output *output, int metadata,
f46376a1 318 const char *channel_path, uint64_t nb_packets_per_stream);
6dc3064a 319
92816cc3 320/* Rotation commands. */
5c408ad8 321int consumer_rotate_channel(struct consumer_socket *socket, uint64_t key,
f46376a1 322 struct consumer_output *output, bool is_metadata_channel);
09ede842 323int consumer_init(struct consumer_socket *socket,
328c2fe7 324 const lttng_uuid& sessiond_uuid);
a1ae2ea5 325
d2956687
JG
326int consumer_create_trace_chunk(struct consumer_socket *socket,
327 uint64_t relayd_id, uint64_t session_id,
5da88b0f
MD
328 struct lttng_trace_chunk *chunk,
329 const char *domain_subdir);
d2956687
JG
330int consumer_close_trace_chunk(struct consumer_socket *socket,
331 uint64_t relayd_id, uint64_t session_id,
ecd1a12f
MD
332 struct lttng_trace_chunk *chunk,
333 char *closed_trace_chunk_path);
d2956687
JG
334int 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);
04ed9e10 338int consumer_open_channel_packets(struct consumer_socket *socket, uint64_t key);
d2956687 339
3b967712 340char *setup_channel_trace_path(struct consumer_output *consumer,
5da88b0f 341 const char *session_path, size_t *consumer_path_offset);
3b967712 342
51a4828f
MD
343/* Clear command */
344int consumer_clear_channel(struct consumer_socket *socket, uint64_t key);
345
f1e16794 346#endif /* _CONSUMER_H */
This page took 0.114925 seconds and 5 git commands to generate.