Fix: Allocate stream hash table in respective threads
[lttng-tools.git] / src / common / consumer.h
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2012 - David Goulet <dgoulet@efficios.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef LIB_CONSUMER_H
21 #define LIB_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 #include <common/sessiond-comm/sessiond-comm.h>
32
33 /* Commands for consumer */
34 enum lttng_consumer_command {
35 LTTNG_CONSUMER_ADD_CHANNEL,
36 LTTNG_CONSUMER_ADD_STREAM,
37 /* pause, delete, active depending on fd state */
38 LTTNG_CONSUMER_UPDATE_STREAM,
39 /* inform the consumer to quit when all fd has hang up */
40 LTTNG_CONSUMER_STOP,
41 LTTNG_CONSUMER_ADD_RELAYD_SOCKET,
42 /* Inform the consumer to kill a specific relayd connection */
43 LTTNG_CONSUMER_DESTROY_RELAYD,
44 /* Return to the sessiond if there is data pending for a session */
45 LTTNG_CONSUMER_DATA_PENDING,
46 };
47
48 /* State of each fd in consumer */
49 enum lttng_consumer_stream_state {
50 LTTNG_CONSUMER_ACTIVE_STREAM,
51 LTTNG_CONSUMER_PAUSE_STREAM,
52 LTTNG_CONSUMER_DELETE_STREAM,
53 };
54
55 enum lttng_consumer_type {
56 LTTNG_CONSUMER_UNKNOWN = 0,
57 LTTNG_CONSUMER_KERNEL,
58 LTTNG_CONSUMER64_UST,
59 LTTNG_CONSUMER32_UST,
60 };
61
62 enum consumer_endpoint_status {
63 CONSUMER_ENDPOINT_ACTIVE,
64 CONSUMER_ENDPOINT_INACTIVE,
65 };
66
67 struct lttng_consumer_channel {
68 struct lttng_ht_node_ulong node;
69 int key;
70 uint64_t max_sb_size; /* the subbuffer size for this channel */
71 int refcount; /* Number of streams referencing this channel */
72 /*
73 * The number of streams to receive initially. Used to guarantee that we do
74 * not destroy a channel before receiving all its associated streams.
75 */
76 unsigned int nb_init_streams;
77
78 /* For UST */
79 int shm_fd;
80 int wait_fd;
81 void *mmap_base;
82 size_t mmap_len;
83 struct lttng_ust_shm_handle *handle;
84 int wait_fd_is_copy;
85 int cpucount;
86 };
87
88 /* Forward declaration for UST. */
89 struct lttng_ust_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 /* HT node used by the data_ht and metadata_ht */
97 struct lttng_ht_node_ulong node;
98 /* HT node used in consumer_data.stream_list_ht */
99 struct lttng_ht_node_ulong node_session_id;
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 int data_read;
122 int hangup_flush_done;
123 /* UID/GID of the user owning the session to which stream belongs */
124 uid_t uid;
125 gid_t gid;
126 /* Network sequence number. Indicating on which relayd socket it goes. */
127 int net_seq_idx;
128 /* Identify if the stream is the metadata */
129 unsigned int metadata_flag;
130 /* Used when the stream is set for network streaming */
131 uint64_t relayd_stream_id;
132 /* Next sequence number to use for trace packet */
133 uint64_t next_net_seq_num;
134 /*
135 * Lock to use the stream FDs since they are used between threads.
136 *
137 * This is nested INSIDE the consumer_data lock.
138 * This is nested OUTSIDE consumer_relayd_sock_pair lock.
139 */
140 pthread_mutex_t lock;
141 /* Tracing session id */
142 uint64_t session_id;
143 /*
144 * Indicates if the stream end point is still active or not (network
145 * streaming or local file system). The thread "owning" the stream is
146 * handling this status and can be notified of a state change through the
147 * consumer data appropriate pipe.
148 */
149 enum consumer_endpoint_status endpoint_status;
150 };
151
152 /*
153 * Internal representation of a relayd socket pair.
154 */
155 struct consumer_relayd_sock_pair {
156 /* Network sequence number. */
157 int net_seq_idx;
158 /* Number of stream associated with this relayd */
159 unsigned int refcount;
160
161 /*
162 * This flag indicates whether or not we should destroy this object. The
163 * destruction should ONLY occurs when this flag is set and the refcount is
164 * set to zero.
165 */
166 unsigned int destroy_flag;
167
168 /*
169 * Mutex protecting the control socket to avoid out of order packets
170 * between threads sending data to the relayd. Since metadata data is sent
171 * over that socket, at least two sendmsg() are needed (header + data)
172 * creating a race for packets to overlap between threads using it.
173 *
174 * This is nested INSIDE the consumer_data lock.
175 * This is nested INSIDE the stream lock.
176 */
177 pthread_mutex_t ctrl_sock_mutex;
178
179 /* Control socket. Command and metadata are passed over it */
180 struct lttcomm_sock control_sock;
181
182 /*
183 * We don't need a mutex at this point since we only splice or write single
184 * large chunk of data with a header appended at the begining. Moreover,
185 * this socket is for now only used in a single thread.
186 */
187 struct lttcomm_sock data_sock;
188 struct lttng_ht_node_ulong node;
189 };
190
191 /*
192 * UST consumer local data to the program. One or more instance per
193 * process.
194 */
195 struct lttng_consumer_local_data {
196 /*
197 * Function to call when data is available on a buffer.
198 * Returns the number of bytes read, or negative error value.
199 */
200 ssize_t (*on_buffer_ready)(struct lttng_consumer_stream *stream,
201 struct lttng_consumer_local_data *ctx);
202 /*
203 * function to call when we receive a new channel, it receives a
204 * newly allocated channel, depending on the return code of this
205 * function, the new channel will be handled by the application
206 * or the library.
207 *
208 * Returns:
209 * > 0 (success, FD is kept by application)
210 * == 0 (success, FD is left to library)
211 * < 0 (error)
212 */
213 int (*on_recv_channel)(struct lttng_consumer_channel *channel);
214 /*
215 * function to call when we receive a new stream, it receives a
216 * newly allocated stream, depending on the return code of this
217 * function, the new stream will be handled by the application
218 * or the library.
219 *
220 * Returns:
221 * > 0 (success, FD is kept by application)
222 * == 0 (success, FD is left to library)
223 * < 0 (error)
224 */
225 int (*on_recv_stream)(struct lttng_consumer_stream *stream);
226 /*
227 * function to call when a stream is getting updated by the session
228 * daemon, this function receives the sessiond key and the new
229 * state, depending on the return code of this function the
230 * update of state for the stream is handled by the application
231 * or the library.
232 *
233 * Returns:
234 * > 0 (success, FD is kept by application)
235 * == 0 (success, FD is left to library)
236 * < 0 (error)
237 */
238 int (*on_update_stream)(int sessiond_key, uint32_t state);
239 /* socket to communicate errors with sessiond */
240 int consumer_error_socket;
241 /* socket to exchange commands with sessiond */
242 char *consumer_command_sock_path;
243 /* communication with splice */
244 int consumer_thread_pipe[2];
245 int consumer_splice_metadata_pipe[2];
246 /* Data stream poll thread pipe. To transfer data stream to the thread */
247 int consumer_data_pipe[2];
248 /* to let the signal handler wake up the fd receiver thread */
249 int consumer_should_quit[2];
250 /* Metadata poll thread pipe. Transfer metadata stream to it */
251 int consumer_metadata_pipe[2];
252 };
253
254 /*
255 * Library-level data. One instance per process.
256 */
257 struct lttng_consumer_global_data {
258 /*
259 * At this time, this lock is used to ensure coherence between the count
260 * and number of element in the hash table. It's also a protection for
261 * concurrent read/write between threads.
262 *
263 * This is nested OUTSIDE the stream lock.
264 * This is nested OUTSIDE the consumer_relayd_sock_pair lock.
265 */
266 pthread_mutex_t lock;
267
268 /*
269 * Number of streams in the data stream hash table declared outside.
270 * Protected by consumer_data.lock.
271 */
272 int stream_count;
273
274 /* Channel hash table protected by consumer_data.lock. */
275 struct lttng_ht *channel_ht;
276 /*
277 * Flag specifying if the local array of FDs needs update in the
278 * poll function. Protected by consumer_data.lock.
279 */
280 unsigned int need_update;
281 enum lttng_consumer_type type;
282
283 /*
284 * Relayd socket(s) hashtable indexed by network sequence number. Each
285 * stream has an index which associate the right relayd socket to use.
286 */
287 struct lttng_ht *relayd_ht;
288
289 /*
290 * This hash table contains all streams (metadata and data) indexed by
291 * session id. In other words, the ht is indexed by session id and each
292 * bucket contains the list of associated streams.
293 *
294 * This HT uses the "node_session_id" of the consumer stream.
295 */
296 struct lttng_ht *stream_list_ht;
297 };
298
299 /*
300 * Init consumer data structures.
301 */
302 extern void lttng_consumer_init(void);
303
304 /*
305 * Set the error socket for communication with a session daemon.
306 */
307 extern void lttng_consumer_set_error_sock(
308 struct lttng_consumer_local_data *ctx, int sock);
309
310 /*
311 * Set the command socket path for communication with a session daemon.
312 */
313 extern void lttng_consumer_set_command_sock_path(
314 struct lttng_consumer_local_data *ctx, char *sock);
315
316 /*
317 * Send return code to session daemon.
318 *
319 * Returns the return code of sendmsg : the number of bytes transmitted or -1
320 * on error.
321 */
322 extern int lttng_consumer_send_error(
323 struct lttng_consumer_local_data *ctx, int cmd);
324
325 /*
326 * Called from signal handler to ensure a clean exit.
327 */
328 extern void lttng_consumer_should_exit(
329 struct lttng_consumer_local_data *ctx);
330
331 /*
332 * Cleanup the daemon's socket on exit.
333 */
334 extern void lttng_consumer_cleanup(void);
335
336 /*
337 * Flush pending writes to trace output disk file.
338 */
339 extern void lttng_consumer_sync_trace_file(
340 struct lttng_consumer_stream *stream, off_t orig_offset);
341
342 /*
343 * Poll on the should_quit pipe and the command socket return -1 on error and
344 * should exit, 0 if data is available on the command socket
345 */
346 extern int lttng_consumer_poll_socket(struct pollfd *kconsumer_sockpoll);
347
348 extern struct lttng_consumer_stream *consumer_allocate_stream(
349 int channel_key, int stream_key,
350 int shm_fd, int wait_fd,
351 enum lttng_consumer_stream_state state,
352 uint64_t mmap_len,
353 enum lttng_event_output output,
354 const char *path_name,
355 uid_t uid,
356 gid_t gid,
357 int net_index,
358 int metadata_flag,
359 uint64_t session_id,
360 int *alloc_ret);
361 extern void consumer_del_stream(struct lttng_consumer_stream *stream,
362 struct lttng_ht *ht);
363 extern void consumer_del_metadata_stream(struct lttng_consumer_stream *stream,
364 struct lttng_ht *ht);
365 extern void consumer_del_channel(struct lttng_consumer_channel *channel);
366 extern struct lttng_consumer_channel *consumer_allocate_channel(
367 int channel_key,
368 int shm_fd, int wait_fd,
369 uint64_t mmap_len,
370 uint64_t max_sb_size,
371 unsigned int nb_init_streams);
372 int consumer_add_channel(struct lttng_consumer_channel *channel);
373
374 /* lttng-relayd consumer command */
375 struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
376 int net_seq_idx);
377 struct consumer_relayd_sock_pair *consumer_find_relayd(int key);
378 int consumer_handle_stream_before_relayd(struct lttng_consumer_stream *stream,
379 size_t data_size);
380 void consumer_steal_stream_key(int key, struct lttng_ht *ht);
381
382 extern struct lttng_consumer_local_data *lttng_consumer_create(
383 enum lttng_consumer_type type,
384 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
385 struct lttng_consumer_local_data *ctx),
386 int (*recv_channel)(struct lttng_consumer_channel *channel),
387 int (*recv_stream)(struct lttng_consumer_stream *stream),
388 int (*update_stream)(int sessiond_key, uint32_t state));
389 extern void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx);
390 extern ssize_t lttng_consumer_on_read_subbuffer_mmap(
391 struct lttng_consumer_local_data *ctx,
392 struct lttng_consumer_stream *stream, unsigned long len,
393 unsigned long padding);
394 extern ssize_t lttng_consumer_on_read_subbuffer_splice(
395 struct lttng_consumer_local_data *ctx,
396 struct lttng_consumer_stream *stream, unsigned long len,
397 unsigned long padding);
398 extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
399 struct lttng_consumer_stream *stream);
400 extern int lttng_consumer_get_produced_snapshot(
401 struct lttng_consumer_local_data *ctx,
402 struct lttng_consumer_stream *stream,
403 unsigned long *pos);
404 extern void *consumer_thread_metadata_poll(void *data);
405 extern void *consumer_thread_data_poll(void *data);
406 extern void *consumer_thread_sessiond_poll(void *data);
407 extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
408 int sock, struct pollfd *consumer_sockpoll);
409
410 ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
411 struct lttng_consumer_local_data *ctx);
412 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream);
413 int consumer_add_relayd_socket(int net_seq_idx, int sock_type,
414 struct lttng_consumer_local_data *ctx, int sock,
415 struct pollfd *consumer_sockpoll, struct lttcomm_sock *relayd_sock);
416 void consumer_flag_relayd_for_destroy(
417 struct consumer_relayd_sock_pair *relayd);
418 int consumer_data_pending(uint64_t id);
419
420 #endif /* LIB_CONSUMER_H */
This page took 0.049044 seconds and 4 git commands to generate.