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/utils.hpp>
22 #include <sys/types.h>
24 static void viewer_stream_release_composite_objects(struct relay_viewer_stream
*vstream
)
26 if (vstream
->stream_file
.handle
) {
27 fs_handle_close(vstream
->stream_file
.handle
);
28 vstream
->stream_file
.handle
= nullptr;
30 if (vstream
->index_file
) {
31 lttng_index_file_put(vstream
->index_file
);
32 vstream
->index_file
= nullptr;
34 if (vstream
->stream
) {
35 stream_put(vstream
->stream
);
36 vstream
->stream
= nullptr;
38 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
39 vstream
->stream_file
.trace_chunk
= nullptr;
42 static void viewer_stream_destroy(struct relay_viewer_stream
*vstream
)
44 free(vstream
->path_name
);
45 free(vstream
->channel_name
);
49 static void viewer_stream_destroy_rcu(struct rcu_head
*head
)
51 struct relay_viewer_stream
*vstream
=
52 lttng::utils::container_of(head
, &relay_viewer_stream::rcu_node
);
54 viewer_stream_destroy(vstream
);
57 /* Relay stream's lock must be held by the caller. */
58 struct relay_viewer_stream
*viewer_stream_create(struct relay_stream
*stream
,
59 struct lttng_trace_chunk
*trace_chunk
,
60 enum lttng_viewer_seek seek_t
)
62 struct relay_viewer_stream
*vstream
= nullptr;
64 ASSERT_LOCKED(stream
->lock
);
66 vstream
= zmalloc
<relay_viewer_stream
>();
68 PERROR("relay viewer stream zmalloc");
73 const bool acquired_reference
= lttng_trace_chunk_get(trace_chunk
);
75 LTTNG_ASSERT(acquired_reference
);
78 vstream
->stream_file
.trace_chunk
= trace_chunk
;
79 vstream
->path_name
= lttng_strndup(stream
->path_name
, LTTNG_VIEWER_PATH_MAX
);
80 if (vstream
->path_name
== nullptr) {
81 PERROR("relay viewer path_name alloc");
84 vstream
->channel_name
= lttng_strndup(stream
->channel_name
, LTTNG_VIEWER_NAME_MAX
);
85 if (vstream
->channel_name
== nullptr) {
86 PERROR("relay viewer channel_name alloc");
90 if (!stream_get(stream
)) {
91 ERR("Cannot get stream");
94 vstream
->stream
= stream
;
96 if (stream
->is_metadata
&& stream
->trace
->viewer_metadata_stream
) {
97 ERR("Cannot attach viewer metadata stream to trace (busy).");
102 case LTTNG_VIEWER_SEEK_BEGINNING
:
104 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
106 if (seq_tail
== -1ULL) {
108 * Tail may not be initialized yet. Nonetheless, we know
109 * we want to send the first index once it becomes
114 vstream
->current_tracefile_id
= tracefile_array_get_file_index_tail(stream
->tfa
);
115 vstream
->index_sent_seqcount
= seq_tail
;
118 case LTTNG_VIEWER_SEEK_LAST
:
119 vstream
->current_tracefile_id
=
120 tracefile_array_get_read_file_index_head(stream
->tfa
);
122 * We seek at the very end of each stream, awaiting for
123 * a future packet to eventually come in.
125 * We don't need to check the head position for -1ULL since the
126 * increment will set it to 0.
128 vstream
->index_sent_seqcount
= tracefile_array_get_seq_head(stream
->tfa
) + 1;
135 * If we never received an index for the current stream, delay
136 * the opening of the index, otherwise open it right now.
138 if (stream
->index_file
== nullptr) {
139 vstream
->index_file
= nullptr;
140 } else if (vstream
->stream_file
.trace_chunk
) {
141 const uint32_t connection_major
= stream
->trace
->session
->major
;
142 const uint32_t connection_minor
= stream
->trace
->session
->minor
;
143 enum lttng_trace_chunk_status chunk_status
;
145 chunk_status
= lttng_index_file_create_from_trace_chunk_read_only(
146 vstream
->stream_file
.trace_chunk
,
148 stream
->channel_name
,
149 stream
->tracefile_size
,
150 vstream
->current_tracefile_id
,
151 lttng_to_index_major(connection_major
, connection_minor
),
152 lttng_to_index_minor(connection_major
, connection_minor
),
154 &vstream
->index_file
);
155 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
156 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
157 vstream
->index_file
= nullptr;
165 * If we never received a data file for the current stream, delay the
166 * opening, otherwise open it right now.
168 if (stream
->file
&& vstream
->stream_file
.trace_chunk
) {
170 char file_path
[LTTNG_PATH_MAX
];
171 enum lttng_trace_chunk_status status
;
173 ret
= utils_stream_file_path(stream
->path_name
,
174 stream
->channel_name
,
175 stream
->tracefile_size
,
176 vstream
->current_tracefile_id
,
184 status
= lttng_trace_chunk_open_fs_handle(vstream
->stream_file
.trace_chunk
,
188 &vstream
->stream_file
.handle
,
190 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
195 if (seek_t
== LTTNG_VIEWER_SEEK_LAST
&& vstream
->index_file
) {
198 lseek_ret
= fs_handle_seek(vstream
->index_file
->file
, 0, SEEK_END
);
203 if (stream
->is_metadata
) {
204 rcu_assign_pointer(stream
->trace
->viewer_metadata_stream
, vstream
);
207 vstream
->last_seen_rotation_count
= stream
->completed_rotation_count
;
209 /* Globally visible after the add unique. */
210 lttng_ht_node_init_u64(&vstream
->stream_n
, stream
->stream_handle
);
211 urcu_ref_init(&vstream
->ref
);
212 lttng_ht_add_unique_u64(viewer_streams_ht
, &vstream
->stream_n
);
218 /* Not using `put` since vstream is assumed to be published. */
219 viewer_stream_release_composite_objects(vstream
);
220 viewer_stream_destroy(vstream
);
225 static void viewer_stream_unpublish(struct relay_viewer_stream
*vstream
)
228 struct lttng_ht_iter iter
;
230 iter
.iter
.node
= &vstream
->stream_n
.node
;
231 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
235 static void viewer_stream_release(struct urcu_ref
*ref
)
237 struct relay_viewer_stream
*vstream
=
238 caa_container_of(ref
, struct relay_viewer_stream
, ref
);
240 if (vstream
->stream
->is_metadata
) {
241 rcu_assign_pointer(vstream
->stream
->trace
->viewer_metadata_stream
, NULL
);
243 viewer_stream_unpublish(vstream
);
244 viewer_stream_release_composite_objects(vstream
);
245 call_rcu(&vstream
->rcu_node
, viewer_stream_destroy_rcu
);
248 /* Must be called with RCU read-side lock held. */
249 bool viewer_stream_get(struct relay_viewer_stream
*vstream
)
251 return urcu_ref_get_unless_zero(&vstream
->ref
);
255 * Get viewer stream by id.
257 * Return viewer stream if found else NULL.
259 struct relay_viewer_stream
*viewer_stream_get_by_id(uint64_t id
)
261 struct lttng_ht_node_u64
*node
;
262 struct lttng_ht_iter iter
;
263 struct relay_viewer_stream
*vstream
= nullptr;
266 lttng_ht_lookup(viewer_streams_ht
, &id
, &iter
);
267 node
= lttng_ht_iter_get_node_u64(&iter
);
269 DBG("Relay viewer stream %" PRIu64
" not found", id
);
272 vstream
= lttng::utils::container_of(node
, &relay_viewer_stream::stream_n
);
273 if (!viewer_stream_get(vstream
)) {
281 void viewer_stream_put(struct relay_viewer_stream
*vstream
)
284 urcu_ref_put(&vstream
->ref
, viewer_stream_release
);
288 void viewer_stream_close_files(struct relay_viewer_stream
*vstream
)
290 if (vstream
->index_file
) {
291 lttng_index_file_put(vstream
->index_file
);
292 vstream
->index_file
= nullptr;
294 if (vstream
->stream_file
.handle
) {
295 fs_handle_close(vstream
->stream_file
.handle
);
296 vstream
->stream_file
.handle
= nullptr;
300 void viewer_stream_sync_tracefile_array_tail(struct relay_viewer_stream
*vstream
)
302 const struct relay_stream
*stream
= vstream
->stream
;
305 vstream
->current_tracefile_id
= tracefile_array_get_file_index_tail(stream
->tfa
);
306 seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
307 if (seq_tail
== -1ULL) {
312 * Move the index sent seqcount forward if it was lagging behind
313 * the new tail of the tracefile array. If the current
314 * index_sent_seqcount is already further than the tracefile
315 * array tail position, keep its current position.
317 vstream
->index_sent_seqcount
= std::max(seq_tail
, vstream
->index_sent_seqcount
);
321 * Rotate a stream to the next tracefile.
323 * Must be called with the rstream lock held.
324 * Returns 0 on success, 1 on EOF.
326 int viewer_stream_rotate(struct relay_viewer_stream
*vstream
)
330 const struct relay_stream
*stream
= vstream
->stream
;
332 /* Detect the last tracefile to open. */
333 if (stream
->index_received_seqcount
== vstream
->index_sent_seqcount
&&
334 stream
->trace
->session
->connection_closed
) {
339 if (stream
->tracefile_count
== 0) {
340 /* Ignore rotation, there is none to do. */
346 * Try to move to the next file.
348 new_id
= (vstream
->current_tracefile_id
+ 1) % stream
->tracefile_count
;
349 if (tracefile_array_seq_in_file(stream
->tfa
, new_id
, vstream
->index_sent_seqcount
)) {
350 vstream
->current_tracefile_id
= new_id
;
352 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
355 * This can only be reached on overwrite, which implies there
356 * has been data written at some point, which will have set the
359 LTTNG_ASSERT(seq_tail
!= -1ULL);
361 * We need to resync because we lag behind tail.
363 vstream
->current_tracefile_id
= tracefile_array_get_file_index_tail(stream
->tfa
);
364 vstream
->index_sent_seqcount
= seq_tail
;
366 viewer_stream_close_files(vstream
);
372 void print_viewer_streams()
374 struct lttng_ht_iter iter
;
375 struct relay_viewer_stream
*vstream
;
377 if (!viewer_streams_ht
) {
382 cds_lfht_for_each_entry (viewer_streams_ht
->ht
, &iter
.iter
, vstream
, stream_n
.node
) {
383 if (!viewer_stream_get(vstream
)) {
386 DBG("vstream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
" session %" PRIu64
,
388 vstream
->ref
.refcount
,
389 vstream
->stream
->stream_handle
,
390 vstream
->stream
->trace
->id
,
391 vstream
->stream
->trace
->session
->id
);
392 viewer_stream_put(vstream
);