Fix order of streams
[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 * lttng-sessiond stops), we can exit cleanly, but if there is a problem and
32 * for 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 lttng_ust_shm_handle *handle;
85 int nr_streams;
86 int shm_fd_is_copy;
87 int wait_fd_is_copy;
88 int cpucount;
89 };
90
91 /* Forward declaration for UST. */
92 struct lttng_ust_lib_ring_buffer;
93
94 /*
95 * Internal representation of the streams, sessiond_key is used to identify
96 * uniquely a stream.
97 */
98 struct lttng_consumer_stream {
99 struct cds_list_head list;
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 */
116 int shm_fd_is_copy;
117 int wait_fd_is_copy;
118 /* For UST */
119 struct lttng_ust_lib_ring_buffer *buf;
120 int cpu;
121 };
122
123 /*
124 * UST consumer local data to the program. One or more instance per
125 * process.
126 */
127 struct lttng_consumer_local_data {
128 /* function to call when data is available on a buffer */
129 int (*on_buffer_ready)(struct lttng_consumer_stream *stream);
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 */
182 struct lttng_consumer_global_data {
183 /*
184 * consumer_data.lock protects consumer_data.fd_list,
185 * consumer_data.stream_count, and consumer_data.need_update. It
186 * ensures the count matches the number of items in the fd_list.
187 * It ensures the list updates *always* trigger an fd_array
188 * update (therefore need to make list update vs
189 * consumer_data.need_update flag update atomic, and also flag
190 * read, fd array and flag clear atomic).
191 */
192 pthread_mutex_t lock;
193 /*
194 * Number of streams in the list below. Protected by
195 * consumer_data.lock.
196 */
197 int stream_count;
198 /*
199 * Lists of streams and channels. Protected by consumer_data.lock.
200 */
201 struct lttng_consumer_stream_list stream_list;
202 struct lttng_consumer_channel_list channel_list;
203 /*
204 * Flag specifying if the local array of FDs needs update in the
205 * poll function. Protected by consumer_data.lock.
206 */
207 unsigned int need_update;
208 enum lttng_consumer_type type;
209 };
210
211 /*
212 * Set the error socket for communication with a session daemon.
213 */
214 extern void lttng_consumer_set_error_sock(
215 struct lttng_consumer_local_data *ctx, int sock);
216
217 /*
218 * Set the command socket path for communication with a session daemon.
219 */
220 extern void lttng_consumer_set_command_sock_path(
221 struct lttng_consumer_local_data *ctx, char *sock);
222
223 /*
224 * Send return code to session daemon.
225 *
226 * Returns the return code of sendmsg : the number of bytes transmitted or -1
227 * on error.
228 */
229 extern int lttng_consumer_send_error(
230 struct lttng_consumer_local_data *ctx, int cmd);
231
232 /*
233 * Called from signal handler to ensure a clean exit.
234 */
235 extern void lttng_consumer_should_exit(
236 struct lttng_consumer_local_data *ctx);
237
238 /*
239 * Cleanup the daemon's socket on exit.
240 */
241 extern void lttng_consumer_cleanup(void);
242
243 /*
244 * Flush pending writes to trace output disk file.
245 */
246 extern void lttng_consumer_sync_trace_file(
247 struct lttng_consumer_stream *stream, off_t orig_offset);
248
249 /*
250 * Poll on the should_quit pipe and the command socket return -1 on error and
251 * should exit, 0 if data is available on the command socket
252 */
253 extern int lttng_consumer_poll_socket(struct pollfd *kconsumer_sockpoll);
254
255 extern int consumer_update_poll_array(
256 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
257 struct lttng_consumer_stream **local_consumer_streams);
258
259 extern struct lttng_consumer_stream *consumer_allocate_stream(
260 int channel_key, int stream_key,
261 int shm_fd, int wait_fd,
262 enum lttng_consumer_stream_state state,
263 uint64_t mmap_len,
264 enum lttng_event_output output,
265 const char *path_name);
266 extern int consumer_add_stream(struct lttng_consumer_stream *stream);
267 extern void consumer_del_stream(struct lttng_consumer_stream *stream);
268 extern void consumer_change_stream_state(int stream_key,
269 enum lttng_consumer_stream_state state);
270 extern void consumer_del_channel(struct lttng_consumer_channel *channel);
271 extern struct lttng_consumer_channel *consumer_allocate_channel(
272 int channel_key,
273 int shm_fd, int wait_fd,
274 uint64_t mmap_len,
275 uint64_t max_sb_size);
276 int consumer_add_channel(struct lttng_consumer_channel *channel);
277
278 extern struct lttng_consumer_local_data *lttng_consumer_create(
279 enum lttng_consumer_type type,
280 int (*buffer_ready)(struct lttng_consumer_stream *stream),
281 int (*recv_channel)(struct lttng_consumer_channel *channel),
282 int (*recv_stream)(struct lttng_consumer_stream *stream),
283 int (*update_stream)(int sessiond_key, uint32_t state));
284 extern void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx);
285 extern int lttng_consumer_on_read_subbuffer_mmap(
286 struct lttng_consumer_local_data *ctx,
287 struct lttng_consumer_stream *stream, unsigned long len);
288 extern int lttng_consumer_on_read_subbuffer_splice(
289 struct lttng_consumer_local_data *ctx,
290 struct lttng_consumer_stream *stream, unsigned long len);
291 extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
292 struct lttng_consumer_stream *stream);
293 extern int lttng_consumer_get_produced_snapshot(
294 struct lttng_consumer_local_data *ctx,
295 struct lttng_consumer_stream *stream,
296 unsigned long *pos);
297 extern void *lttng_consumer_thread_poll_fds(void *data);
298 extern void *lttng_consumer_thread_receive_fds(void *data);
299 extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
300 int sock, struct pollfd *consumer_sockpoll);
301
302 #endif /* _LTTNG_CONSUMER_H */
This page took 0.062266 seconds and 5 git commands to generate.