Change in vdsm[master]: vm: make _getNicStats a function

fromani at redhat.com fromani at redhat.com
Mon Feb 2 15:09:19 UTC 2015


Francesco Romani has uploaded a new change for review.

Change subject: vm: make _getNicStats a function
......................................................................

vm: make _getNicStats a function

_getNicStats used to be a classmethod of VmStatsThread,
but there are no valid reasons anymore to keep it like that.

This patch makes it a plain function, to make room
for more refactoring in this area.

There are no changes in logic.
Except for moving code around, the only significant change
is the removal of the now-useless 'cls' argument.

Change-Id: Ia191fce7a1f3330013a621d97038bb32fd3cd563
Signed-off-by: Francesco Romani <fromani at redhat.com>
---
M tests/vmTests.py
M vdsm/virt/vm.py
2 files changed, 51 insertions(+), 53 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/57/37457/1

diff --git a/tests/vmTests.py b/tests/vmTests.py
index 2b96dd8..919d112 100644
--- a/tests/vmTests.py
+++ b/tests/vmTests.py
@@ -1519,27 +1519,25 @@
     def testGetNicStats(self):
         GBPS = 10 ** 9 / 8
         MAC = '52:54:00:59:F5:3F'
-        with fake.VM() as testvm:
-            mock_stats_thread = vm.VmStatsThread(testvm)
-            pretime = utils.monotonic_time()
-            res = mock_stats_thread._getNicStats(
-                name='vnettest', model='virtio', mac=MAC,
-                start_sample=(2 ** 64 - 15 * GBPS, 1, 2, 3, 0, 4, 5, 6),
-                end_sample=(0, 7, 8, 9, 5 * GBPS, 10, 11, 12),
-                interval=15.0)
-            posttime = utils.monotonic_time()
-            self.assertIn('sampleTime', res)
-            self.assertTrue(pretime <= res['sampleTime'] <= posttime,
-                            'sampleTime not in [%s..%s]' % (pretime, posttime))
-            del res['sampleTime']
-            self.assertEqual(res, {
-                'rxErrors': '8', 'rxDropped': '9',
-                'txErrors': '11', 'txDropped': '12',
-                'macAddr': MAC, 'name': 'vnettest',
-                'speed': '1000', 'state': 'unknown',
-                'rxRate': '100.0', 'txRate': '33.3',
-                'rx': '0', 'tx': '625000000',
-            })
+        pretime = utils.monotonic_time()
+        res = vm._getNicStats(
+            name='vnettest', model='virtio', mac=MAC,
+            start_sample=(2 ** 64 - 15 * GBPS, 1, 2, 3, 0, 4, 5, 6),
+            end_sample=(0, 7, 8, 9, 5 * GBPS, 10, 11, 12),
+            interval=15.0)
+        posttime = utils.monotonic_time()
+        self.assertIn('sampleTime', res)
+        self.assertTrue(pretime <= res['sampleTime'] <= posttime,
+                        'sampleTime not in [%s..%s]' % (pretime, posttime))
+        del res['sampleTime']
+        self.assertEqual(res, {
+            'rxErrors': '8', 'rxDropped': '9',
+            'txErrors': '11', 'txDropped': '12',
+            'macAddr': MAC, 'name': 'vnettest',
+            'speed': '1000', 'state': 'unknown',
+            'rxRate': '100.0', 'txRate': '33.3',
+            'rx': '0', 'tx': '625000000',
+        })
 
     def testGetStatsNoDom(self):
         # bz1073478 - main case
diff --git a/vdsm/virt/vm.py b/vdsm/virt/vm.py
index ff6abba..875cea6 100644
--- a/vdsm/virt/vm.py
+++ b/vdsm/virt/vm.py
@@ -561,37 +561,6 @@
             value = sample['vcpuLimit']
             stats['vcpuUserLimit'] = value
 
-    @classmethod
-    def _getNicStats(cls, name, model, mac,
-                     start_sample, end_sample, interval):
-        ifSpeed = [100, 1000][model in ('e1000', 'virtio')]
-
-        ifStats = {'macAddr': mac,
-                   'name': name,
-                   'speed': str(ifSpeed),
-                   'state': 'unknown'}
-
-        ifStats['rxErrors'] = str(end_sample[2])
-        ifStats['rxDropped'] = str(end_sample[3])
-        ifStats['txErrors'] = str(end_sample[6])
-        ifStats['txDropped'] = str(end_sample[7])
-
-        ifRxBytes = (100.0 *
-                     ((end_sample[0] - start_sample[0]) % 2 ** 32) /
-                     interval / ifSpeed / MBPS_TO_BPS)
-        ifTxBytes = (100.0 *
-                     ((end_sample[4] - start_sample[4]) % 2 ** 32) /
-                     interval / ifSpeed / MBPS_TO_BPS)
-
-        ifStats['rxRate'] = '%.1f' % ifRxBytes
-        ifStats['txRate'] = '%.1f' % ifTxBytes
-
-        ifStats['rx'] = str(end_sample[0])
-        ifStats['tx'] = str(end_sample[4])
-        ifStats['sampleTime'] = utils.monotonic_time()
-
-        return ifStats
-
     def _getNetworkStats(self, stats):
         stats['network'] = {}
         sInfo, eInfo, sampleInterval = self.sampleNet.getStats()
@@ -607,7 +576,7 @@
             if nic.name not in sInfo or nic.name not in eInfo:
                 continue
 
-            stats['network'][nic.name] = self._getNicStats(
+            stats['network'][nic.name] = _getNicStats(
                 nic.name, nic.nicModel, nic.macAddr,
                 sInfo[nic.name], eInfo[nic.name], sampleInterval)
 
@@ -680,6 +649,37 @@
         return True
 
 
+def _getNicStats(name, model, mac,
+                 start_sample, end_sample, interval):
+    ifSpeed = [100, 1000][model in ('e1000', 'virtio')]
+
+    ifStats = {'macAddr': mac,
+               'name': name,
+               'speed': str(ifSpeed),
+               'state': 'unknown'}
+
+    ifStats['rxErrors'] = str(end_sample[2])
+    ifStats['rxDropped'] = str(end_sample[3])
+    ifStats['txErrors'] = str(end_sample[6])
+    ifStats['txDropped'] = str(end_sample[7])
+
+    ifRxBytes = (100.0 *
+                 ((end_sample[0] - start_sample[0]) % 2 ** 32) /
+                 interval / ifSpeed / MBPS_TO_BPS)
+    ifTxBytes = (100.0 *
+                 ((end_sample[4] - start_sample[4]) % 2 ** 32) /
+                 interval / ifSpeed / MBPS_TO_BPS)
+
+    ifStats['rxRate'] = '%.1f' % ifRxBytes
+    ifStats['txRate'] = '%.1f' % ifTxBytes
+
+    ifStats['rx'] = str(end_sample[0])
+    ifStats['tx'] = str(end_sample[4])
+    ifStats['sampleTime'] = utils.monotonic_time()
+
+    return ifStats
+
+
 def _calcDiskRate(vmDrive, sInfo, eInfo, sampleInterval):
     return {
         'readRate': (


-- 
To view, visit http://gerrit.ovirt.org/37457
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia191fce7a1f3330013a621d97038bb32fd3cd563
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Francesco Romani <fromani at redhat.com>


More information about the vdsm-patches mailing list