Fix: Relay daemon ownership and reference counting
[lttng-tools.git] / src / bin / lttng-relayd / viewer-stream.c
1 /*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
27 static void viewer_stream_destroy(struct relay_viewer_stream *vstream)
28 {
29 free(vstream->path_name);
30 free(vstream->channel_name);
31 free(vstream);
32 }
33
34 static void viewer_stream_destroy_rcu(struct rcu_head *head)
35 {
36 struct relay_viewer_stream *vstream =
37 caa_container_of(head, struct relay_viewer_stream, rcu_node);
38
39 viewer_stream_destroy(vstream);
40 }
41
42 struct relay_viewer_stream *viewer_stream_create(struct relay_stream *stream,
43 enum lttng_viewer_seek seek_t)
44 {
45 struct relay_viewer_stream *vstream;
46
47 vstream = zmalloc(sizeof(*vstream));
48 if (!vstream) {
49 PERROR("relay viewer stream zmalloc");
50 goto error;
51 }
52
53 vstream->path_name = strndup(stream->path_name, LTTNG_VIEWER_PATH_MAX);
54 if (vstream->path_name == NULL) {
55 PERROR("relay viewer path_name alloc");
56 goto error;
57 }
58 vstream->channel_name = strndup(stream->channel_name,
59 LTTNG_VIEWER_NAME_MAX);
60 if (vstream->channel_name == NULL) {
61 PERROR("relay viewer channel_name alloc");
62 goto error;
63 }
64
65 switch (seek_t) {
66 case LTTNG_VIEWER_SEEK_BEGINNING:
67 vstream->current_tracefile_id = stream->oldest_tracefile_id;
68 break;
69 case LTTNG_VIEWER_SEEK_LAST:
70 vstream->current_tracefile_id = stream->current_tracefile_id;
71 break;
72 default:
73 goto error;
74 }
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 we never received an index for the current stream, delay the opening
84 * of the index, otherwise open it right now.
85 */
86 if (vstream->current_tracefile_id == stream->current_tracefile_id
87 && stream->total_index_received == 0) {
88 vstream->index_fd = NULL;
89 } else {
90 int read_fd;
91
92 read_fd = index_open(vstream->path_name, vstream->channel_name,
93 stream->tracefile_count,
94 vstream->current_tracefile_id);
95 if (read_fd < 0) {
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;
104 }
105 }
106
107 if (seek_t == LTTNG_VIEWER_SEEK_LAST && vstream->index_fd) {
108 off_t lseek_ret;
109
110 lseek_ret = lseek(vstream->index_fd->fd, 0, SEEK_END);
111 if (lseek_ret < 0) {
112 goto error_unlock;
113 }
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);
121 }
122
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
130 return vstream;
131
132 error_unlock:
133 pthread_mutex_unlock(&stream->lock);
134 error:
135 if (vstream) {
136 viewer_stream_destroy(vstream);
137 }
138 return NULL;
139 }
140
141 static void viewer_stream_unpublish(struct relay_viewer_stream *vstream)
142 {
143 int ret;
144 struct lttng_ht_iter iter;
145
146 iter.iter.node = &vstream->stream_n.node;
147 ret = lttng_ht_del(viewer_streams_ht, &iter);
148 assert(!ret);
149 }
150
151 static void viewer_stream_release(struct urcu_ref *ref)
152 {
153 struct relay_viewer_stream *vstream = caa_container_of(ref,
154 struct relay_viewer_stream, ref);
155
156 if (vstream->stream->is_metadata) {
157 rcu_assign_pointer(vstream->stream->trace->viewer_metadata_stream, NULL);
158 }
159
160 viewer_stream_unpublish(vstream);
161
162 if (vstream->stream_fd) {
163 stream_fd_put(vstream->stream_fd);
164 vstream->stream_fd = NULL;
165 }
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. */
178 bool 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);
186 }
187 pthread_mutex_unlock(&vstream->reflock);
188
189 return has_ref;
190 }
191
192 /*
193 * Get viewer stream by id.
194 *
195 * Return viewer stream if found else NULL.
196 */
197 struct relay_viewer_stream *viewer_stream_get_by_id(uint64_t id)
198 {
199 struct lttng_ht_node_u64 *node;
200 struct lttng_ht_iter iter;
201 struct relay_viewer_stream *vstream = NULL;
202
203 rcu_read_lock();
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 }
210 vstream = caa_container_of(node, struct relay_viewer_stream, stream_n);
211 if (!viewer_stream_get(vstream)) {
212 vstream = NULL;
213 }
214 end:
215 rcu_read_unlock();
216 return vstream;
217 }
218
219 void 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 */
233 bool 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 }
246 }
247
248 /*
249 * Rotate a stream to the next tracefile.
250 *
251 * Must be called with the rstream lock held.
252 * Returns 0 on success, 1 on EOF, a negative value on error.
253 */
254 int viewer_stream_rotate(struct relay_viewer_stream *vstream)
255 {
256 int ret;
257 struct relay_stream *stream = vstream->stream;
258
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;
263 goto end;
264 }
265
266 if (stream->tracefile_count == 0) {
267 /* Ignore rotation, there is none to do. */
268 ret = 0;
269 goto end;
270 }
271
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;
278 } else {
279 vstream->current_tracefile_id =
280 (vstream->current_tracefile_id + 1)
281 % stream->tracefile_count;
282 vstream->current_tracefile_seq++;
283 }
284
285 if (vstream->index_fd) {
286 stream_fd_put(vstream->index_fd);
287 vstream->index_fd = NULL;
288 }
289 if (vstream->stream_fd) {
290 stream_fd_put(vstream->stream_fd);
291 vstream->stream_fd = NULL;
292 }
293
294 ret = index_open(vstream->path_name, vstream->channel_name,
295 stream->tracefile_count,
296 vstream->current_tracefile_id);
297 if (ret < 0) {
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;
308 }
309 end:
310 return ret;
311 }
312
313 void 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.036169 seconds and 4 git commands to generate.