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