From 81ac4e729c0ca9e8fdb8064db30ae05eb8ce74a7 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Wed, 7 Mar 2012 12:49:38 -0500 Subject: Introduce a class for storing libvirt CPU configuration Extend the libvirt config APIs to include a new class LibvirtConfigCPU for storing host/guest CPU configuration data blueprint libvirt-xml-config-apis Change-Id: Ib508637c1e0ca69860d461b0a480347c59165e6b Signed-off-by: Daniel P. Berrange --- nova/tests/test_libvirt_config.py | 37 ++++++++++++++++++++++++++++++++++++ nova/virt/libvirt/config.py | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/nova/tests/test_libvirt_config.py b/nova/tests/test_libvirt_config.py index 186fb5a18..8d6ede1be 100644 --- a/nova/tests/test_libvirt_config.py +++ b/nova/tests/test_libvirt_config.py @@ -361,3 +361,40 @@ class LibvirtConfigGuestTest(LibvirtConfigBaseTest): """) + + +class LibvirtConfigCPUTest(LibvirtConfigBaseTest): + + def test_config_cpu(self): + obj = config.LibvirtConfigCPU() + obj.vendor = "AMD" + obj.model = "Quad-Core AMD Opteron(tm) Processor 2350" + obj.arch = "x86_64" + obj.add_feature("svm") + obj.add_feature("extapic") + obj.add_feature("constant_tsc") + + xml = obj.to_xml() + self.assertXmlEqual(xml, """ + + x86_64 + Quad-Core AMD Opteron(tm) Processor 2350 + AMD + + + + """) + + def test_config_topology(self): + obj = config.LibvirtConfigCPU() + obj.vendor = "AMD" + obj.sockets = 2 + obj.cores = 4 + obj.threads = 2 + + xml = obj.to_xml() + self.assertXmlEqual(xml, """ + + AMD + + """) diff --git a/nova/virt/libvirt/config.py b/nova/virt/libvirt/config.py index 44790a068..e4f99a7ba 100644 --- a/nova/virt/libvirt/config.py +++ b/nova/virt/libvirt/config.py @@ -356,3 +356,43 @@ class LibvirtConfigGuest(LibvirtConfigObject): def add_device(self, dev): self.devices.append(dev) + + +class LibvirtConfigCPU(LibvirtConfigObject): + + def __init__(self, **kwargs): + super(LibvirtConfigCPU, self).__init__(root_name="cpu", + **kwargs) + + self.arch = None + self.model = None + self.vendor = None + self.sockets = None + self.cores = None + self.threads = None + self.features = [] + + def add_feature(self, name): + self.features.append(name) + + def format_dom(self): + cpu = super(LibvirtConfigCPU, self).format_dom() + if self.arch: + cpu.append(self._text_node("arch", self.arch)) + if self.model: + cpu.append(self._text_node("model", self.model)) + if self.vendor: + cpu.append(self._text_node("vendor", self.vendor)) + if (self.sockets is not None and + self.cores is not None and + self.threads is not None): + cpu.append(etree.Element("topology", + sockets=str(self.sockets), + cores=str(self.cores), + threads=str(self.threads))) + + for f in self.features: + cpu.append(etree.Element("feature", + name=f)) + + return cpu -- cgit