200 likes | 352 Views
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
E N D
MP2: Rate-MonotonicCPU Scheduling University of Illinois at Urbana-Champaign Department of Computer Science CS423 Based on slides by Gourav Khaneja, Raoul Rivas and KeunYim
Goal Extending Linux kernel to support RM scheduler • 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
Rate Monotonic Scheduler • Periodic tasks to be completed before next period starts. • Medical systems such as pace maker, airline navigation system CS423 MP2
/Proc • Uses the Procfile systemas an interfaceinstead of system calls • Process • Registers: provide pid, period, processing time • Yield: Finished Processing. Sleep until the next period • De-register CS423 MP2
Context Switch • Use Linux Scheduler for context switches through scheduling API and policies. Linux Scheduler Periodic App. RMS Kernel Module Proc FS Write Proc FS Read User Space Kernel Space CS423 MP2
Design Processing Time Deadline of Job 1 1. Register Job completion 4. Yield Process (or Task) … 5. Deregister 2. Check Period 3. Job Arrival User-Level Periodic Process 1. RegisterProcess Proc FS Write Op. RM Admission Controller 2. List of RMProcesses Proc FS Read Op. Linked List for RM Tasks 4. Yield wakeup() Dispatching Thread (High RT Priority) Yield Func. Handler 5. Deregister Linux Scheduler Timer User Space Kernel Space CS423 MP2
Process Control Block • PCB is defined by thetask_struct structure • PCBs are managed by acircular doubly linked list • task_struct *currentpoints toPCB 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 task_struct task_struct Current task_struct task_struct CS423 MP2
Process Control Block • Additionally PCB contains: • priority: the static priority • rt_priority: the real-time priority • counter: the number of clockticks left in the schedulingbudget of the process • policy: the scheduling policy • SCHED_NORMAL: Normal • SCHED_FIFO or SCHED_RR • mm: the memory descriptor task_struct priority rt_priority counter policy state need_resched CS423 MP2
Mp2_task_struct • structtask_struct * task • Int period • Intprocessing_time • Inttask_state • timer_list timer • … CS423 MP2
Scheduling Points • schedule() is the entry point ofthe scheduler and invoked by: • scheduler_tick() if areschedule 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? Timer 1/HZ tick_periodic add_timer(1 jiffy) jiffies++ scheduler_tick() schedule() Process Context Kernel Context yield() CS423 MP2
Two Halves / Deferring Work TOP HALF • Kernel timers are used tocreate timed events • They use jiffies to measurethe 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) Timer Interrupt context Timer Handler: wake_up(thread); Kernel context Thread: While(1) { Do work(); Schedule(); } BOTTOM HALF CS423 MP2
Scheduler API • schedule(): triggers rescheduling • Runs after changing the state of any process • wake_up_process(structtask_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
Implementation Yield function handler Timer interrupt Scheduler Select the “ready” process with shortest period. State = running Wake up process sleep Update timer State = sleep Wake up scheduler sleep State = ready Wake up scheduler CS423 MP2
Scheduling Hack • Use Linux Scheduling policies structsched_paramsparam; wake_up_process(task): sparam.sched_priority=MAX_USER_RT_PRIO-1; sched_setscheduler(task, SCHED_FIFO, &sparam); CS423 MP2
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
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
Admission Control Floating Point operations are expensive Use Fixed Point numbers CS423 MP2
Test application void main (void) { REGISTER(PID, Period, ProcessTime); //Procfilesystem list=READ STATUS(); //Procfilesystem: 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); //Procfilesystem //this is the real-time loop while(exist jobs) { do_job(); //wakeup_time=t0-gettimeofday() and factorial computation YIELD(PID); //Procfilesystem } UNREGISTER(PID); //Procfilesystem } CS423 MP2
Tests • Calculate the factorial of a fixed number • Estimate approx. processing time • Test with multiple applications • Verify Pre-emption CS423 MP2
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