I/O Systems - Ubiquitous Computing Lab

Download Report

Transcript I/O Systems - Ubiquitous Computing Lab

Hung Q. Ngo
KyungHee University
Spring 2009
http://uclab.khu.ac.kr/lectures/2009-1-os.html
Chapter 13: I/O Systems
Note: Some slides and/or pictures in the following are
adapted from slides ©2005 Silberschatz, Galvin, and Gagne.
Many slides generated from my lecture notes by Kubiatowicz.
The Requirements of I/O
• So far in this course:
– We have learned how to manage CPU, memory
• What about I/O?
– Without I/O, computers are useless (disembodied brains?)
– But… thousands of devices, each slightly different
» How can we standardize the interfaces to these devices?
– Devices unreliable: media failures and transmission errors
» How can we make them reliable???
– Devices unpredictable and/or slow
» How can we manage them if we don’t know what they will do
or how they will perform?
• Some operational parameters:
– Byte/Block
» Some devices provide single byte at a time (e.g. keyboard)
» Others provide whole blocks (e.g. disks, networks, etc)
– Sequential/Random
» Some devices must be accessed sequentially (e.g. tape)
» Others can be accessed randomly (e.g. disk, cd, etc.)
– Polling/Interrupts
» Some devices require continual monitoring
» Others generate interrupts when they need service
Operating System
13.2
Hung Q. Ngo Spring 2009
Modern I/O Systems
Operating System
13.3
Hung Q. Ngo Spring 2009
Example Device-Transfer Rates (Sun Enterprise 6000)
• Device Rates vary over many orders of magnitude
– System better be able to handle this wide range
– Better not have high overhead/byte for fast devices!
– Better not waste time waiting for slow devices
Operating System
13.4
Hung Q. Ngo Spring 2009
The Goal of the I/O Subsystem
• Provide Uniform Interfaces, Despite Wide Range of
Different Devices
– This code works on many different devices:
FILE fd = fopen(“/dev/something”,”rw”);
for (int i = 0; i < 10; i++) {
fprintf(fd,”Count %d\n”,i);
}
close(fd);
– Why? Because code that controls devices (“device
driver”) implements standard interface.
• We will try to get a flavor for what is involved in
actually controlling devices in rest of lecture
– Can only scratch surface!
Operating System
13.5
Hung Q. Ngo Spring 2009
Want Standard Interfaces to Devices
• Block Devices: e.g. disk drives, tape drives, DVD-ROM
–
–
–
–
Access blocks of data
Commands include open(), read(), write(), seek()
Raw I/O or file-system access
Memory-mapped file access possible
• Character Devices: e.g. keyboards, mice, serial ports,
some USB devices
– Single characters at a time
– Commands include get(), put()
– Libraries layered on top allow line editing
• Network Devices: e.g. Ethernet, Wireless, Bluetooth
– Different enough from block/character to have own
interface
– Unix and Windows include socket interface
» Separates network protocol from network operation
» Includes select() functionality
– Usage: pipes, FIFOs, streams, queues, mailboxes
Operating System
13.6
Hung Q. Ngo Spring 2009
How Does User Deal with Timing?
• Blocking Interface: “Wait”
– When request data (e.g. read() system call), put
process to sleep until data is ready
– When write data (e.g. write() system call), put process
to sleep until device is ready for data
• Non-blocking Interface: “Don’t Wait”
– Returns quickly from read or write request with count of
bytes successfully transferred
– Read may return nothing, write may write nothing
• Asynchronous Interface: “Tell Me Later”
– When request data, take pointer to user’s buffer, return
immediately; later kernel fills buffer and notifies user
– When send data, take pointer to user’s buffer, return
immediately; later kernel takes data and notifies user
Operating System
13.7
Hung Q. Ngo Spring 2009
Main components of Intel Chipset: Pentium 4
• Northbridge:
– Handles memory
– Graphics
• Southbridge: I/O
–
–
–
–
–
–
–
PCI bus
Disk controllers
USB controllers
Audio
Serial I/O
Interrupt controller
Timers
Operating System
13.8
Hung Q. Ngo Spring 2009
How does the processor actually talk to the device?
Processor Memory Bus
CPU
Interrupt
Controller
Bus
Adaptor
Other Devices
or Buses
Regular
Memory
Bus
Adaptor
Address+
Data
Interrupt Request
Device
Controller
Hardware
Controller
Bus
Interface
• CPU interacts with a Controller
– Contains a set of registers that
can be read and written
– May contain memory for request
queues or bit-mapped images
read
write
control
status
Registers
(port 0x20)
Addressable
Memory
and/or
Queues
Memory Mapped
Region: 0x8f008020
• Regardless of the complexity of the connections and
buses, processor accesses registers in two ways:
– I/O instructions: in/out instructions
» Example from the Intel architecture: out 0x21,AL
– Memory mapped I/O: load/store instructions
» Registers/memory appear in physical address space
» I/O accomplished with load and store instructions
Operating System
13.9
Hung Q. Ngo Spring 2009
Device I/O Port Locations on PCs (partial)
Operating System
13.10
Hung Q. Ngo Spring 2009
Example: Memory-Mapped Display Controller
• Memory-Mapped:
– Hardware maps control registers
and display memory into physical
address space
» Addresses set by hardware jumpers
or programming at boot time
0x80020000
0x80010000
– Simply writing to display memory
(also called the “frame buffer”)
changes image on screen
0x8000F000
– Writing graphics description to
command-queue area
0x0007F004
» Addr: 0x8000F000—0x8000FFFF
» Say enter a set of triangles that
describe some scene
» Addr: 0x80010000—0x8001FFFF
– Writing to the command register
may cause on-board graphics
hardware to do something
» Say render the above scene
» Addr: 0x0007F004
0x0007F000
Graphics
Command
Queue
Display
Memory
Command
Status
Physical Address
Space
• Can protect with page tables
Operating System
13.11
Hung Q. Ngo Spring 2009
Transfering Data To/From Controller
• Programmed I/O:
– Each byte transferred via processor in/out or load/store
– Pro: Simple hardware, easy to program
– Con: Consumes processor cycles proportional to data size
• Direct Memory Access:
– Give controller access to memory bus
– Bypasses CPU to transfer data directly between I/O
device and memory
Operating System
13.12
Hung Q. Ngo Spring 2009
DMA
Operating System
13.13
Hung Q. Ngo Spring 2009
A Kernel I/O Structure
Operating System
13.14
Hung Q. Ngo Spring 2009
I/O Systems Layers
Operating System
13.15
Hung Q. Ngo Spring 2009
Device Drivers
• Device Driver: Device-specific code in the kernel that
interacts directly with the device hardware
– Supports a standard, internal interface
– Same kernel I/O system can interact easily with
different device drivers
– Special device-specific configuration supported with the
ioctl() system call
• Device Drivers typically divided into two pieces:
– Top half: accessed in call path from system calls
» Implements a set of standard, cross-device calls like
open(), close(), read(), write(), ioctl(),
strategy()
» This is the kernel’s interface to the device driver
» Top half will start I/O to device, may put thread to sleep
until finished
– Bottom half: run as interrupt routine
» Gets input or transfers next block of output
» May wake sleeping threads if I/O now complete
Operating System
13.16
Hung Q. Ngo Spring 2009
Life Cycle of An I/O Request
User
Program
Kernel I/O
Subsystem
Device Driver
Top Half
Device Driver
Bottom Half
Device
Hardware
Operating System
13.17
Hung Q. Ngo Spring 2009
I/O Device Notifying the OS
• The OS needs to know when:
– The I/O device has completed an operation
– The I/O operation has encountered an error
• I/O Interrupt:
– Device generates an interrupt whenever it needs service
– Handled in bottom half of device driver
» Often run on special kernel-level stack
– Pro: handles unpredictable events well
– Con: interrupts relatively high overhead
• Polling:
– OS periodically checks a device-specific status register
» I/O device puts completion information in status register
» Could use timer to invoke lower half of drivers occasionally
– Pro: low overhead
– Con: may waste many cycles on polling if infrequent or
unpredictable I/O operations
• Actual devices combine both polling and interrupts
– For instance: High-bandwidth network device:
» Interrupt for first incoming packet
» Poll for following packets until hardware empty
Operating System
13.18
Hung Q. Ngo Spring 2009
Interrupts
•
•
•
•
CPU Interrupt-request lines 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 nonmaskable
• Interrupt mechanism also used for exceptions
Operating System
13.19
Hung Q. Ngo Spring 2009
Interrupt-Driven I/O Cycle
Page fault
Interrupt suspends
current process &
jumps to the pagefault handler in the
kernel
(Interrupt-request lines)
Handler saves process
state, moves it to wait
queue, performs pagecache management,
schedules an I/O op.
to fetch the page
Handler schedules
another process to
resume execution, then
return from interrupt
Operating System
13.20
Hung Q. Ngo Spring 2009
Intel Pentium Processor Event-Vector Table
Operating System
13.21
Hung Q. Ngo Spring 2009
Summary
• I/O Devices Types:
– Many different speeds (0.1 bytes/sec to GBytes/sec)
– Different Access Patterns:
» Block Devices, Character Devices, Network Devices
– Different Access Timing:
» Blocking, Non-blocking, Asynchronous
• I/O Controllers: Hardware that controls actual device
– Processor Accesses through I/O instructions, load/store
to special physical memory
– Report their results through either interrupts or a status
register that processor looks at occasionally (polling)
• Device Driver: Device-specific code in kernel
Operating System
13.22
Hung Q. Ngo Spring 2009
Disk I/O Operations
Operating System
13.23
Hung Q. Ngo Spring 2009
Disk Structure
• Disk drives are addressed as large 1-dimensional
arrays of logical blocks, where the logical block
is the smallest unit of transfer.
• The 1-dimensional array of logical blocks is
mapped into the sectors of the disk sequentially.
– Sector 0 is the first sector of the first track on
the outermost cylinder.
– Mapping proceeds in order through that track,
then the rest of the tracks in that cylinder, and
then through the rest of the cylinders from
outermost to innermost.
Operating System
13.24
Hung Q. Ngo Spring 2009
Disk Scheduling
• The operating system is responsible for using hardware
efficiently — for the disk drives, this means having a fast
access time and disk bandwidth.
• Access time has two major components
– Seek time is the time for the disk are to move the
heads to the cylinder containing the desired sector.
– Rotational latency is the additional time waiting for the
disk to rotate the desired sector to the disk head.
• Minimize seek time
• Seek time  seek distance
• Disk bandwidth is the total number of bytes transferred,
divided by the total time between the first request for
service and the completion of the last transfer.
Operating System
13.25
Hung Q. Ngo Spring 2009
Disk Scheduling (Cont.)
• Several algorithms exist to schedule the servicing
of disk I/O requests.
• We illustrate them with a request queue (0-199).
98, 183, 37, 122, 14, 124, 65, 67
Head pointer 53
Operating System
13.26
Hung Q. Ngo Spring 2009
FCFS
Illustration shows total head movement of 640 cylinders.
Operating System
13.27
Hung Q. Ngo Spring 2009
SSTF
• Selects the request with the minimum seek time from the
current head position.
• SSTF scheduling is a form of SJF scheduling; may cause
starvation of some requests.
• Illustration shows total head movement of 236 cylinders.
Operating System
13.28
Hung Q. Ngo Spring 2009
SCAN
• The disk arm starts at one end of the disk, and moves
toward the other end, servicing requests until it gets to
the other end of the disk, where the head movement is
reversed and servicing continues.
• Sometimes called the elevator algorithm.
• Illustration shows total head movement of 208 cylinders.
Operating System
13.29
Hung Q. Ngo Spring 2009
C-SCAN
• Provides a more uniform wait time than SCAN.
• The head moves from one end of the disk to the other.
servicing requests as it goes. When it reaches the other
end, however, it immediately returns to the beginning of
the disk, without servicing any requests on the return trip.
• Treats the cylinders as a circular list that wraps around
from the last cylinder to the first one.
Operating System
13.30
Hung Q. Ngo Spring 2009
C-LOOK
• Version of C-SCAN
• Arm only goes as far as the last request in
each direction, then reverses direction
immediately, without first going all the way
to the end of the disk.
Operating System
13.31
Hung Q. Ngo Spring 2009
Selecting a Disk-Scheduling Algorithm
• SSTF is common and has a natural appeal
• SCAN and C-SCAN perform better for systems
that place a heavy load on the disk.
• Performance depends on the number and types of
requests.
• Requests for disk service can be influenced by
the file-allocation method.
• The disk-scheduling algorithm should be written
as a separate module of the operating system,
allowing it to be replaced with a different
algorithm if necessary.
• Either SSTF or LOOK is a reasonable choice for
the default algorithm.
Operating System
13.32
Hung Q. Ngo Spring 2009