9a93c427915bbd6de3aef1bdbc9440320244092a
[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 LIB_CONSUMER_H
21 #define LIB_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 /* Inform the consumer to kill a specific relayd connection */
58 LTTNG_CONSUMER_DESTROY_RELAYD,
59 };
60
61 /* State of each fd in consumer */
62 enum lttng_consumer_stream_state {
63 LTTNG_CONSUMER_ACTIVE_STREAM,
64 LTTNG_CONSUMER_PAUSE_STREAM,
65 LTTNG_CONSUMER_DELETE_STREAM,
66 };
67
68 enum lttng_consumer_type {
69 LTTNG_CONSUMER_UNKNOWN = 0,
70 LTTNG_CONSUMER_KERNEL,
71 LTTNG_CONSUMER64_UST,
72 LTTNG_CONSUMER32_UST,
73 };
74
75 struct lttng_consumer_channel {
76 struct lttng_ht_node_ulong node;
77 int key;
78 uint64_t max_sb_size; /* the subbuffer size for this channel */
79 int refcount; /* Number of streams referencing this channel */
80 /*
81 * The number of streams to receive initially. Used to guarantee that we do
82 * not destroy a channel before receiving all its associated streams.
83 */
84 unsigned int nb_init_streams;
85
86 /* For UST */
87 int shm_fd;
88 int wait_fd;
89 void *mmap_base;
90 size_t mmap_len;
91 struct lttng_ust_shm_handle *handle;
92 int wait_fd_is_copy;
93 int cpucount;
94 };
95
96 /* Forward declaration for UST. */
97 struct lttng_ust_lib_ring_buffer;
98
99 /*
100 * Internal representation of the streams, sessiond_key is used to identify
101 * uniquely a stream.
102 */
103 struct lttng_consumer_stream {
104 struct lttng_ht_node_ulong node;
105 struct lttng_ht_node_ulong waitfd_node;
106 struct lttng_consumer_channel *chan; /* associated channel */
107 /*
108 * key is the key used by the session daemon to refer to the
109 * object in the consumer daemon.
110 */
111 int key;
112 int shm_fd;
113 int wait_fd;
114 int out_fd; /* output file to write the data */
115 off_t out_fd_offset; /* write position in the output file descriptor */
116 char path_name[PATH_MAX]; /* tracefile name */
117 enum lttng_consumer_stream_state state;
118 size_t shm_len;
119 void *mmap_base;
120 size_t mmap_len;
121 enum lttng_event_output output; /* splice or mmap */
122 int shm_fd_is_copy;
123 int wait_fd_is_copy;
124 /* For UST */
125 struct lttng_ust_lib_ring_buffer *buf;
126 int cpu;
127 int data_read;
128 int hangup_flush_done;
129 /* UID/GID of the user owning the session to which stream belongs */
130 uid_t uid;
131 gid_t gid;
132 /* Network sequence number. Indicating on which relayd socket it goes. */
133 int net_seq_idx;
134 /* Identify if the stream is the metadata */
135 unsigned int metadata_flag;
136 /* Used when the stream is set for network streaming */
137 uint64_t relayd_stream_id;
138 /* Next sequence number to use for trace packet */
139 uint64_t next_net_seq_num;
140 };
141
142 /*
143 * Internal representation of a relayd socket pair.
144 */
145 struct consumer_relayd_sock_pair {
146 /* Network sequence number. */
147 int net_seq_idx;
148 /* Number of stream associated with this relayd */
149 unsigned int refcount;
150
151 /*
152 * This flag indicates whether or not we should destroy this object. The
153 * destruction should ONLY occurs when this flag is set and the refcount is
154 * set to zero.
155 */
156 unsigned int destroy_flag;
157
158 /*
159 * Mutex protecting the control socket to avoid out of order packets
160 * between threads sending data to the relayd. Since metadata data is sent
161 * over that socket, at least two sendmsg() are needed (header + data)
162 * creating a race for packets to overlap between threads using it.
163 */
164 pthread_mutex_t ctrl_sock_mutex;
165
166 /* Control socket. Command and metadata are passed over it */
167 struct lttcomm_sock control_sock;
168
169 /*
170 * We don't need a mutex at this point since we only splice or write single
171 * large chunk of data with a header appended at the begining. Moreover,
172 * this socket is for now only used in a single thread.
173 */
174 struct lttcomm_sock data_sock;
175 struct lttng_ht_node_ulong node;
176 };
177
178 /*
179 * UST consumer local data to the program. One or more instance per
180 * process.
181 */
182 struct lttng_consumer_local_data {
183 /*
184 * Function to call when data is available on a buffer.
185 * Returns the number of bytes read, or negative error value.
186 */
187 ssize_t (*on_buffer_ready)(struct lttng_consumer_stream *stream,
188 struct lttng_consumer_local_data *ctx);
189 /*
190 * function to call when we receive a new channel, it receives a
191 * newly allocated channel, depending on the return code of this
192 * function, the new channel will be handled by the application
193 * or the library.
194 *
195 * Returns:
196 * > 0 (success, FD is kept by application)
197 * == 0 (success, FD is left to library)
198 * < 0 (error)
199 */
200 int (*on_recv_channel)(struct lttng_consumer_channel *channel);
201 /*
202 * function to call when we receive a new stream, it receives a
203 * newly allocated stream, depending on the return code of this
204 * function, the new stream will be handled by the application
205 * or the library.
206 *
207 * Returns:
208 * > 0 (success, FD is kept by application)
209 * == 0 (success, FD is left to library)
210 * < 0 (error)
211 */
212 int (*on_recv_stream)(struct lttng_consumer_stream *stream);
213 /*
214 * function to call when a stream is getting updated by the session
215 * daemon, this function receives the sessiond key and the new
216 * state, depending on the return code of this function the
217 * update of state for the stream is handled by the application
218 * or the library.
219 *
220 * Returns:
221 * > 0 (success, FD is kept by application)
222 * == 0 (success, FD is left to library)
223 * < 0 (error)
224 */
225 int (*on_update_stream)(int sessiond_key, uint32_t state);
226 /* socket to communicate errors with sessiond */
227 int consumer_error_socket;
228 /* socket to exchange commands with sessiond */
229 char *consumer_command_sock_path;
230 /* communication with splice */
231 int consumer_thread_pipe[2];
232 int consumer_splice_metadata_pipe[2];
233 /* pipe to wake the poll thread when necessary */
234 int consumer_poll_pipe[2];
235 /* to let the signal handler wake up the fd receiver thread */
236 int consumer_should_quit[2];
237 /* Metadata poll thread pipe. Transfer metadata stream to it */
238 int consumer_metadata_pipe[2];
239 };
240
241 /*
242 * Library-level data. One instance per process.
243 */
244 struct lttng_consumer_global_data {
245 /*
246 * At this time, this lock is used to ensure coherence between the count
247 * and number of element in the hash table. It's also a protection for
248 * concurrent read/write between threads.
249 *
250 * XXX: We need to see if this lock is still needed with the lockless RCU
251 * hash tables.
252 */
253 pthread_mutex_t lock;
254
255 /*
256 * Number of streams in the hash table. Protected by consumer_data.lock.
257 */
258 int stream_count;
259 /*
260 * Hash tables of streams and channels. Protected by consumer_data.lock.
261 */
262 struct lttng_ht *stream_ht;
263 struct lttng_ht *channel_ht;
264 /*
265 * Flag specifying if the local array of FDs needs update in the
266 * poll function. Protected by consumer_data.lock.
267 */
268 unsigned int need_update;
269 enum lttng_consumer_type type;
270
271 /*
272 * Relayd socket(s) hashtable indexed by network sequence number. Each
273 * stream has an index which associate the right relayd socket to use.
274 */
275 struct lttng_ht *relayd_ht;
276 };
277
278 /*
279 * Init consumer data structures.
280 */
281 extern void lttng_consumer_init(void);
282
283 /*
284 * Set the error socket for communication with a session daemon.
285 */
286 extern void lttng_consumer_set_error_sock(
287 struct lttng_consumer_local_data *ctx, int sock);
288
289 /*
290 * Set the command socket path for communication with a session daemon.
291 */
292 extern void lttng_consumer_set_command_sock_path(
293 struct lttng_consumer_local_data *ctx, char *sock);
294
295 /*
296 * Send return code to session daemon.
297 *
298 * Returns the return code of sendmsg : the number of bytes transmitted or -1
299 * on error.
300 */
301 extern int lttng_consumer_send_error(
302 struct lttng_consumer_local_data *ctx, int cmd);
303
304 /*
305 * Called from signal handler to ensure a clean exit.
306 */
307 extern void lttng_consumer_should_exit(
308 struct lttng_consumer_local_data *ctx);
309
310 /*
311 * Cleanup the daemon's socket on exit.
312 */
313 extern void lttng_consumer_cleanup(void);
314
315 /*
316 * Flush pending writes to trace output disk file.
317 */
318 extern void lttng_consumer_sync_trace_file(
319 struct lttng_consumer_stream *stream, off_t orig_offset);
320
321 /*
322 * Poll on the should_quit pipe and the command socket return -1 on error and
323 * should exit, 0 if data is available on the command socket
324 */
325 extern int lttng_consumer_poll_socket(struct pollfd *kconsumer_sockpoll);
326
327 extern int consumer_update_poll_array(
328 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
329 struct lttng_consumer_stream **local_consumer_streams);
330
331 extern struct lttng_consumer_stream *consumer_allocate_stream(
332 int channel_key, int stream_key,
333 int shm_fd, int wait_fd,
334 enum lttng_consumer_stream_state state,
335 uint64_t mmap_len,
336 enum lttng_event_output output,
337 const char *path_name,
338 uid_t uid,
339 gid_t gid,
340 int net_index,
341 int metadata_flag,
342 int *alloc_ret);
343 extern int consumer_add_stream(struct lttng_consumer_stream *stream);
344 extern void consumer_del_stream(struct lttng_consumer_stream *stream);
345 extern void consumer_change_stream_state(int stream_key,
346 enum lttng_consumer_stream_state state);
347 extern void consumer_del_channel(struct lttng_consumer_channel *channel);
348 extern struct lttng_consumer_channel *consumer_allocate_channel(
349 int channel_key,
350 int shm_fd, int wait_fd,
351 uint64_t mmap_len,
352 uint64_t max_sb_size,
353 unsigned int nb_init_streams);
354 int consumer_add_channel(struct lttng_consumer_channel *channel);
355
356 /* lttng-relayd consumer command */
357 struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
358 int net_seq_idx);
359 struct consumer_relayd_sock_pair *consumer_find_relayd(int key);
360 int consumer_handle_stream_before_relayd(struct lttng_consumer_stream *stream,
361 size_t data_size);
362
363 extern struct lttng_consumer_local_data *lttng_consumer_create(
364 enum lttng_consumer_type type,
365 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
366 struct lttng_consumer_local_data *ctx),
367 int (*recv_channel)(struct lttng_consumer_channel *channel),
368 int (*recv_stream)(struct lttng_consumer_stream *stream),
369 int (*update_stream)(int sessiond_key, uint32_t state));
370 extern void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx);
371 extern ssize_t lttng_consumer_on_read_subbuffer_mmap(
372 struct lttng_consumer_local_data *ctx,
373 struct lttng_consumer_stream *stream, unsigned long len,
374 unsigned long padding);
375 extern ssize_t lttng_consumer_on_read_subbuffer_splice(
376 struct lttng_consumer_local_data *ctx,
377 struct lttng_consumer_stream *stream, unsigned long len,
378 unsigned long padding);
379 extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
380 struct lttng_consumer_stream *stream);
381 extern int lttng_consumer_get_produced_snapshot(
382 struct lttng_consumer_local_data *ctx,
383 struct lttng_consumer_stream *stream,
384 unsigned long *pos);
385 extern void *lttng_consumer_thread_poll_fds(void *data);
386 extern void *lttng_consumer_thread_receive_fds(void *data);
387 extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
388 int sock, struct pollfd *consumer_sockpoll);
389
390 ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
391 struct lttng_consumer_local_data *ctx);
392 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream);
393 int consumer_add_relayd_socket(int net_seq_idx, int sock_type,
394 struct lttng_consumer_local_data *ctx, int sock,
395 struct pollfd *consumer_sockpoll, struct lttcomm_sock *relayd_sock);
396 void consumer_flag_relayd_for_destroy(
397 struct consumer_relayd_sock_pair *relayd);
398
399 #endif /* LIB_CONSUMER_H */
This page took 0.035784 seconds and 3 git commands to generate.