| 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 _LGPL_SOURCE |
| 21 | #include <common/common.h> |
| 22 | #include <common/index/index.h> |
| 23 | #include <common/compat/string.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 | struct lttng_trace_chunk *viewer_trace_chunk, |
| 45 | enum lttng_viewer_seek seek_t) |
| 46 | { |
| 47 | struct relay_viewer_stream *vstream = NULL; |
| 48 | const bool acquired_reference = lttng_trace_chunk_get( |
| 49 | viewer_trace_chunk); |
| 50 | |
| 51 | if (!acquired_reference) { |
| 52 | goto error; |
| 53 | } |
| 54 | |
| 55 | vstream = zmalloc(sizeof(*vstream)); |
| 56 | if (!vstream) { |
| 57 | PERROR("relay viewer stream zmalloc"); |
| 58 | goto error; |
| 59 | } |
| 60 | |
| 61 | vstream->stream_file.trace_chunk = viewer_trace_chunk; |
| 62 | viewer_trace_chunk = NULL; |
| 63 | vstream->path_name = lttng_strndup(stream->path_name, LTTNG_VIEWER_PATH_MAX); |
| 64 | if (vstream->path_name == NULL) { |
| 65 | PERROR("relay viewer path_name alloc"); |
| 66 | goto error; |
| 67 | } |
| 68 | vstream->channel_name = lttng_strndup(stream->channel_name, |
| 69 | LTTNG_VIEWER_NAME_MAX); |
| 70 | if (vstream->channel_name == NULL) { |
| 71 | PERROR("relay viewer channel_name alloc"); |
| 72 | goto error; |
| 73 | } |
| 74 | |
| 75 | if (!stream_get(stream)) { |
| 76 | ERR("Cannot get stream"); |
| 77 | goto error; |
| 78 | } |
| 79 | vstream->stream = stream; |
| 80 | |
| 81 | pthread_mutex_lock(&stream->lock); |
| 82 | |
| 83 | if (stream->is_metadata && stream->trace->viewer_metadata_stream) { |
| 84 | ERR("Cannot attach viewer metadata stream to trace (busy)."); |
| 85 | goto error_unlock; |
| 86 | } |
| 87 | |
| 88 | switch (seek_t) { |
| 89 | case LTTNG_VIEWER_SEEK_BEGINNING: |
| 90 | { |
| 91 | uint64_t seq_tail = tracefile_array_get_seq_tail(stream->tfa); |
| 92 | |
| 93 | if (seq_tail == -1ULL) { |
| 94 | /* |
| 95 | * Tail may not be initialized yet. Nonetheless, we know |
| 96 | * we want to send the first index once it becomes |
| 97 | * available. |
| 98 | */ |
| 99 | seq_tail = 0; |
| 100 | } |
| 101 | vstream->current_tracefile_id = |
| 102 | tracefile_array_get_file_index_tail(stream->tfa); |
| 103 | vstream->index_sent_seqcount = seq_tail; |
| 104 | break; |
| 105 | } |
| 106 | case LTTNG_VIEWER_SEEK_LAST: |
| 107 | vstream->current_tracefile_id = |
| 108 | tracefile_array_get_read_file_index_head(stream->tfa); |
| 109 | /* |
| 110 | * We seek at the very end of each stream, awaiting for |
| 111 | * a future packet to eventually come in. |
| 112 | * |
| 113 | * We don't need to check the head position for -1ULL since the |
| 114 | * increment will set it to 0. |
| 115 | */ |
| 116 | vstream->index_sent_seqcount = |
| 117 | tracefile_array_get_seq_head(stream->tfa) + 1; |
| 118 | break; |
| 119 | default: |
| 120 | goto error_unlock; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * If we never received an index for the current stream, delay |
| 125 | * the opening of the index, otherwise open it right now. |
| 126 | */ |
| 127 | if (stream->index_received_seqcount == 0) { |
| 128 | vstream->index_file = NULL; |
| 129 | } else { |
| 130 | const uint32_t connection_major = stream->trace->session->major; |
| 131 | const uint32_t connection_minor = stream->trace->session->minor; |
| 132 | |
| 133 | vstream->index_file = lttng_index_file_create_from_trace_chunk_read_only( |
| 134 | vstream->stream_file.trace_chunk, |
| 135 | stream->path_name, |
| 136 | stream->channel_name, stream->tracefile_size, |
| 137 | vstream->current_tracefile_id, |
| 138 | lttng_to_index_major(connection_major, |
| 139 | connection_minor), |
| 140 | lttng_to_index_minor(connection_major, |
| 141 | connection_minor)); |
| 142 | if (!vstream->index_file) { |
| 143 | goto error_unlock; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (seek_t == LTTNG_VIEWER_SEEK_LAST && vstream->index_file) { |
| 148 | off_t lseek_ret; |
| 149 | |
| 150 | lseek_ret = lseek(vstream->index_file->fd, 0, SEEK_END); |
| 151 | if (lseek_ret < 0) { |
| 152 | goto error_unlock; |
| 153 | } |
| 154 | } |
| 155 | if (stream->is_metadata) { |
| 156 | rcu_assign_pointer(stream->trace->viewer_metadata_stream, |
| 157 | vstream); |
| 158 | } |
| 159 | pthread_mutex_unlock(&stream->lock); |
| 160 | |
| 161 | /* Globally visible after the add unique. */ |
| 162 | lttng_ht_node_init_u64(&vstream->stream_n, stream->stream_handle); |
| 163 | urcu_ref_init(&vstream->ref); |
| 164 | lttng_ht_add_unique_u64(viewer_streams_ht, &vstream->stream_n); |
| 165 | |
| 166 | return vstream; |
| 167 | |
| 168 | error_unlock: |
| 169 | pthread_mutex_unlock(&stream->lock); |
| 170 | error: |
| 171 | if (vstream) { |
| 172 | viewer_stream_destroy(vstream); |
| 173 | } |
| 174 | if (viewer_trace_chunk && acquired_reference) { |
| 175 | lttng_trace_chunk_put(viewer_trace_chunk); |
| 176 | } |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | static void viewer_stream_unpublish(struct relay_viewer_stream *vstream) |
| 181 | { |
| 182 | int ret; |
| 183 | struct lttng_ht_iter iter; |
| 184 | |
| 185 | iter.iter.node = &vstream->stream_n.node; |
| 186 | ret = lttng_ht_del(viewer_streams_ht, &iter); |
| 187 | assert(!ret); |
| 188 | } |
| 189 | |
| 190 | static void viewer_stream_release(struct urcu_ref *ref) |
| 191 | { |
| 192 | struct relay_viewer_stream *vstream = caa_container_of(ref, |
| 193 | struct relay_viewer_stream, ref); |
| 194 | |
| 195 | if (vstream->stream->is_metadata) { |
| 196 | rcu_assign_pointer(vstream->stream->trace->viewer_metadata_stream, NULL); |
| 197 | } |
| 198 | |
| 199 | viewer_stream_unpublish(vstream); |
| 200 | |
| 201 | if (vstream->stream_file.fd) { |
| 202 | stream_fd_put(vstream->stream_file.fd); |
| 203 | vstream->stream_file.fd = NULL; |
| 204 | } |
| 205 | if (vstream->index_file) { |
| 206 | lttng_index_file_put(vstream->index_file); |
| 207 | vstream->index_file = NULL; |
| 208 | } |
| 209 | if (vstream->stream) { |
| 210 | stream_put(vstream->stream); |
| 211 | vstream->stream = NULL; |
| 212 | } |
| 213 | lttng_trace_chunk_put(vstream->stream_file.trace_chunk); |
| 214 | vstream->stream_file.trace_chunk = NULL; |
| 215 | call_rcu(&vstream->rcu_node, viewer_stream_destroy_rcu); |
| 216 | } |
| 217 | |
| 218 | /* Must be called with RCU read-side lock held. */ |
| 219 | bool viewer_stream_get(struct relay_viewer_stream *vstream) |
| 220 | { |
| 221 | return urcu_ref_get_unless_zero(&vstream->ref); |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Get viewer stream by id. |
| 226 | * |
| 227 | * Return viewer stream if found else NULL. |
| 228 | */ |
| 229 | struct relay_viewer_stream *viewer_stream_get_by_id(uint64_t id) |
| 230 | { |
| 231 | struct lttng_ht_node_u64 *node; |
| 232 | struct lttng_ht_iter iter; |
| 233 | struct relay_viewer_stream *vstream = NULL; |
| 234 | |
| 235 | rcu_read_lock(); |
| 236 | lttng_ht_lookup(viewer_streams_ht, &id, &iter); |
| 237 | node = lttng_ht_iter_get_node_u64(&iter); |
| 238 | if (!node) { |
| 239 | DBG("Relay viewer stream %" PRIu64 " not found", id); |
| 240 | goto end; |
| 241 | } |
| 242 | vstream = caa_container_of(node, struct relay_viewer_stream, stream_n); |
| 243 | if (!viewer_stream_get(vstream)) { |
| 244 | vstream = NULL; |
| 245 | } |
| 246 | end: |
| 247 | rcu_read_unlock(); |
| 248 | return vstream; |
| 249 | } |
| 250 | |
| 251 | void viewer_stream_put(struct relay_viewer_stream *vstream) |
| 252 | { |
| 253 | rcu_read_lock(); |
| 254 | urcu_ref_put(&vstream->ref, viewer_stream_release); |
| 255 | rcu_read_unlock(); |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Rotate a stream to the next tracefile. |
| 260 | * |
| 261 | * Must be called with the rstream lock held. |
| 262 | * Returns 0 on success, 1 on EOF, a negative value on error. |
| 263 | */ |
| 264 | int viewer_stream_rotate(struct relay_viewer_stream *vstream) |
| 265 | { |
| 266 | int ret; |
| 267 | uint64_t new_id; |
| 268 | const struct relay_stream *stream = vstream->stream; |
| 269 | const uint32_t connection_major = stream->trace->session->major; |
| 270 | const uint32_t connection_minor = stream->trace->session->minor; |
| 271 | |
| 272 | /* Detect the last tracefile to open. */ |
| 273 | if (stream->index_received_seqcount |
| 274 | == vstream->index_sent_seqcount |
| 275 | && stream->trace->session->connection_closed) { |
| 276 | ret = 1; |
| 277 | goto end; |
| 278 | } |
| 279 | |
| 280 | if (stream->tracefile_count == 0) { |
| 281 | /* Ignore rotation, there is none to do. */ |
| 282 | ret = 0; |
| 283 | goto end; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Try to move to the next file. |
| 288 | */ |
| 289 | new_id = (vstream->current_tracefile_id + 1) |
| 290 | % stream->tracefile_count; |
| 291 | if (tracefile_array_seq_in_file(stream->tfa, new_id, |
| 292 | vstream->index_sent_seqcount)) { |
| 293 | vstream->current_tracefile_id = new_id; |
| 294 | } else { |
| 295 | uint64_t seq_tail = tracefile_array_get_seq_tail(stream->tfa); |
| 296 | |
| 297 | /* |
| 298 | * This can only be reached on overwrite, which implies there |
| 299 | * has been data written at some point, which will have set the |
| 300 | * tail. |
| 301 | */ |
| 302 | assert(seq_tail != -1ULL); |
| 303 | /* |
| 304 | * We need to resync because we lag behind tail. |
| 305 | */ |
| 306 | vstream->current_tracefile_id = |
| 307 | tracefile_array_get_file_index_tail(stream->tfa); |
| 308 | vstream->index_sent_seqcount = seq_tail; |
| 309 | } |
| 310 | |
| 311 | if (vstream->index_file) { |
| 312 | lttng_index_file_put(vstream->index_file); |
| 313 | vstream->index_file = NULL; |
| 314 | } |
| 315 | if (vstream->stream_file.fd) { |
| 316 | stream_fd_put(vstream->stream_file.fd); |
| 317 | vstream->stream_file.fd = NULL; |
| 318 | } |
| 319 | vstream->index_file = |
| 320 | lttng_index_file_create_from_trace_chunk_read_only( |
| 321 | vstream->stream_file.trace_chunk, |
| 322 | stream->path_name, |
| 323 | stream->channel_name, |
| 324 | stream->tracefile_size, |
| 325 | vstream->current_tracefile_id, |
| 326 | lttng_to_index_major(connection_major, |
| 327 | connection_minor), |
| 328 | lttng_to_index_minor(connection_major, |
| 329 | connection_minor)); |
| 330 | if (!vstream->index_file) { |
| 331 | ret = -1; |
| 332 | goto end; |
| 333 | } else { |
| 334 | ret = 0; |
| 335 | } |
| 336 | end: |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | void print_viewer_streams(void) |
| 341 | { |
| 342 | struct lttng_ht_iter iter; |
| 343 | struct relay_viewer_stream *vstream; |
| 344 | |
| 345 | if (!viewer_streams_ht) { |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | rcu_read_lock(); |
| 350 | cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream, |
| 351 | stream_n.node) { |
| 352 | if (!viewer_stream_get(vstream)) { |
| 353 | continue; |
| 354 | } |
| 355 | DBG("vstream %p refcount %ld stream %" PRIu64 " trace %" PRIu64 |
| 356 | " session %" PRIu64, |
| 357 | vstream, |
| 358 | vstream->ref.refcount, |
| 359 | vstream->stream->stream_handle, |
| 360 | vstream->stream->trace->id, |
| 361 | vstream->stream->trace->session->id); |
| 362 | viewer_stream_put(vstream); |
| 363 | } |
| 364 | rcu_read_unlock(); |
| 365 | } |