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 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
25 free(vstream
->path_name
);
26 free(vstream
->channel_name
);
30 static void viewer_stream_destroy_rcu(struct rcu_head
*head
)
32 struct relay_viewer_stream
*vstream
=
33 caa_container_of(head
, struct relay_viewer_stream
, rcu_node
);
35 viewer_stream_destroy(vstream
);
38 /* Relay stream's lock must be held by the caller. */
39 struct relay_viewer_stream
*viewer_stream_create(struct relay_stream
*stream
,
40 struct lttng_trace_chunk
*trace_chunk
,
41 enum lttng_viewer_seek seek_t
)
43 struct relay_viewer_stream
*vstream
= NULL
;
45 ASSERT_LOCKED(stream
->lock
);
47 vstream
= zmalloc(sizeof(*vstream
));
49 PERROR("relay viewer stream zmalloc");
54 const bool acquired_reference
= lttng_trace_chunk_get(
57 LTTNG_ASSERT(acquired_reference
);
60 vstream
->stream_file
.trace_chunk
= trace_chunk
;
61 vstream
->path_name
= lttng_strndup(stream
->path_name
, LTTNG_VIEWER_PATH_MAX
);
62 if (vstream
->path_name
== NULL
) {
63 PERROR("relay viewer path_name alloc");
66 vstream
->channel_name
= lttng_strndup(stream
->channel_name
,
67 LTTNG_VIEWER_NAME_MAX
);
68 if (vstream
->channel_name
== NULL
) {
69 PERROR("relay viewer channel_name alloc");
73 if (!stream_get(stream
)) {
74 ERR("Cannot get stream");
77 vstream
->stream
= stream
;
79 if (stream
->is_metadata
&& stream
->trace
->viewer_metadata_stream
) {
80 ERR("Cannot attach viewer metadata stream to trace (busy).");
85 case LTTNG_VIEWER_SEEK_BEGINNING
:
87 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
89 if (seq_tail
== -1ULL) {
91 * Tail may not be initialized yet. Nonetheless, we know
92 * we want to send the first index once it becomes
97 vstream
->current_tracefile_id
=
98 tracefile_array_get_file_index_tail(stream
->tfa
);
99 vstream
->index_sent_seqcount
= seq_tail
;
102 case LTTNG_VIEWER_SEEK_LAST
:
103 vstream
->current_tracefile_id
=
104 tracefile_array_get_read_file_index_head(stream
->tfa
);
106 * We seek at the very end of each stream, awaiting for
107 * a future packet to eventually come in.
109 * We don't need to check the head position for -1ULL since the
110 * increment will set it to 0.
112 vstream
->index_sent_seqcount
=
113 tracefile_array_get_seq_head(stream
->tfa
) + 1;
120 * If we never received an index for the current stream, delay
121 * the opening of the index, otherwise open it right now.
123 if (stream
->index_file
== NULL
) {
124 vstream
->index_file
= NULL
;
125 } else if (vstream
->stream_file
.trace_chunk
) {
126 const uint32_t connection_major
= stream
->trace
->session
->major
;
127 const uint32_t connection_minor
= stream
->trace
->session
->minor
;
128 enum lttng_trace_chunk_status chunk_status
;
130 chunk_status
= lttng_index_file_create_from_trace_chunk_read_only(
131 vstream
->stream_file
.trace_chunk
,
133 stream
->channel_name
, stream
->tracefile_size
,
134 vstream
->current_tracefile_id
,
135 lttng_to_index_major(connection_major
,
137 lttng_to_index_minor(connection_major
,
139 true, &vstream
->index_file
);
140 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
141 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
142 vstream
->index_file
= NULL
;
150 * If we never received a data file for the current stream, delay the
151 * opening, otherwise open it right now.
153 if (stream
->file
&& vstream
->stream_file
.trace_chunk
) {
155 char file_path
[LTTNG_PATH_MAX
];
156 enum lttng_trace_chunk_status status
;
158 ret
= utils_stream_file_path(stream
->path_name
,
159 stream
->channel_name
, stream
->tracefile_size
,
160 vstream
->current_tracefile_id
, NULL
, file_path
,
166 status
= lttng_trace_chunk_open_fs_handle(
167 vstream
->stream_file
.trace_chunk
, file_path
,
168 O_RDONLY
, 0, &vstream
->stream_file
.handle
,
170 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
175 if (seek_t
== LTTNG_VIEWER_SEEK_LAST
&& vstream
->index_file
) {
178 lseek_ret
= fs_handle_seek(
179 vstream
->index_file
->file
, 0, SEEK_END
);
184 if (stream
->is_metadata
) {
185 rcu_assign_pointer(stream
->trace
->viewer_metadata_stream
,
189 vstream
->last_seen_rotation_count
= stream
->completed_rotation_count
;
191 /* Globally visible after the add unique. */
192 lttng_ht_node_init_u64(&vstream
->stream_n
, stream
->stream_handle
);
193 urcu_ref_init(&vstream
->ref
);
194 lttng_ht_add_unique_u64(viewer_streams_ht
, &vstream
->stream_n
);
200 viewer_stream_destroy(vstream
);
205 static void viewer_stream_unpublish(struct relay_viewer_stream
*vstream
)
208 struct lttng_ht_iter iter
;
210 iter
.iter
.node
= &vstream
->stream_n
.node
;
211 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
215 static void viewer_stream_release(struct urcu_ref
*ref
)
217 struct relay_viewer_stream
*vstream
= caa_container_of(ref
,
218 struct relay_viewer_stream
, ref
);
220 if (vstream
->stream
->is_metadata
) {
221 rcu_assign_pointer(vstream
->stream
->trace
->viewer_metadata_stream
, NULL
);
224 viewer_stream_unpublish(vstream
);
226 if (vstream
->stream_file
.handle
) {
227 fs_handle_close(vstream
->stream_file
.handle
);
228 vstream
->stream_file
.handle
= NULL
;
230 if (vstream
->index_file
) {
231 lttng_index_file_put(vstream
->index_file
);
232 vstream
->index_file
= NULL
;
234 if (vstream
->stream
) {
235 stream_put(vstream
->stream
);
236 vstream
->stream
= NULL
;
238 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
239 vstream
->stream_file
.trace_chunk
= NULL
;
240 call_rcu(&vstream
->rcu_node
, viewer_stream_destroy_rcu
);
243 /* Must be called with RCU read-side lock held. */
244 bool viewer_stream_get(struct relay_viewer_stream
*vstream
)
246 return urcu_ref_get_unless_zero(&vstream
->ref
);
250 * Get viewer stream by id.
252 * Return viewer stream if found else NULL.
254 struct relay_viewer_stream
*viewer_stream_get_by_id(uint64_t id
)
256 struct lttng_ht_node_u64
*node
;
257 struct lttng_ht_iter iter
;
258 struct relay_viewer_stream
*vstream
= NULL
;
261 lttng_ht_lookup(viewer_streams_ht
, &id
, &iter
);
262 node
= lttng_ht_iter_get_node_u64(&iter
);
264 DBG("Relay viewer stream %" PRIu64
" not found", id
);
267 vstream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
268 if (!viewer_stream_get(vstream
)) {
276 void viewer_stream_put(struct relay_viewer_stream
*vstream
)
279 urcu_ref_put(&vstream
->ref
, viewer_stream_release
);
283 void viewer_stream_close_files(struct relay_viewer_stream
*vstream
)
285 if (vstream
->index_file
) {
286 lttng_index_file_put(vstream
->index_file
);
287 vstream
->index_file
= NULL
;
289 if (vstream
->stream_file
.handle
) {
290 fs_handle_close(vstream
->stream_file
.handle
);
291 vstream
->stream_file
.handle
= NULL
;
295 void viewer_stream_sync_tracefile_array_tail(struct relay_viewer_stream
*vstream
)
297 const struct relay_stream
*stream
= vstream
->stream
;
300 vstream
->current_tracefile_id
= tracefile_array_get_file_index_tail(stream
->tfa
);
301 seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
302 if (seq_tail
== -1ULL) {
305 vstream
->index_sent_seqcount
= seq_tail
;
309 * Rotate a stream to the next tracefile.
311 * Must be called with the rstream lock held.
312 * Returns 0 on success, 1 on EOF.
314 int viewer_stream_rotate(struct relay_viewer_stream
*vstream
)
318 const struct relay_stream
*stream
= vstream
->stream
;
320 /* Detect the last tracefile to open. */
321 if (stream
->index_received_seqcount
322 == vstream
->index_sent_seqcount
323 && stream
->trace
->session
->connection_closed
) {
328 if (stream
->tracefile_count
== 0) {
329 /* Ignore rotation, there is none to do. */
335 * Try to move to the next file.
337 new_id
= (vstream
->current_tracefile_id
+ 1)
338 % stream
->tracefile_count
;
339 if (tracefile_array_seq_in_file(stream
->tfa
, new_id
,
340 vstream
->index_sent_seqcount
)) {
341 vstream
->current_tracefile_id
= new_id
;
343 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
346 * This can only be reached on overwrite, which implies there
347 * has been data written at some point, which will have set the
350 LTTNG_ASSERT(seq_tail
!= -1ULL);
352 * We need to resync because we lag behind tail.
354 vstream
->current_tracefile_id
=
355 tracefile_array_get_file_index_tail(stream
->tfa
);
356 vstream
->index_sent_seqcount
= seq_tail
;
358 viewer_stream_close_files(vstream
);
364 void print_viewer_streams(void)
366 struct lttng_ht_iter iter
;
367 struct relay_viewer_stream
*vstream
;
369 if (!viewer_streams_ht
) {
374 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
376 if (!viewer_stream_get(vstream
)) {
379 DBG("vstream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
382 vstream
->ref
.refcount
,
383 vstream
->stream
->stream_handle
,
384 vstream
->stream
->trace
->id
,
385 vstream
->stream
->trace
->session
->id
);
386 viewer_stream_put(vstream
);