CloudSimExample2展示如何创建一个只含一个主机的数据中心,并在其上运行两个云任务。(两个云任务运行在具有相同计算能力的虚拟机上,即两个云任务的执行需要相同的时间)

首先附上CloudSimExample1全部代码:

/** 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*/package org.cloudbus.cloudsim.examples;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.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;/*** A simple example showing how to create* a datacenter with one host and run two* cloudlets on it. The cloudlets run in* VMs with the same MIPS requirements.* The cloudlets will take the same time to* complete the execution.*/
public class CloudSimExample2 {/** The cloudlet list. */private static List<Cloudlet> cloudletList;/** The vmlist. */private static List<Vm> vmlist;/*** Creates main() to run this example*/public static void main(String[] args) {Log.printLine("Starting CloudSimExample2...");try {// First step: Initialize the CloudSim package. It should be called// before creating any entities.int num_user = 1;   // number of cloud usersCalendar calendar = Calendar.getInstance();boolean trace_flag = false;  // mean trace events// Initialize the CloudSim libraryCloudSim.init(num_user, calendar, trace_flag);// Second step: Create Datacenters//Datacenters are the resource providers in CloudSim. We need at list one of them to run a CloudSim simulation@SuppressWarnings("unused")Datacenter datacenter0 = createDatacenter("Datacenter_0");//Third step: Create BrokerDatacenterBroker broker = createBroker();int brokerId = broker.getId();//Fourth step: Create one virtual machinevmlist = new ArrayList<Vm>();//VM descriptionint vmid = 0;int mips = 250;long size = 10000; //image size (MB)int ram = 512; //vm memory (MB)long bw = 1000;int pesNumber = 1; //number of cpusString vmm = "Xen"; //VMM name//create two VMsVm vm1 = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());vmid++;Vm vm2 = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());//add the VMs to the vmListvmlist.add(vm1);vmlist.add(vm2);//submit vm list to the brokerbroker.submitVmList(vmlist);//Fifth step: Create two CloudletscloudletList = new ArrayList<Cloudlet>();//Cloudlet propertiesint id = 0;pesNumber=1;long length = 250000;long fileSize = 300;long outputSize = 300;UtilizationModel utilizationModel = new UtilizationModelFull();Cloudlet cloudlet1 = new Cloudlet(id, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);cloudlet1.setUserId(brokerId);id++;Cloudlet cloudlet2 = new Cloudlet(id, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);cloudlet2.setUserId(brokerId);//add the cloudlets to the listcloudletList.add(cloudlet1);cloudletList.add(cloudlet2);//submit cloudlet list to the brokerbroker.submitCloudletList(cloudletList);//bind the cloudlets to the vms. This way, the broker// will submit the bound cloudlets only to the specific VMbroker.bindCloudletToVm(cloudlet1.getCloudletId(),vm1.getId());broker.bindCloudletToVm(cloudlet2.getCloudletId(),vm2.getId());// Sixth step: Starts the simulationCloudSim.startSimulation();// Final step: Print results when simulation is overList<Cloudlet> newList = broker.getCloudletReceivedList();CloudSim.stopSimulation();printCloudletList(newList);Log.printLine("CloudSimExample2 finished!");}catch (Exception e) {e.printStackTrace();Log.printLine("The simulation has been terminated due to an unexpected error");}}private static Datacenter createDatacenter(String name){// Here are the steps needed to create a PowerDatacenter:// 1. We need to create a list to store//    our machineList<Host> hostList = new ArrayList<Host>();// 2. A Machine contains one or more PEs or CPUs/Cores.// In this example, it will have only one core.List<Pe> peList = new ArrayList<Pe>();int mips = 1000;// 3. Create PEs and add these into a list.peList.add(new Pe(0, new PeProvisionerSimple(mips))); // need to store Pe id and MIPS Rating//4. Create Host with its id and list of PEs and add them to the list of machinesint hostId=0;int ram = 2048; //host memory (MB)long storage = 1000000; //host storageint bw = 10000;hostList.add(new Host(hostId,new RamProvisionerSimple(ram),new BwProvisionerSimple(bw),storage,peList,new VmSchedulerTimeShared(peList))); // This is our machine// 5. Create a DatacenterCharacteristics object that stores the//    properties of a data center: architecture, OS, list of//    Machines, allocation policy: time- or space-shared, time zone//    and its price (G$/Pe time unit).String arch = "x86";      // system architectureString os = "Linux";          // operating systemString vmm = "Xen";double time_zone = 10.0;         // time zone this resource locateddouble cost = 3.0;              // the cost of using processing in this resourcedouble costPerMem = 0.05;      // the cost of using memory in this resourcedouble costPerStorage = 0.001; // the cost of using storage in this resourcedouble costPerBw = 0.0;           // the cost of using bw in this resourceLinkedList<Storage> storageList = new LinkedList<Storage>();   //we are not adding SAN devices by nowDatacenterCharacteristics characteristics = new DatacenterCharacteristics(arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw);// 6. Finally, we need to create a PowerDatacenter object.Datacenter datacenter = null;try {datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0);} catch (Exception e) {e.printStackTrace();}return datacenter;}//We strongly encourage users to develop their own broker policies, to submit vms and cloudlets according//to the specific rules of the simulated scenarioprivate static DatacenterBroker createBroker(){DatacenterBroker broker = null;try {broker = new DatacenterBroker("Broker");} catch (Exception e) {e.printStackTrace();return null;}return broker;}/*** Prints the Cloudlet objects* @param list  list of Cloudlets*/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()));}}}
}

接下来进行详细的分析:(与CloudSimExample1相同之处不再赘述)

第一步:初始化cloudsim包(在创建数据中心的实例前必须进行初始化cloudsim包),直接调用CloudSim.init()函数,是个静态方法,有三个参数。

// First step: Initialize the CloudSim package. It should be called// before creating any entities.int num_user = 1;   // number of cloud users//指定云用户数量为1个。Calendar calendar = Calendar.getInstance();boolean trace_flag = false;  // mean trace events// Initialize the CloudSim libraryCloudSim.init(num_user, calendar, trace_flag);//初始化cloudsim包。//指定云用户数量为1个。Calendar calendar = Calendar.getInstance();boolean trace_flag = false;  // mean trace events// Initialize the CloudSim libraryCloudSim.init(num_user, calendar, trace_flag);//初始化cloudsim包。

第二步:创建数据中心,此处只需要创建,不需要调用,后续继续研究。

// Second step: Create Datacenters//Datacenters are the resource providers in CloudSim. We need at list one of them to run a CloudSim simulation@SuppressWarnings("unused")Datacenter datacenter0 = createDatacenter("Datacenter_0");

第三步:创建代理,一个代理负责代表一个用户,用来提交虚拟机列表和云任务列表。

//Third step: Create BrokerDatacenterBroker broker = createBroker();int brokerId = broker.getId();

第四步:创建虚拟机VM,指定计算能力(用mips表示),内存,带宽,CPU数等,并添加到虚拟机列表,让代理提交。

//Fourth step: Create one virtual machinevmlist = new ArrayList<Vm>();//VM descriptionint vmid = 0;int mips = 250;long size = 10000; //image size (MB)int ram = 512; //vm memory (MB)long bw = 1000;int pesNumber = 1; //number of cpusString vmm = "Xen"; //VMM name//create two VMsVm vm1 = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());vmid++;Vm vm2 = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());//add the VMs to the vmListvmlist.add(vm1);vmlist.add(vm2);//submit vm list to the brokerbroker.submitVmList(vmlist);

第五步:创建云任务,指定云任务的参数(云任务ID,长度,文件大小,输出大小,使用模式),其中length指的是MIPS数(指令数)。将云任务添加到云任务列表,并提交给代理。

//Fifth step: Create two CloudletscloudletList = new ArrayList<Cloudlet>();//Cloudlet propertiesint id = 0;pesNumber=1;long length = 250000;long fileSize = 300;long outputSize = 300;UtilizationModel utilizationModel = new UtilizationModelFull();Cloudlet cloudlet1 = new Cloudlet(id, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);cloudlet1.setUserId(brokerId);id++;Cloudlet cloudlet2 = new Cloudlet(id, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);cloudlet2.setUserId(brokerId);//add the cloudlets to the listcloudletList.add(cloudlet1);cloudletList.add(cloudlet2);//submit cloudlet list to the brokerbroker.submitCloudletList(cloudletList);//bind the cloudlets to the vms. This way, the broker// will submit the bound cloudlets only to the specific VMbroker.bindCloudletToVm(cloudlet1.getCloudletId(),vm1.getId());broker.bindCloudletToVm(cloudlet2.getCloudletId(),vm2.getId());

第六步:开始仿真,直到仿真结束。

                        // Sixth step: Starts the simulationCloudSim.startSimulation();

第七步:当仿真结束后,打印仿真结果。(结果包括云任务队列和每个用户使用数据中心的情况)

// Final step: Print results when simulation is overList<Cloudlet> newList = broker.getCloudletReceivedList();CloudSim.stopSimulation();printCloudletList(newList);Log.printLine("CloudSimExample2 finished!");

接下来是创建数据中心的步骤:

第一步:创建主机列表,来存储主机。

// 1. We need to create a list to store//    our machineList<Host> hostList = new ArrayList<Host>();

第二步:创建Pe(CPU)列表,来存储Pe(CPU)。一个主机有一个(单核)或多个CPU(多核),一个主机含有一个Pe列表,Pe需要指定计算能力,用MIPS表示。

 // 2. A Machine contains one or more PEs or CPUs/Cores.// In this example, it will have only one core.List<Pe> peList = new ArrayList<Pe>();int mips = 1000;

第三步:创建Pe(指定peid,mips),并将其添加到Pe列表。

// 3. Create PEs and add these into a list.peList.add(new Pe(0, new PeProvisionerSimple(mips))); // need to store Pe id and MIPS Rating

第四步:用Pe列表和id创建主机(指定主机id,内存,存储容量,带宽,Pe(CPU)列表等),并将其添加到主机列表。其中VmSchedulerTimeShared表示虚拟机使用时分方式共享主机CPU。

//4. Create Host with its id and list of PEs and add them to the list of machinesint hostId=0;int ram = 2048; //host memory (MB)long storage = 1000000; //host storageint bw = 10000;hostList.add(new Host(hostId,new RamProvisionerSimple(ram),new BwProvisionerSimple(bw),storage,peList,new VmSchedulerTimeShared(peList))); // This is our machine

第五步:创建数据中心特征对象来存储数据中心的参数特征(结构,操作系统,机器列表,分配策略(时间或空间共享)),跟主机架构相关。

// 5. Create a DatacenterCharacteristics object that stores the//    properties of a data center: architecture, OS, list of//    Machines, allocation policy: time- or space-shared, time zone//    and its price (G$/Pe time unit).String arch = "x86";      // system architectureString os = "Linux";          // operating systemString vmm = "Xen";double time_zone = 10.0;         // time zone this resource locateddouble cost = 3.0;              // the cost of using processing in this resourcedouble costPerMem = 0.05;     // the cost of using memory in this resourcedouble costPerStorage = 0.001; // the cost of using storage in this resourcedouble costPerBw = 0.0;           // the cost of using bw in this resourceLinkedList<Storage> storageList = new LinkedList<Storage>();   //we are not adding SAN devices by nowDatacenterCharacteristics characteristics = new DatacenterCharacteristics(arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw);

第六步:创建数据中心对象。其中VmAllocationSimple表示将VM分配到已经使用Pe最少的物理机中。

// 6. Finally, we need to create a PowerDatacenter object.Datacenter datacenter = null;try {datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0);} catch (Exception e) {e.printStackTrace();}return datacenter;}

cloudsim4.0中CloudSimExample2分析相关推荐

  1. cloudsim4.0中CloudSimExample1分析

    CloudSimExample1展示如何创建一个只包含一个主机的数据中心,并且在其上运行一个云任务. 首先附上CloudSimExample1全部代码: package org.cloudbus.cl ...

  2. Hhadoop-2.7.0中HDFS写文件源码分析(二):客户端实现(1)

    一.综述 HDFS写文件是整个Hadoop中最为复杂的流程之一,它涉及到HDFS中NameNode.DataNode.DFSClient等众多角色的分工与合作. 首先上一段代码,客户端是如何写文件的: ...

  3. .net2.0中SqlBulkCopy批量复制数据出错原因分析!

    在项目后台数据库选择SqlServer,进行批量复制数据时,.net2.0中提供的SqlBulkCopy不失为一个好的选择,性能相当可观;最近亲手实验一把,效果不错,大家可以参见http://www. ...

  4. 结合 category 工作原理分析 OC2.0 中的 runtime

    绝大多数 iOS 开发者在学习 runtime 时都阅读过 runtime.h 文件中的这段代码: struct objc_class {Class isa OBJC_ISA_AVAILABILITY ...

  5. JavaScript中的ParseInt(08)和“09”返回0的原因分析及解决办法

    今天在程序中出现一个bugger ,调试了好久,最后才发现,原来是这个问题. 做了一个实验: alert(parseInt("01")),当这个里面的值为01====>07时 ...

  6. 纪中集训2020.01.16【NOIP普及组】模拟赛C组总结+【0.Matrix】分析

    纪中集训2020.01.16[NOIP普及组]模拟赛C组总结+[0.Matrix]分析 题目: 0.matrix 1.product 2.binary 3.value 巨佬估分:100+100+40+ ...

  7. android6.0中app crash流程分析

    要根据这个流程分析一下如何在应用中截获系统的app crash弹框,然后做到更人性化 基于Android 6.0的源码剖析, 分析Android应用Crash是如何处理的. /frameworks/b ...

  8. CloudSim4.0 Learning(1)

    CloudSim4.0 Learning(1) 文章目录 *CloudSim4.0 Learning(1)* *Example1* *1 CloudSim初始化* *1.1 CloudSim.init ...

  9. 《LINUX3.0内核源代码分析》第一章:内存寻址

    https://blog.csdn.net/ekenlinbing/article/details/7613334 摘要:本章主要介绍了LINUX3.0内存寻址方面的内容,重点对follow_page ...

最新文章

  1. 机器人替代研究员,工作007,完成688次实验,登上Nature封面
  2. 小白巷分享 -- Laravel5的新特性之异常处理
  3. Reverse Polish Notation
  4. el表达式动态取值中括号内两点_中考热点:旧瓶新酒,解题新策略分析之玩转动态型热点题型...
  5. 怎么用计算机弹c哩c哩,计算器音乐c哩c哩乐谱 | 手游网游页游攻略大全
  6. python-自定义模块-文件的操作
  7. springboot启动时的一个bug
  8. %lf 和 %f 有什么区别
  9. 软件验收报告文档模版
  10. ArcGIS不同坡度植被覆盖率分析步骤
  11. 国密SM算法有哪些?
  12. unity3D的FingerGestures插件
  13. Assertion failed: Protocol wrong type for socket [10041] zeromq 4.3.1\src\ip.cpp:417)错误
  14. selenium web自动化判断页面元素加载完毕
  15. Python学习记录 逻辑回归
  16. Web服务器(01)——介绍web服务器
  17. 高铁视频监控系统必看五大要求
  18. 工程师的基本功是什么?该如何练习?
  19. VS Code远程调试报错:Exception escaped from start_client
  20. google earth pro无法链接服务器的问题

热门文章

  1. c语言小游戏(c语言小游戏代码飞机大战)
  2. 基于springboot+h5+websocket的即时通讯客服系统和百度实时语音转译(语音在线识别)
  3. 《Learn python3 the hard way》ex20函数和文件
  4. 带有社区检测算法的多标签学习方法预测药物靶点相互作用(DTI-MLCD)
  5. linux串口重定向到usb串口,基于Linux的USB设备重定向研究
  6. 阿里云oss上传下载删除工具类
  7. SuperMap iDesktop常见问题解答集锦(十)
  8. SQL 注入漏洞详解
  9. android 防止反编译 安全加固技术
  10. SVN 删除文件并提交,此后本地UPDATE无法下载问题