cloudsim 学习笔记 实例5

更新时间:2023-05-12 03:51:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

cloudsim 学习笔记 实例

* Title: CloudSim Toolkit

* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation

* of Clouds

* Licence: GPL - /copyleft/gpl.html

*

* Copyright (c) 2009, The University of Melbourne, Australia

*/

【试验五:怎样创建2个数据中心,各含1台主机,并在其上运行二个用户的云任务】 package org.cloudbus.cloudsim.examples;

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.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.VmSchedulerSpaceShared;

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

* two datacenters with one host each and

* run cloudlets of two users on them.

*/

public class CloudSimExample5 {

/** The cloudlet lists. */ 【云任务列表】

cloudsim 学习笔记 实例

private static List<Cloudlet> cloudletList1; 【两个用户的云任务,所以两个云任务列表】 private static List<Cloudlet> cloudletList2;

/** The vmlists. */ 【虚拟机列表,创建了两个】

private static List<Vm> vmlist1;

private static List<Vm> vmlist2;

/**

* Creates main() to run this example

*/ 【主函数运行实例】

public static void main(String[] args) {

Log.printLine("Starting CloudSimExample5...");

try {

// First step: Initialize the CloudSim package. It should be called 【初始化工具包】 // before creating any entities.

int num_user = 2; // number of cloud users 【2个用户】

Calendar calendar = Calendar.getInstance();

boolean trace_flag = false; // mean trace events

// Initialize the CloudSim library

CloudSim.init(num_user, calendar, trace_flag);

// Second step: Create Datacenters 【创建数据中心 2个数据中心】

//Datacenters are the resource providers in CloudSim. We need at list one of them to run a CloudSim simulation

Datacenter datacenter0 = createDatacenter("Datacenter_0");

Datacenter datacenter1 = createDatacenter("Datacenter_1");

//Third step: Create Brokers 【创建2个代理】

DatacenterBroker broker1 = createBroker(1);

int brokerId1 = broker1.getId();

DatacenterBroker broker2 = createBroker(2);

int brokerId2 = broker2.getId();

//Fourth step: Create one virtual machine for each broker/user 【为每个 代理/用户 创建一个虚拟机器】

vmlist1 = new ArrayList<Vm>();

vmlist2 = new ArrayList<Vm>();

//VM description 【虚拟机参数】

int vmid = 0;

cloudsim 学习笔记 实例

int mips = 250;

long size = 10000; //image size (MB)

int ram = 512; //vm memory (MB)

long bw = 1000;

int pesNumber = 1; //number of cpus

String vmm = "Xen"; //VMM name

//create two VMs: the first one belongs to user1 【第一个虚拟机属于用户1】 Vm vm1 = new Vm(vmid, brokerId1, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared()); 【代理1 brokerId1】

//the second VM: this one belongs to user2 【第二个虚拟机属于用户2】

Vm vm2 = new Vm(vmid, brokerId2, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());【代理2 brokerId2】

//add the VMs to the vmlists 【加入虚拟机列表】

vmlist1.add(vm1); 【加入虚拟机列表1】

vmlist2.add(vm2); 【加入虚拟机列表2】

//submit vm list to the broker 【虚拟机列表提交给代理】

broker1.submitVmList(vmlist1);

broker2.submitVmList(vmlist2);

//Fifth step: Create two Cloudlets 【创建二个云任务】

cloudletList1 = new ArrayList<Cloudlet>();

cloudletList2 = new ArrayList<Cloudlet>();

//Cloudlet properties 【云任务参数】

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(brokerId1); 【设置任务1给代理1】

Cloudlet cloudlet2 = new Cloudlet(id, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);

cloudlet2.setUserId(brokerId2); 【设置任务2给代理2】

//add the cloudlets to the lists: each cloudlet belongs to one user 【云任务加入列表,每个云任务属于一个用户】

cloudsim 学习笔记 实例

cloudletList2.add(cloudlet2); //submit cloudlet list to the brokers 【云任务列表提交给代理】 broker1.submitCloudletList(cloudletList1); broker2.submitCloudletList(cloudletList2);

【比较例子4 没有了bind the cloudlets to the vms !!!】

// Sixth step: Starts the simulation 【开始仿真】

CloudSim.startSimulation();

// Final step: Print results when simulation is over 【仿真结束 打印结果】 List<Cloudlet> newList1 = broker1.getCloudletReceivedList();

List<Cloudlet> newList2 = broker2.getCloudletReceivedList();

CloudSim.stopSimulation();

Log.print("=============> User "+brokerId1+" ");

printCloudletList(newList1);

Log.print("=============> User "+brokerId2+" ");

printCloudletList(newList2);

//Print the debt of each user to each datacenter 【打印账单】

datacenter0.printDebts();

datacenter1.printDebts();

Log.printLine("CloudSimExample5 finished!");

}

catch (Exception e) {

e.printStackTrace();

Log.printLine("Unwanted errors happen");

}

}

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 machine

List<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>();

cloudsim 学习笔记 实例

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;

hostList.add(

new Host(

hostId,

new RamProvisionerSimple(ram),

new BwProvisionerSimple(bw),

storage,

peList,

new VmSchedulerSpaceShared(peList) 【虚拟机空间共享】 )

); // This is our first 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 architecture

String os = "Linux"; // operating system

String vmm = "Xen";

double time_zone = 10.0; // time zone this resource located

double cost = 3.0; // the cost of using processing in this resource double costPerMem = 0.05; // the cost of using memory in this resource double costPerStorage = 0.001; // the cost of using storage in this resource double costPerBw = 0.0; // the cost of using bw in this resource

LinkedList<Storage> storageList = new LinkedList<Storage>(); //we are not adding

cloudsim 学习笔记 实例

SAN devices by now

DatacenterCharacteristics 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 scenario

private static DatacenterBroker createBroker(int id){ 【int id 相比例子四多出来的】

DatacenterBroker broker = null;

try {

broker = new DatacenterBroker("Broker"+id);

} 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();

cloudsim 学习笔记 实例

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()));

}

}

}

}

Starting CloudSimExample5...

Initialising...

Starting CloudSim version 2.0

Datacenter_0 is starting...

Datacenter_1 is starting...

Broker1 is starting...

Broker2 is starting...

Entities started.

0.0: Broker1: Cloud Resource List received with 2 resource(s) 2个数据中心 0.0: Broker2: Cloud Resource List received with 2 resource(s) 0.0: Broker1: Trying to Create VM #0 in Datacenter_0

0.0: Broker2: Trying to Create VM #0 in Datacenter_0

[VmScheduler.vmCreate] Allocation of VM #0 to Host #0 failed by MIPS 0.0: Broker1: VM #0 has been created in Datacenter #2, Host #0 0.0: Broker1: Sending cloudlet 0 to VM #0

0.0: Broker2: Creation of VM #0 failed in Datacenter #2

0.0: Broker2: Trying to Create VM #0 in Datacenter_1

cloudsim 学习笔记 实例

0.0: Broker2: VM #0 has been created in Datacenter #3, Host #0 0.0: Broker2: Sending cloudlet 0 to VM #0

160.0: Broker1: Cloudlet 0 received

160.0: Broker1: All Cloudlets executed. Finishing...

160.0: Broker1: Destroying VM #0

160.0: Broker2: Cloudlet 0 received

160.0: Broker2: All Cloudlets executed. Finishing...

160.0: Broker2: Destroying VM #0

Broker1 is shutting down...

Broker2 is shutting down...

Simulation: No more future events

CloudInformationService: Notify all CloudSim entities for shutting down. Datacenter_0 is shutting down...

Datacenter_1 is shutting down...

Broker1 is shutting down...

Broker2 is shutting down...

Simulation completed.

Simulation completed.

=============> User 4

========== OUTPUT ==========

Cloudlet ID STATUS Data center ID VM ID Time Start Time Finish Time

0 SUCCESS 2 0 160 0 160 =============> User 5

========== OUTPUT ==========

Cloudlet ID STATUS Data center ID VM ID Time Start Time Finish Time

0 SUCCESS 3 0 160 0 160 *****PowerDatacenter: Datacenter_0*****

User id

4 Debt 35.6

**********************************

*****PowerDatacenter: Datacenter_1*****

User id

5 Debt 35.6

**********************************

CloudSimExample5 finished!

【疑问】

1. Data center ID是怎么来的?

2. User id 怎么是4 和 5?

本文来源:https://www.bwwdw.com/article/qs8e.html

Top