Merge branch 'master' of git://git.lttng.org/lttng-tools
[lttng-tools.git] / src / common / consumer.h
CommitLineData
3bd1e081
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
8 * of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#ifndef _LTTNG_CONSUMER_H
21#define _LTTNG_CONSUMER_H
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>
3bd1e081
MD
31
32/*
33 * When the receiving thread dies, we need to have a way to make the polling
34 * thread exit eventually. If all FDs hang up (normal case when the
32258573
DG
35 * lttng-sessiond stops), we can exit cleanly, but if there is a problem and
36 * for whatever reason some FDs remain open, the consumer should still exit
3bd1e081
MD
37 * eventually.
38 *
39 * If the timeout is reached, it means that during this period no events
40 * occurred on the FDs so we need to force an exit. This case should not happen
41 * but it is a safety to ensure we won't block the consumer indefinitely.
42 *
43 * The value of 2 seconds is an arbitrary choice.
44 */
45#define LTTNG_CONSUMER_POLL_TIMEOUT 2000
46
47/* Commands for consumer */
48enum lttng_consumer_command {
49 LTTNG_CONSUMER_ADD_CHANNEL,
50 LTTNG_CONSUMER_ADD_STREAM,
51 /* pause, delete, active depending on fd state */
52 LTTNG_CONSUMER_UPDATE_STREAM,
53 /* inform the consumer to quit when all fd has hang up */
54 LTTNG_CONSUMER_STOP,
55};
56
57/* State of each fd in consumer */
58enum lttng_consumer_stream_state {
59 LTTNG_CONSUMER_ACTIVE_STREAM,
60 LTTNG_CONSUMER_PAUSE_STREAM,
61 LTTNG_CONSUMER_DELETE_STREAM,
62};
63
3bd1e081
MD
64enum lttng_consumer_type {
65 LTTNG_CONSUMER_UNKNOWN = 0,
66 LTTNG_CONSUMER_KERNEL,
7753dea8
MD
67 LTTNG_CONSUMER64_UST,
68 LTTNG_CONSUMER32_UST,
3bd1e081
MD
69};
70
71struct lttng_consumer_channel {
e4421fec 72 struct lttng_ht_node_ulong node;
3bd1e081
MD
73 int key;
74 uint64_t max_sb_size; /* the subbuffer size for this channel */
75 int refcount; /* Number of streams referencing this channel */
76 /* For UST */
77 int shm_fd;
78 int wait_fd;
79 void *mmap_base;
80 size_t mmap_len;
13161846 81 struct lttng_ust_shm_handle *handle;
3bd1e081 82 int nr_streams;
b5c5fc29 83 int wait_fd_is_copy;
5af2f756 84 int cpucount;
3bd1e081
MD
85};
86
87/* Forward declaration for UST. */
13161846 88struct lttng_ust_lib_ring_buffer;
3bd1e081
MD
89
90/*
91 * Internal representation of the streams, sessiond_key is used to identify
92 * uniquely a stream.
93 */
94struct lttng_consumer_stream {
e4421fec 95 struct lttng_ht_node_ulong node;
3bd1e081
MD
96 struct lttng_consumer_channel *chan; /* associated channel */
97 /*
98 * key is the key used by the session daemon to refer to the
99 * object in the consumer daemon.
100 */
101 int key;
102 int shm_fd;
103 int wait_fd;
104 int out_fd; /* output file to write the data */
105 off_t out_fd_offset; /* write position in the output file descriptor */
106 char path_name[PATH_MAX]; /* tracefile name */
107 enum lttng_consumer_stream_state state;
108 size_t shm_len;
109 void *mmap_base;
110 size_t mmap_len;
111 enum lttng_event_output output; /* splice or mmap */
b5c5fc29
MD
112 int shm_fd_is_copy;
113 int wait_fd_is_copy;
3bd1e081 114 /* For UST */
13161846 115 struct lttng_ust_lib_ring_buffer *buf;
3bd1e081 116 int cpu;
4078b776 117 int data_read;
d056b477 118 int hangup_flush_done;
6df2e2c9
MD
119 /* UID/GID of the user owning the session to which stream belongs */
120 uid_t uid;
121 gid_t gid;
3bd1e081
MD
122};
123
124/*
125 * UST consumer local data to the program. One or more instance per
126 * process.
127 */
128struct lttng_consumer_local_data {
4078b776
MD
129 /*
130 * Function to call when data is available on a buffer.
131 * Returns the number of bytes read, or negative error value.
132 */
133 ssize_t (*on_buffer_ready)(struct lttng_consumer_stream *stream,
d41f73b7 134 struct lttng_consumer_local_data *ctx);
3bd1e081
MD
135 /*
136 * function to call when we receive a new channel, it receives a
137 * newly allocated channel, depending on the return code of this
138 * function, the new channel will be handled by the application
139 * or the library.
140 *
141 * Returns:
142 * > 0 (success, FD is kept by application)
143 * == 0 (success, FD is left to library)
144 * < 0 (error)
145 */
146 int (*on_recv_channel)(struct lttng_consumer_channel *channel);
147 /*
148 * function to call when we receive a new stream, it receives a
149 * newly allocated stream, depending on the return code of this
150 * function, the new stream will be handled by the application
151 * or the library.
152 *
153 * Returns:
154 * > 0 (success, FD is kept by application)
155 * == 0 (success, FD is left to library)
156 * < 0 (error)
157 */
158 int (*on_recv_stream)(struct lttng_consumer_stream *stream);
159 /*
160 * function to call when a stream is getting updated by the session
161 * daemon, this function receives the sessiond key and the new
162 * state, depending on the return code of this function the
163 * update of state for the stream is handled by the application
164 * or the library.
165 *
166 * Returns:
167 * > 0 (success, FD is kept by application)
168 * == 0 (success, FD is left to library)
169 * < 0 (error)
170 */
171 int (*on_update_stream)(int sessiond_key, uint32_t state);
172 /* socket to communicate errors with sessiond */
173 int consumer_error_socket;
174 /* socket to exchange commands with sessiond */
175 char *consumer_command_sock_path;
176 /* communication with splice */
177 int consumer_thread_pipe[2];
178 /* pipe to wake the poll thread when necessary */
179 int consumer_poll_pipe[2];
180 /* to let the signal handler wake up the fd receiver thread */
181 int consumer_should_quit[2];
182};
183
184/*
185 * Library-level data. One instance per process.
186 */
187struct lttng_consumer_global_data {
e4421fec 188
3bd1e081 189 /*
e4421fec
DG
190 * At this time, this lock is used to ensure coherence between the count
191 * and number of element in the hash table. It's also a protection for
6065ceec
DG
192 * concurrent read/write between threads.
193 *
194 * XXX: We need to see if this lock is still needed with the lockless RCU
195 * hash tables.
3bd1e081
MD
196 */
197 pthread_mutex_t lock;
e4421fec 198
3bd1e081 199 /*
e4421fec 200 * Number of streams in the hash table. Protected by consumer_data.lock.
3bd1e081
MD
201 */
202 int stream_count;
203 /*
e4421fec 204 * Hash tables of streams and channels. Protected by consumer_data.lock.
3bd1e081 205 */
e4421fec
DG
206 struct lttng_ht *stream_ht;
207 struct lttng_ht *channel_ht;
3bd1e081
MD
208 /*
209 * Flag specifying if the local array of FDs needs update in the
210 * poll function. Protected by consumer_data.lock.
211 */
212 unsigned int need_update;
213 enum lttng_consumer_type type;
214};
215
e4421fec
DG
216/*
217 * Init consumer data structures.
218 */
219extern void lttng_consumer_init(void);
220
3bd1e081
MD
221/*
222 * Set the error socket for communication with a session daemon.
223 */
224extern void lttng_consumer_set_error_sock(
225 struct lttng_consumer_local_data *ctx, int sock);
226
227/*
228 * Set the command socket path for communication with a session daemon.
229 */
230extern void lttng_consumer_set_command_sock_path(
231 struct lttng_consumer_local_data *ctx, char *sock);
232
233/*
234 * Send return code to session daemon.
235 *
236 * Returns the return code of sendmsg : the number of bytes transmitted or -1
237 * on error.
238 */
239extern int lttng_consumer_send_error(
240 struct lttng_consumer_local_data *ctx, int cmd);
241
242/*
243 * Called from signal handler to ensure a clean exit.
244 */
245extern void lttng_consumer_should_exit(
246 struct lttng_consumer_local_data *ctx);
247
248/*
249 * Cleanup the daemon's socket on exit.
250 */
251extern void lttng_consumer_cleanup(void);
252
253/*
254 * Flush pending writes to trace output disk file.
255 */
256extern void lttng_consumer_sync_trace_file(
257 struct lttng_consumer_stream *stream, off_t orig_offset);
258
259/*
260 * Poll on the should_quit pipe and the command socket return -1 on error and
261 * should exit, 0 if data is available on the command socket
262 */
263extern int lttng_consumer_poll_socket(struct pollfd *kconsumer_sockpoll);
264
265extern int consumer_update_poll_array(
266 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
267 struct lttng_consumer_stream **local_consumer_streams);
268
269extern struct lttng_consumer_stream *consumer_allocate_stream(
270 int channel_key, int stream_key,
271 int shm_fd, int wait_fd,
272 enum lttng_consumer_stream_state state,
273 uint64_t mmap_len,
274 enum lttng_event_output output,
6df2e2c9
MD
275 const char *path_name,
276 uid_t uid,
277 gid_t gid);
3bd1e081
MD
278extern int consumer_add_stream(struct lttng_consumer_stream *stream);
279extern void consumer_del_stream(struct lttng_consumer_stream *stream);
280extern void consumer_change_stream_state(int stream_key,
281 enum lttng_consumer_stream_state state);
282extern void consumer_del_channel(struct lttng_consumer_channel *channel);
283extern struct lttng_consumer_channel *consumer_allocate_channel(
284 int channel_key,
285 int shm_fd, int wait_fd,
286 uint64_t mmap_len,
287 uint64_t max_sb_size);
288int consumer_add_channel(struct lttng_consumer_channel *channel);
289
290extern struct lttng_consumer_local_data *lttng_consumer_create(
291 enum lttng_consumer_type type,
4078b776 292 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
d41f73b7 293 struct lttng_consumer_local_data *ctx),
3bd1e081
MD
294 int (*recv_channel)(struct lttng_consumer_channel *channel),
295 int (*recv_stream)(struct lttng_consumer_stream *stream),
296 int (*update_stream)(int sessiond_key, uint32_t state));
297extern void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx);
4078b776 298extern ssize_t lttng_consumer_on_read_subbuffer_mmap(
3bd1e081
MD
299 struct lttng_consumer_local_data *ctx,
300 struct lttng_consumer_stream *stream, unsigned long len);
4078b776 301extern ssize_t lttng_consumer_on_read_subbuffer_splice(
3bd1e081
MD
302 struct lttng_consumer_local_data *ctx,
303 struct lttng_consumer_stream *stream, unsigned long len);
304extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
305 struct lttng_consumer_stream *stream);
306extern int lttng_consumer_get_produced_snapshot(
307 struct lttng_consumer_local_data *ctx,
308 struct lttng_consumer_stream *stream,
309 unsigned long *pos);
310extern void *lttng_consumer_thread_poll_fds(void *data);
311extern void *lttng_consumer_thread_receive_fds(void *data);
312extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
313 int sock, struct pollfd *consumer_sockpoll);
314
4078b776 315ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
316 struct lttng_consumer_local_data *ctx);
317int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream);
318
3bd1e081 319#endif /* _LTTNG_CONSUMER_H */
This page took 0.044076 seconds and 4 git commands to generate.