Skip to main content

archivist_lib/logs/
shared_buffer.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5mod cursor;
6
7pub use cursor::{FilterCursor, FilterCursorStream};
8
9use crate::identity::ComponentIdentity;
10use crate::logs::repository::ARCHIVIST_MONIKER;
11use crate::logs::stats::{LogStreamStats, SaturationCurve};
12use derivative::Derivative;
13use diagnostics_log_encoding::{Header, TRACING_FORMAT_LOG_RECORD_TYPE};
14use fidl_fuchsia_diagnostics::{ComponentSelector, StreamMode};
15use fidl_fuchsia_logger::MAX_DATAGRAM_LEN_BYTES;
16use fuchsia_async as fasync;
17use fuchsia_async::condition::{Condition, ConditionGuard};
18use fuchsia_inspect::Node;
19use fuchsia_sync::Mutex;
20use futures::channel::oneshot;
21use log::debug;
22use ring_buffer::{MAX_MESSAGE_SIZE, Reader, RingBuffer, ring_buffer_record_len};
23use std::collections::VecDeque;
24use std::mem::ManuallyDrop;
25use std::ops::{Deref, DerefMut, Range};
26use std::sync::{Arc, Weak};
27use std::time::Duration;
28use zerocopy::FromBytes;
29
30// Aim to keep 25% of the ring buffer free. This is expressed as a fraction: numerator / denominator.
31const SPACE_THRESHOLD_NUMERATOR: usize = 1;
32const SPACE_THRESHOLD_DENOMINATOR: usize = 4;
33
34// The default amount of time that Archivist will sleep for to reduce how often it wakes up to
35// handle log messages.
36const DEFAULT_SLEEP_TIME: Duration = Duration::from_millis(200);
37
38// Target uncompressed size of each LZ4-compressed chunk.
39const CHUNK_SIZE: usize = 65536;
40
41pub struct Buffer {
42    // The underlying IOBuffer ring buffer reader used as the ingestion staging area.
43    reader: Reader,
44
45    // Target budget in bytes for the compressed log buffer (`logs_max_cached_original_bytes`).
46    capacity: usize,
47}
48
49impl Deref for Buffer {
50    type Target = Reader;
51
52    fn deref(&self) -> &Self::Target {
53        &self.reader
54    }
55}
56
57impl DerefMut for Buffer {
58    fn deref_mut(&mut self) -> &mut Self::Target {
59        &mut self.reader
60    }
61}
62
63pub fn create_ring_buffer(capacity: usize) -> Buffer {
64    let page_size = zx::system_get_page_size() as usize;
65    let iob_capacity = (capacity * SPACE_THRESHOLD_NUMERATOR / SPACE_THRESHOLD_DENOMINATOR)
66        .max(MAX_MESSAGE_SIZE)
67        .next_multiple_of(page_size);
68    Buffer {
69        reader: RingBuffer::create(iob_capacity),
70        capacity: capacity.max(CHUNK_SIZE).max(MAX_MESSAGE_SIZE),
71    }
72}
73
74const IOB_PEER_CLOSED_KEY_BASE: u64 = 0x8000_0000_0000_0000;
75
76pub type OnInactive = Box<dyn Fn(Arc<ComponentIdentity>) + Send + Sync>;
77
78struct CompressedChunk {
79    // Monotonic byte range `start..end` in `Inner::data` (guaranteed not to cross a multiple of
80    // `Inner::data.len()`).
81    range: Range<u64>,
82
83    // Uncompressed byte length of this chunk. If equal to `range.end - range.start`, the chunk
84    // was not compressible and is stored uncompressed in `Inner::data`.
85    uncompressed_len: u32,
86
87    // Contiguous range of message IDs stored in this chunk.
88    msg_ids: Range<u64>,
89}
90
91pub struct SharedBuffer {
92    inner: Condition<Inner>,
93
94    // Sockets. This *must* be locked after `inner` and the lock *must* be dropped before
95    // `InnerGuard` is dropped.
96    sockets: Mutex<Slab<Socket>>,
97
98    // Callback for when a container is inactive (i.e. no logs and no sockets).
99    on_inactive: OnInactive,
100
101    // The port that we use to service the sockets.
102    port: zx::Port,
103
104    // Event used for termination.
105    event: zx::Event,
106
107    // The thread that we use to service the sockets.
108    socket_thread: Mutex<Option<std::thread::JoinHandle<()>>>,
109
110    // The task responsible for draining the IOBuffer ring buffer into the compressed buffer and
111    // waking cursors whenever new data arrives.
112    _buffer_monitor_task: fasync::Task<()>,
113
114    /// Metric for tracking buffer saturation.
115    pub(crate) saturation_curve: Arc<SaturationCurve>,
116}
117
118struct InnerGuard<'a> {
119    buffer: &'a Arc<SharedBuffer>,
120
121    guard: ManuallyDrop<ConditionGuard<'a, Inner>>,
122
123    // The list of components that should be reported as inactive once the lock is dropped.
124    on_inactive: Vec<Arc<ComponentIdentity>>,
125
126    // If true, wake all the wakers registered with the ConditionGuard.
127    wake: bool,
128}
129
130impl Drop for InnerGuard<'_> {
131    fn drop(&mut self) {
132        if self.wake {
133            for waker in self.guard.drain_wakers() {
134                waker.wake();
135            }
136        }
137        // SAFETY: This is the only place we drop the guard.
138        unsafe {
139            ManuallyDrop::drop(&mut self.guard);
140        }
141        for identity in self.on_inactive.drain(..) {
142            (*self.buffer.on_inactive)(identity);
143        }
144    }
145}
146
147impl Deref for InnerGuard<'_> {
148    type Target = Inner;
149
150    fn deref(&self) -> &Self::Target {
151        &self.guard
152    }
153}
154
155impl DerefMut for InnerGuard<'_> {
156    fn deref_mut(&mut self) -> &mut Self::Target {
157        &mut self.guard
158    }
159}
160
161struct Inner {
162    // The ring buffer.
163    ring_buffer: Arc<RingBuffer>,
164
165    // Registered containers.
166    containers: Containers,
167
168    // Socket thread message queue.
169    thread_msg_queue: VecDeque<ThreadMessage>,
170
171    // A copy of the tail index in the ring buffer.
172    tail: u64,
173
174    // Single pre-allocated circular buffer holding all sealed compressed chunks.
175    data: Box<[u8]>,
176
177    // Monotonic byte offset in `data` where the next sealed compressed chunk will be written.
178    compressed_head: u64,
179
180    // Metadata for sealed chunks in `data` in chronological order.
181    chunks: VecDeque<CompressedChunk>,
182
183    // Pre-allocated 64 KiB (`CHUNK_SIZE`) staging buffer for the uncompressed chunk currently being
184    // built.
185    active_chunk: Vec<u8>,
186
187    // Contiguous range of message IDs currently stored in `active_chunk`.
188    active_msg_ids: Range<u64>,
189
190    // Monotonically increasing message ID assigned to the next record extracted into `active_chunk`.
191    next_msg_id: u64,
192
193    // The IOBuffer peers that we must watch.
194    iob_peers: Slab<(ContainerId, zx::Iob)>,
195
196    // True, when the buffer has been terminated.
197    terminated: bool,
198}
199
200enum ThreadMessage {
201    // The thread should terminate.
202    Terminate,
203
204    // Process all pending socket messages and report via the Sender when done.
205    Flush(oneshot::Sender<()>),
206}
207
208pub struct SharedBufferOptions {
209    // To reduce how often Archivist wakes when new messages are written, Archivist will sleep for
210    // this time. This will impact how quickly messages show up via the cursors.
211    pub sleep_time: Duration,
212    /// The size of the circular buffer for the saturation curve.
213    pub saturation_curve_size: usize,
214}
215
216impl Default for SharedBufferOptions {
217    fn default() -> Self {
218        Self { sleep_time: DEFAULT_SLEEP_TIME, saturation_curve_size: 254 }
219    }
220}
221
222impl SharedBuffer {
223    /// Returns a new shared buffer and the container for Archivist.
224    pub fn new(
225        ring_buffer: Buffer,
226        on_inactive: OnInactive,
227        options: SharedBufferOptions,
228        inspect_node: &Node,
229    ) -> Arc<Self> {
230        let Buffer { reader: ring_buffer, capacity } = ring_buffer;
231        let saturation_curve =
232            Arc::new(SaturationCurve::new(inspect_node, options.saturation_curve_size));
233        let this = Arc::new_cyclic(|weak: &Weak<Self>| Self {
234            inner: Condition::new(Inner {
235                ring_buffer: Arc::clone(&ring_buffer),
236                containers: Containers::default(),
237                thread_msg_queue: VecDeque::default(),
238                tail: 0,
239                data: vec![0; capacity].into_boxed_slice(),
240                compressed_head: 0,
241                chunks: VecDeque::new(),
242                active_chunk: Vec::with_capacity(CHUNK_SIZE),
243                active_msg_ids: 0..0,
244                next_msg_id: 0,
245                iob_peers: Slab::default(),
246                terminated: false,
247            }),
248            sockets: Mutex::new(Slab::default()),
249            on_inactive,
250            port: zx::Port::create(),
251            event: zx::Event::create(),
252            socket_thread: Mutex::default(),
253            saturation_curve,
254            _buffer_monitor_task: fasync::Task::spawn(Self::buffer_monitor_task(
255                Weak::clone(weak),
256                ring_buffer,
257                options.sleep_time,
258            )),
259        });
260
261        *this.socket_thread.lock() = Some({
262            let this = Arc::clone(&this);
263            std::thread::spawn(move || this.socket_thread(options.sleep_time))
264        });
265        this
266    }
267
268    pub fn new_container_buffer(
269        self: &Arc<Self>,
270        identity: Arc<ComponentIdentity>,
271        stats: Arc<LogStreamStats>,
272    ) -> ContainerBuffer {
273        ContainerBuffer {
274            shared_buffer: Arc::clone(self),
275            container_id: self.inner.lock().containers.new_container(identity, stats),
276        }
277    }
278
279    pub async fn flush(&self) {
280        let (sender, receiver) = oneshot::channel();
281        self.inner.lock().thread_msg_queue.push_back(ThreadMessage::Flush(sender));
282        self.event.signal(zx::Signals::empty(), zx::Signals::USER_0).unwrap();
283        // Ignore failure if Archivist is shutting down.
284        let _ = receiver.await;
285    }
286
287    /// Returns the number of registered containers in use by the buffer.
288    #[cfg(test)]
289    pub fn container_count(&self) -> usize {
290        self.inner.lock().containers.len()
291    }
292
293    /// Terminates the socket thread. The socket thread will drain the sockets before terminating.
294    /// Returns a future that may be awaited if the caller wants to wait for the socket thread to
295    /// terminate.
296    pub fn terminate(self: &Arc<Self>) -> impl Future<Output = ()> {
297        {
298            let mut inner = InnerGuard::new(self);
299            inner.flush_sockets();
300            inner.thread_msg_queue.push_back(ThreadMessage::Terminate);
301        }
302        self.event.signal(zx::Signals::empty(), zx::Signals::USER_0).unwrap();
303        let join_handle = self.socket_thread.lock().take().unwrap();
304        fasync::unblock(|| {
305            let _ = join_handle.join();
306        })
307    }
308
309    /// Returns a cursor that will return messages that match `selectors`. If no selectors are
310    /// specified, *all* messages will be returned.
311    pub fn cursor(
312        self: &Arc<Self>,
313        mode: StreamMode,
314        selectors: Vec<ComponentSelector>,
315    ) -> FilterCursor {
316        InnerGuard::new(self).cursor(mode, selectors)
317    }
318
319    fn socket_thread(self: Arc<Self>, sleep_time: Duration) {
320        const INTERRUPT_KEY: u64 = u64::MAX;
321        let mut sockets_ready = Vec::new();
322        let mut iob_peer_closed = Vec::new();
323        let mut interrupt_needs_arming = true;
324        let mut msg = None;
325
326        loop {
327            let mut deadline = if msg.is_some() {
328                zx::MonotonicInstant::INFINITE_PAST
329            } else {
330                if interrupt_needs_arming {
331                    self.event
332                        .wait_async(
333                            &self.port,
334                            INTERRUPT_KEY,
335                            zx::Signals::USER_0,
336                            zx::WaitAsyncOpts::empty(),
337                        )
338                        .unwrap();
339                    interrupt_needs_arming = false;
340                }
341
342                // Wait so that we're not constantly waking up for every log message that is queued.
343                // Ignore errors here.
344                let _ = self
345                    .event
346                    .wait_one(zx::Signals::USER_0, zx::MonotonicInstant::after(sleep_time.into()));
347                zx::MonotonicInstant::INFINITE
348            };
349
350            // Gather the list of sockets that are ready to read.
351            loop {
352                match self.port.wait(deadline) {
353                    Ok(packet) => {
354                        if packet.key() == INTERRUPT_KEY {
355                            interrupt_needs_arming = true;
356                            // To maintain proper ordering, we must capture the message here whilst
357                            // we are still gathering the list of sockets that are ready to read.
358                            // If we wait till later, we introduce windows where we might miss a
359                            // socket that should be read.
360                            if msg.is_none() {
361                                msg = self.inner.lock().thread_msg_queue.pop_front();
362                            }
363                        } else if packet.key() & IOB_PEER_CLOSED_KEY_BASE != 0 {
364                            iob_peer_closed.push(packet.key() as u32);
365                        } else {
366                            sockets_ready.push(SocketId(packet.key() as u32))
367                        }
368                    }
369                    Err(zx::Status::TIMED_OUT) => break,
370                    Err(status) => panic!("port wait error: {status:?}"),
371                }
372                deadline = zx::MonotonicInstant::INFINITE_PAST;
373            }
374
375            let mut inner = InnerGuard::new(&self);
376
377            if !iob_peer_closed.is_empty() {
378                // See the comment on `is_active()` for why this is required.
379                inner.drain_ring_buffer(inner.ring_buffer.head());
380
381                for iob_peer_closed in iob_peer_closed.drain(..) {
382                    let container_id = inner.iob_peers.free(iob_peer_closed).0;
383                    if let Some(container) = inner.containers.get_mut(container_id) {
384                        container.iob_count -= 1;
385                        if container.iob_count == 0 && !container.is_active() {
386                            if container.should_free() {
387                                inner.containers.free(container_id);
388                            } else {
389                                let identity = Arc::clone(&container.identity);
390                                inner.on_inactive.push(identity);
391                            }
392                        }
393                    }
394                }
395            }
396
397            {
398                let mut sockets = self.sockets.lock();
399                for socket_id in sockets_ready.drain(..) {
400                    inner.read_socket(&mut sockets, socket_id, |socket| {
401                        socket
402                            .socket
403                            .wait_async(
404                                &self.port,
405                                socket_id.0 as u64,
406                                zx::Signals::OBJECT_READABLE | zx::Signals::OBJECT_PEER_CLOSED,
407                                zx::WaitAsyncOpts::empty(),
408                            )
409                            .unwrap();
410                    });
411                }
412            }
413
414            // Now that we've processed the sockets, we can process the message.
415            if let Some(m) = msg.take() {
416                match m {
417                    ThreadMessage::Terminate => {
418                        inner.drain_ring_buffer(inner.ring_buffer.head());
419                        inner.terminated = true;
420                        inner.wake = true;
421                        return;
422                    }
423                    ThreadMessage::Flush(sender) => {
424                        inner.drain_ring_buffer(inner.ring_buffer.head());
425                        let _ = sender.send(());
426                    }
427                }
428
429                // See if there's another message.
430                msg = inner.thread_msg_queue.pop_front();
431                if msg.is_none() {
432                    // If there are no more messages, we must clear the signal so that we get
433                    // notified when the next message arrives. This must be done whilst we are
434                    // holding the lock.
435                    self.event.signal(zx::Signals::USER_0, zx::Signals::empty()).unwrap();
436                }
437            }
438        }
439    }
440
441    async fn buffer_monitor_task(this: Weak<Self>, mut ring_buffer: Reader, sleep_time: Duration) {
442        let mut last_head = 0;
443        loop {
444            // Sleep to limit how often we wake up.
445            fasync::Timer::new(sleep_time).await;
446            let head = ring_buffer.wait(last_head).await;
447            let Some(this) = this.upgrade() else { return };
448            let mut inner = InnerGuard::new(&this);
449            inner.drain_ring_buffer(head);
450            last_head = inner.tail;
451        }
452    }
453}
454
455impl Inner {
456    fn compressed_tail_message_id(&self) -> u64 {
457        self.chunks.front().map(|c| c.msg_ids.start).unwrap_or(self.active_msg_ids.start)
458    }
459
460    fn chunk_bytes(&self, chunk: &CompressedChunk) -> &[u8] {
461        let phys_start = (chunk.range.start % self.data.len() as u64) as usize;
462        let len = (chunk.range.end - chunk.range.start) as usize;
463        &self.data[phys_start..phys_start + len]
464    }
465
466    /// Compresses `self.active_chunk` into the circular `self.data` buffer and records a new
467    /// `CompressedChunk` in `self.chunks`.
468    fn seal_active_chunk(&mut self) {
469        if self.active_chunk.is_empty() {
470            return;
471        }
472        let cap = self.data.len();
473        let uncompressed_len = self.active_chunk.len();
474        let phys_start = (self.compressed_head % cap as u64) as usize;
475        // We only want to store the compressed output if it is strictly smaller than
476        // `uncompressed_len` (since `stored_len == uncompressed_len` indicates an uncompressed
477        // chunk) and fits before the physical end of `self.data`.
478        let max_dst = (cap - phys_start).min(uncompressed_len - 1);
479
480        // Evict any oldest chunks that would be overwritten by writing up to `uncompressed_len`
481        // bytes at `self.compressed_head`.
482        while self.chunks.front().is_some_and(|oldest| {
483            self.compressed_head + uncompressed_len as u64 - oldest.range.start > cap as u64
484        }) {
485            self.chunks.pop_front();
486        }
487
488        let stored_len = if let Ok(compressed_len) =
489            lz4::compress_into(&self.active_chunk, &mut self.data[phys_start..phys_start + max_dst])
490        {
491            // Fast path: the compressed chunk fit in the remaining tail of `self.data` and was
492            // strictly smaller than `uncompressed_len`.
493            compressed_len
494        } else if max_dst < uncompressed_len - 1 {
495            // We are near the physical end of `self.data` (`cap - phys_start < uncompressed_len`)
496            // and the compressed output did not fit in the remaining tail space. Since a chunk
497            // cannot straddle the end of `self.data`, advance `self.compressed_head` to wrap to
498            // physical index 0.
499            self.compressed_head += (cap - phys_start) as u64;
500            // Evict any oldest chunks that would be overwritten by writing up to `uncompressed_len`
501            // bytes at physical index 0.
502            while self.chunks.front().is_some_and(|oldest| {
503                self.compressed_head + uncompressed_len as u64 - oldest.range.start > cap as u64
504            }) {
505                self.chunks.pop_front();
506            }
507            // Retry compressing at the start of `self.data`. If compression still fails, the data
508            // is incompressible, so store it uncompressed at `0..uncompressed_len`.
509            if let Ok(compressed_len) =
510                lz4::compress_into(&self.active_chunk, &mut self.data[0..uncompressed_len - 1])
511            {
512                compressed_len
513            } else {
514                self.data[0..uncompressed_len].copy_from_slice(&self.active_chunk);
515                uncompressed_len
516            }
517        } else {
518            // `max_dst == uncompressed_len - 1`, meaning there was at least `uncompressed_len`
519            // bytes of contiguous space at `phys_start`, so compression failed only because the
520            // chunk did not compress to fewer than `uncompressed_len` bytes. Store the raw bytes
521            // uncompressed; cursors detect this via
522            // `chunk.range.end - chunk.range.start == chunk.uncompressed_len`.
523            self.data[phys_start..phys_start + uncompressed_len]
524                .copy_from_slice(&self.active_chunk);
525            uncompressed_len
526        };
527
528        let range = self.compressed_head..self.compressed_head + stored_len as u64;
529        self.chunks.push_back(CompressedChunk {
530            range: range.clone(),
531            uncompressed_len: uncompressed_len as u32,
532            msg_ids: self.active_msg_ids.clone(),
533        });
534        self.compressed_head = range.end;
535        self.active_chunk.clear();
536        self.active_msg_ids = self.next_msg_id..self.next_msg_id;
537    }
538
539    fn append_message(&mut self, container_id: ContainerId, message: &[u8]) {
540        let msg_len = message.len();
541        let record_bytes = 8 + msg_len;
542
543        // If appending this record would cause `active_chunk` to exceed `CHUNK_SIZE` (64 KiB),
544        // compress and seal the current `active_chunk` into `self.data` first.
545        if !self.active_chunk.is_empty() && self.active_chunk.len() + record_bytes > CHUNK_SIZE {
546            self.seal_active_chunk();
547        }
548
549        let severity = (msg_len >= 8)
550            .then(|| Header::read_from_bytes(&message[0..8]).unwrap().severity().into());
551        self.active_chunk.extend_from_slice(&container_id.0.to_ne_bytes());
552        self.active_chunk.extend_from_slice(&(msg_len as u32).to_ne_bytes());
553        self.active_chunk.extend_from_slice(message);
554
555        let msg_id = self.next_msg_id;
556        self.next_msg_id += 1;
557        self.active_msg_ids.end = self.next_msg_id;
558
559        let container = self.containers.get_mut(container_id).unwrap();
560        container.last_msg_id = Some(msg_id);
561        if let Some(severity) = severity {
562            container.stats.ingest_message(msg_len, severity);
563        }
564    }
565}
566
567impl<'a> InnerGuard<'a> {
568    fn new(buffer: &'a Arc<SharedBuffer>) -> Self {
569        Self {
570            buffer,
571            guard: ManuallyDrop::new(buffer.inner.lock()),
572            on_inactive: Vec::new(),
573            wake: false,
574        }
575    }
576
577    fn finish_ingest(&mut self, old_compressed_tail: u64) {
578        // If any old chunks were evicted while sealing chunks above, check whether any containers
579        // had their last message evicted and are now inactive.
580        let new_compressed_tail = self.compressed_tail_message_id();
581        if new_compressed_tail > old_compressed_tail {
582            let mut id = 0;
583            while let Some(used_id) = self.containers.slab.next_used(id) {
584                id = used_id + 1;
585                let container = self.containers.slab.get_mut(used_id).unwrap();
586                if container.last_msg_id.is_some_and(|msg_id| msg_id < new_compressed_tail) {
587                    container.last_msg_id = None;
588                    if !container.is_active() {
589                        if container.should_free() {
590                            self.containers.slab.free(used_id);
591                        } else {
592                            let identity = Arc::clone(&container.identity);
593                            self.on_inactive.push(identity);
594                        }
595                    }
596                }
597            }
598        }
599
600        self.wake = true;
601        let boot_time = zx::BootInstant::get().into_nanos();
602        let count = self.next_msg_id - new_compressed_tail;
603        self.buffer.saturation_curve.record(boot_time, count);
604    }
605
606    fn ingest(&mut self, msg: &[u8], container_id: ContainerId) {
607        if msg.len() < std::mem::size_of::<Header>() {
608            debug!("message too short ({})", msg.len());
609            if let Some(container) = self.containers.get(container_id) {
610                container.stats.increment_invalid(msg.len());
611            }
612            return;
613        }
614
615        let header = Header::read_from_bytes(&msg[..std::mem::size_of::<Header>()]).unwrap();
616
617        // NOTE: Some tests send messages that are bigger than the header indicates. We ignore the
618        // tail of any such messages.
619        let msg_len = header.size_words() as usize * 8;
620
621        // Check the correct type and size.
622        if header.raw_type() != TRACING_FORMAT_LOG_RECORD_TYPE || msg.len() < msg_len {
623            debug!("bad type or size ({}, {}, {})", header.raw_type(), msg_len, msg.len());
624            if let Some(container) = self.containers.get(container_id) {
625                container.stats.increment_invalid(msg.len());
626            }
627            return;
628        }
629
630        if self.containers.get(container_id).is_none() {
631            return;
632        }
633
634        self.drain_ring_buffer(self.ring_buffer.head());
635        let old_compressed_tail = self.compressed_tail_message_id();
636        self.append_message(container_id, &msg[..msg_len]);
637        self.finish_ingest(old_compressed_tail);
638    }
639
640    /// Drains all records in `self.tail..head` from the ring buffer into `self.active_chunk`
641    /// (sealing chunks into `self.data` as needed) and updates container stats and liveness.
642    fn drain_ring_buffer(&mut self, head: u64) {
643        if self.tail >= head {
644            return;
645        }
646        let old_compressed_tail = self.compressed_tail_message_id();
647        while self.tail < head {
648            let index = self.tail;
649            // Read the message length first so we can seal `active_chunk` before copying if needed.
650            //
651            // SAFETY: `index..head` is within the written range of the ring buffer
652            // (`self.tail..head`), so there is no concurrent write access to this range. Only the
653            // kernel and Archivist can write to the ring buffer and so we trust both not to corrupt
654            // it.
655            let msg_len = unsafe { self.ring_buffer.first_message_in(index..head) }
656                .expect("Unable to read message from ring buffer")
657                .1
658                .len();
659            let record_len = ring_buffer_record_len(msg_len);
660            let record_bytes = 8 + msg_len;
661
662            // If appending this record would cause `active_chunk` to exceed `CHUNK_SIZE` (64 KiB),
663            // compress and seal the current `active_chunk` into `self.data` first.
664            if !self.active_chunk.is_empty() && self.active_chunk.len() + record_bytes > CHUNK_SIZE
665            {
666                self.seal_active_chunk();
667            }
668
669            // Copy `[container_id, msg_len, message]` from the ring buffer into `active_chunk`.
670            let (container_id, severity) = {
671                let Inner { ring_buffer, active_chunk, .. } = &mut **self;
672                // SAFETY: `index..head` is within `self.tail..head`.
673                let (tag, message) = unsafe { ring_buffer.first_message_in(index..head) }
674                    .expect("Unable to read message from ring buffer");
675                let container_id = ContainerId(tag as u32);
676                let severity = (msg_len >= 8)
677                    .then(|| Header::read_from_bytes(&message[0..8]).unwrap().severity().into());
678                active_chunk.extend_from_slice(&container_id.0.to_ne_bytes());
679                active_chunk.extend_from_slice(&(msg_len as u32).to_ne_bytes());
680                active_chunk.extend_from_slice(message);
681                (container_id, severity)
682            };
683
684            self.ring_buffer.increment_tail(record_len);
685            self.tail += record_len as u64;
686
687            let msg_id = self.next_msg_id;
688            self.next_msg_id += 1;
689            self.active_msg_ids.end = self.next_msg_id;
690
691            let container = self.containers.get_mut(container_id).unwrap();
692            container.last_msg_id = Some(msg_id);
693            if let Some(severity) = severity {
694                container.stats.ingest_message(msg_len, severity);
695            }
696        }
697
698        self.finish_ingest(old_compressed_tail);
699    }
700
701    /// Reads a socket. Calls `rearm` to rearm the socket once it has been drained.
702    fn read_socket(
703        &mut self,
704        sockets: &mut Slab<Socket>,
705        socket_id: SocketId,
706        rearm: impl FnOnce(&mut Socket),
707    ) {
708        let Some(socket) = sockets.get_mut(socket_id.0) else { return };
709        let container_id = socket.container_id;
710
711        self.drain_ring_buffer(self.ring_buffer.head());
712        let old_compressed_tail = self.compressed_tail_message_id();
713        let mut ingested = false;
714
715        loop {
716            let mut data = Vec::with_capacity(MAX_DATAGRAM_LEN_BYTES as usize);
717
718            // Read directly into the buffer leaving space for the header.
719            let len = match socket.socket.read_uninit(data.spare_capacity_mut()) {
720                Ok(d) => d.len(),
721                Err(zx::Status::SHOULD_WAIT) => {
722                    // The socket has been drained.
723                    if ingested {
724                        self.finish_ingest(old_compressed_tail);
725                    }
726                    rearm(socket);
727                    return;
728                }
729                Err(_) => break,
730            };
731
732            // SAFETY: `read_uninit` will have written to `len` bytes.
733            unsafe {
734                data.set_len(len);
735            }
736
737            let container = self.containers.get_mut(container_id).unwrap();
738            if data.len() < 16 {
739                container.stats.increment_invalid(data.len());
740                continue;
741            }
742
743            let header = Header::read_from_bytes(&data[..std::mem::size_of::<Header>()]).unwrap();
744            let msg_len = header.size_words() as usize * 8;
745            if header.raw_type() != TRACING_FORMAT_LOG_RECORD_TYPE || msg_len != data.len() {
746                debug!("bad type or size ({}, {}, {})", header.raw_type(), msg_len, data.len());
747                container.stats.increment_invalid(data.len());
748                continue;
749            }
750
751            self.append_message(container_id, &data);
752            ingested = true;
753        }
754
755        // This path is taken when the socket should be closed.
756        if ingested {
757            self.finish_ingest(old_compressed_tail);
758        }
759
760        let container = self.containers.get_mut(container_id).unwrap();
761        container.remove_socket(socket_id, sockets);
762        if !container.is_active() {
763            if container.should_free() {
764                self.containers.free(container_id);
765            } else {
766                let identity = Arc::clone(&container.identity);
767                self.on_inactive.push(identity);
768            }
769        }
770    }
771
772    /// Flushes sockets and drains the ring buffer into the compressed buffer.
773    fn flush_sockets(&mut self) {
774        let mut sockets = self.buffer.sockets.lock();
775        let mut socket_id = 0;
776        while let Some(next_id) = sockets.next_used(socket_id) {
777            // The socket doesn't need to be rearmed here.
778            self.read_socket(&mut sockets, SocketId(next_id), |_| {});
779            socket_id = next_id + 1;
780        }
781        let head = self.ring_buffer.head();
782        self.drain_ring_buffer(head);
783    }
784
785    /// Returns a cursor.
786    fn cursor(&mut self, mode: StreamMode, selectors: Vec<ComponentSelector>) -> FilterCursor {
787        let (message_id, end_msg_id) = match mode {
788            StreamMode::Snapshot => {
789                self.flush_sockets();
790                (self.compressed_tail_message_id(), Some(self.next_msg_id))
791            }
792            StreamMode::SnapshotThenSubscribe => (self.compressed_tail_message_id(), None),
793            StreamMode::Subscribe => {
794                let head = self.ring_buffer.head();
795                self.drain_ring_buffer(head);
796                (self.next_msg_id, None)
797            }
798        };
799        FilterCursor::new(Arc::clone(self.buffer), message_id, end_msg_id, selectors)
800    }
801}
802
803#[derive(Default)]
804struct Containers {
805    slab: Slab<ContainerInfo>,
806}
807
808#[derive(Clone, Copy, Debug, Eq, PartialEq)]
809struct ContainerId(u32);
810
811impl Containers {
812    #[cfg(test)]
813    fn len(&self) -> usize {
814        self.slab.len()
815    }
816
817    fn get(&self, id: ContainerId) -> Option<&ContainerInfo> {
818        self.slab.get(id.0)
819    }
820
821    fn get_mut(&mut self, id: ContainerId) -> Option<&mut ContainerInfo> {
822        self.slab.get_mut(id.0)
823    }
824
825    fn new_container(
826        &mut self,
827        identity: Arc<ComponentIdentity>,
828        stats: Arc<LogStreamStats>,
829    ) -> ContainerId {
830        ContainerId(self.slab.insert(|_| ContainerInfo::new(identity, stats)))
831    }
832
833    fn free(&mut self, id: ContainerId) {
834        self.slab.free(id.0);
835    }
836}
837
838#[derive(Derivative)]
839#[derivative(Debug)]
840struct ContainerInfo {
841    // The identity of the container.
842    identity: Arc<ComponentIdentity>,
843
844    // Message ID of the most recently extracted message for this container, if it is still present
845    // in the compressed buffer.
846    last_msg_id: Option<u64>,
847
848    // Whether the container is terminated.
849    terminated: bool,
850
851    // Inspect instrumentation.
852    #[derivative(Debug = "ignore")]
853    stats: Arc<LogStreamStats>,
854
855    // The first socket ID for this container.
856    first_socket_id: SocketId,
857
858    // The number of client IOBuffers.
859    iob_count: usize,
860}
861
862impl ContainerInfo {
863    fn new(identity: Arc<ComponentIdentity>, stats: Arc<LogStreamStats>) -> Self {
864        Self {
865            identity,
866            last_msg_id: None,
867            terminated: false,
868            stats,
869            first_socket_id: SocketId::NULL,
870            iob_count: 0,
871        }
872    }
873
874    fn should_free(&self) -> bool {
875        self.terminated && !self.is_active()
876    }
877
878    // Returns true if the container is considered active which is the case if it has sockets, io
879    // buffers, or buffered messages.
880    //
881    // NOTE: Whenever a socket or iob is closed, `drain_ring_buffer` must be called to ensure
882    // `last_msg_id` is up to date.
883    fn is_active(&self) -> bool {
884        self.first_socket_id != SocketId::NULL
885            || self.iob_count > 0
886            || self.last_msg_id.is_some()
887            || ARCHIVIST_MONIKER.get().is_some_and(|m| *self.identity == *m)
888    }
889
890    // # Panics
891    //
892    // This will panic if the socket isn't found.
893    fn remove_socket(&mut self, socket_id: SocketId, sockets: &mut Slab<Socket>) {
894        let Socket { prev, next, .. } = *sockets.get(socket_id.0).unwrap();
895        if prev == SocketId::NULL {
896            self.first_socket_id = next;
897        } else {
898            sockets.get_mut(prev.0).unwrap().next = next;
899        }
900        if next != SocketId::NULL {
901            sockets
902                .get_mut(next.0)
903                .unwrap_or_else(|| panic!("next {next:?} has been freed!"))
904                .prev = prev;
905        }
906        sockets.free(socket_id.0);
907        self.stats.close_socket();
908        debug!(identity:% = self.identity; "Socket closed.");
909    }
910}
911
912pub struct ContainerBuffer {
913    shared_buffer: Arc<SharedBuffer>,
914    container_id: ContainerId,
915}
916
917impl ContainerBuffer {
918    /// Returns the tag used by IOBuffers used for this component.
919    pub fn iob_tag(&self) -> u64 {
920        self.container_id.0 as u64
921    }
922
923    /// Ingests a new message.
924    ///
925    /// If the message is invalid, it is dropped.
926    pub fn push_back(&self, msg: &[u8]) {
927        InnerGuard::new(&self.shared_buffer).ingest(msg, self.container_id);
928    }
929
930    /// Returns the identity of the container if it still exists.
931    pub fn identity(&self) -> Option<Arc<ComponentIdentity>> {
932        self.shared_buffer
933            .inner
934            .lock()
935            .containers
936            .get(self.container_id)
937            .map(|c| Arc::clone(&c.identity))
938    }
939
940    /// Returns an IOBuffer for the container.
941    pub fn iob(&self) -> zx::Iob {
942        let mut inner = self.shared_buffer.inner.lock();
943
944        inner.containers.get_mut(self.container_id).unwrap().iob_count += 1;
945
946        let (ep0, ep1) = inner.ring_buffer.new_iob_writer(self.container_id.0 as u64).unwrap();
947
948        inner.iob_peers.insert(|idx| {
949            ep1.wait_async(
950                &self.shared_buffer.port,
951                idx as u64 | IOB_PEER_CLOSED_KEY_BASE,
952                zx::Signals::IOB_PEER_CLOSED,
953                zx::WaitAsyncOpts::empty(),
954            )
955            .unwrap();
956
957            (self.container_id, ep1)
958        });
959
960        ep0
961    }
962
963    /// Marks the buffer as terminated which will force all cursors to end and close all sockets.
964    /// The component's data will remain in the buffer until the messages are rolled out. This will
965    /// *not* drain sockets or close IOBuffers.
966    pub fn terminate(&self) {
967        let mut inner = InnerGuard::new(&self.shared_buffer);
968
969        // See the comment on `is_active()` for why this is required.
970        inner.drain_ring_buffer(inner.ring_buffer.head());
971
972        if let Some(container) = inner.containers.get_mut(self.container_id) {
973            container.terminated = true;
974            if container.first_socket_id != SocketId::NULL {
975                let mut sockets = self.shared_buffer.sockets.lock();
976                loop {
977                    container.remove_socket(container.first_socket_id, &mut sockets);
978                    if container.first_socket_id == SocketId::NULL {
979                        break;
980                    }
981                }
982            }
983            if container.should_free() {
984                inner.containers.free(self.container_id);
985            }
986            inner.wake = true;
987        }
988    }
989
990    /// Returns true if the container has messages, sockets or IOBuffers.
991    pub fn is_active(&self) -> bool {
992        self.shared_buffer
993            .inner
994            .lock()
995            .containers
996            .get(self.container_id)
997            .is_some_and(|c| c.is_active())
998    }
999
1000    /// Adds a socket for this container.
1001    pub fn add_socket(&self, socket: zx::Socket) {
1002        let mut inner = self.shared_buffer.inner.lock();
1003        let Some(container) = inner.containers.get_mut(self.container_id) else { return };
1004        container.stats.open_socket();
1005        let next = container.first_socket_id;
1006        let mut sockets = self.shared_buffer.sockets.lock();
1007        let socket_id = SocketId(sockets.insert(|socket_id| {
1008            socket
1009                .wait_async(
1010                    &self.shared_buffer.port,
1011                    socket_id as u64,
1012                    zx::Signals::OBJECT_READABLE | zx::Signals::OBJECT_PEER_CLOSED,
1013                    zx::WaitAsyncOpts::empty(),
1014                )
1015                .unwrap();
1016            Socket { socket, container_id: self.container_id, prev: SocketId::NULL, next }
1017        }));
1018        if next != SocketId::NULL {
1019            sockets.get_mut(next.0).unwrap().prev = socket_id;
1020        }
1021        container.first_socket_id = socket_id;
1022    }
1023}
1024
1025impl Drop for ContainerBuffer {
1026    fn drop(&mut self) {
1027        self.terminate();
1028    }
1029}
1030
1031/// Implements a simple Slab allocator.
1032struct Slab<T> {
1033    slab: Vec<Slot<T>>,
1034    free_index: usize,
1035}
1036
1037impl<T> Default for Slab<T> {
1038    fn default() -> Self {
1039        Self { slab: Vec::new(), free_index: usize::MAX }
1040    }
1041}
1042
1043enum Slot<T> {
1044    Used(T),
1045    Free(usize),
1046}
1047
1048impl<T> Slab<T> {
1049    /// Returns the number of used entries. This is not performant.
1050    #[cfg(test)]
1051    fn len(&self) -> usize {
1052        self.slab.iter().filter(|c| matches!(c, Slot::Used(_))).count()
1053    }
1054
1055    fn free(&mut self, index: u32) -> T {
1056        let index = index as usize;
1057        let value = match std::mem::replace(&mut self.slab[index], Slot::Free(self.free_index)) {
1058            Slot::Free(_) => panic!("Slot already free"),
1059            Slot::Used(value) => value,
1060        };
1061        self.free_index = index;
1062        value
1063    }
1064
1065    fn get(&self, id: u32) -> Option<&T> {
1066        self.slab.get(id as usize).and_then(|s| match s {
1067            Slot::Used(s) => Some(s),
1068            _ => None,
1069        })
1070    }
1071
1072    fn get_mut(&mut self, id: u32) -> Option<&mut T> {
1073        self.slab.get_mut(id as usize).and_then(|s| match s {
1074            Slot::Used(s) => Some(s),
1075            _ => None,
1076        })
1077    }
1078
1079    fn insert(&mut self, value: impl FnOnce(u32) -> T) -> u32 {
1080        let free_index = self.free_index;
1081        if free_index != usize::MAX {
1082            self.free_index = match std::mem::replace(
1083                &mut self.slab[free_index],
1084                Slot::Used(value(free_index as u32)),
1085            ) {
1086                Slot::Free(next) => next,
1087                _ => unreachable!(),
1088            };
1089            free_index as u32
1090        } else {
1091            // This is < rather than <= because we reserve 0xffff_ffff to be used as a NULL value
1092            // (see SocketId::NULL below).
1093            assert!(self.slab.len() < u32::MAX as usize);
1094            self.slab.push(Slot::Used(value(self.slab.len() as u32)));
1095            (self.slab.len() - 1) as u32
1096        }
1097    }
1098
1099    /// Returns the next used slot starting from `id` (inclusive).
1100    fn next_used(&self, id: u32) -> Option<u32> {
1101        let mut id = id as usize;
1102        while id < self.slab.len() {
1103            if matches!(self.slab[id], Slot::Used(_)) {
1104                return Some(id as u32);
1105            }
1106            id += 1;
1107        }
1108        None
1109    }
1110}
1111
1112#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1113struct SocketId(u32);
1114
1115impl SocketId {
1116    const NULL: Self = SocketId(0xffff_ffff);
1117}
1118
1119struct Socket {
1120    socket: zx::Socket,
1121    container_id: ContainerId,
1122    // Sockets are stored as a linked list for each container.
1123    prev: SocketId,
1124    next: SocketId,
1125}
1126
1127#[cfg(test)]
1128mod tests {
1129    use super::{SharedBuffer, SharedBufferOptions, Slab, create_ring_buffer};
1130    use crate::identity::ComponentIdentity;
1131    use crate::logs::stats::LogStreamStats;
1132    use crate::logs::testing::make_message;
1133    use assert_matches::assert_matches;
1134    use diagnostics_assertions::{AnyProperty, assert_data_tree};
1135    use fidl_fuchsia_diagnostics::StreamMode;
1136    use fuchsia_async as fasync;
1137    use fuchsia_async::TimeoutExt;
1138    use fuchsia_inspect::{Inspector, Node};
1139    use futures::FutureExt;
1140    use futures::channel::mpsc;
1141    use futures::future::OptionFuture;
1142    use futures::stream::{FuturesUnordered, StreamExt as _};
1143    use moniker::ExtendedMoniker;
1144    use ring_buffer::MAX_MESSAGE_SIZE;
1145    use selectors::SelectorExt;
1146    use std::future::poll_fn;
1147    use std::iter::repeat_with;
1148    use std::pin::pin;
1149    use std::sync::Arc;
1150    use std::sync::atomic::{AtomicU64, Ordering};
1151    use std::task::Poll;
1152    use std::time::Duration;
1153    use zerocopy::FromBytes;
1154
1155    fn test_stats() -> Arc<LogStreamStats> {
1156        Arc::new(LogStreamStats::new(
1157            &fuchsia_inspect::Node::default(),
1158            &ComponentIdentity::unknown(),
1159        ))
1160    }
1161
1162    fn container_cursor(
1163        buffer: &Arc<SharedBuffer>,
1164        container: &super::ContainerBuffer,
1165        mode: StreamMode,
1166    ) -> Option<impl futures::Stream<Item = Box<[u8]>>> {
1167        let selectors = vec![container.identity()?.moniker.clone().into_component_selector()];
1168        let mut cursor = Box::pin(buffer.cursor(mode, selectors));
1169        Some(futures::stream::poll_fn(move |cx| {
1170            cursor.as_mut().poll_next(cx).map(|m| m.map(|m| m.parse().unwrap().2.into()))
1171        }))
1172    }
1173
1174    async fn yield_to_executor() {
1175        let mut first_time = true;
1176        poll_fn(|cx| {
1177            if first_time {
1178                cx.waker().wake_by_ref();
1179                first_time = false;
1180                Poll::Pending
1181            } else {
1182                Poll::Ready(())
1183            }
1184        })
1185        .await;
1186    }
1187
1188    #[fuchsia::test]
1189    async fn push_one_message() {
1190        let buffer = SharedBuffer::new(
1191            create_ring_buffer(MAX_MESSAGE_SIZE),
1192            Box::new(|_| {}),
1193            Default::default(),
1194            &fuchsia_inspect::Node::default(),
1195        );
1196        let container_buffer =
1197            buffer.new_container_buffer(Arc::new(vec!["a"].into()), test_stats());
1198        let msg = make_message("a", None, zx::BootInstant::from_nanos(1));
1199        container_buffer.push_back(msg.bytes());
1200
1201        // Make sure the cursor can find it.
1202        let cursor = container_cursor(&buffer, &container_buffer, StreamMode::Snapshot).unwrap();
1203        assert_eq!(cursor.map(|item| assert_eq!(&*item, msg.bytes())).count().await, 1);
1204    }
1205
1206    #[fuchsia::test]
1207    async fn message_too_short() {
1208        let buffer = SharedBuffer::new(
1209            create_ring_buffer(MAX_MESSAGE_SIZE),
1210            Box::new(|_| {}),
1211            Default::default(),
1212            &fuchsia_inspect::Node::default(),
1213        );
1214
1215        let container_buffer =
1216            buffer.new_container_buffer(Arc::new(vec!["a"].into()), test_stats());
1217        container_buffer.push_back(&[0]);
1218
1219        assert_eq!(
1220            container_cursor(&buffer, &container_buffer, StreamMode::Snapshot)
1221                .unwrap()
1222                .count()
1223                .await,
1224            0
1225        );
1226    }
1227
1228    #[fuchsia::test]
1229    async fn bad_type() {
1230        let buffer = SharedBuffer::new(
1231            create_ring_buffer(MAX_MESSAGE_SIZE),
1232            Box::new(|_| {}),
1233            Default::default(),
1234            &fuchsia_inspect::Node::default(),
1235        );
1236        let container_buffer =
1237            buffer.new_container_buffer(Arc::new(vec!["a"].into()), test_stats());
1238        container_buffer.push_back(&[0x77; 16]);
1239
1240        assert_eq!(
1241            container_cursor(&buffer, &container_buffer, StreamMode::Snapshot)
1242                .unwrap()
1243                .count()
1244                .await,
1245            0
1246        );
1247    }
1248
1249    #[fuchsia::test]
1250    async fn message_truncated() {
1251        let buffer = SharedBuffer::new(
1252            create_ring_buffer(MAX_MESSAGE_SIZE),
1253            Box::new(|_| {}),
1254            Default::default(),
1255            &fuchsia_inspect::Node::default(),
1256        );
1257        let container_buffer =
1258            buffer.new_container_buffer(Arc::new(vec!["a"].into()), test_stats());
1259        let msg = make_message("a", None, zx::BootInstant::from_nanos(1));
1260        container_buffer.push_back(&msg.bytes()[..msg.bytes().len() - 1]);
1261
1262        assert_eq!(
1263            container_cursor(&buffer, &container_buffer, StreamMode::Snapshot)
1264                .unwrap()
1265                .count()
1266                .await,
1267            0
1268        );
1269    }
1270
1271    #[fuchsia::test]
1272    async fn buffer_wrapping() {
1273        let buffer = SharedBuffer::new(
1274            create_ring_buffer(MAX_MESSAGE_SIZE),
1275            Box::new(|_| {}),
1276            SharedBufferOptions { sleep_time: Duration::ZERO, ..Default::default() },
1277            &fuchsia_inspect::Node::default(),
1278        );
1279        let container_buffer =
1280            buffer.new_container_buffer(Arc::new(vec!["a"].into()), test_stats());
1281
1282        // Keep writing messages until we wrap.
1283        let mut i = 0;
1284        loop {
1285            let msg = make_message(&format!("{i}"), None, zx::BootInstant::from_nanos(i));
1286            container_buffer.push_back(msg.bytes());
1287            i += 1;
1288
1289            // Yield to the executor to allow messages to be rolled out.
1290            yield_to_executor().await;
1291
1292            let inner = buffer.inner.lock();
1293            if inner.compressed_head > inner.data.len() as u64 {
1294                break;
1295            }
1296        }
1297
1298        // Read back all the messages.
1299        let mut cursor =
1300            pin!(container_cursor(&buffer, &container_buffer, StreamMode::Snapshot).unwrap());
1301
1302        let mut j;
1303        let item = cursor.next().await;
1304        assert_matches!(
1305            item,
1306            Some(item) => {
1307                j = i64::read_from_bytes(&item[8..16]).unwrap();
1308                let msg = make_message(&format!("{j}"),
1309                                       None,
1310                                       zx::BootInstant::from_nanos(j));
1311                assert_eq!(&*item, msg.bytes());
1312            }
1313        );
1314
1315        j += 1;
1316        while j != i {
1317            assert_matches!(
1318                cursor.next().await,
1319                Some(item) => {
1320                    assert_eq!(&*item, make_message(&format!("{j}"),
1321                                                         None,
1322                                                         zx::BootInstant::from_nanos(j)).bytes());
1323                }
1324            );
1325            j += 1;
1326        }
1327
1328        assert!(cursor.next().await.is_none());
1329    }
1330
1331    #[fuchsia::test]
1332    async fn on_inactive() {
1333        let identity = Arc::new(vec!["a"].into());
1334        let on_inactive = Arc::new(AtomicU64::new(0));
1335        let buffer = {
1336            let on_inactive = Arc::clone(&on_inactive);
1337            let identity = Arc::clone(&identity);
1338            Arc::new(SharedBuffer::new(
1339                create_ring_buffer(MAX_MESSAGE_SIZE),
1340                Box::new(move |i| {
1341                    assert_eq!(i, identity);
1342                    on_inactive.fetch_add(1, Ordering::Relaxed);
1343                }),
1344                SharedBufferOptions { sleep_time: Duration::ZERO, ..Default::default() },
1345                &fuchsia_inspect::Node::default(),
1346            ))
1347        };
1348        let container_a = buffer.new_container_buffer(identity, test_stats());
1349        let container_b = buffer.new_container_buffer(Arc::new(vec!["b"].into()), test_stats());
1350
1351        let msg = make_message("a", None, zx::BootInstant::from_nanos(1));
1352        container_a.push_back(msg.bytes());
1353
1354        // Repeatedly write messages to b until a is rolled out.
1355        while container_cursor(&buffer, &container_a, StreamMode::Snapshot).unwrap().count().await
1356            == 1
1357        {
1358            container_b.push_back(msg.bytes());
1359
1360            // Yield to the executor to allow messages to be rolled out.
1361            yield_to_executor().await;
1362        }
1363
1364        assert_eq!(on_inactive.load(Ordering::Relaxed), 1);
1365    }
1366
1367    #[fuchsia::test]
1368    async fn terminate_drops_container() {
1369        // Silence a clippy warning; SharedBuffer needs an executor.
1370        async {}.await;
1371
1372        let buffer = SharedBuffer::new(
1373            create_ring_buffer(MAX_MESSAGE_SIZE),
1374            Box::new(|_| {}),
1375            SharedBufferOptions { sleep_time: Duration::ZERO, ..Default::default() },
1376            &fuchsia_inspect::Node::default(),
1377        );
1378
1379        // terminate when buffer has no logs.
1380        let container_a = buffer.new_container_buffer(Arc::new(vec!["a"].into()), test_stats());
1381        assert_eq!(buffer.container_count(), 1);
1382        container_a.terminate();
1383
1384        assert_eq!(buffer.container_count(), 0);
1385
1386        // terminate when buffer has logs.
1387        let container_a = buffer.new_container_buffer(Arc::new(vec!["a"].into()), test_stats());
1388        let msg = make_message("a", None, zx::BootInstant::from_nanos(1));
1389        container_a.push_back(msg.bytes());
1390        assert_eq!(buffer.container_count(), 1);
1391        container_a.terminate();
1392
1393        // The container should still be there because it has logs.
1394        assert_eq!(buffer.container_count(), 1);
1395
1396        // Roll out the logs.
1397        let container_b = buffer.new_container_buffer(Arc::new(vec!["b"].into()), test_stats());
1398        assert_eq!(buffer.container_count(), 2);
1399
1400        // Repeatedly write messages to b until a's message is dropped and then the container will
1401        // be dropped.
1402        while buffer.container_count() != 1 {
1403            container_b.push_back(msg.bytes());
1404
1405            // Yield to the executor to allow messages to be rolled out.
1406            yield_to_executor().await;
1407        }
1408
1409        assert!(container_cursor(&buffer, &container_a, StreamMode::Subscribe).is_none());
1410    }
1411
1412    #[fuchsia::test]
1413    async fn cursor_subscribe() {
1414        for mode in [StreamMode::Subscribe, StreamMode::SnapshotThenSubscribe] {
1415            let buffer = SharedBuffer::new(
1416                create_ring_buffer(MAX_MESSAGE_SIZE),
1417                Box::new(|_| {}),
1418                Default::default(),
1419                &fuchsia_inspect::Node::default(),
1420            );
1421            let container =
1422                Arc::new(buffer.new_container_buffer(Arc::new(vec!["a"].into()), test_stats()));
1423            let msg = make_message("a", None, zx::BootInstant::from_nanos(1));
1424            container.push_back(msg.bytes());
1425
1426            let (sender, mut receiver) = mpsc::unbounded();
1427
1428            // Run the cursor in a separate task so that we can test it gets woken correctly.
1429            {
1430                let container = Arc::clone(&container);
1431                fasync::Task::spawn(async move {
1432                    let mut cursor =
1433                        pin!(container_cursor(&container.shared_buffer, &container, mode).unwrap());
1434                    while let Some(item) = cursor.next().await {
1435                        sender.unbounded_send(item).unwrap();
1436                    }
1437                })
1438                .detach();
1439            }
1440
1441            // The existing message should only be returned with SnapshotThenSubscribe
1442            if mode == StreamMode::SnapshotThenSubscribe {
1443                assert_matches!(
1444                    receiver.next().await,
1445                    Some(item) if item.as_ref() == msg.bytes()
1446                );
1447            }
1448
1449            // No message should arrive. We can only use a timeout here.
1450            assert!(
1451                OptionFuture::from(Some(receiver.next()))
1452                    .on_timeout(Duration::from_millis(500), || None)
1453                    .await
1454                    .is_none()
1455            );
1456
1457            container.push_back(msg.bytes());
1458
1459            // The message should arrive now.
1460            assert_matches!(
1461                receiver.next().await,
1462                Some(item) if item.as_ref() == msg.bytes()
1463            );
1464        }
1465    }
1466
1467    #[fuchsia::test]
1468    async fn drained_post_termination_cursors() {
1469        let buffer = SharedBuffer::new(
1470            create_ring_buffer(MAX_MESSAGE_SIZE),
1471            Box::new(|_| {}),
1472            Default::default(),
1473            &fuchsia_inspect::Node::default(),
1474        );
1475        let container =
1476            Arc::new(buffer.new_container_buffer(Arc::new(vec!["a"].into()), test_stats()));
1477        let msg = make_message("a", None, zx::BootInstant::from_nanos(1));
1478
1479        let mut cursor_a =
1480            pin!(container_cursor(&buffer, &container, StreamMode::Subscribe).unwrap());
1481        let mut cursor_b =
1482            pin!(container_cursor(&buffer, &container, StreamMode::SnapshotThenSubscribe).unwrap());
1483
1484        container.push_back(msg.bytes());
1485        container.push_back(msg.bytes());
1486        container.push_back(msg.bytes());
1487        container.push_back(msg.bytes());
1488        container.push_back(msg.bytes());
1489
1490        let mut cursor_c =
1491            pin!(container_cursor(&buffer, &container, StreamMode::Snapshot).unwrap());
1492        assert!(cursor_a.next().await.is_some());
1493        assert!(cursor_b.next().await.is_some());
1494        assert!(cursor_c.next().await.is_some());
1495
1496        drop(buffer.terminate());
1497
1498        // All cursors should return the 4 remaining messages.
1499        assert_eq!(cursor_a.count().await, 4);
1500        assert_eq!(cursor_b.count().await, 4);
1501        assert_eq!(cursor_c.count().await, 4);
1502    }
1503
1504    #[fuchsia::test]
1505    async fn empty_post_termination_cursors() {
1506        let buffer = SharedBuffer::new(
1507            create_ring_buffer(MAX_MESSAGE_SIZE),
1508            Box::new(|_| {}),
1509            Default::default(),
1510            &fuchsia_inspect::Node::default(),
1511        );
1512        let container =
1513            Arc::new(buffer.new_container_buffer(Arc::new(vec!["a"].into()), test_stats()));
1514
1515        let cursor_a = container_cursor(&buffer, &container, StreamMode::Subscribe).unwrap();
1516        let cursor_b =
1517            container_cursor(&buffer, &container, StreamMode::SnapshotThenSubscribe).unwrap();
1518        let cursor_c = container_cursor(&buffer, &container, StreamMode::Snapshot).unwrap();
1519
1520        drop(buffer.terminate());
1521
1522        assert_eq!(cursor_a.count().await, 0);
1523        assert_eq!(cursor_b.count().await, 0);
1524        assert_eq!(cursor_c.count().await, 0);
1525    }
1526
1527    #[fuchsia::test]
1528    async fn recycled_container_slot() {
1529        let buffer = Arc::new(SharedBuffer::new(
1530            create_ring_buffer(MAX_MESSAGE_SIZE),
1531            Box::new(|_| {}),
1532            SharedBufferOptions { sleep_time: Duration::ZERO, ..Default::default() },
1533            &fuchsia_inspect::Node::default(),
1534        ));
1535        let container_a =
1536            Arc::new(buffer.new_container_buffer(Arc::new(vec!["a"].into()), test_stats()));
1537        let msg = make_message("a", None, zx::BootInstant::from_nanos(1));
1538        container_a.push_back(msg.bytes());
1539
1540        let mut cursor = pin!(
1541            container_cursor(&buffer, &container_a, StreamMode::SnapshotThenSubscribe).unwrap()
1542        );
1543        assert_matches!(cursor.next().await, Some(_));
1544
1545        // Roll out all the messages.
1546        let container_b =
1547            Arc::new(buffer.new_container_buffer(Arc::new(vec!["b"].into()), test_stats()));
1548        while container_cursor(&buffer, &container_a, StreamMode::Snapshot).unwrap().count().await
1549            > 0
1550        {
1551            container_b.push_back(msg.bytes());
1552
1553            // Yield to the executor to allow messages to be rolled out.
1554            yield_to_executor().await;
1555        }
1556
1557        container_a.terminate();
1558
1559        // This should create a new container that uses a new slot and shouldn't interfere with
1560        // container_a.
1561        let container_c =
1562            Arc::new(buffer.new_container_buffer(Arc::new(vec!["c"].into()), test_stats()));
1563        container_c.push_back(msg.bytes());
1564        container_c.push_back(msg.bytes());
1565    }
1566
1567    #[fuchsia::test]
1568    async fn socket_increments_logstats() {
1569        let inspector = Inspector::default();
1570        let identity =
1571            Arc::new(ComponentIdentity::new(ExtendedMoniker::parse_str("./test").unwrap(), ""));
1572        let stats = Arc::new(LogStreamStats::new(inspector.root(), &identity));
1573        let buffer = Arc::new(SharedBuffer::new(
1574            create_ring_buffer(65536),
1575            Box::new(|_| {}),
1576            Default::default(),
1577            &fuchsia_inspect::Node::default(),
1578        ));
1579        let container_a = Arc::new(buffer.new_container_buffer(Arc::new(vec!["a"].into()), stats));
1580        let msg = make_message("a", None, zx::BootInstant::from_nanos(1));
1581
1582        let (local, remote) = zx::Socket::create_datagram();
1583        container_a.add_socket(remote);
1584
1585        let cursor_a = container_cursor(&buffer, &container_a, StreamMode::Subscribe).unwrap();
1586
1587        // Use FuturesUnordered so that we can make sure that the cursor is woken when a message is
1588        // received (FuturesUnordered uses separate wakers for all the futures it manages).
1589        let mut futures = FuturesUnordered::new();
1590        futures.push(async move {
1591            let mut cursor_a = pin!(cursor_a);
1592            cursor_a.next().await
1593        });
1594        let mut next = futures.next();
1595        assert!(futures::poll!(&mut next).is_pending());
1596
1597        local.write(msg.bytes()).unwrap();
1598
1599        let cursor_b = pin!(container_cursor(&buffer, &container_a, StreamMode::Snapshot).unwrap());
1600
1601        assert_eq!(cursor_b.map(|item| assert_eq!(&*item, msg.bytes())).count().await, 1);
1602
1603        // If cursor_a wasn't woken, this will hang.
1604        next.await;
1605        // Validate logstats (must happen after the socket was handled)
1606        assert_data_tree!(
1607            inspector,
1608            root: contains {
1609                test: {
1610                    url: "",
1611                    last_timestamp: AnyProperty,
1612                    sockets_closed: 0u64,
1613                    sockets_opened: 1u64,
1614                    invalid: {
1615                        number: 0u64,
1616                        bytes: 0u64,
1617                    },
1618                    total: {
1619                        number: 1u64,
1620                        bytes: 88u64,
1621                    },
1622                    trace: {
1623                        number: 0u64,
1624                        bytes: 0u64,
1625                    },
1626                    debug: {
1627                        number: 1u64,
1628                        bytes: 88u64,
1629                    },
1630                    info: {
1631                        number: 0u64,
1632                        bytes: 0u64,
1633                    },
1634                    warn: {
1635                        number: 0u64,
1636                        bytes: 0u64,
1637                    },
1638                    error: {
1639                        number: 0u64,
1640                        bytes: 0u64,
1641                    },
1642                    fatal: {
1643                        number: 0u64,
1644                        bytes: 0u64,
1645                    },
1646                }
1647            }
1648        );
1649    }
1650
1651    #[fuchsia::test]
1652    async fn socket() {
1653        let buffer = Arc::new(SharedBuffer::new(
1654            create_ring_buffer(MAX_MESSAGE_SIZE),
1655            Box::new(|_| {}),
1656            Default::default(),
1657            &fuchsia_inspect::Node::default(),
1658        ));
1659        let container_a =
1660            Arc::new(buffer.new_container_buffer(Arc::new(vec!["a"].into()), test_stats()));
1661        let msg = make_message("a", None, zx::BootInstant::from_nanos(1));
1662
1663        let (local, remote) = zx::Socket::create_datagram();
1664        container_a.add_socket(remote);
1665
1666        let cursor_a = container_cursor(&buffer, &container_a, StreamMode::Subscribe).unwrap();
1667
1668        // Use FuturesUnordered so that we can make sure that the cursor is woken when a message is
1669        // received (FuturesUnordered uses separate wakers for all the futures it manages).
1670        let mut futures = FuturesUnordered::new();
1671        futures.push(async move {
1672            let mut cursor_a = pin!(cursor_a);
1673            cursor_a.next().await
1674        });
1675        let mut next = futures.next();
1676        assert!(futures::poll!(&mut next).is_pending());
1677
1678        local.write(msg.bytes()).unwrap();
1679
1680        let cursor_b = pin!(container_cursor(&buffer, &container_a, StreamMode::Snapshot).unwrap());
1681
1682        assert_eq!(cursor_b.map(|item| assert_eq!(&*item, msg.bytes())).count().await, 1);
1683
1684        // If cursor_a wasn't woken, this will hang.
1685        next.await;
1686    }
1687
1688    #[fuchsia::test]
1689    async fn socket_on_inactive() {
1690        let on_inactive = Arc::new(AtomicU64::new(0));
1691        let a_identity = Arc::new(vec!["a"].into());
1692        let buffer = Arc::new(SharedBuffer::new(
1693            create_ring_buffer(MAX_MESSAGE_SIZE),
1694            {
1695                let on_inactive = Arc::clone(&on_inactive);
1696                let a_identity = Arc::clone(&a_identity);
1697                Box::new(move |id| {
1698                    assert_eq!(id, a_identity);
1699                    on_inactive.fetch_add(1, Ordering::Relaxed);
1700                })
1701            },
1702            SharedBufferOptions { sleep_time: Duration::ZERO, ..Default::default() },
1703            &fuchsia_inspect::Node::default(),
1704        ));
1705        let container_a = Arc::new(buffer.new_container_buffer(a_identity, test_stats()));
1706        let msg = make_message("a", None, zx::BootInstant::from_nanos(1));
1707
1708        let (local, remote) = zx::Socket::create_datagram();
1709        container_a.add_socket(remote);
1710
1711        local.write(msg.bytes()).unwrap();
1712
1713        let cursor = pin!(container_cursor(&buffer, &container_a, StreamMode::Snapshot).unwrap());
1714
1715        assert_eq!(cursor.map(|item| assert_eq!(&*item, msg.bytes())).count().await, 1);
1716
1717        // Now roll out a's messages.
1718        let container_b = buffer.new_container_buffer(Arc::new(vec!["b"].into()), test_stats());
1719        while container_cursor(&buffer, &container_a, StreamMode::Snapshot).unwrap().count().await
1720            == 1
1721        {
1722            container_b.push_back(msg.bytes());
1723
1724            // Yield to the executor to allow messages to be rolled out.
1725            yield_to_executor().await;
1726        }
1727
1728        assert_eq!(on_inactive.load(Ordering::Relaxed), 0);
1729
1730        // Close the socket.
1731        std::mem::drop(local);
1732
1733        // We don't know when the socket thread will run so we have to loop.
1734        while on_inactive.load(Ordering::Relaxed) != 1 {
1735            fasync::Timer::new(Duration::from_millis(50)).await;
1736        }
1737    }
1738
1739    #[fuchsia::test]
1740    async fn flush() {
1741        let a_identity = Arc::new(vec!["a"].into());
1742        let buffer = Arc::new(SharedBuffer::new(
1743            create_ring_buffer(1024 * 1024),
1744            Box::new(|_| {}),
1745            Default::default(),
1746            &fuchsia_inspect::Node::default(),
1747        ));
1748        let container_a = Arc::new(buffer.new_container_buffer(a_identity, test_stats()));
1749        let msg = make_message("a", None, zx::BootInstant::from_nanos(1));
1750
1751        let (local, remote) = zx::Socket::create_datagram();
1752        container_a.add_socket(remote);
1753
1754        let cursor = pin!(container_cursor(&buffer, &container_a, StreamMode::Subscribe).unwrap());
1755
1756        const COUNT: usize = 1000;
1757        for _ in 0..COUNT {
1758            local.write(msg.bytes()).unwrap();
1759        }
1760
1761        // Race two flush futures.
1762        let mut flush_futures = FuturesUnordered::from_iter([buffer.flush(), buffer.flush()]);
1763        flush_futures.next().await;
1764
1765        let messages: Option<Vec<_>> = cursor.take(COUNT).collect().now_or_never();
1766        assert!(messages.is_some());
1767
1768        // Make sure the other one finishes too.
1769        flush_futures.next().await;
1770
1771        // Make sure we can still terminate the buffer.
1772        buffer.terminate().await;
1773    }
1774
1775    #[fuchsia::test]
1776    fn test_slab_next_used() {
1777        let mut slab = Slab::default();
1778        let mut ids: Vec<_> = repeat_with(|| slab.insert(|_| ())).take(4).collect();
1779        ids.sort();
1780        slab.free(ids[0]);
1781        slab.free(ids[2]);
1782        assert_eq!(slab.next_used(0), Some(ids[1]));
1783        assert_eq!(slab.next_used(ids[1]), Some(ids[1]));
1784        assert_eq!(slab.next_used(ids[1] + 1), Some(ids[3]));
1785        assert_eq!(slab.next_used(ids[3] + 1), None);
1786    }
1787
1788    #[fuchsia::test]
1789    async fn small_capacity_incompressible_chunk() {
1790        let buffer = SharedBuffer::new(
1791            create_ring_buffer(0),
1792            Box::new(|_| {}),
1793            Default::default(),
1794            &Node::default(),
1795        );
1796        let container = buffer.new_container_buffer(Arc::new(vec!["a"].into()), test_stats());
1797
1798        // Push > 64 KiB (`CHUNK_SIZE`) of incompressible messages to force sealing an
1799        // incompressible chunk when the requested capacity was smaller than `CHUNK_SIZE`.
1800        let mut rng_state = 0x1234_5678_9abc_def0u64;
1801        for ts in 0..70 {
1802            let text: String = (0..1000)
1803                .map(|_| {
1804                    rng_state = rng_state.wrapping_mul(6364136223846793005).wrapping_add(1);
1805                    (b'!' + ((rng_state >> 32) % 94) as u8) as char
1806                })
1807                .collect();
1808            let msg = make_message(&text, None, zx::BootInstant::from_nanos(ts));
1809            container.push_back(msg.bytes());
1810        }
1811
1812        let cursor = pin!(container_cursor(&buffer, &container, StreamMode::Snapshot).unwrap());
1813        let messages: Vec<_> = cursor.collect().await;
1814        assert!(!messages.is_empty());
1815
1816        buffer.terminate().await;
1817    }
1818}