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>
24 #include "lttng-relayd.h"
25 #include "viewer-stream.h"
27 static void viewer_stream_destroy(struct relay_viewer_stream
*vstream
)
29 free(vstream
->path_name
);
30 free(vstream
->channel_name
);
34 static void viewer_stream_destroy_rcu(struct rcu_head
*head
)
36 struct relay_viewer_stream
*vstream
=
37 caa_container_of(head
, struct relay_viewer_stream
, rcu_node
);
39 viewer_stream_destroy(vstream
);
42 struct relay_viewer_stream
*viewer_stream_create(struct relay_stream
*stream
,
43 enum lttng_viewer_seek seek_t
)
45 struct relay_viewer_stream
*vstream
;
47 vstream
= zmalloc(sizeof(*vstream
));
49 PERROR("relay viewer stream zmalloc");
53 vstream
->path_name
= strndup(stream
->path_name
, LTTNG_VIEWER_PATH_MAX
);
54 if (vstream
->path_name
== NULL
) {
55 PERROR("relay viewer path_name alloc");
58 vstream
->channel_name
= strndup(stream
->channel_name
,
59 LTTNG_VIEWER_NAME_MAX
);
60 if (vstream
->channel_name
== NULL
) {
61 PERROR("relay viewer channel_name alloc");
66 case LTTNG_VIEWER_SEEK_BEGINNING
:
67 vstream
->current_tracefile_id
= stream
->oldest_tracefile_id
;
69 case LTTNG_VIEWER_SEEK_LAST
:
70 vstream
->current_tracefile_id
= stream
->current_tracefile_id
;
75 if (!stream_get(stream
)) {
76 ERR("Cannot get stream");
79 vstream
->stream
= stream
;
81 pthread_mutex_lock(&stream
->lock
);
83 * If we never received an index for the current stream, delay the opening
84 * of the index, otherwise open it right now.
86 if (vstream
->current_tracefile_id
== stream
->current_tracefile_id
87 && stream
->total_index_received
== 0) {
88 vstream
->index_fd
= NULL
;
92 read_fd
= index_open(vstream
->path_name
, vstream
->channel_name
,
93 stream
->tracefile_count
,
94 vstream
->current_tracefile_id
);
98 vstream
->index_fd
= stream_fd_create(read_fd
);
99 if (!vstream
->index_fd
) {
100 if (close(read_fd
)) {
107 if (seek_t
== LTTNG_VIEWER_SEEK_LAST
&& vstream
->index_fd
) {
110 lseek_ret
= lseek(vstream
->index_fd
->fd
, 0, SEEK_END
);
114 vstream
->last_sent_index
= stream
->total_index_received
;
116 pthread_mutex_unlock(&stream
->lock
);
118 if (stream
->is_metadata
) {
119 rcu_assign_pointer(stream
->trace
->viewer_metadata_stream
,
123 /* Globally visible after the add unique. */
124 lttng_ht_node_init_u64(&vstream
->stream_n
, stream
->stream_handle
);
125 lttng_ht_add_unique_u64(viewer_streams_ht
, &vstream
->stream_n
);
127 pthread_mutex_init(&vstream
->reflock
, NULL
);
128 urcu_ref_init(&vstream
->ref
);
133 pthread_mutex_unlock(&stream
->lock
);
136 viewer_stream_destroy(vstream
);
141 static void viewer_stream_unpublish(struct relay_viewer_stream
*vstream
)
144 struct lttng_ht_iter iter
;
146 iter
.iter
.node
= &vstream
->stream_n
.node
;
147 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
151 static void viewer_stream_release(struct urcu_ref
*ref
)
153 struct relay_viewer_stream
*vstream
= caa_container_of(ref
,
154 struct relay_viewer_stream
, ref
);
156 if (vstream
->stream
->is_metadata
) {
157 rcu_assign_pointer(vstream
->stream
->trace
->viewer_metadata_stream
, NULL
);
160 viewer_stream_unpublish(vstream
);
162 if (vstream
->stream_fd
) {
163 stream_fd_put(vstream
->stream_fd
);
164 vstream
->stream_fd
= NULL
;
166 if (vstream
->index_fd
) {
167 stream_fd_put(vstream
->index_fd
);
168 vstream
->index_fd
= NULL
;
170 if (vstream
->stream
) {
171 stream_put(vstream
->stream
);
172 vstream
->stream
= NULL
;
174 call_rcu(&vstream
->rcu_node
, viewer_stream_destroy_rcu
);
177 /* Must be called with RCU read-side lock held. */
178 bool viewer_stream_get(struct relay_viewer_stream
*vstream
)
180 bool has_ref
= false;
182 pthread_mutex_lock(&vstream
->reflock
);
183 if (vstream
->ref
.refcount
!= 0) {
185 urcu_ref_get(&vstream
->ref
);
187 pthread_mutex_unlock(&vstream
->reflock
);
193 * Get viewer stream by id.
195 * Return viewer stream if found else NULL.
197 struct relay_viewer_stream
*viewer_stream_get_by_id(uint64_t id
)
199 struct lttng_ht_node_u64
*node
;
200 struct lttng_ht_iter iter
;
201 struct relay_viewer_stream
*vstream
= NULL
;
204 lttng_ht_lookup(viewer_streams_ht
, &id
, &iter
);
205 node
= lttng_ht_iter_get_node_u64(&iter
);
207 DBG("Relay viewer stream %" PRIu64
" not found", id
);
210 vstream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
211 if (!viewer_stream_get(vstream
)) {
219 void viewer_stream_put(struct relay_viewer_stream
*vstream
)
222 pthread_mutex_lock(&vstream
->reflock
);
223 urcu_ref_put(&vstream
->ref
, viewer_stream_release
);
224 pthread_mutex_unlock(&vstream
->reflock
);
229 * Returns whether the current tracefile is readable. If not, it has
231 * Must be called with rstream lock held.
233 bool viewer_stream_is_tracefile_seq_readable(struct relay_viewer_stream
*vstream
,
236 struct relay_stream
*stream
= vstream
->stream
;
238 if (seq
>= stream
->oldest_tracefile_seq
239 && seq
<= stream
->current_tracefile_seq
) {
240 /* seq is a readable file. */
243 /* seq is not readable. */
249 * Rotate a stream to the next tracefile.
251 * Must be called with the rstream lock held.
252 * Returns 0 on success, 1 on EOF, a negative value on error.
254 int viewer_stream_rotate(struct relay_viewer_stream
*vstream
)
257 struct relay_stream
*stream
= vstream
->stream
;
259 /* Detect the last tracefile to open. */
260 if (stream
->total_index_received
== vstream
->last_sent_index
261 && stream
->trace
->session
->connection_closed
) {
266 if (stream
->tracefile_count
== 0) {
267 /* Ignore rotation, there is none to do. */
272 if (!viewer_stream_is_tracefile_seq_readable(vstream
,
273 vstream
->current_tracefile_seq
+ 1)) {
274 vstream
->current_tracefile_id
=
275 stream
->oldest_tracefile_id
;
276 vstream
->current_tracefile_seq
=
277 stream
->oldest_tracefile_seq
;
279 vstream
->current_tracefile_id
=
280 (vstream
->current_tracefile_id
+ 1)
281 % stream
->tracefile_count
;
282 vstream
->current_tracefile_seq
++;
285 if (vstream
->index_fd
) {
286 stream_fd_put(vstream
->index_fd
);
287 vstream
->index_fd
= NULL
;
289 if (vstream
->stream_fd
) {
290 stream_fd_put(vstream
->stream_fd
);
291 vstream
->stream_fd
= NULL
;
294 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
295 stream
->tracefile_count
,
296 vstream
->current_tracefile_id
);
300 vstream
->index_fd
= stream_fd_create(ret
);
301 if (vstream
->index_fd
) {
313 void print_viewer_streams(void)
315 struct lttng_ht_iter iter
;
316 struct relay_viewer_stream
*vstream
;
319 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
321 if (!viewer_stream_get(vstream
)) {
324 DBG("vstream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
327 vstream
->ref
.refcount
,
328 vstream
->stream
->stream_handle
,
329 vstream
->stream
->trace
->id
,
330 vstream
->stream
->trace
->session
->id
);
331 viewer_stream_put(vstream
);