How to Create Multihost and Multivm in Single Data Center using Cloudsim?
Share
Condition for Multihost and Multivm in Single Data Center using Cloudsim
Description: To create a single data center with multiple hosts and VMs in CloudSim, begin by initializing the CloudSim simulation environment. Define the data center's characteristics, such as architecture, OS, and cost parameters using DatacenterCharacteristics, and create a list of Host objects with specified resources like RAM, PEs, bandwidth, and storage. Instantiate Host objects and set up their VM scheduling policies (e.g., VmSchedulerTimeShared). Next, create Vm objects with specific configurations, including PEs, RAM, and bandwidth, and prepare them for assignment to the broker. Instantiate a DatacenterBroker to manage VM and cloudlet assignments. Submit the VMs to the broker, which will deploy them to hosts in the data center. Finally, start the simulation and observe how VMs are allocated across hosts.
Sample Code
package multiplebrokers;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
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;
public class MultiBrokerSample extends javax.swing.JFrame {
DatacenterBroker broker = null;
private static List cloudletList;
private static List vmlist;
/**
* Creates new form MultiBrokerSample
*/
public MultiBrokerSample() {
initComponents();
}
private static List createVM(int userId, int vms, int idShift) {
//Creates a container to store VMs. This list is passed to the broker later
LinkedList list = new LinkedList<>();
//VM Parameters
long size = MultiBrokerDesignpage.storagesize1; //image size (MB)
int ram = MultiBrokerDesignpage.VMram1; //vm memory (MB)
double mips = MultiBrokerDesignpage.Vmmips1;
long bw = MultiBrokerDesignpage.VMBandwidth1;
int pesNumber = MultiBrokerDesignpage.VMpes1; //number of cpus
String vmm = "Xen"; //VMM name
double min = 1;
Random rt = new Random();
//create VMs
Vm[] vm = new Vm[MultiBrokerDesignpage.NoofVm1];
for (int i = 0, c = 1; i < MultiBrokerDesignpage.NoofVm1 && c <= vms; i++, c++) {
System.out.println("*********** Random mips values for VM**********:" + rt.nextInt((int) mips));
vm[i] = new Vm(idShift + i, userId, (double)rt.nextInt((int) mips), pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());
list.add(vm[i]);
}
return list;
}
private static List createCloudlet(int userId, int cloudlets, int idShift) {
// Creates a container to store Cloudlets
LinkedList list = new LinkedList();
Random rt = new Random();
//cloudlet parameters
long length = MultiBrokerDesignpage.Cloudletlength1;
long fileSize = MultiBrokerDesignpage.Cloudletfilesize1;
long outputSize = MultiBrokerDesignpage.Cloudletoutputsize1;
int pesNumber = MultiBrokerDesignpage.NoOfpes1;
UtilizationModel utilizationModel = new UtilizationModelFull();
Cloudlet[] cloudlet = new Cloudlet[MultiBrokerDesignpage.Noofcloudlet1];
for (int i = 0,c=0; i < MultiBrokerDesignpage.Noofcloudlet1&&c+) {
System.out.println("************* Random values for cloudlet length********:"+rt.nextInt((int) length));
cloudlet[i] = new Cloudlet(idShift + i,(long)rt.nextInt((int) length), pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);
// setting the owner of these Cloudlets
cloudlet[i].setUserId(userId);
list.add(cloudlet[i]);
}
return list;
}
@SuppressWarnings("unchecked")
// //GEN- BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
btnConfiguration = new javax.swing.JButton();
btnDatacenterCreation = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
btnConfiguration.setText("Configuration");
btnConfiguration.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnConfigurationActionPerformed(evt);
}
});
btnDatacenterCreation.setText("DatacenterCreation");
btnDatacenterCreation.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDatacenterCreationActionPerformed(evt);
}
});
jButton1.setText("Simulation");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI NG, false)
.addComponent(btnConfiguration, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnDatacenterCreation, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(59, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(btnConfiguration)
.addGap(18, 18, 18)
.addComponent(btnDatacenterCreation)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addContainerGap(48, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(43, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(29, Short.MAX_VALUE))
);
pack();
}// //GEN-END:initComponents
private void btnDatacenterCreationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDatacenterCreationActionPerformed
// TODO add your handling code here:
try {
// First step: Initialize the CloudSim package. It should be called before creating any entities.
int num_user = 1; // number of grid users
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false; // mean trace events
// Initialize the CloudSim library
CloudSim.init(num_user, calendar, trace_flag);
// Second step: Create Datacenters
Datacenter datacenter1 = createDatacenter("Datacenter_0");
//Third step: Create Broker
broker = createBroker();
int brokerId = broker.getId();
//Fourth step: Create VMs and Cloudlets and send them to broker
vmlist = createVM(brokerId, MultiBrokerDesignpage.NoofVm1, 0); //creating 40 vms
cloudletList = createCloudlet(brokerId, MultiBrokerDesignpage.Noofcloudlet1, 0); // creating 40 cloudlets
broker.submitVmList(vmlist);
broker.submitCloudletList(cloudletList);
// Fifth step: Starts the simulation
} catch (Exception e) {
Log.printLine("The simulation has been terminated due to an unexpected error");
}
}//GEN-LAST:event_btnDatacenterCreationActionPerformed
private void btnConfigurationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfigurationActionPerformed
MultiBrokerDesignpage multibroker = new MultiBrokerDesignpage();
multibroker.setVisible(true);
}//GEN-LAST:event_btnConfigurationActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_jButton1ActionPerformed
// TODO add your handling code here
CloudSim.startSimulation();
// Final step: Print results when simulation is over
List newList = broker.getCloudletReceivedList();
printCloudletList(newList);
CloudSim.stopSimulation();
}//GEN-LAST:event_jButton1ActionPerformed
private static Datacenter createDatacenter(String name) {
// Here are the steps needed to create a PowerDatacenter:
// 1. We need to create a list to store one or more
// Machines
List hostList = new ArrayList<>();
// 2. A Machine contains one or more PEs or CPUs/Cores. Therefore, should
// create a list to store these PEs before creating
// a Machine.
List peList1 = new ArrayList<>();
int mips = MultiBrokerDesignpage.Hostmips1;
// 3. Create PEs and add these into the list.
//for a quad-core machine, a list of 4 PEs is required:
for (int i = 1, c = 1; i <= MultiBrokerDesignpage.Noofhost1 && c <= MultiBrokerDesignpage.Noofhost1; i++, c++) {
Random rt = new Random();
System.out.println("******* Random mips values for Host*******:" + rt.nextInt(mips));
peList1.add(new Pe(i, new PeProvisionerSimple(rt.nextInt(mips))));
}
//Another list, for a dual-core machine
//4. Create Hosts with its id and list of PEs and add them to the list of machines
int hostId;
int ram = MultiBrokerDesignpage.Hostram1;
long storage = MultiBrokerDesignpage.Hoststorage1; //host storage
int bw = MultiBrokerDesignpage.Hostbandwidth1;
//in this example, the VMAllocatonPolicy in use is SpaceShared. It means that only one VM
//is allowed to run on each Pe. As each Host has only one Pe, only one VM can run on each Host.
for (int i = 1; i <= (MultiBrokerDesignpage.Noofhost1); i++) {
hostList.add(
new Host(
i,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList1,
new VmSchedulerTimeShared(peList1)
)
);
// This is our first 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.1; // the cost of using storage in this resource
double costPerBw = 0.1; // the cost of using bw in this resource
LinkedList storageList = new LinkedList<>(); //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) {
}
return datacenter;
}
private static void printCloudletList(List 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()));
}
}
}
private DatacenterBroker createBroker() {
DatacenterBroker broker = null;
try {
broker = new DatacenterBroker("Broker");
} catch (Exception e) {
return null;
}
return broker;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(() -> {
new MultiBrokerSample().setVisible(true);
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnConfiguration;
private javax.swing.JButton btnDatacenterCreation;
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
// End of variables declaration//GEN-END:variables
}
Step 1
To set the environment for multiple host and multiple virtual machine creation.
Step 2
To put the Host, Virtual machine and cloudlet configurations.
Step 3
To create a datacenter for multiple host and multiple virtual machine.
Step 4
To simulate the result of multiple host and multiple virtual machine.
Step 5
To get the results for Host, virtual machines and cloudlets.
Step 6
To get the cloudlet results of creating multiple hosts and multiple virtual machines.