10e4a5504e19148ac7fe69d10783342090019bc4
[lttng-tools.git] / liblttkconsumerd / lttkconsumerd.h
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #ifndef _LIBLTTKCONSUMERD_H
20 #define _LIBLTTKCONSUMERD_H
21
22 #include <lttng-sessiond-comm.h>
23 #include "lttng-kconsumerd.h"
24
25 /*
26 * When the receiving thread dies, we need to have a way to make
27 * the polling thread exit eventually.
28 * If all FDs hang up (normal case when the ltt-sessiond stops),
29 * we can exit cleanly, but if there is a problem and for whatever
30 * reason some FDs remain open, the consumer should still exit eventually.
31 *
32 * If the timeout is reached, it means that during this period
33 * no events occurred on the FDs so we need to force an exit.
34 * This case should not happen but it is a safety to ensure we won't block
35 * the consumer indefinitely.
36 *
37 * The value of 2 seconds is an arbitrary choice.
38 */
39 #define KCONSUMERD_POLL_GRACE_PERIOD 2000
40
41 struct kconsumerd_fd_list {
42 struct cds_list_head head;
43 };
44
45 /*
46 * Internal representation of the FDs,
47 * sessiond_fd is used to identify uniquely a fd
48 */
49 struct kconsumerd_fd {
50 struct cds_list_head list;
51 int sessiond_fd; /* used to identify uniquely a fd with sessiond */
52 int consumerd_fd; /* fd to consume */
53 int out_fd; /* output file to write the data */
54 off_t out_fd_offset; /* write position in the output file descriptor */
55 char path_name[PATH_MAX]; /* tracefile name */
56 enum kconsumerd_fd_state state;
57 unsigned long max_sb_size; /* the subbuffer size for this channel */
58 };
59
60 struct kconsumerd_local_data {
61 /* function to call when data is available on a buffer */
62 int (*on_buffer_ready)(struct kconsumerd_fd *kconsumerd_fd);
63 /* socket to communicate errors with sessiond */
64 int kconsumerd_error_socket;
65 /* socket to exchange commands with sessiond */
66 char *kconsumerd_command_sock_path;
67 /* communication with splice */
68 int kconsumerd_thread_pipe[2];
69 /* pipe to wake the poll thread when necessary */
70 int kconsumerd_poll_pipe[2];
71 /* to let the signal handler wake up the fd receiver thread */
72 int kconsumerd_should_quit[2];
73 };
74
75 /*
76 * kconsumerd_create
77 * initialise the necessary environnement :
78 * - create a new context
79 * - create the poll_pipe
80 * - create the should_quit pipe (for signal handler)
81 * - create the thread pipe (for splice)
82 * Takes a function pointer as argument, this function is called when data is
83 * available on a buffer. This function is responsible to do the
84 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
85 * buffer configuration and then kernctl_put_next_subbuf at the end.
86 * Returns a pointer to the new context or NULL on error.
87 */
88 struct kconsumerd_local_data *kconsumerd_create(
89 int (*buffer_ready)(struct kconsumerd_fd *kconsumerd_fd));
90
91 /*
92 * kconsumerd_destroy
93 * Close all fds associated with the instance and free the context
94 */
95 void kconsumerd_destroy(struct kconsumerd_local_data *ctx);
96
97 /*
98 * kconsumerd_on_read_subbuffer_mmap
99 * mmap the ring buffer, read it and write the data to the tracefile.
100 * Returns the number of bytes written
101 */
102 int kconsumerd_on_read_subbuffer_mmap(struct kconsumerd_local_data *ctx,
103 struct kconsumerd_fd *kconsumerd_fd, unsigned long len);
104
105 /*
106 * kconsumerd_on_read_subbuffer
107 *
108 * Splice the data from the ring buffer to the tracefile.
109 * Returns the number of bytes spliced
110 */
111 int kconsumerd_on_read_subbuffer_splice(struct kconsumerd_local_data *ctx,
112 struct kconsumerd_fd *kconsumerd_fd, unsigned long len);
113
114 /*
115 * kconsumerd_send_error
116 * send return code to ltt-sessiond
117 * returns the return code of sendmsg : the number of bytes transmitted
118 * or -1 on error.
119 */
120 int kconsumerd_send_error(struct kconsumerd_local_data *ctx,
121 enum lttcomm_return_code cmd);
122
123 /*
124 * kconsumerd_poll_socket
125 * Poll on the should_quit pipe and the command socket
126 * return -1 on error and should exit, 0 if data is
127 * available on the command socket
128 */
129 int kconsumerd_poll_socket(struct pollfd *kconsumerd_sockpoll);
130
131 /*
132 * kconsumerd_thread_poll_fds
133 * This thread polls the fds in the ltt_fd_list to consume the data
134 * and write it to tracefile if necessary.
135 */
136 void *kconsumerd_thread_poll_fds(void *data);
137
138 /*
139 * kconsumerd_thread_receive_fds
140 * This thread listens on the consumerd socket and
141 * receives the file descriptors from ltt-sessiond
142 */
143 void *kconsumerd_thread_receive_fds(void *data);
144
145 /*
146 * kconsumerd_should_exit
147 * Called from signal handler to ensure a clean exit
148 */
149 void kconsumerd_should_exit(struct kconsumerd_local_data *ctx);
150
151 /*
152 * kconsumerd_cleanup
153 * Cleanup the daemon's socket on exit
154 */
155 void kconsumerd_cleanup(void);
156
157 /*
158 * kconsumerd_set_error_socket
159 * Set the error socket for communication with a session daemon
160 */
161 void kconsumerd_set_error_socket(struct kconsumerd_local_data *ctx, int sock);
162
163 /*
164 * kconsumerd_set_command_socket_path
165 * Set the command socket path for communication with a session daemon
166 */
167 void kconsumerd_set_command_socket_path(struct kconsumerd_local_data *ctx, char *sock);
168
169 #endif /* _LIBLTTKCONSUMERD_H */
This page took 0.032026 seconds and 3 git commands to generate.