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