Network streaming support
[lttng-tools.git] / src / common / consumer.h
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2012 - David Goulet <dgoulet@efficios.com>
5 *
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.
9 *
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.
14 *
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.
18 */
19
20 #ifndef _LTTNG_CONSUMER_H
21 #define _LTTNG_CONSUMER_H
22
23 #include <limits.h>
24 #include <poll.h>
25 #include <unistd.h>
26
27 #include <lttng/lttng.h>
28
29 #include <common/hashtable/hashtable.h>
30 #include <common/compat/fcntl.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
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
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
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 */
49 enum 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,
56 LTTNG_CONSUMER_ADD_RELAYD_SOCKET,
57 };
58
59 /* State of each fd in consumer */
60 enum lttng_consumer_stream_state {
61 LTTNG_CONSUMER_ACTIVE_STREAM,
62 LTTNG_CONSUMER_PAUSE_STREAM,
63 LTTNG_CONSUMER_DELETE_STREAM,
64 };
65
66 enum lttng_consumer_type {
67 LTTNG_CONSUMER_UNKNOWN = 0,
68 LTTNG_CONSUMER_KERNEL,
69 LTTNG_CONSUMER64_UST,
70 LTTNG_CONSUMER32_UST,
71 };
72
73 struct lttng_consumer_channel {
74 struct lttng_ht_node_ulong node;
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;
83 struct lttng_ust_shm_handle *handle;
84 int nr_streams;
85 int wait_fd_is_copy;
86 int cpucount;
87 };
88
89 /* Forward declaration for UST. */
90 struct lttng_ust_lib_ring_buffer;
91
92 /*
93 * Internal representation of the streams, sessiond_key is used to identify
94 * uniquely a stream.
95 */
96 struct lttng_consumer_stream {
97 struct lttng_ht_node_ulong node;
98 struct lttng_ht_node_ulong waitfd_node;
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 */
115 int shm_fd_is_copy;
116 int wait_fd_is_copy;
117 /* For UST */
118 struct lttng_ust_lib_ring_buffer *buf;
119 int cpu;
120 int data_read;
121 int hangup_flush_done;
122 /* UID/GID of the user owning the session to which stream belongs */
123 uid_t uid;
124 gid_t gid;
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 */
136 struct 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;
146 };
147
148 /*
149 * UST consumer local data to the program. One or more instance per
150 * process.
151 */
152 struct lttng_consumer_local_data {
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,
158 struct lttng_consumer_local_data *ctx);
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 */
211 struct lttng_consumer_global_data {
212 /*
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
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.
219 */
220 pthread_mutex_t lock;
221
222 /*
223 * Number of streams in the hash table. Protected by consumer_data.lock.
224 */
225 int stream_count;
226 /*
227 * Hash tables of streams and channels. Protected by consumer_data.lock.
228 */
229 struct lttng_ht *stream_ht;
230 struct lttng_ht *channel_ht;
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;
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;
243 };
244
245 /*
246 * Init consumer data structures.
247 */
248 extern void lttng_consumer_init(void);
249
250 /*
251 * Set the error socket for communication with a session daemon.
252 */
253 extern 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 */
259 extern 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 */
268 extern 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 */
274 extern void lttng_consumer_should_exit(
275 struct lttng_consumer_local_data *ctx);
276
277 /*
278 * Cleanup the daemon's socket on exit.
279 */
280 extern void lttng_consumer_cleanup(void);
281
282 /*
283 * Flush pending writes to trace output disk file.
284 */
285 extern 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 */
292 extern int lttng_consumer_poll_socket(struct pollfd *kconsumer_sockpoll);
293
294 extern int consumer_update_poll_array(
295 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
296 struct lttng_consumer_stream **local_consumer_streams,
297 struct lttng_ht *metadata_ht);
298
299 extern 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,
305 const char *path_name,
306 uid_t uid,
307 gid_t gid,
308 int net_index,
309 int metadata_flag);
310 extern int consumer_add_stream(struct lttng_consumer_stream *stream);
311 extern void consumer_del_stream(struct lttng_consumer_stream *stream);
312 extern void consumer_change_stream_state(int stream_key,
313 enum lttng_consumer_stream_state state);
314 extern void consumer_del_channel(struct lttng_consumer_channel *channel);
315 extern 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);
320 int consumer_add_channel(struct lttng_consumer_channel *channel);
321
322 /* lttng-relayd consumer command */
323 int consumer_add_relayd(struct consumer_relayd_sock_pair *relayd);
324 struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
325 int net_seq_idx);
326 struct consumer_relayd_sock_pair *consumer_find_relayd(int key);
327 int consumer_handle_stream_before_relayd(struct lttng_consumer_stream *stream,
328 size_t data_size);
329
330 extern struct lttng_consumer_local_data *lttng_consumer_create(
331 enum lttng_consumer_type type,
332 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
333 struct lttng_consumer_local_data *ctx),
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));
337 extern void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx);
338 extern ssize_t lttng_consumer_on_read_subbuffer_mmap(
339 struct lttng_consumer_local_data *ctx,
340 struct lttng_consumer_stream *stream, unsigned long len);
341 extern ssize_t lttng_consumer_on_read_subbuffer_splice(
342 struct lttng_consumer_local_data *ctx,
343 struct lttng_consumer_stream *stream, unsigned long len);
344 extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
345 struct lttng_consumer_stream *stream);
346 extern int lttng_consumer_get_produced_snapshot(
347 struct lttng_consumer_local_data *ctx,
348 struct lttng_consumer_stream *stream,
349 unsigned long *pos);
350 extern void *lttng_consumer_thread_poll_fds(void *data);
351 extern void *lttng_consumer_thread_receive_fds(void *data);
352 extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
353 int sock, struct pollfd *consumer_sockpoll);
354
355 ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
356 struct lttng_consumer_local_data *ctx);
357 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream);
358
359 #endif /* _LTTNG_CONSUMER_H */
This page took 0.052768 seconds and 4 git commands to generate.