I'm seeing some strange behaviour with the timestamp reported by Perl's Time::HiRes
module.
I have a script that gets three timestamps:
- Get timestamp with
Time::HiRes::time
- Create a new file, and get its modification time with
Time::HiRes::stat
- Get timestamp with
Time::HiRes::time
I'd expect the timestamps to be ordered1 < 2 < 3
, however this isn't always the case; often (but not always), the time reported by stat
in 2. is before the timestamp from 1..
I'm on an Ext4 filesystem. Here's an experiment:
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";
}
}
Here are the results of letting the above loop run for a few seconds. where the blue points on the bottom are incidents of the "correct" behaviour. More often than not, I get condition B
, where the modification time from stat
is before the first timestamp.
What could explain this behaviour?
Update: Here's a plot of the timestamp lags for the fist 2000 iterations: