I/O Devices
Table of Contents
1. I/O Devices
Without I/O, computers are useless. But, we have thousands of devices, each slightly different: how can we standardize access to these devices? Devices are also unreliable and inconsistent. I/O devices you recognize are supported by I/O controllers. Processors read and write from them using I/O registers.
I/O devices can be categorized based on their access patterns. Character devices (e.g. keyboards) access data as a character stream, whereas block devices access data in fixed-sized blocks.
These devices are commonly connected on a bus, which is a common set of wires for communication among hardware devices, plus protocols for carrying out transactions. PCI started as a parallel bus: for a 32-bit system, it had 32 address wires, 32 data wires, and power lines. PCI Express (PCIe) however is no longer a parallel bus, but rather a collection of fast serial channels or “lanes.” Devices can use as many serial channels as they need to achieve a desired bandwidth.
1.1. Device Access
The CPU interacts with I/O devices through device controllers, which contains a set of registers that can be read and written, and may contain memory for request queues, etc. Processor can access these registers in two ways: port-mapped I/O (in, out instructions) and memory-mapped I/O (registers/memory appear in physical address space, use load and store instructions).
Alternatively, direct memory access (DMA) allows the I/O device to directly write to main memory without any CPU intervention.
2. Hard Disk Drives (HDD)
Hard disk drives (HDDs) are an example of magnetic memory storage. An arm is held above a platter to read it:
A platter has two faces, and on each face are rings of tracks. Each track is comprised of a set of sectors, and two arms that can rotate into the correct position to hover above a track. The platter can then spin to position the correct sector underneath the arms.
The unit of transfer here is the sector, which are traditionally 512 bytes large (or 4096 bytes on some recent units). Disk tracks are about 1 micron wide, with about 100k tracks on a typical 2.5 inch disk. Each sector is separated by unused guard regions to reduce likelihood of write corrupting adjacent sectors.
For the operating system, the unit of transfer is often a “block” instead, which is usually a group of sectors. The block is usually defined equivalent to how large a physical page in memory is.
2.1. Shingled Magnetic Recording
Shingled magnetic recording (SMR) is a technique used to overlap tracks in order to yield greater density of storage. Normally, conventional magnetic recording (CMR) is used, but with shingled recording, new tracks that are written overlap previously written tracks (similar to roof shingles). While this increases the density of data, it also makes writing to a track harder since it also overwrites adjacent tracks:
Thus, a write to an SMR drive may trigger a cascade of rewrites down the line.
2.2. Performance
Read/write data is a three-step process:
- Seek: the head is positioned over the proper track.
- Rotate: the platter rotates the desired sector underneath the head.
- Transfer: transfer a block of bits (sectors) under the head.
Thus, the total read/write time is a combination of these times plus your software and hardware controller times.
2.2.1. Queueing
Assume requests arrive at regular intervals, take a fixed time to process, and there is plenty of time between requests. Call the number of operations the server can handle per second the service rate, and the number of requests per second the arrival rate. Then, the utilization is defined as arrival arte over the service rate.
In the real world, requests come in bursts, and most requests queue up till they are served. Now we have the same average arrival time, but almost all requests experience large queue delays.
So how do we model the burstiness of these requests? We can use an exponential distribution to do so. An exponential distribution is memoryless: the likelihood of an event occurring is independent of how long we’ve been waiting.
Say we have some queuing system with arrivals and departures. In any stable system, the average arrival rate equals the average departure rate. Then, Little’s law states that the average number of jobs in the system \(N\) is equal to the throughput \(\lambda\) times the response time \(L\):
\begin{align} N = \lambda L \end{align}Applied to queues, we get the relationship: the average length of the queue is equal to the average arrival rate times the average time waiting in queue.
Memoryless service distribution:
\begin{align} T_q = T_s \frac{u}{1-u} \end{align}where \(T_q\) is the time spent in queue, \(T_s\) is the service time, and \(u\) is server utilization.
General service distribution:
\begin{align} T_q = T_s \frac{1}{2}(1+C)u(1-u) \end{align}where \(C\) is the squared coefficient of variance \(\frac{\sigma^2}{\mu^2}\).
2.3. Disk Controller
Sector sparing: remap bad sectors transparently to spare sectors on the same surface Slip sparing: remap all sectors when there is a bad sector to preserve sequential behavior Track skewing: sector numbers offset from one track to the next to allow for disk head movement in sequential operations.
2.4. Disk Scheduling
Since disks can only service one request at a time, we have a number of options to schedule these requests:
- FIFO: first in first out
- SPTF/SSTF: shortest positioning time first/shortest seek time first — greedy algorithm, but can be susceptible to starvation.
- SCAN: implements an elevator algorithm — take the closest request in the direction of travel.
- C-SCAN: circular SCAN — only go in one direction, so as to not be biased towards pages in the middle.
3. Solid State Disks (SSD)
Solid state disks (SSDs) replace the rotating magnetic media with flash memory. Since flash memory has no moving parts, they are very lightweight; however, they are more expensive than magnetic media. One flash memory cell looks something like this:
There are two varieties: NAND, which is denser but must be read and written in blocks, and NOR, which is much less dense but fast to read and write.
This device is like a normal transistor, however it adds a floating gate which is separated by insulators in order to hold charge. We can then take advantage of electron tunneling by raising or lowering the wordline high enough to read/write the gate.
Data is read and written in page-sized chunks. Before writing to a page, data in the block must first be erased. The flash memory controller is responsible for controlling reads and writes to the flash chip. The SSD also maintains a flash translation layer (FTL) which is a layer of inderection between the operating system and the flash controller. The FTL gives us many features, such as copy-on-write and wear levelling.