From 77697dd8a7a76121fd9bbf52b5a42c2fafe525e3 Mon Sep 17 00:00:00 2001
From: Stefan Derkits <stefan@derkits.at>
Date: Tue, 11 Jun 2013 12:37:14 +0200
Subject: [PATCH 01/16] Big changes: change EventMap, move the
 stop/stopOutsourced/delayed/start/startOutsourced cylce into
 AbstractScheduler, remove SortedApp & SortedPhysicalMachine

---
 .../tuwien/lsdc/sched/AbstractScheduler.java  |  96 +++++------
 src/at/ac/tuwien/lsdc/sched/SchedulerA.java   | 151 +++++++++---------
 src/at/ac/tuwien/lsdc/sched/SchedulerB.java   | 113 ++++++-------
 src/at/ac/tuwien/lsdc/sched/SchedulerC.java   |  34 ++--
 src/at/ac/tuwien/lsdc/types/Application.java  |   9 +-
 .../types/ApplicationResourceComparator.java  |  27 ++++
 .../ac/tuwien/lsdc/types/PhysicalMachine.java |  52 +++---
 .../tuwien/lsdc/types/SortedApplication.java  |  38 -----
 .../lsdc/types/SortedPhysicalMachine.java     |  32 ----
 9 files changed, 263 insertions(+), 289 deletions(-)
 create mode 100644 src/at/ac/tuwien/lsdc/types/ApplicationResourceComparator.java
 delete mode 100644 src/at/ac/tuwien/lsdc/types/SortedApplication.java
 delete mode 100644 src/at/ac/tuwien/lsdc/types/SortedPhysicalMachine.java

diff --git a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
index e2eb2be..fc0109c 100644
--- a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
+++ b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
@@ -3,6 +3,7 @@ package at.ac.tuwien.lsdc.sched;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.SortedMap;
 import java.util.TreeMap;
@@ -50,7 +51,7 @@ public abstract class AbstractScheduler {
 	 * this map saves the following Type of Events: start of an Application, end of an
 	 * Application(start outSourced, end outSourced, start inSourced, end inSourced)
 	 */
-	protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
+	protected SortedMap<Long, HashMap<EventType, LinkedList<SchedulerEvent>>> eventMap;
 
 	/**
 	 * Scheduler has an internal Time "Abstraction" at every point in time it checks for Events in
@@ -73,7 +74,7 @@ public abstract class AbstractScheduler {
 		manager = new MachineManager(numPMs);
 		this.schedulerLog = schedulerLog;
 		this.scenario = scenario;
-		eventMap = new TreeMap<Long, LinkedList<SchedulerEvent>>();
+		eventMap = new TreeMap<Long, HashMap<EventType, LinkedList<SchedulerEvent>>>();
 		logger = new CSVLogger(schedulerLog);
 		federation = new Federation(numCloudPartners);
 		delayedApps = new ArrayList<Application>();
@@ -96,57 +97,36 @@ public abstract class AbstractScheduler {
 
 	/**
 	 * Insert a start event into the map, at timestamp when the application should start
-	 * 
+	 *
 	 * @param timestamp the timestamp when the application should start
 	 * @param app the application to start
 	 */
 	protected void insertStartEvent(long timestamp, Application app) {
 		SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startApplication, app);
-		if (!eventMap.containsKey(timestamp)) {
-			LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
-			list.add(evt);
-			eventMap.put(timestamp, list);
-		} else {
-			LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
-			list.add(evt);
-		}
+		insertEvent(evt);
 	}
 
 	/**
 	 * Insert a start outsourced event into the map, at timestamp when the application should start
-	 * 
+	 *
 	 * @param timestamp the timestamp when the application should start
 	 * @param app the application to start
 	 */
 	protected void insertOutsourcedStartEvent(long timestamp, Application app) {
 		SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startOutsourcedApplication,
 				app);
-		if (!eventMap.containsKey(timestamp)) {
-			LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
-			list.add(evt);
-			eventMap.put(timestamp, list);
-		} else {
-			LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
-			list.add(evt);
-		}
+		insertEvent(evt);
 	}
 
 	/**
 	 * Insert a stop event into the map, at timestamp when the application should stop.
-	 * 
+	 *
 	 * @param timestamp the timestamp when the application should stop
 	 * @param app the application to stop
 	 */
 	protected void insertStopEvent(long timestamp, Application app) {
 		SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endApplication, app);
-		if (!eventMap.containsKey(timestamp)) {
-			LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
-			list.add(evt);
-			eventMap.put(timestamp, list);
-		} else {
-			LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
-			list.add(evt);
-		}
+		insertEvent(evt);
 		if (endTime < timestamp) {
 			endTime = timestamp;
 		}
@@ -154,25 +134,33 @@ public abstract class AbstractScheduler {
 
 	/**
 	 * Insert a stop event into the map, at timestamp when the application should stop.
-	 * 
+	 *
 	 * @param timestamp the timestamp when the application should stop
 	 * @param app the application to stop
 	 */
 	protected void insertOutsourcedStopEvent(long timestamp, Application app) {
 		SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endOutsourcedApplication, app);
-		if (!eventMap.containsKey(timestamp)) {
-			LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
-			list.add(evt);
-			eventMap.put(timestamp, list);
-		} else {
-			LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
-			list.add(evt);
-		}
+		insertEvent(evt);
 		if (endTime < timestamp) {
 			endTime = timestamp;
 		}
 	}
 
+	private void insertEvent(SchedulerEvent evt) {
+		LinkedList<SchedulerEvent> list;
+		if (!eventMap.containsKey(evt.getTimestamp())) {
+			HashMap<EventType, LinkedList<SchedulerEvent>> map = new HashMap<EventType, LinkedList<SchedulerEvent>>();
+			eventMap.put(evt.getTimestamp(), map);
+		}
+		if (!eventMap.get(evt.getTimestamp()).containsKey(evt.getType())) {
+			list = new LinkedList<SchedulerEvent>();
+			eventMap.get(evt.getTimestamp()).put(evt.getType(), list);
+		} else {
+			list = eventMap.get(evt.getTimestamp()).get(evt.getType());
+		}
+		list.add(evt);
+	}
+
 	/**
 	 * Start the actual scheduling algorithm. Each scheduler will implement the method handleEvents
 	 * differently
@@ -180,9 +168,8 @@ public abstract class AbstractScheduler {
 	protected void startScheduling() {
 		while (true) {
 			if (eventMap.containsKey(currTime)) {
-				LinkedList<SchedulerEvent> events = eventMap.get(currTime);
 				// log.info(events.size() + " events at timestamp " + currTime);
-				handleEvents(events);
+				handleEvents(eventMap.get(currTime));
 			}
 			doStateLogging();
 			// advance Time to next step
@@ -199,14 +186,33 @@ public abstract class AbstractScheduler {
 	/**
 	 * this method is where the Scheduling Algorithm resides it reads the Events (start & stop of
 	 * applications)
-	 * 
+	 *
 	 * @param events the events to be read and used by the scheduler
 	 */
-	protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
+	private void handleEvents(HashMap<EventType, LinkedList<SchedulerEvent>> events) {
+		if (events.containsKey(EventType.endApplication))
+			handleEndEvents(events.get(EventType.endApplication));
+		if (events.containsKey(EventType.endOutsourcedApplication))
+			handleOutsourcedEndEvents(events.get(EventType.endOutsourcedApplication));
+		handleDelayedApps();
+		if (events.containsKey(EventType.startOutsourcedApplication))
+			handleOutsourcedStartEvents(events.get(EventType.startOutsourcedApplication));
+		if (events.containsKey(EventType.startApplication))
+			handleStartEvents(events.get(EventType.startApplication));
+		runMigration();
+	}
+
+	protected abstract void handleEndEvents(LinkedList<SchedulerEvent> events);
+
+	protected abstract void handleDelayedApps();
+
+	protected abstract void handleStartEvents(LinkedList<SchedulerEvent> events);
+
+	protected abstract void runMigration();
 
 	/**
 	 * handle running of outsourced apps.
-	 * 
+	 *
 	 * @param events list of all events that happened in this timeslot.
 	 */
 	protected void handleOutsourcedStartEvents(LinkedList<SchedulerEvent> events) {
@@ -221,7 +227,7 @@ public abstract class AbstractScheduler {
 
 	/**
 	 * handle stopping of outsourced apps.
-	 * 
+	 *
 	 * @param events list of all events that happened in this timeslot.
 	 */
 	protected void handleOutsourcedEndEvents(LinkedList<SchedulerEvent> events) {
@@ -251,7 +257,7 @@ public abstract class AbstractScheduler {
 
 	/**
 	 * this creates the total summary which should be written to a CSV at the end
-	 * 
+	 *
 	 * @return a ScenarioData Object that holds the values to be logged
 	 */
 	protected ScenarioData doEndLogging() {
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
index a4c6eb2..3a0c479 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
@@ -2,11 +2,9 @@ package at.ac.tuwien.lsdc.sched;
 
 import java.io.File;
 import java.io.IOException;
-
-import java.util.Iterator;
+import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
-import java.util.Collections;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -14,14 +12,11 @@ import org.slf4j.LoggerFactory;
 import at.ac.tuwien.lsdc.exception.ActiveApplicationsException;
 import at.ac.tuwien.lsdc.exception.VMsRunningException;
 import at.ac.tuwien.lsdc.types.Application;
+import at.ac.tuwien.lsdc.types.ApplicationResourceComparator;
 import at.ac.tuwien.lsdc.types.PhysicalMachine;
-import at.ac.tuwien.lsdc.types.ScenarioData;
 import at.ac.tuwien.lsdc.types.ScenarioType;
 import at.ac.tuwien.lsdc.types.SchedulerEvent;
-import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
 import at.ac.tuwien.lsdc.types.SchedulerType;
-import at.ac.tuwien.lsdc.types.SortedApplication;
-import at.ac.tuwien.lsdc.types.SortedPhysicalMachine;
 import at.ac.tuwien.lsdc.types.VirtualMachine;
 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
 
@@ -29,138 +24,138 @@ public class SchedulerA extends AbstractScheduler {
 
 	private static final Logger log = LoggerFactory.getLogger(SchedulerA.class);
 
-	public SchedulerA(int numPMs, int numCloudPartners, File schedulerLog,
-			ScenarioType scenario) throws IOException {
+	public SchedulerA(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
+			throws IOException {
 		super(numPMs, numCloudPartners, schedulerLog, scenario);
 
 		this.vmType = VMType.NonResizable;
 	}
 
 	@Override
-	protected void handleEvents(LinkedList<SchedulerEvent> events) {
+	protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
 		for (SchedulerEvent evt : events) {
-			if (evt.getType() == EventType.endApplication) {
-				VirtualMachine vm = evt.getApp().getRunningOn();
-				vm.stopApplication(evt.getApp());
-				PhysicalMachine pm = vm.getRunningOn();
-				try {
-					pm.stopVirtualMachine(vm);
-					if (pm.countCurrentlyRunningVMs() == 0) {
-						try {
-							manager.stopPhysicalMachine(pm.getId());
-						} catch (VMsRunningException e) {
-							log.warn("PM " + pm.getId()
-									+ " could not be stopped, "
-									+ e.getMessage());
-						}
+			VirtualMachine vm = evt.getApp().getRunningOn();
+			vm.stopApplication(evt.getApp());
+			PhysicalMachine pm = vm.getRunningOn();
+			try {
+				pm.stopVirtualMachine(vm);
+				if (pm.countCurrentlyRunningVMs() == 0) {
+					try {
+						manager.stopPhysicalMachine(pm.getId());
+					} catch (VMsRunningException e) {
+						log.warn("PM " + pm.getId() + " could not be stopped, " + e.getMessage());
 					}
-					log.info("application stopped at timestamp " + currTime
-							+ ", " + "vm " + vm.getPositionOnPM() + ", pm "
-							+ pm.getId());
-				} catch (ActiveApplicationsException e) {
-					log.warn("VM " + vm.getId() + "could not be stopped, "
-							+ e.getMessage());
 				}
+				log.info("application stopped at timestamp " + currTime + ", " + "vm "
+						+ vm.getPositionOnPM() + ", pm " + pm.getId());
+			} catch (ActiveApplicationsException e) {
+				log.warn("VM " + vm.getId() + "could not be stopped, " + e.getMessage());
 			}
 		}
+	}
+
+	@Override
+	protected void handleDelayedApps() {
+		// TODO Auto-generated method stub
+
+	}
 
+	@Override
+	protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
 		// sorting applications by amount of resources (descending)
-		List<SortedApplication> sortedApps = sortApps(events);
+		List<Application> sortedApps = sortApps(events);
 
-		for (Iterator<SortedApplication> iter = sortedApps.iterator(); iter
-				.hasNext();) {
+		for (Application app : sortedApps) {
 			boolean appDeployed = false;
-			Application app = iter.next().getApp();
 
 			if (manager.getPMs().size() == 0) {
 				PhysicalMachine pm = manager.startPhysicalMachine();
-				boolean enoughResources = pm.checkVM(app.getSize(),
-						app.getRam(), app.getCpu());
+				boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
 
 				if (enoughResources) {
-					VirtualMachine vm = pm.startVirtualMachine(app.getSize(),
-							app.getRam(), app.getCpu(), vmType);
+					VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(),
+							app.getCpu(), vmType);
 					vm.startApplication(app);
 					insertStopEvent(currTime + app.getDuration(), app);
 					appDeployed = true;
-					log.info("Application " + app.toString()
-							+ " started on new pm " + pm.getId());
+					log.info("Application " + app.toString() + " started on new pm " + pm.getId());
 				} else {
-					log.warn("Application " + app.toString()
-							+ " cannot be run on empty pm " + pm.getId());
+					log.warn("Application " + app.toString() + " cannot be run on empty pm "
+							+ pm.getId());
 				}
 			} else {
 				// sorting physical machines by resource utilization
 				// (descending)
-				List<SortedPhysicalMachine> sortedPMs = sortPMs();
+				List<PhysicalMachine> sortedPMs = sortPMs();
 
-				for (Iterator<SortedPhysicalMachine> it = sortedPMs.iterator(); it
-						.hasNext();) {
+				for (PhysicalMachine pm : sortedPMs) {
 
-					PhysicalMachine pm = it.next().getPm();
-					boolean enoughResources = pm.checkVM(app.getSize(),
-							app.getRam(), app.getCpu());
+					boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
 
 					if (enoughResources) {
-						VirtualMachine vm = pm.startVirtualMachine(
-								app.getSize(), app.getRam(), app.getCpu(),
-								vmType);
+						VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(),
+								app.getCpu(), vmType);
 						vm.startApplication(app);
 						insertStopEvent(currTime + app.getDuration(), app);
 						appDeployed = true;
-						log.info("Application " + app.toString()
-								+ " started new vm " + vm.getPositionOnPM()
-								+ " on pm " + pm.getId());
+						log.info("Application " + app.toString() + " started new vm "
+								+ vm.getPositionOnPM() + " on pm " + pm.getId());
 						break;
 					}
 				}
-				if (!appDeployed
-						&& (manager.getPMs().size() < manager.getMaxPMs())) {
+				if (!appDeployed && (manager.getPMs().size() < manager.getMaxPMs())) {
 
 					PhysicalMachine pm = manager.startPhysicalMachine();
-					boolean enoughResources = pm.checkVM(app.getSize(),
-							app.getRam(), app.getCpu());
+					boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
 
 					if (enoughResources) {
-						VirtualMachine vm = pm.startVirtualMachine(
-								app.getSize(), app.getRam(), app.getCpu(),
-								vmType);
+						VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(),
+								app.getCpu(), vmType);
 						vm.startApplication(app);
 						insertStopEvent(currTime + app.getDuration(), app);
 						appDeployed = true;
-						log.info("Application " + app.toString()
-								+ " started on new pm " + pm.getId());
+						log.info("Application " + app.toString() + " started on new pm "
+								+ pm.getId());
 					} else {
-						log.warn("Application " + app.toString()
-								+ " cannot be run on empty pm " + pm.getId());
+						log.warn("Application " + app.toString() + " cannot be run on empty pm "
+								+ pm.getId());
 					}
 				}
 			}
+			if (!appDeployed) {
+				if (federation.askToOutsource(app)) {
+					insertOutsourcedStartEvent(currTime + 1, app);
+					appDeployed = true;
+				} else
+					delayedApps.add(app);
+			}
 			if (!appDeployed)
-				log.warn("Application " + app.toString()
-						+ " could not be deployed on any pm");
-			// TODO: save app in a List and try to deploy with next handled
-			// events
+				log.warn("Application " + app.toString() + " could not be deployed on any pm");
 		}
+
+	}
+
+	@Override
+	protected void runMigration() {
+		// Scheduler A doesn't support Migration, nothing to do here
 	}
 
 	// sorting applications by amount of resources (descending)
-	private List<SortedApplication> sortApps(LinkedList<SchedulerEvent> events) {
-		List<SortedApplication> sortedApps = new LinkedList<SortedApplication>();
+	private List<Application> sortApps(LinkedList<SchedulerEvent> events) {
+		List<Application> sortedApps = new LinkedList<Application>();
 		for (SchedulerEvent evt : events) {
-			if (evt.getType() == EventType.startApplication)
-				sortedApps.add(new SortedApplication(evt.getApp()));
+			sortedApps.add(evt.getApp());
 		}
-		Collections.sort(sortedApps);
+		Collections.sort(sortedApps, new ApplicationResourceComparator());
 		Collections.reverse(sortedApps);
 		return sortedApps;
 	}
 
 	// sorting physical machines by resource utilization (descending)
-	private List<SortedPhysicalMachine> sortPMs() {
-		List<SortedPhysicalMachine> sortedPMs = new LinkedList<SortedPhysicalMachine>();
+	private List<PhysicalMachine> sortPMs() {
+		List<PhysicalMachine> sortedPMs = new LinkedList<PhysicalMachine>();
 		for (PhysicalMachine pm : manager.getPMs()) {
-			sortedPMs.add(new SortedPhysicalMachine(pm));
+			sortedPMs.add(pm);
 			// log.info("pm util = "+pm.getAverageUtilization());
 		}
 
@@ -168,7 +163,7 @@ public class SchedulerA extends AbstractScheduler {
 		Collections.reverse(sortedPMs);
 		return sortedPMs;
 	}
-	
+
 	@Override
 	protected String getSchedulerType() {
 		return SchedulerType.A.toString();
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
index d1f7d0a..e71a990 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
@@ -13,23 +13,22 @@ import at.ac.tuwien.lsdc.types.Application;
 import at.ac.tuwien.lsdc.types.PhysicalMachine;
 import at.ac.tuwien.lsdc.types.ScenarioType;
 import at.ac.tuwien.lsdc.types.SchedulerEvent;
-import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
 import at.ac.tuwien.lsdc.types.SchedulerType;
 import at.ac.tuwien.lsdc.types.VirtualMachine;
 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
 
 /**
  * Scheduler B.
- * 
+ *
  * Initial State: All PMs switched off. If an application arrives, try to modify size, CPU and RAM
  * of an existing VM to run the application. If no VM is running, create a new one (start a new PM
  * if necessary). If the application has finished decrease the size, CPU and RAM of the VM. If no
  * applications are running on a VM, shut down the VM. If no VM is running on a PM, shut down the
  * PM. Try to get a maximum of utilization on every PM. Migration: Try to move applications from VMs
  * to other VMs to get a better utilization and to use less PMs.
- * 
+ *
  * @author jan
- * 
+ *
  */
 public class SchedulerB extends AbstractScheduler {
 	/**
@@ -43,21 +42,11 @@ public class SchedulerB extends AbstractScheduler {
 		vmType = VMType.Resizable;
 	}
 
-	@Override
-	protected void handleEvents(LinkedList<SchedulerEvent> events) {
-		LOG.debug("handleEvents():" + events);
-		handleEndEvents(events);
-		handleOutsourcedEndEvents(events);
-		runMigration();
-		runDelayedApps();
-		handleOutsourcedStartEvents(events);
-		handleStartEvents(events);
-	}
-
 	/**
 	 * Check if we have any delayed apps. Try to launch them.
 	 */
-	private void runDelayedApps() {
+	@Override
+	protected void handleDelayedApps() {
 		// TODO: probably mostly the same code as handleStartEvents
 		// TOOD: (namely: use case: "start an application",do merge it
 		for (Application app : delayedApps) {
@@ -97,33 +86,33 @@ public class SchedulerB extends AbstractScheduler {
 	/**
 	 * Check if we can free up a VM to shut it down.
 	 */
-	private void runMigration() {
+	@Override
+	protected void runMigration() {
 		// TODO Auto-generated method stub
 	}
 
 	/**
 	 * Cleanup completed apps. Downsize VMs and if an VM becomes empty, shut down VM + PM.
-	 * 
+	 *
 	 * @param events list of all events that happened in this timeslot.
 	 */
+	@Override
 	protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
 		LOG.debug("stopApps():" + events);
 		for (SchedulerEvent evt : events) {
-			if (evt.getType() == EventType.endApplication) {
-				VirtualMachine vm = evt.getApp().getRunningOn();
-				evt.getApp().setRunningOn(null);
-				vm.stopApplication(evt.getApp());
-				try {
-					vm.resizeVM(vm.getSize() - evt.getApp().getSize(), vm.getRAM()
-							- evt.getApp().getRam(), vm.getCPU() - evt.getApp().getCpu());
-				} catch (VMResizeException e) {
-					// LOG.error("failed to resize VM: " + e.getVm(), e);
-				}
-				if (vm.getApplications().size() == 0) {
-					PhysicalMachine pm = vm.getRunningOn();
-					pm.stopVirtualMachine(vm);
-					manager.stopPhysicalMachine(pm.getId());
-				}
+			VirtualMachine vm = evt.getApp().getRunningOn();
+			evt.getApp().setRunningOn(null);
+			vm.stopApplication(evt.getApp());
+			try {
+				vm.resizeVM(vm.getSize() - evt.getApp().getSize(), vm.getRAM()
+						- evt.getApp().getRam(), vm.getCPU() - evt.getApp().getCpu());
+			} catch (VMResizeException e) {
+				// LOG.error("failed to resize VM: " + e.getVm(), e);
+			}
+			if (vm.getApplications().size() == 0) {
+				PhysicalMachine pm = vm.getRunningOn();
+				pm.stopVirtualMachine(vm);
+				manager.stopPhysicalMachine(pm.getId());
 			}
 		}
 	}
@@ -131,44 +120,42 @@ public class SchedulerB extends AbstractScheduler {
 	/**
 	 * Try to start all Apps. Upsize the VM, if not possible start another PM, if not possible,
 	 * delay start.
-	 * 
+	 *
 	 * @param events list of all events that happened in this timeslot.
 	 */
+	@Override
 	protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
 		LOG.debug("startApps():" + events);
 		for (SchedulerEvent evt : events) {
-			if (evt.getType() == EventType.startApplication) {
-				VirtualMachine vm = null;
-				for (PhysicalMachine pm : manager.getPMs()) {
-					// TODO: choose VM with good Utilization to get even better utilization
-					vm = pm.getVirtualMachines().get(
-							(pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]);
-					try {
-						vm.resizeVM(vm.getSize() + evt.getApp().getSize(), vm.getRAM()
-								+ evt.getApp().getRam(), vm.getCPU() + evt.getApp().getCpu());
-						break;
-					} catch (VMResizeException ex) {
-						vm = null;
-					}
+			VirtualMachine vm = null;
+			for (PhysicalMachine pm : manager.getPMs()) {
+				// TODO: choose VM with good Utilization to get even better utilization
+				vm = pm.getVirtualMachines().get(
+						(pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]);
+				try {
+					vm.resizeVM(vm.getSize() + evt.getApp().getSize(), vm.getRAM()
+							+ evt.getApp().getRam(), vm.getCPU() + evt.getApp().getCpu());
+					break;
+				} catch (VMResizeException ex) {
+					vm = null;
 				}
-				if (vm == null) {
-					try {
-						vm = manager.startPhysicalMachine().startVirtualMachine(
-								evt.getApp().getSize(), evt.getApp().getRam(),
-								evt.getApp().getCpu(), vmType);
-					} catch (OutOfPMsException e) {
-						if (federation.askToOutsource(evt.getApp())) {
-							insertOutsourcedStartEvent(currTime + 1, evt.getApp());
-						} else
-							LOG.info("delaying the start of:" + evt.getApp());
-						delayedApps.add(evt.getApp());
-						return;
-					}
+			}
+			if (vm == null) {
+				try {
+					vm = manager.startPhysicalMachine().startVirtualMachine(evt.getApp().getSize(),
+							evt.getApp().getRam(), evt.getApp().getCpu(), vmType);
+				} catch (OutOfPMsException e) {
+					if (federation.askToOutsource(evt.getApp())) {
+						insertOutsourcedStartEvent(currTime + 1, evt.getApp());
+					} else
+						LOG.info("delaying the start of:" + evt.getApp());
+					delayedApps.add(evt.getApp());
+					return;
 				}
-				vm.startApplication(evt.getApp());
-				evt.getApp().setRunningOn(vm);
-				insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
 			}
+			vm.startApplication(evt.getApp());
+			evt.getApp().setRunningOn(vm);
+			insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
 		}
 	}
 
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
index e44fedf..199a0da 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
@@ -7,30 +7,46 @@ import java.util.LinkedList;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import at.ac.tuwien.lsdc.types.ScenarioData;
 import at.ac.tuwien.lsdc.types.ScenarioType;
 import at.ac.tuwien.lsdc.types.SchedulerEvent;
 import at.ac.tuwien.lsdc.types.SchedulerType;
 
-
 public class SchedulerC extends AbstractScheduler {
 
 	private static final Logger log = LoggerFactory.getLogger(SchedulerC.class);
-	
-	public SchedulerC(int numPMs, int numCloudPartners, File schedulerLog,
-			ScenarioType scenario) throws IOException {
+
+	public SchedulerC(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
+			throws IOException {
 		super(numPMs, numCloudPartners, schedulerLog, scenario);
 	}
 
 	@Override
-	protected void handleEvents(LinkedList<SchedulerEvent> events) {
+	protected String getSchedulerType() {
+		return SchedulerType.C.toString();
+	}
+
+	@Override
+	protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
 		// TODO Auto-generated method stub
-		log.info("Sched C handle Events");
+
 	}
 
 	@Override
-	protected String getSchedulerType() {
-		return SchedulerType.C.toString();
+	protected void handleDelayedApps() {
+		// TODO Auto-generated method stub
+
+	}
+
+	@Override
+	protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
+		// TODO Auto-generated method stub
+
+	}
+
+	@Override
+	protected void runMigration() {
+		// TODO Auto-generated method stub
+
 	}
 
 }
diff --git a/src/at/ac/tuwien/lsdc/types/Application.java b/src/at/ac/tuwien/lsdc/types/Application.java
index 618261e..08d3334 100644
--- a/src/at/ac/tuwien/lsdc/types/Application.java
+++ b/src/at/ac/tuwien/lsdc/types/Application.java
@@ -11,8 +11,7 @@ public class Application implements Comparable<Application> {
 
 	private VirtualMachine runningOn;
 
-	public Application(int id, long timestamp, int size, int ram, int cpu,
-			int duration) {
+	public Application(int id, long timestamp, int size, int ram, int cpu, int duration) {
 		this.id = id;
 		this.timestamp = timestamp;
 		this.size = size;
@@ -45,10 +44,10 @@ public class Application implements Comparable<Application> {
 		return duration;
 	}
 
+	@Override
 	public String toString() {
-		return new String("App ID: " + id + " Timestamp: " + timestamp
-				+ " size: " + size + " ram: " + ram + " cpu: " + cpu
-				+ " duration: " + duration);
+		return new String("App ID: " + id + " Timestamp: " + timestamp + " size: " + size
+				+ " ram: " + ram + " cpu: " + cpu + " duration: " + duration);
 	}
 
 	public VirtualMachine getRunningOn() {
diff --git a/src/at/ac/tuwien/lsdc/types/ApplicationResourceComparator.java b/src/at/ac/tuwien/lsdc/types/ApplicationResourceComparator.java
new file mode 100644
index 0000000..12bff01
--- /dev/null
+++ b/src/at/ac/tuwien/lsdc/types/ApplicationResourceComparator.java
@@ -0,0 +1,27 @@
+package at.ac.tuwien.lsdc.types;
+
+import java.util.Comparator;
+
+public class ApplicationResourceComparator implements Comparator<Application> {
+
+	final int BEFORE = -1;
+	final int EQUAL = 0;
+	final int AFTER = 1;
+
+	@Override
+	public int compare(Application a1, Application a2) {
+		if (a1 == a2)
+			return EQUAL;
+		if (getResourceDifference(a1, a2) < 0)
+			return BEFORE;
+		else if (getResourceDifference(a1, a2) > 0)
+			return AFTER;
+		else
+			return EQUAL;
+	}
+
+	public int getResourceDifference(Application a1, Application a2) {
+		return a1.getSize() - a2.getSize() + a1.getRam() - a2.getRam() + a1.getCpu() - a2.getCpu();
+	}
+
+}
diff --git a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
index f775784..9d52604 100644
--- a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
+++ b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
@@ -6,7 +6,11 @@ import at.ac.tuwien.lsdc.exception.ActiveApplicationsException;
 import at.ac.tuwien.lsdc.exception.VMsRunningException;
 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
 
-public class PhysicalMachine {
+public class PhysicalMachine implements Comparable<PhysicalMachine> {
+
+	final int BEFORE = -1;
+	final int EQUAL = 0;
+	final int AFTER = 1;
 
 	private static int count = 0;
 
@@ -25,7 +29,7 @@ public class PhysicalMachine {
 	private final int initialSize = 850;
 	private final int initialRAM = 300;
 	private final int initialCPU = 500;
-	
+
 	private int totalVMs = 0;
 
 	private boolean running = false;
@@ -44,8 +48,7 @@ public class PhysicalMachine {
 
 	public void stop() throws VMsRunningException {
 		if (VMs.size() > 0)
-			throw new VMsRunningException(
-					"PM cannot be stopped. Some VMs still running");
+			throw new VMsRunningException("PM cannot be stopped. Some VMs still running");
 
 		VMs = new HashMap<Integer, VirtualMachine>();
 		reservedSize = 0;
@@ -61,8 +64,7 @@ public class PhysicalMachine {
 			return 0.0;
 	}
 
-	public VirtualMachine startVirtualMachine(int sz, int ram, int cpu,
-			VMType type) {
+	public VirtualMachine startVirtualMachine(int sz, int ram, int cpu, VMType type) {
 		if (checkVM(sz, ram, cpu)) {
 			VirtualMachine vm = new VirtualMachine(sz, ram, cpu, this, type);
 			VMs.put(vm.getId(), vm);
@@ -75,13 +77,11 @@ public class PhysicalMachine {
 			return null;
 	}
 
-	public void stopVirtualMachine(VirtualMachine vm)
-			throws ActiveApplicationsException {
+	public void stopVirtualMachine(VirtualMachine vm) throws ActiveApplicationsException {
 		if (VMs.containsKey(vm.getId())) {
 			if (vm.getApplications().size() != 0) {
 				throw new ActiveApplicationsException(
-						"Applications must be migrated before stopping a VM, VM id "
-								+ vm.getId());
+						"Applications must be migrated before stopping a VM, VM id " + vm.getId());
 			} else {
 				VMs.remove(vm.getId());
 				reservedSize = reservedSize - vm.getReservedSize();
@@ -92,7 +92,8 @@ public class PhysicalMachine {
 	}
 
 	public boolean checkVM(int size, int RAM, int CPU) {
-		return ((VirtualMachine.initialSize + size) <= availableSize()) && ((VirtualMachine.initialRAM + RAM) <= availableRAM())
+		return ((VirtualMachine.initialSize + size) <= availableSize())
+				&& ((VirtualMachine.initialRAM + RAM) <= availableRAM())
 				&& ((VirtualMachine.initialCPU + CPU) <= availableCPU());
 	}
 
@@ -157,14 +158,14 @@ public class PhysicalMachine {
 		return VMs.get(id);
 	}
 
-  /**
-   * return a list of all VMs running on this PM.
-   * 
-   * @return a HashMap with all VMs running on this PM.
-   */
-  public HashMap<Integer, VirtualMachine> getVirtualMachines() {
-    return VMs;
-  }
+	/**
+	 * return a list of all VMs running on this PM.
+	 *
+	 * @return a HashMap with all VMs running on this PM.
+	 */
+	public HashMap<Integer, VirtualMachine> getVirtualMachines() {
+		return VMs;
+	}
 
 	public int countCurrentlyRunningVMs() {
 		return VMs.values().size();
@@ -185,4 +186,17 @@ public class PhysicalMachine {
 	public int getTotalVMs() {
 		return totalVMs;
 	}
+
+	@Override
+	public int compareTo(PhysicalMachine other) {
+		if (this == other)
+			return EQUAL;
+
+		if (getAverageUtilization() < other.getAverageUtilization())
+			return BEFORE;
+		else if (getAverageUtilization() > other.getAverageUtilization())
+			return AFTER;
+		else
+			return EQUAL;
+	}
 }
diff --git a/src/at/ac/tuwien/lsdc/types/SortedApplication.java b/src/at/ac/tuwien/lsdc/types/SortedApplication.java
deleted file mode 100644
index ff052a3..0000000
--- a/src/at/ac/tuwien/lsdc/types/SortedApplication.java
+++ /dev/null
@@ -1,38 +0,0 @@
-package at.ac.tuwien.lsdc.types;
-
-public class SortedApplication implements Comparable<SortedApplication> {
-
-	
-	private Application app;
-	final int BEFORE = -1;
-	final int EQUAL = 0;
-	final int AFTER = 1;
-	
-	public SortedApplication(Application app) {
-		this.app = app;
-	}
-
-	@Override
-	public int compareTo(SortedApplication other) {
-		if (this == other)
-			return EQUAL;
-		
-		if(getResourceDifference(other) < 0)
-			return BEFORE;
-		else if(getResourceDifference(other) > 0)
-			return AFTER;
-		else
-			return EQUAL;
-	}
-	
-	public Application getApp() {
-		return app;
-	}
-	
-	public int getResourceDifference(SortedApplication other) {
-		return  app.getSize() - other.getApp().getSize()  +
-				app.getRam() - other.getApp().getRam()	+
-				app.getCpu() - other.getApp().getCpu();
-	}
-	
-}
diff --git a/src/at/ac/tuwien/lsdc/types/SortedPhysicalMachine.java b/src/at/ac/tuwien/lsdc/types/SortedPhysicalMachine.java
deleted file mode 100644
index 6d61fbf..0000000
--- a/src/at/ac/tuwien/lsdc/types/SortedPhysicalMachine.java
+++ /dev/null
@@ -1,32 +0,0 @@
-package at.ac.tuwien.lsdc.types;
-
-public class SortedPhysicalMachine implements Comparable<SortedPhysicalMachine> {
-
-	private PhysicalMachine pm;
-	final int BEFORE = -1;
-	final int EQUAL = 0;
-	final int AFTER = 1;
-	
-	public SortedPhysicalMachine(PhysicalMachine pm) {
-		this.pm = pm;
-	}
-	
-	@Override
-	public int compareTo(SortedPhysicalMachine other) {
-		if(this == other)
-			return EQUAL;
-		
-		if(pm.getAverageUtilization() < other.getPm().getAverageUtilization())
-			return BEFORE;
-		else if(pm.getAverageUtilization() > other.getPm().getAverageUtilization())
-			return AFTER;
-		else
-			return EQUAL;
-	}
-
-	
-	public PhysicalMachine getPm() {
-		return pm;
-	}
-	
-}
-- 
2.43.0


From a836b8f0a69d6e39db025b1f19f39770fbc7066f Mon Sep 17 00:00:00 2001
From: Stefan Derkits <stefan@derkits.at>
Date: Tue, 11 Jun 2013 14:18:28 +0200
Subject: [PATCH 02/16] moved handling of delayed Apps into AbstractScheduler

---
 .../tuwien/lsdc/sched/AbstractScheduler.java  | 12 +++++-
 src/at/ac/tuwien/lsdc/sched/SchedulerA.java   |  6 ---
 src/at/ac/tuwien/lsdc/sched/SchedulerB.java   | 42 -------------------
 3 files changed, 10 insertions(+), 50 deletions(-)

diff --git a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
index fc0109c..c6a3779 100644
--- a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
+++ b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
@@ -202,9 +202,17 @@ public abstract class AbstractScheduler {
 		runMigration();
 	}
 
-	protected abstract void handleEndEvents(LinkedList<SchedulerEvent> events);
+	protected void handleDelayedApps() {
+		LinkedList<SchedulerEvent> delayedStartEvents = new LinkedList<SchedulerEvent>();
+		for (Application app : delayedApps) {
+			SchedulerEvent evt = new SchedulerEvent(currTime, EventType.startApplication, app);
+			delayedStartEvents.add(evt);
+			delayedApps.remove(app);
+		}
+		handleStartEvents(delayedStartEvents);
+	}
 
-	protected abstract void handleDelayedApps();
+	protected abstract void handleEndEvents(LinkedList<SchedulerEvent> events);
 
 	protected abstract void handleStartEvents(LinkedList<SchedulerEvent> events);
 
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
index 3a0c479..354bfad 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
@@ -54,12 +54,6 @@ public class SchedulerA extends AbstractScheduler {
 		}
 	}
 
-	@Override
-	protected void handleDelayedApps() {
-		// TODO Auto-generated method stub
-
-	}
-
 	@Override
 	protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
 		// sorting applications by amount of resources (descending)
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
index e71a990..6044677 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
@@ -9,7 +9,6 @@ import org.slf4j.LoggerFactory;
 
 import at.ac.tuwien.lsdc.exception.OutOfPMsException;
 import at.ac.tuwien.lsdc.exception.VMResizeException;
-import at.ac.tuwien.lsdc.types.Application;
 import at.ac.tuwien.lsdc.types.PhysicalMachine;
 import at.ac.tuwien.lsdc.types.ScenarioType;
 import at.ac.tuwien.lsdc.types.SchedulerEvent;
@@ -42,47 +41,6 @@ public class SchedulerB extends AbstractScheduler {
 		vmType = VMType.Resizable;
 	}
 
-	/**
-	 * Check if we have any delayed apps. Try to launch them.
-	 */
-	@Override
-	protected void handleDelayedApps() {
-		// TODO: probably mostly the same code as handleStartEvents
-		// TOOD: (namely: use case: "start an application",do merge it
-		for (Application app : delayedApps) {
-			VirtualMachine vm = null;
-			for (PhysicalMachine pm : manager.getPMs()) {
-				// TODO: choose PM better to get better Utilization
-				vm = pm.getVirtualMachines().get(
-						(pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]);
-				try {
-					vm.resizeVM(vm.getSize() + app.getSize(), vm.getRAM() + app.getRam(),
-							vm.getCPU() + app.getCpu());
-					break;
-				} catch (VMResizeException ex) {
-					vm = null;
-				}
-			}
-			if (vm == null) {
-				try {
-					vm = manager.startPhysicalMachine().startVirtualMachine(app.getSize(),
-							app.getRam(), app.getCpu(), vmType);
-				} catch (OutOfPMsException e) {
-					// LOG.error("failed to start PM.", e);
-					if (federation.askToOutsource(app)) {
-						insertOutsourcedStartEvent(currTime + 1, app);
-					} else
-						LOG.info("delaying the start of:" + app);
-					return;
-				}
-			}
-			vm.startApplication(app);
-			app.setRunningOn(vm);
-			insertStopEvent(currTime + app.getDuration(), app);
-			delayedApps.remove(app);
-		}
-	}
-
 	/**
 	 * Check if we can free up a VM to shut it down.
 	 */
-- 
2.43.0


From f972b2a84853018466353dca00bc0a2d80f0080b Mon Sep 17 00:00:00 2001
From: Stefan Derkits <stefan@derkits.at>
Date: Tue, 11 Jun 2013 15:19:32 +0200
Subject: [PATCH 03/16] moved sorting of PMs into MachineManager

---
 .../lsdc/management/MachineManager.java       | 36 +++++++++++++------
 src/at/ac/tuwien/lsdc/sched/SchedulerA.java   | 16 ++-------
 2 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/src/at/ac/tuwien/lsdc/management/MachineManager.java b/src/at/ac/tuwien/lsdc/management/MachineManager.java
index d41ecb5..70f8d60 100644
--- a/src/at/ac/tuwien/lsdc/management/MachineManager.java
+++ b/src/at/ac/tuwien/lsdc/management/MachineManager.java
@@ -1,7 +1,10 @@
 package at.ac.tuwien.lsdc.management;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 
 import at.ac.tuwien.lsdc.exception.OutOfPMsException;
 import at.ac.tuwien.lsdc.exception.VMsRunningException;
@@ -11,7 +14,7 @@ import at.ac.tuwien.lsdc.util.NumberUtils;
 /**
  * This class is responsible to start and stop PMs & VMs also it will be used to
  * put an application on a VM move an application and get utilization data
- * 
+ *
  */
 public class MachineManager {
 
@@ -28,7 +31,7 @@ public class MachineManager {
 
 	/**
 	 * Start a physical machine
-	 * 
+	 *
 	 * @return the PM that was started, null if all machines already running
 	 */
 	public PhysicalMachine startPhysicalMachine() {
@@ -44,7 +47,7 @@ public class MachineManager {
 
 	/**
 	 * Stops a physical machine with the given id
-	 * 
+	 *
 	 * @param id
 	 *          the id of the PM to stop
 	 * @throws VMsRunningException
@@ -61,7 +64,7 @@ public class MachineManager {
 
 	/**
 	 * Returns all running physical machines
-	 * 
+	 *
 	 * @return the currently active PMs
 	 */
 	public Collection<PhysicalMachine> getPMs() {
@@ -70,7 +73,7 @@ public class MachineManager {
 
 	/**
 	 * Returns the maximum number of available physical machines
-	 * 
+	 *
 	 * @return the maximum number of PMs
 	 */
 	public int getMaxPMs() {
@@ -79,7 +82,7 @@ public class MachineManager {
 
 	/**
 	 * Get the total size on all PMs
-	 * 
+	 *
 	 * @return the total size on all PMs
 	 */
 	public int getTotalSize() {
@@ -93,7 +96,7 @@ public class MachineManager {
 
 	/**
 	 * Get the total Ram usage of all PMs
-	 * 
+	 *
 	 * @return the total usage of Ram
 	 */
 	public int getTotalRam() {
@@ -107,7 +110,7 @@ public class MachineManager {
 
 	/**
 	 * Get the total Cpu usage of all PMs
-	 * 
+	 *
 	 * @return the total usage of Cpu power
 	 */
 	public int getTotalCpu() {
@@ -121,7 +124,7 @@ public class MachineManager {
 
 	/**
 	 * Gets the total power consumption summed up from each PM
-	 * 
+	 *
 	 * @return the total power consumption
 	 */
 	public double getCurrentConsumption() {
@@ -136,7 +139,7 @@ public class MachineManager {
 
 	/**
 	 * Gets the number of currently running PMs
-	 * 
+	 *
 	 * @return the number of currently running PMs
 	 */
 	public int countCurrentlyRunningPMs() {
@@ -150,7 +153,7 @@ public class MachineManager {
 
 	/**
 	 * Gets the number of currently running VMs
-	 * 
+	 *
 	 * @return the number of currently running VMs
 	 */
 	public int countCurrentlyRunningVMs() {
@@ -163,6 +166,17 @@ public class MachineManager {
 		return running;
 	}
 
+	/**
+	 * sorting physical machines by resource utilization
+	 *
+	 * @return List of PMs sorted by resource utilization
+	 */
+	public List<PhysicalMachine> getSortedPMs() {
+		List<PhysicalMachine> sortedPMs = new ArrayList<PhysicalMachine>(PMs.values());
+		Collections.sort(sortedPMs);
+		return sortedPMs;
+	}
+
 	public int getTotalPMs() {
 		return totalPMs;
 	}
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
index 354bfad..6230ec2 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
@@ -80,7 +80,8 @@ public class SchedulerA extends AbstractScheduler {
 			} else {
 				// sorting physical machines by resource utilization
 				// (descending)
-				List<PhysicalMachine> sortedPMs = sortPMs();
+				List<PhysicalMachine> sortedPMs = manager.getSortedPMs();
+				Collections.reverse(sortedPMs);
 
 				for (PhysicalMachine pm : sortedPMs) {
 
@@ -145,19 +146,6 @@ public class SchedulerA extends AbstractScheduler {
 		return sortedApps;
 	}
 
-	// sorting physical machines by resource utilization (descending)
-	private List<PhysicalMachine> sortPMs() {
-		List<PhysicalMachine> sortedPMs = new LinkedList<PhysicalMachine>();
-		for (PhysicalMachine pm : manager.getPMs()) {
-			sortedPMs.add(pm);
-			// log.info("pm util = "+pm.getAverageUtilization());
-		}
-
-		Collections.sort(sortedPMs);
-		Collections.reverse(sortedPMs);
-		return sortedPMs;
-	}
-
 	@Override
 	protected String getSchedulerType() {
 		return SchedulerType.A.toString();
-- 
2.43.0


From d6f2a7322b07764ad53d5fe8b30bf1050e7e7e8b Mon Sep 17 00:00:00 2001
From: Stefan Derkits <stefan@derkits.at>
Date: Tue, 11 Jun 2013 15:20:10 +0200
Subject: [PATCH 04/16] added Migration to SchedB

---
 src/at/ac/tuwien/lsdc/sched/SchedulerB.java   | 71 +++++++++++++++----
 .../ac/tuwien/lsdc/types/VirtualMachine.java  | 11 ++-
 2 files changed, 62 insertions(+), 20 deletions(-)

diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
index 6044677..2f06ac7 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
@@ -2,13 +2,16 @@ package at.ac.tuwien.lsdc.sched;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Collections;
 import java.util.LinkedList;
+import java.util.List;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import at.ac.tuwien.lsdc.exception.OutOfPMsException;
 import at.ac.tuwien.lsdc.exception.VMResizeException;
+import at.ac.tuwien.lsdc.types.Application;
 import at.ac.tuwien.lsdc.types.PhysicalMachine;
 import at.ac.tuwien.lsdc.types.ScenarioType;
 import at.ac.tuwien.lsdc.types.SchedulerEvent;
@@ -46,7 +49,48 @@ public class SchedulerB extends AbstractScheduler {
 	 */
 	@Override
 	protected void runMigration() {
-		// TODO Auto-generated method stub
+		List<PhysicalMachine> pms = manager.getSortedPMs();
+		// iterate through all the PMs (except the one with the highest utilization)
+		for (int i = 0; i < (pms.size() - 1); i++) {
+			PhysicalMachine currentPM = pms.get(i);
+			if (currentPM.isRunning() && (currentPM.getTotalVMs() > 0)) {
+				VirtualMachine currentVM = currentPM.getVirtualMachines().values().iterator()
+						.next();
+				for (Application app : currentVM.getApplications()) {
+					for (int j = i; i < pms.size(); i++) {
+						PhysicalMachine nextPM = pms.get(j);
+						if (nextPM.isRunning() && (nextPM.getTotalVMs() > 0)) {
+							VirtualMachine nextVM = nextPM.getVirtualMachines().values().iterator()
+									.next();
+							if (deployApp(nextVM, app)) {
+								currentVM.stopApplication(app);
+								break;
+							}
+						}
+					}
+				}
+				if (currentVM.getApplications().size() == 0) {
+					currentPM.stopVirtualMachine(currentVM);
+					if (currentPM.getTotalVMs() == 0) {
+						manager.stopPhysicalMachine(currentPM.getId());
+					}
+				}
+			}
+		}
+	}
+
+	private boolean deployApp(VirtualMachine vm, Application app) {
+		if (!vm.enoughResources(app)) {
+			try {
+				vm.resizeVM(vm.getSize() + app.getSize(), vm.getRAM() + app.getRam(), vm.getCPU()
+						+ app.getCpu());
+			} catch (VMResizeException ex) {
+				return false;
+			}
+		}
+		vm.startApplication(app);
+		app.setRunningOn(vm);
+		return true;
 	}
 
 	/**
@@ -84,24 +128,24 @@ public class SchedulerB extends AbstractScheduler {
 	@Override
 	protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
 		LOG.debug("startApps():" + events);
+		boolean deployed = false;
 		for (SchedulerEvent evt : events) {
 			VirtualMachine vm = null;
-			for (PhysicalMachine pm : manager.getPMs()) {
-				// TODO: choose VM with good Utilization to get even better utilization
-				vm = pm.getVirtualMachines().get(
-						(pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]);
-				try {
-					vm.resizeVM(vm.getSize() + evt.getApp().getSize(), vm.getRAM()
-							+ evt.getApp().getRam(), vm.getCPU() + evt.getApp().getCpu());
-					break;
-				} catch (VMResizeException ex) {
-					vm = null;
+			List<PhysicalMachine> sortedPMs = manager.getSortedPMs();
+			Collections.reverse(sortedPMs);
+			for (PhysicalMachine pm : sortedPMs) {
+				if (pm.isRunning() && (pm.getTotalVMs() > 0)) {
+					vm = pm.getVirtualMachines().values().iterator().next();
+					deployed = deployApp(vm, evt.getApp());
+					if (deployed)
+						break;
 				}
 			}
 			if (vm == null) {
 				try {
 					vm = manager.startPhysicalMachine().startVirtualMachine(evt.getApp().getSize(),
 							evt.getApp().getRam(), evt.getApp().getCpu(), vmType);
+					deployed = deployApp(vm, evt.getApp());
 				} catch (OutOfPMsException e) {
 					if (federation.askToOutsource(evt.getApp())) {
 						insertOutsourcedStartEvent(currTime + 1, evt.getApp());
@@ -111,9 +155,8 @@ public class SchedulerB extends AbstractScheduler {
 					return;
 				}
 			}
-			vm.startApplication(evt.getApp());
-			evt.getApp().setRunningOn(vm);
-			insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
+			if (deployed)
+				insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
 		}
 	}
 
diff --git a/src/at/ac/tuwien/lsdc/types/VirtualMachine.java b/src/at/ac/tuwien/lsdc/types/VirtualMachine.java
index abf4637..2fb7673 100644
--- a/src/at/ac/tuwien/lsdc/types/VirtualMachine.java
+++ b/src/at/ac/tuwien/lsdc/types/VirtualMachine.java
@@ -52,7 +52,7 @@ public class VirtualMachine {
 
 	/**
 	 * Set the VM size to the given new values.
-	 * 
+	 *
 	 * @param newSize
 	 *          the new size of the VM, excluding the initial consumption.
 	 * @param newRAM
@@ -76,25 +76,24 @@ public class VirtualMachine {
 	public boolean stopApplication(Application app) {
 		if (applications.containsKey(app.getID())) {
 			applications.remove(app.getID());
-			app.setRunningOn(null);
 			return true;
 		} else
 			return false;
 	}
 
-	private boolean enoughResources(Application app) {
+	public boolean enoughResources(Application app) {
 		return (app.getSize() <= availableSize()) && (app.getRam() <= availableRAM()) && (app.getCpu() <= availableCPU());
 	}
 
-	private int availableSize() {
+	public int availableSize() {
 		return reservedSize - getSize();
 	}
 
-	private int availableRAM() {
+	public int availableRAM() {
 		return reservedRAM - getRAM();
 	}
 
-	private int availableCPU() {
+	public int availableCPU() {
 		return reservedCPU - getCPU();
 	}
 
-- 
2.43.0


From 8397ba2a7d3036e677480747f446f9a672079455 Mon Sep 17 00:00:00 2001
From: Stefan Derkits <stefan@derkits.at>
Date: Tue, 11 Jun 2013 15:28:25 +0200
Subject: [PATCH 05/16] put code for migration into new class
 AbstractSchedulerWithMigration

---
 .../sched/AbstractSchedulerWithMigration.java | 56 +++++++++++++++++++
 src/at/ac/tuwien/lsdc/sched/SchedulerB.java   | 38 +------------
 src/at/ac/tuwien/lsdc/sched/SchedulerC.java   | 10 +++-
 3 files changed, 67 insertions(+), 37 deletions(-)
 create mode 100644 src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java

diff --git a/src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java b/src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java
new file mode 100644
index 0000000..96a93ba
--- /dev/null
+++ b/src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java
@@ -0,0 +1,56 @@
+package at.ac.tuwien.lsdc.sched;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import at.ac.tuwien.lsdc.types.Application;
+import at.ac.tuwien.lsdc.types.PhysicalMachine;
+import at.ac.tuwien.lsdc.types.ScenarioType;
+import at.ac.tuwien.lsdc.types.VirtualMachine;
+
+public abstract class AbstractSchedulerWithMigration extends AbstractScheduler {
+
+	public AbstractSchedulerWithMigration(int numPMs, int numCloudPartners, File schedulerLog,
+			ScenarioType scenario) throws IOException {
+		super(numPMs, numCloudPartners, schedulerLog, scenario);
+	}
+
+	/**
+	 * Check if we can free up a VM to shut it down.
+	 */
+	@Override
+	protected void runMigration() {
+		List<PhysicalMachine> pms = manager.getSortedPMs();
+		// iterate through all the PMs (except the one with the highest utilization)
+		for (int i = 0; i < (pms.size() - 1); i++) {
+			PhysicalMachine currentPM = pms.get(i);
+			if (currentPM.isRunning() && (currentPM.getTotalVMs() > 0)) {
+				VirtualMachine currentVM = currentPM.getVirtualMachines().values().iterator()
+						.next();
+				for (Application app : currentVM.getApplications()) {
+					for (int j = i; i < pms.size(); i++) {
+						PhysicalMachine nextPM = pms.get(j);
+						if (nextPM.isRunning() && (nextPM.getTotalVMs() > 0)) {
+							VirtualMachine nextVM = nextPM.getVirtualMachines().values().iterator()
+									.next();
+							if (deployApp(nextVM, app)) {
+								currentVM.stopApplication(app);
+								break;
+							}
+						}
+					}
+				}
+				if (currentVM.getApplications().size() == 0) {
+					currentPM.stopVirtualMachine(currentVM);
+					if (currentPM.getTotalVMs() == 0) {
+						manager.stopPhysicalMachine(currentPM.getId());
+					}
+				}
+			}
+		}
+	}
+
+	protected abstract boolean deployApp(VirtualMachine vm, Application app);
+
+}
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
index 2f06ac7..5be16e5 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
@@ -32,7 +32,7 @@ import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
  * @author jan
  *
  */
-public class SchedulerB extends AbstractScheduler {
+public class SchedulerB extends AbstractSchedulerWithMigration {
 	/**
 	 * Logger.
 	 */
@@ -44,42 +44,8 @@ public class SchedulerB extends AbstractScheduler {
 		vmType = VMType.Resizable;
 	}
 
-	/**
-	 * Check if we can free up a VM to shut it down.
-	 */
 	@Override
-	protected void runMigration() {
-		List<PhysicalMachine> pms = manager.getSortedPMs();
-		// iterate through all the PMs (except the one with the highest utilization)
-		for (int i = 0; i < (pms.size() - 1); i++) {
-			PhysicalMachine currentPM = pms.get(i);
-			if (currentPM.isRunning() && (currentPM.getTotalVMs() > 0)) {
-				VirtualMachine currentVM = currentPM.getVirtualMachines().values().iterator()
-						.next();
-				for (Application app : currentVM.getApplications()) {
-					for (int j = i; i < pms.size(); i++) {
-						PhysicalMachine nextPM = pms.get(j);
-						if (nextPM.isRunning() && (nextPM.getTotalVMs() > 0)) {
-							VirtualMachine nextVM = nextPM.getVirtualMachines().values().iterator()
-									.next();
-							if (deployApp(nextVM, app)) {
-								currentVM.stopApplication(app);
-								break;
-							}
-						}
-					}
-				}
-				if (currentVM.getApplications().size() == 0) {
-					currentPM.stopVirtualMachine(currentVM);
-					if (currentPM.getTotalVMs() == 0) {
-						manager.stopPhysicalMachine(currentPM.getId());
-					}
-				}
-			}
-		}
-	}
-
-	private boolean deployApp(VirtualMachine vm, Application app) {
+	protected boolean deployApp(VirtualMachine vm, Application app) {
 		if (!vm.enoughResources(app)) {
 			try {
 				vm.resizeVM(vm.getSize() + app.getSize(), vm.getRAM() + app.getRam(), vm.getCPU()
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
index 199a0da..afe1837 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
@@ -7,11 +7,13 @@ import java.util.LinkedList;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import at.ac.tuwien.lsdc.types.Application;
 import at.ac.tuwien.lsdc.types.ScenarioType;
 import at.ac.tuwien.lsdc.types.SchedulerEvent;
 import at.ac.tuwien.lsdc.types.SchedulerType;
+import at.ac.tuwien.lsdc.types.VirtualMachine;
 
-public class SchedulerC extends AbstractScheduler {
+public class SchedulerC extends AbstractSchedulerWithMigration {
 
 	private static final Logger log = LoggerFactory.getLogger(SchedulerC.class);
 
@@ -49,4 +51,10 @@ public class SchedulerC extends AbstractScheduler {
 
 	}
 
+	@Override
+	protected boolean deployApp(VirtualMachine vm, Application app) {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
 }
-- 
2.43.0


From 8d0b2b37dbacb2d9dbf883ae2ff0e308fc33774f Mon Sep 17 00:00:00 2001
From: Stefan Derkits <stefan@derkits.at>
Date: Tue, 11 Jun 2013 15:33:45 +0200
Subject: [PATCH 06/16] only call runMigration() in Schedulers that extend
 AbstractSchedulerWithMigration

---
 src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java    |  5 +----
 .../lsdc/sched/AbstractSchedulerWithMigration.java    | 11 ++++++++++-
 src/at/ac/tuwien/lsdc/sched/SchedulerA.java           |  5 -----
 3 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
index c6a3779..08735a4 100644
--- a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
+++ b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
@@ -189,7 +189,7 @@ public abstract class AbstractScheduler {
 	 *
 	 * @param events the events to be read and used by the scheduler
 	 */
-	private void handleEvents(HashMap<EventType, LinkedList<SchedulerEvent>> events) {
+	protected void handleEvents(HashMap<EventType, LinkedList<SchedulerEvent>> events) {
 		if (events.containsKey(EventType.endApplication))
 			handleEndEvents(events.get(EventType.endApplication));
 		if (events.containsKey(EventType.endOutsourcedApplication))
@@ -199,7 +199,6 @@ public abstract class AbstractScheduler {
 			handleOutsourcedStartEvents(events.get(EventType.startOutsourcedApplication));
 		if (events.containsKey(EventType.startApplication))
 			handleStartEvents(events.get(EventType.startApplication));
-		runMigration();
 	}
 
 	protected void handleDelayedApps() {
@@ -216,8 +215,6 @@ public abstract class AbstractScheduler {
 
 	protected abstract void handleStartEvents(LinkedList<SchedulerEvent> events);
 
-	protected abstract void runMigration();
-
 	/**
 	 * handle running of outsourced apps.
 	 *
diff --git a/src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java b/src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java
index 96a93ba..e0fff5a 100644
--- a/src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java
+++ b/src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java
@@ -2,11 +2,15 @@ package at.ac.tuwien.lsdc.sched;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.LinkedList;
 import java.util.List;
 
 import at.ac.tuwien.lsdc.types.Application;
 import at.ac.tuwien.lsdc.types.PhysicalMachine;
 import at.ac.tuwien.lsdc.types.ScenarioType;
+import at.ac.tuwien.lsdc.types.SchedulerEvent;
+import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
 import at.ac.tuwien.lsdc.types.VirtualMachine;
 
 public abstract class AbstractSchedulerWithMigration extends AbstractScheduler {
@@ -16,10 +20,15 @@ public abstract class AbstractSchedulerWithMigration extends AbstractScheduler {
 		super(numPMs, numCloudPartners, schedulerLog, scenario);
 	}
 
+	@Override
+	protected void handleEvents(HashMap<EventType, LinkedList<SchedulerEvent>> events) {
+		super.handleEvents(events);
+		runMigration();
+	}
+
 	/**
 	 * Check if we can free up a VM to shut it down.
 	 */
-	@Override
 	protected void runMigration() {
 		List<PhysicalMachine> pms = manager.getSortedPMs();
 		// iterate through all the PMs (except the one with the highest utilization)
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
index 6230ec2..7cf8613 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
@@ -130,11 +130,6 @@ public class SchedulerA extends AbstractScheduler {
 
 	}
 
-	@Override
-	protected void runMigration() {
-		// Scheduler A doesn't support Migration, nothing to do here
-	}
-
 	// sorting applications by amount of resources (descending)
 	private List<Application> sortApps(LinkedList<SchedulerEvent> events) {
 		List<Application> sortedApps = new LinkedList<Application>();
-- 
2.43.0


From ab97fe867f3d0af2b5eb7132ded7ea723b799e27 Mon Sep 17 00:00:00 2001
From: Andreas Egger <egger.andreas.1@gmail.com>
Date: Tue, 11 Jun 2013 20:00:34 +0200
Subject: [PATCH 07/16] Refactorings and bugfixes

---
 .../lsdc/management/MachineManager.java       |  4 +--
 src/at/ac/tuwien/lsdc/sched/SchedulerA.java   | 36 +++++++++----------
 src/at/ac/tuwien/lsdc/sched/SchedulerB.java   |  7 ++--
 .../ac/tuwien/lsdc/types/PhysicalMachine.java | 13 +++++++
 4 files changed, 35 insertions(+), 25 deletions(-)

diff --git a/src/at/ac/tuwien/lsdc/management/MachineManager.java b/src/at/ac/tuwien/lsdc/management/MachineManager.java
index 70f8d60..044d44b 100644
--- a/src/at/ac/tuwien/lsdc/management/MachineManager.java
+++ b/src/at/ac/tuwien/lsdc/management/MachineManager.java
@@ -123,9 +123,9 @@ public class MachineManager {
 	}
 
 	/**
-	 * Gets the total power consumption summed up from each PM
+	 * Gets the current power consumption summed up from each PM
 	 *
-	 * @return the total power consumption
+	 * @return the current power consumption
 	 */
 	public double getCurrentConsumption() {
 		double consumption = 0;
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
index 7cf8613..f7355c6 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
@@ -67,15 +67,11 @@ public class SchedulerA extends AbstractScheduler {
 				boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
 
 				if (enoughResources) {
-					VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(),
-							app.getCpu(), vmType);
-					vm.startApplication(app);
-					insertStopEvent(currTime + app.getDuration(), app);
-					appDeployed = true;
+					appDeployed = startApp(pm, app);
 					log.info("Application " + app.toString() + " started on new pm " + pm.getId());
 				} else {
-					log.warn("Application " + app.toString() + " cannot be run on empty pm "
-							+ pm.getId());
+					log.info("Application " + app.toString() + " cannot be run on pm "
+							+ pm.getId() + ", too little space");
 				}
 			} else {
 				// sorting physical machines by resource utilization
@@ -88,13 +84,9 @@ public class SchedulerA extends AbstractScheduler {
 					boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
 
 					if (enoughResources) {
-						VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(),
-								app.getCpu(), vmType);
-						vm.startApplication(app);
-						insertStopEvent(currTime + app.getDuration(), app);
-						appDeployed = true;
+						appDeployed = startApp(pm, app);
 						log.info("Application " + app.toString() + " started new vm "
-								+ vm.getPositionOnPM() + " on pm " + pm.getId());
+								+ pm.getLatestVMID() + " on pm " + pm.getId());
 						break;
 					}
 				}
@@ -104,16 +96,12 @@ public class SchedulerA extends AbstractScheduler {
 					boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
 
 					if (enoughResources) {
-						VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(),
-								app.getCpu(), vmType);
-						vm.startApplication(app);
-						insertStopEvent(currTime + app.getDuration(), app);
-						appDeployed = true;
+						appDeployed = startApp(pm, app);
 						log.info("Application " + app.toString() + " started on new pm "
 								+ pm.getId());
 					} else {
-						log.warn("Application " + app.toString() + " cannot be run on empty pm "
-								+ pm.getId());
+						log.warn("Application " + app.toString() + " cannot be run on pm "
+								+ pm.getId() + ", too little space");
 					}
 				}
 			}
@@ -129,6 +117,14 @@ public class SchedulerA extends AbstractScheduler {
 		}
 
 	}
+	
+	private boolean startApp(PhysicalMachine pm, Application app) {
+		VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(),
+				app.getCpu(), vmType);
+		vm.startApplication(app);
+		insertStopEvent(currTime + app.getDuration(), app);
+		return true;
+	}
 
 	// sorting applications by amount of resources (descending)
 	private List<Application> sortApps(LinkedList<SchedulerEvent> events) {
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
index 5be16e5..cbb006c 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
@@ -115,10 +115,11 @@ public class SchedulerB extends AbstractSchedulerWithMigration {
 				} catch (OutOfPMsException e) {
 					if (federation.askToOutsource(evt.getApp())) {
 						insertOutsourcedStartEvent(currTime + 1, evt.getApp());
-					} else
+					} else {
 						LOG.info("delaying the start of:" + evt.getApp());
-					delayedApps.add(evt.getApp());
-					return;
+						delayedApps.add(evt.getApp());
+						return;
+					}
 				}
 			}
 			if (deployed)
diff --git a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
index 9d52604..89e937b 100644
--- a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
+++ b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
@@ -1,6 +1,7 @@
 package at.ac.tuwien.lsdc.types;
 
 import java.util.HashMap;
+import java.util.Iterator;
 
 import at.ac.tuwien.lsdc.exception.ActiveApplicationsException;
 import at.ac.tuwien.lsdc.exception.VMsRunningException;
@@ -157,6 +158,18 @@ public class PhysicalMachine implements Comparable<PhysicalMachine> {
 	public VirtualMachine getVirtualMachine(int id) {
 		return VMs.get(id);
 	}
+	
+	public VirtualMachine getLatestVM() {
+		VirtualMachine vm = null;
+		for(Iterator<VirtualMachine> it = VMs.values().iterator(); it.hasNext(); ) {
+			vm = it.next();
+		}
+		return vm;
+	}
+	
+	public Integer getLatestVMID() {
+		return getLatestVM().getId();
+	}
 
 	/**
 	 * return a list of all VMs running on this PM.
-- 
2.43.0


From 0eed1063aa192c3f6a8849941d9e1f05ebe14d88 Mon Sep 17 00:00:00 2001
From: Stefan Derkits <stefan@derkits.at>
Date: Wed, 12 Jun 2013 14:19:18 +0200
Subject: [PATCH 08/16] small (but maybe important) bugfix

---
 src/at/ac/tuwien/lsdc/sched/SchedulerB.java | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
index cbb006c..e3384cc 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
@@ -96,6 +96,7 @@ public class SchedulerB extends AbstractSchedulerWithMigration {
 		LOG.debug("startApps():" + events);
 		boolean deployed = false;
 		for (SchedulerEvent evt : events) {
+			deployed = false;
 			VirtualMachine vm = null;
 			List<PhysicalMachine> sortedPMs = manager.getSortedPMs();
 			Collections.reverse(sortedPMs);
-- 
2.43.0


From 6a8eef158b2a8a57de2e975546ca4390f4b5e880 Mon Sep 17 00:00:00 2001
From: Stefan Derkits <stefan@derkits.at>
Date: Wed, 12 Jun 2013 16:58:43 +0200
Subject: [PATCH 09/16] implemented SchedC

---
 src/at/ac/tuwien/lsdc/sched/SchedulerC.java   | 69 ++++++++++++++-----
 .../ac/tuwien/lsdc/types/PhysicalMachine.java | 12 +++-
 2 files changed, 61 insertions(+), 20 deletions(-)

diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
index afe1837..1539854 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
@@ -2,24 +2,31 @@ package at.ac.tuwien.lsdc.sched;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Collections;
 import java.util.LinkedList;
+import java.util.List;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import at.ac.tuwien.lsdc.exception.OutOfPMsException;
 import at.ac.tuwien.lsdc.types.Application;
+import at.ac.tuwien.lsdc.types.PhysicalMachine;
 import at.ac.tuwien.lsdc.types.ScenarioType;
 import at.ac.tuwien.lsdc.types.SchedulerEvent;
 import at.ac.tuwien.lsdc.types.SchedulerType;
 import at.ac.tuwien.lsdc.types.VirtualMachine;
+import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
 
 public class SchedulerC extends AbstractSchedulerWithMigration {
 
-	private static final Logger log = LoggerFactory.getLogger(SchedulerC.class);
+	private static final Logger LOG = LoggerFactory.getLogger(SchedulerC.class);
 
 	public SchedulerC(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
 			throws IOException {
 		super(numPMs, numCloudPartners, schedulerLog, scenario);
+
+		this.vmType = VMType.NonResizable;
 	}
 
 	@Override
@@ -29,32 +36,58 @@ public class SchedulerC extends AbstractSchedulerWithMigration {
 
 	@Override
 	protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
-		// TODO Auto-generated method stub
-
-	}
-
-	@Override
-	protected void handleDelayedApps() {
-		// TODO Auto-generated method stub
-
+		LOG.debug("stopApps():" + events);
+		for (SchedulerEvent evt : events) {
+			VirtualMachine vm = evt.getApp().getRunningOn();
+			vm.stopApplication(evt.getApp());
+			if (vm.getApplications().size() == 0) {
+				PhysicalMachine pm = vm.getRunningOn();
+				pm.stopVirtualMachine(vm);
+				manager.stopPhysicalMachine(pm.getId());
+			}
+		}
 	}
 
 	@Override
 	protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
-		// TODO Auto-generated method stub
-
-	}
-
-	@Override
-	protected void runMigration() {
-		// TODO Auto-generated method stub
+		LOG.debug("startApps():" + events);
+		boolean deployed = false;
+		for (SchedulerEvent evt : events) {
+			deployed = false;
+			VirtualMachine vm = null;
+			List<PhysicalMachine> sortedPMs = manager.getSortedPMs();
+			Collections.reverse(sortedPMs);
+			for (PhysicalMachine pm : sortedPMs) {
+				if (pm.isRunning() && (pm.getTotalVMs() > 0)) {
+					vm = pm.getVirtualMachines().values().iterator().next();
+					deployed = deployApp(vm, evt.getApp());
+					if (deployed)
+						break;
+				}
+			}
+			if (vm == null) {
+				try {
+					vm = manager.startPhysicalMachine().startVirtualMachine(vmType);
+					deployed = deployApp(vm, evt.getApp());
+				} catch (OutOfPMsException e) {
+					if (federation.askToOutsource(evt.getApp())) {
+						insertOutsourcedStartEvent(currTime + 1, evt.getApp());
+					} else {
+						LOG.info("delaying the start of:" + evt.getApp());
+						delayedApps.add(evt.getApp());
+						return;
+					}
+				}
+			}
+			if (deployed)
+				insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
+		}
 
 	}
 
 	@Override
 	protected boolean deployApp(VirtualMachine vm, Application app) {
-		// TODO Auto-generated method stub
-		return false;
+		return vm.startApplication(app);
 	}
 
 }
diff --git a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
index 89e937b..4a482b0 100644
--- a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
+++ b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
@@ -65,6 +65,14 @@ public class PhysicalMachine implements Comparable<PhysicalMachine> {
 			return 0.0;
 	}
 
+	// Creates a VirtualMachine of maximum possible Size
+	public VirtualMachine startVirtualMachine(VMType type) {
+		VirtualMachine vm = new VirtualMachine(maxSize - initialSize - VirtualMachine.initialSize,
+				maxRAM - initialRAM - VirtualMachine.initialRAM, maxCPU - initialCPU
+						- VirtualMachine.initialCPU, this, type);
+		return vm;
+	}
+
 	public VirtualMachine startVirtualMachine(int sz, int ram, int cpu, VMType type) {
 		if (checkVM(sz, ram, cpu)) {
 			VirtualMachine vm = new VirtualMachine(sz, ram, cpu, this, type);
@@ -158,7 +166,7 @@ public class PhysicalMachine implements Comparable<PhysicalMachine> {
 	public VirtualMachine getVirtualMachine(int id) {
 		return VMs.get(id);
 	}
-	
+
 	public VirtualMachine getLatestVM() {
 		VirtualMachine vm = null;
 		for(Iterator<VirtualMachine> it = VMs.values().iterator(); it.hasNext(); ) {
@@ -166,7 +174,7 @@ public class PhysicalMachine implements Comparable<PhysicalMachine> {
 		}
 		return vm;
 	}
-	
+
 	public Integer getLatestVMID() {
 		return getLatestVM().getId();
 	}
-- 
2.43.0


From 6d578e61b3d024514e840f71397941c5bb694504 Mon Sep 17 00:00:00 2001
From: Andreas Egger <egger.andreas.1@gmail.com>
Date: Thu, 13 Jun 2013 13:16:20 +0200
Subject: [PATCH 10/16] Organize logfiles output - less error prone

---
 build.xml                                   | 25 ++++++++++-----------
 src/at/ac/tuwien/lsdc/SchedSimulator.java   |  8 +++----
 src/at/ac/tuwien/lsdc/sched/SchedulerB.java |  2 +-
 src/at/ac/tuwien/lsdc/sched/SchedulerC.java |  2 +-
 4 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/build.xml b/build.xml
index 8b84489..4b37e97 100644
--- a/build.xml
+++ b/build.xml
@@ -17,7 +17,6 @@
 
 	<assertions id="project.assertions">
 		<enable />
-		<!-- <disable/> -->
 	</assertions>
 
 	<target name="compile" description="Compile project.">
@@ -82,11 +81,11 @@
 			<arg value="${numCloudPartners}" />
 			<arg value="scenarios/scenarioA.csv" />
 			<arg value="logs/all.csv" />
-			<arg value="logs/scenarioA_schedulerA.csv" />
+			<arg value="logs/schedulerA_scenarioA.csv" />
 		</java>
 	</target>
 
-	<target name="run-scheduler-A-B" depends="compile" description="Run scheduler B on scenario A">
+	<target name="run-scheduler-A-B" depends="compile" description="Run scheduler A on scenario B">
 		<java classname="at.ac.tuwien.lsdc.SchedSimulator" fork="true" classpathref="project.classpath">
 			<assertions refid="project.assertions" />
 			<arg value="A" />
@@ -95,11 +94,11 @@
 			<arg value="${numCloudPartners}" />
 			<arg value="scenarios/scenarioA.csv" />
 			<arg value="logs/all.csv" />
-			<arg value="logs/scenarioA_schedulerB.csv" />
+			<arg value="logs/schedulerA_scenarioB.csv" />
 		</java>
 	</target>
 
-	<target name="run-scheduler-A-C" depends="compile" description="Run scheduler C on scenario A">
+	<target name="run-scheduler-A-C" depends="compile" description="Run scheduler A on scenario C">
 		<java classname="at.ac.tuwien.lsdc.SchedSimulator" fork="true" classpathref="project.classpath">
 			<assertions refid="project.assertions" />
 			<arg value="A" />
@@ -108,11 +107,11 @@
 			<arg value="${numCloudPartners}" />
 			<arg value="scenarios/scenarioA.csv" />
 			<arg value="logs/all.csv" />
-			<arg value="logs/scenarioA_schedulerC.csv" />
+			<arg value="logs/schedulerA_scenarioC.csv" />
 		</java>
 	</target>
 
-	<target name="run-scheduler-B-A" depends="compile" description="Run scheduler A on scenario B">
+	<target name="run-scheduler-B-A" depends="compile" description="Run scheduler B on scenario A">
 		<java classname="at.ac.tuwien.lsdc.SchedSimulator" fork="true" classpathref="project.classpath">
 			<assertions refid="project.assertions" />
 			<arg value="B" />
@@ -121,7 +120,7 @@
 			<arg value="${numCloudPartners}" />
 			<arg value="scenarios/scenarioB.csv" />
 			<arg value="logs/all.csv" />
-			<arg value="logs/scenarioB_schedulerA.csv" />
+			<arg value="logs/schedulerB_scenarioA.csv" />
 		</java>
 	</target>
 
@@ -134,7 +133,7 @@
 			<arg value="${numCloudPartners}" />
 			<arg value="scenarios/scenarioB.csv" />
 			<arg value="logs/all.csv" />
-			<arg value="logs/scenarioB_schedulerB.csv" />
+			<arg value="logs/schedulerB_scenarioB.csv" />
 		</java>
 	</target>
 
@@ -147,7 +146,7 @@
 			<arg value="${numCloudPartners}" />
 			<arg value="scenarios/scenarioC.csv" />
 			<arg value="logs/all.csv" />
-			<arg value="logs/scenarioC_schedulerB.csv" />
+			<arg value="logs/schedulerB_scenarioC.csv" />
 		</java>
 	</target>
 
@@ -160,7 +159,7 @@
 			<arg value="${numCloudPartners}" />
 			<arg value="scenarios/scenarioA.csv" />
 			<arg value="logs/all.csv" />
-			<arg value="logs/scenarioA_schedulerC.csv" />
+			<arg value="logs/schedulerC_scenarioA.csv" />
 		</java>
 	</target>
 
@@ -173,7 +172,7 @@
 			<arg value="${numCloudPartners}" />
 			<arg value="scenarios/scenarioB.csv" />
 			<arg value="logs/all.csv" />
-			<arg value="logs/scenarioB_schedulerC.csv" />
+			<arg value="logs/schedulerC_scenarioB.csv" />
 		</java>
 	</target>
 
@@ -186,7 +185,7 @@
 			<arg value="${numCloudPartners}" />
 			<arg value="scenarios/scenarioC.csv" />
 			<arg value="logs/all.csv" />
-			<arg value="logs/scenarioC_schedulerC.csv" />
+			<arg value="logs/schedulerC_scenarioC.csv" />
 		</java>
 	</target>
 
diff --git a/src/at/ac/tuwien/lsdc/SchedSimulator.java b/src/at/ac/tuwien/lsdc/SchedSimulator.java
index 2e92150..2ed2bb3 100644
--- a/src/at/ac/tuwien/lsdc/SchedSimulator.java
+++ b/src/at/ac/tuwien/lsdc/SchedSimulator.java
@@ -24,7 +24,7 @@ import at.ac.tuwien.lsdc.util.CSVParser;
  */
 public class SchedSimulator {
 
-	private static final String USAGE = "SchedSimulator needs exactly 7 parameters: scenario, scheduler, numPMS, numCloudPartners, inputFile, generalLogPath, schedulerLogPath";
+	private static final String USAGE = "SchedSimulator needs exactly 7 parameters: scheduler, scenario, numPMS, numCloudPartners, inputFile, generalLogPath, schedulerLogPath";
 
 	private static final Logger log = LoggerFactory
 			.getLogger(SchedSimulator.class);
@@ -73,8 +73,8 @@ public class SchedSimulator {
 
 	/**
 	 * Commandline arguments parsed (mandatory):
-	 * - type of scenario
 	 * - type of scheduler
+	 * - type of scenario
 	 * - number of available PMs
 	 * - number of cloud partners
 	 * - scenario input file with applications
@@ -83,8 +83,8 @@ public class SchedSimulator {
 	 * @param args the command line arguments to parse
 	 */
 	private static void parseCommandLineArgs(String[] args) {
-		String scenarioStr = args[0];
-		String schedulerTypeStr = args[1];
+		String schedulerTypeStr = args[0];
+		String scenarioStr = args[1];
 		String numPMsStr = args[2];
 		String numCloudPartnersStr = args[3];
 		String inputFileStr = args[4];
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
index e3384cc..b859824 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
@@ -108,7 +108,7 @@ public class SchedulerB extends AbstractSchedulerWithMigration {
 						break;
 				}
 			}
-			if (vm == null) {
+			if (!deployed) {
 				try {
 					vm = manager.startPhysicalMachine().startVirtualMachine(evt.getApp().getSize(),
 							evt.getApp().getRam(), evt.getApp().getCpu(), vmType);
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
index 1539854..1c594dd 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
@@ -65,7 +65,7 @@ public class SchedulerC extends AbstractSchedulerWithMigration {
 						break;
 				}
 			}
-			if (vm == null) {
+			if (!deployed) {
 				try {
 					vm = manager.startPhysicalMachine().startVirtualMachine(vmType);
 					deployed = deployApp(vm, evt.getApp());
-- 
2.43.0


From 1f5bf69c069c6c96706d36ed249fc35ce00c071f Mon Sep 17 00:00:00 2001
From: Andreas Egger <egger.andreas.1@gmail.com>
Date: Thu, 13 Jun 2013 13:27:03 +0200
Subject: [PATCH 11/16] Adjust migration algorithm to get max. utilization

---
 src/at/ac/tuwien/lsdc/SchedSimulator.java                 | 8 ++++----
 .../tuwien/lsdc/sched/AbstractSchedulerWithMigration.java | 4 +++-
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/at/ac/tuwien/lsdc/SchedSimulator.java b/src/at/ac/tuwien/lsdc/SchedSimulator.java
index 2ed2bb3..1c03d04 100644
--- a/src/at/ac/tuwien/lsdc/SchedSimulator.java
+++ b/src/at/ac/tuwien/lsdc/SchedSimulator.java
@@ -41,9 +41,9 @@ public class SchedSimulator {
 		if (args.length == 7) {
 			parseCommandLineArgs(args);
 		} else if (args.length == 1) {
-			String[] newArgs = args[0].split(" ");
-			if (newArgs.length == 7) {
-				parseCommandLineArgs(newArgs);
+			String[] splitArgs = args[0].split(" ");
+			if (splitArgs.length == 7) {
+				parseCommandLineArgs(splitArgs);
 			} else {
 				log.info(USAGE);
 				System.exit(1);
@@ -92,8 +92,8 @@ public class SchedSimulator {
 		String schedulerLogStr = args[6];
 
 		try {
-			scenario = ScenarioType.valueOf(scenarioStr);
 			schedulerType = SchedulerType.valueOf(schedulerTypeStr);
+			scenario = ScenarioType.valueOf(scenarioStr);
 			numPMs = Integer.parseInt(numPMsStr);
 			numCloudPartners = Integer.parseInt(numCloudPartnersStr);
 			inputFile = new File(inputFileStr);
diff --git a/src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java b/src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java
index e0fff5a..26d90ae 100644
--- a/src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java
+++ b/src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java
@@ -38,7 +38,9 @@ public abstract class AbstractSchedulerWithMigration extends AbstractScheduler {
 				VirtualMachine currentVM = currentPM.getVirtualMachines().values().iterator()
 						.next();
 				for (Application app : currentVM.getApplications()) {
-					for (int j = i; i < pms.size(); i++) {
+					
+					// try to fit app on most utilized machine to get maximum utilization
+					for (int j = pms.size()-1; j > i; j--) {
 						PhysicalMachine nextPM = pms.get(j);
 						if (nextPM.isRunning() && (nextPM.getTotalVMs() > 0)) {
 							VirtualMachine nextVM = nextPM.getVirtualMachines().values().iterator()
-- 
2.43.0


From b5ded62e964e4a63905a0d1cea5f8f62d85956fb Mon Sep 17 00:00:00 2001
From: Stefan Derkits <stefan@derkits.at>
Date: Thu, 13 Jun 2013 15:16:43 +0200
Subject: [PATCH 12/16] avoid concurrent modification exception in
 handleDelayedApps()

---
 src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
index 08735a4..7998823 100644
--- a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
+++ b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
@@ -206,8 +206,8 @@ public abstract class AbstractScheduler {
 		for (Application app : delayedApps) {
 			SchedulerEvent evt = new SchedulerEvent(currTime, EventType.startApplication, app);
 			delayedStartEvents.add(evt);
-			delayedApps.remove(app);
 		}
+		delayedApps.clear();
 		handleStartEvents(delayedStartEvents);
 	}
 
-- 
2.43.0


From 8b211d534b949d75e67b75df26142e4f8fb790bf Mon Sep 17 00:00:00 2001
From: Andreas Egger <egger.andreas.1@gmail.com>
Date: Thu, 13 Jun 2013 15:22:32 +0200
Subject: [PATCH 13/16] delayed apps test

---
 build.xml                                   |  4 ++--
 src/at/ac/tuwien/lsdc/sched/SchedulerA.java | 15 ++++++++++++---
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/build.xml b/build.xml
index 4b37e97..49475bb 100644
--- a/build.xml
+++ b/build.xml
@@ -5,8 +5,8 @@
 	<property name="lib.dir" value="lib" />
 	<property name="build.dir" value="bin" />
 
-	<property name="numPMs" value="7" />
-	<property name="numCloudPartners" value="2" />
+	<property name="numPMs" value="2" />
+	<property name="numCloudPartners" value="3" />
 
 	<path id="project.classpath">
 		<dirset dir="${build.dir}" />
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
index f7355c6..f65e699 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
@@ -57,9 +57,10 @@ public class SchedulerA extends AbstractScheduler {
 	@Override
 	protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
 		// sorting applications by amount of resources (descending)
-		List<Application> sortedApps = sortApps(events);
-
-		for (Application app : sortedApps) {
+//		List<Application> sortedApps = sortApps(events);
+		List<Application> apps = getApplications(events);
+		
+		for (Application app : apps) {
 			boolean appDeployed = false;
 
 			if (manager.getPMs().size() == 0) {
@@ -136,6 +137,14 @@ public class SchedulerA extends AbstractScheduler {
 		Collections.reverse(sortedApps);
 		return sortedApps;
 	}
+	
+	public List<Application> getApplications(LinkedList<SchedulerEvent> events) {
+		List<Application> apps = new LinkedList<Application>();
+		for(SchedulerEvent evt: events) {
+			apps.add(evt.getApp());
+		}
+		return apps;
+	}
 
 	@Override
 	protected String getSchedulerType() {
-- 
2.43.0


From 33cd813b4fdbfff0049fccd32faf824975690522 Mon Sep 17 00:00:00 2001
From: Andreas Egger <egger.andreas.1@gmail.com>
Date: Thu, 13 Jun 2013 15:37:23 +0200
Subject: [PATCH 14/16] fixed scenario params in build.xml

---
 build.xml | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/build.xml b/build.xml
index 49475bb..26fff2a 100644
--- a/build.xml
+++ b/build.xml
@@ -92,7 +92,7 @@
 			<arg value="B" />
 			<arg value="${numPMs}" />
 			<arg value="${numCloudPartners}" />
-			<arg value="scenarios/scenarioA.csv" />
+			<arg value="scenarios/scenarioB.csv" />
 			<arg value="logs/all.csv" />
 			<arg value="logs/schedulerA_scenarioB.csv" />
 		</java>
@@ -105,7 +105,7 @@
 			<arg value="C" />
 			<arg value="${numPMs}" />
 			<arg value="${numCloudPartners}" />
-			<arg value="scenarios/scenarioA.csv" />
+			<arg value="scenarios/scenarioC.csv" />
 			<arg value="logs/all.csv" />
 			<arg value="logs/schedulerA_scenarioC.csv" />
 		</java>
@@ -118,7 +118,7 @@
 			<arg value="A" />
 			<arg value="${numPMs}" />
 			<arg value="${numCloudPartners}" />
-			<arg value="scenarios/scenarioB.csv" />
+			<arg value="scenarios/scenarioA.csv" />
 			<arg value="logs/all.csv" />
 			<arg value="logs/schedulerB_scenarioA.csv" />
 		</java>
-- 
2.43.0


From 251ef7902782af43570b308542b348b38bd538d2 Mon Sep 17 00:00:00 2001
From: Stefan Derkits <stefan@derkits.at>
Date: Thu, 13 Jun 2013 16:02:05 +0200
Subject: [PATCH 15/16] create 1500 jobs & make jobs run longer

---
 build.xml                               | 6 +++---
 src/at/ac/tuwien/lsdc/JobGenerator.java | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/build.xml b/build.xml
index 26fff2a..97c8391 100644
--- a/build.xml
+++ b/build.xml
@@ -35,7 +35,7 @@
 		<java classname="at.ac.tuwien.lsdc.JobGenerator" fork="true" classpathref="project.classpath">
 			<assertions refid="project.assertions" />
 			<arg value="A" />
-			<arg value="500" />
+			<arg value="1500" />
 			<arg value="scenarios/scenarioA.csv" />
 		</java>
 	</target>
@@ -44,7 +44,7 @@
 		<java classname="at.ac.tuwien.lsdc.JobGenerator" fork="true" classpathref="project.classpath">
 			<assertions refid="project.assertions" />
 			<arg value="B" />
-			<arg value="500" />
+			<arg value="1500" />
 			<arg value="scenarios/scenarioB.csv" />
 		</java>
 	</target>
@@ -53,7 +53,7 @@
 		<java classname="at.ac.tuwien.lsdc.JobGenerator" fork="true" classpathref="project.classpath">
 			<assertions refid="project.assertions" />
 			<arg value="C" />
-			<arg value="500" />
+			<arg value="1500" />
 			<arg value="scenarios/scenarioC.csv" />
 		</java>
 	</target>
diff --git a/src/at/ac/tuwien/lsdc/JobGenerator.java b/src/at/ac/tuwien/lsdc/JobGenerator.java
index 49bb796..091ecc3 100644
--- a/src/at/ac/tuwien/lsdc/JobGenerator.java
+++ b/src/at/ac/tuwien/lsdc/JobGenerator.java
@@ -22,7 +22,7 @@ public class JobGenerator {
 
 	private static int numWBA;
 	private static int numHPA;
-	
+
 	private static Random rand = new Random();
 
 	private static CSVWriter writer;
@@ -108,7 +108,7 @@ public class JobGenerator {
 		int size = randomInt(20, 100);
 		int ram = randomInt(30, 50);
 		int cpu = randomInt(50, 100);
-		int duration = randomInt(1, 50);
+		int duration = randomInt(10, 100) + 75;
 		numWBA++;
 		writeApplication(timestamp,size,ram,cpu,duration);
 	}
@@ -117,7 +117,7 @@ public class JobGenerator {
 		int size = randomInt(150, 500);
 		int ram = randomInt(100, 700);
 		int cpu = randomInt(100, 400);
-		int duration = randomInt(1, 50);
+		int duration = randomInt(10, 100) + 75;
 		numHPA++;
 		writeApplication(timestamp,size,ram,cpu,duration);
 	}
-- 
2.43.0


From 40d8f3d8745cc9479bb490794f72519321480108 Mon Sep 17 00:00:00 2001
From: Stefan Derkits <stefan@derkits.at>
Date: Thu, 13 Jun 2013 16:10:18 +0200
Subject: [PATCH 16/16] lower timestamp advance in JobGenerator

---
 src/at/ac/tuwien/lsdc/JobGenerator.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/at/ac/tuwien/lsdc/JobGenerator.java b/src/at/ac/tuwien/lsdc/JobGenerator.java
index 091ecc3..02ee329 100644
--- a/src/at/ac/tuwien/lsdc/JobGenerator.java
+++ b/src/at/ac/tuwien/lsdc/JobGenerator.java
@@ -47,7 +47,7 @@ public class JobGenerator {
 		writeCSVHeader();
 		int timestamp = 1;
 		for (int i = 0; i < count; i++) {
-			timestamp = timestamp + randomInt(0, 4);
+			timestamp = timestamp + randomInt(0, 2);
 			switch (scenario) {
 			case A:
 				generateWBA(timestamp);
-- 
2.43.0