pclass.h 3.36 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1 2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2000-2006 University of Utah and the Flux Group.
Robert Ricci's avatar
Robert Ricci committed
4 5 6
 * All rights reserved.
 */

Christopher Alfeld's avatar
 
Christopher Alfeld committed
7 8 9
#ifndef __PCLASS_H
#define __PCLASS_H

10 11
#include<map>

12 13 14
// Declared in assign.cc - indicated whether or not we should use pclasses
extern bool use_pclasses;

Christopher Alfeld's avatar
 
Christopher Alfeld committed
15 16 17 18
// tb pnode list is a data structure that acts like list but has
// O(1) removal.  It is a list of tb_pnode*.
class tb_pnodelist {
public:
19 20 21 22 23
  typedef list<tb_pnode*> pnode_list;
  typedef pnode_list::iterator list_iter;
  pnode_list L;
  typedef hash_map<tb_pnode*,list_iter,hashptr<tb_pnode*> > pnode_iter_map;
  pnode_iter_map D;
Christopher Alfeld's avatar
 
Christopher Alfeld committed
24

25 26 27 28
  list_iter push_front(tb_pnode *p) {
    L.push_front(p);
    list_iter it = L.begin();
    D[p]=it;
Christopher Alfeld's avatar
 
Christopher Alfeld committed
29 30
    return it;
  };
31 32 33 34 35
  list_iter push_back(tb_pnode *p) {
    L.push_back(p);
    list_iter it = L.end();
    it--;
    D[p]=it;
Christopher Alfeld's avatar
 
Christopher Alfeld committed
36 37 38 39
    return it;
  };
  int remove(tb_pnode *p) {
    if (exists(p)) {
40 41 42
      pnode_iter_map::iterator dit = D.find(p);
      L.erase((*dit).second);
      D.erase(dit);
Christopher Alfeld's avatar
 
Christopher Alfeld committed
43 44 45 46 47 48
      return 0;
    } else {
      return 1;
    }
  };
  int exists(tb_pnode *p) {
49
    return (D.find(p) != D.end());
Christopher Alfeld's avatar
 
Christopher Alfeld committed
50 51
  }
  tb_pnode *front() {
52
    return (L.empty() ? NULL : L.front());
Christopher Alfeld's avatar
 
Christopher Alfeld committed
53
  };
54

55 56 57 58
  int size() {
      return L.size();
  };

59 60 61 62 63 64 65 66
  friend ostream &operator<<(ostream &o, const tb_pnodelist& l)
  {
    pnode_list::const_iterator lit;
    for (lit=l.L.begin();lit!=l.L.end();++lit) {
      o << "    " << (*lit)->name << endl;
    }
    return o;
  }
Christopher Alfeld's avatar
 
Christopher Alfeld committed
67 68 69 70
};

class tb_pclass {
public:
71 72
  tb_pclass() : name(), size(0), used_members(0), refcount(0), disabled(false)
      {;}
73

74
  typedef map<fstring,tb_pnodelist*> pclass_members_map;
75
  typedef hash_set<tb_pnode*,hashptr<tb_pnode*> > tb_pnodeset;
76
  typedef hash_map<fstring,tb_pnodeset*> pclass_members_set;
77

78
  int add_member(tb_pnode *p, bool is_own_class);
Christopher Alfeld's avatar
 
Christopher Alfeld committed
79

80
  fstring name;			// purely for debugging
Christopher Alfeld's avatar
 
Christopher Alfeld committed
81
  int size;
82
  int used_members;
83 84 85 86 87
  pclass_members_map members;
#ifdef SMART_UNMAP
  pclass_members_set used_members;
#endif

88 89
  bool disabled;

Robert Ricci's avatar
Robert Ricci committed
90 91 92 93
  // A count of how many nodes can use this pclass
  // For use with PRUNE_PCLASSES
  int refcount;

Robert Ricci's avatar
Robert Ricci committed
94 95 96
  // Just used for debugging
  bool is_own_class;

97 98 99
  friend ostream &operator<<(ostream &o, const tb_pclass& p)
  {
    o << p.name << "(" << &p << ") size=" << p.size <<
100
      " used_members=" << p.used_members << " disabled=" << p.disabled << "\n";
101 102 103 104 105 106 107 108
    pclass_members_map::const_iterator dit;
    for (dit=p.members.begin();dit!=p.members.end();++dit) {
      o << "  " << (*dit).first << ":\n";
      o << *((*dit).second) << endl;
    }
    o << endl;
    return o;
  }
Christopher Alfeld's avatar
 
Christopher Alfeld committed
109 110 111
};

typedef list<tb_pclass*> pclass_list;
112 113
typedef vector<tb_pclass*> pclass_vector;
typedef pair<int,pclass_vector*> tt_entry;
114
typedef hash_map<fstring,tt_entry> pclass_types;
Christopher Alfeld's avatar
 
Christopher Alfeld committed
115 116 117 118 119 120 121 122

/* Constants */
#define PCLASS_BASE_WEIGHT 1
#define PCLASS_NEIGHBOR_WEIGHT 1
#define PCLASS_UTIL_WEIGHT 1
#define PCLASS_FDS_WEIGHT 2

/* routines defined in pclass.cc */
123 124 125 126
// Sets pclasses and type_table globals -
// Takes two arguments - a physical graph, and a flag indicating whether or not
// each physical node should get its own pclass (effectively disabling
// pclasses)
127 128
int generate_pclasses(tb_pgraph &PG, bool pclass_for_each_pnode,
	bool dynamic_pclasses);
Christopher Alfeld's avatar
 
Christopher Alfeld committed
129 130 131

/* The following two routines sets and remove mappings in pclass
   datastructures */
132 133
int pclass_set(tb_vnode *v,tb_pnode *p);
int pclass_unset(tb_pnode *p);
Christopher Alfeld's avatar
 
Christopher Alfeld committed
134

135
void pclass_debug();
Christopher Alfeld's avatar
 
Christopher Alfeld committed
136

137 138
int count_enabled_pclasses();

Christopher Alfeld's avatar
 
Christopher Alfeld committed
139
#endif