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