EDF2.py 1.49 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
"""
Implementation of the Global-EDF (Earliest Deadline First) for multiprocessor
architectures.
"""
from simso.core import Scheduler


class EDF2(Scheduler):
    """Earliest Deadline First"""
    def init(self):
        self.running_jobs = []
        self.sched_to_do = False

    def on_activate(self, job):
        self.resched(job.cpu)

    def on_terminated(self, job):
        self.resched(job.cpu)

    def resched(self, cpu):
        if not self.sched_to_do:
            cpu.resched()
        self.sched_to_do = True

    def schedule(self, cpu):
        self.sched_to_do = False
        decisions = []

        ready_jobs = sorted(
            [t.job for t in self.task_list if t.job.is_active()],
            key=lambda x: x.absolute_deadline)
        jobs = ready_jobs[:len(self.processors)]

        # Bind jobs to processors:
        available_procs = list(self.processors)
        was_not_running = []
        for job in jobs:
            if job in self.running_jobs:
                available_procs.remove(job.cpu)
            else:
                was_not_running.append(job)

        remaining_jobs = []
        for job in was_not_running:
            if job.cpu in available_procs:
                decisions.append((job, job.cpu))
                available_procs.remove(job.cpu)
            else:
                remaining_jobs.append(job)

        for p, job in enumerate(remaining_jobs):
            decisions.append((job, available_procs[p]))

        self.running_jobs = jobs

        return decisions