Scheduler activations Landon Cox March 8 2016 What

  • Slides: 49
Download presentation
Scheduler activations Landon Cox March 8, 2016

Scheduler activations Landon Cox March 8, 2016

What is a process? • Informal • A program in execution • Running code

What is a process? • Informal • A program in execution • Running code + things it can read/write • Process ≠ program • Formal • ≥ 1 threads in their own address space

Threads that aren’t running • What is a non-running thread? • thread=“sequence of executing

Threads that aren’t running • What is a non-running thread? • thread=“sequence of executing instructions” • non-running thread=“paused execution” • Must save thread’s private state • To re-run, re-load private state • Want thread to start where it left off

Thread control block (TCB) • What needs to access threads’ private data? • The

Thread control block (TCB) • What needs to access threads’ private data? • The CPU • This info is stored in the PC, SP, other registers • The OS needs pointers to non-running threads’ data • Thread control block (TCB) • Container for non-running threads’ private data • Values of PC, code, SP, stack, registers

Thread control block Address Space TCB 1 TCB 2 TCB 3 PC PC SP

Thread control block Address Space TCB 1 TCB 2 TCB 3 PC PC SP SP registers Code Stack PC Ready queue SP Thread 1 running PC SP registers CPU

Thread control block Address Space Ready queue TCB 2 TCB 3 PC PC SP

Thread control block Address Space Ready queue TCB 2 TCB 3 PC PC SP SP registers Stack Code Stack Thread 1 running PC SP registers CPU

Switching threads • What needs to happen to switch threads? 1. Thread returns control

Switching threads • What needs to happen to switch threads? 1. Thread returns control to scheduler • For example, via timer interrupt 2. Scheduler chooses next thread to run 3. Scheduler saves state of current thread • To its thread control block 4. Scheduler loads context of next thread • From its thread control block 5. Jump to PC in next thread’s TCB

Kernel vs user threads • Kernel threads • Scheduler and queues reside in the

Kernel vs user threads • Kernel threads • Scheduler and queues reside in the kernel • User threads • Scheduler and queues reside in user space

32 -bit address space 4 GB Kernel data (same for all processes) 3 GB

32 -bit address space 4 GB Kernel data (same for all processes) 3 GB (0 xc 0000000) User data (different for every process) 0 GB Virtual memory

Switching threads 4 GB 3 GB Where is the interrupt handler code? In the

Switching threads 4 GB 3 GB Where is the interrupt handler code? In the kernel 0 GB Virtual memory

Switching threads 4 GB SP PC Interrupthandler code 3 GB On which stack is

Switching threads 4 GB SP PC Interrupthandler code 3 GB On which stack is the IRQ handled? A kernel stack 0 GB Virtual memory

Switching threads 4 GB SP PC Interrupthandler code 3 GB Where are threads’ stacks?

Switching threads 4 GB SP PC Interrupthandler code 3 GB Where are threads’ stacks? In user space 0 GB Virtual memory

Switching threads 4 GB SP PC Interrupthandler code 3 GB Where are threads’ stacks?

Switching threads 4 GB SP PC Interrupthandler code 3 GB Where are threads’ stacks? In user space 0 GB Virtual memory

Switching threads 4 GB SP PC Interrupthandler code 3 GB So far, everything is

Switching threads 4 GB SP PC Interrupthandler code 3 GB So far, everything is the same for user-level and kernel threads 0 GB Virtual memory

Kernel threads Scheduler code For kernel threads, the thread scheduler and TCB queues are

Kernel threads Scheduler code For kernel threads, the thread scheduler and TCB queues are in the kernel Handler Interruptcode handler code TCB TCB

Kernel threads Scheduler code Where is the lock/cv code? Handler Interruptcode handler code TCB

Kernel threads Scheduler code Where is the lock/cv code? Handler Interruptcode handler code TCB TCB

Kernel threads Where is the lock/cv code? Also, in the kernel Handler Interruptcode handler

Kernel threads Where is the lock/cv code? Also, in the kernel Handler Interruptcode handler code Scheduler code Synchron. code TCB TCB

User-level threads For user-level threads, the thread scheduler and queues are in user space

User-level threads For user-level threads, the thread scheduler and queues are in user space Handler Interruptcode handler code TCB TCB Scheduler code

User-level threads Where is the lock/cv code? Handler Interruptcode handler code Also, in the

User-level threads Where is the lock/cv code? Handler Interruptcode handler code Also, in the user space TCB TCB Scheduler code Synchron. code

Switching to a new user-level thread

Switching to a new user-level thread

User-level threads SP Handler Interruptcode handler code PC TCB Thread running user code TCB

User-level threads SP Handler Interruptcode handler code PC TCB Thread running user code TCB Program code Scheduler code Synchron. code

User-level threads SP PC Handler Interruptcode handler code Timer interrupt! TCB Program code Scheduler

User-level threads SP PC Handler Interruptcode handler code Timer interrupt! TCB Program code Scheduler code Synchron. code

User-level threads SP PC Handler Interruptcode handler code Does the kernel know where user

User-level threads SP PC Handler Interruptcode handler code Does the kernel know where user left off? Yes, CPU stores this on kernel stack TCB Program code Scheduler code Synchron. code

User-level threads SP PC Handler Interruptcode handler code What does kernel do next? Depends

User-level threads SP PC Handler Interruptcode handler code What does kernel do next? Depends on meaning of the timer … TCB Program code Scheduler code Synchron. code

User-level threads SP PC Handler Interruptcode handler code Assuming it’s a signal for the

User-level threads SP PC Handler Interruptcode handler code Assuming it’s a signal for the process? Kernel delivers signal to process TCB Program code Scheduler code Synchron. code

User-level threads SP PC To deliver a signal to user code: make it look

User-level threads SP PC To deliver a signal to user code: make it look like a forced function call to signal handler. Handler Interruptcode handler code TCB Program code Scheduler code Synchron. code

User-level threads SP PC Handler Interruptcode handler code Where is the process’s timer signal

User-level threads SP PC Handler Interruptcode handler code Where is the process’s timer signal handler? In scheduler code (e. g. , yield) TCB Program code Scheduler code Synchron. code

User-level threads SP PC On what stack should the scheduler code run? Stack of

User-level threads SP PC On what stack should the scheduler code run? Stack of interrupted thread Handler Interruptcode handler code TCB yield Program code Scheduler code Synchron. code

User-level threads PC SP On what stack should the scheduler code run? Stack of

User-level threads PC SP On what stack should the scheduler code run? Stack of interrupted thread Handler Interruptcode handler code TCB yield Program code Scheduler code Synchron. code

User-level threads PC SP Could the kernel transfer control to any other thread? No,

User-level threads PC SP Could the kernel transfer control to any other thread? No, it’s only aware of interrupted one Handler Interruptcode handler code TCB yield Program code Scheduler code Synchron. code

Example user-thread library.

Example user-thread library.

Switching to a new kernel thread

Switching to a new kernel thread

Kernel threads SP Handler Interruptcode handler code PC Thread running user code Scheduler code

Kernel threads SP Handler Interruptcode handler code PC Thread running user code Scheduler code Synchron. code TCB TCB Program code

Kernel threads SP PC Handler Interruptcode handler code Scheduler code Synchron. code TCB TCB

Kernel threads SP PC Handler Interruptcode handler code Scheduler code Synchron. code TCB TCB Timer interrupt! Program code

Kernel threads SP PC Handler Interruptcode handler code Scheduler code Synchron. code TCB TCB

Kernel threads SP PC Handler Interruptcode handler code Scheduler code Synchron. code TCB TCB Which thread could run next? Any thread. Kernel aware of all of them Program code

Advantages of user-level threads • Synchronization primitives are just a function call • Why

Advantages of user-level threads • Synchronization primitives are just a function call • Why are kernel threads more expensive? • Accessing thread management must cross kernel boundary • CPU protection checks, argument checks, handler dispatch, etc. • Switching back to thread also crosses kernel boundary • For user threads, easy to tune scheduling policy to application • Why is this harder for kernel threads? • All processes share the same scheduler • Per-process policies require wider, more complex API

Advantage of kernel threads SP PC Handler Interruptcode handler code Which thread could run

Advantage of kernel threads SP PC Handler Interruptcode handler code Which thread could run next? Only the one that was interrupted TCB Program code Scheduler code Synchron. code

Advantage of kernel threads SP PC Handler Interruptcode handler code Why might we be

Advantage of kernel threads SP PC Handler Interruptcode handler code Why might we be unable to run the interrupted thread? Thread could have made a blocking system call TCB Program code Scheduler code Synchron. code

Advantage of kernel threads SP PC Handler Interruptcode handler code TCB Program code Scheduler

Advantage of kernel threads SP PC Handler Interruptcode handler code TCB Program code Scheduler code Synchron. code

Advantage of kernel threads SP PC Handler Interruptcode handler code Why is this wasteful?

Advantage of kernel threads SP PC Handler Interruptcode handler code Why is this wasteful? There are other threads that could run! Kernel doesn’t know they exist TCB Program code Scheduler code Synchron. code

Single-threaded app User App Server Pull screen Refresh request New messages request Check messages

Single-threaded app User App Server Pull screen Refresh request New messages request Check messages Send messages Horrible lag Recv messages Refresh UI Tap screen • Easy to program, but painfully slow (why? )

Multi-threaded app User App (UI) App (bg) Server Pull screen Refresh request Msg request

Multi-threaded app User App (UI) App (bg) Server Pull screen Refresh request Msg request Tap screen Refresh UI (tap) Recv Update msgs Refresh UI (pull) • Can overlap I/O and other work (e. g. , UI) Check msgs Send msgs

Multi-threaded app User App (UI) App (bg) Server Pull screen Refresh request Msg request

Multi-threaded app User App (UI) App (bg) Server Pull screen Refresh request Msg request Tap screen Check msgs Refresh UI (tap) Send msgs Recv Update msgs Refresh UI (pull) Methods on the UI thread must be fast. Why? Anything slow should run on a background thread

Scheduler activations • Problem with user-level threads • Kernel may not find a thread

Scheduler activations • Problem with user-level threads • Kernel may not find a thread on which to run scheduler • Happens when thread blocks waiting for I/O • This is exactly when you want to run other threads! • Main ideas • • • Kernel assigns user process N virtual processors A virtual processor corresponds to a kernel thread Each kernel thread contains a scheduler activation (initial upcall) Kernel invokes scheduler activation when running a thread Scheduler activations (user-level code) schedule user-level threads Kernel tells user-level code about blocked and pre-empted threads

Blocking I/O example T 1: two virtual processors, run user scheduler on each stack

Blocking I/O example T 1: two virtual processors, run user scheduler on each stack

Blocking I/O example T 2: thread 1 blocks in kernel, create new kernel thread

Blocking I/O example T 2: thread 1 blocks in kernel, create new kernel thread notifying user code thread 1 is blocked. User threads can run in context of new kernel thread.

Blocking I/O example T 3: I/O completes, and pre-empts thread 2. kernel creates new

Blocking I/O example T 3: I/O completes, and pre-empts thread 2. kernel creates new kernel thread to indicate that thread 2 was pre-empted and thread 1 can resume.

Blocking I/O example T 4: with the remaining kernel threads, userlevel code decides whether

Blocking I/O example T 4: with the remaining kernel threads, userlevel code decides whether to run thread 1 or 2, or some other threads.

Course administration • Research projects • Ideally, work on an ongoing project from your

Course administration • Research projects • Ideally, work on an ongoing project from your own research • Otherwise, ask me for suggestions • Timeline for research projects • Proposal due Friday, March 24 • Status report due Wed, April 12 • Final report due Wed, April 19 (plus demo/presentation)