Commit | Line | Data |
---|---|---|
2a174661 DG |
1 | /* |
2 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> | |
3 | * David Goulet <dgoulet@efficios.com> | |
7591bab1 | 4 | * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2a174661 DG |
5 | * |
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. | |
9 | * | |
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 | |
13 | * more details. | |
14 | * | |
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. | |
18 | */ | |
19 | ||
20 | #define _GNU_SOURCE | |
6c1c0768 | 21 | #define _LGPL_SOURCE |
2a174661 | 22 | #include <common/common.h> |
7591bab1 MD |
23 | #include <common/utils.h> |
24 | #include <common/defaults.h> | |
25 | #include <urcu/rculist.h> | |
26 | #include <sys/stat.h> | |
2a174661 | 27 | |
7591bab1 | 28 | #include "lttng-relayd.h" |
2a174661 DG |
29 | #include "index.h" |
30 | #include "stream.h" | |
31 | #include "viewer-stream.h" | |
32 | ||
7591bab1 MD |
33 | /* Should be called with RCU read-side lock held. */ |
34 | bool stream_get(struct relay_stream *stream) | |
35 | { | |
36 | bool has_ref = false; | |
37 | ||
38 | pthread_mutex_lock(&stream->reflock); | |
39 | if (stream->ref.refcount != 0) { | |
40 | has_ref = true; | |
41 | urcu_ref_get(&stream->ref); | |
42 | } | |
43 | pthread_mutex_unlock(&stream->reflock); | |
44 | ||
45 | return has_ref; | |
46 | } | |
47 | ||
2a174661 | 48 | /* |
7591bab1 MD |
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. | |
2a174661 | 52 | */ |
7591bab1 | 53 | struct relay_stream *stream_get_by_id(uint64_t stream_id) |
2a174661 DG |
54 | { |
55 | struct lttng_ht_node_u64 *node; | |
56 | struct lttng_ht_iter iter; | |
57 | struct relay_stream *stream = NULL; | |
58 | ||
7591bab1 MD |
59 | rcu_read_lock(); |
60 | lttng_ht_lookup(relay_streams_ht, &stream_id, &iter); | |
2a174661 | 61 | node = lttng_ht_iter_get_node_u64(&iter); |
7591bab1 | 62 | if (!node) { |
2a174661 DG |
63 | DBG("Relay stream %" PRIu64 " not found", stream_id); |
64 | goto end; | |
65 | } | |
66 | stream = caa_container_of(node, struct relay_stream, node); | |
7591bab1 MD |
67 | if (!stream_get(stream)) { |
68 | stream = NULL; | |
69 | } | |
2a174661 | 70 | end: |
7591bab1 | 71 | rcu_read_unlock(); |
2a174661 DG |
72 | return stream; |
73 | } | |
74 | ||
75 | /* | |
7591bab1 | 76 | * We keep ownership of path_name and channel_name. |
2a174661 | 77 | */ |
7591bab1 MD |
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) | |
2a174661 | 82 | { |
7591bab1 MD |
83 | int ret; |
84 | struct relay_stream *stream = NULL; | |
85 | struct relay_session *session = trace->session; | |
2a174661 | 86 | |
7591bab1 MD |
87 | stream = zmalloc(sizeof(struct relay_stream)); |
88 | if (stream == NULL) { | |
89 | PERROR("relay stream zmalloc"); | |
90 | ret = -1; | |
91 | goto error_no_alloc; | |
92 | } | |
2a174661 | 93 | |
7591bab1 MD |
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; | |
2a174661 | 107 | |
7591bab1 MD |
108 | stream->indexes_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
109 | if (!stream->indexes_ht) { | |
110 | ERR("Cannot created indexes_ht"); | |
111 | ret = -1; | |
112 | goto end; | |
2a174661 DG |
113 | } |
114 | ||
7591bab1 MD |
115 | ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG, |
116 | -1, -1); | |
117 | if (ret < 0) { | |
118 | ERR("relay creating output directory"); | |
119 | goto end; | |
120 | } | |
2a174661 | 121 | |
7591bab1 MD |
122 | /* |
123 | * No need to use run_as API here because whatever we receive, | |
124 | * the relayd uses its own credentials for the stream files. | |
125 | */ | |
126 | ret = utils_create_stream_file(stream->path_name, stream->channel_name, | |
127 | stream->tracefile_size, 0, -1, -1, NULL); | |
128 | if (ret < 0) { | |
129 | ERR("Create output file"); | |
130 | goto end; | |
131 | } | |
132 | stream->stream_fd = stream_fd_create(ret); | |
133 | if (!stream->stream_fd) { | |
134 | if (close(ret)) { | |
135 | PERROR("Error closing file %d", ret); | |
2a174661 | 136 | } |
7591bab1 MD |
137 | ret = -1; |
138 | goto end; | |
2a174661 | 139 | } |
7591bab1 MD |
140 | if (stream->tracefile_size) { |
141 | DBG("Tracefile %s/%s_0 created", stream->path_name, stream->channel_name); | |
142 | } else { | |
143 | DBG("Tracefile %s/%s created", stream->path_name, stream->channel_name); | |
144 | } | |
145 | ||
146 | if (!strncmp(stream->channel_name, DEFAULT_METADATA_NAME, NAME_MAX)) { | |
147 | stream->is_metadata = 1; | |
148 | } | |
149 | ||
150 | stream->in_recv_list = true; | |
151 | ||
152 | /* | |
153 | * Add the stream in the recv list of the session. Once the end stream | |
154 | * message is received, all session streams are published. | |
155 | */ | |
156 | pthread_mutex_lock(&session->recv_list_lock); | |
157 | cds_list_add_rcu(&stream->recv_node, &session->recv_list); | |
158 | session->stream_count++; | |
159 | pthread_mutex_unlock(&session->recv_list_lock); | |
160 | ||
161 | /* | |
162 | * Both in the ctf_trace object and the global stream ht since the data | |
163 | * side of the relayd does not have the concept of session. | |
164 | */ | |
165 | lttng_ht_add_unique_u64(relay_streams_ht, &stream->node); | |
2a174661 | 166 | |
7591bab1 MD |
167 | DBG("Relay new stream added %s with ID %" PRIu64, stream->channel_name, |
168 | stream->stream_handle); | |
169 | ret = 0; | |
170 | ||
171 | end: | |
172 | if (ret) { | |
173 | if (stream->stream_fd) { | |
174 | stream_fd_put(stream->stream_fd); | |
175 | stream->stream_fd = NULL; | |
2a174661 | 176 | } |
7591bab1 MD |
177 | stream_put(stream); |
178 | stream = NULL; | |
2a174661 | 179 | } |
7591bab1 | 180 | return stream; |
2a174661 | 181 | |
7591bab1 MD |
182 | error_no_alloc: |
183 | /* | |
184 | * path_name and channel_name need to be freed explicitly here | |
185 | * because we cannot rely on stream_put(). | |
186 | */ | |
187 | free(path_name); | |
188 | free(channel_name); | |
189 | return NULL; | |
190 | } | |
191 | ||
192 | /* | |
193 | * Called with the session lock held. | |
194 | */ | |
195 | void stream_publish(struct relay_stream *stream) | |
196 | { | |
197 | struct relay_session *session; | |
198 | ||
199 | pthread_mutex_lock(&stream->lock); | |
200 | if (stream->published) { | |
201 | goto unlock; | |
2a174661 DG |
202 | } |
203 | ||
7591bab1 | 204 | session = stream->trace->session; |
2a174661 | 205 | |
7591bab1 MD |
206 | pthread_mutex_lock(&session->recv_list_lock); |
207 | if (stream->in_recv_list) { | |
208 | cds_list_del_rcu(&stream->recv_node); | |
209 | stream->in_recv_list = false; | |
210 | } | |
211 | pthread_mutex_unlock(&session->recv_list_lock); | |
2a174661 | 212 | |
7591bab1 MD |
213 | pthread_mutex_lock(&stream->trace->stream_list_lock); |
214 | cds_list_add_rcu(&stream->stream_node, &stream->trace->stream_list); | |
215 | pthread_mutex_unlock(&stream->trace->stream_list_lock); | |
2a174661 | 216 | |
7591bab1 MD |
217 | stream->published = true; |
218 | unlock: | |
2a174661 | 219 | pthread_mutex_unlock(&stream->lock); |
2a174661 DG |
220 | } |
221 | ||
7591bab1 MD |
222 | /* |
223 | * Only called from destroy. No stream lock needed, since there is a | |
224 | * single user at this point. This is ensured by having the refcount | |
225 | * reaching 0. | |
226 | */ | |
227 | static void stream_unpublish(struct relay_stream *stream) | |
2a174661 | 228 | { |
7591bab1 MD |
229 | if (!stream->published) { |
230 | return; | |
231 | } | |
232 | pthread_mutex_lock(&stream->trace->stream_list_lock); | |
233 | cds_list_del_rcu(&stream->stream_node); | |
234 | pthread_mutex_unlock(&stream->trace->stream_list_lock); | |
235 | ||
236 | stream->published = false; | |
237 | } | |
238 | ||
239 | static void stream_destroy(struct relay_stream *stream) | |
240 | { | |
241 | if (stream->indexes_ht) { | |
242 | lttng_ht_destroy(stream->indexes_ht); | |
243 | } | |
244 | free(stream->path_name); | |
245 | free(stream->channel_name); | |
246 | free(stream); | |
247 | } | |
248 | ||
249 | static void stream_destroy_rcu(struct rcu_head *rcu_head) | |
250 | { | |
251 | struct relay_stream *stream = | |
252 | caa_container_of(rcu_head, struct relay_stream, rcu_node); | |
253 | ||
254 | stream_destroy(stream); | |
255 | } | |
256 | ||
257 | /* | |
258 | * No need to take stream->lock since this is only called on the final | |
259 | * stream_put which ensures that a single thread may act on the stream. | |
260 | * | |
261 | * At that point, the object is also protected by the reflock which | |
262 | * guarantees that no other thread may share ownership of this stream. | |
263 | */ | |
264 | static void stream_release(struct urcu_ref *ref) | |
265 | { | |
266 | struct relay_stream *stream = | |
267 | caa_container_of(ref, struct relay_stream, ref); | |
268 | struct relay_session *session; | |
2a174661 DG |
269 | int ret; |
270 | struct lttng_ht_iter iter; | |
271 | ||
7591bab1 MD |
272 | session = stream->trace->session; |
273 | ||
274 | DBG("Releasing stream id %" PRIu64, stream->stream_handle); | |
275 | ||
276 | pthread_mutex_lock(&session->recv_list_lock); | |
277 | session->stream_count--; | |
278 | if (stream->in_recv_list) { | |
279 | cds_list_del_rcu(&stream->recv_node); | |
280 | stream->in_recv_list = false; | |
281 | } | |
282 | pthread_mutex_unlock(&session->recv_list_lock); | |
2a174661 DG |
283 | |
284 | iter.iter.node = &stream->node.node; | |
7591bab1 | 285 | ret = lttng_ht_del(relay_streams_ht, &iter); |
2a174661 DG |
286 | assert(!ret); |
287 | ||
7591bab1 MD |
288 | stream_unpublish(stream); |
289 | ||
290 | if (stream->stream_fd) { | |
291 | stream_fd_put(stream->stream_fd); | |
292 | stream->stream_fd = NULL; | |
293 | } | |
294 | if (stream->index_fd) { | |
295 | stream_fd_put(stream->index_fd); | |
296 | stream->index_fd = NULL; | |
297 | } | |
298 | if (stream->trace) { | |
299 | ctf_trace_put(stream->trace); | |
300 | stream->trace = NULL; | |
301 | } | |
302 | ||
303 | call_rcu(&stream->rcu_node, stream_destroy_rcu); | |
2a174661 DG |
304 | } |
305 | ||
7591bab1 | 306 | void stream_put(struct relay_stream *stream) |
2a174661 | 307 | { |
7591bab1 MD |
308 | DBG("stream put for stream id %" PRIu64, stream->stream_handle); |
309 | /* | |
310 | * Ensure existence of stream->reflock for stream unlock. | |
311 | */ | |
312 | rcu_read_lock(); | |
313 | /* | |
314 | * Stream reflock ensures that concurrent test and update of | |
315 | * stream ref is atomic. | |
316 | */ | |
317 | pthread_mutex_lock(&stream->reflock); | |
318 | assert(stream->ref.refcount != 0); | |
319 | /* | |
320 | * Wait until we have processed all the stream packets before | |
321 | * actually putting our last stream reference. | |
322 | */ | |
323 | DBG("stream put stream id %" PRIu64 " refcount %d", | |
324 | stream->stream_handle, | |
325 | (int) stream->ref.refcount); | |
326 | urcu_ref_put(&stream->ref, stream_release); | |
327 | pthread_mutex_unlock(&stream->reflock); | |
328 | rcu_read_unlock(); | |
329 | } | |
330 | ||
331 | void stream_close(struct relay_stream *stream) | |
332 | { | |
333 | DBG("closing stream %" PRIu64, stream->stream_handle); | |
334 | pthread_mutex_lock(&stream->lock); | |
335 | relay_index_close_all(stream); | |
336 | pthread_mutex_unlock(&stream->lock); | |
337 | stream_put(stream); | |
338 | } | |
339 | ||
340 | void print_relay_streams(void) | |
341 | { | |
342 | struct lttng_ht_iter iter; | |
343 | struct relay_stream *stream; | |
344 | ||
345 | rcu_read_lock(); | |
346 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, | |
347 | node.node) { | |
348 | if (!stream_get(stream)) { | |
349 | continue; | |
350 | } | |
351 | DBG("stream %p refcount %ld stream %" PRIu64 " trace %" PRIu64 | |
352 | " session %" PRIu64, | |
353 | stream, | |
354 | stream->ref.refcount, | |
355 | stream->stream_handle, | |
356 | stream->trace->id, | |
357 | stream->trace->session->id); | |
358 | stream_put(stream); | |
359 | } | |
360 | rcu_read_unlock(); | |
2a174661 | 361 | } |