stalls/
lib.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
5use std::sync::Arc;
6
7pub trait StallProvider: Sync + Send + 'static {
8    /// Return the current memory stall values from the kernel.
9    fn get_stall_info(&self) -> Result<zx::MemoryStall, anyhow::Error>;
10}
11
12pub struct StallProviderImpl {
13    /// Memory stall kernel resource, for issuing queries.
14    stall_resource: Arc<dyn StallResource>,
15}
16
17/// Trait for a resource exposing memory stall information. Used for dependency injection in unit
18/// tests.
19pub trait StallResource: Sync + Send {
20    fn get_memory_stall(&self) -> Result<zx::MemoryStall, zx::Status>;
21}
22
23impl StallResource for zx::Resource {
24    fn get_memory_stall(&self) -> Result<zx::MemoryStall, zx::Status> {
25        self.memory_stall()
26    }
27}
28
29impl StallProviderImpl {
30    /// Create a new [StallProviderImpl], wrapping a [StallResource].
31    pub fn new(stall_resource: Arc<dyn StallResource>) -> Result<StallProviderImpl, anyhow::Error> {
32        Ok(StallProviderImpl { stall_resource: stall_resource })
33    }
34}
35
36impl StallProvider for StallProviderImpl {
37    fn get_stall_info(&self) -> Result<zx::MemoryStall, anyhow::Error> {
38        Ok(self.stall_resource.get_memory_stall()?)
39    }
40}