Skip to content

Commit

Permalink
Merge pull request #1444 from rafaelweingartner/workAroundPR780
Browse files Browse the repository at this point in the history
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 <williamstevens@gmail.com>
  • Loading branch information
swill committed May 12, 2016
2 parents 20cf8b2 + 417d9a5 commit 8c3722d
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 54 deletions.
6 changes: 6 additions & 0 deletions api/src/com/cloud/vm/VmStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ public interface VmStats {

public double getDiskWriteKBs();

public double getMemoryKBs();

public double getIntFreeMemoryKBs();

public double getTargetMemoryKBs();

}
36 changes: 36 additions & 0 deletions api/src/org/apache/cloudstack/api/response/UserVmResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ public class UserVmResponse extends BaseResponseWithTagInformation implements Co
@Param(description = "the write (bytes) of disk on the vm")
private Long diskKbsWrite;

@SerializedName("memorykbs")
@Param(description = "the memory used by the vm")
private Long memoryKBs;

@SerializedName("memoryintfreekbs")
@Param(description = "the internal memory thats free in vm")
private Long memoryIntFreeKBs;

@SerializedName("memorytargetkbs")
@Param(description = "the target memory in vm")
private Long memoryTargetKBs;

@SerializedName("diskioread")
@Param(description = "the read (io) of disk on the vm")
private Long diskIORead;
Expand Down Expand Up @@ -462,6 +474,18 @@ public Long getDiskKbsWrite() {
return diskKbsWrite;
}

public Long getMemoryKBs() {
return memoryKBs;
}

public Long getMemoryIntFreeKBs() {
return memoryIntFreeKBs;
}

public Long getMemoryTargetKBs() {
return memoryTargetKBs;
}

public Long getDiskIORead() {
return diskIORead;
}
Expand Down Expand Up @@ -637,6 +661,18 @@ public void setDiskIORead(Long diskIORead) {
this.diskIORead = diskIORead;
}

public void setMemoryKBs(Long memoryKBs) {
this.memoryKBs = memoryKBs;
}

public void setMemoryIntFreeKBs(Long memoryIntFreeKBs) {
this.memoryIntFreeKBs = memoryIntFreeKBs;
}

public void setMemoryTargetKBs(Long memoryTargetKBs) {
this.memoryTargetKBs = memoryTargetKBs;
}

public void setDiskIOWrite(Long diskIOWrite) {
this.diskIOWrite = diskIOWrite;
}
Expand Down
35 changes: 34 additions & 1 deletion core/src/com/cloud/agent/api/VmStatsEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@ public class VmStatsEntry implements VmStats {
double diskWriteIOs;
double diskReadKBs;
double diskWriteKBs;
double memoryKBs;
double intfreememoryKBs;
double targetmemoryKBs;
int numCPUs;
String entityType;

public VmStatsEntry() {
}

public VmStatsEntry(double cpuUtilization, double networkReadKBs, double networkWriteKBs, int numCPUs, String entityType) {
public VmStatsEntry(double memoryKBs,double intfreememoryKBs,double targetmemoryKBs, double cpuUtilization, double networkReadKBs, double networkWriteKBs, int numCPUs, String entityType) {
this.memoryKBs = memoryKBs;
this.intfreememoryKBs = intfreememoryKBs;
this.targetmemoryKBs = targetmemoryKBs;
this.cpuUtilization = cpuUtilization;
this.networkReadKBs = networkReadKBs;
this.networkWriteKBs = networkWriteKBs;
Expand Down Expand Up @@ -117,6 +123,33 @@ public void setDiskWriteKBs(double diskWriteKBs) {
this.diskWriteKBs = diskWriteKBs;
}

@Override
public double getMemoryKBs() {
return memoryKBs;
}

public void setMemoryKBs(double memoryKBs) {
this.memoryKBs = memoryKBs;
}

@Override
public double getIntFreeMemoryKBs() {
return intfreememoryKBs;
}

public void setIntFreeMemoryKBs(double intfreememoryKBs) {
this.intfreememoryKBs = intfreememoryKBs;
}

@Override
public double getTargetMemoryKBs() {
return targetmemoryKBs;
}

public void setTargetMemoryKBs(double targetmemoryKBs) {
this.targetmemoryKBs = targetmemoryKBs;
}

public int getNumCPUs() {
return numCPUs;
}
Expand Down
38 changes: 28 additions & 10 deletions plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;
import org.libvirt.Connect;
import org.libvirt.Domain;
Expand All @@ -60,6 +62,7 @@
import org.libvirt.DomainInfo.DomainState;
import org.libvirt.DomainInterfaceStats;
import org.libvirt.LibvirtException;
import org.libvirt.MemoryStatistic;
import org.libvirt.NodeInfo;

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

private long _hvVersion;
private long _kernelVersion;
private int _timeout;
private static final int NUMMEMSTATS =2;

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

final String unameKernelVersion = Script.runSimpleBashScript("uname -r");
final String[] kernelVersions = unameKernelVersion.split("[\\.\\-]");
_kernelVersion = Integer.parseInt(kernelVersions[0]) * 1000 * 1000 + (long)Integer.parseInt(kernelVersions[1]) * 1000 + Integer.parseInt(kernelVersions[2]);

/* Disable this, the code using this is pretty bad and non portable
* getOsVersion();
*/
return true;
}

Expand Down Expand Up @@ -3025,12 +3021,19 @@ public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws Li
Domain dm = null;
try {
dm = getDomain(conn, vmName);
final DomainInfo info = dm.getInfo();

if (dm == null) {
return null;
}
DomainInfo info = dm.getInfo();
final VmStatsEntry stats = new VmStatsEntry();

stats.setNumCPUs(info.nrVirtCpu);
stats.setEntityType("vm");

stats.setMemoryKBs(info.maxMem);
stats.setTargetMemoryKBs(info.memory);
stats.setIntFreeMemoryKBs(getMemoryFreeInKBs(dm));

/* get cpu utilization */
VmStats oldStats = null;

Expand Down Expand Up @@ -3124,6 +3127,21 @@ public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws Li
}
}

/**
* This method retrieves the memory statistics from the domain given as parameters.
* If no memory statistic is found, it will return {@link NumberUtils#LONG_ZERO} as the value of free memory in the domain.
* 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)}.
*
* @return the amount of free memory in KBs
*/
protected long getMemoryFreeInKBs(Domain dm) throws LibvirtException {
MemoryStatistic[] mems = dm.memoryStats(NUMMEMSTATS);
if (ArrayUtils.isEmpty(mems)) {
return NumberUtils.LONG_ZERO;
}
return mems[0].getValue();
}

private boolean canBridgeFirewall(final String prvNic) {
final Script cmd = new Script(_securityGroupPath, _timeout, s_logger);
cmd.add("can_bridge_firewall");
Expand Down
Loading

0 comments on commit 8c3722d

Please sign in to comment.