8d16d5670e353072e00a608a989579e6149fd47f
[lttng-tools.git] / include / lttng / lttng-kconsumerd.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 _LTTNG_KCONSUMERD_H
20 #define _LTTNG_KCONSUMERD_H
21
22 #include <limits.h>
23 #include <lttng/lttng.h>
24
25 /*
26 * When the receiving thread dies, we need to have a way to make the polling
27 * thread exit eventually. If all FDs hang up (normal case when the
28 * ltt-sessiond stops), we can exit cleanly, but if there is a problem and for
29 * whatever reason some FDs remain open, the consumer should still exit
30 * eventually.
31 *
32 * If the timeout is reached, it means that during this period no events
33 * occurred on the FDs so we need to force an exit. This case should not happen
34 * but it is a safety to ensure we won't block the consumer indefinitely.
35 *
36 * The value of 2 seconds is an arbitrary choice.
37 */
38 #define LTTNG_KCONSUMERD_POLL_GRACE_PERIOD 2000
39
40 /* Commands for kconsumerd */
41 enum lttng_kconsumerd_command {
42 ADD_STREAM,
43 UPDATE_STREAM, /* pause, delete, active depending on fd state */
44 STOP, /* inform the kconsumerd to quit when all fd has hang up */
45 };
46
47 /* State of each fd in consumerd */
48 enum lttng_kconsumerd_fd_state {
49 ACTIVE_FD,
50 PAUSE_FD,
51 DELETE_FD,
52 };
53
54 struct lttng_kconsumerd_fd_list {
55 struct cds_list_head head;
56 };
57
58 /*
59 * Internal representation of the FDs, sessiond_fd is used to identify uniquely
60 * a fd
61 */
62 struct lttng_kconsumerd_fd {
63 struct cds_list_head list;
64 int sessiond_fd; /* used to identify uniquely a fd with sessiond */
65 int consumerd_fd; /* fd to consume */
66 int out_fd; /* output file to write the data */
67 off_t out_fd_offset; /* write position in the output file descriptor */
68 char path_name[PATH_MAX]; /* tracefile name */
69 enum lttng_kconsumerd_fd_state state;
70 unsigned long max_sb_size; /* the subbuffer size for this channel */
71 void *mmap_base;
72 size_t mmap_len;
73 enum lttng_event_output output; /* splice or mmap */
74 };
75
76 /*
77 * Kernel consumer local data to the program.
78 */
79 struct lttng_kconsumerd_local_data {
80 /* function to call when data is available on a buffer */
81 int (*on_buffer_ready)(struct lttng_kconsumerd_fd *kconsumerd_fd);
82 /* socket to communicate errors with sessiond */
83 int kconsumerd_error_socket;
84 /* socket to exchange commands with sessiond */
85 char *kconsumerd_command_sock_path;
86 /* communication with splice */
87 int kconsumerd_thread_pipe[2];
88 /* pipe to wake the poll thread when necessary */
89 int kconsumerd_poll_pipe[2];
90 /* to let the signal handler wake up the fd receiver thread */
91 int kconsumerd_should_quit[2];
92 };
93
94 /*
95 * Initialise the necessary environnement:
96 * - create a new context
97 * - create the poll_pipe
98 * - create the should_quit pipe (for signal handler)
99 * - create the thread pipe (for splice)
100 *
101 * Takes a function pointer as argument, this function is called when data is
102 * available on a buffer. This function is responsible to do the
103 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
104 * buffer configuration and then kernctl_put_next_subbuf at the end.
105 *
106 * Returns a pointer to the new context or NULL on error.
107 */
108 extern struct lttng_kconsumerd_local_data *lttng_kconsumerd_create(
109 int (*buffer_ready)(struct lttng_kconsumerd_fd *kconsumerd_fd));
110
111 /*
112 * Close all fds associated with the instance and free the context.
113 */
114 extern void lttng_kconsumerd_destroy(struct lttng_kconsumerd_local_data *ctx);
115
116 /*
117 * Mmap the ring buffer, read it and write the data to the tracefile.
118 *
119 * Returns the number of bytes written.
120 */
121 extern int lttng_kconsumerd_on_read_subbuffer_mmap(
122 struct lttng_kconsumerd_local_data *ctx,
123 struct lttng_kconsumerd_fd *kconsumerd_fd, unsigned long len);
124
125 /*
126 * Splice the data from the ring buffer to the tracefile.
127 *
128 * Returns the number of bytes spliced.
129 */
130 extern int lttng_kconsumerd_on_read_subbuffer_splice(
131 struct lttng_kconsumerd_local_data *ctx,
132 struct lttng_kconsumerd_fd *kconsumerd_fd, unsigned long len);
133
134 /*
135 * Send return code to session daemon.
136 *
137 * Returns the return code of sendmsg : the number of bytes transmitted or -1
138 * on error.
139 */
140 extern int lttng_kconsumerd_send_error(
141 struct lttng_kconsumerd_local_data *ctx, int cmd);
142
143 /*
144 * Poll on the should_quit pipe and the command socket return -1 on error and
145 * should exit, 0 if data is available on the command socket.
146 */
147 extern int lttng_kconsumerd_poll_socket(struct pollfd *kconsumerd_sockpoll);
148
149 /*
150 * This thread polls the fds in the ltt_fd_list to consume the data and write
151 * it to tracefile if necessary.
152 */
153 extern void *lttng_kconsumerd_thread_poll_fds(void *data);
154
155 /*
156 * This thread listens on the consumerd socket and receives the file
157 * descriptors from ltt-sessiond.
158 */
159 extern void *lttng_kconsumerd_thread_receive_fds(void *data);
160
161 /*
162 * Called from signal handler to ensure a clean exit.
163 */
164 extern void lttng_kconsumerd_should_exit(
165 struct lttng_kconsumerd_local_data *ctx);
166
167 /*
168 * Cleanup the daemon's socket on exit.
169 */
170 extern void lttng_kconsumerd_cleanup(void);
171
172 /*
173 * Set the error socket for communication with a session daemon.
174 */
175 extern void lttng_kconsumerd_set_error_sock(
176 struct lttng_kconsumerd_local_data *ctx, int sock);
177
178 /*
179 * Set the command socket path for communication with a session daemon.
180 */
181 extern void lttng_kconsumerd_set_command_sock_path(
182 struct lttng_kconsumerd_local_data *ctx, char *sock);
183
184 #endif /* _LTTNG_KCONSUMERD_H */
This page took 0.031899 seconds and 3 git commands to generate.