Index: DCWoRMS/branches/coolemall/src/example/localplugin/coolemall/computebox2/CB2_FCFS_ConsolidationLowPower_PowerCapping_SP.java
===================================================================
--- DCWoRMS/branches/coolemall/src/example/localplugin/coolemall/computebox2/CB2_FCFS_ConsolidationLowPower_PowerCapping_SP.java	(revision 1319)
+++ DCWoRMS/branches/coolemall/src/example/localplugin/coolemall/computebox2/CB2_FCFS_ConsolidationLowPower_PowerCapping_SP.java	(revision 1319)
@@ -0,0 +1,482 @@
+package example.localplugin.coolemall.computebox2;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import schedframe.events.scheduling.SchedulingEvent;
+import schedframe.exceptions.ResourceException;
+import schedframe.resources.CoolEmAllResourceType;
+import schedframe.resources.ResourceStatus;
+import schedframe.resources.StandardResourceType;
+import schedframe.resources.computing.ComputingResource;
+import schedframe.resources.computing.Core;
+import schedframe.resources.computing.Node;
+import schedframe.resources.computing.Processor;
+import schedframe.resources.computing.Rack;
+import schedframe.resources.computing.coolemall.ComputeBox1;
+import schedframe.resources.computing.coolemall.NodeGroup;
+import schedframe.resources.computing.profiles.energy.airthroughput.UserAirThroughputStateName;
+import schedframe.resources.computing.profiles.energy.power.PState;
+import schedframe.resources.devices.Device;
+import schedframe.resources.devices.Fan;
+import schedframe.resources.units.ProcessingElements;
+import schedframe.resources.units.ResourceUnit;
+import schedframe.resources.units.ResourceUnitName;
+import schedframe.resources.units.StandardResourceUnitName;
+import schedframe.scheduling.manager.resources.ClusterResourceManager;
+import schedframe.scheduling.manager.resources.ResourceManager;
+import schedframe.scheduling.manager.tasks.JobRegistry;
+import schedframe.scheduling.manager.tasks.JobRegistryImpl;
+import schedframe.scheduling.plan.SchedulingPlanInterface;
+import schedframe.scheduling.plan.impl.SchedulingPlan;
+import schedframe.scheduling.plugin.grid.ModuleList;
+import schedframe.scheduling.queue.TaskQueue;
+import schedframe.scheduling.queue.TaskQueueList;
+import schedframe.scheduling.tasks.TaskInterface;
+import test.Node_Fan_Mapping;
+import example.energy.coolemall.CoolEmAllTestbedMeasurements;
+import example.localplugin.BaseLocalSchedulingPlugin;
+import gridsim.dcworms.DCWormsTags;
+
+public class CB2_FCFS_ConsolidationLowPower_PowerCapping_SP extends BaseLocalSchedulingPlugin {
+
+	private Map<String, Double> powerCapsLevels;
+	private Map<String, Double> powerUpgradesLevels;
+	private Map<String, Double> currentFrequencyLevels;
+	
+	public CB2_FCFS_ConsolidationLowPower_PowerCapping_SP () {
+		Node_Fan_Mapping.init();
+		currentFrequencyLevels = new HashMap<String, Double>();
+		powerCapsLevels  = new HashMap<String, Double>();
+		powerUpgradesLevels  = new HashMap<String, Double>();
+	}
+
+	public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
+			 ResourceManager resManager, ModuleList modules) {
+
+		ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
+		SchedulingPlan plan = new SchedulingPlan();
+		// choose the events types to serve.
+		// Different actions for different events are possible.
+		switch (event.getType()) {
+		case START_TASK_EXECUTION:
+		case TASK_FINISHED:
+		//case TIMER:
+			// our tasks are placed only in first queue (see
+			// BaseLocalSchedulingPlugin.placeJobsInQueues() method)
+			TaskQueue q = queues.get(0);
+			
+			// check all tasks in queue
+			for (int i = 0; i < q.size(); i++) {
+				TaskInterface<?> task = q.get(i);
+				// if status of the tasks in READY
+				if (task.getStatus() == DCWormsTags.READY) {
+
+					Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
+					if (choosenResources != null) {
+						addToSchedulingPlan(plan, task, choosenResources);
+					}
+				}
+			}
+			break;
+		case POWER_LIMIT_EXCEEDED:
+			powerCap(resManager);
+			break;
+		}
+		return plan;
+	}
+
+	private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
+			ClusterResourceManager resourceManager, TaskInterface<?> task) {
+
+		Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
+		
+		List<Node> nodes = resourceManager.getNodes();
+		List<Node> avNodes = filterNodes(nodes, task);
+		Collections.sort(avNodes, new PowerComparator());
+		if(avNodes.size() == 0)
+			return null;
+		Node node = findTheMostLoadedNode(avNodes);
+		int cpuRequest;
+		try {
+			cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
+		} catch (NoSuchFieldException e) {
+			cpuRequest = 0;
+		}
+
+		if (cpuRequest != 0) {
+
+			List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();	
+			
+			List<Core> cores = node.getCores();			
+			for (int i = 0; i < cores.size() && cpuRequest > 0; i++) {
+				if (cores.get(i).getStatus() == ResourceStatus.FREE) {
+					choosenResources.add(cores.get(i));
+					cpuRequest--;
+				}
+			}
+			if (cpuRequest > 0) {
+				return null;
+			}
+			//choosenResources.add(node.getProcessors().get(0));
+			ProcessingElements pe = new ProcessingElements();
+			pe.addAll(choosenResources);
+			map.put(StandardResourceUnitName.PE, pe);
+			return map;
+		}
+		return null;
+	}
+	
+	private List<Node> filterNodes(List<Node> nodes, TaskInterface<?> task){
+		List<Node> filteredNodes = new ArrayList<Node>();
+		int cpuRequest;
+		try {
+			cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
+		} catch (NoSuchFieldException e) {
+			cpuRequest = 0;
+		}
+		for (Node node : nodes) {
+
+			if (cpuRequest != 0) {
+
+				List<Core> cores = node.getCores();
+				if (cores.size() < cpuRequest) {
+					if(cores.size() == 0){
+						if(node.getProcessors().size() < cpuRequest)
+							continue;
+					}
+				}
+
+				int freeCores = 0;
+				for(Core core: cores){
+					if(core.getStatus() == ResourceStatus.FREE)
+						freeCores++;
+				}
+				
+				if(freeCores < cpuRequest)
+					continue;
+				
+				filteredNodes.add(node);
+			}
+		}
+		
+		return filteredNodes;
+	}
+	
+	private Node findTheMostLoadedNode(List<Node> nodes){
+		Node mostLoadedNode = null;
+			
+		int maxRunningTask = -1;
+		int nrRunningTasks;
+		for(Node node: nodes){
+			JobRegistry jr = new JobRegistryImpl(node.getFullName());
+			nrRunningTasks = jr.getRunningTasks().size(); 
+			if(nrRunningTasks > maxRunningTask){
+				mostLoadedNode = node;
+				maxRunningTask = nrRunningTasks;
+			}
+		}
+		return mostLoadedNode;
+	}
+	
+
+	class PowerComparator implements Comparator<Node>{
+		
+	    public int compare(Node node1, Node node2){    
+	    	double node1Rank = Double.MAX_VALUE;
+	    	double node2Rank = Double.MAX_VALUE;
+
+    		PState pState;
+	    	for(Processor proc: node1.getProcessors()){
+	    		if(proc.getPowerInterface() != null) {
+		    		pState = proc.getPowerInterface().getLowestPState();
+		    		if(pState != null && pState.getPower() < node1Rank){
+		    			node1Rank = proc.getPowerInterface().getLowestPState().getFrequency();
+		    		}
+	    		}
+	    	}
+	    	for(Processor proc: node2.getProcessors()){
+	    		if(proc.getPowerInterface() != null) {
+		    		pState = proc.getPowerInterface().getLowestPState();
+		    		if(pState != null && pState.getPower() < node2Rank){
+		    			node2Rank = proc.getPowerInterface().getLowestPState().getFrequency();
+		    		}
+	    		}
+	    	}
+	    	
+	        if(node1Rank > node2Rank)
+	        	return 1;
+	        else if (node1Rank < node2Rank)
+	        	return -1;
+	        else return 0;	   
+	    }
+	}
+	
+	private void powerCap(ResourceManager resManager){
+		try {
+			
+			for(ComputingResource compRes: resManager.getResourcesOfType(StandardResourceType.Rack)){
+				if(!powerCapsLevels.containsKey(compRes.getFullName()) || !powerUpgradesLevels.containsKey(compRes.getFullName())){
+					loadThresholds(compRes);
+				}
+				double powerConsumption = compRes.getPowerInterface().getRecentPowerUsage().getValue();
+				//System.out.println(compRes.getFullName() + " current: " + powerConsumption);
+				if(powerConsumption > powerCapsLevels.get(compRes.getFullName())){
+					ComputeBox1 cb1 = (ComputeBox1) compRes;
+					List<Processor> processors = cb1.getProcessors();
+					
+					double currentFrequencyLevel = 0;
+					if(currentFrequencyLevels.get(compRes.getFullName()) != null){
+						currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+					} else {
+						currentFrequencyLevel = processors.get(0).getPowerInterface().getLowestPState().getFrequency();
+					}
+					boolean overThreshold = true;
+					boolean canDoBetter = true;
+
+					while(overThreshold && canDoBetter){
+						//System.out.println(compRes.getFullName() + " - downgrading");
+						int procNumberWithoutChange = 0;
+						boolean improvement = false;
+						//System.out.println(compRes.getFullName() + "; currentFrequencyLevel: " + currentFrequencyLevel);
+						for (Processor proc: processors){
+							PState currPState = proc.getPowerInterface().getPState();
+							//System.out.println(proc.getFullName() + " - checking");
+							if(currPState.getFrequency() <= currentFrequencyLevel && proc.getFreeCores().size() != proc.getCores().size()){
+								//System.out.println("operating same level");
+								procNumberWithoutChange++;
+								continue;
+							} 
+							if (proc.getFreeCores().size() == proc.getCores().size()){	
+								//System.out.println("free");
+								procNumberWithoutChange++;
+								continue;
+							} 
+			
+							double lowerFrequency = proc.getPowerInterface().getHighestPState().getFrequency();
+							for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+								PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+								if(pState.getFrequency() > lowerFrequency && pState.getFrequency() < currPState.getFrequency()){
+									lowerFrequency = pState.getFrequency();
+								}
+							}
+							//System.out.println(proc.getFullName() + "; lower frequency: " + lowerFrequency);
+							if(lowerFrequency < currentFrequencyLevel){
+								//System.out.println(compRes.getFullName() + " - changing current frequency level");
+								currentFrequencyLevels.put(compRes.getFullName(), lowerFrequency);
+								currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+							}
+							
+							if(lowerFrequency != currPState.getFrequency()) {
+								//System.out.println(proc.getFullName() + " - lowering frequency");
+								proc.getPowerInterface().setFrequency(lowerFrequency);
+								Node n = proc.getNode();
+								for(Device device: n.getParent().getResourceCharacteristic().getDevices()){
+									
+									if(device.getType().equals(StandardResourceType.Fan) || device.getType().equals(StandardResourceType.Inlet) | device.getType().equals(StandardResourceType.Outlet)){
+										Fan fan = (Fan) device;
+
+										if(fan.getChilledResources().contains(n.getFullName())){
+											fan.getAirThroughputInterface().setAirThroughputState(new UserAirThroughputStateName("2"));
+										}
+									}
+								}
+								improvement = true;
+							}
+							if(lowerFrequency == proc.getPowerInterface().getHighestPState().getFrequency()){
+								canDoBetter = false;
+							} else {
+								canDoBetter = true;
+							}
+							if(compRes.getPowerInterface().getRecentPowerUsage().getValue() <= powerCapsLevels.get(compRes.getFullName())){
+								overThreshold = false;
+								break;
+							}
+						}	
+						if(procNumberWithoutChange == processors.size() || improvement == false){
+							//System.out.println(compRes.getFullName() + " - failed to improve");
+							double nextFrequencyLevel = processors.get(0).getPowerInterface().getHighestPState().getFrequency();
+							for (Processor proc: processors){
+								for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+									PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+									if(pState.getFrequency() > nextFrequencyLevel && pState.getFrequency() < currentFrequencyLevel){
+										nextFrequencyLevel = pState.getFrequency();
+									} 
+								}
+							}
+							//System.out.println(compRes.getFullName() + "; nextFrequencyLevel: " + nextFrequencyLevel);
+							currentFrequencyLevels.put(compRes.getFullName(), nextFrequencyLevel);
+							currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+							if(currentFrequencyLevel == processors.get(0).getPowerInterface().getHighestPState().getFrequency()){
+								canDoBetter = false;
+							}
+							procNumberWithoutChange = 0;
+						}
+					}
+
+				} else if (powerConsumption < powerUpgradesLevels.get(compRes.getFullName())) {
+					ComputeBox1 cb1 = (ComputeBox1) compRes;
+					List<Processor> processors = cb1.getProcessors();
+					
+					double currentFrequencyLevel = 0;
+					if(currentFrequencyLevels.get(compRes.getFullName()) != null){
+						currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+					} else {
+						currentFrequencyLevel = processors.get(0).getPowerInterface().getLowestPState().getFrequency();
+					}
+					boolean overThreshold = true;
+					boolean canDoBetter = true;
+
+					while(overThreshold && canDoBetter){
+						//System.out.println(compRes.getFullName() + " - downgrading");
+						int procNumberWithoutChange = 0;
+						boolean improvement = false;
+						//System.out.println(compRes.getFullName() + "; currentFrequencyLevel: " + currentFrequencyLevel);
+						for (Processor proc: processors){
+							PState currPState = proc.getPowerInterface().getPState();
+							//System.out.println(proc.getFullName() + " - checking");
+							if(currPState.getFrequency() >= currentFrequencyLevel && proc.getFreeCores().size() != proc.getCores().size()){
+								//System.out.println("operating same level");
+								procNumberWithoutChange++;
+								continue;
+							} 
+							if (proc.getFreeCores().size() == proc.getCores().size()){	
+								//System.out.println("free");
+								procNumberWithoutChange++;
+								continue;
+							} 
+			
+							double higherFrequency = proc.getPowerInterface().getLowestPState().getFrequency();
+							for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+								PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+								if(pState.getFrequency() < higherFrequency && pState.getFrequency() > currPState.getFrequency()){
+									higherFrequency = pState.getFrequency();
+								}
+							}
+							//System.out.println(proc.getFullName() + "; lower frequency: " + lowerFrequency);
+							if(higherFrequency > currentFrequencyLevel){
+								//System.out.println(compRes.getFullName() + " - changing current frequency level");
+								currentFrequencyLevels.put(compRes.getFullName(), higherFrequency);
+								currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+							}
+							
+							if(higherFrequency != currPState.getFrequency()) {
+								//System.out.println(proc.getFullName() + " - lowering frequency");
+								proc.getPowerInterface().setFrequency(higherFrequency);
+								Node n = proc.getNode();
+								for(Device device: n.getParent().getResourceCharacteristic().getDevices()){
+									
+									if(device.getType().equals(StandardResourceType.Fan) || device.getType().equals(StandardResourceType.Inlet) | device.getType().equals(StandardResourceType.Outlet)){
+										Fan fan = (Fan) device;
+
+										if(fan.getChilledResources().contains(n.getFullName())){
+											fan.getAirThroughputInterface().setAirThroughputState(new UserAirThroughputStateName("2"));
+										}
+									}
+								}
+								improvement = true;
+							}
+							if(higherFrequency == proc.getPowerInterface().getLowestPState().getFrequency()){
+								canDoBetter = false;
+							} else {
+								canDoBetter = true;
+							}
+							if(compRes.getPowerInterface().getRecentPowerUsage().getValue() >= powerCapsLevels.get(compRes.getFullName())){
+								overThreshold = false;
+								break;
+							}
+						}
+						if(procNumberWithoutChange == processors.size() || improvement == false){
+							//System.out.println(compRes.getFullName() + " - failed to improve");
+							double nextFrequencyLevel = processors.get(0).getPowerInterface().getLowestPState().getFrequency();
+							for (Processor proc: processors){
+								for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+									PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+									if(pState.getFrequency() < nextFrequencyLevel && pState.getFrequency() > currentFrequencyLevel){
+										nextFrequencyLevel = pState.getFrequency();
+									} 
+								}
+							}
+							//System.out.println(compRes.getFullName() + "; nextFrequencyLevel: " + nextFrequencyLevel);
+							currentFrequencyLevels.put(compRes.getFullName(), nextFrequencyLevel);
+							currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+							if(currentFrequencyLevel == processors.get(0).getPowerInterface().getLowestPState().getFrequency()){
+								canDoBetter = false;
+							}
+							procNumberWithoutChange = 0;
+						}
+					}
+
+				}
+			}
+			
+		} catch (ResourceException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+	}
+	
+	private void loadThresholds(ComputingResource compResource){
+		
+		Rack rack = (Rack) compResource;
+		double pcLevel = 0;
+		double puLevel = 0;
+		for(Processor proc: rack.getProcessors()){
+			PState highestPState =  proc.getPowerInterface().getHighestPState();
+			PState secondHighestPState =  proc.getPowerInterface().getLowestPState();
+			for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+				PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+				if(pState.getFrequency()  < secondHighestPState.getFrequency() && pState.getFrequency() > highestPState.getFrequency()){
+					secondHighestPState = pState;
+				}
+			}
+			double maxMinPowerUsage = 0;
+			double powerUsage = 0;
+			
+			for(Double load: highestPState.getLoadPowerUsage().keySet()){
+				powerUsage = highestPState.getLoadPowerUsage().get(load);
+				if(powerUsage > maxMinPowerUsage){
+					maxMinPowerUsage= powerUsage;				
+				}
+			}
+			pcLevel = pcLevel + maxMinPowerUsage;
+			
+			maxMinPowerUsage = 0;
+			powerUsage = 0;
+			
+			for(Double load: secondHighestPState.getLoadPowerUsage().keySet()){
+				powerUsage = secondHighestPState.getLoadPowerUsage().get(load);
+				if(powerUsage > maxMinPowerUsage){
+					maxMinPowerUsage= powerUsage;				
+				}
+			}
+			puLevel = puLevel + maxMinPowerUsage;
+			
+		}
+		
+		double fpc = 0;
+		List<NodeGroup> nodeGroups = (List<NodeGroup>) rack.getDescendantsByType(CoolEmAllResourceType.NodeGroup);
+		for(NodeGroup nodeGroup: nodeGroups){
+			for(Device device: nodeGroup.getResourceCharacteristic().getDevices()){
+				try {
+					fpc = fpc + device.getAirThroughputInterface().getPowerConsumption(new UserAirThroughputStateName("2"));
+				} catch (NoSuchFieldException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				}
+			}
+
+		}
+		pcLevel = (pcLevel + fpc) / CoolEmAllTestbedMeasurements.POWER_SUPPLY_EFFICIENCY;
+		puLevel = (puLevel + fpc) / CoolEmAllTestbedMeasurements.POWER_SUPPLY_EFFICIENCY;
+
+		puLevel = (pcLevel * pcLevel / puLevel);
+
+		powerCapsLevels.put(compResource.getFullName(), pcLevel);
+		powerUpgradesLevels.put(compResource.getFullName(), puLevel);
+	}
+}
Index: DCWoRMS/branches/coolemall/src/example/localplugin/coolemall/computebox2/CB2_FCFS_LoadBalancing_SP.java
===================================================================
--- DCWoRMS/branches/coolemall/src/example/localplugin/coolemall/computebox2/CB2_FCFS_LoadBalancing_SP.java	(revision 1258)
+++ DCWoRMS/branches/coolemall/src/example/localplugin/coolemall/computebox2/CB2_FCFS_LoadBalancing_SP.java	(revision 1319)
@@ -54,5 +54,4 @@
 	}
 
-	@SuppressWarnings("unchecked")
 	private String chooseProvider(ClusterResourceManager resourceManager) {
 		List<Node> nodes = resourceManager.getNodes();
Index: DCWoRMS/branches/coolemall/src/example/localplugin/coolemall/computebox2/CB2_FCFS_Random_PowerCapping_SP.java
===================================================================
--- DCWoRMS/branches/coolemall/src/example/localplugin/coolemall/computebox2/CB2_FCFS_Random_PowerCapping_SP.java	(revision 1319)
+++ DCWoRMS/branches/coolemall/src/example/localplugin/coolemall/computebox2/CB2_FCFS_Random_PowerCapping_SP.java	(revision 1319)
@@ -0,0 +1,446 @@
+package example.localplugin.coolemall.computebox2;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+import schedframe.events.scheduling.SchedulingEvent;
+import schedframe.exceptions.ResourceException;
+import schedframe.resources.CoolEmAllResourceType;
+import schedframe.resources.ResourceStatus;
+import schedframe.resources.StandardResourceType;
+import schedframe.resources.computing.ComputingResource;
+import schedframe.resources.computing.Core;
+import schedframe.resources.computing.Node;
+import schedframe.resources.computing.Processor;
+import schedframe.resources.computing.Rack;
+import schedframe.resources.computing.coolemall.ComputeBox1;
+import schedframe.resources.computing.coolemall.NodeGroup;
+import schedframe.resources.computing.profiles.energy.airthroughput.UserAirThroughputStateName;
+import schedframe.resources.computing.profiles.energy.power.PState;
+import schedframe.resources.devices.Device;
+import schedframe.resources.devices.Fan;
+import schedframe.resources.units.ProcessingElements;
+import schedframe.resources.units.ResourceUnit;
+import schedframe.resources.units.ResourceUnitName;
+import schedframe.resources.units.StandardResourceUnitName;
+import schedframe.scheduling.manager.resources.ClusterResourceManager;
+import schedframe.scheduling.manager.resources.ResourceManager;
+import schedframe.scheduling.manager.tasks.JobRegistry;
+import schedframe.scheduling.plan.SchedulingPlanInterface;
+import schedframe.scheduling.plan.impl.SchedulingPlan;
+import schedframe.scheduling.plugin.grid.ModuleList;
+import schedframe.scheduling.queue.TaskQueue;
+import schedframe.scheduling.queue.TaskQueueList;
+import schedframe.scheduling.tasks.TaskInterface;
+import test.Node_Fan_Mapping;
+import example.energy.coolemall.CoolEmAllTestbedMeasurements;
+import example.localplugin.BaseLocalSchedulingPlugin;
+import gridsim.dcworms.DCWormsTags;
+
+public class CB2_FCFS_Random_PowerCapping_SP extends BaseLocalSchedulingPlugin {
+
+	private Random rand;
+	
+	private Map<String, Double> powerCapsLevels;
+	private Map<String, Double> powerUpgradesLevels;
+	private Map<String, Double> currentFrequencyLevels;
+	
+	public CB2_FCFS_Random_PowerCapping_SP () {
+		rand = new Random(47);
+		Node_Fan_Mapping.init();
+		currentFrequencyLevels = new HashMap<String, Double>();
+		powerCapsLevels  = new HashMap<String, Double>();
+		powerUpgradesLevels  = new HashMap<String, Double>();
+	}
+
+	public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
+			 ResourceManager resManager, ModuleList modules) {
+
+		ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
+		SchedulingPlan plan = new SchedulingPlan();
+		// choose the events types to serve.
+		// Different actions for different events are possible.
+		switch (event.getType()) {
+		case START_TASK_EXECUTION:
+		case TASK_FINISHED:
+		//case TIMER:
+			// our tasks are placed only in first queue (see
+			// BaseLocalSchedulingPlugin.placeJobsInQueues() method)
+			TaskQueue q = queues.get(0);
+			
+			// check all tasks in queue
+			for (int i = 0; i < q.size(); i++) {
+				TaskInterface<?> task = q.get(i);
+				// if status of the tasks in READY
+				if (task.getStatus() == DCWormsTags.READY) {
+
+					try {
+						if(resourceManager.getResourcesByTypeWithStatus(StandardResourceType.Core, ResourceStatus.FREE).size() == 0)
+							break;
+					} catch (ResourceException e) {
+						// TODO Auto-generated catch block
+						e.printStackTrace();
+					}
+					Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
+					if (choosenResources != null) {
+						addToSchedulingPlan(plan, task, choosenResources);
+					}
+				}
+			}
+			break;
+		case POWER_LIMIT_EXCEEDED:
+			powerCap(resManager);
+			break;
+		}
+		
+		return plan;
+	}
+
+	private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
+			ClusterResourceManager resourceManager, TaskInterface<?> task) {
+
+		Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
+		
+		List<Node> nodes = resourceManager.getNodes();
+		List<Node> avNodes = filterNodes(nodes, task);
+		if(avNodes.size() == 0)
+			return null;
+		Node node = randomNode(avNodes);
+		int cpuRequest;
+		try {
+			cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
+		} catch (NoSuchFieldException e) {
+			cpuRequest = 0;
+		}
+
+		if (cpuRequest != 0) {
+
+			List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();	
+			List<Core> cores = node.getCores();			
+			for (int i = 0; i < cores.size() && cpuRequest > 0; i++) {
+				if (cores.get(i).getStatus() == ResourceStatus.FREE) {
+					choosenResources.add(cores.get(i));
+					cpuRequest--;
+				}
+			}
+			if (cpuRequest > 0) {
+				return null;
+			}
+
+			//choosenResources.add(node.getProcessors().get(0));
+			ProcessingElements pe = new ProcessingElements();
+			pe.addAll(choosenResources);
+			map.put(StandardResourceUnitName.PE, pe);
+			return map;
+		}
+		return null;
+	}
+	
+	private List<Node> filterNodes(List<Node> nodes, TaskInterface<?> task){
+		List<Node> filteredNodes = new ArrayList<Node>();
+		int cpuRequest;
+		try {
+			cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
+		} catch (NoSuchFieldException e) {
+			cpuRequest = 0;
+		}
+		for (Node node : nodes) {
+
+			if (cpuRequest != 0) {
+
+				List<Core> cores = node.getCores();
+				if (cores.size() < cpuRequest) {
+					if(cores.size() == 0){
+						if(node.getProcessors().size() < cpuRequest)
+							continue;
+					}
+				}
+
+				int freeCores = 0;
+				for(Core core: cores){
+					if(core.getStatus() == ResourceStatus.FREE)
+						freeCores++;
+				}
+				
+				if(freeCores < cpuRequest)
+					continue;
+				
+				filteredNodes.add(node);
+			}
+		}
+		
+		return filteredNodes;
+	}
+	
+	private Node randomNode(List<Node> nodes){
+		return nodes.get(rand.nextInt(nodes.size()));
+	}
+	
+	private void powerCap(ResourceManager resManager){
+		try {
+			
+			for(ComputingResource compRes: resManager.getResourcesOfType(StandardResourceType.Rack)){
+				if(!powerCapsLevels.containsKey(compRes.getFullName()) || !powerUpgradesLevels.containsKey(compRes.getFullName())){
+					loadThresholds(compRes);
+				}
+				double powerConsumption = compRes.getPowerInterface().getRecentPowerUsage().getValue();
+				//System.out.println(compRes.getFullName() + " current: " + powerConsumption);
+				if(powerConsumption > powerCapsLevels.get(compRes.getFullName())){
+					ComputeBox1 cb1 = (ComputeBox1) compRes;
+					List<Processor> processors = cb1.getProcessors();
+					
+					double currentFrequencyLevel = 0;
+					if(currentFrequencyLevels.get(compRes.getFullName()) != null){
+						currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+					} else {
+						currentFrequencyLevel = processors.get(0).getPowerInterface().getLowestPState().getFrequency();
+					}
+					boolean overThreshold = true;
+					boolean canDoBetter = true;
+
+					while(overThreshold && canDoBetter){
+						//System.out.println(compRes.getFullName() + " - downgrading");
+						int procNumberWithoutChange = 0;
+						boolean improvement = false;
+						//System.out.println(compRes.getFullName() + "; currentFrequencyLevel: " + currentFrequencyLevel);
+						for (Processor proc: processors){
+							PState currPState = proc.getPowerInterface().getPState();
+							//System.out.println(proc.getFullName() + " - checking");
+							if(currPState.getFrequency() <= currentFrequencyLevel && proc.getFreeCores().size() != proc.getCores().size()){
+								//System.out.println("operating same level");
+								procNumberWithoutChange++;
+								continue;
+							} 
+							if (proc.getFreeCores().size() == proc.getCores().size()){	
+								//System.out.println("free");
+								procNumberWithoutChange++;
+								continue;
+							} 
+			
+							double lowerFrequency = proc.getPowerInterface().getHighestPState().getFrequency();
+							for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+								PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+								if(pState.getFrequency() > lowerFrequency && pState.getFrequency() < currPState.getFrequency()){
+									lowerFrequency = pState.getFrequency();
+								}
+							}
+							//System.out.println(proc.getFullName() + "; lower frequency: " + lowerFrequency);
+							if(lowerFrequency < currentFrequencyLevel){
+								//System.out.println(compRes.getFullName() + " - changing current frequency level");
+								currentFrequencyLevels.put(compRes.getFullName(), lowerFrequency);
+								currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+							}
+							
+							if(lowerFrequency != currPState.getFrequency()) {
+								//System.out.println(proc.getFullName() + " - lowering frequency");
+								proc.getPowerInterface().setFrequency(lowerFrequency);
+								Node n = proc.getNode();
+								for(Device device: n.getParent().getResourceCharacteristic().getDevices()){
+									
+									if(device.getType().equals(StandardResourceType.Fan) || device.getType().equals(StandardResourceType.Inlet) | device.getType().equals(StandardResourceType.Outlet)){
+										Fan fan = (Fan) device;
+
+										if(fan.getChilledResources().contains(n.getFullName())){
+											fan.getAirThroughputInterface().setAirThroughputState(new UserAirThroughputStateName("2"));
+										}
+									}
+								}
+								improvement = true;
+							}
+							if(lowerFrequency == proc.getPowerInterface().getHighestPState().getFrequency()){
+								canDoBetter = false;
+							} else {
+								canDoBetter = true;
+							}
+							if(compRes.getPowerInterface().getRecentPowerUsage().getValue() <= powerCapsLevels.get(compRes.getFullName())){
+								overThreshold = false;
+								break;
+							}
+						}	
+						if(procNumberWithoutChange == processors.size() || improvement == false){
+							//System.out.println(compRes.getFullName() + " - failed to improve");
+							double nextFrequencyLevel = processors.get(0).getPowerInterface().getHighestPState().getFrequency();
+							for (Processor proc: processors){
+								for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+									PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+									if(pState.getFrequency() > nextFrequencyLevel && pState.getFrequency() < currentFrequencyLevel){
+										nextFrequencyLevel = pState.getFrequency();
+									} 
+								}
+							}
+							//System.out.println(compRes.getFullName() + "; nextFrequencyLevel: " + nextFrequencyLevel);
+							currentFrequencyLevels.put(compRes.getFullName(), nextFrequencyLevel);
+							currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+							if(currentFrequencyLevel == processors.get(0).getPowerInterface().getHighestPState().getFrequency()){
+								canDoBetter = false;
+							}
+							procNumberWithoutChange = 0;
+						}
+					}
+
+				} else if (powerConsumption < powerUpgradesLevels.get(compRes.getFullName())) {
+					ComputeBox1 cb1 = (ComputeBox1) compRes;
+					List<Processor> processors = cb1.getProcessors();
+					
+					double currentFrequencyLevel = 0;
+					if(currentFrequencyLevels.get(compRes.getFullName()) != null){
+						currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+					} else {
+						currentFrequencyLevel = processors.get(0).getPowerInterface().getLowestPState().getFrequency();
+					}
+					boolean overThreshold = true;
+					boolean canDoBetter = true;
+
+					while(overThreshold && canDoBetter){
+						//System.out.println(compRes.getFullName() + " - downgrading");
+						int procNumberWithoutChange = 0;
+						boolean improvement = false;
+						//System.out.println(compRes.getFullName() + "; currentFrequencyLevel: " + currentFrequencyLevel);
+						for (Processor proc: processors){
+							PState currPState = proc.getPowerInterface().getPState();
+							//System.out.println(proc.getFullName() + " - checking");
+							if(currPState.getFrequency() >= currentFrequencyLevel && proc.getFreeCores().size() != proc.getCores().size()){
+								//System.out.println("operating same level");
+								procNumberWithoutChange++;
+								continue;
+							} 
+							if (proc.getFreeCores().size() == proc.getCores().size()){	
+								//System.out.println("free");
+								procNumberWithoutChange++;
+								continue;
+							} 
+			
+							double higherFrequency = proc.getPowerInterface().getLowestPState().getFrequency();
+							for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+								PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+								if(pState.getFrequency() < higherFrequency && pState.getFrequency() > currPState.getFrequency()){
+									higherFrequency = pState.getFrequency();
+								}
+							}
+							//System.out.println(proc.getFullName() + "; lower frequency: " + lowerFrequency);
+							if(higherFrequency > currentFrequencyLevel){
+								//System.out.println(compRes.getFullName() + " - changing current frequency level");
+								currentFrequencyLevels.put(compRes.getFullName(), higherFrequency);
+								currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+							}
+							
+							if(higherFrequency != currPState.getFrequency()) {
+								//System.out.println(proc.getFullName() + " - lowering frequency");
+								proc.getPowerInterface().setFrequency(higherFrequency);
+								Node n = proc.getNode();
+								for(Device device: n.getParent().getResourceCharacteristic().getDevices()){
+									
+									if(device.getType().equals(StandardResourceType.Fan) || device.getType().equals(StandardResourceType.Inlet) | device.getType().equals(StandardResourceType.Outlet)){
+										Fan fan = (Fan) device;
+
+										if(fan.getChilledResources().contains(n.getFullName())){
+											fan.getAirThroughputInterface().setAirThroughputState(new UserAirThroughputStateName("2"));
+										}
+									}
+								}
+								improvement = true;
+							}
+							if(higherFrequency == proc.getPowerInterface().getLowestPState().getFrequency()){
+								canDoBetter = false;
+							} else {
+								canDoBetter = true;
+							}
+							if(compRes.getPowerInterface().getRecentPowerUsage().getValue() >= powerCapsLevels.get(compRes.getFullName())){
+								overThreshold = false;
+								break;
+							}
+						}
+						if(procNumberWithoutChange == processors.size() || improvement == false){
+							//System.out.println(compRes.getFullName() + " - failed to improve");
+							double nextFrequencyLevel = processors.get(0).getPowerInterface().getLowestPState().getFrequency();
+							for (Processor proc: processors){
+								for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+									PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+									if(pState.getFrequency() < nextFrequencyLevel && pState.getFrequency() > currentFrequencyLevel){
+										nextFrequencyLevel = pState.getFrequency();
+									} 
+								}
+							}
+							//System.out.println(compRes.getFullName() + "; nextFrequencyLevel: " + nextFrequencyLevel);
+							currentFrequencyLevels.put(compRes.getFullName(), nextFrequencyLevel);
+							currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+							if(currentFrequencyLevel == processors.get(0).getPowerInterface().getLowestPState().getFrequency()){
+								canDoBetter = false;
+							}
+							procNumberWithoutChange = 0;
+						}
+					}
+
+				}
+			}
+			
+		} catch (ResourceException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+	}
+	
+	private void loadThresholds(ComputingResource compResource){
+		
+		Rack rack = (Rack) compResource;
+		double pcLevel = 0;
+		double puLevel = 0;
+		for(Processor proc: rack.getProcessors()){
+			PState highestPState =  proc.getPowerInterface().getHighestPState();
+			PState secondHighestPState =  proc.getPowerInterface().getLowestPState();
+			for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+				PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+				if(pState.getFrequency()  < secondHighestPState.getFrequency() && pState.getFrequency() > highestPState.getFrequency()){
+					secondHighestPState = pState;
+				}
+			}
+			double maxMinPowerUsage = 0;
+			double powerUsage = 0;
+			
+			for(Double load: highestPState.getLoadPowerUsage().keySet()){
+				powerUsage = highestPState.getLoadPowerUsage().get(load);
+				if(powerUsage > maxMinPowerUsage){
+					maxMinPowerUsage= powerUsage;				
+				}
+			}
+			pcLevel = pcLevel + maxMinPowerUsage;
+			
+			maxMinPowerUsage = 0;
+			powerUsage = 0;
+			
+			for(Double load: secondHighestPState.getLoadPowerUsage().keySet()){
+				powerUsage = secondHighestPState.getLoadPowerUsage().get(load);
+				if(powerUsage > maxMinPowerUsage){
+					maxMinPowerUsage= powerUsage;				
+				}
+			}
+			puLevel = puLevel + maxMinPowerUsage;
+			
+		}
+		
+		double fpc = 0;
+		List<NodeGroup> nodeGroups = (List<NodeGroup>) rack.getDescendantsByType(CoolEmAllResourceType.NodeGroup);
+		for(NodeGroup nodeGroup: nodeGroups){
+			for(Device device: nodeGroup.getResourceCharacteristic().getDevices()){
+				try {
+					fpc = fpc + device.getAirThroughputInterface().getPowerConsumption(new UserAirThroughputStateName("2"));
+				} catch (NoSuchFieldException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				}
+			}
+		}
+		
+		pcLevel = (pcLevel + fpc) / CoolEmAllTestbedMeasurements.POWER_SUPPLY_EFFICIENCY;
+		puLevel = (puLevel + fpc) / CoolEmAllTestbedMeasurements.POWER_SUPPLY_EFFICIENCY;
+
+		puLevel = (pcLevel * pcLevel / puLevel);
+
+		powerCapsLevels.put(compResource.getFullName(), pcLevel);
+		powerUpgradesLevels.put(compResource.getFullName(), puLevel);
+	}
+}
+
Index: DCWoRMS/branches/coolemall/src/example/localplugin/coolemall/computebox2/CB2_FCFS_ConsolidationHighPerf_PowerCapping_SP.java
===================================================================
--- DCWoRMS/branches/coolemall/src/example/localplugin/coolemall/computebox2/CB2_FCFS_ConsolidationHighPerf_PowerCapping_SP.java	(revision 1319)
+++ DCWoRMS/branches/coolemall/src/example/localplugin/coolemall/computebox2/CB2_FCFS_ConsolidationHighPerf_PowerCapping_SP.java	(revision 1319)
@@ -0,0 +1,564 @@
+package example.localplugin.coolemall.computebox2;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import schedframe.events.scheduling.SchedulingEvent;
+import schedframe.exceptions.ResourceException;
+import schedframe.resources.CoolEmAllResourceType;
+import schedframe.resources.ResourceStatus;
+import schedframe.resources.StandardResourceType;
+import schedframe.resources.computing.ComputingResource;
+import schedframe.resources.computing.Core;
+import schedframe.resources.computing.Node;
+import schedframe.resources.computing.Processor;
+import schedframe.resources.computing.Rack;
+import schedframe.resources.computing.coolemall.ComputeBox1;
+import schedframe.resources.computing.coolemall.NodeGroup;
+import schedframe.resources.computing.profiles.energy.airthroughput.UserAirThroughputStateName;
+import schedframe.resources.computing.profiles.energy.power.PState;
+import schedframe.resources.devices.Device;
+import schedframe.resources.devices.Fan;
+import schedframe.resources.units.ProcessingElements;
+import schedframe.resources.units.ResourceUnit;
+import schedframe.resources.units.ResourceUnitName;
+import schedframe.resources.units.StandardResourceUnitName;
+import schedframe.scheduling.manager.resources.ClusterResourceManager;
+import schedframe.scheduling.manager.resources.ResourceManager;
+import schedframe.scheduling.manager.tasks.JobRegistry;
+import schedframe.scheduling.manager.tasks.JobRegistryImpl;
+import schedframe.scheduling.plan.SchedulingPlanInterface;
+import schedframe.scheduling.plan.impl.SchedulingPlan;
+import schedframe.scheduling.plugin.grid.ModuleList;
+import schedframe.scheduling.queue.TaskQueue;
+import schedframe.scheduling.queue.TaskQueueList;
+import schedframe.scheduling.tasks.TaskInterface;
+import test.Node_Fan_Mapping;
+import example.energy.coolemall.CoolEmAllTestbedMeasurements;
+import example.localplugin.BaseLocalSchedulingPlugin;
+import gridsim.dcworms.DCWormsTags;
+
+public class CB2_FCFS_ConsolidationHighPerf_PowerCapping_SP extends BaseLocalSchedulingPlugin {
+
+	private Map<String, Double> powerCapsLevels;
+	private Map<String, Double> powerUpgradesLevels;
+	private Map<String, Double> currentFrequencyLevels;
+	
+	public CB2_FCFS_ConsolidationHighPerf_PowerCapping_SP () {
+		Node_Fan_Mapping.init();
+		currentFrequencyLevels = new HashMap<String, Double>();
+		powerCapsLevels  = new HashMap<String, Double>();
+		powerUpgradesLevels  = new HashMap<String, Double>();
+	}
+
+	public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
+			 ResourceManager resManager, ModuleList modules) {
+
+		ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
+		SchedulingPlan plan = new SchedulingPlan();
+		// choose the events types to serve.
+		// Different actions for different events are possible.
+		switch (event.getType()) {
+		case START_TASK_EXECUTION:
+		case TASK_FINISHED:
+		//case TIMER:
+			// our tasks are placed only in first queue (see
+			// BaseLocalSchedulingPlugin.placeJobsInQueues() method)
+			TaskQueue q = queues.get(0);
+			
+			// check all tasks in queue
+			for (int i = 0; i < q.size(); i++) {
+				TaskInterface<?> task = q.get(i);
+				// if status of the tasks in READY
+				if (task.getStatus() == DCWormsTags.READY) {
+
+					Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
+					if (choosenResources != null) {
+						addToSchedulingPlan(plan, task, choosenResources);
+					}
+				}
+			}
+			break;
+		case POWER_LIMIT_EXCEEDED:
+			powerCap(resManager);
+			break;
+		}
+		return plan;
+	}
+
+	private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
+			ClusterResourceManager resourceManager, TaskInterface<?> task) {
+
+		Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
+		
+		List<Node> nodes = resourceManager.getNodes();
+		List<Node> avNodes = filterNodes(nodes, task);
+		Collections.sort(avNodes, new PerformanceComparator());
+		if(avNodes.size() == 0)
+			return null;
+		Node node = findTheMostLoadedNode(avNodes);
+		int cpuRequest;
+		try {
+			cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
+		} catch (NoSuchFieldException e) {
+			cpuRequest = 0;
+		}
+
+		if (cpuRequest != 0) {
+
+			List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();	
+			
+			List<Core> cores = node.getCores();			
+			for (int i = 0; i < cores.size() && cpuRequest > 0; i++) {
+				if (cores.get(i).getStatus() == ResourceStatus.FREE) {
+					choosenResources.add(cores.get(i));
+					cpuRequest--;
+				}
+			}
+			if (cpuRequest > 0) {
+				return null;
+			}
+			//choosenResources.add(node.getProcessors().get(0));
+			ProcessingElements pe = new ProcessingElements();
+			pe.addAll(choosenResources);
+			map.put(StandardResourceUnitName.PE, pe);
+			return map;
+		}
+		return null;
+	}
+	
+	private List<Node> filterNodes(List<Node> nodes, TaskInterface<?> task){
+		List<Node> filteredNodes = new ArrayList<Node>();
+		int cpuRequest;
+		try {
+			cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
+		} catch (NoSuchFieldException e) {
+			cpuRequest = 0;
+		}
+		for (Node node : nodes) {
+
+			if (cpuRequest != 0) {
+
+				List<Core> cores = node.getCores();
+				if (cores.size() < cpuRequest) {
+					if(cores.size() == 0){
+						if(node.getProcessors().size() < cpuRequest)
+							continue;
+					}
+				}
+
+				int freeCores = 0;
+				for(Core core: cores){
+					if(core.getStatus() == ResourceStatus.FREE)
+						freeCores++;
+				}
+				
+				if(freeCores < cpuRequest)
+					continue;
+				
+				filteredNodes.add(node);
+			}
+		}
+		
+		return filteredNodes;
+	}
+	
+	private Node findTheMostLoadedNode(List<Node> nodes){
+		Node mostLoadedNode = null;
+			
+		int maxRunningTask = -1;
+		int nrRunningTasks;
+		for(Node node: nodes){
+			JobRegistry jr = new JobRegistryImpl(node.getFullName());
+			nrRunningTasks = jr.getRunningTasks().size(); 
+			if(nrRunningTasks > maxRunningTask){
+				mostLoadedNode = node;
+				maxRunningTask = nrRunningTasks;
+			}
+		}
+		return mostLoadedNode;
+	}
+	
+
+	class FavourableNodeComparator implements Comparator<Node>{
+		
+	    public int compare(Node node1, Node node2){    
+	    	double node1Rank = Double.MAX_VALUE;
+	    	double node2Rank = Double.MAX_VALUE;
+	    	
+	    	try {
+				String productName = node1.getProcessors().get(0).getResourceCharacteristic().getParameters().get("product").get(0).getContent();
+				if(productName.equals("Fusion G - T40N"))
+					node1Rank = 3;
+				else if(productName.equals("Atom - D510"))
+					node1Rank = 4;
+				else if(productName.equals("Atom - N2600"))
+					node1Rank = 5;
+				else if(productName.equals("Core i7 - 2715QE"))
+					node1Rank = 2;
+				else if(productName.equals("Core i7 - 3615QE"))
+					node1Rank = 1;
+			} catch (Exception e){
+				
+			}
+	    	try {
+				String productName = node2.getProcessors().get(0).getResourceCharacteristic().getParameters().get("product").get(0).getContent();
+				if(productName.equals("Fusion G - T40N"))
+					node2Rank = 3;
+				else if(productName.equals("Atom - D510"))
+					node2Rank = 4;
+				else if(productName.equals("Atom - N2600"))
+					node2Rank = 5;
+				else if(productName.equals("Core i7 - 2715QE"))
+					node2Rank = 2;
+				else if(productName.equals("Core i7 - 3615QE"))
+					node2Rank = 1;
+			} catch (Exception e){
+				
+			}
+	        if(node1Rank > node2Rank)
+	        	return 1;
+	        else if (node1Rank < node2Rank)
+	        	return -1;
+	        else {
+	        	//return 0;
+		    	double node1LoadRank = Double.MIN_VALUE;
+		    	double node2LoadRank = Double.MIN_VALUE;
+		    	
+		    	try {
+		    		Rack rack1 = (Rack) node1.getParent().getParent();
+					JobRegistry jr1 = new JobRegistryImpl(rack1.getFullName());
+		    		node1LoadRank = jr1.getRunningTasks().size(); 
+		    		
+		    		Rack rack2 = (Rack) node2.getParent().getParent();
+					JobRegistry jr2 = new JobRegistryImpl(rack2.getFullName());
+		    		node2LoadRank = jr2.getRunningTasks().size(); 
+			        if(node1LoadRank < node2LoadRank)
+			        	return 1;
+			        else if (node1LoadRank > node2LoadRank)
+			        	return -1;
+			        else return 0;
+		    	} catch (Exception e){
+		    		return 0;
+		    	}
+   
+	        }
+	    }
+	}
+
+	class PerformanceComparator implements Comparator<Node>{
+		
+	    public int compare(Node node1, Node node2){    
+	    	double node1Rank = Double.MIN_VALUE;
+	    	double node2Rank = Double.MIN_VALUE;
+
+	    	double node1Speed = 0;
+	    	for(Processor proc: node1.getProcessors()){
+	    		node1Speed = node1Speed + proc.getMIPS();
+	    	}
+	    	node1Rank = node1Speed / node1.getProcessors().size();
+	    	
+	    	double node2Speed = 0;
+	    	for(Processor proc: node2.getProcessors()){
+	    		node2Speed = node2Speed + proc.getMIPS();
+	    	}
+	    	node2Rank = node2Speed / node2.getProcessors().size();
+	    	
+	    	if(node1Rank == node2Rank){
+	    		node1Rank = Double.MIN_VALUE;
+	    		node2Rank = Double.MIN_VALUE;
+	    		PState pState;
+		    	for(Processor proc: node1.getProcessors()){
+		    		if(proc.getPowerInterface() != null) {
+			    		pState = proc.getPowerInterface().getLowestPState();
+			    		if(pState != null && pState.getFrequency() > node1Rank){
+			    			node1Rank = proc.getPowerInterface().getLowestPState().getFrequency();
+			    		}
+		    		}
+		    	}
+		    	for(Processor proc: node2.getProcessors()){
+		    		if(proc.getPowerInterface() != null) {
+			    		pState = proc.getPowerInterface().getLowestPState();
+			    		if(pState != null && pState.getFrequency() > node2Rank){
+			    			node2Rank = proc.getPowerInterface().getLowestPState().getFrequency();
+			    		}
+		    		}
+		    	}
+	    	}
+	    	
+	        if(node1Rank < node2Rank)
+	        	return 1;
+	        else if (node1Rank > node2Rank)
+	        	return -1;
+	        else return 0;	   
+	    }
+	}	
+	
+	private void powerCap(ResourceManager resManager){
+		try {
+			
+			for(ComputingResource compRes: resManager.getResourcesOfType(StandardResourceType.Rack)){
+				if(!powerCapsLevels.containsKey(compRes.getFullName()) || !powerUpgradesLevels.containsKey(compRes.getFullName())){
+					loadThresholds(compRes);
+				}
+				double powerConsumption = compRes.getPowerInterface().getRecentPowerUsage().getValue();
+				//System.out.println(compRes.getFullName() + " current: " + powerConsumption);
+				if(powerConsumption > powerCapsLevels.get(compRes.getFullName())){
+					ComputeBox1 cb1 = (ComputeBox1) compRes;
+					List<Processor> processors = cb1.getProcessors();
+					
+					double currentFrequencyLevel = 0;
+					if(currentFrequencyLevels.get(compRes.getFullName()) != null){
+						currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+					} else {
+						currentFrequencyLevel = processors.get(0).getPowerInterface().getLowestPState().getFrequency();
+					}
+					boolean overThreshold = true;
+					boolean canDoBetter = true;
+
+					while(overThreshold && canDoBetter){
+						//System.out.println(compRes.getFullName() + " - downgrading");
+						int procNumberWithoutChange = 0;
+						boolean improvement = false;
+						//System.out.println(compRes.getFullName() + "; currentFrequencyLevel: " + currentFrequencyLevel);
+						for (Processor proc: processors){
+							PState currPState = proc.getPowerInterface().getPState();
+							//System.out.println(proc.getFullName() + " - checking");
+							if(currPState.getFrequency() <= currentFrequencyLevel && proc.getFreeCores().size() != proc.getCores().size()){
+								//System.out.println("operating same level");
+								procNumberWithoutChange++;
+								continue;
+							} 
+							if (proc.getFreeCores().size() == proc.getCores().size()){	
+								//System.out.println("free");
+								procNumberWithoutChange++;
+								continue;
+							} 
+			
+							double lowerFrequency = proc.getPowerInterface().getHighestPState().getFrequency();
+							for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+								PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+								if(pState.getFrequency() > lowerFrequency && pState.getFrequency() < currPState.getFrequency()){
+									lowerFrequency = pState.getFrequency();
+								}
+							}
+							//System.out.println(proc.getFullName() + "; lower frequency: " + lowerFrequency);
+							if(lowerFrequency < currentFrequencyLevel){
+								//System.out.println(compRes.getFullName() + " - changing current frequency level");
+								currentFrequencyLevels.put(compRes.getFullName(), lowerFrequency);
+								currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+							}
+							
+							if(lowerFrequency != currPState.getFrequency()) {
+								//System.out.println(proc.getFullName() + " - lowering frequency");
+								proc.getPowerInterface().setFrequency(lowerFrequency);
+								Node n = proc.getNode();
+								for(Device device: n.getParent().getResourceCharacteristic().getDevices()){
+									
+									if(device.getType().equals(StandardResourceType.Fan) || device.getType().equals(StandardResourceType.Inlet) | device.getType().equals(StandardResourceType.Outlet)){
+										Fan fan = (Fan) device;
+
+										if(fan.getChilledResources().contains(n.getFullName())){
+											fan.getAirThroughputInterface().setAirThroughputState(new UserAirThroughputStateName("2"));
+										}
+									}
+								}
+								improvement = true;
+							}
+							if(lowerFrequency == proc.getPowerInterface().getHighestPState().getFrequency()){
+								canDoBetter = false;
+							} else {
+								canDoBetter = true;
+							}
+							if(compRes.getPowerInterface().getRecentPowerUsage().getValue() <= powerCapsLevels.get(compRes.getFullName())){
+								overThreshold = false;
+								break;
+							}
+						}	
+						if(procNumberWithoutChange == processors.size() || improvement == false){
+							//System.out.println(compRes.getFullName() + " - failed to improve");
+							double nextFrequencyLevel = processors.get(0).getPowerInterface().getHighestPState().getFrequency();
+							for (Processor proc: processors){
+								for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+									PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+									if(pState.getFrequency() > nextFrequencyLevel && pState.getFrequency() < currentFrequencyLevel){
+										nextFrequencyLevel = pState.getFrequency();
+									} 
+								}
+							}
+							//System.out.println(compRes.getFullName() + "; nextFrequencyLevel: " + nextFrequencyLevel);
+							currentFrequencyLevels.put(compRes.getFullName(), nextFrequencyLevel);
+							currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+							if(currentFrequencyLevel == processors.get(0).getPowerInterface().getHighestPState().getFrequency()){
+								canDoBetter = false;
+							}
+							procNumberWithoutChange = 0;
+						}
+					}
+
+				} else if (powerConsumption < powerUpgradesLevels.get(compRes.getFullName())) {
+					ComputeBox1 cb1 = (ComputeBox1) compRes;
+					List<Processor> processors = cb1.getProcessors();
+					
+					double currentFrequencyLevel = 0;
+					if(currentFrequencyLevels.get(compRes.getFullName()) != null){
+						currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+					} else {
+						currentFrequencyLevel = processors.get(0).getPowerInterface().getLowestPState().getFrequency();
+					}
+					boolean overThreshold = true;
+					boolean canDoBetter = true;
+
+					while(overThreshold && canDoBetter){
+						//System.out.println(compRes.getFullName() + " - downgrading");
+						int procNumberWithoutChange = 0;
+						boolean improvement = false;
+						//System.out.println(compRes.getFullName() + "; currentFrequencyLevel: " + currentFrequencyLevel);
+						for (Processor proc: processors){
+							PState currPState = proc.getPowerInterface().getPState();
+							//System.out.println(proc.getFullName() + " - checking");
+							if(currPState.getFrequency() >= currentFrequencyLevel && proc.getFreeCores().size() != proc.getCores().size()){
+								//System.out.println("operating same level");
+								procNumberWithoutChange++;
+								continue;
+							} 
+							if (proc.getFreeCores().size() == proc.getCores().size()){	
+								//System.out.println("free");
+								procNumberWithoutChange++;
+								continue;
+							} 
+			
+							double higherFrequency = proc.getPowerInterface().getLowestPState().getFrequency();
+							for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+								PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+								if(pState.getFrequency() < higherFrequency && pState.getFrequency() > currPState.getFrequency()){
+									higherFrequency = pState.getFrequency();
+								}
+							}
+							//System.out.println(proc.getFullName() + "; lower frequency: " + lowerFrequency);
+							if(higherFrequency > currentFrequencyLevel){
+								//System.out.println(compRes.getFullName() + " - changing current frequency level");
+								currentFrequencyLevels.put(compRes.getFullName(), higherFrequency);
+								currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+							}
+							
+							if(higherFrequency != currPState.getFrequency()) {
+								//System.out.println(proc.getFullName() + " - lowering frequency");
+								proc.getPowerInterface().setFrequency(higherFrequency);
+								Node n = proc.getNode();
+								for(Device device: n.getParent().getResourceCharacteristic().getDevices()){
+									
+									if(device.getType().equals(StandardResourceType.Fan) || device.getType().equals(StandardResourceType.Inlet) | device.getType().equals(StandardResourceType.Outlet)){
+										Fan fan = (Fan) device;
+
+										if(fan.getChilledResources().contains(n.getFullName())){
+											fan.getAirThroughputInterface().setAirThroughputState(new UserAirThroughputStateName("2"));
+										}
+									}
+								}
+								improvement = true;
+							}
+							if(higherFrequency == proc.getPowerInterface().getLowestPState().getFrequency()){
+								canDoBetter = false;
+							} else {
+								canDoBetter = true;
+							}
+							if(compRes.getPowerInterface().getRecentPowerUsage().getValue() >= powerCapsLevels.get(compRes.getFullName())){
+								overThreshold = false;
+								break;
+							}
+						}
+						if(procNumberWithoutChange == processors.size() || improvement == false){
+							//System.out.println(compRes.getFullName() + " - failed to improve");
+							double nextFrequencyLevel = processors.get(0).getPowerInterface().getLowestPState().getFrequency();
+							for (Processor proc: processors){
+								for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+									PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+									if(pState.getFrequency() < nextFrequencyLevel && pState.getFrequency() > currentFrequencyLevel){
+										nextFrequencyLevel = pState.getFrequency();
+									} 
+								}
+							}
+							//System.out.println(compRes.getFullName() + "; nextFrequencyLevel: " + nextFrequencyLevel);
+							currentFrequencyLevels.put(compRes.getFullName(), nextFrequencyLevel);
+							currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
+							if(currentFrequencyLevel == processors.get(0).getPowerInterface().getLowestPState().getFrequency()){
+								canDoBetter = false;
+							}
+							procNumberWithoutChange = 0;
+						}
+					}
+
+				}
+			}
+			
+		} catch (ResourceException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+	}
+	
+	private void loadThresholds(ComputingResource compResource){
+		
+		Rack rack = (Rack) compResource;
+		double pcLevel = 0;
+		double puLevel = 0;
+		for(Processor proc: rack.getProcessors()){
+			PState highestPState =  proc.getPowerInterface().getHighestPState();
+			PState secondHighestPState =  proc.getPowerInterface().getLowestPState();
+			for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
+				PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
+				if(pState.getFrequency()  < secondHighestPState.getFrequency() && pState.getFrequency() > highestPState.getFrequency()){
+					secondHighestPState = pState;
+				}
+			}
+			double maxMinPowerUsage = 0;
+			double powerUsage = 0;
+			
+			for(Double load: highestPState.getLoadPowerUsage().keySet()){
+				powerUsage = highestPState.getLoadPowerUsage().get(load);
+				if(powerUsage > maxMinPowerUsage){
+					maxMinPowerUsage= powerUsage;				
+				}
+			}
+			pcLevel = pcLevel + maxMinPowerUsage;
+			
+			maxMinPowerUsage = 0;
+			powerUsage = 0;
+			
+			for(Double load: secondHighestPState.getLoadPowerUsage().keySet()){
+				powerUsage = secondHighestPState.getLoadPowerUsage().get(load);
+				if(powerUsage > maxMinPowerUsage){
+					maxMinPowerUsage= powerUsage;				
+				}
+			}
+			puLevel = puLevel + maxMinPowerUsage;
+			
+		}
+		
+		double fpc = 0;
+		List<NodeGroup> nodeGroups = (List<NodeGroup>) rack.getDescendantsByType(CoolEmAllResourceType.NodeGroup);
+		for(NodeGroup nodeGroup: nodeGroups){
+			for(Device device: nodeGroup.getResourceCharacteristic().getDevices()){
+				try {
+					fpc = fpc + device.getAirThroughputInterface().getPowerConsumption(new UserAirThroughputStateName("2"));
+				} catch (NoSuchFieldException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				}
+			}
+
+		}
+		pcLevel = (pcLevel + fpc) / CoolEmAllTestbedMeasurements.POWER_SUPPLY_EFFICIENCY;
+		puLevel = (puLevel + fpc) / CoolEmAllTestbedMeasurements.POWER_SUPPLY_EFFICIENCY;
+
+		puLevel = (pcLevel * pcLevel / puLevel);
+
+		powerCapsLevels.put(compResource.getFullName(), pcLevel);
+		powerUpgradesLevels.put(compResource.getFullName(), puLevel);
+	}
+}
Index: DCWoRMS/branches/coolemall/src/example/localplugin/coolemall/computebox2/CB2_FCFS_Random_SP.java
===================================================================
--- DCWoRMS/branches/coolemall/src/example/localplugin/coolemall/computebox2/CB2_FCFS_Random_SP.java	(revision 1299)
+++ DCWoRMS/branches/coolemall/src/example/localplugin/coolemall/computebox2/CB2_FCFS_Random_SP.java	(revision 1319)
@@ -79,5 +79,5 @@
 		return plan;
 	}
-	@SuppressWarnings("unchecked")
+
 	private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
 			ClusterResourceManager resourceManager, TaskInterface<?> task) {
