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>
24 #include <common/utils.h>
25 #include <sys/types.h>
29 #include "lttng-relayd.h"
30 #include "viewer-stream.h"
32 static void viewer_stream_destroy(struct relay_viewer_stream
*vstream
)
34 free(vstream
->path_name
);
35 free(vstream
->channel_name
);
39 static void viewer_stream_destroy_rcu(struct rcu_head
*head
)
41 struct relay_viewer_stream
*vstream
=
42 caa_container_of(head
, struct relay_viewer_stream
, rcu_node
);
44 viewer_stream_destroy(vstream
);
47 struct relay_viewer_stream
*viewer_stream_create(struct relay_stream
*stream
,
48 struct lttng_trace_chunk
*viewer_trace_chunk
,
49 enum lttng_viewer_seek seek_t
)
51 struct relay_viewer_stream
*vstream
= NULL
;
52 const bool acquired_reference
= lttng_trace_chunk_get(
55 if (!acquired_reference
) {
59 vstream
= zmalloc(sizeof(*vstream
));
61 PERROR("relay viewer stream zmalloc");
65 vstream
->stream_file
.trace_chunk
= viewer_trace_chunk
;
66 viewer_trace_chunk
= NULL
;
67 vstream
->path_name
= lttng_strndup(stream
->path_name
, LTTNG_VIEWER_PATH_MAX
);
68 if (vstream
->path_name
== NULL
) {
69 PERROR("relay viewer path_name alloc");
72 vstream
->channel_name
= lttng_strndup(stream
->channel_name
,
73 LTTNG_VIEWER_NAME_MAX
);
74 if (vstream
->channel_name
== NULL
) {
75 PERROR("relay viewer channel_name alloc");
79 if (!stream_get(stream
)) {
80 ERR("Cannot get stream");
83 vstream
->stream
= stream
;
85 pthread_mutex_lock(&stream
->lock
);
87 if (stream
->is_metadata
&& stream
->trace
->viewer_metadata_stream
) {
88 ERR("Cannot attach viewer metadata stream to trace (busy).");
93 case LTTNG_VIEWER_SEEK_BEGINNING
:
95 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
97 if (seq_tail
== -1ULL) {
99 * Tail may not be initialized yet. Nonetheless, we know
100 * we want to send the first index once it becomes
105 vstream
->current_tracefile_id
=
106 tracefile_array_get_file_index_tail(stream
->tfa
);
107 vstream
->index_sent_seqcount
= seq_tail
;
110 case LTTNG_VIEWER_SEEK_LAST
:
111 vstream
->current_tracefile_id
=
112 tracefile_array_get_read_file_index_head(stream
->tfa
);
114 * We seek at the very end of each stream, awaiting for
115 * a future packet to eventually come in.
117 * We don't need to check the head position for -1ULL since the
118 * increment will set it to 0.
120 vstream
->index_sent_seqcount
=
121 tracefile_array_get_seq_head(stream
->tfa
) + 1;
128 * If we never received an index for the current stream, delay
129 * the opening of the index, otherwise open it right now.
131 if (stream
->index_file
== NULL
) {
132 vstream
->index_file
= NULL
;
134 const uint32_t connection_major
= stream
->trace
->session
->major
;
135 const uint32_t connection_minor
= stream
->trace
->session
->minor
;
136 enum lttng_trace_chunk_status chunk_status
;
138 chunk_status
= lttng_index_file_create_from_trace_chunk_read_only(
139 vstream
->stream_file
.trace_chunk
,
141 stream
->channel_name
, stream
->tracefile_size
,
142 vstream
->current_tracefile_id
,
143 lttng_to_index_major(connection_major
,
145 lttng_to_index_minor(connection_major
,
147 true, &vstream
->index_file
);
148 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
149 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
150 vstream
->index_file
= NULL
;
158 * If we never received a data file for the current stream, delay the
159 * opening, otherwise open it right now.
163 char file_path
[LTTNG_PATH_MAX
];
164 enum lttng_trace_chunk_status status
;
166 ret
= utils_stream_file_path(stream
->path_name
,
167 stream
->channel_name
, stream
->tracefile_size
,
168 vstream
->current_tracefile_id
, NULL
, file_path
,
174 status
= lttng_trace_chunk_open_fs_handle(
175 vstream
->stream_file
.trace_chunk
, file_path
,
176 O_RDONLY
, 0, &vstream
->stream_file
.handle
,
178 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
183 if (seek_t
== LTTNG_VIEWER_SEEK_LAST
&& vstream
->index_file
) {
186 lseek_ret
= fs_handle_seek(
187 vstream
->index_file
->file
, 0, SEEK_END
);
192 if (stream
->is_metadata
) {
193 rcu_assign_pointer(stream
->trace
->viewer_metadata_stream
,
196 pthread_mutex_unlock(&stream
->lock
);
198 /* Globally visible after the add unique. */
199 lttng_ht_node_init_u64(&vstream
->stream_n
, stream
->stream_handle
);
200 urcu_ref_init(&vstream
->ref
);
201 lttng_ht_add_unique_u64(viewer_streams_ht
, &vstream
->stream_n
);
206 pthread_mutex_unlock(&stream
->lock
);
209 viewer_stream_destroy(vstream
);
211 if (viewer_trace_chunk
&& acquired_reference
) {
212 lttng_trace_chunk_put(viewer_trace_chunk
);
217 static void viewer_stream_unpublish(struct relay_viewer_stream
*vstream
)
220 struct lttng_ht_iter iter
;
222 iter
.iter
.node
= &vstream
->stream_n
.node
;
223 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
227 static void viewer_stream_release(struct urcu_ref
*ref
)
229 struct relay_viewer_stream
*vstream
= caa_container_of(ref
,
230 struct relay_viewer_stream
, ref
);
232 if (vstream
->stream
->is_metadata
) {
233 rcu_assign_pointer(vstream
->stream
->trace
->viewer_metadata_stream
, NULL
);
236 viewer_stream_unpublish(vstream
);
238 if (vstream
->stream_file
.handle
) {
239 fs_handle_close(vstream
->stream_file
.handle
);
240 vstream
->stream_file
.handle
= NULL
;
242 if (vstream
->index_file
) {
243 lttng_index_file_put(vstream
->index_file
);
244 vstream
->index_file
= NULL
;
246 if (vstream
->stream
) {
247 stream_put(vstream
->stream
);
248 vstream
->stream
= NULL
;
250 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
251 vstream
->stream_file
.trace_chunk
= NULL
;
252 call_rcu(&vstream
->rcu_node
, viewer_stream_destroy_rcu
);
255 /* Must be called with RCU read-side lock held. */
256 bool viewer_stream_get(struct relay_viewer_stream
*vstream
)
258 return urcu_ref_get_unless_zero(&vstream
->ref
);
262 * Get viewer stream by id.
264 * Return viewer stream if found else NULL.
266 struct relay_viewer_stream
*viewer_stream_get_by_id(uint64_t id
)
268 struct lttng_ht_node_u64
*node
;
269 struct lttng_ht_iter iter
;
270 struct relay_viewer_stream
*vstream
= NULL
;
273 lttng_ht_lookup(viewer_streams_ht
, &id
, &iter
);
274 node
= lttng_ht_iter_get_node_u64(&iter
);
276 DBG("Relay viewer stream %" PRIu64
" not found", id
);
279 vstream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
280 if (!viewer_stream_get(vstream
)) {
288 void viewer_stream_put(struct relay_viewer_stream
*vstream
)
291 urcu_ref_put(&vstream
->ref
, viewer_stream_release
);
295 void viewer_stream_close_files(struct relay_viewer_stream
*vstream
)
297 if (vstream
->index_file
) {
298 lttng_index_file_put(vstream
->index_file
);
299 vstream
->index_file
= NULL
;
301 if (vstream
->stream_file
.handle
) {
302 fs_handle_close(vstream
->stream_file
.handle
);
303 vstream
->stream_file
.handle
= NULL
;
307 void viewer_stream_sync_tracefile_array_tail(struct relay_viewer_stream
*vstream
)
309 const struct relay_stream
*stream
= vstream
->stream
;
312 vstream
->current_tracefile_id
= tracefile_array_get_file_index_tail(stream
->tfa
);
313 seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
314 if (seq_tail
== -1ULL) {
317 vstream
->index_sent_seqcount
= seq_tail
;
321 * Rotate a stream to the next tracefile.
323 * Must be called with the rstream lock held.
324 * Returns 0 on success, 1 on EOF.
326 int viewer_stream_rotate(struct relay_viewer_stream
*vstream
)
330 const struct relay_stream
*stream
= vstream
->stream
;
332 /* Detect the last tracefile to open. */
333 if (stream
->index_received_seqcount
334 == vstream
->index_sent_seqcount
335 && stream
->trace
->session
->connection_closed
) {
340 if (stream
->tracefile_count
== 0) {
341 /* Ignore rotation, there is none to do. */
347 * Try to move to the next file.
349 new_id
= (vstream
->current_tracefile_id
+ 1)
350 % stream
->tracefile_count
;
351 if (tracefile_array_seq_in_file(stream
->tfa
, new_id
,
352 vstream
->index_sent_seqcount
)) {
353 vstream
->current_tracefile_id
= new_id
;
355 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
358 * This can only be reached on overwrite, which implies there
359 * has been data written at some point, which will have set the
362 assert(seq_tail
!= -1ULL);
364 * We need to resync because we lag behind tail.
366 vstream
->current_tracefile_id
=
367 tracefile_array_get_file_index_tail(stream
->tfa
);
368 vstream
->index_sent_seqcount
= seq_tail
;
370 viewer_stream_close_files(vstream
);
376 void print_viewer_streams(void)
378 struct lttng_ht_iter iter
;
379 struct relay_viewer_stream
*vstream
;
381 if (!viewer_streams_ht
) {
386 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
388 if (!viewer_stream_get(vstream
)) {
391 DBG("vstream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
394 vstream
->ref
.refcount
,
395 vstream
->stream
->stream_handle
,
396 vstream
->stream
->trace
->id
,
397 vstream
->stream
->trace
->session
->id
);
398 viewer_stream_put(vstream
);