chapter 13: i/o systems overview overview i/o hardware i/o hardware i/o api i/o api i/o subsystem...

39
CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Transforming I/O Requests to Hardware Operations Hardware Operations Streams Streams Performance Performance

Upload: eugene-moody

Post on 25-Dec-2015

231 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

CHAPTER 13: I/O SYSTEMS OverviewOverview I/O HardwareI/O Hardware I/O APII/O API I/O SubsystemI/O Subsystem Transforming I/O Requests to Hardware Transforming I/O Requests to Hardware

OperationsOperations StreamsStreams PerformancePerformance

Page 2: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

OVERVIEW Incredible variety of I/O devicesIncredible variety of I/O devices

Increasing standardization of I/O hardware and Increasing standardization of I/O hardware and software interfacesoftware interface

Increasingly broad variety of I/O devicesIncreasingly broad variety of I/O devices Common conceptsCommon concepts

Port Port Bus (daisy chain or shared direct access)Bus (daisy chain or shared direct access)

PCI, ISA, SCSIPCI, ISA, SCSI Controller (host adapter)Controller (host adapter)

Serial port controller, SCSI controller, IDE Serial port controller, SCSI controller, IDE controller, controller,

Page 3: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O HARDWARE Introduction Introduction PollingPolling InterruptsInterrupts DMADMA

Page 4: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Hardware

A typical PC bus structureA typical PC bus structure

Page 5: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Hardware How can the processor give commands and data to a How can the processor give commands and data to a

controller to accomplish an I/O transfercontroller to accomplish an I/O transfer Direct I/O instructionsDirect I/O instructions Memory-mapped I/OMemory-mapped I/O Direct I/O + memory mapped I/ODirect I/O + memory mapped I/O

I/O controller has 4 types of registersI/O controller has 4 types of registers Status registersStatus registers Control registersControl registers Data-in registersData-in registers Data-out registersData-out registers

Page 6: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Hardware

Some typical ports on PCsSome typical ports on PCs

Page 7: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Hardware Three interaction models between I/O controllers and Three interaction models between I/O controllers and

CPUsCPUs PollingPolling

InterruptInterrupt

DMADMA

Page 8: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Hardware: Polling Two bitsTwo bits

The busy bit indicates if the controller is busy or The busy bit indicates if the controller is busy or not working.not working.

The command-ready bit indicates if the a command The command-ready bit indicates if the a command is available or not for the controller to execute. is available or not for the controller to execute.

An exampleAn example The host The host repeatedlyrepeatedly reads the busy bit until that it reads the busy bit until that it

becomes clear (busy waiting or polling)becomes clear (busy waiting or polling) The host sets the write bit in the command register The host sets the write bit in the command register

and writes a byte into the data-out registerand writes a byte into the data-out register

Page 9: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Hardware: Polling The host sets the command-ready bitThe host sets the command-ready bit When the controller notices that the command-When the controller notices that the command-

ready bit is set, it sets the busy bitready bit is set, it sets the busy bit The controller reads the command register and sees The controller reads the command register and sees

the write command. It reads the data-out register to the write command. It reads the data-out register to get the byte, and does the I/O to the device. get the byte, and does the I/O to the device.

The controller clears the command-ready bit, clears The controller clears the command-ready bit, clears the error bit in the status register to indicate that the the error bit in the status register to indicate that the device I/O succeeded, and clears the busy bit to device I/O succeeded, and clears the busy bit to indicate that it is finished. indicate that it is finished.

Page 10: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Hardware: Interrupts CPU Interrupt request line triggered by I/O deviceCPU Interrupt request line triggered by I/O device Interrupt handler receives interruptsInterrupt handler receives interrupts Maskable to ignore or delay some interruptsMaskable to ignore or delay some interrupts Interrupt vector to dispatch interrupt to correct handlerInterrupt vector to dispatch interrupt to correct handler

Based on priorityBased on priority Some unmaskable, some maskableSome unmaskable, some maskable

Interrupt mechanism also used for Interrupt mechanism also used for exceptions and exceptions and system callssystem calls

Interrupt handlers can be divided into twoInterrupt handlers can be divided into two Top halfTop half Bottom halfBottom half

Page 11: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Hardware: Interrupts

Page 12: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Hardware: Interrupts

Page 13: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Hardware: DMA Used to avoid programmed I/O for large data Used to avoid programmed I/O for large data

movement movement

Requires DMA controllerRequires DMA controller

Bypasses CPU to transfer data directly between Bypasses CPU to transfer data directly between I/O device and memoryI/O device and memory

Cycle stealingCycle stealing

Page 14: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Hardware: DMA

Page 15: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O API I/O system calls encapsulate device behaviors in generic classes.I/O system calls encapsulate device behaviors in generic classes. Device-driver layer hides differences among I/O controllers from kernel.Device-driver layer hides differences among I/O controllers from kernel.

Page 16: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O API

Page 17: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O API On one hand, there are so many different I/O devices.On one hand, there are so many different I/O devices. On the other hand, the OS should provide a simple, On the other hand, the OS should provide a simple,

uniform I/O API.uniform I/O API. The solution is the OS should hide (encapsulate) these The solution is the OS should hide (encapsulate) these

differences and provide just a few types of device differences and provide just a few types of device access. access. Block and character devicesBlock and character devices Network DevicesNetwork Devices Clocks and TimersClocks and Timers

Page 18: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O API: Block and Character Device Interfaces

Block devices include disk drives and other block-Block devices include disk drives and other block-oriented devices:oriented devices: Commands include Commands include read, write, seekread, write, seek

Raw I/O or file-system accessRaw I/O or file-system access Memory-mapped file access possibleMemory-mapped file access possible

Character devices include keyboards, mice, serial Character devices include keyboards, mice, serial portsports Commands include Commands include get, putget, put Libraries layered on top allow line editingLibraries layered on top allow line editing

Page 19: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O API: Network Device Interfaces Varying enough from block and character to have own Varying enough from block and character to have own

interfaceinterface

Unix and Windows NT/9Unix and Windows NT/9ii/2000 include socket interfa/2000 include socket interfacece Separates network protocol from network operationSeparates network protocol from network operation Includes Includes selectselect functionality functionality

Man other approaches (pipes, FIFOs, streams, queues, Man other approaches (pipes, FIFOs, streams, queues, mailboxes)mailboxes)

Page 20: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O API: Clocks and Timers Provide current time, elapsed time, timerProvide current time, elapsed time, timer

If programmable interval time used for timings, If programmable interval time used for timings, periodic interruptsperiodic interrupts

ioctlioctl (on UNIX) covers odd aspects of I/O s (on UNIX) covers odd aspects of I/O such as clocks and timersuch as clocks and timers

Page 21: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O API: Blocking and non-blocking I/O Blocking - process suspended until I/O completedBlocking - process suspended until I/O completed

Easy to use and understandEasy to use and understand Insufficient for some needsInsufficient for some needs

Nonblocking - I/O call returns as much as availableNonblocking - I/O call returns as much as available User interface, data copy (buffered I/O)User interface, data copy (buffered I/O) Implemented via multi-threadingImplemented via multi-threading Returns quickly with count of bytes read or writtenReturns quickly with count of bytes read or written

Asynchronous - process runs while I/O executesAsynchronous - process runs while I/O executes Difficult to useDifficult to use I/O subsystem signals process when I/O completedI/O subsystem signals process when I/O completed

Page 22: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O KERNEL SUBSYSTEM I/O schedulingI/O scheduling BufferingBuffering CachingCaching Spooling and device reservationSpooling and device reservation Error handlingError handling Kernel data structuresKernel data structures

Page 23: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Kernel Subsystem SchedulingScheduling

I/O request queue for a deviceI/O request queue for a device Some OSs try fairness (disk I/O)Some OSs try fairness (disk I/O)

Buffering - store data in memory while transferring beBuffering - store data in memory while transferring between devicestween devices To cope with device speed mismatch (modem vs diTo cope with device speed mismatch (modem vs di

sk)sk) To cope with device transfer size mismatch (packet To cope with device transfer size mismatch (packet

vs frames)vs frames) To maintain “copy semantics” (kernel memory spaTo maintain “copy semantics” (kernel memory spa

ce v.s. user memory space)ce v.s. user memory space)

Page 24: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Kernel Subsystem Caching - fast memory holding copy of dataCaching - fast memory holding copy of data

Always just a copyAlways just a copy Key to performanceKey to performance Note: A buffer may hold the only existing copy of a Note: A buffer may hold the only existing copy of a

data item, whereas a cache just holds a copy on a fasdata item, whereas a cache just holds a copy on a faster storage of an item that that resides elsewhere. ter storage of an item that that resides elsewhere.

Spooling - hold output for a deviceSpooling - hold output for a device If a device can serve only one request at a time If a device can serve only one request at a time i.e., Printingi.e., Printing

Device reservation - provides exclusive access to a deviDevice reservation - provides exclusive access to a devicece System calls for allocation and deallocationSystem calls for allocation and deallocation Watch out for deadlockWatch out for deadlock

Page 25: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Kernel Subsystem Error handlingError handling

OS can recover from disk read, device OS can recover from disk read, device unavailable, transient write failuresunavailable, transient write failures

Most return an error number or code when Most return an error number or code when I/O request fails I/O request fails

System error logs hold problem reportsSystem error logs hold problem reports

Page 26: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Kernel Subsystem Kernel keeps state info for I/O components, including Kernel keeps state info for I/O components, including

open file tables, network connections, character devicopen file tables, network connections, character device statee state

Many, many complex data structures to track buffers, Many, many complex data structures to track buffers, memory allocation, “dirty” blocksmemory allocation, “dirty” blocks

Some use object-oriented methods and message passinSome use object-oriented methods and message passing to implement I/Og to implement I/O

Page 27: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Kernel Subsystem

Page 28: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

TRANSFORMING I/O TO HARDWARE OPERATIONS

Consider reading a file from disk for a process:Consider reading a file from disk for a process: Determine the device holding a file Determine the device holding a file Translate the name to the device Translate the name to the device

representationrepresentation Physically read data from disk into bufferPhysically read data from disk into buffer Make data available to the requesting Make data available to the requesting

processprocess Return control to the processReturn control to the process

Page 29: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Requests to Hardware Operations: The connection between a filename and its disk controller

How to map a filename to its disk controller?How to map a filename to its disk controller? For DOS, C:\junk.txtFor DOS, C:\junk.txt

To find the partitionTo find the partitionTo read the starting entry from FATTo read the starting entry from FAT

For Unix, /zapp/root/For Unix, /zapp/root/To find the longest match from the mount tableTo find the longest match from the mount tableTo get <major,minor>To get <major,minor>To read the inodeTo read the inodeTo get the location of data blocksTo get the location of data blocks

Page 30: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Requests to Hardware Operations: Life Cycle of An I/O Request

ReadRead Sys_readSys_read

If in the cache, read from the cacheIf in the cache, read from the cacheOtherwise, read from the hard diskOtherwise, read from the hard disk

• Issue a read request and add itself to the queue aIssue a read request and add itself to the queue and block itselfnd block itself

• The device driver allocates the buffer to receive tThe device driver allocates the buffer to receive the data, and performs the data transferhe data, and performs the data transfer

• The driver gets the data and interrupts the OSThe driver gets the data and interrupts the OS• The process will be unblocked. The process will be unblocked.

Page 31: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

I/O Requests to Hardware Operations: Life Cycle of An I/O Request

Page 32: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

STREAMS STREAMSTREAM – a full-duplex communication channel – a full-duplex communication channel

between a user-level process and a devicebetween a user-level process and a device A STREAM consists of:A STREAM consists of:

- - STREAM headSTREAM head interfaces with the user process interfaces with the user process

- - driver enddriver end interfaces with the device interfaces with the device- zero or more STREAM modules between them.- zero or more STREAM modules between them.

Each module contains a Each module contains a read queueread queue and a and a write write queue.queue.

Message passing is used to communicate between Message passing is used to communicate between queues.queues.

Flow control is used to regulate the flow. Flow control is used to regulate the flow.

Page 33: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

STREAMS

Page 34: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

PERFORMANCE I/O is a major factor in system performance:I/O is a major factor in system performance:

Demands CPU to execute device driver, kernel I/O Demands CPU to execute device driver, kernel I/O codecode

Context switches due to interruptsContext switches due to interrupts Data copyingData copying

Between controllers and kernel Between controllers and kernel Between kernel space and user spaceBetween kernel space and user space

Network traffic especially stressfulNetwork traffic especially stressfulSee the next slideSee the next slide

Page 35: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

Performance: Network traffic

Page 36: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

Performance: How to improve Reduce number of context switchesReduce number of context switches Reduce data copying Reduce data copying Reduce interrupts by using large transfers, Reduce interrupts by using large transfers,

smart controllers, polling smart controllers, polling Use DMAUse DMA Balance CPU, memory, bus, and I/O Balance CPU, memory, bus, and I/O

performance for highest throughputperformance for highest throughput

Page 37: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

Performance: Where to implement To implement experimental I/O algorithms at the applTo implement experimental I/O algorithms at the appl

ication level, ication level, because application code is flexible, and applicatiobecause application code is flexible, and applicatio

n bugs are unlikely to cause system crashes. n bugs are unlikely to cause system crashes. Avoiding rebooting or reloading device driverAvoiding rebooting or reloading device driver

To reimplement the code at the kernel (driver)To reimplement the code at the kernel (driver) Debugging is difficult, but fast. Debugging is difficult, but fast.

To reimplement the code at the controller or device (hTo reimplement the code at the controller or device (hardware)ardware) Fastest, Less flexible. Fastest, Less flexible.

Page 38: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations

Lab

System programmingSystem programming System callsSystem calls stracestrace

Page 39: CHAPTER 13: I/O SYSTEMS Overview Overview I/O Hardware I/O Hardware I/O API I/O API I/O Subsystem I/O Subsystem Transforming I/O Requests to Hardware Operations