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