solution.cc 14.2 KB
Newer Older
1
/*
Robert Ricci's avatar
Robert Ricci committed
2
 * Copyright (c) 2003-2010 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{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/>.
 * 
 * }}}
22 23
 */

24
static const char rcsid[] = "$Id: solution.cc,v 1.15 2009-10-21 20:49:26 tarunp Exp $";
25

26
#include "solution.h"
27
#include "vclass.h"
28
				 
29
#include <string>
30 31 32 33
#include <list>
#include <utility>

#ifdef WITH_XML
34 35 36 37
#include "annotate_rspec.h"
#include "annotate_rspec_v2.h"
#include "annotate_vtop.h"
#include "xstr.h"
38 39 40 41 42 43 44 45
#endif

extern bool ptop_xml_input;
extern bool ptop_rspec_input;

extern bool vtop_xml_input;
extern bool vtop_rspec_input;

46 47
extern int rspec_version;

48 49 50 51 52 53 54 55
bool both_inputs_rspec = false;
bool both_inputs_xml = false;

#ifdef WITH_XML
	annotate_rspec *rspec_annotater;
	annotate_vtop *vtop_annotater;
#endif

56
using namespace std;
57

58 59 60 61 62 63 64 65
bool compare_scores(double score1, double score2) {
    if ((score1 < (score2 + ITTY_BITTY)) && (score1 > (score2 - ITTY_BITTY))) {
	return 1;
    } else {
	return 0;
    }
}

66 67 68
/*
 * Print out the current solution
 */
69
void print_solution(const solution &s) {
70 71 72
    vvertex_iterator vit,veit;
    tb_vnode *vn;

73
#ifdef WITH_XML
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    bool is_generated = false;
    both_inputs_xml = ptop_xml_input && vtop_xml_input;
    both_inputs_rspec = ptop_rspec_input && vtop_rspec_input;
    
    if (both_inputs_rspec == true) {
      switch (rspec_version) {
      case 2:
	rspec_annotater = new annotate_rspec_v2();
	break;
      default:
	rspec_annotater = new annotate_rspec ();
	break;
      }
    }
    else if (both_inputs_xml == true)
      vtop_annotater = new annotate_vtop();
90
#endif	
91 92 93 94 95 96
    /*
     * Start by printing out all node mappings
     */
    cout << "Nodes:" << endl;
    tie(vit,veit) = vertices(VG);
    for (;vit != veit;++vit) {
97 98
      vn = get(vvertex_pmap,*vit);
      if (! s.is_assigned(*vit)) {
99
        cout << "unassigned: " << vn->name << endl;
100 101
      } else {
#ifdef WITH_XML
102 103
	string node_name = XStr(vn -> name).c();
	string assigned_to =XStr(get(pvertex_pmap,s.get_assignment(*vit))->name).c();
104
	if (both_inputs_rspec) {
105 106 107
	  rspec_annotater->annotate_element(node_name.c_str(), assigned_to.c_str());
	  if (rspec_annotater->is_generated_element("node", "virtual_id",
                                              node_name.c_str())) {
108
	    continue;
109
    }
110
	}
111
	else if (both_inputs_xml) {
112
	  vtop_annotater->annotate_element(node_name.c_str(), assigned_to.c_str());
113 114 115 116 117 118
	}
#endif
	cout << vn->name << " " 
	     << get(pvertex_pmap,s.get_assignment(*vit))->name << endl;
      }
    }
119
    cout << "End Nodes" << endl;
120
    
121 122 123 124 125 126 127
    /*
     * Next, edges
     */
    cout << "Edges:" << endl;
    vedge_iterator eit,eendit;
    tie(eit,eendit) = edges(VG);
    for (;eit!=eendit;++eit) {
128
      tb_vlink *vlink = get(vedge_pmap,*eit);
129

130
#ifdef WITH_XML
131
	if (both_inputs_rspec) {
132 133
	  is_generated = rspec_annotater->is_generated_element 
	    ("link", "virtual_id", (vlink->name).c_str());
134 135
	  if (!is_generated)
	    cout << vlink->name;
136 137
	}
	else
138
	  cout << vlink->name;
139
#else
140
	cout << vlink->name;
141 142
#endif
		
143
	list<const char*> links;
144

145
	if (vlink->link_info.type_used == tb_link_info::LINK_DIRECT) {
146 147
	    // Direct link - just need the source and destination
	    tb_plink *p = get(pedge_pmap,vlink->link_info.plinks.front());
148
	    tb_plink *p2 = get(pedge_pmap,vlink->link_info.plinks.back());
149 150 151
	    // XXX: This is not correct because it contradicts the comment earlier
	    // It seems that it will work because the front and back of the list will have the same node
	    // But it needs to be checked anyway.
152
#ifdef WITH_XML
153
	    if (both_inputs_rspec) {
154
	      rspec_annotater->annotate_element((vlink->name).c_str(), 
155
                                          (p->name).c_str());
156 157 158 159
	      if (is_generated)
		continue;
	    }
	    else if (both_inputs_xml == true) {
160
	      //	      annotate_vtop((vlink->name).c_str(), (p->name).c_str());
161
	    }
162
#endif
163 164 165
	    cout << " direct " << p->name << " (" <<
		p->srcmac << "," << p->dstmac << ") " <<
		p2->name << " (" << p2->srcmac << "," << p2->dstmac << ")";
166 167
	} else if (vlink->link_info.type_used ==
		tb_link_info::LINK_INTRASWITCH) {
168 169 170
	    // Intraswitch link - need to grab the plinks to both nodes
	    tb_plink *p = get(pedge_pmap,vlink->link_info.plinks.front());
	    tb_plink *p2 = get(pedge_pmap,vlink->link_info.plinks.back());
171
#ifdef WITH_XML
172 173 174
	    links.push_back((p->name).c_str());
	    links.push_back((p2->name).c_str());
	    if (both_inputs_rspec) {
175
	      rspec_annotater->annotate_element((vlink->name).c_str(), &links);
176 177 178 179 180 181
	      if (is_generated)
		continue;
	    }
	    else if (both_inputs_xml) {
	      vtop_annotater->annotate_element((vlink->name).c_str(), &links);
	    }
182
#endif
183
	    cout << " intraswitch " << p->name << " (" <<
184 185
	      p->srcmac << "," << p->dstmac << ") " <<
	      p2->name << " (" << p2->srcmac << "," << p2->dstmac << ")";
186
	} else if (vlink->link_info.type_used ==
187 188 189 190 191 192 193
		   tb_link_info::LINK_INTERSWITCH) {
	  // Interswitch link - iterate through each intermediate link
	  cout << " interswitch ";
	  for (pedge_path::iterator it=vlink->link_info.plinks.begin(); 
	       it != vlink->link_info.plinks.end();
	       ++it) {
	    tb_plink *p = get(pedge_pmap,*it);
194
#ifdef WITH_XML
195 196 197 198
	    links.push_back((p->name).c_str());
	    if (!is_generated)
	      cout << " " << p->name 
		   << " (" << p->srcmac << "," << p->dstmac << ")";
199
#else
200 201
	    cout << " " << p->name 
		 << " (" << p->srcmac << "," << p->dstmac << ")";
202
#endif
203
	  }
204
#ifdef WITH_XML
205
	    if (both_inputs_rspec) {
206
	      rspec_annotater->annotate_element((vlink->name).c_str(), &links);
207 208 209 210
	    }
	    else if (both_inputs_xml) {
	      vtop_annotater->annotate_element((vlink->name).c_str(), &links);
	    }
211
#endif
212
	} else if (vlink->link_info.type_used == tb_link_info::LINK_TRIVIAL) {
213 214 215 216 217 218 219
	  // Trivial link - we really don't have useful information to
	  // print, but we'll fake a bunch of output here just to make it
	  // consistent with other (ie. intraswitch) output
	  vvertex vv = vlink->src;
	  tb_vnode *vnode = get(vvertex_pmap,vv);
	  pvertex pv = vnode->assignment;
	  tb_pnode *pnode = get(pvertex_pmap,pv);
220
#ifdef WITH_XML
221
	  if (both_inputs_rspec) {
222
	    rspec_annotater->annotate_element((vlink->name).c_str());
223 224 225
	    if (is_generated)
	      continue;
	  }
226
#endif
227 228 229 230 231
	  cout << " trivial " <<  pnode->name << ":loopback" <<
	    " (" << pnode->name << "/null,(null)) " <<
	    pnode->name << ":loopback" << " (" << pnode->name <<
	    "/null,(null)) ";
	  // TODO: Annotate trivial links in the rspec
232
	} else {
233 234
	  // No mapping at all
	  cout << " Mapping Failed";
235 236 237 238 239
	}
	cout << endl;
    }
    cout << "End Edges" << endl;
    cout << "End solution" << endl;
240
#ifdef WITH_XML
241 242 243
    if (both_inputs_rspec == true) {
      rspec_annotater->cleanup();
    }
244
#endif
245 246
}

247 248 249
/* Print out the current solution and annotate the rspec */
void print_solution (const solution &s, const char* output_filename)
{
250
  print_solution(s);
251
#ifdef WITH_XML
252 253 254 255
  // This will work because print_solution is called already
  // and the objects have been created there
  if (both_inputs_rspec == true) {
      cout << "Writing annotated rspec to " << output_filename << endl;
256
      rspec_annotater->write_annotated_file (output_filename);
257 258 259 260 261
  }
  else if (both_inputs_xml == true) {
      cout << "Writing annotated xml to " << output_filename << endl;
      vtop_annotater->write_annotated_file (output_filename);
  }
262 263 264
#endif
}

265 266 267 268 269
/*
 * Print out a summary of the solution - mainly, we print out information from
 * the physical perspective. For example, now many vnodes are assigned to each
 * pnode, and how much total bandwidth each pnode is handling.
 */
270
void print_solution_summary(const solution &s)
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
{
  // First, print the number of vnodes on each pnode, and the total number of
  // pnodes used
  cout << "Summary:" << endl;

  // Go through all physical nodes
  int pnodes_used = 0;
  pvertex_iterator pit, pendit;
  tie(pit, pendit) = vertices(PG);
  for (;pit != pendit; pit++) {
    tb_pnode *pnode = get(pvertex_pmap,*pit);

    // We're going to treat switches specially
    bool isswitch = false;
    if (pnode->is_switch) {
      isswitch = true;
    }

    // Only print pnodes we are using, or switches we are going through
    if ((pnode->total_load > 0) ||
	(isswitch && (pnode->switch_used_links > 0))) {

      // What we really want to know is how many PCs, etc. were used, so don't
      // count switches
      if (!pnode->is_switch) {
	pnodes_used++;
      }

      // Print name, number of vnodes, and some bandwidth numbers
      cout << pnode->name << " " << pnode->total_load << " vnodes, " <<
	pnode->nontrivial_bw_used << " nontrivial BW, " <<
302 303
	pnode->trivial_bw_used << " trivial BW, type=" << pnode->current_type
	<< endl;
304 305 306 307 308 309 310 311 312 313 314 315 316

      // Go through all links on this pnode
      poedge_iterator pedge_it,end_pedge_it;
      tie(pedge_it,end_pedge_it) = out_edges(*pit,PG);
      for (;pedge_it!=end_pedge_it;++pedge_it) {
	tb_plink *plink = get(pedge_pmap,*pedge_it);

	tb_pnode *link_dst = get(pvertex_pmap,source(*pedge_it,PG));
	if (link_dst == pnode) {
	  link_dst = get(pvertex_pmap,target(*pedge_it,PG));
	}

	// Ignore links we aren't using
317
	if ((plink->emulated == 0) && (plink->nonemulated == 0)) {
318 319 320 321 322 323 324 325 326
	  continue;
	}

	// For switches, only print inter-switch links
	if (isswitch && (!link_dst->is_switch)) {
	  continue;
	}
	cout << "    " << plink->bw_used << " " << plink->name << endl;
      }
327 328

      // Print out used local additive features
329
      node_feature_set::iterator feature_it;
330 331
      for (feature_it = pnode->features.begin();
	  feature_it != pnode->features.end();++feature_it) {
332 333 334 335 336
	if (feature_it->is_local() && feature_it->is_l_additive()) {
	    double total = feature_it->cost();
	    double used = feature_it->used();
	  cout << "    " << feature_it->name() << ": used=" << used <<
	      " total=" << total << endl;
337 338
	}
      }
339 340 341 342 343 344 345
    }
  }
  cout << "Total physical nodes used: " << pnodes_used << endl;

  cout << "End summary" << endl;
}

346 347 348
void pvertex_writer::operator()(ostream &out,const pvertex &p) const {
    tb_pnode *pnode = get(pvertex_pmap,p);
    out << "[label=\"" << pnode->name << "\"";
349
    fstring style;
350 351 352 353 354 355 356 357 358 359 360 361 362 363
    if (pnode->types.find("switch") != pnode->types.end()) {
	out << " style=dashed";
    } else if (pnode->types.find("lan") != pnode->types.end()) {
	out << " style=invis";
    }
    out << "]";
}

void vvertex_writer::operator()(ostream &out,const vvertex &v) const {
    tb_vnode *vnode = get(vvertex_pmap,v);
    out << "[label=\"" << vnode->name << " ";
    if (vnode->vclass == NULL) {
	out << vnode->type;
    } else {
364
	out << vnode->vclass->get_name();
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
    }
    out << "\"";
    if (vnode->fixed) {
	out << " style=dashed";
    }
    out << "]";
}

void pedge_writer::operator()(ostream &out,const pedge &p) const {
    out << "[";
    tb_plink *plink = get(pedge_pmap,p);
#ifdef VIZ_LINK_LABELS
    out << "label=\"" << plink->name << " ";
    out << plink->delay_info.bandwidth << "/" <<
	plink->delay_info.delay << "/" << plink->delay_info.loss << "\"";
#endif
381
    if (plink->is_type == tb_plink::PLINK_INTERSWITCH) {
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
	out << " style=dashed";
    }
    tb_pnode *src = get(pvertex_pmap,source(p,PG));
    tb_pnode *dst = get(pvertex_pmap,target(p,PG));
    if ((src->types.find("lan") != src->types.end()) ||
	    (dst->types.find("lan") != dst->types.end())) {
	out << " style=invis";
    }
    out << "]";
}

void sedge_writer::operator()(ostream &out,const sedge &s) const {
    tb_slink *slink = get(sedge_pmap,s);
    pedge_writer pwriter;
    pwriter(out,slink->mate);
}

void svertex_writer::operator()(ostream &out,const svertex &s) const {
    tb_switch *snode = get(svertex_pmap,s);
    pvertex_writer pwriter;
    pwriter(out,snode->mate);
}

void vedge_writer::operator()(ostream &out,const vedge &v) const {
    tb_vlink *vlink = get(vedge_pmap,v);
    out << "[";
#ifdef VIZ_LINK_LABELS
    out << "label=\"" << vlink->name << " ";
    out << vlink->delay_info.bandwidth << "/" <<
	vlink->delay_info.delay << "/" << vlink->delay_info.loss << "\"";
#endif
    if (vlink->emulated) {
	out << "style=dashed";
    }
    out <<"]";
}

void graph_writer::operator()(ostream &out) const {
    out << "graph [size=\"1000,1000\" overlap=\"false\" sep=0.1]" << endl;
}

void solution_edge_writer::operator()(ostream &out,const vedge &v) const {
    tb_link_info &linfo = get(vedge_pmap,v)->link_info;
    out << "[";
426 427 428
    string style;
    string color;
    string label;
429 430
    switch (linfo.type_used) {
	case tb_link_info::LINK_UNMAPPED: style="dotted";color="red"; break;
431 432 433
	case tb_link_info::LINK_DIRECT: style="dashed";color="black"; break;
	case tb_link_info::LINK_INTRASWITCH:
	    style="solid";color="black";
434
	    label=get(pvertex_pmap,linfo.switches.front())->name.c_str();
435 436 437 438 439 440
	    break;
	case tb_link_info::LINK_INTERSWITCH:
	    style="solid";color="blue";
	    label="";
	    for (pvertex_list::const_iterator it=linfo.switches.begin();
		    it!=linfo.switches.end();++it) {
441
		label += get(pvertex_pmap,*it)->name.c_str();
442 443 444 445
		label += " ";
	    }
	    break;
	case tb_link_info::LINK_TRIVIAL: style="dashed";color="blue"; break;
446
	case tb_link_info::LINK_DELAYED: style="dotted";color="green"; break;
447 448 449 450 451 452 453 454 455 456
    }
    out << "style=" << style << " color=" << color;
    if (label.size() != 0) {
	out << " label=\"" << label << "\"";
    }
    out << "]";
}

void solution_vertex_writer::operator()(ostream &out,const vvertex &v) const {
    tb_vnode *vnode = get(vvertex_pmap,v);
457 458 459
    string label = vnode->name.c_str();
    string color;
    if (my_solution.is_assigned(v)) {
460
	label += " ";
461
	label += get(pvertex_pmap,my_solution.get_assignment(v))->name.c_str();
462 463 464 465
	color = "black";
    } else {
	color = "red";
    }
466
    string style;
467 468 469 470 471 472 473 474
    if (vnode->fixed) {
	style="dashed";
    } else {
	style="solid";
    }
    out << "[label=\"" << label << "\" color=" << color <<
	" style=" << style << "]";
}