WC_U_EDF.py 1.38 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
"""
Work-Conserving version of U-EDF.
"""

from U_EDF import U_EDF


class WC_U_EDF(U_EDF):
#    def on_terminated(self, job):
#        self.reschedule()
#
#    def on_aborted(self, job):
#        self.reschedule()

    def schedule(self, cpu):
        decisions = U_EDF.schedule(self, cpu)

        # Get active jobs.
        jobs = sorted(
            (task.job for task in self.task_list if task.job.is_active()),
            key=lambda j: j.absolute_deadline)

        # Bind jobs to processors:
        available_procs = list(self.processors)
        was_not_running = []
        for job in jobs:
            if job in self.running_jobs:
                for djob, dproc in decisions:
                    if djob == job:
                        available_procs.remove(dproc)
                        break
                else:
                    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)

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

        return decisions