require 5.003; use strict;
use Time::Local;
use Getopt::Long;
my $summary_dir = "/tmp";
my $identifier = `hostname`; chomp $identifier; my $offset_limit = 0.128; my $output_file = ""; my $output_file_number = 1; my $gnuplot_terminal = $ENV{DISPLAY} ? "x11" : "dumb";
my $wait_after_plot = 1;
my @peer_list = ();
my %options = ("directory=s" => \$summary_dir,
"identifier=s" => \$identifier,
"offset-limit=f" => \$offset_limit,
"output-file=s" => \$output_file,
"peer=s@" => \@peer_list,
"plot-term=s" => \$gnuplot_terminal,
"wait-after-plot!" => \$wait_after_plot,
);
if ( !GetOptions(%options) ) {
print STDERR "usage: $0\n";
my $opt;
foreach $opt (sort(keys %options)) {
print STDERR "\t--$opt ";
if ( ref($options{$opt}) eq "ARRAY" ) {
print STDERR "(" . join (" ", @{$options{$opt}}) . ")\n";
} else {
print STDERR "(${$options{$opt}})\n";
}
}
print STDERR "\n";
die;
}
chomp $identifier;
die "illegal offset-limit: $offset_limit" unless $offset_limit > 0.0;
$offset_limit *= 1e6;
sub min
{
my ($result, @rest) = @_;
map { $result = $_ if ($_ < $result) } @rest;
return($result);
}
sub max
{
my ($result, @rest) = @_;
map { $result = $_ if ($_ > $result) } @rest;
return($result);
}
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";
}
}
sub maybe_add_pause
{
print "pause -1 \"Press key to continue...\"\n" if $wait_after_plot;
}
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; @Fld = split;
if ($ $_ = $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; $first_day = "$1-$2-$3 ($line)" unless $day_out;
next;
}
if ($ warn "Illegal number of fields in file $fname, line $.";
next;
}
$_ = $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;
}
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]";
}
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;
my $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;
}
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; @Fld = split;
if ($ $_ = $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; $first_day = "$1-$2-$3 ($line)" unless $day_out;
next;
}
if ($ warn "Illegal number of fields in file $fname, line $.";
next;
}
next if ($Fld[0] ne $peer);
$Fld[0] = $line;
print OUTPUT join(' ', @Fld) . "\n";
$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;