Commit | Line | Data |
---|---|---|
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 | ||
a0a4f314 | 11 | #include "consumer-destination-type.hpp" |
28f23191 JG |
12 | #include "snapshot.hpp" |
13 | ||
c9e313bc SM |
14 | #include <common/consumer/consumer.hpp> |
15 | #include <common/hashtable/hashtable.hpp> | |
28f23191 | 16 | |
00e2e675 DG |
17 | #include <lttng/lttng.h> |
18 | ||
28f23191 JG |
19 | #include <algorithm> |
20 | #include <urcu/ref.h> | |
6dc3064a DG |
21 | |
22 | struct snapshot; | |
23 | struct snapshot_output; | |
24 | ||
7966af57 SM |
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 | */ | |
31 | static_assert(__cplusplus == 201103L, ""); | |
31375c42 JG |
32 | template <typename NumericalType> |
33 | constexpr NumericalType max_constexpr(NumericalType l, NumericalType r) | |
7966af57 SM |
34 | { |
35 | return l > r ? l : r; | |
36 | } | |
37 | ||
d2956687 JG |
38 | enum 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 | ||
173af62f | 44 | struct consumer_socket { |
4ce514c4 DG |
45 | /* |
46 | * File descriptor. This is just a reference to the consumer data meaning | |
9363801e DG |
47 | * that every access must be locked and checked for a possible invalid |
48 | * value. | |
4ce514c4 | 49 | */ |
9363801e | 50 | int *fd_ptr; |
4ce514c4 | 51 | |
173af62f DG |
52 | /* |
53 | * To use this socket (send/recv), this lock MUST be acquired. | |
54 | */ | |
55 | pthread_mutex_t *lock; | |
2f77fc4b DG |
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 | ||
ffe60014 DG |
65 | /* Flag if network sockets were sent to the consumer. */ |
66 | unsigned int control_sock_sent; | |
67 | unsigned int data_sock_sent; | |
68 | ||
173af62f | 69 | struct lttng_ht_node_ulong node; |
6dc3064a DG |
70 | |
71 | enum lttng_consumer_type type; | |
173af62f DG |
72 | }; |
73 | ||
f1e16794 | 74 | struct consumer_data { |
28f23191 JG |
75 | explicit consumer_data(lttng_consumer_type type_) : type(type_) |
76 | { | |
77 | } | |
7966af57 | 78 | |
f1e16794 DG |
79 | enum lttng_consumer_type type; |
80 | ||
f1e16794 | 81 | /* Mutex to control consumerd pid assignation */ |
7966af57 SM |
82 | pthread_mutex_t pid_mutex = PTHREAD_MUTEX_INITIALIZER; |
83 | pid_t pid = 0; | |
f1e16794 | 84 | |
7966af57 | 85 | int err_sock = -1; |
331744e3 | 86 | /* These two sockets uses the cmd_unix_sock_path. */ |
7966af57 | 87 | int cmd_sock = -1; |
e9404c27 JG |
88 | /* |
89 | * Write-end of the channel monitoring pipe to be passed to the | |
90 | * consumer. | |
91 | */ | |
7966af57 | 92 | int channel_monitor_pipe = -1; |
4ce514c4 DG |
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 | |
9363801e DG |
96 | * session daemon. For that reason, a variable for the fd is required and |
97 | * the metadata socket fd points to it. | |
4ce514c4 | 98 | */ |
7966af57 SM |
99 | int metadata_fd = 0; |
100 | struct consumer_socket metadata_sock {}; | |
f1e16794 DG |
101 | |
102 | /* consumer error and command Unix socket path */ | |
7966af57 SM |
103 | const char *err_unix_sock_path = nullptr; |
104 | const char *cmd_unix_sock_path = nullptr; | |
44a5e5eb | 105 | |
92db7cdc DG |
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 | */ | |
7966af57 | 111 | pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; |
f1e16794 DG |
112 | }; |
113 | ||
00e2e675 DG |
114 | /* |
115 | * Network URIs | |
116 | */ | |
117 | struct 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; | |
2b29c638 JD |
135 | |
136 | /* <hostname>/<session-name> */ | |
137 | char base_dir[PATH_MAX]; | |
00e2e675 DG |
138 | }; |
139 | ||
140 | /* | |
141 | * Consumer output object describing where and how to send data. | |
142 | */ | |
143 | struct consumer_output { | |
28f23191 | 144 | struct urcu_ref ref; /* Refcount */ |
6addfa37 | 145 | |
00e2e675 | 146 | /* If the consumer is enabled meaning that should be used */ |
66cefebd | 147 | bool enabled; |
00e2e675 | 148 | enum consumer_dst_type type; |
173af62f | 149 | |
00e2e675 DG |
150 | /* |
151 | * The net_seq_index is the index of the network stream on the consumer | |
3f8e211f DG |
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. | |
00e2e675 | 154 | */ |
d88aee68 | 155 | uint64_t net_seq_index; |
b31610f2 JD |
156 | /* Store the relay protocol in use if the session is remote. */ |
157 | uint32_t relay_major_version; | |
158 | uint32_t relay_minor_version; | |
173af62f | 159 | |
eacb7b6f MD |
160 | /* True if relayd supports the clear feature. */ |
161 | bool relay_allows_clear; | |
162 | ||
00e2e675 | 163 | /* |
366a9222 | 164 | * Subdirectory path name used for both local and network |
d2956687 | 165 | * consumer ("kernel", "ust", or empty). |
00e2e675 | 166 | */ |
28f23191 JG |
167 | char domain_subdir[max_constexpr(sizeof(DEFAULT_KERNEL_TRACE_DIR), |
168 | sizeof(DEFAULT_UST_TRACE_DIR))]; | |
173af62f DG |
169 | |
170 | /* | |
171 | * Hashtable of consumer_socket index by the file descriptor value. For | |
b178f53e JG |
172 | * multiarch consumer support, we can have more than one consumer (ex: |
173 | * 32 and 64 bit). | |
173af62f DG |
174 | */ |
175 | struct lttng_ht *socks; | |
176 | ||
7d2f7452 DG |
177 | /* Tell if this output is used for snapshot. */ |
178 | unsigned int snapshot:1; | |
179 | ||
00e2e675 | 180 | union { |
61ba6b6d | 181 | char session_root_path[LTTNG_PATH_MAX]; |
00e2e675 DG |
182 | struct consumer_net net; |
183 | } dst; | |
366a9222 JD |
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 | */ | |
61ba6b6d | 189 | char chunk_path[LTTNG_PATH_MAX]; |
00e2e675 DG |
190 | }; |
191 | ||
28f23191 | 192 | struct consumer_socket *consumer_find_socket(int key, const struct consumer_output *consumer); |
7972aab2 | 193 | struct consumer_socket *consumer_find_socket_by_bitness(int bits, |
28f23191 | 194 | const struct consumer_output *consumer); |
4ce514c4 | 195 | struct consumer_socket *consumer_allocate_socket(int *fd); |
28f23191 JG |
196 | void consumer_add_socket(struct consumer_socket *sock, struct consumer_output *consumer); |
197 | void consumer_del_socket(struct consumer_socket *sock, struct consumer_output *consumer); | |
173af62f | 198 | void consumer_destroy_socket(struct consumer_socket *sock); |
28f23191 | 199 | int consumer_copy_sockets(struct consumer_output *dst, struct consumer_output *src); |
af706bb7 | 200 | void consumer_destroy_output_sockets(struct consumer_output *obj); |
28f23191 JG |
201 | int consumer_socket_send(struct consumer_socket *socket, const void *msg, size_t len); |
202 | int consumer_socket_recv(struct consumer_socket *socket, void *msg, size_t len); | |
173af62f | 203 | |
00e2e675 DG |
204 | struct consumer_output *consumer_create_output(enum consumer_dst_type type); |
205 | struct consumer_output *consumer_copy_output(struct consumer_output *obj); | |
6addfa37 MD |
206 | void consumer_output_get(struct consumer_output *obj); |
207 | void consumer_output_put(struct consumer_output *obj); | |
a0a4f314 | 208 | |
28f23191 JG |
209 | int consumer_send_fds(struct consumer_socket *sock, const int *fds, size_t nb_fd); |
210 | int consumer_send_msg(struct consumer_socket *sock, const struct lttcomm_consumer_msg *msg); | |
f50f23d9 | 211 | int consumer_send_stream(struct consumer_socket *sock, |
28f23191 JG |
212 | struct consumer_output *dst, |
213 | struct lttcomm_consumer_msg *msg, | |
214 | const int *fds, | |
215 | size_t nb_fd); | |
216 | int consumer_send_channel(struct consumer_socket *sock, struct lttcomm_consumer_msg *msg); | |
f50f23d9 | 217 | int consumer_send_relayd_socket(struct consumer_socket *consumer_sock, |
28f23191 JG |
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); | |
229 | int consumer_send_channel_monitor_pipe(struct consumer_socket *consumer_sock, int pipe); | |
230 | int consumer_send_destroy_relayd(struct consumer_socket *sock, struct consumer_output *consumer); | |
f50f23d9 | 231 | int consumer_recv_status_reply(struct consumer_socket *sock); |
ffe60014 | 232 | int consumer_recv_status_channel(struct consumer_socket *sock, |
28f23191 JG |
233 | uint64_t *key, |
234 | unsigned int *stream_count); | |
2f77fc4b | 235 | void consumer_output_send_destroy_relayd(struct consumer_output *consumer); |
28f23191 | 236 | int consumer_create_socket(struct consumer_data *data, struct consumer_output *output); |
37278a1e | 237 | |
ffe60014 | 238 | void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg *msg, |
28f23191 JG |
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); | |
e098433c | 266 | void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg *msg, |
28f23191 JG |
267 | uint64_t channel_key, |
268 | uint64_t stream_key, | |
269 | int32_t cpu); | |
a4baae1b | 270 | void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg *msg, |
28f23191 JG |
271 | enum lttng_consumer_command cmd, |
272 | uint64_t channel_key, | |
273 | uint64_t net_seq_idx); | |
638e7b4e | 274 | void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg *msg, |
28f23191 JG |
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); | |
290 | int consumer_is_data_pending(uint64_t session_id, struct consumer_output *consumer); | |
291 | int consumer_close_metadata(struct consumer_socket *socket, uint64_t metadata_key); | |
292 | int consumer_setup_metadata(struct consumer_socket *socket, uint64_t metadata_key); | |
7972aab2 | 293 | int consumer_push_metadata(struct consumer_socket *socket, |
28f23191 JG |
294 | uint64_t metadata_key, |
295 | char *metadata_str, | |
296 | size_t len, | |
297 | size_t target_offset, | |
298 | uint64_t version); | |
7972aab2 | 299 | int consumer_flush_channel(struct consumer_socket *socket, uint64_t key); |
0dd01979 | 300 | int consumer_clear_quiescent_channel(struct consumer_socket *socket, uint64_t key); |
28f23191 JG |
301 | int consumer_get_discarded_events(uint64_t session_id, |
302 | uint64_t channel_key, | |
303 | struct consumer_output *consumer, | |
304 | uint64_t *discarded); | |
305 | int consumer_get_lost_packets(uint64_t session_id, | |
306 | uint64_t channel_key, | |
307 | struct consumer_output *consumer, | |
308 | uint64_t *lost); | |
00e2e675 | 309 | |
6dc3064a | 310 | /* Snapshot command. */ |
9a654598 | 311 | enum lttng_error_code consumer_snapshot_channel(struct consumer_socket *socket, |
28f23191 JG |
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); | |
6dc3064a | 317 | |
92816cc3 | 318 | /* Rotation commands. */ |
28f23191 JG |
319 | int consumer_rotate_channel(struct consumer_socket *socket, |
320 | uint64_t key, | |
321 | struct consumer_output *output, | |
322 | bool is_metadata_channel); | |
323 | int consumer_init(struct consumer_socket *socket, const lttng_uuid& sessiond_uuid); | |
a1ae2ea5 | 324 | |
d2956687 | 325 | int consumer_create_trace_chunk(struct consumer_socket *socket, |
28f23191 JG |
326 | uint64_t relayd_id, |
327 | uint64_t session_id, | |
328 | struct lttng_trace_chunk *chunk, | |
329 | const char *domain_subdir); | |
d2956687 | 330 | int consumer_close_trace_chunk(struct consumer_socket *socket, |
28f23191 JG |
331 | uint64_t relayd_id, |
332 | uint64_t session_id, | |
333 | struct lttng_trace_chunk *chunk, | |
334 | char *closed_trace_chunk_path); | |
d2956687 | 335 | int consumer_trace_chunk_exists(struct consumer_socket *socket, |
28f23191 JG |
336 | uint64_t relayd_id, |
337 | uint64_t session_id, | |
338 | struct lttng_trace_chunk *chunk, | |
339 | enum consumer_trace_chunk_exists_status *result); | |
04ed9e10 | 340 | int consumer_open_channel_packets(struct consumer_socket *socket, uint64_t key); |
d2956687 | 341 | |
3b967712 | 342 | char *setup_channel_trace_path(struct consumer_output *consumer, |
28f23191 JG |
343 | const char *session_path, |
344 | size_t *consumer_path_offset); | |
3b967712 | 345 | |
51a4828f MD |
346 | /* Clear command */ |
347 | int consumer_clear_channel(struct consumer_socket *socket, uint64_t key); | |
348 | ||
f1e16794 | 349 | #endif /* _CONSUMER_H */ |