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.h>
12 #include <common/index/index.h>
13 #include <common/compat/string.h>
14 #include <common/utils.h>
15 #include <sys/types.h>
20 #include "lttng-relayd.h"
21 #include "viewer-stream.h"
23 static void viewer_stream_destroy(struct relay_viewer_stream
*vstream
)
25 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
26 free(vstream
->path_name
);
27 free(vstream
->channel_name
);
31 static void viewer_stream_destroy_rcu(struct rcu_head
*head
)
33 struct relay_viewer_stream
*vstream
=
34 caa_container_of(head
, struct relay_viewer_stream
, rcu_node
);
36 viewer_stream_destroy(vstream
);
39 /* Relay stream's lock must be held by the caller. */
40 struct relay_viewer_stream
*viewer_stream_create(struct relay_stream
*stream
,
41 struct lttng_trace_chunk
*trace_chunk
,
42 enum lttng_viewer_seek seek_t
)
44 struct relay_viewer_stream
*vstream
= NULL
;
46 ASSERT_LOCKED(stream
->lock
);
48 vstream
= (relay_viewer_stream
*) zmalloc(sizeof(*vstream
));
50 PERROR("relay viewer stream zmalloc");
55 const bool acquired_reference
= lttng_trace_chunk_get(
58 LTTNG_ASSERT(acquired_reference
);
61 vstream
->stream_file
.trace_chunk
= trace_chunk
;
62 vstream
->path_name
= lttng_strndup(stream
->path_name
, LTTNG_VIEWER_PATH_MAX
);
63 if (vstream
->path_name
== NULL
) {
64 PERROR("relay viewer path_name alloc");
67 vstream
->channel_name
= lttng_strndup(stream
->channel_name
,
68 LTTNG_VIEWER_NAME_MAX
);
69 if (vstream
->channel_name
== NULL
) {
70 PERROR("relay viewer channel_name alloc");
74 if (!stream_get(stream
)) {
75 ERR("Cannot get stream");
78 vstream
->stream
= stream
;
80 if (stream
->is_metadata
&& stream
->trace
->viewer_metadata_stream
) {
81 ERR("Cannot attach viewer metadata stream to trace (busy).");
86 case LTTNG_VIEWER_SEEK_BEGINNING
:
88 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
90 if (seq_tail
== -1ULL) {
92 * Tail may not be initialized yet. Nonetheless, we know
93 * we want to send the first index once it becomes
98 vstream
->current_tracefile_id
=
99 tracefile_array_get_file_index_tail(stream
->tfa
);
100 vstream
->index_sent_seqcount
= seq_tail
;
103 case LTTNG_VIEWER_SEEK_LAST
:
104 vstream
->current_tracefile_id
=
105 tracefile_array_get_read_file_index_head(stream
->tfa
);
107 * We seek at the very end of each stream, awaiting for
108 * a future packet to eventually come in.
110 * We don't need to check the head position for -1ULL since the
111 * increment will set it to 0.
113 vstream
->index_sent_seqcount
=
114 tracefile_array_get_seq_head(stream
->tfa
) + 1;
121 * If we never received an index for the current stream, delay
122 * the opening of the index, otherwise open it right now.
124 if (stream
->index_file
== NULL
) {
125 vstream
->index_file
= NULL
;
126 } else if (vstream
->stream_file
.trace_chunk
) {
127 const uint32_t connection_major
= stream
->trace
->session
->major
;
128 const uint32_t connection_minor
= stream
->trace
->session
->minor
;
129 enum lttng_trace_chunk_status chunk_status
;
131 chunk_status
= lttng_index_file_create_from_trace_chunk_read_only(
132 vstream
->stream_file
.trace_chunk
,
134 stream
->channel_name
, stream
->tracefile_size
,
135 vstream
->current_tracefile_id
,
136 lttng_to_index_major(connection_major
,
138 lttng_to_index_minor(connection_major
,
140 true, &vstream
->index_file
);
141 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
142 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
143 vstream
->index_file
= NULL
;
151 * If we never received a data file for the current stream, delay the
152 * opening, otherwise open it right now.
154 if (stream
->file
&& vstream
->stream_file
.trace_chunk
) {
156 char file_path
[LTTNG_PATH_MAX
];
157 enum lttng_trace_chunk_status status
;
159 ret
= utils_stream_file_path(stream
->path_name
,
160 stream
->channel_name
, stream
->tracefile_size
,
161 vstream
->current_tracefile_id
, NULL
, file_path
,
167 status
= lttng_trace_chunk_open_fs_handle(
168 vstream
->stream_file
.trace_chunk
, file_path
,
169 O_RDONLY
, 0, &vstream
->stream_file
.handle
,
171 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
176 if (seek_t
== LTTNG_VIEWER_SEEK_LAST
&& vstream
->index_file
) {
179 lseek_ret
= fs_handle_seek(
180 vstream
->index_file
->file
, 0, SEEK_END
);
185 if (stream
->is_metadata
) {
186 rcu_assign_pointer(stream
->trace
->viewer_metadata_stream
,
190 vstream
->last_seen_rotation_count
= stream
->completed_rotation_count
;
192 /* Globally visible after the add unique. */
193 lttng_ht_node_init_u64(&vstream
->stream_n
, stream
->stream_handle
);
194 urcu_ref_init(&vstream
->ref
);
195 lttng_ht_add_unique_u64(viewer_streams_ht
, &vstream
->stream_n
);
201 viewer_stream_destroy(vstream
);
206 static void viewer_stream_unpublish(struct relay_viewer_stream
*vstream
)
209 struct lttng_ht_iter iter
;
211 iter
.iter
.node
= &vstream
->stream_n
.node
;
212 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
216 static void viewer_stream_release(struct urcu_ref
*ref
)
218 struct relay_viewer_stream
*vstream
= caa_container_of(ref
,
219 struct relay_viewer_stream
, ref
);
221 if (vstream
->stream
->is_metadata
) {
222 rcu_assign_pointer(vstream
->stream
->trace
->viewer_metadata_stream
, NULL
);
225 viewer_stream_unpublish(vstream
);
227 if (vstream
->stream_file
.handle
) {
228 fs_handle_close(vstream
->stream_file
.handle
);
229 vstream
->stream_file
.handle
= NULL
;
231 if (vstream
->index_file
) {
232 lttng_index_file_put(vstream
->index_file
);
233 vstream
->index_file
= NULL
;
235 if (vstream
->stream
) {
236 stream_put(vstream
->stream
);
237 vstream
->stream
= NULL
;
239 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
240 vstream
->stream_file
.trace_chunk
= NULL
;
241 call_rcu(&vstream
->rcu_node
, viewer_stream_destroy_rcu
);
244 /* Must be called with RCU read-side lock held. */
245 bool viewer_stream_get(struct relay_viewer_stream
*vstream
)
247 return urcu_ref_get_unless_zero(&vstream
->ref
);
251 * Get viewer stream by id.
253 * Return viewer stream if found else NULL.
255 struct relay_viewer_stream
*viewer_stream_get_by_id(uint64_t id
)
257 struct lttng_ht_node_u64
*node
;
258 struct lttng_ht_iter iter
;
259 struct relay_viewer_stream
*vstream
= NULL
;
262 lttng_ht_lookup(viewer_streams_ht
, &id
, &iter
);
263 node
= lttng_ht_iter_get_node_u64(&iter
);
265 DBG("Relay viewer stream %" PRIu64
" not found", id
);
268 vstream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
269 if (!viewer_stream_get(vstream
)) {
277 void viewer_stream_put(struct relay_viewer_stream
*vstream
)
280 urcu_ref_put(&vstream
->ref
, viewer_stream_release
);
284 void viewer_stream_close_files(struct relay_viewer_stream
*vstream
)
286 if (vstream
->index_file
) {
287 lttng_index_file_put(vstream
->index_file
);
288 vstream
->index_file
= NULL
;
290 if (vstream
->stream_file
.handle
) {
291 fs_handle_close(vstream
->stream_file
.handle
);
292 vstream
->stream_file
.handle
= NULL
;
296 void viewer_stream_sync_tracefile_array_tail(struct relay_viewer_stream
*vstream
)
298 const struct relay_stream
*stream
= vstream
->stream
;
301 vstream
->current_tracefile_id
= tracefile_array_get_file_index_tail(stream
->tfa
);
302 seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
303 if (seq_tail
== -1ULL) {
308 * Move the index sent seqcount forward if it was lagging behind
309 * the new tail of the tracefile array. If the current
310 * index_sent_seqcount is already further than the tracefile
311 * array tail position, keep its current position.
313 vstream
->index_sent_seqcount
=
314 std::max(seq_tail
, vstream
->index_sent_seqcount
);
318 * Rotate a stream to the next tracefile.
320 * Must be called with the rstream lock held.
321 * Returns 0 on success, 1 on EOF.
323 int viewer_stream_rotate(struct relay_viewer_stream
*vstream
)
327 const struct relay_stream
*stream
= vstream
->stream
;
329 /* Detect the last tracefile to open. */
330 if (stream
->index_received_seqcount
331 == vstream
->index_sent_seqcount
332 && stream
->trace
->session
->connection_closed
) {
337 if (stream
->tracefile_count
== 0) {
338 /* Ignore rotation, there is none to do. */
344 * Try to move to the next file.
346 new_id
= (vstream
->current_tracefile_id
+ 1)
347 % stream
->tracefile_count
;
348 if (tracefile_array_seq_in_file(stream
->tfa
, new_id
,
349 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
=
364 tracefile_array_get_file_index_tail(stream
->tfa
);
365 vstream
->index_sent_seqcount
= seq_tail
;
367 viewer_stream_close_files(vstream
);
373 void print_viewer_streams(void)
375 struct lttng_ht_iter iter
;
376 struct relay_viewer_stream
*vstream
;
378 if (!viewer_streams_ht
) {
383 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
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
);