XenAPI support: Disk rates

Implemented inspect_disk_rates() to inspect the disk statistics as rates
for an instance.

Change-Id: I0220e612b156a967745ff281ce09329fa0d15a12
Implements: blueprint xenapi-support
This commit is contained in:
Qiaowei Ren 2014-08-18 10:28:05 +08:00 committed by Ren Qiaowei
parent 83e216fe0d
commit edee5d4275
2 changed files with 61 additions and 0 deletions

View File

@ -167,3 +167,27 @@ class XenapiInspector(virt_inspector.Inspector):
tx_rate = float(vif_metrics_rec['io_write_kbs']) * units.Ki tx_rate = float(vif_metrics_rec['io_write_kbs']) * units.Ki
stats = virt_inspector.InterfaceRateStats(rx_rate, tx_rate) stats = virt_inspector.InterfaceRateStats(rx_rate, tx_rate)
yield (interface, stats) yield (interface, stats)
def inspect_disk_rates(self, instance, duration=None):
instance_name = util.instance_name(instance)
vm_ref = self._lookup_by_name(instance_name)
vbd_refs = self._call_xenapi("VM.get_VBDs", vm_ref)
if vbd_refs:
for vbd_ref in vbd_refs:
vbd_rec = self._call_xenapi("VBD.get_record", vbd_ref)
vbd_metrics_ref = self._call_xenapi("VBD.get_metrics",
vbd_ref)
vbd_metrics_rec = self._call_xenapi("VBD_metrics.get_record",
vbd_metrics_ref)
disk = virt_inspector.Disk(device=vbd_rec['device'])
# Stats provided from XenServer are in KB/s,
# converting it to B/s.
read_rate = float(vbd_metrics_rec['io_read_kbs']) * units.Ki
write_rate = float(vbd_metrics_rec['io_write_kbs']) * units.Ki
disk_rate_info = virt_inspector.DiskRateStats(
read_bytes_rate=read_rate,
read_requests_rate=0,
write_bytes_rate=write_rate,
write_requests_rate=0)
yield(disk, disk_rate_info)

View File

@ -139,3 +139,40 @@ class TestXenapiInspection(base.BaseTestCase):
self.assertEqual('vif_mac', vnic0.mac) self.assertEqual('vif_mac', vnic0.mac)
self.assertEqual(1024, info0.rx_bytes_rate) self.assertEqual(1024, info0.rx_bytes_rate)
self.assertEqual(2048, info0.tx_bytes_rate) self.assertEqual(2048, info0.tx_bytes_rate)
def test_inspect_disk_rates(self):
fake_instance = {'OS-EXT-SRV-ATTR:instance_name': 'fake_instance_name',
'id': 'fake_instance_id'}
def fake_xenapi_request(method, args):
vbd_rec = {
'device': 'xvdd'
}
vbd_metrics_rec = {
'io_read_kbs': '1',
'io_write_kbs': '2'
}
if method == 'VM.get_by_name_label':
return ['vm_ref']
elif method == 'VM.get_VBDs':
return ['vbd_ref']
elif method == 'VBD.get_record':
return vbd_rec
elif method == 'VBD.get_metrics':
return 'vbd_metrics_ref'
elif method == 'VBD_metrics.get_record':
return vbd_metrics_rec
else:
return None
session = self.inspector.session
with mock.patch.object(session, 'xenapi_request',
side_effect=fake_xenapi_request):
disks = list(self.inspector.inspect_disk_rates(fake_instance))
self.assertEqual(1, len(disks))
disk0, info0 = disks[0]
self.assertEqual('xvdd', disk0.device)
self.assertEqual(1024, info0.read_bytes_rate)
self.assertEqual(2048, info0.write_bytes_rate)