基于CloudSimExample2构建:
单主机,两个虚拟机,两个任务,每个任务绑定指定虚拟机且mips速率相同 、虚拟机的mips速率之和小于datacenter的mips,否则创建失败
1、项目构建结构如下:2、构建MyBroker.java
1 package cloudsim; 2 3 import org.cloudbus.cloudsim.DatacenterBroker; 4 import org.cloudbus.cloudsim.lists.CloudletList; 5 6 public class Mybroker extends DatacenterBroker { 7 8 public Mybroker(String name) throws Exception { 9 super(name); 10 } 11 public void bindCloudletToVm(int cloudletId, int vmId) { 12 CloudletList.getById(getCloudletList(), cloudletId).setVmId(vmId); 13 } 14 //适用自己的调度策略将云任务分配到指定虚拟机上 15 public void mybindCloudletToVm(){ 16 for(int i=0;i<getCloudletList().size();i++){ 17 getCloudletList().get(i).setVmId(getCloudletList().size()-1-i); 18 } 19 } 20 }Mybroker.java
3、构建MyCloudSim2.java文件
1 package cloudsim; 2 3 import java.text.DecimalFormat; 4 import java.util.ArrayList; 5 import java.util.Calendar; 6 import java.util.LinkedList; 7 import java.util.List; 8 9 import org.cloudbus.cloudsim.Cloudlet; 10 import org.cloudbus.cloudsim.CloudletSchedulerTimeShared; 11 import org.cloudbus.cloudsim.Datacenter; 12 import org.cloudbus.cloudsim.DatacenterCharacteristics; 13 import org.cloudbus.cloudsim.Host; 14 import org.cloudbus.cloudsim.Log; 15 import org.cloudbus.cloudsim.Pe; 16 import org.cloudbus.cloudsim.Storage; 17 import org.cloudbus.cloudsim.UtilizationModel; 18 import org.cloudbus.cloudsim.UtilizationModelFull; 19 import org.cloudbus.cloudsim.Vm; 20 import org.cloudbus.cloudsim.VmAllocationPolicySimple; 21 import org.cloudbus.cloudsim.VmSchedulerTimeShared; 22 import org.cloudbus.cloudsim.core.CloudSim; 23 import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple; 24 import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple; 25 import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple; 26 27 /** 28 * 单主机,两个虚拟机,两个任务,每个任务绑定指定虚拟机且mips速率相同 29 * 虚拟机的mips速率之和小于datacenter的mips,否则创建失败 30 * @author 31 * 32 */ 33 public class MyCloudSim2 { 34 //虚拟机列表 35 private static List<Vm> vmlist; 36 //云任务列表 37 private static List<Cloudlet> cloudletList; 38 public static void main(String[] args) { 39 Log.printLine("Starting MyCloudSim..."); 40 try{ 41 //云用户数量 42 int num_user = 1; 43 //日历的字段已使用当前日期和时间初始化。 44 Calendar calendar = Calendar.getInstance(); 45 //跟踪事件 46 boolean trace_flag = false; 47 //初始化CloudSim库。 48 CloudSim.init(num_user, calendar, trace_flag); 49 //第二步:创建数据中心 50 Datacenter datacenter0 = createDatacenter("Datacenter_0"); 51 //第三步:创建代理 52 Mybroker broker = createBroker(); 53 int brokerId = broker.getId(); 54 //第四步:创建两个个虚拟机 55 Vm vm1 = createVm(brokerId,0); 56 Vm vm2 = createVm(brokerId,1); 57 //添加到虚拟机列表 58 vmlist = new ArrayList<Vm>(); 59 vmlist.add(vm1); 60 vmlist.add(vm2); 61 //提交虚拟机列表到代理 62 broker.submitVmList(vmlist); 63 //第五步:创建两个云任务,将云任务添加到列表中,将云任务列表提交给数据中心代理 64 Cloudlet cloudlet1 = createCloudlet(brokerId,0); 65 Cloudlet cloudlet2 = createCloudlet(brokerId,1); 66 //将云任务添加到列表中 67 cloudletList = new ArrayList<Cloudlet>(); 68 cloudletList.add(cloudlet1); 69 cloudletList.add(cloudlet2); 70 //将云任务列表提交给代理 71 broker.submitCloudletList(cloudletList); 72 //将云任务绑定到指定虚拟机上 73 //适用自己的调度策略将云任务分配到指定虚拟机上 74 broker.mybindCloudletToVm(); 75 76 //broker.bindCloudletToVm(cloudlet1.getCloudletId(),vm1.getId()); 77 //broker.bindCloudletToVm(cloudlet2.getCloudletId(),vm2.getId()); 78 //第六步:开始模拟 79 CloudSim.startSimulation(); 80 //CloudSim.stopSimulation(); 81 //最后一步:模拟结束时打印结果 82 List<Cloudlet> newList = broker.getCloudletReceivedList(); 83 printCloudletList(newList); 84 Log.printLine("cloudsim.MyCloudSim2 finished!"); 85 86 }catch(Exception e){ 87 e.printStackTrace(); 88 Log.printLine("The simulation has been terminated due to an unexpected error"); 89 } 90 } 91 //创建云任务 92 private static Cloudlet createCloudlet(int brokerId,int id){ 93 //云任务属性 94 long length = 400000; 95 long fileSize = 300; 96 long outputSize = 300; 97 int pesNumber = 1; 98 UtilizationModel utilizationModel = new UtilizationModelFull(); 99 Cloudlet cloudlet =new Cloudlet(id, length, pesNumber, fileSize, 100 outputSize, utilizationModel, utilizationModel, 101 utilizationModel); 102 cloudlet.setUserId(brokerId); 103 return cloudlet; 104 } 105 //创建虚拟机 106 private static Vm createVm(int brokerId,int vmid){ 107 //Vm 描述 108 int mips = 500;//速率 109 long size = 10000; // 虚拟机的存储大小 (MB) 110 int ram = 512; // 虚拟机内存 (MB) 111 long bw = 1000;//虚拟机带宽 112 int pesNumber = 1; // cpu核数 113 String vmm = "Xen"; // //虚拟机监视器 114 115 Vm vm = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared()); 116 117 return vm; 118 } 119 //创建代理 120 private static Mybroker createBroker() { 121 Mybroker broker = null; 122 try { 123 broker = new Mybroker("Broker"); 124 } catch (Exception e) { 125 e.printStackTrace(); 126 return null; 127 } 128 return broker; 129 } 130 131 //创建数据中心 132 private static Datacenter createDatacenter(String name) { 133 134 //1.定义主机列表 135 List<Host> hostList = new ArrayList<Host>(); 136 //2.创建主机包含的PE或者CPU处理器列表,定义为MIPS速率 137 List<Pe> peList = new ArrayList<Pe>(); 138 int mips = 1000; 139 //3. 创建处理器,并添加到Pe列表中 140 peList.add(new Pe(0, new PeProvisionerSimple(mips))); // 需要Pe id 和 MIPS 速率 141 // 4. 创建主机,并将其添加至主机列表 142 int hostId = 0; 143 int ram = 2048; // 主机内存 (MB) 144 long storage = 1000000; // 主机的存储空间 145 int bw = 10000;//主机带宽 146 147 hostList.add( 148 new Host( 149 hostId, 150 new RamProvisionerSimple(ram),//内存提供者,为虚拟机提供内存 151 new BwProvisionerSimple(bw),//带宽提供者 152 storage, 153 peList, 154 new VmSchedulerTimeShared(peList)//时间共享的VM调度 155 ) 156 ); 157 158 // 5. 创建存储数据中心属性的DatacenterCharacteristics对象:架构,操作系统,机器列表, 159 // 分配策略:时间或空间共享,时区及其价格(G $ / Pe时间单位)。 160 String arch = "x86"; // 架构 161 String os = "Linux"; // 操作系统 162 String vmm = "Xen"; //虚拟机监视器 163 double time_zone = 10.0; // 此资源所在的时区 164 double cost = 3.0; // 处理器花费 165 double costPerMem = 0.05; // 内存花费 166 double costPerStorage = 0.001; // 存储花费 167 double costPerBw = 0.0; //带宽花费 168 LinkedList<Storage> storageList = new LinkedList<Storage>(); // 我们现在不添加SAN设备 169 DatacenterCharacteristics characteristics = new DatacenterCharacteristics( 170 arch, os, vmm, hostList, time_zone, cost, costPerMem, 171 costPerStorage, costPerBw); 172 173 // 6. 最后,我们需要创建一个PowerDatacenter对象。 174 Datacenter datacenter = null; 175 try { 176 datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0); 177 } catch (Exception e) { 178 e.printStackTrace(); 179 } 180 181 return datacenter; 182 } 183 private static void printCloudletList(List<Cloudlet> list) { 184 int size = list.size(); 185 Cloudlet cloudlet; 186 187 String indent = " "; 188 Log.printLine(); 189 Log.printLine("========== OUTPUT =========="); 190 Log.printLine("Cloudlet ID" + indent + "STATUS" + indent 191 + "Data center ID" + indent + "VM ID" + indent + "Time" + indent 192 + "Start Time" + indent + "Finish Time"); 193 194 DecimalFormat dft = new DecimalFormat("###.##"); 195 for (int i = 0; i < size; i++) { 196 cloudlet = list.get(i); 197 Log.print(indent + cloudlet.getCloudletId() + indent + indent); 198 199 if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) { 200 Log.print("SUCCESS"); 201 202 Log.printLine(indent + indent + cloudlet.getResourceId() 203 + indent + indent + indent + cloudlet.getVmId() 204 + indent + indent 205 + dft.format(cloudlet.getActualCPUTime()) + indent 206 + indent + dft.format(cloudlet.getExecStartTime()) 207 + indent + indent 208 + dft.format(cloudlet.getFinishTime())); 209 } 210 } 211 } 212 }MyCloudSim2.java
4、运行结果
标签:仿真,indent,cloudsim,虚拟机,CloudSimExample2,new,import,cloudbus From: https://www.cnblogs.com/holly8/p/17453105.html