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.
161 if (stream
->stream_fd
) {
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_file(
175 vstream
->stream_file
.trace_chunk
,
176 file_path
, O_RDONLY
, 0, &fd
, true);
177 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
180 vstream
->stream_file
.fd
= stream_fd_create(fd
);
181 if (!vstream
->stream_file
.fd
) {
183 PERROR("Failed to close viewer %sfile",
184 stream
->is_metadata
? "metadata " : "");
190 if (seek_t
== LTTNG_VIEWER_SEEK_LAST
&& vstream
->index_file
) {
193 lseek_ret
= lseek(vstream
->index_file
->fd
, 0, SEEK_END
);
198 if (stream
->is_metadata
) {
199 rcu_assign_pointer(stream
->trace
->viewer_metadata_stream
,
202 pthread_mutex_unlock(&stream
->lock
);
204 /* Globally visible after the add unique. */
205 lttng_ht_node_init_u64(&vstream
->stream_n
, stream
->stream_handle
);
206 urcu_ref_init(&vstream
->ref
);
207 lttng_ht_add_unique_u64(viewer_streams_ht
, &vstream
->stream_n
);
212 pthread_mutex_unlock(&stream
->lock
);
215 viewer_stream_destroy(vstream
);
217 if (viewer_trace_chunk
&& acquired_reference
) {
218 lttng_trace_chunk_put(viewer_trace_chunk
);
223 static void viewer_stream_unpublish(struct relay_viewer_stream
*vstream
)
226 struct lttng_ht_iter iter
;
228 iter
.iter
.node
= &vstream
->stream_n
.node
;
229 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
233 static void viewer_stream_release(struct urcu_ref
*ref
)
235 struct relay_viewer_stream
*vstream
= caa_container_of(ref
,
236 struct relay_viewer_stream
, ref
);
238 if (vstream
->stream
->is_metadata
) {
239 rcu_assign_pointer(vstream
->stream
->trace
->viewer_metadata_stream
, NULL
);
242 viewer_stream_unpublish(vstream
);
244 if (vstream
->stream_file
.fd
) {
245 stream_fd_put(vstream
->stream_file
.fd
);
246 vstream
->stream_file
.fd
= NULL
;
248 if (vstream
->index_file
) {
249 lttng_index_file_put(vstream
->index_file
);
250 vstream
->index_file
= NULL
;
252 if (vstream
->stream
) {
253 stream_put(vstream
->stream
);
254 vstream
->stream
= NULL
;
256 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
257 vstream
->stream_file
.trace_chunk
= NULL
;
258 call_rcu(&vstream
->rcu_node
, viewer_stream_destroy_rcu
);
261 /* Must be called with RCU read-side lock held. */
262 bool viewer_stream_get(struct relay_viewer_stream
*vstream
)
264 return urcu_ref_get_unless_zero(&vstream
->ref
);
268 * Get viewer stream by id.
270 * Return viewer stream if found else NULL.
272 struct relay_viewer_stream
*viewer_stream_get_by_id(uint64_t id
)
274 struct lttng_ht_node_u64
*node
;
275 struct lttng_ht_iter iter
;
276 struct relay_viewer_stream
*vstream
= NULL
;
279 lttng_ht_lookup(viewer_streams_ht
, &id
, &iter
);
280 node
= lttng_ht_iter_get_node_u64(&iter
);
282 DBG("Relay viewer stream %" PRIu64
" not found", id
);
285 vstream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
286 if (!viewer_stream_get(vstream
)) {
294 void viewer_stream_put(struct relay_viewer_stream
*vstream
)
297 urcu_ref_put(&vstream
->ref
, viewer_stream_release
);
301 void viewer_stream_close_files(struct relay_viewer_stream
*vstream
)
303 if (vstream
->index_file
) {
304 lttng_index_file_put(vstream
->index_file
);
305 vstream
->index_file
= NULL
;
307 if (vstream
->stream_file
.fd
) {
308 stream_fd_put(vstream
->stream_file
.fd
);
309 vstream
->stream_file
.fd
= NULL
;
313 void viewer_stream_sync_tracefile_array_tail(struct relay_viewer_stream
*vstream
)
315 const struct relay_stream
*stream
= vstream
->stream
;
318 vstream
->current_tracefile_id
= tracefile_array_get_file_index_tail(stream
->tfa
);
319 seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
320 if (seq_tail
== -1ULL) {
323 vstream
->index_sent_seqcount
= seq_tail
;
327 * Rotate a stream to the next tracefile.
329 * Must be called with the rstream lock held.
330 * Returns 0 on success, 1 on EOF.
332 int viewer_stream_rotate(struct relay_viewer_stream
*vstream
)
336 const struct relay_stream
*stream
= vstream
->stream
;
338 /* Detect the last tracefile to open. */
339 if (stream
->index_received_seqcount
340 == vstream
->index_sent_seqcount
341 && stream
->trace
->session
->connection_closed
) {
346 if (stream
->tracefile_count
== 0) {
347 /* Ignore rotation, there is none to do. */
353 * Try to move to the next file.
355 new_id
= (vstream
->current_tracefile_id
+ 1)
356 % stream
->tracefile_count
;
357 if (tracefile_array_seq_in_file(stream
->tfa
, new_id
,
358 vstream
->index_sent_seqcount
)) {
359 vstream
->current_tracefile_id
= new_id
;
361 uint64_t seq_tail
= tracefile_array_get_seq_tail(stream
->tfa
);
364 * This can only be reached on overwrite, which implies there
365 * has been data written at some point, which will have set the
368 assert(seq_tail
!= -1ULL);
370 * We need to resync because we lag behind tail.
372 vstream
->current_tracefile_id
=
373 tracefile_array_get_file_index_tail(stream
->tfa
);
374 vstream
->index_sent_seqcount
= seq_tail
;
376 viewer_stream_close_files(vstream
);
382 void print_viewer_streams(void)
384 struct lttng_ht_iter iter
;
385 struct relay_viewer_stream
*vstream
;
387 if (!viewer_streams_ht
) {
392 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
394 if (!viewer_stream_get(vstream
)) {
397 DBG("vstream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
400 vstream
->ref
.refcount
,
401 vstream
->stream
->stream_handle
,
402 vstream
->stream
->trace
->id
,
403 vstream
->stream
->trace
->session
->id
);
404 viewer_stream_put(vstream
);