Fix: leak of consumer_output when using an explicit snapshot output
[lttng-tools.git] / src / bin / lttng-relayd / stream.c
CommitLineData
2a174661
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>
2a174661
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
6c1c0768 20#define _LGPL_SOURCE
2a174661 21#include <common/common.h>
7591bab1
MD
22#include <common/utils.h>
23#include <common/defaults.h>
24#include <urcu/rculist.h>
25#include <sys/stat.h>
2a174661 26
7591bab1 27#include "lttng-relayd.h"
2a174661
DG
28#include "index.h"
29#include "stream.h"
30#include "viewer-stream.h"
31
7591bab1
MD
32/* Should be called with RCU read-side lock held. */
33bool stream_get(struct relay_stream *stream)
34{
ce4d4083 35 return urcu_ref_get_unless_zero(&stream->ref);
7591bab1
MD
36}
37
2a174661 38/*
7591bab1
MD
39 * Get stream from stream id from the streams hash table. Return stream
40 * if found else NULL. A stream reference is taken when a stream is
41 * returned. stream_put() must be called on that stream.
2a174661 42 */
7591bab1 43struct relay_stream *stream_get_by_id(uint64_t stream_id)
2a174661
DG
44{
45 struct lttng_ht_node_u64 *node;
46 struct lttng_ht_iter iter;
47 struct relay_stream *stream = NULL;
48
7591bab1
MD
49 rcu_read_lock();
50 lttng_ht_lookup(relay_streams_ht, &stream_id, &iter);
2a174661 51 node = lttng_ht_iter_get_node_u64(&iter);
7591bab1 52 if (!node) {
2a174661
DG
53 DBG("Relay stream %" PRIu64 " not found", stream_id);
54 goto end;
55 }
56 stream = caa_container_of(node, struct relay_stream, node);
7591bab1
MD
57 if (!stream_get(stream)) {
58 stream = NULL;
59 }
2a174661 60end:
7591bab1 61 rcu_read_unlock();
2a174661
DG
62 return stream;
63}
64
65/*
7591bab1 66 * We keep ownership of path_name and channel_name.
2a174661 67 */
7591bab1
MD
68struct relay_stream *stream_create(struct ctf_trace *trace,
69 uint64_t stream_handle, char *path_name,
70 char *channel_name, uint64_t tracefile_size,
81164b6b
JG
71 uint64_t tracefile_count,
72 const struct relay_stream_chunk_id *chunk_id)
2a174661 73{
7591bab1
MD
74 int ret;
75 struct relay_stream *stream = NULL;
76 struct relay_session *session = trace->session;
2a174661 77
7591bab1
MD
78 stream = zmalloc(sizeof(struct relay_stream));
79 if (stream == NULL) {
80 PERROR("relay stream zmalloc");
7591bab1
MD
81 goto error_no_alloc;
82 }
2a174661 83
7591bab1 84 stream->stream_handle = stream_handle;
a8f9f353 85 stream->prev_data_seq = -1ULL;
7a45c7e6 86 stream->prev_index_seq = -1ULL;
bda7c7b9 87 stream->last_net_seq_num = -1ULL;
7591bab1
MD
88 stream->ctf_stream_id = -1ULL;
89 stream->tracefile_size = tracefile_size;
90 stream->tracefile_count = tracefile_count;
91 stream->path_name = path_name;
cb523e02 92 stream->prev_path_name = NULL;
7591bab1 93 stream->channel_name = channel_name;
d3ecc550 94 stream->rotate_at_seq_num = -1ULL;
2f9c3030 95 stream->beacon_ts_end = -1ULL;
7591bab1
MD
96 lttng_ht_node_init_u64(&stream->node, stream->stream_handle);
97 pthread_mutex_init(&stream->lock, NULL);
7591bab1
MD
98 urcu_ref_init(&stream->ref);
99 ctf_trace_get(trace);
100 stream->trace = trace;
81164b6b 101 stream->current_chunk_id = *chunk_id;
2a174661 102
7591bab1
MD
103 stream->indexes_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
104 if (!stream->indexes_ht) {
105 ERR("Cannot created indexes_ht");
106 ret = -1;
107 goto end;
2a174661
DG
108 }
109
7591bab1
MD
110 ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG,
111 -1, -1);
112 if (ret < 0) {
113 ERR("relay creating output directory");
114 goto end;
115 }
2a174661 116
7591bab1
MD
117 /*
118 * No need to use run_as API here because whatever we receive,
119 * the relayd uses its own credentials for the stream files.
120 */
121 ret = utils_create_stream_file(stream->path_name, stream->channel_name,
122 stream->tracefile_size, 0, -1, -1, NULL);
123 if (ret < 0) {
124 ERR("Create output file");
125 goto end;
126 }
127 stream->stream_fd = stream_fd_create(ret);
128 if (!stream->stream_fd) {
129 if (close(ret)) {
130 PERROR("Error closing file %d", ret);
2a174661 131 }
7591bab1
MD
132 ret = -1;
133 goto end;
2a174661 134 }
a44ca2ca
MD
135 stream->tfa = tracefile_array_create(stream->tracefile_count);
136 if (!stream->tfa) {
137 ret = -1;
138 goto end;
139 }
7591bab1
MD
140 if (stream->tracefile_size) {
141 DBG("Tracefile %s/%s_0 created", stream->path_name, stream->channel_name);
142 } else {
143 DBG("Tracefile %s/%s created", stream->path_name, stream->channel_name);
144 }
145
36d2e35d 146 if (!strncmp(stream->channel_name, DEFAULT_METADATA_NAME, LTTNG_NAME_MAX)) {
7591bab1
MD
147 stream->is_metadata = 1;
148 }
149
150 stream->in_recv_list = true;
151
152 /*
153 * Add the stream in the recv list of the session. Once the end stream
154 * message is received, all session streams are published.
155 */
156 pthread_mutex_lock(&session->recv_list_lock);
157 cds_list_add_rcu(&stream->recv_node, &session->recv_list);
158 session->stream_count++;
159 pthread_mutex_unlock(&session->recv_list_lock);
160
161 /*
162 * Both in the ctf_trace object and the global stream ht since the data
163 * side of the relayd does not have the concept of session.
164 */
165 lttng_ht_add_unique_u64(relay_streams_ht, &stream->node);
77f7bd85 166 stream->in_stream_ht = true;
2a174661 167
7591bab1
MD
168 DBG("Relay new stream added %s with ID %" PRIu64, stream->channel_name,
169 stream->stream_handle);
170 ret = 0;
171
172end:
173 if (ret) {
174 if (stream->stream_fd) {
175 stream_fd_put(stream->stream_fd);
176 stream->stream_fd = NULL;
2a174661 177 }
7591bab1
MD
178 stream_put(stream);
179 stream = NULL;
2a174661 180 }
7591bab1 181 return stream;
2a174661 182
7591bab1
MD
183error_no_alloc:
184 /*
185 * path_name and channel_name need to be freed explicitly here
186 * because we cannot rely on stream_put().
187 */
188 free(path_name);
189 free(channel_name);
190 return NULL;
191}
192
193/*
194 * Called with the session lock held.
195 */
196void stream_publish(struct relay_stream *stream)
197{
198 struct relay_session *session;
199
200 pthread_mutex_lock(&stream->lock);
201 if (stream->published) {
202 goto unlock;
2a174661
DG
203 }
204
7591bab1 205 session = stream->trace->session;
2a174661 206
7591bab1
MD
207 pthread_mutex_lock(&session->recv_list_lock);
208 if (stream->in_recv_list) {
209 cds_list_del_rcu(&stream->recv_node);
210 stream->in_recv_list = false;
211 }
212 pthread_mutex_unlock(&session->recv_list_lock);
2a174661 213
7591bab1
MD
214 pthread_mutex_lock(&stream->trace->stream_list_lock);
215 cds_list_add_rcu(&stream->stream_node, &stream->trace->stream_list);
216 pthread_mutex_unlock(&stream->trace->stream_list_lock);
2a174661 217
7591bab1
MD
218 stream->published = true;
219unlock:
2a174661 220 pthread_mutex_unlock(&stream->lock);
2a174661
DG
221}
222
7591bab1 223/*
77f7bd85 224 * Stream must be protected by holding the stream lock or by virtue of being
ce4d4083 225 * called from stream_destroy.
7591bab1
MD
226 */
227static void stream_unpublish(struct relay_stream *stream)
2a174661 228{
77f7bd85
MD
229 if (stream->in_stream_ht) {
230 struct lttng_ht_iter iter;
231 int ret;
232
233 iter.iter.node = &stream->node.node;
234 ret = lttng_ht_del(relay_streams_ht, &iter);
235 assert(!ret);
236 stream->in_stream_ht = false;
237 }
238 if (stream->published) {
239 pthread_mutex_lock(&stream->trace->stream_list_lock);
240 cds_list_del_rcu(&stream->stream_node);
241 pthread_mutex_unlock(&stream->trace->stream_list_lock);
242 stream->published = false;
7591bab1 243 }
7591bab1
MD
244}
245
246static void stream_destroy(struct relay_stream *stream)
247{
248 if (stream->indexes_ht) {
49e614cb
MD
249 /*
250 * Calling lttng_ht_destroy in call_rcu worker thread so
251 * we don't hold the RCU read-side lock while calling
252 * it.
253 */
7591bab1
MD
254 lttng_ht_destroy(stream->indexes_ht);
255 }
a44ca2ca
MD
256 if (stream->tfa) {
257 tracefile_array_destroy(stream->tfa);
258 }
7591bab1 259 free(stream->path_name);
cb523e02 260 free(stream->prev_path_name);
7591bab1
MD
261 free(stream->channel_name);
262 free(stream);
263}
264
265static 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.
7591bab1
MD
276 */
277static void stream_release(struct urcu_ref *ref)
278{
279 struct relay_stream *stream =
280 caa_container_of(ref, struct relay_stream, ref);
281 struct relay_session *session;
2a174661 282
7591bab1
MD
283 session = stream->trace->session;
284
285 DBG("Releasing stream id %" PRIu64, stream->stream_handle);
286
287 pthread_mutex_lock(&session->recv_list_lock);
288 session->stream_count--;
289 if (stream->in_recv_list) {
290 cds_list_del_rcu(&stream->recv_node);
291 stream->in_recv_list = false;
292 }
293 pthread_mutex_unlock(&session->recv_list_lock);
2a174661 294
7591bab1
MD
295 stream_unpublish(stream);
296
297 if (stream->stream_fd) {
298 stream_fd_put(stream->stream_fd);
299 stream->stream_fd = NULL;
300 }
f8f3885c
MD
301 if (stream->index_file) {
302 lttng_index_file_put(stream->index_file);
303 stream->index_file = NULL;
7591bab1
MD
304 }
305 if (stream->trace) {
306 ctf_trace_put(stream->trace);
307 stream->trace = NULL;
308 }
309
310 call_rcu(&stream->rcu_node, stream_destroy_rcu);
2a174661
DG
311}
312
7591bab1 313void stream_put(struct relay_stream *stream)
2a174661 314{
7591bab1 315 DBG("stream put for stream id %" PRIu64, stream->stream_handle);
7591bab1 316 rcu_read_lock();
7591bab1
MD
317 assert(stream->ref.refcount != 0);
318 /*
319 * Wait until we have processed all the stream packets before
320 * actually putting our last stream reference.
321 */
322 DBG("stream put stream id %" PRIu64 " refcount %d",
323 stream->stream_handle,
324 (int) stream->ref.refcount);
325 urcu_ref_put(&stream->ref, stream_release);
7591bab1
MD
326 rcu_read_unlock();
327}
328
bda7c7b9 329void try_stream_close(struct relay_stream *stream)
7591bab1 330{
98ba050e
JR
331 bool session_aborted;
332 struct relay_session *session = stream->trace->session;
333
bda7c7b9 334 DBG("Trying to close stream %" PRIu64, stream->stream_handle);
98ba050e
JR
335
336 pthread_mutex_lock(&session->lock);
337 session_aborted = session->aborted;
338 pthread_mutex_unlock(&session->lock);
339
7591bab1 340 pthread_mutex_lock(&stream->lock);
bda7c7b9
JG
341 /*
342 * Can be called concurently by connection close and reception of last
343 * pending data.
344 */
345 if (stream->closed) {
346 pthread_mutex_unlock(&stream->lock);
347 DBG("closing stream %" PRIu64 " aborted since it is already marked as closed", stream->stream_handle);
348 return;
349 }
350
351 stream->close_requested = true;
3d07a857
MD
352
353 if (stream->last_net_seq_num == -1ULL) {
354 /*
355 * Handle connection close without explicit stream close
356 * command.
357 *
358 * We can be clever about indexes partially received in
359 * cases where we received the data socket part, but not
360 * the control socket part: since we're currently closing
361 * the stream on behalf of the control socket, we *know*
362 * there won't be any more control information for this
363 * socket. Therefore, we can destroy all indexes for
364 * which we have received only the file descriptor (from
365 * data socket). This takes care of consumerd crashes
366 * between sending the data and control information for
367 * a packet. Since those are sent in that order, we take
368 * care of consumerd crashes.
369 */
5312a3ed 370 DBG("relay_index_close_partial_fd");
3d07a857
MD
371 relay_index_close_partial_fd(stream);
372 /*
373 * Use the highest net_seq_num we currently have pending
374 * As end of stream indicator. Leave last_net_seq_num
375 * at -1ULL if we cannot find any index.
376 */
377 stream->last_net_seq_num = relay_index_find_last(stream);
5312a3ed 378 DBG("Updating stream->last_net_seq_num to %" PRIu64, stream->last_net_seq_num);
3d07a857
MD
379 /* Fall-through into the next check. */
380 }
381
bda7c7b9 382 if (stream->last_net_seq_num != -1ULL &&
a8f9f353 383 ((int64_t) (stream->prev_data_seq - stream->last_net_seq_num)) < 0
98ba050e 384 && !session_aborted) {
3d07a857
MD
385 /*
386 * Don't close since we still have data pending. This
387 * handles cases where an explicit close command has
388 * been received for this stream, and cases where the
389 * connection has been closed, and we are awaiting for
390 * index information from the data socket. It is
391 * therefore expected that all the index fd information
392 * we need has already been received on the control
393 * socket. Matching index information from data socket
394 * should be Expected Soon(TM).
395 *
396 * TODO: We should implement a timer to garbage collect
397 * streams after a timeout to be resilient against a
398 * consumerd implementation that would not match this
399 * expected behavior.
400 */
bda7c7b9
JG
401 pthread_mutex_unlock(&stream->lock);
402 DBG("closing stream %" PRIu64 " aborted since it still has data pending", stream->stream_handle);
403 return;
404 }
3d07a857
MD
405 /*
406 * We received all the indexes we can expect.
407 */
77f7bd85 408 stream_unpublish(stream);
2229a09c 409 stream->closed = true;
bda7c7b9 410 /* Relay indexes are only used by the "consumer/sessiond" end. */
7591bab1
MD
411 relay_index_close_all(stream);
412 pthread_mutex_unlock(&stream->lock);
bda7c7b9 413 DBG("Succeeded in closing stream %" PRIu64, stream->stream_handle);
7591bab1
MD
414 stream_put(stream);
415}
416
da412cde
MD
417static void print_stream_indexes(struct relay_stream *stream)
418{
419 struct lttng_ht_iter iter;
420 struct relay_index *index;
421
422 rcu_read_lock();
423 cds_lfht_for_each_entry(stream->indexes_ht->ht, &iter.iter, index,
424 index_n.node) {
425 DBG("index %p net_seq_num %" PRIu64 " refcount %ld"
426 " stream %" PRIu64 " trace %" PRIu64
427 " session %" PRIu64,
428 index,
429 index->index_n.key,
430 stream->ref.refcount,
431 index->stream->stream_handle,
432 index->stream->trace->id,
433 index->stream->trace->session->id);
434 }
435 rcu_read_unlock();
436}
437
7591bab1
MD
438void print_relay_streams(void)
439{
440 struct lttng_ht_iter iter;
441 struct relay_stream *stream;
442
ce3f3ba3
JG
443 if (!relay_streams_ht) {
444 return;
445 }
446
7591bab1
MD
447 rcu_read_lock();
448 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
449 node.node) {
450 if (!stream_get(stream)) {
451 continue;
452 }
453 DBG("stream %p refcount %ld stream %" PRIu64 " trace %" PRIu64
454 " session %" PRIu64,
455 stream,
456 stream->ref.refcount,
457 stream->stream_handle,
458 stream->trace->id,
459 stream->trace->session->id);
da412cde 460 print_stream_indexes(stream);
7591bab1
MD
461 stream_put(stream);
462 }
463 rcu_read_unlock();
2a174661 464}
This page took 0.099757 seconds and 4 git commands to generate.