| 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 | void *mmap_base; |
| 59 | size_t mmap_len; |
| 60 | enum lttng_event_output output; /* splice or mmap */ |
| 61 | }; |
| 62 | |
| 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 | |
| 78 | /* |
| 79 | * kconsumerd_create |
| 80 | * initialise the necessary environnement : |
| 81 | * - create a new context |
| 82 | * - create the poll_pipe |
| 83 | * - create the should_quit pipe (for signal handler) |
| 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 |
| 113 | */ |
| 114 | int kconsumerd_on_read_subbuffer_splice(struct kconsumerd_local_data *ctx, |
| 115 | struct kconsumerd_fd *kconsumerd_fd, unsigned long len); |
| 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 | */ |
| 123 | int kconsumerd_send_error(struct kconsumerd_local_data *ctx, |
| 124 | enum lttcomm_return_code cmd); |
| 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 | */ |
| 132 | int kconsumerd_poll_socket(struct pollfd *kconsumerd_sockpoll); |
| 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 | */ |
| 139 | void *kconsumerd_thread_poll_fds(void *data); |
| 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 | */ |
| 146 | void *kconsumerd_thread_receive_fds(void *data); |
| 147 | |
| 148 | /* |
| 149 | * kconsumerd_should_exit |
| 150 | * Called from signal handler to ensure a clean exit |
| 151 | */ |
| 152 | void kconsumerd_should_exit(struct kconsumerd_local_data *ctx); |
| 153 | |
| 154 | /* |
| 155 | * kconsumerd_cleanup |
| 156 | * Cleanup the daemon's socket on exit |
| 157 | */ |
| 158 | void kconsumerd_cleanup(void); |
| 159 | |
| 160 | /* |
| 161 | * kconsumerd_set_error_socket |
| 162 | * Set the error socket for communication with a session daemon |
| 163 | */ |
| 164 | void kconsumerd_set_error_socket(struct kconsumerd_local_data *ctx, int sock); |
| 165 | |
| 166 | /* |
| 167 | * kconsumerd_set_command_socket_path |
| 168 | * Set the command socket path for communication with a session daemon |
| 169 | */ |
| 170 | void kconsumerd_set_command_socket_path(struct kconsumerd_local_data *ctx, char *sock); |
| 171 | |
| 172 | #endif /* _LIBLTTKCONSUMERD_H */ |