Commit | Line | Data |
---|---|---|
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 | ||
41 | struct 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 | 49 | struct 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 | 57 | unsigned long max_sb_size; /* the subbuffer size for this channel */ |
8b270bdb JD |
58 | void *mmap_base; |
59 | size_t mmap_len; | |
60 | enum lttng_event_output output; /* splice or mmap */ | |
d4a1283e JD |
61 | }; |
62 | ||
cb040cc1 JD |
63 | struct kconsumerd_local_data { |
64 | /* function to call when data is available on a buffer */ | |
65 | int (*on_buffer_ready)(struct kconsumerd_fd *kconsumerd_fd); | |
66 | /* socket to communicate errors with sessiond */ | |
67 | int kconsumerd_error_socket; | |
68 | /* socket to exchange commands with sessiond */ | |
69 | char *kconsumerd_command_sock_path; | |
70 | /* communication with splice */ | |
71 | int kconsumerd_thread_pipe[2]; | |
72 | /* pipe to wake the poll thread when necessary */ | |
73 | int kconsumerd_poll_pipe[2]; | |
74 | /* to let the signal handler wake up the fd receiver thread */ | |
75 | int kconsumerd_should_quit[2]; | |
76 | }; | |
77 | ||
c72c2927 | 78 | /* |
cb040cc1 | 79 | * kconsumerd_create |
c72c2927 | 80 | * initialise the necessary environnement : |
cb040cc1 | 81 | * - create a new context |
c72c2927 JD |
82 | * - create the poll_pipe |
83 | * - create the should_quit pipe (for signal handler) | |
cb040cc1 JD |
84 | * - create the thread pipe (for splice) |
85 | * Takes a function pointer as argument, this function is called when data is | |
86 | * available on a buffer. This function is responsible to do the | |
87 | * kernctl_get_next_subbuf, read the data with mmap or splice depending on the | |
88 | * buffer configuration and then kernctl_put_next_subbuf at the end. | |
89 | * Returns a pointer to the new context or NULL on error. | |
90 | */ | |
91 | struct kconsumerd_local_data *kconsumerd_create( | |
92 | int (*buffer_ready)(struct kconsumerd_fd *kconsumerd_fd)); | |
93 | ||
94 | /* | |
95 | * kconsumerd_destroy | |
96 | * Close all fds associated with the instance and free the context | |
97 | */ | |
98 | void kconsumerd_destroy(struct kconsumerd_local_data *ctx); | |
99 | ||
100 | /* | |
101 | * kconsumerd_on_read_subbuffer_mmap | |
102 | * mmap the ring buffer, read it and write the data to the tracefile. | |
103 | * Returns the number of bytes written | |
104 | */ | |
105 | int kconsumerd_on_read_subbuffer_mmap(struct kconsumerd_local_data *ctx, | |
106 | struct kconsumerd_fd *kconsumerd_fd, unsigned long len); | |
107 | ||
108 | /* | |
109 | * kconsumerd_on_read_subbuffer | |
110 | * | |
111 | * Splice the data from the ring buffer to the tracefile. | |
112 | * Returns the number of bytes spliced | |
c72c2927 | 113 | */ |
cb040cc1 JD |
114 | int kconsumerd_on_read_subbuffer_splice(struct kconsumerd_local_data *ctx, |
115 | struct kconsumerd_fd *kconsumerd_fd, unsigned long len); | |
c72c2927 JD |
116 | |
117 | /* | |
118 | * kconsumerd_send_error | |
119 | * send return code to ltt-sessiond | |
120 | * returns the return code of sendmsg : the number of bytes transmitted | |
121 | * or -1 on error. | |
122 | */ | |
cb040cc1 JD |
123 | int kconsumerd_send_error(struct kconsumerd_local_data *ctx, |
124 | enum lttcomm_return_code cmd); | |
c72c2927 JD |
125 | |
126 | /* | |
127 | * kconsumerd_poll_socket | |
128 | * Poll on the should_quit pipe and the command socket | |
129 | * return -1 on error and should exit, 0 if data is | |
130 | * available on the command socket | |
131 | */ | |
4de84ad9 | 132 | int kconsumerd_poll_socket(struct pollfd *kconsumerd_sockpoll); |
c72c2927 JD |
133 | |
134 | /* | |
135 | * kconsumerd_thread_poll_fds | |
136 | * This thread polls the fds in the ltt_fd_list to consume the data | |
137 | * and write it to tracefile if necessary. | |
138 | */ | |
1ce86c9a | 139 | void *kconsumerd_thread_poll_fds(void *data); |
c72c2927 JD |
140 | |
141 | /* | |
142 | * kconsumerd_thread_receive_fds | |
143 | * This thread listens on the consumerd socket and | |
144 | * receives the file descriptors from ltt-sessiond | |
145 | */ | |
1ce86c9a | 146 | void *kconsumerd_thread_receive_fds(void *data); |
c72c2927 JD |
147 | |
148 | /* | |
149 | * kconsumerd_should_exit | |
150 | * Called from signal handler to ensure a clean exit | |
151 | */ | |
cb040cc1 | 152 | void kconsumerd_should_exit(struct kconsumerd_local_data *ctx); |
c72c2927 JD |
153 | |
154 | /* | |
155 | * kconsumerd_cleanup | |
156 | * Cleanup the daemon's socket on exit | |
157 | */ | |
3dcd2721 | 158 | void kconsumerd_cleanup(void); |
c72c2927 JD |
159 | |
160 | /* | |
161 | * kconsumerd_set_error_socket | |
162 | * Set the error socket for communication with a session daemon | |
163 | */ | |
cb040cc1 | 164 | void kconsumerd_set_error_socket(struct kconsumerd_local_data *ctx, int sock); |
c72c2927 JD |
165 | |
166 | /* | |
167 | * kconsumerd_set_command_socket_path | |
168 | * Set the command socket path for communication with a session daemon | |
169 | */ | |
cb040cc1 | 170 | void kconsumerd_set_command_socket_path(struct kconsumerd_local_data *ctx, char *sock); |
1ce86c9a JD |
171 | |
172 | #endif /* _LIBLTTKCONSUMERD_H */ |