1、基于CloudSimExample4构建,两个数据中心,每个中心有一个主机,一共执行两个任务
2、工程构建如下:
3、MyCloudSim4.java源码如下:
1 package org.cloudbus.cloudsim.examples; 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.DatacenterBroker; 13 import org.cloudbus.cloudsim.DatacenterCharacteristics; 14 import org.cloudbus.cloudsim.Host; 15 import org.cloudbus.cloudsim.Log; 16 import org.cloudbus.cloudsim.Pe; 17 import org.cloudbus.cloudsim.Storage; 18 import org.cloudbus.cloudsim.UtilizationModel; 19 import org.cloudbus.cloudsim.UtilizationModelFull; 20 import org.cloudbus.cloudsim.Vm; 21 import org.cloudbus.cloudsim.VmAllocationPolicySimple; 22 import org.cloudbus.cloudsim.VmSchedulerSpaceShared; 23 import org.cloudbus.cloudsim.VmSchedulerTimeShared; 24 import org.cloudbus.cloudsim.core.CloudSim; 25 import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple; 26 import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple; 27 import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple; 28 29 /* 30 * 两个数据中心,每个中心有一个主机,一共执行两个任务 31 */ 32 public class MyCloudSim4 { 33 //虚拟机列表 34 private static List<Vm> vmlist; 35 //云任务列表 36 private static List<Cloudlet> cloudletList; 37 public static void main(String[] args) { 38 Log.printLine("Starting MyCloudSim..."); 39 try{ 40 //云用户数量 41 int num_user = 1; 42 //日历的字段已使用当前日期和时间初始化。 43 Calendar calendar = Calendar.getInstance(); 44 //跟踪事件 45 boolean trace_flag = false; 46 //初始化CloudSim库。 47 CloudSim.init(num_user, calendar, trace_flag); 48 //第二步:创建数据中心 49 Datacenter datacenter0 = createDatacenter("Datacenter_0"); 50 Datacenter datacenter1 = createDatacenter("Datacenter_1"); 51 //第三步:创建代理 52 DatacenterBroker 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 broker.bindCloudletToVm(cloudlet1.getCloudletId(),vm1.getId()); 74 broker.bindCloudletToVm(cloudlet2.getCloudletId(),vm2.getId()); 75 //第六步:开始模拟 76 CloudSim.startSimulation(); 77 //CloudSim.stopSimulation(); 78 //最后一步:模拟结束时打印结果 79 List<Cloudlet> newList = broker.getCloudletReceivedList(); 80 printCloudletList(newList); 81 Log.printLine("MyCloudSim4 finished!"); 82 83 }catch(Exception e){ 84 e.printStackTrace(); 85 Log.printLine("The simulation has been terminated due to an unexpected error"); 86 } 87 } 88 //创建云任务 89 private static Cloudlet createCloudlet(int brokerId,int id){ 90 //云任务属性 91 long length = 400000; 92 long fileSize = 300; 93 long outputSize = 300; 94 int pesNumber = 1; 95 UtilizationModel utilizationModel = new UtilizationModelFull(); 96 Cloudlet cloudlet =new Cloudlet(id, length, pesNumber, fileSize, 97 outputSize, utilizationModel, utilizationModel, 98 utilizationModel); 99 cloudlet.setUserId(brokerId); 100 return cloudlet; 101 } 102 //创建虚拟机 103 private static Vm createVm(int brokerId,int vmid){ 104 //Vm 描述 105 int mips = 500;//速率 106 long size = 10000; // 虚拟机的存储大小 (MB) 107 int ram = 512; // 虚拟机内存 (MB) 108 long bw = 1000;//虚拟机带宽 109 int pesNumber = 1; // cpu核数 110 String vmm = "Xen"; // //虚拟机监视器 111 112 Vm vm = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared()); 113 114 return vm; 115 } 116 //创建代理 117 private static DatacenterBroker createBroker() { 118 DatacenterBroker broker = null; 119 try { 120 broker = new DatacenterBroker("Broker"); 121 } catch (Exception e) { 122 e.printStackTrace(); 123 return null; 124 } 125 return broker; 126 } 127 private static Datacenter createDatacenter2(String name){ 128 List<Host> hostList = new ArrayList<Host>(); 129 List<Pe> peList = new ArrayList<Pe>(); 130 int mips = 2000; 131 peList.add(new Pe(0, new PeProvisionerSimple(mips))); 132 int hostId=0; 133 int ram = 2048; //host memory (MB) 134 long storage = 1000000; //host storage 135 int bw = 10000; 136 hostList.add( 137 new Host( 138 hostId, 139 new RamProvisionerSimple(ram), 140 new BwProvisionerSimple(bw), 141 storage, 142 peList, 143 new VmSchedulerSpaceShared(peList) 144 ) 145 ); 146 String arch = "x86"; // system architecture 147 String os = "Linux"; // operating system 148 String vmm = "Xen"; 149 double time_zone = 10.0; // time zone this resource located 150 double cost = 3.0; // the cost of using processing in this resource 151 double costPerMem = 0.05; // the cost of using memory in this resource 152 double costPerStorage = 0.001; // the cost of using storage in this resource 153 double costPerBw = 0.0; // the cost of using bw in this resource 154 LinkedList<Storage> storageList = new LinkedList<Storage>(); //we are not adding SAN devices by now 155 156 DatacenterCharacteristics characteristics = new DatacenterCharacteristics( 157 arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw); 158 159 160 161 Datacenter datacenter = null; 162 try { 163 datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0); 164 } catch (Exception e) { 165 e.printStackTrace(); 166 } 167 168 return datacenter; 169 } 170 171 //创建数据中心 172 private static Datacenter createDatacenter(String name) { 173 174 //1.定义主机列表 175 List<Host> hostList = new ArrayList<Host>(); 176 //2.创建主机包含的PE或者CPU处理器列表,定义为MIPS速率 177 List<Pe> peList = new ArrayList<Pe>(); 178 int mips = 2000; 179 //3. 创建处理器,并添加到Pe列表中 180 peList.add(new Pe(0, new PeProvisionerSimple(mips))); // 需要Pe id 和 MIPS 速率 181 // 4. 创建主机,并将其添加至主机列表 182 int hostId = 0; 183 int ram = 2048; // 主机内存 (MB) 184 long storage = 1000000; // 主机的存储空间 185 int bw = 10000;//主机带宽 186 hostList.add( 187 new Host( 188 hostId, 189 new RamProvisionerSimple(ram),//内存提供者,为虚拟机提供内存 190 new BwProvisionerSimple(bw),//带宽提供者 191 storage, 192 peList, 193 new VmSchedulerSpaceShared(peList)//空间共享的VM调度 194 ) 195 ); 196 197 // 5. 创建存储数据中心属性的DatacenterCharacteristics对象:架构,操作系统,机器列表, 198 // 分配策略:时间或空间共享,时区及其价格(G $ / Pe时间单位)。 199 String arch = "x86"; // 架构 200 String os = "Linux"; // 操作系统 201 String vmm = "Xen"; //虚拟机监视器 202 double time_zone = 10.0; // 此资源所在的时区 203 double cost = 3.0; // 处理器花费 204 double costPerMem = 0.05; // 内存花费 205 double costPerStorage = 0.001; // 存储花费 206 double costPerBw = 0.0; //带宽花费 207 LinkedList<Storage> storageList = new LinkedList<Storage>(); // 我们现在不添加SAN设备 208 DatacenterCharacteristics characteristics = new DatacenterCharacteristics( 209 arch, os, vmm, hostList, time_zone, cost, costPerMem, 210 costPerStorage, costPerBw); 211 212 // 6. 最后,我们需要创建一个PowerDatacenter对象。 213 Datacenter datacenter = null; 214 try { 215 datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0); 216 } catch (Exception e) { 217 e.printStackTrace(); 218 } 219 220 return datacenter; 221 } 222 private static void printCloudletList(List<Cloudlet> list) { 223 int size = list.size(); 224 Cloudlet cloudlet; 225 226 String indent = " "; 227 Log.printLine(); 228 Log.printLine("========== OUTPUT =========="); 229 Log.printLine("Cloudlet ID" + indent + "STATUS" + indent 230 + "Data center ID" + indent + "VM ID" + indent + "Time" + indent 231 + "Start Time" + indent + "Finish Time"); 232 233 DecimalFormat dft = new DecimalFormat("###.##"); 234 for (int i = 0; i < size; i++) { 235 cloudlet = list.get(i); 236 Log.print(indent + cloudlet.getCloudletId() + indent + indent); 237 238 if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) { 239 Log.print("SUCCESS"); 240 241 Log.printLine(indent + indent + cloudlet.getResourceId() 242 + indent + indent + indent + cloudlet.getVmId() 243 + indent + indent 244 + dft.format(cloudlet.getActualCPUTime()) + indent 245 + indent + dft.format(cloudlet.getExecStartTime()) 246 + indent + indent 247 + dft.format(cloudlet.getFinishTime())); 248 } 249 } 250 } 251 }MyCloudSim4.java
4、执行效果如下:
标签:仿真,indent,org,cloudsim,CloudSimExample4,new,import,cloudbus From: https://www.cnblogs.com/holly8/p/17453077.html