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/utils.h>
24 #include <common/defaults.h>
25 #include <urcu/rculist.h>
28 #include "lttng-relayd.h"
31 #include "viewer-stream.h"
33 /* Should be called with RCU read-side lock held. */
34 bool stream_get(struct relay_stream
*stream
)
38 pthread_mutex_lock(&stream
->reflock
);
39 if (stream
->ref
.refcount
!= 0) {
41 urcu_ref_get(&stream
->ref
);
43 pthread_mutex_unlock(&stream
->reflock
);
49 * Get stream from stream id from the streams hash table. Return stream
50 * if found else NULL. A stream reference is taken when a stream is
51 * returned. stream_put() must be called on that stream.
53 struct relay_stream
*stream_get_by_id(uint64_t stream_id
)
55 struct lttng_ht_node_u64
*node
;
56 struct lttng_ht_iter iter
;
57 struct relay_stream
*stream
= NULL
;
60 lttng_ht_lookup(relay_streams_ht
, &stream_id
, &iter
);
61 node
= lttng_ht_iter_get_node_u64(&iter
);
63 DBG("Relay stream %" PRIu64
" not found", stream_id
);
66 stream
= caa_container_of(node
, struct relay_stream
, node
);
67 if (!stream_get(stream
)) {
76 * We keep ownership of path_name and channel_name.
78 struct relay_stream
*stream_create(struct ctf_trace
*trace
,
79 uint64_t stream_handle
, char *path_name
,
80 char *channel_name
, uint64_t tracefile_size
,
81 uint64_t tracefile_count
)
84 struct relay_stream
*stream
= NULL
;
85 struct relay_session
*session
= trace
->session
;
87 stream
= zmalloc(sizeof(struct relay_stream
));
89 PERROR("relay stream zmalloc");
94 stream
->stream_handle
= stream_handle
;
95 stream
->prev_seq
= -1ULL;
96 stream
->ctf_stream_id
= -1ULL;
97 stream
->tracefile_size
= tracefile_size
;
98 stream
->tracefile_count
= tracefile_count
;
99 stream
->path_name
= path_name
;
100 stream
->channel_name
= channel_name
;
101 lttng_ht_node_init_u64(&stream
->node
, stream
->stream_handle
);
102 pthread_mutex_init(&stream
->lock
, NULL
);
103 pthread_mutex_init(&stream
->reflock
, NULL
);
104 urcu_ref_init(&stream
->ref
);
105 ctf_trace_get(trace
);
106 stream
->trace
= trace
;
108 stream
->indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
109 if (!stream
->indexes_ht
) {
110 ERR("Cannot created indexes_ht");
115 ret
= utils_mkdir_recursive(stream
->path_name
, S_IRWXU
| S_IRWXG
,
118 ERR("relay creating output directory");
123 * No need to use run_as API here because whatever we receive,
124 * the relayd uses its own credentials for the stream files.
126 ret
= utils_create_stream_file(stream
->path_name
, stream
->channel_name
,
127 stream
->tracefile_size
, 0, -1, -1, NULL
);
129 ERR("Create output file");
132 stream
->stream_fd
= stream_fd_create(ret
);
133 if (!stream
->stream_fd
) {
135 PERROR("Error closing file %d", ret
);
140 stream
->tfa
= tracefile_array_create(stream
->tracefile_count
);
145 if (stream
->tracefile_size
) {
146 DBG("Tracefile %s/%s_0 created", stream
->path_name
, stream
->channel_name
);
148 DBG("Tracefile %s/%s created", stream
->path_name
, stream
->channel_name
);
151 if (!strncmp(stream
->channel_name
, DEFAULT_METADATA_NAME
, NAME_MAX
)) {
152 stream
->is_metadata
= 1;
155 stream
->in_recv_list
= true;
158 * Add the stream in the recv list of the session. Once the end stream
159 * message is received, all session streams are published.
161 pthread_mutex_lock(&session
->recv_list_lock
);
162 cds_list_add_rcu(&stream
->recv_node
, &session
->recv_list
);
163 session
->stream_count
++;
164 pthread_mutex_unlock(&session
->recv_list_lock
);
167 * Both in the ctf_trace object and the global stream ht since the data
168 * side of the relayd does not have the concept of session.
170 lttng_ht_add_unique_u64(relay_streams_ht
, &stream
->node
);
172 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
173 stream
->stream_handle
);
178 if (stream
->stream_fd
) {
179 stream_fd_put(stream
->stream_fd
);
180 stream
->stream_fd
= NULL
;
189 * path_name and channel_name need to be freed explicitly here
190 * because we cannot rely on stream_put().
198 * Called with the session lock held.
200 void stream_publish(struct relay_stream
*stream
)
202 struct relay_session
*session
;
204 pthread_mutex_lock(&stream
->lock
);
205 if (stream
->published
) {
209 session
= stream
->trace
->session
;
211 pthread_mutex_lock(&session
->recv_list_lock
);
212 if (stream
->in_recv_list
) {
213 cds_list_del_rcu(&stream
->recv_node
);
214 stream
->in_recv_list
= false;
216 pthread_mutex_unlock(&session
->recv_list_lock
);
218 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
219 cds_list_add_rcu(&stream
->stream_node
, &stream
->trace
->stream_list
);
220 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
222 stream
->published
= true;
224 pthread_mutex_unlock(&stream
->lock
);
228 * Only called from destroy. No stream lock needed, since there is a
229 * single user at this point. This is ensured by having the refcount
232 static void stream_unpublish(struct relay_stream
*stream
)
234 if (!stream
->published
) {
237 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
238 cds_list_del_rcu(&stream
->stream_node
);
239 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
241 stream
->published
= false;
244 static void stream_destroy(struct relay_stream
*stream
)
246 if (stream
->indexes_ht
) {
247 lttng_ht_destroy(stream
->indexes_ht
);
250 tracefile_array_destroy(stream
->tfa
);
252 free(stream
->path_name
);
253 free(stream
->channel_name
);
257 static void stream_destroy_rcu(struct rcu_head
*rcu_head
)
259 struct relay_stream
*stream
=
260 caa_container_of(rcu_head
, struct relay_stream
, rcu_node
);
262 stream_destroy(stream
);
266 * No need to take stream->lock since this is only called on the final
267 * stream_put which ensures that a single thread may act on the stream.
269 * At that point, the object is also protected by the reflock which
270 * guarantees that no other thread may share ownership of this stream.
272 static void stream_release(struct urcu_ref
*ref
)
274 struct relay_stream
*stream
=
275 caa_container_of(ref
, struct relay_stream
, ref
);
276 struct relay_session
*session
;
278 struct lttng_ht_iter iter
;
280 session
= stream
->trace
->session
;
282 DBG("Releasing stream id %" PRIu64
, stream
->stream_handle
);
284 pthread_mutex_lock(&session
->recv_list_lock
);
285 session
->stream_count
--;
286 if (stream
->in_recv_list
) {
287 cds_list_del_rcu(&stream
->recv_node
);
288 stream
->in_recv_list
= false;
290 pthread_mutex_unlock(&session
->recv_list_lock
);
292 iter
.iter
.node
= &stream
->node
.node
;
293 ret
= lttng_ht_del(relay_streams_ht
, &iter
);
296 stream_unpublish(stream
);
298 if (stream
->stream_fd
) {
299 stream_fd_put(stream
->stream_fd
);
300 stream
->stream_fd
= NULL
;
302 if (stream
->index_fd
) {
303 stream_fd_put(stream
->index_fd
);
304 stream
->index_fd
= NULL
;
307 ctf_trace_put(stream
->trace
);
308 stream
->trace
= NULL
;
311 call_rcu(&stream
->rcu_node
, stream_destroy_rcu
);
314 void stream_put(struct relay_stream
*stream
)
316 DBG("stream put for stream id %" PRIu64
, stream
->stream_handle
);
318 * Ensure existence of stream->reflock for stream unlock.
322 * Stream reflock ensures that concurrent test and update of
323 * stream ref is atomic.
325 pthread_mutex_lock(&stream
->reflock
);
326 assert(stream
->ref
.refcount
!= 0);
328 * Wait until we have processed all the stream packets before
329 * actually putting our last stream reference.
331 DBG("stream put stream id %" PRIu64
" refcount %d",
332 stream
->stream_handle
,
333 (int) stream
->ref
.refcount
);
334 urcu_ref_put(&stream
->ref
, stream_release
);
335 pthread_mutex_unlock(&stream
->reflock
);
339 void stream_close(struct relay_stream
*stream
)
341 DBG("closing stream %" PRIu64
, stream
->stream_handle
);
342 pthread_mutex_lock(&stream
->lock
);
343 stream
->closed
= true;
344 relay_index_close_all(stream
);
345 pthread_mutex_unlock(&stream
->lock
);
349 void print_relay_streams(void)
351 struct lttng_ht_iter iter
;
352 struct relay_stream
*stream
;
355 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
357 if (!stream_get(stream
)) {
360 DBG("stream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
363 stream
->ref
.refcount
,
364 stream
->stream_handle
,
366 stream
->trace
->session
->id
);
This page took 0.046113 seconds and 5 git commands to generate.