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