How to Classify the Virtual Machine and Host in Cloudsim?
Share
Condition for Classify the Virtual Machine and Host in Cloudsim
Description: In CloudSim, VMs and Hosts are classified by their characteristics, such as PEs, RAM, bandwidth, and storage, to represent different types optimized for specific workloads. Hosts are also categorized based on these capabilities and their VM scheduling policies (e.g., VmSchedulerTimeShared or VmSchedulerSpaceShared). This classification helps create more realistic simulations and enables efficient resource management and tailored scheduling strategies.
Sample Code
package vmclassification;
import java.text.DecimalFormat;
import java.util.*;
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.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.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 VmClassificationsample extends javax.swing.JFrame {
private static List cloudletList;
private static List vmlist;
DatacenterBroker broker;
static Random rnd = new Random();
static Vm1 vm;
static LinkedList hostList = new LinkedList();
// public static List VMtype = new ArrayList();
/**
* Creates new form VmClassificationsample
*/
public VmClassificationsample() {
initComponents();
}
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
// ArrayList 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();
// 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; i < 9; i++) {
// }
//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 = Configuration.Hostram1;
long storage = Configuration.Hoststorage1; //host storage
int bw = Configuration.Hostbandwidth1;
int mips = Configuration.Hostmips1;
ModifiedHost host;
// int mips1=rt.nextInt(mips);
//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 <= (Configuration.Noofhost1); i++) {
double mips1 = (double) (1000 + rnd.nextInt((int) mips));
peList1.add(new Pe(0, new PeProvisionerSimple(mips1)));
host= new ModifiedHost(
i,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList1,
new VmSchedulerTimeShared(peList1));
hostList.add(host);
if (mips1 < 2000) {
host.setHostType(" small");
} else if (mips1 >= 2000 && mips1 < 3000) {
host.setHostType("mediam");
} else {
host.setHostType("large");
}
// 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) {
e.printStackTrace();
}
return datacenter;
}
private static LinkedList 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 = Configuration.storagesize1; //image size (MB)
int ram = Configuration.VMram1; //vm memory (MB)
double mips = Configuration.Vmmips1;
int pesNumber = Configuration.VMpes1; //number of cpus
String vmm = "Xen"; //VMM name
long bw = Configuration.VMBandwidth1;
for (int i = 0; i < vms; i++) {
double mips1 = (double) (500 + rnd.nextInt((int) mips));
System.out.println("**** Random value for mips****" + mips1);
vm = new Vm1(idShift + i, userId, mips1, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());
list.add(vm);
if (mips1 < 2000) {
vm.setVmType(" small");
} else if (mips1 >= 2000 && mips1 < 3000) {
vm.setVmType("mediam");
} else {
vm.setVmType("large");
}
}
return list;
}
private static LinkedList createCloudlet(int userId, int cloudlets, int idShift) {
// Creates a container to store Cloudlets
LinkedList list = new LinkedList();
//cloudlet parameters
long length = Configuration.Cloudletlength1;
long fileSize = Configuration.Cloudletfilesize1;
long outputSize = Configuration.Cloudletoutputsize1;
int pesNumber = Configuration.NoOfpes1;
UtilizationModel utilizationModel = new UtilizationModelFull();
Cloudlet[] cloudlet = new Cloudlet[cloudlets];
for (int i = 0; i < cloudlets; i++) {
cloudlet[i] = new Cloudlet(idShift + i, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);
// setting the owner of these Cloudlets
cloudlet[i].setUserId(userId);
list.add(cloudlet[i]);
}
return list;
// return Cloudletmap.values();
}
//We strongly encourage users to develop their own broker policies, to submit vms and cloudlets according
//to the specific rules of the simulated scenario
private static DatacenterBroker createBroker() {
DatacenterBroker broker = null;
try {
broker = new DatacenterBroker("Broker");
} catch (Exception e) {
e.printStackTrace();
return null;
}
return broker;
}
// private static LinkedList 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 = Configuration.storagesize1; //image size (MB)
// int ram = Configuration.VMram1; //vm memory (MB)
// double mips = BrokerConfiguration.Vmmips1;
// long bw = Configuration.VMBandwidth1;
// int pesNumber = Configuration.VMpes1; //number of cpus
// String vmm = "Xen"; //VMM name
// //create VMs
// Vm[] vm = new Vm[vms];
// for (int i = 0; i < vms; i++) {
// vm[i] = new Vm(idShift + i, userId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());
// list.add(vm[i]);
// }
//// list.remove(n);
// return list;
// }
//We strongly encourage users to develop their own broker policies, to submit vms and cloudlets according
//to the specific rules of the simulated scenario
/**
* Prints the Cloudlet objects
*
* @param list list of Cloudlets
*/
private static void printCloudletList(List list) {
int size = list.size();
Cloudlet cloudlet = null;
String indent = " ";
Log.printLine();
Log.printLine("========== OUTPUT ==========");
Log.printLine(indent + "Cloudlet ID" + indent + "STATUS" + indent
+ "Data center ID" + indent + "VM ID" + indent + 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);
// +VMtype.get(cloudlet.getVmId()).Vmtype
if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) {
Log.print("SUCCESS");
Log.printLine(indent + indent + cloudlet.getResourceId() + indent + indent + indent + cloudlet.getVmId()
+ indent + indent + indent + indent + dft.format(cloudlet.getActualCPUTime()) + indent + indent + dft.format(cloudlet.getExecStartTime())
+ indent + indent + dft.format(cloudlet.getFinishTime()));
}
}
Log.printLine("========== OUTPUT ==========");
Log.printLine(indent + indent+"vmid"+indent + indent+"mips"+indent + indent+"type");
for(int i=0;i
{
// System.out.println(vmlist.get(cloudlet.getVmId()));
// System.out.println(vmlist.get(i).mips1);
// System.out.println(vmlist.get(cloudlet.getVmId()).getVmType());
Log.printLine(indent + indent+vmlist.get(i).getId()+indent + indent+vmlist.get(i).getMips()+indent + indent+vmlist.get(i).getVmType());
}
Log.printLine("========== OUTPUT ==========");
Log.printLine(indent + indent+"Hostid"+indent + indent+"mips"+indent + indent+"type");
for(int i=0;i
{
Log.printLine(indent + indent+hostList.get(i).getId()+indent + indent+hostList.get(i).getPeList().get(i).getMips()+indent + indent+hostList.get(i).getHostType());
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// //GEN- BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
btnConfiguration = new javax.swing.JButton();
btnVmCreation = new javax.swing.JButton();
btnSimulation = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
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);
}
});
btnVmCreation.setText("Datacenter Creation");
btnVmCreation.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnVmCreationActionPerformed(evt);
}
});
btnSimulation.setText("Simulation");
btnSimulation.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSimulationActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI NG)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(85, 85, 85)
.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(btnVmCreation, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnSimulation, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(90, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(btnConfiguration)
.addGap(29, 29, 29)
.addComponent(btnVmCreation)
.addGap(28, 28, 28)
.addComponent(btnSimulation)
.addGap(34, 34, 34)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(45, 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(32, 32, 32)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(18, Short.MAX_VALUE))
);
pack();
}// //GEN-END:initComponents
private void btnConfigurationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfigurationActionPerformed
// TODO add your handling code here:
Configuration configuration = new Configuration();
configuration.setVisible(true);
}//GEN-LAST:event_btnConfigurationActionPerformed
private void btnVmCreationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVmCreationActionPerformed
// TODO add your handling code here:
// VMmipsConfiguration vmmipsConfiguration = new VMmipsConfiguration();
// vmmipsConfiguration.setVisible(true);
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();
boolean trace_flag = false; // mean trace events
// Initialize the CloudSim library
CloudSim.init(num_user, calendar, trace_flag);
// Second step: Create Datacenters
Datacenter datacenter0 = 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, Configuration.NoofVm1, 0); //creating 40 vms
cloudletList = createCloudlet(brokerId, Configuration.Noofcloudlet1, 0); // creating 40 cloudlets
broker.submitVmList(vmlist);
broker.submitCloudletList(cloudletList);
} catch (Exception e) {
Log.printLine("The simulation has been terminated due to an unexpected error");
}
}//GEN-LAST:event_btnVmCreationActionPerformed
private void btnSimulationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimulationActionPerformed
CloudSim.startSimulation();
List newList1 = broker.getCloudletReceivedList();
jTextArea1.append(newList1+"\n");
CloudSim.stopSimulation();
printCloudletList(newList1);
}//GEN-LAST:event_btnSimulationActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(VmClassificationsample.class.getName()).log(java .util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(VmClassificationsample.class.getName()).log(java
.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(VmClassificationsample.class.getName()).log(java .util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(VmClassificationsample.class.getName()).log(java .util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new VmClassificationsample().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnConfiguration;
private javax.swing.JButton btnSimulation;
private javax.swing.JButton btnVmCreation;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration//GEN-END:variables
}
Step 1
To set the environment for virtual machine and host classifications.
Step 2
To put the Host, Virtual machine, cloudlet and broker configurations.
Step 3
To create a datacenter for virtual machine and host classifications.
Step 4
To simulate the result of virtual machine and host classifications.
Step 5
To get the results for Host and virtual machines.
Step 6
To get the list of cloudlets received results.
Step 7
To get the cloudlet results for virtual machine and host classifications.