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.
22 #include <common/common.h>
23 #include <common/index/index.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
= 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
= strndup(stream
->channel_name
,
60 LTTNG_VIEWER_NAME_MAX
);
61 if (vstream
->channel_name
== NULL
) {
62 PERROR("relay viewer channel_name alloc");
67 case LTTNG_VIEWER_SEEK_BEGINNING
:
68 vstream
->current_tracefile_id
= stream
->oldest_tracefile_id
;
70 case LTTNG_VIEWER_SEEK_LAST
:
71 vstream
->current_tracefile_id
= stream
->current_tracefile_id
;
76 if (!stream_get(stream
)) {
77 ERR("Cannot get stream");
80 vstream
->stream
= stream
;
82 pthread_mutex_lock(&stream
->lock
);
84 * If we never received an index for the current stream, delay the opening
85 * of the index, otherwise open it right now.
87 if (vstream
->current_tracefile_id
== stream
->current_tracefile_id
88 && stream
->total_index_received
== 0) {
89 vstream
->index_fd
= NULL
;
93 read_fd
= index_open(vstream
->path_name
, vstream
->channel_name
,
94 stream
->tracefile_count
,
95 vstream
->current_tracefile_id
);
99 vstream
->index_fd
= stream_fd_create(read_fd
);
100 if (!vstream
->index_fd
) {
101 if (close(read_fd
)) {
108 if (seek_t
== LTTNG_VIEWER_SEEK_LAST
&& vstream
->index_fd
) {
111 lseek_ret
= lseek(vstream
->index_fd
->fd
, 0, SEEK_END
);
115 vstream
->last_sent_index
= stream
->total_index_received
;
117 pthread_mutex_unlock(&stream
->lock
);
119 if (stream
->is_metadata
) {
120 rcu_assign_pointer(stream
->trace
->viewer_metadata_stream
,
124 /* Globally visible after the add unique. */
125 lttng_ht_node_init_u64(&vstream
->stream_n
, stream
->stream_handle
);
126 lttng_ht_add_unique_u64(viewer_streams_ht
, &vstream
->stream_n
);
128 pthread_mutex_init(&vstream
->reflock
, NULL
);
129 urcu_ref_init(&vstream
->ref
);
134 pthread_mutex_unlock(&stream
->lock
);
137 viewer_stream_destroy(vstream
);
142 static void viewer_stream_unpublish(struct relay_viewer_stream
*vstream
)
145 struct lttng_ht_iter iter
;
147 iter
.iter
.node
= &vstream
->stream_n
.node
;
148 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
152 static void viewer_stream_release(struct urcu_ref
*ref
)
154 struct relay_viewer_stream
*vstream
= caa_container_of(ref
,
155 struct relay_viewer_stream
, ref
);
157 if (vstream
->stream
->is_metadata
) {
158 rcu_assign_pointer(vstream
->stream
->trace
->viewer_metadata_stream
, NULL
);
161 viewer_stream_unpublish(vstream
);
163 if (vstream
->stream_fd
) {
164 stream_fd_put(vstream
->stream_fd
);
165 vstream
->stream_fd
= NULL
;
167 if (vstream
->index_fd
) {
168 stream_fd_put(vstream
->index_fd
);
169 vstream
->index_fd
= NULL
;
171 if (vstream
->stream
) {
172 stream_put(vstream
->stream
);
173 vstream
->stream
= NULL
;
175 call_rcu(&vstream
->rcu_node
, viewer_stream_destroy_rcu
);
178 /* Must be called with RCU read-side lock held. */
179 bool viewer_stream_get(struct relay_viewer_stream
*vstream
)
181 bool has_ref
= false;
183 pthread_mutex_lock(&vstream
->reflock
);
184 if (vstream
->ref
.refcount
!= 0) {
186 urcu_ref_get(&vstream
->ref
);
188 pthread_mutex_unlock(&vstream
->reflock
);
194 * Get viewer stream by id.
196 * Return viewer stream if found else NULL.
198 struct relay_viewer_stream
*viewer_stream_get_by_id(uint64_t id
)
200 struct lttng_ht_node_u64
*node
;
201 struct lttng_ht_iter iter
;
202 struct relay_viewer_stream
*vstream
= NULL
;
205 lttng_ht_lookup(viewer_streams_ht
, &id
, &iter
);
206 node
= lttng_ht_iter_get_node_u64(&iter
);
208 DBG("Relay viewer stream %" PRIu64
" not found", id
);
211 vstream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
212 if (!viewer_stream_get(vstream
)) {
220 void viewer_stream_put(struct relay_viewer_stream
*vstream
)
223 pthread_mutex_lock(&vstream
->reflock
);
224 urcu_ref_put(&vstream
->ref
, viewer_stream_release
);
225 pthread_mutex_unlock(&vstream
->reflock
);
230 * Returns whether the current tracefile is readable. If not, it has
232 * Must be called with rstream lock held.
234 bool viewer_stream_is_tracefile_seq_readable(struct relay_viewer_stream
*vstream
,
237 struct relay_stream
*stream
= vstream
->stream
;
239 if (seq
>= stream
->oldest_tracefile_seq
240 && seq
<= stream
->current_tracefile_seq
) {
241 /* seq is a readable file. */
244 /* seq is not readable. */
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
;
260 /* Detect the last tracefile to open. */
261 if (stream
->total_index_received
== vstream
->last_sent_index
262 && stream
->trace
->session
->connection_closed
) {
267 if (stream
->tracefile_count
== 0) {
268 /* Ignore rotation, there is none to do. */
273 if (!viewer_stream_is_tracefile_seq_readable(vstream
,
274 vstream
->current_tracefile_seq
+ 1)) {
275 vstream
->current_tracefile_id
=
276 stream
->oldest_tracefile_id
;
277 vstream
->current_tracefile_seq
=
278 stream
->oldest_tracefile_seq
;
280 vstream
->current_tracefile_id
=
281 (vstream
->current_tracefile_id
+ 1)
282 % stream
->tracefile_count
;
283 vstream
->current_tracefile_seq
++;
286 if (vstream
->index_fd
) {
287 stream_fd_put(vstream
->index_fd
);
288 vstream
->index_fd
= NULL
;
290 if (vstream
->stream_fd
) {
291 stream_fd_put(vstream
->stream_fd
);
292 vstream
->stream_fd
= NULL
;
295 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
296 stream
->tracefile_count
,
297 vstream
->current_tracefile_id
);
301 vstream
->index_fd
= stream_fd_create(ret
);
302 if (vstream
->index_fd
) {
314 void print_viewer_streams(void)
316 struct lttng_ht_iter iter
;
317 struct relay_viewer_stream
*vstream
;
320 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
322 if (!viewer_stream_get(vstream
)) {
325 DBG("vstream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
328 vstream
->ref
.refcount
,
329 vstream
->stream
->stream_handle
,
330 vstream
->stream
->trace
->id
,
331 vstream
->stream
->trace
->session
->id
);
332 viewer_stream_put(vstream
);