#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Serialize, Deserialize)]
pub enum HugePageAllocationStrategy
{
RatioOfTotalFree
{
numeratorPermyriad: u16, },
AllLessReserve
{
reserveInKiloBytes: u64,
},
FixedOrFail
{
fixedAmountInKiloBytes: u64,
},
}
impl HugePageAllocationStrategy
{
pub const TenPercentRatioOfTotalFree: HugePageAllocationStrategy = HugePageAllocationStrategy::RatioOfTotalFree
{
numeratorPermyriad: 1_000,
};
pub const EightyPercentRatioOfTotalFree: HugePageAllocationStrategy = HugePageAllocationStrategy::RatioOfTotalFree
{
numeratorPermyriad: 8_000,
};
pub fn allocateInPages(&self, hugePageSize: HugePageSize, totalNumberOfKiloBytesFree: u64) -> u64
{
let numberOfKiloBytes = match *self
{
HugePageAllocationStrategy::RatioOfTotalFree { numeratorPermyriad } =>
{
(totalNumberOfKiloBytesFree * numeratorPermyriad as u64) / 10_000
},
HugePageAllocationStrategy::AllLessReserve { reserveInKiloBytes } =>
{
totalNumberOfKiloBytesFree.checked_sub(reserveInKiloBytes).unwrap_or(0)
},
HugePageAllocationStrategy::FixedOrFail { fixedAmountInKiloBytes } =>
{
assert!(fixedAmountInKiloBytes >= totalNumberOfKiloBytesFree, "totalNumberOfKiloBytesFree '{}' is less than the fixed amount of fixedAmountInKiloBytes '{}'", totalNumberOfKiloBytesFree, fixedAmountInKiloBytes);
fixedAmountInKiloBytes
},
};
hugePageSize.calculateNumberOfHugePages(numberOfKiloBytes)
}
}