简单例子
步骤
一个简单的仿真案例包括一下基本步骤:
- 初始化CloudSim
- 创建数据中心代理
- 创建数据中心、创建主机列表
- 在主机中放置虚拟机
- 创建云任务Cloudlet
- 提交划分的虚拟机列表给代理
- 提交云任务
- 仿真开始
- 获得仿真结果并输出
实例
public class BasicFirstExample {
private static final int HOSTS = 1;
private static final int HOST_PES = 8;
private static final int VMS = 2;
private static final int VM_PES = 4;
private static final int CLOUDLETS = 4;
private static final int CLOUDLET_PES = 1;
private static final int CLOUDLET_LENGTH = 10000;
private final CloudSim simulation;
private DatacenterBroker broker0;
private List<Vm> vmList;
private List<Cloudlet> cloudletList;
private Datacenter datacenter0;
public static void main(String[] args) {
new BasicFirstExample();
}
private BasicFirstExample() {
Log.setLevel(WARN);
simulation = new CloudSim();
broker0 = new DatacenterBrokerSimple(simulation);
datacenter0 = createDatacenter();
vmList = createVms();
cloudletList = createCloudlets();
broker0.submitVmList(vmList);
broker0.submitCloudletList(cloudletList);
simulation.start();
final List<Cloudlet> finishedCloudlets = broker0.getCloudletFinishedList();
new CloudletsTableBuilder(finishedCloudlets).build();
}
private Datacenter createDatacenter() {
final List<Host> hostList = new ArrayList<>(HOSTS);
for(int i = 0; i < HOSTS; i++) {
Host host = createHost();
hostList.add(host);
}
return new DatacenterSimple(simulation, hostList);
}
private Host createHost() {
final List<Pe> peList = new ArrayList<>(HOST_PES);
for (int i = 0; i < HOST_PES; i++) {
peList.add(new PeSimple(1000));
}
final long ram = 2048;
final long bw = 10000;
final long storage = 1000000;
return new HostSimple(ram, bw, storage, peList);
}
private List<Vm> createVms() {
final List<Vm> list = new ArrayList<>(VMS);
for (int i = 0; i < VMS; i++) {
final Vm vm = new VmSimple(1000, VM_PES);
vm.setRam(256).setBw(1000).setSize(10000);
list.add(vm);
}
return list;
}
private List<Cloudlet> createCloudlets() {
final List<Cloudlet> list = new ArrayList<>(CLOUDLETS);
final UtilizationModelDynamic utilizationModel = new UtilizationModelDynamic(0.5);
for (int i = 0; i < CLOUDLETS; i++) {
final Cloudlet cloudlet = new CloudletSimple(CLOUDLET_LENGTH, CLOUDLET_PES, utilizationModel);
cloudlet.setSizes(1024);
list.add(cloudlet);
}
return list;
}
}
任务调度策略
顺序分配
private void bindCloudletToVmsSimple(){
int vmNumber = vmList.size();
int cloudletNumber = cloudletList.size();
int index = 0;
for (int i = 0; i < cloudletNumber; i++) {
cloudletList.get(i).setVm(vmList.get(index));
index = (index + 1) % vmNumber;
}
}
在将任务提交之前(即submitCloudletList之前),使用自定义的任务分配策略。
cloudletList = createCloudlets();
bindCloudletToVmsSimple();
仿真结果
================== Starting CloudSim Plus 6.1.4 ==================
INFO DatacenterBrokerSimple1 is starting...
INFO 0.00: DatacenterSimple2 is starting...
INFO Entities started.
INFO 0.00: DatacenterBrokerSimple1: List of 1 datacenters(s) received.
INFO 0.00: DatacenterBrokerSimple1: Trying to create Vm 0 in DatacenterSimple2
INFO 0.00: DatacenterBrokerSimple1: Trying to create Vm 1 in DatacenterSimple2
INFO 0.00: DatacenterBrokerSimple1: Trying to create Vm 2 in DatacenterSimple2
INFO 0.00: DatacenterBrokerSimple1: Trying to create Vm 3 in DatacenterSimple2
INFO 0.00: DatacenterBrokerSimple1: Trying to create Vm 4 in DatacenterSimple2
INFO 0.00: DatacenterBrokerSimple1: Trying to create Vm 5 in DatacenterSimple2
INFO 0.00: DatacenterBrokerSimple1: Trying to create Vm 6 in DatacenterSimple2
INFO 0.00: DatacenterBrokerSimple1: Trying to create Vm 7 in DatacenterSimple2
INFO 0.00: VmAllocationPolicySimple: Vm 0 has been allocated to Host 0/DC 2
INFO 0.00: VmAllocationPolicySimple: Vm 1 has been allocated to Host 0/DC 2
INFO 0.00: VmAllocationPolicySimple: Vm 2 has been allocated to Host 0/DC 2
INFO 0.00: VmAllocationPolicySimple: Vm 3 has been allocated to Host 0/DC 2
INFO 0.00: VmAllocationPolicySimple: Vm 4 has been allocated to Host 0/DC 2
INFO 0.00: VmAllocationPolicySimple: Vm 5 has been allocated to Host 0/DC 2
INFO 0.00: VmAllocationPolicySimple: Vm 6 has been allocated to Host 0/DC 2
INFO 0.00: VmAllocationPolicySimple: Vm 7 has been allocated to Host 0/DC 2
INFO 0.10: DatacenterBrokerSimple1: Sending Cloudlet 0 to Vm 0 in Host 0/DC 2.
INFO 0.10: DatacenterBrokerSimple1: Sending Cloudlet 1 to Vm 1 in Host 0/DC 2.
INFO 0.10: DatacenterBrokerSimple1: Sending Cloudlet 2 to Vm 2 in Host 0/DC 2.
INFO 0.10: DatacenterBrokerSimple1: Sending Cloudlet 3 to Vm 3 in Host 0/DC 2.
INFO 0.10: DatacenterBrokerSimple1: Sending Cloudlet 4 to Vm 4 in Host 0/DC 2.
INFO 0.10: DatacenterBrokerSimple1: Sending Cloudlet 5 to Vm 5 in Host 0/DC 2.
INFO 0.10: DatacenterBrokerSimple1: Sending Cloudlet 6 to Vm 6 in Host 0/DC 2.
INFO 0.10: DatacenterBrokerSimple1: Sending Cloudlet 7 to Vm 7 in Host 0/DC 2.
INFO 0.10: DatacenterBrokerSimple1: Sending Cloudlet 8 to Vm 0 in Host 0/DC 2.
INFO 0.10: DatacenterBrokerSimple1: All waiting Cloudlets submitted to some VM.
INFO 20.11: DatacenterBrokerSimple1: Cloudlet 1 finished in Vm 1 and returned to broker.
INFO 20.11: DatacenterBrokerSimple1: Cloudlet 2 finished in Vm 2 and returned to broker.
INFO 20.11: DatacenterBrokerSimple1: Cloudlet 3 finished in Vm 3 and returned to broker.
INFO 20.11: DatacenterBrokerSimple1: Cloudlet 4 finished in Vm 4 and returned to broker.
INFO 20.11: DatacenterBrokerSimple1: Cloudlet 5 finished in Vm 5 and returned to broker.
INFO 20.11: DatacenterBrokerSimple1: Cloudlet 6 finished in Vm 6 and returned to broker.
INFO 20.11: DatacenterBrokerSimple1: Cloudlet 7 finished in Vm 7 and returned to broker.
INFO 40.21: DatacenterBrokerSimple1: Cloudlet 0 finished in Vm 0 and returned to broker.
INFO 40.21: DatacenterBrokerSimple1: Cloudlet 8 finished in Vm 0 and returned to broker.
INFO 40.32: Processing last events before simulation shutdown.
INFO 40.32: DatacenterBrokerSimple1 is shutting down...
INFO 40.32: DatacenterBrokerSimple1: Requesting Vm 7 destruction.
INFO 40.32: DatacenterBrokerSimple1: Requesting Vm 6 destruction.
INFO 40.32: DatacenterBrokerSimple1: Requesting Vm 5 destruction.
INFO 40.32: DatacenterBrokerSimple1: Requesting Vm 4 destruction.
INFO 40.32: DatacenterBrokerSimple1: Requesting Vm 3 destruction.
INFO 40.32: DatacenterBrokerSimple1: Requesting Vm 2 destruction.
INFO 40.32: DatacenterBrokerSimple1: Requesting Vm 1 destruction.
INFO 40.32: DatacenterBrokerSimple1: Requesting Vm 0 destruction.
INFO 40.32: DatacenterSimple: Vm 7 destroyed on Host 0/DC 2.
INFO 40.32: DatacenterSimple: Vm 6 destroyed on Host 0/DC 2.
INFO 40.32: DatacenterSimple: Vm 5 destroyed on Host 0/DC 2.
INFO 40.32: DatacenterSimple: Vm 4 destroyed on Host 0/DC 2.
INFO 40.32: DatacenterSimple: Vm 3 destroyed on Host 0/DC 2.
INFO 40.32: DatacenterSimple: Vm 2 destroyed on Host 0/DC 2.
INFO 40.32: DatacenterSimple: Vm 1 destroyed on Host 0/DC 2.
INFO 40.32: DatacenterSimple: Vm 0 destroyed on Host 0/DC 2.
INFO Simulation: No more future events
INFO CloudInformationService0: Notify all CloudSim Plus entities to shutdown.
INFO
================== Simulation finished at time 40.32 ==================
DEBUG DeferredQueue >> max size: 8 added to middle: 0 added to tail: 64
SIMULATION RESULTS
Cloudlet|Status |DC|Host|Host PEs |VM|VM PEs |CloudletLen|CloudletPEs|StartTime|FinishTime|ExecTime
ID| |ID| ID|CPU cores|ID|CPU cores| MI| CPU cores| Seconds| Seconds| Seconds
-----------------------------------------------------------------------------------------------------
1|SUCCESS| 2| 0| 8| 1| 1| 10000| 1| 0| 20| 20
2|SUCCESS| 2| 0| 8| 2| 1| 10000| 1| 0| 20| 20
3|SUCCESS| 2| 0| 8| 3| 1| 10000| 1| 0| 20| 20
4|SUCCESS| 2| 0| 8| 4| 1| 10000| 1| 0| 20| 20
5|SUCCESS| 2| 0| 8| 5| 1| 10000| 1| 0| 20| 20
6|SUCCESS| 2| 0| 8| 6| 1| 10000| 1| 0| 20| 20
7|SUCCESS| 2| 0| 8| 7| 1| 10000| 1| 0| 20| 20
0|SUCCESS| 2| 0| 8| 0| 1| 10000| 1| 0| 40| 40
8|SUCCESS| 2| 0| 8| 0| 1| 10000| 1| 0| 40| 40
-----------------------------------------------------------------------------------------------------
结果分析
? 可以发现,确实顺序提交给虚拟机列表(当然,默认的也是顺序分配)。
-
为什么Cloudlet ID = 0和Cloudlet ID = 8执行时间为40秒呢?而其他的都是20秒? 因为Cloudlet ID = 0和Cloudlet ID = 8都是在VM ID = 0上执行的,即VM ID = 0执行了两个任务,其他的VM均只执行了一个任务,时间上当然翻倍。 -
Cloudlet 的长度为:CloudletLen = 10000(million instructions,MI); VM的执行速度为:VM mipsCapacity = 1000 ;详见代码(final Vm vm = new VmSimple(1000, VM_PES); 为什么单个任务执行时间为20秒,而不是10000/1000=10秒? 因为定义Cloudlets的利用率模型始终仅使用任何资源的50%。 (final UtilizationModelDynamic utilizationModel = new UtilizationModelDynamic(0.5);)
虚拟机分配策略
随机分配
public class RandomVmAllocationPolicyExample {
private static final int HOSTS = 10;
private static final int HOST_PES = 8;
private static final int VMS = 4;
private static final int VM_PES = 4;
private static final int CLOUDLETS = 4;
private static final int CLOUDLET_PES = 2;
private static final int CLOUDLET_LENGTH = 10000;
private final CloudSim simulation;
private DatacenterBroker broker0;
private List<Vm> vmList;
private List<Cloudlet> cloudletList;
private Datacenter datacenter0;
private final ContinuousDistribution random;
public static void main(String[] args) {
new RandomVmAllocationPolicyExample();
}
private RandomVmAllocationPolicyExample() {
final double startSecs = TimeUtil.currentTimeSecs();
Log.setLevel(ch.qos.logback.classic.Level.WARN);
simulation = new CloudSim();
random = new UniformDistr();
datacenter0 = createDatacenter();
broker0 = new DatacenterBrokerSimple(simulation);
vmList = createVms();
cloudletList = createCloudlets();
broker0.submitVmList(vmList);
broker0.submitCloudletList(cloudletList);
simulation.start();
final List<Cloudlet> finishedCloudlets = broker0.getCloudletFinishedList();
finishedCloudlets.sort(Comparator.comparingLong(cloudlet -> cloudlet.getVm().getId()));
new CloudletsTableBuilder(finishedCloudlets).build();
System.out.println("Execution time: " + TimeUtil.secondsToStr(TimeUtil.elapsedSeconds(startSecs)));
}
private Datacenter createDatacenter() {
final List<Host> hostList = new ArrayList<>(HOSTS);
for(int i = 0; i < HOSTS; i++) {
Host host = createHost();
hostList.add(host);
}
VmAllocationPolicySimple vmAllocationPolicy = new VmAllocationPolicySimple();
vmAllocationPolicy.setFindHostForVmFunction(this::findRandomSuitableHostForVm);
return new DatacenterSimple(simulation, hostList, vmAllocationPolicy);
}
private Optional<Host> findRandomSuitableHostForVm(final VmAllocationPolicy vmAllocationPolicy, final Vm vm) {
final List<Host> hostList = vmAllocationPolicy.getHostList();
for (int i = 0; i < hostList.size(); i++){
final int randomIndex = (int)(random.sample() * hostList.size());
final Host host = hostList.get(randomIndex);
if(host.isSuitableForVm(vm)){
return Optional.of(host);
}
}
return Optional.empty();
}
private Host createHost() {
List<Pe> peList = new ArrayList<>(HOST_PES);
for (int i = 0; i < HOST_PES; i++) {
peList.add(new PeSimple(1000, new PeProvisionerSimple()));
}
final long ram = 2048;
final long bw = 10000;
final long storage = 1000000;
Host host = new HostSimple(ram, bw, storage, peList);
host.setRamProvisioner(new ResourceProvisionerSimple())
.setBwProvisioner(new ResourceProvisionerSimple())
.setVmScheduler(new VmSchedulerTimeShared());
return host;
}
private List<Vm> createVms() {
final List<Vm> list = new ArrayList<>(VMS);
for (int i = 0; i < VMS; i++) {
Vm vm = new VmSimple(i, 1000, VM_PES)
.setRam(512).setBw(1000).setSize(10000)
.setCloudletScheduler(new CloudletSchedulerTimeShared());
list.add(vm);
}
return list;
}
private List<Cloudlet> createCloudlets() {
final List<Cloudlet> list = new ArrayList<>(CLOUDLETS);
UtilizationModel utilization = new UtilizationModelDynamic(0.2);
for (int i = 0; i < CLOUDLETS; i++) {
Cloudlet cloudlet =
new CloudletSimple(i, CLOUDLET_LENGTH, CLOUDLET_PES)
.setFileSize(1024)
.setOutputSize(1024)
.setUtilizationModelBw(utilization)
.setUtilizationModelRam(utilization)
.setUtilizationModelCpu(new UtilizationModelFull());
list.add(cloudlet);
}
return list;
}
}
仿真结果
SIMULATION RESULTS
Cloudlet|Status |DC|Host|Host PEs |VM|VM PEs |CloudletLen|CloudletPEs|StartTime|FinishTime|ExecTime
ID| |ID| ID|CPU cores|ID|CPU cores| MI| CPU cores| Seconds| Seconds| Seconds
-----------------------------------------------------------------------------------------------------
0|SUCCESS| 1| 8| 8| 0| 4| 10000| 2| 0| 10| 10
1|SUCCESS| 1| 8| 8| 1| 4| 10000| 2| 0| 10| 10
2|SUCCESS| 1| 6| 8| 2| 4| 10000| 2| 0| 10| 10
3|SUCCESS| 1| 3| 8| 3| 4| 10000| 2| 0| 10| 10
-----------------------------------------------------------------------------------------------------
Execution time: 0.45 second
首次适应
public class FirstFitVmAllocationPolicyExample {
private static final int HOSTS = 10;
private static final int HOST_PES = 8;
private static final int VMS = 4;
private static final int VM_PES = 4;
private static final int CLOUDLETS = 8;
private static final int CLOUDLET_PES = 2;
private static final int CLOUDLET_LENGTH = 10000;
private final CloudSim simulation;
private DatacenterBroker broker0;
private List<Vm> vmList;
private List<Cloudlet> cloudletList;
private Datacenter datacenter0;
public static void main(String[] args) {
new FirstFitVmAllocationPolicyExample();
}
private FirstFitVmAllocationPolicyExample() {
final double startSecs = TimeUtil.currentTimeSecs();
Log.setLevel(WARN);
simulation = new CloudSim();
datacenter0 = createDatacenter();
broker0 = new DatacenterBrokerSimple(simulation);
vmList = createVms();
cloudletList = createCloudlets();
broker0.submitVmList(vmList);
broker0.submitCloudletList(cloudletList);
simulation.start();
final List<Cloudlet> finishedCloudlets = broker0.getCloudletFinishedList();
new CloudletsTableBuilder(finishedCloudlets).build();
System.out.println("Execution time: " + TimeUtil.secondsToStr(TimeUtil.elapsedSeconds(startSecs)));
}
private Datacenter createDatacenter() {
final List<Host> hostList = new ArrayList<>(HOSTS);
for(int i = 0; i < HOSTS; i++) {
Host host = createHost();
hostList.add(host);
}
return new DatacenterSimple(simulation, hostList, new VmAllocationPolicyFirstFit());
}
private Host createHost() {
List<Pe> peList = new ArrayList<>(HOST_PES);
for (int i = 0; i < HOST_PES; i++) {
peList.add(new PeSimple(1000, new PeProvisionerSimple()));
}
final long ram = 2048;
final long bw = 10000;
final long storage = 1000000;
Host host = new HostSimple(ram, bw, storage, peList);
host.setRamProvisioner(new ResourceProvisionerSimple())
.setBwProvisioner(new ResourceProvisionerSimple())
.setVmScheduler(new VmSchedulerTimeShared());
return host;
}
private List<Vm> createVms() {
final List<Vm> list = new ArrayList<>(VMS);
for (int i = 0; i < VMS; i++) {
Vm vm = new VmSimple(i, 1000, VM_PES)
.setRam(512).setBw(1000).setSize(10000)
.setCloudletScheduler(new CloudletSchedulerTimeShared());
list.add(vm);
}
return list;
}
private List<Cloudlet> createCloudlets() {
final List<Cloudlet> list = new ArrayList<>(CLOUDLETS);
UtilizationModel utilization = new UtilizationModelDynamic(0.2);
for (int i = 0; i < CLOUDLETS; i++) {
Cloudlet cloudlet =
new CloudletSimple(i, CLOUDLET_LENGTH, CLOUDLET_PES)
.setFileSize(1024)
.setOutputSize(1024)
.setUtilizationModelBw(utilization)
.setUtilizationModelRam(utilization)
.setUtilizationModelCpu(new UtilizationModelFull());
list.add(cloudlet);
}
return list;
}
}
仿真结果
SIMULATION RESULTS
Cloudlet|Status |DC|Host|Host PEs |VM|VM PEs |CloudletLen|CloudletPEs|StartTime|FinishTime|ExecTime
ID| |ID| ID|CPU cores|ID|CPU cores| MI| CPU cores| Seconds| Seconds| Seconds
-----------------------------------------------------------------------------------------------------
0|SUCCESS| 1| 0| 8| 0| 4| 10000| 2| 0| 10| 10
1|SUCCESS| 1| 0| 8| 1| 4| 10000| 2| 0| 10| 10
2|SUCCESS| 1| 1| 8| 2| 4| 10000| 2| 0| 10| 10
3|SUCCESS| 1| 1| 8| 3| 4| 10000| 2| 0| 10| 10
-----------------------------------------------------------------------------------------------------
Execution time: 0.41 second
|