From ef2d8bd0ecd8397c49684405bcbf92ab36412ea2 Mon Sep 17 00:00:00 2001 From: Michael Schmid Date: Fri, 2 Aug 2019 10:35:57 +0200 Subject: [PATCH] refactoring of simulator for SchmidMottok --- src/main/java/mvd/jester/App.java | 4 ++-- src/main/java/mvd/jester/simulator/AbstractSimulator.java | 60 +++++++++++++++++++++++++++++++++++++++++++++--------------- src/main/java/mvd/jester/simulator/MaiaBertogna.java | 42 +++++++----------------------------------- src/main/java/mvd/jester/simulator/SchmidMottok.java | 20 +++++++++++++++++--- src/main/java/mvd/jester/simulator/internals/JobContext.java | 86 -------------------------------------------------------------------------------------- src/main/java/mvd/jester/simulator/internals/JobContextInterface.java | 19 +++++++++++++++++++ src/main/java/mvd/jester/simulator/internals/ProcessorContext.java | 14 +++++++------- src/main/java/mvd/jester/simulator/internals/SegmentContext.java | 49 ------------------------------------------------- src/main/java/mvd/jester/simulator/internals/SortedTaskContextSet.java | 2 +- src/main/java/mvd/jester/simulator/internals/TaskContext.java | 79 ------------------------------------------------------------------------------- src/main/java/mvd/jester/simulator/internals/TaskContextInterface.java | 18 ++++++++++++++++++ src/main/java/mvd/jester/simulator/internals/TaskletContext.java | 19 ------------------- src/main/java/mvd/jester/simulator/internals/maiabertogna/JobContext.java | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/mvd/jester/simulator/internals/maiabertogna/SegmentContext.java | 43 +++++++++++++++++++++++++++++++++++++++++++ src/main/java/mvd/jester/simulator/internals/maiabertogna/TaskContext.java | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/mvd/jester/simulator/internals/schmidmottok/JobContext.java | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/mvd/jester/simulator/internals/schmidmottok/SegmentContext.java | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/mvd/jester/simulator/internals/schmidmottok/TaskContext.java | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/mvd/jester/simulator/internals/schmidmottok/TaskletContext.java | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/mvd/jester/tests/TestEnvironment.java | 110 -------------------------------------------------------------------------------------------------------------- 20 files changed, 625 insertions(+), 406 deletions(-) delete mode 100644 src/main/java/mvd/jester/simulator/internals/JobContext.java create mode 100644 src/main/java/mvd/jester/simulator/internals/JobContextInterface.java delete mode 100644 src/main/java/mvd/jester/simulator/internals/SegmentContext.java delete mode 100644 src/main/java/mvd/jester/simulator/internals/TaskContext.java create mode 100644 src/main/java/mvd/jester/simulator/internals/TaskContextInterface.java delete mode 100644 src/main/java/mvd/jester/simulator/internals/TaskletContext.java create mode 100644 src/main/java/mvd/jester/simulator/internals/maiabertogna/JobContext.java create mode 100644 src/main/java/mvd/jester/simulator/internals/maiabertogna/SegmentContext.java create mode 100644 src/main/java/mvd/jester/simulator/internals/maiabertogna/TaskContext.java create mode 100644 src/main/java/mvd/jester/simulator/internals/schmidmottok/JobContext.java create mode 100644 src/main/java/mvd/jester/simulator/internals/schmidmottok/SegmentContext.java create mode 100644 src/main/java/mvd/jester/simulator/internals/schmidmottok/TaskContext.java create mode 100644 src/main/java/mvd/jester/simulator/internals/schmidmottok/TaskletContext.java delete mode 100644 src/main/java/mvd/jester/tests/TestEnvironment.java diff --git a/src/main/java/mvd/jester/App.java b/src/main/java/mvd/jester/App.java index 1bb4f56..2bfd00d 100644 --- a/src/main/java/mvd/jester/App.java +++ b/src/main/java/mvd/jester/App.java @@ -1,6 +1,5 @@ package mvd.jester; -import java.io.File; import mvd.jester.model.SystemSetup; /** @@ -13,10 +12,11 @@ public class App { TestEnvironment te = new TestEnvironment(builder, 40000); - // te.registerTestInterface(SchmidMottok.class); te.registerTestPair(mvd.jester.tests.MaiaBertogna.class, mvd.jester.simulator.MaiaBertogna.class); + te.registerTestPair(mvd.jester.tests.SchmidMottok.class, + mvd.jester.simulator.SchmidMottok.class); te.runTests(); diff --git a/src/main/java/mvd/jester/simulator/AbstractSimulator.java b/src/main/java/mvd/jester/simulator/AbstractSimulator.java index 54aa99e..90f190f 100644 --- a/src/main/java/mvd/jester/simulator/AbstractSimulator.java +++ b/src/main/java/mvd/jester/simulator/AbstractSimulator.java @@ -1,14 +1,14 @@ package mvd.jester.simulator; import java.util.HashSet; +import java.util.Optional; import java.util.Set; -import com.google.common.math.LongMath; +import java.util.TreeSet; import mvd.jester.model.SystemSetup; -import mvd.jester.model.Task; import mvd.jester.priority.RateMonotonic; import mvd.jester.simulator.internals.ProcessorContext; import mvd.jester.simulator.internals.SortedTaskContextSet; -import mvd.jester.simulator.internals.TaskContext; +import mvd.jester.simulator.internals.TaskContextInterface; /** @@ -29,10 +29,49 @@ public abstract class AbstractSimulator implements SimulatorInterface { processors.add(new ProcessorContext(i)); } this.hyperPeriod = systemSetup.getTasks().last().getPeriod() * 10; - // LongMath.pow(systemSetup.getTasks().last().getPeriod(), 2); } - protected void init() { + + protected abstract boolean releaseTasks(long timeStep); + + @Override + public boolean runSimulation() { + init(); + for (int t = 0; t < hyperPeriod; ++t) { + if (!releaseTasks(t)) { + return false; + } + + Set sortedProcessors = + new TreeSet<>((p1, p2) -> (int) (!p1.getJob().isPresent() ? -1 + : !p2.getJob().isPresent() ? 1 : -1)); + + processors.forEach(p -> sortedProcessors.add(p)); + for (ProcessorContext p : sortedProcessors) { + for (TaskContextInterface tc : readyTasks) { + if (p.acceptTask(tc, t)) { + break; + } + } + + } + + for (ProcessorContext p : processors) { + Optional optionalTc = p.updateExecution(t); + if (optionalTc.isPresent()) { + TaskContextInterface tc = optionalTc.get(); + if (t >= tc.getDeadline()) { + return false; + } + readyTasks.remove(optionalTc.get()); + } + } + } + + return true; + } + + private void init() { this.readyTasks.clear(); for (ProcessorContext p : processors) { p.setJob(null); @@ -40,15 +79,6 @@ public abstract class AbstractSimulator implements SimulatorInterface { this.hyperPeriod = systemSetup.getTasks().last().getPeriod() * 10; } - protected boolean releaseTasks(long timeStep) { - for (Task t : systemSetup.getTasks()) { - if (timeStep % t.getPeriod() == 0) { - if (!readyTasks.add(new TaskContext(t, timeStep))) { - return false; - } - } - } - return true; - } + } diff --git a/src/main/java/mvd/jester/simulator/MaiaBertogna.java b/src/main/java/mvd/jester/simulator/MaiaBertogna.java index a74729c..cd0f096 100644 --- a/src/main/java/mvd/jester/simulator/MaiaBertogna.java +++ b/src/main/java/mvd/jester/simulator/MaiaBertogna.java @@ -1,11 +1,8 @@ package mvd.jester.simulator; -import java.util.Comparator; -import java.util.Optional; -import java.util.TreeSet; import mvd.jester.model.SystemSetup; -import mvd.jester.simulator.internals.ProcessorContext; -import mvd.jester.simulator.internals.TaskContext; +import mvd.jester.model.Task; +import mvd.jester.simulator.internals.maiabertogna.TaskContext; /** * MaiaBertogna @@ -17,39 +14,14 @@ public class MaiaBertogna extends AbstractSimulator { } @Override - public boolean runSimulation() { - init(); - for (int t = 0; t < hyperPeriod; ++t) { - if (!releaseTasks(t)) { - return false; - } - - TreeSet sortedProcessors = - new TreeSet<>((p1, p2) -> (int) (!p1.getJob().isPresent() ? -1 - : !p2.getJob().isPresent() ? 1 : -1)); - - processors.forEach(p -> sortedProcessors.add(p)); - for (ProcessorContext p : sortedProcessors) { - for (TaskContext tc : readyTasks) { - if (p.acceptTask(tc, t)) { - break; - } - } - - } - - for (ProcessorContext p : processors) { - Optional optionalTc = p.updateExecution(t); - if (optionalTc.isPresent()) { - TaskContext tc = optionalTc.get(); - if (t >= tc.getDeadline()) { - return false; - } - readyTasks.remove(optionalTc.get()); + protected boolean releaseTasks(long timeStep) { + for (Task t : systemSetup.getTasks()) { + if (timeStep % t.getPeriod() == 0) { + if (!readyTasks.add(new TaskContext(t, timeStep))) { + return false; } } } - return true; } diff --git a/src/main/java/mvd/jester/simulator/SchmidMottok.java b/src/main/java/mvd/jester/simulator/SchmidMottok.java index bf1e7e8..5c435ad 100644 --- a/src/main/java/mvd/jester/simulator/SchmidMottok.java +++ b/src/main/java/mvd/jester/simulator/SchmidMottok.java @@ -1,21 +1,35 @@ package mvd.jester.simulator; + import mvd.jester.model.SystemSetup; +import mvd.jester.model.Task; +import mvd.jester.simulator.internals.schmidmottok.TaskContext; /** * SchmidMottok */ public class SchmidMottok extends AbstractSimulator { - SchmidMottok(SystemSetup systemSetup) { + public SchmidMottok(SystemSetup systemSetup) { super(systemSetup); } + @Override - public boolean runSimulation() { - return false; + protected boolean releaseTasks(long timeStep) { + for (Task t : systemSetup.getTasks()) { + if (timeStep % t.getPeriod() == 0) { + TaskContext tc = new TaskContext(t, timeStep); + if (!readyTasks.add(tc)) { + return false; + } + // System.out.println("Time " + timeStep + ": " + "Task " + tc + "activated!"); + } + } + return true; } + @Override public String getName() { return "SchmidMottok"; diff --git a/src/main/java/mvd/jester/simulator/internals/JobContext.java b/src/main/java/mvd/jester/simulator/internals/JobContext.java deleted file mode 100644 index b2e82e5..0000000 --- a/src/main/java/mvd/jester/simulator/internals/JobContext.java +++ /dev/null @@ -1,86 +0,0 @@ -package mvd.jester.simulator.internals; - -import java.util.Optional; - -/** - * Job - * - * @param - */ -public class JobContext { - - private final TaskContext taskContext; - private final SegmentContext segmentContext; - private final long wcet; - private Optional currentProcessor; - private long executionTime; - - - public JobContext(TaskContext taskContext, SegmentContext segmentContext) { - this.currentProcessor = Optional.empty(); - this.taskContext = taskContext; - this.segmentContext = segmentContext; - this.wcet = segmentContext.getSegment().getJobWcet(); - this.executionTime = wcet; - } - - public Optional updateExecution(long time) { - executionTime--; - - if (executionTime == 0) { - // System.out.println("Time " + time + ": " + currentProcessor.get() - // + " finished execution of job " + this + "!"); - currentProcessor.get().setJob(null); - currentProcessor = Optional.empty(); - return taskContext.acceptNotification(time); - } - - return Optional.empty(); - } - - public boolean checkExecutionTime() { - return executionTime > 0; - } - - /** - * @return the wcet - */ - public long getWcet() { - return wcet; - } - - /** - * @param processor the currentProcessor to set - */ - public void setCurrentProcessor(ProcessorContext processor) { - this.currentProcessor = Optional.ofNullable(processor); - } - - /** - * @return the currentProcessor - */ - public Optional getCurrentProcessor() { - return currentProcessor; - } - - /** - * @return the segmentContext - */ - public SegmentContext getSegmentContext() { - return segmentContext; - } - - - /** - * @return the taskContext - */ - public TaskContext getTaskContext() { - return taskContext; - } - - @Override - public String toString() { - return "(jobWcet=" + wcet + ")"; - } - -} diff --git a/src/main/java/mvd/jester/simulator/internals/JobContextInterface.java b/src/main/java/mvd/jester/simulator/internals/JobContextInterface.java new file mode 100644 index 0000000..9f67651 --- /dev/null +++ b/src/main/java/mvd/jester/simulator/internals/JobContextInterface.java @@ -0,0 +1,19 @@ +package mvd.jester.simulator.internals; + +import java.util.Optional; + +/** + * JobContextInterface + */ +public interface JobContextInterface { + + public Optional updateExecution(long time); + + public boolean checkExecutionTime(); + + public void setCurrentProcessor(ProcessorContext processor); + + public Optional getCurrentProcessor(); + + public TaskContextInterface getTaskContext(); +} diff --git a/src/main/java/mvd/jester/simulator/internals/ProcessorContext.java b/src/main/java/mvd/jester/simulator/internals/ProcessorContext.java index 1ca94fe..2778269 100644 --- a/src/main/java/mvd/jester/simulator/internals/ProcessorContext.java +++ b/src/main/java/mvd/jester/simulator/internals/ProcessorContext.java @@ -7,7 +7,7 @@ import java.util.Optional; */ public class ProcessorContext { - private Optional currentJob; + private Optional currentJob; private final long processorId; public ProcessorContext(long processorId) { @@ -15,18 +15,18 @@ public class ProcessorContext { this.processorId = processorId; } - public void setJob(JobContext job) { + public void setJob(JobContextInterface job) { this.currentJob = Optional.ofNullable(job); } /** * @return the currentJob */ - public Optional getJob() { + public Optional getJob() { return currentJob; } - public Optional updateExecution(long time) { + public Optional updateExecution(long time) { if (currentJob.isPresent()) { return currentJob.get().updateExecution(time); } @@ -35,10 +35,10 @@ public class ProcessorContext { } - public boolean acceptTask(TaskContext taskContext, int t) { + public boolean acceptTask(TaskContextInterface taskContext, int t) { if (!currentJob.isPresent() || currentJob.get().getTaskContext().getTask() .getPeriod() > taskContext.getTask().getPeriod()) { - Optional optionalJob = taskContext.getNextJob(); + Optional optionalJob = taskContext.getNextJob(); if (optionalJob.isPresent()) { if (currentJob.isPresent()) { @@ -46,7 +46,7 @@ public class ProcessorContext { } currentJob = optionalJob; currentJob.get().setCurrentProcessor(this); - // System.out.println("Time " + t + ": " + this + " started job " + currentJob + // System.out.println("Time " + t + ": " + this + " started job " + currentJob.get() // + " of task" + taskContext + "!"); return true; } else { diff --git a/src/main/java/mvd/jester/simulator/internals/SegmentContext.java b/src/main/java/mvd/jester/simulator/internals/SegmentContext.java deleted file mode 100644 index a1fdeab..0000000 --- a/src/main/java/mvd/jester/simulator/internals/SegmentContext.java +++ /dev/null @@ -1,49 +0,0 @@ -package mvd.jester.simulator.internals; - -import java.util.HashSet; -import java.util.Optional; -import java.util.Set; -import mvd.jester.model.Segment; - -/** - * Segment - */ -public class SegmentContext { - private final Segment segment; - private final Set jobs; - private final Set tasklets; - - public SegmentContext(TaskContext taskContext, Segment segment) { - this.segment = segment; - jobs = new HashSet<>(); - tasklets = new HashSet<>(); - - for (int j = 0; j < segment.getNumberOfJobs(); ++j) { - jobs.add(new JobContext(taskContext, this)); - } - - for (int t = 0; t < segment.getNumberOfTasklets(); ++t) { - tasklets.add(new TaskletContext(this)); - } - } - - - /** - * @return the segment - */ - public Segment getSegment() { - return segment; - } - - Optional getNextJob() { - return jobs.stream() - .filter(j -> !j.getCurrentProcessor().isPresent() && j.checkExecutionTime()) - .findFirst(); - } - - - @Override - public String toString() { - return "(nJobs=" + segment.getNumberOfJobs() + ", jobWcet=" + segment.getJobWcet() + ")"; - } -} diff --git a/src/main/java/mvd/jester/simulator/internals/SortedTaskContextSet.java b/src/main/java/mvd/jester/simulator/internals/SortedTaskContextSet.java index 0c3521a..8716e8f 100644 --- a/src/main/java/mvd/jester/simulator/internals/SortedTaskContextSet.java +++ b/src/main/java/mvd/jester/simulator/internals/SortedTaskContextSet.java @@ -6,7 +6,7 @@ import mvd.jester.priority.PriorityManager; /** * SortedTaskContextSet */ -public class SortedTaskContextSet extends TreeSet { +public class SortedTaskContextSet extends TreeSet { private static final long serialVersionUID = 4808544133562675597L; diff --git a/src/main/java/mvd/jester/simulator/internals/TaskContext.java b/src/main/java/mvd/jester/simulator/internals/TaskContext.java deleted file mode 100644 index b07ab4b..0000000 --- a/src/main/java/mvd/jester/simulator/internals/TaskContext.java +++ /dev/null @@ -1,79 +0,0 @@ -package mvd.jester.simulator.internals; - -import java.util.ArrayList; -import java.util.Optional; -import mvd.jester.model.Segment; -import mvd.jester.model.Task; - -/** - * TaskContext - */ -public class TaskContext { - - private final Task task; - private final ArrayList segments; - private final long deadline; - private int currentSegment; - private int segmentCounter; - - - public TaskContext(Task task, long timeStep) { - this.task = task; - this.segments = new ArrayList<>(); - this.currentSegment = 0; - this.segmentCounter = 0; - this.deadline = timeStep + task.getDeadline(); - - for (Segment s : task.getSegments()) { - segments.add(new SegmentContext(this, s)); - } - } - - /** - * @return the task - */ - public Task getTask() { - return task; - } - - /** - * @return the deadline - */ - public long getDeadline() { - return deadline; - } - - public Optional acceptNotification(long time) { - segmentCounter++; - - if (segmentCounter >= segments.get(currentSegment).getSegment().getNumberOfJobs()) { - currentSegment++; - segmentCounter = 0; - if (currentSegment >= segments.size()) { - // System.out.println("Time " + time + ": Task " + this + "finished!"); - return Optional.of(this); - } - } - - - return Optional.empty(); - } - - public Optional getNextJob() { - if (currentSegment < segments.size()) { - Optional optionalJob = segments.get(currentSegment).getNextJob(); - if (optionalJob.isPresent()) { - return optionalJob; - } - } - - return Optional.empty(); - } - - - @Override - public String toString() { - return "(period=" + task.getPeriod() + ", deadline=" + deadline + ", segments=" - + segments.size() + ")"; - } -} diff --git a/src/main/java/mvd/jester/simulator/internals/TaskContextInterface.java b/src/main/java/mvd/jester/simulator/internals/TaskContextInterface.java new file mode 100644 index 0000000..c08f761 --- /dev/null +++ b/src/main/java/mvd/jester/simulator/internals/TaskContextInterface.java @@ -0,0 +1,18 @@ +package mvd.jester.simulator.internals; + +import java.util.Optional; +import mvd.jester.model.Task; + +/** + * TaskContextInterface + */ +public interface TaskContextInterface { + + public Task getTask(); + + public Optional acceptNotification(); + + public Optional getNextJob(); + + public long getDeadline(); +} diff --git a/src/main/java/mvd/jester/simulator/internals/TaskletContext.java b/src/main/java/mvd/jester/simulator/internals/TaskletContext.java deleted file mode 100644 index 6d9feb6..0000000 --- a/src/main/java/mvd/jester/simulator/internals/TaskletContext.java +++ /dev/null @@ -1,19 +0,0 @@ -package mvd.jester.simulator.internals; - - -/** - * Tasklet - */ -public class TaskletContext { - - private final SegmentContext segment; - private final long wcet; - private long executionTime; - - - public TaskletContext(SegmentContext segment) { - this.segment = segment; - this.wcet = segment.getSegment().getTaskletWcet(); - this.executionTime = 0; - } -} diff --git a/src/main/java/mvd/jester/simulator/internals/maiabertogna/JobContext.java b/src/main/java/mvd/jester/simulator/internals/maiabertogna/JobContext.java new file mode 100644 index 0000000..8c6ff2c --- /dev/null +++ b/src/main/java/mvd/jester/simulator/internals/maiabertogna/JobContext.java @@ -0,0 +1,89 @@ +package mvd.jester.simulator.internals.maiabertogna; + +import java.util.Optional; +import mvd.jester.simulator.internals.JobContextInterface; +import mvd.jester.simulator.internals.ProcessorContext; +import mvd.jester.simulator.internals.TaskContextInterface; + +/** + * Job + * + * @param + */ +public class JobContext implements JobContextInterface { + + private final TaskContext taskContext; + private final SegmentContext segmentContext; + private final long wcet; + private Optional currentProcessor; + private long executionTime; + + + public JobContext(TaskContext taskContext, SegmentContext segmentContext) { + this.currentProcessor = Optional.empty(); + this.taskContext = taskContext; + this.segmentContext = segmentContext; + this.wcet = segmentContext.getSegment().getJobWcet(); + this.executionTime = wcet; + } + + public Optional updateExecution(long time) { + executionTime--; + + if (executionTime == 0) { + // System.out.println("Time " + time + ": " + currentProcessor.get() + // + " finished execution of job " + this + "!"); + currentProcessor.get().setJob(null); + currentProcessor = Optional.empty(); + return taskContext.acceptNotification(); + } + + return Optional.empty(); + } + + public boolean checkExecutionTime() { + return executionTime > 0; + } + + /** + * @return the wcet + */ + public long getWcet() { + return wcet; + } + + /** + * @param processor the currentProcessor to set + */ + public void setCurrentProcessor(ProcessorContext processor) { + this.currentProcessor = Optional.ofNullable(processor); + } + + /** + * @return the currentProcessor + */ + public Optional getCurrentProcessor() { + return currentProcessor; + } + + /** + * @return the segmentContext + */ + public SegmentContext getSegmentContext() { + return segmentContext; + } + + + /** + * @return the taskContext + */ + public TaskContext getTaskContext() { + return taskContext; + } + + @Override + public String toString() { + return "(jobWcet=" + wcet + ")"; + } + +} diff --git a/src/main/java/mvd/jester/simulator/internals/maiabertogna/SegmentContext.java b/src/main/java/mvd/jester/simulator/internals/maiabertogna/SegmentContext.java new file mode 100644 index 0000000..5109612 --- /dev/null +++ b/src/main/java/mvd/jester/simulator/internals/maiabertogna/SegmentContext.java @@ -0,0 +1,43 @@ +package mvd.jester.simulator.internals.maiabertogna; + +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; +import mvd.jester.model.Segment; +import mvd.jester.simulator.internals.JobContextInterface; + +/** + * Segment + */ +public class SegmentContext { + private final Segment segment; + private final Set jobs; + + public SegmentContext(TaskContext taskContext, Segment segment) { + this.segment = segment; + jobs = new HashSet<>(); + + for (int j = 0; j < segment.getNumberOfJobs(); ++j) { + jobs.add(new JobContext(taskContext, this)); + } + } + + + /** + * @return the segment + */ + public Segment getSegment() { + return segment; + } + + Optional getNextJob() { + return jobs.stream() + .filter(j -> !j.getCurrentProcessor().isPresent() && j.checkExecutionTime()) + .findFirst(); + } + + @Override + public String toString() { + return "(nJobs=" + segment.getNumberOfJobs() + ", jobWcet=" + segment.getJobWcet() + ")"; + } +} diff --git a/src/main/java/mvd/jester/simulator/internals/maiabertogna/TaskContext.java b/src/main/java/mvd/jester/simulator/internals/maiabertogna/TaskContext.java new file mode 100644 index 0000000..bf09ec9 --- /dev/null +++ b/src/main/java/mvd/jester/simulator/internals/maiabertogna/TaskContext.java @@ -0,0 +1,81 @@ +package mvd.jester.simulator.internals.maiabertogna; + +import java.util.ArrayList; +import java.util.Optional; +import mvd.jester.model.Segment; +import mvd.jester.model.Task; +import mvd.jester.simulator.internals.JobContextInterface; +import mvd.jester.simulator.internals.TaskContextInterface; + +/** + * TaskContext + */ +public class TaskContext implements TaskContextInterface { + + private final Task task; + private final ArrayList segments; + private final long deadline; + private int currentSegment; + private int segmentCounter; + + + public TaskContext(Task task, long timeStep) { + this.task = task; + this.segments = new ArrayList<>(); + this.currentSegment = 0; + this.segmentCounter = 0; + this.deadline = timeStep + task.getDeadline(); + + for (Segment s : task.getSegments()) { + segments.add(new SegmentContext(this, s)); + } + } + + /** + * @return the task + */ + public Task getTask() { + return task; + } + + /** + * @return the deadline + */ + public long getDeadline() { + return deadline; + } + + public Optional acceptNotification() { + segmentCounter++; + + if (segmentCounter >= segments.get(currentSegment).getSegment().getNumberOfJobs()) { + currentSegment++; + segmentCounter = 0; + if (currentSegment >= segments.size()) { + // System.out.println("Time " + time + ": Task " + this + "finished!"); + return Optional.of(this); + } + } + + + return Optional.empty(); + } + + public Optional getNextJob() { + if (currentSegment < segments.size()) { + Optional optionalJob = segments.get(currentSegment).getNextJob(); + if (optionalJob.isPresent()) { + return optionalJob; + } + } + + return Optional.empty(); + } + + + @Override + public String toString() { + return "(period=" + task.getPeriod() + ", deadline=" + deadline + ", segments=" + + segments.size() + ")"; + } +} diff --git a/src/main/java/mvd/jester/simulator/internals/schmidmottok/JobContext.java b/src/main/java/mvd/jester/simulator/internals/schmidmottok/JobContext.java new file mode 100644 index 0000000..7a4239d --- /dev/null +++ b/src/main/java/mvd/jester/simulator/internals/schmidmottok/JobContext.java @@ -0,0 +1,103 @@ +package mvd.jester.simulator.internals.schmidmottok; + +import java.util.Optional; +import mvd.jester.simulator.internals.JobContextInterface; +import mvd.jester.simulator.internals.ProcessorContext; +import mvd.jester.simulator.internals.TaskContextInterface; + +/** + * Job + * + * @param + */ +public class JobContext implements JobContextInterface { + + private final TaskContext taskContext; + private final SegmentContext segmentContext; + private final long wcet; + private Optional currentProcessor; + private Optional currentTasklet; + private long executionTime; + + + public JobContext(TaskContext taskContext, SegmentContext segmentContext) { + this.currentProcessor = Optional.empty(); + this.currentTasklet = Optional.empty(); + this.taskContext = taskContext; + this.segmentContext = segmentContext; + this.wcet = segmentContext.getSegment().getJobWcet(); + this.executionTime = wcet; + } + + public Optional updateExecution(long time) { + if (!currentTasklet.isPresent()) { + currentTasklet = segmentContext.getNextTasklet(); + // if (currentTasklet.isPresent()) { + // System.out.println("Time " + time + ": Job " + this + " started executing tasklet " + // + currentTasklet.get() + " on Processor " + currentProcessor.get()); + // } + } + + if (currentTasklet.isPresent()) { + currentTasklet.get().setCurrentJob(this); + return currentTasklet.get().updateExecution(); + } else { + currentProcessor.get().setJob(null); + currentProcessor = Optional.empty(); + return Optional.empty(); + } + } + + public boolean checkExecutionTime() { + return executionTime > 0; + } + + /** + * @return the wcet + */ + public long getWcet() { + return wcet; + } + + /** + * @param processor the currentProcessor to set + */ + public void setCurrentProcessor(ProcessorContext processor) { + this.currentProcessor = Optional.ofNullable(processor); + } + + /** + * @return the currentProcessor + */ + public Optional getCurrentProcessor() { + return currentProcessor; + } + + /** + * @param currentTasklet the currentTasklet to set + */ + public void setCurrentTasklet(TaskletContext currentTasklet) { + this.currentTasklet = Optional.ofNullable(currentTasklet); + } + + /** + * @return the segmentContext + */ + public SegmentContext getSegmentContext() { + return segmentContext; + } + + + /** + * @return the taskContext + */ + public TaskContext getTaskContext() { + return taskContext; + } + + @Override + public String toString() { + return "(of task=" + taskContext + ")"; + } + +} diff --git a/src/main/java/mvd/jester/simulator/internals/schmidmottok/SegmentContext.java b/src/main/java/mvd/jester/simulator/internals/schmidmottok/SegmentContext.java new file mode 100644 index 0000000..ea46968 --- /dev/null +++ b/src/main/java/mvd/jester/simulator/internals/schmidmottok/SegmentContext.java @@ -0,0 +1,57 @@ +package mvd.jester.simulator.internals.schmidmottok; + +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; +import mvd.jester.model.Segment; +import mvd.jester.simulator.internals.JobContextInterface; + +/** + * Segment + */ +public class SegmentContext { + private final Segment segment; + private final Set jobs; + private final Set tasklets; + + public SegmentContext(TaskContext taskContext, Segment segment) { + this.segment = segment; + jobs = new HashSet<>(); + tasklets = new HashSet<>(); + + for (int j = 0; j < segment.getNumberOfJobs(); ++j) { + jobs.add(new JobContext(taskContext, this)); + } + + for (int j = 0; j < segment.getNumberOfTasklets(); ++j) { + tasklets.add(new TaskletContext(taskContext, this)); + } + } + + + /** + * @return the segment + */ + public Segment getSegment() { + return segment; + } + + Optional getNextJob() { + return jobs.stream() + .filter(j -> !j.getCurrentProcessor().isPresent() && j.checkExecutionTime()) + .findFirst(); + } + + Optional getNextTasklet() { + return tasklets.stream() + .filter(t -> !t.getCurrentJob().isPresent() && t.checkExecutionTime()).findFirst(); + } + + + + @Override + public String toString() { + return "(nJobs=" + segment.getNumberOfJobs() + ", nTasklets=" + + segment.getNumberOfTasklets() + ", taskletWcet=" + segment.getTaskletWcet() + ")"; + } +} diff --git a/src/main/java/mvd/jester/simulator/internals/schmidmottok/TaskContext.java b/src/main/java/mvd/jester/simulator/internals/schmidmottok/TaskContext.java new file mode 100644 index 0000000..fe66331 --- /dev/null +++ b/src/main/java/mvd/jester/simulator/internals/schmidmottok/TaskContext.java @@ -0,0 +1,81 @@ +package mvd.jester.simulator.internals.schmidmottok; + +import java.util.ArrayList; +import java.util.Optional; +import mvd.jester.model.Segment; +import mvd.jester.model.Task; +import mvd.jester.simulator.internals.JobContextInterface; +import mvd.jester.simulator.internals.TaskContextInterface; + +/** + * TaskContext + */ +public class TaskContext implements TaskContextInterface { + + private final Task task; + private final ArrayList segments; + private final long deadline; + private int currentSegment; + private int segmentCounter; + + + public TaskContext(Task task, long timeStep) { + this.task = task; + this.segments = new ArrayList<>(); + this.currentSegment = 0; + this.segmentCounter = 0; + this.deadline = timeStep + task.getDeadline(); + + for (Segment s : task.getSegments()) { + segments.add(new SegmentContext(this, s)); + } + } + + /** + * @return the task + */ + public Task getTask() { + return task; + } + + /** + * @return the deadline + */ + public long getDeadline() { + return deadline; + } + + public Optional acceptNotification() { + segmentCounter++; + + if (segmentCounter >= segments.get(currentSegment).getSegment().getNumberOfTasklets()) { + currentSegment++; + segmentCounter = 0; + if (currentSegment >= segments.size()) { + // System.out.println("Time " + time + ": Task " + this + "finished!"); + return Optional.of(this); + } + } + + + return Optional.empty(); + } + + public Optional getNextJob() { + if (currentSegment < segments.size()) { + Optional optionalJob = segments.get(currentSegment).getNextJob(); + if (optionalJob.isPresent()) { + return optionalJob; + } + } + + return Optional.empty(); + } + + + @Override + public String toString() { + return "(period=" + task.getPeriod() + ", deadline=" + deadline + ", segments=" + + segments.size() + ")"; + } +} diff --git a/src/main/java/mvd/jester/simulator/internals/schmidmottok/TaskletContext.java b/src/main/java/mvd/jester/simulator/internals/schmidmottok/TaskletContext.java new file mode 100644 index 0000000..8662571 --- /dev/null +++ b/src/main/java/mvd/jester/simulator/internals/schmidmottok/TaskletContext.java @@ -0,0 +1,55 @@ +package mvd.jester.simulator.internals.schmidmottok; + +import java.util.Optional; +import mvd.jester.simulator.internals.TaskContextInterface; + +/** + * Tasklet + */ +public class TaskletContext { + + private final TaskContext taskContext; + private Optional currentJob; + private final long wcet; + private long executionTime; + + + public TaskletContext(TaskContext taskContext, SegmentContext segment) { + this.taskContext = taskContext; + this.wcet = segment.getSegment().getTaskletWcet(); + this.executionTime = wcet; + currentJob = Optional.empty(); + } + + + /** + * @return the currentJob + */ + public Optional getCurrentJob() { + return currentJob; + } + + public void setCurrentJob(JobContext jobContext) { + currentJob = Optional.ofNullable(jobContext); + } + + public Optional updateExecution() { + executionTime--; + + if (executionTime == 0) { + currentJob.get().setCurrentTasklet(null); + currentJob = Optional.empty(); + return taskContext.acceptNotification(); + } + return Optional.empty(); + } + + public boolean checkExecutionTime() { + return executionTime > 0; + } + + @Override + public String toString() { + return "(wcet=" + wcet + ", task=" + taskContext + ")"; + } +} diff --git a/src/main/java/mvd/jester/tests/TestEnvironment.java b/src/main/java/mvd/jester/tests/TestEnvironment.java deleted file mode 100644 index a390c97..0000000 --- a/src/main/java/mvd/jester/tests/TestEnvironment.java +++ /dev/null @@ -1,110 +0,0 @@ -package mvd.jester.tests; - -import java.lang.reflect.Constructor; -import java.time.LocalTime; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import mvd.jester.model.SystemSetup; -import mvd.jester.utils.Logger; - -/** - * TestEnvironment - */ -public class TestEnvironment { - - private final long numberOfTaskSets; - private final SystemSetup systemSetup; - private final SystemSetup.Builder builder; - private final Set> abstractTests; - - public TestEnvironment(SystemSetup.Builder builder, long numberOfTaskSets) { - this.numberOfTaskSets = numberOfTaskSets; - abstractTests = new HashSet<>(); - - this.builder = builder; - this.systemSetup = builder.build(); - } - - public TestEnvironment registerTestInterface(Class abstractTest) { - try { - abstractTests.add(abstractTest.getConstructor(SystemSetup.class)); - } catch (Exception e) { - System.out.println("Missing constructor!"); - } - - return this; - } - - public void runTests() { - Set testCases = new HashSet<>(); - - Map> results = new HashMap<>(); - - for (Constructor t : abstractTests) { - try { - testCases.add(t.newInstance(this.systemSetup)); - } catch (Exception e) { - System.out.println("Could not instantiate object of AbstractTest!"); - } - } - - long checkedTasksets = 0; - - while (checkedTasksets < numberOfTaskSets) { - builder.rebuild(this.systemSetup); - - double utilization = this.systemSetup.getUtilization(); - - while (utilization <= this.systemSetup.getNumberOfProcessors() - && checkedTasksets < numberOfTaskSets) { - checkedTasksets++; - - long roundedUtilization = (long) (utilization * 10); - for (AbstractTest t : testCases) { - if (t.runSchedulabilityCheck()) { - results.computeIfAbsent(roundedUtilization, - k -> new HashMap()) - .compute(t, (k, v) -> (v == null) ? 1 : v + 1); - } - } - builder.addTask(systemSetup); - - utilization = this.systemSetup.getUtilization(); - } - } - - logResults(testCases, results); - } - - private void logResults(Set testCases, - Map> results) { - LocalTime date = LocalTime.now(); - Logger log = new Logger("./results/results_" + systemSetup.getNumberOfProcessors() + "_" - + date.getHour() + ":" + date.getMinute() + ".txt"); - String firstLine = new String("Utilization"); - - for (AbstractTest t : testCases) { - firstLine = firstLine + "\t" + t.getName(); - } - - log.log(firstLine); - - for (Long util : results.keySet()) { - String line = String.valueOf((double) util / 10); - Map tests = results.get(util); - - for (AbstractTest t : testCases) { - if (tests.containsKey(t)) { - line += "\t" + tests.get(t); - } else { - line += "\t" + "0"; - } - } - log.log(line); - } - - log.finalize(); - } -} -- libgit2 0.26.0