Skip to content

Commit

Permalink
chore: fix aarch64 clippy lints
Browse files Browse the repository at this point in the history
Signed-off-by: Egor Lazarchuk <[email protected]>
  • Loading branch information
ShadowCurse committed Nov 29, 2024
1 parent ffb697b commit 775c84f
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions src/vmm/src/arch/aarch64/cache_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ pub(crate) enum CacheInfoError {
MissingOptionalAttr(String, CacheEntry),
}

struct CacheEngine {
store: Box<dyn CacheStore>,
struct CacheEngine<T: CacheStore = HostCacheStore> {
store: T,
}

trait CacheStore: std::fmt::Debug {
fn get_by_key(&self, index: u8, file_name: &str) -> Result<String, CacheInfoError>;
}

#[derive(Debug)]
pub(crate) struct CacheEntry {
pub struct CacheEntry {
// Cache Level: 1, 2, 3..
pub level: u8,
// Type of cache: Unified, Data, Instruction.
Expand All @@ -45,17 +45,16 @@ pub(crate) struct CacheEntry {
}

#[derive(Debug)]
struct HostCacheStore {
pub struct HostCacheStore {
cache_dir: PathBuf,
}

#[cfg(not(test))]
impl Default for CacheEngine {
impl Default for CacheEngine<HostCacheStore> {
fn default() -> Self {
CacheEngine {
store: Box::new(HostCacheStore {
store: HostCacheStore {
cache_dir: PathBuf::from("/sys/devices/system/cpu/cpu0/cache"),
}),
},
}
}
}
Expand All @@ -72,7 +71,7 @@ impl CacheStore for HostCacheStore {
}

impl CacheEntry {
fn from_index(index: u8, store: &dyn CacheStore) -> Result<CacheEntry, CacheInfoError> {
fn from_index(index: u8, store: &impl CacheStore) -> Result<CacheEntry, CacheInfoError> {
let mut err_str = String::new();
let mut cache: CacheEntry = CacheEntry::default();

Expand Down Expand Up @@ -287,10 +286,10 @@ pub(crate) fn read_cache_config(
// Also without this mechanism we would be logging the warnings for each level which pollutes
// a lot the logs.
let mut logged_missing_attr = false;
let engine = CacheEngine::default();
let engine = CacheEngine::<HostCacheStore>::default();

for index in 0..=MAX_CACHE_LEVEL {
match CacheEntry::from_index(index, engine.store.as_ref()) {
match CacheEntry::from_index(index, &engine.store) {
Ok(cache) => {
append_cache_level(cache_l1, cache_non_l1, cache);
}
Expand Down Expand Up @@ -326,22 +325,22 @@ mod tests {
dummy_fs: HashMap<String, String>,
}

impl Default for CacheEngine {
impl Default for CacheEngine<MockCacheStore> {
fn default() -> Self {
CacheEngine {
store: Box::new(MockCacheStore {
store: MockCacheStore {
dummy_fs: create_default_store(),
}),
},
}
}
}

impl CacheEngine {
impl CacheEngine<MockCacheStore> {
fn new(map: &HashMap<String, String>) -> Self {
CacheEngine {
store: Box::new(MockCacheStore {
store: MockCacheStore {
dummy_fs: map.clone(),
}),
},
}
}
}
Expand Down Expand Up @@ -425,12 +424,12 @@ mod tests {
let mut map1 = default_map.clone();
map1.remove("index0/type");
let engine = CacheEngine::new(&map1);
let res = CacheEntry::from_index(0, engine.store.as_ref());
let res = CacheEntry::from_index(0, &engine.store);
// We did create the level file but we still do not have the type file.
assert!(matches!(res.unwrap_err(), CacheInfoError::MissingCacheType));

let engine = CacheEngine::new(&default_map);
let res = CacheEntry::from_index(0, engine.store.as_ref());
let res = CacheEntry::from_index(0, &engine.store);
assert_eq!(
format!("{}", res.unwrap_err()),
"shared cpu map, coherency line size, size, number of sets",
Expand All @@ -440,15 +439,15 @@ mod tests {
let mut map2 = default_map.clone();
map2.insert("index0/level".to_string(), "d".to_string());
let engine = CacheEngine::new(&map2);
let res = CacheEntry::from_index(0, engine.store.as_ref());
let res = CacheEntry::from_index(0, &engine.store);
assert_eq!(
format!("{}", res.unwrap_err()),
"Invalid cache configuration found for level: invalid digit found in string"
);

default_map.insert("index0/type".to_string(), "Instructionn".to_string());
let engine = CacheEngine::new(&default_map);
let res = CacheEntry::from_index(0, engine.store.as_ref());
let res = CacheEntry::from_index(0, &engine.store);
assert_eq!(
format!("{}", res.unwrap_err()),
"Invalid cache configuration found for type: Instructionn"
Expand All @@ -464,7 +463,7 @@ mod tests {
"00000000,00000001".to_string(),
);
let engine = CacheEngine::new(&default_map);
let res = CacheEntry::from_index(0, engine.store.as_ref());
let res = CacheEntry::from_index(0, &engine.store);
assert_eq!(
format!("{}", res.unwrap_err()),
"coherency line size, size, number of sets"
Expand All @@ -475,15 +474,15 @@ mod tests {
"00000000,0000000G".to_string(),
);
let engine = CacheEngine::new(&default_map);
let res = CacheEntry::from_index(0, engine.store.as_ref());
let res = CacheEntry::from_index(0, &engine.store);
assert_eq!(
format!("{}", res.unwrap_err()),
"Invalid cache configuration found for shared_cpu_map: invalid digit found in string"
);

default_map.insert("index0/shared_cpu_map".to_string(), "00000000".to_string());
let engine = CacheEngine::new(&default_map);
let res = CacheEntry::from_index(0, engine.store.as_ref());
let res = CacheEntry::from_index(0, &engine.store);
assert_eq!(
format!("{}", res.unwrap_err()),
"Invalid cache configuration found for shared_cpu_map: 00000000"
Expand All @@ -496,7 +495,7 @@ mod tests {

default_map.insert("index0/coherency_line_size".to_string(), "64".to_string());
let engine = CacheEngine::new(&default_map);
let res = CacheEntry::from_index(0, engine.store.as_ref());
let res = CacheEntry::from_index(0, &engine.store);
assert_eq!(
"shared cpu map, size, number of sets",
format!("{}", res.unwrap_err())
Expand All @@ -507,7 +506,7 @@ mod tests {
"Instruction".to_string(),
);
let engine = CacheEngine::new(&default_map);
let res = CacheEntry::from_index(0, engine.store.as_ref());
let res = CacheEntry::from_index(0, &engine.store);
assert_eq!(
format!("{}", res.unwrap_err()),
"Invalid cache configuration found for coherency_line_size: invalid digit found in \
Expand All @@ -521,23 +520,23 @@ mod tests {

default_map.insert("index0/size".to_string(), "64K".to_string());
let engine = CacheEngine::new(&default_map);
let res = CacheEntry::from_index(0, engine.store.as_ref());
let res = CacheEntry::from_index(0, &engine.store);
assert_eq!(
format!("{}", res.unwrap_err()),
"shared cpu map, coherency line size, number of sets",
);

default_map.insert("index0/size".to_string(), "64".to_string());
let engine = CacheEngine::new(&default_map);
let res = CacheEntry::from_index(0, engine.store.as_ref());
let res = CacheEntry::from_index(0, &engine.store);
assert_eq!(
format!("{}", res.unwrap_err()),
"Invalid cache configuration found for size: 64"
);

default_map.insert("index0/size".to_string(), "64Z".to_string());
let engine = CacheEngine::new(&default_map);
let res = CacheEntry::from_index(0, engine.store.as_ref());
let res = CacheEntry::from_index(0, &engine.store);
assert_eq!(
format!("{}", res.unwrap_err()),
"Invalid cache configuration found for size: 64Z"
Expand All @@ -550,15 +549,15 @@ mod tests {

default_map.insert("index0/number_of_sets".to_string(), "64".to_string());
let engine = CacheEngine::new(&default_map);
let res = CacheEntry::from_index(0, engine.store.as_ref());
let res = CacheEntry::from_index(0, &engine.store);
assert_eq!(
"shared cpu map, coherency line size, size",
format!("{}", res.unwrap_err())
);

default_map.insert("index0/number_of_sets".to_string(), "64K".to_string());
let engine = CacheEngine::new(&default_map);
let res = CacheEntry::from_index(0, engine.store.as_ref());
let res = CacheEntry::from_index(0, &engine.store);
assert_eq!(
format!("{}", res.unwrap_err()),
"Invalid cache configuration found for number_of_sets: invalid digit found in string"
Expand Down

0 comments on commit 775c84f

Please sign in to comment.