12b15cb3dSCy Schubert#! @PATH_PERL@ -w
2*f5f40dd6SCy Schubert# @configure_input@
32b15cb3dSCy Schubert#
42b15cb3dSCy Schubert# drift of 104.8576 -> +1 tick.  Base  of 10000 ticks.
52b15cb3dSCy Schubert#
62b15cb3dSCy Schubert# 970306 HMS Deal with nanoseconds.  Fix sign of adjustments.
72b15cb3dSCy Schubertpackage calc_tickadj;
82b15cb3dSCy Schubertuse strict;
92b15cb3dSCy Schubert
102b15cb3dSCy Schubertexit run(@ARGV) unless caller;
112b15cb3dSCy Schubert
122b15cb3dSCy Schubertsub run {
132b15cb3dSCy Schubert    my $opts;
142b15cb3dSCy Schubert    if (!processOptions(\@_, $opts)) {
152b15cb3dSCy Schubert        usage(1);
162b15cb3dSCy Schubert    };
172b15cb3dSCy Schubert    my $drift_file = $opts->{'drift-file'};
182b15cb3dSCy Schubert    my $tick       = $opts->{'tick'};
192b15cb3dSCy Schubert
202b15cb3dSCy Schubert    if (!$tick) {
212b15cb3dSCy Schubert        my ($fl) = `tickadj`;
222b15cb3dSCy Schubert        if (defined $fl && $fl =~ /(?:KERNEL|PRESET)?\s*tick\s+=\s+(\d+)/) {
232b15cb3dSCy Schubert            $tick = $1;
242b15cb3dSCy Schubert        }
252b15cb3dSCy Schubert        else {
262b15cb3dSCy Schubert            die "Could not get tick value, try manually with -t/--tick\n";
272b15cb3dSCy Schubert        }
282b15cb3dSCy Schubert    }
292b15cb3dSCy Schubert
302b15cb3dSCy Schubert    # Drift file is in PPM where Milion is actually 2**20
312b15cb3dSCy Schubert    my $cvt   = (2 ** 20) / $tick;
322b15cb3dSCy Schubert    my $drift = 0.;
332b15cb3dSCy Schubert
342b15cb3dSCy Schubert    open my $dfh, $drift_file or die "Could not open $drift_file: $!\n";
352b15cb3dSCy Schubert
362b15cb3dSCy Schubert    $drift = <$dfh>;
372b15cb3dSCy Schubert
382b15cb3dSCy Schubert    close $dfh;
392b15cb3dSCy Schubert    die "Invalid drift file value <$drift>" if $drift !~ /[+-]?\d+\.?[0-9]+/;
402b15cb3dSCy Schubert
412b15cb3dSCy Schubert    while ($drift < 0) {
422b15cb3dSCy Schubert        $drift += $cvt;
432b15cb3dSCy Schubert        $tick--;
442b15cb3dSCy Schubert    }
452b15cb3dSCy Schubert
462b15cb3dSCy Schubert    while ($drift > $cvt) {
472b15cb3dSCy Schubert        $drift -= $cvt;
482b15cb3dSCy Schubert        $tick++;
492b15cb3dSCy Schubert    }
502b15cb3dSCy Schubert
512b15cb3dSCy Schubert    printf "%.3f (drift)\n", $drift;
522b15cb3dSCy Schubert    printf "%d usec; %d nsec\n", $tick, ($tick + ($drift/$cvt)) * 1000;
532b15cb3dSCy Schubert
542b15cb3dSCy Schubert    return 0;
552b15cb3dSCy Schubert}
562b15cb3dSCy Schubert
572b15cb3dSCy Schubert@calc_tickadj_opts@
582b15cb3dSCy Schubert
592b15cb3dSCy Schubert1;
602b15cb3dSCy Schubert__END__
61