createClusters.pl 18.4 KB
Newer Older
1 2 3
#!/usr/bin/perl
#
# Copyright (c) 2006 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
use Getopt::Long;
25

26 27 28 29
local $RubensteinScriptPath = "/proj/tbres/pramod/bottleneck-emulab/filter/genjitco.i386";
local $MinSikKimScriptPath = "/proj/tbres/pramod/bottleneck-emulab/devel/MinSikKim/MinSikKimProgram";
local $ClusterProgramPath = "/proj/tbres/pramod/bottleneck-emulab/devel/Clustering/c++-samples/wvcluster";
local $elabInitScript = "/proj/tbres/duerig/testbed/pelab/init-elabnodes.pl";
30

31

32 33 34 35 36 37
local $newExpName = "";
local $newProjName = "";
local $logsDir = "";
local $nodeListFilename = "";
local $Algorithm = "";
local $initialConditionsFilename = "";
38

39
local $scratchSpaceDir = "/tmp/bw-bottleneck/rubenstein";
40

41
# Print usage and die.
42

43 44 45 46 47 48
sub PrintUsage()
{
        print "Usage: perl createClusters.pl -NewProj=newProjName -NewExp=newExpName -LogDir=/path/to/logs -nodeFile=/path/to/nodeListFile -Algorithm=rubestein/wavelet (rest optional) -RubensteinPath=/path/to/genjitco.arch -MinSikKimPath=/path/to/MinSikKimProgram -ClusterProgPath=/path/to/wvcluster -InitConditions=/path/to/initial_conditions.txt\n";
        exit(1);

}
49

50
# Check for the validity of command line arguments.
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
sub processOptions()
{
    &GetOptions("LogDir=s"=>\$logsDir,
            "NewProj=s"=>\$newProjName,
            "NewExp=s"=>\$newExpName,
            "InitConditions=s"=>\$initialConditionsFilename,
            "NodeFile=s"=>\$nodeListFilename,
            "Algorithm=s"=>\$Algorithm,
            "RubensteinPath=s"=>\$RubensteinScriptPath,
            "MinSikKimPath=s"=>\$MinSikKimScriptPath,
            "ClusterProgPath=s"=>\$ClusterProgramPath);

    if($newExpName eq "" or $newProjName eq "" or $logsDir eq "" or $nodeListFilename eq "" or $Algorithm eq "")
    {
        print "ERROR: One of the mandatory arguments is empty.\n";
        &PrintUsage();
    }
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
    if(not(-e $logsDir))
    {
        print "ERROR: Path provided to the logs directory is invalid.\n";
        &PrintUsage();
    }

    if(not(-e $nodeListFilename))
    {
        print "ERROR: Path provided to the file with node names is invalid.\n";
        &PrintUsage();

    }

    if( not(  ($Algorithm =~ /rubenstein/i) || ($Algorithm =~ /wavelet/i) ) )
    {
        print "ERROR: Please choose either 'rubenstein' or 'wavelet' ( without quotes, case insensitive ) as the algorithm\n";
        &PrintUsage();
    }

    if($initialConditionsFilename ne "")
    {
        if(not(-e $initialConditionsFilename))
        {
            print "WARNING: The initial conditions file path provided at command line is invalid.\n";
            print "Reverting back to using init-elabnodes.pl\n";
                
        }
    }
}


#############################################################################
############################    MAIN    #####################################
#############################################################################

# Process the command line options.
&processOptions();
107 108

# Get the initial conditions.
109 110 111
# Get initial conditions for the paths of interest
# from the database, using init-elabnodes.pl

112 113
$initConditionsCommand = $elabInitScript . " -o /tmp/initial-conditions.txt " . $newProjName . " " . $newExpName; 

114
if( ($initialConditionsFilename eq "") or (not(-e $initialConditionsFilename)) ) 
115 116 117 118 119 120
{
    system($initConditionsCommand);
    $initialConditionsFilename = "/tmp/initial-conditions.txt";
}

open(CONDITIONS, $initialConditionsFilename);
121 122
my @initialConditions = <CONDITIONS>;
chomp(@initialConditions);
123 124 125 126 127 128 129 130
close(CONDITIONS);

my @addressList = ();
my $addrFlag = 0;

my %bwMap = {};
my %delayMap = {};
my %elabMap = {};
131 132
my $numNodes = 0;
# Create a list of the IP addresses listed in the initial-conditions file.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
foreach $conditionLine (@initialConditions)
{
    if($conditionLine =~ /(\d*?\.\d*?\.\d*?\.(\d*?))\s(\d*?\.\d*?\.\d*?\.\d*?)\s(\d*?)\s(\d*?)\s[\d\w\-\.]*/)
    {
        $srcAddress = $1;
        $addrFlag = 0;

        foreach $addrEntry (@addressList)
        {
            if($addrEntry eq $srcAddress)
            {
                $addrFlag = 1;
            }
        }

        if($addrFlag == 0)
        {
            push(@addressList, $srcAddress);
151
            $numNodes += 1;
152 153

            $elabMap{$srcAddress} = "elabc-elab-" . $2;
154
            print "Mapping $srcAddress to $elabMap{$srcAddress}\n";
155 156 157 158 159 160 161 162
        }

# Create a mapping of the initial conditions.
        $bwMap{$1}{$3} = $4;
        $delayMap{$1}{$3} = $5;
    }
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
# Read the file listing the hostnames of the nodes involved in this
# particular measurement.

open(INFILE, "<$nodeListFilename");
my @nodeNameList = <INFILE>;
chomp(@nodeNameList);
close(INFILE);

# A little checking to ensure that the number of nodes in the NodeList file
# matches the values in the initial conditions file.
if($numNodes != ($#nodeNameList+1) )
{
    print "ERROR: Mismatch between the number of nodes in InitialConditions.txt and in the NodeList file.\n";
    print "Both the files should have the same number of nodes for a 1-to-1 mapping to be possible.\n";
    exit(1);
}

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
foreach $tmpDirName (@nodeNameList)
{
    # Create empty directories if some of the log files were missing.
    if(not (-d "$logsDir/$tmpDirName"))
    {
        print "@ WARNING: Creating dummy directory $logsDir/$tmpDirName and its sub-directories.\n";
        foreach $tmpDirName2 (@nodeNameList)
        {
            if($tmpDirName2 ne $tmpDirName)
            {
                system("mkdir -p $logsDir/$tmpDirName/$tmpDirName2");
            }
        }
    }
}

196 197 198 199 200 201 202 203 204
# Convert the delay data produced by the wavelet code
# into something palatable to the Rubenstein program.
if($Algorithm =~ /rubenstein/i)
{
    if(-e $scratchSpaceDir)
    {
        `rm -rf $scratchSpaceDir`;
    }

205
    `mkdir -p $scratchSpaceDir`;
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

    opendir(logsDirHandle, $logsDir);
    foreach $sourceName (readdir(logsDirHandle))
    {
        if( (-d $logsDir . "/" . $sourceName ) and $sourceName ne "." and $sourceName ne ".." )
        {
            if( grep(/^$sourceName$/, @nodeNameList) )
            {
                @dirNameList = ();

                opendir(sourceDirHandle, $logsDir . "/" . $sourceName);
                foreach $destOne (readdir(sourceDirHandle))
                {
                    if( (-d $logsDir . "/" . $sourceName . "/" . $destOne) && $destOne ne "." && $destOne ne ".." )
                    {
                        if( grep(/^$destOne$/, @nodeNameList) )
                        {
                            if(-e "$logsDir/$sourceName/$destOne/delay.log")
                            {
                                push(@dirNameList, $destOne);
                            }
                            else
                            {
229
                                push(@dirNameList, $destOne);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
                                print "WARNING: Missing log file $logsDir/$sourceName/$destOne/delay.log\n";
                            }
                        }
                    }
                }
                closedir(sourceDirHandle);

                my $firstLevelIndex = 0, $secondLevelIndex = 0;

                foreach $destOne (@dirNameList)
                {
                    $secondLevelIndex = 0;
                    foreach $destTwo (@dirNameList)
                    {
                        if($secondLevelIndex > $firstLevelIndex)
                        {
                            `mkdir -p $scratchSpaceDir/$sourceName/$destOne/$destTwo`;
247 248 249 250
                            if((-e "$logsDir/$sourceName/$destOne/delay.log") and (-e "$logsDir/$sourceName/$destTwo/delay.log"))
                            {
                                `paste -d ' ' $logsDir/$sourceName/$destOne/delay.log $logsDir/$sourceName/$destTwo/delay.log > $scratchSpaceDir/$sourceName/$destOne/$destTwo/delay.log `;
                            }
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
                        }

                        $secondLevelIndex++;
                    }
                    $firstLevelIndex++;
                }
            }
        }
    }

    closedir(sourceDirHandle);
# Call our sharedBottle.pl to feed these .filter files we created to
# sharedBottle.pl - It will then create equivalence classes for the
# paths.

    system("perl sharedBottle.pl $scratchSpaceDir $newProjName $newExpName $initialConditionsFilename $RubensteinScriptPath");

# Our work here is done, sign off.
    exit(0);
}

#############################################################################
######################### Wavelet processing ################################
#############################################################################

%bottleNecks = {};
my %nodeClasses;
my %nodeNameMap = {};

# Create & send events.
my $tevc = "/usr/testbed/bin/tevc -e $newProjName/$newExpName now";

283 284
#`/usr/testbed/bin/tevc -w -e $newProjName/$newExpName now elabc reset`;
#`$tevc elabc create start`;
285

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
opendir(logsDirHandle, $logsDir);

my $addrIndex = 0;
my %addrNodeMapping = {};
my %nodeNumberMapping = {};
my $numDests = 0;

foreach $sourceName (readdir(logsDirHandle))
{
# Map the elab IP address in the initial conditions file, with
# the node names in the gather-results logs directory.

    if( (-d $logsDir . "/" . $sourceName ) && $sourceName ne "." && $sourceName ne ".." )
    {
        $addrNodeMapping{$sourceName} = $addressList[$addrIndex];
        $nodeNameMapping{$sourceName} = $addrIndex + 1; 
        $nodeNumberMapping{$addrIndex + 1} = $sourceName; 
303
        print "Mapping $sourceName to $addressList[$addrIndex]\n";
304 305 306 307 308 309 310 311 312 313
        $addrIndex++;
        $numDests++;
    }
}

rewinddir(logsDirHandle);

# Descend into all the source directories
foreach $sourceName (readdir(logsDirHandle))
{
314
    if( (-d "$logsDir/$sourceName" ) && $sourceName ne "." && $sourceName ne ".." )
315 316 317 318 319 320 321 322 323
    {

        my @destLists;
# Then search for all possible destinations for 
# a particular source.
        opendir(sourceDirHandle, $logsDir . "/" . $sourceName);

        my @denoisedDelays = ();

324 325 326 327 328
        for($i = 0; $i < $numDests; $i++)
        {
            push(@denoisedDelays, 0);
        }

329 330
        foreach $destOne (readdir(sourceDirHandle))
        {
331
            if( (-d "$logsDir/$sourceName/$destOne") && $destOne ne "." && $destOne ne ".." )
332 333 334 335 336
            {
                # Inside each destination directory, look for 
                # delay.log file with delay values.

                $fullPath = "$logsDir/$sourceName/$destOne/delay.log";
337 338 339 340 341 342
                my @scriptOutput = ();
                if(not(-r $fullPath))
                {
                    $denoisedDelays[$nodeNameMapping{$destOne}] = [ @scriptOutput ];
                    next;
                }
343
                $waveletScript = "$MinSikKimScriptPath $fullPath";
344

345 346 347 348 349 350 351 352 353 354
# Indicates low variance of the delay samples.
                if((&CheckVariance($fullPath) == 1))
                {
                    $denoisedDelays[$nodeNameMapping{$destOne}] = [ @scriptOutput ];
                }
                else
                {
                    @scriptOutput = `$waveletScript`;
                    $denoisedDelays[$nodeNameMapping{$destOne}] = [ @scriptOutput ];
                }
355 356 357 358 359

            }
        }
        closedir(sourceDirHandle);

360
        my $numOfSamples = 128;
361 362 363 364 365
        local @equivClasses = ();

        $tmpTreeRecordFile = "/tmp/bw-wavelet-clustering.rcd";

        open(RECORDFILE, ">$tmpTreeRecordFile");
366
        print RECORDFILE "$numOfSamples\n";
367 368 369 370 371 372 373 374 375 376 377 378 379
        for($i = 1; $i <= $numDests; $i++)
        {
            if($i == $nodeNameMapping{$sourceName})
            {
                next;
            }
            else
            {
                my @scriptOutput;
                @scriptOutput = @{ $denoisedDelays[$i] };

                # Put this destination in a seperate cluster - we
                # have zero samples/delay values.
380
                if( ($#scriptOutput == 0) or ($#scriptOutput < $numOfSamples) )
381 382 383 384 385
                {
                    my @newEquivClass = ();
                    push(@newEquivClass, $i);

                    push(@equivClasses, [@newEquivClass]);
386
                    print "@@ $sourceName: WARNING: Creating a new equiv class/cluster for $nodeNumberMapping{$i} due to lack of samples($#scriptOutput) or low variance\n";
387 388 389 390 391 392 393 394 395 396
                }
                else
                {
                    my $counter = 0;
                    my $avgValue = 0;
                    my @delayValueArray = ();
                    foreach $delayValue (@scriptOutput)
                    {
                        chomp($delayValue);

397
                        if($counter < $numOfSamples)
398 399 400 401 402 403 404 405 406 407
                        {
                            $avgValue += $delayValue;
                            push(@delayValueArray, $delayValue);
                            $counter++;
                        }
                        else
                        {
                            last;
                        }
                    }
408 409
                    #############################################
                    #$avgValue = $avgValue/$numOfSamples;
410

411 412 413 414 415 416
                    #my $denominator = 0;
                    #foreach $delayValue (@delayValueArray)
                    #{
                    #    $denominator += ($delayValue - $avgValue)*($delayValue - $avgValue);
                    #}
                    #$denominator = sqrt($denominator);
417

418
                    # Exclude paths with low-variance.
419 420 421 422 423 424 425 426 427 428
                    #if($denominator < 25)
                    #{
                    #    my @newEquivClass = ();
                    #    push(@newEquivClass, $i);
#
#                        push(@equivClasses, [@newEquivClass]);
#                        print "$sourceName: WARNING: Creating a new equiv class/cluster for $nodeNumberMapping{$i} due to low variance of samples($denominator)\n";
#                        next;
#                    }
                    #############################################
429

430 431
                    foreach $delayValue (@delayValueArray)
                    {
432 433 434
                        #$delayValue = ($delayValue - $avgValue)/$denominator;
                        #print RECORDFILE "$delayValue:";
                        printf (RECORDFILE "%.4f:",$delayValue);
435 436 437 438 439 440 441 442
                    }

                    print RECORDFILE "$i\n";
                }
            }
        }
        close(RECORDFILE);

443

444
        $clusteringProgram = "$ClusterProgramPath $tmpTreeRecordFile /tmp/tmp.idx";
445

446
        my @clusteringOutput  = (); 
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465

        @clusteringOutput = `$clusteringProgram`;

        foreach $clusterLine (@clusteringOutput)
        {
            @clusterElements = split(/ /, $clusterLine);
            my @newEquivClass = ();

            foreach $nodeId (@clusterElements)
            {
                if($nodeId =~ /\d+/)
                {
                    push(@newEquivClass, $nodeId);
                }

            }
            push(@equivClasses, [@newEquivClass]);
        }

466
        print "@@@ Clusters for $sourceName:\n";
467 468
        foreach $tmpName (@equivClasses)
        {
469
            print "@@@ ";
470 471 472 473
            foreach $tmpName2 (@$tmpName)
            { 
                print "$nodeNumberMapping{$tmpName2} ";
            }
474
            print " \n";
475 476
        }

477
        print "@@@ \n";
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

# Send the events to all the nodes which form an equivalent class.
        foreach $tmpName (@equivClasses)
        {
            my $bwEventCommand = "$tevc $elabMap{$addrNodeMapping{$sourceName}} modify DEST=";
            my $firstDestFlag = 0;

            my $maxBw = 0;

# Find the maximum available bandwidth in all the paths of this equivalence class.
            foreach $tmpName2 (@$tmpName)
            {
                if($bwMap{$addrNodeMapping{$sourceName}}{$addrNodeMapping{$nodeNumberMapping{$tmpName2}}} > $maxBw)
                {
                    $maxBw = $bwMap{$addrNodeMapping{$sourceName}}{$addrNodeMapping{$nodeNumberMapping{$tmpName2}}};
                }
            }

            foreach $tmpName2 (@$tmpName)
            {
                if($firstDestFlag == 0)
                {
                    $bwEventCommand = $bwEventCommand . $addrNodeMapping{$nodeNumberMapping{$tmpName2}};
                    $firstDestFlag = 1;
                }
                else
                {
                    $bwEventCommand = $bwEventCommand . "," . $addrNodeMapping{$nodeNumberMapping{$tmpName2}};
                }


                my $delayEventCommand = "$tevc ".$elabMap{$addrNodeMapping{$nodeNumberMapping{$tmpName2}}}." modify DEST=" . $addrNodeMapping{$nodeNumberMapping{$tmpName2}}." SRC=".$addrNodeMapping{$sourceName};

                $delayEventCommand = $delayEventCommand . " " . "DELAY=" . ($delayMap{$addrNodeMapping{$sourceName}}{$addrNodeMapping{$nodeNumberMapping{$tmpName2}}});
# Execute the delay event command.
                print "EXECUTE $delayEventCommand\n";
514
                #`$delayEventCommand`;
515 516 517 518
            }
            $bwEventCommand = $bwEventCommand . " " . "BANDWIDTH=" . $maxBw;
# Execute the event to set the bandwidth for this equivalence class.
            print "EXECUTE $bwEventCommand\n";
519
            #`$bwEventCommand`;
520 521 522 523 524 525 526
        }

            print "\n\n";
    }
}

closedir(logsDirHandle);
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

sub CheckVariance()
{
    open(FILEHANDLE, $_[0]);
    my @delayValueArray = ();
    while(<FILEHANDLE>)
    {
        if(/(\-?[0-9]*) ([0-9]*)/)
        {
            push(@delayValueArray, $1);
        }

    }
    close(FILEHANDLE);
    chomp($delayValueArray);


    my $avgValue = 0;
    if($#delayValueArray == -1)
    {
        return 1;
    }

    foreach $delayValue (@delayValueArray)
    {
        $avgValue += $delayValue;
    }
    $avgValue = $avgValue/($#delayValueArray+1);

    my $variance = 0;
    foreach $delayValue (@delayValueArray)
    {
        $variance += ($delayValue - $avgValue)*($delayValue - $avgValue);
    }
    $variance = sqrt($variance);

# Exclude paths with low-variance.
    if($variance < 25)
    {
        print "@@ WARNING: Low Variance($variance) for $fullPath\n";
        return 1;
    }
    else
    {
        return 0;
    }
}