All new accounts created on Gitlab now require administrator approval. If you invite any collaborators, please let Flux staff know so they can approve the accounts.

assign.cc 36.2 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1 2
/*
 * EMULAB-COPYRIGHT
Mike Hibler's avatar
Mike Hibler committed
3
 * Copyright (c) 2000-2010 University of Utah and the Flux Group.
Robert Ricci's avatar
Robert Ricci committed
4 5 6
 * All rights reserved.
 */

7 8 9 10 11 12 13 14
#include "port.h"

#include <boost/config.hpp>
#include <boost/utility.hpp>
#include <boost/property_map.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
15
#ifdef GRAPHVIZ_SUPPORT
16
#include <boost/graph/graphviz.hpp>
17
#endif
18

19 20
#include <fstream>
#include <iostream>
21
#include <time.h>
Mac Newbold's avatar
Mac Newbold committed
22
#include <stdlib.h>
23
#include <math.h>
Mac Newbold's avatar
Mac Newbold committed
24 25
#include <sys/types.h>
#include <sys/time.h>
26
#include <sys/resource.h>
27 28
#include <signal.h>
#include <sys/signal.h>
29
#include <queue>
30
#include <algorithm>
31 32

using namespace boost;
Mac Newbold's avatar
Mac Newbold committed
33

34 35
#include <stdio.h>

Mac Newbold's avatar
Mac Newbold committed
36
#include "common.h"
37
#include "delay.h"
Mac Newbold's avatar
Mac Newbold committed
38
#include "physical.h"
39
#include "virtual.h"
40
#include "vclass.h"
41
#include "pclass.h"
42
#include "score.h"
43 44 45
#include "solution.h"
#include "maps.h"
#include "anneal.h"
46 47
#include "config.h"

48 49
#ifdef WITH_XML
#include "parse_ptop_xml.h"
50 51 52
#include "parse_vtop_xml.h"
#include "parse_advertisement_rspec.h"
#include "parse_request_rspec.h"
53
#endif
54 55 56

// Here we set up all our graphs.  Need to create the graphs
// themselves and then setup the property maps.
57
tb_pgraph PG;
58 59 60 61 62 63 64 65 66 67
tb_pgraph_vertex_pmap pvertex_pmap = get(vertex_data, PG);
tb_pgraph_edge_pmap pedge_pmap = get(edge_data, PG);
tb_sgraph SG;
tb_sgraph_vertex_pmap svertex_pmap = get(vertex_data, SG);
tb_sgraph_edge_pmap sedge_pmap = get(edge_data, SG);
tb_vgraph VG;
tb_vgraph_vertex_pmap vvertex_pmap = get(vertex_data, VG);
tb_vgraph_edge_pmap vedge_pmap = get(edge_data, VG);

// List of virtual types by name.
68
name_count_map vtypes;
69
name_list_map vclasses;
70 71

// A list of all pclasses.
72
pclass_list pclasses;
73 74 75
 	
// Map from a pnode* to the the corresponding pvertex.
pnode_pvertex_map pnode2vertex;
76 77 78

// Map of a type to a tt_entry, a vector of pclasses and the size of
// the vector.
79
pclass_types type_table;
80 81 82
#ifdef PER_VNODE_TT
pclass_types vnode_type_table;
#endif
83

84 85 86 87 88
// This datastructure contains all the information needed to calculate
// the shortest path between any two switches.  Indexed by svertex,
// the value will be a predicate map (indexed by svertex as well) of
// the shortest paths for the given vertex.
switch_pred_map_map switch_preds;
89

90 91
// Same, but for distances 
switch_dist_map_map switch_dist;
Christopher Alfeld's avatar
 
Christopher Alfeld committed
92

93 94
// Time started, finished, and the time limit
double timestart, timeend, timelimit, timetarget;
95

96 97 98
// An amount to scale the neighborhood size by
double scale_neighborhood = 1.0;

99 100 101 102 103
#ifdef GNUPLOT_OUTPUT
FILE *scoresout, *tempout, *deltaout;
#endif
// Whether or not assign is allowed to generate trivial links
bool allow_trivial_links = true;
104

105
// Whether or not assign should use pclasses
106
bool disable_pclasses = false;
107

Robert Ricci's avatar
Robert Ricci committed
108 109 110
// Whether or not assign should prune out pclasses that it knows can
// never be used
bool prune_pclasses = false;
111 112 113

// Whether or not we should use the experimental support for dynamic pclasses
bool dynamic_pclasses = false;
114 115 116

// Whether or not to allow assign to temporarily over-subscribe pnodes
bool allow_overload = false;
Robert Ricci's avatar
Robert Ricci committed
117 118 119 120

// Forces assign to do greedy link assignment, by chosing the first link in its
// list, which is usually the lowest-cost
bool greedy_link_assignment = false;
Robert Ricci's avatar
Robert Ricci committed
121 122 123 124 125

// Forces assign to skip melting, and, instead, use the temperature given as
// the initial temperature
bool no_melting = false;
double initial_temperature = 0.0f;
126 127 128 129 130 131

// Print out a summary of the solution in addition to the solution itself
bool print_summary = false;

// Use the 'connected' find algorithm
double use_connected_pnode_find = 0.0f;
132

133 134 135
// Whether or not to perform all checks on fixed nodes
bool check_fixed_nodes = false;

136 137 138
// If true, dump a bunch of configrution information
bool dump_config = false;

139
// Use XML for file input
140 141 142 143 144 145
// bool xml_input = false;
#ifdef WITH_XML
bool ptop_xml_input = false;
bool vtop_xml_input = false;
bool ptop_rspec_input = false;
bool vtop_rspec_input = false;
146
#endif
147

148 149 150
// XXX - shouldn't be in this file
double absbest;
int absbestviolated, iters, iters_to_best;
Robert Ricci's avatar
Robert Ricci committed
151

152 153 154
// Map of all physical types in use in the system
tb_ptype_map ptypes;

155 156 157
/*
 * Internal functions
 */
Mac Newbold's avatar
Mac Newbold committed
158

Robert Ricci's avatar
Robert Ricci committed
159
// Return the CPU time (in seconds) used by this process
160
float used_time() {
161 162 163 164 165 166
  struct rusage ru;
  getrusage(RUSAGE_SELF,&ru);
  return ru.ru_utime.tv_sec+ru.ru_utime.tv_usec/1000000.0+
    ru.ru_stime.tv_sec+ru.ru_stime.tv_usec/1000000.0;
}

167 168 169 170 171 172 173 174 175 176 177 178
// Constructs the output file name for the annotated rspec
string annotated_filename (const char* filepath)
{
	string input_filepath = string(filepath);
	int pos_last_backslash = input_filepath.find_last_of("/");
	string input_filename = input_filepath.substr(pos_last_backslash+1, input_filepath.length()-pos_last_backslash-1);
	string output_filepath = input_filepath.substr(0, pos_last_backslash+1);
	output_filepath.append("annotated-");
	output_filepath.append(input_filename);
	return (output_filepath);
}

Robert Ricci's avatar
Robert Ricci committed
179
// Read in the .ptop file
180
void read_physical_topology(char *filename) {
181 182
  ifstream ptopfile;
  ptopfile.open(filename);
Robert Ricci's avatar
Robert Ricci committed
183
  if (!ptopfile.is_open()) {
184
      cout << "*** Unable to open ptop file " << filename << endl;
185
      exit(EXIT_FATAL);
Robert Ricci's avatar
Robert Ricci committed
186
  }
187 188
  
#ifdef WITH_XML
189
  if (ptop_xml_input) {
190 191 192 193
	cout << "Physical Graph: " << parse_ptop_xml(PG,SG,filename) << endl;
  } 
  else if (ptop_rspec_input) {
	cout << "Physical Graph: " << parse_advertisement(PG, SG, filename) << endl;
194 195
	}
	else {
196 197 198
      cout << "Physical Graph: " << parse_ptop(PG,SG,ptopfile) << endl;
  }
#else
199
	cout << "Physical Graph: " << parse_ptop(PG,SG,ptopfile) << endl;
200
#endif
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

#ifdef DUMP_GRAPH
  {
    cout << "Physical Graph:" << endl;
    
    pvertex_iterator vit,vendit;
    tie(vit,vendit) = vertices(PG);
    
    for (;vit != vendit;vit++) {
      tb_pnode *p = get(pvertex_pmap,*vit);
      cout << *vit << "\t" << *p;
    }
    
    pedge_iterator eit,eendit;
    tie(eit,eendit) = edges(PG);

    for (;eit != eendit;eit++) {
      tb_plink *p = get(pedge_pmap,*eit);
      cout << *eit << " (" << source(*eit,PG) << " <-> " <<
	target(*eit,PG) << ")\t" << *p;
    }
  }
Robert Ricci's avatar
Robert Ricci committed
223
#endif // DUMP_GRAPH
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

#ifdef GRAPH_DEBUG
  {
    cout << "Switch Graph:" << endl;
    

    svertex_iterator vit,vendit;
    tie(vit,vendit) = vertices(SG);
    
    for (;vit != vendit;vit++) {
      tb_switch *p = get(svertex_pmap,*vit);
      cout << *vit << "\t" << *p;
    }
    
    sedge_iterator eit,eendit;
    tie(eit,eendit) = edges(SG);

    for (;eit != eendit;eit++) {
      tb_slink *p = get(sedge_pmap,*eit);
      cout << *eit << " (" << source(*eit,SG) << " <-> " <<
	target(*eit,SG) << ")\t" << *p;
    }
  }
Robert Ricci's avatar
Robert Ricci committed
247
#endif // GRAPH_DEBUG
248

Robert Ricci's avatar
Robert Ricci committed
249 250
  // Set up pnode2vertex - a mapping between vertices in the physical graph and
  // the pnodes that we just read in from the ptop file
251 252 253 254 255 256 257 258
  pvertex_iterator pvit,pvendit;
  tie(pvit,pvendit) = vertices(PG);
  for (;pvit != pvendit;pvit++) {
    pnode2vertex[get(pvertex_pmap,*pvit)]=*pvit;
  }

}

Robert Ricci's avatar
Robert Ricci committed
259 260
// Calculate the minimum spanning tree for the switches - we only consider one
// potential path between each pair of switches.
261
// XXX: Should soon be replaced by calculate_shortest_routes()
262
void calculate_switch_MST() {
263
  cout << "Calculating shortest paths on switch fabric." << endl;
Robert Ricci's avatar
Robert Ricci committed
264 265

  // Set up the weight map for Dijkstra's
266 267 268 269 270 271 272 273 274 275
  tb_sgraph_weight_pmap sweight_pmap = get(edge_weight, SG);
  sedge_iterator seit,seendit;
  tie(seit,seendit) = edges(SG);
  for (;seit != seendit;seit++) {
    tb_slink *slink = get(sedge_pmap,*seit);
    // XXX should we make this more complicated depending on
    // latency/loss as well?
    put(sweight_pmap,*seit,
	100000000-get(pedge_pmap,slink->mate)->delay_info.bandwidth);
  }
Christopher Alfeld's avatar
 
Christopher Alfeld committed
276

Robert Ricci's avatar
Robert Ricci committed
277
  // Let boost do the Disjktra's for us, from each switch
278 279 280 281 282 283 284 285
  svertex_iterator svit,svendit;
  tie(svit,svendit) = vertices(SG);
  for (;svit != svendit;svit++) {
    switch_preds[*svit] = new switch_pred_map(num_vertices(SG));
    switch_dist[*svit] = new switch_dist_map(num_vertices(SG));
    dijkstra_shortest_paths(SG,*svit,
    			    predecessor_map(&((*switch_preds[*svit])[0])).
			    distance_map(&((*switch_dist[*svit])[0])));
Christopher Alfeld's avatar
 
Christopher Alfeld committed
286 287
  }

288 289 290 291 292 293 294 295 296
#ifdef GRAPH_DEBUG
  cout << "Shortest paths" << endl;
  tie(svit,svendit) = vertices(SG);
  for (;svit != svendit;svit++) {
    cout << *svit << ":" << endl;
    for (unsigned int i = 0;i<num_vertices(SG);++i) {
      cout << i << " " << (*switch_dist[*svit])[i] << endl;
    }
  }
Robert Ricci's avatar
Robert Ricci committed
297
#endif // GRAPH_DEBUG
298 299
}

Robert Ricci's avatar
Robert Ricci committed
300
// Read in the .top file
301
void read_virtual_topology(char *filename) {
302 303
  ifstream topfile;
  topfile.open(filename);
Robert Ricci's avatar
Robert Ricci committed
304
  if (!topfile.is_open()) {
305
      cout << "*** Unable to open top file " << filename << endl;
306
      exit(EXIT_FATAL);
Robert Ricci's avatar
Robert Ricci committed
307
  }
308
  
309 310 311 312 313
#ifdef WITH_XML  
  if (vtop_xml_input) {
	cout << "Virtual Graph: " << parse_vtop_xml(VG,filename) << endl;
  }
  else if (vtop_rspec_input){
314
  	  cout << "Virtual Graph: " << parse_request (VG, filename) << endl;
315 316
  }
  else {
317 318 319
      cout << "Virtual Graph: " << parse_top(VG,topfile) << endl;
  }
#else
320
	cout << "Virtual Graph: " << parse_top(VG,topfile) << endl;
321
#endif
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

#ifdef DUMP_GRAPH
  {
    cout << "Virtual Graph:" << endl;
    

    vvertex_iterator vit,vendit;
    tie(vit,vendit) = vertices(VG);
    
    for (;vit != vendit;vit++) {
      tb_vnode *p = get(vvertex_pmap,*vit);
      cout << *vit << "\t" << *p;
    }
    
    vedge_iterator eit,eendit;
    tie(eit,eendit) = edges(VG);

    for (;eit != eendit;eit++) {
      tb_vlink *p = get(vedge_pmap,*eit);
      cout << *eit << " (" << source(*eit,VG) << " <-> " <<
	target(*eit,VG) << ")\t" << *p;
    }
  }  
#endif
}
Robert Ricci's avatar
Robert Ricci committed
347 348 349 350 351 352 353 354 355 356
/*
 * Make a pass through the pclasses, looking for ones that no node can use, and
 * nuking them.
 */
void prune_unusable_pclasses() {
    cout << "Pruning pclasses." << endl;
    int pruned = 0;
    pclass_list::iterator pclass_iterator = pclasses.begin();
    while (pclass_iterator != pclasses.end()) {
	if ((*pclass_iterator)->refcount == 0) {
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
            /*
             * Remove the nodes in the pclass we're removing from the slot
             * counts for their ptypes
             */
            tb_pclass::pclass_members_map::iterator ptype_iterator;
            ptype_iterator = (*pclass_iterator)->members.begin();
            while (ptype_iterator != (*pclass_iterator)->members.end()) {
                /*
                 * Find the recort for this type in the ptypes structure
                 */
                fstring this_type = ptype_iterator->first;
                tb_ptype_map::iterator ptype = ptypes.find(this_type);
                assert(ptype != ptypes.end());
                tb_ptype *this_type_p = ptype->second;

                /*
                 * For every node with this type, we want to remove its slot
                 * count from the total slot count for the type, since we know
375 376 377
                 * we will never use this particular node.
                 * Note: We only have to do this for pclasses that are "real",
                 * not dynamic ones.
378
                 */
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
                if (!(*pclass_iterator)->is_dynamic) {
                    tb_pnodelist::list_iter pnode_iterator =
                        ptype_iterator->second->L.begin();
                    while (pnode_iterator != ptype_iterator->second->L.end()) {
                        /*
                         * Get the slotcount for this ptype
                         */
                        tb_pnode::types_map::iterator tm_iterator;
                        tm_iterator = (*pnode_iterator)->types.find(this_type);
                        assert(tm_iterator != (*pnode_iterator)->types.end());

                        /*
                         * Remove it from the current ptype
                         */
                        this_type_p->remove_slots(
                                tm_iterator->second->get_max_load());

                        /*
                         * Move on to the next node
                         */
                        pnode_iterator++;
                    }
401 402 403 404
                }
                ptype_iterator++;
            }

Robert Ricci's avatar
Robert Ricci committed
405 406 407 408 409 410
	    pclass_list::iterator nukeme = pclass_iterator;
	    pclass_iterator++;
#ifdef PCLASS_DEBUG
	    cout << "Pruning " << (*nukeme)->name << endl;
#endif
	    pruned++;
411

Robert Ricci's avatar
Robert Ricci committed
412 413 414 415 416 417 418 419 420 421
	    delete *nukeme;
	    pclasses.erase(nukeme);
	} else {
	    pclass_iterator++;
	}
    }
    cout << "pclass pruning complete: removed " << pruned << " pclasses, " <<
	pclasses.size() << " remain." << endl;
}

422
void print_help() {
423
  cout << "assign [options] ptopfile topfile [cparams]" << endl;
424
  cout << "Options: " << endl;
425
#ifdef TIME_TERMINATE
426
  cout << "  -l <time>   - Limit runtime." << endl;
Mac Newbold's avatar
Mac Newbold committed
427
#endif
428
  cout << "  -s <seed>   - Set the seed." << endl;
429
#ifdef GRAPHVIZ_SUPPORT
430
  cout << "  -v <viz>    - Produce graphviz files with given prefix." <<
431
    endl;
432
#endif
433 434 435
  cout << "  -r          - Don't allow trivial links." << endl;
  cout << "  -p          - Disable pclasses." << endl;
  cout << "  -d          - Enable dynamic pclasses." << endl;
436
#ifdef PER_VNODE_TT
437
  cout << "  -P          - Prune unusable pclasses." << endl;
438
#endif
439 440 441
  cout << "  -T          - Doing some scoring self-testing." << endl;
  cout << "  -H <float>  - Try <float> times harder." << endl;
  cout << "  -o          - Allow overloaded pnodes to be considered." << endl;
Robert Ricci's avatar
Robert Ricci committed
442 443
  cout << "  -t <float>  - Start the temperature at <float> instead of melting."
      << endl;
444 445
  cout << "  -u           - Print a summary of the solution." << endl;
  cout << "  -c <float>   - Use the 'connected' pnode finding algorithm ";
446 447
  cout <<                   "<float>*100%" << endl;
  cout << "                 of the time." << endl;
448 449 450 451
  cout << "  -n           - Don't anneal - just do the prechecks." << endl;

  cout << "  -x <file>    - Specify a text ptop file" << endl;
  cout << "  -y <file>    - Specify a text top file" << endl;
452
#ifdef WITH_XML
453
  cout << "  -W <file>    - Specify the output rspec file" << endl;
454 455 456
  cout << "  -f <T>[/<T>] - Specify the ptop/vtop file formats " << endl;
  cout << "                 T should be one of (text|xml|rspec)" << endl;
  cout << "                 Specifying only one T is equivalent to -f T/T"<<endl;
457
#endif
458 459 460 461 462
  cout << "  -F          - Apply additional checking to fixed nodes" << endl;
  cout << "  -D          - Dump configuration options" << endl;
  cout << "  cparams     - You probably don't want to touch these!" << endl;
  cout << "                If you must, see config.h in the source for a list"
       << endl;
463
  exit(EXIT_FATAL);
464 465 466 467 468
}
 
// Perfrom a pre-cehck to make sure that there are enough free nodes of the
// proper types. Returns 1 if the proper types exist, 0 if they do not.
// TODO - move away from using global variables
469
int type_precheck(int round) {
470
    cout << "Type precheck:" << endl;
471

472
    bool ok = true;
Mac Newbold's avatar
Mac Newbold committed
473

474 475 476 477 478 479 480 481 482 483 484
    /*
     * Tailor our error messages, depending on the round - the first round of
     * the precheck is looking for available pnodes, the second is looking for
     * sutiable nodes (ie. at least one vnode could map to it)
     */
    char *round_str;
    if (round == 1) {
        round_str = "available";
    } else {
        round_str = "suitable";
    }
485
    // First, check the regular types
486 487
    for (name_count_map::iterator vtype_it=vtypes.begin();
	    vtype_it != vtypes.end();++vtype_it) {
488

489
	// Check to see if there were any pnodes of the type at all
490
	tb_ptype_map::iterator ptype_it = ptypes.find(vtype_it->first);
491
	if (ptype_it == ptypes.end()) {
492
	    cout << "  *** No " << round_str << " physical nodes of type "
493 494
                << vtype_it->first << " found (" << vtype_it->second
                << " requested)" << endl;
495
	    ok = false;
496
	} else {
497
	    // Okay, there are some - are there enough?
498
	    if (ptype_it->second->pnode_slots() < vtype_it->second) {
499
		cout << "  *** " << vtype_it->second << " nodes of type " <<
500 501 502
                    vtype_it->first << " requested, but only " <<
                    ptype_it->second->pnode_slots() << " " << round_str <<
                    " nodes of type " << vtype_it->first<< " found" << endl;
503 504 505 506 507 508 509 510
		ok = false;
	    }
	    // Okay, there are enough - but are we allowed to use them?
	    if (ptype_it->second->maxusers() &&
		    (ptype_it->second->maxusers() < vtype_it->second)) {
		cout << "  *** " << vtype_it->second << " nodes of type " <<
		    vtype_it->first << " requested, but you are only " <<
		    "allowed to use " << ptype_it->second->maxusers() << endl;
511 512
		ok = false;
	    }
513
	}
514
    }
515 516 517 518

    // Check the vclasses, too
    for (name_list_map::iterator vclass_it = vclasses.begin();
	    vclass_it != vclasses.end(); ++vclass_it) {
519
	bool found_match = false;
520
        // Make sure we actually use this vclass
521
        name_vclass_map::iterator dit = vclass_map.find(vclass_it->first);
522 523 524 525 526 527 528 529 530 531 532
        if (dit == vclass_map.end()) {
            cout << "***: Internal error - unable to find vtype " <<
                vclass_it->first << endl;
            exit(EXIT_FATAL);
        } else {
            if (dit->second->empty()) {
                // Nobody uses it, don't check
                continue;
            }
        }

533
	for (vector<fstring>::iterator vtype_it = vclass_it->second.begin();
534
		vtype_it != vclass_it->second.end(); vtype_it++) {
535 536
	    tb_ptype_map::iterator mit = ptypes.find(*vtype_it);
	    if ((mit != ptypes.end()) && (mit->second->pnode_slots() != 0)) {
537 538 539 540 541 542
		found_match = true;
		break;
	    }
	}

	if (!found_match) {
543 544
	    cout << "  *** No " << round_str <<
                " physical nodes can satisfy vclass " << vclass_it->first << endl;
545 546 547 548
	    ok = false;
	}
    }

549
    if (ok) {
550
      cout << "Type precheck passed." << endl;
551 552
      return 1;
    } else {
553
      cout << "*** Type precheck failed!" << endl;
554 555 556
      return 0;
    }
}
557

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
// Perfrom a pre-cehck to make sure that every vnode has at least one possible
// mapping.  Returns 1 if this is the case, 0 if not.
// TODO - move away from using global variables
int mapping_precheck() {
#ifdef PER_VNODE_TT
    cout << "Node mapping precheck:" << endl;
    /*
     * Build up an entry in the type table for each vnode, by first looking at
     * the type table entry for the vnode's type, then checking each entry to
     * make sure that it:
     * (1) Has enough interfaces
     * (2) Has enough total bandwidth (for emulated links)
     * (3) Meets any 1.0-weight features and/or desires
     */
    vvertex_iterator vit,vendit;
    tie(vit,vendit) = vertices(VG);
574

575 576 577 578
    /*
     * Indicates whether all nodes have potential matches or not
     */
    bool ok = true;
579

580 581
    for (;vit != vendit;vit++) {
	tb_vnode *v = get(vvertex_pmap,*vit);
582

583 584 585 586 587 588 589 590
	pclass_vector *vec = new pclass_vector();
	vnode_type_table[v->name] = tt_entry(0,vec);

	// This constitutes a list of the number of ptypes that matched the
	// criteria. We use to guess what's wrong with the vnode.
	int matched_bw = 0;
	// Keep track of desires had how many 'hits', so that we can tell
	// if any simply were not matched
591
	map<fstring,int> matched_desires;
592

593 594 595
	// Keep track of which link types had how many 'hits', so that we can
	// tell which type(s) caused this node to fail
	tb_vnode::link_counts_map matched_link_counts;
596 597
        hash_map<fstring,int> max_links;
        hash_map<fstring,int> desired_links;
598
	map<fstring,bool> matched_links;
599

600
	tb_vclass *vclass = v->vclass;
601
	tb_vclass::members_map::const_iterator mit;
602
	if (vclass) {
603
	    mit = vclass->get_members().begin();
604
	}
605 606 607
	for (;;) {
	    // Loop over all types this node can take on, which might be only
	    // one, if it's not part of a vclass
608
	    fstring this_type;
609 610 611 612 613
	    if (vclass) {
		this_type = mit->first;
	    } else {
		this_type = v->type;
	    }
614

615 616 617 618 619 620 621 622
            // Check to make sure there are actually nodes of this type in the
            // physical topology
            if (type_table.find(this_type) == type_table.end()) {
                // Yes, I know, goto is evil. But I'm not gonna indent the next
                // 100 lines of code for this error case
                goto nosuchtype;
            }

623 624
	    for (pclass_vector::iterator it = type_table[this_type].second->begin();
		    it != type_table[this_type].second->end(); it++) {
625

626 627
		bool potential_match = true;
		// Grab the first node of the pclass as a representative sample
628
	 	tb_pnode *pnode = *((*it)->members[this_type]->L.begin());
629 630 631

		// Check to see if any of the link that this pnode has are of
		// the correct type for the virtual node
Mac Newbold's avatar
Mac Newbold committed
632

633 634 635 636
		// Check bandwidth on emulated links
		if (pnode->total_bandwidth >= v->total_bandwidth) {
		    matched_bw++;
		} else {
637
			potential_match = false;
638
		}
Mac Newbold's avatar
Mac Newbold committed
639

640 641
		// Check to see if if the pnode has enough slots of the
		// appropriate type available
642 643 644
		tb_pnode::types_map::iterator type_iterator =
		    pnode->types.find(v->type);
		if (type_iterator == pnode->types.end()) {
645 646
		    // Must have been a vtype to get here - ignore it
		} else {
647
		    if (v->typecount > type_iterator->second->get_max_load()) {
648 649 650 651 652
			// Nope, this vnode is too demanding
			potential_match = false;
		    }
		}

653 654 655 656 657 658 659
		/*
		 * Check features and desires
		*/
		tb_featuredesire_set_iterator
		    fdit(v->desires.begin(),v->desires.end(),
			 pnode->features.begin(),pnode->features.end());
		for (;!fdit.done();fdit++) {
660
		    // Skip 'local' and 'global' features
661
		    if (fdit->is_global() || fdit->is_local()) {
662 663
			continue;
		    }
664 665 666
		    // Only check for FDs that would result in a violation if
		    // unmatched.
		    if (fdit.either_violateable()) {
667 668 669 670 671
			if (fdit.membership() !=
				tb_featuredesire_set_iterator::BOTH) {
			    potential_match = false;
			}

672 673
			// We look for violateable desires on vnodes so that we
			// can report them to the user
674 675 676 677 678 679 680 681 682
			if ((fdit.membership() ==
				tb_featuredesire_set_iterator::FIRST_ONLY ||
			    fdit.membership() ==
				 tb_featuredesire_set_iterator::BOTH)
				&& fdit.first_iterator()->is_violateable()) {
			    if (matched_desires.find(fdit->name()) ==
				    matched_desires.end()) {
				matched_desires[fdit->name()] = 0;
			    }
683 684 685 686
			}
			if (fdit.membership() ==
				tb_featuredesire_set_iterator::BOTH) {
			    matched_desires[fdit->name()]++;
687 688 689
			}
		    }
		}
690
		
691 692 693 694 695 696
		// Check link types - right now, we treat LANs specially, which
                // I am not happy about, but it seems to be necessary.
                // Otherwise, we can get a false negative when there are few
                // ports on a switch available, but we could map by using
                // the trunk links
                if (this_type != "lan") {
697 698 699 700 701 702
                  tb_vnode::link_counts_map::iterator link_it;
                  for (link_it = v->link_counts.begin();
		       link_it != v->link_counts.end();
                       link_it++) {
                    fstring type = link_it->first;
                    int count = link_it->second;
703
                    desired_links[type] = count;
704 705 706 707
                    if (pnode->link_counts.find(type) !=
                          pnode->link_counts.end()) {
                      // Found at least one link of this type
                      matched_links[type] = true;
708 709 710
                      if (pnode->link_counts[type] > max_links[type]) {
                        max_links[type] = pnode->link_counts[type];
                      }
711 712 713 714 715 716 717 718 719 720 721
                      if (pnode->link_counts[type] >= count) {
                        // Great, there are enough, too
                        matched_link_counts[type]++;
                      } else {
                        potential_match = false;
                      }
                    } else {
                      potential_match = false;
                    }
                  }
                }
Mac Newbold's avatar
Mac Newbold committed
722

723 724 725 726 727 728
		if (potential_match) {
		    vec->push_back(*it);
		    vnode_type_table[v->name].first++;
		    (*it)->refcount++;
#ifdef PCLASS_DEBUG
		    cerr << v->name << " can map to " << (*it)->name << endl;
729
#endif
730 731
		}
	    }
732

733
nosuchtype:
734 735
	    if (vclass) { 
		mit++;
736
		if (mit == vclass->get_members().end()) {
737 738 739 740 741 742
		    break;
		}
	    } else {
		// If not a vtype, we only had to do this once
		break;
	    }
743
	}
744

745
	if (vnode_type_table[v->name].first == 0) {
746
	    cout << "  *** No possible mapping for " << v->name << endl;
747
	    // Make an attempt to figure out why it didn't match
748 749 750 751 752
	    
	    // Check all of its link types
	    tb_vnode::link_counts_map::iterator lit;
	    for (lit = v->link_counts.begin(); lit != v->link_counts.end();
		lit++) {
753
	      fstring type = lit->first;
754
	      if (!matched_links[type]) {
755 756
		cout << "      No links of type " << type << " found! (" <<
                  desired_links[type] << " requested)" << endl;
757 758
	      } else {
		if (!matched_link_counts[type]) {
759 760 761
		  cout << "      Too many links of type " << type << "! (" <<
                    desired_links[type] << " requested, " << max_links[type] <<
                    " found)" << endl;
762 763
		}
	      }
764
	    }
765

766
	    if (!matched_bw) {
767
		cout << "      Too much bandwidth on emulated links!" << endl;
768
	    }
769

770
	    for (map<fstring,int>::iterator dit = matched_desires.begin();
771 772 773
		    dit != matched_desires.end();
		    dit++) {
		if (dit->second == 0) {
774
		    cout << "      No physical nodes have feature " << dit->first
775 776
			<< "!" << endl;
		}
777
	    }
778
	    ok = false;
779
	}
780 781 782
#ifdef PCLASS_DEBUG
	cerr << v->name << " can map to " << vnode_type_table[v->name].first << " pclasses"
	    << endl;
783
#endif
784

785
    }
786

787 788 789
    if (ok) {
	cout << "Node mapping precheck succeeded" << endl;
	return 1;
790
    } else {
791
	cout << "*** Node mapping precheck failed!" << endl;
792
	return 0;
793 794
    }

795 796 797
#else // PER_VNODE_TT
    // PER_VNODE_TT is required for this check, just pretend it's OK.
    return 1;
Robert Ricci's avatar
Robert Ricci committed
798
#endif
799
}
Christopher Alfeld's avatar
 
Christopher Alfeld committed
800

801 802 803 804 805 806 807 808 809 810 811 812 813 814
// Perfrom a pre-cehck to make sure that polices that are checkable at precheck
// time are not violated. Returns 1 if everything is A-OK, 0 otherwise
// TODO - move away from using global variables
int policy_precheck() {
  cout << "Policy precheck:" << endl;
  if (tb_featuredesire::check_desire_policies()) {
    cout << "Policy precheck succeeded" << endl;
    return 1;
  } else {
    cout << "*** Policy precheck failed!" << endl;
    return 0;
  }
}  

815 816 817
// Signal handler - add a convneint way to kill assign and make it return an
// unretryable error
void exit_unretryable(int signal) {
818
  cout << "*** Killed with signal " << signal << " - exiting!" << endl;
819 820 821 822 823 824 825 826 827
  _exit(EXIT_UNRETRYABLE);
}

// Singal handler - add a way for a user to get some status information
extern double temp;
void status_report(int signal) {
  cout << "I: " << iters << " T: " << temp << " S: " << get_score() << " V: "
    << violated << " (Best S: " << absbest << " V:" << absbestviolated << ")"
    << endl;
828
  cout.flush();
829 830
}

831 832 833 834
// From anneal.cc - the best solution found
extern solution best_solution;

int main(int argc,char **argv) {
835
  int seed = 0;
836
#ifdef GRAPHVIZ_SUPPORT
837
  fstring viz_prefix;
838
#endif
839
  bool scoring_selftest = false;
840
  bool prechecks_only = false;
841 842 843 844 845
  
  // Handle command line
  char ch;
  timelimit = 0.0;
  timetarget = 0.0;
846 847 848
  
  char* ptopFilename = "";
  char* vtopFilename = "";
849 850
  char* vtopOutputFilename = 0;

851 852 853 854 855 856
#ifdef WITH_XML
	char* ptopFileFormat;
	char* vtopFileFormat;
	char* delims = "/";
	char* flags = "s:v:l:t:rpPTdH:oguc:nx:y:W:FDf:";
#else
Mike Hibler's avatar
Mike Hibler committed
857
	char* flags = "s:v:l:t:rpPTdH:oguc:nx:y:FD";
858 859 860
#endif	
	
  while ((ch = getopt(argc,argv,flags)) != -1) {
861 862 863
    switch (ch) {
    case 's':
      if (sscanf(optarg,"%d",&seed) != 1) {
864
		print_help();
865
      }
866
      break;
867
#ifdef GRAPHVIZ_SUPPORT
868 869 870
    case 'v':
      viz_prefix = optarg;
      break;
871
#endif
872 873 874 875 876 877 878 879 880 881 882 883 884
#ifdef TIME_TERMINATE
    case 'l':
      if (sscanf(optarg,"%lf",&timelimit) != 1) {
	print_help();
      }
      break;
#endif
#ifdef TIME_TARGET
    case 't':
      if (sscanf(optarg,"%lf",&timetarget) != 1) {
	print_help();
      }
      break;
885
#endif
886 887 888
    case 'r':
      allow_trivial_links = false; break;
    case 'p':
889
      disable_pclasses = true; break;
Robert Ricci's avatar
Robert Ricci committed
890 891 892 893
#ifdef PER_VNODE_TT
    case 'P':
      prune_pclasses = true; break;
#endif
894 895
    case 'T':
      scoring_selftest = true; break;
896 897
    case 'd':
      dynamic_pclasses = true; break;
898 899 900 901 902
    case 'H':
      if (sscanf(optarg,"%lf",&scale_neighborhood) != 1) {
	print_help();
      }
      break;
903 904
    case 'o':
      allow_overload = true; break;
Robert Ricci's avatar
Robert Ricci committed
905 906
    case 'g':
      greedy_link_assignment = true; break;
Robert Ricci's avatar
Robert Ricci committed
907 908 909 910 911
    case 't':
      if (sscanf(optarg,"%lf",&initial_temperature) != 1) {
	print_help();
      }
      no_melting = true; break;
912 913 914 915 916 917 918
    case 'u':
      print_summary = true; break;
    case 'c':
      if (sscanf(optarg,"%lf",&use_connected_pnode_find) != 1) {
	print_help();
      }
      break;
919 920 921 922
    case 'n':
      prechecks_only = true;
      cout << "Doing only prechecks, exiting early" << endl;
      break;
923 924 925
    case 'D':
      dump_config = true;
      break;
926
    case 'x':
927 928 929 930 931 932 933
#ifdef WITH_XML
      ptop_xml_input = false;
#endif
      if (strcmp(optarg, "") == 0) {
      	print_help();
	  }
	  ptopFilename = optarg;
934
      break;
935 936
    
		case 'y':
937 938
#ifdef WITH_XML
      vtop_xml_input = false;
939
#endif
940 941 942 943 944
      if (strcmp(optarg, "") == 0) {
      	print_help();
	  }
	  vtopFilename = optarg;
    break;
945
	  case 'F':
946 947 948 949 950
          check_fixed_nodes = true;
    break;

#ifdef WITH_XML

951 952 953 954 955
    case 'W':
    if (strcmp(optarg, "") == 0) {
      print_help();
    }
    vtopOutputFilename = optarg;
956
    break;
957

958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
	case 'f':
		if (strcmp(optarg, "") == 0) {
			print_help();
		}
		
		ptopFileFormat = strtok(optarg, delims);
		vtopFileFormat = strtok(NULL, delims);
		if (strcmp(ptopFileFormat, "text") == 0) {
			ptop_xml_input = false;
		}
		else if (strstr(ptopFileFormat, "rspec") != NULL) {
			ptop_rspec_input = true;
		}
		else if (strstr(ptopFileFormat, "xml") != NULL){
			ptop_xml_input = true;
		}
		else {
			print_help();
		}
		
		if (vtopFileFormat == NULL)
		{
			vtop_xml_input = ptop_xml_input;
			vtop_rspec_input = ptop_rspec_input;
		}
		else 
		{
			if (strcmp(vtopFileFormat, "text") == 0) {
				vtop_xml_input = false;
			}
			else if (strstr(vtopFileFormat, "rspec") != NULL) {
				vtop_rspec_input = true;
			}
			else if (strstr(vtopFileFormat, "xml") != NULL){
				vtop_xml_input = true;
			}
			else {
				print_help();
			}
		}
		
		break;
1000
#endif
1001 1002
		
	default:
1003
      print_help();
1004
    }
1005
  }
1006 1007 1008 1009

  // Save argv and argc, and advance past the initial options
  char **oldargv = argv;
  int oldargc = argc;
1010 1011
  argc -= optind;
  argv += optind;
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023

  if (strcmp(ptopFilename, "") == 0 && argc >= 1) {
      ptopFilename = argv[0];
      argc -= 1;
      argv += 1;
  }

  if (strcmp(vtopFilename, "") == 0 && argc >= 1) {
      vtopFilename = argv[0];
      argc -= 1;
      argv += 1;
  }
1024
  
1025
  if (argc > 0)
1026
  {
1027 1028 1029
      // If there are still more options, they must be from the common.h
      // parameters.
      parse_options(argv, options, noptions);
1030 1031 1032 1033 1034 1035 1036 1037
  }
  
  if (strcmp(ptopFilename, "") == 0)
	  print_help();
  	
  if (strcmp(vtopFilename, "") == 0)
	  print_help();	
  
1038 1039 1040 1041 1042 1043
  if (seed == 0) {
    if (getenv("ASSIGN_SEED") != NULL) {
      sscanf(getenv("ASSIGN_SEED"),"%d",&seed);
    } else {
      seed = time(NULL)+getpid();
    }
1044
  }
1045

1046
#ifdef GRAPHVIZ_SUPPORT
1047 1048 1049
  if (viz_prefix.size() == 0) {
    if (getenv("ASSIGN_GRAPHVIZ") != NULL) {
      viz_prefix = getenv("ASSIGN_GRAPHVIZ");
1050
    }
1051
  }
Robert Ricci's avatar