Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

44
Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    221
  • download

    0

Transcript of Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Page 1: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 1

Introduction to Systems Programming Lecture 9

Input-Output Devices

Page 2: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 2

I/O Software Organization

Page 3: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 3

Layers of the I/O system

Page 4: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 4

Device drivers

• Every I/O controller has its own control registers, timing requirements, and command sets

• Device driver is the device-specific software that talks to the controller (the adapter)

• Usually device driver is written by the device manufacturer

Page 5: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 5

Device Drivers

• Logical position of device drivers is shown here• Communications between drivers and device controllers goes over the bus

Page 6: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 6

Generic Device Driver Structure• Check input parameters for validity.• Translate to device terms (e.g., block number

cylinder/track/sector/head).• In use queue. • Activate device if needed (power on, spin motor)• Loop: Send control command, Wait for response.

– Usually implemented via interrupts, not by polling

• Wait for data transfer to complete.• Check for errors.• Pass data and/or status to device-independent code.

Page 7: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 7

Disks

Arm

Platter

Track

Read/Write Head

Page 8: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 8

Disk Terminology

• Disk Cylinders (concentric circles)

• Cylinder Tracks (one per read-write head)

• Tracks Sectors (sequentially around the track)– Sector == Block

Page 9: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 9

Disk Parameters

msec

Page 10: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 10

Improvement in Disk Technology

• All parameters got better, but not at same rate:

• Seek time: x 7

• Transfer time: x 1300

• Capacity: x 50000

• Surface density improved much more than mechanical performance.

Page 11: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 11

Disk Layout Affects PerformanceAverageSeek

Rotationaldelay

Read time(1 track)

Number ofSectors/track

20 ms 8.3 ms 16.7 ms 32

256 sector file stored contiguously

Number of tracks = 8

Total time = seek + 8*(track time)

Track time = Rot. Delay + Read time = 8.3 + 16.7 = 25 ms

Total time = 20ms + 8*25ms = 220 ms

256 sector file stored randomly

Total time = 256 * sector time

sector time = seek + Rot. Delay + Read time = 20 + 8.3 + 0.5

= 28.8 ms

Total time = 256 * 28.8ms = 7372.8 ms

•Only one seek overhead

•Rotational delay once for every 32 sectors

•Seek and rotational delay for every sector

Page 12: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 12

What goes on inside the disk

Page 13: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 13

Disk Geometry

• Physical geometry of a disk with two zones

Page 14: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 14

Virtual Disk Geometry

• OS / Driver does not deal with zone

• Disk exports virtual geometry, all tracks have same number of sectors

• IBM PC (BIOS) only allowed 65535 Cyl / 16 Heads / 63 Sectors per track– Max disk size: 31GB

• To avoid: disks use logical block addressing: – driver refers to blocks by number, not by

(cyl/head/sector)

Page 15: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 15

A disk sector• Before a disk can be used, it needs a low-level format

16 bytes error-correcting code for 512 byte sector is normal.

“my number is sector N”

Page 16: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 16

Performance through geometry

• Assume 32 sectors per track

• To read 40 sequential sectors:– read 32 sectors from track x– move head to track x+1 (slow!)– read 8 sectors

• by the time head is at track x+1, sector 0 will pass

• need to wait a whole rotation for it to return

Page 17: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 17

An illustration of cylinder skew

Page 18: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 18

More geometry tweaks

• Reading a sector includes:1. read data into controller’s buffer

2. compute ECC, correct errors

3. transfer data to main memory

• By the time steps 2+3 done, next sector passes the head [on slow controllers].

Page 19: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 19

Disk Interleaving

• No interleaving• Single interleaving• Double interleaving

Page 20: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 20

Dealing with bad blocks

• Disks have manufacturing defects

--> each track has some spare sectors

• During low-level format, a spare can replace the bad block

Page 21: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 21

Error Handling

• A disk track with a bad sector• Substituting a spare for the bad sector• Shifting all the sectors to bypass the bad one

Page 22: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 22

Disk Scheduling

Page 23: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 23

Controller task: Disk Scheduling

• The controller receives a request for a sector, and issues an interrupt when done.

• Keep a queue of pending requests

• Serve them in some non-FIFO order to reduce average waiting time

• This is a type of a scheduling problem!

Page 24: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 24

Disk Arm Scheduling Algorithms

• Time required to read or write a disk block determined by 3 factors

Seek time Rotational delay Actual transfer time

• Seek time dominates

Page 25: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 25

Shortest Seek First (SSF)

Initialposition

Pendingrequests

• Go to requested cylinder that is closest to the head position

• Better average seek time than FCFS

• Unfair: cylinders near edges served more slowly

Page 26: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 26

The elevator algorithm

• Go “up” and service all upward requests

• Then go “down” and serve the downward requests

Page 27: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 27

Pre-fetching

• Seek time is much slower than transfer time• Controller often reads the whole track into an

internal read-ahead cache even if only one sector is requested

• Different from OS data cache:– OS caches sectors that were read, hoping they will be

needed again– Disk controller caches sectors that were convenient to

read, hoping they may be needed in future

Page 28: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 28

On-disk sector cache• modern hard disks typically include

512kB to 2MB of RAM, used as a read-ahead cache– when a read request comes in, the accessed

sector is cached– subsequent sectors cached as well

• this has essentially no cost, since the drive heads need only stay over the drive surface for one revolution (approx. 8 ms at 7200 rpm) to read all sectors in a given track– typically takes heads several milliseconds to

move between tracks anyway

Page 29: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 29

RAID

Page 30: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 30

RAID

• Redundant Array of Inexpensive Disks

• Idea: use a box with several disks to get:– parallel I/O (can read/write to several disks at once)– fault-tolerance (disk crashes can be masked without

losing data and without down-time)

• Patterson-Gibson-Katz, 1988, suggested 6 RAID schemes, called RAID level 0-5.

Page 31: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 31

RAID – Cont.

• Appears as a large disk to OS

• All the “intelligence” is in the controller

• Inside the box: e.g., a SCSI bus with several disks

Page 32: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 32

RAID-0: Stripping

• Virtual disk sectors split into strips of k sectors• Strips placed on disks cyclically

• No overhead• Maximal parallelism• No fault tolerance (worse than single disk).• Not “real” RAID – no redundancy

Page 33: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 33

RAID-1: Mirroring

• Every disk has a copy (a mirror)• Simple• Write does 2 I/O, Read can be from either copy

• 100% overhead• Excellent fault tolerance • 2 disk operations per write, can read either copy.

Page 34: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 34

RAID-2: ECC across disks

• Use Error-Correcting Code (ECC) – example: 4 data bits, 3 parity bits

• Spread each word’s bits across the disks

• Disk drives have to be perfectly synchronized• Lower overhead than RAID-1 (depends on ECC)• Complicated, expensive controller

Page 35: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 35

RAID-3: Parity Disk

• Single parity disk, stores XOR of other disks.

• Provides 1-disk crash tolerance – crashed disk position known.

• Disk drives need to be synchronized.

• Low overhead (1/N for N disks)

• No parallelism

Page 36: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 36

RAID-4: Parity + Stripping

• Like RAID 3 but with strip-for-strip parity

• No drive synchronization.

• Each write causes 2 reads (data, parity) and 2 writes (new data, new parity)

• Parity drive can become a bottleneck

Page 37: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 37

Writing to a Raid-4 Disk

• Initially: S0 S1 S2 S3 P03 = 0

• Want to write value V into strip 0:– Read S0– Compute ΔS = S0 V– Write V into strip 0 (replace S0)– Read P03– Write (P03 ΔS) as new parity

• Verify:– V S1 S2 S3 (P03 ΔS) = … = 0

Page 38: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 38

RAID-5: Distributed Parity drive

• Like RAID-4 but parity strips spread over all disks

• More complicated controller & crash recovery process

Page 39: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 39

Clocks

Page 40: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 40

Clock Hardware

A programmable clock

Page 41: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 41

Square-Wave mode

Repeat:

• Holding register copied into counter

• Each pulse decrements the counter

• When counter == 0, interrupt CPU

• Each interrupt is a clock tick.

Page 42: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 42

Maintaining the time of day

• With battery-powered backup clock for when power is off

Page 43: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 43

Simulating multiple timers with a single clock“Delta-list”

Page 44: Avishai Wool lecture 9 - 1 Introduction to Systems Programming Lecture 9 Input-Output Devices.

Avishai Woollecture 9 - 44

Concepts for Review• Device Driver• Disk Platter• Disk Arm• Cylinder/Track/Sector• Seek time• Cylinder skew• Disk Interleaving• Disk arm scheduling:

– SSF– Elevator

• Pre-fetching• RAID• RAID-0: Stripping• RAID-1: Mirroring• RAID-4: Parity +

Stripping• Programmable clock• Timer Delta-list