Fix: relayd metadata size
[lttng-tools.git] / src / common / consumer.h
CommitLineData
3bd1e081
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
00e2e675
DG
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2012 - David Goulet <dgoulet@efficios.com>
3bd1e081 5 *
d14d33bf
AM
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
3bd1e081 9 *
d14d33bf
AM
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
3bd1e081 14 *
d14d33bf
AM
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3bd1e081
MD
18 */
19
20#ifndef _LTTNG_CONSUMER_H
21#define _LTTNG_CONSUMER_H
22
23#include <limits.h>
24#include <poll.h>
6df2e2c9 25#include <unistd.h>
e4421fec 26
3bd1e081 27#include <lttng/lttng.h>
10a8a223 28
b9182dd9
DG
29#include <common/hashtable/hashtable.h>
30#include <common/compat/fcntl.h>
00e2e675 31#include <common/sessiond-comm/sessiond-comm.h>
3bd1e081
MD
32
33/*
34 * When the receiving thread dies, we need to have a way to make the polling
35 * thread exit eventually. If all FDs hang up (normal case when the
32258573
DG
36 * lttng-sessiond stops), we can exit cleanly, but if there is a problem and
37 * for whatever reason some FDs remain open, the consumer should still exit
3bd1e081
MD
38 * eventually.
39 *
40 * If the timeout is reached, it means that during this period no events
41 * occurred on the FDs so we need to force an exit. This case should not happen
42 * but it is a safety to ensure we won't block the consumer indefinitely.
43 *
44 * The value of 2 seconds is an arbitrary choice.
45 */
46#define LTTNG_CONSUMER_POLL_TIMEOUT 2000
47
48/* Commands for consumer */
49enum lttng_consumer_command {
50 LTTNG_CONSUMER_ADD_CHANNEL,
51 LTTNG_CONSUMER_ADD_STREAM,
52 /* pause, delete, active depending on fd state */
53 LTTNG_CONSUMER_UPDATE_STREAM,
54 /* inform the consumer to quit when all fd has hang up */
55 LTTNG_CONSUMER_STOP,
00e2e675 56 LTTNG_CONSUMER_ADD_RELAYD_SOCKET,
3bd1e081
MD
57};
58
59/* State of each fd in consumer */
60enum lttng_consumer_stream_state {
61 LTTNG_CONSUMER_ACTIVE_STREAM,
62 LTTNG_CONSUMER_PAUSE_STREAM,
63 LTTNG_CONSUMER_DELETE_STREAM,
64};
65
3bd1e081
MD
66enum lttng_consumer_type {
67 LTTNG_CONSUMER_UNKNOWN = 0,
68 LTTNG_CONSUMER_KERNEL,
7753dea8
MD
69 LTTNG_CONSUMER64_UST,
70 LTTNG_CONSUMER32_UST,
3bd1e081
MD
71};
72
73struct lttng_consumer_channel {
e4421fec 74 struct lttng_ht_node_ulong node;
3bd1e081
MD
75 int key;
76 uint64_t max_sb_size; /* the subbuffer size for this channel */
77 int refcount; /* Number of streams referencing this channel */
78 /* For UST */
79 int shm_fd;
80 int wait_fd;
81 void *mmap_base;
82 size_t mmap_len;
13161846 83 struct lttng_ust_shm_handle *handle;
3bd1e081 84 int nr_streams;
b5c5fc29 85 int wait_fd_is_copy;
5af2f756 86 int cpucount;
3bd1e081
MD
87};
88
89/* Forward declaration for UST. */
13161846 90struct lttng_ust_lib_ring_buffer;
3bd1e081
MD
91
92/*
93 * Internal representation of the streams, sessiond_key is used to identify
94 * uniquely a stream.
95 */
96struct lttng_consumer_stream {
e4421fec 97 struct lttng_ht_node_ulong node;
00e2e675 98 struct lttng_ht_node_ulong waitfd_node;
3bd1e081
MD
99 struct lttng_consumer_channel *chan; /* associated channel */
100 /*
101 * key is the key used by the session daemon to refer to the
102 * object in the consumer daemon.
103 */
104 int key;
105 int shm_fd;
106 int wait_fd;
107 int out_fd; /* output file to write the data */
108 off_t out_fd_offset; /* write position in the output file descriptor */
109 char path_name[PATH_MAX]; /* tracefile name */
110 enum lttng_consumer_stream_state state;
111 size_t shm_len;
112 void *mmap_base;
113 size_t mmap_len;
114 enum lttng_event_output output; /* splice or mmap */
b5c5fc29
MD
115 int shm_fd_is_copy;
116 int wait_fd_is_copy;
3bd1e081 117 /* For UST */
13161846 118 struct lttng_ust_lib_ring_buffer *buf;
3bd1e081 119 int cpu;
4078b776 120 int data_read;
d056b477 121 int hangup_flush_done;
6df2e2c9
MD
122 /* UID/GID of the user owning the session to which stream belongs */
123 uid_t uid;
124 gid_t gid;
00e2e675
DG
125 /* Network sequence number. Indicating on which relayd socket it goes. */
126 int net_seq_idx;
127 /* Identify if the stream is the metadata */
128 unsigned int metadata_flag;
129 /* Used when the stream is set for network streaming */
130 uint64_t relayd_stream_id;
131};
132
133/*
134 * Internal representation of a relayd socket pair.
135 */
136struct consumer_relayd_sock_pair {
137 /* Network sequence number. */
138 int net_seq_idx;
139 /* Number of stream associated with this relayd */
140 unsigned int refcount;
51d7db73
DG
141 /*
142 * Mutex protecting the control socket to avoid out of order packets
143 * between threads sending data to the relayd. Since metadata data is sent
144 * over that socket, at least two sendmsg() are needed (header + data)
145 * creating a race for packets to overlap between threads using it.
146 */
00e2e675 147 pthread_mutex_t ctrl_sock_mutex;
51d7db73
DG
148
149 /* Control socket. Command and metadata are passed over it */
00e2e675 150 struct lttcomm_sock control_sock;
51d7db73
DG
151
152 /*
153 * We don't need a mutex at this point since we only splice or write single
154 * large chunk of data with a header appended at the begining. Moreover,
155 * this socket is for now only used in a single thread.
156 */
00e2e675
DG
157 struct lttcomm_sock data_sock;
158 struct lttng_ht_node_ulong node;
3bd1e081
MD
159};
160
161/*
162 * UST consumer local data to the program. One or more instance per
163 * process.
164 */
165struct lttng_consumer_local_data {
4078b776
MD
166 /*
167 * Function to call when data is available on a buffer.
168 * Returns the number of bytes read, or negative error value.
169 */
170 ssize_t (*on_buffer_ready)(struct lttng_consumer_stream *stream,
d41f73b7 171 struct lttng_consumer_local_data *ctx);
3bd1e081
MD
172 /*
173 * function to call when we receive a new channel, it receives a
174 * newly allocated channel, depending on the return code of this
175 * function, the new channel will be handled by the application
176 * or the library.
177 *
178 * Returns:
179 * > 0 (success, FD is kept by application)
180 * == 0 (success, FD is left to library)
181 * < 0 (error)
182 */
183 int (*on_recv_channel)(struct lttng_consumer_channel *channel);
184 /*
185 * function to call when we receive a new stream, it receives a
186 * newly allocated stream, depending on the return code of this
187 * function, the new stream will be handled by the application
188 * or the library.
189 *
190 * Returns:
191 * > 0 (success, FD is kept by application)
192 * == 0 (success, FD is left to library)
193 * < 0 (error)
194 */
195 int (*on_recv_stream)(struct lttng_consumer_stream *stream);
196 /*
197 * function to call when a stream is getting updated by the session
198 * daemon, this function receives the sessiond key and the new
199 * state, depending on the return code of this function the
200 * update of state for the stream is handled by the application
201 * or the library.
202 *
203 * Returns:
204 * > 0 (success, FD is kept by application)
205 * == 0 (success, FD is left to library)
206 * < 0 (error)
207 */
208 int (*on_update_stream)(int sessiond_key, uint32_t state);
209 /* socket to communicate errors with sessiond */
210 int consumer_error_socket;
211 /* socket to exchange commands with sessiond */
212 char *consumer_command_sock_path;
213 /* communication with splice */
214 int consumer_thread_pipe[2];
215 /* pipe to wake the poll thread when necessary */
216 int consumer_poll_pipe[2];
217 /* to let the signal handler wake up the fd receiver thread */
218 int consumer_should_quit[2];
219};
220
221/*
222 * Library-level data. One instance per process.
223 */
224struct lttng_consumer_global_data {
225 /*
e4421fec
DG
226 * At this time, this lock is used to ensure coherence between the count
227 * and number of element in the hash table. It's also a protection for
6065ceec
DG
228 * concurrent read/write between threads.
229 *
230 * XXX: We need to see if this lock is still needed with the lockless RCU
231 * hash tables.
3bd1e081
MD
232 */
233 pthread_mutex_t lock;
e4421fec 234
3bd1e081 235 /*
e4421fec 236 * Number of streams in the hash table. Protected by consumer_data.lock.
3bd1e081
MD
237 */
238 int stream_count;
239 /*
e4421fec 240 * Hash tables of streams and channels. Protected by consumer_data.lock.
3bd1e081 241 */
e4421fec
DG
242 struct lttng_ht *stream_ht;
243 struct lttng_ht *channel_ht;
3bd1e081
MD
244 /*
245 * Flag specifying if the local array of FDs needs update in the
246 * poll function. Protected by consumer_data.lock.
247 */
248 unsigned int need_update;
249 enum lttng_consumer_type type;
00e2e675
DG
250
251 /*
252 * Relayd socket(s) hashtable indexed by network sequence number. Each
253 * stream has an index which associate the right relayd socket to use.
254 */
255 struct lttng_ht *relayd_ht;
3bd1e081
MD
256};
257
e4421fec
DG
258/*
259 * Init consumer data structures.
260 */
261extern void lttng_consumer_init(void);
262
3bd1e081
MD
263/*
264 * Set the error socket for communication with a session daemon.
265 */
266extern void lttng_consumer_set_error_sock(
267 struct lttng_consumer_local_data *ctx, int sock);
268
269/*
270 * Set the command socket path for communication with a session daemon.
271 */
272extern void lttng_consumer_set_command_sock_path(
273 struct lttng_consumer_local_data *ctx, char *sock);
274
275/*
276 * Send return code to session daemon.
277 *
278 * Returns the return code of sendmsg : the number of bytes transmitted or -1
279 * on error.
280 */
281extern int lttng_consumer_send_error(
282 struct lttng_consumer_local_data *ctx, int cmd);
283
284/*
285 * Called from signal handler to ensure a clean exit.
286 */
287extern void lttng_consumer_should_exit(
288 struct lttng_consumer_local_data *ctx);
289
290/*
291 * Cleanup the daemon's socket on exit.
292 */
293extern void lttng_consumer_cleanup(void);
294
295/*
296 * Flush pending writes to trace output disk file.
297 */
298extern void lttng_consumer_sync_trace_file(
299 struct lttng_consumer_stream *stream, off_t orig_offset);
300
301/*
302 * Poll on the should_quit pipe and the command socket return -1 on error and
303 * should exit, 0 if data is available on the command socket
304 */
305extern int lttng_consumer_poll_socket(struct pollfd *kconsumer_sockpoll);
306
307extern int consumer_update_poll_array(
308 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
00e2e675
DG
309 struct lttng_consumer_stream **local_consumer_streams,
310 struct lttng_ht *metadata_ht);
3bd1e081
MD
311
312extern struct lttng_consumer_stream *consumer_allocate_stream(
313 int channel_key, int stream_key,
314 int shm_fd, int wait_fd,
315 enum lttng_consumer_stream_state state,
316 uint64_t mmap_len,
317 enum lttng_event_output output,
6df2e2c9
MD
318 const char *path_name,
319 uid_t uid,
00e2e675
DG
320 gid_t gid,
321 int net_index,
322 int metadata_flag);
3bd1e081
MD
323extern int consumer_add_stream(struct lttng_consumer_stream *stream);
324extern void consumer_del_stream(struct lttng_consumer_stream *stream);
325extern void consumer_change_stream_state(int stream_key,
326 enum lttng_consumer_stream_state state);
327extern void consumer_del_channel(struct lttng_consumer_channel *channel);
328extern struct lttng_consumer_channel *consumer_allocate_channel(
329 int channel_key,
330 int shm_fd, int wait_fd,
331 uint64_t mmap_len,
332 uint64_t max_sb_size);
333int consumer_add_channel(struct lttng_consumer_channel *channel);
334
00e2e675
DG
335/* lttng-relayd consumer command */
336int consumer_add_relayd(struct consumer_relayd_sock_pair *relayd);
337struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
338 int net_seq_idx);
339struct consumer_relayd_sock_pair *consumer_find_relayd(int key);
340int consumer_handle_stream_before_relayd(struct lttng_consumer_stream *stream,
341 size_t data_size);
342
3bd1e081
MD
343extern struct lttng_consumer_local_data *lttng_consumer_create(
344 enum lttng_consumer_type type,
4078b776 345 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
d41f73b7 346 struct lttng_consumer_local_data *ctx),
3bd1e081
MD
347 int (*recv_channel)(struct lttng_consumer_channel *channel),
348 int (*recv_stream)(struct lttng_consumer_stream *stream),
349 int (*update_stream)(int sessiond_key, uint32_t state));
350extern void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx);
4078b776 351extern ssize_t lttng_consumer_on_read_subbuffer_mmap(
3bd1e081
MD
352 struct lttng_consumer_local_data *ctx,
353 struct lttng_consumer_stream *stream, unsigned long len);
4078b776 354extern ssize_t lttng_consumer_on_read_subbuffer_splice(
3bd1e081
MD
355 struct lttng_consumer_local_data *ctx,
356 struct lttng_consumer_stream *stream, unsigned long len);
357extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
358 struct lttng_consumer_stream *stream);
359extern int lttng_consumer_get_produced_snapshot(
360 struct lttng_consumer_local_data *ctx,
361 struct lttng_consumer_stream *stream,
362 unsigned long *pos);
363extern void *lttng_consumer_thread_poll_fds(void *data);
364extern void *lttng_consumer_thread_receive_fds(void *data);
365extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
366 int sock, struct pollfd *consumer_sockpoll);
367
4078b776 368ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
369 struct lttng_consumer_local_data *ctx);
370int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream);
371
3bd1e081 372#endif /* _LTTNG_CONSUMER_H */
This page took 0.045707 seconds and 4 git commands to generate.