Francesco Romani has uploaded a new change for review.

Change subject: sampling: move cpu usage computations into helper
......................................................................

sampling: move cpu usage computations into helper

Factor out repeated cpu usage computations
(user, sys, nice) into helper functions, to
reduce duplications

Change-Id: I9cc7c89667321cc5150f059430cf8b8cb3db18a0
Signed-off-by: Francesco Romani <[email protected]>
---
M vdsm/virt/sampling.py
1 file changed, 26 insertions(+), 18 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/52/41252/1

diff --git a/vdsm/virt/sampling.py b/vdsm/virt/sampling.py
index 65f1a98..35112cb 100644
--- a/vdsm/virt/sampling.py
+++ b/vdsm/virt/sampling.py
@@ -606,17 +606,18 @@
             return stats
         hs0, hs1 = self._samples[0], self._samples[-1]
         interval = hs1.timestamp - hs0.timestamp
-        jiffies = (hs1.pidcpu.user - hs0.pidcpu.user) % (2 ** 32)
-        stats['cpuUserVdsmd'] = jiffies / interval
-        jiffies = (hs1.pidcpu.sys - hs0.pidcpu.sys) % (2 ** 32)
-        stats['cpuSysVdsmd'] = jiffies / interval
 
-        jiffies = (hs1.totcpu.user - hs0.totcpu.user) % (2 ** 32)
-        stats['cpuUser'] = jiffies / interval / self._ncpus
-        jiffies = (hs1.totcpu.sys - hs0.totcpu.sys) % (2 ** 32)
-        stats['cpuSys'] = jiffies / interval / self._ncpus
-        stats['cpuIdle'] = max(0.0,
-                               100.0 - stats['cpuUser'] - stats['cpuSys'])
+        stats['cpuUserVdsmd'] = _compute_cpu_usage(
+            hs0.pidcpu.user, hs1.pidcpu.user, interval)
+        stats['cpuSysVdsmd'] = _compute_cpu_usage(
+            hs0.pidcpu.sys, hs1.pidcpu.sys, interval)
+
+        stats['cpuUser'] = _compute_cpu_usage(
+            hs0.totcpu.user, hs1.totcpu.user, interval, self._ncpus)
+        stats['cpuSys'] = _compute_cpu_usage(
+            hs0.totcpu.sys, hs1.totcpu.sys, interval, self._ncpus)
+
+        stats['cpuIdle'] = _compute_cpu_idle(stats)
         stats['memUsed'] = hs1.memUsed
         stats['anonHugePages'] = hs1.anonHugePages
         stats['cpuLoad'] = hs1.cpuLoad
@@ -651,16 +652,14 @@
                 sample0 = hs0.cpuCores.get(cpuCore)
                 sample1 = hs1.cpuCores.get(cpuCore)
 
-                jiffies = (sample1.user - sample0.user) % (2 ** 32)
-                coreStat['cpuUser'] = ("%.2f" % (jiffies / interval))
+                coreStat['cpuUser'] = "%.2f" % (
+                    _compute_cpu_usage(sample0.user, sample1.user, interval))
 
-                jiffies = (sample1.sys - sample0.sys) % (2 ** 32)
-                coreStat['cpuSys'] = ("%.2f" % (jiffies / interval))
+                coreStat['cpuSys'] = "%.2f" % (
+                    _compute_cpu_usage(sample0.sys, sample1.sys, interval))
 
-                coreStat['cpuIdle'] = ("%.2f" %
-                                       max(0.0, 100.0 -
-                                           float(coreStat['cpuUser']) -
-                                           float(coreStat['cpuSys'])))
+                coreStat['cpuIdle'] = "%.2f" % (
+                    _compute_cpu_idle(coreStat))
                 cpuCoreStats[str(cpuCore)] = coreStat
         return cpuCoreStats
 
@@ -741,6 +740,15 @@
         return stats
 
 
+def _compute_cpu_usage(first_cpu_value, last_cpu_value, interval, ncpus=1.):
+    jiffies = (last_cpu_value - first_cpu_value) % (2 ** 32)
+    return jiffies / interval / ncpus
+
+
+def _compute_cpu_idle(cpu_stats):
+    return max(0.0, 100.0 - cpu_stats['cpuUser'] - cpu_stats['cpuSys'])
+
+
 def _getLinkSpeed(dev):
     if dev.isNIC():
         speed = netinfo.nicSpeed(dev.name)


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

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