不懂的地方请自行AI注释,或者评论区@我
import lombok.Data; //lombok依赖
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;
import org.cloudbus.cloudsim.Vm;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;
@Data //这个注解主要就是在编译时自动生成属性的get和set方法
public class Instance {
private Integer vmId=0;
private Integer cloudletId=0;
private List<Host> hostList=new ArrayList<>();
private List<Pe> peList=new ArrayList<>();
private List<Vm> vmlist=new ArrayList<>();
private List<Cloudlet> cloudletList=new ArrayList<>();
private List<DatacenterBroker> brokerList=new ArrayList<>() ;
private List<Datacenter> datacenterList=new ArrayList<>();
public void initHost(Integer hostId)
{
hostList = new ArrayList<Host>();
peList=new ArrayList<>();
int mips=3000;
peList.add(new Pe(0, new PeProvisionerSimple(mips)));
peList.add(new Pe(0, new PeProvisionerSimple(mips)));
int ram = 2048;
long storage = 1000000;
int bw = 4000;
hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerSpaceShared(peList)
)
);
}
public Datacenter initDatacenter(String name,Integer hostId)
{
initHost(hostId);
String arch = "x86";
String os = "Linux";
String vmm = "Xen";
double time_zone = 10.0;
double cost = 3.0;
double costPerMem = 0.05;
double costPerStorage = 0.001;
double costPerBw = 0.0;
LinkedList<Storage> storageList = new LinkedList<Storage>();
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw);
Datacenter datacenter = null;
try {
datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList),
storageList, 0);
datacenterList.add(datacenter);
System.out.println(name+"创建成功");
return datacenter;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("DataCenter创建失败");
}
}
public void initVm(Integer num,Integer brokerIndex)
{
int mips = 1000;
long size = 10000;
int ram = 512;
long bw = 1000;
int pesNumber = 1;
String vmm = "Xen";
try {
for(int i=0;i<num;i++) {
Vm vm = new Vm(vmId, brokerList.get(brokerIndex).getId(), mips, pesNumber, ram, bw, size, vmm,
new CloudletSchedulerTimeShared());
vmId++;
vmlist.add(vm);
}
List<Vm> collect = vmlist.stream().filter((vm) -> vm.getUserId() == brokerList.get(brokerIndex).getId()).collect(Collectors.toList());
brokerList.get(brokerIndex).submitVmList(collect);
}catch (Exception e){
e.printStackTrace();
}
}
public void initCloudlet(Integer start,Integer end,Integer brokerIndex)
{
long length = 400000;
long fileSize = 300;
long outputSize = 300;
UtilizationModel utilizationModel = new UtilizationModelFull();
for(int i=start;i<=end;i++) {
Cloudlet cloudlet = new Cloudlet(cloudletId, length, 1, fileSize, outputSize,
utilizationModel, utilizationModel, utilizationModel);
cloudlet.setUserId(brokerList.get(brokerIndex).getId());
cloudlet.setVmId(vmlist.get(i).getId());
cloudletId++;
cloudletList.add(cloudlet);
}
List<Cloudlet> collect = cloudletList.stream().filter((cloudlet) -> cloudlet.getUserId() == brokerList.get(brokerIndex).getId()).collect(Collectors.toList());
brokerList.get(brokerIndex).submitCloudletList(collect);
}
public static void printCloudletList(List<Cloudlet> list) {
int size = list.size();
Cloudlet cloudlet;
String indent = " ";
Log.printLine();
Log.printLine("========== OUTPUT ==========");
Log.printLine("Cloudlet ID" + indent + "STATUS" + indent
+ "Data center ID" + indent + "VM ID" + indent + "Time" + indent
+ "Start Time" + indent + "Finish Time");
DecimalFormat dft = new DecimalFormat("###.##");
for (int i = 0; i < size; i++) {
cloudlet = list.get(i);
Log.print(indent + cloudlet.getCloudletId() + indent + indent);
if (cloudlet.getStatus() == Cloudlet.SUCCESS) {
Log.print("SUCCESS");
Log.printLine(indent + indent + cloudlet.getResourceId()
+ indent + indent + indent + cloudlet.getVmId()
+ indent + indent
+ dft.format(cloudlet.getActualCPUTime()) + indent
+ indent + dft.format(cloudlet.getExecStartTime())
+ indent + indent
+ dft.format(cloudlet.getFinishTime()));
}
}
public static void main(String[] args) throws CloneNotSupportedException {
int numUser = 7;
Calendar calendar = Calendar.getInstance();
boolean traceFlag = false;
CloudSim.init(numUser, calendar, traceFlag);
Instance instance=new Instance();
try{
Datacenter datacenter = instance.initDatacenter("Datacenter_1", 0);
Datacenter datacenter1 = instance.initDatacenter("Datacenter_2", 0);
Datacenter datacenter2 = instance.initDatacenter("Datacenter_3", 1);
Datacenter datacenter3 = instance.initDatacenter("Datacenter_4", 1);
DatacenterBroker broker1=new DatacenterBroker("1");
DatacenterBroker broker2=new DatacenterBroker("2");
DatacenterBroker broker3=new DatacenterBroker("3");
DatacenterBroker broker4=new DatacenterBroker("4");
instance.getBrokerList().add(broker1);
instance.getBrokerList().add(broker2);
instance.getBrokerList().add(broker3);
instance.getBrokerList().add(broker4);
instance.initVm(2,0);
instance.initVm(2,1);
instance.initVm(2,2);
instance.initVm(2,3);
instance.initCloudlet(0,1,0);
instance.initCloudlet(2,3,1);
instance.initCloudlet(4,5,2);
instance.initCloudlet(6,7,3);
CloudSim.startSimulation();
CloudSim.stopSimulation();
List<Cloudlet> list = instance.getBrokerList().get(0).getCloudletReceivedList();
printCloudletList(list);
List<Cloudlet> list2 = instance.getBrokerList().get(1).getCloudletReceivedList();
printCloudletList(list2);
List<Cloudlet> list3 = instance.getBrokerList().get(2).getCloudletReceivedList();
printCloudletList(list3);
List<Cloudlet> list4 = instance.getBrokerList().get(3).getCloudletReceivedList();
printCloudletList(list4);
List<Cloudlet> total=new ArrayList<Cloudlet>(){{ addAll(list);addAll(list2);addAll(list3);addAll(list4);}};
printCloudletList(total);
}catch (Exception e)
{
e.printStackTrace();
System.out.println("初始化失败");
}
}
}
架构大概是这样的