Fix: Relay daemon ownership and reference counting
[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>
9a78c92e 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
21#include <common/common.h>
22#include <common/index/index.h>
23
24#include "lttng-relayd.h"
25#include "viewer-stream.h"
26
9a78c92e 27static void viewer_stream_destroy(struct relay_viewer_stream *vstream)
2f8f53af 28{
9a78c92e
MD
29 free(vstream->path_name);
30 free(vstream->channel_name);
31 free(vstream);
2f8f53af
DG
32}
33
9a78c92e 34static void viewer_stream_destroy_rcu(struct rcu_head *head)
2f8f53af 35{
9a78c92e 36 struct relay_viewer_stream *vstream =
2f8f53af
DG
37 caa_container_of(head, struct relay_viewer_stream, rcu_node);
38
9a78c92e 39 viewer_stream_destroy(vstream);
2f8f53af
DG
40}
41
42struct relay_viewer_stream *viewer_stream_create(struct relay_stream *stream,
9a78c92e 43 enum lttng_viewer_seek seek_t)
2f8f53af
DG
44{
45 struct relay_viewer_stream *vstream;
46
2f8f53af
DG
47 vstream = zmalloc(sizeof(*vstream));
48 if (!vstream) {
49 PERROR("relay viewer stream zmalloc");
50 goto error;
51 }
52
2f8f53af 53 vstream->path_name = strndup(stream->path_name, LTTNG_VIEWER_PATH_MAX);
a8267672
MD
54 if (vstream->path_name == NULL) {
55 PERROR("relay viewer path_name alloc");
56 goto error;
57 }
2f8f53af
DG
58 vstream->channel_name = strndup(stream->channel_name,
59 LTTNG_VIEWER_NAME_MAX);
a8267672
MD
60 if (vstream->channel_name == NULL) {
61 PERROR("relay viewer channel_name alloc");
62 goto error;
63 }
2f8f53af
DG
64
65 switch (seek_t) {
c4e361a4 66 case LTTNG_VIEWER_SEEK_BEGINNING:
9a78c92e 67 vstream->current_tracefile_id = stream->oldest_tracefile_id;
2f8f53af 68 break;
c4e361a4 69 case LTTNG_VIEWER_SEEK_LAST:
9a78c92e 70 vstream->current_tracefile_id = stream->current_tracefile_id;
2f8f53af
DG
71 break;
72 default:
2f8f53af
DG
73 goto error;
74 }
9a78c92e
MD
75 if (!stream_get(stream)) {
76 ERR("Cannot get stream");
77 goto error;
2f8f53af 78 }
9a78c92e 79 vstream->stream = stream;
2f8f53af 80
9a78c92e 81 pthread_mutex_lock(&stream->lock);
2f8f53af
DG
82 /*
83 * If we never received an index for the current stream, delay the opening
84 * of the index, otherwise open it right now.
85 */
9a78c92e
MD
86 if (vstream->current_tracefile_id == stream->current_tracefile_id
87 && stream->total_index_received == 0) {
88 vstream->index_fd = NULL;
2f8f53af
DG
89 } else {
90 int read_fd;
91
92 read_fd = index_open(vstream->path_name, vstream->channel_name,
9a78c92e
MD
93 stream->tracefile_count,
94 vstream->current_tracefile_id);
2f8f53af 95 if (read_fd < 0) {
9a78c92e
MD
96 goto error_unlock;
97 }
98 vstream->index_fd = stream_fd_create(read_fd);
99 if (!vstream->index_fd) {
100 if (close(read_fd)) {
101 PERROR("close");
102 }
103 goto error_unlock;
2f8f53af 104 }
2f8f53af
DG
105 }
106
9a78c92e 107 if (seek_t == LTTNG_VIEWER_SEEK_LAST && vstream->index_fd) {
2f8f53af
DG
108 off_t lseek_ret;
109
9a78c92e 110 lseek_ret = lseek(vstream->index_fd->fd, 0, SEEK_END);
2f8f53af 111 if (lseek_ret < 0) {
9a78c92e 112 goto error_unlock;
2f8f53af 113 }
9a78c92e
MD
114 vstream->last_sent_index = stream->total_index_received;
115 }
116 pthread_mutex_unlock(&stream->lock);
117
118 if (stream->is_metadata) {
119 rcu_assign_pointer(stream->trace->viewer_metadata_stream,
120 vstream);
2f8f53af
DG
121 }
122
9a78c92e
MD
123 /* Globally visible after the add unique. */
124 lttng_ht_node_init_u64(&vstream->stream_n, stream->stream_handle);
125 lttng_ht_add_unique_u64(viewer_streams_ht, &vstream->stream_n);
126
127 pthread_mutex_init(&vstream->reflock, NULL);
128 urcu_ref_init(&vstream->ref);
129
2f8f53af
DG
130 return vstream;
131
9a78c92e
MD
132error_unlock:
133 pthread_mutex_unlock(&stream->lock);
2f8f53af
DG
134error:
135 if (vstream) {
9a78c92e 136 viewer_stream_destroy(vstream);
2f8f53af
DG
137 }
138 return NULL;
139}
140
9a78c92e 141static void viewer_stream_unpublish(struct relay_viewer_stream *vstream)
2f8f53af
DG
142{
143 int ret;
144 struct lttng_ht_iter iter;
145
9a78c92e 146 iter.iter.node = &vstream->stream_n.node;
2f8f53af
DG
147 ret = lttng_ht_del(viewer_streams_ht, &iter);
148 assert(!ret);
149}
150
9a78c92e 151static void viewer_stream_release(struct urcu_ref *ref)
2f8f53af 152{
9a78c92e
MD
153 struct relay_viewer_stream *vstream = caa_container_of(ref,
154 struct relay_viewer_stream, ref);
2a174661 155
9a78c92e
MD
156 if (vstream->stream->is_metadata) {
157 rcu_assign_pointer(vstream->stream->trace->viewer_metadata_stream, NULL);
2a174661 158 }
2f8f53af 159
9a78c92e
MD
160 viewer_stream_unpublish(vstream);
161
162 if (vstream->stream_fd) {
163 stream_fd_put(vstream->stream_fd);
164 vstream->stream_fd = NULL;
2f8f53af 165 }
9a78c92e
MD
166 if (vstream->index_fd) {
167 stream_fd_put(vstream->index_fd);
168 vstream->index_fd = NULL;
169 }
170 if (vstream->stream) {
171 stream_put(vstream->stream);
172 vstream->stream = NULL;
173 }
174 call_rcu(&vstream->rcu_node, viewer_stream_destroy_rcu);
175}
176
177/* Must be called with RCU read-side lock held. */
178bool viewer_stream_get(struct relay_viewer_stream *vstream)
179{
180 bool has_ref = false;
181
182 pthread_mutex_lock(&vstream->reflock);
183 if (vstream->ref.refcount != 0) {
184 has_ref = true;
185 urcu_ref_get(&vstream->ref);
2f8f53af 186 }
9a78c92e 187 pthread_mutex_unlock(&vstream->reflock);
2f8f53af 188
9a78c92e 189 return has_ref;
2f8f53af
DG
190}
191
192/*
9a78c92e 193 * Get viewer stream by id.
2f8f53af 194 *
9a78c92e 195 * Return viewer stream if found else NULL.
2f8f53af 196 */
9a78c92e 197struct relay_viewer_stream *viewer_stream_get_by_id(uint64_t id)
2f8f53af
DG
198{
199 struct lttng_ht_node_u64 *node;
200 struct lttng_ht_iter iter;
9a78c92e 201 struct relay_viewer_stream *vstream = NULL;
2f8f53af 202
9a78c92e 203 rcu_read_lock();
2f8f53af
DG
204 lttng_ht_lookup(viewer_streams_ht, &id, &iter);
205 node = lttng_ht_iter_get_node_u64(&iter);
206 if (!node) {
207 DBG("Relay viewer stream %" PRIu64 " not found", id);
208 goto end;
209 }
9a78c92e
MD
210 vstream = caa_container_of(node, struct relay_viewer_stream, stream_n);
211 if (!viewer_stream_get(vstream)) {
212 vstream = NULL;
213 }
2f8f53af 214end:
9a78c92e
MD
215 rcu_read_unlock();
216 return vstream;
217}
218
219void viewer_stream_put(struct relay_viewer_stream *vstream)
220{
221 rcu_read_lock();
222 pthread_mutex_lock(&vstream->reflock);
223 urcu_ref_put(&vstream->ref, viewer_stream_release);
224 pthread_mutex_unlock(&vstream->reflock);
225 rcu_read_unlock();
226}
227
228/*
229 * Returns whether the current tracefile is readable. If not, it has
230 * been overwritten.
231 * Must be called with rstream lock held.
232 */
233bool viewer_stream_is_tracefile_seq_readable(struct relay_viewer_stream *vstream,
234 uint64_t seq)
235{
236 struct relay_stream *stream = vstream->stream;
237
238 if (seq >= stream->oldest_tracefile_seq
239 && seq <= stream->current_tracefile_seq) {
240 /* seq is a readable file. */
241 return true;
242 } else {
243 /* seq is not readable. */
244 return false;
245 }
2f8f53af
DG
246}
247
248/*
249 * Rotate a stream to the next tracefile.
250 *
9a78c92e 251 * Must be called with the rstream lock held.
2f8f53af
DG
252 * Returns 0 on success, 1 on EOF, a negative value on error.
253 */
9a78c92e 254int viewer_stream_rotate(struct relay_viewer_stream *vstream)
2f8f53af
DG
255{
256 int ret;
9a78c92e 257 struct relay_stream *stream = vstream->stream;
2f8f53af 258
9a78c92e
MD
259 /* Detect the last tracefile to open. */
260 if (stream->total_index_received == vstream->last_sent_index
261 && stream->trace->session->connection_closed) {
262 ret = 1;
2a174661
DG
263 goto end;
264 }
2f8f53af 265
9a78c92e
MD
266 if (stream->tracefile_count == 0) {
267 /* Ignore rotation, there is none to do. */
268 ret = 0;
2f8f53af
DG
269 goto end;
270 }
271
9a78c92e
MD
272 if (!viewer_stream_is_tracefile_seq_readable(vstream,
273 vstream->current_tracefile_seq + 1)) {
274 vstream->current_tracefile_id =
275 stream->oldest_tracefile_id;
276 vstream->current_tracefile_seq =
277 stream->oldest_tracefile_seq;
2f8f53af 278 } else {
9a78c92e
MD
279 vstream->current_tracefile_id =
280 (vstream->current_tracefile_id + 1)
281 % stream->tracefile_count;
282 vstream->current_tracefile_seq++;
2f8f53af 283 }
2f8f53af 284
9a78c92e
MD
285 if (vstream->index_fd) {
286 stream_fd_put(vstream->index_fd);
287 vstream->index_fd = NULL;
2f8f53af 288 }
9a78c92e
MD
289 if (vstream->stream_fd) {
290 stream_fd_put(vstream->stream_fd);
291 vstream->stream_fd = NULL;
2f8f53af 292 }
2f8f53af 293
2f8f53af 294 ret = index_open(vstream->path_name, vstream->channel_name,
9a78c92e
MD
295 stream->tracefile_count,
296 vstream->current_tracefile_id);
2f8f53af 297 if (ret < 0) {
9a78c92e
MD
298 goto end;
299 }
300 vstream->index_fd = stream_fd_create(ret);
301 if (vstream->index_fd) {
302 ret = 0;
303 } else {
304 if (close(ret)) {
305 PERROR("close");
306 }
307 ret = -1;
2f8f53af 308 }
2f8f53af 309end:
2f8f53af
DG
310 return ret;
311}
9a78c92e
MD
312
313void print_viewer_streams(void)
314{
315 struct lttng_ht_iter iter;
316 struct relay_viewer_stream *vstream;
317
318 rcu_read_lock();
319 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream,
320 stream_n.node) {
321 if (!viewer_stream_get(vstream)) {
322 continue;
323 }
324 DBG("vstream %p refcount %ld stream %" PRIu64 " trace %" PRIu64
325 " session %" PRIu64,
326 vstream,
327 vstream->ref.refcount,
328 vstream->stream->stream_handle,
329 vstream->stream->trace->id,
330 vstream->stream->trace->session->id);
331 viewer_stream_put(vstream);
332 }
333 rcu_read_unlock();
334}
This page took 0.041653 seconds and 4 git commands to generate.