AbstractSimulator.java 4.28 KB
Newer Older
1 2
package mvd.jester.simulator;

3
import java.util.Comparator;
4 5
import java.util.HashSet;
import java.util.Set;
6
import java.util.TreeSet;
7
import com.google.common.collect.TreeMultiset;
Michael Schmid committed
8
import mvd.jester.model.SystemManager;
9
import mvd.jester.priority.PriorityManager;
10
import mvd.jester.priority.RateMonotonic;
11
import mvd.jester.TypeInterface;
12
import mvd.jester.info.SchedulingInfo;
13
import mvd.jester.simulator.internals.ProcessorContext;
14
import mvd.jester.simulator.internals.TaskContextInterface;
15 16 17 18 19


/**
 * AbstractSimulator
 */
20
public abstract class AbstractSimulator implements SimulatorInterface, TypeInterface {
21

Michael Schmid committed
22
    protected final SystemManager systemSetup;
23
    protected final Set<ProcessorContext> processors;
24
    protected TreeMultiset<TaskContextInterface> readyTasks;
25

Michael Schmid committed
26
    AbstractSimulator(SystemManager systemSetup) {
27
        this.systemSetup = systemSetup;
28
        this.readyTasks = TreeMultiset.create((t1, t2) -> new RateMonotonic().compare(t1, t2));
29 30 31 32 33 34
        processors = new HashSet<>();
        for (int i = 0; i < systemSetup.getNumberOfProcessors(); ++i) {
            processors.add(new ProcessorContext(i));
        }
    }

35 36 37 38

    protected abstract boolean releaseTasks(long timeStep);

    @Override
39
    public SchedulingInfo runSimulation(PriorityManager priorityManager) {
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        // SchedulingInfo schedulingInfo = new SchedulingInfo(systemSetup.getParallelTaskRatio(),
        // systemSetup.getUtilization());
        // long hyperPeriod = init(priorityManager);
        // for (int t = 0; t < hyperPeriod; ++t) {
        // if (!releaseTasks(t)) {
        // throw new RuntimeException("Could not release a task. This should not happen!");
        // }

        // Set<ProcessorContext> sortedProcessors = sortProcessors(processors);

        // for (ProcessorContext p : sortedProcessors) {
        // for (TaskContextInterface tc : readyTasks) {
        // if (p.acceptTask(tc, t)) {
        // break;
        // }
        // }

        // }

        // for (ProcessorContext p : processors) {
        // Optional<TaskContextInterface> optionalTc = p.updateExecution(t);
        // if (optionalTc.isPresent()) {
        // TaskContextInterface tc = optionalTc.get();
        // if (t >= tc.getDeadline()) {
        // TerminationInfo terminationInfo =
        // new TerminationInfo(tc.getReleaseTime(), tc.getDeadline(), t);
        // schedulingInfo.addTerminationInfo(terminationInfo);


        // EventPrinter.print("Time " + t + ": Task " + tc + " failed its deadline!");
        // schedulingInfo.setFailedTerminationInfo(terminationInfo);
        // return schedulingInfo;
        // }

        // readyTasks.remove(optionalTc.get());
        // }
        // }
        // }

        // return schedulingInfo;
        return null;
81 82
    }

83
    private long init(PriorityManager priorityManager) {
84
        this.readyTasks = TreeMultiset.create((t1, t2) -> priorityManager.compare(t1, t2));
85 86 87
        for (ProcessorContext p : processors) {
            p.setJob(null);
        }
88
        return getHyperPeriod();
89
    }
90

91 92
    private Set<ProcessorContext> sortProcessors(Set<ProcessorContext> processors) {
        Set<ProcessorContext> sortedProcessors = new TreeSet<>(new ProcessorComparator());
93

94 95 96 97 98
        processors.forEach(p -> sortedProcessors.add(p));

        return sortedProcessors;
    }

99
    private long getHyperPeriod() {
Michael Schmid committed
100 101 102 103
        // return
        // systemSetup.getTasks().stream().max(Comparator.comparing(SynchronousTask::getPeriod))
        // .get().getPeriod() * 10;
        return 10;
104 105
    }

Michael Schmid committed
106 107


108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    private class ProcessorComparator implements Comparator<ProcessorContext> {

        @Override
        public int compare(ProcessorContext p1, ProcessorContext p2) {
            if (!p1.getJob().isPresent()) {
                return -1;
            } else if (!p2.getJob().isPresent()) {
                return 1;
            } else {
                long p1Period = p1.getJob().get().getTaskContext().getTask().getPeriod();
                long p2Period = p2.getJob().get().getTaskContext().getTask().getPeriod();
                if (p1Period == p2Period) {
                    return 1;
                } else {
                    return (int) (p2.getJob().get().getTaskContext().getTask().getPeriod()
                            - p1.getJob().get().getTaskContext().getTask().getPeriod());
                }
            }
        }

    }
129
}