Understanding Disk I/O: Read, Write, DMA, Page Cache, mmap and Performance Optimizations
The article explains the fundamentals of disk I/O, covering read/write processes, IO interrupts, DMA, page cache, mmap, buffered versus unbuffered file operations, ByteBuffer usage, Linux dirty‑page parameters, and how these mechanisms affect application performance and reliability.
Developers often hear that improving program performance is essential; slow page loads can stem from network, hardware, or program issues such as inefficient disk I/O. When network and hardware are optimal, optimizing I/O—both disk and network—becomes critical.
What is disk I/O? When a program accesses data not present in memory, the OS performs a system call to read the required data from disk into memory, constituting classic disk I/O.
IO Interrupt Process (Read)
1. The user process calls read , entering a blocked state. 2. The OS forwards the request to the disk. 3. The disk driver reads data into its buffer and signals an interrupt when the buffer is full. 4. The kernel copies data from the driver buffer to the kernel buffer using CPU time. 5. If the kernel buffer does not satisfy the request, steps 3‑4 repeat. 6. Finally, the CPU copies data from the kernel buffer to the user buffer and returns from the system call.
Because the CPU participates in each step, large reads can consume significant CPU time.
DMA Principle
Direct Memory Access (DMA) reduces CPU involvement by allowing the disk driver to transfer data directly to memory. The DMA‑based flow mirrors the interrupt flow but offloads the copy step to DMA, only involving the CPU when the kernel buffer is full and needs to be copied to user space.
Key Concepts to Know
01. What is DMA? DMA enables hardware devices to transfer data directly to/from memory without heavy CPU interruption, reducing CPU load.
02. What is page cache? Page cache stores file data in memory in 4 KB pages, speeding up subsequent reads. Modified pages become “dirty” and are eventually flushed to disk by the kernel.
03. What is mmap? mmap maps a file directly into a process’s address space, allowing the program to read/write via pointers and letting the kernel handle dirty page write‑back, eliminating explicit read / write calls.
Buffered vs. Unbuffered File I/O (Java examples)
//1. No buffer
public class OSFileIO {
static byte[] data = "123456789\n".getBytes();
static String path = "/root/testfileio/out.txt";
public static void testBasicFileIO() throws Exception {
File file = new File(path);
FileOutputStream out = new FileOutputStream(file);
while (true) {
out.write(data);
}
}
} //2. With buffer
public class OSFileIO2 {
static byte[] data = "123456789\n".getBytes();
static String path = "/root/testfileio/out1.txt";
public static void testBufferedFileIO() throws Exception {
File file = new File(path);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
while (true) {
out.write(data);
}
}
}The JVM’s default buffer size is 8 KB; buffered I/O is noticeably faster because system calls occur only when the buffer fills.
ByteBuffer Operations (Java NIO demo)
public void whatByteBuffer() {
//ByteBuffer buffer = ByteBuffer.allocate(1024);
ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
System.out.println("postition: " + buffer.position());
System.out.println("limit: " + buffer.limit());
System.out.println("capacity: " + buffer.capacity());
System.out.println("mark: " + buffer);
buffer.put("123".getBytes());
System.out.println("-------------put:123......");
System.out.println("mark: " + buffer);
buffer.flip(); // switch to read mode
System.out.println("-------------flip......");
System.out.println("mark: " + buffer);
buffer.get();
System.out.println("-------------get......");
System.out.println("mark: " + buffer);
buffer.compact();
System.out.println("-------------compact......");
System.out.println("mark: " + buffer);
buffer.clear();
System.out.println("-------------clear......");
System.out.println("mark: " + buffer);
}Key ByteBuffer methods: put , flip , get , compact , clear , and the concepts of position , limit , and capacity .
Linux Kernel Dirty‑Page Parameters
vm.dirty_background_ratio = 0 # percentage threshold for background writeback
vm.dirty_background_bytes = 1048576 # byte threshold for background writeback
vm.dirty_ratio = 0 # when dirty pages reach this percentage, writes block
vm.dirty_bytes = 1048576 # byte limit that triggers blocking
vm.dirty_writeback_centisecs = 5000 # interval for writeback (0.01 s units)
vm.dirty_expire_centisecs = 30000 # max age of dirty pages before forced writebackThese settings control when the kernel flushes dirty pages to disk, balancing performance against data safety.
Conclusion
All disk I/O operations aim to reduce data movement and kernel involvement to improve performance, yet the OS cannot guarantee zero data loss; it trades reliability for speed. Developers should understand these mechanisms—IO interrupts, DMA, page cache, mmap, buffering, and kernel parameters—to minimize unnecessary copies and system calls, thereby optimizing application performance.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.