Skip to content

Commit

Permalink
feat: parse human size for malloc and null bdevs
Browse files Browse the repository at this point in the history
Parse a size with unit post-fix for the malloc and null bdevs.
This makes it much easier to use, example:
size=1TiB vs size_mb=1048576

Signed-off-by: Tiago Castro <[email protected]>
  • Loading branch information
tiagolobocastro committed Jan 23, 2025
1 parent 1e1c05a commit a367f85
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
37 changes: 30 additions & 7 deletions io-engine/src/bdev/malloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,40 @@ impl TryFrom<&Url> for Malloc {
512
};

let size: u32 = if let Some(value) = parameters.remove("size_mb") {
value.parse().context(bdev_api::IntParamParseFailed {
let size_mb: Option<u64> = if let Some(value) = parameters.remove("size_mb") {
Some(value.parse().context(bdev_api::IntParamParseFailed {
uri: uri.to_string(),
parameter: String::from("size_mb"),
value: value.clone(),
})?
})?)
} else {
0
None
};

let num_blocks: u32 = if let Some(value) = parameters.remove("num_blocks") {
let size_b: Option<u64> = if let Some(value) = parameters.remove("size") {
Some(
byte_unit::Byte::parse_str(&value, true)
.map_err(|error| BdevError::InvalidUri {
uri: uri.to_string(),
message: format!("'size' is invalid: {error}"),
})?
.as_u64(),
)
} else {
None
};

let size = match (size_mb, size_b) {
(Some(_), Some(_)) => Err(BdevError::InvalidUri {
uri: uri.to_string(),
message: "Can't specify both size and size_mb".to_string(),
}),
(Some(size_mb), None) => Ok(size_mb * 1024 * 1024),
(None, Some(size)) => Ok(size),
(None, None) => Ok(0),
}?;

let num_blocks: u64 = if let Some(value) = parameters.remove("num_blocks") {
value.parse().context(bdev_api::IntParamParseFailed {
uri: uri.to_string(),
parameter: String::from("num_blocks"),
Expand Down Expand Up @@ -133,8 +156,8 @@ impl TryFrom<&Url> for Malloc {
num_blocks: if num_blocks != 0 {
num_blocks
} else {
(size << 20) / blk_size
} as u64,
size / (blk_size as u64)
},
blk_size,
uuid,
resizing,
Expand Down
38 changes: 31 additions & 7 deletions io-engine/src/bdev/null_bdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,39 @@ impl TryFrom<&Url> for Null {
});
}

let size: u64 = if let Some(value) = parameters.remove("size_mb") {
value.parse().context(bdev_api::IntParamParseFailed {
let size_mb: Option<u64> = if let Some(value) = parameters.remove("size_mb") {
Some(value.parse().context(bdev_api::IntParamParseFailed {
uri: uri.to_string(),
parameter: String::from("size_mb"),
value: value.clone(),
})?
})?)
} else {
0
None
};

let size_b: Option<u64> = if let Some(value) = parameters.remove("size") {
Some(
byte_unit::Byte::parse_str(&value, true)
.map_err(|error| BdevError::InvalidUri {
uri: uri.to_string(),
message: format!("'size' is invalid: {error}"),
})?
.as_u64(),
)
} else {
None
};

let size = match (size_mb, size_b) {
(Some(_), Some(_)) => Err(BdevError::InvalidUri {
uri: uri.to_string(),
message: "Can't specify both size and size_mb".to_string(),
}),
(Some(size_mb), None) => Ok(size_mb * 1024 * 1024),
(None, Some(size)) => Ok(size),
(None, None) => Ok(0),
}?;

let num_blocks: u64 = if let Some(value) = parameters.remove("num_blocks") {
value.parse().context(bdev_api::IntParamParseFailed {
uri: uri.to_string(),
Expand All @@ -86,8 +109,9 @@ impl TryFrom<&Url> for Null {
if size != 0 && num_blocks != 0 {
return Err(BdevError::InvalidUri {
uri: uri.to_string(),
message: "conflicting parameters num_blocks and size_mb are mutually exclusive"
.to_string(),
message:
"conflicting parameters num_blocks and size/size_mb are mutually exclusive"
.to_string(),
});
}

Expand All @@ -104,7 +128,7 @@ impl TryFrom<&Url> for Null {
num_blocks: if num_blocks != 0 {
num_blocks
} else {
(size << 20) / (blk_size as u64)
size / (blk_size as u64)
},
blk_size,
uuid: uuid.or_else(|| Some(Uuid::new_v4())),
Expand Down

0 comments on commit a367f85

Please sign in to comment.