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