2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
16 #include <common/common.h>
17 #include <common/consumer/consumer-timer.h>
18 #include <common/consumer/consumer-timer.h>
19 #include <common/consumer/consumer.h>
20 #include <common/consumer/consumer.h>
21 #include <common/consumer/metadata-bucket.h>
22 #include <common/consumer/metadata-bucket.h>
23 #include <common/index/index.h>
24 #include <common/kernel-consumer/kernel-consumer.h>
25 #include <common/kernel-ctl/kernel-ctl.h>
26 #include <common/macros.h>
27 #include <common/relayd/relayd.h>
28 #include <common/ust-consumer/ust-consumer.h>
29 #include <common/utils.h>
31 #include "consumer-stream.h"
34 * RCU call to free stream. MUST only be used with call_rcu().
36 static void free_stream_rcu(struct rcu_head
*head
)
38 struct lttng_ht_node_u64
*node
=
39 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
40 struct lttng_consumer_stream
*stream
=
41 caa_container_of(node
, struct lttng_consumer_stream
, node
);
43 pthread_mutex_destroy(&stream
->lock
);
47 static void consumer_stream_data_lock_all(struct lttng_consumer_stream
*stream
)
49 pthread_mutex_lock(&stream
->chan
->lock
);
50 pthread_mutex_lock(&stream
->lock
);
53 static void consumer_stream_data_unlock_all(struct lttng_consumer_stream
*stream
)
55 pthread_mutex_unlock(&stream
->lock
);
56 pthread_mutex_unlock(&stream
->chan
->lock
);
59 static void consumer_stream_data_assert_locked_all(struct lttng_consumer_stream
*stream
)
61 ASSERT_LOCKED(stream
->lock
);
62 ASSERT_LOCKED(stream
->chan
->lock
);
65 static void consumer_stream_metadata_lock_all(struct lttng_consumer_stream
*stream
)
67 consumer_stream_data_lock_all(stream
);
68 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
71 static void consumer_stream_metadata_unlock_all(struct lttng_consumer_stream
*stream
)
73 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
74 consumer_stream_data_unlock_all(stream
);
77 static void consumer_stream_metadata_assert_locked_all(struct lttng_consumer_stream
*stream
)
79 ASSERT_LOCKED(stream
->metadata_rdv_lock
);
80 consumer_stream_data_assert_locked_all(stream
);
83 /* Only used for data streams. */
84 static int consumer_stream_update_stats(struct lttng_consumer_stream
*stream
,
85 const struct stream_subbuffer
*subbuf
)
88 uint64_t sequence_number
;
89 const uint64_t discarded_events
= subbuf
->info
.data
.events_discarded
;
91 if (!subbuf
->info
.data
.sequence_number
.is_set
) {
92 /* Command not supported by the tracer. */
93 sequence_number
= -1ULL;
94 stream
->sequence_number_unavailable
= true;
96 sequence_number
= subbuf
->info
.data
.sequence_number
.value
;
100 * Start the sequence when we extract the first packet in case we don't
101 * start at 0 (for example if a consumer is not connected to the
102 * session immediately after the beginning).
104 if (stream
->last_sequence_number
== -1ULL) {
105 stream
->last_sequence_number
= sequence_number
;
106 } else if (sequence_number
> stream
->last_sequence_number
) {
107 stream
->chan
->lost_packets
+= sequence_number
-
108 stream
->last_sequence_number
- 1;
110 /* seq <= last_sequence_number */
111 ERR("Sequence number inconsistent : prev = %" PRIu64
112 ", current = %" PRIu64
,
113 stream
->last_sequence_number
, sequence_number
);
117 stream
->last_sequence_number
= sequence_number
;
119 if (discarded_events
< stream
->last_discarded_events
) {
121 * Overflow has occurred. We assume only one wrap-around
124 stream
->chan
->discarded_events
+=
125 (1ULL << (CAA_BITS_PER_LONG
- 1)) -
126 stream
->last_discarded_events
+
129 stream
->chan
->discarded_events
+= discarded_events
-
130 stream
->last_discarded_events
;
132 stream
->last_discarded_events
= discarded_events
;
140 void ctf_packet_index_populate(struct ctf_packet_index
*index
,
141 off_t offset
, const struct stream_subbuffer
*subbuffer
)
143 *index
= (typeof(*index
)){
144 .offset
= htobe64(offset
),
145 .packet_size
= htobe64(subbuffer
->info
.data
.packet_size
),
146 .content_size
= htobe64(subbuffer
->info
.data
.content_size
),
147 .timestamp_begin
= htobe64(
148 subbuffer
->info
.data
.timestamp_begin
),
149 .timestamp_end
= htobe64(
150 subbuffer
->info
.data
.timestamp_end
),
151 .events_discarded
= htobe64(
152 subbuffer
->info
.data
.events_discarded
),
153 .stream_id
= htobe64(subbuffer
->info
.data
.stream_id
),
154 .stream_instance_id
= htobe64(
155 subbuffer
->info
.data
.stream_instance_id
.is_set
?
156 subbuffer
->info
.data
.stream_instance_id
.value
: -1ULL),
157 .packet_seq_num
= htobe64(
158 subbuffer
->info
.data
.sequence_number
.is_set
?
159 subbuffer
->info
.data
.sequence_number
.value
: -1ULL),
163 static ssize_t
consumer_stream_consume_mmap(
164 struct lttng_consumer_local_data
*ctx
,
165 struct lttng_consumer_stream
*stream
,
166 const struct stream_subbuffer
*subbuffer
)
168 const unsigned long padding_size
=
169 subbuffer
->info
.data
.padded_subbuf_size
-
170 subbuffer
->info
.data
.subbuf_size
;
171 const ssize_t written_bytes
= lttng_consumer_on_read_subbuffer_mmap(
172 stream
, &subbuffer
->buffer
.buffer
, padding_size
);
174 if (stream
->net_seq_idx
== -1ULL) {
176 * When writing on disk, check that only the subbuffer (no
177 * padding) was written to disk.
179 if (written_bytes
!= subbuffer
->info
.data
.padded_subbuf_size
) {
180 DBG("Failed to write the entire padded subbuffer on disk (written_bytes: %zd, padded subbuffer size %lu)",
182 subbuffer
->info
.data
.padded_subbuf_size
);
186 * When streaming over the network, check that the entire
187 * subbuffer including padding was successfully written.
189 if (written_bytes
!= subbuffer
->info
.data
.subbuf_size
) {
190 DBG("Failed to write only the subbuffer over the network (written_bytes: %zd, subbuffer size %lu)",
192 subbuffer
->info
.data
.subbuf_size
);
197 * If `lttng_consumer_on_read_subbuffer_mmap()` returned an error, pass
198 * it along to the caller, else return zero.
200 if (written_bytes
< 0) {
201 ERR("Error reading mmap subbuffer: %zd", written_bytes
);
204 return written_bytes
;
207 static ssize_t
consumer_stream_consume_splice(
208 struct lttng_consumer_local_data
*ctx
,
209 struct lttng_consumer_stream
*stream
,
210 const struct stream_subbuffer
*subbuffer
)
212 const ssize_t written_bytes
= lttng_consumer_on_read_subbuffer_splice(
213 ctx
, stream
, subbuffer
->info
.data
.padded_subbuf_size
, 0);
215 if (written_bytes
!= subbuffer
->info
.data
.padded_subbuf_size
) {
216 DBG("Failed to write the entire padded subbuffer (written_bytes: %zd, padded subbuffer size %lu)",
218 subbuffer
->info
.data
.padded_subbuf_size
);
222 * If `lttng_consumer_on_read_subbuffer_splice()` returned an error,
223 * pass it along to the caller, else return zero.
225 if (written_bytes
< 0) {
226 ERR("Error reading splice subbuffer: %zd", written_bytes
);
229 return written_bytes
;
232 static int consumer_stream_send_index(
233 struct lttng_consumer_stream
*stream
,
234 const struct stream_subbuffer
*subbuffer
,
235 struct lttng_consumer_local_data
*ctx
)
237 off_t packet_offset
= 0;
238 struct ctf_packet_index index
= {};
241 * This is called after consuming the sub-buffer; substract the
242 * effect this sub-buffer from the offset.
244 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
245 packet_offset
= stream
->out_fd_offset
-
246 subbuffer
->info
.data
.padded_subbuf_size
;
249 ctf_packet_index_populate(&index
, packet_offset
, subbuffer
);
250 return consumer_stream_write_index(stream
, &index
);
254 * Actually do the metadata sync using the given metadata stream.
256 * Return 0 on success else a negative value. ENODATA can be returned also
257 * indicating that there is no metadata available for that stream.
259 static int do_sync_metadata(struct lttng_consumer_stream
*metadata
,
260 struct lttng_consumer_local_data
*ctx
)
263 enum sync_metadata_status status
;
266 assert(metadata
->metadata_flag
);
270 * In UST, since we have to write the metadata from the cache packet
271 * by packet, we might need to start this procedure multiple times
272 * until all the metadata from the cache has been extracted.
277 * - Lock the metadata stream
278 * - Check if metadata stream node was deleted before locking.
279 * - if yes, release and return success
280 * - Check if new metadata is ready (flush + snapshot pos)
281 * - If nothing : release and return.
282 * - Lock the metadata_rdv_lock
283 * - Unlock the metadata stream
284 * - cond_wait on metadata_rdv to wait the wakeup from the
286 * - Unlock the metadata_rdv_lock
288 pthread_mutex_lock(&metadata
->lock
);
291 * There is a possibility that we were able to acquire a reference on the
292 * stream from the RCU hash table but between then and now, the node might
293 * have been deleted just before the lock is acquired. Thus, after locking,
294 * we make sure the metadata node has not been deleted which means that the
295 * buffers are closed.
297 * In that case, there is no need to sync the metadata hence returning a
298 * success return code.
300 ret
= cds_lfht_is_node_deleted(&metadata
->node
.node
);
303 goto end_unlock_mutex
;
307 case LTTNG_CONSUMER_KERNEL
:
309 * Empty the metadata cache and flush the current stream.
311 status
= lttng_kconsumer_sync_metadata(metadata
);
313 case LTTNG_CONSUMER32_UST
:
314 case LTTNG_CONSUMER64_UST
:
316 * Ask the sessiond if we have new metadata waiting and update the
317 * consumer metadata cache.
319 status
= lttng_ustconsumer_sync_metadata(ctx
, metadata
);
326 case SYNC_METADATA_STATUS_NEW_DATA
:
328 case SYNC_METADATA_STATUS_NO_DATA
:
330 goto end_unlock_mutex
;
331 case SYNC_METADATA_STATUS_ERROR
:
333 goto end_unlock_mutex
;
339 * At this point, new metadata have been flushed, so we wait on the
340 * rendez-vous point for the metadata thread to wake us up when it
341 * finishes consuming the metadata and continue execution.
344 pthread_mutex_lock(&metadata
->metadata_rdv_lock
);
347 * Release metadata stream lock so the metadata thread can process it.
349 pthread_mutex_unlock(&metadata
->lock
);
352 * Wait on the rendez-vous point. Once woken up, it means the metadata was
353 * consumed and thus synchronization is achieved.
355 pthread_cond_wait(&metadata
->metadata_rdv
, &metadata
->metadata_rdv_lock
);
356 pthread_mutex_unlock(&metadata
->metadata_rdv_lock
);
357 } while (status
== SYNC_METADATA_STATUS_NEW_DATA
);
363 pthread_mutex_unlock(&metadata
->lock
);
368 * Synchronize the metadata using a given session ID. A successful acquisition
369 * of a metadata stream will trigger a request to the session daemon and a
370 * snapshot so the metadata thread can consume it.
372 * This function call is a rendez-vous point between the metadata thread and
375 * Return 0 on success or else a negative value.
377 int consumer_stream_sync_metadata(struct lttng_consumer_local_data
*ctx
,
381 struct lttng_consumer_stream
*stream
= NULL
;
382 struct lttng_ht_iter iter
;
387 /* Ease our life a bit. */
388 ht
= the_consumer_data
.stream_list_ht
;
392 /* Search the metadata associated with the session id of the given stream. */
394 cds_lfht_for_each_entry_duplicate(ht
->ht
,
395 ht
->hash_fct(&session_id
, lttng_ht_seed
), ht
->match_fct
,
396 &session_id
, &iter
.iter
, stream
, node_session_id
.node
) {
397 if (!stream
->metadata_flag
) {
401 ret
= do_sync_metadata(stream
, ctx
);
408 * Force return code to 0 (success) since ret might be ENODATA for instance
409 * which is not an error but rather that we should come back.
418 static int consumer_stream_sync_metadata_index(
419 struct lttng_consumer_stream
*stream
,
420 const struct stream_subbuffer
*subbuffer
,
421 struct lttng_consumer_local_data
*ctx
)
423 bool missed_metadata_flush
;
426 /* Block until all the metadata is sent. */
427 pthread_mutex_lock(&stream
->metadata_timer_lock
);
428 assert(!stream
->missed_metadata_flush
);
429 stream
->waiting_on_metadata
= true;
430 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
432 ret
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
434 pthread_mutex_lock(&stream
->metadata_timer_lock
);
435 stream
->waiting_on_metadata
= false;
436 missed_metadata_flush
= stream
->missed_metadata_flush
;
437 if (missed_metadata_flush
) {
438 stream
->missed_metadata_flush
= false;
440 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
445 ret
= consumer_stream_send_index(stream
, subbuffer
, ctx
);
447 * Send the live inactivity beacon to handle the situation where
448 * the live timer is prevented from sampling this stream
449 * because the stream lock was being held while this stream is
450 * waiting on metadata. This ensures live viewer progress in the
451 * unlikely scenario where a live timer would be prevented from
452 * locking a stream lock repeatedly due to a steady flow of
453 * incoming metadata, for a stream which is mostly inactive.
455 * It is important to send the inactivity beacon packet to
456 * relayd _after_ sending the index associated with the data
457 * that was just sent, otherwise this can cause live viewers to
458 * observe timestamps going backwards between an inactivity
459 * beacon and a following trace packet.
461 if (missed_metadata_flush
) {
462 (void) stream
->read_subbuffer_ops
.send_live_beacon(stream
);
469 * Check if the local version of the metadata stream matches with the version
470 * of the metadata stream in the kernel. If it was updated, set the reset flag
474 int metadata_stream_check_version(struct lttng_consumer_stream
*stream
,
475 const struct stream_subbuffer
*subbuffer
)
477 if (stream
->metadata_version
== subbuffer
->info
.metadata
.version
) {
481 DBG("New metadata version detected");
482 consumer_stream_metadata_set_version(stream
,
483 subbuffer
->info
.metadata
.version
);
485 if (stream
->read_subbuffer_ops
.reset_metadata
) {
486 stream
->read_subbuffer_ops
.reset_metadata(stream
);
494 bool stream_is_rotating_to_null_chunk(
495 const struct lttng_consumer_stream
*stream
)
497 bool rotating_to_null_chunk
= false;
499 if (stream
->rotate_position
== -1ULL) {
500 /* No rotation ongoing. */
504 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
||
505 !stream
->chan
->trace_chunk
) {
506 rotating_to_null_chunk
= true;
509 return rotating_to_null_chunk
;
512 enum consumer_stream_open_packet_status
consumer_stream_open_packet(
513 struct lttng_consumer_stream
*stream
)
516 enum consumer_stream_open_packet_status status
;
517 unsigned long produced_pos_before
, produced_pos_after
;
519 ret
= lttng_consumer_sample_snapshot_positions(stream
);
521 ERR("Failed to snapshot positions before post-rotation empty packet flush: stream id = %" PRIu64
522 ", channel name = %s, session id = %" PRIu64
,
523 stream
->key
, stream
->chan
->name
,
524 stream
->chan
->session_id
);
525 status
= CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
;
529 ret
= lttng_consumer_get_produced_snapshot(
530 stream
, &produced_pos_before
);
532 ERR("Failed to read produced position before post-rotation empty packet flush: stream id = %" PRIu64
533 ", channel name = %s, session id = %" PRIu64
,
534 stream
->key
, stream
->chan
->name
,
535 stream
->chan
->session_id
);
536 status
= CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
;
540 ret
= consumer_stream_flush_buffer(stream
, 0);
542 ERR("Failed to flush an empty packet at rotation point: stream id = %" PRIu64
543 ", channel name = %s, session id = %" PRIu64
,
544 stream
->key
, stream
->chan
->name
,
545 stream
->chan
->session_id
);
546 status
= CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
;
550 ret
= lttng_consumer_sample_snapshot_positions(stream
);
552 ERR("Failed to snapshot positions after post-rotation empty packet flush: stream id = %" PRIu64
553 ", channel name = %s, session id = %" PRIu64
,
554 stream
->key
, stream
->chan
->name
,
555 stream
->chan
->session_id
);
556 status
= CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
;
560 ret
= lttng_consumer_get_produced_snapshot(stream
, &produced_pos_after
);
562 ERR("Failed to read produced position after post-rotation empty packet flush: stream id = %" PRIu64
563 ", channel name = %s, session id = %" PRIu64
,
564 stream
->key
, stream
->chan
->name
,
565 stream
->chan
->session_id
);
566 status
= CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
;
571 * Determine if the flush had an effect by comparing the produced
572 * positons before and after the flush.
574 status
= produced_pos_before
!= produced_pos_after
?
575 CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
576 CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
;
577 if (status
== CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
) {
578 stream
->opened_packet_in_current_trace_chunk
= true;
586 * An attempt to open a new packet is performed after a rotation completes to
587 * get a begin timestamp as close as possible to the rotation point.
589 * However, that initial attempt at opening a packet can fail due to a full
590 * ring-buffer. In that case, a second attempt is performed after consuming
591 * a packet since that will have freed enough space in the ring-buffer.
594 int post_consume_open_new_packet(struct lttng_consumer_stream
*stream
,
595 const struct stream_subbuffer
*subbuffer
,
596 struct lttng_consumer_local_data
*ctx
)
600 if (!stream
->opened_packet_in_current_trace_chunk
&&
601 stream
->trace_chunk
&&
602 !stream_is_rotating_to_null_chunk(stream
)) {
603 const enum consumer_stream_open_packet_status status
=
604 consumer_stream_open_packet(stream
);
607 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
608 DBG("Opened a packet after consuming a packet rotation: stream id = %" PRIu64
609 ", channel name = %s, session id = %" PRIu64
,
610 stream
->key
, stream
->chan
->name
,
611 stream
->chan
->session_id
);
612 stream
->opened_packet_in_current_trace_chunk
= true;
614 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
616 * Can't open a packet as there is no space left.
617 * This means that new events were produced, resulting
618 * in a packet being opened, which is what we want
621 DBG("No space left to open a packet after consuming a packet: stream id = %" PRIu64
622 ", channel name = %s, session id = %" PRIu64
,
623 stream
->key
, stream
->chan
->name
,
624 stream
->chan
->session_id
);
625 stream
->opened_packet_in_current_trace_chunk
= true;
627 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
628 /* Logged by callee. */
635 stream
->opened_packet_in_current_trace_chunk
= true;
642 struct lttng_consumer_stream
*consumer_stream_create(
643 struct lttng_consumer_channel
*channel
,
644 uint64_t channel_key
,
646 const char *channel_name
,
649 struct lttng_trace_chunk
*trace_chunk
,
652 enum consumer_channel_type type
,
653 unsigned int monitor
)
656 struct lttng_consumer_stream
*stream
;
658 stream
= zmalloc(sizeof(*stream
));
659 if (stream
== NULL
) {
660 PERROR("malloc struct lttng_consumer_stream");
667 if (trace_chunk
&& !lttng_trace_chunk_get(trace_chunk
)) {
668 ERR("Failed to acquire trace chunk reference during the creation of a stream");
673 stream
->send_node
= CDS_LIST_HEAD_INIT(stream
->send_node
);
674 stream
->chan
= channel
;
675 stream
->key
= stream_key
;
676 stream
->trace_chunk
= trace_chunk
;
678 stream
->out_fd_offset
= 0;
679 stream
->output_written
= 0;
680 stream
->net_seq_idx
= relayd_id
;
681 stream
->session_id
= session_id
;
682 stream
->monitor
= monitor
;
683 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
684 stream
->index_file
= NULL
;
685 stream
->last_sequence_number
= -1ULL;
686 stream
->rotate_position
= -1ULL;
687 /* Buffer is created with an open packet. */
688 stream
->opened_packet_in_current_trace_chunk
= true;
689 pthread_mutex_init(&stream
->lock
, NULL
);
690 pthread_mutex_init(&stream
->metadata_timer_lock
, NULL
);
692 /* If channel is the metadata, flag this stream as metadata. */
693 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
694 stream
->metadata_flag
= 1;
695 /* Metadata is flat out. */
696 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
697 /* Live rendez-vous point. */
698 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
699 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
701 /* Format stream name to <channel_name>_<cpu_number> */
702 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
705 PERROR("snprintf stream name");
710 switch (channel
->output
) {
711 case CONSUMER_CHANNEL_SPLICE
:
712 stream
->output
= LTTNG_EVENT_SPLICE
;
713 ret
= utils_create_pipe(stream
->splice_pipe
);
718 case CONSUMER_CHANNEL_MMAP
:
719 stream
->output
= LTTNG_EVENT_MMAP
;
725 /* Key is always the wait_fd for streams. */
726 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
728 /* Init node per channel id key */
729 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
731 /* Init session id node with the stream session id */
732 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
734 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
735 " relayd_id %" PRIu64
", session_id %" PRIu64
,
736 stream
->name
, stream
->key
, channel_key
,
737 stream
->net_seq_idx
, stream
->session_id
);
741 lttng_dynamic_array_init(&stream
->read_subbuffer_ops
.post_consume_cbs
,
742 sizeof(post_consume_cb
), NULL
);
744 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
745 stream
->read_subbuffer_ops
.lock
=
746 consumer_stream_metadata_lock_all
;
747 stream
->read_subbuffer_ops
.unlock
=
748 consumer_stream_metadata_unlock_all
;
749 stream
->read_subbuffer_ops
.assert_locked
=
750 consumer_stream_metadata_assert_locked_all
;
751 stream
->read_subbuffer_ops
.pre_consume_subbuffer
=
752 metadata_stream_check_version
;
754 const post_consume_cb post_consume_index_op
= channel
->is_live
?
755 consumer_stream_sync_metadata_index
:
756 consumer_stream_send_index
;
758 ret
= lttng_dynamic_array_add_element(
759 &stream
->read_subbuffer_ops
.post_consume_cbs
,
760 &post_consume_index_op
);
762 PERROR("Failed to add `send index` callback to stream's post consumption callbacks");
766 ret
= lttng_dynamic_array_add_element(
767 &stream
->read_subbuffer_ops
.post_consume_cbs
,
768 &(post_consume_cb
) { post_consume_open_new_packet
});
770 PERROR("Failed to add `open new packet` callback to stream's post consumption callbacks");
774 stream
->read_subbuffer_ops
.lock
= consumer_stream_data_lock_all
;
775 stream
->read_subbuffer_ops
.unlock
=
776 consumer_stream_data_unlock_all
;
777 stream
->read_subbuffer_ops
.assert_locked
=
778 consumer_stream_data_assert_locked_all
;
779 stream
->read_subbuffer_ops
.pre_consume_subbuffer
=
780 consumer_stream_update_stats
;
783 if (channel
->output
== CONSUMER_CHANNEL_MMAP
) {
784 stream
->read_subbuffer_ops
.consume_subbuffer
=
785 consumer_stream_consume_mmap
;
787 stream
->read_subbuffer_ops
.consume_subbuffer
=
788 consumer_stream_consume_splice
;
795 lttng_trace_chunk_put(stream
->trace_chunk
);
796 lttng_dynamic_array_reset(&stream
->read_subbuffer_ops
.post_consume_cbs
);
806 * Close stream on the relayd side. This call can destroy a relayd if the
807 * conditions are met.
809 * A RCU read side lock MUST be acquired if the relayd object was looked up in
810 * a hash table before calling this.
812 void consumer_stream_relayd_close(struct lttng_consumer_stream
*stream
,
813 struct consumer_relayd_sock_pair
*relayd
)
820 if (stream
->sent_to_relayd
) {
821 uatomic_dec(&relayd
->refcount
);
822 assert(uatomic_read(&relayd
->refcount
) >= 0);
825 /* Closing streams requires to lock the control socket. */
826 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
827 ret
= relayd_send_close_stream(&relayd
->control_sock
,
828 stream
->relayd_stream_id
,
829 stream
->next_net_seq_num
- 1);
830 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
832 ERR("Relayd send close stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
833 lttng_consumer_cleanup_relayd(relayd
);
836 /* Both conditions are met, we destroy the relayd. */
837 if (uatomic_read(&relayd
->refcount
) == 0 &&
838 uatomic_read(&relayd
->destroy_flag
)) {
839 consumer_destroy_relayd(relayd
);
841 stream
->net_seq_idx
= (uint64_t) -1ULL;
842 stream
->sent_to_relayd
= 0;
846 * Close stream's file descriptors and, if needed, close stream also on the
849 * The consumer data lock MUST be acquired.
850 * The stream lock MUST be acquired.
852 void consumer_stream_close(struct lttng_consumer_stream
*stream
)
855 struct consumer_relayd_sock_pair
*relayd
;
859 switch (the_consumer_data
.type
) {
860 case LTTNG_CONSUMER_KERNEL
:
861 if (stream
->mmap_base
!= NULL
) {
862 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
868 if (stream
->wait_fd
>= 0) {
869 ret
= close(stream
->wait_fd
);
873 stream
->wait_fd
= -1;
875 if (stream
->chan
->output
== CONSUMER_CHANNEL_SPLICE
) {
876 utils_close_pipe(stream
->splice_pipe
);
879 case LTTNG_CONSUMER32_UST
:
880 case LTTNG_CONSUMER64_UST
:
883 * Special case for the metadata since the wait fd is an internal pipe
884 * polled in the metadata thread.
886 if (stream
->metadata_flag
&& stream
->chan
->monitor
) {
887 int rpipe
= stream
->ust_metadata_poll_pipe
[0];
890 * This will stop the channel timer if one and close the write side
891 * of the metadata poll pipe.
893 lttng_ustconsumer_close_metadata(stream
->chan
);
897 PERROR("closing metadata pipe read side");
899 stream
->ust_metadata_poll_pipe
[0] = -1;
905 ERR("Unknown consumer_data type");
909 /* Close output fd. Could be a socket or local file at this point. */
910 if (stream
->out_fd
>= 0) {
911 ret
= close(stream
->out_fd
);
918 if (stream
->index_file
) {
919 lttng_index_file_put(stream
->index_file
);
920 stream
->index_file
= NULL
;
923 lttng_trace_chunk_put(stream
->trace_chunk
);
924 stream
->trace_chunk
= NULL
;
926 /* Check and cleanup relayd if needed. */
928 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
929 if (relayd
!= NULL
) {
930 consumer_stream_relayd_close(stream
, relayd
);
936 * Delete the stream from all possible hash tables.
938 * The consumer data lock MUST be acquired.
939 * The stream lock MUST be acquired.
941 void consumer_stream_delete(struct lttng_consumer_stream
*stream
,
945 struct lttng_ht_iter iter
;
948 /* Should NEVER be called not in monitor mode. */
949 assert(stream
->chan
->monitor
);
954 iter
.iter
.node
= &stream
->node
.node
;
955 ret
= lttng_ht_del(ht
, &iter
);
959 /* Delete from stream per channel ID hash table. */
960 iter
.iter
.node
= &stream
->node_channel_id
.node
;
962 * The returned value is of no importance. Even if the node is NOT in the
963 * hash table, we continue since we may have been called by a code path
964 * that did not add the stream to a (all) hash table. Same goes for the
965 * next call ht del call.
967 (void) lttng_ht_del(the_consumer_data
.stream_per_chan_id_ht
, &iter
);
969 /* Delete from the global stream list. */
970 iter
.iter
.node
= &stream
->node_session_id
.node
;
971 /* See the previous ht del on why we ignore the returned value. */
972 (void) lttng_ht_del(the_consumer_data
.stream_list_ht
, &iter
);
976 if (!stream
->metadata_flag
) {
977 /* Decrement the stream count of the global consumer data. */
978 assert(the_consumer_data
.stream_count
> 0);
979 the_consumer_data
.stream_count
--;
984 * Free the given stream within a RCU call.
986 void consumer_stream_free(struct lttng_consumer_stream
*stream
)
990 metadata_bucket_destroy(stream
->metadata_bucket
);
991 call_rcu(&stream
->node
.head
, free_stream_rcu
);
995 * Destroy the stream's buffers of the tracer.
997 void consumer_stream_destroy_buffers(struct lttng_consumer_stream
*stream
)
1001 switch (the_consumer_data
.type
) {
1002 case LTTNG_CONSUMER_KERNEL
:
1004 case LTTNG_CONSUMER32_UST
:
1005 case LTTNG_CONSUMER64_UST
:
1006 lttng_ustconsumer_del_stream(stream
);
1009 ERR("Unknown consumer_data type");
1015 * Destroy and close a already created stream.
1017 static void destroy_close_stream(struct lttng_consumer_stream
*stream
)
1021 DBG("Consumer stream destroy monitored key: %" PRIu64
, stream
->key
);
1023 /* Destroy tracer buffers of the stream. */
1024 consumer_stream_destroy_buffers(stream
);
1025 /* Close down everything including the relayd if one. */
1026 consumer_stream_close(stream
);
1030 * Decrement the stream's channel refcount and if down to 0, return the channel
1031 * pointer so it can be destroyed by the caller or NULL if not.
1033 static struct lttng_consumer_channel
*unref_channel(
1034 struct lttng_consumer_stream
*stream
)
1036 struct lttng_consumer_channel
*free_chan
= NULL
;
1039 assert(stream
->chan
);
1041 /* Update refcount of channel and see if we need to destroy it. */
1042 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1043 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1044 free_chan
= stream
->chan
;
1051 * Destroy a stream completely. This will delete, close and free the stream.
1052 * Once return, the stream is NO longer usable. Its channel may get destroyed
1053 * if conditions are met for a monitored stream.
1055 * This MUST be called WITHOUT the consumer data and stream lock acquired if
1056 * the stream is in _monitor_ mode else it does not matter.
1058 void consumer_stream_destroy(struct lttng_consumer_stream
*stream
,
1059 struct lttng_ht
*ht
)
1063 cds_list_del_init(&stream
->send_node
);
1065 /* Stream is in monitor mode. */
1066 if (stream
->monitor
) {
1067 struct lttng_consumer_channel
*free_chan
= NULL
;
1070 * This means that the stream was successfully removed from the streams
1071 * list of the channel and sent to the right thread managing this
1072 * stream thus being globally visible.
1074 if (stream
->globally_visible
) {
1075 pthread_mutex_lock(&the_consumer_data
.lock
);
1076 pthread_mutex_lock(&stream
->chan
->lock
);
1077 pthread_mutex_lock(&stream
->lock
);
1078 /* Remove every reference of the stream in the consumer. */
1079 consumer_stream_delete(stream
, ht
);
1081 destroy_close_stream(stream
);
1083 /* Update channel's refcount of the stream. */
1084 free_chan
= unref_channel(stream
);
1086 /* Indicates that the consumer data state MUST be updated after this. */
1087 the_consumer_data
.need_update
= 1;
1089 pthread_mutex_unlock(&stream
->lock
);
1090 pthread_mutex_unlock(&stream
->chan
->lock
);
1091 pthread_mutex_unlock(&the_consumer_data
.lock
);
1094 * If the stream is not visible globally, this needs to be done
1095 * outside of the consumer data lock section.
1097 destroy_close_stream(stream
);
1098 free_chan
= unref_channel(stream
);
1102 consumer_del_channel(free_chan
);
1105 destroy_close_stream(stream
);
1108 /* Free stream within a RCU call. */
1109 lttng_trace_chunk_put(stream
->trace_chunk
);
1110 stream
->trace_chunk
= NULL
;
1111 lttng_dynamic_array_reset(&stream
->read_subbuffer_ops
.post_consume_cbs
);
1112 consumer_stream_free(stream
);
1116 * Write index of a specific stream either on the relayd or local disk.
1118 * Return 0 on success or else a negative value.
1120 int consumer_stream_write_index(struct lttng_consumer_stream
*stream
,
1121 struct ctf_packet_index
*element
)
1129 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1130 struct consumer_relayd_sock_pair
*relayd
;
1131 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1133 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1134 ret
= relayd_send_index(&relayd
->control_sock
, element
,
1135 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1138 * Communication error with lttng-relayd,
1139 * perform cleanup now
1141 ERR("Relayd send index failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1142 lttng_consumer_cleanup_relayd(relayd
);
1145 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1147 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't write index.",
1148 stream
->key
, stream
->net_seq_idx
);
1152 if (lttng_index_file_write(stream
->index_file
, element
)) {
1167 int consumer_stream_create_output_files(struct lttng_consumer_stream
*stream
,
1171 enum lttng_trace_chunk_status chunk_status
;
1172 const int flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
1173 const mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
1174 char stream_path
[LTTNG_PATH_MAX
];
1176 ASSERT_LOCKED(stream
->lock
);
1177 assert(stream
->trace_chunk
);
1179 ret
= utils_stream_file_path(stream
->chan
->pathname
, stream
->name
,
1180 stream
->chan
->tracefile_size
,
1181 stream
->tracefile_count_current
, NULL
,
1182 stream_path
, sizeof(stream_path
));
1187 if (stream
->out_fd
>= 0) {
1188 ret
= close(stream
->out_fd
);
1190 PERROR("Failed to close stream file \"%s\"",
1194 stream
->out_fd
= -1;
1197 DBG("Opening stream output file \"%s\"", stream_path
);
1198 chunk_status
= lttng_trace_chunk_open_file(stream
->trace_chunk
, stream_path
,
1199 flags
, mode
, &stream
->out_fd
, false);
1200 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1201 ERR("Failed to open stream file \"%s\"", stream
->name
);
1206 if (!stream
->metadata_flag
&& (create_index
|| stream
->index_file
)) {
1207 if (stream
->index_file
) {
1208 lttng_index_file_put(stream
->index_file
);
1210 chunk_status
= lttng_index_file_create_from_trace_chunk(
1211 stream
->trace_chunk
,
1212 stream
->chan
->pathname
,
1214 stream
->chan
->tracefile_size
,
1215 stream
->tracefile_count_current
,
1216 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
,
1217 false, &stream
->index_file
);
1218 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1224 /* Reset current size because we just perform a rotation. */
1225 stream
->tracefile_size_current
= 0;
1226 stream
->out_fd_offset
= 0;
1231 int consumer_stream_rotate_output_files(struct lttng_consumer_stream
*stream
)
1235 stream
->tracefile_count_current
++;
1236 if (stream
->chan
->tracefile_count
> 0) {
1237 stream
->tracefile_count_current
%=
1238 stream
->chan
->tracefile_count
;
1241 DBG("Rotating output files of stream \"%s\"", stream
->name
);
1242 ret
= consumer_stream_create_output_files(stream
, true);
1251 bool consumer_stream_is_deleted(struct lttng_consumer_stream
*stream
)
1254 * This function does not take a const stream since
1255 * cds_lfht_is_node_deleted was not const before liburcu 0.12.
1258 return cds_lfht_is_node_deleted(&stream
->node
.node
);
1261 static ssize_t
metadata_bucket_flush(
1262 const struct stream_subbuffer
*buffer
, void *data
)
1265 struct lttng_consumer_stream
*stream
= data
;
1267 ret
= consumer_stream_consume_mmap(NULL
, stream
, buffer
);
1275 static ssize_t
metadata_bucket_consume(
1276 struct lttng_consumer_local_data
*unused
,
1277 struct lttng_consumer_stream
*stream
,
1278 const struct stream_subbuffer
*subbuffer
)
1281 enum metadata_bucket_status status
;
1283 status
= metadata_bucket_fill(stream
->metadata_bucket
, subbuffer
);
1285 case METADATA_BUCKET_STATUS_OK
:
1286 /* Return consumed size. */
1287 ret
= subbuffer
->buffer
.buffer
.size
;
1296 int consumer_stream_enable_metadata_bucketization(
1297 struct lttng_consumer_stream
*stream
)
1301 assert(stream
->metadata_flag
);
1302 assert(!stream
->metadata_bucket
);
1303 assert(stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
);
1305 stream
->metadata_bucket
= metadata_bucket_create(
1306 metadata_bucket_flush
, stream
);
1307 if (!stream
->metadata_bucket
) {
1312 stream
->read_subbuffer_ops
.consume_subbuffer
= metadata_bucket_consume
;
1317 void consumer_stream_metadata_set_version(
1318 struct lttng_consumer_stream
*stream
, uint64_t new_version
)
1320 assert(new_version
> stream
->metadata_version
);
1321 stream
->metadata_version
= new_version
;
1322 stream
->reset_metadata_flag
= 1;
1324 if (stream
->metadata_bucket
) {
1325 metadata_bucket_reset(stream
->metadata_bucket
);
1329 int consumer_stream_flush_buffer(struct lttng_consumer_stream
*stream
,
1330 bool producer_active
)
1334 switch (the_consumer_data
.type
) {
1335 case LTTNG_CONSUMER_KERNEL
:
1336 if (producer_active
) {
1337 ret
= kernctl_buffer_flush(stream
->wait_fd
);
1339 ERR("Failed to flush kernel stream");
1343 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
1346 * Doing a buffer flush which does not take into
1347 * account empty packets. This is not perfect,
1348 * but required as a fall-back when
1349 * "flush_empty" is not implemented by
1352 ret
= kernctl_buffer_flush(stream
->wait_fd
);
1354 ERR("Failed to flush kernel stream");
1360 case LTTNG_CONSUMER32_UST
:
1361 case LTTNG_CONSUMER64_UST
:
1362 ret
= lttng_ustconsumer_flush_buffer(stream
, (int) producer_active
);
1365 ERR("Unknown consumer_data type");