cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bhais...@apache.org
Subject git commit: updated refs/heads/CLOUDSTACK-8247 to 1f99692
Date Fri, 22 May 2015 13:45:48 GMT
Repository: cloudstack
Updated Branches:
  refs/heads/CLOUDSTACK-8247 [created] 1f99692d3


CLOUDSTACK-8247: Pull average Cpu util report between polling

Pull average Cpu util report between polling intervals instead of since boot
instead of using values since uptime

Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/1f99692d
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/1f99692d
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/1f99692d

Branch: refs/heads/CLOUDSTACK-8247
Commit: 1f99692d31913077167ced985ca5cf154e192149
Parents: 90ac1ab
Author: Rohit Yadav <rohit.yadav@shapeblue.com>
Authored: Fri May 22 12:20:50 2015 +0100
Committer: Rohit Yadav <rohit.yadav@shapeblue.com>
Committed: Fri May 22 14:45:14 2015 +0100

----------------------------------------------------------------------
 .../kvm/resource/LibvirtComputingResource.java  |  14 +--
 .../apache/cloudstack/utils/linux/CPUStat.java  | 100 +++++++++++++++++++
 2 files changed, 103 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/1f99692d/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
index a29ecb4..4ceccaa 100755
--- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
+++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
@@ -61,6 +61,7 @@ import java.util.regex.Pattern;
 import javax.ejb.Local;
 import javax.naming.ConfigurationException;
 
+import org.apache.cloudstack.utils.linux.CPUStat;
 import org.apache.cloudstack.utils.linux.MemStat;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
@@ -464,6 +465,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements
Serv
     protected int _timeout;
     protected int _cmdsTimeout;
     protected int _stopTimeout;
+    private CPUStat _cpuStat = new CPUStat();
     private MemStat _memStat = new MemStat();
 
     protected static final HashMap<DomainState, PowerState> s_powerStatesTable;
@@ -3266,17 +3268,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements
Serv
     }
 
     private Answer execute(GetHostStatsCommand cmd) {
-        final Script cpuScript = new Script("/bin/bash", s_logger);
-        cpuScript.add("-c");
-        cpuScript.add("idle=$(top -b -n 1| awk -F, '/^[%]*[Cc]pu/{$0=$4; gsub(/[^0-9.,]+/,\"\");
print }'); echo $idle");
-
-        final OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser();
-        String result = cpuScript.execute(parser);
-        if (result != null) {
-            s_logger.debug("Unable to get the host CPU state: " + result);
-            return new Answer(cmd, false, result);
-        }
-        double cpuUtil = (100.0D - Double.parseDouble(parser.getLine()));
+        double cpuUtil = _cpuStat.getCpuUsedPercent();
 
         _memStat.refresh();
         double totMem = _memStat.getTotal();

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/1f99692d/plugins/hypervisors/kvm/src/org/apache/cloudstack/utils/linux/CPUStat.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/kvm/src/org/apache/cloudstack/utils/linux/CPUStat.java b/plugins/hypervisors/kvm/src/org/apache/cloudstack/utils/linux/CPUStat.java
new file mode 100644
index 0000000..e275784
--- /dev/null
+++ b/plugins/hypervisors/kvm/src/org/apache/cloudstack/utils/linux/CPUStat.java
@@ -0,0 +1,100 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// the License.  You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.cloudstack.utils.linux;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.Scanner;
+
+public class CPUStat {
+    private Integer _cores;
+    private UptimeStats _lastStats;
+    private final String _sysfsCpuDir = "/sys/devices/system/cpu";
+    private final String _uptimeFile = "/proc/uptime";
+
+    class UptimeStats {
+        public Double upTime = 0d;
+        public Double cpuIdleTime = 0d;
+
+        public UptimeStats(Double upTime, Double cpuIdleTime) {
+            this.upTime = upTime;
+            this.cpuIdleTime = cpuIdleTime;
+        }
+    }
+
+    public CPUStat () {
+        init();
+    }
+
+    private void init() {
+        _cores = getCoresFromLinux();
+        _lastStats = getUptimeAndCpuIdleTime();
+    }
+
+    private UptimeStats getUptimeAndCpuIdleTime() {
+        UptimeStats uptime = new UpstimeStats(0d, 0d);
+        try {
+            String[] stats =  new Scanner(new File(_uptimeFile)).useDelimiter("\\Z").next().split("\\s+");
+            uptime = new UptimeStats(Double.parseDouble(stats[0]), Double.parseDouble(stats[1]));
+        } catch (FileNotFoundException ex) {
+            throw new RuntimeException("File " + _uptimeFile + " not found:" + ex.toString());
+        }
+        return uptime;
+    }
+
+    private Integer getCoresFromLinux() {
+        Integer cpus = 0;
+        File cpuDir = new File(_sysfsCpuDir);
+        File[] files = cpuDir.listFiles();
+        if (files != null) {
+            for (File file : files) {
+                if (file.getName().matches("cpu\\d+")) {
+                    cpus++;
+                }
+            }
+        }
+        return cpus;
+    }
+
+    public Integer getCores() {
+        return _cores;
+    }
+
+    public Double getCpuUsedPercent() {
+        Double cpuUsed = 0d;
+        if (_cores == null || _cores == 0) {
+            _cores = getCoresFromLinux();
+        }
+
+        UptimeStats currentStats = getUptimeAndCpuIdleTime();
+        if (currentStats == null) {
+            return cpuUsed;
+        }
+
+        Double timeElapsed = currentStats.upTime - _lastStats.upTime;
+        Double cpuElapsed = (currentStats.cpuIdleTime - _lastStats.cpuIdleTime) / _cores;
+        if (timeElapsed > 0) {
+            cpuUsed = (1 - (cpuElapsed / timeElapsed)) * 100;
+        }
+        if (cpuUsed < 0) {
+            cpuUsed = 0d;
+        }
+        _lastStats = currentStats;
+        return cpuUsed;
+    }
+}


Mime
View raw message