OS 20 I/O Management

Download Report

Transcript OS 20 I/O Management

I/O Management
1
I/O Systems
 I/O Hardware
 Application I/O Interface
 Kernel I/O Subsystem
 Transforming I/O Requests to Hardware
Operations
 Streams
 Performance
 I/O Buffer
2
I/O Hardware
 Incredible variety of I/O devices
 Common concepts
Port
Bus (daisy chain or shared direct access)
Controller (host adapter)
 I/O instructions control devices
 Devices have addresses, used by
Direct I/O instructions
Memory-mapped I/O
3
A Typical PC Bus Structure
4
Device I/O Port Locations on PCs
(partial)
5
I/O Devices and Controllers
 In general, the closer an I/O unit is to the CPU, the faster it
can be accessed.
 Three categories:
 Human Readable (display, keyboard)
 Machine Readable (disk)
 Communication (modem, network)
80 b/s
6 Mb/s
1 Gb/s
 Other Differences:
 Data rate, latency, signal propagation, bus arbitration, buffering
time
 Application - disk requires file system support
 Complexity of Control - keyboard driver simpler than disk driver
 Unit of Transfer (byte, block)
 Data Representation (character set, parity)
 Error Conditions
6
Evolution of the I/O Function
 CPU directly controls device
 Seen in simple microprocessor devices
 I/O module (i.e., serial port chip)
 CPU uses programmed I/O
 Add Interrupts
 More efficient – No polling needed
 I/O module supports DMA
 Device can move a block of data without involving the
CPU
 I/O Processor
 I/O device enhanced with a set of I/O-specific instructions
 CPU creates I/O program
 I/O Module with memory
 Module becomes a computer of its own
 Minimal CPU involvement
7
Polling
 Determines state of device
command-ready
busy
Error
 Busy-wait cycle to wait for I/O from device
8
Interrupts
 CPU Interrupt request line triggered by I/O device
 Interrupt handler receives interrupts
 Maskable to ignore or delay some interrupts
 Interrupt vector to dispatch interrupt to correct handler
 Based on priority
 Some unmaskable
 Interrupt mechanism also used for exceptions
9
Interrupt-Driven I/O Cycle
10
Intel Pentium Processor EventVector Table
11
DMA
 In general, polling is a waste of CPU time
 Interrupt processing overhead is greater than
polling but without “wasted” cycles
 DMA reduces CPU involvement when
transferring larger chunks of data
 Cycle stealing causes the CPU to execute more
slowly
 mitigated by cache
 smarter DMA controllers
 Improvement when path between DMA module
and I/O module that does not include the system
bus
12
DMA (continued…)
 Block structure
 Data count (# of bytes/words)
 Data register (data to/from device)
 Address register (memory location)
 Control logic
 Structure of operations:
 CPU initializes DMA unit




Data Count
Data Lines
Address Lines
Inform unit if this is a read or write DMA request
Set the address of I/O device DMA acknowledge
Interrupt
Address of memory to read/write
Read
Amount of data to transfer
Data Register
Address
Register
Control Logic
Write
 DMA device performs transfer
 Steals memory cycles from CPU
 These may occur in the middle of an instruction
 DMA device interrupts CPU at end
13
DMA Configuration
14
Six Step Process to Perform DMA
Transfer
15
I/O Design Objectives
 Efficiency
 I/O can be a bottleneck
 Swapping is used to increase number of ready processes
 Requires I/O activity to do this
 Major emphasis is on disk efficiency due to its importance
 Generality
 Easier to handle devices uniformly
 How processes view I/O devices
 How O.S. manages I/O devices
 Because of differences in devices, it is hard to achieve true
generality
 Can use a hierarchical, modular approach to hide I/O details
 Processes see open/read/write/close for all devices
16
Application I/O Interface
 I/O system calls encapsulate device behaviors in
generic classes
 Device-driver layer hides differences among I/O
controllers from kernel
 Devices vary in many dimensions
 Character-stream or block
 Sequential or random-access
 Sharable or dedicated
 Speed of operation
 read-write, read only, or write only
17
A Kernel I/O Structure
18
Characteristics of I/O Devices
19
Block and Character Devices
 Block devices include disk drives
Commands include read, write, seek
Raw I/O or file-system access
Memory-mapped file access possible
 Character devices include keyboards, mice,
serial ports
Commands include get, put
Libraries layered on top allow line editing
20
Network Devices
 Varying enough from block and character to have
own interface
 Unix and Windows NT/9i/2000 include socket
interface
 Separates network protocol from network operation
 Includes select functionality
 Approaches vary widely (pipes, FIFOs, streams,
queues, mailboxes)
21
Clocks and Timers
 Provide current time, elapsed time, timer
 If programmable interval time used for
timings, periodic interrupts
 ioctl (on UNIX) covers odd aspects of I/O
such as clocks and timers
22
Blocking and Nonblocking I/O

Blocking - process suspended until I/O completed
 Easy to use and understand
 Insufficient for some needs

Nonblocking - I/O call returns as much as available
 User interface, data copy (buffered I/O)
 Implemented via multi-threading
 Returns quickly with count of bytes read or written

Asynchronous - process runs while I/O executes
 Difficult to use
 I/O subsystem signals process when I/O completed
23
Kernel I/O Subsystem
 Scheduling
 Some I/O request ordering via per-device queue
 Some OSs try fairness
 Buffering - store data in memory while
transferring between devices
 To cope with device speed mismatch
 To cope with device transfer size mismatch
 To maintain “copy semantics”
24
Sun Enterprise 6000 Device-Transfer
Rates
25
Kernel I/O Subsystem
 Caching - fast memory holding copy of data
 Always just a copy
 Key to performance
 Spooling - hold output for a device
 If device can serve only one request at a time
 i.e., Printing
 Device reservation - provides exclusive
access to a device
 System calls for allocation and deallocation
 Watch out for deadlock
26
Error Handling
 OS can recover from disk read, device
unavailable, transient write failures
 Most return an error number or code when
I/O request fails
 System error logs hold problem reports
27
Kernel Data Structures
 Kernel keeps state info for I/O components,
including open file tables, network connections,
character device state
 Many, many complex data structures to track
buffers, memory allocation, “dirty” blocks
 Some use object-oriented methods and message
passing to implement I/O
28
UNIX I/O Kernel Structure
29
I/O Requests to Hardware
Operations
 Consider reading a file from disk for a
process:
Determine device holding file
Translate name to device representation
Physically read data from disk into buffer
Make data available to requesting process
Return control to process
30
Life Cycle of An I/O Request
31
STREAMS
 STREAM – a full-duplex communication channel
between a user-level process and a device
 A STREAM consists of:
- STREAM head interfaces with the user process
- driver end interfaces with the device
- zero or more STREAM modules between them.
 Each module contains a read queue and a write
queue
 Message passing is used to communicate
between queues
32
The STREAMS Structure
33
Performance
 I/O a major factor in system performance:
Demands CPU to execute device driver, kernel
I/O code
Context switches due to interrupts
Data copying
Network traffic especially stressful
34
Intercomputer Communications
35
Improving Performance
 Reduce number of context switches
 Reduce data copying
 Reduce interrupts by using large transfers,
smart controllers, polling
 Use DMA
 Balance CPU, memory, bus, and I/O
performance for highest throughput
36
Device-Functionality Progression
37
Logical Structure
 Hierarchical structure
Divide levels by
 complexity
 time scale
 level of abstraction
Low levels may work
at nanosecond time
frames
 Simple structure
38
I/O Organization
 Logical I/O – Think of the device as a logical resource
 Directory Management
 Convert file names to identifiers that reference a file
 File System
 Deal with logical structure and open, read, write, also permissions
 Physical Organization
 Convert logical references to storage addresses
 Device I/O – Convert operations and data into I/O
instruction sequences
 May use buffering to improve speed
 Scheduling and Control – Actual queuing and scheduling
of operations
 This level handles interrupts, status
39
I/O Buffering
No buffering
Single buffering
I/O Device
Operating System
User Process
Operating System
User Process
In
In
I/O Device
Move
Operating System
Double buffering
I/O Device
Move
In
Operating System
Circular buffering
I/O Device
User Process
In
User Process
Move
40
I/O Buffering
 I/O operations from user memory space creates problems
 The pages holding the data must remain in main memory
 Limits choices O.S. can make
 Cannot completely swap out process, or deadlock may result
 Process waiting for I/O to complete
 I/O waiting for process to be swapped in
 May want to read in advance
 May want to delay writes
 Combine writes when updating disk
 Buffering Methods:
41
I/O Buffering
 Block-oriented
information is stored in fixed sized blocks
transfers are made a block at a time
used for disks and tapes
 Stream-oriented
transfer information as a stream of bytes
used for terminals, printers, communication
ports, mouse, and most other devices that are
not secondary storage
42
Single Buffering






I/O device transfers data to a system buffer, then O.S. copies data to
user
As soon as transfer is completed, try to read the next block in advance
 Hopefully that block will be used next
 User process can be working on one block while the next is being
read
Block transfer time: max[C,T]+M
 C = time to compute, T = time to do I/O, M = time to transfer to
buffer
 Note: with no buffering, time is C+T
O.S. must track which buffers are assigned to user processes
 Don’t want to swap to a device if we are waiting on that device for
I/O
Able to swap processes without interfering with I/O operations
For character-I/O, can use buffer for byte buffering or line buffering Producer/Consumer type problem
43
Buffering
 Double Buffering
 Uses two system buffers, read into one buffer, copy data to user
from the other
 Block transfer time: max[C,T]
 max[C+M,T]? – Account for move time
 Can do transfer while next I/O starts
 Circular Buffering
 Similar to double buffering, but have multiple system buffers
 Helps handle I/O bursts
 Summary
 Buffering helps smooth out peaks in I/O use
 Doesn’t improve sustained I/O rate
 Generally helps in multiprogramming systems
44