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