使用cloudsim进行云计算仿真步骤_公司分析中最重要的是

使用cloudsim进行云计算仿真步骤_公司分析中最重要的是CloudSimExample1展示如何创建一个只包含一个主机的数据中心,并且在其上运行一个云任务。首先附上CloudSimExample1全部代码:packageorg.cloudbus.cloudsim.examples;/**Title:CloudSimToolkit*Description:CloudSim(CloudSimulation…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

CloudSimExample1展示如何创建一个只包含一个主机的数据中心,并且在其上运行一个云任务。

首先附上CloudSimExample1全部代码:

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;
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 data center with one host and run one cloudlet on it.
 */
public class CloudSimExample1 {
	/** The cloudlet list. */
	private static List<Cloudlet> cloudletList;
	/** The vmlist. */
	private static List<Vm> vmlist;

	/**
	 * Creates main() to run this example.
	 *
	 * @param args the args
	 */
	@SuppressWarnings("unused")
	public static void main(String[] args) {
		Log.printLine("Starting CloudSimExample1...");

		try {
			// First step: Initialize the CloudSim package. It should be called before creating any entities.
			int num_user = 1; // number of cloud users
			Calendar calendar = Calendar.getInstance(); // Calendar whose fields have been initialized with the current date and time.
 			boolean trace_flag = false; // trace events

			/* Comment Start - Dinesh Bhagwat 
			 * Initialize the CloudSim library. 
			 * init() invokes initCommonVariable() which in turn calls initialize() (all these 3 methods are defined in CloudSim.java).
			 * initialize() creates two collections - an ArrayList of SimEntity Objects (named entities which denote the simulation entities) and 
			 * a LinkedHashMap (named entitiesByName which denote the LinkedHashMap of the same simulation entities), with name of every SimEntity as the key.
			 * initialize() creates two queues - a Queue of SimEvents (future) and another Queue of SimEvents (deferred). 
			 * initialize() creates a HashMap of of Predicates (with integers as keys) - 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.
			 * Once initialize() returns (note that we are in method initCommonVariable() now), a CloudSimShutDown (which is derived from SimEntity) instance is created 
			 * (with numuser as 1, its name as CloudSimShutDown, id as -1, and state as RUNNABLE). Then this new entity is added to the simulation 
			 * While being added to the simulation, its id changes to 0 (from the earlier -1). The two collections - entities and entitiesByName are updated with this SimEntity.
			 * the shutdownId (whose default value was -1) is 0    
			 * Once initCommonVariable() returns (note that we are in method init() now), a CloudInformationService (which is also derived from SimEntity) instance is created 
			 * (with its name as CloudInformatinService, id as -1, and state as RUNNABLE). Then this new entity is also added to the simulation. 
			 * 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. 
			 * The two collections - entities and 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);

			// 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();
			int brokerId = broker.getId();

			// Fourth step: Create one virtual machine
			vmlist = new ArrayList<Vm>();

			// 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 cpus
			String vmm = "Xen"; // 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
			cloudletList = new ArrayList<Cloudlet>();

			// Cloudlet properties
			int id = 0;
			long length = 400000;
			long fileSize = 300;
			long outputSize = 300;
			UtilizationModel utilizationModel = new UtilizationModelFull();

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

			// Sixth step: Starts the simulation
			CloudSim.startSimulation();

			CloudSim.stopSimulation();

			//Final step: Print results when simulation is over
			List<Cloudlet> newList = broker.getCloudletReceivedList();
			printCloudletList(newList);

			Log.printLine("CloudSimExample1 finished!");
		} catch (Exception e) {
			e.printStackTrace();
			Log.printLine("Unwanted errors happen");
		}
	}

	/**
	 * Creates the datacenter.
	 *
	 * @param name the name
	 *
	 * @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.
		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 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. 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 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
	/**
	 * Creates the broker.
	 *
	 * @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()));
			}
		}
	}
}

接下来进行详细分析:

package org.cloudbus.cloudsim.examples;

这行代码说明该java文件所处包的位置。

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;

这部分代码是引用接口部分:分为俩部分,一部分是引用java自身的,例如队列,日历等;另一部分是引用cloudsim的,例如存储,数据中心等。

/** The cloudlet list. */
private static List<Cloudlet> cloudletList;
/** The vmlist. */
private static List<Vm> vmlist;

这段代码声明了俩个域,分别是cloudletlist和vmlist,分别是云任务队列和虚拟机队列。

接下来是主函数部分:

public static void main(String[] args) {
		Log.printLine("Starting CloudSimExample1...");
                //输出这一行“Starting CloudSimExample1...”代表main()函数开始执行。

		try {
			// First step: Initialize the CloudSim package. It should be called before creating any entities.
			int num_user = 1; // number of cloud users//云用户数量,建立只有一个云用户的实例。
			Calendar calendar = Calendar.getInstance(); // Calendar whose fields have been initialized with the current date and time.//日历直接调用。
 			boolean trace_flag = false; // trace events//设置标志位。
			CloudSim.init(num_user, calendar, trace_flag);//云用户数量,建立只有一个云用户的实例。
			Calendar calendar = Calendar.getInstance(); // Calendar whose fields have been initialized with the current date and time.//日历直接调用。
 			boolean trace_flag = false; // trace events//设置标志位。
			CloudSim.init(num_user, calendar, trace_flag);
                         //第一步:初始化cloudsim包(在创建数据中心的实例前必须进行初始化cloudsim包),直接调用CloudSim.init()函数,是个静态方法,有三个参数。 
			// 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");//第一步:初始化cloudsim包(在创建数据中心的实例前必须进行初始化cloudsim包),直接调用CloudSim.init()函数,是个静态方法,有三个参数。 
			// 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();
			int brokerId = broker.getId();
			// Third step: Create Broker
			DatacenterBroker broker = createBroker();
			int brokerId = broker.getId();
                       //第三步:创建代理,一个代理负责代表一个用户,用来提交虚拟机列表和云任务列表。

			// Fourth step: Create one virtual machine
			vmlist = new ArrayList<Vm>();//第三步:创建代理,一个代理负责代表一个用户,用来提交虚拟机列表和云任务列表。

			// Fourth step: Create one virtual machine
			vmlist = new ArrayList<Vm>();
                        //第四步:创建虚拟机VM,指定计算能力(用mips表示),内存,带宽,CPU数等,并添加到虚拟机列表,让代理提交。

			// VM description//对虚拟机参数进行描述,包括虚拟机id,mips,虚拟机内存,带宽,CPU数,vmm名字等。
			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 = 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
			cloudletList = new ArrayList<Cloudlet>(); //第四步:创建虚拟机VM,指定计算能力(用mips表示),内存,带宽,CPU数等,并添加到虚拟机列表,让代理提交。

			// VM description//对虚拟机参数进行描述,包括虚拟机id,mips,虚拟机内存,带宽,CPU数,vmm名字等。
			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 = 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
			cloudletList = new ArrayList<Cloudlet>();
                        //第五步:创建云任务,指定云任务的参数(云任务ID,长度,文件大小,输出大小,使用模式),其中length指的是MIPS数(指令数)。将云任务添加到云任务列表,并提交给代理。

			// Cloudlet properties//对云任务参数进行描述,包括云任务ID,长度,文件大小,输出大小,使用模式。
			int id = 0;
			long length = 400000;
			long fileSize = 300;
			long outputSize = 300;
			UtilizationModel utilizationModel = new UtilizationModelFull();

			Cloudlet cloudlet = 
                                new Cloudlet(id, length, pesNumber, fileSize, 
                                        outputSize, utilizationModel, utilizationModel, 
                                        utilizationModel);
			cloudlet.setUserId(brokerId);
			cloudlet.setVmId(vmid);//cloudlet可以设置代理ID(一个代理和一个用户对应),表示拥有此cloudlet的用户或者vm ID,表示运行此cloudlet的vm。
			// add the cloudlet to the list//将创建好的云任务添加到云任务列表。
			cloudletList.add(cloudlet);

			// submit cloudlet list to the broker//将云任务列表提交给用户。
			broker.submitCloudletList(cloudletList);

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

			// Cloudlet properties//对云任务参数进行描述,包括云任务ID,长度,文件大小,输出大小,使用模式。
			int id = 0;
			long length = 400000;
			long fileSize = 300;
			long outputSize = 300;
			UtilizationModel utilizationModel = new UtilizationModelFull();

			Cloudlet cloudlet = 
                                new Cloudlet(id, length, pesNumber, fileSize, 
                                        outputSize, utilizationModel, utilizationModel, 
                                        utilizationModel);
			cloudlet.setUserId(brokerId);
			cloudlet.setVmId(vmid);//cloudlet可以设置代理ID(一个代理和一个用户对应),表示拥有此cloudlet的用户或者vm ID,表示运行此cloudlet的vm。
			// add the cloudlet to the list//将创建好的云任务添加到云任务列表。
			cloudletList.add(cloudlet);

			// submit cloudlet list to the broker//将云任务列表提交给用户。
			broker.submitCloudletList(cloudletList);

			// Sixth step: Starts the simulation
			CloudSim.startSimulation();
			CloudSim.stopSimulation();
                        //第六步:开始仿真,直到仿真结束。

			//Final step: Print results when simulation is over
			List<Cloudlet> newList = broker.getCloudletReceivedList();
			printCloudletList(newList);//第六步:开始仿真,直到仿真结束。

			//Final step: Print results when simulation is over
			List<Cloudlet> newList = broker.getCloudletReceivedList();
			printCloudletList(newList);
                        //第七步:当仿真结束后,打印仿真结果。(结果包括云任务队列和每个用户使用数据中心的情况)

			Log.printLine("CloudSimExample1 finished!");
		} catch (Exception e) {
			e.printStackTrace();
			Log.printLine("Unwanted errors happen");
		}
	} //第七步:当仿真结束后,打印仿真结果。(结果包括云任务队列和每个用户使用数据中心的情况)

			Log.printLine("CloudSimExample1 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>();
		int mips = 1000; //第一步:创建主机列表,来存储主机。

		// 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(CPU)列表,来存储Pe(CPU)。一个主机有一个(单核)或多个CPU(多核),一个主机含有一个Pe列表,Pe需要指定计算能力,用MIPS表示。

		// 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(CPU)列表,来存储Pe(CPU)。一个主机有一个(单核)或多个CPU(多核),一个主机含有一个Pe列表,Pe需要指定计算能力,用MIPS表示。

		// 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(指定peid,mips),并将其添加到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 VmSchedulerTimeShared(peList)
			)
		); // This is our machine//第三步:创建Pe(指定peid,mips),并将其添加到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 VmSchedulerTimeShared(peList)
			)
		); // This is our machine
               //第四步:用Pe列表和id创建主机(指定主机id,内存,存储容量,带宽,Pe(CPU)列表等),并将其添加到主机列表。其中VmSchedulerTimeShared表示 虚拟机使用时分方式共享主机CPU。

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

		// 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 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;
              //第六步:创建数据中心对象。其中VmAllocationSimple表示将VM分配到已经使用Pe最少的物理机中。
	}//第六步:创建数据中心对象。其中VmAllocationSimple表示将VM分配到已经使用Pe最少的物理机中。
	}
private static DatacenterBroker createBroker() {
		DatacenterBroker broker = null;
		try {
			broker = new DatacenterBroker("Broker");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return broker;
	}

上述这段代码用来创建代理。

 

private static void printCloudletList(List<Cloudlet> list) {
		int size = list.size();
		Cloudlet cloudlet;

		String indent = "    "; //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");//打印参数:云任务ID,状态,数据中心ID,VM ID,时间,开始时间,结束时间。 
		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()));
			}
		}//循环整个云任务列表,并以十进制格式输出各参数数据。
	}//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");//打印参数:云任务ID,状态,数据中心ID,VM ID,时间,开始时间,结束时间。

		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 CloudSimExample1...
Initialising...
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)
0.0: Broker: Trying to Create VM #0 in Datacenter_0
0.1: Broker: VM #0 has been created in Datacenter #2, Host #0
0.1: Broker: Sending cloudlet 0 to VM #0
400.1: Broker: Cloudlet 0 received
400.1: Broker: All Cloudlets executed. Finishing...
400.1: 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        400        0.1        400.1
CloudSimExample1 finished!

仿真结果显示:云任务ID为0,云任务状态为SUCCESS,数据中心ID为2,VM ID为0,云任务0运行时间为400,开始时间为0.1,结束时间为400.1。

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/182555.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 什么是devops思想在运维方面的具体实践_devops四个维度

    什么是devops思想在运维方面的具体实践_devops四个维度DevOps是最近非常火的一个概念,谈IT流程建设不说点DevOps都不好意思和人打招呼。但是DevOps究竟是个什么东西,这个东西能不能用?怎么用?什么样的情况才叫做DevOps落地成功?对于这些问题的答案,虽然网上有铺天盖地的文章和教程,但是一般来说都是从理论或者方法论上去阐述,也有大厂的实施经历。个人就感觉这里的它山之石,很难攻玉了。最终还是得思考下DevOps的由来,综合自己所在企业的现实…

    2022年10月5日
    0
  • 深入浅出ES6(四):模板字符串

    在上一篇文章中,我说过要写一篇风格迥异的新文章,在了解了迭代器和生成器后,是时候来品味一些不烧脑的简单知识,如果你们觉得太难了,还不快去啃犀牛书!现在,就让我们从最简单的知识学起吧!反撇号(`)基础知识ES6引入了一种新型的字符串字面量语法,我们称之为模板字符串(templatestrings)。除了使用反撇号字符`代替普通字符串的引号’或”外,它们看起来与普通

    2022年4月5日
    123
  • 电脑史话(说历史视频)

    1、计算机始祖从1980年8月到1981年8月,在整整一年的时间里,埃斯特奇领导着“国际象棋”工程计划13人小组奋力攻关。“当时很少有人体会到,这一小组人即将改写全世界的历史。”(英特尔华裔副总裁虞有澄语)据说,IBM公司后来围绕PC机的各项开发,投入的力量逐步达到450人,英特尔公司也组成“特殊客户部”为PC机供应高质量的芯片。  根据协定,微软公司应该为PC机提供包括BASIC在内的

    2022年4月15日
    33
  • deepin安装rabbitvcs svn[通俗易懂]

    deepin安装rabbitvcs svn[通俗易懂]https://bbs.deepin.org/post/189817

    2022年7月18日
    17
  • 一个简单需求:HashMap实现相同key存入数据后不被覆盖

    做一个积极的人编码、改bug、提升自己我有一个乐园,面向编程,春暖花开!看似是一个简单的问题,其实里面包含很多的东西!需求:实现一个在HashMap中存入(任意类型)相同的key值后,key中的value不会被覆盖,而是能够进行叠加!拿到一个需求的时候,我们要先进行分析,看此需求能否实现,基于已有的知识(经验),然后在通过目前的一些技术看此需求如何实现。要实现在HashMap中插…

    2022年2月28日
    55
  • Shell Step by Step (4) —— Cron &amp; Echo「建议收藏」

    Shell Step by Step (4) —— Cron &amp; Echo

    2022年2月7日
    42

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号