LSTR.py 1.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
from simso.core import Scheduler, Timer
from simso.schedulers import scheduler


@scheduler("simso.schedulers.LSTR")
class LSTR(Scheduler):
    """
    Least Slack Time Rate First
    """

    def init(self):
fshahinfar1 committed
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 57 58 59 60 61 62
        self.ready_list = []
        self.timer = Timer(self.sim, LSTR.virtual_event,
                           (self, self.processors[0]), 1, one_shot=False)
        self.timer.start()

    def virtual_event(self, cpu):
        self.reschedule(cpu)

    def reschedule(self, cpu):
        if self.ready_list:
            cpu.resched()

    def LSTR_rank(self, job):
        """
        calculates rank described in LSTR algorithm for given job
        return value should be non-negative, negative value means that job has passed deadline
        """
        if not job:
            return 0
        # print(job.absolute_deadline, job.deadline)
        din = job.absolute_deadline - self.sim.now_ms()
        if din == 0:
            return 0
        return job.ret / din

    def on_activate(self, job):
        self.ready_list.append(job)
        self.reschedule(job.cpu)

    def on_terminated(self, job):
        self.ready_list.remove(job)
        self.reschedule(job.cpu)

    def schedule(self, cpu):
        decision = []
        if self.ready_list:
            self.ready_list.sort(key=self.LSTR_rank, reverse=True)
            number_of_processors = len(self.processors)
            jobs = self.ready_list[:number_of_processors]

            available_proc = [p for p in self.processors if p.running not in jobs]
            for job in jobs:
                if job.is_running():
                    continue
                for cpu in available_proc:
                    job_on_cpu_rank = self.LSTR_rank(cpu.running)
                    if job_on_cpu_rank < self.LSTR_rank(job):
                        decision.append((job, cpu))
                        available_proc.remove(cpu)
                        break
        return decision