简体   繁体   English

触摸EXT4上的时间戳准确度

[英]touch timestamp accuracy on EXT4

Update : Turns out I was being very stupid. 更新 :事实证明我非常愚蠢。 I was checking the modification time when I should be checking the access time. 我正在检查修改时间,我应该检查访问时间。 The reason it was not reproducible was that the test files were made with dd if=/dev/urandom of="$target" bs='1K' count=1 || exit 1 它不可重复的原因是测试文件是用dd if=/dev/urandom of="$target" bs='1K' count=1 || exit 1 dd if=/dev/urandom of="$target" bs='1K' count=1 || exit 1 , which most of the time was too fast for the modification time (end of dd ) of the new files to be different from the access time (start time of dd ). dd if=/dev/urandom of="$target" bs='1K' count=1 || exit 1 ,其中大部分时间对于新文件的修改时间( dd结束)来说太快而与访问时间( dd开始时间)不同。 Another thing to watch out for. 另外需要注意的事项。

I'm working on a script to apply the access time of one file plus two years to another file. 我正在编写一个脚本,将一个文件的访问时间加上两年的时间应用到另一个文件中。 This uses stat -c %x , date --rfc-3339=ns and touch -a --date="$result" . 这使用stat -c %xdate --rfc-3339=nstouch -a --date="$result" stat and date both output date strings with nanoseconds, like statdate两个输出日期字符串都有纳秒,比如

2012-11-17 10:22:15.390351800+01:00

, and info coreutils 'touch invocation' says it supports nanoseconds. info coreutils 'touch invocation'说它支持纳秒。 But sometimes when applying touch there is a small difference between the timestamp applied and the one returned afterwards by stat. 但有时候在应用触摸时,应用的时间戳与之后返回的时间戳之间存在细微差别。 Here's data from an actual run: 这是来自实际运行的数据:

$ for i in {1..100}; do ./t_timecopy.sh 2>/dev/null| grep ASSERT; done
ASSERT:Expecting same access time expected:<2012-11-17 10:58:40.719320935+01:00> but was:<2012-11-17 10:58:40.723322203+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:00:04.342346275+01:00> but was:<2012-11-17 11:00:04.346358718+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:00:39.343348183+01:00> but was:<2012-11-17 11:00:39.347351686+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:01:08.655348312+01:00> but was:<2012-11-17 11:01:08.659347625+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:01:37.930346876+01:00> but was:<2012-11-17 11:01:37.934347311+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:02:16.939319832+01:00> but was:<2012-11-17 11:02:16.943323061+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:02:46.456443149+01:00> but was:<2012-11-17 11:02:46.458379114+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:03:15.487339595+01:00> but was:<2012-11-17 11:03:15.491341426+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:04:04.646335863+01:00> but was:<2012-11-17 11:04:04.650346634+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:04:14.410326608+01:00> but was:<2012-11-17 11:04:14.414331233+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:04:24.159367348+01:00> but was:<2012-11-17 11:04:24.163352418+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:04:33.931387953+01:00> but was:<2012-11-17 11:04:33.935350115+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:05:03.394361030+01:00> but was:<2012-11-17 11:05:03.398320957+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:05:42.054317430+01:00> but was:<2012-11-17 11:05:42.059106497+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:06:40.346320820+01:00> but was:<2012-11-17 11:06:40.350346956+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:08:17.194346778+01:00> but was:<2012-11-17 11:08:17.198338832+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:08:27.102347603+01:00> but was:<2012-11-17 11:08:27.106320380+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:09:16.247322948+01:00> but was:<2012-11-17 11:09:16.251347966+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:09:55.191325266+01:00> but was:<2012-11-17 11:09:55.195320672+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:12:09.915318301+01:00> but was:<2012-11-17 11:12:09.919334099+01:00>
ASSERT:Expecting same access time expected:<2012-11-17 11:12:28.906346914+01:00> but was:<2012-11-17 11:12:28.910348186+01:00>

So 21 out of 100 tests failed, with a mean of 3.938ms and a median of 4.001 ms. 因此,100个测试中有21个失败,平均值为3.938毫秒,中位数为4.001毫秒。 Any ideas what could cause this? 有什么想法会导致这种情况吗?

$ uname -a
Linux user 2.6.32-22-generic #33-Ubuntu SMP Wed Apr 28 13:27:30 UTC 2010 i686 GNU/Linux

Touch on Windows 7 64 bit brings similar problems. 触摸Windows 7 64位带来类似的问题。 This is my exploit code: 这是我的漏洞利用代码:

touch a && touch b && ls --full-time a b
touch -r a b && ls --full-time a b

And the output: 并输出:

-rw-rw-rw-  1 Jarek 0 0 2012-05-09 12:05:27.851839700 +0200 a
-rw-rw-rw-  1 Jarek 0 0 2012-05-09 12:05:27.874841000 +0200 b

-rw-rw-rw-  1 Jarek 0 0 2012-05-09 12:05:27.851839700 +0200 a
-rw-rw-rw-  1 Jarek 0 0 2012-05-09 12:05:27.851839000 +0200 b

ls and touch come from gnuwin32. lstouch来自gnuwin32。 In first 2 output lines there is a timestamp difference of 20 ms. 在前2个输出行中,存在20ms的时间戳差异。 Good. 好。 But in the second run they should be equal ( b took the stamp from a ). 但在第二轮他们应该是相等的( b把邮票从a )。 No luck. 没运气。 There is a difference of 0.7 us :). 相差0.7美元:)。

svn status sees the difference and hence it's hard to fool it with touch . svn status看到了差异,因此很难用touch来欺骗它。

I used this bunch of (admittedly quick & dirty) oneliners to test your issue on my system - a Mandriva Linux 2010.1 (x86-64): 我使用了一堆(公认的快速和肮脏)oneliners在我的系统上测试你的问题 - 一个Mandriva Linux 2010.1(x86-64):

seq 1 1000 | while read f; do sleep 0.01; touch test-$f-0; done

seq 1 1000 | while read f; do touch -a -d "$(stat -c %x test-$f-0 | sed 's|^2010|2012|')" test-$f-1; done

seq 1 1000 | while read f; do A="$(stat -c %x test-$f-0)"; B="$(stat -c %x test-$f-1)"; if [[ ! "${A#2010}" = "${B#2012}" ]]; then echo test-$f; fi; done

I was unable to reproduce your issue even once. 我甚至无法重现你的问题。 It sounds like touch is not fed the expected timestamp at the -d parameter, but something computed otherwise. 听起来好像没有在-d参数的预期时间戳中提供触摸,但是会以其他方式计算。

Of course the issue could be system-specific, in which case we'd need more information on your system (CPU, is the OS 32 or 64 bit, kernel/glibc/coreutils versions etc). 当然,问题可能是系统特定的,在这种情况下,我们需要有关系统的更多信息(CPU,OS 32或64位,内核/ glibc / coreutils版本等)。

UPDATE: 更新:

I tried the same with 32-bit versions of stat and touch. 我尝试使用32位版本的stat和touch。 No issues came up. 没有问题出现。 The kernel was still an 64-bit one. 内核仍然是64位的。

UPDATE2: UPDATE2:

I also tried this set of oneliners, that focus more on atime: 我也尝试过这套oneliner,更多关注atime:

$ seq 1 1000 | while read f; do sleep 0.01; touch test-$f-0; done
$ seq 1 1000 | while read f; do sleep 0.01; touch test-$f-1; done
$ seq 1 1000 | while read f; do sleep 0.01; cat test-$f-0; done
$ seq 1 1000 | while read f; do touch -a -d "$(stat -c %x test-$f-0 | sed 's|^2010|2012|')" test-$f-1; done
$ seq 1 1000 | while read f; do A="$(stat -c %x test-$f-0)"; B="$(stat -c %x test-$f-1)"; if [[ ! "${A#2010}" = "${B#2012}" ]]; then echo test-$f; fi; done

Again no issue detected. 再次没有检测到问题。 I tried this with both the relatime and strictatime mount options. 我试用了relatime和strictatime挂载选项。

UPDATE3: UPDATE3:

I just got to perform the tests above on my Mandriva i686 laptop. 我只需要在我的Mandriva i686笔记本电脑上进行上述测试。 I seem to get no issues with nanosecond accuracy there either. 我似乎也没有纳秒精度的问题。 I also verified on another 32bit system that if nanosecond accuracy is not supported (eg on ext3), the nanosecond field in the stat output becomes zero. 我还在另一个32位系统上验证,如果不支持纳秒精度(例如在ext3上),stat输出中的纳秒字段变为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM