Skip to content

Commit

Permalink
kata-sys-util: Move additional functionality to cpu.rs
Browse files Browse the repository at this point in the history
Make certain imports architecture specific as these are not used on all
architectures.
Move additional constants and functionality to cpu.rs.

Signed-off-by: Archana Shinde <archana.m.shinde@intel.com>
  • Loading branch information
amshinde committed Jul 13, 2023
1 parent 304b9d9 commit f5d1957
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/libs/kata-sys-util/src/cpu.rs
Expand Up @@ -3,8 +3,16 @@
// SPDX-License-Identifier: Apache-2.0
//

#[cfg(any(target_arch = "s390x", target_arch = "x86_64"))]
use anyhow::{anyhow, Result};

#[cfg(target_arch = "s390x")]
use std::collections::HashMap;
#[cfg(target_arch = "s390x")]
use std::io::BufRead;
#[cfg(target_arch = "s390x")]
use std::io::BufReader;

#[allow(dead_code)]
const ERR_NO_CPUINFO: &str = "cpu_info string is empty";

Expand Down Expand Up @@ -114,6 +122,41 @@ fn get_cpu_flags_from_file(cpu_info: &str, cpu_flags_tag: &str) -> Result<String
Ok("".to_string())
}

#[cfg(target_arch = "s390x")]
pub fn retrieve_cpu_facilities() -> Result<HashMap<i32, bool>> {
let f = std::fs::File::open(PROC_CPUINFO)?;
let mut reader = BufReader::new(f);
let mut contents = String::new();
let facilities_field = "facilities";
let mut facilities = HashMap::new();

while reader.read_line(&mut contents)? > 0 {
let fields: Vec<&str> = contents.split_whitespace().collect();
if fields.len() < 2 {
contents.clear();
continue;
}

if !fields[0].starts_with(facilities_field) {
contents.clear();
continue;
}

let mut start = 1;
if fields[1] == ":" {
start = 2;
}

for field in fields.iter().skip(start) {
let bit = field.parse::<i32>()?;
facilities.insert(bit, true);
}
return Ok(facilities);
}

Ok(facilities)
}

#[cfg(any(target_arch = "s390x", target_arch = "x86_64"))]
#[cfg(test)]
mod tests {
Expand Down

0 comments on commit f5d1957

Please sign in to comment.