Skip to main content

fuchsia_storage_benchmarks/filesystems/
fxblob.rs

1// Copyright 2023 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 crate::filesystems::{BlobFilesystem, FsManagementFilesystemInstance};
6use async_trait::async_trait;
7use fidl_fuchsia_fxfs::{BlobCreatorMarker, BlobCreatorProxy, BlobReaderMarker, BlobReaderProxy};
8use fidl_fuchsia_io as fio;
9use fuchsia_component::client::{connect_to_protocol_at_dir_root, connect_to_protocol_at_dir_svc};
10use std::path::Path;
11use storage_benchmarks::{
12    BlockDeviceConfig, BlockDeviceFactory, CacheClearableFilesystem, Filesystem, FilesystemConfig,
13};
14
15/// Config object for starting Fxblob instances.
16#[derive(Clone)]
17pub struct Fxblob;
18
19#[async_trait]
20impl FilesystemConfig for Fxblob {
21    type Filesystem = FxblobInstance;
22
23    async fn start_filesystem(
24        &self,
25        block_device_factory: &dyn BlockDeviceFactory,
26    ) -> FxblobInstance {
27        let block_device = block_device_factory
28            .create_block_device(&BlockDeviceConfig {
29                requires_fvm: false,
30                use_zxcrypt: false,
31                volume_size: Some(104 * 1024 * 1024),
32            })
33            .await;
34        let fxblob = FsManagementFilesystemInstance::new(
35            || fs_management::Fxfs { allow_type3_blobs: true, ..Default::default() },
36            block_device,
37            None,
38            /*as_blob=*/ true,
39        )
40        .await;
41        let blob_creator =
42            connect_to_protocol_at_dir_svc::<BlobCreatorMarker>(fxblob.exposed_dir())
43                .expect("failed to connect to the BlobCreator protocol");
44        let blob_reader = connect_to_protocol_at_dir_svc::<BlobReaderMarker>(fxblob.exposed_dir())
45            .expect("failed to connect to the BlobReader protocol");
46        FxblobInstance { blob_creator, blob_reader, fxblob }
47    }
48
49    fn name(&self) -> String {
50        "fxblob".to_owned()
51    }
52}
53
54pub struct FxblobInstance {
55    blob_creator: BlobCreatorProxy,
56    blob_reader: BlobReaderProxy,
57    fxblob: FsManagementFilesystemInstance,
58}
59
60impl FxblobInstance {
61    pub fn connect_to_debug(&self) -> fidl_fuchsia_fxfs::DebugProxy {
62        connect_to_protocol_at_dir_root::<fidl_fuchsia_fxfs::DebugMarker>(
63            self.fxblob.exposed_services_dir(),
64        )
65        .expect("failed to connect to Debug protocol")
66    }
67}
68
69#[async_trait]
70impl Filesystem for FxblobInstance {
71    async fn shutdown(self) {
72        self.fxblob.shutdown().await
73    }
74
75    fn benchmark_dir(&self) -> &Path {
76        self.fxblob.benchmark_dir()
77    }
78}
79
80#[async_trait]
81impl CacheClearableFilesystem for FxblobInstance {
82    async fn clear_cache(&mut self) {
83        let () = self.fxblob.clear_cache().await;
84        self.blob_creator =
85            connect_to_protocol_at_dir_svc::<BlobCreatorMarker>(self.fxblob.exposed_dir())
86                .expect("failed to connect to the BlobCreator protocol");
87        self.blob_reader =
88            connect_to_protocol_at_dir_svc::<BlobReaderMarker>(self.fxblob.exposed_dir())
89                .expect("failed to connect to the BlobReader protocol");
90    }
91}
92
93#[async_trait]
94impl BlobFilesystem for FxblobInstance {
95    fn blob_creator(&self) -> &BlobCreatorProxy {
96        &self.blob_creator
97    }
98
99    fn blob_reader(&self) -> &BlobReaderProxy {
100        &self.blob_reader
101    }
102
103    fn exposed_dir(&self) -> &fio::DirectoryProxy {
104        self.fxblob.exposed_dir()
105    }
106}
107
108#[cfg(test)]
109mod tests {
110    use super::Fxblob;
111    use crate::filesystems::testing::check_blob_filesystem;
112
113    #[fuchsia::test]
114    async fn start_fxblob_new() {
115        check_blob_filesystem(Fxblob).await;
116    }
117}