| 1 | /* |
| 2 | * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
| 4 | * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-only |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #define _LGPL_SOURCE |
| 11 | #include <common/common.h> |
| 12 | #include <common/index/index.h> |
| 13 | #include <common/compat/string.h> |
| 14 | #include <common/utils.h> |
| 15 | #include <sys/types.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <algorithm> |
| 19 | |
| 20 | #include "lttng-relayd.h" |
| 21 | #include "viewer-stream.h" |
| 22 | |
| 23 | static void viewer_stream_destroy(struct relay_viewer_stream *vstream) |
| 24 | { |
| 25 | lttng_trace_chunk_put(vstream->stream_file.trace_chunk); |
| 26 | free(vstream->path_name); |
| 27 | free(vstream->channel_name); |
| 28 | free(vstream); |
| 29 | } |
| 30 | |
| 31 | static void viewer_stream_destroy_rcu(struct rcu_head *head) |
| 32 | { |
| 33 | struct relay_viewer_stream *vstream = |
| 34 | caa_container_of(head, struct relay_viewer_stream, rcu_node); |
| 35 | |
| 36 | viewer_stream_destroy(vstream); |
| 37 | } |
| 38 | |
| 39 | /* Relay stream's lock must be held by the caller. */ |
| 40 | struct relay_viewer_stream *viewer_stream_create(struct relay_stream *stream, |
| 41 | struct lttng_trace_chunk *trace_chunk, |
| 42 | enum lttng_viewer_seek seek_t) |
| 43 | { |
| 44 | struct relay_viewer_stream *vstream = NULL; |
| 45 | |
| 46 | ASSERT_LOCKED(stream->lock); |
| 47 | |
| 48 | vstream = (relay_viewer_stream *) zmalloc(sizeof(*vstream)); |
| 49 | if (!vstream) { |
| 50 | PERROR("relay viewer stream zmalloc"); |
| 51 | goto error; |
| 52 | } |
| 53 | |
| 54 | if (trace_chunk) { |
| 55 | const bool acquired_reference = lttng_trace_chunk_get( |
| 56 | trace_chunk); |
| 57 | |
| 58 | LTTNG_ASSERT(acquired_reference); |
| 59 | } |
| 60 | |
| 61 | vstream->stream_file.trace_chunk = trace_chunk; |
| 62 | vstream->path_name = lttng_strndup(stream->path_name, LTTNG_VIEWER_PATH_MAX); |
| 63 | if (vstream->path_name == NULL) { |
| 64 | PERROR("relay viewer path_name alloc"); |
| 65 | goto error; |
| 66 | } |
| 67 | vstream->channel_name = lttng_strndup(stream->channel_name, |
| 68 | LTTNG_VIEWER_NAME_MAX); |
| 69 | if (vstream->channel_name == NULL) { |
| 70 | PERROR("relay viewer channel_name alloc"); |
| 71 | goto error; |
| 72 | } |
| 73 | |
| 74 | if (!stream_get(stream)) { |
| 75 | ERR("Cannot get stream"); |
| 76 | goto error; |
| 77 | } |
| 78 | vstream->stream = stream; |
| 79 | |
| 80 | if (stream->is_metadata && stream->trace->viewer_metadata_stream) { |
| 81 | ERR("Cannot attach viewer metadata stream to trace (busy)."); |
| 82 | goto error; |
| 83 | } |
| 84 | |
| 85 | switch (seek_t) { |
| 86 | case LTTNG_VIEWER_SEEK_BEGINNING: |
| 87 | { |
| 88 | uint64_t seq_tail = tracefile_array_get_seq_tail(stream->tfa); |
| 89 | |
| 90 | if (seq_tail == -1ULL) { |
| 91 | /* |
| 92 | * Tail may not be initialized yet. Nonetheless, we know |
| 93 | * we want to send the first index once it becomes |
| 94 | * available. |
| 95 | */ |
| 96 | seq_tail = 0; |
| 97 | } |
| 98 | vstream->current_tracefile_id = |
| 99 | tracefile_array_get_file_index_tail(stream->tfa); |
| 100 | vstream->index_sent_seqcount = seq_tail; |
| 101 | break; |
| 102 | } |
| 103 | case LTTNG_VIEWER_SEEK_LAST: |
| 104 | vstream->current_tracefile_id = |
| 105 | tracefile_array_get_read_file_index_head(stream->tfa); |
| 106 | /* |
| 107 | * We seek at the very end of each stream, awaiting for |
| 108 | * a future packet to eventually come in. |
| 109 | * |
| 110 | * We don't need to check the head position for -1ULL since the |
| 111 | * increment will set it to 0. |
| 112 | */ |
| 113 | vstream->index_sent_seqcount = |
| 114 | tracefile_array_get_seq_head(stream->tfa) + 1; |
| 115 | break; |
| 116 | default: |
| 117 | goto error; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * If we never received an index for the current stream, delay |
| 122 | * the opening of the index, otherwise open it right now. |
| 123 | */ |
| 124 | if (stream->index_file == NULL) { |
| 125 | vstream->index_file = NULL; |
| 126 | } else if (vstream->stream_file.trace_chunk) { |
| 127 | const uint32_t connection_major = stream->trace->session->major; |
| 128 | const uint32_t connection_minor = stream->trace->session->minor; |
| 129 | enum lttng_trace_chunk_status chunk_status; |
| 130 | |
| 131 | chunk_status = lttng_index_file_create_from_trace_chunk_read_only( |
| 132 | vstream->stream_file.trace_chunk, |
| 133 | stream->path_name, |
| 134 | stream->channel_name, stream->tracefile_size, |
| 135 | vstream->current_tracefile_id, |
| 136 | lttng_to_index_major(connection_major, |
| 137 | connection_minor), |
| 138 | lttng_to_index_minor(connection_major, |
| 139 | connection_minor), |
| 140 | true, &vstream->index_file); |
| 141 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 142 | if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE) { |
| 143 | vstream->index_file = NULL; |
| 144 | } else { |
| 145 | goto error; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * If we never received a data file for the current stream, delay the |
| 152 | * opening, otherwise open it right now. |
| 153 | */ |
| 154 | if (stream->file && vstream->stream_file.trace_chunk) { |
| 155 | int ret; |
| 156 | char file_path[LTTNG_PATH_MAX]; |
| 157 | enum lttng_trace_chunk_status status; |
| 158 | |
| 159 | ret = utils_stream_file_path(stream->path_name, |
| 160 | stream->channel_name, stream->tracefile_size, |
| 161 | vstream->current_tracefile_id, NULL, file_path, |
| 162 | sizeof(file_path)); |
| 163 | if (ret < 0) { |
| 164 | goto error; |
| 165 | } |
| 166 | |
| 167 | status = lttng_trace_chunk_open_fs_handle( |
| 168 | vstream->stream_file.trace_chunk, file_path, |
| 169 | O_RDONLY, 0, &vstream->stream_file.handle, |
| 170 | true); |
| 171 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 172 | goto error; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if (seek_t == LTTNG_VIEWER_SEEK_LAST && vstream->index_file) { |
| 177 | off_t lseek_ret; |
| 178 | |
| 179 | lseek_ret = fs_handle_seek( |
| 180 | vstream->index_file->file, 0, SEEK_END); |
| 181 | if (lseek_ret < 0) { |
| 182 | goto error; |
| 183 | } |
| 184 | } |
| 185 | if (stream->is_metadata) { |
| 186 | rcu_assign_pointer(stream->trace->viewer_metadata_stream, |
| 187 | vstream); |
| 188 | } |
| 189 | |
| 190 | vstream->last_seen_rotation_count = stream->completed_rotation_count; |
| 191 | |
| 192 | /* Globally visible after the add unique. */ |
| 193 | lttng_ht_node_init_u64(&vstream->stream_n, stream->stream_handle); |
| 194 | urcu_ref_init(&vstream->ref); |
| 195 | lttng_ht_add_unique_u64(viewer_streams_ht, &vstream->stream_n); |
| 196 | |
| 197 | return vstream; |
| 198 | |
| 199 | error: |
| 200 | if (vstream) { |
| 201 | viewer_stream_destroy(vstream); |
| 202 | } |
| 203 | return NULL; |
| 204 | } |
| 205 | |
| 206 | static void viewer_stream_unpublish(struct relay_viewer_stream *vstream) |
| 207 | { |
| 208 | int ret; |
| 209 | struct lttng_ht_iter iter; |
| 210 | |
| 211 | iter.iter.node = &vstream->stream_n.node; |
| 212 | ret = lttng_ht_del(viewer_streams_ht, &iter); |
| 213 | LTTNG_ASSERT(!ret); |
| 214 | } |
| 215 | |
| 216 | static void viewer_stream_release(struct urcu_ref *ref) |
| 217 | { |
| 218 | struct relay_viewer_stream *vstream = caa_container_of(ref, |
| 219 | struct relay_viewer_stream, ref); |
| 220 | |
| 221 | if (vstream->stream->is_metadata) { |
| 222 | rcu_assign_pointer(vstream->stream->trace->viewer_metadata_stream, NULL); |
| 223 | } |
| 224 | |
| 225 | viewer_stream_unpublish(vstream); |
| 226 | |
| 227 | if (vstream->stream_file.handle) { |
| 228 | fs_handle_close(vstream->stream_file.handle); |
| 229 | vstream->stream_file.handle = NULL; |
| 230 | } |
| 231 | if (vstream->index_file) { |
| 232 | lttng_index_file_put(vstream->index_file); |
| 233 | vstream->index_file = NULL; |
| 234 | } |
| 235 | if (vstream->stream) { |
| 236 | stream_put(vstream->stream); |
| 237 | vstream->stream = NULL; |
| 238 | } |
| 239 | lttng_trace_chunk_put(vstream->stream_file.trace_chunk); |
| 240 | vstream->stream_file.trace_chunk = NULL; |
| 241 | call_rcu(&vstream->rcu_node, viewer_stream_destroy_rcu); |
| 242 | } |
| 243 | |
| 244 | /* Must be called with RCU read-side lock held. */ |
| 245 | bool viewer_stream_get(struct relay_viewer_stream *vstream) |
| 246 | { |
| 247 | return urcu_ref_get_unless_zero(&vstream->ref); |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Get viewer stream by id. |
| 252 | * |
| 253 | * Return viewer stream if found else NULL. |
| 254 | */ |
| 255 | struct relay_viewer_stream *viewer_stream_get_by_id(uint64_t id) |
| 256 | { |
| 257 | struct lttng_ht_node_u64 *node; |
| 258 | struct lttng_ht_iter iter; |
| 259 | struct relay_viewer_stream *vstream = NULL; |
| 260 | |
| 261 | rcu_read_lock(); |
| 262 | lttng_ht_lookup(viewer_streams_ht, &id, &iter); |
| 263 | node = lttng_ht_iter_get_node_u64(&iter); |
| 264 | if (!node) { |
| 265 | DBG("Relay viewer stream %" PRIu64 " not found", id); |
| 266 | goto end; |
| 267 | } |
| 268 | vstream = caa_container_of(node, struct relay_viewer_stream, stream_n); |
| 269 | if (!viewer_stream_get(vstream)) { |
| 270 | vstream = NULL; |
| 271 | } |
| 272 | end: |
| 273 | rcu_read_unlock(); |
| 274 | return vstream; |
| 275 | } |
| 276 | |
| 277 | void viewer_stream_put(struct relay_viewer_stream *vstream) |
| 278 | { |
| 279 | rcu_read_lock(); |
| 280 | urcu_ref_put(&vstream->ref, viewer_stream_release); |
| 281 | rcu_read_unlock(); |
| 282 | } |
| 283 | |
| 284 | void viewer_stream_close_files(struct relay_viewer_stream *vstream) |
| 285 | { |
| 286 | if (vstream->index_file) { |
| 287 | lttng_index_file_put(vstream->index_file); |
| 288 | vstream->index_file = NULL; |
| 289 | } |
| 290 | if (vstream->stream_file.handle) { |
| 291 | fs_handle_close(vstream->stream_file.handle); |
| 292 | vstream->stream_file.handle = NULL; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | void viewer_stream_sync_tracefile_array_tail(struct relay_viewer_stream *vstream) |
| 297 | { |
| 298 | const struct relay_stream *stream = vstream->stream; |
| 299 | uint64_t seq_tail; |
| 300 | |
| 301 | vstream->current_tracefile_id = tracefile_array_get_file_index_tail(stream->tfa); |
| 302 | seq_tail = tracefile_array_get_seq_tail(stream->tfa); |
| 303 | if (seq_tail == -1ULL) { |
| 304 | seq_tail = 0; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Move the index sent seqcount forward if it was lagging behind |
| 309 | * the new tail of the tracefile array. If the current |
| 310 | * index_sent_seqcount is already further than the tracefile |
| 311 | * array tail position, keep its current position. |
| 312 | */ |
| 313 | vstream->index_sent_seqcount = |
| 314 | std::max(seq_tail, vstream->index_sent_seqcount); |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Rotate a stream to the next tracefile. |
| 319 | * |
| 320 | * Must be called with the rstream lock held. |
| 321 | * Returns 0 on success, 1 on EOF. |
| 322 | */ |
| 323 | int viewer_stream_rotate(struct relay_viewer_stream *vstream) |
| 324 | { |
| 325 | int ret; |
| 326 | uint64_t new_id; |
| 327 | const struct relay_stream *stream = vstream->stream; |
| 328 | |
| 329 | /* Detect the last tracefile to open. */ |
| 330 | if (stream->index_received_seqcount |
| 331 | == vstream->index_sent_seqcount |
| 332 | && stream->trace->session->connection_closed) { |
| 333 | ret = 1; |
| 334 | goto end; |
| 335 | } |
| 336 | |
| 337 | if (stream->tracefile_count == 0) { |
| 338 | /* Ignore rotation, there is none to do. */ |
| 339 | ret = 0; |
| 340 | goto end; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Try to move to the next file. |
| 345 | */ |
| 346 | new_id = (vstream->current_tracefile_id + 1) |
| 347 | % stream->tracefile_count; |
| 348 | if (tracefile_array_seq_in_file(stream->tfa, new_id, |
| 349 | vstream->index_sent_seqcount)) { |
| 350 | vstream->current_tracefile_id = new_id; |
| 351 | } else { |
| 352 | uint64_t seq_tail = tracefile_array_get_seq_tail(stream->tfa); |
| 353 | |
| 354 | /* |
| 355 | * This can only be reached on overwrite, which implies there |
| 356 | * has been data written at some point, which will have set the |
| 357 | * tail. |
| 358 | */ |
| 359 | LTTNG_ASSERT(seq_tail != -1ULL); |
| 360 | /* |
| 361 | * We need to resync because we lag behind tail. |
| 362 | */ |
| 363 | vstream->current_tracefile_id = |
| 364 | tracefile_array_get_file_index_tail(stream->tfa); |
| 365 | vstream->index_sent_seqcount = seq_tail; |
| 366 | } |
| 367 | viewer_stream_close_files(vstream); |
| 368 | ret = 0; |
| 369 | end: |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | void print_viewer_streams(void) |
| 374 | { |
| 375 | struct lttng_ht_iter iter; |
| 376 | struct relay_viewer_stream *vstream; |
| 377 | |
| 378 | if (!viewer_streams_ht) { |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | rcu_read_lock(); |
| 383 | cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream, |
| 384 | stream_n.node) { |
| 385 | if (!viewer_stream_get(vstream)) { |
| 386 | continue; |
| 387 | } |
| 388 | DBG("vstream %p refcount %ld stream %" PRIu64 " trace %" PRIu64 |
| 389 | " session %" PRIu64, |
| 390 | vstream, |
| 391 | vstream->ref.refcount, |
| 392 | vstream->stream->stream_handle, |
| 393 | vstream->stream->trace->id, |
| 394 | vstream->stream->trace->session->id); |
| 395 | viewer_stream_put(vstream); |
| 396 | } |
| 397 | rcu_read_unlock(); |
| 398 | } |