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 | ||
6c1c0768 | 20 | #define _LGPL_SOURCE |
2a174661 | 21 | #include <common/common.h> |
7591bab1 MD |
22 | #include <common/utils.h> |
23 | #include <common/defaults.h> | |
24 | #include <urcu/rculist.h> | |
25 | #include <sys/stat.h> | |
2a174661 | 26 | |
7591bab1 | 27 | #include "lttng-relayd.h" |
2a174661 DG |
28 | #include "index.h" |
29 | #include "stream.h" | |
30 | #include "viewer-stream.h" | |
31 | ||
7591bab1 MD |
32 | /* Should be called with RCU read-side lock held. */ |
33 | bool stream_get(struct relay_stream *stream) | |
34 | { | |
ce4d4083 | 35 | return urcu_ref_get_unless_zero(&stream->ref); |
7591bab1 MD |
36 | } |
37 | ||
2a174661 | 38 | /* |
7591bab1 MD |
39 | * Get stream from stream id from the streams hash table. Return stream |
40 | * if found else NULL. A stream reference is taken when a stream is | |
41 | * returned. stream_put() must be called on that stream. | |
2a174661 | 42 | */ |
7591bab1 | 43 | struct relay_stream *stream_get_by_id(uint64_t stream_id) |
2a174661 DG |
44 | { |
45 | struct lttng_ht_node_u64 *node; | |
46 | struct lttng_ht_iter iter; | |
47 | struct relay_stream *stream = NULL; | |
48 | ||
7591bab1 MD |
49 | rcu_read_lock(); |
50 | lttng_ht_lookup(relay_streams_ht, &stream_id, &iter); | |
2a174661 | 51 | node = lttng_ht_iter_get_node_u64(&iter); |
7591bab1 | 52 | if (!node) { |
2a174661 DG |
53 | DBG("Relay stream %" PRIu64 " not found", stream_id); |
54 | goto end; | |
55 | } | |
56 | stream = caa_container_of(node, struct relay_stream, node); | |
7591bab1 MD |
57 | if (!stream_get(stream)) { |
58 | stream = NULL; | |
59 | } | |
2a174661 | 60 | end: |
7591bab1 | 61 | rcu_read_unlock(); |
2a174661 DG |
62 | return stream; |
63 | } | |
64 | ||
65 | /* | |
7591bab1 | 66 | * We keep ownership of path_name and channel_name. |
2a174661 | 67 | */ |
7591bab1 MD |
68 | struct relay_stream *stream_create(struct ctf_trace *trace, |
69 | uint64_t stream_handle, char *path_name, | |
70 | char *channel_name, uint64_t tracefile_size, | |
81164b6b JG |
71 | uint64_t tracefile_count, |
72 | const struct relay_stream_chunk_id *chunk_id) | |
2a174661 | 73 | { |
7591bab1 MD |
74 | int ret; |
75 | struct relay_stream *stream = NULL; | |
76 | struct relay_session *session = trace->session; | |
2a174661 | 77 | |
7591bab1 MD |
78 | stream = zmalloc(sizeof(struct relay_stream)); |
79 | if (stream == NULL) { | |
80 | PERROR("relay stream zmalloc"); | |
7591bab1 MD |
81 | goto error_no_alloc; |
82 | } | |
2a174661 | 83 | |
7591bab1 MD |
84 | stream->stream_handle = stream_handle; |
85 | stream->prev_seq = -1ULL; | |
bda7c7b9 | 86 | stream->last_net_seq_num = -1ULL; |
7591bab1 MD |
87 | stream->ctf_stream_id = -1ULL; |
88 | stream->tracefile_size = tracefile_size; | |
89 | stream->tracefile_count = tracefile_count; | |
90 | stream->path_name = path_name; | |
91 | stream->channel_name = channel_name; | |
d3ecc550 | 92 | stream->rotate_at_seq_num = -1ULL; |
7591bab1 MD |
93 | lttng_ht_node_init_u64(&stream->node, stream->stream_handle); |
94 | pthread_mutex_init(&stream->lock, NULL); | |
7591bab1 MD |
95 | urcu_ref_init(&stream->ref); |
96 | ctf_trace_get(trace); | |
97 | stream->trace = trace; | |
81164b6b | 98 | stream->current_chunk_id = *chunk_id; |
2a174661 | 99 | |
7591bab1 MD |
100 | stream->indexes_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
101 | if (!stream->indexes_ht) { | |
102 | ERR("Cannot created indexes_ht"); | |
103 | ret = -1; | |
104 | goto end; | |
2a174661 DG |
105 | } |
106 | ||
7591bab1 MD |
107 | ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG, |
108 | -1, -1); | |
109 | if (ret < 0) { | |
110 | ERR("relay creating output directory"); | |
111 | goto end; | |
112 | } | |
2a174661 | 113 | |
7591bab1 MD |
114 | /* |
115 | * No need to use run_as API here because whatever we receive, | |
116 | * the relayd uses its own credentials for the stream files. | |
117 | */ | |
032932a6 JG |
118 | stream->stream_fd = stream_fd_create(stream->path_name, |
119 | stream->channel_name, stream->tracefile_size, 0, NULL); | |
7591bab1 MD |
120 | if (!stream->stream_fd) { |
121 | if (close(ret)) { | |
122 | PERROR("Error closing file %d", ret); | |
2a174661 | 123 | } |
7591bab1 MD |
124 | ret = -1; |
125 | goto end; | |
2a174661 | 126 | } |
a44ca2ca MD |
127 | stream->tfa = tracefile_array_create(stream->tracefile_count); |
128 | if (!stream->tfa) { | |
129 | ret = -1; | |
130 | goto end; | |
131 | } | |
7591bab1 MD |
132 | if (stream->tracefile_size) { |
133 | DBG("Tracefile %s/%s_0 created", stream->path_name, stream->channel_name); | |
134 | } else { | |
135 | DBG("Tracefile %s/%s created", stream->path_name, stream->channel_name); | |
136 | } | |
137 | ||
36d2e35d | 138 | if (!strncmp(stream->channel_name, DEFAULT_METADATA_NAME, LTTNG_NAME_MAX)) { |
7591bab1 MD |
139 | stream->is_metadata = 1; |
140 | } | |
141 | ||
142 | stream->in_recv_list = true; | |
143 | ||
144 | /* | |
145 | * Add the stream in the recv list of the session. Once the end stream | |
146 | * message is received, all session streams are published. | |
147 | */ | |
148 | pthread_mutex_lock(&session->recv_list_lock); | |
149 | cds_list_add_rcu(&stream->recv_node, &session->recv_list); | |
150 | session->stream_count++; | |
151 | pthread_mutex_unlock(&session->recv_list_lock); | |
152 | ||
153 | /* | |
154 | * Both in the ctf_trace object and the global stream ht since the data | |
155 | * side of the relayd does not have the concept of session. | |
156 | */ | |
157 | lttng_ht_add_unique_u64(relay_streams_ht, &stream->node); | |
77f7bd85 | 158 | stream->in_stream_ht = true; |
2a174661 | 159 | |
7591bab1 MD |
160 | DBG("Relay new stream added %s with ID %" PRIu64, stream->channel_name, |
161 | stream->stream_handle); | |
162 | ret = 0; | |
163 | ||
164 | end: | |
165 | if (ret) { | |
166 | if (stream->stream_fd) { | |
167 | stream_fd_put(stream->stream_fd); | |
168 | stream->stream_fd = NULL; | |
2a174661 | 169 | } |
7591bab1 MD |
170 | stream_put(stream); |
171 | stream = NULL; | |
2a174661 | 172 | } |
7591bab1 | 173 | return stream; |
2a174661 | 174 | |
7591bab1 MD |
175 | error_no_alloc: |
176 | /* | |
177 | * path_name and channel_name need to be freed explicitly here | |
178 | * because we cannot rely on stream_put(). | |
179 | */ | |
180 | free(path_name); | |
181 | free(channel_name); | |
182 | return NULL; | |
183 | } | |
184 | ||
185 | /* | |
186 | * Called with the session lock held. | |
187 | */ | |
188 | void stream_publish(struct relay_stream *stream) | |
189 | { | |
190 | struct relay_session *session; | |
191 | ||
192 | pthread_mutex_lock(&stream->lock); | |
193 | if (stream->published) { | |
194 | goto unlock; | |
2a174661 DG |
195 | } |
196 | ||
7591bab1 | 197 | session = stream->trace->session; |
2a174661 | 198 | |
7591bab1 MD |
199 | pthread_mutex_lock(&session->recv_list_lock); |
200 | if (stream->in_recv_list) { | |
201 | cds_list_del_rcu(&stream->recv_node); | |
202 | stream->in_recv_list = false; | |
203 | } | |
204 | pthread_mutex_unlock(&session->recv_list_lock); | |
2a174661 | 205 | |
7591bab1 MD |
206 | pthread_mutex_lock(&stream->trace->stream_list_lock); |
207 | cds_list_add_rcu(&stream->stream_node, &stream->trace->stream_list); | |
208 | pthread_mutex_unlock(&stream->trace->stream_list_lock); | |
2a174661 | 209 | |
7591bab1 MD |
210 | stream->published = true; |
211 | unlock: | |
2a174661 | 212 | pthread_mutex_unlock(&stream->lock); |
2a174661 DG |
213 | } |
214 | ||
7591bab1 | 215 | /* |
77f7bd85 | 216 | * Stream must be protected by holding the stream lock or by virtue of being |
ce4d4083 | 217 | * called from stream_destroy. |
7591bab1 MD |
218 | */ |
219 | static void stream_unpublish(struct relay_stream *stream) | |
2a174661 | 220 | { |
77f7bd85 MD |
221 | if (stream->in_stream_ht) { |
222 | struct lttng_ht_iter iter; | |
223 | int ret; | |
224 | ||
225 | iter.iter.node = &stream->node.node; | |
226 | ret = lttng_ht_del(relay_streams_ht, &iter); | |
227 | assert(!ret); | |
228 | stream->in_stream_ht = false; | |
229 | } | |
230 | if (stream->published) { | |
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 | stream->published = false; | |
7591bab1 | 235 | } |
7591bab1 MD |
236 | } |
237 | ||
238 | static void stream_destroy(struct relay_stream *stream) | |
239 | { | |
240 | if (stream->indexes_ht) { | |
49e614cb MD |
241 | /* |
242 | * Calling lttng_ht_destroy in call_rcu worker thread so | |
243 | * we don't hold the RCU read-side lock while calling | |
244 | * it. | |
245 | */ | |
7591bab1 MD |
246 | lttng_ht_destroy(stream->indexes_ht); |
247 | } | |
a44ca2ca MD |
248 | if (stream->tfa) { |
249 | tracefile_array_destroy(stream->tfa); | |
250 | } | |
7591bab1 MD |
251 | free(stream->path_name); |
252 | free(stream->channel_name); | |
253 | free(stream); | |
254 | } | |
255 | ||
256 | static void stream_destroy_rcu(struct rcu_head *rcu_head) | |
257 | { | |
258 | struct relay_stream *stream = | |
259 | caa_container_of(rcu_head, struct relay_stream, rcu_node); | |
260 | ||
261 | stream_destroy(stream); | |
262 | } | |
263 | ||
264 | /* | |
265 | * No need to take stream->lock since this is only called on the final | |
266 | * stream_put which ensures that a single thread may act on the stream. | |
7591bab1 MD |
267 | */ |
268 | static void stream_release(struct urcu_ref *ref) | |
269 | { | |
270 | struct relay_stream *stream = | |
271 | caa_container_of(ref, struct relay_stream, ref); | |
272 | struct relay_session *session; | |
2a174661 | 273 | |
7591bab1 MD |
274 | session = stream->trace->session; |
275 | ||
276 | DBG("Releasing stream id %" PRIu64, stream->stream_handle); | |
277 | ||
278 | pthread_mutex_lock(&session->recv_list_lock); | |
279 | session->stream_count--; | |
280 | if (stream->in_recv_list) { | |
281 | cds_list_del_rcu(&stream->recv_node); | |
282 | stream->in_recv_list = false; | |
283 | } | |
284 | pthread_mutex_unlock(&session->recv_list_lock); | |
2a174661 | 285 | |
7591bab1 MD |
286 | stream_unpublish(stream); |
287 | ||
288 | if (stream->stream_fd) { | |
289 | stream_fd_put(stream->stream_fd); | |
290 | stream->stream_fd = NULL; | |
291 | } | |
f8f3885c | 292 | if (stream->index_file) { |
2b508613 | 293 | relay_index_file_put(stream->index_file); |
f8f3885c | 294 | stream->index_file = NULL; |
7591bab1 MD |
295 | } |
296 | if (stream->trace) { | |
297 | ctf_trace_put(stream->trace); | |
298 | stream->trace = NULL; | |
299 | } | |
300 | ||
301 | call_rcu(&stream->rcu_node, stream_destroy_rcu); | |
2a174661 DG |
302 | } |
303 | ||
7591bab1 | 304 | void stream_put(struct relay_stream *stream) |
2a174661 | 305 | { |
7591bab1 | 306 | DBG("stream put for stream id %" PRIu64, stream->stream_handle); |
7591bab1 | 307 | rcu_read_lock(); |
7591bab1 MD |
308 | assert(stream->ref.refcount != 0); |
309 | /* | |
310 | * Wait until we have processed all the stream packets before | |
311 | * actually putting our last stream reference. | |
312 | */ | |
313 | DBG("stream put stream id %" PRIu64 " refcount %d", | |
314 | stream->stream_handle, | |
315 | (int) stream->ref.refcount); | |
316 | urcu_ref_put(&stream->ref, stream_release); | |
7591bab1 MD |
317 | rcu_read_unlock(); |
318 | } | |
319 | ||
bda7c7b9 | 320 | void try_stream_close(struct relay_stream *stream) |
7591bab1 | 321 | { |
98ba050e JR |
322 | bool session_aborted; |
323 | struct relay_session *session = stream->trace->session; | |
324 | ||
bda7c7b9 | 325 | DBG("Trying to close stream %" PRIu64, stream->stream_handle); |
98ba050e JR |
326 | |
327 | pthread_mutex_lock(&session->lock); | |
328 | session_aborted = session->aborted; | |
329 | pthread_mutex_unlock(&session->lock); | |
330 | ||
7591bab1 | 331 | pthread_mutex_lock(&stream->lock); |
bda7c7b9 JG |
332 | /* |
333 | * Can be called concurently by connection close and reception of last | |
334 | * pending data. | |
335 | */ | |
336 | if (stream->closed) { | |
337 | pthread_mutex_unlock(&stream->lock); | |
338 | DBG("closing stream %" PRIu64 " aborted since it is already marked as closed", stream->stream_handle); | |
339 | return; | |
340 | } | |
341 | ||
342 | stream->close_requested = true; | |
3d07a857 MD |
343 | |
344 | if (stream->last_net_seq_num == -1ULL) { | |
345 | /* | |
346 | * Handle connection close without explicit stream close | |
347 | * command. | |
348 | * | |
349 | * We can be clever about indexes partially received in | |
350 | * cases where we received the data socket part, but not | |
351 | * the control socket part: since we're currently closing | |
352 | * the stream on behalf of the control socket, we *know* | |
353 | * there won't be any more control information for this | |
354 | * socket. Therefore, we can destroy all indexes for | |
355 | * which we have received only the file descriptor (from | |
356 | * data socket). This takes care of consumerd crashes | |
357 | * between sending the data and control information for | |
358 | * a packet. Since those are sent in that order, we take | |
359 | * care of consumerd crashes. | |
360 | */ | |
5312a3ed | 361 | DBG("relay_index_close_partial_fd"); |
3d07a857 MD |
362 | relay_index_close_partial_fd(stream); |
363 | /* | |
364 | * Use the highest net_seq_num we currently have pending | |
365 | * As end of stream indicator. Leave last_net_seq_num | |
366 | * at -1ULL if we cannot find any index. | |
367 | */ | |
368 | stream->last_net_seq_num = relay_index_find_last(stream); | |
5312a3ed | 369 | DBG("Updating stream->last_net_seq_num to %" PRIu64, stream->last_net_seq_num); |
3d07a857 MD |
370 | /* Fall-through into the next check. */ |
371 | } | |
372 | ||
bda7c7b9 | 373 | if (stream->last_net_seq_num != -1ULL && |
98ba050e JR |
374 | ((int64_t) (stream->prev_seq - stream->last_net_seq_num)) < 0 |
375 | && !session_aborted) { | |
3d07a857 MD |
376 | /* |
377 | * Don't close since we still have data pending. This | |
378 | * handles cases where an explicit close command has | |
379 | * been received for this stream, and cases where the | |
380 | * connection has been closed, and we are awaiting for | |
381 | * index information from the data socket. It is | |
382 | * therefore expected that all the index fd information | |
383 | * we need has already been received on the control | |
384 | * socket. Matching index information from data socket | |
385 | * should be Expected Soon(TM). | |
386 | * | |
387 | * TODO: We should implement a timer to garbage collect | |
388 | * streams after a timeout to be resilient against a | |
389 | * consumerd implementation that would not match this | |
390 | * expected behavior. | |
391 | */ | |
bda7c7b9 JG |
392 | pthread_mutex_unlock(&stream->lock); |
393 | DBG("closing stream %" PRIu64 " aborted since it still has data pending", stream->stream_handle); | |
394 | return; | |
395 | } | |
3d07a857 MD |
396 | /* |
397 | * We received all the indexes we can expect. | |
398 | */ | |
77f7bd85 | 399 | stream_unpublish(stream); |
2229a09c | 400 | stream->closed = true; |
bda7c7b9 | 401 | /* Relay indexes are only used by the "consumer/sessiond" end. */ |
7591bab1 MD |
402 | relay_index_close_all(stream); |
403 | pthread_mutex_unlock(&stream->lock); | |
bda7c7b9 | 404 | DBG("Succeeded in closing stream %" PRIu64, stream->stream_handle); |
7591bab1 MD |
405 | stream_put(stream); |
406 | } | |
407 | ||
da412cde MD |
408 | static void print_stream_indexes(struct relay_stream *stream) |
409 | { | |
410 | struct lttng_ht_iter iter; | |
411 | struct relay_index *index; | |
412 | ||
413 | rcu_read_lock(); | |
414 | cds_lfht_for_each_entry(stream->indexes_ht->ht, &iter.iter, index, | |
415 | index_n.node) { | |
416 | DBG("index %p net_seq_num %" PRIu64 " refcount %ld" | |
417 | " stream %" PRIu64 " trace %" PRIu64 | |
418 | " session %" PRIu64, | |
419 | index, | |
420 | index->index_n.key, | |
421 | stream->ref.refcount, | |
422 | index->stream->stream_handle, | |
423 | index->stream->trace->id, | |
424 | index->stream->trace->session->id); | |
425 | } | |
426 | rcu_read_unlock(); | |
427 | } | |
428 | ||
7591bab1 MD |
429 | void print_relay_streams(void) |
430 | { | |
431 | struct lttng_ht_iter iter; | |
432 | struct relay_stream *stream; | |
433 | ||
ce3f3ba3 JG |
434 | if (!relay_streams_ht) { |
435 | return; | |
436 | } | |
437 | ||
7591bab1 MD |
438 | rcu_read_lock(); |
439 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, | |
440 | node.node) { | |
441 | if (!stream_get(stream)) { | |
442 | continue; | |
443 | } | |
444 | DBG("stream %p refcount %ld stream %" PRIu64 " trace %" PRIu64 | |
445 | " session %" PRIu64, | |
446 | stream, | |
447 | stream->ref.refcount, | |
448 | stream->stream_handle, | |
449 | stream->trace->id, | |
450 | stream->trace->session->id); | |
da412cde | 451 | print_stream_indexes(stream); |
7591bab1 MD |
452 | stream_put(stream); |
453 | } | |
454 | rcu_read_unlock(); | |
2a174661 | 455 | } |