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