我看到Perl的Time::HiRes
模塊報告的時間戳有一些奇怪的行爲。爲什麼這些時間戳與Perl Time :: HiRes不同?
我有一個腳本,達到三星時間戳:
- 獲取時間戳和
Time::HiRes::time
- 創建一個新的文件,並獲取其修改時間與
Time::HiRes::stat
- 獲取時間戳和
Time::HiRes::time
我希望訂購時間戳1 < 2 < 3
,但事實並非總是如此;經常(但不總是),時間由stat
報告2.是之前時間戳從1 ..
我在Ext4文件系統上。這裏是一個實驗:
use Time::HiRes qw/ time stat /;
while(1){
# t0
my $t0 = time;
# Create a file
my $f = '/tmp/dummy.test';
open(my $fh, '>', $f) || die;
print $fh "hi\n";
close($fh) || die;
# FS: file modification time, according to the filestystem
my $fs = (stat($f))[9];
# t1
my $t1 = time;
## Report how the timestamps relate to each other
# A. All good
if($t0 < $fs && $fs < $t1){
print "$t1,0\n";
}
# B. FS before t0
elsif($t0 > $fs && $fs < $t1){
print "$t1,1\n";
}
# C. FS after t1
elsif($t0 < $fs && $fs > $t1){
print "$t1,2\n";
}
# D. this should never happen (t0 and t1 probably can't flip)
elsif($t0 > $fs && $fs > $t1){
print "$t1,3\n";
}
}
這裏是讓上面的循環運行幾秒鐘的結果。底部的藍色點是「正確」行爲的事件。通常情況下,我得到條件B
,其中stat
的修改時間爲之前的第一個時間戳。
什麼能解釋這種現象?
更新:這裏的時間戳的曲線滯後的拳頭2000次迭代:
'$ t0- $ fs,「」,$ t1- $ fs'顯示$ t0和$ t1都非常接近,遠遠超過$ fs –