package clousimtest;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import org.cloudbus.cloudsim.Cloudlet;
import org.cloudbus.cloudsim.CloudletSchedulerTimeShared;
import org.cloudbus.cloudsim.Datacenter;
import org.cloudbus.cloudsim.DatacenterBroker;
import org.cloudbus.cloudsim.DatacenterCharacteristics;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Pe;
import org.cloudbus.cloudsim.Storage;
import org.cloudbus.cloudsim.UtilizationModel;
import org.cloudbus.cloudsim.UtilizationModelFull;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.VmSchedulerSpaceShared;
import org.cloudbus.cloudsim.VmSchedulerTimeShared;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;
/**
* 两个数据中心,每个中心有一个主机,一共执行两个任务
* @author ytw
*
*/
public class mycloudsim4 {
//虚拟机列表
private static List<Vm> vmlist;
//云任务列表
private static List<Cloudlet> cloudletList;
public static void main(String[] args) {
Log.printLine("Starting MyCloudSim...");
try{
//云用户数量
int num_user = 1;
//日历的字段已使用当前日期和时间初始化。
Calendar calendar = Calendar.getInstance();
//跟踪事件
boolean trace_flag = false;
//初始化CloudSim库。
CloudSim.init(num_user, calendar, trace_flag);
//第二步:创建数据中心
Datacenter datacenter0 = createDatacenter("Datacenter_0");
Datacenter datacenter1 = createDatacenter("Datacenter_1");
//第三步:创建代理
DatacenterBroker broker = createBroker();
int brokerId = broker.getId();
//第四步:创建两个个虚拟机
Vm vm1 = createVm(brokerId,0);
Vm vm2 = createVm(brokerId,1);
//添加到虚拟机列表
vmlist = new ArrayList<Vm>();
vmlist.add(vm1);
vmlist.add(vm2);
//提交虚拟机列表到代理
broker.submitVmList(vmlist);
//第五步:创建两个云任务,将云任务添加到列表中,将云任务列表提交给数据中心代理
Cloudlet cloudlet1 = createCloudlet(brokerId,0);
Cloudlet cloudlet2 = createCloudlet(brokerId,1);
//将云任务添加到列表中
cloudletList = new ArrayList<Cloudlet>();
cloudletList.add(cloudlet1);
cloudletList.add(cloudlet2);
//将云任务列表提交给代理
broker.submitCloudletList(cloudletList);
//将云任务绑定到指定虚拟机上
broker.bindCloudletToVm(cloudlet1.getCloudletId(),vm1.getId());
broker.bindCloudletToVm(cloudlet2.getCloudletId(),vm2.getId());
//第六步:开始模拟
CloudSim.startSimulation();
//CloudSim.stopSimulation();
//最后一步:模拟结束时打印结果
List<Cloudlet> newList = broker.getCloudletReceivedList();
printCloudletList(newList);
Log.printLine("MyCloudSim4 finished!");
}catch(Exception e){
e.printStackTrace();
Log.printLine("The simulation has been terminated due to an unexpected error");
}
}
//创建云任务
private static Cloudlet createCloudlet(int brokerId,int id){
//云任务属性
long length = 400000;
long fileSize = 300;
long outputSize = 300;
int pesNumber = 1;
UtilizationModel utilizationModel = new UtilizationModelFull();
Cloudlet cloudlet =new Cloudlet(id, length, pesNumber, fileSize,
outputSize, utilizationModel, utilizationModel,
utilizationModel);
cloudlet.setUserId(brokerId);
return cloudlet;
}
//创建虚拟机
private static Vm createVm(int brokerId,int vmid){
//Vm 描述
int mips = 500;//速率
long size = 10000; // 虚拟机的存储大小 (MB)
int ram = 512; // 虚拟机内存 (MB)
long bw = 1000;//虚拟机带宽
int pesNumber = 1; // cpu核数
String vmm = "Xen"; // //虚拟机监视器
Vm vm = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());
return vm;
}
//创建代理
private static DatacenterBroker createBroker() {
DatacenterBroker broker = null;
try {
broker = new DatacenterBroker("Broker");
} catch (Exception e) {
e.printStackTrace();
return null;
}
return broker;
}
private static Datacenter createDatacenter2(String name){
List<Host> hostList = new ArrayList<Host>();
List<Pe> peList = new ArrayList<Pe>();
int mips = 2000;
peList.add(new Pe(0, new PeProvisionerSimple(mips)));
int hostId=0;
int ram = 2048; //host memory (MB)
long storage = 1000000; //host storage
int bw = 10000;
hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerSpaceShared(peList)
)
);
String arch = "x86"; // system architecture
String os = "Linux"; // operating system
String vmm = "Xen";
double time_zone = 10.0; // time zone this resource located
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.001; // the cost of using storage in this resource
double costPerBw = 0.0; // 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, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw);
Datacenter datacenter = null;
try {
datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0);
} catch (Exception e) {
e.printStackTrace();
}
return datacenter;
}
//创建数据中心
private static Datacenter createDatacenter(String name) {
//1.定义主机列表
List<Host> hostList = new ArrayList<Host>();
//2.创建主机包含的PE或者CPU处理器列表,定义为MIPS速率
List<Pe> peList = new ArrayList<Pe>();
int mips = 2000;
//3. 创建处理器,并添加到Pe列表中
peList.add(new Pe(0, new PeProvisionerSimple(mips))); // 需要Pe id 和 MIPS 速率
// 4. 创建主机,并将其添加至主机列表
int hostId = 0;
int ram = 2048; // 主机内存 (MB)
long storage = 1000000; // 主机的存储空间
int bw = 10000;//主机带宽
hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),//内存提供者,为虚拟机提供内存
new BwProvisionerSimple(bw),//带宽提供者
storage,
peList,
new VmSchedulerSpaceShared(peList)//空间共享的VM调度
)
);
// 5. 创建存储数据中心属性的DatacenterCharacteristics对象:架构,操作系统,机器列表,
// 分配策略:时间或空间共享,时区及其价格(G $ / Pe时间单位)。
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>(); // 我们现在不添加SAN设备
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, time_zone, cost, costPerMem,
costPerStorage, costPerBw);
// 6. 最后,我们需要创建一个PowerDatacenter对象。
Datacenter datacenter = null;
try {
datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0);
} catch (Exception e) {
e.printStackTrace();
}
return datacenter;
}
private 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.getCloudletStatus() == 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()));
}
}
}
}
cloudsim基于例子自己建立自己云计算仿真平台(仿CloudSimExample4.java)
最新推荐文章于 2023-01-09 22:08:05 发布