+ All Categories

l13

Date post: 29-Sep-2015
Category:
Upload: madhu-sudhan
View: 215 times
Download: 2 times
Share this document with a friend
Description:
linux scheduler
Popular Tags:
40
Linux Scheduler Linux Scheduler Descending to Reality. . . Philosophies Processor Scheduling Processor Affinity Basic Scheduling Algorithm The Run Queue The Highest Priority Process Calculating Timeslices Typical Quanta Dynamic Priority Interactive Processes Using Quanta Avoiding Indefinite Overtaking The Priority Arrays Swapping Arrays Why Two Arrays? The Traditional Algorithm Linux is More Efficient Locking Runqueues Real-Time Scheduling Sleeping and Waking Timers 1 / 40
Transcript
  • Linux Scheduler

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    1 / 40

  • Descending to Reality. . .

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    2 / 40

    n The Linux scheduler tries to be very efficientn To do that, it uses some complex data

    structuresn Some of what it does actually contradicts the

    schemes weve been discussing. . .

  • Philosophies

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    3 / 40

    n Use large quanta for important processesn Modify quanta based on CPU usen Bind processes to CPUsn Do everything in O(1) time

  • Processor Scheduling

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    4 / 40

    n Have a separate run queue for each processorn Each processor only selects processes from its

    own queue to runn Yes, its possible for one processor to be idle

    while others have jobs waiting in their runqueues

    n Periodically, the queues are rebalanced: if oneprocessors run queue is too long, someprocesses are moved from it to anotherprocessors queue

  • Processor Affinity

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    5 / 40

    n Each process has a bitmask saying what CPUsit can run on

    n Normally, of course, all CPUs are listedn Processes can change the maskn The mask is inherited by child processes (and

    threads), thus tending to keep them on thesame CPU

    n Rebalancing does not override affinity

  • Basic Scheduling Algorithm

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    6 / 40

    n Find the highest-priority queue with a runnableprocess

    n Find the first process on that queuen Calculate its quantum sizen Let it runn When its time is up, put it on the expired listn Repeat

  • The Run Queue

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    7 / 40

    n 140 separate queues, one for each priority leveln Actually, that number can be changed at a

    given siten Actually, two sets, active and expiredn Priorities 0-99 for real-time processesn Priorities 100-139 for normal processes; value

    set via nice() system call

  • The Highest Priority Process

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    8 / 40

    n There is a bit map indicating which queueshave processes that are ready to run

    n Find the first bit thats set:

    u 140 queues 5 integersu Only a few compares to find the first that

    is non-zerou Hardware instruction to find the first 1-bitu Time depends on the number of priority

    levels, not the number of processes

  • Calculating Timeslices

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    9 / 40

    n Calculate

    Quantum =

    (140 SP) 20 if SP < 120(140 SP) 5 if SP 120

    where SP is the static priorityn Higher priority process get longer quantan Basic idea: important processes should run

    longern Other mechanisms used for quick interactive

    response

  • Typical Quanta

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    10 / 40

    StaticPri Niceness Quantum

    Highest Static Pri 100 20 800 msHigh Static Pri 110 -10 600 msNormal 120 0 100 msLow Static Pri 130 +10 50 msLowest Static Pri 139 +20 5 ms

  • Dynamic Priority

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    11 / 40

    n Dynamic priority is calculated from staticpriority and average sleep time

    n When process wakes up, record how long itwas sleeping, up to some maximum value

    n When the process is running, decrease thatvalue each timer tick

    n Roughly speaking, the bonus is a number in[0, 10] that measures what percentage of thetime the process was sleeping recently; 5 isneutral, 10 helps priority by 5, 0 hurts priorityby 5

    DP = max(100, min(SP bonus + 5, 139))

  • Interactive Processes

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    12 / 40

    n A process is interactive if

    bonus 5 S/4 28

    n Low-priority processes have a hard timebecoming interactive

    n A default priority process becomes interactivewhen its sleep time is greater than 700 ms

  • Using Quanta

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    13 / 40

    n At every time tick, decrease the quantum ofthe current running process

    n If the time goes to zero, the process is donen If the process is non-interactive, put it aside on

    the expired listn If the process is interactive, put it at the end

    of the current priority queuen If theres nothing else at that priority, it will

    run again immediatelyn Of course, by running so much is bonus will go

    down, and so will its priority and its interativestatus

  • Avoiding Indefinite Overtaking

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    14 / 40

    n There are two sets of 140 queues, active andexpired

    n The system only runs processes from activequeues, and puts them on expired queueswhen they use up their quanta

    n When a priority level of the active queue isempty, the scheduler looks for the next-highestpriority queue

    n After running all of the active queues, theactive and expired queues are swapped

    n There are pointers to the current arrays; at theend of a cycle, the pointers are switched

  • The Priority Arrays

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    15 / 40

    struct runqueue {struct prioarray *active;

    struct prioarray *expired;

    struct prioarray arrays[2];

    };

    struct prioarray {int nr_active; /* # Runnable */

    unsigned long bitmap[5];

    struct list_head queue[140];

    };

  • Swapping Arrays

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    16 / 40

    struct prioarray *array = rq->active;

    if (array->nr_active == 0) {rq->active = rq->expired;

    rq->expired = array;

    }

  • Why Two Arrays?

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    17 / 40

    n Why is it done this way?n It avoids the need for traditional agingn Why is aging bad?n Its O(n) at each clock tick

  • The Traditional Algorithm

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    18 / 40

    for(pp = proc; pp < proc+NPROC; pp++) {if (pp->prio != MAX)

    pp->prio++;

    if (pp->prio > curproc->prio)

    reschedule();

    }

    Every process is examined, quite frequently(This code is taken almost verbatim from 6thEdition Unix, circa 1976.)

  • Linux is More Efficient

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    19 / 40

    n Processes are touched only when they start orstop running

    n Thats when we recalculate priorities, bonuses,quanta, and interactive status

    n There are no loops over all processes or evenover all runnableprocesses

  • Locking Runqueues

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    20 / 40

    n To rebalance, the kernel sometimes needs tomove processes from one runqueue to another

    n This is actually done by special kernel threadsn Naturally, the runqueue must be locked before

    this happensn The kernel always locks runqueues in order of

    increasing addressn Why? Deadlock prevention! (It is good for

    something. . .

  • Real-Time Scheduling

    Linux SchedulerDescending toReality. . .

    Philosophies

    ProcessorScheduling

    Processor Affinity

    Basic SchedulingAlgorithm

    The Run Queue

    The Highest PriorityProcessCalculatingTimeslices

    Typical Quanta

    Dynamic Priority

    Interactive Processes

    Using Quanta

    Avoiding IndefiniteOvertaking

    The Priority Arrays

    Swapping Arrays

    Why Two Arrays?

    The TraditionalAlgorithm

    Linux is MoreEfficient

    Locking Runqueues

    Real-TimeScheduling

    Sleeping and Waking

    Timers

    21 / 40

    n Linux has soft real-time schedulingn Processes with priorities [0, 99] are real-timen All real-time processes are higher priority than

    any conventional processesn Two real-time scheduling systems, FCFS and

    round-robinn First-come, first-served: process is only

    preempted for a higher-priority process; notime quanta

    n Round-robin: real-time processes at a givenlevel take turns running for their time quantum

  • Sleeping and Waking

    Linux Scheduler

    Sleeping and Waking

    Sleeping and Waking

    Sleeping

    Waking Up aProcessScheduler-RelatedSystem Calls

    Major KernelFunctionsFair ShareScheduling

    Timers

    22 / 40

  • Sleeping and Waking

    Linux Scheduler

    Sleeping and Waking

    Sleeping and Waking

    Sleeping

    Waking Up aProcessScheduler-RelatedSystem Calls

    Major KernelFunctionsFair ShareScheduling

    Timers

    23 / 40

    n Processes need to wait for eventsn Waiting is done by putting the process on a

    wait queuen Wakeups can happen too soon; the process

    must check its condition and perhaps go backto sleep

  • Sleeping

    Linux Scheduler

    Sleeping and Waking

    Sleeping and Waking

    Sleeping

    Waking Up aProcessScheduler-RelatedSystem Calls

    Major KernelFunctionsFair ShareScheduling

    Timers

    24 / 40

    DECLARE_WAIT_QUEUE(wait, current);

    /* Sleep on queue q */

    add_wait_queue(q, &wait);

    while (!condition) {set_current_state(TASK_INTERRUPTIBLE);

    if (signal_pending(current))

    /* handle signal */

    schedule();

    }set_current_state(TASK_RUNNING);

    remove_wait_queue(q, &wait);

  • Waking Up a Process

    Linux Scheduler

    Sleeping and Waking

    Sleeping and Waking

    Sleeping

    Waking Up aProcessScheduler-RelatedSystem Calls

    Major KernelFunctionsFair ShareScheduling

    Timers

    25 / 40

    n You dont wake a process, you wake a waitqueue

    n There may be multiple processes waiting forthe event, i.e., several processes trying to reada single disk block

    n The condition may not, in fact, have beensatisfied

    n Thats why the sleep routine has a loop

  • Scheduler-Related System Calls

    Linux Scheduler

    Sleeping and Waking

    Sleeping and Waking

    Sleeping

    Waking Up aProcessScheduler-RelatedSystem Calls

    Major KernelFunctionsFair ShareScheduling

    Timers

    26 / 40

    nice() Lower a process static prioritygetpriority()/setpriority() Change priorities of

    a process groupsched getscheduler()/sched setscheduler()

    Set scheduling policy and parameters. (Manymore starting with sched ; use man -k tolearn their names.)

  • Major Kernel Functions

    Linux Scheduler

    Sleeping and Waking

    Sleeping and Waking

    Sleeping

    Waking Up aProcessScheduler-RelatedSystem Calls

    Major KernelFunctionsFair ShareScheduling

    Timers

    27 / 40

    scheduler tick() Called each timer tick to up-date quanta

    try to wakeup() Attempts to wake a process,put in on a run queue, rebal-ance loads, etc.

    recalc task prio() update average sleep time andynamic priority

    schedule() Pick the next process to runrebalance tick() Check if load-banlancing is

    needed

  • Fair Share Scheduling

    Linux Scheduler

    Sleeping and Waking

    Sleeping and Waking

    Sleeping

    Waking Up aProcessScheduler-RelatedSystem Calls

    Major KernelFunctionsFair ShareScheduling

    Timers

    28 / 40

    n Suppose we wanted to add a fair sharescheduler to Linux

    n What should be done?n Add a new scheduler type for

    sched setscheduler()

    n Calculate process priority, interactivity, bonus,etc., based on all processes owned by that user

    n How can that be done efficiently? What sortsof new data structures are needed?

  • Timers

    Linux Scheduler

    Sleeping and Waking

    TimersWhy Does theKernel NeedTimers?

    Two Basic Functions

    Timer Types

    Timer Ticks

    JiffiesPotent and EvilMagic

    Time of Day

    Kernel Timers

    Dynamic Timers

    Delay Functions

    System Calls

    29 / 40

  • Why Does the Kernel Need Timers?

    Linux Scheduler

    Sleeping and Waking

    TimersWhy Does theKernel NeedTimers?

    Two Basic Functions

    Timer Types

    Timer Ticks

    JiffiesPotent and EvilMagic

    Time of Day

    Kernel Timers

    Dynamic Timers

    Delay Functions

    System Calls

    30 / 40

    n Animated applicationsn Screen-saversn Time of day for file timestampsn Quanta!

  • Two Basic Functions

    Linux Scheduler

    Sleeping and Waking

    TimersWhy Does theKernel NeedTimers?

    Two Basic Functions

    Timer Types

    Timer Ticks

    JiffiesPotent and EvilMagic

    Time of Day

    Kernel Timers

    Dynamic Timers

    Delay Functions

    System Calls

    31 / 40

    n Time of day (especially as a service toapplications)

    n Interval timers something should happen nms from now

  • Timer Types

    Linux Scheduler

    Sleeping and Waking

    TimersWhy Does theKernel NeedTimers?

    Two Basic Functions

    Timer Types

    Timer Ticks

    JiffiesPotent and EvilMagic

    Time of Day

    Kernel Timers

    Dynamic Timers

    Delay Functions

    System Calls

    32 / 40

    n Real-Time Clock: tracks time and date, even ifthe computer is off; can interrupt at a certainrate or at a certain time. Use by Linux only atboot time to get time of day

    n Time Stamp Counter: ticks once per CPUclock; provides very accurate interval timing

    n Programmable Interval Timer: generatesperiodic interrupts. On Linux, the rate, calledHZ, is usually 1000 Hz (100 Hz on slow CPUs)

    n A variety of special, less common (and lessused) timers

  • Timer Ticks

    Linux Scheduler

    Sleeping and Waking

    TimersWhy Does theKernel NeedTimers?

    Two Basic Functions

    Timer Types

    Timer Ticks

    JiffiesPotent and EvilMagic

    Time of Day

    Kernel Timers

    Dynamic Timers

    Delay Functions

    System Calls

    33 / 40

    n Linux programs a timer to interrupt at acertain rate

    n Each tick, a number of operations are carriedout

    n Three most important

    u Keeping track of timeu Invoking dynamic timer routinesu Calling scheduler tick()

    n The system uses the best timer available

  • Jiffies

    Linux Scheduler

    Sleeping and Waking

    TimersWhy Does theKernel NeedTimers?

    Two Basic Functions

    Timer Types

    Timer Ticks

    JiffiesPotent and EvilMagic

    Time of Day

    Kernel Timers

    Dynamic Timers

    Delay Functions

    System Calls

    34 / 40

    n Each timer tick, a variable called jiffies isincremented

    n It is thus (roughly) the number of HZ sincesystem boot

    n A 32-bit counter incremented at 1000 Hzwraps around in about 50 days

    n We need 64 bits but theres a problem

  • Potent and Evil Magic

    Linux Scheduler

    Sleeping and Waking

    TimersWhy Does theKernel NeedTimers?

    Two Basic Functions

    Timer Types

    Timer Ticks

    JiffiesPotent and EvilMagic

    Time of Day

    Kernel Timers

    Dynamic Timers

    Delay Functions

    System Calls

    35 / 40

    n A 64-bit value cannot be accessed atomicallyon a 32-bit machine

    n A spin-lock is used to synchronize access tojiffies 64; kernel routines callget jiffies 64()

    n But we dont want to have to increment twovariables each tick

    n Linker magic is used to make jiffies thelow-order 32 bits of jiffies 64

    n Ugly!

  • Time of Day

    Linux Scheduler

    Sleeping and Waking

    TimersWhy Does theKernel NeedTimers?

    Two Basic Functions

    Timer Types

    Timer Ticks

    JiffiesPotent and EvilMagic

    Time of Day

    Kernel Timers

    Dynamic Timers

    Delay Functions

    System Calls

    36 / 40

    n The time of day is stored in xtime, which is astruct timespec

    n Its incremented once per tickn Again, a spin-lock is used to synchronize

    access to itn The apparent tick rate can be adjusted

    slightly, via the adjtimex() system call

  • Kernel Timers

    Linux Scheduler

    Sleeping and Waking

    TimersWhy Does theKernel NeedTimers?

    Two Basic Functions

    Timer Types

    Timer Ticks

    JiffiesPotent and EvilMagic

    Time of Day

    Kernel Timers

    Dynamic Timers

    Delay Functions

    System Calls

    37 / 40

    n Two types of timers use by kernel routinesn Dynamic timer call some routine after a

    particular intervaln Delay loops tight spin loops for very short

    delaysn User-mode interval timers are similar to kernel

    dynamic timers

  • Dynamic Timers

    Linux Scheduler

    Sleeping and Waking

    TimersWhy Does theKernel NeedTimers?

    Two Basic Functions

    Timer Types

    Timer Ticks

    JiffiesPotent and EvilMagic

    Time of Day

    Kernel Timers

    Dynamic Timers

    Delay Functions

    System Calls

    38 / 40

    n Specify an interval, a subroutine to call, and aparameter to pass to that subroutine

    n Parameter used to differentiate differentinstantiations of the same timer if you have4 Ethernet cards creating dynamic timers, theparameter is typically the address of theper-card data structure

    n Timers can be cancelled; there is (as usual) adelicate synchronization dance onmultiprocessors. See the text for details

  • Delay Functions

    Linux Scheduler

    Sleeping and Waking

    TimersWhy Does theKernel NeedTimers?

    Two Basic Functions

    Timer Types

    Timer Ticks

    JiffiesPotent and EvilMagic

    Time of Day

    Kernel Timers

    Dynamic Timers

    Delay Functions

    System Calls

    39 / 40

    n Spin in a tight loop for a short time microseconds or nanoseconds

    n Nothing else can use that CPU during thattime, except via interrupt

    n Used only when the overhead of creating adynamic timer is too great for a very shortdelay

  • System Calls

    Linux Scheduler

    Sleeping and Waking

    TimersWhy Does theKernel NeedTimers?

    Two Basic Functions

    Timer Types

    Timer Ticks

    JiffiesPotent and EvilMagic

    Time of Day

    Kernel Timers

    Dynamic Timers

    Delay Functions

    System Calls

    40 / 40

    n time() and gettimeofday()n adjtimex() tweaks apparent clock rate

    (even the best crystals drift; see the RemotePhysical Device Fingerprinting paper from myCOMS E6184 class

    n setitimer() and alarm() interval timersfor applications

    Linux SchedulerDescending to RealityPhilosophiesProcessor SchedulingProcessor AffinityBasic Scheduling AlgorithmThe Run QueueThe Highest Priority ProcessCalculating TimeslicesTypical QuantaDynamic PriorityInteractive ProcessesUsing QuantaAvoiding Indefinite OvertakingThe Priority ArraysSwapping ArraysWhy Two Arrays?The Traditional AlgorithmLinux is More EfficientLocking RunqueuesReal-Time Scheduling

    Sleeping and WakingSleeping and WakingSleepingWaking Up a ProcessScheduler-Related System CallsMajor Kernel FunctionsFair Share Scheduling

    TimersWhy Does the Kernel Need Timers?Two Basic FunctionsTimer TypesTimer TicksJiffiesPotent and Evil MagicTime of DayKernel TimersDynamic TimersDelay FunctionsSystem Calls


Recommended