使用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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • docker快速安装fastdfs服务springboot访问

    docker快速安装fastdfs服务springboot访问拉取镜像dockerpullmorunchang/fastdfs运行tracker跟踪器dockerrun-d–nametracker–net=hostmorunchang/fastdfsshtracker.sh运行storage存储器【注意:修改IP为自己的IP端口不变】dockerrun-d–namestorage–net=host-eTRACKER_IP=192.168.61.200:22122-eGROUP_NAME=gr

    2022年6月24日
    25
  • lea指令!「建议收藏」

    lea指令!「建议收藏」
    最近在看linux-0.11内核,看到lea这个指令,google搜索了一下,转给大家,一起学习@!
     
    先看这个这个语法格式吧:
    对AT&T来说,寻址方式比较怪异,但又非常简洁,语法格式如下:segreg:base_address(offset_address,index,size) ;例子movl%eax,label1(,$2,$4)movl%ebx,(label2,$2,)movl%ecx,(%esp)
    其效果为

    2025年7月27日
    2
  • Hi3516A开发–视频接口[通俗易懂]

    Hi3516A开发–视频接口[通俗易懂]参看:几种常用的视频接口我们经常在家里的电视机、各种播放器上,视频会议产品和监控产品的编解码器的视频输入输出接口上看到很多视频接口,这些视频接口哪些是模拟接口、哪些是数字接口,哪些接口可以传输高清图像等,下面就做一个详细的介绍。  目前最基本的视频接口是复合视频接口、S-vidio接口;另外常见的还有色差接口、VGA接口、接口、HDMI接口、SDI接口。  1、复合

    2022年5月15日
    62
  • DNS负载均衡技术

    DNS负载均衡技术负载均衡技术能够平衡服务器集群中所有的服务器和请求应用之间的通信负载,根据实时响应时间进行判断,将任务交由负载最轻的服务器来处理,以实现真正的智能通信管理和最佳的服务器群性能,从而使网站始终保持运行和保证其可访问性。  为了充分利用现有服务器软件的种种优势,负载均衡最好是在服务器软件之外来完成。而最早使用的负载均衡技术是通过DNS服务中的随机名字解析来实现的。这就是通常所说的DNS负载均衡

    2022年7月14日
    15
  • Pandas笔记_python总结笔记

    Pandas笔记_python总结笔记创建数据随机数据创建一个Series,pandas可以生成一个默认的索引s=pd.Series([1,3,5,np.nan,6,8])通过numpy创建DataFrame,包含一个日期索引,以及标记的列dates=pd.date_range(‘20170101’,periods=6)df=pd.DataFrame(np.random.randn(6,…

    2022年8月26日
    4
  • rtp载荷类型_架体荷载

    rtp载荷类型_架体荷载 1简介在Internet上用分组传送话音的质量不够好的一个重要原因是比较高的丢包率。尤其在广域网中,这个问题相当突出。不幸的是,实时多媒体业务对于延时的要求相当严格,因此不大可能通过重传来解决丢包的问题。正是出于这个原因,大家提出用前向纠错(FEC)来解决Internet上的丢包问题[1][2]。尤其是对于传统纠错码如校验码、RS码、汉明码等的使用引起了很多人的注意。为了能够更好地应用这些纠错码

    2022年8月11日
    5

发表回复

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

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