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 struct lttng_trace_chunk
*viewer_trace_chunk
,
46 enum lttng_viewer_seek seek_t
)
48 struct relay_viewer_stream
*vstream
= NULL
;
49 const bool acquired_reference
= lttng_trace_chunk_get(
52 if (!acquired_reference
) {
56 vstream
= zmalloc(sizeof(*vstream
));
58 PERROR("relay viewer stream zmalloc");
62 vstream
->stream_file
.trace_chunk
= viewer_trace_chunk
;
63 viewer_trace_chunk
= NULL
;
64 vstream
->path_name
= lttng_strndup(stream
->path_name
, LTTNG_VIEWER_PATH_MAX
);
65 if (vstream
->path_name
== NULL
) {
66 PERROR("relay viewer path_name alloc");
69 vstream
->channel_name
= lttng_strndup(stream
->channel_name
,
70 LTTNG_VIEWER_NAME_MAX
);
71 if (vstream
->channel_name
== NULL
) {
72 PERROR("relay viewer channel_name alloc");
76 if (!stream_get(stream
)) {
77 ERR("Cannot get stream");
80 vstream
->stream
= stream
;
82 pthread_mutex_lock(&stream
->lock
);
84 if (stream
->is_metadata
&& stream
->trace
->viewer_metadata_stream
) {
85 ERR("Cannot attach viewer metadata stream to trace (busy).");
90 case LTTNG_VIEWER_SEEK_BEGINNING
:
92 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
94 if (seq_tail
== -1ULL) {
96 * Tail may not be initialized yet. Nonetheless, we know
97 * we want to send the first index once it becomes
102 vstream
->current_tracefile_id
=
103 tracefile_array_get_file_index_tail(stream
->tfa
);
104 vstream
->index_sent_seqcount
= seq_tail
;
107 case LTTNG_VIEWER_SEEK_LAST
:
108 vstream
->current_tracefile_id
=
109 tracefile_array_get_read_file_index_head(stream
->tfa
);
111 * We seek at the very end of each stream, awaiting for
112 * a future packet to eventually come in.
114 * We don't need to check the head position for -1ULL since the
115 * increment will set it to 0.
117 vstream
->index_sent_seqcount
=
118 tracefile_array_get_seq_head(stream
->tfa
) + 1;
125 * If we never received an index for the current stream, delay
126 * the opening of the index, otherwise open it right now.
128 if (stream
->index_received_seqcount
== 0) {
129 vstream
->index_file
= NULL
;
131 const uint32_t connection_major
= stream
->trace
->session
->major
;
132 const uint32_t connection_minor
= stream
->trace
->session
->minor
;
134 vstream
->index_file
= lttng_index_file_create_from_trace_chunk_read_only(
135 vstream
->stream_file
.trace_chunk
,
137 stream
->channel_name
, stream
->tracefile_size
,
138 vstream
->current_tracefile_id
,
139 lttng_to_index_major(connection_major
,
141 lttng_to_index_minor(connection_major
,
143 if (!vstream
->index_file
) {
148 if (seek_t
== LTTNG_VIEWER_SEEK_LAST
&& vstream
->index_file
) {
151 lseek_ret
= lseek(vstream
->index_file
->fd
, 0, SEEK_END
);
156 if (stream
->is_metadata
) {
157 rcu_assign_pointer(stream
->trace
->viewer_metadata_stream
,
160 pthread_mutex_unlock(&stream
->lock
);
162 /* Globally visible after the add unique. */
163 lttng_ht_node_init_u64(&vstream
->stream_n
, stream
->stream_handle
);
164 lttng_ht_add_unique_u64(viewer_streams_ht
, &vstream
->stream_n
);
166 urcu_ref_init(&vstream
->ref
);
171 pthread_mutex_unlock(&stream
->lock
);
174 viewer_stream_destroy(vstream
);
176 if (viewer_trace_chunk
&& acquired_reference
) {
177 lttng_trace_chunk_put(viewer_trace_chunk
);
182 static void viewer_stream_unpublish(struct relay_viewer_stream
*vstream
)
185 struct lttng_ht_iter iter
;
187 iter
.iter
.node
= &vstream
->stream_n
.node
;
188 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
192 static void viewer_stream_release(struct urcu_ref
*ref
)
194 struct relay_viewer_stream
*vstream
= caa_container_of(ref
,
195 struct relay_viewer_stream
, ref
);
197 if (vstream
->stream
->is_metadata
) {
198 rcu_assign_pointer(vstream
->stream
->trace
->viewer_metadata_stream
, NULL
);
201 viewer_stream_unpublish(vstream
);
203 if (vstream
->stream_file
.fd
) {
204 stream_fd_put(vstream
->stream_file
.fd
);
205 vstream
->stream_file
.fd
= NULL
;
207 if (vstream
->index_file
) {
208 lttng_index_file_put(vstream
->index_file
);
209 vstream
->index_file
= NULL
;
211 if (vstream
->stream
) {
212 stream_put(vstream
->stream
);
213 vstream
->stream
= NULL
;
216 call_rcu(&vstream
->rcu_node
, viewer_stream_destroy_rcu
);
219 /* Must be called with RCU read-side lock held. */
220 bool viewer_stream_get(struct relay_viewer_stream
*vstream
)
222 return urcu_ref_get_unless_zero(&vstream
->ref
);
226 * Get viewer stream by id.
228 * Return viewer stream if found else NULL.
230 struct relay_viewer_stream
*viewer_stream_get_by_id(uint64_t id
)
232 struct lttng_ht_node_u64
*node
;
233 struct lttng_ht_iter iter
;
234 struct relay_viewer_stream
*vstream
= NULL
;
237 lttng_ht_lookup(viewer_streams_ht
, &id
, &iter
);
238 node
= lttng_ht_iter_get_node_u64(&iter
);
240 DBG("Relay viewer stream %" PRIu64
" not found", id
);
243 vstream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
244 if (!viewer_stream_get(vstream
)) {
252 void viewer_stream_put(struct relay_viewer_stream
*vstream
)
255 urcu_ref_put(&vstream
->ref
, viewer_stream_release
);
260 * Rotate a stream to the next tracefile.
262 * Must be called with the rstream lock held.
263 * Returns 0 on success, 1 on EOF, a negative value on error.
265 int viewer_stream_rotate(struct relay_viewer_stream
*vstream
)
269 const struct relay_stream
*stream
= vstream
->stream
;
270 const uint32_t connection_major
= stream
->trace
->session
->major
;
271 const uint32_t connection_minor
= stream
->trace
->session
->minor
;
273 /* Detect the last tracefile to open. */
274 if (stream
->index_received_seqcount
275 == vstream
->index_sent_seqcount
276 && stream
->trace
->session
->connection_closed
) {
281 if (stream
->tracefile_count
== 0) {
282 /* Ignore rotation, there is none to do. */
288 * Try to move to the next file.
290 new_id
= (vstream
->current_tracefile_id
+ 1)
291 % stream
->tracefile_count
;
292 if (tracefile_array_seq_in_file(stream
->tfa
, new_id
,
293 vstream
->index_sent_seqcount
)) {
294 vstream
->current_tracefile_id
= new_id
;
296 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
299 * This can only be reached on overwrite, which implies there
300 * has been data written at some point, which will have set the
303 assert(seq_tail
!= -1ULL);
305 * We need to resync because we lag behind tail.
307 vstream
->current_tracefile_id
=
308 tracefile_array_get_file_index_tail(stream
->tfa
);
309 vstream
->index_sent_seqcount
= seq_tail
;
312 if (vstream
->index_file
) {
313 lttng_index_file_put(vstream
->index_file
);
314 vstream
->index_file
= NULL
;
316 if (vstream
->stream_file
.fd
) {
317 stream_fd_put(vstream
->stream_file
.fd
);
318 vstream
->stream_file
.fd
= NULL
;
320 vstream
->index_file
=
321 lttng_index_file_create_from_trace_chunk_read_only(
322 vstream
->stream_file
.trace_chunk
,
324 stream
->channel_name
,
325 stream
->tracefile_size
,
326 vstream
->current_tracefile_id
,
327 lttng_to_index_major(connection_major
,
329 lttng_to_index_minor(connection_major
,
331 if (!vstream
->index_file
) {
341 void print_viewer_streams(void)
343 struct lttng_ht_iter iter
;
344 struct relay_viewer_stream
*vstream
;
346 if (!viewer_streams_ht
) {
351 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
353 if (!viewer_stream_get(vstream
)) {
356 DBG("vstream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
359 vstream
->ref
.refcount
,
360 vstream
->stream
->stream_handle
,
361 vstream
->stream
->trace
->id
,
362 vstream
->stream
->trace
->session
->id
);
363 viewer_stream_put(vstream
);