磁盤文件讀性能測試

未緩存前:html

time ./x bin.tar node

file size is 816322560linux

816322560 bytes read now緩存

real    0m3.378sdom

user    0m0.000s性能

sys     0m0.996s測試

 

被緩存後:優化

time ./x bin.tar spa

file size is 816322560htm

816322560 bytes read now

real    0m0.770s

user    0m0.000s

sys     0m0.768s

 

硬盤讀取性能:

hdparm -t /dev/sdb 

/dev/sdb:

 Timing buffered disk reads: 2454 MB in  3.00 seconds = 817.84 MB/sec

 

10塊物理磁盤,作了Raid10,所以讀性能高,達每秒817.84MB

 

測試程序:

// 非優化方式編譯:g++ -g -o x x.cpp

#include <errno.h>

#include <fcntl.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <unistd.h>

 

// 帶一個參數,爲被讀取文件的大小

int main(int argc, char* argv[])

{

        int fd = open(argv[1], O_RDONLY);

        if (-1 == fd)

        {

                perror(strerror(errno));

                exit(1);

        }

 

        struct stat st;

        if (0 == fstat(fd, &st))

        {

                // 輸出文件大小,單位爲字節

                printf("file size is %d\n", st.st_size);

 

                // 一次性將整個文件讀到內存中

                char* bytes = new char[st.st_size];

                int bytes_read = read(fd, bytes, st.st_size);

 

                // 顯示實際成功讀取的字節數

                printf("%d bytes read now\n", bytes_read);

                delete []bytes;

        }

 

        close(fd);

        return 0;

}

 

清緩存:

使用free命令觀察下列操做的變化,以root用戶執行:先執行下sync命令,以將數據更新到磁盤,再執行echo 3 > /proc/sys/vm/drop_caches,以清除系統的cached

 

文件內存的緩存會反應出free命令輸出的cached值的變化,實際就是Page cache,文件內容的讀取會緩存在這裏。若是讀取一個大文件,能夠看到cached的值明顯增漲,而且增漲大小差很少就是文件的大小,buffers至關於cached的元信息,好比文件的inode

 

cached影響文件的讀取性能,而buffers影響到文件的打開性能。

 

drop_caches使用匯總:

echo 0 > /proc/sys/vm/drop_caches

不作釋放

echo 1 > /proc/sys/vm/drop_caches

釋放Page Cache

echo 2 > /proc/sys/vm/drop_caches

釋放Dentries/inodes Cache(其中,Dentry是目錄信息,inode是文件信息)

echo 3 > /proc/sys/vm/drop_caches

釋放PageDentries/inodes Cache

 

這個特性由2.6.16內核開始提供,參考資料:

1) /proc/sys/vm/drop_caches

http://linux-mm.org/Drop_Caches

2) Linux Buffer vs Cache explained

http://random-techbits.blogspot.com/2012/06/linux-buffer-vs-cache-explained.html

相關文章
相關標籤/搜索