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.

pclass.cc 6.63 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.
 */

Christopher Alfeld's avatar
 
Christopher Alfeld committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

#include <LEDA/graph_alg.h>
#include <LEDA/graphwin.h>
#include <LEDA/ugraph.h>
#include <LEDA/dictionary.h>
#include <LEDA/map.h>
#include <LEDA/graph_iterator.h>
#include <LEDA/node_pq.h>
#include <LEDA/sortseq.h>
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <assert.h>

#include "common.h"
#include "physical.h"
27
#include "vclass.h"
Christopher Alfeld's avatar
 
Christopher Alfeld committed
28 29 30 31 32 33
#include "virtual.h"
#include "pclass.h"

extern node pnodes[MAX_PNODES];
extern dictionary<tb_pnode*,node> pnode2node;
extern pclass_list pclasses;
34
extern pclass_types type_table;
Christopher Alfeld's avatar
 
Christopher Alfeld committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 107 108 109 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

typedef two_tuple<node,int> link_info; // dst, bw

// returns 1 if a and b are equivalent.  They are equivalent if the
// type and features information match and if there is a one-to-one
// mapping between links that preserves bw, and destination.
int pclass_equiv(tb_pgraph &PG, tb_pnode *a,tb_pnode *b)
{
  // check type information
  dic_item it;
  forall_items(it,a->types) {
    dic_item bit;
    bit = b->types.lookup(a->types.key(it));
    if (bit == nil)
      return 0;
    if (b->types[bit] != a->types.inf(it))
      return 0;
  }

  // check features
  seq_item sit;
  forall_items(sit,a->features) {
    seq_item bit;
    bit = b->features.lookup(a->features.key(sit));
    if (bit == nil)
      return 0;
    if (b->features[bit] != a->features.inf(sit))
      return 0;
  }

  // check links - to do this we first create sets of every link in b.
  // we then loop through every link in a, find a match in the set, and
  // remove it from the set.
  node an = pnode2node.access(a);
  node bn = pnode2node.access(b);

  // yet another place where we'd like to use sets <sigh>
  list<link_info> b_links;
  
  edge e;
  forall_inout_edges(e,bn) {
    node dst=PG.target(e);
    if (dst == bn)
      dst = PG.source(e);
    b_links.push_front(link_info(dst,PG[e].bandwidth));
  }
  forall_inout_edges(e,an) {
    link_info link;
    node dst=PG.target(e);
    if (dst == an)
      dst = PG.source(e);
    int bw = PG[e].bandwidth;
    bool found = 0;
    list_item lit;
    forall_items(lit,b_links) {
      link=b_links.inf(lit);
      if ((dst == link.first()) && (bw == link.second())) {
	found = 1;
	b_links.del(lit);
	break;
      }
    }
    if (found == 0) return 0;
  }
  return 1;
}

/* This function takes a physical graph and generates an set of
   equivalence classes of physical nodes.  The globals pclasses (a
   list of all equivalence classes) and type_table (and table of
   physical type to list of classes that can satisfy that type) are
   set by this routine. */
int generate_pclasses(tb_pgraph &PG) {
  node cur;
  dictionary<tb_pclass*,tb_pnode*> canonical_members;
  
  forall_nodes(cur, PG) {
    tb_pclass *curclass;
    bool found_class = 0;
    tb_pnode *curP = &PG[cur];
    dic_item dit;
    forall_items(dit,canonical_members) {
      curclass=canonical_members.key(dit);
      if (pclass_equiv(PG,curP,canonical_members[dit])) {
	// found the right class
	found_class=1;
	curclass->add_member(curP);
	break;
      }
    }
    if (found_class == 0) {
      // new class
      tb_pclass *n = new tb_pclass;
      pclasses.push_back(n);
      canonical_members.insert(n,curP);
      n->name = curP->name;
      n->add_member(curP);
    }
  }

135 136
  dictionary<string,pclass_list*> pre_type_table;
  
Christopher Alfeld's avatar
 
Christopher Alfeld committed
137 138 139 140 141
  list_item it;
  forall_items(it,pclasses) {
    tb_pclass *cur = pclasses[it];
    dic_item dit;
    forall_items(dit,cur->members) {
142 143
      if (pre_type_table.lookup(cur->members.key(dit)) == nil) {
	pre_type_table.insert(cur->members.key(dit),new pclass_list);
Christopher Alfeld's avatar
 
Christopher Alfeld committed
144
      }
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
      pre_type_table.access(cur->members.key(dit))->push_back(cur);
    }
  }

  // now we convert the lists in pre_type_table into arrays for
  // faster access.
  dic_item dit;
  forall_items(dit,pre_type_table) {
    pclass_list *L = pre_type_table.inf(dit);
    pclass_array *A = new pclass_array(L->length());
    int i=0;
    
    type_table.insert(pre_type_table.key(dit),tt_entry(L->length(),A));
    
    forall_items(it,*L) {
      (*A)[i++] = L->inf(it);
Christopher Alfeld's avatar
 
Christopher Alfeld committed
161
    }
162
    delete L;
Christopher Alfeld's avatar
 
Christopher Alfeld committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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 220 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
  }
  return 0;
}

int tb_pclass::add_member(tb_pnode *p)
{
  dic_item it;
  forall_items(it,p->types) {
    string type = p->types.key(it);
    if (members.lookup(type) == nil) {
      members.insert(type,new tb_pnodelist);
    }
    members.access(type)->push_back(p);
  }
  size++;
  p->my_class=this;
  return 0;
}

// should be called after add_node
int pclass_set(tb_vnode &v,tb_pnode &p)
{
  tb_pclass *c = p.my_class;
  
  // remove p node from correct lists in equivalence class.
  dic_item dit;
  forall_items(dit,c->members) {
    if (c->members.key(dit) == p.current_type) {
      // same class - only remove if node is full
      if (p.current_load == p.max_load) {
	(c->members.inf(dit))->remove(&p);
      }
    } else {
      // If it's not in the list then this fails quietly.
      (c->members.inf(dit))->remove(&p);
    }
  }
  
  c->used += 1.0/(p.max_load);
  
  return 0;
}

int pclass_unset(tb_pnode &p)
{
  // add pnode to all lists in equivalence class.
  tb_pclass *c = p.my_class;
  dic_item dit;

  forall_items(dit,c->members) {
    if (c->members.key(dit) == p.current_type) {
      // If it's not in the list then we need to add it to the back if it's
      // empty and the front if it's not.  Since unset is called before
      // remove_node empty means only one user.
      if (! (c->members.inf(dit))->exists(&p)) {
	assert(p.current_load > 0);
	if (p.current_load == 1) {
	  (c->members.inf(dit))->push_back(&p);
	} else {
	  (c->members.inf(dit))->push_front(&p);
	}
      }
    } else {
      (c->members.inf(dit))->push_back(&p);
    }
  }

  c->used -= 1.0/(p.max_load);
  
  return 0;
}

void pclass_debug()
{
  cout << "PClasses:\n";
  list_item it;
  forall_items(it,pclasses) {
    tb_pclass *p = pclasses[it];
    cout << p->name << ": size = " << p->size << "\n";
    dic_item dit;
    forall_items(dit,p->members) {
      cout << "  " << p->members.key(dit) << ":\n";
      list_item lit;
      const list<tb_pnode*> L = p->members.inf(dit)->L;
      forall_items(lit,L) {
	cout << "    " << L.inf(lit)->name << "\n";
      }
    }
    cout << "\n";
  }

  cout << "\n";
  cout << "Type Table:\n";
  dic_item dit;
  forall_items(dit,type_table) {
    cout << type_table.key(dit) << ":";
259 260 261
    int n = type_table.inf(dit).first();
    pclass_array &A = *(type_table.inf(dit).second());
    for (int i = 0; i < n ; ++i) {
262
      cout << " " << A[i]->name;
Christopher Alfeld's avatar
 
Christopher Alfeld committed
263 264 265 266 267 268 269 270 271 272 273
    }
    cout << "\n";
  }
}

int compare(tb_pclass *const &a, tb_pclass *const &b)
{
  if (a==b) return 0;
  if (a < b) return -1;
  return 1;
}