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