Magnetic Disks (Hard Drives)

Lecture Notes for CS 140
Spring 2019
John Ousterhout

  • Readings for this topic from Operating Systems: Principles and Practice: Section 12.1.
  • Basic geometry:
    • 1-5 platters, magnetically coated on each surface
    • Platters spin continuously at 5000-15000 RPM
    • Actuator arm positions heads, which can read and write data on the magnetic surfaces.
    • Overall size of disk package: 1-8 inches.
  • Organization of disk data:
    • Circular tracks corresponding to a particular position of the actuator arm.
    • Typical density today: 200,000 tracks per radial inch.
    • Tracks divided into 4096-byte sectors. Typical tracks contain a few thousand sectors.
    • Typical total drive capacities: 100GB-2TB
      • 100GB ~ 50M double-spaced pages of text.
    • Disk technology has been one of the most rapidly advancing technologies: until recently, capacities have increased faster than Moore's Law.
  • Reading and writing:
    • Seek: move actuator arm to position heads over desired track. Typical seek time: 2-10ms.
    • Select a particular head.
    • Rotational latency: wait for desired sector to pass under the head. One-half disk rotation on average (4ms @ 7500RPM)
    • Transfer: read or write one or more sectors as they pass under the head. Typical transfer rates: 100-150 MBytes/sec.
    • Latency refers to the sum of seek time plus rotational latency; typically 5-10ms.
  • API for disks:
    read(startSector, sectorCount, physAddr)
    write(startSector, sectorCount, physAddr)
    
  • In the old days the track and surface structure of the disk was visible to software:
    read(track, surface, sector, sectorCount, physAddr)
    
  • Nowadays the track structure is hidden inside the disk:
    • Inner tracks have fewer sectors than outer tracks
    • If some sectors are bad, disk software automatically remaps them to spare sectors.

Communicating with I/O Devices

  • Device registers:
    • Each device appears in the physical address space of the machine as a few words, called device registers.
    • The operating system reads and writes device registers to control the device.
    • Bits in device registers serve 3 purposes:
      • Parameters provided by CPU to device (e.g. number of first sector to read)
      • Status bits provided by device to CPU (e.g. "operation complete" or "error occurred").
      • Control bits set by CPU (e.g. "start disk read") to initiate operations.
    • Device registers don't behave like ordinary memory locations:
      • "Start operation" bit may always read as 0.
      • Bits may change without being written by CPU (e.g. "operation complete" bit).
  • Typical I/O operation:
    • CPU writes device registers to start operation (e.g., read)
    • CPU polls ready bit in device register
    • When operation is finished, device sets ready
    • Most devices support DMA (Direct Memory Access):
      • Device can transfer data to and from physical memory, without help from the CPU.
      • CPU loads buffer address into a device register before starting operation (e.g., where to copy data read from disk).
      • Device moves data directly to/from physical memory.
  • Interrupts: allow CPU to do other work while devices are operating.
    • "Interrupt Enable" bit in device register
    • CPU starts I/O operation, sets IE bit, then works on other things.
    • When device needs attention (operation completes) it interrupts the CPU:
      • A forced procedure call to a particular address in the kernel.
    • Operating system figures out which device interrupted, services that device.
      • Either start a new operation or turn off the IE bit.
    • Operating system returns from the interrupt back to whatever it was working on.
    • Interrupts make the operating system much more efficient; for example, can keep many devices busy at the same time, while also running user code.
    • Multi-core machines spread the interrupts around to balance loads.