Description: To create a power model for a host in CloudSim, you use a power model class like PowerModelCubic, which calculates power consumption based on CPU utilization. You define the model by specifying the maximum power (max) and idle power factor (k). The power model is then assigned to the host via the PowerHost class, which also includes resources like RAM, bandwidth, and processing elements (PEs). This setup enables CloudSim to simulate the host's dynamic power consumption based on its utilization.
Sample Code
1. PowerModelExample.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.power.PowerHost;
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 PowerModelExample {
/**
* The cloudlet list.
*/
private static List cloudletList;
/**
* The vmlist.
*/
private static List 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
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 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 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 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) {
// List to store Hosts
List hostList = new ArrayList();
// Create PEs (Processing Elements)
List peList = new ArrayList();
int mips = 1000;
peList.add(new Pe(0, new PeProvisionerSimple(mips))); // Adding PE to the list
// Host parameters
int hostId = 0;
int ram = 2048; // in MB
long storage = 1000000; // in bytes
int bw = 10000; // in bw units
// Create the PowerModel instance
ModelPower powerModel = new ModelPower(500, 0.5); // Max power and idle power percentage
// Call the getPower method and print the value of 's' for different utilization values
double utilization = 0.5; // Example CPU utilization (50%)
double powerConsumption = powerModel.getPower(utilization); // Get power consumption
System.out.println("Power consumption for utilization " + utilization + ": " + powerConsumption + " watts");
// Create Host with power model
PowerHost host = new PowerHost(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerTimeShared(peList),
powerModel // Assign power model to the host
);
hostList.add(host); // Add host to the list of hosts
// Datacenter characteristics
String arch = "x86";
String os = "Linux";
String vmm = "Xen";
double timeZone = 10.0;
double cost = 3.0;
double costPerMem = 0.05;
double costPerStorage = 0.001;
double costPerBw = 0.0;
LinkedList storageList = new LinkedList();
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, timeZone, cost, costPerMem, costPerStorage, costPerBw
);
// Create the Datacenter object with custom allocation policy
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 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()));
}
}
}
}
2. ModelPower.java:
import org.cloudbus.cloudsim.power.models.PowerModelCubic;
public class ModelPower extends PowerModelCubic {
private double max = 1.0; // Maximum power consumption (could be in watts)
private double k = 0.5; // Scaling factor for idle power
// Constructor for the ModelPower class
/**
*
* @param max
* @param k
*/
public ModelPower(double max, double k) {
// Calling the constructor of PowerModelCubic to set up power model
super(max, k);
this.max = max;
this.k = k;
}
/**
* Method to calculate power consumption based on the given CPU utilization.
*
* @param utilization The CPU utilization of the host (between 0 and 1)
* @return Power consumption in watts
* @throws IllegalArgumentException If utilization is out of range (not between 0 and 1)
*/
public double getPower(double utilization) throws IllegalArgumentException {
// Ensure utilization is within valid range
if (utilization < 0 || utilization > 1) {
throw new IllegalArgumentException("Utilization value must be between 0 and 1");
}
// When utilization is zero, return zero power
if (utilization == 0) {
return 0;
}
// Example power model equation
// s = k * max + (1 - k) * max * utilization
// This means when the host is idle (utilization = 0), it uses k * max power,
// and as the utilization increases, the power consumption increases accordingly.
double s = k * max + (1 - k) * max * utilization;
// Print the value of s to the console
System.out.println("Calculated Power Consumption (s) for utilization " + utilization + " is: " + s + " watts");
return s;
}
// Getter and Setter for max power
public double getMax() {
return max;
}
public void setMax(double max) {
this.max = max;
}
// Getter and Setter for k (idle power factor)
public double getK() {
return k;
}
public void setK(double k) {
this.k = k;
}
}