check-top 26.1 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# Copyright (c) 2007 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#

#
# checktop - a script for checking, manipulating, and normalizing top/ptop
# files from various versions of assign. Really, it's a full top/ptop file
# parser.
#

#
# TODO list:
# Normailzation:
#       Remove LAN nodes from old ptop files? (put LAN node on switches
#           instead)
#       Rename nodes in virtual topologies

use strict;

use Getopt::Std;

my %opt;
Robert Ricci's avatar
Robert Ricci committed
43
my $optlist = "ltL:evDnrRsa";
44
if (!getopts($optlist,\%opt)){ 
Robert Ricci's avatar
Robert Ricci committed
45
    warn "Usage: $0 [-r] [-R] [-l] [-e] [-t] [-L limit] [-v] [-D] [-n] [-s]\n";
46
    warn " -r   Remove any files that fail the check\n";
Robert Ricci's avatar
Robert Ricci committed
47
    warn " -R   Rename any files that fail the check\n";
48 49 50 51 52 53 54 55 56 57 58
    warn " -l   List files that pass the check to stdout\n";
    warn " -e   Print parse errors from failed files to stderr\n";
    warn " -t   When finished, dump information about all types and\n";
    warn "        features seen to stdout\n";
    warn " -L   Stop after about <limit> files have been processed\n";
    warn " -v   Print some progress information to stderr\n";
    warn " -D   Include debugging output\n";
    warn " -n   Normalize all files by re-writing them. Strips out some\n";
    warn "        private information\n";
    warn " -s   Strict checking - only pass files that would be parsed by\n";
    warn "        latest version of assign\n";
Robert Ricci's avatar
Robert Ricci committed
59
    warn " -a   Enable anonymization\n";
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
    exit 1;
}

#
# Lists of legal flags 
#
my %ptop_nodeflags =  (
    'trivial_bw' => 'int',
    'subnode_of' => 'string',
    'unique' => 'none'
);

my %top_nodeflags = (
    'disallow_trivial_mix' => 'none',
    'subnode_of' => 'string'
);

my %top_linkflags = (
    'nodelay' => 'none',
    'emulated' => 'none',
    'trivial_ok' => 'none',
    'fixsrciface' => 'string',
    'fixdstface' => 'string'
);

#
# Check to see if a given feature is allowed or if it should be filtered
#
sub is_feature_okay($) {
    my ($feature) = @_;
Robert Ricci's avatar
Robert Ricci committed
90 91 92 93 94 95 96 97
    
    # If we are not anonymizing, all features are considered okay
    if (!$opt{a}) {
        return 1;
    }
    
    # If anyonymizing, don't let through OS features that expose the project
    # name
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
    if ($feature =~ /^OS-/) {
        # OSes in the emulab-ops project are okay, as are ones that are just
        # specified with a numeric ID
        if ($feature =~ /^OS-\d+$/ || $feature =~ /^OS-emulab-ops/) {
            #print "Allowing OS feature $feature\n"; 
            return 1;
        } else {
            #print "Denying OS feature $feature\n";
            return 0;
        }
    } else {
        # Right now, everything that isn't an OS is considered 'safe'
        return 1;
    }
}

#
# Make a list of types, features, and desires that we see, so that we can be
# reasonably sure we're only seeing real ones
#
my %seen_node_types;
my %seen_link_types;
my %seen_fds;

sub merge_sets($$) {
    my ($global, $local) = @_;
    foreach my $key (keys %$local) {
        $global->{$key} = 1;
    }
}

sub check_type($$) {
    my ($type, $string) = @_;
    my $passed;
    if ($type eq "none") {
        $passed = !defined($string);
    } elsif ($type eq "int") {
        $passed = defined($string) && ($string =~ /^-?\d+/);
    } elsif ($type eq "float") {
        $passed = defined($string) && (($string =~ /^-?\d+(\.\d+)?/) ||
                                       ($string =~ /^-?\.\d+?/));
    } elsif ($type eq "string") {
        $passed = defined($string);
    } else {
        die "Bad type check for type '$type'\n";
    }
    if (!$passed) {
        if (!defined($string)) {
            $string = "(undefined)";
        }
        die "Type error: Looking for $type, got '$string'";
    }
}

sub check_flag($$) {
    my ($flagset, $flag) = @_;
    my ($flagname,$flagval) = split(/:/, $flag, 2);
    my $valtype = $flagset->{$flagname};
    if (!defined($valtype)) {
        die "Illegal flag: '$flagname'";
    } else {
        check_type($valtype, $flagval);
    }
}

sub check_flag_nonfatal($$) {
    my ($flagset,$flag) = @_;
    #print "checking flag $flag\n";
    eval {check_flag($flagset,$flag)};
    #if (eval {check_flag($flagset,$flag)}) {
    if ($@) {
        #print "returning 0: $@\n";
        return 0;
    } else {
        #print "returning 1\n";
        return 1;
    }
}

sub check_node_type($) {
    my ($type) = @_;
    my ($typename,$typeval) = split(/:/, $type, 2);
    check_type('string', $type);
    # TODO Check number of slots
}

#
# Normalize our internal represetnation of a ptop file by filling in any
# missing fields with reasonable defaults
#
sub normalize_ptop($) {
    my ($ptop) = @_;
Robert Ricci's avatar
Robert Ricci committed
190
    my ($nodes, $links, $limits,$policies) = @$ptop;
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
    foreach my $node (@$nodes) {
        #print "Normalizing $node->{name}\n";
        if (!$node->{types}) {
            # Assume that the node is a PC
            $node->{types} = [["pc", 1]];
        } else {
            # All types should be a pair, including the number of slots - we
            # fill in '1' as the slot count if not given
            @{$node->{types}} = map {
                if (ref($_) eq "ARRAY") {
                    $_;
                } else {
                     [$_,1];
                }
            } @{$node->{types}};
        }

        # Just make sure the features and flags entries are not null
        if (!$node->{features}) {
            $node->{features} = [];
        }
        if (!$node->{flags}) {
            $node->{flags} = [];
        }

        # Filter out some OS features
        @{$node->{features}} = grep { is_feature_okay($$_[0]) } @{$node->{features}};
    }

    foreach my $link (@$links) {
        # In the future, we would have to do something fancy here, if we're
        # renaming nodes
        if (!exists $link->{delay}) {
            $link->{delay} = 0;
        }
        if (!exists $link->{loss}) {
            $link->{loss} = 0;
        }
        if (!exists $link->{slots}) {
            $link->{slots} = 1;
        }

        # Give the link a type of 'ethernet' if it isn't typed
        if (!exists $link->{types} || $link->{types} == []) {
            $link->{types} = ["ethernet"];
        }
    }

Robert Ricci's avatar
Robert Ricci committed
239
    # Nothing to do for limits or policies, for now
240 241 242 243
}

sub write_normalized_ptop($$) {
    my ($outfile, $ptop) = @_;
Robert Ricci's avatar
Robert Ricci committed
244
    my ($nodes, $links, $limits, $policies) = @$ptop;
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

    open (OF,">$outfile") or die "Unable to open $outfile for writing";
    foreach my $node (@$nodes) {
        my $name = $node->{name};
        my $types = join(" ", map { join ":", @$_; } @{$node->{types}});
        my $features = join(" ", map { join ":", @$_; } @{$node->{features}});
        my $flags = join(" ", map {
                if (ref($_) eq "ARRAY") {
                    join ":", @$_;
                } else {
                    $_;
                }
            } @{$node->{flags}});

        # Avoid extraneous spaces if some options fields are empty, by building
        # spaces into the strings
        if ($features ne "") {
            $features = " $features";
        }
        if ($flags ne "") {
            $flags = " $flags";
        }
        print OF "node $name $types -$features -$flags\n";
    }

    foreach my $link (@$links) {
        my $name = $link->{name};
        my $srciface = $link->{srciface};
        my $dstiface = $link->{dstiface};
        my $bw = $link->{bw};
        my $delay = $link->{delay};
        my $loss = $link->{loss};
        my $slots = $link->{slots};
        my $types = join " ", @{$link->{types}};

        print OF "link $name $srciface $dstiface $bw $delay $loss $slots $types\n";
    }

    foreach my $limit (@$limits) {
        my $type = $limit->{type};
        my $count = $limit->{limit};

        print OF "set-type-limit $type $count\n";
    }

Robert Ricci's avatar
Robert Ricci committed
290 291 292 293
    foreach my $policy (@$policies) {
        print OF "policy " . join(" ",@$policy) . "\n";
    }

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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    close(OF);
}

sub normalize_top($) {
    my ($top) = @_;
    my ($nodes, $links, $vclasses, $fixes, $hints) = @$top;

    foreach my $node (@$nodes) {
        #print "Normalizing $node->{name}\n";
        if (!$node->{type}) {
            # Assume that the node is a PC
            $node->{type} = ["pc", 1];
        } else {
            # All types should be a pair, including the number of slots - we
            # fill in '1' as the slot count if not given
            if (ref($node->{type}) ne "ARRAY") {
                $node->{type} = [$node->{type},1];
            }
        }

        # Just make sure the features and flags entries are not null
        if (!$node->{desires}) {
            $node->{desires} = [];
        }
        if (!$node->{flags}) {
            $node->{flags} = [];
        }

        # Filter out some OS desires
        @{$node->{desires}} = grep { is_feature_okay($$_[0]) } @{$node->{desires}};
    }

    foreach my $link (@$links) {
        # In the future, we would have to do something fancy here, if we're
        # renaming nodes
        if (!exists $link->{delay}) {
            $link->{delay} = 0;
        }
        if (!exists $link->{loss}) {
            $link->{loss} = 0;
        }
        if (!exists $link->{slots}) {
            $link->{slots} = 1;
        }

        # Give the link a type of 'ethernet' if it isn't typed
        if (!exists $link->{type}) {
            $link->{type} = "ethernet";
        }
    }

    # Nothing to do, for now, for vclasses, fixes, or hints
}

sub write_normalized_top($$) {
    my ($outfile, $top) = @_;
    my ($nodes, $links, $vclasses, $fixes, $hints) = @$top;

    open (OF,">$outfile") or die "Unable to open $outfile for writing";

    # We put vclasses first so that we're guaranteed that to be defined before
    # the vnodes that use them
    foreach my $vclass (@$vclasses) {
        my $name = $vclass->{name};
        my $weight = $vclass->{weight};
        my $types = join(" ",@{$vclass->{types}});

        print OF "make-vclass $name $weight $types\n";
    }


    foreach my $node (@$nodes) {
        my $name = $node->{name};
        my $type = join(":",@{$node->{type}});
        my $flags = join(" ", map {
                if (ref($_) eq "ARRAY") {
                    join ":", @$_;
                } else {
                    $_;
                }
            } @{$node->{flags}});
        my $desires = join(" ", map { join ":", @$_; } @{$node->{desires}});

        print OF "node $name $type";
        
        # Avoid empty spaces if $flags and/or $desires are empty
        if ($flags ne "") {
            print OF " $flags";
        }

        if ($desires ne "") {
            print OF " $desires";
        }
        
        print OF "\n";
    }

    foreach my $link (@$links) {
        my $name = $link->{name};
        my $srciface = $link->{srciface};
        my $dstiface = $link->{dstiface};
        my $bw = $link->{bw};
        my $delay = $link->{delay};
        my $loss = $link->{loss};
        my $type = $link->{type};
        my $flags = join(" ", map {
                if (ref($_) eq "ARRAY") {
                    join ":", @$_;
                } else {
                    $_;
                }
            } @{$link->{flags}});

        # Avoid spaces if $flags is empty
        print OF "link $name $srciface $dstiface $bw $delay $loss $type";
        if ($flags ne "") {
            print OF " $flags";
        }
        print OF "\n";

    }

    foreach my $fix (@$fixes) {
        my $vnode = $fix->{vnode};
        my $pnode = $fix->{pnode};

        print OF "fix-node $vnode $pnode\n";
    }

    foreach my $hint (@$hints) {
        my $vnode = $hint->{vnode};
        my $pnode = $hint->{pnode};

        print OF "node-hint $vnode $pnode\n";
    }

    close(OF);
}

sub parse_ptop($) {
    my ($ptop) = @_;

    open PT, "<$ptop" or die "Unable to open $ptop: $!\n";

    # We will only merge these into the global set if we succesfully parse the
    # whole file
    my %local_seen_node_types;
    my %local_seen_link_types;
    my %local_seen_fds;

Robert Ricci's avatar
Robert Ricci committed
444
    my (@nodes, @links, @limits, @policies);
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

    my $line_no = 0;
    while (my $line = <PT>) {
        $line_no++;
        chomp $line;

        my @tokens = split /\s+/,$line;
        my $line_type = shift @tokens;

        if (!@tokens) {
            # Skip blank lines
            next;
        }

        if ($line_type eq "node") {
            my %node;

            my $name = shift @tokens;
            check_type("string",$name);

            # Grab all types for this node, list terminated with a '-'
            my @types;
            while (my $token = shift @tokens) {
                if ($token eq "-") {
                    last;
                } else {
                    my ($type, $slots) = split(/:/,$token,2);
                    if (!defined $slots) {
                        $slots = 1;
                    }
                    $local_seen_node_types{$type} = 1;
                    push @types, [$type, $slots];
                }
            }
            if (!@types) {
                die "No types for node $name on $ptop line $line_no\n";
            }

            # Grab features, list terminated with a '-'. Can be empty
            my @features;
            while (my $token = shift @tokens) {
                if ($token eq "-") {
                    last;
                } else {
                    my ($feature, $weight) = split(/:/,$token,2);
                    if (!defined $weight) {
                        die "Malformed feature '$token'";
                    }
                    $local_seen_fds{$feature} = 1;
                    push @features, [$feature,$weight];
                }
            }

            # Grab flags and make sure they are legal flags
            my @flags = @tokens;
            @tokens = ();
            foreach my $flag (@flags) {
                check_flag(\%ptop_nodeflags,$flag);
            }

            $node{name} = $name;
            $node{types} = \@types;
            $node{features} = \@features;
            $node{flags} = \@flags;

            push @nodes, \%node;

        } elsif ($line_type eq "link") {
            my %link;

            $link{name} = shift @tokens;
            check_type("string",$link{name});

            # TODO - probably add a special type check for interfaces
            $link{srciface} = shift @tokens;
            check_type("string",$link{srciface});
            $link{dstiface} = shift @tokens;
            check_type("string",$link{dstiface});

            $link{bw} = shift @tokens;
            check_type("int",$link{bw});

            # This is a bit tricky - figure out if there are delay and
            # loss present on the line
            if (scalar(@tokens) < 2) {
                if ($opt{s}) {
                    die "No latency/loss on $ptop line $line_no\n";
                }
                if (scalar(@tokens) == 1) {
                    $link{slots} = shift @tokens;
                    check_type("int",$link{slots});
                }
                # Note: There were no types on link lines old enough to
                # not have delay/loss
            } else {
                $link{delay} = shift @tokens;
                check_type("float",$link{delay});
                $link{loss} = shift @tokens;
                check_type("float",$link{loss});
                $link{slots} = shift @tokens;
                check_type("int",$link{slots});
                my @types = ();
                if (@tokens) {
                    @types = @tokens;
                    @tokens = ();
                    foreach my $type (@types) {
                        check_type("string",$type);
                        $seen_link_types{$type} = 1;
                    }
                    $link{types} = \@types;
                } else {
                    if ($opt{s}) {
                        # Only die if doing strict checking - otherwise, it's
                        # okay to have no types
                        die "No link type on $ptop line $line_no\n";
                    }
                }
            }

            push @links, \%link;

        } elsif ($line_type eq "set-type-limit") {
            my $type = shift @tokens;
            check_type("string", $type);
            $local_seen_node_types{$type} = 1;
            my $limit = shift @tokens;
            check_type("int", $limit);

            my %limit = (type => $type, limit => $limit);
            push @limits, \%limit;
Robert Ricci's avatar
Robert Ricci committed
575 576 577 578 579
        } elsif ($line_type eq "policy") {
            # For now, we are just going to pass the rest of the string
            # along rather than parsing it carefully, since we don't support
            # fancy policies in any ptop version yet
            push @policies, [@tokens];
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
        } else {
            die "Unknown line type '$line_type' on $ptop line $line_no\n";
        }

        # Make sure that we've parsed all tokens
        if (@tokens) {
            die "Tokens left after parsing finished on $ptop line $line_no\n
            @tokens\n";
        }
    }

    merge_sets(\%seen_node_types,\%local_seen_node_types);
    merge_sets(\%seen_link_types,\%local_seen_link_types);
    merge_sets(\%seen_fds,\%local_seen_fds);

    close PT;

Robert Ricci's avatar
Robert Ricci committed
597
    return [\@nodes, \@links, \@limits,\@policies];
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
}

sub parse_top($) {
    my ($top) = @_;

    # We will only merge these into the global set if we succesfully parse the
    # whole file
    my %local_seen_node_types;
    my %local_seen_link_types;
    my %local_seen_fds;

    open TF, "<$top" or die "Unable to open $top: $!\n";

    my (@nodes, @links, @vclasses, @fixes, @hints);
    my $line_no = 0;
    while (my $line = <TF>) {
        $line_no++;
        chomp $line;

        my @tokens = split /\s+/,$line;
        my $line_type = shift @tokens;

        if (!@tokens) {
            # Skip blank lines
            next;
        }

        if ($line_type eq "node") {
            my %node;

            $node{name} = shift @tokens;
            check_type("string",$node{name});

            my $token = shift @tokens;

            my ($type, $slots) = split(/:/,$token,2);
            check_node_type($type);
            if ($slots) {
                check_type("int",$slots);
                $node{type} = [$type, $slots];
            } else {
                $node{type} = $type;
            }
            $local_seen_node_types{$type} = 1;

            # Grab desires, which can be intermixed with flags. Ick.
            my @desires;
            my @flags;
            while (my $token = shift @tokens) {
                if (check_flag_nonfatal(\%top_nodeflags,$token)) {
                    push @flags, $token;
                } else {
                    my ($desire, $weight) = split(/:/,$token,2);
                    check_type('string',$desire);
                    check_type('float', $weight);
                    $local_seen_fds{$desire} = 1;
                    push @desires, [$desire,$weight];
                }
            }

            $node{desires} = \@desires;
            $node{flags} = \@flags;

            push @nodes, \%node;

        } elsif ($line_type eq "link") {
            my %link;

            $link{name} = shift @tokens;
            check_type("string",$link{name});
            $link{srciface} = shift @tokens;
            check_type("string",$link{srciface});
            $link{dstiface} = shift @tokens;
            check_type("string",$link{dstiface});

            $link{bw} = shift @tokens;
Robert Ricci's avatar
Robert Ricci committed
674 675 676 677 678 679 680 681

            #
            # The bandwidth should be either the string '*' (which means
            # 'native speed') or an int
            #
            if ($link{bw} ne "*") {
                check_type("int",$link{bw});
            }
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
            
            # This is a bit tricky - figure out if there are delay and
            # loss present on the line
            # XXX - I'm not sure if there were flags on lines that didn't
            # have latency/loss
            if (!scalar(@tokens)) {
                if ($opt{s}) {
                    die "No latency/loss on $top line $line_no\n";
                }
            } elsif (scalar(@tokens) == 1) {
                die "Link line in a format I don't recognize on $top line " .
                    "$line_no\n";
            } else {
                $link{delay} = shift @tokens;
                check_type("float",$link{delay});
                $link{loss} = shift @tokens;
                check_type("float",$link{loss});
            }

            # Distintuish between the two different formats of link lines
            if (@tokens) {
                my @flags;

                my $maybe_flag = shift @tokens;
                if (check_flag_nonfatal(\%top_linkflags,$maybe_flag)) {
                    push @flags, $maybe_flag;
                    if ($opt{s}) {
                        die "No link type on $top line $line_no\n";
                    }
                } else {
                    $link{type} = $maybe_flag;
                    $local_seen_link_types{$link{type}} = 1;
                }

                push @flags, @tokens;
                @tokens = ();
                foreach my $flag (@flags) {
                    check_flag(\%top_linkflags,$flag);
                }

                $link{flags} = \@flags;
            }

            push @links, \%link;
            
        } elsif ($line_type eq "make-vclass") {
            my $name = shift @tokens;
            check_type("string", $name);
            my $penalty = shift @tokens;
            check_type("float", $penalty);

            my @types = @tokens;
            if (!@types) {
                die "No types given for vclass on $top line $line_no\n";
            } else {
                foreach my $type (@types) {
                    $local_seen_node_types{$type} = 1;
                }
            }
            @tokens = ();

            my %vclass = (name => $name, weight => $penalty, types => \@types);
            push @vclasses, \%vclass;
        } elsif ($line_type eq "fix-node") {

            my $vnode = shift @tokens;
            check_type("string",$vnode);
            my $pnode = shift @tokens;
            check_type("string",$pnode);

            my %fix = (vnode => $vnode, pnode => $pnode);
            push @fixes, \%fix;

        } elsif ($line_type eq "node-hint") {
            my $vnode = shift @tokens;
            check_type("string",$vnode);
            my $pnode = shift @tokens;
            check_type("string",$pnode);

            my %hint = (vnode => $vnode, pnode => $pnode);
            push @hints, \%hint;
        } else {
            die "Unknown line type '$line_type' on $top line $line_no\n";
        }

        # Make sure that we've parsed all tokens
        if (@tokens) {
            die "Tokens left after parsing finished on $top line $line_no\n
            @tokens\n";
        }
    }

    merge_sets(\%seen_node_types,\%local_seen_node_types);
    merge_sets(\%seen_link_types,\%local_seen_link_types);
    merge_sets(\%seen_fds,\%local_seen_fds);

    close TF;

    return [\@nodes, \@links, \@vclasses, \@fixes, \@hints];
}

#
# Find top files
#
my @dirs = `ls -1`;
chomp @dirs;
my $processed_files = 0;
my $passed_files = 0;
foreach my $dir (@dirs) {

Robert Ricci's avatar
Robert Ricci committed
792 793
    my @topfiles = `find $dir/ -name \*.vtop`;
    my @ptopfiles = `find $dir/ -name \*.ptop`;
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808

    chomp @ptopfiles;
    chomp @topfiles;
    foreach my $ptop (@ptopfiles) {
        if ($opt{D}) {
            print "Checking ptop file $ptop\n";
        }
        $processed_files++;
        my $ptop_data = eval { parse_ptop($ptop); };
        if (!$@) {
            $passed_files++;
            if ($opt{l}) {
                print "$ptop\n";
            }
            if ($opt{n}) {
Robert Ricci's avatar
Robert Ricci committed
809 810 811
                # Grab timestamps so that we can preserve them
                my ($atime, $mtime) = (stat($ptop))[8,9];

812 813
                normalize_ptop($ptop_data);
                write_normalized_ptop($ptop,$ptop_data);
Robert Ricci's avatar
Robert Ricci committed
814 815 816

                # Restore timestamp
                utime $atime, $mtime, $ptop;
817 818 819 820
            }
        } else {
            if ($opt{r}) {
                system "rm $ptop";
Robert Ricci's avatar
Robert Ricci committed
821 822
            } elsif ($opt{R}) {
                system "mv $ptop $ptop.failed";
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
            }
            if ($opt{e}) {
                print "*** FAILED: $ptop: $@\n";
            }
        }

        if ($processed_files % 100 == 0) {
            if ($opt{v}) {
                print STDERR "*** Processed $processed_files files ($passed_files passed)\n";
            }
        }
    }
    foreach my $top (@topfiles) {
        if ($opt{D}) {
            print "Checking top file $top\n";
        }
        $processed_files++;
        my $top_data = eval { parse_top($top); };
        if (!$@) {
            $passed_files++;
            if ($opt{l}) {
                print "$top\n";
            }
            if ($opt{n}) {
Robert Ricci's avatar
Robert Ricci committed
847 848 849
                # Grab timestamps so that we can preserve them
                my ($atime, $mtime) = (stat($top))[8,9];

850 851
                normalize_top($top_data);
                write_normalized_top($top,$top_data);
Robert Ricci's avatar
Robert Ricci committed
852 853 854

                # Restore timestamp
                utime $atime, $mtime, $top;
855 856 857 858
            }
        } else {
            if ($opt{r}) {
                system "rm $top";
Robert Ricci's avatar
Robert Ricci committed
859 860
            } elsif ($opt{R}) {
                system "mv $top $top.failed";
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
            }
            if ($opt{e}) {
                print "*** FAILED: $top: $@\n";
            }
        }

        if ($processed_files % 100 == 0) {
            if ($opt{v}) {
                print STDERR "*** Processed $processed_files files ($passed_files passed)\n";
            }
        }
    }

    if ($opt{L} && $processed_files > $opt{L}) {
        last;
    }
}

if ($opt{t}) {
    # Dump the types we've seen
    print "Seen node types:\n";
    foreach my $node_type (sort keys %seen_node_types) {
        print "$node_type\n";
    }

    print "Seen link types:\n";
    foreach my $link_type (sort keys %seen_link_types) {
        print "$link_type\n";
    }

    print "Seen features/desires:\n";
    foreach my $fd (sort keys %seen_fds) {
        print "$fd\n";
    }
}