代码改变世界

Linux中atime,ctime与mtime的细节疑问总结

2024-04-29 23:04  潇湘隐者  阅读(34)  评论(0编辑  收藏  举报

在Linux文件系统中,每一个文件都有三个时间属性,它们分别是atime,mtime,ctime,一般来说,atime比较好理解,但是很多时候,我们往往会混淆mtime和ctime这两个时间属性,或者搞不清楚两者的区别。在展开介绍之前,我们先来看看如何查看文件的atime,mtime,ctime属性。 下面简单介绍一下:

查看atime属性

$ ls -lu

查看mtime属性

$ ls -l

查看ctime属性

$ ls -lc

具体可以查看命令ls的帮助手册(man ls)

-c     with -lt: sort by, and show, ctime (time of last modification of file status information); with -l: show ctime and
              sort by name; otherwise: sort by ctime, newest first

-u     with  -lt:  sort  by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access
              time, newest first

使用stat命令查看文件的atime/mtime/ctime属性

stat filename

下面是是一个构造的例子,如下所示:

[mysql@dbtest04 kerry]$ stat test.log
  File: test.log
  Size: 5               Blocks: 8          IO Block: 4096   regular file
Device: fd05h/64773d    Inode: 1193621     Links: 1
Access: (0777/-rwxrwxrwx)  Uid: (  801/   mysql)   Gid: (  800/   mysql)
Access: 2024-04-29 11:03:21.616417230 +0800
Modify: 2024-04-29 11:01:56.599421770 +0800
Change: 2024-04-29 11:34:16.385318171 +0800
 Birth: 2024-04-29 11:00:02.907427842 +0800
[mysql@dbtest04 kerry]$ ls -l test.log
-rwxrwxrwx 1 mysql mysql 5 Apr 29 11:01 test.log
[mysql@dbtest04 kerry]$ ls -lu test.log
-rwxrwxrwx 1 mysql mysql 5 Apr 29 11:03 test.log
[mysql@dbtest04 kerry]$ ls -lc test.log
-rwxrwxrwx 1 mysql mysql 5 Apr 29 11:34 test.log

那么接下来,我们来看看atime/mtime/ctime三者的定义吧。

那么我们接下来看看ctime与mtime的区别,一般来说,如果修改了文件的权限,属主等属性时,ctime会变化(mtime不会变化),修改文件的内容时,mtime & ctime都会变化,也就是说如果mtime变化了的话,ctime一定会变化,而ctime变化,mtime不一定变化。 其实ctime表示最近一次metadata修改时间,这里的修改有两层意思:

  • 1 修改文件/文件夹的metadata, 例如,文件的访问权限(chmod)或文件属主。

  • 2 修改文件内容

如果简单一点的描述,就是mtime就是ctime的一个子集,ctime包含mtime,如果mtime变化了,ctime一定会变化。如下测试所示(这里我们用root账号来测试,方便修改文件的属主以及权限。)

[root@dbtest04 ~]# touch test.log
[root@dbtest04 ~]# stat test.log
  File: test.log
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 2837        Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2024-04-29 12:03:01.741226024 +0800
Modify: 2024-04-29 12:03:01.741226024 +0800
Change: 2024-04-29 12:03:01.741226024 +0800
 Birth: 2024-04-29 12:03:01.741226024 +0800
[root@dbtest04 ~]# chown mysql:mysql test.log
[root@dbtest04 ~]# stat test.log
  File: test.log
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 2837        Links: 1
Access: (0644/-rw-r--r--)  Uid: (  801/   mysql)   Gid: (  800/   mysql)
Access: 2024-04-29 12:03:01.741226024 +0800
Modify: 2024-04-29 12:03:01.741226024 +0800
Change: 2024-04-29 12:03:48.956223503 +0800
 Birth: 2024-04-29 12:03:01.741226024 +0800
[root@dbtest04 ~]# chmod 755 test.log
[root@dbtest04 ~]# stat test.log
  File: test.log
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 2837        Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (  801/   mysql)   Gid: (  800/   mysql)
Access: 2024-04-29 12:03:01.741226024 +0800
Modify: 2024-04-29 12:03:01.741226024 +0800
Change: 2024-04-29 12:04:22.778221696 +0800
 Birth: 2024-04-29 12:03:01.741226024 +0800
[root@dbtest04 ~]# echo "it is test" > test.log
[root@dbtest04 ~]# stat test.log
  File: test.log
  Size: 11              Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 2837        Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (  801/   mysql)   Gid: (  800/   mysql)
Access: 2024-04-29 12:03:01.741226024 +0800
Modify: 2024-04-29 12:04:57.245219856 +0800
Change: 2024-04-29 12:04:57.245219856 +0800
 Birth: 2024-04-29 12:03:01.741226024 +0800

不知道你注意到没有,往文件写入内容的时候,atime居然没有变化,这里为了验证,我们再做一次测试,如下截图所示

如上截图所示,我们往文件中写入内容,atime居然没有变化,其实你使用more、cat命令访问文件,文件的atime也没有变化,Why?Why?Why?直到我看到这篇文章"文件atime未变问题的研究"[1](下面部分内容来自这篇文章):

在kernel版本2.6.30之前,Linux的核心开发人员针对Ext3/Ext4文件系统的性能进行了讨论,其中包括atime。在Ksernel 2.6.30之前,文件系统中默认会及时的更新atime,这样会带来两个问题:

  1. 系统中大量的文件访问,将atime写入到磁盘中,消耗时间,从而降低性能

  2. 这样的操作也会消耗电能

在Linux上运行的,很少的应用程序需要获取精确的atime时间,并且Linux核心开发人员从Ext3/Ext4文件系统的性能角度出发,决定在2.6.30版本的内核中修改atime的更新方式,只有在以下三种情况之一才会更新atime:

  • (1) 如果将分区mount的挂载的时候指定采用非relatime方式(默认采用relatime方式),如strictatime.

补充:在OS启动的时候,将各个分区挂载到不同的目录,在挂载(mount)的参数中采用strictatime,表明及时更新atime。在2.6.30之后mount添加了”relatime”和”strictatime”两个选项,详细的可以通过”man mount”查看。

  • (2) atime小于ctime或者小于mtime的时候

  • (3) 本次的access time和上次的atime超过24个小时

这种做法避免了频繁的更新atime,提高了文件系统的性能。果然做Linux内核的大牛无不从每一个细节抓起呢,敬佩。

更多的详细信息,我们可以参考这篇文章[2],这里额外展开一下,ls 和 stat命令并不会修改文件的atime属性,如下测试所示

[mysql@dbtest04 kerry]$ rm kerry.log
[mysql@dbtest04 kerry]$ touch kerry.log
[mysql@dbtest04 kerry]$ stat kerry.log
  File: kerry.log
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd05h/64773d    Inode: 1193622     Links: 1
Access: (0644/-rw-r--r--)  Uid: (  801/   mysql)   Gid: (  800/   mysql)
Access: 2024-04-29 15:35:58.084543673 +0800
Modify: 2024-04-29 15:35:58.084543673 +0800
Change: 2024-04-29 15:35:58.084543673 +0800
 Birth: 2024-04-29 15:35:58.084543673 +0800
[mysql@dbtest04 kerry]$ ls kerry.log
kerry.log
[mysql@dbtest04 kerry]$ stat kerry.log 
  File: kerry.log
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd05h/64773d    Inode: 1193622     Links: 1
Access: (0644/-rw-r--r--)  Uid: (  801/   mysql)   Gid: (  800/   mysql)
Access: 2024-04-29 15:35:58.084543673 +0800
Modify: 2024-04-29 15:35:58.084543673 +0800
Change: 2024-04-29 15:35:58.084543673 +0800
 Birth: 2024-04-29 15:35:58.084543673 +0800

参考资料

[1]

1: https://blog.csdn.net/cjf_iceking/article/details/11988525

[2]

2: http://www.h-online.com/open/news/item/Kernel-Log-What-s-coming-in-2-6-30-File-systems-New-and-revamped-file-systems-741319.html