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