Register the consuming function and add a library context
[lttng-tools.git] / liblttkconsumerd / lttkconsumerd.h
CommitLineData
d4a1283e
JD
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
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
d4a1283e
JD
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
1ce86c9a
JD
19#ifndef _LIBLTTKCONSUMERD_H
20#define _LIBLTTKCONSUMERD_H
d4a1283e 21
e88129fc 22#include <lttng-sessiond-comm.h>
5dc18550
DG
23#include "lttng-kconsumerd.h"
24
1ce86c9a
JD
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
41struct kconsumerd_fd_list {
d4a1283e
JD
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 */
1ce86c9a 49struct kconsumerd_fd {
d4a1283e
JD
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 */
5dc18550 56 enum kconsumerd_fd_state state;
d4a1283e
JD
57 unsigned long max_sb_size; /* the subbuffer size for this channel */
58};
59
cb040cc1
JD
60struct 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
c72c2927 75/*
cb040cc1 76 * kconsumerd_create
c72c2927 77 * initialise the necessary environnement :
cb040cc1 78 * - create a new context
c72c2927
JD
79 * - create the poll_pipe
80 * - create the should_quit pipe (for signal handler)
cb040cc1
JD
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 */
88struct 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 */
95void 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 */
102int 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
c72c2927 110 */
cb040cc1
JD
111int kconsumerd_on_read_subbuffer_splice(struct kconsumerd_local_data *ctx,
112 struct kconsumerd_fd *kconsumerd_fd, unsigned long len);
c72c2927
JD
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 */
cb040cc1
JD
120int kconsumerd_send_error(struct kconsumerd_local_data *ctx,
121 enum lttcomm_return_code cmd);
c72c2927
JD
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 */
4de84ad9 129int kconsumerd_poll_socket(struct pollfd *kconsumerd_sockpoll);
c72c2927
JD
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 */
1ce86c9a 136void *kconsumerd_thread_poll_fds(void *data);
c72c2927
JD
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 */
1ce86c9a 143void *kconsumerd_thread_receive_fds(void *data);
c72c2927
JD
144
145/*
146 * kconsumerd_should_exit
147 * Called from signal handler to ensure a clean exit
148 */
cb040cc1 149void kconsumerd_should_exit(struct kconsumerd_local_data *ctx);
c72c2927
JD
150
151/*
152 * kconsumerd_cleanup
153 * Cleanup the daemon's socket on exit
154 */
3dcd2721 155void kconsumerd_cleanup(void);
c72c2927
JD
156
157/*
158 * kconsumerd_set_error_socket
159 * Set the error socket for communication with a session daemon
160 */
cb040cc1 161void kconsumerd_set_error_socket(struct kconsumerd_local_data *ctx, int sock);
c72c2927
JD
162
163/*
164 * kconsumerd_set_command_socket_path
165 * Set the command socket path for communication with a session daemon
166 */
cb040cc1 167void kconsumerd_set_command_socket_path(struct kconsumerd_local_data *ctx, char *sock);
1ce86c9a
JD
168
169#endif /* _LIBLTTKCONSUMERD_H */
This page took 0.029776 seconds and 4 git commands to generate.