相信對性能、優(yōu)化這些關鍵字有興趣的朋友都知道在 Linux 下面掛載文件系統(tǒng)的時候設置 noatime 可以顯著提高文件系統(tǒng)的性能。默認情況下,Linux ext2/ext3 文件系統(tǒng)在文件被訪問、創(chuàng)建、修改等的時候記錄下了文件的一些時間戳,比如:文件創(chuàng)建時間、最近一次修改時間和最近一次訪問時間。因為系統(tǒng)運行的時候要訪 問大量文件,如果能減少一些動作(比如減少時間戳的記錄次數(shù)等)將會顯著提高磁盤 IO 的效率、提升文件系統(tǒng)的性能。Linux 提供了 noatime 這個參數(shù)來禁止記錄最近一次訪問時間戳。
給文件系統(tǒng)掛載的時候加上 noatime 參數(shù)能大幅提高文件系統(tǒng)性能:
# vi /etc/fstab
/dev/sda1 / ext3 defaults,noatime,errors=remount-ro 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
proc /proc proc defaults 0 0
/dev/sda2 swap swap defaults,noatime 0 0
修改設置后只需要重新掛載文件系統(tǒng)、不需要重啟就可以應用新設置:
# mount -o remount /
# mount
/dev/sda1 on / type ext3 (rw,noatime,errors=remount-ro)
proc on /proc type proc (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
網(wǎng)上很多資料都提到要同時設置 noatime 和 nodiratime,不知道這個結論來自哪里,其實不需要像設置 noatime 那樣設置 nodiratime,最可靠的資料應該是源代碼,VPSee 查了一下源代碼,發(fā)現(xiàn)在內(nèi)核源代碼 linux-2.6.33/fs/inode.c 文件里有一個 touch_atime 函數(shù),可以看出如果 inode 的標記位是 NOATIME 的話就直接返回了,根本就走不到 NODIRATIME 那里去,所以只設置 noatime 就可以了,不必再設置 nodiratime.
void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
1405{
1406 struct inode *inode = dentry->d_inode;
1407 struct timespec now;
1408
1409 if (inode->i_flags & S_NOATIME)
1410 return;
1411 if (IS_NOATIME(inode))
1412 return;
1413 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
1414 return;
1415
1416 if (mnt->mnt_flags & MNT_NOATIME)
1417 return;
1418 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1419 return;
...
1435}