Network streaming support
[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;
141 pthread_mutex_t ctrl_sock_mutex;
142 /* Sockets information */
143 struct lttcomm_sock control_sock;
144 struct lttcomm_sock data_sock;
145 struct lttng_ht_node_ulong node;
3bd1e081
MD
146};
147
148/*
149 * UST consumer local data to the program. One or more instance per
150 * process.
151 */
152struct lttng_consumer_local_data {
4078b776
MD
153 /*
154 * Function to call when data is available on a buffer.
155 * Returns the number of bytes read, or negative error value.
156 */
157 ssize_t (*on_buffer_ready)(struct lttng_consumer_stream *stream,
d41f73b7 158 struct lttng_consumer_local_data *ctx);
3bd1e081
MD
159 /*
160 * function to call when we receive a new channel, it receives a
161 * newly allocated channel, depending on the return code of this
162 * function, the new channel will be handled by the application
163 * or the library.
164 *
165 * Returns:
166 * > 0 (success, FD is kept by application)
167 * == 0 (success, FD is left to library)
168 * < 0 (error)
169 */
170 int (*on_recv_channel)(struct lttng_consumer_channel *channel);
171 /*
172 * function to call when we receive a new stream, it receives a
173 * newly allocated stream, depending on the return code of this
174 * function, the new stream will be handled by the application
175 * or the library.
176 *
177 * Returns:
178 * > 0 (success, FD is kept by application)
179 * == 0 (success, FD is left to library)
180 * < 0 (error)
181 */
182 int (*on_recv_stream)(struct lttng_consumer_stream *stream);
183 /*
184 * function to call when a stream is getting updated by the session
185 * daemon, this function receives the sessiond key and the new
186 * state, depending on the return code of this function the
187 * update of state for the stream is 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_update_stream)(int sessiond_key, uint32_t state);
196 /* socket to communicate errors with sessiond */
197 int consumer_error_socket;
198 /* socket to exchange commands with sessiond */
199 char *consumer_command_sock_path;
200 /* communication with splice */
201 int consumer_thread_pipe[2];
202 /* pipe to wake the poll thread when necessary */
203 int consumer_poll_pipe[2];
204 /* to let the signal handler wake up the fd receiver thread */
205 int consumer_should_quit[2];
206};
207
208/*
209 * Library-level data. One instance per process.
210 */
211struct lttng_consumer_global_data {
212 /*
e4421fec
DG
213 * At this time, this lock is used to ensure coherence between the count
214 * and number of element in the hash table. It's also a protection for
6065ceec
DG
215 * concurrent read/write between threads.
216 *
217 * XXX: We need to see if this lock is still needed with the lockless RCU
218 * hash tables.
3bd1e081
MD
219 */
220 pthread_mutex_t lock;
e4421fec 221
3bd1e081 222 /*
e4421fec 223 * Number of streams in the hash table. Protected by consumer_data.lock.
3bd1e081
MD
224 */
225 int stream_count;
226 /*
e4421fec 227 * Hash tables of streams and channels. Protected by consumer_data.lock.
3bd1e081 228 */
e4421fec
DG
229 struct lttng_ht *stream_ht;
230 struct lttng_ht *channel_ht;
3bd1e081
MD
231 /*
232 * Flag specifying if the local array of FDs needs update in the
233 * poll function. Protected by consumer_data.lock.
234 */
235 unsigned int need_update;
236 enum lttng_consumer_type type;
00e2e675
DG
237
238 /*
239 * Relayd socket(s) hashtable indexed by network sequence number. Each
240 * stream has an index which associate the right relayd socket to use.
241 */
242 struct lttng_ht *relayd_ht;
3bd1e081
MD
243};
244
e4421fec
DG
245/*
246 * Init consumer data structures.
247 */
248extern void lttng_consumer_init(void);
249
3bd1e081
MD
250/*
251 * Set the error socket for communication with a session daemon.
252 */
253extern void lttng_consumer_set_error_sock(
254 struct lttng_consumer_local_data *ctx, int sock);
255
256/*
257 * Set the command socket path for communication with a session daemon.
258 */
259extern void lttng_consumer_set_command_sock_path(
260 struct lttng_consumer_local_data *ctx, char *sock);
261
262/*
263 * Send return code to session daemon.
264 *
265 * Returns the return code of sendmsg : the number of bytes transmitted or -1
266 * on error.
267 */
268extern int lttng_consumer_send_error(
269 struct lttng_consumer_local_data *ctx, int cmd);
270
271/*
272 * Called from signal handler to ensure a clean exit.
273 */
274extern void lttng_consumer_should_exit(
275 struct lttng_consumer_local_data *ctx);
276
277/*
278 * Cleanup the daemon's socket on exit.
279 */
280extern void lttng_consumer_cleanup(void);
281
282/*
283 * Flush pending writes to trace output disk file.
284 */
285extern void lttng_consumer_sync_trace_file(
286 struct lttng_consumer_stream *stream, off_t orig_offset);
287
288/*
289 * Poll on the should_quit pipe and the command socket return -1 on error and
290 * should exit, 0 if data is available on the command socket
291 */
292extern int lttng_consumer_poll_socket(struct pollfd *kconsumer_sockpoll);
293
294extern int consumer_update_poll_array(
295 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
00e2e675
DG
296 struct lttng_consumer_stream **local_consumer_streams,
297 struct lttng_ht *metadata_ht);
3bd1e081
MD
298
299extern struct lttng_consumer_stream *consumer_allocate_stream(
300 int channel_key, int stream_key,
301 int shm_fd, int wait_fd,
302 enum lttng_consumer_stream_state state,
303 uint64_t mmap_len,
304 enum lttng_event_output output,
6df2e2c9
MD
305 const char *path_name,
306 uid_t uid,
00e2e675
DG
307 gid_t gid,
308 int net_index,
309 int metadata_flag);
3bd1e081
MD
310extern int consumer_add_stream(struct lttng_consumer_stream *stream);
311extern void consumer_del_stream(struct lttng_consumer_stream *stream);
312extern void consumer_change_stream_state(int stream_key,
313 enum lttng_consumer_stream_state state);
314extern void consumer_del_channel(struct lttng_consumer_channel *channel);
315extern struct lttng_consumer_channel *consumer_allocate_channel(
316 int channel_key,
317 int shm_fd, int wait_fd,
318 uint64_t mmap_len,
319 uint64_t max_sb_size);
320int consumer_add_channel(struct lttng_consumer_channel *channel);
321
00e2e675
DG
322/* lttng-relayd consumer command */
323int consumer_add_relayd(struct consumer_relayd_sock_pair *relayd);
324struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
325 int net_seq_idx);
326struct consumer_relayd_sock_pair *consumer_find_relayd(int key);
327int consumer_handle_stream_before_relayd(struct lttng_consumer_stream *stream,
328 size_t data_size);
329
3bd1e081
MD
330extern struct lttng_consumer_local_data *lttng_consumer_create(
331 enum lttng_consumer_type type,
4078b776 332 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
d41f73b7 333 struct lttng_consumer_local_data *ctx),
3bd1e081
MD
334 int (*recv_channel)(struct lttng_consumer_channel *channel),
335 int (*recv_stream)(struct lttng_consumer_stream *stream),
336 int (*update_stream)(int sessiond_key, uint32_t state));
337extern void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx);
4078b776 338extern ssize_t lttng_consumer_on_read_subbuffer_mmap(
3bd1e081
MD
339 struct lttng_consumer_local_data *ctx,
340 struct lttng_consumer_stream *stream, unsigned long len);
4078b776 341extern ssize_t lttng_consumer_on_read_subbuffer_splice(
3bd1e081
MD
342 struct lttng_consumer_local_data *ctx,
343 struct lttng_consumer_stream *stream, unsigned long len);
344extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
345 struct lttng_consumer_stream *stream);
346extern int lttng_consumer_get_produced_snapshot(
347 struct lttng_consumer_local_data *ctx,
348 struct lttng_consumer_stream *stream,
349 unsigned long *pos);
350extern void *lttng_consumer_thread_poll_fds(void *data);
351extern void *lttng_consumer_thread_receive_fds(void *data);
352extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
353 int sock, struct pollfd *consumer_sockpoll);
354
4078b776 355ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
356 struct lttng_consumer_local_data *ctx);
357int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream);
358
3bd1e081 359#endif /* _LTTNG_CONSUMER_H */
This page took 0.044686 seconds and 4 git commands to generate.