Fix: consumer: unbalanced RCU read-side lock on error
[lttng-tools.git] / src / common / consumer / consumer-stream.c
CommitLineData
51230d70
DG
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
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
51230d70 21#include <assert.h>
10a50311 22#include <inttypes.h>
51230d70
DG
23#include <sys/mman.h>
24#include <unistd.h>
25
26#include <common/common.h>
1c20f0e2 27#include <common/index/index.h>
94d49140 28#include <common/kernel-consumer/kernel-consumer.h>
51230d70
DG
29#include <common/relayd/relayd.h>
30#include <common/ust-consumer/ust-consumer.h>
a2361a61 31#include <common/utils.h>
6f1177cf
JG
32#include <common/consumer/consumer.h>
33#include <common/consumer/consumer-timer.h>
8d18bcae 34#include <common/consumer/metadata-bucket.h>
51230d70
DG
35
36#include "consumer-stream.h"
37
38/*
39 * RCU call to free stream. MUST only be used with call_rcu().
40 */
41static void free_stream_rcu(struct rcu_head *head)
42{
43 struct lttng_ht_node_u64 *node =
44 caa_container_of(head, struct lttng_ht_node_u64, head);
45 struct lttng_consumer_stream *stream =
46 caa_container_of(node, struct lttng_consumer_stream, node);
47
48 pthread_mutex_destroy(&stream->lock);
49 free(stream);
50}
51
6f1177cf
JG
52static void consumer_stream_data_lock_all(struct lttng_consumer_stream *stream)
53{
54 pthread_mutex_lock(&stream->chan->lock);
55 pthread_mutex_lock(&stream->lock);
56}
57
58static void consumer_stream_data_unlock_all(struct lttng_consumer_stream *stream)
59{
60 pthread_mutex_unlock(&stream->lock);
61 pthread_mutex_unlock(&stream->chan->lock);
62}
63
64static void consumer_stream_metadata_lock_all(struct lttng_consumer_stream *stream)
65{
66 consumer_stream_data_lock_all(stream);
67 pthread_mutex_lock(&stream->metadata_rdv_lock);
68}
69
70static void consumer_stream_metadata_unlock_all(struct lttng_consumer_stream *stream)
71{
72 pthread_mutex_unlock(&stream->metadata_rdv_lock);
73 consumer_stream_data_unlock_all(stream);
74}
75
76/* Only used for data streams. */
77static int consumer_stream_update_stats(struct lttng_consumer_stream *stream,
78 const struct stream_subbuffer *subbuf)
79{
80 int ret = 0;
81 uint64_t sequence_number;
ec8b7679 82 const uint64_t discarded_events = subbuf->info.data.events_discarded;
6f1177cf
JG
83
84 if (!subbuf->info.data.sequence_number.is_set) {
85 /* Command not supported by the tracer. */
86 sequence_number = -1ULL;
87 stream->sequence_number_unavailable = true;
88 } else {
89 sequence_number = subbuf->info.data.sequence_number.value;
90 }
91
92 /*
93 * Start the sequence when we extract the first packet in case we don't
94 * start at 0 (for example if a consumer is not connected to the
95 * session immediately after the beginning).
96 */
97 if (stream->last_sequence_number == -1ULL) {
98 stream->last_sequence_number = sequence_number;
99 } else if (sequence_number > stream->last_sequence_number) {
100 stream->chan->lost_packets += sequence_number -
101 stream->last_sequence_number - 1;
102 } else {
103 /* seq <= last_sequence_number */
104 ERR("Sequence number inconsistent : prev = %" PRIu64
105 ", current = %" PRIu64,
106 stream->last_sequence_number, sequence_number);
107 ret = -1;
108 goto end;
109 }
110 stream->last_sequence_number = sequence_number;
111
112 if (discarded_events < stream->last_discarded_events) {
113 /*
114 * Overflow has occurred. We assume only one wrap-around
115 * has occurred.
116 */
117 stream->chan->discarded_events +=
118 (1ULL << (CAA_BITS_PER_LONG - 1)) -
119 stream->last_discarded_events +
120 discarded_events;
121 } else {
122 stream->chan->discarded_events += discarded_events -
123 stream->last_discarded_events;
124 }
125 stream->last_discarded_events = discarded_events;
126 ret = 0;
127
128end:
129 return ret;
130}
131
132static
133void ctf_packet_index_populate(struct ctf_packet_index *index,
134 off_t offset, const struct stream_subbuffer *subbuffer)
135{
136 *index = (typeof(*index)){
137 .offset = htobe64(offset),
138 .packet_size = htobe64(subbuffer->info.data.packet_size),
139 .content_size = htobe64(subbuffer->info.data.content_size),
140 .timestamp_begin = htobe64(
141 subbuffer->info.data.timestamp_begin),
142 .timestamp_end = htobe64(
143 subbuffer->info.data.timestamp_end),
144 .events_discarded = htobe64(
145 subbuffer->info.data.events_discarded),
146 .stream_id = htobe64(subbuffer->info.data.stream_id),
147 .stream_instance_id = htobe64(
148 subbuffer->info.data.stream_instance_id.is_set ?
149 subbuffer->info.data.stream_instance_id.value : -1ULL),
150 .packet_seq_num = htobe64(
151 subbuffer->info.data.sequence_number.is_set ?
152 subbuffer->info.data.sequence_number.value : -1ULL),
153 };
154}
155
156static ssize_t consumer_stream_consume_mmap(
157 struct lttng_consumer_local_data *ctx,
158 struct lttng_consumer_stream *stream,
159 const struct stream_subbuffer *subbuffer)
160{
161 const unsigned long padding_size =
162 subbuffer->info.data.padded_subbuf_size -
163 subbuffer->info.data.subbuf_size;
917cd4fd 164 const ssize_t written_bytes = lttng_consumer_on_read_subbuffer_mmap(
8d18bcae 165 stream, &subbuffer->buffer.buffer, padding_size);
917cd4fd
FD
166
167 if (stream->net_seq_idx == -1ULL) {
168 /*
169 * When writing on disk, check that only the subbuffer (no
170 * padding) was written to disk.
171 */
172 if (written_bytes != subbuffer->info.data.padded_subbuf_size) {
173 DBG("Failed to write the entire padded subbuffer on disk (written_bytes: %zd, padded subbuffer size %lu)",
174 written_bytes,
175 subbuffer->info.data.padded_subbuf_size);
176 }
177 } else {
178 /*
179 * When streaming over the network, check that the entire
180 * subbuffer including padding was successfully written.
181 */
182 if (written_bytes != subbuffer->info.data.subbuf_size) {
183 DBG("Failed to write only the subbuffer over the network (written_bytes: %zd, subbuffer size %lu)",
184 written_bytes,
185 subbuffer->info.data.subbuf_size);
186 }
187 }
188
189 /*
190 * If `lttng_consumer_on_read_subbuffer_mmap()` returned an error, pass
191 * it along to the caller, else return zero.
192 */
193 if (written_bytes < 0) {
194 ERR("Error reading mmap subbuffer: %zd", written_bytes);
195 }
196
197 return written_bytes;
6f1177cf
JG
198}
199
200static ssize_t consumer_stream_consume_splice(
201 struct lttng_consumer_local_data *ctx,
202 struct lttng_consumer_stream *stream,
203 const struct stream_subbuffer *subbuffer)
204{
917cd4fd
FD
205 const ssize_t written_bytes = lttng_consumer_on_read_subbuffer_splice(
206 ctx, stream, subbuffer->info.data.padded_subbuf_size, 0);
207
208 if (written_bytes != subbuffer->info.data.padded_subbuf_size) {
209 DBG("Failed to write the entire padded subbuffer (written_bytes: %zd, padded subbuffer size %lu)",
210 written_bytes,
211 subbuffer->info.data.padded_subbuf_size);
212 }
213
214 /*
215 * If `lttng_consumer_on_read_subbuffer_splice()` returned an error,
216 * pass it along to the caller, else return zero.
217 */
218 if (written_bytes < 0) {
219 ERR("Error reading splice subbuffer: %zd", written_bytes);
220 }
221
222 return written_bytes;
6f1177cf
JG
223}
224
225static int consumer_stream_send_index(
226 struct lttng_consumer_stream *stream,
227 const struct stream_subbuffer *subbuffer,
228 struct lttng_consumer_local_data *ctx)
229{
230 off_t packet_offset = 0;
231 struct ctf_packet_index index = {};
232
233 /*
234 * This is called after consuming the sub-buffer; substract the
235 * effect this sub-buffer from the offset.
236 */
237 if (stream->net_seq_idx == (uint64_t) -1ULL) {
238 packet_offset = stream->out_fd_offset -
239 subbuffer->info.data.padded_subbuf_size;
240 }
241
242 ctf_packet_index_populate(&index, packet_offset, subbuffer);
243 return consumer_stream_write_index(stream, &index);
244}
245
246/*
247 * Actually do the metadata sync using the given metadata stream.
248 *
249 * Return 0 on success else a negative value. ENODATA can be returned also
250 * indicating that there is no metadata available for that stream.
251 */
252static int do_sync_metadata(struct lttng_consumer_stream *metadata,
253 struct lttng_consumer_local_data *ctx)
254{
255 int ret;
27e2fa5d 256 enum sync_metadata_status status;
6f1177cf
JG
257
258 assert(metadata);
259 assert(metadata->metadata_flag);
260 assert(ctx);
261
262 /*
263 * In UST, since we have to write the metadata from the cache packet
264 * by packet, we might need to start this procedure multiple times
265 * until all the metadata from the cache has been extracted.
266 */
267 do {
268 /*
269 * Steps :
270 * - Lock the metadata stream
271 * - Check if metadata stream node was deleted before locking.
272 * - if yes, release and return success
273 * - Check if new metadata is ready (flush + snapshot pos)
274 * - If nothing : release and return.
275 * - Lock the metadata_rdv_lock
276 * - Unlock the metadata stream
277 * - cond_wait on metadata_rdv to wait the wakeup from the
278 * metadata thread
279 * - Unlock the metadata_rdv_lock
280 */
281 pthread_mutex_lock(&metadata->lock);
282
283 /*
284 * There is a possibility that we were able to acquire a reference on the
285 * stream from the RCU hash table but between then and now, the node might
286 * have been deleted just before the lock is acquired. Thus, after locking,
287 * we make sure the metadata node has not been deleted which means that the
288 * buffers are closed.
289 *
290 * In that case, there is no need to sync the metadata hence returning a
291 * success return code.
292 */
293 ret = cds_lfht_is_node_deleted(&metadata->node.node);
294 if (ret) {
295 ret = 0;
296 goto end_unlock_mutex;
297 }
298
299 switch (ctx->type) {
300 case LTTNG_CONSUMER_KERNEL:
301 /*
302 * Empty the metadata cache and flush the current stream.
303 */
27e2fa5d 304 status = lttng_kconsumer_sync_metadata(metadata);
6f1177cf
JG
305 break;
306 case LTTNG_CONSUMER32_UST:
307 case LTTNG_CONSUMER64_UST:
308 /*
309 * Ask the sessiond if we have new metadata waiting and update the
310 * consumer metadata cache.
311 */
27e2fa5d 312 status = lttng_ustconsumer_sync_metadata(ctx, metadata);
6f1177cf
JG
313 break;
314 default:
27e2fa5d 315 abort();
6f1177cf 316 }
27e2fa5d
JG
317
318 switch (status) {
319 case SYNC_METADATA_STATUS_NEW_DATA:
320 break;
321 case SYNC_METADATA_STATUS_NO_DATA:
322 ret = 0;
6f1177cf 323 goto end_unlock_mutex;
27e2fa5d
JG
324 case SYNC_METADATA_STATUS_ERROR:
325 ret = -1;
326 goto end_unlock_mutex;
327 default:
328 abort();
6f1177cf
JG
329 }
330
331 /*
332 * At this point, new metadata have been flushed, so we wait on the
333 * rendez-vous point for the metadata thread to wake us up when it
334 * finishes consuming the metadata and continue execution.
335 */
336
337 pthread_mutex_lock(&metadata->metadata_rdv_lock);
338
339 /*
340 * Release metadata stream lock so the metadata thread can process it.
341 */
342 pthread_mutex_unlock(&metadata->lock);
343
344 /*
345 * Wait on the rendez-vous point. Once woken up, it means the metadata was
346 * consumed and thus synchronization is achieved.
347 */
348 pthread_cond_wait(&metadata->metadata_rdv, &metadata->metadata_rdv_lock);
349 pthread_mutex_unlock(&metadata->metadata_rdv_lock);
27e2fa5d 350 } while (status == SYNC_METADATA_STATUS_NEW_DATA);
6f1177cf
JG
351
352 /* Success */
353 return 0;
354
355end_unlock_mutex:
356 pthread_mutex_unlock(&metadata->lock);
357 return ret;
358}
359
360/*
361 * Synchronize the metadata using a given session ID. A successful acquisition
362 * of a metadata stream will trigger a request to the session daemon and a
363 * snapshot so the metadata thread can consume it.
364 *
365 * This function call is a rendez-vous point between the metadata thread and
366 * the data thread.
367 *
368 * Return 0 on success or else a negative value.
369 */
370int consumer_stream_sync_metadata(struct lttng_consumer_local_data *ctx,
371 uint64_t session_id)
372{
373 int ret;
374 struct lttng_consumer_stream *stream = NULL;
375 struct lttng_ht_iter iter;
376 struct lttng_ht *ht;
377
378 assert(ctx);
379
380 /* Ease our life a bit. */
381 ht = consumer_data.stream_list_ht;
382
383 rcu_read_lock();
384
385 /* Search the metadata associated with the session id of the given stream. */
386
387 cds_lfht_for_each_entry_duplicate(ht->ht,
388 ht->hash_fct(&session_id, lttng_ht_seed), ht->match_fct,
389 &session_id, &iter.iter, stream, node_session_id.node) {
390 if (!stream->metadata_flag) {
391 continue;
392 }
393
394 ret = do_sync_metadata(stream, ctx);
395 if (ret < 0) {
396 goto end;
397 }
398 }
399
400 /*
401 * Force return code to 0 (success) since ret might be ENODATA for instance
402 * which is not an error but rather that we should come back.
403 */
404 ret = 0;
405
406end:
407 rcu_read_unlock();
408 return ret;
409}
410
411static int consumer_stream_sync_metadata_index(
412 struct lttng_consumer_stream *stream,
413 const struct stream_subbuffer *subbuffer,
414 struct lttng_consumer_local_data *ctx)
415{
416 int ret;
417
418 /* Block until all the metadata is sent. */
419 pthread_mutex_lock(&stream->metadata_timer_lock);
420 assert(!stream->missed_metadata_flush);
421 stream->waiting_on_metadata = true;
422 pthread_mutex_unlock(&stream->metadata_timer_lock);
423
424 ret = consumer_stream_sync_metadata(ctx, stream->session_id);
425
426 pthread_mutex_lock(&stream->metadata_timer_lock);
427 stream->waiting_on_metadata = false;
428 if (stream->missed_metadata_flush) {
429 stream->missed_metadata_flush = false;
430 pthread_mutex_unlock(&stream->metadata_timer_lock);
431 (void) stream->read_subbuffer_ops.send_live_beacon(stream);
432 } else {
433 pthread_mutex_unlock(&stream->metadata_timer_lock);
434 }
435 if (ret < 0) {
436 goto end;
437 }
438
439 ret = consumer_stream_send_index(stream, subbuffer, ctx);
440end:
441 return ret;
442}
443
444/*
445 * Check if the local version of the metadata stream matches with the version
446 * of the metadata stream in the kernel. If it was updated, set the reset flag
447 * on the stream.
448 */
449static
450int metadata_stream_check_version(struct lttng_consumer_stream *stream,
451 const struct stream_subbuffer *subbuffer)
452{
453 if (stream->metadata_version == subbuffer->info.metadata.version) {
454 goto end;
455 }
456
457 DBG("New metadata version detected");
443d6e6f
JG
458 consumer_stream_metadata_set_version(stream,
459 subbuffer->info.metadata.version);
8d18bcae 460
6f1177cf
JG
461 if (stream->read_subbuffer_ops.reset_metadata) {
462 stream->read_subbuffer_ops.reset_metadata(stream);
463 }
464
465end:
466 return 0;
467}
468
469struct lttng_consumer_stream *consumer_stream_create(
470 struct lttng_consumer_channel *channel,
471 uint64_t channel_key,
472 uint64_t stream_key,
473 const char *channel_name,
474 uint64_t relayd_id,
475 uint64_t session_id,
476 struct lttng_trace_chunk *trace_chunk,
477 int cpu,
478 int *alloc_ret,
479 enum consumer_channel_type type,
480 unsigned int monitor)
481{
482 int ret;
483 struct lttng_consumer_stream *stream;
484
485 stream = zmalloc(sizeof(*stream));
486 if (stream == NULL) {
487 PERROR("malloc struct lttng_consumer_stream");
488 ret = -ENOMEM;
489 goto end;
490 }
491
78f68974
MD
492 rcu_read_lock();
493
6f1177cf
JG
494 if (trace_chunk && !lttng_trace_chunk_get(trace_chunk)) {
495 ERR("Failed to acquire trace chunk reference during the creation of a stream");
496 ret = -1;
497 goto error;
498 }
499
6f1177cf
JG
500 stream->chan = channel;
501 stream->key = stream_key;
502 stream->trace_chunk = trace_chunk;
503 stream->out_fd = -1;
504 stream->out_fd_offset = 0;
505 stream->output_written = 0;
506 stream->net_seq_idx = relayd_id;
507 stream->session_id = session_id;
508 stream->monitor = monitor;
509 stream->endpoint_status = CONSUMER_ENDPOINT_ACTIVE;
510 stream->index_file = NULL;
511 stream->last_sequence_number = -1ULL;
512 stream->rotate_position = -1ULL;
513 pthread_mutex_init(&stream->lock, NULL);
514 pthread_mutex_init(&stream->metadata_timer_lock, NULL);
515
516 /* If channel is the metadata, flag this stream as metadata. */
517 if (type == CONSUMER_CHANNEL_TYPE_METADATA) {
518 stream->metadata_flag = 1;
519 /* Metadata is flat out. */
520 strncpy(stream->name, DEFAULT_METADATA_NAME, sizeof(stream->name));
521 /* Live rendez-vous point. */
522 pthread_cond_init(&stream->metadata_rdv, NULL);
523 pthread_mutex_init(&stream->metadata_rdv_lock, NULL);
524 } else {
525 /* Format stream name to <channel_name>_<cpu_number> */
526 ret = snprintf(stream->name, sizeof(stream->name), "%s_%d",
527 channel_name, cpu);
528 if (ret < 0) {
529 PERROR("snprintf stream name");
530 goto error;
531 }
532 }
533
534 switch (channel->output) {
535 case CONSUMER_CHANNEL_SPLICE:
536 stream->output = LTTNG_EVENT_SPLICE;
537 ret = utils_create_pipe(stream->splice_pipe);
538 if (ret < 0) {
539 goto error;
540 }
541 break;
542 case CONSUMER_CHANNEL_MMAP:
543 stream->output = LTTNG_EVENT_MMAP;
544 break;
545 default:
546 abort();
547 }
548
549 /* Key is always the wait_fd for streams. */
550 lttng_ht_node_init_u64(&stream->node, stream->key);
551
552 /* Init node per channel id key */
553 lttng_ht_node_init_u64(&stream->node_channel_id, channel_key);
554
555 /* Init session id node with the stream session id */
556 lttng_ht_node_init_u64(&stream->node_session_id, stream->session_id);
557
558 DBG3("Allocated stream %s (key %" PRIu64 ", chan_key %" PRIu64
559 " relayd_id %" PRIu64 ", session_id %" PRIu64,
560 stream->name, stream->key, channel_key,
561 stream->net_seq_idx, stream->session_id);
562
563 rcu_read_unlock();
564
565 if (type == CONSUMER_CHANNEL_TYPE_METADATA) {
566 stream->read_subbuffer_ops.lock =
567 consumer_stream_metadata_lock_all;
568 stream->read_subbuffer_ops.unlock =
569 consumer_stream_metadata_unlock_all;
570 stream->read_subbuffer_ops.pre_consume_subbuffer =
571 metadata_stream_check_version;
572 } else {
573 stream->read_subbuffer_ops.lock = consumer_stream_data_lock_all;
574 stream->read_subbuffer_ops.unlock =
575 consumer_stream_data_unlock_all;
576 stream->read_subbuffer_ops.pre_consume_subbuffer =
577 consumer_stream_update_stats;
578 if (channel->is_live) {
579 stream->read_subbuffer_ops.post_consume =
580 consumer_stream_sync_metadata_index;
581 } else {
582 stream->read_subbuffer_ops.post_consume =
583 consumer_stream_send_index;
584 }
585 }
586
587 if (channel->output == CONSUMER_CHANNEL_MMAP) {
588 stream->read_subbuffer_ops.consume_subbuffer =
589 consumer_stream_consume_mmap;
590 } else {
591 stream->read_subbuffer_ops.consume_subbuffer =
592 consumer_stream_consume_splice;
593 }
594
595 return stream;
596
597error:
598 rcu_read_unlock();
599 lttng_trace_chunk_put(stream->trace_chunk);
600 free(stream);
601end:
602 if (alloc_ret) {
603 *alloc_ret = ret;
604 }
605 return NULL;
606}
607
51230d70
DG
608/*
609 * Close stream on the relayd side. This call can destroy a relayd if the
610 * conditions are met.
611 *
612 * A RCU read side lock MUST be acquired if the relayd object was looked up in
613 * a hash table before calling this.
614 */
615void consumer_stream_relayd_close(struct lttng_consumer_stream *stream,
616 struct consumer_relayd_sock_pair *relayd)
617{
618 int ret;
619
620 assert(stream);
621 assert(relayd);
622
d01178b6
DG
623 if (stream->sent_to_relayd) {
624 uatomic_dec(&relayd->refcount);
625 assert(uatomic_read(&relayd->refcount) >= 0);
626 }
51230d70
DG
627
628 /* Closing streams requires to lock the control socket. */
629 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
630 ret = relayd_send_close_stream(&relayd->control_sock,
631 stream->relayd_stream_id,
632 stream->next_net_seq_num - 1);
633 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
634 if (ret < 0) {
11413bb9
JR
635 ERR("Relayd send close stream failed. Cleaning up relayd %" PRIu64 ".", relayd->net_seq_idx);
636 lttng_consumer_cleanup_relayd(relayd);
51230d70
DG
637 }
638
639 /* Both conditions are met, we destroy the relayd. */
640 if (uatomic_read(&relayd->refcount) == 0 &&
641 uatomic_read(&relayd->destroy_flag)) {
642 consumer_destroy_relayd(relayd);
643 }
10a50311 644 stream->net_seq_idx = (uint64_t) -1ULL;
d01178b6 645 stream->sent_to_relayd = 0;
51230d70
DG
646}
647
648/*
649 * Close stream's file descriptors and, if needed, close stream also on the
650 * relayd side.
651 *
652 * The consumer data lock MUST be acquired.
653 * The stream lock MUST be acquired.
654 */
655void consumer_stream_close(struct lttng_consumer_stream *stream)
656{
657 int ret;
658 struct consumer_relayd_sock_pair *relayd;
659
660 assert(stream);
661
662 switch (consumer_data.type) {
663 case LTTNG_CONSUMER_KERNEL:
664 if (stream->mmap_base != NULL) {
665 ret = munmap(stream->mmap_base, stream->mmap_len);
666 if (ret != 0) {
667 PERROR("munmap");
668 }
669 }
670
671 if (stream->wait_fd >= 0) {
672 ret = close(stream->wait_fd);
673 if (ret) {
674 PERROR("close");
675 }
10a50311 676 stream->wait_fd = -1;
51230d70 677 }
a2361a61
JD
678 if (stream->chan->output == CONSUMER_CHANNEL_SPLICE) {
679 utils_close_pipe(stream->splice_pipe);
680 }
51230d70
DG
681 break;
682 case LTTNG_CONSUMER32_UST:
683 case LTTNG_CONSUMER64_UST:
6d574024
DG
684 {
685 /*
686 * Special case for the metadata since the wait fd is an internal pipe
687 * polled in the metadata thread.
688 */
689 if (stream->metadata_flag && stream->chan->monitor) {
690 int rpipe = stream->ust_metadata_poll_pipe[0];
691
692 /*
693 * This will stop the channel timer if one and close the write side
694 * of the metadata poll pipe.
695 */
696 lttng_ustconsumer_close_metadata(stream->chan);
697 if (rpipe >= 0) {
698 ret = close(rpipe);
699 if (ret < 0) {
b4a650f3 700 PERROR("closing metadata pipe read side");
6d574024
DG
701 }
702 stream->ust_metadata_poll_pipe[0] = -1;
703 }
704 }
51230d70 705 break;
6d574024 706 }
51230d70
DG
707 default:
708 ERR("Unknown consumer_data type");
709 assert(0);
710 }
711
712 /* Close output fd. Could be a socket or local file at this point. */
713 if (stream->out_fd >= 0) {
714 ret = close(stream->out_fd);
715 if (ret) {
716 PERROR("close");
717 }
10a50311 718 stream->out_fd = -1;
51230d70
DG
719 }
720
f8f3885c
MD
721 if (stream->index_file) {
722 lttng_index_file_put(stream->index_file);
723 stream->index_file = NULL;
309167d2
JD
724 }
725
e5148e25
JG
726 lttng_trace_chunk_put(stream->trace_chunk);
727 stream->trace_chunk = NULL;
728
51230d70
DG
729 /* Check and cleanup relayd if needed. */
730 rcu_read_lock();
731 relayd = consumer_find_relayd(stream->net_seq_idx);
732 if (relayd != NULL) {
733 consumer_stream_relayd_close(stream, relayd);
734 }
735 rcu_read_unlock();
736}
737
738/*
739 * Delete the stream from all possible hash tables.
740 *
741 * The consumer data lock MUST be acquired.
742 * The stream lock MUST be acquired.
743 */
744void consumer_stream_delete(struct lttng_consumer_stream *stream,
745 struct lttng_ht *ht)
746{
747 int ret;
748 struct lttng_ht_iter iter;
749
750 assert(stream);
10a50311
JD
751 /* Should NEVER be called not in monitor mode. */
752 assert(stream->chan->monitor);
51230d70
DG
753
754 rcu_read_lock();
755
756 if (ht) {
757 iter.iter.node = &stream->node.node;
758 ret = lttng_ht_del(ht, &iter);
759 assert(!ret);
760 }
761
762 /* Delete from stream per channel ID hash table. */
763 iter.iter.node = &stream->node_channel_id.node;
764 /*
765 * The returned value is of no importance. Even if the node is NOT in the
766 * hash table, we continue since we may have been called by a code path
767 * that did not add the stream to a (all) hash table. Same goes for the
768 * next call ht del call.
769 */
770 (void) lttng_ht_del(consumer_data.stream_per_chan_id_ht, &iter);
771
772 /* Delete from the global stream list. */
773 iter.iter.node = &stream->node_session_id.node;
774 /* See the previous ht del on why we ignore the returned value. */
775 (void) lttng_ht_del(consumer_data.stream_list_ht, &iter);
776
777 rcu_read_unlock();
778
6d574024
DG
779 if (!stream->metadata_flag) {
780 /* Decrement the stream count of the global consumer data. */
781 assert(consumer_data.stream_count > 0);
782 consumer_data.stream_count--;
783 }
51230d70
DG
784}
785
786/*
787 * Free the given stream within a RCU call.
788 */
789void consumer_stream_free(struct lttng_consumer_stream *stream)
790{
791 assert(stream);
792
8d18bcae 793 metadata_bucket_destroy(stream->metadata_bucket);
51230d70
DG
794 call_rcu(&stream->node.head, free_stream_rcu);
795}
796
797/*
10a50311 798 * Destroy the stream's buffers of the tracer.
51230d70 799 */
10a50311 800void consumer_stream_destroy_buffers(struct lttng_consumer_stream *stream)
51230d70 801{
10a50311
JD
802 assert(stream);
803
804 switch (consumer_data.type) {
805 case LTTNG_CONSUMER_KERNEL:
806 break;
807 case LTTNG_CONSUMER32_UST:
808 case LTTNG_CONSUMER64_UST:
809 lttng_ustconsumer_del_stream(stream);
810 break;
811 default:
812 ERR("Unknown consumer_data type");
813 assert(0);
814 }
815}
51230d70 816
10a50311 817/*
4891ece8 818 * Destroy and close a already created stream.
10a50311 819 */
4891ece8 820static void destroy_close_stream(struct lttng_consumer_stream *stream)
10a50311 821{
51230d70
DG
822 assert(stream);
823
4891ece8 824 DBG("Consumer stream destroy monitored key: %" PRIu64, stream->key);
10a50311
JD
825
826 /* Destroy tracer buffers of the stream. */
827 consumer_stream_destroy_buffers(stream);
828 /* Close down everything including the relayd if one. */
829 consumer_stream_close(stream);
830}
51230d70 831
10a50311 832/*
4891ece8
DG
833 * Decrement the stream's channel refcount and if down to 0, return the channel
834 * pointer so it can be destroyed by the caller or NULL if not.
10a50311 835 */
4891ece8
DG
836static struct lttng_consumer_channel *unref_channel(
837 struct lttng_consumer_stream *stream)
10a50311 838{
4891ece8
DG
839 struct lttng_consumer_channel *free_chan = NULL;
840
10a50311 841 assert(stream);
4891ece8 842 assert(stream->chan);
10a50311 843
4891ece8
DG
844 /* Update refcount of channel and see if we need to destroy it. */
845 if (!uatomic_sub_return(&stream->chan->refcount, 1)
846 && !uatomic_read(&stream->chan->nb_init_stream_left)) {
847 free_chan = stream->chan;
848 }
51230d70 849
4891ece8 850 return free_chan;
10a50311 851}
51230d70 852
10a50311
JD
853/*
854 * Destroy a stream completely. This will delete, close and free the stream.
855 * Once return, the stream is NO longer usable. Its channel may get destroyed
856 * if conditions are met for a monitored stream.
857 *
858 * This MUST be called WITHOUT the consumer data and stream lock acquired if
859 * the stream is in _monitor_ mode else it does not matter.
860 */
861void consumer_stream_destroy(struct lttng_consumer_stream *stream,
862 struct lttng_ht *ht)
863{
864 assert(stream);
865
866 /* Stream is in monitor mode. */
4891ece8 867 if (stream->monitor) {
10a50311 868 struct lttng_consumer_channel *free_chan = NULL;
51230d70 869
4891ece8
DG
870 /*
871 * This means that the stream was successfully removed from the streams
872 * list of the channel and sent to the right thread managing this
873 * stream thus being globally visible.
874 */
875 if (stream->globally_visible) {
876 pthread_mutex_lock(&consumer_data.lock);
a9838785 877 pthread_mutex_lock(&stream->chan->lock);
4891ece8
DG
878 pthread_mutex_lock(&stream->lock);
879 /* Remove every reference of the stream in the consumer. */
880 consumer_stream_delete(stream, ht);
881
882 destroy_close_stream(stream);
883
884 /* Update channel's refcount of the stream. */
885 free_chan = unref_channel(stream);
886
887 /* Indicates that the consumer data state MUST be updated after this. */
888 consumer_data.need_update = 1;
889
890 pthread_mutex_unlock(&stream->lock);
a9838785 891 pthread_mutex_unlock(&stream->chan->lock);
4891ece8
DG
892 pthread_mutex_unlock(&consumer_data.lock);
893 } else {
894 /*
895 * If the stream is not visible globally, this needs to be done
896 * outside of the consumer data lock section.
897 */
898 free_chan = unref_channel(stream);
10a50311
JD
899 }
900
10a50311
JD
901 if (free_chan) {
902 consumer_del_channel(free_chan);
903 }
904 } else {
4891ece8 905 destroy_close_stream(stream);
51230d70
DG
906 }
907
908 /* Free stream within a RCU call. */
e5148e25
JG
909 lttng_trace_chunk_put(stream->trace_chunk);
910 stream->trace_chunk = NULL;
51230d70
DG
911 consumer_stream_free(stream);
912}
1c20f0e2
JD
913
914/*
915 * Write index of a specific stream either on the relayd or local disk.
916 *
917 * Return 0 on success or else a negative value.
918 */
919int consumer_stream_write_index(struct lttng_consumer_stream *stream,
f8f3885c 920 struct ctf_packet_index *element)
1c20f0e2
JD
921{
922 int ret;
1c20f0e2
JD
923
924 assert(stream);
f8f3885c 925 assert(element);
1c20f0e2
JD
926
927 rcu_read_lock();
23c910e5
JR
928 if (stream->net_seq_idx != (uint64_t) -1ULL) {
929 struct consumer_relayd_sock_pair *relayd;
930 relayd = consumer_find_relayd(stream->net_seq_idx);
931 if (relayd) {
932 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
933 ret = relayd_send_index(&relayd->control_sock, element,
1c20f0e2 934 stream->relayd_stream_id, stream->next_net_seq_num - 1);
11413bb9
JR
935 if (ret < 0) {
936 /*
937 * Communication error with lttng-relayd,
938 * perform cleanup now
939 */
940 ERR("Relayd send index failed. Cleaning up relayd %" PRIu64 ".", relayd->net_seq_idx);
941 lttng_consumer_cleanup_relayd(relayd);
942 ret = -1;
943 }
23c910e5
JR
944 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
945 } else {
946 ERR("Stream %" PRIu64 " relayd ID %" PRIu64 " unknown. Can't write index.",
947 stream->key, stream->net_seq_idx);
948 ret = -1;
949 }
1c20f0e2 950 } else {
f8f3885c 951 if (lttng_index_file_write(stream->index_file, element)) {
6cd525e8
MD
952 ret = -1;
953 } else {
954 ret = 0;
955 }
1c20f0e2
JD
956 }
957 if (ret < 0) {
958 goto error;
959 }
960
961error:
962 rcu_read_unlock();
963 return ret;
964}
94d49140 965
e5148e25
JG
966int consumer_stream_create_output_files(struct lttng_consumer_stream *stream,
967 bool create_index)
968{
969 int ret;
970 enum lttng_trace_chunk_status chunk_status;
971 const int flags = O_WRONLY | O_CREAT | O_TRUNC;
972 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
973 char stream_path[LTTNG_PATH_MAX];
974
975 ASSERT_LOCKED(stream->lock);
976 assert(stream->trace_chunk);
977
978 ret = utils_stream_file_path(stream->chan->pathname, stream->name,
979 stream->chan->tracefile_size,
13d934a5 980 stream->tracefile_count_current, NULL,
e5148e25
JG
981 stream_path, sizeof(stream_path));
982 if (ret < 0) {
983 goto end;
984 }
985
986 if (stream->out_fd >= 0) {
987 ret = close(stream->out_fd);
988 if (ret < 0) {
989 PERROR("Failed to close stream file \"%s\"",
990 stream->name);
991 goto end;
992 }
993 stream->out_fd = -1;
994 }
995
996 DBG("Opening stream output file \"%s\"", stream_path);
997 chunk_status = lttng_trace_chunk_open_file(stream->trace_chunk, stream_path,
998 flags, mode, &stream->out_fd);
999 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1000 ERR("Failed to open stream file \"%s\"", stream->name);
1001 ret = -1;
1002 goto end;
1003 }
1004
1005 if (!stream->metadata_flag && (create_index || stream->index_file)) {
1006 if (stream->index_file) {
1007 lttng_index_file_put(stream->index_file);
1008 }
1009 stream->index_file = lttng_index_file_create_from_trace_chunk(
1010 stream->trace_chunk,
1011 stream->chan->pathname,
1012 stream->name,
1013 stream->chan->tracefile_size,
1014 stream->tracefile_count_current,
1015 CTF_INDEX_MAJOR, CTF_INDEX_MINOR,
1016 false);
1017 if (!stream->index_file) {
1018 ret = -1;
1019 goto end;
1020 }
1021 }
1022
1023 /* Reset current size because we just perform a rotation. */
1024 stream->tracefile_size_current = 0;
1025 stream->out_fd_offset = 0;
1026end:
1027 return ret;
1028}
1029
1030int consumer_stream_rotate_output_files(struct lttng_consumer_stream *stream)
1031{
1032 int ret;
1033
1034 stream->tracefile_count_current++;
1035 if (stream->chan->tracefile_count > 0) {
1036 stream->tracefile_count_current %=
1037 stream->chan->tracefile_count;
1038 }
1039
1040 DBG("Rotating output files of stream \"%s\"", stream->name);
1041 ret = consumer_stream_create_output_files(stream, true);
1042 if (ret) {
1043 goto end;
1044 }
1045
1046end:
1047 return ret;
1048}
1d18f2d9
JG
1049
1050bool consumer_stream_is_deleted(struct lttng_consumer_stream *stream)
1051{
1052 /*
1053 * This function does not take a const stream since
1054 * cds_lfht_is_node_deleted was not const before liburcu 0.12.
1055 */
1056 assert(stream);
1057 return cds_lfht_is_node_deleted(&stream->node.node);
1058}
8d18bcae
JG
1059
1060static ssize_t metadata_bucket_flush(
1061 const struct stream_subbuffer *buffer, void *data)
1062{
1063 ssize_t ret;
1064 struct lttng_consumer_stream *stream = data;
1065
1066 ret = consumer_stream_consume_mmap(NULL, stream, buffer);
1067 if (ret < 0) {
1068 goto end;
1069 }
1070end:
1071 return ret;
1072}
1073
1074static ssize_t metadata_bucket_consume(
1075 struct lttng_consumer_local_data *unused,
1076 struct lttng_consumer_stream *stream,
1077 const struct stream_subbuffer *subbuffer)
1078{
1079 ssize_t ret;
1080 enum metadata_bucket_status status;
1081
1082 status = metadata_bucket_fill(stream->metadata_bucket, subbuffer);
1083 switch (status) {
1084 case METADATA_BUCKET_STATUS_OK:
1085 /* Return consumed size. */
1086 ret = subbuffer->buffer.buffer.size;
1087 break;
1088 default:
1089 ret = -1;
1090 }
1091
1092 return ret;
1093}
1094
1095int consumer_stream_enable_metadata_bucketization(
1096 struct lttng_consumer_stream *stream)
1097{
1098 int ret = 0;
1099
1100 assert(stream->metadata_flag);
1101 assert(!stream->metadata_bucket);
1102 assert(stream->chan->output == CONSUMER_CHANNEL_MMAP);
1103
1104 stream->metadata_bucket = metadata_bucket_create(
1105 metadata_bucket_flush, stream);
1106 if (!stream->metadata_bucket) {
1107 ret = -1;
1108 goto end;
1109 }
1110
1111 stream->read_subbuffer_ops.consume_subbuffer = metadata_bucket_consume;
1112end:
1113 return ret;
1114}
443d6e6f
JG
1115
1116void consumer_stream_metadata_set_version(
1117 struct lttng_consumer_stream *stream, uint64_t new_version)
1118{
1119 assert(new_version > stream->metadata_version);
1120 stream->metadata_version = new_version;
1121 stream->reset_metadata_flag = 1;
1122
1123 if (stream->metadata_bucket) {
1124 metadata_bucket_reset(stream->metadata_bucket);
1125 }
1126}
This page took 0.088942 seconds and 4 git commands to generate.