novaでproc/meminfoを読み込む
3601 ワード
nova/libvirt/host.py get_memory_mb_used proc/meminfo。
:
ef get_memory_mb_used(self):
"""Get the used memory size(MB) of physical computer.
:returns: the total usage of memory(MB).
"""
if sys.platform.upper() not in ['LINUX2', 'LINUX3']:
return 0
with open('/proc/meminfo') as fp:
m = fp.read().split()
idx1 = m.index('MemFree:')
idx2 = m.index('Buffers:')
idx3 = m.index('Cached:')
avail = (int(m[idx1 + 1]) + int(m[idx2 + 1]) + int(m[idx3 + 1]))
# Convert it to MB
return self.get_memory_mb_total() - avail // units.Ki
open proc/meminfo , parse :
linux-eabw:~ # cat /proc/meminfo
MemTotal: 263878132 kB
MemFree: 262125224 kB
MemAvailable: 261697292 kB
Buffers: 2196 kB
Cached: 821680 kB
SwapCached: 0 kB
>>> with open('/proc/meminfo') as fp:
... m = fp.read().split()
...
>>> m
['MemTotal:', '263878132', 'kB', 'MemFree:', '262121912', 'kB', 'MemAvailable:', '261693340', 'kB', 'Buffers:', '2196', 'kB', 'Cached:', '821332', 'kB', 'SwapCached:', '0', 'kB', 'Active:', '720800', 'kB', 'Inactive:', '261540', 'kB', 'Active(anon):', '159636', 'kB', 'Inactive(anon):', '9520', 'kB', 'Active(file):', '561164', 'kB', 'Inactive(file):', '252020', 'kB', 'Unevictable:', '80', 'kB', 'Mlocked:', '80', 'kB', 'SwapTotal:', '2104316', 'kB', 'SwapFree:', '2104316', 'kB', 'Dirty:', '0', 'kB', 'Writeback:', '0', 'kB', 'AnonPages:', '159132', 'kB', 'Mapped:', '100644', 'kB', 'Shmem:', '10104', 'kB', 'Slab:', '128496', 'kB', 'SReclaimable:', '57512', 'kB', 'SUnreclaim:', '70984', 'kB', 'KernelStack:', '12336', 'kB', 'PageTables:', '2856', 'kB', 'NFS_Unstable:', '0', 'kB', 'Bounce:', '0', 'kB', 'WritebackTmp:', '0', 'kB', 'CommitLimit:', '134043380', 'kB', 'Committed_AS:', '1008196', 'kB', 'VmallocTotal:', '135290290112', 'kB', 'VmallocUsed:', '0', 'kB', 'VmallocChunk:', '0', 'kB', 'AnonHugePages:', '79872', 'kB', 'CmaTotal:', '0', 'kB', 'CmaFree:', '0', 'kB', 'HugePages_Total:', '0', 'HugePages_Free:', '0', 'HugePages_Rsvd:', '0', 'HugePages_Surp:', '0', 'Hugepagesize:', '2048', 'kB']
>>> idx1 = m.index('MemFree:')
>>> idx1
3
>>> int(m[idx1])
Traceback (most recent call last):
File "", line 1, in
ValueError: invalid literal for int() with base 10: 'MemFree:'
>>> int(m[idx1+1])
262121912
>>>