Francesco Romani has uploaded a new change for review.

Change subject: sampling: simplify CpuCoreSample
......................................................................

sampling: simplify CpuCoreSample

This patch aims to simplify and strip the baggage from CpuCoreSample

- make it a plain function returning a plain dict (of CpuSample
  namedtuples)
- remove getCoreSample helper

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


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/51/41251/1

diff --git a/vdsm/virt/sampling.py b/vdsm/virt/sampling.py
index 6b13913..65f1a98 100644
--- a/vdsm/virt/sampling.py
+++ b/vdsm/virt/sampling.py
@@ -133,29 +133,26 @@
         return CpuSample(user + userNice, userNice, sys, idle)
 
 
-class CpuCoreSample(object):
+_CPU_CORE_STATS_PATTERN = re.compile(r'cpu(\d+)\s+(.*)')
+
+
+def _get_cpu_core_sample():
     """
     A sample of the CPU consumption of each core
 
     The sample is taken at initialization time and can't be updated.
     """
-    CPU_CORE_STATS_PATTERN = re.compile(r'cpu(\d+)\s+(.*)')
 
-    def __init__(self):
-        self.coresSample = {}
-        with open('/proc/stat') as src:
-            for line in src:
-                match = self.CPU_CORE_STATS_PATTERN.match(line)
-                if match:
-                    user, userNice, sys, idle = \
-                        map(int, match.group(2).split()[0:4])
-                    self.coresSample[match.group(1)] = CpuSample(
-                        user, userNice, sys, idle)
-
-    def getCoreSample(self, coreId):
-        strCoreId = str(coreId)
-        if strCoreId in self.coresSample:
-            return self.coresSample[strCoreId]
+    with open('/proc/stat') as src:
+        cores_sample = {}
+        for line in src:
+            match = _CPU_CORE_STATS_PATTERN.match(line)
+            if match:
+                user, userNice, sys, idle = map(
+                    int, match.group(2).split()[0:4])
+                cores_sample[match.group(1)] = CpuSample(
+                    user, userNice, sys, idle)
+        return cores_sample
 
 
 class NumaNodeMemorySample(object):
@@ -278,7 +275,7 @@
                 self.thpState = s[s.index('[') + 1:s.index(']')]
         except:
             self.thpState = 'never'
-        self.cpuCores = CpuCoreSample()
+        self.cpuCores = _get_cpu_core_sample()
         self.numaNodeMem = NumaNodeMemorySample()
         ENGINE_DEFAULT_POLL_INTERVAL = 15
         try:
@@ -651,8 +648,8 @@
                 coreStat['nodeIndex'] = int(nodeIndex)
                 hs0, hs1 = self._samples[0], self._samples[-1]
                 interval = hs1.timestamp - hs0.timestamp
-                sample0 = hs0.cpuCores.getCoreSample(cpuCore)
-                sample1 = hs1.cpuCores.getCoreSample(cpuCore)
+                sample0 = hs0.cpuCores.get(cpuCore)
+                sample1 = hs1.cpuCores.get(cpuCore)
 
                 jiffies = (sample1.user - sample0.user) % (2 ** 32)
                 coreStat['cpuUser'] = ("%.2f" % (jiffies / interval))


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

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