用cloudsim实现如下仿真:2个用户,2个数据中心,每个数据中心有7个host,执行7个云任务。
时间: 2023-11-22 13:56:52 浏览: 81
cloudsim仿真
好的,让我们来一步步实现这个仿真:
1. 下载和安装CloudSim
首先,你需要从CloudSim的官方网站下载和安装CloudSim。安装完毕后,你可以使用Eclipse或者NetBeans等集成开发环境来编写和运行Java代码。
2. 创建Hosts和Datacenters
首先,我们需要创建两个数据中心,每个数据中心都包含7个主机。在CloudSim中,我们可以使用`DatacenterCharacteristics`类来设置数据中心的特征(如处理能力、存储容量等)。在本例中,我们假设每个主机的处理能力和存储容量都相同。以下是相应的Java代码:
```java
int numHosts = 7;
// Create DatacenterCharacteristics
String arch = "x86"; // CPU architecture
String os = "Linux"; // Operating System
String vmm = "Xen"; // Virtual Machine Monitor
double time_zone = 10.0; // Time zone of the resource
double cost = 3.0; // The cost of using processing in this resource
double costPerMem = 0.05; // The cost of using memory in this resource
double costPerStorage = 0.1; // The cost of using storage in this resource
double costPerBw = 0.1; // The cost of using bw in this resource
LinkedList<Storage> storageList = new LinkedList<Storage>(); // We are not adding SAN devices by now
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(arch, os, vmm, storageList, time_zone, cost, costPerMem, costPerStorage, costPerBw);
// Create Hosts
for (int i = 0; i < numHosts; i++) {
int hostId = i;
long storage = 1000000; // host storage
int bw = 10000;
// Create PEs
ArrayList<Pe> peList = new ArrayList<Pe>();
int mips = 1000;
for (int j = 0; j < 2; j++) {
peList.add(new Pe(j, new PeProvisionerSimple(mips))); // create and add PE to the list
}
// Create Host
Host host = new Host(
hostId,
new RamProvisionerSimple(512), // RAM capacity of the host
new BwProvisionerSimple(bw), // bandwidth capacity of the host
storage,
peList,
new VmSchedulerSpaceShared(peList)
);
hostList.add(host);
}
// Create Datacenters
for (int i = 0; i < numDatacenters; i++) {
String name = "Datacenter_" + i;
Datacenter datacenter = null;
try {
datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0);
} catch (Exception e) {
e.printStackTrace();
}
datacenterList.add(datacenter);
}
```
3. 创建VM和Cloudlets
我们需要创建7个Cloudlet和7个VM,然后将它们分配给两个用户。以下是相应的Java代码:
```java
int numCloudlets = 7;
int numVms = 7;
// Create VMs
for (int i = 0; i < numVms; i++) {
int vmId = i;
int mips = 250;
long size = 10000;
int ram = 512;
long bw = 1000;
int pesNumber = 2;
String vmm = "Xen";
// Create VM
Vm vm = new Vm(vmId, userIdList.get(i % 2), mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerSpaceShared());
// Add VM to the list
vmList.add(vm);
}
// Create Cloudlets
for (int i = 0; i < numCloudlets; i++) {
int cloudletId = i;
int pesNumber = 2;
long length = 40000;
long fileSize = 300;
long outputSize = 300;
UtilizationModel utilizationModel = new UtilizationModelFull();
// Create Cloudlet
Cloudlet cloudlet = new Cloudlet(cloudletId, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);
// Add Cloudlet to the list
cloudletList.add(cloudlet);
}
// Assign Cloudlets to VMs
for (int i = 0; i < numCloudlets; i++) {
Cloudlet cloudlet = cloudletList.get(i);
Vm vm = vmList.get(i % numVms);
cloudlet.setUserId(vm.getUserId());
cloudlet.setVmId(vm.getId());
// Add Cloudlet to VM's list
vm.getCloudletList().add(cloudlet);
}
```
4. 运行仿真
最后,我们需要将所有的主机、数据中心、VM和Cloudlet都添加到仿真中,并开始仿真。以下是相应的Java代码:
```java
// Create Simulation
CloudSim simulation = new CloudSim();
// Add Hosts to Datacenters
for (int i = 0; i < hostList.size(); i++) {
datacenterList.get(i % numDatacenters).getHostList().add(hostList.get(i));
}
// Add Datacenters to Simulation
for (Datacenter datacenter : datacenterList) {
simulation.addEntity(datacenter);
}
// Add VMs to Simulation
for (Vm vm : vmList) {
simulation.addEntity(vm);
}
// Add Cloudlets to Simulation
for (Cloudlet cloudlet : cloudletList) {
simulation.addEntity(cloudlet);
}
// Start Simulation
simulation.start();
```
这样,我们就完成了这个仿真的实现。你可以根据自己的需求调整参数和代码。
阅读全文