使用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)
上一篇 2022年10月13日 上午8:16
下一篇 2022年10月13日 上午8:16


相关推荐

  • MyBatis-Plus 分页查询以及自定义sql分页

    MyBatis-Plus 分页查询以及自定义sql分页一、引言分页查询每个人程序猿几乎都使用过,但是有部分同学不懂什么是物理分页和逻辑分页。物理分页:相当于执行了limit分页语句,返回部分数据。物理分页只返回部分数据占用内存小,能够获取数据库最新的状态,实施性比较强,一般适用于数据量比较大,数据更新比较频繁的场景。逻辑分页:一次性把全部的数据取出来,通过程序进行筛选数据。如果数据量大的情况下会消耗大量的内存,由于逻辑分页只需要读取数据库…

    2022年6月26日
    34
  • rdlc mysql_RDLC使用手册_RDLC报表部署

    rdlc mysql_RDLC使用手册_RDLC报表部署原文 http blog csdn net lwjnumber article details RDLC 报表部署 限于 rdlc 报表 windows 应用程序 1 RDLC 报表所需的 4 个 DLL 文件提取 RDLC 报表文件部署在客户端若要正常工作 需 4 个 dll 文件 分别是 Microsoft ReportViewer Common dll Microsoft ReportV

    2026年3月18日
    2
  • 堆排序算法图解详细流程(堆排序过程图解)

    堆排序的时间复杂度O(N*logN),额外空间复杂度O(1),是一个不稳定性的排序目录一准备知识1.1大根堆和小根堆二堆排序基本步骤2.1构造堆2.2固定最大值再构造堆三总结四代码一准备知识堆的结构可以分为大根堆和小根堆,是一个完全二叉树,而堆排序是根据堆的这种数据结构设计的一种排序,下面先来看看什么是大根堆和小根堆1.1大根…

    2022年4月18日
    363
  • http中的expect

    http中的expect1 http100 continue 用于客户端在发送 POST 数据给服务器前 征询服务器情况 看服务器是否处理 POST 的数据 如果不处理 客户端则不上传 POST 数据 如果处理 则 POST 上传数据 在现实应用中 通过在 POST 大数据时 才会使用 100 continue 协议 2 客户端策略 1 如果客户端有 POST 数据要上传 可以考虑使用 100 continue 协议 加入头 Ex

    2026年3月18日
    1
  • java io 试题_Java IO流面试题

    java io 试题_Java IO流面试题字节流与字符流的不同是他们的处理方式,字节流是最基本的,采用ASCII编码。但是实际上很多数据是文本,所以提出字符流的概念,采用unicode编码两者之间通过inputStreamReader与outputStreamWriter来关联,实际上是通过byte[]与String来关联字节流输出:程序–>字节流–>文件字符流输出:程序–>字符流–>缓冲–>文件程序中所有…

    2022年4月25日
    31
  • php实现html转图片_php获取word内容

    php实现html转图片_php获取word内容Html转Word目测方法大概有两种:1.直接把html代码写入word以二进制的方式2.通过mnt这个介质生成word方法一(推荐):造了个轮子https://packagist.org/packages/cshaptx4869/html2wordcomposerrequirecshaptx4869/html2word…

    2022年10月12日
    5

发表回复

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

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