1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// This file is part of dpdk. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/dpdk/master/COPYRIGHT. No part of dpdk, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2016-2017 The developers of dpdk. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/dpdk/master/COPYRIGHT.


#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MemoryStatistics(HashMap<MemoryStatisticName, u64>);

// Super-detailed information (hard to parse, too) is in /proc/zoneinfo
// This is broken down into DMA, DMA33 and Normal sub-zones and then by CPU for each Numa Node (aka 'zone')
// A sort of detailed version of /proc/vmstat
impl MemoryStatistics
{
	#[inline]
	pub fn getStatistic(&self, memoryStatisticName: &MemoryStatisticName) -> Option<u64>
	{
		match self.0.get(memoryStatisticName)
		{
			None => None,
			Some(value) => Some(*value),
		}
	}
	
	pub fn freePhysicalRam(&self) -> Option<u64>
	{
		self.getStatistic(&MemoryStatisticName::FreePhysicalRam)
	}
	
	pub fn defaultHugePageSize(&self) -> Option<HugePageSize>
	{
		if let Some(sizeInBytes) = self.getStatistic(&MemoryStatisticName::SizeOfAHugePage)
		{
			HugePageSize::fromProcMemInfoValue(sizeInBytes)
		}
		else
		{
			None
		}
	}
	
	#[inline]
	pub fn usedPhysicalRam(&self) -> Option<u64>
	{
		if let Some(totalPhysicalRam) = self.getStatistic(&MemoryStatisticName::TotalPhyiscalRam)
		{
			if let Some(freePhysicalRam) = self.getStatistic(&MemoryStatisticName::FreePhysicalRam)
			{
				Some(totalPhysicalRam - freePhysicalRam)
			}
			else
			{
				None
			}
		}
		else
		{
			None
		}
	}
	
	#[inline]
	pub fn usedSwap(&self) -> Option<u64>
	{
		if let Some(totalSwap) = self.getStatistic(&MemoryStatisticName::TotalSwap)
		{
			if let Some(freeSwap) = self.getStatistic(&MemoryStatisticName::FreeSwap)
			{
				Some(totalSwap - freeSwap)
			}
			else
			{
				None
			}
		}
		else
		{
			None
		}
	}
	
	pub fn forMachine(procPath: &Path) -> Result<Self, MemoryStatisticsParseError>
	{
		Self::parse(procPath, "")
	}
	
	pub fn parse(parentFolderPath: &Path, memoryStatisticNamePrefix: &str) -> Result<Self, MemoryStatisticsParseError>
	{
		let mut meminfoFilePath = PathBuf::from(parentFolderPath);
		meminfoFilePath.push("meminfo");
		
		let openFile = try!(File::open(meminfoFilePath));
		let mut reader = BufReader::with_capacity(4096, openFile);
		
		let mut memoryStatistics = HashMap::new();
		let mut lineCount = 0;
		let mut line = String::with_capacity(512);
		while try!(reader.read_line(&mut line)) > 0
		{
			{
				let mut split = line.splitn(2, ':');

				let memoryStatisticName = MemoryStatisticName::parse(split.next().unwrap(), memoryStatisticNamePrefix);
				
				let memoryStatisticValue = match split.next()
				{
					None => return Err(MemoryStatisticsParseError::CouldNotParseMemoryStatisticValue(lineCount, memoryStatisticName)),
					Some(rawValue) =>
					{
						let trimmedRawValue = rawValue.trim();
						let endsWith = memoryStatisticName.unit().endsWith();
					
						if !trimmedRawValue.ends_with(endsWith)
						{
							return Err(MemoryStatisticsParseError::CouldNotParseMemoryStatisticValue(lineCount, memoryStatisticName));
						}
					
						let trimmed = &rawValue[0..rawValue.len() - endsWith.len()];
					
						match trimmed.parse::<u64>()
						{
							Ok(value) => value,
							Err(intParseError) => return Err(MemoryStatisticsParseError::CouldNotParseMemoryStatisticValueAsU64(lineCount, memoryStatisticName, rawValue.to_owned(), intParseError))
						}
					}
				};
				
				if memoryStatistics.contains_key(&memoryStatisticName)
				{
					return Err(MemoryStatisticsParseError::DuplicateMemoryStatistic(lineCount, memoryStatisticName, memoryStatisticValue));
				}
				
				memoryStatistics.insert(memoryStatisticName, memoryStatisticValue);
			}
			
			line.clear();
			lineCount += 1;
		}
		
		Ok(MemoryStatistics(memoryStatistics))
	}
}