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 free(vstream
->path_name
);
31 free(vstream
->channel_name
);
35 static void viewer_stream_destroy_rcu(struct rcu_head
*head
)
37 struct relay_viewer_stream
*vstream
=
38 caa_container_of(head
, struct relay_viewer_stream
, rcu_node
);
40 viewer_stream_destroy(vstream
);
43 struct relay_viewer_stream
*viewer_stream_create(struct relay_stream
*stream
,
44 enum lttng_viewer_seek seek_t
)
46 struct relay_viewer_stream
*vstream
;
48 vstream
= zmalloc(sizeof(*vstream
));
50 PERROR("relay viewer stream zmalloc");
54 vstream
->path_name
= lttng_strndup(stream
->path_name
, LTTNG_VIEWER_PATH_MAX
);
55 if (vstream
->path_name
== NULL
) {
56 PERROR("relay viewer path_name alloc");
59 vstream
->channel_name
= lttng_strndup(stream
->channel_name
,
60 LTTNG_VIEWER_NAME_MAX
);
61 if (vstream
->channel_name
== NULL
) {
62 PERROR("relay viewer channel_name alloc");
66 if (!stream_get(stream
)) {
67 ERR("Cannot get stream");
70 vstream
->stream
= stream
;
72 pthread_mutex_lock(&stream
->lock
);
74 if (stream
->is_metadata
&& stream
->trace
->viewer_metadata_stream
) {
75 ERR("Cannot attach viewer metadata stream to trace (busy).");
80 case LTTNG_VIEWER_SEEK_BEGINNING
:
82 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
84 if (seq_tail
== -1ULL) {
86 * Tail may not be initialized yet. Nonetheless, we know
87 * we want to send the first index once it becomes
92 vstream
->current_tracefile_id
=
93 tracefile_array_get_file_index_tail(stream
->tfa
);
94 vstream
->index_sent_seqcount
= seq_tail
;
97 case LTTNG_VIEWER_SEEK_LAST
:
98 vstream
->current_tracefile_id
=
99 tracefile_array_get_file_index_head(stream
->tfa
);
101 * We seek at the very end of each stream, awaiting for
102 * a future packet to eventually come in.
104 * We don't need to check the head position for -1ULL since the
105 * increment will set it to 0.
107 vstream
->index_sent_seqcount
=
108 tracefile_array_get_seq_head(stream
->tfa
) + 1;
115 * If we never received an index for the current stream, delay
116 * the opening of the index, otherwise open it right now.
118 if (stream
->index_received_seqcount
== 0) {
119 vstream
->index_file
= NULL
;
121 vstream
->index_file
= lttng_index_file_open(vstream
->path_name
,
122 vstream
->channel_name
,
123 stream
->tracefile_count
,
124 vstream
->current_tracefile_id
);
125 if (!vstream
->index_file
) {
130 if (seek_t
== LTTNG_VIEWER_SEEK_LAST
&& vstream
->index_file
) {
133 lseek_ret
= lseek(vstream
->index_file
->fd
, 0, SEEK_END
);
138 if (stream
->is_metadata
) {
139 rcu_assign_pointer(stream
->trace
->viewer_metadata_stream
,
142 pthread_mutex_unlock(&stream
->lock
);
144 /* Globally visible after the add unique. */
145 lttng_ht_node_init_u64(&vstream
->stream_n
, stream
->stream_handle
);
146 lttng_ht_add_unique_u64(viewer_streams_ht
, &vstream
->stream_n
);
148 pthread_mutex_init(&vstream
->reflock
, NULL
);
149 urcu_ref_init(&vstream
->ref
);
154 pthread_mutex_unlock(&stream
->lock
);
157 viewer_stream_destroy(vstream
);
162 static void viewer_stream_unpublish(struct relay_viewer_stream
*vstream
)
165 struct lttng_ht_iter iter
;
167 iter
.iter
.node
= &vstream
->stream_n
.node
;
168 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
172 static void viewer_stream_release(struct urcu_ref
*ref
)
174 struct relay_viewer_stream
*vstream
= caa_container_of(ref
,
175 struct relay_viewer_stream
, ref
);
177 if (vstream
->stream
->is_metadata
) {
178 rcu_assign_pointer(vstream
->stream
->trace
->viewer_metadata_stream
, NULL
);
181 viewer_stream_unpublish(vstream
);
183 if (vstream
->stream_fd
) {
184 stream_fd_put(vstream
->stream_fd
);
185 vstream
->stream_fd
= NULL
;
187 if (vstream
->index_file
) {
188 lttng_index_file_put(vstream
->index_file
);
189 vstream
->index_file
= NULL
;
191 if (vstream
->stream
) {
192 stream_put(vstream
->stream
);
193 vstream
->stream
= NULL
;
195 call_rcu(&vstream
->rcu_node
, viewer_stream_destroy_rcu
);
198 /* Must be called with RCU read-side lock held. */
199 bool viewer_stream_get(struct relay_viewer_stream
*vstream
)
201 bool has_ref
= false;
203 pthread_mutex_lock(&vstream
->reflock
);
204 if (vstream
->ref
.refcount
!= 0) {
206 urcu_ref_get(&vstream
->ref
);
208 pthread_mutex_unlock(&vstream
->reflock
);
214 * Get viewer stream by id.
216 * Return viewer stream if found else NULL.
218 struct relay_viewer_stream
*viewer_stream_get_by_id(uint64_t id
)
220 struct lttng_ht_node_u64
*node
;
221 struct lttng_ht_iter iter
;
222 struct relay_viewer_stream
*vstream
= NULL
;
225 lttng_ht_lookup(viewer_streams_ht
, &id
, &iter
);
226 node
= lttng_ht_iter_get_node_u64(&iter
);
228 DBG("Relay viewer stream %" PRIu64
" not found", id
);
231 vstream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
232 if (!viewer_stream_get(vstream
)) {
240 void viewer_stream_put(struct relay_viewer_stream
*vstream
)
243 pthread_mutex_lock(&vstream
->reflock
);
244 urcu_ref_put(&vstream
->ref
, viewer_stream_release
);
245 pthread_mutex_unlock(&vstream
->reflock
);
250 * Rotate a stream to the next tracefile.
252 * Must be called with the rstream lock held.
253 * Returns 0 on success, 1 on EOF, a negative value on error.
255 int viewer_stream_rotate(struct relay_viewer_stream
*vstream
)
258 struct relay_stream
*stream
= vstream
->stream
;
261 /* Detect the last tracefile to open. */
262 if (stream
->index_received_seqcount
263 == vstream
->index_sent_seqcount
264 && stream
->trace
->session
->connection_closed
) {
269 if (stream
->tracefile_count
== 0) {
270 /* Ignore rotation, there is none to do. */
276 * Try to move to the next file.
278 new_id
= (vstream
->current_tracefile_id
+ 1)
279 % stream
->tracefile_count
;
280 if (tracefile_array_seq_in_file(stream
->tfa
, new_id
,
281 vstream
->index_sent_seqcount
)) {
282 vstream
->current_tracefile_id
= new_id
;
284 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
287 * This can only be reached on overwrite, which implies there
288 * has been data written at some point, which will have set the
291 assert(seq_tail
!= -1ULL);
293 * We need to resync because we lag behind tail.
295 vstream
->current_tracefile_id
=
296 tracefile_array_get_file_index_tail(stream
->tfa
);
297 vstream
->index_sent_seqcount
= seq_tail
;
300 if (vstream
->index_file
) {
301 lttng_index_file_put(vstream
->index_file
);
302 vstream
->index_file
= NULL
;
304 if (vstream
->stream_fd
) {
305 stream_fd_put(vstream
->stream_fd
);
306 vstream
->stream_fd
= NULL
;
309 vstream
->index_file
= lttng_index_file_open(vstream
->path_name
,
310 vstream
->channel_name
,
311 stream
->tracefile_count
,
312 vstream
->current_tracefile_id
);
313 if (!vstream
->index_file
) {
323 void print_viewer_streams(void)
325 struct lttng_ht_iter iter
;
326 struct relay_viewer_stream
*vstream
;
328 if (!viewer_streams_ht
) {
333 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
335 if (!viewer_stream_get(vstream
)) {
338 DBG("vstream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
341 vstream
->ref
.refcount
,
342 vstream
->stream
->stream_handle
,
343 vstream
->stream
->trace
->id
,
344 vstream
->stream
->trace
->session
->id
);
345 viewer_stream_put(vstream
);