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>
19 #include "lttng-relayd.h"
20 #include "viewer-stream.h"
22 static void viewer_stream_destroy(struct relay_viewer_stream
*vstream
)
24 free(vstream
->path_name
);
25 free(vstream
->channel_name
);
29 static void viewer_stream_destroy_rcu(struct rcu_head
*head
)
31 struct relay_viewer_stream
*vstream
=
32 caa_container_of(head
, struct relay_viewer_stream
, rcu_node
);
34 viewer_stream_destroy(vstream
);
37 struct relay_viewer_stream
*viewer_stream_create(struct relay_stream
*stream
,
38 struct lttng_trace_chunk
*viewer_trace_chunk
,
39 enum lttng_viewer_seek seek_t
)
41 struct relay_viewer_stream
*vstream
= NULL
;
42 const bool acquired_reference
= lttng_trace_chunk_get(
45 if (!acquired_reference
) {
49 vstream
= zmalloc(sizeof(*vstream
));
51 PERROR("relay viewer stream zmalloc");
55 vstream
->stream_file
.trace_chunk
= viewer_trace_chunk
;
56 viewer_trace_chunk
= NULL
;
57 vstream
->path_name
= lttng_strndup(stream
->path_name
, LTTNG_VIEWER_PATH_MAX
);
58 if (vstream
->path_name
== NULL
) {
59 PERROR("relay viewer path_name alloc");
62 vstream
->channel_name
= lttng_strndup(stream
->channel_name
,
63 LTTNG_VIEWER_NAME_MAX
);
64 if (vstream
->channel_name
== NULL
) {
65 PERROR("relay viewer channel_name alloc");
69 if (!stream_get(stream
)) {
70 ERR("Cannot get stream");
73 vstream
->stream
= stream
;
75 pthread_mutex_lock(&stream
->lock
);
77 if (stream
->is_metadata
&& stream
->trace
->viewer_metadata_stream
) {
78 ERR("Cannot attach viewer metadata stream to trace (busy).");
83 case LTTNG_VIEWER_SEEK_BEGINNING
:
85 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
87 if (seq_tail
== -1ULL) {
89 * Tail may not be initialized yet. Nonetheless, we know
90 * we want to send the first index once it becomes
95 vstream
->current_tracefile_id
=
96 tracefile_array_get_file_index_tail(stream
->tfa
);
97 vstream
->index_sent_seqcount
= seq_tail
;
100 case LTTNG_VIEWER_SEEK_LAST
:
101 vstream
->current_tracefile_id
=
102 tracefile_array_get_read_file_index_head(stream
->tfa
);
104 * We seek at the very end of each stream, awaiting for
105 * a future packet to eventually come in.
107 * We don't need to check the head position for -1ULL since the
108 * increment will set it to 0.
110 vstream
->index_sent_seqcount
=
111 tracefile_array_get_seq_head(stream
->tfa
) + 1;
118 * If we never received an index for the current stream, delay
119 * the opening of the index, otherwise open it right now.
121 if (stream
->index_file
== NULL
) {
122 vstream
->index_file
= NULL
;
124 const uint32_t connection_major
= stream
->trace
->session
->major
;
125 const uint32_t connection_minor
= stream
->trace
->session
->minor
;
126 enum lttng_trace_chunk_status chunk_status
;
128 chunk_status
= lttng_index_file_create_from_trace_chunk_read_only(
129 vstream
->stream_file
.trace_chunk
,
131 stream
->channel_name
, stream
->tracefile_size
,
132 vstream
->current_tracefile_id
,
133 lttng_to_index_major(connection_major
,
135 lttng_to_index_minor(connection_major
,
137 true, &vstream
->index_file
);
138 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
139 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
140 vstream
->index_file
= NULL
;
148 * If we never received a data file for the current stream, delay the
149 * opening, otherwise open it right now.
153 char file_path
[LTTNG_PATH_MAX
];
154 enum lttng_trace_chunk_status status
;
156 ret
= utils_stream_file_path(stream
->path_name
,
157 stream
->channel_name
, stream
->tracefile_size
,
158 vstream
->current_tracefile_id
, NULL
, file_path
,
164 status
= lttng_trace_chunk_open_fs_handle(
165 vstream
->stream_file
.trace_chunk
, file_path
,
166 O_RDONLY
, 0, &vstream
->stream_file
.handle
,
168 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
173 if (seek_t
== LTTNG_VIEWER_SEEK_LAST
&& vstream
->index_file
) {
176 lseek_ret
= fs_handle_seek(
177 vstream
->index_file
->file
, 0, SEEK_END
);
182 if (stream
->is_metadata
) {
183 rcu_assign_pointer(stream
->trace
->viewer_metadata_stream
,
186 pthread_mutex_unlock(&stream
->lock
);
188 /* Globally visible after the add unique. */
189 lttng_ht_node_init_u64(&vstream
->stream_n
, stream
->stream_handle
);
190 urcu_ref_init(&vstream
->ref
);
191 lttng_ht_add_unique_u64(viewer_streams_ht
, &vstream
->stream_n
);
196 pthread_mutex_unlock(&stream
->lock
);
199 viewer_stream_destroy(vstream
);
201 if (viewer_trace_chunk
&& acquired_reference
) {
202 lttng_trace_chunk_put(viewer_trace_chunk
);
207 static void viewer_stream_unpublish(struct relay_viewer_stream
*vstream
)
210 struct lttng_ht_iter iter
;
212 iter
.iter
.node
= &vstream
->stream_n
.node
;
213 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
217 static void viewer_stream_release(struct urcu_ref
*ref
)
219 struct relay_viewer_stream
*vstream
= caa_container_of(ref
,
220 struct relay_viewer_stream
, ref
);
222 if (vstream
->stream
->is_metadata
) {
223 rcu_assign_pointer(vstream
->stream
->trace
->viewer_metadata_stream
, NULL
);
226 viewer_stream_unpublish(vstream
);
228 if (vstream
->stream_file
.handle
) {
229 fs_handle_close(vstream
->stream_file
.handle
);
230 vstream
->stream_file
.handle
= NULL
;
232 if (vstream
->index_file
) {
233 lttng_index_file_put(vstream
->index_file
);
234 vstream
->index_file
= NULL
;
236 if (vstream
->stream
) {
237 stream_put(vstream
->stream
);
238 vstream
->stream
= NULL
;
240 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
241 vstream
->stream_file
.trace_chunk
= NULL
;
242 call_rcu(&vstream
->rcu_node
, viewer_stream_destroy_rcu
);
245 /* Must be called with RCU read-side lock held. */
246 bool viewer_stream_get(struct relay_viewer_stream
*vstream
)
248 return urcu_ref_get_unless_zero(&vstream
->ref
);
252 * Get viewer stream by id.
254 * Return viewer stream if found else NULL.
256 struct relay_viewer_stream
*viewer_stream_get_by_id(uint64_t id
)
258 struct lttng_ht_node_u64
*node
;
259 struct lttng_ht_iter iter
;
260 struct relay_viewer_stream
*vstream
= NULL
;
263 lttng_ht_lookup(viewer_streams_ht
, &id
, &iter
);
264 node
= lttng_ht_iter_get_node_u64(&iter
);
266 DBG("Relay viewer stream %" PRIu64
" not found", id
);
269 vstream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
270 if (!viewer_stream_get(vstream
)) {
278 void viewer_stream_put(struct relay_viewer_stream
*vstream
)
281 urcu_ref_put(&vstream
->ref
, viewer_stream_release
);
285 void viewer_stream_close_files(struct relay_viewer_stream
*vstream
)
287 if (vstream
->index_file
) {
288 lttng_index_file_put(vstream
->index_file
);
289 vstream
->index_file
= NULL
;
291 if (vstream
->stream_file
.handle
) {
292 fs_handle_close(vstream
->stream_file
.handle
);
293 vstream
->stream_file
.handle
= NULL
;
297 void viewer_stream_sync_tracefile_array_tail(struct relay_viewer_stream
*vstream
)
299 const struct relay_stream
*stream
= vstream
->stream
;
302 vstream
->current_tracefile_id
= tracefile_array_get_file_index_tail(stream
->tfa
);
303 seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
304 if (seq_tail
== -1ULL) {
307 vstream
->index_sent_seqcount
= seq_tail
;
311 * Rotate a stream to the next tracefile.
313 * Must be called with the rstream lock held.
314 * Returns 0 on success, 1 on EOF.
316 int viewer_stream_rotate(struct relay_viewer_stream
*vstream
)
320 const struct relay_stream
*stream
= vstream
->stream
;
322 /* Detect the last tracefile to open. */
323 if (stream
->index_received_seqcount
324 == vstream
->index_sent_seqcount
325 && stream
->trace
->session
->connection_closed
) {
330 if (stream
->tracefile_count
== 0) {
331 /* Ignore rotation, there is none to do. */
337 * Try to move to the next file.
339 new_id
= (vstream
->current_tracefile_id
+ 1)
340 % stream
->tracefile_count
;
341 if (tracefile_array_seq_in_file(stream
->tfa
, new_id
,
342 vstream
->index_sent_seqcount
)) {
343 vstream
->current_tracefile_id
= new_id
;
345 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
348 * This can only be reached on overwrite, which implies there
349 * has been data written at some point, which will have set the
352 assert(seq_tail
!= -1ULL);
354 * We need to resync because we lag behind tail.
356 vstream
->current_tracefile_id
=
357 tracefile_array_get_file_index_tail(stream
->tfa
);
358 vstream
->index_sent_seqcount
= seq_tail
;
360 viewer_stream_close_files(vstream
);
366 void print_viewer_streams(void)
368 struct lttng_ht_iter iter
;
369 struct relay_viewer_stream
*vstream
;
371 if (!viewer_streams_ht
) {
376 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
378 if (!viewer_stream_get(vstream
)) {
381 DBG("vstream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
384 vstream
->ref
.refcount
,
385 vstream
->stream
->stream_handle
,
386 vstream
->stream
->trace
->id
,
387 vstream
->stream
->trace
->session
->id
);
388 viewer_stream_put(vstream
);