m p2 : rate-monotonic cpu scheduling

20
MP2: RATE-MONOTONIC CPU SCHEDULING Based on slides by Gourav Khaneja, Raoul Rivas and Keun Yim University of Illinois at Urbana-Champaign Department of Computer Science CS423

Upload: tiva

Post on 05-Jan-2016

55 views

Category:

Documents


2 download

DESCRIPTION

M P2 : Rate-Monotonic CPU Scheduling. University of Illinois at Urbana-Champaign Department of Computer Science CS423. Based on slides by Gourav Khaneja , Raoul Rivas and Keun Yim. Goal. Extending Linux kernel to support RM scheduler. Understand real-time scheduling concepts - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: M P2 :  Rate-Monotonic CPU  Scheduling

MP2: RATE-MONOTONICCPU SCHEDULING

Based on slides by Gourav Khaneja, Raoul Rivas and Keun Yim

University of Illinois at Urbana-Champaign

Department of Computer Science

CS423

Page 2: M P2 :  Rate-Monotonic CPU  Scheduling

GOAL

Understand real-time scheduling concepts Design a real-time scheduler (i.e., RM) and

its admission controller in OS Implement the RM scheduler and admission

controller as a Linux kernel module Learn how to use the kernel-lv. API of the

Linux scheduler, timer, and proc file system Test the kernel-level real-time scheduler by

implementing a user-level periodic task

CS423 MP2 2

Extending Linux kernel to support RM scheduler

Page 3: M P2 :  Rate-Monotonic CPU  Scheduling

RATE MONOTONIC SCHEDULER

Periodic tasks to be completed before next period starts.

Medical systems such as pace maker, airline navigation system

CS423 MP2 3

Page 4: M P2 :  Rate-Monotonic CPU  Scheduling

/PROC Uses the Proc file system as an interface

instead of system calls Process

Registers: provide pid, period, processing time

Yield: Finished Processing. Sleep until the next period

De-register

CS423 MP2 4

Page 5: M P2 :  Rate-Monotonic CPU  Scheduling

CONTEXT SWITCH

Use Linux Scheduler for context switches through scheduling API and policies.

CS423 MP2 5

LinuxScheduler

User Space

Proc FS Read

Proc FS Write

RMS Kernel Module

Kernel Space

PeriodicApp.

Page 6: M P2 :  Rate-Monotonic CPU  Scheduling

DESIGN

CS423 MP2 6

Process(or Task)

Period

ProcessingTime Deadline of Job 1

3. Job Arrival

Job completion 4. Yield

Kernel Space

Proc FSRead Op.

Proc FS Write Op.

2. List of RMProcesses

1. RegisterProcess

Linked Listfor RM Tasks

DispatchingThread

(High RT Priority)

Timer

RM Admission Controller

Yield Func. Handler

LinuxScheduler

User-Level

PeriodicProcess

4. Yield

5. Deregister

1. Register

2. Check

wakeup()

User Space

… 5. Deregister

Page 7: M P2 :  Rate-Monotonic CPU  Scheduling

PROCESS CONTROL BLOCK PCB is defined by the

task_struct structure PCBs are managed by a

circular doubly linked list task_struct *current points to

PCB of the currently runningprocess.

PCB contains the process state:Running or ready: TASK_RUNNING

Waiting: TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE

Other: TASK_TRACED, TASK_STOPPED

CS423 MP2 7

task_struct

task_struct

task_struct

task_struct

Current

Page 8: M P2 :  Rate-Monotonic CPU  Scheduling

task_struct

PROCESS CONTROL BLOCK Additionally PCB contains:

priority: the static priorityrt_priority: the real-time prioritycounter: the number of clock

ticks left in the schedulingbudget of the process

policy: the scheduling policy○ SCHED_NORMAL: Normal○ SCHED_FIFO or SCHED_RR

mm: the memory descriptor

CS423 MP2 8

priority

rt_priority

counter

policy

state

need_resched

Page 9: M P2 :  Rate-Monotonic CPU  Scheduling

MP2_TASK_STRUCT struct task_struct * task Int period Int processing_time Int task_state timer_list timer …

CS423 MP2 9

Page 10: M P2 :  Rate-Monotonic CPU  Scheduling

SCHEDULING POINTS schedule() is the entry point of

the scheduler and invoked by:scheduler_tick() if a

reschedule is marked. (task_struct→needs_resched)

yield() is called by anapplication in user space

schedule() in kernel orprocess context in kernel space

Can schedule() be calledinside interrupt handler?

CS423 MP2 10

schedule()

Timer1/HZ

add_timer(1 jiffy)jiffies++

scheduler_tick()

tick_periodic

Kernel Context yield()

Process Context

Page 11: M P2 :  Rate-Monotonic CPU  Scheduling

TWO HALVES / DEFERRING WORK Kernel timers are used to

create timed events They use jiffies to measure

the system time Timers are interrupts

We can't sleep or hog CPUin them!

Solution:Divide the work into two parts

Uses the timer handler tosignal a thread. (TOP HALF)

Let the kernel threaddo the real job. (BOTTOM HALF)

CS423 MP2 11

Timer

Timer Handler:wake_up(thread);

Thread:While(1){ Do work(); Schedule();}

Interruptcontext

Kernelcontext

TOP HALF

BOTTOM HALF

Page 12: M P2 :  Rate-Monotonic CPU  Scheduling

SCHEDULER API schedule(): triggers rescheduling

Runs after changing the state of any process wake_up_process(struct task_struct *):

This can be called in the interrupt context. sched_setscheduler(): sets sched. parameters

e.g., priority & scheduling policy set_current_state(): changes the state

used to put the running context to sleep. set_task_state(): changes the state of another

CS423 MP2 12

Page 13: M P2 :  Rate-Monotonic CPU  Scheduling

IMPLEMENTATION

CS423 MP2 13

Select the “ready” process with shortest period.

State = running

Wake up process

sleep

State = ready

Wake up scheduler

Update timer

State = sleep

Wake up scheduler

sleep

Yield function handler

Scheduler Timer interrupt

Page 14: M P2 :  Rate-Monotonic CPU  Scheduling

SCHEDULING HACK

Use Linux Scheduling policies

CS423 MP2 14

struct sched_param sparam; wake_up_process(task): sparam.sched_priority=MAX_USER_RT_PRIO-1; sched_setscheduler(task, SCHED_FIFO, &sparam);

Page 15: M P2 :  Rate-Monotonic CPU  Scheduling

LOCKS Spinlocks inside timer interrupts

spin_lock_irqsave(spinlock_t *, unsigned )

Disable interrupts: can safely implement spinlocks on single core machines

spin_unlock_irqrestore(spinlock_t *, unsigned int)

Mutex for linked list

CS423 MP2 15

Page 16: M P2 :  Rate-Monotonic CPU  Scheduling

MEMORY CACHE Improves memory management by reducing

fragmentation For frequent allocation/de-allocation of

given objects

Kmem_cache_create Kmem_cache_alloc Kmem_cache_free Kmem_cache_destroy

CS423 MP2 16

Page 17: M P2 :  Rate-Monotonic CPU  Scheduling

ADMISSION CONTROL

Floating Point operations are expensive

Use Fixed Point numbers

CS423 MP2 17

Page 18: M P2 :  Rate-Monotonic CPU  Scheduling

TEST APPLICATIONvoid main (void)

{

REGISTER(PID, Period, ProcessTime); //Proc filesystem

list=READ STATUS(); //Proc filesystem: Verify the process was admitted

if (!process in the list) exit 1;

//setup everything needed for real-time loop: t0=gettimeofday() for test.c

YIELD(PID); //Proc filesystem

//this is the real-time loop

while(exist jobs)

{

do_job(); //wakeup_time=t0-gettimeofday() and factorial computation

YIELD(PID); //Proc filesystem

}

UNREGISTER(PID); //Proc filesystem

}

CS423 MP2 18

Page 19: M P2 :  Rate-Monotonic CPU  Scheduling

TESTS Calculate the factorial of a fixed number Estimate approx. processing time Test with multiple applications Verify Pre-emption

CS423 MP2 19

Page 20: M P2 :  Rate-Monotonic CPU  Scheduling

CONCLUSION Linux uses Priority based Scheduling Priorities are adjusted based on the state of the

process and its scheduling policy The PCB (task_struct) contains the information about

the state of a process Rescheduling in Linux follows the Two-Halves

approach. Reschedule can occur in process context or kernel context only.

We can use the Scheduler API to build our own policy on top of the Linux scheduler.

Our RMS scheduler will have a scheduler thread and will be invoked after a Job Release and after a Job Completion

CS423 MP2 20