CloudSim的学习和使用1

一、认识CloudSim

1.1 什么是CloudSim

CloudSim: A Framework For Modeling And Simulation Of Cloud Computing Infrastructures And Services
一个建模与仿真云计算基础设计和服务的框架。

1.2 去哪找CloudSim的资料

序号 类别 备注
1 CloudSim官网 英文
2 CloudSim github 英文
3 Cloudsim Tutorials在线教程 英文

二、运行CloudSim样例

2.1 下载CloudSim包

去Github网站上下载CloudSim的zip包
download

2.2 运行示例代码

解压CloudSim.zip包,并进入jars目录,打开cmd,
执行:java -classpath jars\cloudsim-4.0.jar;examples org.cloudbus.cloudsim.examples.CloudSimExample1可以运行CloudSimExample1,
运行结果如下图。

CloudSimExample1中的1可以改成1-8中任意数字即可运行对应示例程序,
比如如下图运行示例代码4,

下载的zip包中有一个examples.txt讲述了详细的使用方法,
具体内容如下:

Running the CloudSim examples
-----------------------------

To run the CloudSim examples you need to do the following steps.

In Windows:

1. cd <PATH TO CLOUDSIM PACKAGE>\jars
2. java -classpath cloudsim-<VERSION>.jar;cloudsim-examples-<VERSION>.jar org.cloudbus.cloudsim.examples.CloudSimExample<EXAMPLE NUMBER>

In Unix/Linux:

1. cd <PATH TO CLOUDSIM PACKAGE>/jars
2. java -classpath cloudsim-<VERSION>.jar:cloudsim-examples-<VERSION>.jar org.cloudbus.cloudsim.examples.CloudSimExample<EXAMPLE NUMBER>

Where you need to replace:

<PATH TO CLOUDSIM PACKAGE> - by the path to a directory where you have unpacked the CloudSim package
<VERSION> - by the version of the downloaded CloudSim package
<EXAMPLE NUMBER> - by the of number of the example you want to run


CloudSim examples source code
-----------------------------

You can find the source code of the examples in <PATH TO CLOUDSIM PACKAGE>/examples/org/cloudbus/cloudsim/examples/


Compiling and running examples
------------------------------

To compile and run an example (let's say org.cloudbus.cloudsim.examples.CloudSimExample1) you need to do the following steps:

In Windows:

1. cd <PATH TO CLOUDSIM PACKAGE>
2. javac -classpath jars\cloudsim-<VERSION>.jar;examples examples\org\cloudbus\cloudsim\examples\CloudSimExample1.java
3. java -classpath jars\cloudsim-<VERSION>.jar;examples org.cloudbus.cloudsim.examples.CloudSimExample1

In Unix/Linux:

1. cd <PATH TO CLOUDSIM PACKAGE>
2. javac -classpath jars/cloudsim-<VERSION>.jar:examples examples/org/cloudbus/cloudsim/examples/CloudSimExample1.java
3. java -classpath jars/cloudsim-<VERSION>.jar:examples org.cloudbus.cloudsim.examples.CloudSimExample1


Compiling and running examples from the power package
-----------------------------------------------------

To compile and run an example (let's say org.cloudbus.cloudsim.examples.power.planetlab.LrMc) you need to do the following steps:

In Windows:

1. Download Michael Thomas Flanagan's Java Scientific Library from http://www.ee.ucl.ac.uk/~mflanaga/java/
2. Copy flanagan.jar to <PATH TO CLOUDSIM PACKAGE>/jars/
3. cd <PATH TO CLOUDSIM PACKAGE>
4. javac -classpath jars\cloudsim-<VERSION>.jar;jars\flanagan.jar;examples examples\org\cloudbus\cloudsim\examples\power\planetlab\LrMc.java
5. java -classpath jars\cloudsim-<VERSION>.jar;jars\flanagan.jar;examples org.cloudbus.cloudsim.examples.power.planetlab.LrMc

In Unix/Linux:

1. cd <PATH TO CLOUDSIM PACKAGE>
2. chmod +x ./install-flanagan.sh
3. ./install-flanagan.jar
4. javac -classpath jars/cloudsim-<VERSION>.jar:jars/flanagan.jar:examples examples/org/cloudbus/cloudsim/examples/power/planetlab/LrMc.java
5. java -classpath jars/cloudsim-<VERSION>.jar:jars/flanagan.jar:examples org.cloudbus.cloudsim.examples.power.planetlab.LrMc


Description of the CloudSim examples
------------------------------------

Here is the description of what each example does:

CloudSimExample1.java : shows how to create a datacenter with one host and run one cloudlet on it.

CloudSimExample2.java : shows how to create a datacenter with one host and run two cloudlets on it.
			The cloudlets run in VMs with the same MIPS requirements. The cloudlets will take the same 
			time to complete the execution.

CloudSimExample3.java : shows how to create a datacenter with two hosts and run two cloudlets on it.
			The cloudlets run in VMs with different MIPS requirements. The cloudlets will take different 
			time to complete the execution depending on the requested VM performance.

CloudSimExample4.java : shows how to create two datacenters with one host each and run two cloudlets on them.

CloudSimExample5.java : shows how to create two datacenters with one host each and run cloudlets of two users on them. 

CloudSimExample6.java : shows how to create scalable simulations.

CloudSimExample7.java : shows how to pause simulations.

CloudSimExample8.java : shows how to add entities in run time.

network: this package contains examples on how to run simulation with network simulation.

power: this package contains examples on how to use CloudSim's power-aware features.

三、修改示例代码测试

3.1 下载带有源代码的zip包

3.2 在eclipse中新建项目并导入cloudsim

3.2 修改代码使其创建10个虚拟机并仿真


具体代码如下:

package org.cloudbus.cloudsim.examples;


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;

/**
 * 创建一个数据中心,10个虚拟主机,10个云任务在上面运行
 *
 */
public class MyExample1 {

	/** 云任务列表. */
	private static List<Cloudlet> cloudletList;

	/** 虚拟机列表 */
	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("开始仿真...");

		try {
			// First step: Initialize the CloudSim package. It should be called
			// before creating any entities.
			// 第一步,在创建任务实体之前实例化CloudSim包
			
			int num_user = 1; // 云用户的数量
			Calendar calendar = Calendar.getInstance();
			boolean trace_flag = false; // 是否跟踪事件

			// Initialize the CloudSim library
			// 实例化CloudSim库
			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
			// 第二部,创建数据中心,在CloudSim中数据中心是资源提供者,
			// 我们需要在他们之中运行仿真
			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;  // 虚拟机ID
			int mips = 100;  // 速率
			long size = 10000; // image size (MB)  镜像大小
			int ram = 512; // vm memory (MB)  虚拟机内存大小
			long bw = 1000;  // 虚拟机带宽
			int pesNumber = 1; // number of cpus  cpu数量
			String vmm = "Xen"; // VMM name  架构
			
			// Fifth step: Create one Cloudlet
			// 第五步,创建一个云任务列表
			cloudletList = new ArrayList<Cloudlet>();
			int CloudletId = 0;
			
			// FOR循环创建10个虚拟机
			for (int i = 0; i < 10; i++) {
				// VM description
				vmid += 1;
				mips += 100;
				size = 1000; // image size (MB)
				ram = 512; // vm memory (MB)
				bw = 1000;
				pesNumber = 1; // number of cpus
				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);

				// Cloudlet properties
				CloudletId += 1;
				long length = 400000;
				long fileSize = 300;
				long outputSize = 300;
				UtilizationModel utilizationModel = new UtilizationModelFull();
	
				// 实例化云任务
				Cloudlet cloudlet = new Cloudlet(CloudletId, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);
				cloudlet.setUserId(brokerId);
				cloudlet.setVmId(vmid);
	
				// add the cloudlet to the list
				// 将云任务添加进列表
				cloudletList.add(cloudlet);
			}
			
			// submit vm list to the broker
			broker.submitVmList(vmlist);
			// 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("MAIN函数结束");
		} catch (Exception e) {
			e.printStackTrace();
			Log.printLine("不知道啥玩意错了");
		}
	}

	/**
	 * 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 = 100000;

		// 3. Create PEs and add these into a list.
		// 实例化PE并添加进列表
		peList.add(new Pe(0, new PeProvisionerSimple(mips))); // need to store Pe id and MIPS Rating

		// 4. Create Host with its id and list of PEs and add them to the list
		// of machines
		// 实例化一个物理机器并放置进机器列表
		int hostId = 0;
		int ram = 20480; // 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 = 8.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("成功");

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

}
posted @   那个白熊  阅读(3132)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示