CSE3320 Operating Systems I/O Devices
Transcript of CSE3320 Operating Systems I/O Devices
CSE 3320OperatingSystems
I/ODevices
Jia RaoDepartmentofComputerScience and Engineering
http://ranger.uta.edu/~jrao
RecapofPreviousClasseso CPUscheduling
o Memorymanagement
o FileSystems
o Thischapterà I/Odevices(e.g.,harddisks)
I/OofanOSMainFunction:Controlallthecomputer’sI/Odevices
o Issuecommandstothedevices,catchinterrupts,andhandleerrors
o Provideinterfacebetweenthedevicesandtherestofsystemthatissimpleandeasytouse
WhatwecareI/Odevicesmostassoftwaredesignersandprogrammers?
Theinterfacepresentedtous:(1)Thecommandsthehardwareaccepts(2)Thefunctionsitcarriesout(3)Theerrorsthatcanbereportedback
PrinciplesofI/OHardware:I/ODevices
Sometypicaldevice,network,anddatabaserates
• Blockdevices• Characterdevices• Others
DeviceControllers• I/Odeviceshavetwocomponents:
o mechanicalcomponento electroniccomponent
• Theelectroniccomponentisthedevicecontrollero maybeabletohandlemultiplebutidenticaldevices
• Controller'staskso convertserialbitstreamtoblockofbyteso performerrorcorrectionasnecessaryo makeblocksavailabletomainmemory
buffer/cache
Devicecontroller
I/ODevice
Standardinterface:IDE,SCSI
register
CPUàI/O:SpecialI/OInstructions• HowdoestheCPUcommunicatewiththecontrolregistersandthedevicedata
buffers?Twomethodsareavailable.• MethodI:Special(assembly)I/OInstructions(IBM360)
o EachcontrolregisterisassignedanI/Oportnumber(device#)INREG,PORT
OUTPORT,REG
o ThememoryaddressspaceandI/OaddressspacearedifferentINR0,4 ;port4
MOVR0,4 ;memoryword4
CPUàI/O:Memory-mappedI/O° MethodII:Memory-mappedI/O(Pentium)
• Eachregisterisassignedauniquememoryaddresstowhichnomemoryisassigned;read/writetoaddressesareI/Ooperations
• Howitworks?
CPUàI/O:Memory-mappedI/O(2)° AdvantagesofMemory-mappedI/O
• AnI/OdevicedrivercanbewritteninC,insteadofassembly• Protectioniseasy(byaddresstranslation);userprogramsarepreventedfrom
issuingI/Ooperationsdirectly- ThatI/O-mappedspacenotinanyuser’svirtualaddr.space
(a)SeparateI/Oandmemoryspace;(b)Memory-mappedI/O;(c)Hybrid(PDP-11) (Pentium)
CPUàI/O:Memory-mappedI/O(3)° DisadvantagesofMemory-mappedI/O
• Cachingadevicecontrolregisterwouldbedisastrous• BothmemoryandallI/Odevicesmustexamineallmemoryreferencestosee
whichonestorespondto,noteasyifbushierarchy
(a)Asingle-busarchitecture;(b)Adual-busmemoryarchitecture
Howtoovercomethedisadvantages?Selectivecachingandmore...
CPUàI/O:Memory-mappedI/O(4)• PCIbridgechipfiltersaddressestodifferentbuses
I/Oà CPU:DeviceNotifyingtheCPU• TheOSneedstoknowwhen:
o TheI/Odevicehascompletedanoperationo TheI/Ooperationhasencounteredanerror
• Thiscanbeaccomplishedintwodifferentways:o Polling(BusyWaiting):
} TheI/Odeviceputinformationinastatusregister} TheCPUperiodicallyorcontinuouslycheckthestatusregister
o I/OInterrupt:} WheneveranI/Odeviceneedsattentionfromtheprocessor,
itinterruptstheprocessorfromwhatitiscurrentlydoing.
Inreal-timesystems,ahybridapproachisoftenused} UseaclocktoperiodicallyinterrupttheCPU,atwhichtimetheCPUpollsallI/Odevices
DataTransfer:ProgrammedI/O(BusyWaiting)
CPU
IOC
device
Memory
Is thedata
ready?
readdata
storedata
yes no
done? noyes
busywaitloopnotanefficient
waytousetheCPUunlessthedevice
isveryfast!
• Advantage:o Simple:theprocessoristotallyincontrolanddoesallthework
• Disadvantage:o PollingoverheadcanconsumealotofCPUtime
• Advantage:o Userprogramprogressisonlyhaltedduringactualtransfer
• Disadvantage,specialhardwareisneededto:o Causeaninterrupt(I/Odevice)o Detectaninterrupt(processor)o Savetheproperstatestoresumeaftertheinterrupt(processor)
DataTransfer:InterruptDrivenaddsubandornop
readstore...rtimemory
userprogram(1) I/O
interrupt
(2) save PC
(3) interruptservice addr
interruptserviceroutine(4)
CPU
IOC
device
Memory
:
DirectMemoryAccess(DMA)° DMAcontrollerhasaccesstosystembusindependentofCPU
• Amemoryaddressregister• Abytecountregister• Acontrolregister(direction,transferunit,andtransfermode)• Multiplereg.setsifmultipletransferatonce
OperationofaDMAtransfer
DMATransferModes° Cyclestealingmode:word-at-a-time,sneaksinandstealsanoccasional
buscyclefromtheCPUonceinawhile
° Burstmode:asksI/Odevicetoacquirethebus,issueaseriesoftransfers,thenreleasethebus
° Tradeoff:I/OefficiencyandCPUblockedWhyneedaninternalbufferatthediskcontroller?
Checksumcalculationandbusblocking
InterruptsRevisited° OnewayofhowI/OnotifiesCPU
• Interruptvectorandinterruptserviceprocedures• Priorityifmultiplesimultaneousinterrupts• CPUdelaysACKuntilitisreadytohandlethenextinterruptisawaytoavoid
raceconditions involvingmultiplealmostsimultaneousinterrupts
GoalsofI/OSoftware• Deviceindependence
o WriteprogramsthatcanaccessanyI/Odevicewithoutspecifyingdeviceinadvance
· (floppy,harddrive,orCD-ROM)
• Uniformnamingo nameofafileordeviceastringoraninteger(e.g.,pathname)o notdependingonwhichmachine/deviceo UNIXMountSystem
• Errorhandlingo handleasclosetothehardwareaspossible,i.e.,controllerfirst
thendevicedrivernext
GoalsofI/OSoftware(2)• Synchronousvs.asynchronoustransfers
o blockedtransfersvs.interrupt-driven
o Logicallysynchronoustoprogrammersandphysicallyasynchronous
• Buffering
o datacomingoffadevicecannotbestoreddirectly infinaldestination,e.g.,network-I/Ooperations
} Forchecksumandtimingconstraints
• Sharablevs.dedicateddevices
o disksaresharable
o tapedriveswouldnotbe
o dedicationintroducesdeadlockWhatarethreefundamentaldifferentwaysofperformingI/O?
ProgrammedI/O° Polling(busywaiting)
• CPUcontinuouslypollsthedevice(register)toseeifitisready• SimplebutCPUtimewasteful
Stepsinprintingastring
ProgrammedI/O(2)
WritingastringtotheprinterusingprogrammedI/O
Whatiftheprintercanprint100character/sec?
CPUwouldbeidlefor10mseachloopiteration!Whynotswitchingtoanotherprocess?
Interrupt-DrivenI/O
Writingastringtotheprinterusinginterrupt-drivenI/O(a)Codeexecutedwhenprintsystemcallismade(b)Interruptserviceprocedure
° AwaytoallowtheCPUtodosomethingelsewhilewaitingfortheprintertobecomeready
Still,aninterruptoccurseverycharacter,whattodo?
I/OUsingDMA
• PrintingastringusingDMA
o codeexecutedwhentheprintsystemcallismade
o interruptserviceprocedure
° Awaytoreducethenumberofinterruptsfromonepercharactertooneperbufferprinted• Transfersablockofdatadirectlytoorfrommemory• Aninterruptissentwhenthetaskiscomplete• Theprocessorisonlyinvolvedatthebeginningandendofthetransfer
I/OSoftwareHierarchy
LayersoftheI/OSoftwareSystem
Oneobjective:aslittleofOSaspossibleknowsabouttheinterrupts
Howaboutthefollowingjobs:(1) Checkiftheuserispermittedtousethedevice(2) Writingcommandstothedeviceregisters(3) Formattingtheoutputdataforprinting
OSInterruptHandling
• Interruptvectoro containstheaddressoftheinterruptserviceprocedures
o Jumptable
SkeletonofwhatlowestlevelofOSdoeswhenaninterruptoccurswhenaprocessisrunning
InterruptHandlers• Interrupthandlersarebesthidden
o havedriverstartinganI/Ooperationblockuntilinterruptnotifiesofcompletion
• Interruptproceduredoesitstask,handlinginterruptso thenunblocksdriverthatstartedit
• Butactually,manyOSstepsmustbeperformedinsoftwareafterhardwareinterrupt hascompleted
1. Saveregs notalreadysavedbyinterrupthardware2. Setupcontextforinterruptserviceprocedure3. ……
DeviceDrivers
• Logicalpositionofdevicedriversisshownhere• Communicationsbetweendriversanddevicecontrollersgooverthebus
° Devicedrivers:providedbydevice’smanufacturer,device-specificcodeforcontrollingthedevice(viadevicecontrollerregisters)
DeviceDriversInstallationHowtoinstalldevicedriverstoOSkernel?• Staticinstallationandrecompiling(Unix)
o Recompilethekernelwiththenewdriver• Staticregistrationandrebooting(earlyWindows)
o MakeanentryinanOSfileandrebootthesystem• Dynamicinstallation(Windows&Unix)
o On-the-flyloadingintothesystemduringtheexecution
Device-IndependentI/OSoftware° ThebasicfunctionistoperformtheI/Ofunctionsthatarecommontoall
devicesandtoprovideauniforminterfacetotheuser-levelsoftware
Functionsofthedevice-independentI/Osoftware
Uniform interfacing for device drivers
Buffering
Error reporting
Allocating and releasing dedicate devices
Providing a device-independent block size
AnExampleofDevice-independentI/OSoftware
Buffering° (a)Unbuffered input:doesn’tithavetoomanyinterrupts?
° (b)Bufferinginuserspace:Whathappensifthebufferispagedout (todisk)whenacharacterarrives?Gopinning?
° (c)Bufferinginthekernelfollowedbycopyingtouserspace
• Whathappenstocharactersthatarrivewhilethepagewiththeuserbufferisbeingbrought(inmemory)fromthedisk?
° (d)Doublebuffering inthekernel;twobufferstaketurns.
DisadvantagesofBuffering° Whytheprocessdoesnotcopythedatatothewiredirectlyfromkernel
memory?• Buthowabouttheperformance?
- Whynotbit-by-bitfromthekernelbuffertothenetwork?
Networkingmayinvolvemanycopiesofapacket,andallstepsmusthappensequentially
User-SpaceI/OSoftware
LayersoftheI/Osystemandthemainfunctionsofeachlayer
° SomeofI/Osoftwareconsistsofuser-spacelibraries• ThestandardI/OlibrarycontainsanumberofproceduresthatinvolvewithI/O
andallrunaspartofuserprograms
- E.g.,scanf(),printf()forI/Oformatting,IntelDPDKforhigh-speedNICs
HardDisks
• Theconventionalsector-track-cylindermodelo Astackofplatters,asurfacewithamagneticcoatingo Magneticdisksareorganizedintocylinders,forS/R/W
• Overlappedseeks:onediskcontrollerseeks2+drivessimultaneouslyo Transferbetweenthediskandthecontrollercanbesimultaneouso Butonetransferbetweenthecontrollerandthemainmemory
Sector
Track
Three-StageDiskR/WProcess• Three-stageDiskR/Wprocess:timerequiredtoreadorwriteadisk
blockdeterminedmainlyby3factors1. Seektime2. Rotationaldelay3. Actualtransfertime
• Seektimedominates
DiskAccessTime=Seektime+RotationalLatency+Transfertime+ControllerTime+QueuingDelay
Howtooptimizeseektime?Diskarmschedulingalgorithms
SSFDiskArmSchedulingAlgorithm° Howtooptimizeseektimeifatableavailable,indexedbycylinder
number,withallthependingdiskrequests foreachcylinderchainedinalinkedlist headedbythetableentries?• Example:11(c),1,36,16,34,9,12byFCFSandSSF
• SSFcutsaverageseektimeofFCFSalmostinhalf(111à61)
ShortestSeekFirst(SSF)diskschedulingalgorithm
Initialposition
Pendingrequests
WhatisthekeyproblemwithSSF?Howwouldyoudealwithit?
TheElevatorDiskArmSchedulingAlgorithm
Theelevatoralgorithmforschedulingdiskrequests
° Elevatoralgorithm:keepmovinginthesamedirectionuntiltherearenomoreoutstanding requestsinthatdirection• Onecurrentdirectionbit:UPandDown• Property:givenanycollectionofrequests,theupperboundonthetotal
motionisfixed:twicethenumberofcylinders
• Example:11(c),1,36,16,34,9,12bytheelevator
ErrorHandling
Controllerdoessectorsubstitution OSdoesstep(c)• Adisktrackwithabadsector.• Substitutingaspare forthebadsector• Shifting allthesectorstobypassthebadone
° Twogeneralapproachestodealwithbadblocks/sectors• diskcontrollerbasedvs.OSbased
Clocks(Timers)° Clockhardware:generateinterruptsatknownintervals
• Aprogrammableclock:itsinterruptfrequencycanbecontrolledbysoftware
• A500MHzcrystalwitha32-bitregistercangenerateinterruptevery2nsto8.6s
Aprogrammableclock
ClockSoftware° ClockSoftware(clockdriver):
• Maintainingthetimeofday
• supporttime-sharedscheduling
• AccountingCPUusage
• Handlingthealarm systemcall
• …
MaintainingtheTimeofDay
Threewaystomaintainthetimeofday
(a)Using64-bitscounter(b)Recordingtimeintermsofsecondswithasecondcounterinticks(c) Relativetosystemboottime,insteadofafixedexternalmoment
° Howaboutincrementingacounterateachclocktick?• Givenaclockrateof60Hz,a32-bitcounterlastabout2years
SoftTimers• Thereisaneedofasecondhardwareclocktocausetimer
interruptsatwhateverrateaprogramneedso specifiedbyapplicationso noproblemsifinterruptfrequencyislow
• TwowaysofmanagingI/Oo Pollinghasanaveragelatencyofhalfthepollingintervalo Interruptcanbecostly,say4.45µsona300MHzPIIo AGiga-bitEthernetasksforapacketevery12µs
• Softtimersavoidcostlyinterruptso kernelchecksforsofttimerexpirationbeforeitexitstouser
modeo howwellthisworksdependsonrateofkernelentries!Thecombinationofsofttimersandalow-frequencyhardwaretimermaybebetterthanpureinterrupt-drivenI/Oorpurepolling
PowerManagement
Powerconsumptionofvariouspartsofalaptopcomputer
° Powerconsumptionisimportanttodesktopsandbattery-poweredcomputer
° TwoGeneralapproachestobatteryconservation• Turnoff(down)someparts,top3powereaters:display,harddisk,andCPU• Applicationuseslesspower,multiplestates:on/off,sleep,hibernating
Power-awarecomputinginmobileandsensornetworks,manytradeoffs!
OSIssues:TheDisplay
° On-offswitching• Displayconsistofsomezonesthatcanbeindependentlypoweredupor
down
OSIssues:TheHardDisk° Ittakessubstantialenergytokeepitspinningathighspeed,evenifthere
arenodiskaccesses
° ButRestartingaharddiskconsumesconsiderableenergy• Whatisthebreak-evenpoint?• Td [5s,15s]• Ifthenextdiskaccessisexpectedsometimet inthefuture,comparet toTd
determinestheappropriatediskaction
° Alternative:haveasubstantialdiskcacheinRAM
° Alternative:OSkeepsrunningprogramsinformedaboutthediskstates,sothatsomeaccessescanbedelayed(andclustered)
SomethingcanbedonetoCPUtoo!
OSIssues:TheCPU
• Runningatfullclockspeed(fullvoltage)• Cuttingvoltagebytwo
o cutsclockspeedbytwoo cutspowerbyfour(powerconsumption~voltage^2)
Summary• OSresponsibilitiesinI/Ooperations
o ProtectionandSchedulingo CPUcommunicateswithI/Odeviceso I/OdevicesnotifyOS/CPU
• I/Osoftwarehierarchy
o Interrupthandlerso Devicedriverso Buffering
• StorageSystems
o Diskheadschedulingalgorithms• PowerManagement
Ref.MOS3E,OS@Austin,Columbia,RochesterUC.ColoradoSpringsCS4500/5500
Thankyouall!