I/O & Disks
CS-446/646
C. Papachristos
Robotic Workers (RoboWork) Lab
University of Nevada, Reno
I/O & Disks
OS Abstractions
I/O Management is another major component of OS
CS446/646 C. Papachristos
Concurrency
Threads
Synchronization
Semaphores & Monitors
Virtualization
Processes
Scheduling
Virtual Memory
Persistence
I/O
Disks
Filesystems
I/O & Disks
I/O Devices
Issues to address:
CS446/646 C. Papachristos
I/O & Disks
Structure of Input/Output (I/O) Device
CS446/646 C. Papachristos
CPU
Memory
Graphics
Memory Bus
(proprietary)
General I/O Bus
(e.g. PCI)
Peripheral I/O Bus
(e.g. SCSI, SATA, USB)
I/O & Disks
I/O Device Interfaces
CS446/646 C. Papachristos
I/O & Disks
I/O Bus – Example: Peripheral Component Interconnect (PCI) Bus
CS446/646 C. Papachristos
I/O & Disks
Device “Standardized” I/O Port Mappings on PCs
“I/O Port”: Technical term for a specific “special use” Address on the x86’s I/O Bus
CS446/646 C. Papachristos
Note: Also see https://wiki.osdev.org/I/O_Ports
I/O & Disks
Canonical I/O Device
CS446/646 C. Papachristos
Device Registers
Status
Command
Data
Micro-controller (CPU)
Memory (DRAM or SRAM or both)
Other Hardware-specific Chips
Interface
Internals
OS reads/writes to these
I/O & Disks
Hardware Interface Of Canonical Device
Registers-based:
.
CS446/646 C. Papachristos
while (STATUS == BUSY); //wait until device is not busy
write data to data register
write command to command register //doing this starts the device and executes the command
while (STATUS == BUSY); //wait until device is done with the request
I/O & Disks
Device Interaction
How the OS can communicate with a Device:
1) I/O Instructions for Device control
2) “Memory-Mapped” I/O
CS446/646 C. Papachristos
I/O & Disks
x86 I/O Instructions
Example: Pintos threads/io.h
CS446/646 C. Papachristos
static inline uint8_t inb (uint16_t port) {
uint8_t data;
asm volatile ("inb %w1, %b0" : "=a" (data) : "Nd" (port));
return data;
}
static inline void outb (uint16_t port, uint8_t data) {
asm volatile ("outb %b0, %w1" : : "a" (data), "Nd" (port));
}
static inline void insw (uint16_t port, void *addr, size_t cnt) {
asm volatile ("rep insw" : "+D" (addr), "+c" (cnt) : "d" (port) : "memory");
}
I/O & Disks
Example: IDE Disk Driver with x86 I/O Instructions
CS446/646 C. Papachristos
void IDE_ReadSector(int disk,
int off,
void *buf) {
// Select Drive
outb( 0x1F6 , disk == 0 ? 0xE0 : 0xF0);
IDEWait();
// Read length (1 Sector = 512 B)
outb( 0x1F2 , 1); // 1 Sector
outb( 0x1F3 , off); // Logical Block Address low
outb( 0x1F4 , off >> 8); // Logical Block Address mid
outb( 0x1F5 , off >> 16); // Logical Block Address high
outb( 0x1F7 , 0x20); // Read command
insw( 0x1F0 , buf, 256); // Read 256 words
}
void IDE_Wait() {
// Discard status 4 times
inb( 0x1F7 ); inb( 0x1F7 );
inb( 0x1F7 ); inb( 0x1F7 );
// Wait for status BUSY flag to clear
while ((inb( 0x1F7 ) & 0x80) != 0);
}
Remember:
I/O & Disks
Memory-Mapped I/O
I/O Port Mappings & in/out Instructions are slow and clunky
Devices can achieve same effect with dedicated I/O-Mapped Addresses, e.g.:
CS446/646 C. Papachristos
volatile int32_t *device_control = (int32_t *) (0xc0100 + PHYS_BASE);
*device_control = 0x80; // write
int32_t status = *device_control; // read
I/O & Disks
Polling
OS waits until the Device is ready by repeatedly reading the Status Register
CS446/646 C. Papachristos
Diagram of CPU utilization when Polling
I/O & Disks
CS446/646 C. Papachristos
Diagram of CPU utilization with Interrupts
I/O & Disks
CS446/646 C. Papachristos
I/O & Disks
Protocol Variants
CS446/646 C. Papachristos
Device Registers
Status
Command
Data
Micro-controller (CPU)
Memory (DRAM or SRAM or both)
Other Hardware-specific Chips
I/O & Disks
Variety is a Challenge
Problem:
We want to avoid writing a slightly different OS for each piece of Hardware
Solution: Abstraction
CS446/646 C. Papachristos
I/O & Disks
Filesystem Abstraction
Filesystem specifics of which Disk class it is using
CS446/646 C. Papachristos
Hard Drive
Device Driver [SCSI, ATA, etc.]
Generic Block Layer
Filesystem
Application
User Space
Kernel Space
The Filesystem stack
Specific Block Interface [Protocol-specific read/write]
Generic Block Interface
POSIX API [open, read, write, close, etc]
Note: “ Block” is the fundamental allocation unit that a Filesystem uses
I/O & Disks
Hard Disks – Basic Interface
CS446/646 C. Papachristos
I/O & Disks
Hard Disks – Basic Geometry
CS446/646 C. Papachristos
I/O & Disks
Hard Disks – Basic Geometry
CS446/646 C. Papachristos
I/O & Disks
Cylinders, Tracks, Sectors
CS446/646 C. Papachristos
I/O & Disks
A simple Hard Disk Drive
CS446/646 C. Papachristos
A single Track + a Head
I/O & Disks
Single-track Latency: The Rotational Delay
CS446/646 C. Papachristos
A single Track + a Head
I/O & Disks
Multiple Tracks: Start a Read
CS446/646 C. Papachristos
I/O & Disks
Multiple Tracks: Seek to Track (/Cylinder)
CS446/646 C. Papachristos
I/O & Disks
Multiple Tracks: Wait for Rotation
CS446/646 C. Papachristos
I/O & Disks
Multiple Tracks: Transfer Data
CS446/646 C. Papachristos
I/O & Disks
Multiple Tracks: Transaction Complete
CS446/646 C. Papachristos
I/O & Disks
Disk Latencies
CS446/646 C. Papachristos
I/O & Disks
CS446/646 C. Papachristos
I/O & Disks
Seek, Rotate, Transfer
CS446/646 C. Papachristos
I/O & Disks
CS446/646 C. Papachristos
I/O & Disks
Workload
So, what kind of Workload is fastest for Disks?
CS446/646 C. Papachristos
I/O & Disks
Sector Mapping
Logical Sector 0
CS446/646 C. Papachristos
I/O & Disks
Sector Mapping
Advantages
Limitations
CS446/646 C. Papachristos
I/O & Disks
Disk Cache
CS446/646 C. Papachristos
I/O & Disks
Disk Scheduling
1. Schedule requests in order received ( FCFS )
2. Handle nearest Cylinder next ( SSTF )
3. One-direction Sweeping of Disk ( SCAN / C-SCAN )
CS446/646 C. Papachristos
I/O & Disks
Disk Scheduling – FCFS
1. First Come First Served ( FCFS )
CS446/646 C. Papachristos
I/O & Disks
Disk Scheduling – FCFS
CS446/646 C. Papachristos
I/O & Disks
Disk Scheduling – SSTF (/SPTF)
2. Shortest Seek-Time First ( SSTF ) –or– Shortest Positioning Time First ( SPTF )
CS446/646 C. Papachristos
I/O & Disks
Disk Scheduling – SSTF (/SPTF)
CS446/646 C. Papachristos
I/O & Disks
“Elevator” Scheduling – SCAN (/C-SCAN)
3. SCAN –or– Circular SCAN ( C-SCAN )
Sweep across Disk, servicing all Track(/Cylinder) requests we pass
CS446/646 C. Papachristos
I/O & Disks
Disk Scheduling – C-SCAN
CS446/646 C. Papachristos
I/O & Disks
C. Papachristos
I/O & Disks
New Mass Storage Technologies
Disadvantages
C. Papachristos
I/O & Disks
Flash Memory
Today, we increasingly use Flash Memory
C. Papachristos
I/O & Disks
Redundant Array of Independent Disks (RAID)
Motivation:
C. Papachristos
I/O & Disks
Redundant Array of Independent Disks (RAID)
Idea:
C. Papachristos
I/O & Disks
Redundant Array of Independent Disks (RAID)
Evaluating RAID:
C. Papachristos
I/O & Disks
Redundant Array of Independent Disks (RAID)
Evaluating RAID:
C. Papachristos
I/O & Disks
Redundant Array of Independent Disks (RAID)
Evaluating RAID:
Compute:
C. Papachristos
I/O & Disks
Redundant Array of Independent Disks (RAID)
Evaluating RAID:
C. Papachristos
I/O & Disks
C. Papachristos
Blocks
Note:
A “Block” is the fundamental allocation unit (i.e. size) a Filesystem (more later…) uses
I/O & Disks
RAID 0 Performance
Large read of 100 Blocks:
Performance:
C. Papachristos
I/O & Disks
RAID 1 : Mirroring
Structure:
Advantages:
Disadvantages:
C. Papachristos
Blocks
I/O & Disks
RAID 1 Performance
Performance
C. Papachristos
I/O & Disks
RAID 2 : Memory-Style Error-Correcting Parity
Structure:
Advantages:
Disadvantages:
C. Papachristos
Blocks
I/O & Disks
RAID 3 : Bit-Interleaved Parity
Structure:
Advantages:
Disadvantages:
C. Papachristos
Blocks
I/O & Disks
RAID 4 : Block-Interleaved Parity
Structure:
Advantages:
Disadvantages:
C. Papachristos
Blocks
I/O & Disks
RAID 4 Performance
Performance
C. Papachristos
I/O & Disks
RAID 5 : Block-Interleaved Distributed Parity
Structure:
Advantages:
C. Papachristos
I/O & Disks
RAID 5 Performance
Performance
C. Papachristos
I/O & Disks
RAID 6 : P+Q Redundancy
Structure:
Advantages:
C. Papachristos
I/O & Disks
RAID Levels
C. Papachristos
Time for Questions !
CS-446/646
CS446/646 C. Papachristos