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