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