From 98a5c8f0740dd4830a666122b4892003fa40e226 Mon Sep 17 00:00:00 2001 From: Stefan Derkits Date: Sat, 18 May 2013 18:38:46 +0200 Subject: [PATCH] started implementing MachineManager, VM & PM --- .../lsdc/management/MachineManager.java | 20 +++- src/at/ac/tuwien/lsdc/types/Application.java | 47 +++++--- .../ac/tuwien/lsdc/types/PhysicalMachine.java | 104 ++++++++++++++++++ .../ac/tuwien/lsdc/types/VirtualMachine.java | 92 ++++++++++++++++ 4 files changed, 245 insertions(+), 18 deletions(-) diff --git a/src/at/ac/tuwien/lsdc/management/MachineManager.java b/src/at/ac/tuwien/lsdc/management/MachineManager.java index 027d64a..49bf340 100644 --- a/src/at/ac/tuwien/lsdc/management/MachineManager.java +++ b/src/at/ac/tuwien/lsdc/management/MachineManager.java @@ -1,8 +1,22 @@ package at.ac.tuwien.lsdc.management; +import java.util.HashMap; + +import at.ac.tuwien.lsdc.types.PhysicalMachine; + public class MachineManager { - //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 + // 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 + + private HashMap PMs = new HashMap(); + public double getTotalConsumption() { + double consumption = 0; + for (PhysicalMachine pm : PMs.values()) { + if (pm.isRunning()) + consumption = consumption + pm.getConsumption(); + } + return consumption; + } } diff --git a/src/at/ac/tuwien/lsdc/types/Application.java b/src/at/ac/tuwien/lsdc/types/Application.java index b5ead0a..618261e 100644 --- a/src/at/ac/tuwien/lsdc/types/Application.java +++ b/src/at/ac/tuwien/lsdc/types/Application.java @@ -1,15 +1,18 @@ package at.ac.tuwien.lsdc.types; public class Application implements Comparable { - + private int id; private long timestamp; private int size; private int ram; private int cpu; private int duration; - - public Application(int id, long timestamp, int size, int ram, int cpu, int duration) { + + private VirtualMachine runningOn; + + public Application(int id, long timestamp, int size, int ram, int cpu, + int duration) { this.id = id; this.timestamp = timestamp; this.size = size; @@ -17,7 +20,7 @@ public class Application implements Comparable { this.cpu = cpu; this.duration = duration; } - + public int getID() { return id; } @@ -41,25 +44,39 @@ public class Application implements Comparable { public int getDuration() { return duration; } - + 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() { + return runningOn; + } + + public void setRunningOn(VirtualMachine vm) { + runningOn = vm; } @Override public int compareTo(Application other) { final int BEFORE = -1; - final int EQUAL = 0; - final int AFTER = 1; + final int EQUAL = 0; + final int AFTER = 1; - //this optimization is usually worthwhile, and can - //always be added - if ( this == other ) return EQUAL; - if ( this.getTimestamp() == other.getTimestamp() ) return EQUAL; - if ( this.getTimestamp() < other.getTimestamp() ) return BEFORE; - if ( this.getTimestamp() > other.getTimestamp() ) return AFTER; + // this optimization is usually worthwhile, and can + // always be added + if (this == other) + return EQUAL; + if (this.getTimestamp() == other.getTimestamp()) + return EQUAL; + if (this.getTimestamp() < other.getTimestamp()) + return BEFORE; + if (this.getTimestamp() > other.getTimestamp()) + return AFTER; - return EQUAL; + return EQUAL; } } diff --git a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java index f7b2722..9489797 100644 --- a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java +++ b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java @@ -1,5 +1,109 @@ package at.ac.tuwien.lsdc.types; +import java.util.HashMap; + public class PhysicalMachine { + private static int count = 0; + + private HashMap VMs = new HashMap(); + + private int id; + + private final int maxSize = 50000; + private final int maxRAM = 4700; + private final int maxCPU = 2400; + + private int size; + private int RAM; + private int CPU; + + private final int initialSize = 850; + private final int initialRAM = 300; + private final int initialCPU = 500; + + private boolean running = false; + + public PhysicalMachine() { + id = count; + count++; + } + + public void start() { + size = initialSize; + RAM = initialRAM; + CPU = initialCPU; + running = true; + } + + public void stop() { + // TODO: anything else we need to do? maybe implement a stop method in + // VirtualMachine and call it on every VM + VMs = new HashMap(); + size = 0; + RAM = 0; + CPU = 0; + running = false; + } + + public double getConsumption() { + return 200 + 0.3 * CPU; + } + + public int startVirtualMachine(int size, int RAM, int CPU) { + if (checkVM(size, RAM, CPU)) { + VirtualMachine vm = new VirtualMachine(size, RAM, CPU, this); + VMs.put(vm.getId(), vm); + size = size + vm.getSize(); + RAM = RAM + vm.getRAM(); + CPU = CPU + vm.getCPU(); + return vm.getId(); + } else + return -1; + } + + public boolean stopVirtualMachine(VirtualMachine vm) { + if (VMs.containsKey(vm.getId())) { + if (vm.getApplications().size() != 0) { + // apps must be migrated before stopping a VM + return false; + } else { + VMs.remove(vm.getId()); + size = size - vm.getSize(); + RAM = RAM - vm.getRAM(); + CPU = CPU - vm.getCPU(); + return true; + } + } else + return false; + } + + private boolean checkVM(int size, int RAM, int CPU) { + return (size <= availableSize()) && (RAM <= availableRAM()) + && (CPU <= availableCPU()); + } + + private int availableSize() { + return maxSize - size; + } + + private int availableRAM() { + return maxRAM - RAM; + } + + private int availableCPU() { + return maxCPU - CPU; + } + + public int getId() { + return id; + } + + public boolean isRunning() { + return running; + } + + public VirtualMachine getVirtualMachine(int id) { + return VMs.get(id); + } } diff --git a/src/at/ac/tuwien/lsdc/types/VirtualMachine.java b/src/at/ac/tuwien/lsdc/types/VirtualMachine.java index 2f1108b..766e55c 100644 --- a/src/at/ac/tuwien/lsdc/types/VirtualMachine.java +++ b/src/at/ac/tuwien/lsdc/types/VirtualMachine.java @@ -1,5 +1,97 @@ package at.ac.tuwien.lsdc.types; +import java.util.ArrayList; +import java.util.HashMap; + public class VirtualMachine { + private static int count = 0; + + private PhysicalMachine runningOn; + + private HashMap applications = new HashMap(); + + private int id; + + public static final int initialSize = 100; + public static final int initialRAM = 50; + public static final int initialCPU = 150; + + private int size; + private int RAM; + private int CPU; + + public VirtualMachine(int size, int RAM, int CPU, PhysicalMachine pm) { + this.id = count; + count++; + this.size = size + initialSize; + this.RAM = RAM + initialRAM; + this.CPU = CPU + initialCPU; + this.runningOn = pm; + } + + public boolean startApplication(Application app) { + if (checkApp(app)) { + applications.put(app.getID(), app); + size = size + app.getSize(); + RAM = RAM + app.getRam(); + CPU = CPU + app.getCpu(); + return true; + } else + return false; + } + + public boolean stopApplication(Application app) { + if (applications.containsKey(app.getID())) { + size = size - app.getSize(); + RAM = RAM - app.getRam(); + CPU = CPU - app.getCpu(); + applications.remove(app.getID()); + return true; + } else + return false; + } + + private boolean checkApp(Application app) { + return (app.getSize() <= availableSize()) + && (app.getRam() <= availableRAM()) + && (app.getCpu() <= availableCPU()); + } + + private int availableSize() { + return size - initialSize; + } + + private int availableRAM() { + return RAM - initialRAM; + } + + private int availableCPU() { + return CPU - initialCPU; + } + + public int getId() { + return id; + } + + public PhysicalMachine getRunningOn() { + return runningOn; + } + + public ArrayList getApplications() { + return new ArrayList(applications.values()); + } + + public int getSize() { + return size; + } + + public int getRAM() { + return RAM; + } + + public int getCPU() { + return CPU; + } + } -- 2.43.0