]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/scripts/plot_summary.in
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / ntp / scripts / plot_summary.in
1 #! @PATH_PERL@ -w
2 # $Id$
3 #
4 # Use Gnuplot to display data in summary files produced by summary.pl.
5 # This script requires GNUPLOT 3.7!
6 #
7 # Copyright (c) 1997, 1999 by Ulrich Windl <Ulrich.Windl@rz.uni-regensburg.de>
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 package plot_summary;
23 use 5.006_000;
24 use strict;
25 use Time::Local;
26
27 my ($identifier, $offset_limit, $gnuplot_terminal, $wait_after_plot,
28     $output_file, $output_file_number);
29
30 exit run(@ARGV) unless caller;
31
32 sub run {
33     my $opts;
34     if (!processOptions(\@_, $opts)) {
35         usage(1);
36     }
37
38     $identifier  = $opts->{'identifier'};
39     if (!$identifier) {
40         $identifier = "host".`hostname`;
41         chomp $identifier;
42     }
43     $offset_limit       = $opts->{'offset-limit'};
44     $output_file        = $opts->{'output-file'};
45     $output_file_number = 1;
46     $gnuplot_terminal   = $opts->{'plot-terminal'}
47     || ( $ENV{DISPLAY} ? "x11" : "dumb" );
48     $wait_after_plot    = !$opts->{'dont-wait'};
49
50     die "illegal offset-limit: $offset_limit" unless $offset_limit > 0.0;
51     $offset_limit *= 1e6;                       # scale to microseconds
52
53     my $summary_dir = $opts->{'directory'};
54
55     my $loop_summary ="$summary_dir/loop_summary";
56     my $peer_summary ="$summary_dir/peer_summary";
57     my $clock_summary="$summary_dir/clock_summary";
58
59     my @peer_list   = @{$opts->{'peer'}};
60
61     do_loop($loop_summary);
62     do_peer($peer_summary, $_) for @peer_list;
63 }
64
65 # return the smallest value in the given list
66 sub min
67 {
68     my ($result, @rest) = @_;
69     map { $result = $_ if ($_ < $result) } @rest;
70     return($result);
71 }
72
73 # return the largest value in the given list
74 sub max
75 {
76     my ($result, @rest) = @_;
77     map { $result = $_ if ($_ > $result) } @rest;
78     return($result);
79 }
80
81 # maybe open alternate output file
82 sub open_output
83 {
84     my $file;
85     if ($output_file) {
86         while ( -r ($file = "$output_file$output_file_number") ) {
87             ++$output_file_number;
88         }
89         open TOUCH, ">$file" and close TOUCH or die "$file: $!";
90         print "set output \"$file\"\n";
91     }
92 }
93
94 # make Gnuplot wait
95 sub maybe_add_pause
96 {
97     print "pause -1 \"Press key to continue...\"\n" if $wait_after_plot;
98 }
99
100 # plot data from loop summary
101 sub do_loop
102 {
103     my $fname = shift;
104     my $line;
105     my $out_file = "/tmp/tempdata$$";
106     my $cmd_file = "/tmp/tempcmd$$";
107     my ($first_day, $day_out) = ("", 0);
108     my ($lower_bound, $upper_bound, $rms);
109     my ($min_offs, $max_offs) = (1e9, -1e9);
110     my ($min_rms, $max_rms) = (1e9, -1e9);
111     open INPUT, "$fname" or die "$fname: $!";
112     open OUTPUT, ">$out_file" or die "$out_file: $!";
113     my @Fld;
114     while (<INPUT>) {
115         chop;   # strip record separator
116         @Fld = split;
117         if ($#Fld == 0) {
118 # loops.19960405
119             $_ = $Fld[0]; s/.*([12]\d{3}[01]\d[0-3]\d)$/$1/;
120             m/(\d{4})(\d{2})(\d{2})/;
121             $line = timegm(59, 59, 23, $3, $2 - 1, $1 - 1900, 0, 0, 0);
122             $line = int $line / 86400;  # days relative to 1970
123             $first_day = "$1-$2-$3 ($line)" unless $day_out;
124             next;
125         }
126         if ($#Fld != 8) {
127             warn "Illegal number of fields in file $fname, line $.";
128             next;
129         }
130 # loop 216, 856106+/-874041.5, rms 117239.8, freq 67.52+/-10.335, var 4.850
131         $_ = $Fld[1]; s/,/ /; $line .= " $_";
132         $_ = $Fld[2]; m:(.+?)\+/-(.+),:;
133         $lower_bound = $1 - $2;
134         $upper_bound = $1 + $2;
135         $line .= "$1 $lower_bound $upper_bound";
136         $min_offs = min($min_offs, $lower_bound);
137         $max_offs = max($max_offs, $upper_bound);
138         $_ = $Fld[4]; s/,/ /; $rms = $_;
139         $min_rms = min($min_rms, $rms);
140         $max_rms = max($max_rms, $rms);
141         $line .= " $rms";
142         $_ = $Fld[6]; m:(.+?)\+/-(.+),:;
143         $line .= " $1 " . ($1-$2) . " " . ($1+$2);
144         $line .= " $Fld[8]";
145         print OUTPUT "$line\n";
146         $day_out = 1;
147 # 9621 216 856106 -17935.5 1730147.5 117239.8  67.52 57.185 77.855 4.850
148     }
149     close INPUT;
150     close OUTPUT or die "close failed on $out_file: $!";
151     my $ylimit = "[";
152     if ($min_offs < -$offset_limit) {
153         $ylimit .= "-$offset_limit";
154     }
155     $ylimit .= ":";
156     if ($max_offs > $offset_limit) {
157         $ylimit .= "$offset_limit";
158     }
159     if ( $ylimit eq "[:" ) {
160         $ylimit = "";
161     } else {
162         $ylimit = "[] $ylimit]";
163     }
164 # build command file for GNUplot
165     open OUTPUT, "> $cmd_file" or die "$cmd_file: $!";
166     my $oldfh = select OUTPUT;
167     print "set term $gnuplot_terminal\n";
168     open_output;
169     print "set grid\n";
170     print "set title \"Loop Summary for $identifier: " .
171         "Daily mean values since $first_day\\n" .
172         "(Offset limit is $offset_limit microseconds)\"\n";
173     print "set ylabel \"[us]\"\n";
174     print "set style data yerrorbars\n";
175     print "set multiplot\n";
176     print "set size 1, 0.5\n";
177     print "set lmargin 8\n";
178     print "set origin 0, 0.5\n";
179     print "plot $ylimit \"$out_file\"" .
180         " using 1:3:4:5 title \"mean offset\", ";
181     print "\"$out_file\" using 1:(\$3-\$6/2) " .
182         "title \"(sigma low)\" with lines, ";
183     print "\"$out_file\" using 1:3 smooth bezier " .
184         "title \"(Bezier med)\" with lines, ";
185     print "\"$out_file\" using 1:(\$3+\$6/2) " .
186         "title \"(sigma high)\" with lines\n";
187     print "set ylabel \"[ppm]\"\n";
188     print "set origin 0, 0.0\n";
189     print "set title\n";
190     print "set xlabel \"Days relative to 1970\"\n";
191     print "plot \"$out_file\" using 1:7:8:9 title \"mean frequency\", ";
192     print "\"$out_file\" using 1:(\$7-\$10/2) " .
193         "title \"(sigma low)\" with lines, ";
194     print "\"$out_file\" using 1:7 smooth bezier " .
195         "title \"(Bezier med)\" with lines, ";
196     print "\"$out_file\" using 1:(\$7+\$10/2) " .
197         "title \"(sigma high)\" with lines\n";
198     print "set nomultiplot\n";
199     maybe_add_pause;
200
201     $ylimit = "[";
202     if ($min_rms < -$offset_limit) {
203         $ylimit .= "-$offset_limit";
204     }
205     $ylimit .= ":";
206     if ($max_rms > $offset_limit) {
207         $ylimit .= "$offset_limit";
208     }
209     if ( $ylimit eq "[:" ) {
210         $ylimit ="";
211     } else {
212         $ylimit = "[] $ylimit]";
213     }
214
215     open_output;
216     print "set title \"Loop Summary for $identifier: " .
217         "Standard deviation since $first_day\\n" .
218         "(Offset limit is $offset_limit microseconds)\"\n";
219     print "set xlabel\n";
220     print "set ylabel \"[us]\"\n";
221     print "set origin 0, 0.5\n";
222     print "set style data linespoints\n";
223     print "set multiplot\n";
224     print "plot $ylimit \"$out_file\" using 1:6 title \"Offset\", ";
225     print "\"$out_file\" using 1:6 smooth bezier " .
226         "title \"(Bezier)\" with lines\n";
227     print "set title\n";
228     print "set origin 0, 0.0\n";
229     print "set xlabel \"Days relative to 1970\"\n";
230     print "set ylabel \"[ppm]\"\n";
231     print "plot \"$out_file\" using 1:10 title \"Frequency\", ";
232     print "\"$out_file\" using 1:10 smooth bezier " .
233         "title \"(Bezier)\" with lines\n";
234     print "set nomultiplot\n";
235     maybe_add_pause;
236
237     close OUTPUT or die "close failed on $cmd_file: $!";
238     select $oldfh;
239     print `gnuplot $cmd_file`;
240     unlink $cmd_file;
241     unlink $out_file;
242 }
243
244 # plot data form peer summary
245 sub do_peer
246 {
247     my $fname = shift;
248     my $peer = shift;
249     my $out_file = "/tmp/tempdata$$";
250     my $cmd_file = "/tmp/tempcmd$$";
251     my $line;
252     my ($first_day, $day_out) = ("", 0);
253     open INPUT, "$fname" or die "$fname: $!";
254     open OUTPUT, ">$out_file" or die "$out_file: $!";
255     my @Fld;
256     while (<INPUT>) {
257         chop;   # strip record separator
258         @Fld = split;
259         if ($#Fld == 0) {
260 # peers.19960405
261             $_ = $Fld[0]; s/.*([12]\d{3}[01]\d[0-3]\d)$/$1/;
262             m/(\d{4})(\d{2})(\d{2})/ or next;
263             $line = timegm(59, 59, 23, $3, $2 - 1, $1 - 1900, 0, 0, 0);
264             $line = int $line / 86400;  # days relative to 1970
265             $first_day = "$1-$2-$3 ($line)" unless $day_out;
266             next;
267         }
268         if ($#Fld != 7) {
269             warn "Illegal number of fields in file $fname, line $.";
270             next;
271         }
272         next if ($Fld[0] ne $peer);
273 #       ident     cnt     mean     rms      max     delay     dist     disp
274 # 127.127.8.1       38   30.972  189.867 1154.607    0.000  879.760  111.037
275         $Fld[0] = $line;
276         print OUTPUT join(' ', @Fld) . "\n";
277 # 9969 38 30.972 189.867 1154.607 0.000 879.760 111.037
278         $day_out = 1;
279     }
280     close INPUT;
281     close OUTPUT or die "close failed on $out_file: $!";
282     die "no data found for peer $peer" if !$day_out;
283     open OUTPUT, "> $cmd_file" or die "$cmd_file: $!";
284     my $oldfh = select OUTPUT;
285     print "set term $gnuplot_terminal\n";
286     open_output;
287     print "set grid\n";
288     print "set multiplot\n";
289     print "set lmargin 8\n";
290     print "set size 1, 0.34\n";
291     print "set origin 0, 0.66\n";
292     print "set title " .
293         "\"Peer Summary for $peer on $identifier since $first_day\"\n";
294     print "set style data linespoints\n";
295     print "set ylabel \"[us]\"\n";
296     print "plot \"$out_file\" using 1:3 title \"mean offset\", ";
297     print "\"$out_file\" using 1:3 smooth bezier " .
298         "title \"(Bezier)\" with lines, ";
299     print "\"$out_file\" using 1:(\$3-\$7/2) " .
300         "title \"(sigma low)\" with lines, ";
301     print "\"$out_file\" using 1:(\$3+\$7/2) " .
302         "title \"(sigma high)\" with lines\n";
303     print "set title\n";
304     print "set origin 0, 0.34\n";
305     print "set size 1, 0.32\n";
306     print "set ylabel\n";
307     print "plot \"$out_file\" using 1:7 title \"dist\", ";
308     print "\"$out_file\" using 1:7 smooth bezier " .
309         "title \"(Bezier)\" with lines\n";
310     print "set origin 0, 0.00\n";
311     print "set size 1, 0.35\n";
312     print "set xlabel \"Days relative to 1970\"\n";
313     print "plot \"$out_file\" using 1:8 title \"disp\", ";
314     print "\"$out_file\" using 1:8 smooth bezier " .
315         "title \"(Bezier)\" with lines\n";
316     print "set nomultiplot\n";
317     maybe_add_pause;
318
319     select $oldfh;
320     close OUTPUT or die "close failed on $cmd_file: $!";
321     print `gnuplot $cmd_file`;
322     unlink $cmd_file;
323     unlink $out_file;
324 }
325
326 @plot_summary_opts@
327
328 1;
329 __END__