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