CloudSim 学习实例1

CloudSim Example 1

cloudsim 教程例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
 */

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.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;
//cloudsim相关包

public class CloudSimExample1 {
	/** The cloudlet list. */ 云任务列表
	private static List<Cloudlet> cloudletList;
	/** The vmlist. */ 虚拟机列表
	private static List<Vm> vmlist;

	@SuppressWarnings("unused") 
	//主函数运行
	public static void main(String[] args) {
		Log.printLine("Starting CloudSimExample1...");  //结果输出

		try {
			// 第一步:初始化工具包
			int num_user = 1; // number of cloud users 云用户数量
			Calendar calendar = Calendar.getInstance(); //  用当前日期和时间初始化字段的日历
 			boolean trace_flag = false; // trace events 事件跟踪
 			
    ************************************************************************************************/
    *  Comment Start 
    * Initialize the CloudSim library. 
	* init()调用 initCommonVariable(), initCommonVariable()反过来调用initialize()
	           (all these 3 methods are defined in CloudSim.java).
	* initialize() 创建了两个集合 - an 数组列表 of SimEntity 数组对象 (表示模拟实体的named entities) and 
	* a LinkedHashMap 链表哈希映射 (named entitiesByName 表示相同模拟实体的LinkedHashMap), 
	           以每个SimEntity的名称作为键.
	* initialize() 创建两个队列 - a Queue of SimEvents (future) and another Queue of SimEvents (deferred). 
	* initialize() creates a 哈希表 of 预测 (以整数为键) - these predicates are used 
			   to select a particular event from the deferred queue. 
	* initialize() sets the simulation clock to 0 and running (a boolean flag) to false.
	* 一旦initialize()返回(请注意,我们现在处于方法initCommonVariable()), 
			   就会创建一个CloudSimShutDown(从SimEntity派生而来)实例
	* (with numuser as 1, its name as CloudSimShutDown, id as -1, and state as RUNNABLE). 
			   然后将这个新实体添加到模拟中
	* 当添加到模拟中时,它的id变为0(从前面的-1). 两个集合 - entities 和 entitiesByName 使用此SimEntity更新.
	* the shutdownId (whose default value was -1) is 0    
	* 一旦initCommonVariable()返回(请注意,我们现在是在方法init()), 就会创建一个        
			   CloudInformationService(它也派生自SimEntity)实例
	* (with its name as CloudInformatinService, id as -1, and state as RUNNABLE). 
			   然后这个新实体也被添加到模拟中. 
	* While being added to the simulation, the id of the SimEntitiy is changed to 1 
			   (which is the next id) from its earlier value of -1. 
	* 两个集合 - entities 和 entitiesByName are updated with this SimEntity.
	* the cisId(whose default value is -1) is 1
	* Comment End - Dinesh Bhagwat 
	************************************************************************************************/
			 
			CloudSim.init(num_user, calendar, trace_flag);

			// 第二步:创建数据中心
			// Datacenters are the resource providers in CloudSim. We need at
			// list one of them to run a CloudSim simulation
			Datacenter datacenter0 = createDatacenter("Datacenter_0");

			// 第三步: Create Broker 代理
			DatacenterBroker broker = createBroker();
			int brokerId = broker.getId();

			// 第四步: Create one virtual machine
			vmlist = new ArrayList<Vm>();

			// VM 参数
			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 cpus
			String vmm = "Xen"; // VMM name

			// create VM 以刚才定义的参数创建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);

			// 第五步: Create one Cloudlet 创建一个云任务
			cloudletList = new ArrayList<Cloudlet>();

			// Cloudlet 参数
			int id = 0;
			long length = 400000;
			long fileSize = 300;
			long outputSize = 300;
			UtilizationModel utilizationModel = new UtilizationModelFull();
			// create cloudlet 以刚才定义的参数创建云任务
			Cloudlet cloudlet = 
                                new Cloudlet(id, length, pesNumber, 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);

			// 第六步: Starts the simulation 开始仿真
			CloudSim.startSimulation();

			CloudSim.stopSimulation();

			//最后一步: Print results when simulation is over  仿真结束 打印结果
			List<Cloudlet> newList = broker.getCloudletReceivedList();
			printCloudletList(newList);

			Log.printLine("CloudSimExample1 finished!");
		} catch (Exception e) {             Java Exception 异常处理机制
			e.printStackTrace();
			Log.printLine("Unwanted errors happen");
		}
	}

	/**函数
	 * Creates the datacenter.
	 *
	 * @命名
	 *
	 * @return the datacenter
	 */
	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>();

		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 id 和 处理速度

		// 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 VmSchedulerTimeShared(peList) 虚拟机时间共享
			)
		); // This is our machine

		// 5. 创建一个数据中心特征对象,
		// 该对象存储数据中心的属性:体系结构、操作系统、机器列表、
		// 分配策略:时间或空间共享、时区及其价格(G$/Pe时间单位)
		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 SAN devices by now
		//特征对象属性
		DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
				arch, os, vmm, hostList, time_zone, cost, costPerMem,
				costPerStorage, costPerBw);

		// 6. 最后, 创建数据中心对象
		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
	/** 创建代理,可以根据特定需求发展自己的代理协议来提交虚拟机和云任务
	 *
	 * @return the datacenter broker
	 */
	private 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()));
			}
		}
	}
}

运行结果:

结果

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值