Fix: unpublish stream on close
[lttng-tools.git] / src / bin / lttng-relayd / 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 #define _LGPL_SOURCE
22 #include <common/common.h>
23 #include <common/utils.h>
24 #include <common/defaults.h>
25 #include <urcu/rculist.h>
26 #include <sys/stat.h>
27
28 #include "lttng-relayd.h"
29 #include "index.h"
30 #include "stream.h"
31 #include "viewer-stream.h"
32
33 /* Should be called with RCU read-side lock held. */
34 bool stream_get(struct relay_stream *stream)
35 {
36 bool has_ref = false;
37
38 pthread_mutex_lock(&stream->reflock);
39 if (stream->ref.refcount != 0) {
40 has_ref = true;
41 urcu_ref_get(&stream->ref);
42 }
43 pthread_mutex_unlock(&stream->reflock);
44
45 return has_ref;
46 }
47
48 /*
49 * Get stream from stream id from the streams hash table. Return stream
50 * if found else NULL. A stream reference is taken when a stream is
51 * returned. stream_put() must be called on that stream.
52 */
53 struct relay_stream *stream_get_by_id(uint64_t stream_id)
54 {
55 struct lttng_ht_node_u64 *node;
56 struct lttng_ht_iter iter;
57 struct relay_stream *stream = NULL;
58
59 rcu_read_lock();
60 lttng_ht_lookup(relay_streams_ht, &stream_id, &iter);
61 node = lttng_ht_iter_get_node_u64(&iter);
62 if (!node) {
63 DBG("Relay stream %" PRIu64 " not found", stream_id);
64 goto end;
65 }
66 stream = caa_container_of(node, struct relay_stream, node);
67 if (!stream_get(stream)) {
68 stream = NULL;
69 }
70 end:
71 rcu_read_unlock();
72 return stream;
73 }
74
75 /*
76 * We keep ownership of path_name and channel_name.
77 */
78 struct relay_stream *stream_create(struct ctf_trace *trace,
79 uint64_t stream_handle, char *path_name,
80 char *channel_name, uint64_t tracefile_size,
81 uint64_t tracefile_count)
82 {
83 int ret;
84 struct relay_stream *stream = NULL;
85 struct relay_session *session = trace->session;
86
87 stream = zmalloc(sizeof(struct relay_stream));
88 if (stream == NULL) {
89 PERROR("relay stream zmalloc");
90 ret = -1;
91 goto error_no_alloc;
92 }
93
94 stream->stream_handle = stream_handle;
95 stream->prev_seq = -1ULL;
96 stream->ctf_stream_id = -1ULL;
97 stream->tracefile_size = tracefile_size;
98 stream->tracefile_count = tracefile_count;
99 stream->path_name = path_name;
100 stream->channel_name = channel_name;
101 lttng_ht_node_init_u64(&stream->node, stream->stream_handle);
102 pthread_mutex_init(&stream->lock, NULL);
103 pthread_mutex_init(&stream->reflock, NULL);
104 urcu_ref_init(&stream->ref);
105 ctf_trace_get(trace);
106 stream->trace = trace;
107
108 stream->indexes_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
109 if (!stream->indexes_ht) {
110 ERR("Cannot created indexes_ht");
111 ret = -1;
112 goto end;
113 }
114
115 ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG,
116 -1, -1);
117 if (ret < 0) {
118 ERR("relay creating output directory");
119 goto end;
120 }
121
122 /*
123 * No need to use run_as API here because whatever we receive,
124 * the relayd uses its own credentials for the stream files.
125 */
126 ret = utils_create_stream_file(stream->path_name, stream->channel_name,
127 stream->tracefile_size, 0, -1, -1, NULL);
128 if (ret < 0) {
129 ERR("Create output file");
130 goto end;
131 }
132 stream->stream_fd = stream_fd_create(ret);
133 if (!stream->stream_fd) {
134 if (close(ret)) {
135 PERROR("Error closing file %d", ret);
136 }
137 ret = -1;
138 goto end;
139 }
140 stream->tfa = tracefile_array_create(stream->tracefile_count);
141 if (!stream->tfa) {
142 ret = -1;
143 goto end;
144 }
145 if (stream->tracefile_size) {
146 DBG("Tracefile %s/%s_0 created", stream->path_name, stream->channel_name);
147 } else {
148 DBG("Tracefile %s/%s created", stream->path_name, stream->channel_name);
149 }
150
151 if (!strncmp(stream->channel_name, DEFAULT_METADATA_NAME, NAME_MAX)) {
152 stream->is_metadata = 1;
153 }
154
155 stream->in_recv_list = true;
156
157 /*
158 * Add the stream in the recv list of the session. Once the end stream
159 * message is received, all session streams are published.
160 */
161 pthread_mutex_lock(&session->recv_list_lock);
162 cds_list_add_rcu(&stream->recv_node, &session->recv_list);
163 session->stream_count++;
164 pthread_mutex_unlock(&session->recv_list_lock);
165
166 /*
167 * Both in the ctf_trace object and the global stream ht since the data
168 * side of the relayd does not have the concept of session.
169 */
170 lttng_ht_add_unique_u64(relay_streams_ht, &stream->node);
171 stream->in_stream_ht = true;
172
173 DBG("Relay new stream added %s with ID %" PRIu64, stream->channel_name,
174 stream->stream_handle);
175 ret = 0;
176
177 end:
178 if (ret) {
179 if (stream->stream_fd) {
180 stream_fd_put(stream->stream_fd);
181 stream->stream_fd = NULL;
182 }
183 stream_put(stream);
184 stream = NULL;
185 }
186 return stream;
187
188 error_no_alloc:
189 /*
190 * path_name and channel_name need to be freed explicitly here
191 * because we cannot rely on stream_put().
192 */
193 free(path_name);
194 free(channel_name);
195 return NULL;
196 }
197
198 /*
199 * Called with the session lock held.
200 */
201 void stream_publish(struct relay_stream *stream)
202 {
203 struct relay_session *session;
204
205 pthread_mutex_lock(&stream->lock);
206 if (stream->published) {
207 goto unlock;
208 }
209
210 session = stream->trace->session;
211
212 pthread_mutex_lock(&session->recv_list_lock);
213 if (stream->in_recv_list) {
214 cds_list_del_rcu(&stream->recv_node);
215 stream->in_recv_list = false;
216 }
217 pthread_mutex_unlock(&session->recv_list_lock);
218
219 pthread_mutex_lock(&stream->trace->stream_list_lock);
220 cds_list_add_rcu(&stream->stream_node, &stream->trace->stream_list);
221 pthread_mutex_unlock(&stream->trace->stream_list_lock);
222
223 stream->published = true;
224 unlock:
225 pthread_mutex_unlock(&stream->lock);
226 }
227
228 /*
229 * Stream must be protected by holding the stream lock or by virtue of being
230 * called from stream_destroy, in which case it is guaranteed to be accessed
231 * from a single thread by the reflock.
232 */
233 static void stream_unpublish(struct relay_stream *stream)
234 {
235 if (stream->in_stream_ht) {
236 struct lttng_ht_iter iter;
237 int ret;
238
239 iter.iter.node = &stream->node.node;
240 ret = lttng_ht_del(relay_streams_ht, &iter);
241 assert(!ret);
242 stream->in_stream_ht = false;
243 }
244 if (stream->published) {
245 pthread_mutex_lock(&stream->trace->stream_list_lock);
246 cds_list_del_rcu(&stream->stream_node);
247 pthread_mutex_unlock(&stream->trace->stream_list_lock);
248 stream->published = false;
249 }
250 }
251
252 static void stream_destroy(struct relay_stream *stream)
253 {
254 if (stream->indexes_ht) {
255 lttng_ht_destroy(stream->indexes_ht);
256 }
257 if (stream->tfa) {
258 tracefile_array_destroy(stream->tfa);
259 }
260 free(stream->path_name);
261 free(stream->channel_name);
262 free(stream);
263 }
264
265 static void stream_destroy_rcu(struct rcu_head *rcu_head)
266 {
267 struct relay_stream *stream =
268 caa_container_of(rcu_head, struct relay_stream, rcu_node);
269
270 stream_destroy(stream);
271 }
272
273 /*
274 * No need to take stream->lock since this is only called on the final
275 * stream_put which ensures that a single thread may act on the stream.
276 *
277 * At that point, the object is also protected by the reflock which
278 * guarantees that no other thread may share ownership of this stream.
279 */
280 static void stream_release(struct urcu_ref *ref)
281 {
282 struct relay_stream *stream =
283 caa_container_of(ref, struct relay_stream, ref);
284 struct relay_session *session;
285
286 session = stream->trace->session;
287
288 DBG("Releasing stream id %" PRIu64, stream->stream_handle);
289
290 pthread_mutex_lock(&session->recv_list_lock);
291 session->stream_count--;
292 if (stream->in_recv_list) {
293 cds_list_del_rcu(&stream->recv_node);
294 stream->in_recv_list = false;
295 }
296 pthread_mutex_unlock(&session->recv_list_lock);
297
298 stream_unpublish(stream);
299
300 if (stream->stream_fd) {
301 stream_fd_put(stream->stream_fd);
302 stream->stream_fd = NULL;
303 }
304 if (stream->index_fd) {
305 stream_fd_put(stream->index_fd);
306 stream->index_fd = NULL;
307 }
308 if (stream->trace) {
309 ctf_trace_put(stream->trace);
310 stream->trace = NULL;
311 }
312
313 call_rcu(&stream->rcu_node, stream_destroy_rcu);
314 }
315
316 void stream_put(struct relay_stream *stream)
317 {
318 DBG("stream put for stream id %" PRIu64, stream->stream_handle);
319 /*
320 * Ensure existence of stream->reflock for stream unlock.
321 */
322 rcu_read_lock();
323 /*
324 * Stream reflock ensures that concurrent test and update of
325 * stream ref is atomic.
326 */
327 pthread_mutex_lock(&stream->reflock);
328 assert(stream->ref.refcount != 0);
329 /*
330 * Wait until we have processed all the stream packets before
331 * actually putting our last stream reference.
332 */
333 DBG("stream put stream id %" PRIu64 " refcount %d",
334 stream->stream_handle,
335 (int) stream->ref.refcount);
336 urcu_ref_put(&stream->ref, stream_release);
337 pthread_mutex_unlock(&stream->reflock);
338 rcu_read_unlock();
339 }
340
341 void stream_close(struct relay_stream *stream)
342 {
343 DBG("closing stream %" PRIu64, stream->stream_handle);
344 pthread_mutex_lock(&stream->lock);
345 stream_unpublish(stream);
346 stream->closed = true;
347 relay_index_close_all(stream);
348 pthread_mutex_unlock(&stream->lock);
349 stream_put(stream);
350 }
351
352 void print_relay_streams(void)
353 {
354 struct lttng_ht_iter iter;
355 struct relay_stream *stream;
356
357 rcu_read_lock();
358 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
359 node.node) {
360 if (!stream_get(stream)) {
361 continue;
362 }
363 DBG("stream %p refcount %ld stream %" PRIu64 " trace %" PRIu64
364 " session %" PRIu64,
365 stream,
366 stream->ref.refcount,
367 stream->stream_handle,
368 stream->trace->id,
369 stream->trace->session->id);
370 stream_put(stream);
371 }
372 rcu_read_unlock();
373 }
This page took 0.036963 seconds and 5 git commands to generate.