DagTask.java 9.69 KB
Newer Older
1 2
package mvd.jester.model;

Michael Schmid committed
3
import java.util.HashSet;
4
import java.util.LinkedHashSet;
Michael Schmid committed
5 6
import java.util.LinkedList;
import java.util.List;
7
import java.util.Set;
Michael Schmid committed
8
import org.jgrapht.Graphs;
9 10
import org.jgrapht.experimental.dag.DirectedAcyclicGraph;
import org.jgrapht.graph.DefaultEdge;
Michael Schmid committed
11
import org.jgrapht.traverse.BreadthFirstIterator;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 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 110 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

public class DagTask implements Task {

    private DirectedAcyclicGraph<Job, DefaultEdge> jobDag;
    private final Set<Segment> workloadDistribution;
    private final long workload;
    private final long criticalPath;
    private final long period;
    private final long deadline;
    private final long numberOfThreads;

    public DagTask(DirectedAcyclicGraph<Job, DefaultEdge> jobDag, long period,
            long numberOfThreads) {
        this.jobDag = jobDag;
        this.period = period;
        this.deadline = period;
        this.numberOfThreads = numberOfThreads;
        this.workload = DagUtils.calculateWorkload(this.jobDag);
        this.criticalPath = DagUtils.calculateCriticalPath(this.jobDag);
        this.workloadDistribution =
                DagUtils.calculateWorkloadDistribution(this.jobDag, this.criticalPath);
    }

    public double getUtilization() {
        return (double) workload / period;
    }

    /**
     * @return the deadline
     */
    public long getDeadline() {
        return deadline;
    }

    /**
     * @return the jobDag
     */
    public DirectedAcyclicGraph<Job, DefaultEdge> getJobDag() {
        return jobDag;
    }

    /**
     * @param jobDag the jobDag to set
     */
    public void setJobDag(DirectedAcyclicGraph<Job, DefaultEdge> jobDag) {
        this.jobDag = jobDag;
    }

    /**
     * @return the period
     */
    public long getPeriod() {
        return period;
    }

    /**
     * @return the workload
     */
    public long getWorkload() {
        return workload;
    }

    /**
     * @return the criticalPath
     */
    public long getCriticalPath() {
        return criticalPath;
    }


    /**
     * @return the workloadDistribution
     */
    public Set<Segment> getWorkloadDistribution() {
        return workloadDistribution;
    }

    @Override
    public long getMaximumParallelism() {
        long max = 0;
        for (Segment s : workloadDistribution) {
            if (max < s.getNumberOfJobs()) {
                max = s.getNumberOfJobs();
            }
        }
        return max;
    }


    @Override
    public long getNumberOfThreads() {
        return numberOfThreads;
    }

    public static class DagUtils {
        public static long calculateWorkload(DirectedAcyclicGraph<Job, DefaultEdge> jobDag) {
            long workload = 0;

            for (Job job : jobDag) {
                workload += job.getWcet();
            }
            return workload;
        }

        public static long calculateCriticalPath(DirectedAcyclicGraph<Job, DefaultEdge> jobDag) {
            long criticalPath = 0;
            for (Job job : jobDag) {
                Set<DefaultEdge> edges = jobDag.incomingEdgesOf(job);
                long longestRelativeCompletionTime = 0;
                for (DefaultEdge e : edges) {
                    Job source = jobDag.getEdgeSource(e);
                    longestRelativeCompletionTime =
                            longestRelativeCompletionTime >= source.getRelativeCompletionTime()
                                    ? longestRelativeCompletionTime
                                    : source.getRelativeCompletionTime();
                }

                job.setRelativeCompletionTime(longestRelativeCompletionTime + job.getWcet());
                criticalPath = job.getRelativeCompletionTime();
            }

            return criticalPath;
        }

        public static LinkedHashSet<Segment> calculateWorkloadDistribution(
                DirectedAcyclicGraph<Job, DefaultEdge> jobDag, long criticalPath) {
            LinkedHashSet<Segment> segments = new LinkedHashSet<>();
            long segmentDuration = 0;
            long segmentHeight = 1;
            for (long t = 0; t < criticalPath; ++t) {
                long currentHeight = 0;
                for (Job j : jobDag) {
                    if (t >= j.getRelativeCompletionTime() - j.getWcet()
                            && t < j.getRelativeCompletionTime()) {
                        currentHeight++;
                    }
                }
                if (currentHeight == segmentHeight) {
                    segmentDuration++;
                } else {
                    segments.add(new Segment(segmentDuration, segmentHeight));
                    segmentDuration = 1;
                    segmentHeight = currentHeight;
                }
            }
            segments.add(new Segment(segmentDuration, segmentHeight));

            return segments;
        }

Michael Schmid committed
162 163 164 165 166 167 168
        public static DirectedAcyclicGraph<Job, DefaultEdge> createNFJGraph(
                DirectedAcyclicGraph<Job, DefaultEdge> jobDag) {
            DirectedAcyclicGraph<Job, DefaultEdge> modifiedJobDag =
                    new DirectedAcyclicGraph<>(DefaultEdge.class);
            Graphs.addGraph(modifiedJobDag, jobDag);
            LinkedList<Job> joinNodes = new LinkedList<>();
            List<Job> forkNodes = new LinkedList<>();
169

Michael Schmid committed
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
            BreadthFirstIterator<Job, DefaultEdge> breadthFirstIterator =
                    new BreadthFirstIterator<>(modifiedJobDag);
            while (breadthFirstIterator.hasNext()) {
                Job j = breadthFirstIterator.next();
                if (modifiedJobDag.inDegreeOf(j) > 1) {
                    joinNodes.add(j);
                }
                if (modifiedJobDag.outDegreeOf(j) > 1) {
                    forkNodes.add(j);
                }
            }

            Job sink = joinNodes.getLast();

            for (Job j : joinNodes) {
                Set<DefaultEdge> edgeSet = new HashSet<>(modifiedJobDag.incomingEdgesOf(j));
                for (DefaultEdge e : edgeSet) {
                    Job predecessor = modifiedJobDag.getEdgeSource(e);
                    boolean satisfiesProposition =
                            DagUtils.checkForFork(modifiedJobDag, j, forkNodes, predecessor);
                    if (!satisfiesProposition) {
                        modifiedJobDag.removeEdge(e);
                        if (modifiedJobDag.outgoingEdgesOf(predecessor).isEmpty()) {
                            try {
                                modifiedJobDag.addDagEdge(predecessor, sink);
                            } catch (Exception ex) {
                            }
                        }
                    }

                    if (modifiedJobDag.inDegreeOf(j) == 1) {
                        break;
                    }
                }
                // Find fork node f following the path along this edge e
                // if f has successor that is not ancestor of j -> e is conflicting edge
                // get sorcetarget of e
                // remove e
                // if sourcetarget has no successor -> connect sourcetraget to sink
                // if indegree = 1 -> break;
            }

            // if (!DagUtils.checkProperty1(modifiedJobDag)) {
            // throw new RuntimeException("abs");
            // }
            return modifiedJobDag;
        }

        private static boolean checkProperty1(DirectedAcyclicGraph<Job, DefaultEdge> jobDag) {
            LinkedList<Job> joinNodes = new LinkedList<>();
            List<Job> forkNodes = new LinkedList<>();

            BreadthFirstIterator<Job, DefaultEdge> breadthFirstIterator =
                    new BreadthFirstIterator<>(jobDag);
            while (breadthFirstIterator.hasNext()) {
                Job j = breadthFirstIterator.next();
226 227 228 229 230 231 232 233 234
                if (jobDag.inDegreeOf(j) > 1) {
                    joinNodes.add(j);
                }
                if (jobDag.outDegreeOf(j) > 1) {
                    forkNodes.add(j);
                }
            }

            for (Job j : joinNodes) {
Michael Schmid committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
                nextFork: for (Job f : forkNodes) {
                    Set<DefaultEdge> edgeSet = jobDag.getAllEdges(f, j);

                    for (DefaultEdge e : edgeSet) {
                        Job a = jobDag.getEdgeSource(e);
                        if (a != f) {
                            Set<Job> succAndPred = new HashSet<>();
                            succAndPred.addAll(Graphs.predecessorListOf(jobDag, a));
                            succAndPred.addAll(Graphs.successorListOf(jobDag, a));

                            for (Job b : succAndPred) {
                                if (!((jobDag.getAncestors(jobDag, j).contains(b) || b == j)
                                        && (jobDag.getDescendants(jobDag, f).contains(b)
                                                || b == f))) {
                                    continue nextFork;
                                }
                            }
                        }
                    }
                    return true;
                }
            }
            return false;
        }

        private static boolean checkForFork(DirectedAcyclicGraph<Job, DefaultEdge> jobDag,
                Job joinNode, List<Job> forkNodes, Job job) {
            List<Job> pred = Graphs.predecessorListOf(jobDag, job);

            for (Job p : pred) {
                if (forkNodes.contains(p)) {
                    for (DefaultEdge successorEdge : jobDag.outgoingEdgesOf(p)) {
                        Job successor = jobDag.getEdgeSource(successorEdge);
                        if (jobDag.getAncestors(jobDag, joinNode).contains(successor)) {
                            return false;
                        }
                    }
                } else {
                    return checkForFork(jobDag, joinNode, forkNodes, p);
274 275
                }
            }
Michael Schmid committed
276 277

            return true;
278 279 280 281
        }

    }
}