DagTask.java 5.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
package mvd.jester.model;

import java.util.LinkedHashSet;
import java.util.Set;
import org.jgrapht.experimental.dag.DirectedAcyclicGraph;
import org.jgrapht.graph.DefaultEdge;

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;
        }

        public static void createNFJGraph(DirectedAcyclicGraph<Job, DefaultEdge> jobDag) {
            Set<Job> joinNodes = new LinkedHashSet<>();
            Set<Job> forkNodes = new LinkedHashSet<>();

            for (Job j : jobDag) {
                if (jobDag.inDegreeOf(j) > 1) {
                    joinNodes.add(j);
                }
                if (jobDag.outDegreeOf(j) > 1) {
                    forkNodes.add(j);
                }
            }

            for (Job j : joinNodes) {
                for (Job f : forkNodes) {
                    Set<DefaultEdge> edges = jobDag.getAllEdges(f, j);
                    // jobDag.
                }
            }
        }

    }
}