SystemSetup.java 5.88 KB
Newer Older
1 2
package mvd.jester.model;

3 4 5 6
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
7
import java.util.HashSet;
8 9 10
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
11 12
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
13 14 15 16 17

/**
 * TaskSet
 */
public class SystemSetup {
18
    private Set<Task> tasks;
19 20
    private final long numberOfProcessors;

21
    public SystemSetup(Set<Task> tasks, long numberOfProcessors) {
22 23 24 25 26 27 28
        this.tasks = tasks;
        this.numberOfProcessors = numberOfProcessors;
    }

    /**
     * @return the tasks
     */
29
    public Set<Task> getTasks() {
30 31 32
        return tasks;
    }

33
    public void setTasks(Set<Task> tasks) {
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        this.tasks = tasks;
    }

    /**
     * @return the numberOfProcessors
     */
    public long getNumberOfProcessors() {
        return numberOfProcessors;
    }

    public double getUtilization() {
        double utilization = 0;
        for (Task t : tasks) {
            utilization += (double) t.getMaximumWcet() / t.getPeriod();
        }

        return utilization;
    }

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
    @Override
    public String toString() {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        return gson.toJson(tasks);
    }

    public static SystemSetup fromFile(String path) {
        String jsonString;
        try {
            byte[] encoded = Files.readAllBytes(Paths.get(path));
            jsonString = new String(encoded, Charset.defaultCharset());
        } catch (IOException e) {
            System.out.println(e.getMessage());
            jsonString = new String("");
        }
        return SystemSetup.fromString(jsonString);
    }

    public static SystemSetup fromString(String json) {
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(SortedTaskSet.class, new SortedTaskSet.Deserializer())
                .create();

        SortedTaskSet tasks = gson.fromJson(json, SortedTaskSet.class);
        return new SystemSetup(tasks, 8);
    }

80
    public static class Builder {
81 82 83 84 85 86 87 88 89 90
        private long numberOfProcessors = 4;
        private long minPeriod = 100;
        private long maxPeriod = 1000;
        private long minNumberOfSegments = 3;
        private long maxNumberOfSegments = 7;
        private long minNumberOfJobs = 2;
        private long maxNumberOfJobs = 3 * numberOfProcessors / 2;
        private long minWcet = 1;
        private long ratio = randomTaskRatio();

91
        public Builder() {
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

        }

        private long randomTaskPeriod() {
            return ThreadLocalRandom.current().nextLong(minPeriod, maxPeriod);
        }

        private long randomTaskRatio() {
            return ThreadLocalRandom.current().nextLong(0, 100);
        }

        private long randomNumberOfSegments() {
            return ThreadLocalRandom.current().nextLong(minNumberOfSegments, maxNumberOfSegments);
        }

        private long randomNumberOfJobs() {
108
            this.maxNumberOfJobs = 3 * this.numberOfProcessors / 2;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
            return ThreadLocalRandom.current().nextLong(minNumberOfJobs, maxNumberOfJobs);
        }

        private long randomWcet(long period, long numberOfSegments) {
            long maxWcet = period / numberOfSegments;

            return ThreadLocalRandom.current().nextLong(minWcet, maxWcet);
        }

        private Task generateTask() {
            boolean serial = randomTaskRatio() > this.ratio;
            long period = randomTaskPeriod();
            long numberOfSegments = 1;
            Set<Segment> segments = new LinkedHashSet<Segment>();
            if (!serial) {
                numberOfSegments = randomNumberOfSegments();
            } else {
                numberOfSegments = 1;
            }
128
            long parallelNumberOfJobs = serial ? 1 : randomNumberOfJobs();
129
            for (int i = 0; i < numberOfSegments; ++i) {
130 131 132 133
                long numberOfJobs = 1;
                if (i % 2 == 1) {
                    numberOfJobs = parallelNumberOfJobs;
                }
134 135 136 137 138 139
                long wcet = randomWcet(period, numberOfSegments);
                segments.add(new Segment(wcet, numberOfJobs));
            }
            return new Task(period, segments);
        }

140 141 142
        private Set<Task> generateTaskSet() {
            // SortedTaskSet taskSet = new SortedTaskSet(priorityManager);
            Set<Task> taskSet = new HashSet<>();
143 144 145 146 147 148 149 150 151 152 153

            for (int i = 0; i < 4; ++i) {
                Task task = generateTask();
                taskSet.add(task);
            }

            return taskSet;
        }

        public SystemSetup build() {
            this.ratio = randomTaskRatio();
154
            Set<Task> taskSet = generateTaskSet();
155 156 157
            return new SystemSetup(taskSet, numberOfProcessors);
        }

158 159 160
        public void rebuild(SystemSetup systemSetup) {
            this.ratio = randomTaskRatio();
            systemSetup.tasks = generateTaskSet();
161 162
        }

163 164
        public void addTask(SystemSetup systemSetup) {
            systemSetup.tasks.add(generateTask());
165 166
        }

167 168 169 170 171
        public Builder setNumberOfProcessors(long numberOfProcessors) {
            this.numberOfProcessors = numberOfProcessors;

            return this;
        }
172

173
        public Builder setNumberOfSegments(long minNumberOfSegments, long maxNumberOfSegments) {
174 175 176 177 178 179
            this.minNumberOfSegments = minNumberOfSegments;
            this.maxNumberOfSegments = maxNumberOfSegments;

            return this;
        }

180
        public Builder setPeriods(long minPeriod, long maxPeriod) {
181 182 183 184 185 186 187 188 189
            this.minPeriod = minPeriod;
            this.maxPeriod = maxPeriod;

            return this;
        }

        /**
         * @param maxNumberOfJobs the maxNumberOfJobs to set
         */
190
        public Builder setNumberOfJobs(long minNumberOfJobs, long maxNumberOfJobs) {
191 192 193 194 195 196 197
            this.minNumberOfJobs = minNumberOfJobs;
            this.maxNumberOfJobs = maxNumberOfJobs;
            return this;
        }

    }
}