The impact of Linux file pre-reading on the system

Source: Internet
Author: User

One of the most important performance points of the Linux system is its pagecache, because memory is much faster than IO, so everyone wants to get in the way of using the cache. File systems are no exception, in order to achieve high performance, file reads are usually pre-read to predict user behavior, the user may need to pre-read data to the cache to achieve high performance purposes.

The implementation of the Linux distribution ReadAhead varies greatly, and here we focus on the behavior of 2.6.18, RHEL 5u4 release. The implementation of file pre-reading is mainly in MM/READAHEAD.C, the code is only 603 lines. The pre-read process is probably the case, the user needs the file page when the entry function Do_generic_mapping_read will delegate page_cache_readahead to handle. It first determines whether the user's Io is sequential or random, and if it is random, there is no good pre-reading. In order, the read-ahead algorithm evaluates the pre-read window based on the usage of the last page read by the user, and determines how many pages to read. The module reading the page first checks to see if the page is already present in the Pagecache, and if it does not exist, it needs to initiate an IO request to read the corresponding page.

The key parameters of this read-ahead are 3: The user's req_size, the nr_to_read that the read-ahead algorithm evaluates, and the number of pages actually read by IO actual.

The next step is to see how the system works, so I first wrote a systemtap script called RATOP.STP to get the data:

Perl ">$ Uname-r2.6.18-164.el5$ rpm-i kernel-debuginfo-common-2.6.18-164.el5.x86_64.rpm$ rpm-i kernel-debuginfo-2.6.18-164.EL5.X86_64.RPM $ cat > RATOP.STP#!/usr/bin/stap-dmaxmapentries=10240global Total, Skipglobal req, To_read, Actualglobal __inode_filenameprobe Kernel.function ("Page_cache_readahead") {ino = __file_ino ($FILP) req[ino]+=$req _size;total++;If$ra->flags &0x2) skip++;} Probe Kernel.function ("__do_page_cache_readahead").Return{ino = __file_ino ($FILP) to_read[ino]+=$nr _to_read;If$return >0) actual[ino]+=$return;} Probe timer.ms (5000) {if (total) {foreach (ino in req-) {  S0+= REQ[ino]; s1+= To_read[ino] s2+= Actual[ino];}printf"\\n%25s,%5s%6D,%5s%6D,%5s%8D,%5s%8D,%5s%8d\\n\\n ", CTime (gettimeofday_s ()),  Total,  "Skip:", Skip,  "REQ:",S0,  "To_rd:", S1,  "Nr_rd:", s2 )/*Print Header*/ printf"%25s%8s%8s%8s\\n ",  "FILENAME","REQ","To_rd","Nr_rd") foreach (Ino in Req-limit20)  printf"%25s%8d%8d%8d\\n ", Find_filename (INO), reQ[ino], To_read[ino], Actual[ino]);}Delete total;Delete Skip;Delete req;Delete To_read;Delete actual;} Probe Generic.fop.open{__inode_filename[ino]= filename}function find_filename (ino) { return __inode_filename[ino]==chmod +x ratop.stp$ sudo.  /RATOP.STP:: Tue May 05:41:37, total:2321, skip:0, req:6308, to_rd:6308, Nr_rd:1424    FILENAME reqto_rdnr_rd   056878.sst 150   062889.SST 13 136. The meanings of each parameter are explained as follows: Total: How many read-ahead skipped by the system: due to the presence of the page in Pagecache, the number of pre-read req: How many pages the user is ready to read to_ RD: Read-ahead algorithm tells us the number of pages to read nr_rd: Number of pages read by the actual IO system This script prints the current pre-read status of the system every 5 seconds.  

Well, with this tool we can do the experiment.

First run our script under a terminal:

$ su do./RATOP.STP:: #等着出数据 ...

Then do the experiment under another terminal:

#准备个数据文件$ dd if=/dev/zero of=test count= 1024 Bs=40961024+0 records span class= "keyword" >in1024+0 records Out4194304 Bytes (4.2 MB) copied, 0. 008544 seconds, 491 mb/s #清空pagecache $ sudo sysctl vm.drop_caches=3vm.drop_caches = 3 #第一次拷贝 $ CP Test junk && sleep 5# Second copy $ CP Test junk            

We can see the following information in the previous script window:

#第一次拷贝Test, we can see that the user wants 1025 pages, pre-read decides to read 1084, but the actualIO read 1024, very reasonable, because at that timePagecache is empty.TueMay 31 05: 50: 21 2011,total:1038,skip:0,req:1039, to_rd:1320, nr_rd:1109 FILENAME req to_rd nr_rd test 1025 1084 1024x768 CP 3 ... #第二次拷贝 test, we can see the user to 1025 pages, pre-reading decided to read 284, but the actual io read 0, very reasonable, Because all the pages in pagecache inside are already exist tue may 05 : 50:46, total:1038, skip:804, req: 1039, to_rd:328, nr_rd:0 filename REQ to_rd nr_rd test 1025 284 0 CP 3 4 0 ...    

The Linux system not only provides automatic pre-reading for file reads, but also provides system calls and tools such as ReadAhead to help users proactively preload data, which we demonstrate under:

$ readahead Junk0 files (5 ms 

Another window says:

May: £ º:    readahead 3 4 0   

Linux also supports the default size of pre-read for each device, and different sizes can be used to control the amount of read-ahead, and the user can change it by themselves:

$ pwd/sys/block/sda/queue$ cat read_ahead_kb|sudo tee  read_ahead_kb   

Later I will use this tool to analyze the behavior of LEVELDB database, welcome attention!

Summary: If actual read more than the user req, then many of our pre-reading is wasted, you can consider reducing the size of pre-reading.

    • This article is from: Linux learning

The impact of Linux file pre-reading on the system

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.