Job.py 9.47 KB
Newer Older
1 2
# coding=utf-8

3 4
# from SimPy.Simulation import Process, hold, passivate
from simpy import Process, Interrupt
5 6 7 8
from simso.core.JobEvent import JobEvent
from math import ceil


9
class Job:
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
    """The Job class simulate the behavior of a real Job. This *should* only be
    instantiated by a Task."""

    def __init__(self, task, name, pred, monitor, etm, sim):
        """
        Args:
            - `task`: The parent :class:`task <simso.core.Task.Task>`.
            - `name`: The name for this job.
            - `pred`: If the task is not periodic, pred is the job that \
            released this one.
            - `monitor`: A monitor is an object that log in time.
            - `etm`: The execution time model.
            - `sim`: :class:`Model <simso.core.Model>` instance.

        :type task: GenericTask
        :type name: str
        :type pred: bool
        :type monitor: Monitor
        :type etm: AbstractExecutionTimeModel
        :type sim: Model
        """
        self._task = task
        self._pred = pred
        self.instr_count = 0  # Updated by the cache model.
        self._computation_time = 0
        self._last_exec = None
        self._n_instr = task.n_instr
        self._start_date = None
        self._end_date = None
        self._is_preempted = False
        self._aborted = False
        self._sim = sim
42 43
        self._activation_date = self._sim.now_ms()
        self._absolute_deadline = self._sim.now_ms() + task.deadline
44 45 46
        self._monitor = monitor
        self._etm = etm
        self._was_running_on = task.cpu
47
        self._wcet = task.wcet
48
        self.name = name
49 50 51

        self._on_activate()

52 53
        self.process = None

54 55 56
        self.processor_ok = self._sim.event()
        self.context_ok = self._sim.event()
        self.context_ok.succeed()
57 58 59 60 61 62 63 64 65 66 67 68 69

    def is_active(self):
        """
        Return True if the job is still active.
        """
        return self._end_date is None

    def _on_activate(self):
        self._monitor.observe(JobEvent(self, JobEvent.ACTIVATE))
        self._sim.logger.log(self.name + " Activated.", kernel=True)
        self._etm.on_activate(self)

    def _on_execute(self):
70
        self._last_exec = self._sim.now
71 72 73 74 75 76 77 78 79 80 81 82 83

        self._etm.on_execute(self)
        if self._is_preempted:
            self._is_preempted = False

        self.cpu.was_running = self

        self._monitor.observe(JobEvent(self, JobEvent.EXECUTE, self.cpu))
        self._sim.logger.log("{} Executing on {}".format(
            self.name, self._task.cpu.name), kernel=True)

    def _on_stop_exec(self):
        if self._last_exec is not None:
84
            self._computation_time += self._sim.now - self._last_exec
85 86 87 88 89 90 91 92 93 94
        self._last_exec = None

    def _on_preempted(self):
        self._on_stop_exec()
        self._etm.on_preempted(self)
        self._is_preempted = True
        self._was_running_on = self.cpu

        self._monitor.observe(JobEvent(self, JobEvent.PREEMPTED))
        self._sim.logger.log(self.name + " Preempted! ret: " +
95
                             str(self.ret), kernel=True)  # TODO: what to pass as interrupted?
96

97
    # TODO: Jobs that terminate after their deadline are not recorded
98 99 100 101
    def _on_terminated(self):
        self._on_stop_exec()
        self._etm.on_terminated(self)

102
        self._end_date = self._sim.now
103 104 105 106 107 108 109 110
        self._monitor.observe(JobEvent(self, JobEvent.TERMINATED))
        self._task.end_job(self)
        self._task.cpu.terminate(self)
        self._sim.logger.log(self.name + " Terminated.", kernel=True)

    def _on_abort(self):
        self._on_stop_exec()
        self._etm.on_abort(self)
111
        self._end_date = self._sim.now
112 113 114 115
        self._aborted = True
        self._monitor.observe(JobEvent(self, JobEvent.ABORTED))
        self._task.end_job(self)
        self._task.cpu.terminate(self)
116 117
        self._sim.logger.log("Job " + str(self.name) +
                             " aborted! ret:" + str(self.ret))
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

    def is_running(self):
        """
        Return True if the job is currently running on a processor.
        Equivalent to ``self.cpu.running == self``.

        :rtype: bool
        """
        return self.cpu.running == self

    def abort(self):
        """
        Abort this job. Warning, this is currently only used by the Task when
        the job exceeds its deadline. It has not be tested from outside, such
        as from the scheduler.
        """
        self._on_abort()

    @property
    def aborted(self):
        """
        True if the job has been aborted.

        :rtype: bool
        """
        return self._aborted

    @property
    def exceeded_deadline(self):
        """
        True if the end_date is greater than the deadline or if the job was
        aborted.
        """
        return (self._absolute_deadline * self._sim.cycles_per_ms <
                self._end_date or self._aborted)

    @property
    def start_date(self):
        """
        Date (in ms) when this job started executing
        (different than the activation).
        """
        return self._start_date

    @property
    def end_date(self):
        """
        Date (in ms) when this job finished its execution.
        """
        return self._end_date

    @property
    def response_time(self):
        if self._end_date:
            return (float(self._end_date) / self._sim.cycles_per_ms -
                    self._activation_date)
        else:
            return None

    @property
    def ret(self):
        """
180
        Remaining execution time in ms.
181 182 183 184
        """
        return self.wcet - self.actual_computation_time

    @property
185 186 187 188 189
    def laxity(self):
        """
        Dynamic laxity of the job in ms.
        """
        return (self.absolute_deadline - self.ret
190
                ) * self._sim.cycles_per_ms - self._sim.now
191 192

    @property
193
    def computation_time(self):
194 195 196
        """
        Time spent executing the job in ms.
        """
197 198 199 200
        return float(self.computation_time_cycles) / self._sim.cycles_per_ms

    @property
    def computation_time_cycles(self):
201 202 203
        """
        Time spent executing the job.
        """
204 205 206 207
        if self._last_exec is None:
            return int(self._computation_time)
        else:
            return (int(self._computation_time) +
208
                    self._sim.now - self._last_exec)
209 210 211

    @property
    def actual_computation_time(self):
212 213 214 215
        """
        Computation time in ms as if the processor speed was 1.0 during the
        whole execution.
        """
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
        return float(
            self.actual_computation_time_cycles) / self._sim.cycles_per_ms

    @property
    def actual_computation_time_cycles(self):
        """
        Computation time as if the processor speed was 1.0 during the whole
        execution.
        """
        return self._etm.get_executed(self)

    @property
    def cpu(self):
        """
        The :class:`processor <simso.core.Processor.Processor>` on which the
        job is attached. Equivalent to ``self.task.cpu``.
        """
        return self._task.cpu

    @property
    def task(self):
        """The :class:`task <simso.core.Task.Task>` for this job."""
        return self._task

    @property
    def data(self):
        """
        The extra data specified for the task. Equivalent to
        ``self.task.data``.
        """
        return self._task.data

    @property
    def wcet(self):
        """
        Worst-Case Execution Time in milliseconds.
        Equivalent to ``self.task.wcet``.
        """
254 255 256 257 258
        return self._wcet

    @wcet.setter
    def wcet(self, value):
        self._wcet = value
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

    @property
    def activation_date(self):
        """
        Activation date in milliseconds for this job.
        """
        return self._activation_date

    @property
    def absolute_deadline(self):
        """
        Absolute deadline in milliseconds for this job. This is the activation
        date + the relative deadline.
        """
        return self._absolute_deadline

    @property
    def absolute_deadline_cycles(self):
        return self._absolute_deadline * self._sim.cycles_per_ms

    @property
    def period(self):
        """Period in milliseconds. Equivalent to ``self.task.period``."""
        return self._task.period

    @property
    def deadline(self):
        """
        Relative deadline in milliseconds.
        Equivalent to ``self.task.deadline``.
        """
        return self._task.deadline

    @property
    def pred(self):
        return self._pred

    def activate_job(self):
297
        self._start_date = self._sim.now
298
        # Notify the OS.
299 300 301 302 303 304
        if self._task.cpu.has_failure():
            proc = next(p for p in self._sim.processors if not p.has_failure())
            proc.activate(self)
            proc.resched()
        else:
            self._task.cpu.activate(self)
305 306 307 308

        # While the job's execution is not finished.
        while self._end_date is None:
            # Wait an execute order.
309
            try:
310
                yield self.processor_ok
311
                self.processor_ok = self._sim.event()
312 313 314
            except Interrupt:
                pass
            else:
315 316 317 318 319
                self._on_execute()
                # ret is a duration lower than the remaining execution time.
                ret = self._etm.get_ret(self)

                while ret > 0:
320 321 322
                    try:
                        yield self._sim.timeout(int(ceil(ret)))
                    except Interrupt:
323 324
                        self._on_preempted()
                        break
325 326
                    else:
                        ret = self._etm.get_ret(self)
327 328 329 330

                if ret <= 0:
                    # End of job.
                    self._on_terminated()