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