Skip to content

Commit

Permalink
chore: wrapper cache type, reorg utils, add metrics framework (#15)
Browse files Browse the repository at this point in the history
Signed-off-by: MrCroxx <[email protected]>
  • Loading branch information
MrCroxx authored May 29, 2023
1 parent 9646741 commit 1e3be82
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 35 deletions.
62 changes: 62 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]

members = ["foyer", "foyer-bench"]
members = ["foyer", "foyer-bench", "foyer-utils"]

[patch.crates-io]
# cmsketch = { path = "../cmsketch-rs" }
17 changes: 7 additions & 10 deletions foyer-bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ use std::time::{Duration, Instant};
use analyze::{analyze, monitor, Metrics};
use clap::Parser;

use foyer::container::{Config, Container};

use foyer::store::read_only_file_store::{Config as ReadOnlyFileStoreConfig, ReadOnlyFileStore};

use foyer::policies::tinylfu::{Config as TinyLfuConfig, Handle, TinyLfu};
use foyer::{
ReadOnlyFileStoreConfig, TinyLfuConfig, TinyLfuReadOnlyFileStoreCache,
TinyLfuReadOnlyFileStoreCacheConfig,
};
use futures::future::join_all;
use itertools::Itertools;
use rand::rngs::StdRng;
Expand Down Expand Up @@ -109,9 +108,7 @@ impl Args {
assert!(self.pools.is_power_of_two());
}
}

type TContainer =
Container<u64, TinyLfu<u64>, Handle<u64>, Vec<u8>, ReadOnlyFileStore<u64, Vec<u8>>>;
type TContainer = TinyLfuReadOnlyFileStoreCache<u64, Vec<u8>>;

fn is_send_sync_static<T: Send + Sync + 'static>() {}

Expand Down Expand Up @@ -159,7 +156,7 @@ async fn main() {
tiny_lru_capacity_ratio: 0.01,
};

let config: Config<u64, TinyLfu<_>, _, ReadOnlyFileStore<_, Vec<u8>>> = Config {
let config = TinyLfuReadOnlyFileStoreCacheConfig {
capacity: args.capacity * 1024 * 1024,
pool_count_bits: (args.pools as f64).log2() as usize,
policy_config,
Expand All @@ -168,7 +165,7 @@ async fn main() {

println!("{:#?}", config);

let container = Container::open(config).await.unwrap();
let container = TinyLfuReadOnlyFileStoreCache::open(config).await.unwrap();
let container = Arc::new(container);

let (iostat_stop_tx, iostat_stop_rx) = oneshot::channel();
Expand Down
40 changes: 40 additions & 0 deletions foyer-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
name = "foyer-utils"
version = "0.1.0"
edition = "2021"
authors = ["MrCroxx <[email protected]>"]
description = "Hybrid cache for Rust"
license = "Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait = "0.1"
bytes = "1"
cmsketch = "0.1"
crossbeam = "0.8"
futures = "0.3"
itertools = "0.10.5"
libc = "0.2"
memoffset = "0.8"
nix = { version = "0.26", features = ["fs", "mman"] }
parking_lot = "0.12"
paste = "1.0"
prometheus = "0.13"
thiserror = "1"
tokio = { version = "1", features = [
"rt",
"rt-multi-thread",
"sync",
"macros",
"time",
"signal",
] }
twox-hash = "1"

[dev-dependencies]
bytesize = "1"
clap = { version = "4", features = ["derive"] }
hdrhistogram = "7"
rand = "0.8.5"
rand_mt = "4.2.1"
tempfile = "3"
File renamed without changes.
5 changes: 2 additions & 3 deletions foyer/src/collections/dlist.rs → foyer-utils/src/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ pub trait Adapter<E> {
fn el2en(_: NonNull<E>) -> NonNull<Entry>;
}

#[macro_export]
macro_rules! intrusive_dlist {
(
$element:ident $(< $( $lt:tt $( : $clt:tt $(+ $dlt:tt)* )? ),+ >)?, $entry:ident, $adapter:ident
) => {
struct $adapter;

impl $(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $crate::collections::dlist::Adapter<$element $(< $( $lt ),+ >)?> for $adapter {
impl $(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $crate::dlist::Adapter<$element $(< $( $lt ),+ >)?> for $adapter {
fn en2el(entry: std::ptr::NonNull<Entry>) -> std::ptr::NonNull<$element $(< $( $lt ),+ >)?> {
unsafe {
let ptr = (entry.as_ptr() as *mut u8)
Expand All @@ -61,8 +62,6 @@ macro_rules! intrusive_dlist {
};
}

pub(crate) use intrusive_dlist;

/// TODO: write docs
#[derive(Debug)]
pub struct DList<E, A: Adapter<E>> {
Expand Down
3 changes: 3 additions & 0 deletions foyer/src/collections/mod.rs → foyer-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![feature(trait_alias)]

pub mod bits;
pub mod dlist;
2 changes: 2 additions & 0 deletions foyer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ async-trait = "0.1"
bytes = "1"
cmsketch = "0.1"
crossbeam = "0.8"
foyer-utils = { path = "../foyer-utils" }
futures = "0.3"
itertools = "0.10.5"
libc = "0.2"
memoffset = "0.8"
nix = { version = "0.26", features = ["fs", "mman"] }
parking_lot = "0.12"
paste = "1.0"
prometheus = "0.13"
thiserror = "1"
tokio = { version = "1", features = [
"rt",
Expand Down
5 changes: 2 additions & 3 deletions foyer/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ where
.into_iter()
.enumerate()
.map(|(id, store)| Pool {
id,
_id: id,
policy: P::new(config.policy_config.clone()),
capacity,
size: 0,
Expand Down Expand Up @@ -175,8 +175,7 @@ where
D: Data,
S: Store<I = I, D = D>,
{
#[allow(unused)]
id: usize,
_id: usize,

policy: P,

Expand Down
50 changes: 44 additions & 6 deletions foyer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ pub use policies::Policy;

use paste::paste;

pub mod collections;
pub mod container;
pub mod policies;
pub mod store;
mod container;
mod metrics;
mod policies;
mod store;

pub trait Weight {
fn weight(&self) -> usize;
Expand Down Expand Up @@ -87,9 +87,47 @@ pub struct WrappedNonNull<T>(pub NonNull<T>);
unsafe impl<T> Send for WrappedNonNull<T> {}
unsafe impl<T> Sync for WrappedNonNull<T> {}

#[cfg(test)]
// TODO(MrCroxx): Add global error type.
pub type Error = store::error::Error;

pub type TinyLfuReadOnlyFileStoreCache<I, D> = container::Container<
I,
policies::tinylfu::TinyLfu<I>,
policies::tinylfu::Handle<I>,
D,
store::read_only_file_store::ReadOnlyFileStore<I, D>,
>;

pub type TinyLfuReadOnlyFileStoreCacheConfig<I, D> = container::Config<
I,
policies::tinylfu::TinyLfu<I>,
policies::tinylfu::Handle<I>,
store::read_only_file_store::ReadOnlyFileStore<I, D>,
>;

pub type LruReadOnlyFileStoreCache<I, D> = container::Container<
I,
policies::lru::Lru<I>,
policies::lru::Handle<I>,
D,
store::read_only_file_store::ReadOnlyFileStore<I, D>,
>;

pub type LruReadOnlyFileStoreCacheConfig<I, D> = container::Config<
I,
policies::lru::Lru<I>,
policies::lru::Handle<I>,
store::read_only_file_store::ReadOnlyFileStore<I, D>,
>;

pub use metrics::Metrics;

pub use policies::lru::Config as LruConfig;
pub use policies::tinylfu::Config as TinyLfuConfig;
pub use store::read_only_file_store::Config as ReadOnlyFileStoreConfig;

pub mod tests {
#[cfg(test)]

mod tests {
pub fn is_send_sync_static<T: Send + Sync + 'static>() {}
}
61 changes: 61 additions & 0 deletions foyer/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2023 MrCroxx
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use prometheus::{
register_counter_vec_with_registry, register_histogram_vec_with_registry,
register_int_counter_with_registry, CounterVec, HistogramVec, IntCounter, Registry,
};

pub struct Metrics {
pub miss: IntCounter,

pub latency: HistogramVec,
pub bytes: CounterVec,
}

impl Default for Metrics {
fn default() -> Self {
Self::new(Registry::new())
}
}

impl Metrics {
pub fn new(registry: Registry) -> Self {
let miss =
register_int_counter_with_registry!("foyer_cache_miss", "file cache miss", registry)
.unwrap();

let latency = register_histogram_vec_with_registry!(
"foyer_latency",
"foyer latency",
&["op"],
vec![
0.0001, 0.001, 0.005, 0.01, 0.02, 0.03, 0.04, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75,
1.0
],
registry
)
.unwrap();

let bytes =
register_counter_vec_with_registry!("foyer_bytes", "foyer bytes", &["op"], registry)
.unwrap();

Self {
miss,
latency,
bytes,
}
}
}
Loading

0 comments on commit 1e3be82

Please sign in to comment.