Skip to content

Commit 5fa300c

Browse files
committed
kvm: Define CPU shares in domain XML
Since libvirt 0.9.0 this can be specified in the XML. This used to be set by invoking virsh after starting the instance, but this is cleaner.
1 parent 64def0e commit 5fa300c

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
import com.cloud.agent.api.to.VolumeTO;
156156
import com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool;
157157
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.ConsoleDef;
158+
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.CpuTuneDef;
158159
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DevicesDef;
159160
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef;
160161
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef.diskProtocol;
@@ -2475,6 +2476,10 @@ protected LibvirtVMDef createVMFromSpec(VirtualMachineTO vmTO) {
24752476
grd.setVcpuNum(vmTO.getCpus());
24762477
vm.addComp(grd);
24772478

2479+
CpuTuneDef ctd = new CpuTuneDef();
2480+
ctd.setShares(vmTO.getCpus() * vmTO.getSpeed());
2481+
vm.addComp(ctd);
2482+
24782483
FeaturesDef features = new FeaturesDef();
24792484
features.addFeatures("pae");
24802485
features.addFeatures("apic");
@@ -2550,9 +2555,6 @@ protected synchronized StartAnswer execute(StartCommand cmd) {
25502555

25512556
s_logger.debug("starting " + vmName + ": " + vm.toString());
25522557
startDomain(conn, vmName, vm.toString());
2553-
Script.runSimpleBashScript("virsh schedinfo " + vmName
2554-
+ " --set cpu_shares=" + vmSpec.getCpus()
2555-
* vmSpec.getSpeed());
25562558

25572559
NicTO[] nics = vmSpec.getNics();
25582560
for (NicTO nic : nics) {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,29 @@ public String toString() {
752752
}
753753
}
754754

755+
public static class CpuTuneDef {
756+
private int _shares = 0;
757+
758+
public void setShares(int shares) {
759+
_shares = shares;
760+
}
761+
762+
public int getShares() {
763+
return _shares;
764+
}
765+
766+
@Override
767+
public String toString() {
768+
StringBuilder cpuTuneBuilder = new StringBuilder();
769+
cpuTuneBuilder.append("<cputune>\n");
770+
if (_shares > 0) {
771+
cpuTuneBuilder.append("<shares>" + _shares + "</shares>\n");
772+
}
773+
cpuTuneBuilder.append("</cputune>\n");
774+
return cpuTuneBuilder.toString();
775+
}
776+
}
777+
755778
public static class SerialDef {
756779
private final String _type;
757780
private final String _source;

0 commit comments

Comments
 (0)