Commit 91a440f6 authored by Nils Vreman's avatar Nils Vreman
Browse files

Added ScheduLearn project to my project folder

parents
package strategy;
import java.util.Comparator;
import tasks.Task;
public interface PriorityComparator<Task> extends Comparator<Task> {
/*
* compares two tasks according to scheduling algorithm.
*/
public int compare(Object t1, Object t2);
}
package strategy;
import tasks.Taskset;
import tasks.Task;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Arrays;
public abstract class QueueStrategy implements SchedulingStrategy {
/*
* Generate schedule from taskset ts based on the desired strategy
* Return: int array containing task number i at slot position j. Empty array if taskset is invalid.
*/
public int[] generate(Taskset ts) {
// If we haven't added any tasks yet: return empty array
if (ts.nbrTasks() == 0) return new int[0];
int[] schedule = new int[ts.hyperperiod()];
Arrays.fill(schedule, 0);
Queue<Integer> taskQ = new PriorityQueue<>();
for (int t = 0; t < schedule.length; t++) {
taskQ = updateQueue(taskQ, ts, t);
if (taskQ.size() != 0) schedule[t] = taskQ.poll();
}
return schedule;
}
/*
* Return: Current version of the ready queue
*/
protected abstract Queue<Integer> updateQueue(Queue<Integer> q, Taskset ts, int time);
}
package strategy;
import java.util.Comparator;
import java.util.Queue;
import java.util.PriorityQueue;
import java.util.Map;
import java.util.HashMap;
import tasks.Taskset;
import tasks.Task;
public class RMS extends QueueStrategy {
/*
* Return: Current version of the ready queue according to the EDF scheduling strategy
*/
protected Queue<Integer> updateQueue(Queue<Integer> q, Taskset ts, int time) {
//Create new Q that uses order comparator and move all current tasks to that Q
Comparator<Integer> order =
(Integer i1, Integer i2)->
ts.getTask(i1).get().period() - time % ts.getTask(i1).get().period() -
(ts.getTask(i2).get().period() - time % ts.getTask(i2).get().period());
Queue<Integer> internalQ = new PriorityQueue<>(order);
while (q.size() != 0) internalQ.add(q.poll());
//If we are at a job release, add that much CPU usage to the Q.
for (int taskNbr : ts.getPriorities()) {
Task t = ts.getTask(taskNbr).get(); //I know it exists so I can collect it from the optional directly
if (time % t.period() == 0) {
for (int i = 0; i < t.exectime(); i++) {
internalQ.add(taskNbr);
}
}
}
return internalQ;
}
/*
* Return which Strategy you are using
*/
public String toString() {
return "RMS";
}
/*
* Return a Comparator for this scheduling strategy
*/
public Comparator<Task> prioComparator() {
return new RMSComparator();
}
}
package strategy;
import java.util.Comparator;
import tasks.Task;
public class RMSComparator implements PriorityComparator {
/*
* compares two tasks according to scheduling algorithm.
*/
public int compare(Object o1, Object o2) {
Task t1 = (Task)o1;
Task t2 = (Task)o2;
if (t1.period() == t2.period()) {
return t1.deadline() - t2.deadline();
}
return t1.period() - t2.period();
}
}
package strategy;
import java.util.Comparator;
import tasks.Taskset;
import tasks.Task;
public interface SchedulingStrategy {
/*
* Generate schedule from taskset ts based on the desired strategy
* Return: int array containing task number i at slot position j. Empty array if taskset is invalid.
*/
public int[] generate(Taskset ts);
/*
* Return which Strategy you are using
*/
public String toString();
/*
* Return a Comparator for this scheduling strategy
*/
public Comparator<Task> prioComparator();
/*
* Return True if this is the same SchedulingStrategy as strat; else false
*/
public default boolean equals(SchedulingStrategy strat) {
return toString().equals(strat.toString());
}
}
package tasks;
public class Task {
private int exectime, period, deadline;
public Task(int exectime, int period, int deadline) {
this.exectime = exectime;
this.period = period;
this.deadline = deadline;
}
public boolean isImplicit() { return this.period == this.deadline; }
public int exectime() { return exectime; }
public int period() { return period; }
public int deadline() { return deadline; }
public int[] attributes() { return new int[]{exectime, period, deadline}; }
public double utilization() { return ((double) exectime) / ((double) period); }
public String toString() {
return exectime + "," + period + "," + deadline;
}
}
package tasks;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;
import java.util.Observable;
import java.util.Optional;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.io.FileNotFoundException;
public class Taskset extends Observable {
private List<Task> tasks;
private Task idle;
private Comparator<Task> stratComp;
/*
* Constructor
*/
public Taskset() {
tasks = new ArrayList<>();
}
/*
* Add task, with attributes, to taskset
*/
public void addTask(int exec, int period, int deadline) {
tasks.add(new Task(exec, period, deadline));
tasks.sort(stratComp); // Sort tasks in order according to comparator
idle = generateIdle();
setChanged();
notifyObservers();
}
/*
* Sorts this Taskset based on the strategy comparator
*/
public void setComparator(Comparator<Task> stratComp) {
this.stratComp = stratComp;
tasks.sort(stratComp); // Sort tasks in order according to comparator
setChanged();
notifyObservers(new Boolean(true));
}
/*
* change task, with attributes, in taskset
*/
public void changeTask(int prio, int exec, int period, int deadline) throws NoSuchElementException {
if (tasks.size() < prio) throw new NoSuchElementException("Task with priority " + prio + " doesn't exist!");
tasks.set(prio - 1, new Task(exec, period, deadline));
tasks.sort(stratComp); // Sort tasks in order according to comparator
idle = generateIdle();
setChanged();
notifyObservers();
}
/*
* Remove task with priority from taskset
*/
public void removeTask(int prio) throws NoSuchElementException {
if (tasks.size() < prio) throw new NoSuchElementException("Task with priority " + prio + " doesn't exist!");
tasks.remove(prio - 1);
tasks.sort(stratComp); // Sort tasks in order according to comparator
idle = generateIdle();
setChanged();
notifyObservers();
}
/*
* Generates new Idle task based on current tasks in taskset
*/
private Task generateIdle() {
double idle_u = 1 - utilization();
int idle_period = hyperperiod();
int idle_exectime = (int) (idle_u * idle_period);
return new Task(idle_exectime, idle_period, idle_period);
}
/*
* Prints the taskset by printing:
* Each task with: Priority, execution time, period, and deadline.
*/
public String toString() {
StringBuilder sb = new StringBuilder("Pri\te_i\tt_i\td_i\n");
for (int i = 0; i < tasks.size(); i++) {
Task t = tasks.get(i);
sb.append(i + "\t"
+ t.exectime() + "\t"
+ t.period() + "\t"
+ t.deadline() + "\n");
}
sb.delete(sb.length()-1, sb.length());
return sb.toString();
}
/*
* Return: Hyperperiod of this taskset
*/
public int hyperperiod() {
if (nbrTasks() == 0) return 0;
int[] periods = new int[tasks.size()];
int i = 0;
for (Task task : tasks) {
periods[i] = task.period();
i++;
}
return Util.lcm(periods);
}
/*
* Return: Optional<Task> with priority i or empty optional if it doesn't exist.
* Throws NoSuchElementException if i is illegal
*/
public Optional<Task> getTask(int i) {
if (i == 0) return Optional.of(idle);
return Optional.ofNullable(tasks.get(i-1));
}
/*
* Return: Priority set
*/
public Set<Integer> getPriorities() {
Set<Integer> set = new TreeSet<>();
for (int i = 1; i <= tasks.size(); i++) set.add(i);
return set;
}
/*
* Return: number of tasks in taskset
*/
public int nbrTasks() { return tasks.size(); }
/*
* Return: Total taskset utilization()
*/
public double utilization() {
double u = 0;
for (Task task : tasks) u += task.utilization();
return u;
}
/*
* Return: task with priority i:s utilization
* or 0 if doesn't exist
*/
public double utilization(int i) {
if (i == 0) return idle.utilization();
return (tasks.size() >= i) ? tasks.get(i-1).utilization() : 0;
}
/*
* Saves the taskset in file
*/
public void save(String path) throws FileNotFoundException {
TasksetPrintStream writer = null;
try {
writer = new TasksetPrintStream(path);
} catch (FileNotFoundException e) {
throw e;
}
writer.save(tasks);
setChanged();
notifyObservers();
}
/*
* Loads the taskset in file
*/
public void load(String path) throws FileNotFoundException {
TasksetBufferedReader reader = null;
try {
reader = new TasksetBufferedReader(path);
} catch (FileNotFoundException e) {
throw e;
}
tasks.clear();
try {
reader.load(this);
} catch (Exception e) {
return;
}
setChanged();
notifyObservers();
}
}
package tasks;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.util.Map;
public class TasksetBufferedReader extends BufferedReader {
public TasksetBufferedReader(String name) throws FileNotFoundException {
super(new FileReader(name));
}
public void load(Taskset taskset) throws NumberFormatException, IOException {
try {
while (ready()) {
String[] line = readLine().trim().split(",");
try {
taskset.addTask(parse(line[0]), parse(line[1]), parse(line[2]));
} catch (NumberFormatException e) {
throw e;
}
}
} catch (IOException e) {
throw e;
}
}
private int parse(String s) throws NumberFormatException {
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
throw e;
}
}
}
package tasks;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.List;
public class TasksetPrintStream extends PrintStream {
public TasksetPrintStream(String fileName) throws FileNotFoundException {
super(fileName);
}
public void save(List<Task> tasks) {
for (Task task: tasks) {
println(task.toString());
}
flush();
close();
}
}
package tasks;
/*
* Utility class
*/
public class Util {
public static int gcd(int a, int b) {
while (b > 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
private static int lcm(int a, int b) {
return a * (b / gcd(a, b));
}
public static int lcm(int[] in) {
int result = in[0];
for (int i = 1; i < in.length; i++) result = lcm(result, in[i]);
return result;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment