2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2 only, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <common/common.h>
22 #include <common/index/index.h>
23 #include <common/compat/string.h>
25 #include "lttng-relayd.h"
26 #include "viewer-stream.h"
28 static void viewer_stream_destroy(struct relay_viewer_stream
*vstream
)
30 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
31 free(vstream
->path_name
);
32 free(vstream
->channel_name
);
36 static void viewer_stream_destroy_rcu(struct rcu_head
*head
)
38 struct relay_viewer_stream
*vstream
=
39 caa_container_of(head
, struct relay_viewer_stream
, rcu_node
);
41 viewer_stream_destroy(vstream
);
44 struct relay_viewer_stream
*viewer_stream_create(struct relay_stream
*stream
,
45 enum lttng_viewer_seek seek_t
)
47 struct relay_viewer_stream
*vstream
= NULL
;
48 const bool acquired_reference
= lttng_trace_chunk_get(
51 if (!acquired_reference
) {
55 vstream
= zmalloc(sizeof(*vstream
));
57 PERROR("relay viewer stream zmalloc");
61 vstream
->stream_file
.trace_chunk
= stream
->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 pthread_mutex_lock(&stream
->lock
);
82 if (stream
->is_metadata
&& stream
->trace
->viewer_metadata_stream
) {
83 ERR("Cannot attach viewer metadata stream to trace (busy).");
88 case LTTNG_VIEWER_SEEK_BEGINNING
:
90 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
92 if (seq_tail
== -1ULL) {
94 * Tail may not be initialized yet. Nonetheless, we know
95 * we want to send the first index once it becomes
100 vstream
->current_tracefile_id
=
101 tracefile_array_get_file_index_tail(stream
->tfa
);
102 vstream
->index_sent_seqcount
= seq_tail
;
105 case LTTNG_VIEWER_SEEK_LAST
:
106 vstream
->current_tracefile_id
=
107 tracefile_array_get_file_index_head(stream
->tfa
);
109 * We seek at the very end of each stream, awaiting for
110 * a future packet to eventually come in.
112 * We don't need to check the head position for -1ULL since the
113 * increment will set it to 0.
115 vstream
->index_sent_seqcount
=
116 tracefile_array_get_seq_head(stream
->tfa
) + 1;
123 * If we never received an index for the current stream, delay
124 * the opening of the index, otherwise open it right now.
126 if (stream
->index_received_seqcount
== 0) {
127 vstream
->index_file
= NULL
;
129 const uint32_t connection_major
= stream
->trace
->session
->major
;
130 const uint32_t connection_minor
= stream
->trace
->session
->minor
;
132 vstream
->index_file
= lttng_index_file_create_from_trace_chunk_read_only(
133 stream
->trace_chunk
, stream
->path_name
,
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 if (!vstream
->index_file
) {
145 if (seek_t
== LTTNG_VIEWER_SEEK_LAST
&& vstream
->index_file
) {
148 lseek_ret
= lseek(vstream
->index_file
->fd
, 0, SEEK_END
);
153 if (stream
->is_metadata
) {
154 rcu_assign_pointer(stream
->trace
->viewer_metadata_stream
,
157 pthread_mutex_unlock(&stream
->lock
);
159 /* Globally visible after the add unique. */
160 lttng_ht_node_init_u64(&vstream
->stream_n
, stream
->stream_handle
);
161 lttng_ht_add_unique_u64(viewer_streams_ht
, &vstream
->stream_n
);
163 urcu_ref_init(&vstream
->ref
);
168 pthread_mutex_unlock(&stream
->lock
);
171 viewer_stream_destroy(vstream
);
176 static void viewer_stream_unpublish(struct relay_viewer_stream
*vstream
)
179 struct lttng_ht_iter iter
;
181 iter
.iter
.node
= &vstream
->stream_n
.node
;
182 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
186 static void viewer_stream_release(struct urcu_ref
*ref
)
188 struct relay_viewer_stream
*vstream
= caa_container_of(ref
,
189 struct relay_viewer_stream
, ref
);
191 if (vstream
->stream
->is_metadata
) {
192 rcu_assign_pointer(vstream
->stream
->trace
->viewer_metadata_stream
, NULL
);
195 viewer_stream_unpublish(vstream
);
197 if (vstream
->stream_file
.fd
) {
198 stream_fd_put(vstream
->stream_file
.fd
);
199 vstream
->stream_file
.fd
= NULL
;
201 if (vstream
->index_file
) {
202 lttng_index_file_put(vstream
->index_file
);
203 vstream
->index_file
= NULL
;
205 if (vstream
->stream
) {
206 stream_put(vstream
->stream
);
207 vstream
->stream
= NULL
;
210 call_rcu(&vstream
->rcu_node
, viewer_stream_destroy_rcu
);
213 /* Must be called with RCU read-side lock held. */
214 bool viewer_stream_get(struct relay_viewer_stream
*vstream
)
216 return urcu_ref_get_unless_zero(&vstream
->ref
);
220 * Get viewer stream by id.
222 * Return viewer stream if found else NULL.
224 struct relay_viewer_stream
*viewer_stream_get_by_id(uint64_t id
)
226 struct lttng_ht_node_u64
*node
;
227 struct lttng_ht_iter iter
;
228 struct relay_viewer_stream
*vstream
= NULL
;
231 lttng_ht_lookup(viewer_streams_ht
, &id
, &iter
);
232 node
= lttng_ht_iter_get_node_u64(&iter
);
234 DBG("Relay viewer stream %" PRIu64
" not found", id
);
237 vstream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
238 if (!viewer_stream_get(vstream
)) {
246 void viewer_stream_put(struct relay_viewer_stream
*vstream
)
249 urcu_ref_put(&vstream
->ref
, viewer_stream_release
);
254 * Rotate a stream to the next tracefile.
256 * Must be called with the rstream lock held.
257 * Returns 0 on success, 1 on EOF, a negative value on error.
259 int viewer_stream_rotate(struct relay_viewer_stream
*vstream
)
263 const struct relay_stream
*stream
= vstream
->stream
;
264 const uint32_t connection_major
= stream
->trace
->session
->major
;
265 const uint32_t connection_minor
= stream
->trace
->session
->minor
;
267 /* Detect the last tracefile to open. */
268 if (stream
->index_received_seqcount
269 == vstream
->index_sent_seqcount
270 && stream
->trace
->session
->connection_closed
) {
275 if (stream
->tracefile_count
== 0) {
276 /* Ignore rotation, there is none to do. */
282 * Try to move to the next file.
284 new_id
= (vstream
->current_tracefile_id
+ 1)
285 % stream
->tracefile_count
;
286 if (tracefile_array_seq_in_file(stream
->tfa
, new_id
,
287 vstream
->index_sent_seqcount
)) {
288 vstream
->current_tracefile_id
= new_id
;
290 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
293 * This can only be reached on overwrite, which implies there
294 * has been data written at some point, which will have set the
297 assert(seq_tail
!= -1ULL);
299 * We need to resync because we lag behind tail.
301 vstream
->current_tracefile_id
=
302 tracefile_array_get_file_index_tail(stream
->tfa
);
303 vstream
->index_sent_seqcount
= seq_tail
;
306 if (vstream
->index_file
) {
307 lttng_index_file_put(vstream
->index_file
);
308 vstream
->index_file
= NULL
;
310 if (vstream
->stream_file
.fd
) {
311 stream_fd_put(vstream
->stream_file
.fd
);
312 vstream
->stream_file
.fd
= NULL
;
314 vstream
->index_file
=
315 lttng_index_file_create_from_trace_chunk_read_only(
316 stream
->trace_chunk
, stream
->path_name
,
317 stream
->channel_name
,
318 stream
->tracefile_size
,
319 vstream
->current_tracefile_id
,
320 lttng_to_index_major(connection_major
,
322 lttng_to_index_minor(connection_major
,
324 if (!vstream
->index_file
) {
334 void print_viewer_streams(void)
336 struct lttng_ht_iter iter
;
337 struct relay_viewer_stream
*vstream
;
339 if (!viewer_streams_ht
) {
344 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
346 if (!viewer_stream_get(vstream
)) {
349 DBG("vstream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
352 vstream
->ref
.refcount
,
353 vstream
->stream
->stream_handle
,
354 vstream
->stream
->trace
->id
,
355 vstream
->stream
->trace
->session
->id
);
356 viewer_stream_put(vstream
);