consumerd: refactor: split read_subbuf into sub-operations
[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>
29d1a7ae
JG
32#include <common/consumer/consumer.h>
33#include <common/consumer/consumer-timer.h>
51230d70
DG
34
35#include "consumer-stream.h"
36
37/*
38 * RCU call to free stream. MUST only be used with call_rcu().
39 */
40static void free_stream_rcu(struct rcu_head *head)
41{
42 struct lttng_ht_node_u64 *node =
43 caa_container_of(head, struct lttng_ht_node_u64, head);
44 struct lttng_consumer_stream *stream =
45 caa_container_of(node, struct lttng_consumer_stream, node);
46
47 pthread_mutex_destroy(&stream->lock);
48 free(stream);
49}
50
29d1a7ae
JG
51static void consumer_stream_data_lock_all(struct lttng_consumer_stream *stream)
52{
53 pthread_mutex_lock(&stream->chan->lock);
54 pthread_mutex_lock(&stream->lock);
55}
56
57static void consumer_stream_data_unlock_all(struct lttng_consumer_stream *stream)
58{
59 pthread_mutex_unlock(&stream->lock);
60 pthread_mutex_unlock(&stream->chan->lock);
61}
62
63static void consumer_stream_metadata_lock_all(struct lttng_consumer_stream *stream)
64{
65 consumer_stream_data_lock_all(stream);
66 pthread_mutex_lock(&stream->metadata_rdv_lock);
67}
68
69static void consumer_stream_metadata_unlock_all(struct lttng_consumer_stream *stream)
70{
71 pthread_mutex_unlock(&stream->metadata_rdv_lock);
72 consumer_stream_data_unlock_all(stream);
73}
74
75/* Only used for data streams. */
76static int consumer_stream_update_stats(struct lttng_consumer_stream *stream,
77 const struct stream_subbuffer *subbuf)
78{
79 int ret = 0;
80 uint64_t sequence_number;
81 const uint64_t discarded_events =
82 LTTNG_OPTIONAL_GET(subbuf->info.data.sequence_number);
83
84 if (!subbuf->info.data.sequence_number.is_set) {
85 /* Command not supported by the tracer. */
86 sequence_number = -1ULL;
87 } else {
88 sequence_number = subbuf->info.data.sequence_number.value;
89 }
90
91 /*
92 * Start the sequence when we extract the first packet in case we don't
93 * start at 0 (for example if a consumer is not connected to the
94 * session immediately after the beginning).
95 */
96 if (stream->last_sequence_number == -1ULL) {
97 stream->last_sequence_number = sequence_number;
98 } else if (sequence_number > stream->last_sequence_number) {
99 stream->chan->lost_packets += sequence_number -
100 stream->last_sequence_number - 1;
101 } else {
102 /* seq <= last_sequence_number */
103 ERR("Sequence number inconsistent : prev = %" PRIu64
104 ", current = %" PRIu64,
105 stream->last_sequence_number, sequence_number);
106 ret = -1;
107 goto end;
108 }
109 stream->last_sequence_number = sequence_number;
110
111 if (discarded_events < stream->last_discarded_events) {
112 /*
113 * Overflow has occurred. We assume only one wrap-around
114 * has occurred.
115 */
116 stream->chan->discarded_events +=
117 (1ULL << (CAA_BITS_PER_LONG - 1)) -
118 stream->last_discarded_events +
119 discarded_events;
120 } else {
121 stream->chan->discarded_events += discarded_events -
122 stream->last_discarded_events;
123 }
124 stream->last_discarded_events = discarded_events;
125 ret = 0;
126
127end:
128 return ret;
129}
130
131static
132void ctf_packet_index_populate(struct ctf_packet_index *index,
133 off_t offset, const struct stream_subbuffer *subbuffer)
134{
135 *index = (typeof(*index)){
136 .offset = htobe64(offset),
137 .packet_size = htobe64(subbuffer->info.data.packet_size),
138 .content_size = htobe64(subbuffer->info.data.content_size),
139 .timestamp_begin = htobe64(
140 subbuffer->info.data.timestamp_begin),
141 .timestamp_end = htobe64(
142 subbuffer->info.data.timestamp_end),
143 .events_discarded = htobe64(
144 subbuffer->info.data.events_discarded),
145 .stream_id = htobe64(subbuffer->info.data.stream_id),
146 .stream_instance_id = htobe64(
147 subbuffer->info.data.stream_instance_id.is_set ?
148 subbuffer->info.data.stream_instance_id.value : -1ULL),
149 .packet_seq_num = htobe64(
150 subbuffer->info.data.sequence_number.is_set ?
151 subbuffer->info.data.sequence_number.value : -1ULL),
152 };
153}
154
155static ssize_t consumer_stream_consume_mmap(
156 struct lttng_consumer_local_data *ctx,
157 struct lttng_consumer_stream *stream,
158 const struct stream_subbuffer *subbuffer)
159{
160 const unsigned long padding_size =
161 subbuffer->info.data.padded_subbuf_size -
162 subbuffer->info.data.subbuf_size;
163
164 return lttng_consumer_on_read_subbuffer_mmap(
165 ctx, stream, &subbuffer->buffer.buffer, padding_size);
166}
167
168static ssize_t consumer_stream_consume_splice(
169 struct lttng_consumer_local_data *ctx,
170 struct lttng_consumer_stream *stream,
171 const struct stream_subbuffer *subbuffer)
172{
173 return lttng_consumer_on_read_subbuffer_splice(ctx, stream,
174 subbuffer->info.data.padded_subbuf_size, 0);
175}
176
177static int consumer_stream_send_index(
178 struct lttng_consumer_stream *stream,
179 const struct stream_subbuffer *subbuffer,
180 struct lttng_consumer_local_data *ctx)
181{
182 off_t packet_offset = 0;
183 struct ctf_packet_index index = {};
184
185 /*
186 * This is called after consuming the sub-buffer; substract the
187 * effect this sub-buffer from the offset.
188 */
189 if (stream->relayd_id == (uint64_t) -1ULL) {
190 packet_offset = stream->out_fd_offset -
191 subbuffer->info.data.padded_subbuf_size;
192 }
193
194 ctf_packet_index_populate(&index, packet_offset, subbuffer);
195 return consumer_stream_write_index(stream, &index);
196}
197
198/*
199 * Actually do the metadata sync using the given metadata stream.
200 *
201 * Return 0 on success else a negative value. ENODATA can be returned also
202 * indicating that there is no metadata available for that stream.
203 */
204static int do_sync_metadata(struct lttng_consumer_stream *metadata,
205 struct lttng_consumer_local_data *ctx)
206{
207 int ret;
208
209 assert(metadata);
210 assert(metadata->metadata_flag);
211 assert(ctx);
212
213 /*
214 * In UST, since we have to write the metadata from the cache packet
215 * by packet, we might need to start this procedure multiple times
216 * until all the metadata from the cache has been extracted.
217 */
218 do {
219 /*
220 * Steps :
221 * - Lock the metadata stream
222 * - Check if metadata stream node was deleted before locking.
223 * - if yes, release and return success
224 * - Check if new metadata is ready (flush + snapshot pos)
225 * - If nothing : release and return.
226 * - Lock the metadata_rdv_lock
227 * - Unlock the metadata stream
228 * - cond_wait on metadata_rdv to wait the wakeup from the
229 * metadata thread
230 * - Unlock the metadata_rdv_lock
231 */
232 pthread_mutex_lock(&metadata->lock);
233
234 /*
235 * There is a possibility that we were able to acquire a reference on the
236 * stream from the RCU hash table but between then and now, the node might
237 * have been deleted just before the lock is acquired. Thus, after locking,
238 * we make sure the metadata node has not been deleted which means that the
239 * buffers are closed.
240 *
241 * In that case, there is no need to sync the metadata hence returning a
242 * success return code.
243 */
244 ret = cds_lfht_is_node_deleted(&metadata->node.node);
245 if (ret) {
246 ret = 0;
247 goto end_unlock_mutex;
248 }
249
250 switch (ctx->type) {
251 case LTTNG_CONSUMER_KERNEL:
252 /*
253 * Empty the metadata cache and flush the current stream.
254 */
255 ret = lttng_kconsumer_sync_metadata(metadata);
256 break;
257 case LTTNG_CONSUMER32_UST:
258 case LTTNG_CONSUMER64_UST:
259 /*
260 * Ask the sessiond if we have new metadata waiting and update the
261 * consumer metadata cache.
262 */
263 ret = lttng_ustconsumer_sync_metadata(ctx, metadata);
264 break;
265 default:
266 assert(0);
267 ret = -1;
268 break;
269 }
270 /*
271 * Error or no new metadata, we exit here.
272 */
273 if (ret <= 0 || ret == ENODATA) {
274 goto end_unlock_mutex;
275 }
276
277 /*
278 * At this point, new metadata have been flushed, so we wait on the
279 * rendez-vous point for the metadata thread to wake us up when it
280 * finishes consuming the metadata and continue execution.
281 */
282
283 pthread_mutex_lock(&metadata->metadata_rdv_lock);
284
285 /*
286 * Release metadata stream lock so the metadata thread can process it.
287 */
288 pthread_mutex_unlock(&metadata->lock);
289
290 /*
291 * Wait on the rendez-vous point. Once woken up, it means the metadata was
292 * consumed and thus synchronization is achieved.
293 */
294 pthread_cond_wait(&metadata->metadata_rdv, &metadata->metadata_rdv_lock);
295 pthread_mutex_unlock(&metadata->metadata_rdv_lock);
296 } while (ret == EAGAIN);
297
298 /* Success */
299 return 0;
300
301end_unlock_mutex:
302 pthread_mutex_unlock(&metadata->lock);
303 return ret;
304}
305
306/*
307 * Synchronize the metadata using a given session ID. A successful acquisition
308 * of a metadata stream will trigger a request to the session daemon and a
309 * snapshot so the metadata thread can consume it.
310 *
311 * This function call is a rendez-vous point between the metadata thread and
312 * the data thread.
313 *
314 * Return 0 on success or else a negative value.
315 */
316int consumer_stream_sync_metadata(struct lttng_consumer_local_data *ctx,
317 uint64_t session_id)
318{
319 int ret;
320 struct lttng_consumer_stream *stream = NULL;
321 struct lttng_ht_iter iter;
322 struct lttng_ht *ht;
323
324 assert(ctx);
325
326 /* Ease our life a bit. */
327 ht = consumer_data.stream_list_ht;
328
329 rcu_read_lock();
330
331 /* Search the metadata associated with the session id of the given stream. */
332
333 cds_lfht_for_each_entry_duplicate(ht->ht,
334 ht->hash_fct(&session_id, lttng_ht_seed), ht->match_fct,
335 &session_id, &iter.iter, stream, node_session_id.node) {
336 if (!stream->metadata_flag) {
337 continue;
338 }
339
340 ret = do_sync_metadata(stream, ctx);
341 if (ret < 0) {
342 goto end;
343 }
344 }
345
346 /*
347 * Force return code to 0 (success) since ret might be ENODATA for instance
348 * which is not an error but rather that we should come back.
349 */
350 ret = 0;
351
352end:
353 rcu_read_unlock();
354 return ret;
355}
356
357static int consumer_stream_sync_metadata_index(
358 struct lttng_consumer_stream *stream,
359 const struct stream_subbuffer *subbuffer,
360 struct lttng_consumer_local_data *ctx)
361{
362 int ret;
363
364 /* Block until all the metadata is sent. */
365 pthread_mutex_lock(&stream->metadata_timer_lock);
366 assert(!stream->missed_metadata_flush);
367 stream->waiting_on_metadata = true;
368 pthread_mutex_unlock(&stream->metadata_timer_lock);
369
370 ret = consumer_stream_sync_metadata(ctx, stream->session_id);
371
372 pthread_mutex_lock(&stream->metadata_timer_lock);
373 stream->waiting_on_metadata = false;
374 if (stream->missed_metadata_flush) {
375 stream->missed_metadata_flush = false;
376 pthread_mutex_unlock(&stream->metadata_timer_lock);
377 (void) stream->read_subbuffer_ops.send_live_beacon(stream);
378 } else {
379 pthread_mutex_unlock(&stream->metadata_timer_lock);
380 }
381 if (ret < 0) {
382 goto end;
383 }
384
385 ret = consumer_stream_send_index(stream, subbuffer, ctx);
386end:
387 return ret;
388}
389
390/*
391 * Check if the local version of the metadata stream matches with the version
392 * of the metadata stream in the kernel. If it was updated, set the reset flag
393 * on the stream.
394 */
395static
396int metadata_stream_check_version(struct lttng_consumer_stream *stream,
397 const struct stream_subbuffer *subbuffer)
398{
399 if (stream->metadata_version == subbuffer->info.metadata.version) {
400 goto end;
401 }
402
403 DBG("New metadata version detected");
404 stream->metadata_version = subbuffer->info.metadata.version;
405 stream->reset_metadata_flag = 1;
406
407 if (stream->read_subbuffer_ops.reset_metadata) {
408 stream->read_subbuffer_ops.reset_metadata(stream);
409 }
410
411end:
412 return 0;
413}
414
415struct lttng_consumer_stream *consumer_stream_create(
416 struct lttng_consumer_channel *channel,
417 uint64_t channel_key,
418 uint64_t stream_key,
419 enum lttng_consumer_stream_state state,
420 const char *channel_name,
421 uid_t uid,
422 gid_t gid,
423 uint64_t relayd_id,
424 uint64_t session_id,
425 int cpu,
426 int *alloc_ret,
427 enum consumer_channel_type type,
428 unsigned int monitor)
429{
430 int ret;
431 struct lttng_consumer_stream *stream;
432
433 stream = zmalloc(sizeof(*stream));
434 if (stream == NULL) {
435 PERROR("malloc struct lttng_consumer_stream");
436 ret = -ENOMEM;
437 goto end;
438 }
439
440 rcu_read_lock();
441 stream->chan = channel;
442 stream->key = stream_key;
443 stream->out_fd = -1;
444 stream->out_fd_offset = 0;
445 stream->output_written = 0;
446 stream->state = state;
447 stream->uid = uid;
448 stream->gid = gid;
449 stream->relayd_id = relayd_id;
450 stream->session_id = session_id;
451 stream->monitor = monitor;
452 stream->endpoint_status = CONSUMER_ENDPOINT_ACTIVE;
453 stream->index_file = NULL;
454 stream->last_sequence_number = -1ULL;
455 pthread_mutex_init(&stream->lock, NULL);
456 pthread_mutex_init(&stream->metadata_timer_lock, NULL);
457
458 /* If channel is the metadata, flag this stream as metadata. */
459 if (type == CONSUMER_CHANNEL_TYPE_METADATA) {
460 stream->metadata_flag = 1;
461 /* Metadata is flat out. */
462 strncpy(stream->name, DEFAULT_METADATA_NAME, sizeof(stream->name));
463 /* Live rendez-vous point. */
464 pthread_cond_init(&stream->metadata_rdv, NULL);
465 pthread_mutex_init(&stream->metadata_rdv_lock, NULL);
466 } else {
467 /* Format stream name to <channel_name>_<cpu_number> */
468 ret = snprintf(stream->name, sizeof(stream->name), "%s_%d",
469 channel_name, cpu);
470 if (ret < 0) {
471 PERROR("snprintf stream name");
472 goto error;
473 }
474 }
475
476 /* Key is always the wait_fd for streams. */
477 lttng_ht_node_init_u64(&stream->node, stream->key);
478
479 /* Init node per channel id key */
480 lttng_ht_node_init_u64(&stream->node_channel_id, channel_key);
481
482 /* Init session id node with the stream session id */
483 lttng_ht_node_init_u64(&stream->node_session_id, stream->session_id);
484
485 DBG3("Allocated stream %s (key %" PRIu64 ", chan_key %" PRIu64
486 " relayd_id %" PRIu64 ", session_id %" PRIu64,
487 stream->name, stream->key, channel_key,
488 stream->relayd_id, stream->session_id);
489
490 rcu_read_unlock();
491
492 switch (channel->output) {
493 case CONSUMER_CHANNEL_SPLICE:
494 stream->output = LTTNG_EVENT_SPLICE;
495 ret = utils_create_pipe(stream->splice_pipe);
496 if (ret < 0) {
497 goto error;
498 }
499 break;
500 case CONSUMER_CHANNEL_MMAP:
501 stream->output = LTTNG_EVENT_MMAP;
502 break;
503 default:
504 abort();
505 }
506
507 if (type == CONSUMER_CHANNEL_TYPE_METADATA) {
508 stream->read_subbuffer_ops.lock =
509 consumer_stream_metadata_lock_all;
510 stream->read_subbuffer_ops.unlock =
511 consumer_stream_metadata_unlock_all;
512 stream->read_subbuffer_ops.pre_consume_subbuffer =
513 metadata_stream_check_version;
514 } else {
515 stream->read_subbuffer_ops.lock = consumer_stream_data_lock_all;
516 stream->read_subbuffer_ops.unlock =
517 consumer_stream_data_unlock_all;
518 stream->read_subbuffer_ops.pre_consume_subbuffer =
519 consumer_stream_update_stats;
520 if (channel->is_live) {
521 stream->read_subbuffer_ops.post_consume =
522 consumer_stream_sync_metadata_index;
523 } else {
524 stream->read_subbuffer_ops.post_consume =
525 consumer_stream_send_index;
526 }
527 }
528
529 if (channel->output == CONSUMER_CHANNEL_MMAP) {
530 stream->read_subbuffer_ops.consume_subbuffer =
531 consumer_stream_consume_mmap;
532 } else {
533 stream->read_subbuffer_ops.consume_subbuffer =
534 consumer_stream_consume_splice;
535 }
536
537 return stream;
538
539error:
540 rcu_read_unlock();
541 free(stream);
542end:
543 if (alloc_ret) {
544 *alloc_ret = ret;
545 }
546 return NULL;
547}
548
51230d70
DG
549/*
550 * Close stream on the relayd side. This call can destroy a relayd if the
551 * conditions are met.
552 *
553 * A RCU read side lock MUST be acquired if the relayd object was looked up in
554 * a hash table before calling this.
555 */
556void consumer_stream_relayd_close(struct lttng_consumer_stream *stream,
557 struct consumer_relayd_sock_pair *relayd)
558{
559 int ret;
560
561 assert(stream);
562 assert(relayd);
563
d01178b6
DG
564 if (stream->sent_to_relayd) {
565 uatomic_dec(&relayd->refcount);
566 assert(uatomic_read(&relayd->refcount) >= 0);
567 }
51230d70
DG
568
569 /* Closing streams requires to lock the control socket. */
570 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
571 ret = relayd_send_close_stream(&relayd->control_sock,
572 stream->relayd_stream_id,
573 stream->next_net_seq_num - 1);
51230d70 574 if (ret < 0) {
6d40f8fa 575 ERR("Relayd send close stream failed. Cleaning up relayd %" PRIu64 ".", relayd->id);
874e1255 576 lttng_consumer_cleanup_relayd(relayd);
51230d70 577 }
874e1255 578 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
51230d70
DG
579
580 /* Both conditions are met, we destroy the relayd. */
581 if (uatomic_read(&relayd->refcount) == 0 &&
582 uatomic_read(&relayd->destroy_flag)) {
583 consumer_destroy_relayd(relayd);
584 }
6d40f8fa 585 stream->relayd_id = (uint64_t) -1ULL;
d01178b6 586 stream->sent_to_relayd = 0;
51230d70
DG
587}
588
589/*
590 * Close stream's file descriptors and, if needed, close stream also on the
591 * relayd side.
592 *
593 * The consumer data lock MUST be acquired.
594 * The stream lock MUST be acquired.
595 */
596void consumer_stream_close(struct lttng_consumer_stream *stream)
597{
598 int ret;
599 struct consumer_relayd_sock_pair *relayd;
600
601 assert(stream);
602
603 switch (consumer_data.type) {
604 case LTTNG_CONSUMER_KERNEL:
605 if (stream->mmap_base != NULL) {
606 ret = munmap(stream->mmap_base, stream->mmap_len);
607 if (ret != 0) {
608 PERROR("munmap");
609 }
610 }
611
612 if (stream->wait_fd >= 0) {
613 ret = close(stream->wait_fd);
614 if (ret) {
615 PERROR("close");
616 }
10a50311 617 stream->wait_fd = -1;
51230d70 618 }
a2361a61
JD
619 if (stream->chan->output == CONSUMER_CHANNEL_SPLICE) {
620 utils_close_pipe(stream->splice_pipe);
621 }
51230d70
DG
622 break;
623 case LTTNG_CONSUMER32_UST:
624 case LTTNG_CONSUMER64_UST:
6d574024
DG
625 {
626 /*
627 * Special case for the metadata since the wait fd is an internal pipe
628 * polled in the metadata thread.
629 */
630 if (stream->metadata_flag && stream->chan->monitor) {
631 int rpipe = stream->ust_metadata_poll_pipe[0];
632
633 /*
634 * This will stop the channel timer if one and close the write side
635 * of the metadata poll pipe.
636 */
637 lttng_ustconsumer_close_metadata(stream->chan);
638 if (rpipe >= 0) {
639 ret = close(rpipe);
640 if (ret < 0) {
b4a650f3 641 PERROR("closing metadata pipe read side");
6d574024
DG
642 }
643 stream->ust_metadata_poll_pipe[0] = -1;
644 }
645 }
51230d70 646 break;
6d574024 647 }
51230d70
DG
648 default:
649 ERR("Unknown consumer_data type");
650 assert(0);
651 }
652
653 /* Close output fd. Could be a socket or local file at this point. */
654 if (stream->out_fd >= 0) {
655 ret = close(stream->out_fd);
656 if (ret) {
657 PERROR("close");
658 }
10a50311 659 stream->out_fd = -1;
51230d70
DG
660 }
661
e0547b83
MD
662 if (stream->index_file) {
663 lttng_index_file_put(stream->index_file);
664 stream->index_file = NULL;
309167d2
JD
665 }
666
51230d70
DG
667 /* Check and cleanup relayd if needed. */
668 rcu_read_lock();
6d40f8fa 669 relayd = consumer_find_relayd(stream->relayd_id);
51230d70
DG
670 if (relayd != NULL) {
671 consumer_stream_relayd_close(stream, relayd);
672 }
673 rcu_read_unlock();
674}
675
676/*
677 * Delete the stream from all possible hash tables.
678 *
679 * The consumer data lock MUST be acquired.
680 * The stream lock MUST be acquired.
681 */
682void consumer_stream_delete(struct lttng_consumer_stream *stream,
683 struct lttng_ht *ht)
684{
685 int ret;
686 struct lttng_ht_iter iter;
687
688 assert(stream);
10a50311
JD
689 /* Should NEVER be called not in monitor mode. */
690 assert(stream->chan->monitor);
51230d70
DG
691
692 rcu_read_lock();
693
694 if (ht) {
695 iter.iter.node = &stream->node.node;
696 ret = lttng_ht_del(ht, &iter);
697 assert(!ret);
698 }
699
700 /* Delete from stream per channel ID hash table. */
701 iter.iter.node = &stream->node_channel_id.node;
702 /*
703 * The returned value is of no importance. Even if the node is NOT in the
704 * hash table, we continue since we may have been called by a code path
705 * that did not add the stream to a (all) hash table. Same goes for the
706 * next call ht del call.
707 */
708 (void) lttng_ht_del(consumer_data.stream_per_chan_id_ht, &iter);
709
710 /* Delete from the global stream list. */
711 iter.iter.node = &stream->node_session_id.node;
712 /* See the previous ht del on why we ignore the returned value. */
713 (void) lttng_ht_del(consumer_data.stream_list_ht, &iter);
714
715 rcu_read_unlock();
716
6d574024
DG
717 if (!stream->metadata_flag) {
718 /* Decrement the stream count of the global consumer data. */
719 assert(consumer_data.stream_count > 0);
720 consumer_data.stream_count--;
721 }
51230d70
DG
722}
723
724/*
725 * Free the given stream within a RCU call.
726 */
727void consumer_stream_free(struct lttng_consumer_stream *stream)
728{
729 assert(stream);
730
731 call_rcu(&stream->node.head, free_stream_rcu);
732}
733
734/*
10a50311 735 * Destroy the stream's buffers of the tracer.
51230d70 736 */
10a50311 737void consumer_stream_destroy_buffers(struct lttng_consumer_stream *stream)
51230d70 738{
10a50311
JD
739 assert(stream);
740
741 switch (consumer_data.type) {
742 case LTTNG_CONSUMER_KERNEL:
743 break;
744 case LTTNG_CONSUMER32_UST:
745 case LTTNG_CONSUMER64_UST:
746 lttng_ustconsumer_del_stream(stream);
747 break;
748 default:
749 ERR("Unknown consumer_data type");
750 assert(0);
751 }
752}
51230d70 753
10a50311 754/*
4891ece8 755 * Destroy and close a already created stream.
10a50311 756 */
4891ece8 757static void destroy_close_stream(struct lttng_consumer_stream *stream)
10a50311 758{
51230d70
DG
759 assert(stream);
760
4891ece8 761 DBG("Consumer stream destroy monitored key: %" PRIu64, stream->key);
10a50311
JD
762
763 /* Destroy tracer buffers of the stream. */
764 consumer_stream_destroy_buffers(stream);
765 /* Close down everything including the relayd if one. */
766 consumer_stream_close(stream);
767}
51230d70 768
10a50311 769/*
4891ece8
DG
770 * Decrement the stream's channel refcount and if down to 0, return the channel
771 * pointer so it can be destroyed by the caller or NULL if not.
10a50311 772 */
4891ece8
DG
773static struct lttng_consumer_channel *unref_channel(
774 struct lttng_consumer_stream *stream)
10a50311 775{
4891ece8
DG
776 struct lttng_consumer_channel *free_chan = NULL;
777
10a50311 778 assert(stream);
4891ece8 779 assert(stream->chan);
10a50311 780
4891ece8
DG
781 /* Update refcount of channel and see if we need to destroy it. */
782 if (!uatomic_sub_return(&stream->chan->refcount, 1)
783 && !uatomic_read(&stream->chan->nb_init_stream_left)) {
784 free_chan = stream->chan;
785 }
51230d70 786
4891ece8 787 return free_chan;
10a50311 788}
51230d70 789
10a50311
JD
790/*
791 * Destroy a stream completely. This will delete, close and free the stream.
792 * Once return, the stream is NO longer usable. Its channel may get destroyed
793 * if conditions are met for a monitored stream.
794 *
795 * This MUST be called WITHOUT the consumer data and stream lock acquired if
796 * the stream is in _monitor_ mode else it does not matter.
797 */
798void consumer_stream_destroy(struct lttng_consumer_stream *stream,
799 struct lttng_ht *ht)
800{
801 assert(stream);
802
803 /* Stream is in monitor mode. */
4891ece8 804 if (stream->monitor) {
10a50311 805 struct lttng_consumer_channel *free_chan = NULL;
51230d70 806
4891ece8
DG
807 /*
808 * This means that the stream was successfully removed from the streams
809 * list of the channel and sent to the right thread managing this
810 * stream thus being globally visible.
811 */
812 if (stream->globally_visible) {
813 pthread_mutex_lock(&consumer_data.lock);
a9838785 814 pthread_mutex_lock(&stream->chan->lock);
4891ece8
DG
815 pthread_mutex_lock(&stream->lock);
816 /* Remove every reference of the stream in the consumer. */
817 consumer_stream_delete(stream, ht);
818
819 destroy_close_stream(stream);
820
821 /* Update channel's refcount of the stream. */
822 free_chan = unref_channel(stream);
823
824 /* Indicates that the consumer data state MUST be updated after this. */
825 consumer_data.need_update = 1;
826
827 pthread_mutex_unlock(&stream->lock);
a9838785 828 pthread_mutex_unlock(&stream->chan->lock);
4891ece8
DG
829 pthread_mutex_unlock(&consumer_data.lock);
830 } else {
831 /*
832 * If the stream is not visible globally, this needs to be done
833 * outside of the consumer data lock section.
834 */
835 free_chan = unref_channel(stream);
10a50311
JD
836 }
837
10a50311
JD
838 if (free_chan) {
839 consumer_del_channel(free_chan);
840 }
841 } else {
4891ece8 842 destroy_close_stream(stream);
51230d70
DG
843 }
844
845 /* Free stream within a RCU call. */
846 consumer_stream_free(stream);
847}
1c20f0e2
JD
848
849/*
850 * Write index of a specific stream either on the relayd or local disk.
851 *
852 * Return 0 on success or else a negative value.
853 */
854int consumer_stream_write_index(struct lttng_consumer_stream *stream,
e0547b83 855 struct ctf_packet_index *element)
1c20f0e2
JD
856{
857 int ret;
1c20f0e2
JD
858
859 assert(stream);
e0547b83 860 assert(element);
1c20f0e2
JD
861
862 rcu_read_lock();
6d40f8fa 863 if (stream->relayd_id != (uint64_t) -1ULL) {
aad71b20 864 struct consumer_relayd_sock_pair *relayd;
1121820b 865
6d40f8fa 866 relayd = consumer_find_relayd(stream->relayd_id);
aad71b20
JR
867 if (relayd) {
868 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
869 ret = relayd_send_index(&relayd->control_sock, element,
1c20f0e2 870 stream->relayd_stream_id, stream->next_net_seq_num - 1);
874e1255
JR
871 if (ret < 0) {
872 /*
873 * Communication error with lttng-relayd,
874 * perform cleanup now
875 */
6d40f8fa 876 ERR("Relayd send index failed. Cleaning up relayd %" PRIu64 ".", relayd->id);
874e1255
JR
877 lttng_consumer_cleanup_relayd(relayd);
878 ret = -1;
879 }
aad71b20
JR
880 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
881 } else {
882 ERR("Stream %" PRIu64 " relayd ID %" PRIu64 " unknown. Can't write index.",
6d40f8fa 883 stream->key, stream->relayd_id);
aad71b20
JR
884 ret = -1;
885 }
1c20f0e2 886 } else {
e0547b83 887 if (lttng_index_file_write(stream->index_file, element)) {
6cd525e8
MD
888 ret = -1;
889 } else {
890 ret = 0;
891 }
1c20f0e2
JD
892 }
893 if (ret < 0) {
894 goto error;
895 }
896
897error:
898 rcu_read_unlock();
899 return ret;
29d1a7ae 900}
This page took 0.084208 seconds and 5 git commands to generate.