How to Calculate the Energy Power Consumption of Each Host?
Share
Rendition for Calculate the Energy Power Consumption of Each Host
Description: To calculate energy or power consumption in CloudSim, to use the PowerHost class with power models like PowerModelSpecPower, which calculates power based on CPU utilization. To retrieve the power consumption at any time using methods like getPowerConsumption. To calculate total energy, multiply the power consumption by the host's active duration. This approach helps track and optimize energy usage in cloud environments.
Sample Code
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.power.*;
import org.cloudbus.cloudsim.provisioners.*;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.power.models.PowerModelLinear;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class PowerConsumption {
public static void main(String[] args) {
try {
// Initialize CloudSim
int numUsers = 1;
Calendar calendar = Calendar.getInstance();
boolean traceFlag = false;
CloudSim.init(numUsers, calendar, traceFlag);
// Create Datacenter
Datacenter datacenter = createDatacenter("Datacenter_1");
// Create Broker
DatacenterBroker broker = new DatacenterBroker("Broker_1");
// Create VM List
List vmlist = new ArrayList<>();
int vmid = 0;
int mips = 1000;
long size = 10000; // image size (MB)
int ram = 512; // VM memory (MB)
long bw = 1000; // bandwidth
int pesNumber = 1; // number of CPUs
Vm vm = new Vm(vmid, broker.getId(), mips, pesNumber, ram, bw, size, "Xen", new CloudletSchedulerTimeShared());
vmlist.add(vm);
broker.submitVmList(vmlist);
// Create Cloudlets
List cloudletList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Cloudlet cloudlet = new Cloudlet(
i, 20000, pesNumber, 300, 300,
new UtilizationModelFull(), new UtilizationModelFull(), new UtilizationModelFull()
);
cloudlet.setUserId(broker.getId());
cloudletList.add(cloudlet);
}
broker.submitCloudletList(cloudletList);
// Start simulation
CloudSim.startSimulation();
// Stop simulation
CloudSim.stopSimulation();
// Display Results
for (Cloudlet cloudlet : broker.getCloudletReceivedList()) {
System.out.println("Cloudlet ID: " + cloudlet.getCloudletId() + " Status: " + cloudlet.getStatus());
}
// Calculate Energy Consumption
double totalEnergy = 0.0;
for (Host host : datacenter.getHostList()) {
if (host instanceof PowerHost) {
PowerHost powerHost = (PowerHost) host;
// Calculate energy consumption using power model and utilization
double time = CloudSim.clock(); // Total simulation time in seconds
double utilization = 0.8; // Simulated utilization
double power = powerHost.getPowerModel().getPower(utilization);
// Energy consumption is power * time (convert time to hours)
double hostEnergy = power * (time / 3600);
totalEnergy += hostEnergy;
System.out.printf("Host ID: %d, Utilization: %.1f, Power: %.1f W, Energy: %.4f kWh%n",
powerHost.getId(), utilization, power, hostEnergy);
}
}
// Print Total Energy Consumption
System.out.printf("Total Energy Consumption: %.4f kWh%n", totalEnergy);
} catch (Exception e) {
}
}
private static Datacenter createDatacenter(String name) throws Exception {
List hostList = new ArrayList<>();
// Create a host with PowerModelLinear
int hostId = 0;
int ram = 2048; // host memory (MB)
long storage = 1000000; // host storage
int bw = 10000; // bandwidth
int pesNumber = 1; // number of CPUs
PowerModelLinear powerModel = new PowerModelLinear(100, 250); // Idle=100W, Max=250W
List peList = new ArrayList<>();
for (int i = 0; i < pesNumber; i++) {
peList.add(new Pe(i, new PeProvisionerSimple(1000))); // 1000 MIPS per PE
}
Host host = new PowerHost(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerTimeShared(peList),
powerModel
);
hostList.add(host);
// Create Datacenter Characteristics
String arch = "x86";
String os = "Linux";
String vmm = "Xen";
double timeZone = 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.001; // the cost of using storage in this resource
double costPerBw = 0.0; // the cost of using bandwidth in this resource
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, timeZone, cost, costPerMem, costPerStorage, costPerBw
);
return new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), new ArrayList<>(), 0);
}
}