Skip to content

Commit 8c3722d

Browse files
committed
Merge pull request #1444 from rafaelweingartner/workAroundPR780
CLOUDSTACK-8800 : Improved the listVirtualMachines API call to include memory utilization information for a VMThis PR introduces the changes proposed in PR #780 with some work to make the code null safe. During this PR, I have also removed some unused code. * pr/1444: Removed unnecessary check when creating the “userVmResponse” object. Fixed issues from CLOUDSTACK-8800 that were introduced in PR 780 CLOUDSTACK-8800 : Improved the listVirtualMachines API call to include memory utilization information for a VM for xenserver,kvm and for vmware. Signed-off-by: Will Stevens <[email protected]>
2 parents 20cf8b2 + 417d9a5 commit 8c3722d

File tree

10 files changed

+216
-54
lines changed

10 files changed

+216
-54
lines changed

api/src/com/cloud/vm/VmStats.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ public interface VmStats {
3232

3333
public double getDiskWriteKBs();
3434

35+
public double getMemoryKBs();
36+
37+
public double getIntFreeMemoryKBs();
38+
39+
public double getTargetMemoryKBs();
40+
3541
}

api/src/org/apache/cloudstack/api/response/UserVmResponse.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,18 @@ public class UserVmResponse extends BaseResponseWithTagInformation implements Co
196196
@Param(description = "the write (bytes) of disk on the vm")
197197
private Long diskKbsWrite;
198198

199+
@SerializedName("memorykbs")
200+
@Param(description = "the memory used by the vm")
201+
private Long memoryKBs;
202+
203+
@SerializedName("memoryintfreekbs")
204+
@Param(description = "the internal memory thats free in vm")
205+
private Long memoryIntFreeKBs;
206+
207+
@SerializedName("memorytargetkbs")
208+
@Param(description = "the target memory in vm")
209+
private Long memoryTargetKBs;
210+
199211
@SerializedName("diskioread")
200212
@Param(description = "the read (io) of disk on the vm")
201213
private Long diskIORead;
@@ -462,6 +474,18 @@ public Long getDiskKbsWrite() {
462474
return diskKbsWrite;
463475
}
464476

477+
public Long getMemoryKBs() {
478+
return memoryKBs;
479+
}
480+
481+
public Long getMemoryIntFreeKBs() {
482+
return memoryIntFreeKBs;
483+
}
484+
485+
public Long getMemoryTargetKBs() {
486+
return memoryTargetKBs;
487+
}
488+
465489
public Long getDiskIORead() {
466490
return diskIORead;
467491
}
@@ -637,6 +661,18 @@ public void setDiskIORead(Long diskIORead) {
637661
this.diskIORead = diskIORead;
638662
}
639663

664+
public void setMemoryKBs(Long memoryKBs) {
665+
this.memoryKBs = memoryKBs;
666+
}
667+
668+
public void setMemoryIntFreeKBs(Long memoryIntFreeKBs) {
669+
this.memoryIntFreeKBs = memoryIntFreeKBs;
670+
}
671+
672+
public void setMemoryTargetKBs(Long memoryTargetKBs) {
673+
this.memoryTargetKBs = memoryTargetKBs;
674+
}
675+
640676
public void setDiskIOWrite(Long diskIOWrite) {
641677
this.diskIOWrite = diskIOWrite;
642678
}

core/src/com/cloud/agent/api/VmStatsEntry.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,19 @@ public class VmStatsEntry implements VmStats {
3030
double diskWriteIOs;
3131
double diskReadKBs;
3232
double diskWriteKBs;
33+
double memoryKBs;
34+
double intfreememoryKBs;
35+
double targetmemoryKBs;
3336
int numCPUs;
3437
String entityType;
3538

3639
public VmStatsEntry() {
3740
}
3841

39-
public VmStatsEntry(double cpuUtilization, double networkReadKBs, double networkWriteKBs, int numCPUs, String entityType) {
42+
public VmStatsEntry(double memoryKBs,double intfreememoryKBs,double targetmemoryKBs, double cpuUtilization, double networkReadKBs, double networkWriteKBs, int numCPUs, String entityType) {
43+
this.memoryKBs = memoryKBs;
44+
this.intfreememoryKBs = intfreememoryKBs;
45+
this.targetmemoryKBs = targetmemoryKBs;
4046
this.cpuUtilization = cpuUtilization;
4147
this.networkReadKBs = networkReadKBs;
4248
this.networkWriteKBs = networkWriteKBs;
@@ -117,6 +123,33 @@ public void setDiskWriteKBs(double diskWriteKBs) {
117123
this.diskWriteKBs = diskWriteKBs;
118124
}
119125

126+
@Override
127+
public double getMemoryKBs() {
128+
return memoryKBs;
129+
}
130+
131+
public void setMemoryKBs(double memoryKBs) {
132+
this.memoryKBs = memoryKBs;
133+
}
134+
135+
@Override
136+
public double getIntFreeMemoryKBs() {
137+
return intfreememoryKBs;
138+
}
139+
140+
public void setIntFreeMemoryKBs(double intfreememoryKBs) {
141+
this.intfreememoryKBs = intfreememoryKBs;
142+
}
143+
144+
@Override
145+
public double getTargetMemoryKBs() {
146+
return targetmemoryKBs;
147+
}
148+
149+
public void setTargetMemoryKBs(double targetmemoryKBs) {
150+
this.targetmemoryKBs = targetmemoryKBs;
151+
}
152+
120153
public int getNumCPUs() {
121154
return numCPUs;
122155
}

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

100755100644
Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
5353
import org.apache.commons.io.FileUtils;
5454
import org.apache.commons.io.IOUtils;
55+
import org.apache.commons.lang.ArrayUtils;
56+
import org.apache.commons.lang.math.NumberUtils;
5557
import org.apache.log4j.Logger;
5658
import org.libvirt.Connect;
5759
import org.libvirt.Domain;
@@ -60,6 +62,7 @@
6062
import org.libvirt.DomainInfo.DomainState;
6163
import org.libvirt.DomainInterfaceStats;
6264
import org.libvirt.LibvirtException;
65+
import org.libvirt.MemoryStatistic;
6366
import org.libvirt.NodeInfo;
6467

6568
import com.cloud.agent.api.Answer;
@@ -188,8 +191,8 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
188191
private String _clusterId;
189192

190193
private long _hvVersion;
191-
private long _kernelVersion;
192194
private int _timeout;
195+
private static final int NUMMEMSTATS =2;
193196

194197
private KVMHAMonitor _monitor;
195198
public static final String SSHKEYSPATH = "/root/.ssh";
@@ -956,13 +959,6 @@ public boolean configure(final String name, final Map<String, Object> params) th
956959
storageProcessor.configure(name, params);
957960
storageHandler = new StorageSubsystemCommandHandlerBase(storageProcessor);
958961

959-
final String unameKernelVersion = Script.runSimpleBashScript("uname -r");
960-
final String[] kernelVersions = unameKernelVersion.split("[\\.\\-]");
961-
_kernelVersion = Integer.parseInt(kernelVersions[0]) * 1000 * 1000 + (long)Integer.parseInt(kernelVersions[1]) * 1000 + Integer.parseInt(kernelVersions[2]);
962-
963-
/* Disable this, the code using this is pretty bad and non portable
964-
* getOsVersion();
965-
*/
966962
return true;
967963
}
968964

@@ -3025,12 +3021,19 @@ public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws Li
30253021
Domain dm = null;
30263022
try {
30273023
dm = getDomain(conn, vmName);
3028-
final DomainInfo info = dm.getInfo();
3029-
3024+
if (dm == null) {
3025+
return null;
3026+
}
3027+
DomainInfo info = dm.getInfo();
30303028
final VmStatsEntry stats = new VmStatsEntry();
3029+
30313030
stats.setNumCPUs(info.nrVirtCpu);
30323031
stats.setEntityType("vm");
30333032

3033+
stats.setMemoryKBs(info.maxMem);
3034+
stats.setTargetMemoryKBs(info.memory);
3035+
stats.setIntFreeMemoryKBs(getMemoryFreeInKBs(dm));
3036+
30343037
/* get cpu utilization */
30353038
VmStats oldStats = null;
30363039

@@ -3124,6 +3127,21 @@ public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws Li
31243127
}
31253128
}
31263129

3130+
/**
3131+
* This method retrieves the memory statistics from the domain given as parameters.
3132+
* If no memory statistic is found, it will return {@link NumberUtils#LONG_ZERO} as the value of free memory in the domain.
3133+
* If it can retrieve the domain memory statistics, it will return the free memory statistic; that means, it returns the value at the first position of the array returned by {@link Domain#memoryStats(int)}.
3134+
*
3135+
* @return the amount of free memory in KBs
3136+
*/
3137+
protected long getMemoryFreeInKBs(Domain dm) throws LibvirtException {
3138+
MemoryStatistic[] mems = dm.memoryStats(NUMMEMSTATS);
3139+
if (ArrayUtils.isEmpty(mems)) {
3140+
return NumberUtils.LONG_ZERO;
3141+
}
3142+
return mems[0].getValue();
3143+
}
3144+
31273145
private boolean canBridgeFirewall(final String prvNic) {
31283146
final Script cmd = new Script(_securityGroupPath, _timeout, s_logger);
31293147
cmd.add("can_bridge_firewall");

0 commit comments

Comments
 (0)