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