CloudSim3.0.3 网络编程

来源:互联网 发布:如何上传网站源码 编辑:程序博客网 时间:2024/06/06 09:42
以 org.cloudbus.cloudsim.examples.network.NetworkExample1 为例,该例子展示了如何创建一个有网络拓扑的数据中心并且在其上运行一个云任务。例子通过读取topology.brite 文件来构造网络拓扑。网络拓扑的信息包括节点的位置,节点间的有向边,边时延,边带宽等信息。能够模拟基于网络位置,时延,带宽等的网络环境,有效地计算网络传输造成的花销。与前面例子不同的是,网络编程需要调用 org.cloudbus.cloudsim.NetworkTopology 构造网络拓扑图,然后把 CloudSim 实体与拓扑图的节点进行映射。  


/** * org.cloudbus.cloudsim.examples.network.NetworkExample1 * A simple example showing how to create * a datacenter with one host and a network * topology and and run one cloudlet on it. */public static void main(String[] args) {  Log.printLine("Starting NetworkExample1...");  // 第一步:初始化 CloudSim  int num_user = 1;  Calendar calendar = Calendar.getInstance();  boolean trace_flag = false;  CloudSim.init(num_user, calendar, trace_flag); //第二步:创建数据中心  Datacenter datacenter0 = createDatacenter("Datacenter_0");  //第三步:创建代理  DatacenterBroker broker = createBroker();  int brokerId = broker.getId(); //第四步:创建一个虚拟机  vmlist = new ArrayList<Vm>();  //虚拟机参数  int vmid = 0;  int mips = 250;  long size = 10000;  int ram = 512;  long bw = 1000;  int pesNumber = 1;  String vmm = "Xen";  //创建虚拟机  Vm vm1 = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());  vmlist.add(vm1); //提交虚拟机列表到代理  broker.submitVmList(vmlist); //第五步:创建一个任务  cloudletList = new ArrayList<Cloudlet>(); //任务参数  int id = 0;  long length = 40000;  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);  cloudletList.add(cloudlet1);  //提交任务列表到代理  broker.submitCloudletList(cloudletList); //第六步:配置网络  //加载网络拓扑文件 
 NetworkTopology.buildNetworkTopology("topology.brite");  //注意:直接运行该例子,可能会运行失败,报错找不到 topology.brite  //解决方法:方法一:buildNetworkTopology()中的参数改为 topology.brite //的绝对路径;方法二:把 topology.brite 拷贝到项目的根目录下 //CloudSim 实体与拓扑图中的对象建立映射 //数据中心对应拓扑图的节点 0 int briteNode=0;  NetworkTopology.mapNode(datacenter0.getId(),briteNode);  //代理对应拓扑图的节点 3  briteNode=3;  NetworkTopology.mapNode(broker.getId(),briteNode); 
 // 第七步:启动仿真  CloudSim.startSimulation();  //第七步:统计结果并输出结果  List<Cloudlet> newList = broker.getCloudletReceivedList(); CloudSim.stopSimulation();  printCloudletList(newList); //打印数据中心的成本  datacenter0.printDebts(); Log.printLine("NetworkExample1 finished!");  } 



topology.brite 如下:

Topology: ( 5 Nodes, 8 Edges ) Model (1 - RTWaxman): 5 5 5 1 2 0.15000000596046448 0.20000000298023224 1 1 10.0 1024.0 Nodes: ( 5 ) 0 1 3 3 3 -1 RT_NODE 1 0 3 3 3 -1 RT_NODE 2 4 3 3 3 -1 RT_NODE 3 3 1 3 3 -1 RT_NODE 4 3 3 4 4 -1 RT_NODE Edges: ( 8 ) 0 2 0 3.0 1.1 10.0 -1 -1 E_RT U 1 2 1 4.0 2.1 10.0 -1 -1 E_RT U 2 3 0 2.8284271247461903 3.9 10.0 -1 -1 E_RT U 3 3 1 3.605551275463989 4.1 10.0 -1 -1 E_RT U 4 4 3 2.0 5.0 10.0 -1 -1 E_RT U 5 4 2 1.0 4.0 10.0 -1 -1 E_RT U 6 0 4 2.0 3.0 10.0 -1 -1 E_RT U 7 1 4 3.0 4.1 10.0 -1 -1 E_RT U 
程序会寻找标记“Nodes:”和“Edges:”,“Nodes”是节点信息,其中第一列是节点序号,第二列是节点的横坐标,第三列是纵坐标。“Edges”是边信息,第一列是边序号,第二列是
始节点序号,第三列是终节点序号,第四列是边长度,第五列是边时延,第六列是边带宽。CloudSim 中只用到了以上信息。如此,我们就能构造自己需要的网络拓扑了。

运行仿真样例的结果如下图:

Starting NetworkExample1... 
Initialising... 
Topology file: topology.brite 
Starting CloudSim version 3.0 
Datacenter_0 is starting... 
Broker is starting... 
Entities started. 
0.0: Broker: Cloud Resource List received with 1 resource(s) 
7.800000190734863: Broker: Trying to Create VM #0 in Datacenter_0 
15.700000381469726: Broker: VM #0 has been created in Datacenter #2, Host #0 
15.700000381469726: Broker: Sending cloudlet 0 to VM #0 
183.50000057220458: Broker: Cloudlet 0 received 
183.50000057220458: Broker: All Cloudlets executed. Finishing... 
183.50000057220458: Broker: Destroying VM #0 
Broker is shutting down... 
Simulation: No more future events 
CloudInformationService: Notify all CloudSim entities for shutting down. 
Datacenter_0 is shutting down... 
Broker is shutting down... 
Simulation completed. 
Simulation completed. 
========== OUTPUT ========== 
Cloudlet ID STATUS Data center ID VM ID Time Start Time 
Finish Time 
 0 SUCCESS 2 0 160 19.6 
179.6 
*****Datacenter: Datacenter_0***** 
User id Debt 
3 35.6 
********************************** 
NetworkExample1 finished! 



    那么实体之间的通信延迟是如何影响到模拟程序的clock推演的呢?答案是SimEntity.send(int entityId, double delay, int cloudSimTag, Object data) 函数中调用了delay +=getNetworkDelay(srcId, entityId)。具体而言,所有实体(包括DC, DCBroker)在向其它实体发送事件时,delay参数中包含了通信延迟。由笔者系列文章的第一篇(CloudSim(3.0.3)运行机制见解)可知,事件是先被缓存在CloudSim.future队列中,该事件被取出时clock自然就被推进了,最后目标实体调用相关方法完成事件处理。

    那么在“Broker: Trying to Create VM #0 in Datacenter_0”之前的7.8秒(模拟时间)是怎么产生的呢?这其实是3.9*2=7.8,其中3.9s是DC(拓扑0号节点)和DCBroker(拓扑3号节点)之间的通信延迟。DCBroker从CloudSim类处弄到了DC列表之后,向DC发送CloudSimTags.RESOURCE_CHARACTERISTICS事件,DC再回复带有DC特性对象的CloudSimTags.RESOURCE_CHARACTERISTICS事件,这样一来一回,clock就推演了3.9*2=7.8s。

原创粉丝点击