2 * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
11 #include "lttng-relayd.hpp"
12 #include "viewer-stream.hpp"
14 #include <common/common.hpp>
15 #include <common/compat/string.hpp>
16 #include <common/index/index.hpp>
17 #include <common/urcu.hpp>
18 #include <common/utils.hpp>
23 #include <sys/types.h>
25 static void viewer_stream_release_composite_objects(struct relay_viewer_stream
*vstream
)
27 if (vstream
->stream_file
.handle
) {
28 fs_handle_close(vstream
->stream_file
.handle
);
29 vstream
->stream_file
.handle
= nullptr;
31 if (vstream
->index_file
) {
32 lttng_index_file_put(vstream
->index_file
);
33 vstream
->index_file
= nullptr;
35 if (vstream
->stream
) {
36 stream_put(vstream
->stream
);
37 vstream
->stream
= nullptr;
39 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
40 vstream
->stream_file
.trace_chunk
= nullptr;
43 static void viewer_stream_destroy(struct relay_viewer_stream
*vstream
)
45 free(vstream
->path_name
);
46 free(vstream
->channel_name
);
50 static void viewer_stream_destroy_rcu(struct rcu_head
*head
)
52 struct relay_viewer_stream
*vstream
=
53 lttng::utils::container_of(head
, &relay_viewer_stream::rcu_node
);
55 viewer_stream_destroy(vstream
);
58 /* Relay stream's lock must be held by the caller. */
59 struct relay_viewer_stream
*viewer_stream_create(struct relay_stream
*stream
,
60 struct lttng_trace_chunk
*trace_chunk
,
61 enum lttng_viewer_seek seek_t
)
63 struct relay_viewer_stream
*vstream
= nullptr;
65 ASSERT_LOCKED(stream
->lock
);
67 vstream
= zmalloc
<relay_viewer_stream
>();
69 PERROR("relay viewer stream zmalloc");
74 const bool acquired_reference
= lttng_trace_chunk_get(trace_chunk
);
76 LTTNG_ASSERT(acquired_reference
);
79 vstream
->stream_file
.trace_chunk
= trace_chunk
;
80 vstream
->path_name
= lttng_strndup(stream
->path_name
, LTTNG_VIEWER_PATH_MAX
);
81 if (vstream
->path_name
== nullptr) {
82 PERROR("relay viewer path_name alloc");
85 vstream
->channel_name
= lttng_strndup(stream
->channel_name
, LTTNG_VIEWER_NAME_MAX
);
86 if (vstream
->channel_name
== nullptr) {
87 PERROR("relay viewer channel_name alloc");
91 if (!stream_get(stream
)) {
92 ERR("Cannot get stream");
95 vstream
->stream
= stream
;
97 if (stream
->is_metadata
&& stream
->trace
->viewer_metadata_stream
) {
98 ERR("Cannot attach viewer metadata stream to trace (busy).");
103 case LTTNG_VIEWER_SEEK_BEGINNING
:
105 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
107 if (seq_tail
== -1ULL) {
109 * Tail may not be initialized yet. Nonetheless, we know
110 * we want to send the first index once it becomes
115 vstream
->current_tracefile_id
= tracefile_array_get_file_index_tail(stream
->tfa
);
116 vstream
->index_sent_seqcount
= seq_tail
;
119 case LTTNG_VIEWER_SEEK_LAST
:
120 vstream
->current_tracefile_id
=
121 tracefile_array_get_read_file_index_head(stream
->tfa
);
123 * We seek at the very end of each stream, awaiting for
124 * a future packet to eventually come in.
126 * We don't need to check the head position for -1ULL since the
127 * increment will set it to 0.
129 vstream
->index_sent_seqcount
= tracefile_array_get_seq_head(stream
->tfa
) + 1;
136 * If we never received an index for the current stream, delay
137 * the opening of the index, otherwise open it right now.
139 if (stream
->index_file
== nullptr) {
140 vstream
->index_file
= nullptr;
141 } else if (vstream
->stream_file
.trace_chunk
) {
142 const uint32_t connection_major
= stream
->trace
->session
->major
;
143 const uint32_t connection_minor
= stream
->trace
->session
->minor
;
144 enum lttng_trace_chunk_status chunk_status
;
146 chunk_status
= lttng_index_file_create_from_trace_chunk_read_only(
147 vstream
->stream_file
.trace_chunk
,
149 stream
->channel_name
,
150 stream
->tracefile_size
,
151 vstream
->current_tracefile_id
,
152 lttng_to_index_major(connection_major
, connection_minor
),
153 lttng_to_index_minor(connection_major
, connection_minor
),
155 &vstream
->index_file
);
156 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
157 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
158 vstream
->index_file
= nullptr;
166 * If we never received a data file for the current stream, delay the
167 * opening, otherwise open it right now.
169 if (stream
->file
&& vstream
->stream_file
.trace_chunk
) {
171 char file_path
[LTTNG_PATH_MAX
];
172 enum lttng_trace_chunk_status status
;
174 ret
= utils_stream_file_path(stream
->path_name
,
175 stream
->channel_name
,
176 stream
->tracefile_size
,
177 vstream
->current_tracefile_id
,
185 status
= lttng_trace_chunk_open_fs_handle(vstream
->stream_file
.trace_chunk
,
189 &vstream
->stream_file
.handle
,
191 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
196 if (seek_t
== LTTNG_VIEWER_SEEK_LAST
&& vstream
->index_file
) {
199 lseek_ret
= fs_handle_seek(vstream
->index_file
->file
, 0, SEEK_END
);
204 if (stream
->is_metadata
) {
205 rcu_assign_pointer(stream
->trace
->viewer_metadata_stream
, vstream
);
208 vstream
->last_seen_rotation_count
= stream
->completed_rotation_count
;
210 /* Globally visible after the add unique. */
211 lttng_ht_node_init_u64(&vstream
->stream_n
, stream
->stream_handle
);
212 urcu_ref_init(&vstream
->ref
);
213 lttng_ht_add_unique_u64(viewer_streams_ht
, &vstream
->stream_n
);
219 /* Not using `put` since vstream is assumed to be published. */
220 viewer_stream_release_composite_objects(vstream
);
221 viewer_stream_destroy(vstream
);
226 static void viewer_stream_unpublish(struct relay_viewer_stream
*vstream
)
229 struct lttng_ht_iter iter
;
231 iter
.iter
.node
= &vstream
->stream_n
.node
;
232 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
236 static void viewer_stream_release(struct urcu_ref
*ref
)
238 struct relay_viewer_stream
*vstream
=
239 caa_container_of(ref
, struct relay_viewer_stream
, ref
);
241 if (vstream
->stream
->is_metadata
) {
242 rcu_assign_pointer(vstream
->stream
->trace
->viewer_metadata_stream
, NULL
);
244 viewer_stream_unpublish(vstream
);
245 viewer_stream_release_composite_objects(vstream
);
246 call_rcu(&vstream
->rcu_node
, viewer_stream_destroy_rcu
);
249 /* Must be called with RCU read-side lock held. */
250 bool viewer_stream_get(struct relay_viewer_stream
*vstream
)
252 return urcu_ref_get_unless_zero(&vstream
->ref
);
256 * Get viewer stream by id.
258 * Return viewer stream if found else NULL.
260 struct relay_viewer_stream
*viewer_stream_get_by_id(uint64_t id
)
262 struct lttng_ht_node_u64
*node
;
263 struct lttng_ht_iter iter
;
264 struct relay_viewer_stream
*vstream
= nullptr;
266 lttng::urcu::read_lock_guard read_lock
;
267 lttng_ht_lookup(viewer_streams_ht
, &id
, &iter
);
268 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
270 DBG("Relay viewer stream %" PRIu64
" not found", id
);
273 vstream
= lttng::utils::container_of(node
, &relay_viewer_stream::stream_n
);
274 if (!viewer_stream_get(vstream
)) {
281 void viewer_stream_put(struct relay_viewer_stream
*vstream
)
283 lttng::urcu::read_lock_guard read_lock
;
284 urcu_ref_put(&vstream
->ref
, viewer_stream_release
);
287 void viewer_stream_close_files(struct relay_viewer_stream
*vstream
)
289 if (vstream
->index_file
) {
290 lttng_index_file_put(vstream
->index_file
);
291 vstream
->index_file
= nullptr;
293 if (vstream
->stream_file
.handle
) {
294 fs_handle_close(vstream
->stream_file
.handle
);
295 vstream
->stream_file
.handle
= nullptr;
299 void viewer_stream_sync_tracefile_array_tail(struct relay_viewer_stream
*vstream
)
301 const struct relay_stream
*stream
= vstream
->stream
;
304 vstream
->current_tracefile_id
= tracefile_array_get_file_index_tail(stream
->tfa
);
305 seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
306 if (seq_tail
== -1ULL) {
311 * Move the index sent seqcount forward if it was lagging behind
312 * the new tail of the tracefile array. If the current
313 * index_sent_seqcount is already further than the tracefile
314 * array tail position, keep its current position.
316 vstream
->index_sent_seqcount
= std::max(seq_tail
, vstream
->index_sent_seqcount
);
320 * Rotate a stream to the next tracefile.
322 * Must be called with the rstream lock held.
323 * Returns 0 on success, 1 on EOF.
325 int viewer_stream_rotate(struct relay_viewer_stream
*vstream
)
329 const struct relay_stream
*stream
= vstream
->stream
;
331 /* Detect the last tracefile to open. */
332 if (stream
->index_received_seqcount
== vstream
->index_sent_seqcount
&&
333 stream
->trace
->session
->connection_closed
) {
338 if (stream
->tracefile_count
== 0) {
339 /* Ignore rotation, there is none to do. */
345 * Try to move to the next file.
347 new_id
= (vstream
->current_tracefile_id
+ 1) % stream
->tracefile_count
;
348 if (tracefile_array_seq_in_file(stream
->tfa
, new_id
, vstream
->index_sent_seqcount
)) {
349 vstream
->current_tracefile_id
= new_id
;
351 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
354 * This can only be reached on overwrite, which implies there
355 * has been data written at some point, which will have set the
358 LTTNG_ASSERT(seq_tail
!= -1ULL);
360 * We need to resync because we lag behind tail.
362 vstream
->current_tracefile_id
= tracefile_array_get_file_index_tail(stream
->tfa
);
363 vstream
->index_sent_seqcount
= seq_tail
;
365 viewer_stream_close_files(vstream
);
371 void print_viewer_streams()
373 struct lttng_ht_iter iter
;
374 struct relay_viewer_stream
*vstream
;
376 if (!viewer_streams_ht
) {
381 lttng::urcu::read_lock_guard read_lock
;
383 cds_lfht_for_each_entry (
384 viewer_streams_ht
->ht
, &iter
.iter
, vstream
, stream_n
.node
) {
385 if (!viewer_stream_get(vstream
)) {
388 DBG("vstream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
391 vstream
->ref
.refcount
,
392 vstream
->stream
->stream_handle
,
393 vstream
->stream
->trace
->id
,
394 vstream
->stream
->trace
->session
->id
);
395 viewer_stream_put(vstream
);