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 <common/common.hpp>
12 #include <common/index/index.hpp>
13 #include <common/compat/string.hpp>
14 #include <common/utils.hpp>
15 #include <sys/types.h>
20 #include "lttng-relayd.hpp"
21 #include "viewer-stream.hpp"
23 static void viewer_stream_release_composite_objects(struct relay_viewer_stream
*vstream
)
25 if (vstream
->stream_file
.handle
) {
26 fs_handle_close(vstream
->stream_file
.handle
);
27 vstream
->stream_file
.handle
= NULL
;
29 if (vstream
->index_file
) {
30 lttng_index_file_put(vstream
->index_file
);
31 vstream
->index_file
= NULL
;
33 if (vstream
->stream
) {
34 stream_put(vstream
->stream
);
35 vstream
->stream
= NULL
;
37 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
38 vstream
->stream_file
.trace_chunk
= NULL
;
41 static void viewer_stream_destroy(struct relay_viewer_stream
*vstream
)
43 free(vstream
->path_name
);
44 free(vstream
->channel_name
);
48 static void viewer_stream_destroy_rcu(struct rcu_head
*head
)
50 struct relay_viewer_stream
*vstream
=
51 caa_container_of(head
, struct relay_viewer_stream
, rcu_node
);
53 viewer_stream_destroy(vstream
);
56 /* Relay stream's lock must be held by the caller. */
57 struct relay_viewer_stream
*viewer_stream_create(struct relay_stream
*stream
,
58 struct lttng_trace_chunk
*trace_chunk
,
59 enum lttng_viewer_seek seek_t
)
61 struct relay_viewer_stream
*vstream
= NULL
;
63 ASSERT_LOCKED(stream
->lock
);
65 vstream
= zmalloc
<relay_viewer_stream
>();
67 PERROR("relay viewer stream zmalloc");
72 const bool acquired_reference
= lttng_trace_chunk_get(
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
== NULL
) {
81 PERROR("relay viewer path_name alloc");
84 vstream
->channel_name
= lttng_strndup(stream
->channel_name
,
85 LTTNG_VIEWER_NAME_MAX
);
86 if (vstream
->channel_name
== NULL
) {
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
=
116 tracefile_array_get_file_index_tail(stream
->tfa
);
117 vstream
->index_sent_seqcount
= seq_tail
;
120 case LTTNG_VIEWER_SEEK_LAST
:
121 vstream
->current_tracefile_id
=
122 tracefile_array_get_read_file_index_head(stream
->tfa
);
124 * We seek at the very end of each stream, awaiting for
125 * a future packet to eventually come in.
127 * We don't need to check the head position for -1ULL since the
128 * increment will set it to 0.
130 vstream
->index_sent_seqcount
=
131 tracefile_array_get_seq_head(stream
->tfa
) + 1;
138 * If we never received an index for the current stream, delay
139 * the opening of the index, otherwise open it right now.
141 if (stream
->index_file
== NULL
) {
142 vstream
->index_file
= NULL
;
143 } else if (vstream
->stream_file
.trace_chunk
) {
144 const uint32_t connection_major
= stream
->trace
->session
->major
;
145 const uint32_t connection_minor
= stream
->trace
->session
->minor
;
146 enum lttng_trace_chunk_status chunk_status
;
148 chunk_status
= lttng_index_file_create_from_trace_chunk_read_only(
149 vstream
->stream_file
.trace_chunk
,
151 stream
->channel_name
, stream
->tracefile_size
,
152 vstream
->current_tracefile_id
,
153 lttng_to_index_major(connection_major
,
155 lttng_to_index_minor(connection_major
,
157 true, &vstream
->index_file
);
158 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
159 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
160 vstream
->index_file
= NULL
;
168 * If we never received a data file for the current stream, delay the
169 * opening, otherwise open it right now.
171 if (stream
->file
&& vstream
->stream_file
.trace_chunk
) {
173 char file_path
[LTTNG_PATH_MAX
];
174 enum lttng_trace_chunk_status status
;
176 ret
= utils_stream_file_path(stream
->path_name
,
177 stream
->channel_name
, stream
->tracefile_size
,
178 vstream
->current_tracefile_id
, NULL
, file_path
,
184 status
= lttng_trace_chunk_open_fs_handle(
185 vstream
->stream_file
.trace_chunk
, file_path
,
186 O_RDONLY
, 0, &vstream
->stream_file
.handle
,
188 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
193 if (seek_t
== LTTNG_VIEWER_SEEK_LAST
&& vstream
->index_file
) {
196 lseek_ret
= fs_handle_seek(
197 vstream
->index_file
->file
, 0, SEEK_END
);
202 if (stream
->is_metadata
) {
203 rcu_assign_pointer(stream
->trace
->viewer_metadata_stream
,
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
= caa_container_of(ref
,
238 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
= NULL
;
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
= caa_container_of(node
, struct 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
= NULL
;
294 if (vstream
->stream_file
.handle
) {
295 fs_handle_close(vstream
->stream_file
.handle
);
296 vstream
->stream_file
.handle
= NULL
;
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
=
318 std::max(seq_tail
, vstream
->index_sent_seqcount
);
322 * Rotate a stream to the next tracefile.
324 * Must be called with the rstream lock held.
325 * Returns 0 on success, 1 on EOF.
327 int viewer_stream_rotate(struct relay_viewer_stream
*vstream
)
331 const struct relay_stream
*stream
= vstream
->stream
;
333 /* Detect the last tracefile to open. */
334 if (stream
->index_received_seqcount
335 == vstream
->index_sent_seqcount
336 && stream
->trace
->session
->connection_closed
) {
341 if (stream
->tracefile_count
== 0) {
342 /* Ignore rotation, there is none to do. */
348 * Try to move to the next file.
350 new_id
= (vstream
->current_tracefile_id
+ 1)
351 % stream
->tracefile_count
;
352 if (tracefile_array_seq_in_file(stream
->tfa
, new_id
,
353 vstream
->index_sent_seqcount
)) {
354 vstream
->current_tracefile_id
= new_id
;
356 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
359 * This can only be reached on overwrite, which implies there
360 * has been data written at some point, which will have set the
363 LTTNG_ASSERT(seq_tail
!= -1ULL);
365 * We need to resync because we lag behind tail.
367 vstream
->current_tracefile_id
=
368 tracefile_array_get_file_index_tail(stream
->tfa
);
369 vstream
->index_sent_seqcount
= seq_tail
;
371 viewer_stream_close_files(vstream
);
377 void print_viewer_streams(void)
379 struct lttng_ht_iter iter
;
380 struct relay_viewer_stream
*vstream
;
382 if (!viewer_streams_ht
) {
387 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
389 if (!viewer_stream_get(vstream
)) {
392 DBG("vstream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
395 vstream
->ref
.refcount
,
396 vstream
->stream
->stream_handle
,
397 vstream
->stream
->trace
->id
,
398 vstream
->stream
->trace
->session
->id
);
399 viewer_stream_put(vstream
);