linux scheduling cs 4510. scheduling policy ► the scheduling algorithm of traditional unix systems...

28
Linux Scheduling Linux Scheduling CS 4510 CS 4510

Upload: barrie-caldwell

Post on 16-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Linux SchedulingLinux Scheduling

CS 4510CS 4510

Page 2: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Scheduling PolicyScheduling Policy

► The scheduling algorithm of traditional Unix The scheduling algorithm of traditional Unix systems must fulfill several conflicting systems must fulfill several conflicting objectivesobjectives Fast process response timeFast process response time Good throughput for background jobsGood throughput for background jobs Avoidance of process starvationAvoidance of process starvation Etc.Etc.

► The set of rules used to determine when and The set of rules used to determine when and how to select a new process to run is called how to select a new process to run is called the the scheduling policyscheduling policy

Page 3: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Scheduling PolicyScheduling Policy

►Linux scheduling is based on time Linux scheduling is based on time sharingsharing

►CPU time is divided into slices, one for CPU time is divided into slices, one for each runnable processeach runnable process

► If a currently running process is not If a currently running process is not terminated when its quantum expires, terminated when its quantum expires, a process switch may take placea process switch may take place

Page 4: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Scheduling PolicyScheduling Policy

►The scheduling policy ranks processes The scheduling policy ranks processes according to their priorityaccording to their priority

► In Linux, process priority is dynamic.In Linux, process priority is dynamic. Processes that have been denied the use Processes that have been denied the use

of the CPU for a long time are boosted by of the CPU for a long time are boosted by dynamically increasing their prioritydynamically increasing their priority

Long running processes have their priority Long running processes have their priority lowered.lowered.

Page 5: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Classes of ProcessesClasses of Processes

► Interactive ProcessesInteractive Processes Must respond quicklyMust respond quickly

►Typically between 50 and 150 msTypically between 50 and 150 ms►Variance must also be boundedVariance must also be bounded

Includes:Includes:►Command ShellsCommand Shells►Text editorsText editors►Graphical applicationsGraphical applications

Page 6: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Classes of ProcessesClasses of Processes

►Batch processesBatch processes Do not need user interactionDo not need user interaction Often penalized by the SchedulerOften penalized by the Scheduler Includes:Includes:

►CompilersCompilers►Database Search EnginesDatabase Search Engines►Scientific ComputationsScientific Computations

Page 7: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Classes of ProcessesClasses of Processes

► Real-time processesReal-time processes Have very stringent scheduling requirementsHave very stringent scheduling requirements Should never be blocked by lower-priority Should never be blocked by lower-priority

processesprocesses Needs Needs

►Short guaranteed response timeShort guaranteed response time►Minimum varianceMinimum variance

IncludesIncludes►Video and sound applicationsVideo and sound applications►Robot controllersRobot controllers►Data Collectors from physical sensorsData Collectors from physical sensors

Page 8: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Process PreemptionProcess Preemption

► If a process enters the TASK_RUNNING state, If a process enters the TASK_RUNNING state, the kernel checks whether its dynamic the kernel checks whether its dynamic priority is greater than the priority of the priority is greater than the priority of the currently running process.currently running process.

► If so, the current process is interrupted and If so, the current process is interrupted and the scheduler is invoked to select another the scheduler is invoked to select another process to run.process to run.

► A preempted process is not suspended it A preempted process is not suspended it remains in the TASK_RUNNING state; it remains in the TASK_RUNNING state; it simply no longer uses the CPUsimply no longer uses the CPU

Page 9: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

How Long Should a Quantum How Long Should a Quantum Last?Last?

► If its too short, system overhead is highIf its too short, system overhead is high► If too long, processes no longer appear to be If too long, processes no longer appear to be

responsiveresponsive► Long quantum durations do not usually Long quantum durations do not usually

degrade response timedegrade response time Higher priority process, such as interactive Higher priority process, such as interactive

process, will quickly interrupt lower priority process, will quickly interrupt lower priority processes like batch processes processes like batch processes

► The choice of quantum duration is always a The choice of quantum duration is always a compromise compromise Choose a duration as long as possible while Choose a duration as long as possible while

keeping good system response time.keeping good system response time.

Page 10: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

The Scheduling AlgorithmThe Scheduling Algorithm

►CPU time is divided into CPU time is divided into epochs. epochs. Every process has a time quantum whose Every process has a time quantum whose

duration is computed when the epoch duration is computed when the epoch beginsbegins

The quantum is the maximum CPU time The quantum is the maximum CPU time assigned to the process in that epochassigned to the process in that epoch

A process can be selected several times A process can be selected several times from the scheduler in the same epoch, as from the scheduler in the same epoch, as long as its quantum is not exhaustedlong as its quantum is not exhausted

Page 11: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

The Scheduling AlgorithmThe Scheduling Algorithm

►Base Time QuantumBase Time Quantum Assigned by the scheduler if a process has Assigned by the scheduler if a process has

exhausted its time quantum in the exhausted its time quantum in the previous epochprevious epoch

Users can change the base time quantum Users can change the base time quantum by using the system calls nice( ) and by using the system calls nice( ) and setPriority( )setPriority( )

A new process inherits the base time A new process inherits the base time quantum of its parentquantum of its parent

Page 12: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

The Scheduling AlgorithmThe Scheduling Algorithm

►Process PrioritiesProcess Priorities Static priorityStatic priority

►Assigned by the users to real-time processAssigned by the users to real-time process►Ranges from 1 to 99Ranges from 1 to 99►Never changed by the schedulerNever changed by the scheduler

Dynamic priorityDynamic priority►Applies only to conventional processesApplies only to conventional processes►Sum of Sum of

Base time quantum Base time quantum Ticks of CPU time left to the process before its Ticks of CPU time left to the process before its

quantum expiresquantum expires

Page 13: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

CPU’s Data StructuresCPU’s Data Structures

► nicenice Determines the length of the process time Determines the length of the process time

quantum when a new epoch begins. Ranges quantum when a new epoch begins. Ranges between -20 and +19between -20 and +19►Negative values correspond to high priority processesNegative values correspond to high priority processes

► countercounter Number of ticks of CPU time left to the process Number of ticks of CPU time left to the process

before its quantum expiresbefore its quantum expires► need_reschedneed_resched

A flag checked by ret_from_sys_call( ) to decide A flag checked by ret_from_sys_call( ) to decide whether to invoke the schedule( ) functionwhether to invoke the schedule( ) function

Page 14: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

The schedule( ) functionThe schedule( ) function

► Direct InvocationDirect Invocation The scheduler is invoked directly when the The scheduler is invoked directly when the

current process must be blocked right awaycurrent process must be blocked right away► StepsSteps

1.1. Inserts current in the proper wait queueInserts current in the proper wait queue2.2. Changes state to TASK_INTERRUPTIBLE or Changes state to TASK_INTERRUPTIBLE or

TASK_UNINTERRUPTIBLETASK_UNINTERRUPTIBLE3.3. Invokes schedule( )Invokes schedule( )4.4. Goto step 2 unless resource is availableGoto step 2 unless resource is available5.5. Once resource is available, removes current Once resource is available, removes current

from wait queuefrom wait queue

Page 15: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

The schedule( ) FunctionThe schedule( ) Function

►Lazy InvocationLazy Invocation The scheduler can also be invoked in a lazy way by The scheduler can also be invoked in a lazy way by

setting the “need_resched” field of current to 1setting the “need_resched” field of current to 1 A check on this value is always made before resuming A check on this value is always made before resuming

execution of a User Mode processexecution of a User Mode process Schedule will be invoked at some time in the near Schedule will be invoked at some time in the near

futurefuture► Lazy invocation is performed in the following Lazy invocation is performed in the following

casescases When current has used up its time quantumWhen current has used up its time quantum When a process is woken up and its priority is higher When a process is woken up and its priority is higher

than the current processthan the current process When a sched_yield( ) system call is issuedWhen a sched_yield( ) system call is issued

Page 16: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Actions Performed Before Actions Performed Before a Process Switcha Process Switch

►The key outcome of the function is to The key outcome of the function is to set a local variable ‘next’ so that it set a local variable ‘next’ so that it points to the PCB of the process points to the PCB of the process selected to replace currentselected to replace current

Page 17: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Actions Performed Before Actions Performed Before a Process Switcha Process Switch

►The schedule function starts with the The schedule function starts with the following codefollowing codeprev = current;prev = current;

this_cpu = prev->processor;this_cpu = prev->processor;

sched_data = &aligned_data[this_cpu]sched_data = &aligned_data[this_cpu]

Page 18: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Actions Performed Before Actions Performed Before a Process Switcha Process Switch

►Before starting to look at the runnable Before starting to look at the runnable processes, schedule( ) must disable processes, schedule( ) must disable the local interrupts and acquire the the local interrupts and acquire the spin lock that protects the run queue.spin lock that protects the run queue.

►spin_lock_irq(&runqueue_lock)spin_lock_irq(&runqueue_lock)

Page 19: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Actions Performed Before Actions Performed Before a Process Switcha Process Switch

► If prev is not in the TASK_RUNNING state, schedule( If prev is not in the TASK_RUNNING state, schedule( ) was directly invoked by the process itself because ) was directly invoked by the process itself because it had to wait on some external resource; therefore, it had to wait on some external resource; therefore, prev must be removed from the runqueue prev must be removed from the runqueue

► if (prev->state != TASK_RUNNING) if (prev->state != TASK_RUNNING) del_from_runqueue(prev);del_from_runqueue(prev);

► The function also resets the need_resched field of The function also resets the need_resched field of current, just in case the scheduler was activated in current, just in case the scheduler was activated in the lazy way:the lazy way:

► prev->need_resched = 0;prev->need_resched = 0;

Page 20: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Actions Performed Before Actions Performed Before a Process Switcha Process Switch

► Now schedule scans the runqueue to find the Now schedule scans the runqueue to find the process to be executed in the next quantum.process to be executed in the next quantum.

repeat_schedule:repeat_schedule:next = init_tasks[this_cpu];next = init_tasks[this_cpu];c = -1000;c = -1000;list_for_each(tmp, &runqueue_head)list_for_each(tmp, &runqueue_head)

P = list_entry(tmp, struct task_struct, runlist);P = list_entry(tmp, struct task_struct, runlist);int weight = goodness(p, this_cpu, prev->active_mm);int weight = goodness(p, this_cpu, prev->active_mm);If (weight > c) c = weight, next = p;If (weight > c) c = weight, next = p;

► The goodness( ) function returns an integer that The goodness( ) function returns an integer that denotes the priority of the process passed as a denotes the priority of the process passed as a parameterparameter

Page 21: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Actions Performed Before Actions Performed Before a Process Switcha Process Switch

►While scanning processes in the While scanning processes in the runqueue, schedule( ) considers only runqueue, schedule( ) considers only those that arethose that are Runnable on the executing CPU Runnable on the executing CPU

►(cpus_allowed & 1<<this_cpu)(cpus_allowed & 1<<this_cpu)

Not alreading on some other CPUNot alreading on some other CPU►(cpus_runnable & 1<<this_cpu)(cpus_runnable & 1<<this_cpu)

Page 22: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Actions Performed Before Actions Performed Before a Process Switcha Process Switch

► If the run-queue is empty, next points If the run-queue is empty, next points to the swapper kernel thread to the swapper kernel thread associated with the executing CPU.associated with the executing CPU.

► It is also possible that the best It is also possible that the best candidate turns out to be the old candidate turns out to be the old current process -- prevcurrent process -- prev

Page 23: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Actions Performed Before Actions Performed Before a Process Switcha Process Switch

► If If C C is set to 0is set to 0,, then all processes have exhausted then all processes have exhausted their time quantum. When this happens a new their time quantum. When this happens a new epoch begins, and all processes are assigned a epoch begins, and all processes are assigned a fresh quantumfresh quantum

(if !c)(if !c)for_each_task(p)for_each_task(p)

p->counter = (p->counter >> 1) + p->counter = (p->counter >> 1) + (20 – p->nice) / 4 + 1(20 – p->nice) / 4 + 1

goto repeat_schedulegoto repeat_schedule

► Suspended or stopped process have their dynamic Suspended or stopped process have their dynamic priorities periodically increased. This gives a higher priorities periodically increased. This gives a higher priority to interactive processespriority to interactive processes

Page 24: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Actions Performed Before Actions Performed Before a Process Switcha Process Switch

►The last thing that is done before the The last thing that is done before the process switch is to be sure that the process switch is to be sure that the processes memory is set up properlyprocesses memory is set up properly

Page 25: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Actions Performed after aActions Performed after a Context Switch Context Switch

►Schedule( ) invokes Schedule( ) invokes _schedule_tail()_schedule_tail()

►This function checks whether some This function checks whether some other process has set the other process has set the need_reschedneed_resched field of prev while it field of prev while it was not running. In this case, the was not running. In this case, the whole schedule function is reexecuted whole schedule function is reexecuted from the beginning.from the beginning.

Page 26: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Actions Performed After Actions Performed After a Process Switcha Process Switch

►Most of the functions performed by Most of the functions performed by schedule( ) after the context switch schedule( ) after the context switch are primarily important for multi-are primarily important for multi-processor systems.processor systems.

Page 27: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Goodness ValuesGoodness Values

►weight == -1weight == -1 p is the previous process, and its SCHED_YIELD p is the previous process, and its SCHED_YIELD

flag is set. The process will be selected only if no flag is set. The process will be selected only if no other runnable processes are in the runqueueother runnable processes are in the runqueue

►weight == 0weight == 0 p is a conventional process that has exhausted p is a conventional process that has exhausted

its quantumits quantum► 2 <= weight <= 772 <= weight <= 77

p is a conventional process that has not p is a conventional process that has not exhausted its quantumexhausted its quantum

►weight >= 1000weight >= 1000 p is a real-time processp is a real-time process

Page 28: Linux Scheduling CS 4510. Scheduling Policy ► The scheduling algorithm of traditional Unix systems must fulfill several conflicting objectives  Fast

Goodness ValuesGoodness Values

►The goodness value is computed as The goodness value is computed as followsfollows

► weight = p->counter +20 - p->niceweight = p->counter +20 - p->nice;;

►A bonus is also given to process running A bonus is also given to process running on the same CPU and those which share on the same CPU and those which share the same memory space as the kernelthe same memory space as the kernel