Skip to content

Commit 78fa786

Browse files
author
Maneesha.P
committed
CLOUDSTACK-8800 : Improved the listVirtualMachines API call to include memory utilization information for a VM for xenserver
for hyperv and for vmware
1 parent 8442bd8 commit 78fa786

File tree

10 files changed

+148
-13
lines changed

10 files changed

+148
-13
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 BaseResponse implements ControlledEntityResp
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;
@@ -466,6 +478,18 @@ public Long getDiskKbsWrite() {
466478
return diskKbsWrite;
467479
}
468480

481+
public Long getMemoryKBs() {
482+
return memoryKBs;
483+
}
484+
485+
public Long getMemoryIntFreeKBs() {
486+
return memoryIntFreeKBs;
487+
}
488+
489+
public Long getMemoryTargetKBs() {
490+
return memoryTargetKBs;
491+
}
492+
469493
public Long getDiskIORead() {
470494
return diskIORead;
471495
}
@@ -645,6 +669,18 @@ public void setDiskIORead(Long diskIORead) {
645669
this.diskIORead = diskIORead;
646670
}
647671

672+
public void setMemoryKBs(Long memoryKBs) {
673+
this.memoryKBs = memoryKBs;
674+
}
675+
676+
public void setMemoryIntFreeKBs(Long memoryIntFreeKBs) {
677+
this.memoryIntFreeKBs = memoryIntFreeKBs;
678+
}
679+
680+
public void setMemoryTargetKBs(Long memoryTargetKBs) {
681+
this.memoryTargetKBs = memoryTargetKBs;
682+
}
683+
648684
public void setDiskIOWrite(Long diskIOWrite) {
649685
this.diskIOWrite = diskIOWrite;
650686
}

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.libvirt.Domain;
5858
import org.libvirt.DomainBlockStats;
5959
import org.libvirt.DomainInfo;
60+
import org.libvirt.MemoryStatistic;
6061
import org.libvirt.DomainInfo.DomainState;
6162
import org.libvirt.DomainInterfaceStats;
6263
import org.libvirt.LibvirtException;
@@ -189,6 +190,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
189190
private long _hvVersion;
190191
private long _kernelVersion;
191192
private int _timeout;
193+
private int _nummemstats =2;
192194

193195
private KVMHAMonitor _monitor;
194196
public static final String SSHKEYSPATH = "/root/.ssh";
@@ -2973,6 +2975,9 @@ private class VmStats {
29732975
long _ioWrote;
29742976
long _bytesRead;
29752977
long _bytesWrote;
2978+
long _intmemfree;
2979+
long _memory;
2980+
long _maxmemory;
29762981
Calendar _timestamp;
29772982
}
29782983

@@ -2981,11 +2986,16 @@ public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws Li
29812986
try {
29822987
dm = getDomain(conn, vmName);
29832988
final DomainInfo info = dm.getInfo();
2989+
final MemoryStatistic[] mems = dm.memoryStats(_nummemstats); //number of memory statistics required.
29842990

29852991
final VmStatsEntry stats = new VmStatsEntry();
2992+
29862993
stats.setNumCPUs(info.nrVirtCpu);
29872994
stats.setEntityType("vm");
29882995

2996+
stats.setMemoryKBs(info.maxMem);
2997+
stats.setTargetMemoryKBs(info.memory);
2998+
stats.setIntFreeMemoryKBs((double) mems[0].getValue());
29892999
/* get cpu utilization */
29903000
VmStats oldStats = null;
29913001

@@ -3070,6 +3080,9 @@ public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws Li
30703080
newStat._bytesRead = bytes_rd;
30713081
newStat._bytesWrote = bytes_wr;
30723082
newStat._timestamp = now;
3083+
newStat._intmemfree = mems[0].getValue();
3084+
newStat._memory = info.memory;
3085+
newStat._maxmemory = info.maxMem;
30733086
_vmStats.put(vmName, newStat);
30743087
return stats;
30753088
} finally {

plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
import org.libvirt.LibvirtException;
6767
import org.libvirt.NodeInfo;
6868
import org.libvirt.StorageVol;
69+
import org.libvirt.MemoryStatistic;
70+
6971
import org.mockito.Matchers;
7072
import org.mockito.Mock;
7173
import org.mockito.Mockito;
@@ -409,7 +411,10 @@ public void testGetVmStat() throws LibvirtException {
409411
final Connect connect = Mockito.mock(Connect.class);
410412
final Domain domain = Mockito.mock(Domain.class);
411413
final DomainInfo domainInfo = new DomainInfo();
414+
final MemoryStatistic[] domainMem = new MemoryStatistic[2];
415+
domainMem[0] = Mockito.mock(MemoryStatistic.class);
412416
Mockito.when(domain.getInfo()).thenReturn(domainInfo);
417+
Mockito.when(domain.memoryStats(2)).thenReturn(domainMem);
413418
Mockito.when(connect.domainLookupByName(VMNAME)).thenReturn(domain);
414419
final NodeInfo nodeInfo = new NodeInfo();
415420
nodeInfo.cpus = 8;
@@ -476,6 +481,10 @@ public List<DiskDef> getDisks(final Connect conn, final String vmName) {
476481
// IO traffic as generated by the logic above, must be greater than zero
477482
Assert.assertTrue(vmStat.getDiskReadKBs() > 0);
478483
Assert.assertTrue(vmStat.getDiskWriteKBs() > 0);
484+
// Memory limit of VM must be greater than zero
485+
Assert.assertTrue(vmStat.getIntFreeMemoryKBs() >= 0);
486+
Assert.assertTrue(vmStat.getMemoryKBs() >= 0);
487+
Assert.assertTrue(vmStat.getTargetMemoryKBs() >= vmStat.getMemoryKBs());
479488
}
480489

481490
@Test
@@ -4999,4 +5008,4 @@ public void testUpdateHostPasswordCommandFail() {
49995008

50005009
assertFalse(answer.getResult());
50015010
}
5002-
}
5011+
}

plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockVmManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public Answer getVmStats(final GetVmStatsCommand cmd) {
321321
final HashMap<String, VmStatsEntry> vmStatsNameMap = new HashMap<String, VmStatsEntry>();
322322
final List<String> vmNames = cmd.getVmNames();
323323
for (final String vmName : vmNames) {
324-
final VmStatsEntry entry = new VmStatsEntry(0, 0, 0, 0, "vm");
324+
final VmStatsEntry entry = new VmStatsEntry(0, 0, 0, 0, 0, 0, 0, "vm");
325325
entry.setNetworkReadKBs(32768); // default values 256 KBps
326326
entry.setNetworkWriteKBs(16384);
327327
entry.setCPUUtilization(10);

plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
import org.apache.cloudstack.storage.command.StorageSubSystemCommand;
9898
import org.apache.cloudstack.storage.to.TemplateObjectTO;
9999
import org.apache.cloudstack.storage.to.VolumeObjectTO;
100+
import org.apache.commons.lang.math.NumberUtils;
100101

101102
import com.cloud.agent.IAgentControl;
102103
import com.cloud.agent.api.Answer;
@@ -4662,15 +4663,24 @@ private HashMap<String, VmStatsEntry> getVmStats(List<String> vmNames) throws Ex
46624663
}
46634664
String instanceNameCustomField = "value[" + key + "]";
46644665

4666+
String numCpuStr = "summary.config.numCpu";
4667+
String cpuUseStr = "summary.quickStats.overallCpuUsage";
4668+
String guestMemUseStr = "summary.quickStats.guestMemoryUsage";
4669+
String memLimitStr = "resourceConfig.memoryAllocation.limit";
4670+
String memMbStr = "config.hardware.memoryMB";
4671+
46654672
ObjectContent[] ocs =
4666-
hyperHost.getVmPropertiesOnHyperHost(new String[] {"name", "summary.config.numCpu", "summary.quickStats.overallCpuUsage", instanceNameCustomField});
4673+
hyperHost.getVmPropertiesOnHyperHost(new String[] {"name", numCpuStr, cpuUseStr ,guestMemUseStr ,memLimitStr ,memMbStr, instanceNameCustomField});
46674674
if (ocs != null && ocs.length > 0) {
46684675
for (ObjectContent oc : ocs) {
46694676
List<DynamicProperty> objProps = oc.getPropSet();
46704677
if (objProps != null) {
46714678
String name = null;
46724679
String numberCPUs = null;
46734680
String maxCpuUsage = null;
4681+
String memlimit = null;
4682+
String memkb = null;
4683+
String guestMemusage = null;
46744684
String vmNameOnVcenter = null;
46754685
String vmInternalCSName = null;
46764686
for (DynamicProperty objProp : objProps) {
@@ -4679,10 +4689,16 @@ private HashMap<String, VmStatsEntry> getVmStats(List<String> vmNames) throws Ex
46794689
} else if (objProp.getName().contains(instanceNameCustomField)) {
46804690
if (objProp.getVal() != null)
46814691
vmInternalCSName = ((CustomFieldStringValue)objProp.getVal()).getValue();
4682-
} else if (objProp.getName().equals("summary.config.numCpu")) {
4692+
}else if(objProp.getName().equals(guestMemusage)){
4693+
guestMemusage = objProp.getVal().toString();
4694+
}else if (objProp.getName().equals(numCpuStr)) {
46834695
numberCPUs = objProp.getVal().toString();
4684-
} else if (objProp.getName().equals("summary.quickStats.overallCpuUsage")) {
4696+
} else if (objProp.getName().equals(cpuUseStr)) {
46854697
maxCpuUsage = objProp.getVal().toString();
4698+
} else if (objProp.getName().equals(memLimitStr)) {
4699+
memlimit = objProp.getVal().toString();
4700+
} else if (objProp.getName().equals(memMbStr)) {
4701+
memkb = objProp.getVal().toString();
46864702
}
46874703
}
46884704
new VirtualMachineMO(hyperHost.getContext(), oc.getObj());
@@ -4751,7 +4767,7 @@ private HashMap<String, VmStatsEntry> getVmStats(List<String> vmNames) throws Ex
47514767
}
47524768
}
47534769
}
4754-
vmResponseMap.put(name, new VmStatsEntry(Integer.parseInt(maxCpuUsage), networkReadKBs, networkWriteKBs, Integer.parseInt(numberCPUs), "vm"));
4770+
vmResponseMap.put(name, new VmStatsEntry( NumberUtils.toDouble(memkb)*1024,NumberUtils.toDouble(guestMemusage)*1024,NumberUtils.toDouble(memlimit)*1024, NumberUtils.toDouble(maxCpuUsage), networkReadKBs, networkWriteKBs, NumberUtils.toInt(numberCPUs), "vm"));
47554771
}
47564772
}
47574773
}

plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3294,7 +3294,7 @@ public HashMap<String, VmStatsEntry> getVmStats(final Connection conn, final Get
32943294
final HashMap<String, VmStatsEntry> vmResponseMap = new HashMap<String, VmStatsEntry>();
32953295

32963296
for (final String vmUUID : vmUUIDs) {
3297-
vmResponseMap.put(vmUUID, new VmStatsEntry(0, 0, 0, 0, "vm"));
3297+
vmResponseMap.put(vmUUID, new VmStatsEntry(0,0,0,0, 0, 0, 0, "vm"));
32983298
}
32993299

33003300
final Object[] rrdData = getRRDData(conn, 2); // call rrddata with 2 for
@@ -3348,7 +3348,14 @@ public HashMap<String, VmStatsEntry> getVmStats(final Connection conn, final Get
33483348
vmStatsAnswer.setDiskReadKBs(vmStatsAnswer.getDiskReadKBs() + getDataAverage(dataNode, col, numRows) / 1000);
33493349
} else if (param.matches("vbd_.*_write")) {
33503350
vmStatsAnswer.setDiskWriteKBs(vmStatsAnswer.getDiskWriteKBs() + getDataAverage(dataNode, col, numRows) / 1000);
3351+
} else if (param.contains("memory_internal_free")) {
3352+
vmStatsAnswer.setIntFreeMemoryKBs(vmStatsAnswer.getIntFreeMemoryKBs() + getDataAverage(dataNode, col, numRows) / 1000);
3353+
} else if (param.contains("memory_target")) {
3354+
vmStatsAnswer.setTargetMemoryKBs(vmStatsAnswer.getTargetMemoryKBs() + getDataAverage(dataNode, col, numRows) / 1000);
3355+
} else if (param.contains("memory")) {
3356+
vmStatsAnswer.setMemoryKBs(vmStatsAnswer.getMemoryKBs() + getDataAverage(dataNode, col, numRows) / 1000);
33513357
}
3358+
33523359
}
33533360
}
33543361

@@ -3364,6 +3371,7 @@ public HashMap<String, VmStatsEntry> getVmStats(final Connection conn, final Get
33643371
s_logger.debug("Vm cpu utilization " + vmStatsAnswer.getCPUUtilization());
33653372
}
33663373
}
3374+
33673375
return vmResponseMap;
33683376
}
33693377

@@ -5398,4 +5406,4 @@ public boolean attachConfigDriveToMigratedVm(Connection conn, String vmName, Str
53985406

53995407
}
54005408

5401-
}
5409+
}

server/src/com/cloud/api/query/dao/UserVmJoinDaoImpl.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,21 @@ public UserVmResponse newUserVmResponse(ResponseView view, String objectName, Us
209209

210210
userVmResponse.setNetworkKbsWrite((long)vmStats.getNetworkWriteKBs());
211211

212-
if ((userVm.getHypervisorType() != null) && (userVm.getHypervisorType().equals(HypervisorType.KVM) || userVm.getHypervisorType().equals(HypervisorType.XenServer))) { // support KVM and XenServer only util 2013.06.25
212+
if ((userVm.getHypervisorType() != null) && (userVm.getHypervisorType().equals(HypervisorType.KVM) || userVm.getHypervisorType().equals(HypervisorType.XenServer) || userVm.getHypervisorType().equals(HypervisorType.VMware))) { // support KVM and XenServer only util 2013.06.25 . supporting Vmware from 2015.09.02
213213
userVmResponse.setDiskKbsRead((long)vmStats.getDiskReadKBs());
214214

215-
userVmResponse.setDiskKbsWrite((long)vmStats.getDiskWriteKBs());
215+
userVmResponse.setDiskKbsWrite((long) vmStats.getDiskWriteKBs());
216216

217-
userVmResponse.setDiskIORead((long)vmStats.getDiskReadIOs());
217+
userVmResponse.setDiskIORead((long) vmStats.getDiskReadIOs());
218+
219+
userVmResponse.setDiskIOWrite((long) vmStats.getDiskWriteIOs());
220+
221+
userVmResponse.setMemoryKBs((long) vmStats.getMemoryKBs());
222+
223+
userVmResponse.setMemoryIntFreeKBs((long) vmStats.getIntFreeMemoryKBs());
224+
225+
userVmResponse.setMemoryTargetKBs((long) vmStats.getTargetMemoryKBs());
218226

219-
userVmResponse.setDiskIOWrite((long)vmStats.getDiskWriteIOs());
220227
}
221228
}
222229
}

server/src/com/cloud/server/StatsCollector.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,9 @@ protected void runInContext() {
462462
statsInMemory.setDiskReadIOs(statsInMemory.getDiskReadIOs() + statsForCurrentIteration.getDiskReadIOs());
463463
statsInMemory.setDiskWriteIOs(statsInMemory.getDiskWriteIOs() + statsForCurrentIteration.getDiskWriteIOs());
464464
statsInMemory.setDiskReadKBs(statsInMemory.getDiskReadKBs() + statsForCurrentIteration.getDiskReadKBs());
465+
statsInMemory.setMemoryKBs(statsForCurrentIteration.getMemoryKBs());
466+
statsInMemory.setIntFreeMemoryKBs(statsForCurrentIteration.getIntFreeMemoryKBs());
467+
statsInMemory.setTargetMemoryKBs(statsForCurrentIteration.getTargetMemoryKBs());
465468

466469
_VmStats.put(vmId, statsInMemory);
467470
}
@@ -482,6 +485,10 @@ protected void runInContext() {
482485
metrics.put(externalStatsPrefix + "cloudstack.stats.instances." + vmName + ".disk.read_kbs", statsForCurrentIteration.getDiskReadKBs());
483486
metrics.put(externalStatsPrefix + "cloudstack.stats.instances." + vmName + ".disk.write_iops", statsForCurrentIteration.getDiskWriteIOs());
484487
metrics.put(externalStatsPrefix + "cloudstack.stats.instances." + vmName + ".disk.read_iops", statsForCurrentIteration.getDiskReadIOs());
488+
metrics.put(externalStatsPrefix + "cloudstack.stats.instances." + vmName + ".memory.total_kbs", statsForCurrentIteration.getMemoryKBs());
489+
metrics.put(externalStatsPrefix + "cloudstack.stats.instances." + vmName + ".memory.internalfree_kbs", statsForCurrentIteration.getIntFreeMemoryKBs());
490+
metrics.put(externalStatsPrefix + "cloudstack.stats.instances." + vmName + ".memory.target_kbs", statsForCurrentIteration.getTargetMemoryKBs());
491+
485492
}
486493

487494
}

0 commit comments

Comments
 (0)