Skip to content

Commit

Permalink
Report memory correctly on Xen. Fixes bug 997014
Browse files Browse the repository at this point in the history
/proc/meminfo may show wrong values for the memory when using Xen, so
this fix computes the memory quering libivrt.

Change-Id: I188e2d34bcee13954653b93b9b816cf4530b8859
  • Loading branch information
alvarolopez committed May 10, 2012
1 parent 1d53273 commit 5b93a57
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions nova/virt/libvirt/connection.py
Expand Up @@ -1859,10 +1859,15 @@ def get_memory_mb_total():
if sys.platform.upper() not in ['LINUX2', 'LINUX3']:
return 0

meminfo = open('/proc/meminfo').read().split()
idx = meminfo.index('MemTotal:')
# transforming kb to mb.
return int(meminfo[idx + 1]) / 1024
if FLAGS.libvirt_type == 'xen':
meminfo = self._conn.getInfo()[1]
# this is in MB
return meminfo
else:
meminfo = open('/proc/meminfo').read().split()
idx = meminfo.index('MemTotal:')
# transforming KB to MB
return int(meminfo[idx + 1]) / 1024

@staticmethod
def get_local_gb_total():
Expand Down Expand Up @@ -1911,8 +1916,26 @@ def get_memory_mb_used(self):
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])) / 1024
return self.get_memory_mb_total() - avail
if FLAGS.libvirt_type == 'xen':
used = 0
for domain_id in self._conn.listDomainsID():
# skip dom0
dom_mem = int(self._conn.lookupByID(domain_id).info()[2])
if domain_id != 0:
used += dom_mem
else:
# the mem reported by dom0 is be greater of what
# it is being used
used += (dom_mem -
(int(m[idx1 + 1]) +
int(m[idx2 + 1]) +
int(m[idx3 + 1])))
# Convert it to MB
return used / 1024
else:
avail = (int(m[idx1 + 1]) + int(m[idx2 + 1]) + int(m[idx3 + 1]))
# Convert it to MB
return self.get_memory_mb_total() - avail / 1024

def get_local_gb_used(self):
"""Get the free hdd size(GB) of physical computer.
Expand Down

0 comments on commit 5b93a57

Please sign in to comment.