Job.py 9.38 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 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
from simso.core.JobEvent import JobEvent
from math import ceil


class Job(Process):
    """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
        """
31
        Process.__init__(self, env=sim,generator=self.activate_job())
32 33 34 35 36 37 38 39 40 41 42
        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
43 44
        self._activation_date = self._sim.now_ms()
        self._absolute_deadline = self._sim.now_ms() + task.deadline
45 46 47
        self._monitor = monitor
        self._etm = etm
        self._was_running_on = task.cpu
48
        self._wcet = task.wcet
49
        self.name = name
50 51 52

        self._on_activate()

53 54 55 56
        self.processor_ok = self._sim.event()
        self.context_ok = self._sim.event()
        self.context_ok.succeed()
        self.context_ready=True
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("Don't know what to pass else?"), kernel=True) # TODO: what to pass as interrupted?
96 97 98 99 100

    def _on_terminated(self):
        self._on_stop_exec()
        self._etm.on_terminated(self)

101
        self._end_date = self._sim.now
102 103 104 105 106 107 108 109
        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)
110
        self._end_date = self._sim.now
111 112 113 114
        self._aborted = True
        self._monitor.observe(JobEvent(self, JobEvent.ABORTED))
        self._task.end_job(self)
        self._task.cpu.terminate(self)
115 116
        self._sim.logger.log("Job " + str(self.name) +
                             " aborted! ret:" + str(self.ret))
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

    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):
        """
179
        Remaining execution time in ms.
180 181 182 183
        """
        return self.wcet - self.actual_computation_time

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

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

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

    @property
    def actual_computation_time(self):
211 212 213 214
        """
        Computation time in ms as if the processor speed was 1.0 during the
        whole execution.
        """
215 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
        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``.
        """
253 254 255 256 257
        return self._wcet

    @wcet.setter
    def wcet(self, value):
        self._wcet = value
258 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

    @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):
296
        self._start_date = self._sim.now
297 298 299 300 301 302
        # Notify the OS.
        self._task.cpu.activate(self)

        # While the job's execution is not finished.
        while self._end_date is None:
            # Wait an execute order.
303
            try:
304 305
                yield self.processor_ok
                self.processor_ok = self._sim.event() 
306 307 308
            except Interrupt:
                pass
            else:
309 310 311 312 313
                self._on_execute()
                # ret is a duration lower than the remaining execution time.
                ret = self._etm.get_ret(self)

                while ret > 0:
314 315 316
                    try:
                        yield self._sim.timeout(int(ceil(ret)))
                    except Interrupt:
317 318
                        self._on_preempted()
                        break
319 320
                    else:
                        ret = self._etm.get_ret(self)
321 322 323 324 325

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

326 327 328 329 330 331 332

class SequentialJob(Job):
    pass


class ParallelJob(Job):
    pass