Port: Use LTTNG_PATH_MAX instead of PATH_MAX
[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
20#define _GNU_SOURCE
6c1c0768 21#define _LGPL_SOURCE
2f8f53af
DG
22#include <common/common.h>
23#include <common/index/index.h>
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,
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
2f8f53af 54 vstream->path_name = 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 }
2f8f53af
DG
59 vstream->channel_name = strndup(stream->channel_name,
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) {
7591bab1 119 vstream->index_fd = NULL;
2f8f53af
DG
120 } else {
121 int read_fd;
122
123 read_fd = index_open(vstream->path_name, vstream->channel_name,
7591bab1
MD
124 stream->tracefile_count,
125 vstream->current_tracefile_id);
2f8f53af 126 if (read_fd < 0) {
7591bab1
MD
127 goto error_unlock;
128 }
129 vstream->index_fd = stream_fd_create(read_fd);
130 if (!vstream->index_fd) {
131 if (close(read_fd)) {
132 PERROR("close");
133 }
134 goto error_unlock;
2f8f53af 135 }
2f8f53af
DG
136 }
137
7591bab1 138 if (seek_t == LTTNG_VIEWER_SEEK_LAST && vstream->index_fd) {
2f8f53af
DG
139 off_t lseek_ret;
140
7591bab1 141 lseek_ret = lseek(vstream->index_fd->fd, 0, SEEK_END);
2f8f53af 142 if (lseek_ret < 0) {
7591bab1 143 goto error_unlock;
2f8f53af 144 }
7591bab1 145 }
7591bab1
MD
146 if (stream->is_metadata) {
147 rcu_assign_pointer(stream->trace->viewer_metadata_stream,
148 vstream);
2f8f53af 149 }
a44ca2ca 150 pthread_mutex_unlock(&stream->lock);
2f8f53af 151
7591bab1
MD
152 /* Globally visible after the add unique. */
153 lttng_ht_node_init_u64(&vstream->stream_n, stream->stream_handle);
154 lttng_ht_add_unique_u64(viewer_streams_ht, &vstream->stream_n);
155
156 pthread_mutex_init(&vstream->reflock, NULL);
157 urcu_ref_init(&vstream->ref);
158
2f8f53af
DG
159 return vstream;
160
7591bab1
MD
161error_unlock:
162 pthread_mutex_unlock(&stream->lock);
2f8f53af
DG
163error:
164 if (vstream) {
7591bab1 165 viewer_stream_destroy(vstream);
2f8f53af
DG
166 }
167 return NULL;
168}
169
7591bab1 170static void viewer_stream_unpublish(struct relay_viewer_stream *vstream)
2f8f53af
DG
171{
172 int ret;
173 struct lttng_ht_iter iter;
174
7591bab1 175 iter.iter.node = &vstream->stream_n.node;
2f8f53af
DG
176 ret = lttng_ht_del(viewer_streams_ht, &iter);
177 assert(!ret);
178}
179
7591bab1 180static void viewer_stream_release(struct urcu_ref *ref)
2f8f53af 181{
7591bab1
MD
182 struct relay_viewer_stream *vstream = caa_container_of(ref,
183 struct relay_viewer_stream, ref);
2a174661 184
7591bab1
MD
185 if (vstream->stream->is_metadata) {
186 rcu_assign_pointer(vstream->stream->trace->viewer_metadata_stream, NULL);
2a174661 187 }
2f8f53af 188
7591bab1
MD
189 viewer_stream_unpublish(vstream);
190
191 if (vstream->stream_fd) {
192 stream_fd_put(vstream->stream_fd);
193 vstream->stream_fd = NULL;
2f8f53af 194 }
7591bab1
MD
195 if (vstream->index_fd) {
196 stream_fd_put(vstream->index_fd);
197 vstream->index_fd = NULL;
198 }
199 if (vstream->stream) {
200 stream_put(vstream->stream);
201 vstream->stream = NULL;
202 }
203 call_rcu(&vstream->rcu_node, viewer_stream_destroy_rcu);
204}
205
206/* Must be called with RCU read-side lock held. */
207bool viewer_stream_get(struct relay_viewer_stream *vstream)
208{
209 bool has_ref = false;
210
211 pthread_mutex_lock(&vstream->reflock);
212 if (vstream->ref.refcount != 0) {
213 has_ref = true;
214 urcu_ref_get(&vstream->ref);
2f8f53af 215 }
7591bab1 216 pthread_mutex_unlock(&vstream->reflock);
2f8f53af 217
7591bab1 218 return has_ref;
2f8f53af
DG
219}
220
221/*
7591bab1 222 * Get viewer stream by id.
2f8f53af 223 *
7591bab1 224 * Return viewer stream if found else NULL.
2f8f53af 225 */
7591bab1 226struct relay_viewer_stream *viewer_stream_get_by_id(uint64_t id)
2f8f53af
DG
227{
228 struct lttng_ht_node_u64 *node;
229 struct lttng_ht_iter iter;
7591bab1 230 struct relay_viewer_stream *vstream = NULL;
2f8f53af 231
7591bab1 232 rcu_read_lock();
2f8f53af
DG
233 lttng_ht_lookup(viewer_streams_ht, &id, &iter);
234 node = lttng_ht_iter_get_node_u64(&iter);
235 if (!node) {
236 DBG("Relay viewer stream %" PRIu64 " not found", id);
237 goto end;
238 }
7591bab1
MD
239 vstream = caa_container_of(node, struct relay_viewer_stream, stream_n);
240 if (!viewer_stream_get(vstream)) {
241 vstream = NULL;
242 }
2f8f53af 243end:
7591bab1
MD
244 rcu_read_unlock();
245 return vstream;
246}
247
248void viewer_stream_put(struct relay_viewer_stream *vstream)
249{
250 rcu_read_lock();
251 pthread_mutex_lock(&vstream->reflock);
252 urcu_ref_put(&vstream->ref, viewer_stream_release);
253 pthread_mutex_unlock(&vstream->reflock);
254 rcu_read_unlock();
255}
256
2f8f53af
DG
257/*
258 * Rotate a stream to the next tracefile.
259 *
7591bab1 260 * Must be called with the rstream lock held.
2f8f53af
DG
261 * Returns 0 on success, 1 on EOF, a negative value on error.
262 */
7591bab1 263int viewer_stream_rotate(struct relay_viewer_stream *vstream)
2f8f53af
DG
264{
265 int ret;
7591bab1 266 struct relay_stream *stream = vstream->stream;
a44ca2ca 267 uint64_t new_id;
2f8f53af 268
7591bab1 269 /* Detect the last tracefile to open. */
a44ca2ca
MD
270 if (stream->index_received_seqcount
271 == vstream->index_sent_seqcount
7591bab1
MD
272 && stream->trace->session->connection_closed) {
273 ret = 1;
2a174661
DG
274 goto end;
275 }
2f8f53af 276
7591bab1
MD
277 if (stream->tracefile_count == 0) {
278 /* Ignore rotation, there is none to do. */
279 ret = 0;
2f8f53af
DG
280 goto end;
281 }
282
a44ca2ca
MD
283 /*
284 * Try to move to the next file.
285 */
286 new_id = (vstream->current_tracefile_id + 1)
287 % stream->tracefile_count;
288 if (tracefile_array_seq_in_file(stream->tfa, new_id,
289 vstream->index_sent_seqcount)) {
290 vstream->current_tracefile_id = new_id;
2f8f53af 291 } else {
a44ca2ca
MD
292 uint64_t seq_tail = tracefile_array_get_seq_tail(stream->tfa);
293
294 /*
295 * This can only be reached on overwrite, which implies there
296 * has been data written at some point, which will have set the
297 * tail.
298 */
299 assert(seq_tail != -1ULL);
300 /*
301 * We need to resync because we lag behind tail.
302 */
7591bab1 303 vstream->current_tracefile_id =
a44ca2ca
MD
304 tracefile_array_get_file_index_tail(stream->tfa);
305 vstream->index_sent_seqcount = seq_tail;
2f8f53af 306 }
2f8f53af 307
7591bab1
MD
308 if (vstream->index_fd) {
309 stream_fd_put(vstream->index_fd);
310 vstream->index_fd = NULL;
2f8f53af 311 }
7591bab1
MD
312 if (vstream->stream_fd) {
313 stream_fd_put(vstream->stream_fd);
314 vstream->stream_fd = NULL;
2f8f53af 315 }
2f8f53af 316
2f8f53af 317 ret = index_open(vstream->path_name, vstream->channel_name,
7591bab1
MD
318 stream->tracefile_count,
319 vstream->current_tracefile_id);
2f8f53af 320 if (ret < 0) {
7591bab1
MD
321 goto end;
322 }
323 vstream->index_fd = stream_fd_create(ret);
324 if (vstream->index_fd) {
325 ret = 0;
326 } else {
327 if (close(ret)) {
328 PERROR("close");
329 }
330 ret = -1;
2f8f53af 331 }
2f8f53af 332end:
2f8f53af
DG
333 return ret;
334}
7591bab1
MD
335
336void print_viewer_streams(void)
337{
338 struct lttng_ht_iter iter;
339 struct relay_viewer_stream *vstream;
340
341 rcu_read_lock();
342 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream,
343 stream_n.node) {
344 if (!viewer_stream_get(vstream)) {
345 continue;
346 }
347 DBG("vstream %p refcount %ld stream %" PRIu64 " trace %" PRIu64
348 " session %" PRIu64,
349 vstream,
350 vstream->ref.refcount,
351 vstream->stream->stream_handle,
352 vstream->stream->trace->id,
353 vstream->stream->trace->session->id);
354 viewer_stream_put(vstream);
355 }
356 rcu_read_unlock();
357}
This page took 0.066767 seconds and 4 git commands to generate.