Skip to content

Commit

Permalink
qemu_monitor: Add info_numa() method
Browse files Browse the repository at this point in the history
Unit test for the response parsing included.

Signed-off-by: Eduardo Habkost <[email protected]>
  • Loading branch information
ehabkost authored and lmr committed Feb 26, 2013
1 parent fc9dc11 commit 76ac528
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
45 changes: 45 additions & 0 deletions virttest/qemu_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,51 @@ def human_monitor_cmd(self, cmd="", timeout=None,
raise NotImplementedError


# Methods that should work on both classes, as long as human_monitor_cmd()
# works:

re_numa_nodes = re.compile(r"^([0-9]+) nodes$", re.M)
re_numa_node_info = re.compile(r"^node ([0-9]+) (cpus|size): (.*)$", re.M)
@classmethod
def parse_info_numa(cls, r):
"""
Parse 'info numa' output
See info_numa() for information about the return value.
"""

nodes = cls.re_numa_nodes.search(r)
if nodes is None:
raise Exception("Couldn't get number of nodes from 'info numa' output")
nodes = int(nodes.group(1))

data = [[0, set()] for i in range(nodes)]
for nodenr,field,value in cls.re_numa_node_info.findall(r):
nodenr = int(nodenr)
if nodenr > nodes:
raise Exception("Invalid node number on 'info numa' output: %d", nodenr)
if field == 'size':
if not value.endswith(' MB'):
raise Exception("Unexpected size value: %s", value)
megabytes = int(value[:-3])
data[nodenr][0] = megabytes
elif field == 'cpus':
cpus = set([int(v) for v in value.split()])
data[nodenr][1] = cpus
data = [tuple(i) for i in data]
return data

def info_numa(self):
"""
Run 'info numa' command and parse returned information
@return: An array of (ram, cpus) tuples, where ram is the RAM size in
MB and cpus is a set of CPU numbers
"""
r = self.human_monitor_cmd("info numa")
return self.parse_info_numa(r)


class HumanMonitor(Monitor):
"""
Wraps "human monitor" commands.
Expand Down
21 changes: 21 additions & 0 deletions virttest/qemu_monitor_unittest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest
from qemu_monitor import Monitor

class InfoNumaTests(unittest.TestCase):
def testZeroNodes(self):
d = "0 nodes\n"
r = Monitor.parse_info_numa(d)
self.assertEquals(r, [])

def testTwoNodes(self):
d = "2 nodes\n" + \
"node 0 cpus: 0 2 4\n" + \
"node 0 size: 12 MB\n" + \
"node 1 cpus: 1 3 5\n" + \
"node 1 size: 34 MB\n"
r = Monitor.parse_info_numa(d)
self.assertEquals(r, [(12, set([0, 2, 4])),
(34, set([1, 3, 5]))])

if __name__ == "__main__":
unittest.main()

0 comments on commit 76ac528

Please sign in to comment.