xref: /freebsd/contrib/ntp/scripts/calc_tickadj/calc_tickadj.in (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1#! @PATH_PERL@ -w
2# @configure_input@
3#
4# drift of 104.8576 -> +1 tick.  Base  of 10000 ticks.
5#
6# 970306 HMS Deal with nanoseconds.  Fix sign of adjustments.
7package calc_tickadj;
8use strict;
9
10exit run(@ARGV) unless caller;
11
12sub run {
13    my $opts;
14    if (!processOptions(\@_, $opts)) {
15        usage(1);
16    };
17    my $drift_file = $opts->{'drift-file'};
18    my $tick       = $opts->{'tick'};
19
20    if (!$tick) {
21        my ($fl) = `tickadj`;
22        if (defined $fl && $fl =~ /(?:KERNEL|PRESET)?\s*tick\s+=\s+(\d+)/) {
23            $tick = $1;
24        }
25        else {
26            die "Could not get tick value, try manually with -t/--tick\n";
27        }
28    }
29
30    # Drift file is in PPM where Milion is actually 2**20
31    my $cvt   = (2 ** 20) / $tick;
32    my $drift = 0.;
33
34    open my $dfh, $drift_file or die "Could not open $drift_file: $!\n";
35
36    $drift = <$dfh>;
37
38    close $dfh;
39    die "Invalid drift file value <$drift>" if $drift !~ /[+-]?\d+\.?[0-9]+/;
40
41    while ($drift < 0) {
42        $drift += $cvt;
43        $tick--;
44    }
45
46    while ($drift > $cvt) {
47        $drift -= $cvt;
48        $tick++;
49    }
50
51    printf "%.3f (drift)\n", $drift;
52    printf "%d usec; %d nsec\n", $tick, ($tick + ($drift/$cvt)) * 1000;
53
54    return 0;
55}
56
57@calc_tickadj_opts@
58
591;
60__END__
61