Skip to content

Commit c12ccbd

Browse files
author
Murali Reddy
committed
Bug 9534 : implement CPU cap
Introducing new boolean flag in service offering to restrict the user VM's CPU utilization to what service offering it is entitled for.
1 parent 7960a49 commit c12ccbd

14 files changed

Lines changed: 92 additions & 17 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ public class VirtualMachineTO {
3939
String[] bootupScripts;
4040
boolean rebootOnCrash;
4141
boolean enableHA;
42+
boolean limitCpuUse;
4243
String vncPassword;
4344
Map<String, String> params;
4445

4546
VolumeTO[] disks;
4647
NicTO[] nics;
4748

48-
public VirtualMachineTO(long id, String instanceName, VirtualMachine.Type type, int cpus, Integer speed, long minRam, long maxRam, BootloaderType bootloader, String os, boolean enableHA, String vncPassword) {
49+
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) {
4950
this.id = id;
5051
this.name = instanceName;
5152
this.type = type;
@@ -56,6 +57,7 @@ public VirtualMachineTO(long id, String instanceName, VirtualMachine.Type type,
5657
this.bootloader = bootloader;
5758
this.os = os;
5859
this.enableHA = enableHA;
60+
this.limitCpuUse = limitCpuUse;
5961
this.vncPassword = vncPassword;
6062
}
6163

@@ -102,6 +104,10 @@ public Integer getSpeed() {
102104
return speed;
103105
}
104106

107+
public boolean getLimitCpuUse() {
108+
return limitCpuUse;
109+
}
110+
105111
public long getMinRam() {
106112
return minRam;
107113
}

api/src/com/cloud/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public class ApiConstants {
9898
public static final String JOB_STATUS = "jobstatus";
9999
public static final String LASTNAME = "lastname";
100100
public static final String LEVEL = "level";
101+
public static final String LIMIT_CPU_USE = "limitcpuuse";
101102
public static final String LOCK = "lock";
102103
public static final String LUN = "lun";
103104
public static final String MAX = "max";

api/src/com/cloud/api/commands/CreateServiceOfferingCmd.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public class CreateServiceOfferingCmd extends BaseCmd {
5656
@Parameter(name=ApiConstants.OFFER_HA, type=CommandType.BOOLEAN, description="the HA for the service offering")
5757
private Boolean offerHa;
5858

59+
@Parameter(name=ApiConstants.LIMIT_CPU_USE, type=CommandType.BOOLEAN, description="restrict the CPU usage to committed service offering")
60+
private Boolean limitCpuUse;
61+
5962
@Parameter(name=ApiConstants.STORAGE_TYPE, type=CommandType.STRING, description="the storage type of the service offering. Values are local and shared.")
6063
private String storageType;
6164

@@ -97,6 +100,10 @@ public Boolean getOfferHa() {
97100
return offerHa;
98101
}
99102

103+
public Boolean GetLimitCpuUse() {
104+
return limitCpuUse;
105+
}
106+
100107
public String getStorageType() {
101108
return storageType;
102109
}

api/src/com/cloud/offering/ServiceOffering.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public interface ServiceOffering {
5656
* @return Does this service plan offer HA?
5757
*/
5858
boolean getOfferHA();
59+
60+
/**
61+
* @return Does this service plan offer VM to use CPU resources beyond the service offering limits?
62+
*/
63+
boolean getLimitCpuUse();
5964

6065
/**
6166
* @return the rate in megabits per sec to which a VM's network interface is throttled to

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,11 @@ public static boolean isSystemVM(VirtualMachine.Type vmtype) {
236236
* @return should HA be enabled for this machine?
237237
*/
238238
public boolean isHaEnabled();
239-
239+
240+
/**
241+
* @return should limit CPU usage to the service offering?
242+
*/
243+
public boolean limitCpuUse();
240244
/**
241245
* @return date when machine was created
242246
*/

core/src/com/cloud/vm/UserVmVO.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ public UserVmVO(long id,
8080
HypervisorType hypervisorType,
8181
long guestOsId,
8282
boolean haEnabled,
83+
boolean limitCpuUse,
8384
long domainId,
8485
long accountId,
8586
long serviceOfferingId,
8687
String userData,
8788
String name) {
88-
super(id, serviceOfferingId, name, instanceName, Type.User, templateId, hypervisorType, guestOsId, domainId, accountId, haEnabled);
89+
super(id, serviceOfferingId, name, instanceName, Type.User, templateId, hypervisorType, guestOsId, domainId, accountId, haEnabled, limitCpuUse);
8990
this.userData = userData;
9091
this.displayName = displayName != null ? displayName : null;
9192
this.details = new HashMap<String, String>();

core/src/com/cloud/vm/VMInstanceVO.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ public class VMInstanceVO implements VirtualMachine, FiniteStateObject<State, Vi
107107

108108
@Column(name="ha_enabled", updatable=true, nullable=true)
109109
protected boolean haEnabled;
110-
110+
111+
@Column(name="limit_cpu_use", updatable=true, nullable=true)
112+
private boolean limitCpuUse;
113+
111114
@Column(name="update_count", updatable = true, nullable=false)
112115
protected long updated; // This field should be updated everytime the state is updated. There's no set method in the vo object because it is done with in the dao code.
113116

@@ -163,8 +166,25 @@ public VMInstanceVO(long id,
163166
this.domainId = domainId;
164167
this.serviceOfferingId = serviceOfferingId;
165168
this.hypervisorType = hypervisorType;
169+
this.limitCpuUse = false;
166170
}
167-
171+
172+
public VMInstanceVO(long id,
173+
long serviceOfferingId,
174+
String name,
175+
String instanceName,
176+
Type type,
177+
Long vmTemplateId,
178+
HypervisorType hypervisorType,
179+
long guestOSId,
180+
long domainId,
181+
long accountId,
182+
boolean haEnabled,
183+
boolean limitResourceUse) {
184+
this(id, serviceOfferingId, name, instanceName, type, vmTemplateId, hypervisorType, guestOSId, domainId, accountId, haEnabled);
185+
this.limitCpuUse = limitResourceUse;
186+
}
187+
168188
protected VMInstanceVO() {
169189
}
170190

@@ -330,8 +350,13 @@ public void setHostId(Long hostId) {
330350
@Override
331351
public boolean isHaEnabled() {
332352
return haEnabled;
333-
}
353+
}
334354

355+
@Override
356+
public boolean limitCpuUse() {
357+
return limitCpuUse;
358+
}
359+
335360
@Override
336361
public String getPrivateMacAddress() {
337362
return privateMacAddress;

server/src/com/cloud/configuration/ConfigurationManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public interface ConfigurationManager extends ConfigurationService, Manager {
7070
* @param hostTag
7171
* @return ID
7272
*/
73-
ServiceOfferingVO createServiceOffering(long userId, String name, int cpu, int ramSize, int speed, String displayText, boolean localStorageRequired, boolean offerHA, String tags, Long domainId, String hostTag);
73+
ServiceOfferingVO createServiceOffering(long userId, String name, int cpu, int ramSize, int speed, String displayText, boolean localStorageRequired, boolean offerHA, boolean limitResourceUse, String tags, Long domainId, String hostTag);
7474

7575
/**
7676
* Creates a new disk offering

server/src/com/cloud/configuration/ConfigurationManagerImpl.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,16 +1448,21 @@ public ServiceOffering createServiceOffering(CreateServiceOfferingCmd cmd) {
14481448
offerHA = false;
14491449
}
14501450

1451+
Boolean limitCpuUse = cmd.GetLimitCpuUse();
1452+
if (limitCpuUse == null) {
1453+
limitCpuUse = false;
1454+
}
1455+
14511456
return createServiceOffering(userId, cmd.getServiceOfferingName(), cpuNumber.intValue(), memory.intValue(), cpuSpeed.intValue(), cmd.getDisplayText(), localStorageRequired, offerHA,
1452-
cmd.getTags(), cmd.getDomainId(), cmd.getHostTag());
1457+
limitCpuUse, cmd.getTags(), cmd.getDomainId(), cmd.getHostTag());
14531458
}
14541459

14551460
@Override
14561461
@ActionEvent(eventType = EventTypes.EVENT_SERVICE_OFFERING_CREATE, eventDescription = "creating service offering")
1457-
public ServiceOfferingVO createServiceOffering(long userId, String name, int cpu, int ramSize, int speed, String displayText, boolean localStorageRequired, boolean offerHA, String tags,
1462+
public ServiceOfferingVO createServiceOffering(long userId, String name, int cpu, int ramSize, int speed, String displayText, boolean localStorageRequired, boolean offerHA, boolean limitResourceUse, String tags,
14581463
Long domainId, String hostTag) {
14591464
tags = cleanupTags(tags);
1460-
ServiceOfferingVO offering = new ServiceOfferingVO(name, cpu, ramSize, speed, null, null, offerHA, displayText, localStorageRequired, false, tags, false, domainId, hostTag);
1465+
ServiceOfferingVO offering = new ServiceOfferingVO(name, cpu, ramSize, speed, null, null, offerHA, limitResourceUse, displayText, localStorageRequired, false, tags, false, domainId, hostTag);
14611466

14621467
if ((offering = _serviceOfferingDao.persist(offering)) != null) {
14631468
UserContext.current().setEventDetails("Service offering id=" + offering.getId());

server/src/com/cloud/hypervisor/HypervisorGuruBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected <T extends VirtualMachine> VirtualMachineTO toVirtualMachineTO(Virtual
6060
VirtualMachine vm = vmProfile.getVirtualMachine();
6161

6262
VirtualMachineTO to = new VirtualMachineTO(vm.getId(), vm.getInstanceName(), vm.getType(), offering.getCpu(), offering.getSpeed(),
63-
offering.getRamSize() * 1024l * 1024l, offering.getRamSize() * 1024l * 1024l, null, null, vm.isHaEnabled(), vm.getVncPassword());
63+
offering.getRamSize() * 1024l * 1024l, offering.getRamSize() * 1024l * 1024l, null, null, vm.isHaEnabled(), vm.limitCpuUse(), vm.getVncPassword());
6464
to.setBootArgs(vmProfile.getBootArgs());
6565

6666
List<NicProfile> nicProfiles = vmProfile.getNics();

0 commit comments

Comments
 (0)