《cloudsim 学习笔记 实例1》由会员分享,可在线阅读,更多相关《cloudsim 学习笔记 实例1(7页珍藏版)》请在金锄头文库上搜索。
1、package org.cloudbus.cloudsim.examples;/* Title: CloudSim Toolkit* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation* of Clouds* Licence: GPL - http:/www.gnu.org/copyleft/gpl.html* Copyright (c) 2009, The University of Melbourne, Australia*/ 【试验一:怎样创建一个含一台主机的数据中心,并在其上运行一个云
2、任务怎样创建一个含一台主机的数据中心,并在其上运行一个云任务】import java.text.DecimalFormat; 【处理文本、日期、数字和消息的类和接口】 【十进制格式】import java.util.ArrayList; 【Java 的实用工具类库 java.util 包】 import java.util.Calendar; import java.util.LinkedList; import java.util.List;import org.cloudbus.cloudsim.Cloudlet; import org.cloudbus.cloudsim.Cloudlet
3、SchedulerTimeShared; 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.Stor
4、age; import org.cloudbus.cloudsim.UtilizationModel; import org.cloudbus.cloudsim.UtilizationModelFull; import org.cloudbus.cloudsim.Vm; import org.cloudbus.cloudsim.VmAllocationPolicySimple; import org.cloudbus.cloudsim.VmSchedulerTimeShared; import org.cloudbus.cloudsim.core.CloudSim; import org.cl
5、oudbus.cloudsim.provisioners.BwProvisionerSimple; import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple; import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;/* A simple example showing how to create a datacenter with one host and run one* cloudlet on it.*/ public class CloudSimExa
6、mple1 /* The cloudlet list. */ 【云任务列表】private static List cloudletList;/* The vmlist. */ 【虚拟机列表】 private static List vmlist;/* Creates main() to run this example.* param args the args*/ 【主函数运行实例】 public static void main(String args) Log.printLine(“Starting CloudSimExample1.“); 【实验结果输出】try / First st
7、ep: Initialize the CloudSim package. It should be called 【初始化工具包】/ before creating any entities. int num_user = 1; / number of cloud users Calendar calendar = Calendar.getInstance(); boolean trace_flag = false; / mean trace events/ Initialize the CloudSim library CloudSim.init(num_user, calendar, tr
8、ace_flag);/ Second step: Create Datacenters 【创建数据中心】 / Datacenters are the resource providers in CloudSim. We need at / list one of them to run a CloudSim simulation Datacenter datacenter0 = createDatacenter(“Datacenter_0“);/ Third step: Create Broker 【创建代理】 DatacenterBroker broker = createBroker();
9、 int brokerId = broker.getId();/ Fourth step: Create one virtual machine 【创建一个虚拟机】 vmlist = new ArrayList();/ VM description 【虚拟机参数】 int vmid = 0; int mips = 1000; long size = 10000; / image size (MB) int ram = 512; / vm memory (MB) long bw = 1000; int pesNumber = 1; / number of cpusString vmm = “Xe
10、n“; / VMM name/ create VM 【虚拟机创建】 Vm vm = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared();/ add the VM to the vmList 【加入虚拟机列表】 vmlist.add(vm);/ submit vm list to the broker 【虚拟机列表提交给代理】 broker.submitVmList(vmlist);/ Fifth step: Create one Cloudlet 【创建一个云
11、任务】 cloudletList = new ArrayList();/ Cloudlet properties 【云任务参数】 int id = 0; long length = 400000; long fileSize = 300; long outputSize = 300; UtilizationModel utilizationModel = new UtilizationModelFull();【getUtilization()函数 :输入具体时间参数 返回类型是计算资源的利用百分比】 Cloudlet cloudlet = new Cloudlet(id, length, pe
12、sNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel); cloudlet.setUserId(brokerId); cloudlet.setVmId(vmid);/ add the cloudlet to the list 【云任务加入列表】 cloudletList.add(cloudlet);/ submit cloudlet list to the broker 【云任务列表提交给代理】 broker.submitCloudletList(cloudletList);/ S
13、ixth step: Starts the simulation 【开始仿真】 CloudSim.startSimulation();CloudSim.stopSimulation();/Final step: Print results when simulation is over 【仿真结束 打印结果】 List newList = broker.getCloudletReceivedList(); printCloudletList(newList);/ Print the debt of each user to each datacenter 【打印账单】 datacenter0.
14、printDebts();Log.printLine(“CloudSimExample1 finished!“); catch (Exception e) 【Java Exception 异常处理机制】 e.printStackTrace(); Log.printLine(“Unwanted errors happen“); /* Creates the datacenter.* param name the name* return the datacenter*/ 【创建数据中心】 private static Datacenter createDatacenter(String name
15、) / Here are the steps needed to create a PowerDatacenter: / 1. We need to create a list to store / our machine 【创建列表储存机器】 List hostList = new ArrayList();/ 2. A Machine contains one or more PEs or CPUs/Cores. 【一个机器含有的处理器】 / In this example, it will have only one core. List peList = new ArrayList();
16、int mips = 1000;/ 3. Create PEs and add these into a list. 【创建处理器 并添加到 Pe 列表】 peList.add(new Pe(0, new PeProvisionerSimple(mips); / need to store Pe id and MIPS Rating 【Pe 编号 和 处理速度】/ 4. Create Host with its id and list of PEs and add them to the list/ of machines 【创建主机】 int hostId = 0; int ram = 2048; / host memory (MB) long storage = 1000000; / host storage int bw = 10000;hostL