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