storage_benchmarks/
filesystem.rs1use crate::BlockDeviceFactory;
6use async_trait::async_trait;
7use std::future::Future;
8use std::path::{Path, PathBuf};
9use std::pin::Pin;
10
11#[async_trait]
13pub trait FilesystemConfig: Send + Sync {
14    type Filesystem: Filesystem;
17
18    async fn start_filesystem(
20        &self,
21        block_device_factory: &dyn BlockDeviceFactory,
22    ) -> Self::Filesystem;
23
24    fn name(&self) -> String;
26}
27
28#[async_trait]
30pub trait Filesystem: Send + Sync + BoxedFilesystem {
31    async fn shutdown(self);
33
34    fn benchmark_dir(&self) -> &Path;
37}
38
39pub trait BoxedFilesystem: Send + Sync {
41    fn shutdown_boxed<'a>(self: Box<Self>) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>
46    where
47        Self: 'a;
48}
49
50impl<T: Filesystem> BoxedFilesystem for T {
51    fn shutdown_boxed<'a>(self: Box<Self>) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>
52    where
53        Self: 'a,
54    {
55        (*self).shutdown()
56    }
57}
58
59#[async_trait]
61pub trait CacheClearableFilesystem: Filesystem {
62    async fn clear_cache(&mut self);
66}
67
68#[derive(Clone)]
70pub struct MountedFilesystem {
71    dir: PathBuf,
73
74    name: String,
76}
77
78impl MountedFilesystem {
79    pub fn new<P: Into<PathBuf>>(dir: P, name: String) -> Self {
80        Self { dir: dir.into(), name }
81    }
82}
83
84#[async_trait]
85impl FilesystemConfig for MountedFilesystem {
86    type Filesystem = MountedFilesystemInstance;
87    async fn start_filesystem(
88        &self,
89        _block_device_factory: &dyn BlockDeviceFactory,
90    ) -> MountedFilesystemInstance {
91        let path = self.dir.join("benchmark");
94        std::fs::create_dir(&path).unwrap_or_else(|e| {
95            panic!("failed to created benchmark directory '{}': {:?}", path.display(), e)
96        });
97        MountedFilesystemInstance::new(path)
98    }
99
100    fn name(&self) -> String {
101        self.name.clone()
102    }
103}
104
105pub struct MountedFilesystemInstance {
107    dir: PathBuf,
108}
109
110impl MountedFilesystemInstance {
111    pub fn new<P: Into<PathBuf>>(dir: P) -> Self {
112        Self { dir: dir.into() }
113    }
114}
115
116#[async_trait]
117impl Filesystem for MountedFilesystemInstance {
118    async fn shutdown(self) {
119        std::fs::remove_dir_all(self.dir).expect("Failed to remove benchmark directory");
120    }
121
122    fn benchmark_dir(&self) -> &Path {
123        self.dir.as_path()
124    }
125}