使用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。

 

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • keyvaluepair_c# KeyValuePair用法「建议收藏」

    keyvaluepair_c# KeyValuePair用法「建议收藏」privateKeyValuePairSetKeyValuePair(){intintKey=1;stringstrValue=”Myvalue”;KeyValuePairkvp=newKeyValuePair(intKey,strValue);returnkvp;}//////获得键/值对///privatevoidGetKeyValuePairDemo(…

  • Java课设:学生管理系统

    Java课设:学生管理系统文章目录StudentManagerStudentManagerMain.javaStuInfo.javaClassInfo.javaScoreInfo.javaAdd.javaDelete.javaStudentManager查询学生的个人基本信息,查询课程表、选课情况,查询课程的成绩信息。其中课程表及选课信息和成绩信息无法改动,个人基本信息可以添加或者删除。这是一个比较简单的管理系统,具…

  • 推荐一个比较好用的画廊展示图片(支持无限轮播)的控件ViewPagerGallery「建议收藏」

    推荐一个比较好用的画廊展示图片(支持无限轮播)的控件ViewPagerGallery「建议收藏」1.在此我们引用支持无限滑动的3D视觉的画廊效果、平面普通广告栏轮播这个例子中有可以运行的效果,大家可以下载下来先看一下,在我开始使用的时候,发现,想要调整pageitem中的间距,比较困难,并没有暴露方法出来。所以就要看一下源代码,发现,在不使用3D效果,即初始化:initBanner(urlList,false),没有什么问题,(这里说的3D效果,就是左右item要比正在显示的ite…

  • 卷积神经网络和图像识别[通俗易懂]

    卷积神经网络和图像识别[通俗易懂]卷积神经网络与图像识别我们介绍了人工神经网络,以及它的训练和使用。我们用它来识别了手写数字,然而,这种结构的网络对于图像识别任务来说并不是很合适。本文将要介绍一种更适合图像、语音识别任务的神经网络结构——卷积神经网络(ConvolutionalNeuralNetwork,CNN)。说卷积神经网络是最重要的一种神经网络也不为过,它在最近几年大放异彩,几乎所有图像、语音识别领域的…

  • 八路抢答器单片机c语言程序_八路抢答器单片机c语言程序

    八路抢答器单片机c语言程序_八路抢答器单片机c语言程序该楼层疑似违规已被系统折叠隐藏此楼查看此楼改成开始前抢答蜂鸣器响,红灯亮#include#defineuintunsignedint#defineucharunsignedcharsbitSW1=P1^0;//******sbitSW2=P1^1;//*八*sbitSW3=P1^2;//*路*sbitSW4=P1^3;//*抢*sb…

    2022年10月20日
  • 爬了1000张清纯妹子私房照,我流鼻血了…[通俗易懂]

    爬了1000张清纯妹子私房照,我流鼻血了…[通俗易懂]想看漂亮小姐姐照片?不如试试爬虫批量下载,自己一个人在被窝里慢慢看

发表回复

您的电子邮箱地址不会被公开。

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