Fix: relayd: live: erroneous message timestamp observed from live viewer
[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
2f8f53af
DG
11#include <common/common.h>
12#include <common/index/index.h>
f5436bfc 13#include <common/compat/string.h>
b0d240a2
MD
14#include <common/utils.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
7fb6d478 18#include <algorithm>
2f8f53af
DG
19
20#include "lttng-relayd.h"
21#include "viewer-stream.h"
22
7591bab1 23static void viewer_stream_destroy(struct relay_viewer_stream *vstream)
2f8f53af 24{
80516611 25 lttng_trace_chunk_put(vstream->stream_file.trace_chunk);
7591bab1
MD
26 free(vstream->path_name);
27 free(vstream->channel_name);
28 free(vstream);
2f8f53af
DG
29}
30
7591bab1 31static void viewer_stream_destroy_rcu(struct rcu_head *head)
2f8f53af 32{
7591bab1 33 struct relay_viewer_stream *vstream =
2f8f53af
DG
34 caa_container_of(head, struct relay_viewer_stream, rcu_node);
35
7591bab1 36 viewer_stream_destroy(vstream);
2f8f53af
DG
37}
38
9edaf114 39/* Relay stream's lock must be held by the caller. */
2f8f53af 40struct relay_viewer_stream *viewer_stream_create(struct relay_stream *stream,
9edaf114 41 struct lttng_trace_chunk *trace_chunk,
7591bab1 42 enum lttng_viewer_seek seek_t)
2f8f53af 43{
ebb29c10 44 struct relay_viewer_stream *vstream = NULL;
ebb29c10 45
9edaf114 46 ASSERT_LOCKED(stream->lock);
2f8f53af 47
ac497a37 48 vstream = (relay_viewer_stream *) zmalloc(sizeof(*vstream));
2f8f53af
DG
49 if (!vstream) {
50 PERROR("relay viewer stream zmalloc");
51 goto error;
52 }
53
80516611
JG
54 if (trace_chunk) {
55 const bool acquired_reference = lttng_trace_chunk_get(
56 trace_chunk);
57
a0377dfe 58 LTTNG_ASSERT(acquired_reference);
80516611
JG
59 }
60
9edaf114 61 vstream->stream_file.trace_chunk = trace_chunk;
f5436bfc 62 vstream->path_name = lttng_strndup(stream->path_name, LTTNG_VIEWER_PATH_MAX);
b272577e
MD
63 if (vstream->path_name == NULL) {
64 PERROR("relay viewer path_name alloc");
65 goto error;
66 }
f5436bfc 67 vstream->channel_name = lttng_strndup(stream->channel_name,
2f8f53af 68 LTTNG_VIEWER_NAME_MAX);
b272577e
MD
69 if (vstream->channel_name == NULL) {
70 PERROR("relay viewer channel_name alloc");
71 goto error;
72 }
2f8f53af 73
a44ca2ca
MD
74 if (!stream_get(stream)) {
75 ERR("Cannot get stream");
76 goto error;
77 }
78 vstream->stream = stream;
79
a44ca2ca
MD
80 if (stream->is_metadata && stream->trace->viewer_metadata_stream) {
81 ERR("Cannot attach viewer metadata stream to trace (busy).");
71381736 82 goto error;
a44ca2ca
MD
83 }
84
2f8f53af 85 switch (seek_t) {
c4e361a4 86 case LTTNG_VIEWER_SEEK_BEGINNING:
a44ca2ca
MD
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;
2f8f53af 101 break;
a44ca2ca 102 }
c4e361a4 103 case LTTNG_VIEWER_SEEK_LAST:
a44ca2ca 104 vstream->current_tracefile_id =
78118e3b 105 tracefile_array_get_read_file_index_head(stream->tfa);
a44ca2ca
MD
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;
2f8f53af
DG
115 break;
116 default:
71381736 117 goto error;
2f8f53af 118 }
2f8f53af 119
2f8f53af 120 /*
a44ca2ca
MD
121 * If we never received an index for the current stream, delay
122 * the opening of the index, otherwise open it right now.
2f8f53af 123 */
b0d240a2 124 if (stream->index_file == NULL) {
f8f3885c 125 vstream->index_file = NULL;
0eafad74 126 } else if (vstream->stream_file.trace_chunk) {
ebb29c10
JG
127 const uint32_t connection_major = stream->trace->session->major;
128 const uint32_t connection_minor = stream->trace->session->minor;
3ff5c5db 129 enum lttng_trace_chunk_status chunk_status;
ebb29c10 130
3ff5c5db 131 chunk_status = lttng_index_file_create_from_trace_chunk_read_only(
b66a15d1
JG
132 vstream->stream_file.trace_chunk,
133 stream->path_name,
ebb29c10
JG
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,
3ff5c5db
MD
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 {
71381736 145 goto error;
3ff5c5db 146 }
2f8f53af 147 }
2f8f53af
DG
148 }
149
b0d240a2
MD
150 /*
151 * If we never received a data file for the current stream, delay the
152 * opening, otherwise open it right now.
153 */
0eafad74 154 if (stream->file && vstream->stream_file.trace_chunk) {
8bb66c3c 155 int ret;
b0d240a2
MD
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) {
71381736 164 goto error;
b0d240a2
MD
165 }
166
8bb66c3c
JG
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);
b0d240a2 171 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
71381736 172 goto error;
b0d240a2 173 }
b0d240a2
MD
174 }
175
f8f3885c 176 if (seek_t == LTTNG_VIEWER_SEEK_LAST && vstream->index_file) {
2f8f53af
DG
177 off_t lseek_ret;
178
8bb66c3c
JG
179 lseek_ret = fs_handle_seek(
180 vstream->index_file->file, 0, SEEK_END);
2f8f53af 181 if (lseek_ret < 0) {
71381736 182 goto error;
2f8f53af 183 }
7591bab1 184 }
7591bab1
MD
185 if (stream->is_metadata) {
186 rcu_assign_pointer(stream->trace->viewer_metadata_stream,
187 vstream);
2f8f53af
DG
188 }
189
80516611
JG
190 vstream->last_seen_rotation_count = stream->completed_rotation_count;
191
7591bab1
MD
192 /* Globally visible after the add unique. */
193 lttng_ht_node_init_u64(&vstream->stream_n, stream->stream_handle);
7591bab1 194 urcu_ref_init(&vstream->ref);
c51b2177 195 lttng_ht_add_unique_u64(viewer_streams_ht, &vstream->stream_n);
7591bab1 196
2f8f53af
DG
197 return vstream;
198
199error:
200 if (vstream) {
7591bab1 201 viewer_stream_destroy(vstream);
2f8f53af
DG
202 }
203 return NULL;
204}
205
7591bab1 206static void viewer_stream_unpublish(struct relay_viewer_stream *vstream)
2f8f53af
DG
207{
208 int ret;
209 struct lttng_ht_iter iter;
210
7591bab1 211 iter.iter.node = &vstream->stream_n.node;
2f8f53af 212 ret = lttng_ht_del(viewer_streams_ht, &iter);
a0377dfe 213 LTTNG_ASSERT(!ret);
2f8f53af
DG
214}
215
7591bab1 216static void viewer_stream_release(struct urcu_ref *ref)
2f8f53af 217{
7591bab1
MD
218 struct relay_viewer_stream *vstream = caa_container_of(ref,
219 struct relay_viewer_stream, ref);
2a174661 220
7591bab1
MD
221 if (vstream->stream->is_metadata) {
222 rcu_assign_pointer(vstream->stream->trace->viewer_metadata_stream, NULL);
2a174661 223 }
2f8f53af 224
7591bab1
MD
225 viewer_stream_unpublish(vstream);
226
8bb66c3c
JG
227 if (vstream->stream_file.handle) {
228 fs_handle_close(vstream->stream_file.handle);
229 vstream->stream_file.handle = NULL;
2f8f53af 230 }
f8f3885c
MD
231 if (vstream->index_file) {
232 lttng_index_file_put(vstream->index_file);
233 vstream->index_file = NULL;
7591bab1
MD
234 }
235 if (vstream->stream) {
236 stream_put(vstream->stream);
237 vstream->stream = NULL;
238 }
4c2717fc
JG
239 lttng_trace_chunk_put(vstream->stream_file.trace_chunk);
240 vstream->stream_file.trace_chunk = NULL;
7591bab1
MD
241 call_rcu(&vstream->rcu_node, viewer_stream_destroy_rcu);
242}
243
244/* Must be called with RCU read-side lock held. */
245bool viewer_stream_get(struct relay_viewer_stream *vstream)
246{
ce4d4083 247 return urcu_ref_get_unless_zero(&vstream->ref);
2f8f53af
DG
248}
249
250/*
7591bab1 251 * Get viewer stream by id.
2f8f53af 252 *
7591bab1 253 * Return viewer stream if found else NULL.
2f8f53af 254 */
7591bab1 255struct relay_viewer_stream *viewer_stream_get_by_id(uint64_t id)
2f8f53af
DG
256{
257 struct lttng_ht_node_u64 *node;
258 struct lttng_ht_iter iter;
7591bab1 259 struct relay_viewer_stream *vstream = NULL;
2f8f53af 260
7591bab1 261 rcu_read_lock();
2f8f53af
DG
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 }
7591bab1
MD
268 vstream = caa_container_of(node, struct relay_viewer_stream, stream_n);
269 if (!viewer_stream_get(vstream)) {
270 vstream = NULL;
271 }
2f8f53af 272end:
7591bab1
MD
273 rcu_read_unlock();
274 return vstream;
275}
276
277void viewer_stream_put(struct relay_viewer_stream *vstream)
278{
279 rcu_read_lock();
7591bab1 280 urcu_ref_put(&vstream->ref, viewer_stream_release);
7591bab1
MD
281 rcu_read_unlock();
282}
283
3087b021
MD
284void 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 }
8bb66c3c
JG
290 if (vstream->stream_file.handle) {
291 fs_handle_close(vstream->stream_file.handle);
292 vstream->stream_file.handle = NULL;
3087b021
MD
293 }
294}
295
296void 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 }
7fb6d478
MD
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);
3087b021
MD
315}
316
2f8f53af
DG
317/*
318 * Rotate a stream to the next tracefile.
319 *
7591bab1 320 * Must be called with the rstream lock held.
b0d240a2 321 * Returns 0 on success, 1 on EOF.
2f8f53af 322 */
7591bab1 323int viewer_stream_rotate(struct relay_viewer_stream *vstream)
2f8f53af
DG
324{
325 int ret;
a44ca2ca 326 uint64_t new_id;
ebb29c10 327 const struct relay_stream *stream = vstream->stream;
2f8f53af 328
7591bab1 329 /* Detect the last tracefile to open. */
a44ca2ca
MD
330 if (stream->index_received_seqcount
331 == vstream->index_sent_seqcount
7591bab1
MD
332 && stream->trace->session->connection_closed) {
333 ret = 1;
2a174661
DG
334 goto end;
335 }
2f8f53af 336
7591bab1
MD
337 if (stream->tracefile_count == 0) {
338 /* Ignore rotation, there is none to do. */
339 ret = 0;
2f8f53af
DG
340 goto end;
341 }
342
a44ca2ca
MD
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;
2f8f53af 351 } else {
a44ca2ca
MD
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 */
a0377dfe 359 LTTNG_ASSERT(seq_tail != -1ULL);
a44ca2ca
MD
360 /*
361 * We need to resync because we lag behind tail.
362 */
7591bab1 363 vstream->current_tracefile_id =
a44ca2ca
MD
364 tracefile_array_get_file_index_tail(stream->tfa);
365 vstream->index_sent_seqcount = seq_tail;
2f8f53af 366 }
b0d240a2
MD
367 viewer_stream_close_files(vstream);
368 ret = 0;
2f8f53af 369end:
2f8f53af
DG
370 return ret;
371}
7591bab1
MD
372
373void print_viewer_streams(void)
374{
375 struct lttng_ht_iter iter;
376 struct relay_viewer_stream *vstream;
377
ce3f3ba3
JG
378 if (!viewer_streams_ht) {
379 return;
380 }
381
7591bab1
MD
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}
This page took 0.067818 seconds and 4 git commands to generate.