1 / 48

Linux Scheduling

Linux Scheduling. 9662541 張文軒 9665510 許晉榮 9665531 林奕翔. Outline. Introduction Linux 2.6 Scheduling Policy Priority Time Quantum Duration Data Structure Functions Used by the Scheduler Runqueue Balancing in Multiprocessor Systems System call related to scheduling Reference. Outline.

Download Presentation

Linux Scheduling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linux Scheduling 9662541 張文軒 9665510 許晉榮 9665531 林奕翔

  2. Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • System call related to scheduling • Reference Advanced Operation System - Linux Scheduling

  3. Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • Reference Advanced Operation System - Linux Scheduling

  4. Introduction • What is Scheduling? • when to switch and which process to choose. • Goal of the Linux scheduling: • fast process response time. • good throughput for background jobs. • avoidance of process starvation. • reconciliation of the needs of low- and high-priority processes. • etc… Advanced Operation System - Linux Scheduling

  5. New feature of scheduling in Linux 2.6 • Preemptable kernel bits: • Even as the system enters Kernel Mode, the process can still be interrupted. • New ultra-scalable O(1) schedule: • Every algorithm in the v2.6 scheduling algorithm in constant time, regardless of the number of running processes. • Implement perfect Symmetric Multiprocessing (SMP) scalability: • Each process has its own locking and individual runqueue. Advanced Operation System - Linux Scheduling

  6. Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • Reference Advanced Operation System - Linux Scheduling

  7. System Call Related to Scheduling Advanced Operation System - Linux Scheduling

  8. Scheduling Policy • Linux scheduling is based on time sharing technique • The scheduling policy is based on ranking processes according to their dynamic priority. • Process priority is dynamically determined by scheduler periodically. Advanced Operation System - Linux Scheduling

  9. Scheduling Policy • Process classification: • Interactive process. • Batch process. • Real-time process. • CPU-bound vs. I/O-bound. • Heuristic algorithm Advanced Operation System - Linux Scheduling

  10. Scheduling Policy • In linux 2.6, processes are preemptable • when higher priority process enters the TASK_RUNNING state, the current one will be preempted. • TIF_NEED_RESCHED • Every Linux process is always scheduled according to one of the following scheduling classes: • SCHED_NORMAL=0 • SCHED_FIFO (real-time process)=1 • SCHED_RR (real-time process)=2 Advanced Operation System - Linux Scheduling

  11. Scheduler v2.4 User mode User mode Kernel mode Scheduler v2.6 Kernel mode Kernel mode Process A Process B User mode User mode Scheduler v2.4 Scheduler v2.6 Process Preempt Advanced Operation System - Linux Scheduling

  12. Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • Reference Advanced Operation System - Linux Scheduling

  13. Normal Process Priority • Static Priority: • Have the same meanings with nice value in linux 2.4. • Ranging from 100 (highest priority) to 139 (lowest priority) • Determines the base time-slice of a process. • Nice Value: • Used by linux 2.4. • Ranging from -20 to 19. • In linux 2.6, Be replaced by static priority. • static_prio = MAX_RT_PRIO + nice + 20 Advanced Operation System - Linux Scheduling

  14. Normal Process Priority • Dynamic priority • Looked up by the scheduler when selecting the new process to run. • Related to the average sleep time(sleep_avg) of the process. Advanced Operation System - Linux Scheduling

  15. Normal Process Priority • User could change priority by: • nice() • setpriority() Advanced Operation System - Linux Scheduling

  16. Real-time Process Priority • Each real-time process has a real-time priority: • Ranging from 1 (highest priority) to 99 (lowest priority). • A RT process is replaced by another process only when one of the following events occurs: • Be preempted by another process having higher RT priority. • Performs a blocking operation, and it is put to sleep. • The process is stopped or it is killed. • Invoking the sched_yield( ) system call. • It has exhausted its time quantum(SCHED_RR). Advanced Operation System - Linux Scheduling

  17. Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • Reference Advanced Operation System - Linux Scheduling

  18. Calculate Time-Slice • In linux 2.6 Advanced Operation System - Linux Scheduling

  19. Calculate Time-Slice • According to the code, time-slice will range from 5~800ms. • The higher the static priority, the longer the time-slice. • In linux 2.4, the time-slice range from 10~100ms • So, linux 2.6 has higher elasticity. Advanced Operation System - Linux Scheduling

  20. Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • Reference Advanced Operation System - Linux Scheduling

  21. Data Structure (runqueue) • The runqueue data structure is the most important data structure of the Linux 2.6 scheduler. • Each CPU in the system has its own runqueue. • The arrays field of the runqueue is an array consisting of two prio_array_t structures. • One is for active process. • The other is for expired process. Advanced Operation System - Linux Scheduling

  22. Data Structure (runqueue) • Active processes • These runnable processes have not yet exhausted their time quantum and are thus allowed to run. • Expired processes • These runnable processes have exhausted their time quantum and are thus forbidden to run until all active processes expire. Advanced Operation System - Linux Scheduling

  23. Active and Expired Array • Includes 140 doubly linked list heads (one list for each possible process priority) Advanced Operation System - Linux Scheduling

  24. Runqueue(v2.6) vs. Task Queue(v2.4) • Linux 2.6 • Each processor has its own runqueue. Advanced Operation System - Linux Scheduling

  25. Runqueue(v2.6) vs. Task Queue(v2.4) • Linux 2.4 • All processor use a global task queue. Advanced Operation System - Linux Scheduling

  26. The Important Fields of the runqueue Structure Advanced Operation System - Linux Scheduling

  27. The Important Fields of the runqueue Structure Advanced Operation System - Linux Scheduling

  28. The Important Fields of the Process Descriptor Advanced Operation System - Linux Scheduling

  29. bit 2 priority 2 bit 0 priority 0 Queue of runnable tasks for priority 2 Bit Map sched_find_first_bit() schedule() Find the Next Process in the Queue • In linux 2.6 • Use Bitmap technique -> O(1) Advanced Operation System - Linux Scheduling

  30. Find the Next Process in the Queue • Through sched_find_first_bit() to find Advanced Operation System - Linux Scheduling

  31. Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • Reference Advanced Operation System - Linux Scheduling

  32. Functions Used by the Scheduler • The scheduler relies on several functions in order to do its work; the most important are: • scheduler_tick( ): Keeps the time_slice counter of current up-to-date. • try_to_wake_up( ): Awakens a sleeping process. • recalc_task_prio( ): Updates the dynamic priority of a process. • schedule( ): Selects a new process to be executed. Advanced Operation System - Linux Scheduling

  33. Schedule() in linux 2.6 • The schedule( ) function implements the scheduler. • Its objective is to find a process in the runqueue list and then assign the CPU to it. • schedule() preprocessing • Disable preemption and initializing a few local variables. • Determine the length of time that the previous task has been running. Advanced Operation System - Linux Scheduling

  34. Schedule() in linux 2.6 • schedule( ) to make the process switch • If there are runnable tasks in the runqueue but not in the active priority array, then the active and expired priority arrays are swapped. • Exchanging two array pointer takes constant-time O(1). Advanced Operation System - Linux Scheduling

  35. Schedule() in linux 2.6 • Use bitmap to find the next task. • Or load balancing is made. • Or a switch to the idle task is made. Advanced Operation System - Linux Scheduling

  36. Schedule() in linux 2.6 • schedule( ) after a process switch • TIF_NEED_RESCHED flag clear. • Previous task gets its run time deducted from its sleep_avg. Advanced Operation System - Linux Scheduling

  37. 1. Initialize some local variables 2. Release the kernel lock Get the lock of run queue 3. Set the status of previous process 4. Calculate goodness of each process. And get the next process to run. All processes use up it time quantum 5. Start a new epoch and reset the time quantum Select one process 6. Perform context switching Select the same process 7. Return Schedule() in linux 2.4 Advanced Operation System - Linux Scheduling

  38. Schedule() in linux 2.4 • goodness(p, this_cpu, prev->active_mm) • weight = -1 • p is the prev and its SCHED_YIELD flag is set • weight = 0 • p is a conventional process • Has exhausted its quantum (p->counter is zero) • 2 <= weight <= 77 • p is a conventional process • Has not exhausted its quantum • weight >= 1000 • p is a real-time process Advanced Operation System - Linux Scheduling

  39. Schedule() in linux 2.4 • Main Problem of the schedule() in linux 2.4 • Have an extra operation to calculate Weight of each process through goodness(). • When all processes in task queue can’t be execute, , calculate time-slice of all processes again. • This operation takes O(n). Advanced Operation System - Linux Scheduling

  40. Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • System call related to scheduling • Reference Advanced Operation System - Linux Scheduling

  41. Runqueue Balancing in Multiprocessor Systems • A given CPU can execute only the runnable processes that are contained in the corresponding runqueue. • No runnable process ever appears in two or more runqueues. • The kernel periodically checks whether theworkloads of the runqueues are balanced. • If necessary, moves some processfrom one runqueue to another. Advanced Operation System - Linux Scheduling

  42. The Main Function for Runqueue Balancing • rebalance_tick( ) • The rebalance_tick( ) function is invoked by scheduler_tick( ) once every tick. • Determines whether the time has come to invoke the load_balance( ) function • load_balance() • Checks whether a scheduling domain is significantly unbalanced. • Moving some processes from the busiest group to the runqueue of the local CPU. • move_tasks( ) • Moves processes from a source runqueue to the local runqueue. Advanced Operation System - Linux Scheduling

  43. Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • System call related to scheduling • Reference Advanced Operation System - Linux Scheduling

  44. System call related to scheduling • nice( ) • Change the static priority of a conventional process. • getpriority( ) • Get the maximum static priority of a group of conventional processes. • setpriority( ) • Set the static priority of a group of conventional processes. • sched_getscheduler( ) • Get the scheduling policy of a process. Advanced Operation System - Linux Scheduling

  45. System call related to scheduling • sched_setscheduler( ) • Set the scheduling policy and the real-time priority of a process. • sched_getparam( ) • Get the real-time priority of a process. • sched_setparam( ) • Set the real-time priority of a process. • sched_yield( ) • Relinquish the processor voluntarily without blocking. Advanced Operation System - Linux Scheduling

  46. System call related to scheduling • sched_get_priority_min() • Get the minimum real-time priority value for a policy. • sched_get_priority_max() • Get the maximum real-time priority value for a policy. • sched_rr_get_interval() • Get a process’s timeslice value for the Round Robin policy. • sched_getaffinity() • Get the CPU affinity mask of a process. • sched_setaffinity() • Set the CPU affinity mask of a process. Advanced Operation System - Linux Scheduling

  47. Outline • Introduction • Linux 2.6 • Scheduling Policy • Priority • Time Quantum Duration • Data Structure • Functions Used by the Scheduler • Runqueue Balancing in Multiprocessor Systems • System call related to scheduling • Reference Advanced Operation System - Linux Scheduling

  48. Reference • Understanding the Linux Kernel, 3rd Edition Daniel P. Bovet, Marco Cesati • Linux 2.4/2.6 核心排程機制剖析 http://loda.zhupiter.com/Linux%202.4-2.6%20KernelSchedulandRealTimeSupport_2.1.htm • Linux 2.6調度系統分析 http://blog.chinaunix.net/u/7270/showart_300343.html • Previous PowerPoint Slices Advanced Operation System - Linux Scheduling

More Related