Why The Design:Why is the CPU slow to access the hard disk?

·

8 min read

This article is first published in the medium MPP plan. If you are a medium user, please follow me in medium. Thank you very much.

This post is part of the Why The Design series. If you want to learn more about the Why The Design series, visit this blog below.

Mechanical hard disk drives (HDD) and solid-state drives (SSD) are two of the most common types of hard drives. As external storage for computers, it takes a long time for the CPU to access the data stored on them. According to the table below, accessing 4KB of data randomly in an SSD takes 1,500 times longer than accessing main memory, while the seek time for a mechanical disk is 100,000 times longer than accessing main memory:

WorkLatency
L1 cache reference0.5 ns
Branch mispredict5 ns
L2 cache reference7 ns
Mutex lock/unlock25 ns
Main memory reference100 ns
Compress 1K bytes with Zippy3,000 ns
Send 1K bytes over 1 Gbps network10,000 ns
Read 4K randomly from SSD*150,000 ns
Read 1 MB sequentially from memory250,000 ns
Round trip within same datacenter500,000 ns
Read 1 MB sequentially from SSD*1,000,000 ns
Disk seek10,000,000 ns
Read 1 MB sequentially from disk20,000,000 ns
Send packet CA->Netherlands->CA150,000,000 ns

Latency Numbers Every Programmer Should Know https://gist.github.com/jboner/2841832 ↩︎

Although the seek time for a disk is only 10 ms, it is already a very long time for the CPU. When we scale up the times mentioned above proportionally, we can intuitively feel the performance differences. For example, if accessing L1 cache takes 1 second for the CPU, accessing main memory would take 3 minutes, reading data randomly from an SSD would take 3.4 days, disk seek would take 2 months, and network transmission could take over a year.

In computer architecture, hard disks are common input/output devices, and the operating system does not necessarily need a hard disk to start. It can start through a hard disk, a network device, or an external device. Therefore, a hard disk is not a necessary condition for computer operation.

von-neumann-architecture.svg

Figure 1 - Input/Output Devices

As an external input/output device, compared to CPU cache and memory, the slow read and write speed of a hard disk is reasonable. However, the several thousand to several hundred thousand times difference in speed does make it difficult to imagine or accept. In this article, we will analyze why accessing a hard disk is very slow for the CPU:

  • The process of CPU accessing data from a hard disk is complex. It first reads the data from the disk into memory through I/O operations and then accesses the data stored in memory.
  • Mechanical hard disks rely on mechanical structures to access the data in the disk, which requires moving the mechanical arm of the disk.

I/O Operations

In order for the CPU to access data from the disk, it must first read the data from the disk into memory through I/O operations, and then access the data stored in memory. There are three common types of I/O operations in computers: Programmed I/O, Interrupt-driven I/O, and Direct Memory Access (DMA). We will introduce these operations one by one:

The simplest form of performing I/O operations is using Programmed I/O. When using Programmed I/O, the CPU is responsible for all the work. For example, if we want to output "Hello World" on the screen, the CPU will write a new character to the I/O device each time, and after writing, it will poll the device's status and wait for it to complete its work before writing a new character. This method is simple but it occupies all the CPU resources, which can cause serious waste of computing resources in some complex systems.

Interrupt-driven I/O is a more efficient way to perform I/O operations. In Programmed I/O, the CPU actively retrieves the device's status and waits for the device to become idle. However, if Interrupt-driven I/O is used, the device will actively initiate an interrupt when it is idle, pause the current process, and save the context. The operating system will then execute the interrupt handler for the I/O device:

  • If there are no characters to be printed at the moment, the interrupt handler stops and resumes the paused process.
  • If there are characters to be printed, the next character is copied to the device and the paused process is resumed.

Using Interrupt-driven I/O allows the CPU to handle other tasks when the device is busy, thus maximizing CPU utilization and avoiding wasting precious computing resources. Compared to Programmed I/O, Interrupt-driven I/O delegates some work to the I/O device, thereby improving resource utilization.

Direct Memory Access (DMA) uses a DMA controller to perform I/O operations. Interrupt-driven I/O requires triggering an operating system interrupt for each character, which consumes CPU time. When we use a DMA controller, the CPU reads all the data from the buffer into the DMA controller at once, and the DMA controller is responsible for writing the data to the I/O device character by character。
Although the DMA controller can free up the CPU and reduce the number of interrupts, its execution speed is much slower than the CPU. If the DMA controller cannot drive the I/O device quickly, the CPU may have to wait for the DMA controller to trigger an interrupt. In this case, Interrupt-driven I/O or even Programmed I/O can provide faster access speed.

By default, we use a DMA controller to perform I/O tasks. However, Programmed I/O and Interrupt-driven I/O are also acceptable options. When the CPU frequently needs to wait for the DMA controller to perform I/O tasks, using Interrupt-driven I/O or even polling Programmed I/O can achieve higher throughput. However, regardless of the method used, I/O is a complex and time-consuming operation in the program.

Mechanical Hard Disk

A mechanical hard disk drive (HDD) is an electronic, non-volatile mechanical data storage device. It uses magnetic storage to store and retrieve data on the disk. During the process of reading and writing data, the disk head connected to the mechanical arm of the hard disk reads and writes bits on the surface of the disk4.

Because the disk has a complex mechanical structure, reading and writing data on the disk takes a lot of time. The read and write performance of databases also depends on the performance of the disk. If we randomly query a piece of data in a database using a mechanical hard disk, it may trigger random I/O on the disk. However, it requires a significant cost to read data from the disk into memory. Loading data from a regular disk (non-SSD) involves processes such as queueing, seeking, rotating, and transferring data, which takes about 10 ms.

When estimating the query performance of a database, we can use the order of magnitude of 10 ms to estimate the time occupied by random I/O. It is worth mentioning that random I/O has a significant impact on the query performance of databases. On the other hand, reading data sequentially from a disk can achieve a speed of up to 40 MB/s, which is several orders of magnitude faster. Therefore, we should try to minimize the number of random I/O operations in order to improve performance.

A solid-state drive (SSD) is a computer storage device that uses flash memory as persistent storage. Unlike mechanical hard disks, SSDs do not contain any mechanical structures. When we read or write data using an SSD, no mechanical structures are involved because everything is done by circuits. Therefore, the read and write speed of an SSD is much faster than that of an HDD.

hdd-ssd-price

Figure 5 - HDD and SSD Prices

Since their inception, the prices of both mechanical hard disks and SSDs have been continuously decreasing. Mechanical hard disks are the main external storage used in data centers today. Most general-purpose commercial servers use mechanical hard disks as their main external storage. However, because the read and write speed of SSDs is tens of times faster than that of mechanical hard disks, more and more servers, especially databases, use SSDs as their external storage. However, as an external storage device with mechanical structures, it is susceptible to external interference when subjected to vibration.

Conclusion

Hard disks are external storage devices in computers that can store large amounts of data persistently. However, the CPU cannot directly access the data on the hard disk. When a computer starts, the operating system loads the necessary data from the disk into memory for CPU access. But if the data the CPU wants to access is not in memory, it takes several thousand to several hundred thousand times longer to read the data. This is mainly due to the following two reasons:

  • CPU needs to access data in external storage through I/O operations. The three methods of Programmed I/O, Interrupt-driven I/O, and DMA all incur additional overhead and consume a significant amount of CPU time.
  • Mechanical hard disks access the data in the disk through mechanical structures. Each random I/O operation on the hard disk requires several processes such as queueing, seeking, rotating, and transferring data, which takes about 10 ms.

As mentioned in the article, a hard disk is not a necessary hardware device for computer operation. A computer can load the necessary data for startup from any external storage device such as a disk or CD-ROM into memory and start normally. However, hard disks are currently the most common external storage devices. In the end, let's look at some open-ended questions related to the topic. Interested readers can carefully consider the following questions:

  • Is data written to a hard disk always persistently stored without loss?
  • Why is the data in memory cleared after a power outage and restart?

If you have any questions about the content of the article or want to learn more about the reasons behind some design decisions in software engineering, you can leave a comment below the blog post. The author will reply to related questions in a timely manner and select suitable topics for future content based on them.