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