score.cc 20.6 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1 2 3 4 5 6
/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2000-2002 University of Utah and the Flux Group.
 * All rights reserved.
 */

Mac Newbold's avatar
Mac Newbold committed
7 8 9 10 11 12 13 14 15 16 17 18
/*
 * ASSUMPTIONS:
 *  1. Any switch can get to any other switch either directly
 *     or via at most one other switch (star formation).
 */

// Note on variable names: LEDA has generic 'edge' and 'node'.  When
// these are translated to 'tb_*' structures the variables end in
// r.  I.e. dst -> dstr.  dst is a node, and dstr is a tb_pnode or similar.

// Not sure we need all these LEDA includes.
#include <LEDA/graph_alg.h>
19
#include <LEDA/ugraph.h>
Mac Newbold's avatar
Mac Newbold committed
20 21 22 23
#include <LEDA/graphwin.h>
#include <LEDA/dictionary.h>
#include <LEDA/map.h>
#include <LEDA/graph_iterator.h>
24
#include <LEDA/sortseq.h>
Mac Newbold's avatar
Mac Newbold committed
25 26 27 28 29 30 31 32 33
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>

#include "common.h"
34
#include "vclass.h"
Mac Newbold's avatar
Mac Newbold committed
35 36
#include "virtual.h"
#include "physical.h"
Christopher Alfeld's avatar
 
Christopher Alfeld committed
37 38 39
#include "pclass.h"
#include "score.h"

Mac Newbold's avatar
Mac Newbold committed
40 41 42

#include "assert.h"

43 44 45
typedef node_array<edge> switch_pred_array;
extern node_array<switch_pred_array> switch_preds;

Mac Newbold's avatar
Mac Newbold committed
46 47 48 49 50 51 52 53 54 55
float score;			// The score of the current mapping
int violated;			// How many times the restrictions
				// have been violated.
node pnodes[MAX_PNODES];	// int->node map
				// pnodes[0] == NULL

violated_info vinfo;		// specific info on violations

extern tb_vgraph G;		// virtual graph
extern tb_pgraph PG;		// physical grpaph
56
extern tb_sgraph SG;		// switch fabric
Mac Newbold's avatar
Mac Newbold committed
57 58 59 60 61 62

edge direct_link(node a,node b);
void score_link(edge e,edge v,bool interswitch);
void unscore_link(edge e,edge v,bool interswitch);
edge find_link_to_switch(node n);
int find_intraswitch_path(node src,node dst,edge *first,edge *second);
63
int find_interswitch_path(node src,node dst,int bandwidth,list<edge> &L);
Mac Newbold's avatar
Mac Newbold committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

#ifdef SCORE_DEBUG_MORE
#define SADD(amount) fprintf(stderr,"SADD: %s = %.2f\n",#amount,amount);score+=amount
#define SSUB(amount) fprintf(stderr,"SSUB: %s = %.2f\n",#amount,amount);score-=amount
#else
#define SADD(amount) score += amount
#define SSUB(amount) score -= amount
#endif

/*
 * score()
 * Returns the score.
 */
float get_score() {return score;}

/*
 * init_score()
 * This initialized the scoring system.  It also clears all
 * assignments.
 */
void init_score()
{
#ifdef SCORE_DEBUG
  fprintf(stderr,"SCORE: Initializing\n");
#endif
  score=0;
  violated=0;
  vinfo.unassigned = vinfo.pnode_load = 0;
  vinfo.no_connection = vinfo.link_users = vinfo.bandwidth = 0;

  node n;
  edge e;
  forall_nodes(n,G) {
    tb_vnode &vn=G[n];
    vn.posistion=0;
    vn.no_connections=0;
    SADD(SCORE_UNASSIGNED);
    vinfo.unassigned++;
    violated++;
  }
  forall_edges(e,G) {
    tb_vlink &ve=G[e];
    ve.type=tb_vlink::LINK_UNKNOWN;
Christopher Alfeld's avatar
 
Christopher Alfeld committed
107
    ve.plink=NULL;
Mac Newbold's avatar
Mac Newbold committed
108 109 110 111 112 113 114 115 116 117
  }
  forall_nodes(n,PG) {
    tb_pnode &pn=PG[n];
    pn.typed=false;
    pn.current_load=0;
    pn.pnodes_used=0;
  }
  forall_edges(e,PG) {
    tb_plink &pe=PG[e];
    pe.bw_used=0;
118 119
    pe.emulated=0;
    pe.nonemulated=0;
Mac Newbold's avatar
Mac Newbold committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  }

  assert(pnodes[0] == NULL);
#ifdef SCORE_DEBUG
  fprintf(stderr,"  score = %.2f violated = %d\n",score,violated);
#endif
}

/*
 * void remove_node(node node)
 * This removes a virtual node from the assignments, adjusting
 * the score appropriately.
 */
void remove_node(node n)
{
  /* Find pnode assigned to */
  node pnode;

  tb_vnode &vnoder = G[n];
  pnode = pnodes[vnoder.posistion];
  tb_pnode &pnoder = PG[pnode];

#ifdef SCORE_DEBUG
143
  cerr <<  "SCORE: remove_node(" << vnoder.name << ")\n";
Mac Newbold's avatar
Mac Newbold committed
144 145 146 147 148
  fprintf(stderr,"       no_connections = %d\n",vnoder.no_connections);
#endif

  assert(pnode != NULL);

Christopher Alfeld's avatar
 
Christopher Alfeld committed
149 150 151 152 153
  pclass_unset(pnoder);

  // pclass
  if (pnoder.my_class->used == 0) {
#ifdef SCORE_DEBUG
154
    cerr << "  freeing pclass\n";
Christopher Alfeld's avatar
 
Christopher Alfeld committed
155 156 157
#endif
    SSUB(SCORE_PCLASS);
  }
158 159 160 161 162 163 164 165 166 167 168 169 170 171

  // vclass
  if (vnoder.vclass != NULL) {
    double score_delta = vnoder.vclass->unassign_node(vnoder.type);
#ifdef SCORE_DEBUG
    cerr << "  vclass unassign " << score_delta << endl;
#endif
    
    if (score_delta <= -1) {
      violated--;
      vinfo.vclass--;
    }
    SSUB(-score_delta*SCORE_VCLASS);
  }
Christopher Alfeld's avatar
 
Christopher Alfeld committed
172
  
Mac Newbold's avatar
Mac Newbold committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  edge e;
  tb_vlink *vlink;
  node vdst;
  tb_vnode *vdstr;
  node pdst;
  tb_pnode *pdstr;

  // remove the scores associated with each edge
  forall_inout_edges(e,n) {
    vlink=&G[e];
    vdst=G.target(e);
    if (vdst == n)
      vdst = G.source(e);
    vdstr=&G[vdst];
#ifdef SCORE_DEBUG
188
    cerr << "  edge to " << vdstr->name << endl;
Mac Newbold's avatar
Mac Newbold committed
189 190 191 192 193 194 195 196 197 198 199 200 201
#endif
    if (vdstr->posistion == 0) continue;
    if (vlink->type == tb_vlink::LINK_DIRECT) {
      // DIRECT LINK
#ifdef SCORE_DEBUG
      fprintf(stderr,"   direct link\n");
#endif
      unscore_link(vlink->plink,e,false);
    } else if (vlink->type == tb_vlink::LINK_INTERSWITCH) {
      // INTERSWITCH LINK
      pdst=pnodes[vdstr->posistion];
      pdstr=&PG[pdst];
#ifdef SCORE_DEBUG
202
      cerr << "  interswitch link\n";
Mac Newbold's avatar
Mac Newbold committed
203 204
#endif

205 206 207 208 209 210 211
      edge cur;      
      forall(cur,G[e].path) {
	SSUB(SCORE_INTERSWITCH_LINK);
	unscore_link(cur,e,true);
      }
      G[e].path.clear();
      
Mac Newbold's avatar
Mac Newbold committed
212 213 214 215 216 217 218 219 220 221 222
      unscore_link(vlink->plink_local_one,e,false);
      unscore_link(vlink->plink_local_two,e,false);
    } else if (vlink->type == tb_vlink::LINK_INTRASWITCH) {
      // INTRASWITCH LINK
#ifdef SCORE_DEBUG
      fprintf(stderr,"   intraswitch link\n");
#endif
      SSUB(SCORE_INTRASWITCH_LINK);

      unscore_link(vlink->plink,e,false);
      unscore_link(vlink->plink_two,e,false); 
223
    } else if (vlink->type != tb_vlink::LINK_TRIVIAL) {
Mac Newbold's avatar
Mac Newbold committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
      // No link
      fprintf(stderr,"Internal error - no link\n");
      abort();
    }
  }

  // remove scores associated with the node
  SSUB(SCORE_NO_CONNECTION*vnoder.no_connections);
  violated -= vnoder.no_connections;
  vinfo.no_connection -= vnoder.no_connections;

  // adjust pnode scores
  pnoder.current_load--;
  vnoder.posistion = 0;
  if (pnoder.current_load == 0) {
    // release pnode
#ifdef SCORE_DEBUG
    fprintf(stderr,"  releasing pnode\n");
#endif
    SSUB(SCORE_PNODE);
    node the_switch=pnoder.the_switch;
    if (the_switch) {
      if ((--PG[the_switch].pnodes_used) == 0) {
#ifdef SCORE_DEBUG
248
	cerr << "  releasing switch " << PG[the_switch].name << endl;
Mac Newbold's avatar
Mac Newbold committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
#endif
	// release switch
	SSUB(SCORE_SWITCH);
      }
    }
    // revert pnode type
    pnoder.typed=false;
  } else if (pnoder.current_load >= pnoder.max_load) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  reducing penalty, new load = %d (>= %d)\n",pnoder.current_load,pnoder.max_load);
#endif
    SSUB(SCORE_PNODE_PENALTY);
    vinfo.pnode_load--;
    violated--;
  }
  // add score for unassigned node
  SADD(SCORE_UNASSIGNED);
  vinfo.unassigned++;
  violated++;
268 269

  // features/desires
Christopher Alfeld's avatar
 
Christopher Alfeld committed
270 271 272 273 274
  int fd_violated;
  double fds=fd_score(vnoder,pnoder,&fd_violated);
  SSUB(fds);
  violated -= fd_violated;
  vinfo.desires -= fd_violated;
275
  
Mac Newbold's avatar
Mac Newbold committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
#ifdef SCORE_DEBUG
  fprintf(stderr,"  new score = %.2f  new violated = %d\n",score,violated);
#endif
}

/*
 * int add_node(node node,int ploc)
 * Add a mapping of node to ploc and adjust score appropriately.
 * Returns 1 in the case of an incompatible mapping.  This should
 * never happen as the same checks should be in place in a higher
 * level.  (Optimization?)
 */
int add_node(node n,int ploc)
{
  tb_vnode &vnoder=G[n];
  node pnode = pnodes[ploc];
  tb_pnode &pnoder=PG[pnode];

#ifdef SCORE_DEBUG
295
  cerr << "SCORE: add_node(" << vnoder.name << "," << pnoder.name << "[" << ploc << "])\n";
Mac Newbold's avatar
Mac Newbold committed
296 297
  fprintf(stderr,"  vnode type = ");
  cerr << vnoder.type << " pnode switch = ";
298 299 300 301 302
  if (pnoder.the_switch) {
    cerr << PG[pnoder.the_switch].name;
  } else {
    cerr << "No switch";
  }
Mac Newbold's avatar
Mac Newbold committed
303 304 305 306 307 308 309 310
  cerr << endl;
#endif
  
  // set up pnode
  // figure out type
  if (!pnoder.typed) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  virgin pnode\n");
311
    cerr << "    vtype = " << vnoder.type << "\n";
Mac Newbold's avatar
Mac Newbold committed
312 313 314 315 316
#endif
    
    // Remove check assuming at higher level?
    // Remove higher level checks?
    pnoder.max_load=0;
317 318
    if (pnoder.types.lookup(vnoder.type) != nil)
      pnoder.max_load = pnoder.types.access(vnoder.type);
Mac Newbold's avatar
Mac Newbold committed
319 320 321 322 323 324 325 326
    
    if (pnoder.max_load == 0) {
      // didn't find a type
#ifdef SCORE_DEBUG
      fprintf(stderr,"  no matching type\n");
#endif
      return 1;
    }
327 328 329 330

    pnoder.current_type=vnoder.type;
    pnoder.typed=true;

Mac Newbold's avatar
Mac Newbold committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
#ifdef SCORE_DEBUG
    fprintf(stderr,"  matching type found (");
    cerr << pnoder.current_type << ", max = " << pnoder.max_load;
    cerr << ")" << endl;
#endif
  } else {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  pnode already has type\n");
#endif
    if (pnoder.current_type != vnoder.type) {
#ifdef SCORE_DEBUG      
      fprintf(stderr,"  incompatible types\n");
#endif
      return 1;
    } else {
#ifdef SCORE_DEBUG
      fprintf(stderr,"  comaptible types\n");
#endif
      if (pnoder.current_load == pnoder.max_load) {
350 351 352
	/* XXX - We could ignore this check and let the code
	   at the end of the routine penalize for going over
	   load.  Failing here seems to work better though. */
Mac Newbold's avatar
Mac Newbold committed
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
#ifdef SCORE_DEBUG
	fprintf(stderr,"  node is full\n");
#endif
	return 1;
      }
      
    }
  }

  // set up links
  vnoder.no_connections=0;
  edge e;
  node dst;
  tb_vlink *er;
  tb_plink *pl;
  edge pedge;
  forall_inout_edges(e,n) {
    dst=G.source(e);
    er=&G[e];
    if (dst == n) {
      dst=G.target(e);
    }
    tb_vnode &dstr=G[dst];
    assert(dst != n);
    assert(&dstr != &vnoder);
#ifdef SCORE_DEBUG
379
    cerr << "  edge to " << dstr.name << endl;
Mac Newbold's avatar
Mac Newbold committed
380 381 382 383 384 385 386 387
#endif

    if (dstr.posistion != 0) {
      // dstr is assigned
      node dpnode=pnodes[dstr.posistion];
      tb_pnode &dpnoder=PG[dpnode];

#ifdef SCORE_DEBUG
388
      cerr << "   goes to " << dpnoder.name << endl;
Mac Newbold's avatar
Mac Newbold committed
389 390
#endif

391 392 393 394
      if (dpnode == pnode) {
#ifdef SCORE_DEBUG
	fprintf(stderr,"  trivial link\n");
#endif SCORE_DEBUG
395 396 397 398 399
	if (allow_trivial_links) {
	  er->type = tb_vlink::LINK_TRIVIAL;
	} else {
	  goto CLEANUP;
	}
400
      } else if ((pedge=direct_link(dpnode,pnode)) != NULL) {
Mac Newbold's avatar
Mac Newbold committed
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
#ifdef SCORE_DEBUG
	fprintf(stderr,"   found direct link = %p\n",pedge);
#endif
	pl = &PG[pedge];

	// direct
	er->type = tb_vlink::LINK_DIRECT;
	er->plink = pedge;

	score_link(pedge,e,false);
      } else if (pnoder.the_switch &&
		 (pnoder.the_switch == dpnoder.the_switch)) {
	// intraswitch
	assert(pnoder.the_switch == dpnoder.the_switch);
	edge first,second;
	if (find_intraswitch_path(pnode,dpnode,&first,&second) == 1) {
	  fprintf(stderr,"Internal error: Could not find intraswitch link!\n");
	  abort();
	}

	assert(first != NULL);
	assert(second != NULL);
	
#ifdef SCORE_DEBUG
	fprintf(stderr,"   found intraswitch link (%p,%p)\n",first,second);
#endif
	er->type = tb_vlink::LINK_INTRASWITCH;
	er->plink = first;
	er->plink_two = second;
	SADD(SCORE_INTRASWITCH_LINK);

	// check users and bandwidth
	score_link(first,e,false);
	score_link(second,e,false);
      } else {
	// try to find interswitch
#ifdef SCORE_DEBUG
438
	cerr << "   looking for interswitch link " <<
439 440
	  (pnoder.the_switch != nil?PG[pnoder.the_switch].name:string("No Switch")) << " " <<
	  (dpnoder.the_switch != nil?PG[dpnoder.the_switch].name:string("No Switch")) << endl;
Mac Newbold's avatar
Mac Newbold committed
441 442
#endif
	if (find_interswitch_path(pnoder.the_switch,dpnoder.the_switch,
443
				  er->bandwidth,er->path) == 0) {
Mac Newbold's avatar
Mac Newbold committed
444 445 446
#ifdef SCORE_DEBUG
	  fprintf(stderr,"   could not find path - no connection\n");
#endif
Christopher Alfeld's avatar
 
Christopher Alfeld committed
447

448
CLEANUP:
Christopher Alfeld's avatar
 
Christopher Alfeld committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
	  // Need to free up all links already made and abort
	  forall_inout_edges(e,n) {
	    tb_vlink &vlink = G[e];
	    if (vlink.type == tb_vlink::LINK_DIRECT) {
	      unscore_link(vlink.plink,e,false);
	    } else if (vlink.type == tb_vlink::LINK_INTRASWITCH) {
	      SSUB(SCORE_INTRASWITCH_LINK);
	      unscore_link(vlink.plink,e,false);
	      unscore_link(vlink.plink_two,e,false);
	    } else if (vlink.type == tb_vlink::LINK_INTERSWITCH) {
	      edge cur;
	      forall(cur,G[e].path) {
		SSUB(SCORE_INTERSWITCH_LINK);
		unscore_link(cur,e,true);
	      }
	      G[e].path.clear();
	      unscore_link(vlink.plink_local_one,e,false);
	      unscore_link(vlink.plink_local_two,e,false);
	    } // else LINK_UNKNOWN i.e. unassigned.
	  }

	  // Reset to be retyped next time and abort.  This is a
	  // fatal error.
Mac Newbold's avatar
Mac Newbold committed
472 473 474 475
	  pnoder.typed = false;
	  return 1;
 	} else {
#ifdef SCORE_DEBUG
476
	  fprintf(stderr,"   found interswitch link\n");
Mac Newbold's avatar
Mac Newbold committed
477 478 479
#endif
	  er->type=tb_vlink::LINK_INTERSWITCH;

480 481 482 483 484 485 486 487
	  edge cur;
	  forall(cur,er->path) {
	    SADD(SCORE_INTERSWITCH_LINK);
#ifdef SCORE_DEBUG
	    cerr << "     " << PG[cur].name << endl;
#endif
	    score_link(cur,e,true);
	  }
Mac Newbold's avatar
Mac Newbold committed
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

	  er->plink_local_one = find_link_to_switch(pnode);
	  assert(er->plink_local_one != NULL);
	  er->plink_local_two = find_link_to_switch(dpnode);
	  assert(er->plink_local_two != NULL);

	  score_link(er->plink_local_one,e,false);
	  score_link(er->plink_local_two,e,false);
	}
      }
    }
  }
    
  // finish setting up pnode
  pnoder.current_load++;
  vnoder.posistion = ploc;
  if (pnoder.current_load > pnoder.max_load) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  load to high - penalty (%d)\n",pnoder.current_load);
#endif
    SADD(SCORE_PNODE_PENALTY);
    vinfo.pnode_load++;
    violated++;
  } else {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  load is fine\n");
#endif
  }
  if (pnoder.current_load == 1) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  new pnode\n");
#endif
    SADD(SCORE_PNODE);
    if (pnoder.the_switch &&
	(++PG[pnoder.the_switch].pnodes_used) == 1) {
#ifdef SCORE_DEBUG
      fprintf(stderr,"  new switch\n");
#endif
      SADD(SCORE_SWITCH);
    }
  }

  // node no longer unassigned
  SSUB(SCORE_UNASSIGNED);
  vinfo.unassigned--;
  violated--;

535
  // features/desires
Christopher Alfeld's avatar
 
Christopher Alfeld committed
536 537 538 539 540 541 542 543
  int fd_violated;
  double fds = fd_score(vnoder,pnoder,&fd_violated);
  SADD(fds);
  violated += fd_violated;
  vinfo.desires += fd_violated;

  // pclass
  if (pnoder.my_class->used == 0) {
544
#ifdef SCORE_DEBUG
545
    cerr << "  new pclass\n";
546
#endif
Christopher Alfeld's avatar
 
Christopher Alfeld committed
547
    SADD(SCORE_PCLASS);
548
  }
549 550 551 552 553 554 555 556 557 558 559 560 561 562

  // vclass
  if (vnoder.vclass != NULL) {
    double score_delta = vnoder.vclass->assign_node(vnoder.type);
#ifdef SCORE_DEBUG
    cerr << "  vclass assign " << score_delta << endl;
#endif
    SADD(score_delta*SCORE_VCLASS);
    if (score_delta >= 1) {
      violated++;
      vinfo.vclass++;
    }
  }

Mac Newbold's avatar
Mac Newbold committed
563 564 565 566
#ifdef SCORE_DEBUG
  fprintf(stderr,"  posistion = %d\n",vnoder.posistion);
  fprintf(stderr,"  new score = %.2f  new violated = %d\n",score,violated);
#endif
Christopher Alfeld's avatar
 
Christopher Alfeld committed
567 568 569

  pclass_set(vnoder,pnoder);
  
Mac Newbold's avatar
Mac Newbold committed
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
  return 0;
}

// returns "best" direct link between a and b.
// best = less users
//        break ties with minimum bw_used
edge direct_link(node a,node b)
{
  node dst;
  edge e;
  edge best = NULL;
  tb_plink *pl;
  tb_plink *bestpl = NULL;
  forall_inout_edges(e,a) {
    dst=PG.target(e);
    if (dst == a)
      dst=PG.source(e);
    if (dst == b) {
      pl = &PG[e];
      if (! bestpl ||
590 591 592 593 594
	  ((pl->emulated+pl->nonemulated <
	    bestpl->emulated+bestpl->nonemulated) ||
	   (pl->emulated+pl->nonemulated ==
	    bestpl->emulated+bestpl->nonemulated) &&
	   (pl->bw_used < bestpl->bw_used))) {
Mac Newbold's avatar
Mac Newbold committed
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
	best = e;
	bestpl = pl;
      }
    }
  }
  return best;
}

edge find_link_to_switch(node n)
{
  tb_pnode &nr = PG[n];

  edge e;
  node edst;
  float best_bw=1000.0;
  int best_users = 1000;
  float bw;
  edge best=NULL;
  forall_inout_edges(e,n) {
    edst = PG.target(e);
    if (edst == n)
      edst = PG.source(e);
    if (edst == nr.the_switch) {
      tb_plink &er = PG[e];
      bw = er.bw_used / er.bandwidth;
620 621
      if ((er.emulated+er.nonemulated < best_users) ||
	  ((er.emulated+er.nonemulated == best_users) && (bw < best_bw))) {
Mac Newbold's avatar
Mac Newbold committed
622 623
	best = e;
	best_bw = bw;
624
	best_users = er.emulated+er.nonemulated;
Mac Newbold's avatar
Mac Newbold committed
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
      }
    }
  }
  
  return best;
}

// this looks for the best links between the src and the switch
// and the dst and the switch.
int find_intraswitch_path(node src,node dst,edge *first,edge *second)
{
  tb_pnode &srcr=PG[src];
  tb_pnode &dstr=PG[dst];

  assert(srcr.the_switch == dstr.the_switch);
  assert(srcr.the_switch != NULL);
  assert(src != dst);

  *first = find_link_to_switch(src);
  *second = find_link_to_switch(dst);

  if ((*first != NULL) &&
      (*second != NULL)) return 0;
  return 1;
}

651 652 653 654 655
// this uses the shortest paths calculated over the switch graph to
// find a path between src and dst.  It passes out list<edge>, a list
// of the edges used. (assumed to be empty to begin with).
// Returns 0 if no path exists and 1 otherwise.
int find_interswitch_path(node src,node dst,int bandwidth,list<edge> &L)
Mac Newbold's avatar
Mac Newbold committed
656
{
657 658 659 660 661 662 663
  // We know the shortest path from src to node already.  It's stored
  // in switch_preds[src] and is a node_array<edge>.  Let P be this
  // array.  We can trace our shortest path by starting at the end and
  // following the pred edges back until we reach src.  We need to be
  // careful though because the switch_preds deals with elements of SG
  // and we have elements of PG.
  if ((src == nil) || (dst == nil)) {return 0;}
Mac Newbold's avatar
Mac Newbold committed
664
  
665 666 667 668 669 670 671
  node sg_src = PG[src].sgraph_switch;
  node sg_dst = PG[dst].sgraph_switch;
  edge sg_ed;
  node sg_cur=sg_dst;
  switch_pred_array &preds = switch_preds[sg_src];
  if (preds[sg_dst] == nil) {
    // unreachable
Mac Newbold's avatar
Mac Newbold committed
672 673
    return 0;
  }
674 675 676 677 678 679 680 681 682 683
  while (sg_cur != sg_src) {
    sg_ed = preds[sg_cur];
    L.push(SG[sg_ed].mate);
    if (SG.source(sg_ed) == sg_cur) {
      sg_cur = SG.target(sg_ed);
    } else {
      sg_cur = SG.source(sg_ed);
    }
  }
  return 1;
Mac Newbold's avatar
Mac Newbold committed
684 685 686 687 688 689 690 691 692
}

// this does scoring for over users and over bandwidth on edges.
void score_link(edge e,edge v,bool interswitch)
{
  tb_plink &pl = PG[e];
  tb_vlink &er = G[v];

#ifdef SCORE_DEBUG
693
  cerr << "  score_link(" << e << ") - " << pl.name << " / " << er.name << endl;
Mac Newbold's avatar
Mac Newbold committed
694 695 696
#endif

  if (! interswitch) {
697 698 699 700 701 702 703 704 705
    // need too account for three things here, the possiblity of a new plink
    // the user of a new emulated link, and a possible violation.
    if (er.emulated) {
      pl.emulated++;
      SADD(SCORE_EMULATED_LINK);
    }
    else pl.nonemulated++;
    if (pl.nonemulated+pl.emulated == 1) {
      // new link
706
#ifdef SCORE_DEBUG
707
      fprintf(stderr,"    first user\n");
708
#endif
709
      SADD(SCORE_DIRECT_LINK);
Mac Newbold's avatar
Mac Newbold committed
710
    } else {
711 712 713 714
      // check for violation, basically if this is the first of it's
      // type to be added.
      if (((! er.emulated) && (pl.nonemulated == 1)) ||
	  ((er.emulated) && (pl.emulated == 1))) {
Mac Newbold's avatar
Mac Newbold committed
715
#ifdef SCORE_DEBUG
716
	  fprintf(stderr,"    link user - penalty\n");
Mac Newbold's avatar
Mac Newbold committed
717
#endif
718 719 720
	  SADD(SCORE_DIRECT_LINK_PENALTY);
	  vinfo.link_users++;
	  violated++;
721
      }
Mac Newbold's avatar
Mac Newbold committed
722
    }
723 724
  }
    
Mac Newbold's avatar
Mac Newbold committed
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
  // bandwidth
  int prev_bw = pl.bw_used;
  pl.bw_used += er.bandwidth;
  if ((pl.bw_used > pl.bandwidth) &&
      (prev_bw <= pl.bandwidth)) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"    went over bandwidth (%d > %d)\n",
	    pl.bw_used,pl.bandwidth);
#endif
    violated++;
    vinfo.bandwidth++;
    SADD(SCORE_OVER_BANDWIDTH);
  }
}

void unscore_link(edge e,edge v,bool interswitch)
{
  tb_plink &pl = PG[e];
  tb_vlink &er = G[v];

#ifdef SCORE_DEBUG
  fprintf(stderr,"  unscore_link(%p)\n",e);
#endif

  if (!interswitch) {
750 751 752 753 754 755 756 757
    if (er.emulated) {
      pl.emulated--;
      SSUB(SCORE_EMULATED_LINK);
    } else {
      pl.nonemulated--;
    }
    if (pl.nonemulated+pl.emulated == 0) {
      // link no longer used
Mac Newbold's avatar
Mac Newbold committed
758
#ifdef SCORE_DEBUG
759
      fprintf(stderr,"   freeing link\n");
Mac Newbold's avatar
Mac Newbold committed
760
#endif
761 762 763 764 765 766 767
      SSUB(SCORE_DIRECT_LINK);
    } else {
      // check to see if re freed up a violation, basically did
      // we remove the last of it's link type.
      if ((er.emulated && (pl.emulated == 0)) ||
	  ((! er.emulated) && pl.nonemulated == 0)) {
	// all good
768
#ifdef SCORE_DEBUG
769
	fprintf(stderr,"   users ok\n");
770 771 772 773 774
#endif
	SSUB(SCORE_DIRECT_LINK_PENALTY);
	vinfo.link_users--;
	violated--;
      }
Mac Newbold's avatar
Mac Newbold committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    }
  }
  
  // bandwidth check
  int prev_bw = pl.bw_used;
  pl.bw_used -= er.bandwidth;
  if ((pl.bw_used <= pl.bandwidth) &&
      (prev_bw > pl.bandwidth)) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"   went under bandwidth (%d <= %d)\n",
	    pl.bw_used,pl.bandwidth);
#endif
    violated--;
    vinfo.bandwidth--;
    SSUB(SCORE_OVER_BANDWIDTH);
  }
Christopher Alfeld's avatar
 
Christopher Alfeld committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839

  er.type = tb_vlink::LINK_UNKNOWN;
}

double fd_score(tb_vnode &vnoder,tb_pnode &pnoder,int *fd_violated)
{
  double fd_score=0;
  (*fd_violated)=0;
  
  seq_item desire;
  seq_item feature;
  double value;
  for (desire = vnoder.desires.min_item();desire;
       desire = vnoder.desires.succ(desire)) {
    feature = pnoder.features.lookup(vnoder.desires.key(desire));
#ifdef SCORE_DEBUG
    cerr << "  desire = " << vnoder.desires.key(desire) \
	 << " " << vnoder.desires.inf(desire) << "\n";
#endif
    if (!feature) {
      // Unmatched desire.  Add cost.
#ifdef SCORE_DEBUG
      cerr << "    unmatched\n";
#endif
      value = vnoder.desires.inf(desire);
      fd_score += SCORE_DESIRE*value;
      if (value >= 1) {
	(*fd_violated)++;
      }
    }
  }
  for (feature = pnoder.features.min_item();feature;
       feature = pnoder.features.succ(feature)) {
    desire = vnoder.desires.lookup(pnoder.features.key(feature));
#ifdef SCORE_DEBUG
    cerr << "  feature = " << pnoder.features.key(feature) \
	 << " " << pnoder.features.inf(feature) << "\n";
#endif
    if (! desire) {
      // Unused feature.  Add weight
#ifdef SCORE_DEBUG
      cerr << "    unused\n";
#endif
      value = pnoder.features.inf(feature);
      fd_score+=SCORE_FEATURE*value;
    }
  }

  return fd_score;
Mac Newbold's avatar
Mac Newbold committed
840
}