一、認識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包。

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