Merge pull request #11470 from fidencio/topic/runtime-rs-fix-odd-memory-size-calculation

runtime-rs: Fix calculation of odd memory sizes
This commit is contained in:
Fabiano Fidêncio 2025-06-30 07:26:30 +02:00 committed by GitHub
commit 8d4e3b47b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,6 +22,8 @@ struct InitialSize {
orig_toml_default_mem: u32, orig_toml_default_mem: u32,
} }
const MIB: i64 = 1024 * 1024;
// generate initial resource(vcpu and memory in MiB) from annotations // generate initial resource(vcpu and memory in MiB) from annotations
impl TryFrom<&HashMap<String, String>> for InitialSize { impl TryFrom<&HashMap<String, String>> for InitialSize {
type Error = anyhow::Error; type Error = anyhow::Error;
@ -168,10 +170,15 @@ fn get_nr_vcpu(resource: &LinuxContainerCpuResources) -> u32 {
fn convert_memory_to_mb(memory_in_byte: i64) -> u32 { fn convert_memory_to_mb(memory_in_byte: i64) -> u32 {
if memory_in_byte < 0 { if memory_in_byte < 0 {
0 return 0;
} else {
(memory_in_byte / 1024 / 1024) as u32
} }
let mem_size = (memory_in_byte / MIB) as u32;
// memory size must be 2MB aligned for hugepage support
if mem_size % 2 != 0 {
return mem_size + 1;
}
mem_size
} }
// from the upper layer runtime's annotation (e.g. crio, k8s), get the *cpu quota, // from the upper layer runtime's annotation (e.g. crio, k8s), get the *cpu quota,
@ -227,7 +234,7 @@ mod tests {
input: InputData { input: InputData {
period: Some(100_000), period: Some(100_000),
quota: Some(220_000), quota: Some(220_000),
memory: Some(1024 * 1024 * 512), memory: Some(512 * MIB),
}, },
result: InitialSize { result: InitialSize {
vcpu: 3, vcpu: 3,
@ -235,6 +242,19 @@ mod tests {
orig_toml_default_mem: 0, orig_toml_default_mem: 0,
}, },
}, },
TestData {
desc: "Odd memory in resource limits",
input: InputData {
period: None,
quota: None,
memory: Some(513 * MIB),
},
result: InitialSize {
vcpu: 0,
mem_mb: 514,
orig_toml_default_mem: 0,
},
},
] ]
.to_vec() .to_vec()
} }