| 1 | /* |
| 2 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * David Goulet <dgoulet@efficios.com> |
| 4 | * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License, version 2 only, as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 17 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #define _GNU_SOURCE |
| 21 | #define _LGPL_SOURCE |
| 22 | #include <common/common.h> |
| 23 | #include <common/index/index.h> |
| 24 | |
| 25 | #include "lttng-relayd.h" |
| 26 | #include "viewer-stream.h" |
| 27 | |
| 28 | static void viewer_stream_destroy(struct relay_viewer_stream *vstream) |
| 29 | { |
| 30 | free(vstream->path_name); |
| 31 | free(vstream->channel_name); |
| 32 | free(vstream); |
| 33 | } |
| 34 | |
| 35 | static void viewer_stream_destroy_rcu(struct rcu_head *head) |
| 36 | { |
| 37 | struct relay_viewer_stream *vstream = |
| 38 | caa_container_of(head, struct relay_viewer_stream, rcu_node); |
| 39 | |
| 40 | viewer_stream_destroy(vstream); |
| 41 | } |
| 42 | |
| 43 | struct relay_viewer_stream *viewer_stream_create(struct relay_stream *stream, |
| 44 | enum lttng_viewer_seek seek_t) |
| 45 | { |
| 46 | struct relay_viewer_stream *vstream; |
| 47 | |
| 48 | vstream = zmalloc(sizeof(*vstream)); |
| 49 | if (!vstream) { |
| 50 | PERROR("relay viewer stream zmalloc"); |
| 51 | goto error; |
| 52 | } |
| 53 | |
| 54 | vstream->path_name = strndup(stream->path_name, LTTNG_VIEWER_PATH_MAX); |
| 55 | if (vstream->path_name == NULL) { |
| 56 | PERROR("relay viewer path_name alloc"); |
| 57 | goto error; |
| 58 | } |
| 59 | vstream->channel_name = strndup(stream->channel_name, |
| 60 | LTTNG_VIEWER_NAME_MAX); |
| 61 | if (vstream->channel_name == NULL) { |
| 62 | PERROR("relay viewer channel_name alloc"); |
| 63 | goto error; |
| 64 | } |
| 65 | |
| 66 | switch (seek_t) { |
| 67 | case LTTNG_VIEWER_SEEK_BEGINNING: |
| 68 | vstream->current_tracefile_id = stream->oldest_tracefile_id; |
| 69 | break; |
| 70 | case LTTNG_VIEWER_SEEK_LAST: |
| 71 | vstream->current_tracefile_id = stream->current_tracefile_id; |
| 72 | break; |
| 73 | default: |
| 74 | goto error; |
| 75 | } |
| 76 | if (!stream_get(stream)) { |
| 77 | ERR("Cannot get stream"); |
| 78 | goto error; |
| 79 | } |
| 80 | vstream->stream = stream; |
| 81 | |
| 82 | pthread_mutex_lock(&stream->lock); |
| 83 | /* |
| 84 | * If we never received an index for the current stream, delay the opening |
| 85 | * of the index, otherwise open it right now. |
| 86 | */ |
| 87 | if (vstream->current_tracefile_id == stream->current_tracefile_id |
| 88 | && stream->total_index_received == 0) { |
| 89 | vstream->index_fd = NULL; |
| 90 | } else { |
| 91 | int read_fd; |
| 92 | |
| 93 | read_fd = index_open(vstream->path_name, vstream->channel_name, |
| 94 | stream->tracefile_count, |
| 95 | vstream->current_tracefile_id); |
| 96 | if (read_fd < 0) { |
| 97 | goto error_unlock; |
| 98 | } |
| 99 | vstream->index_fd = stream_fd_create(read_fd); |
| 100 | if (!vstream->index_fd) { |
| 101 | if (close(read_fd)) { |
| 102 | PERROR("close"); |
| 103 | } |
| 104 | goto error_unlock; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if (seek_t == LTTNG_VIEWER_SEEK_LAST && vstream->index_fd) { |
| 109 | off_t lseek_ret; |
| 110 | |
| 111 | lseek_ret = lseek(vstream->index_fd->fd, 0, SEEK_END); |
| 112 | if (lseek_ret < 0) { |
| 113 | goto error_unlock; |
| 114 | } |
| 115 | vstream->last_sent_index = stream->total_index_received; |
| 116 | } |
| 117 | pthread_mutex_unlock(&stream->lock); |
| 118 | |
| 119 | if (stream->is_metadata) { |
| 120 | rcu_assign_pointer(stream->trace->viewer_metadata_stream, |
| 121 | vstream); |
| 122 | } |
| 123 | |
| 124 | /* Globally visible after the add unique. */ |
| 125 | lttng_ht_node_init_u64(&vstream->stream_n, stream->stream_handle); |
| 126 | lttng_ht_add_unique_u64(viewer_streams_ht, &vstream->stream_n); |
| 127 | |
| 128 | pthread_mutex_init(&vstream->reflock, NULL); |
| 129 | urcu_ref_init(&vstream->ref); |
| 130 | |
| 131 | return vstream; |
| 132 | |
| 133 | error_unlock: |
| 134 | pthread_mutex_unlock(&stream->lock); |
| 135 | error: |
| 136 | if (vstream) { |
| 137 | viewer_stream_destroy(vstream); |
| 138 | } |
| 139 | return NULL; |
| 140 | } |
| 141 | |
| 142 | static void viewer_stream_unpublish(struct relay_viewer_stream *vstream) |
| 143 | { |
| 144 | int ret; |
| 145 | struct lttng_ht_iter iter; |
| 146 | |
| 147 | iter.iter.node = &vstream->stream_n.node; |
| 148 | ret = lttng_ht_del(viewer_streams_ht, &iter); |
| 149 | assert(!ret); |
| 150 | } |
| 151 | |
| 152 | static void viewer_stream_release(struct urcu_ref *ref) |
| 153 | { |
| 154 | struct relay_viewer_stream *vstream = caa_container_of(ref, |
| 155 | struct relay_viewer_stream, ref); |
| 156 | |
| 157 | if (vstream->stream->is_metadata) { |
| 158 | rcu_assign_pointer(vstream->stream->trace->viewer_metadata_stream, NULL); |
| 159 | } |
| 160 | |
| 161 | viewer_stream_unpublish(vstream); |
| 162 | |
| 163 | if (vstream->stream_fd) { |
| 164 | stream_fd_put(vstream->stream_fd); |
| 165 | vstream->stream_fd = NULL; |
| 166 | } |
| 167 | if (vstream->index_fd) { |
| 168 | stream_fd_put(vstream->index_fd); |
| 169 | vstream->index_fd = NULL; |
| 170 | } |
| 171 | if (vstream->stream) { |
| 172 | stream_put(vstream->stream); |
| 173 | vstream->stream = NULL; |
| 174 | } |
| 175 | call_rcu(&vstream->rcu_node, viewer_stream_destroy_rcu); |
| 176 | } |
| 177 | |
| 178 | /* Must be called with RCU read-side lock held. */ |
| 179 | bool viewer_stream_get(struct relay_viewer_stream *vstream) |
| 180 | { |
| 181 | bool has_ref = false; |
| 182 | |
| 183 | pthread_mutex_lock(&vstream->reflock); |
| 184 | if (vstream->ref.refcount != 0) { |
| 185 | has_ref = true; |
| 186 | urcu_ref_get(&vstream->ref); |
| 187 | } |
| 188 | pthread_mutex_unlock(&vstream->reflock); |
| 189 | |
| 190 | return has_ref; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Get viewer stream by id. |
| 195 | * |
| 196 | * Return viewer stream if found else NULL. |
| 197 | */ |
| 198 | struct relay_viewer_stream *viewer_stream_get_by_id(uint64_t id) |
| 199 | { |
| 200 | struct lttng_ht_node_u64 *node; |
| 201 | struct lttng_ht_iter iter; |
| 202 | struct relay_viewer_stream *vstream = NULL; |
| 203 | |
| 204 | rcu_read_lock(); |
| 205 | lttng_ht_lookup(viewer_streams_ht, &id, &iter); |
| 206 | node = lttng_ht_iter_get_node_u64(&iter); |
| 207 | if (!node) { |
| 208 | DBG("Relay viewer stream %" PRIu64 " not found", id); |
| 209 | goto end; |
| 210 | } |
| 211 | vstream = caa_container_of(node, struct relay_viewer_stream, stream_n); |
| 212 | if (!viewer_stream_get(vstream)) { |
| 213 | vstream = NULL; |
| 214 | } |
| 215 | end: |
| 216 | rcu_read_unlock(); |
| 217 | return vstream; |
| 218 | } |
| 219 | |
| 220 | void viewer_stream_put(struct relay_viewer_stream *vstream) |
| 221 | { |
| 222 | rcu_read_lock(); |
| 223 | pthread_mutex_lock(&vstream->reflock); |
| 224 | urcu_ref_put(&vstream->ref, viewer_stream_release); |
| 225 | pthread_mutex_unlock(&vstream->reflock); |
| 226 | rcu_read_unlock(); |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Returns whether the current tracefile is readable. If not, it has |
| 231 | * been overwritten. |
| 232 | * Must be called with rstream lock held. |
| 233 | */ |
| 234 | bool viewer_stream_is_tracefile_seq_readable(struct relay_viewer_stream *vstream, |
| 235 | uint64_t seq) |
| 236 | { |
| 237 | struct relay_stream *stream = vstream->stream; |
| 238 | |
| 239 | if (seq >= stream->oldest_tracefile_seq |
| 240 | && seq <= stream->current_tracefile_seq) { |
| 241 | /* seq is a readable file. */ |
| 242 | return true; |
| 243 | } else { |
| 244 | /* seq is not readable. */ |
| 245 | return false; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Rotate a stream to the next tracefile. |
| 251 | * |
| 252 | * Must be called with the rstream lock held. |
| 253 | * Returns 0 on success, 1 on EOF, a negative value on error. |
| 254 | */ |
| 255 | int viewer_stream_rotate(struct relay_viewer_stream *vstream) |
| 256 | { |
| 257 | int ret; |
| 258 | struct relay_stream *stream = vstream->stream; |
| 259 | |
| 260 | /* Detect the last tracefile to open. */ |
| 261 | if (stream->total_index_received == vstream->last_sent_index |
| 262 | && stream->trace->session->connection_closed) { |
| 263 | ret = 1; |
| 264 | goto end; |
| 265 | } |
| 266 | |
| 267 | if (stream->tracefile_count == 0) { |
| 268 | /* Ignore rotation, there is none to do. */ |
| 269 | ret = 0; |
| 270 | goto end; |
| 271 | } |
| 272 | |
| 273 | if (!viewer_stream_is_tracefile_seq_readable(vstream, |
| 274 | vstream->current_tracefile_seq + 1)) { |
| 275 | vstream->current_tracefile_id = |
| 276 | stream->oldest_tracefile_id; |
| 277 | vstream->current_tracefile_seq = |
| 278 | stream->oldest_tracefile_seq; |
| 279 | } else { |
| 280 | vstream->current_tracefile_id = |
| 281 | (vstream->current_tracefile_id + 1) |
| 282 | % stream->tracefile_count; |
| 283 | vstream->current_tracefile_seq++; |
| 284 | } |
| 285 | |
| 286 | if (vstream->index_fd) { |
| 287 | stream_fd_put(vstream->index_fd); |
| 288 | vstream->index_fd = NULL; |
| 289 | } |
| 290 | if (vstream->stream_fd) { |
| 291 | stream_fd_put(vstream->stream_fd); |
| 292 | vstream->stream_fd = NULL; |
| 293 | } |
| 294 | |
| 295 | ret = index_open(vstream->path_name, vstream->channel_name, |
| 296 | stream->tracefile_count, |
| 297 | vstream->current_tracefile_id); |
| 298 | if (ret < 0) { |
| 299 | goto end; |
| 300 | } |
| 301 | vstream->index_fd = stream_fd_create(ret); |
| 302 | if (vstream->index_fd) { |
| 303 | ret = 0; |
| 304 | } else { |
| 305 | if (close(ret)) { |
| 306 | PERROR("close"); |
| 307 | } |
| 308 | ret = -1; |
| 309 | } |
| 310 | end: |
| 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | void print_viewer_streams(void) |
| 315 | { |
| 316 | struct lttng_ht_iter iter; |
| 317 | struct relay_viewer_stream *vstream; |
| 318 | |
| 319 | rcu_read_lock(); |
| 320 | cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream, |
| 321 | stream_n.node) { |
| 322 | if (!viewer_stream_get(vstream)) { |
| 323 | continue; |
| 324 | } |
| 325 | DBG("vstream %p refcount %ld stream %" PRIu64 " trace %" PRIu64 |
| 326 | " session %" PRIu64, |
| 327 | vstream, |
| 328 | vstream->ref.refcount, |
| 329 | vstream->stream->stream_handle, |
| 330 | vstream->stream->trace->id, |
| 331 | vstream->stream->trace->session->id); |
| 332 | viewer_stream_put(vstream); |
| 333 | } |
| 334 | rcu_read_unlock(); |
| 335 | } |