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