Predictable Execution: Operating Systems Issues

54
Predictable Execution: Predictable Execution: Operating Systems Issues Operating Systems Issues Michael B. Jones Microsoft Research Joint work with John Regehr University of Virginia

Transcript of Predictable Execution: Operating Systems Issues

Page 1: Predictable Execution: Operating Systems Issues

Predictable Execution:Predictable Execution:Operating Systems IssuesOperating Systems Issues

Michael B. Jones Ñ Microsoft Research

Joint work with

John Regehr Ñ University of Virginia

Page 2: Predictable Execution: Operating Systems Issues

Goal: Coexisting IndependentGoal: Coexisting IndependentReal-Time ApplicationsReal-Time Applications

u Independently developedu Predictable concurrent execution of

Ø real-time and non-real-time apps

u Meeting all appsÕ timing needsØ Informing apps when not possible

Page 3: Predictable Execution: Operating Systems Issues

OverviewOverview

u Developing soft real-time schedulerfor Windows NTØ Described in second half of talk

u Predictability issuesØ How large are observed worst-case

thread scheduling latencies?Ø What causes them?Ø What has been done about them?Ø Described in first half of talk

Page 4: Predictable Execution: Operating Systems Issues

Part I:Part I:

The Problems YouÕre HavingThe Problems YouÕre HavingMay Not Be the Problems YouMay Not Be the Problems You

Think YouÕre Having:Think YouÕre Having:

Results from a Latency StudyResults from a Latency Studyof Windows NTof Windows NT

Page 5: Predictable Execution: Operating Systems Issues

Research ContextResearch Context

u Developing soft real-time schedulerfor Windows NT

u Predictability issues:Ø How large are observed worst-case

thread scheduling latencies?Ø Can they be improved?

u Measured actual latencies

Page 6: Predictable Execution: Operating Systems Issues

NT Latency ResultsNT Latency Results

u Typically can schedule tasks everysmall number of milliseconds

u But ill-behaved drivers, hardware cantake many millisecondsØ Software delays of up to 16ms observedØ Hardware delays of up to 30ms observed

Results from NT 5, Pentium II-333

Page 7: Predictable Execution: Operating Systems Issues

Deferred Procedure CallsDeferred Procedure Calls

u Analogous to Unix bottom halvesu Are preempted by interruptsu Preempt normal threadsu May not blocku Are run in FIFO orderu Typical Uses:

Ø I/O Completion ProcessingØ Background Driver Housekeeping

Page 8: Predictable Execution: Operating Systems Issues

Non-ProblemsNon-Problemsu Interrupts

Ø Interrupt handlers needing substantialwork queue DPCs

Ø Never observed interrupt handlertaking substantial fraction of ms

u Ethernet Packet ProcessingØ With back-to-back 100Mbit incoming

packets of UDP or TCP data:Ø Longest observed DPC 600µsØ Longest delay of user code ~2ms

u Tested four common Ethernet cards

Page 9: Predictable Execution: Operating Systems Issues

Problem: ÒUnimportantÓProblem: ÒUnimportantÓBackground WorkBackground Work

u DEC dc21x4 PCI Fast 10/100 Ethernetu 6ms periodic DPC every 5s

Ø Autosense processingu Most of 6ms in five 0.88ms calls to

routine that reads device register that:Ø Writes a HW register Ð 1.5µsØ Stalls for 5µsØ Writes HW register again Ð 1.5µsØ Stalls for 5µsØ Reads a HW register Ð 1.5µsØ Stalls for 5µs

u And does this 16 times! (once per bit)

Page 10: Predictable Execution: Operating Systems Issues

Another Long DPC: Intel EE 16Another Long DPC: Intel EE 16

u Intel EtherExpress 16 ISA Ethernetu 17ms DPC every 10su Card reset for no received packets

Amusing Observationu Unplugging Ethernet makes latency

worse!Ø Despite conventional wisdom to the

contrary

Page 11: Predictable Execution: Operating Systems Issues

Even Worse: Video CardsEven Worse: Video Cards

u Video cards and drivers conspire tohog the PCI bus

u Dragging large window locks outinterrupts for up to 30ms

u Obliterates sound I/O, for instanceu Can set registry key to ask drivers to

behave, but not defaultØ No problem when set correctly

u ManufacturersÕ motivation:WinBench ~ 5% improvement

Page 12: Predictable Execution: Operating Systems Issues

Video CardVideo CardMisbehavior DetailsMisbehavior Details

u DonÕt check if card FIFO full before writeØ Eliminate a PCI read

u Stalls PCI bus if full to prevent overflowØ Even with AGP, big blits are slow

u Problem observed on:Ø AccelStar II AGPØ Matrox Millenium II

u Several other major cards also do this

Page 13: Predictable Execution: Operating Systems Issues

Example Bug:Example Bug:Multimedia TimersMultimedia Timers

u MP HAL uses 976µs interrupt periodu Multimedia timers compute absolute

time for next wakeup in whole msØ Converted to relative wakeup time and

passed to kernel

u Interrupt occurs just before wakeupØ Timer doesnÕt fireØ Next time, fires twice to catch up

u Fix: compute wakeup in 100ns units

Page 14: Predictable Execution: Operating Systems Issues

Windows Media PlayerWindows Media PlayerAudio DropoutsAudio Dropouts

u Playback glitches when in contentionwith other apps

u Concerted effort to find, fix causesbefore Windows 2000 ship

Page 15: Predictable Execution: Operating Systems Issues

Media Player ThreadMedia Player ThreadStructure (Simplified)Structure (Simplified)

Thread Period (ms) Priority

Kernel Mixer 10 24

MP3 Decoder 100 9

Disk Reader 2000 8

User Interface 45 8

Page 16: Predictable Execution: Operating Systems Issues

MP3 Playback w/o ContentionMP3 Playback w/o Contention

u Working fineu Kmixer thread (bottom) runs every 10msu MP3 decoder (middle) runs every 100ms

Page 17: Predictable Execution: Operating Systems Issues

Priority Inversion Caused ByPriority Inversion Caused ByCompeting ThreadCompeting Thread

u Priority inversion at time 14324msu Busy thread (top) preempts decoder thread

while holding kmixer buffer locku Kmixer (bottom) starves causing audio dropout

u Fix: Raise priority in decoder to that of kmixerbefore acquiring lockØ Manual application of Priority Ceiling Protocol

Page 18: Predictable Execution: Operating Systems Issues

LessonLesson

Your Intuition AboutPerformance is Wrong

Only MeasurementReveals the Truth!

Page 19: Predictable Execution: Operating Systems Issues

Bottom LineBottom Lineu Yes, NT can do RT schedulingu Have done a prototype

Ø But will be of limited value ifunscheduled activities continue takingtens of milliseconds

u NT developed, tested for throughputØ Not small numbers of ms of latencyØ Improvement will require

Ø Systematic latency testingØ Latency requirements specifications

Page 20: Predictable Execution: Operating Systems Issues

Progress Since Initial WorkProgress Since Initial Work

u WHQL tests for video driversØ Verify PCI timing with hardware

u Many timing bugs found, fixedØ E.g., media player priority inversion

u Attempting to institute systematiclatency specifications and testingØ Interrupt hold times & countsØ DPC hold times & countsØ Standards for use of priority values

Page 21: Predictable Execution: Operating Systems Issues

Part II:Part II:

CPU Reservations and TimeCPU Reservations and TimeConstraints: ImplementationConstraints: ImplementationExperience on Windows NTExperience on Windows NT

Page 22: Predictable Execution: Operating Systems Issues

Part II OutlinePart II Outline

IntroductionIntroduction

Rialto BackgroundRialto Background

Windows NT ImplementationWindows NT Implementation

Performance and TracesPerformance and Traces

Related Work and ConclusionsRelated Work and Conclusions

Page 23: Predictable Execution: Operating Systems Issues

What We DidWhat We Didu Created Rialto/NT

Ø Based on Windows 2000Ø Added CPU Reservations & Time Constraints

Ø Abstractions originally developed withinRialto real-time system at MicrosoftResearch

u WhatÕs newØ Coexistence with Windows NT schedulerØ Multiprocessor capabilityØ Periodic clock

Ø As opposed to fine-grained individuallyscheduled interrupts

Page 24: Predictable Execution: Operating Systems Issues

Real-TimeReal-Time

u Real-time computations have deadlinesu Examples

Ø Fly-by-wire aircraft:Ø Missed deadline may endanger the aircraft

Ø Soft modem:Ø Missed deadline may reset the connection

Ø Video conferencing:Ø Missed deadline degrades audio or video

quality

Page 25: Predictable Execution: Operating Systems Issues

Why not use Windows NT as is?Why not use Windows NT as is?u Real-time using priorities requires global

coordinationØ Windows is an open system

Ø Priority inflationØ No path for timing information

u There are scheduling algorithms that donot require global coordinationØ CPU Reservations and Time ConstraintsØ Apps state timing requirements directlyØ Independently developed apps can run

concurrently

Page 26: Predictable Execution: Operating Systems Issues

Teaser CapabilityTeaser Capabilityu Apps can ask scheduler:

Ø ÒCan I do 5ms of work betweennow+30ms and now+40ms?Ó

u Scheduler answers either:Ø ÒI guarantee itÓ orØ ÒYou probably canÕtÓ

u Guaranteeing this 5ms work in future10ms interval does not require reserving50% of CPU for next 40ms

Page 27: Predictable Execution: Operating Systems Issues

How did we do it?How did we do it?

u Explicitly represent future time

u Map app declarations of timingneeds into grants of future time

Enables:

u Advance guarantees to applications, or

u Denial of requests up front

Page 28: Predictable Execution: Operating Systems Issues

IntroductionIntroduction

Rialto BackgroundRialto Background

Windows NT ImplementationWindows NT Implementation

Performance and TracesPerformance and Traces

Related Work and ConclusionsRelated Work and Conclusions

Page 29: Predictable Execution: Operating Systems Issues

Abstraction: CPU ReservationAbstraction: CPU Reservation

u Guaranteed execution rate andgranularity for a threadØ X units of time out of every Y units, e.g.

Ø 1ms every 5ms

Ø 7.5ms every 33.3ms

Ø one second every minute

Page 30: Predictable Execution: Operating Systems Issues

Abstraction: Time ConstraintAbstraction: Time Constraint

u Deadline-based thread executionØ Guarantees execution within interval, orØ Proactively denies constraint request

schedulable = BeginConstraint (time_interval, estimate);if (schedulable) then

Do normal work under constraintelse

Transient overload -- shed load if possibletime_taken = EndConstraint ();

Page 31: Predictable Execution: Operating Systems Issues

Implementation:Implementation:Precomputed Scheduling PlanPrecomputed Scheduling Planu Tree-based periodic map of time

Ø Supports widely varying periods

u Allocation of future time intervalsØ Ongoing for CPU ReservationsØ One-shot for Time Constraints

u Enables efficient:Ø Scheduling decisionsØ Feasibility analysis for constraintsØ Guarantees for reservations, constraints

Page 32: Predictable Execution: Operating Systems Issues

Scheduling Plan ExampleScheduling Plan Example

Thread A B C D E F Amount 4ms 3ms 2ms 1ms 1ms 5ms Period 20ms 10ms 40ms 20ms 10ms 40ms

3 : B 1: E

4: A 2: free

1: D

3: free2: C

5: F

Page 33: Predictable Execution: Operating Systems Issues

IntroductionIntroduction

Rialto BackgroundRialto Background

Windows NT ImplementationWindows NT Implementation

Performance and TracesPerformance and Traces

Related Work and ConclusionsRelated Work and Conclusions

Page 34: Predictable Execution: Operating Systems Issues

Using the Windows NTUsing the Windows NTSchedulerScheduler

u Rialto/NT uses existing priorityscheduler to schedule its threadsØ Rialto/NT elevates thread priorities to

cause dispatching

u Existing apps, abstractions work asbefore

u Windows NT scheduler also canschedule Rialto/NT threads

Page 35: Predictable Execution: Operating Systems Issues

Multiprocessor IssuesMultiprocessor Issuesu One scheduling plan per processor

Ø Tree walking happens on all plansØ Heuristic: add new reservation to plans in

increasing order of processor utilization

u Plans not pinned to particular CPUsØ Allow NT scheduler to choose CPUØ Rely on schedule properties, affinity to

run threads mostly on same CPUØ Permits opportunistic scheduling on other

processors by existing scheduler

Page 36: Predictable Execution: Operating Systems Issues

Affinity vs. PriorityAffinity vs. Priorityu Rialto/NT counts on priority elevation

to cause thread dispatchingØ No contention because at most one

elevated (Rialto/NT) thread per CPU

u On MP highest priority threads notalways scheduledØ Heuristics sometimes elevate thread

affinity over thread priority

u Changed scheduler to immediatelydispatch Rialto/NT elevated-prioritythreads when ready

Page 37: Predictable Execution: Operating Systems Issues

Discrete TimeDiscrete Time

u Windows NT clock interrupts onperiodic basisØ Typically 10-15ms, HAL-dependentØ Can usually be set to 1ms period

u Discrete interrupts limit enforceablescheduling granularity

u So, Rialto/NT:Ø Initializes interrupt period to 1msØ Aligns rescheduling with clock interrupts

Page 38: Predictable Execution: Operating Systems Issues

Implementation DetailsImplementation Details

u Reschedule runs in DPC contextØ Use NT kernel timers to schedule DPCs

u Rialto/NT threads run at priority 30Ø Second highest Windows NT priorityØ Other values could be chosen

u New plans for reservations computedin requesting thread contextØ Optimistic concurrency control to avoid

perturbing existing schedule

Page 39: Predictable Execution: Operating Systems Issues

Non-invasive ImplementationNon-invasive Implementation

u Easier to argue correctnessu Modified only two kernel routines

Ø Changed behavior of one

u Added to the kernel:Ø 6000 lines of CØ 4 system calls

Page 40: Predictable Execution: Operating Systems Issues

Complication: UnpredictableComplication: UnpredictableDispatch LatencyDispatch Latency

u When latency occurs we:Ø Penalize the running threadØ Keep the schedule on time

u Causes of scheduling latency:Ø Interrupt handlersØ Kernel code running at high IRQLØ Long DPCs

u Latencies controllable throughconcerted latency testing discipline

Page 41: Predictable Execution: Operating Systems Issues

Better Living ThroughBetter Living ThroughSimulationSimulation

u Rialto/NT runs in simulator inaddition to kernelØ Exactly the same sources

u Makes some debugging much easierØ Reproducible runsØ Better toolsØ No race conditionsØ Reboot time not in critical path

Page 42: Predictable Execution: Operating Systems Issues

IntroductionIntroduction

Rialto BackgroundRialto Background

Windows NT ImplementationWindows NT Implementation

Performance and TracesPerformance and Traces

Related Work and ConclusionsRelated Work and Conclusions

Page 43: Predictable Execution: Operating Systems Issues

Test PlatformTest Platform

u 333 MHz Pentium II PCsØ 128MB RAMØ Intel EtherExpress ProØ Adaptec SCSI

u Single- and dual-processor tests

Page 44: Predictable Execution: Operating Systems Issues

Context Switch TimeContext Switch Time

u Tested:Ø 10 threads on released Windows 2000 beta 3Ø 10 Rialto/NT threads with CPU Reservations

u Rialto/NT adds 8µs to median contextswitch timeØ 0.8% overhead at 1ms scheduling

granularity

Page 45: Predictable Execution: Operating Systems Issues

Time to Acquire ReservationsTime to Acquire ReservationsReasonable even in pathological cases

Times to begin simultaneous reservations

0

1

2

3

4

5

0 10 20 30 40 50 60

Reservation Number

Tim

e to

Mak

e R

eser

vati

on

(ms

)

Uniprocessor Dual-processor

Page 46: Predictable Execution: Operating Systems Issues

Time to Acquire ConstraintsTime to Acquire ConstraintsReasonable even in pathological cases

Times to begin simultaneous constraints

0

10

20

30

40

0 10 20 30 40 50 60Time Constraint Number

Co

nst

rain

t A

cqu

ire

Tim

e (µ

s)

Without Reservations With Reservations

Page 47: Predictable Execution: Operating Systems Issues

Reservations with aReservations with aBackground ThreadBackground Thread

Threads run only during time assigned to theirreservations

1 processor, 3 threads with reservations, 1 high-priority competitor thread

Page 48: Predictable Execution: Operating Systems Issues

Reservations and ConstraintsReservations and Constraints

Thread 3 gains additional time with constraints

1 processor, 3 threads with reservations (one alsousing constraints), 1 high-priority competitorthread

Page 49: Predictable Execution: Operating Systems Issues

Dual Processor TracesDual Processor TracesWithout affinity change: thread 3 not always scheduled

With affinity change: all threads properly scheduled

Page 50: Predictable Execution: Operating Systems Issues

IntroductionIntroduction

Rialto BackgroundRialto Background

Windows NT ImplementationWindows NT Implementation

Performance and TracesPerformance and Traces

Related Work and ConclusionsRelated Work and Conclusions

Page 51: Predictable Execution: Operating Systems Issues

Related WorkRelated Worku Real-time add-ins for Windows NT

Ø RTX from VenturCom, INtime from RadiSys,Hyperkernel from Imagination Systems

u Lin et. al Õ98Ø Windows NT soft real-time scheduling server

u Candea & Jones Õ98Ø Vassal loadable scheduler framework

u Lots of reservation- and deadline-basedscheduling work

Page 52: Predictable Execution: Operating Systems Issues

Further ResearchFurther Research

u Evaluate when applied to real appsØ Some work submitted to RTAS 2000

u Lots of policy issuesØ Resource managementØ Placement of reservations among CPUs

Page 53: Predictable Execution: Operating Systems Issues

ConclusionsConclusions

u Scheduling plan effective on MPsu Plan adapted to use periodic clocku New scheduler cooperatively coexists

with, uses Windows NT scheduleru Rialto/NT brings CPU Reservations

and Time Constraints to Windows NT

Page 54: Predictable Execution: Operating Systems Issues

Thanks for Your Invitation!Thanks for Your Invitation!u References:

Ø Latency study published in 1999 RTASØ Rialto/NT published in 1999 USENIX

Windows NT Symposium

u For more on this research seehttp://research.microsoft.com/~mbj/

u For a great priority inversion story besure to follow the What really happenedon Mars? link