35a72b50f6078e333f1fe754de5f183417b71767
[lttng-tools.git] / src / common / 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 <unistd.h>
26
27 #include <lttng/lttng.h>
28
29 #include <common/hashtable/hashtable.h>
30 #include <common/compat/fcntl.h>
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
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
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 */
48 enum 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 */
58 enum lttng_consumer_stream_state {
59 LTTNG_CONSUMER_ACTIVE_STREAM,
60 LTTNG_CONSUMER_PAUSE_STREAM,
61 LTTNG_CONSUMER_DELETE_STREAM,
62 };
63
64 enum lttng_consumer_type {
65 LTTNG_CONSUMER_UNKNOWN = 0,
66 LTTNG_CONSUMER_KERNEL,
67 LTTNG_CONSUMER64_UST,
68 LTTNG_CONSUMER32_UST,
69 };
70
71 struct lttng_consumer_channel {
72 struct lttng_ht_node_ulong node;
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;
81 struct lttng_ust_shm_handle *handle;
82 int nr_streams;
83 int wait_fd_is_copy;
84 int cpucount;
85 };
86
87 /* Forward declaration for UST. */
88 struct lttng_ust_lib_ring_buffer;
89
90 /*
91 * Internal representation of the streams, sessiond_key is used to identify
92 * uniquely a stream.
93 */
94 struct lttng_consumer_stream {
95 struct lttng_ht_node_ulong node;
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 */
112 int shm_fd_is_copy;
113 int wait_fd_is_copy;
114 /* For UST */
115 struct lttng_ust_lib_ring_buffer *buf;
116 int cpu;
117 int hangup_flush_done;
118 /* UID/GID of the user owning the session to which stream belongs */
119 uid_t uid;
120 gid_t gid;
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 struct lttng_consumer_local_data *ctx);
131 /*
132 * function to call when we receive a new channel, it receives a
133 * newly allocated channel, depending on the return code of this
134 * function, the new channel will be handled by the application
135 * or the library.
136 *
137 * Returns:
138 * > 0 (success, FD is kept by application)
139 * == 0 (success, FD is left to library)
140 * < 0 (error)
141 */
142 int (*on_recv_channel)(struct lttng_consumer_channel *channel);
143 /*
144 * function to call when we receive a new stream, it receives a
145 * newly allocated stream, depending on the return code of this
146 * function, the new stream will be handled by the application
147 * or the library.
148 *
149 * Returns:
150 * > 0 (success, FD is kept by application)
151 * == 0 (success, FD is left to library)
152 * < 0 (error)
153 */
154 int (*on_recv_stream)(struct lttng_consumer_stream *stream);
155 /*
156 * function to call when a stream is getting updated by the session
157 * daemon, this function receives the sessiond key and the new
158 * state, depending on the return code of this function the
159 * update of state for the stream is handled by the application
160 * or the library.
161 *
162 * Returns:
163 * > 0 (success, FD is kept by application)
164 * == 0 (success, FD is left to library)
165 * < 0 (error)
166 */
167 int (*on_update_stream)(int sessiond_key, uint32_t state);
168 /* socket to communicate errors with sessiond */
169 int consumer_error_socket;
170 /* socket to exchange commands with sessiond */
171 char *consumer_command_sock_path;
172 /* communication with splice */
173 int consumer_thread_pipe[2];
174 /* pipe to wake the poll thread when necessary */
175 int consumer_poll_pipe[2];
176 /* to let the signal handler wake up the fd receiver thread */
177 int consumer_should_quit[2];
178 };
179
180 /*
181 * Library-level data. One instance per process.
182 */
183 struct lttng_consumer_global_data {
184
185 /*
186 * At this time, this lock is used to ensure coherence between the count
187 * and number of element in the hash table. It's also a protection for
188 * concurrent read/write between threads.
189 *
190 * XXX: We need to see if this lock is still needed with the lockless RCU
191 * hash tables.
192 */
193 pthread_mutex_t lock;
194
195 /*
196 * Number of streams in the hash table. Protected by consumer_data.lock.
197 */
198 int stream_count;
199 /*
200 * Hash tables of streams and channels. Protected by consumer_data.lock.
201 */
202 struct lttng_ht *stream_ht;
203 struct lttng_ht *channel_ht;
204 /*
205 * Flag specifying if the local array of FDs needs update in the
206 * poll function. Protected by consumer_data.lock.
207 */
208 unsigned int need_update;
209 enum lttng_consumer_type type;
210 };
211
212 /*
213 * Init consumer data structures.
214 */
215 extern void lttng_consumer_init(void);
216
217 /*
218 * Set the error socket for communication with a session daemon.
219 */
220 extern void lttng_consumer_set_error_sock(
221 struct lttng_consumer_local_data *ctx, int sock);
222
223 /*
224 * Set the command socket path for communication with a session daemon.
225 */
226 extern void lttng_consumer_set_command_sock_path(
227 struct lttng_consumer_local_data *ctx, char *sock);
228
229 /*
230 * Send return code to session daemon.
231 *
232 * Returns the return code of sendmsg : the number of bytes transmitted or -1
233 * on error.
234 */
235 extern int lttng_consumer_send_error(
236 struct lttng_consumer_local_data *ctx, int cmd);
237
238 /*
239 * Called from signal handler to ensure a clean exit.
240 */
241 extern void lttng_consumer_should_exit(
242 struct lttng_consumer_local_data *ctx);
243
244 /*
245 * Cleanup the daemon's socket on exit.
246 */
247 extern void lttng_consumer_cleanup(void);
248
249 /*
250 * Flush pending writes to trace output disk file.
251 */
252 extern void lttng_consumer_sync_trace_file(
253 struct lttng_consumer_stream *stream, off_t orig_offset);
254
255 /*
256 * Poll on the should_quit pipe and the command socket return -1 on error and
257 * should exit, 0 if data is available on the command socket
258 */
259 extern int lttng_consumer_poll_socket(struct pollfd *kconsumer_sockpoll);
260
261 extern int consumer_update_poll_array(
262 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
263 struct lttng_consumer_stream **local_consumer_streams);
264
265 extern struct lttng_consumer_stream *consumer_allocate_stream(
266 int channel_key, int stream_key,
267 int shm_fd, int wait_fd,
268 enum lttng_consumer_stream_state state,
269 uint64_t mmap_len,
270 enum lttng_event_output output,
271 const char *path_name,
272 uid_t uid,
273 gid_t gid);
274 extern int consumer_add_stream(struct lttng_consumer_stream *stream);
275 extern void consumer_del_stream(struct lttng_consumer_stream *stream);
276 extern void consumer_change_stream_state(int stream_key,
277 enum lttng_consumer_stream_state state);
278 extern void consumer_del_channel(struct lttng_consumer_channel *channel);
279 extern struct lttng_consumer_channel *consumer_allocate_channel(
280 int channel_key,
281 int shm_fd, int wait_fd,
282 uint64_t mmap_len,
283 uint64_t max_sb_size);
284 int consumer_add_channel(struct lttng_consumer_channel *channel);
285
286 extern struct lttng_consumer_local_data *lttng_consumer_create(
287 enum lttng_consumer_type type,
288 int (*buffer_ready)(struct lttng_consumer_stream *stream,
289 struct lttng_consumer_local_data *ctx),
290 int (*recv_channel)(struct lttng_consumer_channel *channel),
291 int (*recv_stream)(struct lttng_consumer_stream *stream),
292 int (*update_stream)(int sessiond_key, uint32_t state));
293 extern void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx);
294 extern int lttng_consumer_on_read_subbuffer_mmap(
295 struct lttng_consumer_local_data *ctx,
296 struct lttng_consumer_stream *stream, unsigned long len);
297 extern int lttng_consumer_on_read_subbuffer_splice(
298 struct lttng_consumer_local_data *ctx,
299 struct lttng_consumer_stream *stream, unsigned long len);
300 extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
301 struct lttng_consumer_stream *stream);
302 extern int lttng_consumer_get_produced_snapshot(
303 struct lttng_consumer_local_data *ctx,
304 struct lttng_consumer_stream *stream,
305 unsigned long *pos);
306 extern void *lttng_consumer_thread_poll_fds(void *data);
307 extern void *lttng_consumer_thread_receive_fds(void *data);
308 extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
309 int sock, struct pollfd *consumer_sockpoll);
310
311 int lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
312 struct lttng_consumer_local_data *ctx);
313 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream);
314
315 #endif /* _LTTNG_CONSUMER_H */
This page took 0.049633 seconds and 3 git commands to generate.