Why is the server disk io so slow?
PC users often think that "the disk is fast, so it takes 2 minutes for brother to copy a 1 GB film"
The server engineer may think that "Why is the disk so slow, I/O is full in 1 Mbit/s, and his meow disk is slower than the network speed, my server is stuck"
Why is the server disk io So slow sometimes?
The disk we use (IDE, SATA, or SCSI) is also called a "mechanical disk ".
From the name, we can see that a disk is not a pure "Electronic Product", and it depends largely on a "mechanical arm" to read and write data, this mechanical arm is the culprit of slow disk io.
As shown in, the data on the disk is stored on a circular disk. The data on the disk is read and written by the head, and the head is mounted on a mechanical arm (Head arm.
When we need to read and write a file, the kernel maps the file to a linear disk address (LBA ). The disk first finds a point on the disk based on LBA, then sets the head to the point, and then reads the data through the head.
The time spent reading files is divided into two parts:
1. Time for the head to be located at the specified position (average seek time)
2. Time when the head reads data from the disk (Transfer Time)
How to locate the head to a specified position:
This involves moving in two directions. The head moves to the center (or away from the center) under the control of the manipulator, and the disk is rotated.
The time required to move the head to a proper position is called the average seek time, which is generally about 10 milliseconds. The time required to rotate a disk to a specified position is the average latent time, which is about 4 ms for 7200-to-disk conversion.
1 second round (4 MS + 10 ms) = 71
What does this mean?
This indicates that the disk can only be located 71 times per second on average !!!
In short, it can be understood that the disk can only read 71 files per second (the actual situation is slightly different, please refer to the second chapter below)
If the server stores a large number of small files, each file is 10 KB, and the head location speed is 71 times per second, the speed of reading files per second can only reach kb/s.
Why is the speed of copying a PC film so fast?
When copying a disk, the head basically does not need to be moved. The main time is to let the head read data from the disk by rotating the disk.
The disk rotation speed is much higher than the head movement speed. The speed of reading consecutive files is generally higher than 100 Mb/s, so the speed of copying a video from a PC is very fast.
Io performance loss of file systems
The disk can be searched 70 times per second on average, but the actual number of read/write files can be less than 70, because there are also fragments in the file, so reading a file requires many searches.
We can think of the disk space as a huge memory. LBA (linear disk address) is equivalent to the memory address. The file system needs to allocate addresses for each file, just as malloc needs to allocate memory for each memory block.
Disk space allocation has two features, which may lead to fragmentation in the file system:
1. When we create a file, the file is empty. As we write data, the file becomes larger and larger. Therefore, disk space allocation is not completed at one time, but is gradually appended with file writing.
2 files do not need to be stored continuously in disk space, and can be cut into multiple fragments. However, the discontinuous cost is that multiple seek operations are required during reading, and the performance is greatly reduced.
Windows file systems have always been famous for their large volumes of file fragments, while linux mainstream file systems have fewer fragments.
Many friends think that linux does not have file fragments, which is a misunderstanding.
Compared with the windows File System, the mainstream linux File System has a huge improvement, greatly reducing file fragmentation, but this does not mean that there is no file fragmentation:
When windows allocates LBA, it tries to allocate it consecutively:
Assume that file A occupies the address 0-kb, and then file B occupies-kb when applying for 8 KB disk space. In this way, when file A applies for disk space again, fragments are generated.
Linux considers the reserved address when allocating LBA:
Assume that file A occupies the address 0-kb, and file B applies for an 8 KB disk space. For example, it may occupy 1 GB-1 GB + 8 KB. This ensures continuity when file A applies for disk space again.
When the disk has a large amount of available space, mainstream linux file systems can effectively avoid file fragmentation. For PCs and some servers, it can be considered that mainstream linux file systems do not have fragments.
However, for linux servers that frequently occupy a large amount of disk space and need to delete files constantly, file fragments may reduce disk I/O performance by several times.
For more information, see:
Design Principle of ext4 splitters: http://jsmylinux.no-ip.org/applications/using-e4defrag/
Xfs File System author explains how to improve file system performance and reduce the generation of fragments: http://oss.sgi.com/projects/xfs/papers/xfs_usenix/index.html
Basic Principle of kernel pre-read: http:// OS .51cto.com/art/200711/60574.htm
You can also directly view the ext4_ext_map_block function in the kernel code.