solution.cc 9.33 KB
Newer Older
1 2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2003-2006 University of Utah and the Flux Group.
4 5 6 7
 * All rights reserved.
 */

#include "solution.h"
8
#include "vclass.h"
9 10
#include <string>
using namespace std;
11

12 13 14 15 16 17 18 19
bool compare_scores(double score1, double score2) {
    if ((score1 < (score2 + ITTY_BITTY)) && (score1 > (score2 - ITTY_BITTY))) {
	return 1;
    } else {
	return 0;
    }
}

20 21 22
/*
 * Print out the current solution
 */
23
void print_solution(const solution &s) {
24 25 26 27 28 29 30 31 32 33
    vvertex_iterator vit,veit;
    tb_vnode *vn;

    /*
     * Start by printing out all node mappings
     */
    cout << "Nodes:" << endl;
    tie(vit,veit) = vertices(VG);
    for (;vit != veit;++vit) {
	vn = get(vvertex_pmap,*vit);
34
	if (! s.is_assigned(*vit)) {
35 36 37
	    cout << "unassigned: " << vn->name << endl;
	} else {
	    cout << vn->name << " "
38
		<< get(pvertex_pmap,s.get_assignment(*vit))->name << endl;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	}
    }
    cout << "End Nodes" << endl;

    /*
     * Next, edges
     */
    cout << "Edges:" << endl;
    vedge_iterator eit,eendit;
    tie(eit,eendit) = edges(VG);
    for (;eit!=eendit;++eit) {
	tb_vlink *vlink = get(vedge_pmap,*eit);

	cout << vlink->name;

54
	if (vlink->link_info.type_used == tb_link_info::LINK_DIRECT) {
55 56
	    // Direct link - just need the source and destination
	    tb_plink *p = get(pedge_pmap,vlink->link_info.plinks.front());
57
	    tb_plink *p2 = get(pedge_pmap,vlink->link_info.plinks.back());
58
	    cout << " direct " << p->name << " (" <<
59 60
		p->srcmac << "," << p->dstmac << ") " <<
		p2->name << " (" << p2->srcmac << "," << p2->dstmac << ")";
61 62
	} else if (vlink->link_info.type_used ==
		tb_link_info::LINK_INTRASWITCH) {
63 64 65 66 67 68
	    // 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());
	    cout << " intraswitch " << p->name << " (" <<
		p->srcmac << "," << p->dstmac << ") " <<
		p2->name << " (" << p2->srcmac << "," << p2->dstmac << ")";
69 70
	} else if (vlink->link_info.type_used ==
		tb_link_info::LINK_INTERSWITCH) {
71
	    // Interswitch link - iterate through each intermediate link
72 73 74 75 76 77 78
	    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);
		cout << " " << p->name << " (" << p->srcmac << "," <<
		    p->dstmac << ")";
	    }
79
	} else if (vlink->link_info.type_used == tb_link_info::LINK_TRIVIAL) {
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	    // 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);
	    cout << " trivial " <<  pnode->name << ":loopback" <<
		" (" << pnode->name << "/null,(null)) " <<
		pnode->name << ":loopback" << " (" << pnode->name <<
		"/null,(null)) ";
	} else {
	    // No mapping at all
	    cout << " Mapping Failed";
	}

	cout << endl;
    }

    cout << "End Edges" << endl;

    cout << "End solution" << endl;
}

104 105 106 107 108
/*
 * 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.
 */
109
void print_solution_summary(const solution &s)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
{
  // 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, " <<
141 142
	pnode->trivial_bw_used << " trivial BW, type=" << pnode->current_type
	<< endl;
143 144 145 146 147 148 149 150 151 152 153 154 155

      // 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
156
	if ((plink->emulated == 0) && (plink->nonemulated == 0)) {
157 158 159 160 161 162 163 164 165
	  continue;
	}

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

      // Print out used local additive features
168
      node_feature_set::iterator feature_it;
169 170
      for (feature_it = pnode->features.begin();
	  feature_it != pnode->features.end();++feature_it) {
171 172 173 174 175
	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;
176 177
	}
      }
178 179 180 181 182 183 184
    }
  }
  cout << "Total physical nodes used: " << pnodes_used << endl;

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

185 186 187
void pvertex_writer::operator()(ostream &out,const pvertex &p) const {
    tb_pnode *pnode = get(pvertex_pmap,p);
    out << "[label=\"" << pnode->name << "\"";
188
    fstring style;
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    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 {
	out << vnode->vclass->name;
    }
    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
220
    if (plink->is_type == tb_plink::PLINK_INTERSWITCH) {
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
	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 << "[";
265 266 267
    string style;
    string color;
    string label;
268 269
    switch (linfo.type_used) {
	case tb_link_info::LINK_UNMAPPED: style="dotted";color="red"; break;
270 271 272
	case tb_link_info::LINK_DIRECT: style="dashed";color="black"; break;
	case tb_link_info::LINK_INTRASWITCH:
	    style="solid";color="black";
273
	    label=get(pvertex_pmap,linfo.switches.front())->name.c_str();
274 275 276 277 278 279
	    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) {
280
		label += get(pvertex_pmap,*it)->name.c_str();
281 282 283 284 285 286 287 288 289 290 291 292 293 294
		label += " ";
	    }
	    break;
	case tb_link_info::LINK_TRIVIAL: style="dashed";color="blue"; break;
    }
    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);
295 296 297
    string label = vnode->name.c_str();
    string color;
    if (my_solution.is_assigned(v)) {
298
	label += " ";
299
	label += get(pvertex_pmap,my_solution.get_assignment(v))->name.c_str();
300 301 302 303
	color = "black";
    } else {
	color = "red";
    }
304
    string style;
305 306 307 308 309 310 311 312
    if (vnode->fixed) {
	style="dashed";
    } else {
	style="solid";
    }
    out << "[label=\"" << label << "\" color=" << color <<
	" style=" << style << "]";
}