aboutsummaryrefslogtreecommitdiff
path: root/contrib/ntp/scripts/plot_summary.pl
blob: b0e9a4a1ad7fb447c8c1f5664579ec3373a85eed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/perl -w
# $Id: plot_summary.pl,v 1.2 1999/12/02 01:59:05 stenn Exp $
#
# Use Gnuplot to display data in summary files produced by summary.pl.
# This script requires GNUPLOT 3.7!
#
# Copyright (c) 1997, 1999 by Ulrich Windl <Ulrich.Windl@rz.uni-regensburg.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

require 5.003; # "never tested with any other version of Perl"
use strict;

use Time::Local;
use Getopt::Long;

# parse command line
my $summary_dir = "/tmp";
my $identifier = "host " . `hostname`;	# origin of these data
chomp $identifier;			# remove newline
my $offset_limit = 0.128;		# limit of absolute offset
my $output_file = "";			# output file defaults to stdout
my $output_file_number = 1;		# numbering of output files
my $gnuplot_terminal = $ENV{DISPLAY} ? "x11" : "dumb";
my $wait_after_plot = 1;
my @peer_list = ();

my %options = ("directory|input-directory=s" => \$summary_dir,
	       "identifier=s" => \$identifier,
	       "offset-limit=f" => \$offset_limit,
	       "output-file=s" => \$output_file,
	       "peer=s@" => \@peer_list,
	       "plot-term|gnuplot-term=s" => \$gnuplot_terminal,
	       "wait-after-plot!" => \$wait_after_plot,
	       );

if ( !GetOptions(%options) )
{
    print STDERR "valid options for $0 are:\n";
    my $opt;
    foreach $opt (sort(keys %options)) {
	print STDERR "\t--$opt\t(default is ";
	if ( ref($options{$opt}) eq "ARRAY" ) {
	    print STDERR join(", ",  map { "'$_'" } @{$options{$opt}});
	} else {
	    print STDERR "'${$options{$opt}}'";
	}
	print STDERR ")\n";
    }
    print STDERR "\n";
    die;
}

chomp $identifier;
die "illegal offset-limit: $offset_limit" unless $offset_limit > 0.0;
$offset_limit *= 1e6;			# scale to microseconds

# return the smallest value in the given list
sub min
{
    my ($result, @rest) = @_;
    map { $result = $_ if ($_ < $result) } @rest;
    return($result);
}

# return the largest value in the given list
sub max
{
    my ($result, @rest) = @_;
    map { $result = $_ if ($_ > $result) } @rest;
    return($result);
}

# maybe open alternate output file
sub open_output
{
    my $file;
    if ($output_file) {
	while ( -r ($file = "$output_file$output_file_number") ) {
	    ++$output_file_number;
	}
	open TOUCH, ">$file" and close TOUCH or die "$file: $!";
	print "set output \"$file\"\n";
    }
}

# make Gnuplot wait
sub maybe_add_pause
{
    print "pause -1 \"Press key to continue...\"\n" if $wait_after_plot;
}

# plot data from loop summary
sub do_loop
{
    my $fname = shift;
    my $line;
    my $out_file = "/tmp/tempdata$$";
    my $cmd_file = "/tmp/tempcmd$$";
    my ($first_day, $day_out) = ("", 0);
    my ($lower_bound, $upper_bound, $rms);
    my ($min_offs, $max_offs) = (1e9, -1e9);
    my ($min_rms, $max_rms) = (1e9, -1e9);
    open INPUT, "$fname" or die "$fname: $!";
    open OUTPUT, ">$out_file" or die "$out_file: $!";
    my @Fld;
    while (<INPUT>) {
	chop;	# strip record separator
	@Fld = split;
	if ($#Fld == 0) {
# loops.19960405
	    $_ = $Fld[0]; s/.*([12]\d{3}[01]\d[0-3]\d)$/$1/;
	    m/(\d{4})(\d{2})(\d{2})/;
	    $line = timegm(59, 59, 23, $3, $2 - 1, $1 - 1900, 0, 0, 0);
	    $line = int $line / 86400;	# days relative to 1970
	    $first_day = "$1-$2-$3 ($line)" unless $day_out;
	    next;
	}
	if ($#Fld != 8) {
	    warn "Illegal number of fields in file $fname, line $.";
	    next;
	}
# loop 216, 856106+/-874041.5, rms 117239.8, freq 67.52+/-10.335, var 4.850
	$_ = $Fld[1]; s/,/ /; $line .= " $_";
	$_ = $Fld[2]; m:(.+?)\+/-(.+),:;
	$lower_bound = $1 - $2;
	$upper_bound = $1 + $2;
	$line .= "$1 $lower_bound $upper_bound";
	$min_offs = min($min_offs, $lower_bound);
	$max_offs = max($max_offs, $upper_bound);
	$_ = $Fld[4]; s/,/ /; $rms = $_;
	$min_rms = min($min_rms, $rms);
	$max_rms = max($max_rms, $rms);
	$line .= " $rms";
	$_ = $Fld[6]; m:(.+?)\+/-(.+),:;
	$line .= " $1 " . ($1-$2) . " " . ($1+$2);
	$line .= " $Fld[8]";
	print OUTPUT "$line\n";
	$day_out = 1;
# 9621 216 856106 -17935.5 1730147.5 117239.8  67.52 57.185 77.855 4.850
    }
    close INPUT;
    close OUTPUT or die "close failed on $out_file: $!";
    my $ylimit = "[";
    if ($min_offs < -$offset_limit) {
	$ylimit .= "-$offset_limit";
    }
    $ylimit .= ":";
    if ($max_offs > $offset_limit) {
	$ylimit .= "$offset_limit";
    }
    if ( $ylimit eq "[:" ) {
	$ylimit = "";
    } else {
	$ylimit = "[] $ylimit]";
    }
# build command file for GNUplot
    open OUTPUT, "> $cmd_file" or die "$cmd_file: $!";
    my $oldfh = select OUTPUT;
    print "set term $gnuplot_terminal\n";
    open_output;
    print "set grid\n";
    print "set title \"Loop Summary for $identifier: " .
	"Daily mean values since $first_day\\n" .
	"(Offset limit is $offset_limit microseconds)\"\n";
    print "set ylabel \"[us]\"\n";
    print "set data style yerrorbars\n";
    print "set multiplot\n";
    print "set size 1, 0.5\n";
    print "set lmargin 8\n";
    print "set origin 0, 0.5\n";
    print "plot $ylimit \"$out_file\"" .
	" using 1:3:4:5 title \"mean offset\", ";
    print "\"$out_file\" using 1:(\$3-\$6/2) " .
	"title \"(sigma low)\" with lines, ";
    print "\"$out_file\" using 1:3 smooth bezier " .
	"title \"(Bezier med)\" with lines, ";
    print "\"$out_file\" using 1:(\$3+\$6/2) " .
	"title \"(sigma high)\" with lines\n";
    print "set ylabel \"[ppm]\"\n";
    print "set origin 0, 0.0\n";
    print "set title\n";
    print "set xlabel \"Days relative to 1970\"\n";
    print "plot \"$out_file\" using 1:7:8:9 title \"mean frequency\", ";
    print "\"$out_file\" using 1:(\$7-\$10/2) " .
	"title \"(sigma low)\" with lines, ";
    print "\"$out_file\" using 1:7 smooth bezier " .
	"title \"(Bezier med)\" with lines, ";
    print "\"$out_file\" using 1:(\$7+\$10/2) " .
	"title \"(sigma high)\" with lines\n";
    print "set nomultiplot\n";
    maybe_add_pause;

    $ylimit = "[";
    if ($min_rms < -$offset_limit) {
	$ylimit .= "-$offset_limit";
    }
    $ylimit .= ":";
    if ($max_rms > $offset_limit) {
	$ylimit .= "$offset_limit";
    }
    if ( $ylimit eq "[:" ) {
	$ylimit ="";
    } else {
	$ylimit = "[] $ylimit]";
    }

    open_output;
    print "set title \"Loop Summary for $identifier: " .
	"Standard deviation since $first_day\\n" .
	"(Offset limit is $offset_limit microseconds)\"\n";
    print "set xlabel\n";
    print "set ylabel \"[us]\"\n";
    print "set origin 0, 0.5\n";
    print "set data style linespoints\n";
    print "set multiplot\n";
    print "plot $ylimit \"$out_file\" using 1:6 title \"Offset\", ";
    print "\"$out_file\" using 1:6 smooth bezier " .
	"title \"(Bezier)\" with lines\n";
    print "set title\n";
    print "set origin 0, 0.0\n";
    print "set xlabel \"Days relative to 1970\"\n";
    print "set ylabel \"[ppm]\"\n";
    print "plot \"$out_file\" using 1:10 title \"Frequency\", ";
    print "\"$out_file\" using 1:10 smooth bezier " .
	"title \"(Bezier)\" with lines\n";
    print "set nomultiplot\n";
    maybe_add_pause;

    close OUTPUT or die "close failed on $cmd_file: $!";
    select $oldfh;
    print `gnuplot $cmd_file`;
    unlink $cmd_file;
    unlink $out_file;
}

# plot data form peer summary
sub do_peer
{
    my $fname = shift;
    my $peer = shift;
    my $out_file = "/tmp/tempdata$$";
    my $cmd_file = "/tmp/tempcmd$$";
    my $line;
    my ($first_day, $day_out) = ("", 0);
    open INPUT, "$fname" or die "$fname: $!";
    open OUTPUT, ">$out_file" or die "$out_file: $!";
    my @Fld;
    while (<INPUT>) {
	chop;	# strip record separator
	@Fld = split;
	if ($#Fld == 0) {
# peers.19960405
	    $_ = $Fld[0]; s/.*([12]\d{3}[01]\d[0-3]\d)$/$1/;
	    m/(\d{4})(\d{2})(\d{2})/ or next;
	    $line = timegm(59, 59, 23, $3, $2 - 1, $1 - 1900, 0, 0, 0);
	    $line = int $line / 86400;	# days relative to 1970
	    $first_day = "$1-$2-$3 ($line)" unless $day_out;
	    next;
	}
	if ($#Fld != 7) {
	    warn "Illegal number of fields in file $fname, line $.";
	    next;
	}
	next if ($Fld[0] ne $peer);
#       ident     cnt     mean     rms      max     delay     dist     disp
# 127.127.8.1       38   30.972  189.867 1154.607    0.000  879.760  111.037
	$Fld[0] = $line;
	print OUTPUT join(' ', @Fld) . "\n";
# 9969 38 30.972 189.867 1154.607 0.000 879.760 111.037
	$day_out = 1;
    }
    close INPUT;
    close OUTPUT or die "close failed on $out_file: $!";
    die "no data found for peer $peer" if !$day_out;
    open OUTPUT, "> $cmd_file" or die "$cmd_file: $!";
    my $oldfh = select OUTPUT;
    print "set term $gnuplot_terminal\n";
    open_output;
    print "set grid\n";
    print "set multiplot\n";
    print "set lmargin 8\n";
    print "set size 1, 0.34\n";
    print "set origin 0, 0.66\n";
    print "set title " .
	"\"Peer Summary for $peer on $identifier since $first_day\"\n";
    print "set data style linespoints\n";
    print "set ylabel \"[us]\"\n";
    print "plot \"$out_file\" using 1:3 title \"mean offset\", ";
    print "\"$out_file\" using 1:3 smooth bezier " .
	"title \"(Bezier)\" with lines, ";
    print "\"$out_file\" using 1:(\$3-\$7/2) " .
	"title \"(sigma low)\" with lines, ";
    print "\"$out_file\" using 1:(\$3+\$7/2) " .
	"title \"(sigma high)\" with lines\n";
    print "set title\n";
    print "set origin 0, 0.34\n";
    print "set size 1, 0.32\n";
    print "set ylabel\n";
    print "plot \"$out_file\" using 1:7 title \"dist\", ";
    print "\"$out_file\" using 1:7 smooth bezier " .
	"title \"(Bezier)\" with lines\n";
    print "set origin 0, 0.00\n";
    print "set size 1, 0.35\n";
    print "set xlabel \"Days relative to 1970\"\n";
    print "plot \"$out_file\" using 1:8 title \"disp\", ";
    print "\"$out_file\" using 1:8 smooth bezier " .
	"title \"(Bezier)\" with lines\n";
    print "set nomultiplot\n";
    maybe_add_pause;

    select $oldfh;
    close OUTPUT or die "close failed on $cmd_file: $!";
    print `gnuplot $cmd_file`;
    unlink $cmd_file;
    unlink $out_file;
}


my $loop_summary ="$summary_dir/loop_summary";
my $peer_summary ="$summary_dir/peer_summary";
my $clock_summary="$summary_dir/clock_summary";

do_loop $loop_summary;
map { do_peer $peer_summary, $_ } @peer_list;