FonsecaNelis.java 7.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
package mvd.jester.tests;

import java.math.RoundingMode;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.Lists;
import com.google.common.math.LongMath;
Michael Schmid committed
12 13
import org.jgrapht.experimental.dag.DirectedAcyclicGraph;
import org.jgrapht.graph.DefaultEdge;
14 15 16 17
import mvd.jester.info.SchedulingInfo;
import mvd.jester.info.TerminationInfo;
import mvd.jester.info.TerminationInfo.Level;
import mvd.jester.model.DagTask;
Michael Schmid committed
18
import mvd.jester.model.Job;
19 20 21
import mvd.jester.model.Segment;
import mvd.jester.model.SortedTaskSet;
import mvd.jester.model.Task;
Michael Schmid committed
22
import mvd.jester.model.DagTask.DagUtils;
23 24 25 26 27 28 29
import mvd.jester.priority.PriorityManager;
import mvd.jester.priority.RateMonotonic;

public class FonsecaNelis extends AbstractTest<DagTask> {

    private final Map<Task, TerminationInfo> responseTimes;
    private final PriorityManager priorityManager;
Michael Schmid committed
30
    private final Map<Task, Set<Segment>> sortedSegments;
31 32 33 34 35

    public FonsecaNelis(long numberOfProcessors) {
        super(numberOfProcessors);
        this.responseTimes = new HashMap<>();
        this.priorityManager = new RateMonotonic();
Michael Schmid committed
36
        this.sortedSegments = new HashMap<>();
37 38 39 40 41 42 43 44 45
    }

    @Override
    public PriorityManager getPriorityManager() {
        return priorityManager;
    }

    @Override
    public SchedulingInfo runSchedulabilityCheck(SortedTaskSet<DagTask> tasks) {
Michael Schmid committed
46
        sortedSegments.clear();
47
        responseTimes.clear();
Michael Schmid committed
48
        createNFJandDecompositionTree(tasks);
49 50 51 52 53 54 55 56 57 58
        for (DagTask t : tasks) {
            long responseTime = calculateResponseTime(tasks, t);
            responseTimes.put(t, new TerminationInfo(t.getDeadline(), responseTime, Level.HIGH));
        }

        return new SchedulingInfo(new HashSet<>(responseTimes.values()),
                tasks.getParallelTaskRatio(), tasks.getUtilization());
    }

    private void createNFJandDecompositionTree(SortedTaskSet<DagTask> tasks) {
Michael Schmid committed
59 60 61 62 63 64 65 66 67 68
        for (DagTask t : tasks) {
            DirectedAcyclicGraph<Job, DefaultEdge> modifiedJobDag =
                    DagUtils.createNFJGraph(t.getJobDag());
            // List<Segment> sortedSegment = new LinkedList<>(t.getWorkloadDistribution());
            // Collections.sort(sortedSegment,
            // (s1, s2) -> (int) (s2.getNumberOfJobs() - s1.getNumberOfJobs()));

            // Set<Segment> sortedSet = new LinkedHashSet<>(sortedSegment);
            // sortedSegments.put(t, sortedSet);
        }
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    }

    private long calculateResponseTime(SortedTaskSet<DagTask> tasks, DagTask task) {
        long criticalPath = task.getCriticalPath();
        long responseTime = criticalPath;
        long previousResponseTime = 0;

        do {
            previousResponseTime = responseTime;
            double taskInterference = 0;
            for (DagTask t : tasks) {
                if (t.getPeriod() < task.getPeriod()) {
                    taskInterference += getTaskInterference(t, responseTime);
                }
            }

            taskInterference /= numberOfProcessors;

            double selfInterference = getSelfInterference(task);

            long totalInterference = (long) Math.floor(taskInterference + selfInterference);
            responseTime = criticalPath + totalInterference;

        } while (responseTime != previousResponseTime);

        return responseTime;
    }

    private double getTaskInterference(DagTask task, long interval) {
        long period = task.getPeriod();
        long criticalPath = task.getCriticalPath();
        long numberOfIntervals =
                LongMath.divide(interval - criticalPath, period, RoundingMode.FLOOR);
        long carryInAndOutInterval = interval - Math.max(0, numberOfIntervals) * period;
        long numberOfBodyJobs =
                LongMath.divide(interval - carryInAndOutInterval, period, RoundingMode.FLOOR);

        long bodyWorkload = Math.max(0, numberOfBodyJobs) * task.getWorkload();


        long carryInAndOutWorkload = getCarryInAndOutWorkload(task, task.getWorkloadDistribution(),
Michael Schmid committed
110
                sortedSegments.get(task), carryInAndOutInterval);
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

        return carryInAndOutWorkload + bodyWorkload;
    }

    private long getCarryInAndOutWorkload(DagTask task, Set<Segment> carryInDistribution,
            Set<Segment> carryOutDistribution, long carryInAndOutPeriod) {
        long workload = getCarryOutWorkload(task, carryOutDistribution, carryInAndOutPeriod);
        long carryInPeriod = task.getPeriod() - responseTimes.get(task).getResponseTime();
        long carryOutPeriod = 0;
        List<Segment> carryInList = Lists.newArrayList(carryInDistribution);
        Collections.reverse(carryInList);

        for (Segment s : carryInList) {
            carryInPeriod += s.getJobWcet();
            carryOutPeriod = carryInAndOutPeriod - carryInPeriod;
            long carryInWorkload = getCarryInWorkload(task, carryInDistribution, carryInPeriod);
            long carryOutWorkload = getCarryOutWorkload(task, carryOutDistribution, carryOutPeriod);
            workload = Math.max(workload, carryInWorkload + carryOutWorkload);
        }

        workload = Math.max(workload,
                getCarryInWorkload(task, carryInDistribution, carryInAndOutPeriod));
        carryOutPeriod = 0;

        for (Segment s : carryOutDistribution) {
            carryOutPeriod += s.getJobWcet();
            carryInPeriod = carryInAndOutPeriod - carryOutPeriod;
            long carryInWorkload = getCarryInWorkload(task, carryInDistribution, carryInPeriod);
            long carryOutWorkload = getCarryOutWorkload(task, carryOutDistribution, carryOutPeriod);
            workload = Math.max(workload, carryInWorkload + carryOutWorkload);
        }

        return workload;
    }

    private long getCarryOutWorkload(DagTask task, Set<Segment> carryOutDistribution,
            long carryOutPeriod) {
        long workload = 0;
        long period = task.getPeriod();
        long responseTime = responseTimes.get(task).getResponseTime();
        List<Segment> distributionList = Lists.newArrayList(carryOutDistribution);

        for (int i = 0; i < distributionList.size(); ++i) {
            Segment s = distributionList.get(i);
            long weightOfPreviousSegments = 0;
            for (int j = 0; j < i; ++j) {
                weightOfPreviousSegments += distributionList.get(j).getJobWcet();
            }
            long width = carryOutPeriod - weightOfPreviousSegments;

            workload += Math.max(Math.min(width, s.getJobWcet()), 0) * s.getNumberOfJobs();
        }

        long improvedWorkload =
                Math.max(carryOutPeriod - (period - responseTime), 0) * numberOfProcessors;

        return Math.min(improvedWorkload, workload);
    }

    private long getCarryInWorkload(DagTask task, Set<Segment> carryInDistribution,
            long carryInPeriod) {
        long workload = 0;
        long period = task.getPeriod();
        long responseTime = responseTimes.get(task).getResponseTime();
        List<Segment> distributionList = Lists.newArrayList(carryInDistribution);

        for (int i = 0; i < carryInDistribution.size(); ++i) {
            Segment s = distributionList.get(i);
            long weightOfRemainingSegments = 0;
            for (int j = i + 1; j < carryInDistribution.size(); ++j) {
                weightOfRemainingSegments += distributionList.get(j).getJobWcet();
            }
            long width = carryInPeriod - period + responseTime - weightOfRemainingSegments;

            workload += Math.max(Math.min(width, s.getJobWcet()), 0) * s.getNumberOfJobs();
        }

        long improvedWorkload =
                Math.max(carryInPeriod - (period - responseTime), 0) * numberOfProcessors;

        return Math.min(improvedWorkload, workload);
    }

    private double getSelfInterference(DagTask task) {
        long criticalPath = task.getCriticalPath();
        long workload = task.getWorkload();

        return (double) (workload - criticalPath) / numberOfProcessors;
    }

    @Override
    public String getName() {
        return "FonsecaNelis";
    }


}