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 | 84 | stream->stream_handle = stream_handle; |
a8f9f353 | 85 | stream->prev_data_seq = -1ULL; |
7a45c7e6 | 86 | stream->prev_index_seq = -1ULL; |
bda7c7b9 | 87 | stream->last_net_seq_num = -1ULL; |
7591bab1 MD |
88 | stream->ctf_stream_id = -1ULL; |
89 | stream->tracefile_size = tracefile_size; | |
90 | stream->tracefile_count = tracefile_count; | |
91 | stream->path_name = path_name; | |
92 | stream->channel_name = channel_name; | |
d3ecc550 | 93 | stream->rotate_at_seq_num = -1ULL; |
7591bab1 MD |
94 | lttng_ht_node_init_u64(&stream->node, stream->stream_handle); |
95 | pthread_mutex_init(&stream->lock, NULL); | |
7591bab1 MD |
96 | urcu_ref_init(&stream->ref); |
97 | ctf_trace_get(trace); | |
98 | stream->trace = trace; | |
81164b6b | 99 | stream->current_chunk_id = *chunk_id; |
2a174661 | 100 | |
7591bab1 MD |
101 | stream->indexes_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
102 | if (!stream->indexes_ht) { | |
103 | ERR("Cannot created indexes_ht"); | |
104 | ret = -1; | |
105 | goto end; | |
2a174661 DG |
106 | } |
107 | ||
7591bab1 MD |
108 | ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG, |
109 | -1, -1); | |
110 | if (ret < 0) { | |
111 | ERR("relay creating output directory"); | |
112 | goto end; | |
113 | } | |
2a174661 | 114 | |
7591bab1 MD |
115 | /* |
116 | * No need to use run_as API here because whatever we receive, | |
117 | * the relayd uses its own credentials for the stream files. | |
118 | */ | |
119 | ret = utils_create_stream_file(stream->path_name, stream->channel_name, | |
120 | stream->tracefile_size, 0, -1, -1, NULL); | |
121 | if (ret < 0) { | |
122 | ERR("Create output file"); | |
123 | goto end; | |
124 | } | |
125 | stream->stream_fd = stream_fd_create(ret); | |
126 | if (!stream->stream_fd) { | |
127 | if (close(ret)) { | |
128 | PERROR("Error closing file %d", ret); | |
2a174661 | 129 | } |
7591bab1 MD |
130 | ret = -1; |
131 | goto end; | |
2a174661 | 132 | } |
a44ca2ca MD |
133 | stream->tfa = tracefile_array_create(stream->tracefile_count); |
134 | if (!stream->tfa) { | |
135 | ret = -1; | |
136 | goto end; | |
137 | } | |
7591bab1 MD |
138 | if (stream->tracefile_size) { |
139 | DBG("Tracefile %s/%s_0 created", stream->path_name, stream->channel_name); | |
140 | } else { | |
141 | DBG("Tracefile %s/%s created", stream->path_name, stream->channel_name); | |
142 | } | |
143 | ||
36d2e35d | 144 | if (!strncmp(stream->channel_name, DEFAULT_METADATA_NAME, LTTNG_NAME_MAX)) { |
7591bab1 MD |
145 | stream->is_metadata = 1; |
146 | } | |
147 | ||
148 | stream->in_recv_list = true; | |
149 | ||
150 | /* | |
151 | * Add the stream in the recv list of the session. Once the end stream | |
152 | * message is received, all session streams are published. | |
153 | */ | |
154 | pthread_mutex_lock(&session->recv_list_lock); | |
155 | cds_list_add_rcu(&stream->recv_node, &session->recv_list); | |
156 | session->stream_count++; | |
157 | pthread_mutex_unlock(&session->recv_list_lock); | |
158 | ||
159 | /* | |
160 | * Both in the ctf_trace object and the global stream ht since the data | |
161 | * side of the relayd does not have the concept of session. | |
162 | */ | |
163 | lttng_ht_add_unique_u64(relay_streams_ht, &stream->node); | |
77f7bd85 | 164 | stream->in_stream_ht = true; |
2a174661 | 165 | |
7591bab1 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 | } |
7591bab1 MD |
176 | stream_put(stream); |
177 | stream = NULL; | |
2a174661 | 178 | } |
7591bab1 | 179 | return stream; |
2a174661 | 180 | |
7591bab1 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 | ||
7591bab1 | 203 | session = stream->trace->session; |
2a174661 | 204 | |
7591bab1 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 | |
7591bab1 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 | |
7591bab1 MD |
216 | stream->published = true; |
217 | unlock: | |
2a174661 | 218 | pthread_mutex_unlock(&stream->lock); |
2a174661 DG |
219 | } |
220 | ||
7591bab1 | 221 | /* |
77f7bd85 | 222 | * Stream must be protected by holding the stream lock or by virtue of being |
ce4d4083 | 223 | * called from stream_destroy. |
7591bab1 MD |
224 | */ |
225 | static void stream_unpublish(struct relay_stream *stream) | |
2a174661 | 226 | { |
77f7bd85 MD |
227 | if (stream->in_stream_ht) { |
228 | struct lttng_ht_iter iter; | |
229 | int ret; | |
230 | ||
231 | iter.iter.node = &stream->node.node; | |
232 | ret = lttng_ht_del(relay_streams_ht, &iter); | |
233 | assert(!ret); | |
234 | stream->in_stream_ht = false; | |
235 | } | |
236 | 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); | |
240 | stream->published = false; | |
7591bab1 | 241 | } |
7591bab1 MD |
242 | } |
243 | ||
244 | static void stream_destroy(struct relay_stream *stream) | |
245 | { | |
246 | if (stream->indexes_ht) { | |
49e614cb MD |
247 | /* |
248 | * Calling lttng_ht_destroy in call_rcu worker thread so | |
249 | * we don't hold the RCU read-side lock while calling | |
250 | * it. | |
251 | */ | |
7591bab1 MD |
252 | lttng_ht_destroy(stream->indexes_ht); |
253 | } | |
a44ca2ca MD |
254 | if (stream->tfa) { |
255 | tracefile_array_destroy(stream->tfa); | |
256 | } | |
7591bab1 MD |
257 | free(stream->path_name); |
258 | free(stream->channel_name); | |
259 | free(stream); | |
260 | } | |
261 | ||
262 | static void stream_destroy_rcu(struct rcu_head *rcu_head) | |
263 | { | |
264 | struct relay_stream *stream = | |
265 | caa_container_of(rcu_head, struct relay_stream, rcu_node); | |
266 | ||
267 | stream_destroy(stream); | |
268 | } | |
269 | ||
270 | /* | |
271 | * No need to take stream->lock since this is only called on the final | |
272 | * stream_put which ensures that a single thread may act on the stream. | |
7591bab1 MD |
273 | */ |
274 | static void stream_release(struct urcu_ref *ref) | |
275 | { | |
276 | struct relay_stream *stream = | |
277 | caa_container_of(ref, struct relay_stream, ref); | |
278 | struct relay_session *session; | |
2a174661 | 279 | |
7591bab1 MD |
280 | session = stream->trace->session; |
281 | ||
282 | DBG("Releasing stream id %" PRIu64, stream->stream_handle); | |
283 | ||
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; | |
289 | } | |
290 | pthread_mutex_unlock(&session->recv_list_lock); | |
2a174661 | 291 | |
7591bab1 MD |
292 | stream_unpublish(stream); |
293 | ||
294 | if (stream->stream_fd) { | |
295 | stream_fd_put(stream->stream_fd); | |
296 | stream->stream_fd = NULL; | |
297 | } | |
f8f3885c MD |
298 | if (stream->index_file) { |
299 | lttng_index_file_put(stream->index_file); | |
300 | stream->index_file = NULL; | |
7591bab1 MD |
301 | } |
302 | if (stream->trace) { | |
303 | ctf_trace_put(stream->trace); | |
304 | stream->trace = NULL; | |
305 | } | |
306 | ||
307 | call_rcu(&stream->rcu_node, stream_destroy_rcu); | |
2a174661 DG |
308 | } |
309 | ||
7591bab1 | 310 | void stream_put(struct relay_stream *stream) |
2a174661 | 311 | { |
7591bab1 | 312 | DBG("stream put for stream id %" PRIu64, stream->stream_handle); |
7591bab1 | 313 | rcu_read_lock(); |
7591bab1 MD |
314 | assert(stream->ref.refcount != 0); |
315 | /* | |
316 | * Wait until we have processed all the stream packets before | |
317 | * actually putting our last stream reference. | |
318 | */ | |
319 | DBG("stream put stream id %" PRIu64 " refcount %d", | |
320 | stream->stream_handle, | |
321 | (int) stream->ref.refcount); | |
322 | urcu_ref_put(&stream->ref, stream_release); | |
7591bab1 MD |
323 | rcu_read_unlock(); |
324 | } | |
325 | ||
bda7c7b9 | 326 | void try_stream_close(struct relay_stream *stream) |
7591bab1 | 327 | { |
98ba050e JR |
328 | bool session_aborted; |
329 | struct relay_session *session = stream->trace->session; | |
330 | ||
bda7c7b9 | 331 | DBG("Trying to close stream %" PRIu64, stream->stream_handle); |
98ba050e JR |
332 | |
333 | pthread_mutex_lock(&session->lock); | |
334 | session_aborted = session->aborted; | |
335 | pthread_mutex_unlock(&session->lock); | |
336 | ||
7591bab1 | 337 | pthread_mutex_lock(&stream->lock); |
bda7c7b9 JG |
338 | /* |
339 | * Can be called concurently by connection close and reception of last | |
340 | * pending data. | |
341 | */ | |
342 | if (stream->closed) { | |
343 | pthread_mutex_unlock(&stream->lock); | |
344 | DBG("closing stream %" PRIu64 " aborted since it is already marked as closed", stream->stream_handle); | |
345 | return; | |
346 | } | |
347 | ||
348 | stream->close_requested = true; | |
3d07a857 MD |
349 | |
350 | if (stream->last_net_seq_num == -1ULL) { | |
351 | /* | |
352 | * Handle connection close without explicit stream close | |
353 | * command. | |
354 | * | |
355 | * We can be clever about indexes partially received in | |
356 | * cases where we received the data socket part, but not | |
357 | * the control socket part: since we're currently closing | |
358 | * the stream on behalf of the control socket, we *know* | |
359 | * there won't be any more control information for this | |
360 | * socket. Therefore, we can destroy all indexes for | |
361 | * which we have received only the file descriptor (from | |
362 | * data socket). This takes care of consumerd crashes | |
363 | * between sending the data and control information for | |
364 | * a packet. Since those are sent in that order, we take | |
365 | * care of consumerd crashes. | |
366 | */ | |
5312a3ed | 367 | DBG("relay_index_close_partial_fd"); |
3d07a857 MD |
368 | relay_index_close_partial_fd(stream); |
369 | /* | |
370 | * Use the highest net_seq_num we currently have pending | |
371 | * As end of stream indicator. Leave last_net_seq_num | |
372 | * at -1ULL if we cannot find any index. | |
373 | */ | |
374 | stream->last_net_seq_num = relay_index_find_last(stream); | |
5312a3ed | 375 | DBG("Updating stream->last_net_seq_num to %" PRIu64, stream->last_net_seq_num); |
3d07a857 MD |
376 | /* Fall-through into the next check. */ |
377 | } | |
378 | ||
bda7c7b9 | 379 | if (stream->last_net_seq_num != -1ULL && |
a8f9f353 | 380 | ((int64_t) (stream->prev_data_seq - stream->last_net_seq_num)) < 0 |
98ba050e | 381 | && !session_aborted) { |
3d07a857 MD |
382 | /* |
383 | * Don't close since we still have data pending. This | |
384 | * handles cases where an explicit close command has | |
385 | * been received for this stream, and cases where the | |
386 | * connection has been closed, and we are awaiting for | |
387 | * index information from the data socket. It is | |
388 | * therefore expected that all the index fd information | |
389 | * we need has already been received on the control | |
390 | * socket. Matching index information from data socket | |
391 | * should be Expected Soon(TM). | |
392 | * | |
393 | * TODO: We should implement a timer to garbage collect | |
394 | * streams after a timeout to be resilient against a | |
395 | * consumerd implementation that would not match this | |
396 | * expected behavior. | |
397 | */ | |
bda7c7b9 JG |
398 | pthread_mutex_unlock(&stream->lock); |
399 | DBG("closing stream %" PRIu64 " aborted since it still has data pending", stream->stream_handle); | |
400 | return; | |
401 | } | |
3d07a857 MD |
402 | /* |
403 | * We received all the indexes we can expect. | |
404 | */ | |
77f7bd85 | 405 | stream_unpublish(stream); |
2229a09c | 406 | stream->closed = true; |
bda7c7b9 | 407 | /* Relay indexes are only used by the "consumer/sessiond" end. */ |
7591bab1 MD |
408 | relay_index_close_all(stream); |
409 | pthread_mutex_unlock(&stream->lock); | |
bda7c7b9 | 410 | DBG("Succeeded in closing stream %" PRIu64, stream->stream_handle); |
7591bab1 MD |
411 | stream_put(stream); |
412 | } | |
413 | ||
da412cde MD |
414 | static void print_stream_indexes(struct relay_stream *stream) |
415 | { | |
416 | struct lttng_ht_iter iter; | |
417 | struct relay_index *index; | |
418 | ||
419 | rcu_read_lock(); | |
420 | cds_lfht_for_each_entry(stream->indexes_ht->ht, &iter.iter, index, | |
421 | index_n.node) { | |
422 | DBG("index %p net_seq_num %" PRIu64 " refcount %ld" | |
423 | " stream %" PRIu64 " trace %" PRIu64 | |
424 | " session %" PRIu64, | |
425 | index, | |
426 | index->index_n.key, | |
427 | stream->ref.refcount, | |
428 | index->stream->stream_handle, | |
429 | index->stream->trace->id, | |
430 | index->stream->trace->session->id); | |
431 | } | |
432 | rcu_read_unlock(); | |
433 | } | |
434 | ||
7591bab1 MD |
435 | void print_relay_streams(void) |
436 | { | |
437 | struct lttng_ht_iter iter; | |
438 | struct relay_stream *stream; | |
439 | ||
ce3f3ba3 JG |
440 | if (!relay_streams_ht) { |
441 | return; | |
442 | } | |
443 | ||
7591bab1 MD |
444 | rcu_read_lock(); |
445 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, | |
446 | node.node) { | |
447 | if (!stream_get(stream)) { | |
448 | continue; | |
449 | } | |
450 | DBG("stream %p refcount %ld stream %" PRIu64 " trace %" PRIu64 | |
451 | " session %" PRIu64, | |
452 | stream, | |
453 | stream->ref.refcount, | |
454 | stream->stream_handle, | |
455 | stream->trace->id, | |
456 | stream->trace->session->id); | |
da412cde | 457 | print_stream_indexes(stream); |
7591bab1 MD |
458 | stream_put(stream); |
459 | } | |
460 | rcu_read_unlock(); | |
2a174661 | 461 | } |