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