Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(deps): upgrade to byte-unit 5 #593

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 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
Expand Up @@ -40,7 +40,7 @@ default = []
instrument = ["rftrace", "rftrace-frontend"]

[dependencies]
byte-unit = { version = "4.0", default-features = false, features = ["std"] }
byte-unit = { version = "5", features = ["byte"] }
clap = { version = "4.4", features = ["derive", "env"] }
core_affinity = "0.8"
either = "1.9"
Expand Down
2 changes: 1 addition & 1 deletion benches/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn load_vm_hello_world(c: &mut Criterion) {
.iter()
.collect();
let params = Params {
memory_size: Byte::from_bytes(1024 * 100000).try_into().unwrap(),
memory_size: Byte::from_u64(1024 * 100_000).try_into().unwrap(),
..Default::default()
};
let mut vm = Uhyve::new(path, params).expect("Unable to create VM");
Expand Down
28 changes: 16 additions & 12 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
str::FromStr,
};

use byte_unit::{n_mib_bytes, AdjustedByte, Byte, ByteError};
use byte_unit::{Byte, Unit};
use thiserror::Error;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -103,43 +103,47 @@ pub struct GuestMemorySize(Byte);

impl GuestMemorySize {
const fn minimum() -> Byte {
Byte::from_bytes(16 * 1024 * 1024)
let Some(byte) = Byte::from_u64_with_unit(16, Unit::MiB) else {
panic!()
};
byte
}

pub fn get(self) -> usize {
self.0.get_bytes().try_into().unwrap()
self.0.as_u64().try_into().unwrap()
}
}

impl Default for GuestMemorySize {
fn default() -> Self {
Self(Byte::from_bytes(64 * 1024 * 1024))
Self(Byte::from_u64_with_unit(64, Unit::MiB).unwrap())
}
}

impl fmt::Display for GuestMemorySize {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.get_appropriate_unit(true).fmt(f)
self.0.fmt(f)
}
}

#[derive(Error, Debug)]
pub enum InvalidGuestMemorySizeError {
#[error("Not enough guest memory. Must be at least {} (is {0})", GuestMemorySize::minimum().get_appropriate_unit(true))]
MemoryTooSmall(AdjustedByte),
#[error(
"Not enough guest memory. Must be at least {} (is {0})",
GuestMemorySize::minimum()
)]
MemoryTooSmall(Byte),
#[error("Invalid amount of guest memory. Must be a multiple of 2 MiB (is {0})")]
NotAHugepage(AdjustedByte),
NotAHugepage(Byte),
}

impl TryFrom<Byte> for GuestMemorySize {
type Error = InvalidGuestMemorySizeError;

fn try_from(value: Byte) -> Result<Self, Self::Error> {
if value < Self::minimum() {
let value = value.get_appropriate_unit(true);
Err(InvalidGuestMemorySizeError::MemoryTooSmall(value))
} else if value.get_bytes() % n_mib_bytes!(2) != 0 {
let value = value.get_appropriate_unit(true);
} else if value.as_u64() % Byte::from_u64_with_unit(2, Unit::MiB).unwrap().as_u64() != 0 {
Err(InvalidGuestMemorySizeError::NotAHugepage(value))
} else {
Ok(Self(value))
Expand All @@ -150,7 +154,7 @@ impl TryFrom<Byte> for GuestMemorySize {
#[derive(Error, Debug)]
pub enum ParseByteError {
#[error(transparent)]
Parse(#[from] ByteError),
Parse(#[from] byte_unit::ParseError),

#[error(transparent)]
InvalidMemorySize(#[from] InvalidGuestMemorySizeError),
Expand Down
7 changes: 5 additions & 2 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
process::Command,
};

use byte_unit::Byte;
use byte_unit::{Byte, Unit};
use uhyvelib::{params::Params, Uhyve};

/// Uses Cargo to build a kernel in the `tests/test-kernels` directory.
Expand Down Expand Up @@ -42,7 +42,10 @@ pub fn run_simple_vm(kernel_path: PathBuf) {
let params = Params {
verbose: true,
cpu_count: 2.try_into().unwrap(),
memory_size: Byte::from_bytes(32 * 1024 * 1024).try_into().unwrap(),
memory_size: Byte::from_u64_with_unit(32, Unit::MiB)
.unwrap()
.try_into()
.unwrap(),
..Default::default()
};
let code = Uhyve::new(kernel_path, params).unwrap().run(None);
Expand Down