44
1 I/O Management

1 I/O Management. 2 I/O Systems I/O Hardware Application I/O Interface Kernel I/O Subsystem Transforming I/O Requests to Hardware Operations

Embed Size (px)

Citation preview

Page 1: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

1

I/O Management

Page 2: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

2

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

Page 3: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

3

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

Page 4: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

4

A Typical PC Bus Structure

Page 5: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

5

Device I/O Port Locations on PCs (partial)

Page 6: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

6

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) 80 b/s Machine Readable (disk) 6 Mb/s Communication (modem, network) 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

Page 7: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

7

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

Page 8: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

8

Polling

Determines state of device command-ready busy Error

Busy-wait cycle to wait for I/O from device

Page 9: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

9

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

Page 10: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

10

Interrupt-Driven I/O Cycle

Page 11: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

11

Intel Pentium Processor Event-Vector Table

Page 12: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

12

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

Page 13: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

13

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

Inform unit if this is a read or write Set the address of I/O device Address of memory to read/write Amount of data to transfer

DMA device performs transfer Steals memory cycles from CPU These may occur in the middle of an instruction

DMA device interrupts CPU at end

Data Count

Data Register

Address Register

Control Logic

DMA requestDMA acknowledge

InterruptReadWrite

Address Lines

Data Lines

Page 14: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

14

DMA Configuration

Page 15: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

15

Six Step Process to Perform DMA Transfer

Page 16: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

16

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

Page 17: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

17

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

Page 18: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

18

A Kernel I/O Structure

Page 19: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

19

Characteristics of I/O Devices

Page 20: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

20

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

Page 21: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

21

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)

Page 22: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

22

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

Page 23: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

23

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

Page 24: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

24

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”

Page 25: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

25

Sun Enterprise 6000 Device-Transfer Rates

Page 26: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

26

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

Page 27: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

27

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

Page 28: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

28

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

Page 29: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

29

UNIX I/O Kernel Structure

Page 30: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

30

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

Page 31: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

31

Life Cycle of An I/O Request

Page 32: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

32

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

Page 33: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

33

The STREAMS Structure

Page 34: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

34

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

Page 35: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

35

Intercomputer Communications

Page 36: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

36

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

Page 37: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

37

Device-Functionality Progression

Page 38: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

38

Logical Structure Hierarchical structure

Divide levels bycomplexitytime scalelevel of abstraction

Low levels may work at nanosecond time frames

Simple structure

Page 39: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

39

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

Page 40: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

40

I/O Buffering

No buffering

Operating System User Process

I/O DeviceIn

Single buffering

Operating System User Process

I/O DeviceIn Move

Double buffering I/O Device In Move

Operating System User Process

Circular buffering

Operating System User Process

I/O DeviceIn Move

Page 41: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

41

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:

Page 42: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

42

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

Page 43: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

43

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

Page 44: 1 I/O Management. 2 I/O Systems  I/O Hardware  Application I/O Interface  Kernel I/O Subsystem  Transforming I/O Requests to Hardware Operations

44

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