consumer: introduce channel lock
[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 #include <urcu/list.h>
27
28 #include <lttng/lttng.h>
29
30 #include <common/hashtable/hashtable.h>
31 #include <common/compat/fcntl.h>
32 #include <common/compat/uuid.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/pipe.h>
35
36 /* Commands for consumer */
37 enum lttng_consumer_command {
38 LTTNG_CONSUMER_ADD_CHANNEL,
39 LTTNG_CONSUMER_ADD_STREAM,
40 /* pause, delete, active depending on fd state */
41 LTTNG_CONSUMER_UPDATE_STREAM,
42 /* inform the consumer to quit when all fd has hang up */
43 LTTNG_CONSUMER_STOP,
44 LTTNG_CONSUMER_ADD_RELAYD_SOCKET,
45 /* Inform the consumer to kill a specific relayd connection */
46 LTTNG_CONSUMER_DESTROY_RELAYD,
47 /* Return to the sessiond if there is data pending for a session */
48 LTTNG_CONSUMER_DATA_PENDING,
49 /* Consumer creates a channel and returns it to sessiond. */
50 LTTNG_CONSUMER_ASK_CHANNEL_CREATION,
51 LTTNG_CONSUMER_GET_CHANNEL,
52 LTTNG_CONSUMER_DESTROY_CHANNEL,
53 LTTNG_CONSUMER_PUSH_METADATA,
54 LTTNG_CONSUMER_CLOSE_METADATA,
55 LTTNG_CONSUMER_SETUP_METADATA,
56 LTTNG_CONSUMER_FLUSH_CHANNEL,
57 };
58
59 /* State of each fd in consumer */
60 enum lttng_consumer_stream_state {
61 LTTNG_CONSUMER_ACTIVE_STREAM,
62 LTTNG_CONSUMER_PAUSE_STREAM,
63 LTTNG_CONSUMER_DELETE_STREAM,
64 };
65
66 enum lttng_consumer_type {
67 LTTNG_CONSUMER_UNKNOWN = 0,
68 LTTNG_CONSUMER_KERNEL,
69 LTTNG_CONSUMER64_UST,
70 LTTNG_CONSUMER32_UST,
71 };
72
73 enum consumer_endpoint_status {
74 CONSUMER_ENDPOINT_ACTIVE,
75 CONSUMER_ENDPOINT_INACTIVE,
76 };
77
78 enum consumer_channel_output {
79 CONSUMER_CHANNEL_MMAP = 0,
80 CONSUMER_CHANNEL_SPLICE = 1,
81 };
82
83 enum consumer_channel_type {
84 CONSUMER_CHANNEL_TYPE_METADATA = 0,
85 CONSUMER_CHANNEL_TYPE_DATA = 1,
86 };
87
88 struct stream_list {
89 struct cds_list_head head;
90 unsigned int count;
91 };
92
93 /* Stub. */
94 struct consumer_metadata_cache;
95
96 struct lttng_consumer_channel {
97 /* HT node used for consumer_data.channel_ht */
98 struct lttng_ht_node_u64 node;
99 /* Indexed key. Incremented value in the consumer. */
100 uint64_t key;
101 /* Number of streams referencing this channel */
102 int refcount;
103 /* Tracing session id on the session daemon side. */
104 uint64_t session_id;
105 /*
106 * Session id when requesting metadata to the session daemon for
107 * a session with per-PID buffers.
108 */
109 uint64_t session_id_per_pid;
110 /* Channel trace file path name. */
111 char pathname[PATH_MAX];
112 /* Channel name. */
113 char name[LTTNG_SYMBOL_NAME_LEN];
114 /* UID and GID of the channel. */
115 uid_t uid;
116 gid_t gid;
117 /* Relayd id of the channel. -1ULL if it does not apply. */
118 uint64_t relayd_id;
119 /*
120 * Number of streams NOT initialized yet. This is used in order to not
121 * delete this channel if streams are getting initialized.
122 */
123 unsigned int nb_init_stream_left;
124 /* Output type (mmap or splice). */
125 enum consumer_channel_output output;
126 /* Channel type for stream */
127 enum consumer_channel_type type;
128
129 /* For UST */
130 struct ustctl_consumer_channel *uchan;
131 unsigned char uuid[UUID_STR_LEN];
132 /*
133 * Temporary stream list used to store the streams once created and waiting
134 * to be sent to the session daemon by receiving the
135 * LTTNG_CONSUMER_GET_CHANNEL.
136 */
137 struct stream_list streams;
138 /*
139 * Set if the channel is metadata. We keep a reference to the stream
140 * because we have to flush data once pushed by the session daemon. For a
141 * regular channel, this is always set to NULL.
142 */
143 struct lttng_consumer_stream *metadata_stream;
144
145 /* for UST */
146 int wait_fd;
147 /* Node within channel thread ht */
148 struct lttng_ht_node_u64 wait_fd_node;
149
150 /* Metadata cache is metadata channel */
151 struct consumer_metadata_cache *metadata_cache;
152 /* For metadata periodical flush */
153 int switch_timer_enabled;
154 timer_t switch_timer;
155 int switch_timer_error;
156
157 /* On-disk circular buffer */
158 uint64_t tracefile_size;
159 uint64_t tracefile_count;
160 /*
161 * Channel lock.
162 *
163 * This is nested INSIDE the consumer data lock.
164 * This is nested OUTSIDE the metadata cache lock.
165 * This is nested OUTSIDE stream lock.
166 * This is nested OUTSIDE consumer_relayd_sock_pair lock.
167 */
168 pthread_mutex_t lock;
169 };
170
171 /*
172 * Internal representation of the streams, sessiond_key is used to identify
173 * uniquely a stream.
174 */
175 struct lttng_consumer_stream {
176 /* HT node used by the data_ht and metadata_ht */
177 struct lttng_ht_node_u64 node;
178 /* stream indexed per channel key node */
179 struct lttng_ht_node_u64 node_channel_id;
180 /* HT node used in consumer_data.stream_list_ht */
181 struct lttng_ht_node_u64 node_session_id;
182 /* Pointer to associated channel. */
183 struct lttng_consumer_channel *chan;
184
185 /* Key by which the stream is indexed for 'node'. */
186 uint64_t key;
187 /*
188 * File descriptor of the data output file. This can be either a file or a
189 * socket fd for relayd streaming.
190 */
191 int out_fd; /* output file to write the data */
192 /* Write position in the output file descriptor */
193 off_t out_fd_offset;
194 enum lttng_consumer_stream_state state;
195 int shm_fd_is_copy;
196 int data_read;
197 int hangup_flush_done;
198 enum lttng_event_output output;
199 /* Maximum subbuffer size. */
200 unsigned long max_sb_size;
201
202 /*
203 * Still used by the kernel for MMAP output. For UST, the ustctl getter is
204 * used for the mmap base and offset.
205 */
206 void *mmap_base;
207 unsigned long mmap_len;
208
209 /* For UST */
210
211 int wait_fd;
212 /* UID/GID of the user owning the session to which stream belongs */
213 uid_t uid;
214 gid_t gid;
215 /* Network sequence number. Indicating on which relayd socket it goes. */
216 uint64_t net_seq_idx;
217 /* Identify if the stream is the metadata */
218 unsigned int metadata_flag;
219 /* Used when the stream is set for network streaming */
220 uint64_t relayd_stream_id;
221 /*
222 * When sending a stream packet to a relayd, this number is used to track
223 * the packet sent by the consumer and seen by the relayd. When sending the
224 * data header to the relayd, this number is sent and if the transmission
225 * was successful, it is incremented.
226 *
227 * Even if the full data is not fully transmitted it won't matter since
228 * only two possible error can happen after that where either the relayd
229 * died or a read error is detected on the stream making this value useless
230 * after that.
231 *
232 * This value SHOULD be read/updated atomically or with the lock acquired.
233 */
234 uint64_t next_net_seq_num;
235 /*
236 * Lock to use the stream FDs since they are used between threads.
237 *
238 * This is nested INSIDE the consumer_data lock.
239 * This is nested INSIDE the metadata cache lock.
240 * This is nested INSIDE the channel lock.
241 * This is nested OUTSIDE consumer_relayd_sock_pair lock.
242 */
243 pthread_mutex_t lock;
244 /* Tracing session id */
245 uint64_t session_id;
246 /*
247 * Indicates if the stream end point is still active or not (network
248 * streaming or local file system). The thread "owning" the stream is
249 * handling this status and can be notified of a state change through the
250 * consumer data appropriate pipe.
251 */
252 enum consumer_endpoint_status endpoint_status;
253 /* Stream name. Format is: <channel_name>_<cpu_number> */
254 char name[LTTNG_SYMBOL_NAME_LEN];
255 /* Internal state of libustctl. */
256 struct ustctl_consumer_stream *ustream;
257 struct cds_list_head send_node;
258 /* On-disk circular buffer */
259 uint64_t tracefile_size_current;
260 uint64_t tracefile_count_current;
261 };
262
263 /*
264 * Internal representation of a relayd socket pair.
265 */
266 struct consumer_relayd_sock_pair {
267 /* Network sequence number. */
268 uint64_t net_seq_idx;
269 /* Number of stream associated with this relayd */
270 unsigned int refcount;
271
272 /*
273 * This flag indicates whether or not we should destroy this object. The
274 * destruction should ONLY occurs when this flag is set and the refcount is
275 * set to zero.
276 */
277 unsigned int destroy_flag;
278
279 /*
280 * Mutex protecting the control socket to avoid out of order packets
281 * between threads sending data to the relayd. Since metadata data is sent
282 * over that socket, at least two sendmsg() are needed (header + data)
283 * creating a race for packets to overlap between threads using it.
284 *
285 * This is nested INSIDE the consumer_data lock.
286 * This is nested INSIDE the stream lock.
287 */
288 pthread_mutex_t ctrl_sock_mutex;
289
290 /* Control socket. Command and metadata are passed over it */
291 struct lttcomm_relayd_sock control_sock;
292
293 /*
294 * We don't need a mutex at this point since we only splice or write single
295 * large chunk of data with a header appended at the begining. Moreover,
296 * this socket is for now only used in a single thread.
297 */
298 struct lttcomm_relayd_sock data_sock;
299 struct lttng_ht_node_u64 node;
300
301 /* Session id on both sides for the sockets. */
302 uint64_t relayd_session_id;
303 uint64_t sessiond_session_id;
304 };
305
306 /*
307 * UST consumer local data to the program. One or more instance per
308 * process.
309 */
310 struct lttng_consumer_local_data {
311 /*
312 * Function to call when data is available on a buffer.
313 * Returns the number of bytes read, or negative error value.
314 */
315 ssize_t (*on_buffer_ready)(struct lttng_consumer_stream *stream,
316 struct lttng_consumer_local_data *ctx);
317 /*
318 * function to call when we receive a new channel, it receives a
319 * newly allocated channel, depending on the return code of this
320 * function, the new channel will be handled by the application
321 * or the library.
322 *
323 * Returns:
324 * > 0 (success, FD is kept by application)
325 * == 0 (success, FD is left to library)
326 * < 0 (error)
327 */
328 int (*on_recv_channel)(struct lttng_consumer_channel *channel);
329 /*
330 * function to call when we receive a new stream, it receives a
331 * newly allocated stream, depending on the return code of this
332 * function, the new stream will be handled by the application
333 * or the library.
334 *
335 * Returns:
336 * > 0 (success, FD is kept by application)
337 * == 0 (success, FD is left to library)
338 * < 0 (error)
339 */
340 int (*on_recv_stream)(struct lttng_consumer_stream *stream);
341 /*
342 * function to call when a stream is getting updated by the session
343 * daemon, this function receives the sessiond key and the new
344 * state, depending on the return code of this function the
345 * update of state for the stream is handled by the application
346 * or the library.
347 *
348 * Returns:
349 * > 0 (success, FD is kept by application)
350 * == 0 (success, FD is left to library)
351 * < 0 (error)
352 */
353 int (*on_update_stream)(uint64_t sessiond_key, uint32_t state);
354 enum lttng_consumer_type type;
355 /* socket to communicate errors with sessiond */
356 int consumer_error_socket;
357 /* socket to ask metadata to sessiond */
358 int consumer_metadata_socket;
359 /* socket to exchange commands with sessiond */
360 char *consumer_command_sock_path;
361 /* communication with splice */
362 int consumer_thread_pipe[2];
363 int consumer_channel_pipe[2];
364 int consumer_splice_metadata_pipe[2];
365 /* Data stream poll thread pipe. To transfer data stream to the thread */
366 struct lttng_pipe *consumer_data_pipe;
367 /* to let the signal handler wake up the fd receiver thread */
368 int consumer_should_quit[2];
369 /* Metadata poll thread pipe. Transfer metadata stream to it */
370 struct lttng_pipe *consumer_metadata_pipe;
371 };
372
373 /*
374 * Library-level data. One instance per process.
375 */
376 struct lttng_consumer_global_data {
377 /*
378 * At this time, this lock is used to ensure coherence between the count
379 * and number of element in the hash table. It's also a protection for
380 * concurrent read/write between threads.
381 *
382 * This is nested OUTSIDE the stream lock.
383 * This is nested OUTSIDE the consumer_relayd_sock_pair lock.
384 */
385 pthread_mutex_t lock;
386
387 /*
388 * Number of streams in the data stream hash table declared outside.
389 * Protected by consumer_data.lock.
390 */
391 int stream_count;
392
393 /* Channel hash table protected by consumer_data.lock. */
394 struct lttng_ht *channel_ht;
395 /*
396 * Flag specifying if the local array of FDs needs update in the
397 * poll function. Protected by consumer_data.lock.
398 */
399 unsigned int need_update;
400 enum lttng_consumer_type type;
401
402 /*
403 * Relayd socket(s) hashtable indexed by network sequence number. Each
404 * stream has an index which associate the right relayd socket to use.
405 */
406 struct lttng_ht *relayd_ht;
407
408 /*
409 * This hash table contains all streams (metadata and data) indexed by
410 * session id. In other words, the ht is indexed by session id and each
411 * bucket contains the list of associated streams.
412 *
413 * This HT uses the "node_session_id" of the consumer stream.
414 */
415 struct lttng_ht *stream_list_ht;
416
417 /*
418 * This HT uses the "node_channel_id" of the consumer stream.
419 */
420 struct lttng_ht *stream_per_chan_id_ht;
421 };
422
423 /*
424 * Init consumer data structures.
425 */
426 void lttng_consumer_init(void);
427
428 /*
429 * Set the error socket for communication with a session daemon.
430 */
431 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data *ctx,
432 int sock);
433
434 /*
435 * Set the command socket path for communication with a session daemon.
436 */
437 void lttng_consumer_set_command_sock_path(
438 struct lttng_consumer_local_data *ctx, char *sock);
439
440 /*
441 * Send return code to session daemon.
442 *
443 * Returns the return code of sendmsg : the number of bytes transmitted or -1
444 * on error.
445 */
446 int lttng_consumer_send_error(struct lttng_consumer_local_data *ctx, int cmd);
447
448 /*
449 * Called from signal handler to ensure a clean exit.
450 */
451 void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx);
452
453 /*
454 * Cleanup the daemon's socket on exit.
455 */
456 void lttng_consumer_cleanup(void);
457
458 /*
459 * Flush pending writes to trace output disk file.
460 */
461 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
462 off_t orig_offset);
463
464 /*
465 * Poll on the should_quit pipe and the command socket return -1 on error and
466 * should exit, 0 if data is available on the command socket
467 */
468 int lttng_consumer_poll_socket(struct pollfd *kconsumer_sockpoll);
469
470 struct lttng_consumer_stream *consumer_allocate_stream(uint64_t channel_key,
471 uint64_t stream_key,
472 enum lttng_consumer_stream_state state,
473 const char *channel_name,
474 uid_t uid,
475 gid_t gid,
476 uint64_t relayd_id,
477 uint64_t session_id,
478 int cpu,
479 int *alloc_ret,
480 enum consumer_channel_type type);
481 struct lttng_consumer_channel *consumer_allocate_channel(uint64_t key,
482 uint64_t session_id,
483 const char *pathname,
484 const char *name,
485 uid_t uid,
486 gid_t gid,
487 uint64_t relayd_id,
488 enum lttng_event_output output,
489 uint64_t tracefile_size,
490 uint64_t tracefile_count,
491 uint64_t session_id_per_pid);
492 void consumer_del_stream(struct lttng_consumer_stream *stream,
493 struct lttng_ht *ht);
494 void consumer_del_metadata_stream(struct lttng_consumer_stream *stream,
495 struct lttng_ht *ht);
496 int consumer_add_channel(struct lttng_consumer_channel *channel,
497 struct lttng_consumer_local_data *ctx);
498 void consumer_del_channel(struct lttng_consumer_channel *channel);
499
500 /* lttng-relayd consumer command */
501 struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
502 uint64_t net_seq_idx);
503 struct consumer_relayd_sock_pair *consumer_find_relayd(uint64_t key);
504 struct lttng_consumer_channel *consumer_find_channel(uint64_t key);
505 int consumer_handle_stream_before_relayd(struct lttng_consumer_stream *stream,
506 size_t data_size);
507 void consumer_steal_stream_key(int key, struct lttng_ht *ht);
508
509 struct lttng_consumer_local_data *lttng_consumer_create(
510 enum lttng_consumer_type type,
511 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
512 struct lttng_consumer_local_data *ctx),
513 int (*recv_channel)(struct lttng_consumer_channel *channel),
514 int (*recv_stream)(struct lttng_consumer_stream *stream),
515 int (*update_stream)(uint64_t sessiond_key, uint32_t state));
516 void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx);
517 ssize_t lttng_consumer_on_read_subbuffer_mmap(
518 struct lttng_consumer_local_data *ctx,
519 struct lttng_consumer_stream *stream, unsigned long len,
520 unsigned long padding);
521 ssize_t lttng_consumer_on_read_subbuffer_splice(
522 struct lttng_consumer_local_data *ctx,
523 struct lttng_consumer_stream *stream, unsigned long len,
524 unsigned long padding);
525 int lttng_consumer_take_snapshot(struct lttng_consumer_stream *stream);
526 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
527 unsigned long *pos);
528 void *consumer_thread_metadata_poll(void *data);
529 void *consumer_thread_data_poll(void *data);
530 void *consumer_thread_sessiond_poll(void *data);
531 void *consumer_thread_channel_poll(void *data);
532 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
533 int sock, struct pollfd *consumer_sockpoll);
534
535 ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
536 struct lttng_consumer_local_data *ctx);
537 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream);
538 int consumer_add_relayd_socket(uint64_t net_seq_idx, int sock_type,
539 struct lttng_consumer_local_data *ctx, int sock,
540 struct pollfd *consumer_sockpoll, struct lttcomm_relayd_sock *relayd_sock,
541 uint64_t sessiond_id);
542 void consumer_flag_relayd_for_destroy(
543 struct consumer_relayd_sock_pair *relayd);
544 int consumer_data_pending(uint64_t id);
545 int consumer_send_status_msg(int sock, int ret_code);
546 int consumer_send_status_channel(int sock,
547 struct lttng_consumer_channel *channel);
548 void notify_thread_del_channel(struct lttng_consumer_local_data *ctx,
549 uint64_t key);
550
551 #endif /* LIB_CONSUMER_H */
This page took 0.039629 seconds and 4 git commands to generate.