Francesco Romani has uploaded a new change for review.

Change subject: sampling: stats: remove try/except for KeyError
......................................................................

sampling: stats: remove try/except for KeyError

Catching general exceptions should be done
sparingly. Let's remove a try/except block
guarding for KeyError.

As side effect, this streamlines the function.

Change-Id: I40120e3f89a812126b9dd74495be6a1fad461b17
Signed-off-by: Francesco Romani <[email protected]>
---
M vdsm/virt/vmstats.py
1 file changed, 15 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/44/42744/1

diff --git a/vdsm/virt/vmstats.py b/vdsm/virt/vmstats.py
index a434a8c..1978f2a 100644
--- a/vdsm/virt/vmstats.py
+++ b/vdsm/virt/vmstats.py
@@ -81,22 +81,23 @@
             interval, vm.id)
         return
 
-    try:
-        stats['cpuUsage'] = str(last_sample['cpu.system'] +
-                                last_sample['cpu.user'])
+    if any(k not in sample
+           for k in ('cpu.user', 'cpu.system', 'cpu.time')
+           for sample in (first_sample, last_sample)):
+        return
 
-        stats['cpuSys'] = _usage_percentage(
-            _diff(last_sample, first_sample, 'cpu.user') +
-            _diff(last_sample, first_sample, 'cpu.system'),
-            interval)
-        stats['cpuUser'] = _usage_percentage(
-            _diff(last_sample, first_sample, 'cpu.time')
-            - _diff(last_sample, first_sample, 'cpu.user')
-            - _diff(last_sample, first_sample, 'cpu.system'),
-            interval)
+    stats['cpuUsage'] = str(last_sample['cpu.system'] +
+                            last_sample['cpu.user'])
 
-    except KeyError:
-        logging.warning("CPU stats not available for %s", vm.id)
+    stats['cpuSys'] = _usage_percentage(
+        _diff(last_sample, first_sample, 'cpu.user') +
+        _diff(last_sample, first_sample, 'cpu.system'),
+        interval)
+    stats['cpuUser'] = _usage_percentage(
+        _diff(last_sample, first_sample, 'cpu.time')
+        - _diff(last_sample, first_sample, 'cpu.user')
+        - _diff(last_sample, first_sample, 'cpu.system'),
+        interval)
 
 
 def balloon(vm, stats, sample):


-- 
To view, visit https://gerrit.ovirt.org/42744
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I40120e3f89a812126b9dd74495be6a1fad461b17
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Francesco Romani <[email protected]>
_______________________________________________
vdsm-patches mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to