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