Change in vdsm[master]: vmstats: from EAFP to LBYL

fromani at redhat.com fromani at redhat.com
Tue Feb 16 18:05:27 UTC 2016


Francesco Romani has uploaded a new change for review.

Change subject: vmstats: from EAFP to LBYL
......................................................................

vmstats: from EAFP to LBYL

libvirt bulk stats' API allows to return partial result.
Hence, seems better to Look Before You Leap in this flow,
instead of thinking that is Easier (to) Ask Forgiveness (than)
Permission.

As nice side effect, this patch removes spam from the logs.

Change-Id: I7383ff62e8c7e7923567b6d04942bb1e92aab84a
Bug-Url: https://bugzilla.redhat.com/1299480
Backport-To: 3.6
Signed-off-by: Francesco Romani <fromani at redhat.com>
---
M vdsm/virt/vmstats.py
1 file changed, 5 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/13/53613/1

diff --git a/vdsm/virt/vmstats.py b/vdsm/virt/vmstats.py
index 447d5e6..eada899 100644
--- a/vdsm/virt/vmstats.py
+++ b/vdsm/virt/vmstats.py
@@ -109,7 +109,10 @@
             interval)
         return None
 
-    try:
+    keys = ('cpu.system', 'cpu.user')
+    samples = (last_sample, first_sample)
+
+    if all(k in s for k in keys for s in samples):
         stats['cpuUsage'] = str(last_sample['cpu.system'] +
                                 last_sample['cpu.user'])
 
@@ -117,12 +120,10 @@
                    (last_sample['cpu.system'] - first_sample['cpu.system']))
         stats['cpuSys'] = _usage_percentage(cpu_sys, interval)
 
+    if all('cpu.time' in s for s in samples):
         stats['cpuUser'] = _usage_percentage(
             (last_sample['cpu.time'] - first_sample['cpu.time']) - cpu_sys,
             interval)
-
-    except KeyError as e:
-        logging.exception("CPU stats not available: %s", e)
 
     return stats
 


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7383ff62e8c7e7923567b6d04942bb1e92aab84a
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