Skip to content

Commit 23e54bb

Browse files
bvbharatAbhinandan Prateek
authored andcommitted
Cloudstack-711: Cpu and Ram Overcommit Ratio.
1 parent ea3db2f commit 23e54bb

33 files changed

Lines changed: 537 additions & 304 deletions

File tree

api/src/com/cloud/agent/api/to/VirtualMachineTO.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public class VirtualMachineTO {
2828
private BootloaderType bootloader;
2929
Type type;
3030
int cpus;
31-
Integer speed;
31+
Integer minSpeed;
32+
Integer maxSpeed;
3233
long minRam;
3334
long maxRam;
3435
String hostName;
@@ -47,12 +48,13 @@ public class VirtualMachineTO {
4748
VolumeTO[] disks;
4849
NicTO[] nics;
4950

50-
public VirtualMachineTO(long id, String instanceName, VirtualMachine.Type type, int cpus, Integer speed, long minRam, long maxRam, BootloaderType bootloader, String os, boolean enableHA, boolean limitCpuUse, String vncPassword) {
51+
public VirtualMachineTO(long id, String instanceName, VirtualMachine.Type type, int cpus, Integer minSpeed, Integer maxSpeed, long minRam, long maxRam, BootloaderType bootloader, String os, boolean enableHA, boolean limitCpuUse, String vncPassword) {
5152
this.id = id;
5253
this.name = instanceName;
5354
this.type = type;
5455
this.cpus = cpus;
55-
this.speed = speed;
56+
this.minSpeed = minSpeed;
57+
this.maxSpeed = maxSpeed;
5658
this.minRam = minRam;
5759
this.maxRam = maxRam;
5860
this.bootloader = bootloader;
@@ -101,10 +103,13 @@ public void setCpus(int cpus) {
101103
this.cpus = cpus;
102104
}
103105

104-
public Integer getSpeed() {
105-
return speed;
106+
public Integer getMinSpeed() {
107+
return minSpeed;
106108
}
107109

110+
public Integer getMaxSpeed() {
111+
return maxSpeed;
112+
}
108113
public boolean getLimitCpuUse() {
109114
return limitCpuUse;
110115
}

api/src/com/cloud/resource/ResourceService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public interface ResourceService {
7171

7272
boolean deleteCluster(DeleteClusterCmd cmd);
7373

74-
Cluster updateCluster(Cluster cluster, String clusterType, String hypervisor, String allocationState, String managedstate);
74+
Cluster updateCluster(Cluster cluster, String clusterType, String hypervisor, String allocationState, String managedstate, Float memoryOvercommitRaito, Float cpuOvercommitRatio);
7575

7676
List<? extends Host> discoverHosts(AddHostCmd cmd) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException;
7777

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,10 @@ public String getName() {
136136
BootloaderType getBootLoaderType();
137137

138138
Map<Param, Object> getParameters();
139+
140+
Float getCpuOvercommitRatio();
141+
142+
Float getMemoryOvercommitRatio();
143+
144+
139145
}

api/src/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class ApiConstants {
4646
public static final String COMPONENT = "component";
4747
public static final String CPU_NUMBER = "cpunumber";
4848
public static final String CPU_SPEED = "cpuspeed";
49+
public static final String CPU_OVERCOMMIT_RATIO="cpuovercommitratio";
4950
public static final String CREATED = "created";
5051
public static final String CUSTOMIZED = "customized";
5152
public static final String DESCRIPTION = "description";
@@ -119,6 +120,7 @@ public class ApiConstants {
119120
public static final String MAX = "max";
120121
public static final String MAX_SNAPS = "maxsnaps";
121122
public static final String MEMORY = "memory";
123+
public static final String MEMORY_OVERCOMMIT_RATIO="memoryovercommitratio";
122124
public static final String MODE = "mode";
123125
public static final String NAME = "name";
124126
public static final String METHOD_NAME = "methodname";

api/src/org/apache/cloudstack/api/command/admin/cluster/AddClusterCmd.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222

23+
import com.cloud.exception.InvalidParameterValueException;
24+
import org.apache.cloudstack.api.*;
25+
import org.apache.log4j.Logger;
26+
2327
import org.apache.cloudstack.api.APICommand;
2428
import org.apache.cloudstack.api.ApiConstants;
2529
import org.apache.cloudstack.api.ApiErrorCode;
@@ -81,6 +85,12 @@ public class AddClusterCmd extends BaseCmd {
8185
@Parameter(name = ApiConstants.VSM_IPADDRESS, type = CommandType.STRING, required = false, description = "the ipaddress of the VSM associated with this cluster")
8286
private String vsmipaddress;
8387

88+
@Parameter (name=ApiConstants.CPU_OVERCOMMIT_RATIO, type = CommandType.STRING, required = false , description = "value of the cpu overcommit ratio, defaults to 1")
89+
private String cpuovercommitRatio;
90+
91+
@Parameter(name = ApiConstants.MEMORY_OVERCOMMIT_RATIO, type = CommandType.STRING, required = false ,description = "value of the default ram overcommit ratio, defaults to 1")
92+
private String memoryovercommitratio;
93+
8494
public String getVSMIpaddress() {
8595
return vsmipaddress;
8696
}
@@ -147,9 +157,26 @@ public void setAllocationState(String allocationState) {
147157
this.allocationState = allocationState;
148158
}
149159

160+
public Float getCpuOvercommitRatio (){
161+
if(cpuovercommitRatio != null){
162+
return Float.parseFloat(cpuovercommitRatio);
163+
}
164+
return 1.0f;
165+
}
166+
167+
public Float getMemoryOvercommitRaito (){
168+
if (memoryovercommitratio != null){
169+
return Float.parseFloat(memoryovercommitratio);
170+
}
171+
return 1.0f;
172+
}
173+
150174
@Override
151175
public void execute(){
152176
try {
177+
if ((getMemoryOvercommitRaito().compareTo(1f) < 0) | (getCpuOvercommitRatio().compareTo(1f) < 0)) {
178+
throw new InvalidParameterValueException("Cpu and ram overcommit ratios should not be less than 1");
179+
}
153180
List<? extends Cluster> result = _resourceService.discoverCluster(this);
154181
ListResponse<ClusterResponse> response = new ListResponse<ClusterResponse>();
155182
List<ClusterResponse> clusterResponses = new ArrayList<ClusterResponse>();

api/src/org/apache/cloudstack/api/command/admin/cluster/UpdateClusterCmd.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public class UpdateClusterCmd extends BaseCmd {
5454
@Parameter(name=ApiConstants.MANAGED_STATE, type=CommandType.STRING, description="whether this cluster is managed by cloudstack")
5555
private String managedState;
5656

57+
@Parameter(name=ApiConstants.CPU_OVERCOMMIT_RATIO, type = CommandType.STRING, description = "Value of cpu overcommit ratio")
58+
private String cpuovercommitratio;
59+
60+
@Parameter(name=ApiConstants.MEMORY_OVERCOMMIT_RATIO, type = CommandType.STRING, description = "Value of ram overcommit ratio")
61+
private String memoryovercommitratio;
62+
63+
5764
public String getClusterName() {
5865
return clusterName;
5966
}
@@ -100,14 +107,32 @@ public void setManagedstate(String managedstate) {
100107
this.managedState = managedstate;
101108
}
102109

110+
public Float getCpuOvercommitRatio (){
111+
if(cpuovercommitratio != null){
112+
return Float.parseFloat(cpuovercommitratio);
113+
}
114+
return 1.0f;
115+
}
116+
117+
public Float getMemoryOvercommitRaito (){
118+
if (memoryovercommitratio != null){
119+
return Float.parseFloat(memoryovercommitratio);
120+
}
121+
return 1.0f;
122+
}
123+
103124
@Override
104125
public void execute(){
105126
Cluster cluster = _resourceService.getCluster(getId());
106127
if (cluster == null) {
107128
throw new InvalidParameterValueException("Unable to find the cluster by id=" + getId());
108129
}
109130

110-
Cluster result = _resourceService.updateCluster(cluster, getClusterType(), getHypervisor(), getAllocationState(), getManagedstate());
131+
if ((getMemoryOvercommitRaito().compareTo(1f) < 0) | (getCpuOvercommitRatio().compareTo(1f) < 0)) {
132+
throw new InvalidParameterValueException("Cpu and ram overcommit ratios should be greater than one");
133+
}
134+
135+
Cluster result = _resourceService.updateCluster(cluster, getClusterType(), getHypervisor(), getAllocationState(), getManagedstate(), getMemoryOvercommitRaito(), getCpuOvercommitRatio());
111136
if (result != null) {
112137
ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster, false);
113138
clusterResponse.setResponseName(getCommandName());

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public class ClusterResponse extends BaseResponse {
6262
@SerializedName("capacity") @Param(description="the capacity of the Cluster", responseObject = CapacityResponse.class)
6363
private List<CapacityResponse> capacitites;
6464

65+
@SerializedName("cpuovercommitratio") @Param(description = "The cpu overcommit ratio of the cluster")
66+
private String cpuovercommitratio;
67+
68+
@SerializedName("memoryovercommitratio") @Param (description = "The ram overcommit ratio of the cluster")
69+
private String memoryovercommitratio;
70+
6571
public String getId() {
6672
return id;
6773
}
@@ -149,4 +155,18 @@ public List<CapacityResponse> getCapacitites() {
149155
public void setCapacitites(ArrayList<CapacityResponse> arrayList) {
150156
this.capacitites = arrayList;
151157
}
158+
public void setCpuovercommitratio(String cpuovercommitratio){
159+
this.cpuovercommitratio= cpuovercommitratio;
160+
}
161+
public void setRamovercommitratio (String memoryOvercommitRatio){
162+
this.memoryovercommitratio= memoryOvercommitRatio;
163+
}
164+
165+
public String getCpuovercommitratio (){
166+
return cpuovercommitratio;
167+
}
168+
169+
public String getRamovercommitratio (){
170+
return memoryovercommitratio;
171+
}
152172
}

plugins/hypervisors/baremetal/src/com/cloud/baremetal/manager/BareMetalPlanner.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@
2323
import javax.inject.Inject;
2424
import javax.naming.ConfigurationException;
2525

26+
import com.cloud.dc.*;
27+
import com.cloud.dc.ClusterDetailsDao;
2628
import org.apache.log4j.Logger;
2729

2830
import com.cloud.capacity.CapacityManager;
2931
import com.cloud.configuration.Config;
3032
import com.cloud.configuration.dao.ConfigurationDao;
31-
import com.cloud.dc.ClusterVO;
32-
import com.cloud.dc.DataCenter;
33-
import com.cloud.dc.Pod;
3433
import com.cloud.dc.dao.ClusterDao;
3534
import com.cloud.dc.dao.DataCenterDao;
3635
import com.cloud.dc.dao.HostPodDao;
@@ -61,16 +60,14 @@ public class BareMetalPlanner extends AdapterBase implements DeploymentPlanner {
6160
@Inject protected ConfigurationDao _configDao;
6261
@Inject protected CapacityManager _capacityMgr;
6362
@Inject protected ResourceManager _resourceMgr;
63+
@Inject protected ClusterDetailsDao _clusterDetailsDao;
6464

6565
@Override
6666
public DeployDestination plan(VirtualMachineProfile<? extends VirtualMachine> vmProfile, DeploymentPlan plan, ExcludeList avoid) throws InsufficientServerCapacityException {
6767
VirtualMachine vm = vmProfile.getVirtualMachine();
68-
ServiceOffering offering = vmProfile.getServiceOffering();
68+
ServiceOffering offering = vmProfile.getServiceOffering();
6969
String hostTag = null;
70-
71-
String opFactor = _configDao.getValue(Config.CPUOverprovisioningFactor.key());
72-
float cpuOverprovisioningFactor = NumbersUtil.parseFloat(opFactor, 1);
73-
70+
7471
String haVmTag = (String)vmProfile.getParameter(VirtualMachineProfile.Param.HaTag);
7572

7673
if (vm.getLastHostId() != null && haVmTag == null) {
@@ -126,7 +123,13 @@ public DeployDestination plan(VirtualMachineProfile<? extends VirtualMachine> vm
126123
return null;
127124
}
128125
for (HostVO h : hosts) {
129-
if (_capacityMgr.checkIfHostHasCapacity(h.getId(), cpu_requested, ram_requested, false, cpuOverprovisioningFactor, true)) {
126+
long cluster_id = h.getClusterId();
127+
ClusterDetailsVO cluster_detail_cpu = _clusterDetailsDao.findDetail(cluster_id,"cpuOvercommitRatio") ;
128+
ClusterDetailsVO cluster_detail_ram = _clusterDetailsDao.findDetail(cluster_id,"memoryOvercommitRatio");
129+
Float cpuOvercommitRatio = Float.parseFloat(cluster_detail_cpu.getValue());
130+
Float memoryOvercommitRatio = Float.parseFloat(cluster_detail_ram.getValue());
131+
132+
if (_capacityMgr.checkIfHostHasCapacity(h.getId(), cpu_requested, ram_requested, false, cpuOvercommitRatio, memoryOvercommitRatio, true)) {
130133
s_logger.debug("Find host " + h.getId() + " has enough capacity");
131134
DataCenter dc = _dcDao.findById(h.getDataCenterId());
132135
Pod pod = _podDao.findById(h.getPodId());

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2921,12 +2921,24 @@ protected LibvirtVMDef createVMFromSpec(VirtualMachineTO vmTO) {
29212921
vm.addComp(guest);
29222922

29232923
GuestResourceDef grd = new GuestResourceDef();
2924-
grd.setMemorySize(vmTO.getMinRam() / 1024);
2924+
//check if overcommit should be considered.
2925+
if(vmTO.getMinSpeed() == vmTO.getMaxSpeed()){
2926+
2927+
2928+
}
2929+
if (vmTO.getMinRam() != vmTO.getMaxRam()){
2930+
grd.setMemBalloning(true);
2931+
grd.setCurrentMem((int)vmTO.getMinRam()/1024);
2932+
grd.setMemorySize((int)vmTO.getMaxRam()/1024);
2933+
}
2934+
else{
2935+
grd.setMemorySize(vmTO.getMaxRam() / 1024);
2936+
}
29252937
grd.setVcpuNum(vmTO.getCpus());
29262938
vm.addComp(grd);
29272939

29282940
CpuTuneDef ctd = new CpuTuneDef();
2929-
ctd.setShares(vmTO.getCpus() * vmTO.getSpeed());
2941+
ctd.setShares(vmTO.getCpus() * vmTO.getMinSpeed());
29302942
vm.addComp(ctd);
29312943

29322944
FeaturesDef features = new FeaturesDef();

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public static class GuestResourceDef {
116116
private int _currentMem = -1;
117117
private String _memBacking;
118118
private int _vcpu = -1;
119+
private boolean _memBalloning= false;
119120

120121
public void setMemorySize(long mem) {
121122
_mem = mem;
@@ -133,6 +134,10 @@ public void setVcpuNum(int vcpu) {
133134
_vcpu = vcpu;
134135
}
135136

137+
public void setMemBalloning(boolean turnon){
138+
_memBalloning = turnon;
139+
}
140+
136141
@Override
137142
public String toString() {
138143
StringBuilder resBuidler = new StringBuilder();
@@ -145,6 +150,9 @@ public String toString() {
145150
resBuidler.append("<memoryBacking>" + "<" + _memBacking + "/>"
146151
+ "</memoryBacking>\n");
147152
}
153+
if (_memBalloning){
154+
resBuidler.append("<devices>\n" + "<memballoon model='virtio'/>\n" + "</devices>\n");
155+
}
148156
if (_vcpu != -1) {
149157
resBuidler.append("<vcpu>" + _vcpu + "</vcpu>\n");
150158
}

0 commit comments

Comments
 (0)