clang-tidy: add readability-simplify-boolean-expr
[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
c9e313bc
SM
11#include <common/consumer/consumer.hpp>
12#include <common/hashtable/hashtable.hpp>
00e2e675 13#include <lttng/lttng.h>
6addfa37 14#include <urcu/ref.h>
7966af57 15#include <algorithm>
00e2e675 16
c9e313bc 17#include "snapshot.hpp"
6dc3064a
DG
18
19struct snapshot;
20struct snapshot_output;
b178f53e 21struct ltt_session;
6dc3064a 22
7966af57
SM
23/*
24 * Needed until we use C++14, where std::max is constexpr.
25 *
26 * Use a static_assert so we remember to remove it when we upgrade to a newer
27 * C++.
28 */
29static_assert(__cplusplus == 201103L, "");
30template <typename T>
31constexpr T max_constexpr(T l, T r)
32{
33 return l > r ? l : r;
34}
35
00e2e675
DG
36enum consumer_dst_type {
37 CONSUMER_DST_LOCAL,
38 CONSUMER_DST_NET,
39};
f1e16794 40
d2956687
JG
41enum 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,
45};
46
173af62f 47struct consumer_socket {
4ce514c4
DG
48 /*
49 * File descriptor. This is just a reference to the consumer data meaning
9363801e
DG
50 * that every access must be locked and checked for a possible invalid
51 * value.
4ce514c4 52 */
9363801e 53 int *fd_ptr;
4ce514c4 54
173af62f
DG
55 /*
56 * To use this socket (send/recv), this lock MUST be acquired.
57 */
58 pthread_mutex_t *lock;
2f77fc4b
DG
59
60 /*
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.
65 */
66 unsigned int registered;
67
ffe60014
DG
68 /* Flag if network sockets were sent to the consumer. */
69 unsigned int control_sock_sent;
70 unsigned int data_sock_sent;
71
173af62f 72 struct lttng_ht_node_ulong node;
6dc3064a
DG
73
74 enum lttng_consumer_type type;
173af62f
DG
75};
76
f1e16794 77struct consumer_data {
cd9adb8b 78 explicit consumer_data (lttng_consumer_type type_)
7966af57
SM
79 : type(type_)
80 {}
81
f1e16794
DG
82 enum lttng_consumer_type type;
83
f1e16794 84 /* Mutex to control consumerd pid assignation */
7966af57
SM
85 pthread_mutex_t pid_mutex = PTHREAD_MUTEX_INITIALIZER;
86 pid_t pid = 0;
f1e16794 87
7966af57 88 int err_sock = -1;
331744e3 89 /* These two sockets uses the cmd_unix_sock_path. */
7966af57 90 int cmd_sock = -1;
e9404c27
JG
91 /*
92 * Write-end of the channel monitoring pipe to be passed to the
93 * consumer.
94 */
7966af57 95 int channel_monitor_pipe = -1;
4ce514c4
DG
96 /*
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
9363801e
DG
99 * session daemon. For that reason, a variable for the fd is required and
100 * the metadata socket fd points to it.
4ce514c4 101 */
7966af57
SM
102 int metadata_fd = 0;
103 struct consumer_socket metadata_sock {};
f1e16794
DG
104
105 /* consumer error and command Unix socket path */
7966af57
SM
106 const char *err_unix_sock_path = nullptr;
107 const char *cmd_unix_sock_path = nullptr;
44a5e5eb 108
92db7cdc
DG
109 /*
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
112 * operations.
113 */
7966af57 114 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
f1e16794
DG
115};
116
00e2e675
DG
117/*
118 * Network URIs
119 */
120struct consumer_net {
121 /*
122 * Indicate if URI type is set. Those flags should only be set when the
123 * created URI is done AND valid.
124 */
125 int control_isset;
126 int data_isset;
127
128 /*
129 * The following two URIs MUST have the same destination address for
130 * network streaming to work. Network hop are not yet supported.
131 */
132
133 /* Control path for network streaming. */
134 struct lttng_uri control;
135
136 /* Data path for network streaming. */
137 struct lttng_uri data;
2b29c638
JD
138
139 /* <hostname>/<session-name> */
140 char base_dir[PATH_MAX];
00e2e675
DG
141};
142
143/*
144 * Consumer output object describing where and how to send data.
145 */
146struct consumer_output {
6addfa37
MD
147 struct urcu_ref ref; /* Refcount */
148
00e2e675
DG
149 /* If the consumer is enabled meaning that should be used */
150 unsigned int enabled;
151 enum consumer_dst_type type;
173af62f 152
00e2e675
DG
153 /*
154 * The net_seq_index is the index of the network stream on the consumer
3f8e211f
DG
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.
00e2e675 157 */
d88aee68 158 uint64_t net_seq_index;
b31610f2
JD
159 /* Store the relay protocol in use if the session is remote. */
160 uint32_t relay_major_version;
161 uint32_t relay_minor_version;
173af62f 162
eacb7b6f
MD
163 /* True if relayd supports the clear feature. */
164 bool relay_allows_clear;
165
00e2e675 166 /*
366a9222 167 * Subdirectory path name used for both local and network
d2956687 168 * consumer ("kernel", "ust", or empty).
00e2e675 169 */
7966af57
SM
170 char domain_subdir[
171 max_constexpr(sizeof(DEFAULT_KERNEL_TRACE_DIR),
b178f53e 172 sizeof(DEFAULT_UST_TRACE_DIR))];
173af62f
DG
173
174 /*
175 * Hashtable of consumer_socket index by the file descriptor value. For
b178f53e
JG
176 * multiarch consumer support, we can have more than one consumer (ex:
177 * 32 and 64 bit).
173af62f
DG
178 */
179 struct lttng_ht *socks;
180
7d2f7452
DG
181 /* Tell if this output is used for snapshot. */
182 unsigned int snapshot:1;
183
00e2e675 184 union {
61ba6b6d 185 char session_root_path[LTTNG_PATH_MAX];
00e2e675
DG
186 struct consumer_net net;
187 } dst;
366a9222
JD
188
189 /*
190 * Sub-directory below the session_root_path where the next chunk of
191 * trace will be stored (\0 before the first session rotation).
192 */
61ba6b6d 193 char chunk_path[LTTNG_PATH_MAX];
00e2e675
DG
194};
195
173af62f 196struct consumer_socket *consumer_find_socket(int key,
348a81dc 197 const struct consumer_output *consumer);
7972aab2 198struct consumer_socket *consumer_find_socket_by_bitness(int bits,
348a81dc 199 const struct consumer_output *consumer);
4ce514c4 200struct consumer_socket *consumer_allocate_socket(int *fd);
173af62f
DG
201void consumer_add_socket(struct consumer_socket *sock,
202 struct consumer_output *consumer);
203void consumer_del_socket(struct consumer_socket *sock,
204 struct consumer_output *consumer);
205void consumer_destroy_socket(struct consumer_socket *sock);
6dc3064a
DG
206int consumer_copy_sockets(struct consumer_output *dst,
207 struct consumer_output *src);
af706bb7 208void consumer_destroy_output_sockets(struct consumer_output *obj);
04ed9e10 209int consumer_socket_send(struct consumer_socket *socket, const void *msg,
52898cb1
DG
210 size_t len);
211int consumer_socket_recv(struct consumer_socket *socket, void *msg,
212 size_t len);
173af62f 213
00e2e675
DG
214struct consumer_output *consumer_create_output(enum consumer_dst_type type);
215struct consumer_output *consumer_copy_output(struct consumer_output *obj);
6addfa37
MD
216void consumer_output_get(struct consumer_output *obj);
217void consumer_output_put(struct consumer_output *obj);
b178f53e
JG
218int consumer_set_network_uri(const struct ltt_session *session,
219 struct consumer_output *obj,
00e2e675 220 struct lttng_uri *uri);
ac2f30af
JG
221int consumer_send_fds(struct consumer_socket *sock, const int *fds,
222 size_t nb_fd);
ffe60014 223int consumer_send_msg(struct consumer_socket *sock,
04ed9e10 224 const struct lttcomm_consumer_msg *msg);
f50f23d9
DG
225int consumer_send_stream(struct consumer_socket *sock,
226 struct consumer_output *dst, struct lttcomm_consumer_msg *msg,
ac2f30af 227 const int *fds, size_t nb_fd);
f50f23d9
DG
228int consumer_send_channel(struct consumer_socket *sock,
229 struct lttcomm_consumer_msg *msg);
230int consumer_send_relayd_socket(struct consumer_socket *consumer_sock,
6151a90f 231 struct lttcomm_relayd_sock *rsock, struct consumer_output *consumer,
d3e2ba59 232 enum lttng_stream_type type, uint64_t session_id,
fb9a95c4 233 const char *session_name, const char *hostname,
6fa5fe7c 234 const char *base_path, int session_live_timer,
46ef2188
MD
235 const uint64_t *current_chunk_id, time_t session_creation_time,
236 bool session_name_contains_creation_time);
e9404c27
JG
237int consumer_send_channel_monitor_pipe(struct consumer_socket *consumer_sock,
238 int pipe);
173af62f
DG
239int consumer_send_destroy_relayd(struct consumer_socket *sock,
240 struct consumer_output *consumer);
f50f23d9 241int consumer_recv_status_reply(struct consumer_socket *sock);
ffe60014 242int consumer_recv_status_channel(struct consumer_socket *sock,
d88aee68 243 uint64_t *key, unsigned int *stream_count);
2f77fc4b 244void consumer_output_send_destroy_relayd(struct consumer_output *consumer);
a4b92340
DG
245int consumer_create_socket(struct consumer_data *data,
246 struct consumer_output *output);
37278a1e 247
ffe60014
DG
248void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg *msg,
249 uint64_t subbuf_size,
250 uint64_t num_subbuf,
251 int overwrite,
252 unsigned int switch_timer_interval,
253 unsigned int read_timer_interval,
ecc48a90 254 unsigned int live_timer_interval,
a2814ea7 255 bool is_in_live_session,
e9404c27 256 unsigned int monitor_timer_interval,
ffe60014
DG
257 int output,
258 int type,
259 uint64_t session_id,
260 const char *pathname,
261 const char *name,
d88aee68
DG
262 uint64_t relayd_id,
263 uint64_t key,
328c2fe7 264 const lttng_uuid& uuid,
1624d5b7
JD
265 uint32_t chan_id,
266 uint64_t tracefile_size,
2bba9e53 267 uint64_t tracefile_count,
1950109e 268 uint64_t session_id_per_pid,
567eb353 269 unsigned int monitor,
d7ba1388 270 uint32_t ust_app_uid,
491d1539 271 int64_t blocking_timeout,
3d071855 272 const char *root_shm_path,
e098433c 273 const char *shm_path,
1b0bebcb
JG
274 struct lttng_trace_chunk *trace_chunk,
275 const struct lttng_credentials *buffer_credentials);
e098433c 276void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg *msg,
d88aee68
DG
277 uint64_t channel_key,
278 uint64_t stream_key,
d2956687 279 int32_t cpu);
a4baae1b
JD
280void 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);
638e7b4e 283void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg *msg,
d88aee68 284 uint64_t channel_key,
ffe60014
DG
285 uint64_t session_id,
286 const char *pathname,
d88aee68 287 uint64_t relayd_id,
c30aaa51 288 const char *name,
ffe60014
DG
289 unsigned int nb_init_streams,
290 enum lttng_event_output output,
1624d5b7
JD
291 int type,
292 uint64_t tracefile_size,
2bba9e53 293 uint64_t tracefile_count,
ecc48a90 294 unsigned int monitor,
e9404c27 295 unsigned int live_timer_interval,
a2814ea7 296 bool is_in_live_session,
d2956687
JG
297 unsigned int monitor_timer_interval,
298 struct lttng_trace_chunk *trace_chunk);
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.097926 seconds and 4 git commands to generate.