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