Ten questions about Linux--file system

Source: Internet
Author: User
Tags php script inode usage

Ten questions about file systemReference: http://djt.qq.com/article/view/620 about Linux file system related issues: 1, mechanical disk random read and write speed is very slow, what is the operating system used to improve the random read and write performance? 2. Does touch a new empty file occupy disk space? How much does it take to occupy? 3. Do you want to create a new empty directory that consumes disk space? How much do you occupy? Which occupies a larger size than a new file? 4. Do you know where the file name is recorded on the disk? 5. How long is the filename? Under what constraints? 6, the file name is too long will affect system performance? Why does it have an impact? 7. How many files can be built in a directory? 8, create a new content size of 1k files, how much disk space will actually occupy? 9, to the operating system to initiate the Read File 2Byte command, the actual operating system will read how much? 10. How can we improve disk IO speed When we use files?
I. Disk composition and Partitioning 1. Disk Physical StructureThis article discusses only mechanical disks, and SSDs are not covered in this article. For managing disks, split disk faces, heads, tracks, cylinders, and sectors. disk Face: The disk is made up of a stack of disk faces, as shown in the left-hand image. Head (Heads): Each head corresponds to a disk face, which is responsible for reading and writing the data on the disk surface. tracks (track): Each disc is divided into concentric circles around the center of the circle, each of which is called a track. cylindrical surface (cylinders): The three-dimensional track consisting of the same position on all platters is called a cylindrical surface. sector (Sector): The disk is still too large to manage the disks, so the computer predecessors have divided each track into multiple sectors, and on the right, you can view the physical information of the disks used by the current system by using the FDISK command on Linux. You can see that my disk has 255 heads, which means there are 255 disks. 3,263 cylinders, which means there are 3,263 tracks on each disc, 63sectors/track says there are 63 sectors on each track. The command result also gives a value of sector size of 512bytes. So calculate the size of the disk, the result is 26.8G, and the total size of the disk match. 255 Disk face * 3263 CYLINDER * 63 sectors * 512bytes per sector = 26839088640byte. 2. PartitioningPartitioning is the first step of the operating system to manage disks, such as C, D, E, and F in Windows. What would you do if you had to divide the whole disk into partitions such as C and d?Scheme one: 255 disks, C disk is 0-100 disc, D disk is 101-200 disc,......Scenario Two: 3,263 cylinders, C-plate 0-1000 cylinders, D-plate, 1001-20001-cylinder,......First, the process of disk IO. The first step (Seek Time), the first is the radial movement of the head to find the track where the data resides. The second step (Rotation delay), locate the target track and rotate through the disk face to move the target sector directly below the head. The third step (Access Time) to read or write data to the target sector. So far, one disk IO is complete, so: single disk IO time = seek time + rotation delay + access time. For rotational delay, now the main server is often used 1W rpm disk, each rotation of the time required for a week is 60*1000ms/10000 to =6ms, so its rotational delay is (0-6ms). For access times, the general time is short, fraction Ms. For seek time, the modern disk is probably in 3-15ms, in which the seek time is mainly affected by the relative distance between the current position of the head and the location of the target track. adopt the first kind, the head needs to jump over more than 3,000 track intervals so that the disk's seek time doubles and disk performance degrades. adopt the second type of , if the disk C, only need to move the head in between 1-1000 tracks can be, greatly reducing the seek time. Therefore, the partitioning method of scenario two can reduce the disk IO time in the Seek time section, all operating systems are using scenario two, there is no plan one.  Note: In fact, the partition is not starting from 0, and the corresponding cylinder of the first track of the disk will be used to install the boot loader and the disk partition table. Back to the beginning of question 1, what are the techniques used by the operating system to reduce the performance of random Read and write? The operating system divides the partition by the cylinder corresponding to the track to reduce the seek time spent on disk IO, thus improving the read and write performance of the disk.

ii. Directories and documents 1. IntroIs the file system not a directory or a file? First to create an empty directory and empty files, see the results such as: The fifth column shows the amount of space occupied, then ask a few small questions.
(1) Why does the directory occupy 4096 of the space?    (2) Why are empty files occupying 0 of space? (3) If the empty file really occupies 0byte space, then the file name, creator and Permissions-rw-rw-r-folder related information to where to save?
2, empty files Occupy space

Enter Df–i to view inodes information:

Touch an empty file again df-i view inodes information:

This experiment proves that the operating system "deceives" us, and it consumes an inode. So what is the node size of the Inode, using the DUMPE2FS command can help us to see the actual size of this stuff.

In the result of the output we can find the following line:

It tells us that the size of each inode is 256Byte. Of course, the size of each machine will be different, it is actually in the system to format the disk when the decision.

Well, the second question has an answer. The original new empty file will occupy disk space, the actual consumption is 256Byte. Oh, no, the exact argument should be an inode size, and the specific value is determined at the time of formatting.

Let's talk about creating a new empty directory. The new empty directory consumes 4KB of disk space. So it's just that? We also use Df–i to monitor system inode usage before and after the new catalog.

The original directory will also occupy an Inode node, the third question also has the answer, a new empty directory will occupy disk space 4KB + inode size. Oh, this is not necessarily 4K on your system, it's actually a block size. Also can be seen under the DUMPE2FS.

But my disk in the format of the use of the size of 4KB, hehe!

3, the mysterious empty directory of 4KB

empty directory occupied by the 4KB, these spaces are used to save what? CD to a new directory to view.

We'll create a new two empty file, and then we'll look at the space usage of the directory below.

Because the empty file does not occupy block, it is still shown here that the directory occupies the block, and the previous size has not changed. Use a PHP script to create 100 empty files with a file name length of 32Byte.

At this point we found that the directory occupies a larger disk space, 3 blocks. This is the answer to our fourth question, the file name is stored in the directory occupied block . In addition, a new empty directory, created 100 file name length of 32*3 empty files, the temporary directory occupies the following disk space:

Why didn't the number of blocks occupied become 3 times times the size of the block? In fact, the Linux file system about the structure of the file, in addition to the filename, there are some other fields, the file name is 3 times times longer than the structure will be 3 times times larger, this can refer to the Linux system kernel-related books.

Well, now that's the beginning of question 6, there's an answer. A long file name will certainly have an impact on system performance, as this may result in more disk IO. Many programmers like to name the file a meaningful long string, so that people can see the file name to know the purpose. Of course I'm not saying it's bad, but if you have a fairly large number of files, you should consider whether your file name causes your directory block to take up too much. The space is small, the disk is cheap, but you have to think about the operating system when looking for files in the directory, the operating system may need to use the file name you provide for string comparison, and bad luck, you need to name all blocks in their names to do it again. (Of course, the length of your file name is not abnormal, and the number is not up to 100,000 orders of magnitude, actually this cost is not too big, but this overhead you still know for good)

As for the opening question 5, the file name is the longest. In fact, the Linux operating system is to avoid the programmer to use the long file name, impose a limit, not more than 255byte.

In addition, we have no experience, in the directory under a lot of files, we use the LS command is very slow. Now you know why, when the operating system is actually reading all the blocks of the current directory, if the block is more, it may require multiple IO operations to complete this simple ls command.

I have created 1100 w empty files in a directory of my own computer, the LS command has not been produced in 1 minutes, I dropped the CTRL + C. Do not do this in your own project, although the operating system can cache your directory data, so that your next call will be a lot of blocks, but I still recommend you in a single directory of the number of files not million. Otherwise, your program may have poor performance when it first runs after a reboot.

Well, back to the beginning of question 7, do you have an answer? The maximum number of files that can be built in a directory, which is actually limited to the inode count of the partition in which you have the directory, you have 100W inode, and you can create up to 100W files. However, it says, the number of files in a single directory is best not to be million, or it will bring system performance problems.


4, the File block

Let's do an experiment about the files. I created a new empty directory, and created a new file under it, only a blank space in the data, save the following the Du command is displayed as follows:

The 8 k in the 4 K is the directory, you can also calculate the operating system to contain only a single space file allocated 4KB. In fact, the file block is relatively simple, unlike the directory block will save a lot of file system structure, the file block will only save the file data. The above experiment shows that the operating system allocates space with block as the smallest unit . This means that as long as your file data is not empty, the operating system will assign you at least one block to store until you exceed 4KB, and the operating system assigns you the next block. So for the opening question 8, a new file with a content size of 1k will actually occupy 1 blocks (typically 4k) and an inode (typically 256byte).

In fact, when the file system initiates an IO request to disk, it is also in block size. Even if you only start reading files to the operating system 2Byte, but the operating system will give you a one-time reading 4KB back. So disk IO is really slow, and as long as we have access to the 2Byte, it is very likely to continue to access the next 2byte content, which is the principle of program localization, so the operating system simply once more read back. Oh, that's the answer to question 9.
It's like we're going to the supermarket and it's really a waste of time, and it's a lot slower than the pit daddy's disk IO. We will not stroll around the supermarket to buy an Apple will come back, we will certainly buy more things for the home after the need to prepare, anyway, buy a bunch of things than buy an apple also didn't spend much time, beheading for it, that's the truth.

Say the beginning of question 10, how to design your file can improve some IO speed? That is, if you know how much space your new file will occupy, such as 1M. Then, when you create a new file, say it to the operating system and let it keep the size of the file for you. In fact, the operating system will be as far as possible for you to allocate a continuous block, so you read this file, the head will save a lot of seek time, IO speed appears much faster.


Three, written in the back of the words

All we said was based on my own filesystem, the case is a block size is 4KB, an inode size is 256byte, including the number of inode on my virtual machine only 140多万个. These values are actually not fixed and you can set them to other values when formatting your hard drive.  The principle of setting is to look at the capacity of your hard drive and your use.  If your files are larger than 4KB, even a few m, a few grams of files, then suggest that your block is as large as possible, so that the inode can be less than a few addresses.  If your files are mostly 1K or less, then it is a bit of a waste to actually use a 4K block, so you can consider setting your block a bit smaller. Also, be concerned about the inode of your file system. The operating system hides the Inode node when viewing the disk space information occupied by the directory and the file, which is intended to provide the user with a white-box environment, to let us know the space occupied by the data, and to hide the inode information in order to reduce the difficulty of understanding the operating system. In fact, we, as non-ordinary users of developers should have this right to know. This stuff is directly related to your file system to create the number of files. Otherwise, you find that there is a lot of space left on the online machine disk, but the inode uses the light, and then only reformat or migrate the server, Tian Deng. These two operations think all feel bitter force ah, still can avoid to try to avoid it.

Ten questions about Linux--file system

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.