#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Serialize, Deserialize)]
pub struct MemoryLimits
{
perNumaNodeMemoryInMegabytes: [Option<u31>; MaximumNumaSockets],
}
impl MemoryLimits
{
pub fn totalMemoryInMegabytes(&self, numaSockets: &NumaSockets) -> Option<u31>
{
const _512GbInMegabytes: u31 = 524_288;
let memoryTotal = if numaSockets.isANumaMachine
{
let mut memoryTotal = 0;
for numaMemoryIndex in 0..MaximumNumaSockets
{
if let Some(megabytes) = self.perNumaNodeMemoryInMegabytes[numaMemoryIndex]
{
if numaSockets.isValidNumaSocket(numaMemoryIndex)
{
memoryTotal += megabytes;
}
}
}
memoryTotal
}
else
{
let mut memoryTotal = 0;
for index in 0..MaximumNumaSockets
{
if let Some(megabytes) = self.perNumaNodeMemoryInMegabytes[index]
{
memoryTotal = megabytes;
break;
}
}
memoryTotal
};
match memoryTotal
{
0 => None,
doesNotExceed512Gb if doesNotExceed512Gb < _512GbInMegabytes => Some(doesNotExceed512Gb),
_ => Some(_512GbInMegabytes),
}
}
pub fn asInitialisationStringIfIsANumaMachine(&self, useHugePages: bool, numaSockets: &NumaSockets) -> (Option<CString>, Option<u31>)
{
const SOCKET_MEM_STRLEN: usize = MaximumNumaSockets * 10;
if useHugePages && numaSockets.isANumaMachine
{
assert!(useHugePages, "Can not have per NUMA socket memory (memoryLimits) and then have useHugePages as false");
let mut numaMemoryString = String::with_capacity(SOCKET_MEM_STRLEN);
let mut hasValidEntries = false;
for numaMemoryIndex in 0..MaximumNumaSockets
{
if likely(numaMemoryIndex != 0)
{
numaMemoryString.push(',')
}
if let Some(megabytes) = self.perNumaNodeMemoryInMegabytes[numaMemoryIndex]
{
if numaSockets.isValidNumaSocket(numaMemoryIndex)
{
hasValidEntries = true;
let x = format!("{}", megabytes);
numaMemoryString.push_str(&x);
}
}
}
if hasValidEntries
{
(Some(CString::new(numaMemoryString).unwrap()), None)
}
else
{
(None, None)
}
}
else
{
(None, self.totalMemoryInMegabytes(numaSockets))
}
}
}