| 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 | /* |
| 83 | * function to call when we receive a new fd, it receives a |
| 84 | * newly allocated kconsumerd_fd, depending on the return code |
| 85 | * of this function, the new FD will be handled by the |
| 86 | * application or the library. |
| 87 | * |
| 88 | * Returns: |
| 89 | * > 0 (success, FD is kept by application) |
| 90 | * == 0 (success, FD is left to library) |
| 91 | * < 0 (error) |
| 92 | */ |
| 93 | int (*on_recv_fd)(struct lttng_kconsumerd_fd *kconsumerd_fd); |
| 94 | /* |
| 95 | * function to call when a FD is getting updated by the session |
| 96 | * daemon, this function receives the FD as seen by the session |
| 97 | * daemon (sessiond_fd) and the new state, depending on the |
| 98 | * return code of this function the update of state for the FD |
| 99 | * is handled by the application or the library. |
| 100 | * |
| 101 | * Returns: |
| 102 | * > 0 (success, FD is kept by application) |
| 103 | * == 0 (success, FD is left to library) |
| 104 | * < 0 (error) |
| 105 | */ |
| 106 | int (*on_update_fd)(int sessiond_fd, uint32_t state); |
| 107 | /* socket to communicate errors with sessiond */ |
| 108 | int kconsumerd_error_socket; |
| 109 | /* socket to exchange commands with sessiond */ |
| 110 | char *kconsumerd_command_sock_path; |
| 111 | /* communication with splice */ |
| 112 | int kconsumerd_thread_pipe[2]; |
| 113 | /* pipe to wake the poll thread when necessary */ |
| 114 | int kconsumerd_poll_pipe[2]; |
| 115 | /* to let the signal handler wake up the fd receiver thread */ |
| 116 | int kconsumerd_should_quit[2]; |
| 117 | }; |
| 118 | |
| 119 | /* |
| 120 | * Initialise the necessary environnement: |
| 121 | * - create a new context |
| 122 | * - create the poll_pipe |
| 123 | * - create the should_quit pipe (for signal handler) |
| 124 | * - create the thread pipe (for splice) |
| 125 | * |
| 126 | * Takes the function pointers to the on_buffer_ready, on_recv_fd, and |
| 127 | * on_update_fd callbacks. |
| 128 | * |
| 129 | * Returns a pointer to the new context or NULL on error. |
| 130 | */ |
| 131 | extern struct lttng_kconsumerd_local_data *lttng_kconsumerd_create( |
| 132 | int (*buffer_ready)(struct lttng_kconsumerd_fd *kconsumerd_fd), |
| 133 | int (*recv_fd)(struct lttng_kconsumerd_fd *kconsumerd_fd), |
| 134 | int (*update_fd)(int sessiond_fd, uint32_t state)); |
| 135 | |
| 136 | /* |
| 137 | * Close all fds associated with the instance and free the context. |
| 138 | */ |
| 139 | extern void lttng_kconsumerd_destroy(struct lttng_kconsumerd_local_data *ctx); |
| 140 | |
| 141 | /* |
| 142 | * Mmap the ring buffer, read it and write the data to the tracefile. |
| 143 | * |
| 144 | * Returns the number of bytes written. |
| 145 | */ |
| 146 | extern int lttng_kconsumerd_on_read_subbuffer_mmap( |
| 147 | struct lttng_kconsumerd_local_data *ctx, |
| 148 | struct lttng_kconsumerd_fd *kconsumerd_fd, unsigned long len); |
| 149 | |
| 150 | /* |
| 151 | * Splice the data from the ring buffer to the tracefile. |
| 152 | * |
| 153 | * Returns the number of bytes spliced. |
| 154 | */ |
| 155 | extern int lttng_kconsumerd_on_read_subbuffer_splice( |
| 156 | struct lttng_kconsumerd_local_data *ctx, |
| 157 | struct lttng_kconsumerd_fd *kconsumerd_fd, unsigned long len); |
| 158 | |
| 159 | /* |
| 160 | * Take a snapshot for a specific fd |
| 161 | * |
| 162 | * Returns 0 on success, < 0 on error |
| 163 | */ |
| 164 | int lttng_kconsumerd_take_snapshot(struct lttng_kconsumerd_local_data *ctx, |
| 165 | struct lttng_kconsumerd_fd *kconsumerd_fd); |
| 166 | |
| 167 | /* |
| 168 | * Get the produced position |
| 169 | * |
| 170 | * Returns 0 on success, < 0 on error |
| 171 | */ |
| 172 | int lttng_kconsumerd_get_produced_snapshot( |
| 173 | struct lttng_kconsumerd_local_data *ctx, |
| 174 | struct lttng_kconsumerd_fd *kconsumerd_fd, |
| 175 | unsigned long *pos); |
| 176 | |
| 177 | /* |
| 178 | * Send return code to session daemon. |
| 179 | * |
| 180 | * Returns the return code of sendmsg : the number of bytes transmitted or -1 |
| 181 | * on error. |
| 182 | */ |
| 183 | extern int lttng_kconsumerd_send_error( |
| 184 | struct lttng_kconsumerd_local_data *ctx, int cmd); |
| 185 | |
| 186 | /* |
| 187 | * Poll on the should_quit pipe and the command socket return -1 on error and |
| 188 | * should exit, 0 if data is available on the command socket. |
| 189 | */ |
| 190 | extern int lttng_kconsumerd_poll_socket(struct pollfd *kconsumerd_sockpoll); |
| 191 | |
| 192 | /* |
| 193 | * This thread polls the fds in the ltt_fd_list to consume the data and write |
| 194 | * it to tracefile if necessary. |
| 195 | */ |
| 196 | extern void *lttng_kconsumerd_thread_poll_fds(void *data); |
| 197 | |
| 198 | /* |
| 199 | * This thread listens on the consumerd socket and receives the file |
| 200 | * descriptors from ltt-sessiond. |
| 201 | */ |
| 202 | extern void *lttng_kconsumerd_thread_receive_fds(void *data); |
| 203 | |
| 204 | /* |
| 205 | * Called from signal handler to ensure a clean exit. |
| 206 | */ |
| 207 | extern void lttng_kconsumerd_should_exit( |
| 208 | struct lttng_kconsumerd_local_data *ctx); |
| 209 | |
| 210 | /* |
| 211 | * Cleanup the daemon's socket on exit. |
| 212 | */ |
| 213 | extern void lttng_kconsumerd_cleanup(void); |
| 214 | |
| 215 | /* |
| 216 | * Set the error socket for communication with a session daemon. |
| 217 | */ |
| 218 | extern void lttng_kconsumerd_set_error_sock( |
| 219 | struct lttng_kconsumerd_local_data *ctx, int sock); |
| 220 | |
| 221 | /* |
| 222 | * Set the command socket path for communication with a session daemon. |
| 223 | */ |
| 224 | extern void lttng_kconsumerd_set_command_sock_path( |
| 225 | struct lttng_kconsumerd_local_data *ctx, char *sock); |
| 226 | |
| 227 | #endif /* _LTTNG_KCONSUMERD_H */ |