2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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
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.
21 #include <common/common.h>
22 #include <common/utils.h>
23 #include <common/defaults.h>
24 #include <urcu/rculist.h>
27 #include "lttng-relayd.h"
30 #include "viewer-stream.h"
32 #include <sys/types.h>
35 /* Should be called with RCU read-side lock held. */
36 bool stream_get(struct relay_stream
*stream
)
38 return urcu_ref_get_unless_zero(&stream
->ref
);
42 * Get stream from stream id from the streams hash table. Return stream
43 * if found else NULL. A stream reference is taken when a stream is
44 * returned. stream_put() must be called on that stream.
46 struct relay_stream
*stream_get_by_id(uint64_t stream_id
)
48 struct lttng_ht_node_u64
*node
;
49 struct lttng_ht_iter iter
;
50 struct relay_stream
*stream
= NULL
;
53 lttng_ht_lookup(relay_streams_ht
, &stream_id
, &iter
);
54 node
= lttng_ht_iter_get_node_u64(&iter
);
56 DBG("Relay stream %" PRIu64
" not found", stream_id
);
59 stream
= caa_container_of(node
, struct relay_stream
, node
);
60 if (!stream_get(stream
)) {
68 static int stream_create_data_output_file(struct relay_stream
*stream
)
71 enum lttng_trace_chunk_status status
;
72 const int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
73 const mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
74 char stream_path
[LTTNG_PATH_MAX
];
76 ASSERT_LOCKED(stream
->lock
);
77 assert(stream
->trace_chunk
);
79 if (stream
->stream_fd
) {
80 stream_fd_put(stream
->stream_fd
);
81 stream
->stream_fd
= NULL
;
84 ret
= utils_stream_file_path(stream
->path_name
, stream
->channel_name
,
85 stream
->tracefile_size
, stream
->tracefile_count
, NULL
,
86 stream_path
, sizeof(stream_path
));
91 DBG("Opening stream output file \"%s\"", stream_path
);
92 status
= lttng_trace_chunk_open_file(
93 stream
->trace_chunk
, stream_path
, flags
, mode
, &fd
);
94 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
95 ERR("Failed to open stream file \"%s\"", stream
->channel_name
);
100 stream
->stream_fd
= stream_fd_create(fd
);
101 if (!stream
->stream_fd
) {
103 PERROR("Error closing stream file descriptor %d", ret
);
112 static int stream_set_trace_chunk(struct relay_stream
*stream
,
113 struct lttng_trace_chunk
*chunk
)
116 enum lttng_trace_chunk_status status
;
117 bool acquired_reference
;
119 pthread_mutex_lock(&stream
->lock
);
120 status
= lttng_trace_chunk_create_subdirectory(chunk
,
122 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
127 lttng_trace_chunk_put(stream
->trace_chunk
);
128 acquired_reference
= lttng_trace_chunk_get(chunk
);
129 assert(acquired_reference
);
130 stream
->trace_chunk
= chunk
;
131 ret
= stream_create_data_output_file(stream
);
133 pthread_mutex_unlock(&stream
->lock
);
138 * We keep ownership of path_name and channel_name.
140 struct relay_stream
*stream_create(struct ctf_trace
*trace
,
141 uint64_t stream_handle
, char *path_name
,
142 char *channel_name
, uint64_t tracefile_size
,
143 uint64_t tracefile_count
)
146 struct relay_stream
*stream
= NULL
;
147 struct relay_session
*session
= trace
->session
;
148 bool acquired_reference
= false;
149 struct lttng_trace_chunk
*current_trace_chunk
;
151 stream
= zmalloc(sizeof(struct relay_stream
));
152 if (stream
== NULL
) {
153 PERROR("relay stream zmalloc");
157 stream
->stream_handle
= stream_handle
;
158 stream
->prev_data_seq
= -1ULL;
159 stream
->prev_index_seq
= -1ULL;
160 stream
->last_net_seq_num
= -1ULL;
161 stream
->ctf_stream_id
= -1ULL;
162 stream
->tracefile_size
= tracefile_size
;
163 stream
->tracefile_count
= tracefile_count
;
164 stream
->path_name
= path_name
;
165 stream
->prev_path_name
= NULL
;
166 stream
->channel_name
= channel_name
;
167 stream
->rotate_at_seq_num
= -1ULL;
168 stream
->beacon_ts_end
= -1ULL;
169 lttng_ht_node_init_u64(&stream
->node
, stream
->stream_handle
);
170 pthread_mutex_init(&stream
->lock
, NULL
);
171 urcu_ref_init(&stream
->ref
);
172 ctf_trace_get(trace
);
173 stream
->trace
= trace
;
175 pthread_mutex_lock(&trace
->session
->lock
);
176 current_trace_chunk
= trace
->session
->current_trace_chunk
;
177 if (current_trace_chunk
) {
178 acquired_reference
= lttng_trace_chunk_get(current_trace_chunk
);
180 pthread_mutex_unlock(&trace
->session
->lock
);
181 if (!acquired_reference
) {
182 ERR("Cannot create stream for channel \"%s\" as a reference to the session's current trace chunk could not be acquired",
188 stream
->indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
189 if (!stream
->indexes_ht
) {
190 ERR("Cannot created indexes_ht");
195 ret
= stream_set_trace_chunk(stream
, current_trace_chunk
);
197 ERR("Failed to set the current trace chunk of session \"%s\" on newly created stream of channel \"%s\"",
198 trace
->session
->session_name
,
199 stream
->channel_name
);
203 stream
->tfa
= tracefile_array_create(stream
->tracefile_count
);
209 stream
->is_metadata
= !strcmp(stream
->channel_name
,
210 DEFAULT_METADATA_NAME
);
211 stream
->in_recv_list
= true;
214 * Add the stream in the recv list of the session. Once the end stream
215 * message is received, all session streams are published.
217 pthread_mutex_lock(&session
->recv_list_lock
);
218 cds_list_add_rcu(&stream
->recv_node
, &session
->recv_list
);
219 session
->stream_count
++;
220 pthread_mutex_unlock(&session
->recv_list_lock
);
223 * Both in the ctf_trace object and the global stream ht since the data
224 * side of the relayd does not have the concept of session.
226 lttng_ht_add_unique_u64(relay_streams_ht
, &stream
->node
);
227 stream
->in_stream_ht
= true;
229 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
230 stream
->stream_handle
);
235 if (stream
->stream_fd
) {
236 stream_fd_put(stream
->stream_fd
);
237 stream
->stream_fd
= NULL
;
242 lttng_trace_chunk_put(current_trace_chunk
);
247 * path_name and channel_name need to be freed explicitly here
248 * because we cannot rely on stream_put().
256 * Called with the session lock held.
258 void stream_publish(struct relay_stream
*stream
)
260 struct relay_session
*session
;
262 pthread_mutex_lock(&stream
->lock
);
263 if (stream
->published
) {
267 session
= stream
->trace
->session
;
269 pthread_mutex_lock(&session
->recv_list_lock
);
270 if (stream
->in_recv_list
) {
271 cds_list_del_rcu(&stream
->recv_node
);
272 stream
->in_recv_list
= false;
274 pthread_mutex_unlock(&session
->recv_list_lock
);
276 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
277 cds_list_add_rcu(&stream
->stream_node
, &stream
->trace
->stream_list
);
278 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
280 stream
->published
= true;
282 pthread_mutex_unlock(&stream
->lock
);
286 * Stream must be protected by holding the stream lock or by virtue of being
287 * called from stream_destroy.
289 static void stream_unpublish(struct relay_stream
*stream
)
291 if (stream
->in_stream_ht
) {
292 struct lttng_ht_iter iter
;
295 iter
.iter
.node
= &stream
->node
.node
;
296 ret
= lttng_ht_del(relay_streams_ht
, &iter
);
298 stream
->in_stream_ht
= false;
300 if (stream
->published
) {
301 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
302 cds_list_del_rcu(&stream
->stream_node
);
303 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
304 stream
->published
= false;
308 static void stream_destroy(struct relay_stream
*stream
)
310 if (stream
->indexes_ht
) {
312 * Calling lttng_ht_destroy in call_rcu worker thread so
313 * we don't hold the RCU read-side lock while calling
316 lttng_ht_destroy(stream
->indexes_ht
);
319 tracefile_array_destroy(stream
->tfa
);
321 free(stream
->path_name
);
322 free(stream
->prev_path_name
);
323 free(stream
->channel_name
);
327 static void stream_destroy_rcu(struct rcu_head
*rcu_head
)
329 struct relay_stream
*stream
=
330 caa_container_of(rcu_head
, struct relay_stream
, rcu_node
);
332 stream_destroy(stream
);
336 * No need to take stream->lock since this is only called on the final
337 * stream_put which ensures that a single thread may act on the stream.
339 static void stream_release(struct urcu_ref
*ref
)
341 struct relay_stream
*stream
=
342 caa_container_of(ref
, struct relay_stream
, ref
);
343 struct relay_session
*session
;
345 session
= stream
->trace
->session
;
347 DBG("Releasing stream id %" PRIu64
, stream
->stream_handle
);
349 pthread_mutex_lock(&session
->recv_list_lock
);
350 session
->stream_count
--;
351 if (stream
->in_recv_list
) {
352 cds_list_del_rcu(&stream
->recv_node
);
353 stream
->in_recv_list
= false;
355 pthread_mutex_unlock(&session
->recv_list_lock
);
357 stream_unpublish(stream
);
359 if (stream
->stream_fd
) {
360 stream_fd_put(stream
->stream_fd
);
361 stream
->stream_fd
= NULL
;
363 if (stream
->index_file
) {
364 lttng_index_file_put(stream
->index_file
);
365 stream
->index_file
= NULL
;
368 ctf_trace_put(stream
->trace
);
369 stream
->trace
= NULL
;
371 lttng_trace_chunk_put(stream
->trace_chunk
);
372 stream
->trace_chunk
= NULL
;
374 call_rcu(&stream
->rcu_node
, stream_destroy_rcu
);
377 void stream_put(struct relay_stream
*stream
)
379 DBG("stream put for stream id %" PRIu64
, stream
->stream_handle
);
381 assert(stream
->ref
.refcount
!= 0);
383 * Wait until we have processed all the stream packets before
384 * actually putting our last stream reference.
386 DBG("stream put stream id %" PRIu64
" refcount %d",
387 stream
->stream_handle
,
388 (int) stream
->ref
.refcount
);
389 urcu_ref_put(&stream
->ref
, stream_release
);
393 void try_stream_close(struct relay_stream
*stream
)
395 bool session_aborted
;
396 struct relay_session
*session
= stream
->trace
->session
;
398 DBG("Trying to close stream %" PRIu64
, stream
->stream_handle
);
400 pthread_mutex_lock(&session
->lock
);
401 session_aborted
= session
->aborted
;
402 pthread_mutex_unlock(&session
->lock
);
404 pthread_mutex_lock(&stream
->lock
);
406 * Can be called concurently by connection close and reception of last
409 if (stream
->closed
) {
410 pthread_mutex_unlock(&stream
->lock
);
411 DBG("closing stream %" PRIu64
" aborted since it is already marked as closed", stream
->stream_handle
);
415 stream
->close_requested
= true;
417 if (stream
->last_net_seq_num
== -1ULL) {
419 * Handle connection close without explicit stream close
422 * We can be clever about indexes partially received in
423 * cases where we received the data socket part, but not
424 * the control socket part: since we're currently closing
425 * the stream on behalf of the control socket, we *know*
426 * there won't be any more control information for this
427 * socket. Therefore, we can destroy all indexes for
428 * which we have received only the file descriptor (from
429 * data socket). This takes care of consumerd crashes
430 * between sending the data and control information for
431 * a packet. Since those are sent in that order, we take
432 * care of consumerd crashes.
434 DBG("relay_index_close_partial_fd");
435 relay_index_close_partial_fd(stream
);
437 * Use the highest net_seq_num we currently have pending
438 * As end of stream indicator. Leave last_net_seq_num
439 * at -1ULL if we cannot find any index.
441 stream
->last_net_seq_num
= relay_index_find_last(stream
);
442 DBG("Updating stream->last_net_seq_num to %" PRIu64
, stream
->last_net_seq_num
);
443 /* Fall-through into the next check. */
446 if (stream
->last_net_seq_num
!= -1ULL &&
447 ((int64_t) (stream
->prev_data_seq
- stream
->last_net_seq_num
)) < 0
448 && !session_aborted
) {
450 * Don't close since we still have data pending. This
451 * handles cases where an explicit close command has
452 * been received for this stream, and cases where the
453 * connection has been closed, and we are awaiting for
454 * index information from the data socket. It is
455 * therefore expected that all the index fd information
456 * we need has already been received on the control
457 * socket. Matching index information from data socket
458 * should be Expected Soon(TM).
460 * TODO: We should implement a timer to garbage collect
461 * streams after a timeout to be resilient against a
462 * consumerd implementation that would not match this
465 pthread_mutex_unlock(&stream
->lock
);
466 DBG("closing stream %" PRIu64
" aborted since it still has data pending", stream
->stream_handle
);
470 * We received all the indexes we can expect.
472 stream_unpublish(stream
);
473 stream
->closed
= true;
474 /* Relay indexes are only used by the "consumer/sessiond" end. */
475 relay_index_close_all(stream
);
476 pthread_mutex_unlock(&stream
->lock
);
477 DBG("Succeeded in closing stream %" PRIu64
, stream
->stream_handle
);
481 static void print_stream_indexes(struct relay_stream
*stream
)
483 struct lttng_ht_iter iter
;
484 struct relay_index
*index
;
487 cds_lfht_for_each_entry(stream
->indexes_ht
->ht
, &iter
.iter
, index
,
489 DBG("index %p net_seq_num %" PRIu64
" refcount %ld"
490 " stream %" PRIu64
" trace %" PRIu64
494 stream
->ref
.refcount
,
495 index
->stream
->stream_handle
,
496 index
->stream
->trace
->id
,
497 index
->stream
->trace
->session
->id
);
502 void print_relay_streams(void)
504 struct lttng_ht_iter iter
;
505 struct relay_stream
*stream
;
507 if (!relay_streams_ht
) {
512 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
514 if (!stream_get(stream
)) {
517 DBG("stream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
520 stream
->ref
.refcount
,
521 stream
->stream_handle
,
523 stream
->trace
->session
->id
);
524 print_stream_indexes(stream
);