Commit 06322462 authored by Anton Beloglazov's avatar Anton Beloglazov

Refactored power VM allocation policies, removed old policies, reimplemented the…

Refactored power VM allocation policies, removed old policies, reimplemented the DVFS and single threshold policies
parent 4529b632
...@@ -153,7 +153,7 @@ public class CloudSimExample2 { ...@@ -153,7 +153,7 @@ public class CloudSimExample2 {
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Log.printLine("Unwanted errors happen"); Log.printLine("The simulation has been terminated due to an unexpected error");
} }
} }
......
...@@ -152,7 +152,7 @@ public class CloudSimExample3 { ...@@ -152,7 +152,7 @@ public class CloudSimExample3 {
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Log.printLine("Unwanted errors happen"); Log.printLine("The simulation has been terminated due to an unexpected error");
} }
} }
......
...@@ -150,7 +150,7 @@ public class CloudSimExample4 { ...@@ -150,7 +150,7 @@ public class CloudSimExample4 {
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Log.printLine("Unwanted errors happen"); Log.printLine("The simulation has been terminated due to an unexpected error");
} }
} }
......
...@@ -154,7 +154,7 @@ public class CloudSimExample5 { ...@@ -154,7 +154,7 @@ public class CloudSimExample5 {
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Log.printLine("Unwanted errors happen"); Log.printLine("The simulation has been terminated due to an unexpected error");
} }
} }
......
...@@ -152,7 +152,7 @@ public class CloudSimExample6 { ...@@ -152,7 +152,7 @@ public class CloudSimExample6 {
catch (Exception e) catch (Exception e)
{ {
e.printStackTrace(); e.printStackTrace();
Log.printLine("Unwanted errors happen"); Log.printLine("The simulation has been terminated due to an unexpected error");
} }
} }
......
...@@ -190,7 +190,7 @@ public class CloudSimExample7 { ...@@ -190,7 +190,7 @@ public class CloudSimExample7 {
catch (Exception e) catch (Exception e)
{ {
e.printStackTrace(); e.printStackTrace();
Log.printLine("Unwanted errors happen"); Log.printLine("The simulation has been terminated due to an unexpected error");
} }
} }
......
...@@ -154,7 +154,7 @@ public class CloudSimExample8 { ...@@ -154,7 +154,7 @@ public class CloudSimExample8 {
catch (Exception e) catch (Exception e)
{ {
e.printStackTrace(); e.printStackTrace();
Log.printLine("Unwanted errors happen"); Log.printLine("The simulation has been terminated due to an unexpected error");
} }
} }
......
...@@ -146,7 +146,7 @@ public class NetworkExample1 { ...@@ -146,7 +146,7 @@ public class NetworkExample1 {
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Log.printLine("Unwanted errors happen"); Log.printLine("The simulation has been terminated due to an unexpected error");
} }
} }
......
...@@ -171,7 +171,7 @@ public class NetworkExample2 { ...@@ -171,7 +171,7 @@ public class NetworkExample2 {
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Log.printLine("Unwanted errors happen"); Log.printLine("The simulation has been terminated due to an unexpected error");
} }
} }
......
...@@ -177,7 +177,7 @@ public class NetworkExample3 { ...@@ -177,7 +177,7 @@ public class NetworkExample3 {
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Log.printLine("Unwanted errors happen"); Log.printLine("The simulation has been terminated due to an unexpected error");
} }
} }
......
...@@ -138,7 +138,7 @@ public class NetworkExample4 { ...@@ -138,7 +138,7 @@ public class NetworkExample4 {
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Log.printLine("Unwanted errors happen"); Log.printLine("The simulation has been terminated due to an unexpected error");
} }
} }
......
...@@ -127,10 +127,10 @@ public class CloudSim { ...@@ -127,10 +127,10 @@ public class CloudSim {
// set all the above entity IDs // set all the above entity IDs
cisId = cis.getId(); cisId = cis.getId();
} catch (IllegalArgumentException s) { } catch (IllegalArgumentException s) {
Log.printLine("CloudSim.init(): Unwanted errors happen"); Log.printLine("CloudSim.init(): The simulation has been terminated due to an unexpected error");
Log.printLine(s.getMessage()); Log.printLine(s.getMessage());
} catch (Exception e) { } catch (Exception e) {
Log.printLine("CloudSim.init(): Unwanted errors happen"); Log.printLine("CloudSim.init(): The simulation has been terminated due to an unexpected error");
Log.printLine(e.getMessage()); Log.printLine(e.getMessage());
} }
} }
......
package org.cloudbus.cloudsim.network.datacenter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.TreeSet;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.core.CloudSimTags;
import org.cloudbus.cloudsim.core.SimEntity;
import org.cloudbus.cloudsim.core.SimEvent;
import org.cloudbus.cloudsim.core.predicates.PredicateType;
import org.cloudbus.cloudsim.lists.VmList;
public class Switch extends SimEntity {
// switch level
public int id;
public int level;// three levels
public int datacenterid;
public Map<Integer, List<NetworkPacket>> uplinkswitchpktlist;
public Map<Integer, List<NetworkPacket>> downlinkswitchpktlist;
public Map<Integer, NetworkHost> hostlist;
public List<Switch> uplinkswitches;
public List<Switch> downlinkswitches;
public Map<Integer, List<NetworkPacket>> packetTohost;
int type;// edge switch or aggregation switch
public double uplinkbandwidth;
public double downlinkbandwidth;
public double latency;
public double numport;
public NetworkDatacenter dc;
public SortedMap<Double, List<NetworkHost>> fintimelistHost = new TreeMap<Double, List<NetworkHost>>();// something
// is
// running
// on
// these
// hosts
public SortedMap<Double, List<NetworkVm>> fintimelistVM = new TreeMap<Double, List<NetworkVm>>();// something
// is
// running
// on
// these
// hosts
public ArrayList<NetworkPacket> pktlist;
public List<Vm> BagofTaskVm = new ArrayList<Vm>();
public double switching_delay;
public Map<Integer, NetworkVm> Vmlist;
public Switch(String name, int level, NetworkDatacenter dc) {
super(name);
this.level = level;
/*
* if(level==Constants.EDGE_LEVEL) { hostlist=new
* HashMap<Integer,HPCHost>(); uplinkswitchpktlist=new
* HashMap<Integer,List<HostPacket>>(); packetTohost=new
* HashMap<Integer,List<HostPacket>>();
* uplinkbandwidth=Constants.BandWidthEdgeAgg;
* downlinkbandwidth=Constants.BandWidthEdgeHost;
* latency=Constants.SwitchingDelayEdge;
* numport=Constants.EdgeSwitchPort; uplinkswitches=new
* ArrayList<Switch>();
*
* } if(level==Constants.Agg_LEVEL) { downlinkswitchpktlist=new
* HashMap<Integer,List<HostPacket>>(); uplinkswitchpktlist=new
* HashMap<Integer,List<HostPacket>>();
* uplinkbandwidth=Constants.BandWidthAggRoot;
* downlinkbandwidth=Constants.BandWidthEdgeAgg;
* latency=Constants.SwitchingDelayAgg; numport=Constants.AggSwitchPort;
* uplinkswitches=new ArrayList<Switch>(); downlinkswitches=new
* ArrayList<Switch>(); } if(level==Constants.ROOT_LEVEL) {
* downlinkswitchpktlist=new HashMap<Integer,List<HostPacket>>();
* downlinkswitches=new ArrayList<Switch>();
*
* downlinkbandwidth=Constants.BandWidthAggRoot;
* latency=Constants.SwitchingDelayRoot;
* numport=Constants.RootSwitchPort;
*
* }
*/
this.dc = dc;
// TODO Auto-generated constructor stub
}
@Override
public void startEntity() {
Log.printLine(getName() + " is starting...");
schedule(getId(), 0, CloudSimTags.RESOURCE_CHARACTERISTICS_REQUEST);
}
@Override
public void processEvent(SimEvent ev) {
// Log.printLine(CloudSim.clock()+"[Broker]: event received:"+ev.getTag());
switch (ev.getTag()) {
// Resource characteristics request
case CloudSimTags.Network_Event_UP:
// process the packet from down switch or host
processpacket_up(ev);
break;
case CloudSimTags.Network_Event_DOWN:
// process the packet from uplink
processpacket_down(ev);
break;
case CloudSimTags.Network_Event_send:
processpacketforward(ev);
break;
case CloudSimTags.Network_Event_Host:
processhostpacket(ev);
break;
// Resource characteristics answer
case CloudSimTags.RESOURCE_Register:
registerHost(ev);
break;
// other unknown tags are processed by this method
default:
processOtherEvent(ev);
break;
}
}
protected void processhostpacket(SimEvent ev) {
// TODO Auto-generated method stub
// Send packet to host
NetworkPacket hspkt = (NetworkPacket) ev.getData();
NetworkHost hs = this.hostlist.get(hspkt.recieverhostid);
hs.packetrecieved.add(hspkt);
}
protected void processpacket_down(SimEvent ev) {
// TODO Auto-generated method stub
// packet coming from up level router.
// has to send downward
// check which switch to forward to
// add packet in the switch list
// add packet in the host list
// int src=ev.getSource();
NetworkPacket hspkt = (NetworkPacket) ev.getData();
int recvVMid = hspkt.pkt.reciever;
CloudSim.cancelAll(getId(), new PredicateType(
CloudSimTags.Network_Event_send));
schedule(getId(), this.latency, CloudSimTags.Network_Event_send);
if (this.level == NetworkConstants.EDGE_LEVEL) {
// packet is to be recieved by host
int hostid = dc.VmtoHostlist.get(recvVMid);
hspkt.recieverhostid = hostid;
List<NetworkPacket> pktlist = this.packetTohost.get(hostid);
if (pktlist == null) {
pktlist = new ArrayList<NetworkPacket>();
this.packetTohost.put(hostid, pktlist);
}
pktlist.add(hspkt);
return;
}
if (this.level == NetworkConstants.Agg_LEVEL) {
// packet is coming from root so need to be sent to edgelevel swich
// find the id for edgelevel switch
int switchid = dc.VmToSwitchid.get(recvVMid);
List<NetworkPacket> pktlist = this.downlinkswitchpktlist.get(switchid);
if (pktlist == null) {
pktlist = new ArrayList<NetworkPacket>();
this.downlinkswitchpktlist.put(switchid, pktlist);
}
pktlist.add(hspkt);
return;
}
}
protected void processpacket_up(SimEvent ev) {
// TODO Auto-generated method stub
// packet coming from down level router.
// has to send up
// check which switch to forward to
// add packet in the switch list
//
// int src=ev.getSource();
NetworkPacket hspkt = (NetworkPacket) ev.getData();
int recvVMid = hspkt.pkt.reciever;
CloudSim.cancelAll(getId(), new PredicateType(
CloudSimTags.Network_Event_send));
schedule(getId(), this.switching_delay, CloudSimTags.Network_Event_send);
if (this.level == NetworkConstants.EDGE_LEVEL) {
// packet is recieved from host
// packet is to be sent to aggregate level or to another host in the
// same level
int hostid = dc.VmtoHostlist.get(recvVMid);
NetworkHost hs = this.hostlist.get(hostid);
hspkt.recieverhostid = hostid;
if (hs != null) {
// packet to be sent to host connected to the switch
List<NetworkPacket> pktlist = this.packetTohost.get(hostid);
if (pktlist == null) {
pktlist = new ArrayList<NetworkPacket>();
this.packetTohost.put(hostid, pktlist);
}
pktlist.add(hspkt);
return;
}
// packet is to be sent to upper switch
// ASSUMPTION EACH EDGE is Connected to one aggregate level switch
Switch sw = this.uplinkswitches.get(0);
List<NetworkPacket> pktlist = this.uplinkswitchpktlist.get(sw.getId());
if (pktlist == null) {
pktlist = new ArrayList<NetworkPacket>();
this.uplinkswitchpktlist.put(sw.getId(), pktlist);
}
pktlist.add(hspkt);
return;
}
if (this.level == NetworkConstants.Agg_LEVEL) {
// packet is coming from edge level router so need to be sent to
// either root or another edge level swich
// find the id for edgelevel switch
int switchid = dc.VmToSwitchid.get(recvVMid);
boolean flagtoswtich = false;
for (Switch sw : this.downlinkswitches) {
if (switchid == sw.getId())
flagtoswtich = true;
}
if (flagtoswtich) {
List<NetworkPacket> pktlist = this.downlinkswitchpktlist
.get(switchid);
if (pktlist == null) {
pktlist = new ArrayList<NetworkPacket>();
this.downlinkswitchpktlist.put(switchid, pktlist);
}
pktlist.add(hspkt);
} else// send to up
{
Switch sw = this.uplinkswitches.get(0);
List<NetworkPacket> pktlist = this.uplinkswitchpktlist.get(sw
.getId());
if (pktlist == null) {
pktlist = new ArrayList<NetworkPacket>();
this.uplinkswitchpktlist.put(sw.getId(), pktlist);
}
pktlist.add(hspkt);
}
}
if (this.level == NetworkConstants.ROOT_LEVEL) {
// get id of edge router
int edgeswitchid = dc.VmToSwitchid.get(recvVMid);
// search which aggregate switch has it
int aggSwtichid = -1;
;
for (Switch sw : this.downlinkswitches) {
for (Switch edge : sw.downlinkswitches) {
if (edge.getId() == edgeswitchid) {
aggSwtichid = sw.getId();
break;
}
}
}
if (aggSwtichid < 0)
System.out.println(" No destination for this packet");
else {
List<NetworkPacket> pktlist = this.downlinkswitchpktlist
.get(aggSwtichid);
if (pktlist == null) {
pktlist = new ArrayList<NetworkPacket>();
this.downlinkswitchpktlist.put(aggSwtichid, pktlist);
}
pktlist.add(hspkt);
}
}
}
private void registerHost(SimEvent ev) {
// TODO Auto-generated method stub
NetworkHost hs = (NetworkHost) ev.getData();
hostlist.put(hs.getId(), (NetworkHost) ev.getData());
}
protected void processpacket(SimEvent ev) {
// TODO Auto-generated method stub
// send packet to itself with switching delay (discarding other)
CloudSim.cancelAll(getId(), new PredicateType(
CloudSimTags.Network_Event_UP));
schedule(getId(), this.switching_delay, CloudSimTags.Network_Event_UP);
pktlist.add((NetworkPacket) ev.getData());
// add the packet in the list
}
private void processOtherEvent(SimEvent ev) {
// TODO Auto-generated method stub
}
protected void processpacketforward(SimEvent ev) {
// TODO Auto-generated method stub
// search for the host and packets..send to them
if (this.downlinkswitchpktlist != null) {
for (Entry<Integer, List<NetworkPacket>> es : downlinkswitchpktlist
.entrySet()) {
int tosend = es.getKey();
List<NetworkPacket> hspktlist = es.getValue();
if (!hspktlist.isEmpty()) {
double avband = this.downlinkbandwidth / hspktlist.size();
Iterator<NetworkPacket> it = hspktlist.iterator();
while (it.hasNext()) {
NetworkPacket hspkt = it.next();
double delay = 1000 * hspkt.pkt.data / avband;
this.send(tosend, delay,
CloudSimTags.Network_Event_DOWN, hspkt);
}
hspktlist.clear();
}
}
}
if (this.uplinkswitchpktlist != null) {
for (Entry<Integer, List<NetworkPacket>> es : uplinkswitchpktlist
.entrySet()) {
int tosend = es.getKey();
List<NetworkPacket> hspktlist = es.getValue();
if (!hspktlist.isEmpty()) {
double avband = this.uplinkbandwidth / hspktlist.size();
Iterator<NetworkPacket> it = hspktlist.iterator();
while (it.hasNext()) {
NetworkPacket hspkt = it.next();
double delay = 1000 * hspkt.pkt.data / avband;
this.send(tosend, delay, CloudSimTags.Network_Event_UP,
hspkt);
}
hspktlist.clear();
}
}
}
if (this.packetTohost != null) {
for (Entry<Integer, List<NetworkPacket>> es : packetTohost.entrySet()) {
int tosend = es.getKey();
NetworkHost hs = this.hostlist.get(tosend);
List<NetworkPacket> hspktlist = es.getValue();
if (!hspktlist.isEmpty()) {
double avband = this.downlinkbandwidth / hspktlist.size();
Iterator<NetworkPacket> it = hspktlist.iterator();
while (it.hasNext()) {
NetworkPacket hspkt = it.next();
// hspkt.recieverhostid=tosend;
// hs.packetrecieved.add(hspkt);
this.send(this.getId(), hspkt.pkt.data / avband,
CloudSimTags.Network_Event_Host, hspkt);
}
hspktlist.clear();
}
}
}
// or to switch at next level.
// clear the list
}
private NetworkHost getHostwithVM(int vmid) {
// TODO Auto-generated method stub
for (Entry<Integer, NetworkHost> es : this.hostlist.entrySet()) {
Vm vm = VmList.getById(es.getValue().getVmList(), vmid);
if (vm != null)
return es.getValue();
}
return null;
}
private List<NetworkVm> getfreeVmlist(int numVMReq) {
// TODO Auto-generated method stub
List<NetworkVm> freehostls = new ArrayList<NetworkVm>();
for (Entry<Integer, NetworkVm> et : this.Vmlist.entrySet()) {
if (et.getValue().isFree()) {
freehostls.add(et.getValue());
}
if (freehostls.size() == numVMReq)
break;
}
return freehostls;
}
private List<NetworkHost> getfreehostlist(int numhost) {
// TODO Auto-generated method stub
<<<<<<< .mine
List<NetworkHost> freehostls=new ArrayList<NetworkHost>();
for(Entry<Integer,NetworkHost> et:this.hostlist.entrySet())
{
if(et.getValue().getNumberOfFreePes()==et.getValue().getNumberOfPes())
{
=======
List<NetworkHost> freehostls = new ArrayList<NetworkHost>();
for (Entry<Integer, NetworkHost> et : this.hostlist.entrySet()) {
if (et.getValue().getFreePesNumber() == et.getValue()
.getPesNumber()) {
>>>>>>> .r111
freehostls.add(et.getValue());
}
if (freehostls.size() == numhost)
break;
}
return freehostls;
}
@Override
public void shutdownEntity() {
Log.printLine(getName() + " is shutting down...");
}
}
/*
* Title: CloudSim Toolkit Description: CloudSim (Cloud Simulation) Toolkit for Modeling and
* Simulation of Parallel and Distributed Systems such as Clusters and Clouds Licence: GPL -
* http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2008, The University of Melbourne, Australia
*/
package org.cloudbus.cloudsim.power; package org.cloudbus.cloudsim.power;
import java.util.HashMap; import java.util.HashMap;
...@@ -19,34 +11,29 @@ import org.cloudbus.cloudsim.VmAllocationPolicy; ...@@ -19,34 +11,29 @@ import org.cloudbus.cloudsim.VmAllocationPolicy;
import org.cloudbus.cloudsim.core.CloudSim; import org.cloudbus.cloudsim.core.CloudSim;
/** /**
* PowerVmAllocationPolicySingleThresholdFixed is an VMAllocationPolicy that chooses, as the host * The Class PowerVmAllocationPolicyAbstract.
* for a VM, the host with the least power increase due to utilization increase.
* *
* @author Anton Beloglazov * @author Anton Beloglazov
* @since CloudSim Toolkit 4.3 * @since Jan 5, 2012
* @invariant $none
*/ */
public class PowerVmAllocationPolicySimple extends VmAllocationPolicy { public abstract class PowerVmAllocationPolicyAbstract extends VmAllocationPolicy {
/** The vm table. */ /** The vm table. */
private Map<String, Host> vmTable; private final Map<String, Host> vmTable = new HashMap<String, Host>();
/** /**
* Instantiates a new PowerVmAllocationPolicySimple. * Instantiates a new power vm allocation policy abstract.
* *
* @param hostList the host list * @param list the list
*/ */
public PowerVmAllocationPolicySimple(List<? extends Host> hostList) { public PowerVmAllocationPolicyAbstract(List<? extends Host> list) {
super(hostList); super(list);
setVmTable(new HashMap<String, Host>());
} }
/** /*
* Allocates a host for a given VM. * (non-Javadoc)
*
* @param vm VM specification
* *
* @return $true if the host could be allocated; $false otherwise * @see org.cloudbus.cloudsim.VmAllocationPolicy#allocateHostForVm(org.cloudbus.cloudsim.Vm)
*/ */
@Override @Override
public boolean allocateHostForVm(Vm vm) { public boolean allocateHostForVm(Vm vm) {
...@@ -79,11 +66,10 @@ public class PowerVmAllocationPolicySimple extends VmAllocationPolicy { ...@@ -79,11 +66,10 @@ public class PowerVmAllocationPolicySimple extends VmAllocationPolicy {
} }
/** /**
* Determines a host to allocate for the VM. * Find host for vm.
* *
* @param vm the vm * @param vm the vm
* * @return the power host
* @return the host
*/ */
public PowerHost findHostForVm(Vm vm) { public PowerHost findHostForVm(Vm vm) {
for (PowerHost host : this.<PowerHost> getHostList()) { for (PowerHost host : this.<PowerHost> getHostList()) {
...@@ -97,18 +83,7 @@ public class PowerVmAllocationPolicySimple extends VmAllocationPolicy { ...@@ -97,18 +83,7 @@ public class PowerVmAllocationPolicySimple extends VmAllocationPolicy {
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see org.cloudbus.cloudsim.VmAllocationPolicy#optimizeAllocation(java.util.List) * @see org.cloudbus.cloudsim.VmAllocationPolicy#deallocateHostForVm(org.cloudbus.cloudsim.Vm)
*/
@Override
public List<Map<String, Object>> optimizeAllocation(List<? extends Vm> vmList) {
// This policy doesn't optimize the VM allocation
return null;
}
/**
* Releases the host used by a VM.
*
* @param vm the vm
*/ */
@Override @Override
public void deallocateHostForVm(Vm vm) { public void deallocateHostForVm(Vm vm) {
...@@ -118,40 +93,26 @@ public class PowerVmAllocationPolicySimple extends VmAllocationPolicy { ...@@ -118,40 +93,26 @@ public class PowerVmAllocationPolicySimple extends VmAllocationPolicy {
} }
} }
/** /*
* Gets the host that is executing the given VM belonging to the given user. * (non-Javadoc)
*
* @param vm the vm
* *
* @return the Host with the given vmID and userID; $null if not found * @see org.cloudbus.cloudsim.VmAllocationPolicy#getHost(org.cloudbus.cloudsim.Vm)
*/ */
@Override @Override
public Host getHost(Vm vm) { public Host getHost(Vm vm) {
return getVmTable().get(vm.getUid()); return getVmTable().get(vm.getUid());
} }
/** /*
* Gets the host that is executing the given VM belonging to the given user. * (non-Javadoc)
*
* @param vmId the vm id
* @param userId the user id
* *
* @return the Host with the given vmID and userID; $null if not found * @see org.cloudbus.cloudsim.VmAllocationPolicy#getHost(int, int)
*/ */
@Override @Override
public Host getHost(int vmId, int userId) { public Host getHost(int vmId, int userId) {
return getVmTable().get(Vm.getUid(userId, vmId)); return getVmTable().get(Vm.getUid(userId, vmId));
} }
/**
* Sets the vm table.
*
* @param vmTable the vm table
*/
public void setVmTable(Map<String, Host> vmTable) {
this.vmTable = vmTable;
}
/** /**
* Gets the vm table. * Gets the vm table.
* *
......
/*
* Title: CloudSim Toolkit Description: CloudSim (Cloud Simulation) Toolkit for Modeling and
* Simulation of Parallel and Distributed Systems such as Clusters and Clouds Licence: GPL -
* http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2008, The University of Melbourne, Australia
*/
package org.cloudbus.cloudsim.power;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.core.CloudSim;
/**
* The Class PowerVmAllocationPolicyDoubleThresholdAbstract.
*/
public class PowerVmAllocationPolicyDoubleThresholdAbstract extends VmAllocationPolicySimple {
/** The hosts PEs. */
protected Map<String, Integer> hostsPes;
/** The hosts utilization. */
protected Map<Integer, Double> hostsUtilization;
/** The hosts memory. */
protected Map<Integer, Integer> hostsRam;
/** The hosts bw. */
protected Map<Integer, Long> hostsBw;
/** The old allocation. */
private List<Map<String, Object>> savedAllocation;
/** The utilization threshold lower. */
private double utilizationThresholdLower;
/** The utilization threshold upper. */
private double utilizationThresholdUpper;
/**
* Instantiates a new vM provisioner mpp.
*
* @param list the list
* @param utilizationThresholdLower the utilization threshold lower
* @param utilizationThresholdUpper the utilization threshold upper
*/
public PowerVmAllocationPolicyDoubleThresholdAbstract(
List<? extends PowerHost> list,
double utilizationThresholdLower,
double utilizationThresholdUpper) {
super(list);
setSavedAllocation(new ArrayList<Map<String, Object>>());
setUtilizationThresholdLower(utilizationThresholdLower);
setUtilizationThresholdUpper(utilizationThresholdUpper);
}
/**
* Determines a host to allocate for the VM.
*
* @param vm the vm
*
* @return the host
*/
public PowerHost findHostForVm(Vm vm) {
double minPower = Double.MAX_VALUE;
PowerHost allocatedHost = null;
for (PowerHost host : this.<PowerHost> getHostList()) {
if (host.isSuitableForVm(vm)) {
if (getMaxUtilizationAfterAllocation(host, vm) > getUtilizationThresholdUpper()) {
continue;
}
try {
double powerAfterAllocation = getPowerAfterAllocation(host, vm);
if (powerAfterAllocation != -1) {
double powerDiff = powerAfterAllocation - host.getPower();
if (powerDiff < minPower) {
minPower = powerDiff;
allocatedHost = host;
}
}
} catch (Exception e) {
}
}
}
return allocatedHost;
}
/**
* Allocates a host for a given VM.
*
* @param vm VM specification
*
* @return $true if the host could be allocated; $false otherwise
*
* @pre $none
* @post $none
*/
@Override
public boolean allocateHostForVm(Vm vm) {
PowerHost allocatedHost = findHostForVm(vm);
if (allocatedHost != null && allocatedHost.vmCreate(vm)) { // if vm has been succesfully
// created in the host
getVmTable().put(vm.getUid(), allocatedHost);
Log.formatLine(
"%.2f: VM #" + vm.getId() + " has been allocated to the host #" + allocatedHost.getId()
+ "\n",
CloudSim.clock());
return true;
}
return false;
}
/**
* Releases the host used by a VM.
*
* @param vm the vm
*
* @pre $none
* @post none
*/
@Override
public void deallocateHostForVm(Vm vm) {
if (getVmTable().containsKey(vm.getUid())) {
PowerHost host = (PowerHost) getVmTable().remove(vm.getUid());
if (host != null) {
host.vmDestroy(vm);
}
}
}
/**
* Optimize allocation of the VMs according to current utilization.
*
* @param vmList the vm list
*
* @return the array list< hash map< string, object>>
*/
@Override
public List<Map<String, Object>> optimizeAllocation(List<? extends Vm> vmList) {
Log.printLine("optimizeAllocation() method must be overriden by a child class");
return null;
}
/**
* Save allocation.
*
* @param vmList the vm list
*/
protected void saveAllocation(List<? extends Vm> vmList) {
getSavedAllocation().clear();
for (Vm vm : vmList) {
Map<String, Object> map = new HashMap<String, Object>();
// Log.printLine("Trying to save VM #" + vm.getId());
map.put("vm", vm);
map.put("host", vm.getHost());
getSavedAllocation().add(map);
// we do not destroy current allocation
// vm.getHost().vmDestroy(vm);
// getVmTable().remove(vm.getUid());
}
}
/**
* Restore allocation.
*
* @param vmsToRestore the vms to restore
* @param list
*/
protected void restoreAllocation(List<Vm> vmsToRestore, List<Host> hostList) {
// getVmTable().clear();
for (Host host : hostList) {
host.vmDestroyAll();
host.reallocateMigratingInVms();
}
/*
* for (Map<String, Object> map : getSavedAllocation()) { //Vm vm = (Vm) map.get("vm");
* PowerHost host = (PowerHost) map.get("host"); //PowerHost host = (PowerHost)
* vm.getHost(); if (host != null) { host.vmDestroyAll(); // vm.getHost().vmDestroy(vm); }
* //PowerHost host = (PowerHost) map.get("host"); //host.vmDestroy(vm);
* //host.vmDestroyAll(); }
*/
for (Map<String, Object> map : getSavedAllocation()) {
Vm vm = (Vm) map.get("vm");
PowerHost host = (PowerHost) map.get("host");
// Log.printLine("Trying to restore VM #" + vm.getId() + " on host #" + host.getId());
if (!vmsToRestore.contains(vm)) {
// Log.printLine("VM #" + vm.getVmId() +
// " skipped because it's not in the toRestore list");
continue;
}
if (!host.vmCreate(vm)) {
Log.printLine("failed");
}
getVmTable().put(vm.getUid(), host);
Log.printLine("Restored VM #" + vm.getId() + " on host #" + host.getId());
// host.vmDestroy(vm);
// if (!host.vmCreate(vm)) {
// host.vmCreateBestEffort(vm);
// }
// getVmTable().put(vm.getUid(), host);
}
}
/**
* Gets the power after allocation.
*
* @param host the host
* @param vm the vm
*
* @return the power after allocation
*/
protected double getPowerAfterAllocation(PowerHost host, Vm vm) {
List<Double> allocatedMipsForVm = null;
PowerHost allocatedHost = (PowerHost) vm.getHost();
if (allocatedHost != null) {
allocatedMipsForVm = allocatedHost.getAllocatedMipsForVm(vm);
}
if (!host.allocatePesForVm(vm, vm.getCurrentRequestedMips())) {
return -1;
}
double power = host.getPower();
host.deallocatePesForVm(vm);
if (allocatedHost != null && allocatedMipsForVm != null) {
vm.getHost().allocatePesForVm(vm, allocatedMipsForVm);
}
return power;
}
/**
* Gets the power after allocation.
*
* @param host the host
* @param vm the vm
*
* @return the power after allocation
*/
protected double getMaxUtilizationAfterAllocation(PowerHost host, Vm vm) {
List<Double> allocatedMipsForVm = null;
PowerHost allocatedHost = (PowerHost) vm.getHost();
if (allocatedHost != null) {
allocatedMipsForVm = vm.getHost().getAllocatedMipsForVm(vm);
}
if (!host.allocatePesForVm(vm, vm.getCurrentRequestedMips())) {
return -1;
}
double maxUtilization = host.getMaxUtilizationAmongVmsPes(vm);
host.deallocatePesForVm(vm);
if (allocatedHost != null && allocatedMipsForVm != null) {
vm.getHost().allocatePesForVm(vm, allocatedMipsForVm);
}
return maxUtilization;
}
/**
* Gets the saved allocation.
*
* @return the saved allocation
*/
protected List<Map<String, Object>> getSavedAllocation() {
return savedAllocation;
}
/**
* Sets the saved allocation.
*
* @param savedAllocation the saved allocation
*/
protected void setSavedAllocation(List<Map<String, Object>> savedAllocation) {
this.savedAllocation = savedAllocation;
}
/**
* Sets the utilization threshold lower.
*
* @param utilizationThresholdLower the new utilization threshold lower
*/
public void setUtilizationThresholdLower(double utilizationThresholdLower) {
this.utilizationThresholdLower = utilizationThresholdLower;
}
/**
* Gets the utilization threshold lower.
*
* @return the utilization threshold lower
*/
public double getUtilizationThresholdLower() {
return utilizationThresholdLower;
}
/**
* Sets the utilization threshold upper.
*
* @param utilizationThresholdUpper the new utilization threshold upper
*/
public void setUtilizationThresholdUpper(double utilizationThresholdUpper) {
this.utilizationThresholdUpper = utilizationThresholdUpper;
}
/**
* Gets the utilization threshold upper.
*
* @return the utilization threshold upper
*/
public double getUtilizationThresholdUpper() {
return utilizationThresholdUpper;
}
}
package org.cloudbus.cloudsim.power;
import java.util.List;
import java.util.Map;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Vm;
/**
* This a simple class representing a DVFS VM allocation policy. The actual application of DVFS
* (adjustment of the host's power consumption) happens in the PowerDatacenter class.
*
* @author Anton Beloglazov
* @since Jan 5, 2012
*
*/
public class PowerVmAllocationPolicyDvfs extends PowerVmAllocationPolicyAbstract {
/**
* Instantiates a new power vm allocation policy dvfs.
*
* @param list the list
*/
public PowerVmAllocationPolicyDvfs(List<? extends Host> list) {
super(list);
}
/*
* (non-Javadoc)
*
* @see org.cloudbus.cloudsim.VmAllocationPolicy#optimizeAllocation(java.util.List)
*/
@Override
public List<Map<String, Object>> optimizeAllocation(List<? extends Vm> vmList) {
// This policy doesn't optimize the VM allocation
return null;
}
}
...@@ -19,7 +19,7 @@ import org.cloudbus.cloudsim.util.ExecutionTimeMeasurer; ...@@ -19,7 +19,7 @@ import org.cloudbus.cloudsim.util.ExecutionTimeMeasurer;
/** /**
* The Class PowerVmAllocationPolicyMigrationAbstract. * The Class PowerVmAllocationPolicyMigrationAbstract.
*/ */
public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAllocationPolicySimple { public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAllocationPolicyAbstract {
/** The vm selection policy. */ /** The vm selection policy. */
private PowerVmSelectionPolicy vmSelectionPolicy; private PowerVmSelectionPolicy vmSelectionPolicy;
...@@ -27,9 +27,6 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl ...@@ -27,9 +27,6 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl
/** The saved allocation. */ /** The saved allocation. */
private final List<Map<String, Object>> savedAllocation = new ArrayList<Map<String, Object>>(); private final List<Map<String, Object>> savedAllocation = new ArrayList<Map<String, Object>>();
/** The utilization threshold. */
private double utilizationThreshold = 0.9;
/** The utilization history. */ /** The utilization history. */
private final Map<Integer, List<Double>> utilizationHistory = new HashMap<Integer, List<Double>>(); private final Map<Integer, List<Double>> utilizationHistory = new HashMap<Integer, List<Double>>();
...@@ -64,21 +61,6 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl ...@@ -64,21 +61,6 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl
setVmSelectionPolicy(vmSelectionPolicy); setVmSelectionPolicy(vmSelectionPolicy);
} }
/**
* Instantiates a new power vm allocation policy migration abstract.
*
* @param hostList the host list
* @param vmSelectionPolicy the vm selection policy
* @param utilizationThreshold the utilization threshold
*/
public PowerVmAllocationPolicyMigrationAbstract(
List<? extends Host> hostList,
PowerVmSelectionPolicy vmSelectionPolicy,
double utilizationThreshold) {
this(hostList, vmSelectionPolicy);
setUtilizationThreshold(utilizationThreshold);
}
/** /**
* Optimize allocation of the VMs according to current utilization. * Optimize allocation of the VMs according to current utilization.
* *
...@@ -130,28 +112,15 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl ...@@ -130,28 +112,15 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl
List<PowerHostUtilizationHistory> overUtilizedHosts) { List<PowerHostUtilizationHistory> overUtilizedHosts) {
List<Map<String, Object>> migrationMap = new LinkedList<Map<String, Object>>(); List<Map<String, Object>> migrationMap = new LinkedList<Map<String, Object>>();
List<PowerHost> switchedOffHosts = getSwitchedOffHosts(); List<PowerHost> switchedOffHosts = getSwitchedOffHosts();
Set<PowerHost> excludedHostsForFindingUnderUtilizedHost = new HashSet<PowerHost>(); // over-utilized
// hosts // over-utilized hosts + hosts that are selected to migrate VMs to from over-utilized hosts
// + Set<PowerHost> excludedHostsForFindingUnderUtilizedHost = new HashSet<PowerHost>();
// hosts
// that
// are
// selected
// to
// migrate
// VMs
// to
// from
// over-utilized
// hosts
excludedHostsForFindingUnderUtilizedHost.addAll(overUtilizedHosts); excludedHostsForFindingUnderUtilizedHost.addAll(overUtilizedHosts);
excludedHostsForFindingUnderUtilizedHost.addAll(switchedOffHosts); excludedHostsForFindingUnderUtilizedHost.addAll(switchedOffHosts);
excludedHostsForFindingUnderUtilizedHost.addAll(extractHostListFromMigrationMap(migrationMap)); excludedHostsForFindingUnderUtilizedHost.addAll(extractHostListFromMigrationMap(migrationMap));
Set<PowerHost> excludedHostsForFindingNewVmPlacement = new HashSet<PowerHost>(); // over-utilized // over-utilized + under-utilized hosts
// + Set<PowerHost> excludedHostsForFindingNewVmPlacement = new HashSet<PowerHost>();
// under-utilized
// hosts
excludedHostsForFindingNewVmPlacement.addAll(overUtilizedHosts); excludedHostsForFindingNewVmPlacement.addAll(overUtilizedHosts);
excludedHostsForFindingNewVmPlacement.addAll(switchedOffHosts); excludedHostsForFindingNewVmPlacement.addAll(switchedOffHosts);
...@@ -188,9 +157,6 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl ...@@ -188,9 +157,6 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl
List<Map<String, Object>> newVmPlacement = getNewVmPlacementFromUnderUtilizedHost( List<Map<String, Object>> newVmPlacement = getNewVmPlacementFromUnderUtilizedHost(
vmsToMigrateFromUnderUtilizedHost, vmsToMigrateFromUnderUtilizedHost,
excludedHostsForFindingNewVmPlacement); excludedHostsForFindingNewVmPlacement);
// if (newVmPlacement.isEmpty()) {
// break;
// }
excludedHostsForFindingUnderUtilizedHost.addAll(extractHostListFromMigrationMap(newVmPlacement)); excludedHostsForFindingUnderUtilizedHost.addAll(extractHostListFromMigrationMap(newVmPlacement));
...@@ -268,12 +234,11 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl ...@@ -268,12 +234,11 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl
return isHostOverUtilizedAfterAllocation; return isHostOverUtilizedAfterAllocation;
} }
/* /**
* (non-Javadoc) * Find host for vm.
* *
* @see * @param vm the vm
* org.cloudbus.cloudsim.experiments.power.PowerVmAllocationPolicySimple#findHostForVm(org.cloudbus * @return the power host
* .cloudsim.Vm)
*/ */
@Override @Override
public PowerHost findHostForVm(Vm vm) { public PowerHost findHostForVm(Vm vm) {
...@@ -325,6 +290,13 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl ...@@ -325,6 +290,13 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl
return migrationMap; return migrationMap;
} }
/**
* Gets the new vm placement from under utilized host.
*
* @param vmsToMigrate the vms to migrate
* @param excludedHosts the excluded hosts
* @return the new vm placement from under utilized host
*/
protected List<Map<String, Object>> getNewVmPlacementFromUnderUtilizedHost( protected List<Map<String, Object>> getNewVmPlacementFromUnderUtilizedHost(
List<? extends Vm> vmsToMigrate, List<? extends Vm> vmsToMigrate,
Set<? extends Host> excludedHosts) { Set<? extends Host> excludedHosts) {
...@@ -567,14 +539,8 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl ...@@ -567,14 +539,8 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl
double hostUtilizationMips = host.getUtilizationOfCpuMips(); double hostUtilizationMips = host.getUtilizationOfCpuMips();
for (Vm vm2 : host.getVmList()) { for (Vm vm2 : host.getVmList()) {
if (host.getVmsMigratingIn().contains(vm2)) { if (host.getVmsMigratingIn().contains(vm2)) {
hostUtilizationMips += host.getTotalAllocatedMipsForVm(vm2) * 0.9 / 0.1; // calculate // calculate additional potential CPU usage of a migrating in VM
// additional hostUtilizationMips += host.getTotalAllocatedMipsForVm(vm2) * 0.9 / 0.1;
// potential
// CPU
// usage
// of a
// migrating
// in VM
} }
} }
...@@ -592,24 +558,6 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl ...@@ -592,24 +558,6 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl
return savedAllocation; return savedAllocation;
} }
/**
* Sets the utilization threshold.
*
* @param utilizationThreshold the new utilization threshold
*/
protected void setUtilizationThreshold(double utilizationThreshold) {
this.utilizationThreshold = utilizationThreshold;
}
/**
* Gets the utilization threshold.
*
* @return the utilization threshold
*/
protected double getUtilizationThreshold() {
return utilizationThreshold;
}
/** /**
* Sets the vm selection policy. * Sets the vm selection policy.
* *
......
...@@ -33,7 +33,7 @@ public class PowerVmAllocationPolicyMigrationInterQuartileRange extends ...@@ -33,7 +33,7 @@ public class PowerVmAllocationPolicyMigrationInterQuartileRange extends
double safetyParameter, double safetyParameter,
PowerVmAllocationPolicyMigrationAbstract fallbackVmAllocationPolicy, PowerVmAllocationPolicyMigrationAbstract fallbackVmAllocationPolicy,
double utilizationThreshold) { double utilizationThreshold) {
super(hostList, vmSelectionPolicy, utilizationThreshold); super(hostList, vmSelectionPolicy);
setSafetyParameter(safetyParameter); setSafetyParameter(safetyParameter);
setFallbackVmAllocationPolicy(fallbackVmAllocationPolicy); setFallbackVmAllocationPolicy(fallbackVmAllocationPolicy);
} }
......
...@@ -36,7 +36,7 @@ public class PowerVmAllocationPolicyMigrationLocalRegression extends PowerVmAllo ...@@ -36,7 +36,7 @@ public class PowerVmAllocationPolicyMigrationLocalRegression extends PowerVmAllo
double schedulingInterval, double schedulingInterval,
PowerVmAllocationPolicyMigrationAbstract fallbackVmAllocationPolicy, PowerVmAllocationPolicyMigrationAbstract fallbackVmAllocationPolicy,
double utilizationThreshold) { double utilizationThreshold) {
super(hostList, vmSelectionPolicy, utilizationThreshold); super(hostList, vmSelectionPolicy);
setSafetyParameter(safetyParameter); setSafetyParameter(safetyParameter);
setSchedulingInterval(schedulingInterval); setSchedulingInterval(schedulingInterval);
setFallbackVmAllocationPolicy(fallbackVmAllocationPolicy); setFallbackVmAllocationPolicy(fallbackVmAllocationPolicy);
......
...@@ -33,7 +33,7 @@ public class PowerVmAllocationPolicyMigrationMedianAbsoluteDeviation extends ...@@ -33,7 +33,7 @@ public class PowerVmAllocationPolicyMigrationMedianAbsoluteDeviation extends
double safetyParameter, double safetyParameter,
PowerVmAllocationPolicyMigrationAbstract fallbackVmAllocationPolicy, PowerVmAllocationPolicyMigrationAbstract fallbackVmAllocationPolicy,
double utilizationThreshold) { double utilizationThreshold) {
super(hostList, vmSelectionPolicy, utilizationThreshold); super(hostList, vmSelectionPolicy);
setSafetyParameter(safetyParameter); setSafetyParameter(safetyParameter);
setFallbackVmAllocationPolicy(fallbackVmAllocationPolicy); setFallbackVmAllocationPolicy(fallbackVmAllocationPolicy);
} }
......
...@@ -35,7 +35,7 @@ public class PowerVmAllocationPolicyMigrationMedianAbsoluteDeviation2 extends ...@@ -35,7 +35,7 @@ public class PowerVmAllocationPolicyMigrationMedianAbsoluteDeviation2 extends
double safetyParameter, double safetyParameter,
PowerVmAllocationPolicyMigrationAbstract fallbackVmAllocationPolicy, PowerVmAllocationPolicyMigrationAbstract fallbackVmAllocationPolicy,
double utilizationThreshold) { double utilizationThreshold) {
super(hostList, vmSelectionPolicy, utilizationThreshold); super(hostList, vmSelectionPolicy);
setSafetyParameter(safetyParameter); setSafetyParameter(safetyParameter);
setFallbackVmAllocationPolicy(fallbackVmAllocationPolicy); setFallbackVmAllocationPolicy(fallbackVmAllocationPolicy);
} }
......
...@@ -8,7 +8,10 @@ import org.cloudbus.cloudsim.Vm; ...@@ -8,7 +8,10 @@ import org.cloudbus.cloudsim.Vm;
/** /**
* The Class PowerVmAllocationPolicyMigrationThresholds. * The Class PowerVmAllocationPolicyMigrationThresholds.
*/ */
public class PowerVmAllocationPolicyMigrationThresholds extends PowerVmAllocationPolicyMigrationAbstract { public class PowerVmAllocationPolicyMigrationStaticThreshold extends PowerVmAllocationPolicyMigrationAbstract {
/** The utilization threshold. */
private double utilizationThreshold = 0.9;
/** /**
* Instantiates a new power vm allocation policy migration mad. * Instantiates a new power vm allocation policy migration mad.
...@@ -17,11 +20,12 @@ public class PowerVmAllocationPolicyMigrationThresholds extends PowerVmAllocatio ...@@ -17,11 +20,12 @@ public class PowerVmAllocationPolicyMigrationThresholds extends PowerVmAllocatio
* @param vmSelectionPolicy the vm selection policy * @param vmSelectionPolicy the vm selection policy
* @param utilizationThreshold the utilization threshold * @param utilizationThreshold the utilization threshold
*/ */
public PowerVmAllocationPolicyMigrationThresholds( public PowerVmAllocationPolicyMigrationStaticThreshold(
List<? extends Host> hostList, List<? extends Host> hostList,
PowerVmSelectionPolicy vmSelectionPolicy, PowerVmSelectionPolicy vmSelectionPolicy,
double utilizationThreshold) { double utilizationThreshold) {
super(hostList, vmSelectionPolicy, utilizationThreshold); super(hostList, vmSelectionPolicy);
setUtilizationThreshold(utilizationThreshold);
} }
/** /**
...@@ -41,4 +45,22 @@ public class PowerVmAllocationPolicyMigrationThresholds extends PowerVmAllocatio ...@@ -41,4 +45,22 @@ public class PowerVmAllocationPolicyMigrationThresholds extends PowerVmAllocatio
return utilization > getUtilizationThreshold(); return utilization > getUtilizationThreshold();
} }
/**
* Sets the utilization threshold.
*
* @param utilizationThreshold the new utilization threshold
*/
protected void setUtilizationThreshold(double utilizationThreshold) {
this.utilizationThreshold = utilizationThreshold;
}
/**
* Gets the utilization threshold.
*
* @return the utilization threshold
*/
protected double getUtilizationThreshold() {
return utilizationThreshold;
}
} }
/*
* Title: CloudSim Toolkit Description: CloudSim (Cloud Simulation) Toolkit for Modeling and
* Simulation of Clouds Licence: GPL - http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2009-2010, The University of Melbourne, Australia
*/
package org.cloudbus.cloudsim.power;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.power.lists.PowerVmList;
/**
* PowerVmAllocationPolicySingleThreshold is an VMAllocationPolicy that chooses a host with the
* least power increase due to utilization increase caused by the VM allocation.
*
* @author Anton Beloglazov
* @since CloudSim Toolkit 2.0
*/
public class PowerVmAllocationPolicySingleThreshold extends VmAllocationPolicySimple {
/** The hosts PEs. */
protected Map<String, Integer> hostsPes;
/** The hosts utilization. */
protected Map<Integer, Double> hostsUtilization;
/** The hosts memory. */
protected Map<Integer, Integer> hostsRam;
/** The hosts bw. */
protected Map<Integer, Long> hostsBw;
/** The old allocation. */
private List<Map<String, Object>> savedAllocation;
/** The utilization threshold. */
private double utilizationThreshold;
/**
* Instantiates a new vM provisioner mpp.
*
* @param list the list
* @param utilizationThreshold the utilization bound
*/
public PowerVmAllocationPolicySingleThreshold(List<? extends PowerHost> list, double utilizationThreshold) {
super(list);
setSavedAllocation(new ArrayList<Map<String, Object>>());
setUtilizationThreshold(utilizationThreshold);
}
/**
* Determines a host to allocate for the VM.
*
* @param vm the vm
*
* @return the host
*/
public PowerHost findHostForVm(Vm vm) {
double minPower = Double.MAX_VALUE;
PowerHost allocatedHost = null;
for (PowerHost host : this.<PowerHost> getHostList()) {
if (host.isSuitableForVm(vm)) {
if (getMaxUtilizationAfterAllocation(host, vm) > getUtilizationThreshold()) {
continue;
}
try {
double powerAfterAllocation = getPowerAfterAllocation(host, vm);
if (powerAfterAllocation != -1) {
double powerDiff = powerAfterAllocation - host.getPower();
if (powerDiff < minPower) {
minPower = powerDiff;
allocatedHost = host;
}
}
} catch (Exception e) {
}
}
}
return allocatedHost;
}
/**
* Allocates a host for a given VM.
*
* @param vm VM specification
*
* @return $true if the host could be allocated; $false otherwise
*
* @pre $none
* @post $none
*/
@Override
public boolean allocateHostForVm(Vm vm) {
PowerHost allocatedHost = findHostForVm(vm);
if (allocatedHost != null && allocatedHost.vmCreate(vm)) { // if vm has been succesfully
// created in the host
getVmTable().put(vm.getUid(), allocatedHost);
Log.formatLine(
"%.2f: VM #" + vm.getId() + " has been allocated to the host #" + allocatedHost.getId()
+ "\n",
CloudSim.clock());
return true;
}
return false;
}
/**
* Releases the host used by a VM.
*
* @param vm the vm
*
* @pre $none
* @post none
*/
@Override
public void deallocateHostForVm(Vm vm) {
if (getVmTable().containsKey(vm.getUid())) {
PowerHost host = (PowerHost) getVmTable().remove(vm.getUid());
if (host != null) {
host.vmDestroy(vm);
}
}
}
/**
* Optimize allocation of the VMs according to current utilization.
*
* @param vmList the vm list
* @param utilizationThreshold the utilization bound
*
* @return the array list< hash map< string, object>>
*/
@Override
public List<Map<String, Object>> optimizeAllocation(List<? extends Vm> vmList) {
List<Map<String, Object>> migrationMap = new ArrayList<Map<String, Object>>();
if (vmList.isEmpty()) {
return migrationMap;
}
saveAllocation(vmList);
List<Vm> vmsToRestore = new ArrayList<Vm>();
vmsToRestore.addAll(vmList);
List<Vm> vmsToMigrate = new ArrayList<Vm>();
for (Vm vm : vmList) {
if (vm.isInMigration()) {
continue;
}
vmsToMigrate.add(vm);
vm.getHost().vmDestroy(vm);
}
PowerVmList.sortByCpuUtilization(vmsToMigrate);
for (PowerHost host : this.<PowerHost> getHostList()) {
host.reallocateMigratingInVms();
}
for (Vm vm : vmsToMigrate) {
PowerHost oldHost = (PowerHost) getVmTable().get(vm.getUid());
PowerHost allocatedHost = findHostForVm(vm);
if (allocatedHost != null && allocatedHost.getId() != oldHost.getId()) {
allocatedHost.vmCreate(vm);
Log.printLine("VM #" + vm.getId() + " allocated to host #" + allocatedHost.getId());
Map<String, Object> migrate = new HashMap<String, Object>();
migrate.put("vm", vm);
migrate.put("host", allocatedHost);
migrationMap.add(migrate);
}
}
restoreAllocation(vmsToRestore, getHostList());
return migrationMap;
}
/**
* Save allocation.
*
* @param vmList the vm list
*/
protected void saveAllocation(List<? extends Vm> vmList) {
getSavedAllocation().clear();
for (Vm vm : vmList) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("vm", vm);
map.put("host", vm.getHost());
getSavedAllocation().add(map);
}
}
/**
* Restore allocation.
*
* @param vmsToRestore the vms to restore
*/
protected void restoreAllocation(List<Vm> vmsToRestore, List<Host> hostList) {
for (Host host : hostList) {
host.vmDestroyAll();
host.reallocateMigratingInVms();
}
for (Map<String, Object> map : getSavedAllocation()) {
Vm vm = (Vm) map.get("vm");
PowerHost host = (PowerHost) map.get("host");
if (!vmsToRestore.contains(vm)) {
continue;
}
if (!host.vmCreate(vm)) {
Log.printLine("Something is wrong, the VM can's be restored");
System.exit(0);
}
getVmTable().put(vm.getUid(), host);
Log.printLine("Restored VM #" + vm.getId() + " on host #" + host.getId());
}
}
/**
* Gets the power after allocation.
*
* @param host the host
* @param vm the vm
*
* @return the power after allocation
*/
protected double getPowerAfterAllocation(PowerHost host, Vm vm) {
List<Double> allocatedMipsForVm = null;
PowerHost allocatedHost = (PowerHost) vm.getHost();
if (allocatedHost != null) {
allocatedMipsForVm = allocatedHost.getAllocatedMipsForVm(vm);
}
if (!host.allocatePesForVm(vm, vm.getCurrentRequestedMips())) {
return -1;
}
double power = host.getPower();
host.deallocatePesForVm(vm);
if (allocatedHost != null && allocatedMipsForVm != null) {
vm.getHost().allocatePesForVm(vm, allocatedMipsForVm);
}
return power;
}
/**
* Gets the power after allocation.
*
* @param host the host
* @param vm the vm
*
* @return the power after allocation
*/
protected double getMaxUtilizationAfterAllocation(PowerHost host, Vm vm) {
List<Double> allocatedMipsForVm = null;
PowerHost allocatedHost = (PowerHost) vm.getHost();
if (allocatedHost != null) {
allocatedMipsForVm = vm.getHost().getAllocatedMipsForVm(vm);
}
if (!host.allocatePesForVm(vm, vm.getCurrentRequestedMips())) {
return -1;
}
double maxUtilization = host.getMaxUtilizationAmongVmsPes(vm);
host.deallocatePesForVm(vm);
if (allocatedHost != null && allocatedMipsForVm != null) {
vm.getHost().allocatePesForVm(vm, allocatedMipsForVm);
}
return maxUtilization;
}
/**
* Gets the saved allocation.
*
* @return the saved allocation
*/
protected List<Map<String, Object>> getSavedAllocation() {
return savedAllocation;
}
/**
* Sets the saved allocation.
*
* @param savedAllocation the saved allocation
*/
protected void setSavedAllocation(List<Map<String, Object>> savedAllocation) {
this.savedAllocation = savedAllocation;
}
/**
* Gets the utilization bound.
*
* @return the utilization bound
*/
protected double getUtilizationThreshold() {
return utilizationThreshold;
}
/**
* Sets the utilization bound.
*
* @param utilizationThreshold the new utilization bound
*/
protected void setUtilizationThreshold(double utilizationThreshold) {
this.utilizationThreshold = utilizationThreshold;
}
}
/*
* Title: CloudSim Toolkit Description: CloudSim (Cloud Simulation) Toolkit for Modeling and
* Simulation of Parallel and Distributed Systems such as Clusters and Clouds Licence: GPL -
* http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2008, The University of Melbourne, Australia
*/
package org.cloudbus.cloudsim.power;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.power.lists.PowerVmList;
// TODO: rewrite this as a child of PolicyMigrationAbstract, remove doubleThresholdAbstract, single
// threshold and policy simple
/**
* PowerVmAllocationPolicySingleThresholdFixed is an VMAllocationPolicy that chooses, as the host
* for a VM, the host with the least power increase due to utilization increase.
*
* @author Anton Beloglazov
* @since CloudSim Toolkit 4.3
* @invariant $none
*/
public class PowerVmAllocationPolicySingleThresholdFixed extends
PowerVmAllocationPolicyDoubleThresholdAbstract {
/**
* Instantiates a new vM provisioner mpp.
*
* @param list the list
* @param utilizationThreshold the utilization bound
*/
public PowerVmAllocationPolicySingleThresholdFixed(
List<? extends PowerHost> list,
double utilizationThreshold) {
super(list, 0, utilizationThreshold);
}
/**
* Optimize allocation of the VMs according to current utilization.
*
* @param vmList the vm list
* @param utilizationThreshold the utilization bound
*
* @return the array list< hash map< string, object>>
*/
@Override
public List<Map<String, Object>> optimizeAllocation(List<? extends Vm> vmList) {
List<Map<String, Object>> migrationMap = new ArrayList<Map<String, Object>>();
if (vmList.isEmpty()) {
return migrationMap;
}
saveAllocation(vmList);
List<Vm> vmsToRestore = new ArrayList<Vm>();
vmsToRestore.addAll(vmList);
List<Vm> vmTmpList = new ArrayList<Vm>();
vmTmpList.addAll(vmList);
PowerVmList.sortByCpuUtilization(vmTmpList);
for (PowerHost host : this.<PowerHost> getHostList()) {
// host.vmDestroyAll();
host.reallocateMigratingInVms();
}
for (Vm vm : vmTmpList) {
// Log.printLine("VM #" + vm.getId() + " utilization: " + String.format("%.2f;",
// vm.getTotalUtilization(CloudSim.clock()) * 100));
if (vm.isInMigration()) {
continue;
}
/**
* TODO: some VMs are still not migrated to the destination but, but migration takes
* long time and before the end new optimization happens. Some new Vms are allocated to
* that host, because there is no knowledge that some VMs are being migrated to that
* host. Therefore, some allocations fail.
*/
PowerHost oldHost = (PowerHost) getVmTable().get(vm.getUid());
// if (oldHost.getId() == 375 && vm.getId() == 383) {
// Log.printLine("found");
// }
// PowerHost oldHost = vm.getHost();
PowerHost allocatedHost = findHostForVm(vm);
// if (allocatedHost.getId() == 235 && vm.getId() == 383) {
// Log.printLine("found");
// }
if (allocatedHost != null) {
// if (vm.getId() == 60 && allocatedHost.getId() == 51) {
// Log.printLine(vm.getId());
// }
Log.printLine("VM #" + vm.getId() + " allocated to host #" + allocatedHost.getId());
allocatedHost.vmCreate(vm);
if (allocatedHost.getId() != oldHost.getId()) {
Map<String, Object> migrate = new HashMap<String, Object>();
migrate.put("vm", vm);
migrate.put("host", allocatedHost);
migrationMap.add(migrate);
// getVmTable().put(vm.getUid(), allocatedHost);
// vmsToRestore.remove(vm);
// Log.printLine("Skip restore of VM #" + vm.getId());
}
}
}
restoreAllocation(vmsToRestore, getHostList());
return migrationMap;
}
}
...@@ -147,7 +147,7 @@ public class TimeSharedProblemDetector { ...@@ -147,7 +147,7 @@ public class TimeSharedProblemDetector {
Log.printLine("CloudSimExample1 finished!"); Log.printLine("CloudSimExample1 finished!");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Log.printLine("Unwanted errors happen"); Log.printLine("The simulation has been terminated due to an unexpected error");
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment