sources:
generator.cpp (709 bytes)
generator.h (417 bytes)
generator_n.cpp (4.0k)
lbs_i.cpp (2.6k)
lbs_i.h (430 bytes)
lbs_i_n.cpp (4.1k)
lbs_ii.cpp (3.3k)
lbs_ii.h (439 bytes)
lbs_ii_n.cpp (4.1k)
minimumselector.cpp (1.7k)
minimumselector.h (359 bytes)
minimumselector_n.cpp (4.1k)
randomselector.cpp (454 bytes)
randomselector.h (266 bytes)
randomselector_n.cpp (4.1k)
server.cpp (1.6k)
server.h (508 bytes)
server_n.cpp (4.0k)
servicenet_n.cpp (13.6k)
sink.cpp (1.3k)
sink.h (495 bytes)
sink_n.cpp (3.9k)


website:
more info here


screenshot:
studies/performance/Performance-Code5/lbs_ii.cpp
download file

  1 #include "lbs_ii.h"
  2
  3 Define_Module( LBS_II )
  4
  5 void LBS_II::activity()
  6 {
  7     // get parameters
  8     simtime_t delta_t = par("delta_t");
  9
 10     // search servers and save reference to its workload parameter
 11     // as well as their rate
 12     cModule *compound = parentModule();
 13
 14     for (int i = 0; i<serverCount; i++) {
 15         cModule *svr = compound->submodule("server", i);
 16         if ((svr==0) || (strcmp(svr->className(), "Server")!=0)) {
 17             throw cException("server[] contains non-Server");
 18         }
 19         server[i] = (Server*) svr;
 20         rate[i] = svr->par("rate");
 21     }
 22
 23     // create update-message and issue it immediately
 24     cMessage *updateMsg = new cMessage("update-balancer");
 25     scheduleAt(simTime() + delta_t, updateMsg);
 26     update();
 27
 28     // message loop
 29     for (;;) {
 30         // get message
 31         cMessage *msg = receive();
 32
 33         if (msg==updateMsg) {
 34             // update received, so do it and issue the message again delta_t secs later
 35             update();
 36             scheduleAt(simTime() + delta_t, updateMsg);
 37         } else {
 38             // send the message to a server
 39             send(msg, "out", chooseServer());
 40         }
 41     }
 42
 43 }
 44
 45 void LBS_II::update()
 46 {
 47     // search minimum workload
 48     int i;
 49     double load = 100000000;
 50     for (i = 0; i<serverCount; i++) {
 51         // get queue length
 52         long l = server[i]->getJobsInSystem();
 53         if (l>0) --l;
 54         // calculate performance measure and save it
 55         double bl = (double) l/rate[i];
 56         balancing[i] = bl;
 57         // record smallest workload
 58         if (bl<load) {
 59             load = bl;
 60         }
 61     }
 62
 63     // calculate weights
 64     if (load==0.0) {
 65         // minimum workload is zero => use different set of equations
 66         for (i = 0; i<serverCount; i++) {
 67             if (balancing[i]!=0.0) balancing[i] = 1/(1+balancing[i]);
 68             else balancing[i] = 1.0;
 69         }
 70     } else {
 71         // standard weight calculation
 72         for (i = 0; i<serverCount; i++) {
 73             if (balancing[i]!=0.0) balancing[i] = load/balancing[i];
 74             else balancing[i] = 1.0;
 75         }
 76     }
 77
 78     // total sum of weights
 79     double wSum = 0.0;
 80     for (i = 0; i<serverCount; i++) wSum += balancing[i];
 81    
 82     // normalize weights
 83     for (i = 0; i<serverCount; i++) {
 84         balancing[i] /= wSum;
 85         ev << "balancing[" << i << "] = " << balancing[i] << endl;
 86     }
 87
 88     // change balancing into distribution
 89     for (i = 1; i<serverCount; i++) balancing[i] += balancing[i-1];
 90 }
 91
 92 int LBS_II::chooseServer()
 93 {
 94     // get random value
 95     double rVal = uniform(0, 1, rg);
 96
 97     // get server no via inverse transformation method
 98     int i = 0;
 99     while ((i<serverCount) && (balancing[i]<rVal)) ++i;
100     if (i==serverCount) throw cException("error while choosing server!");
101
102     ev << "chose server " << i << endl;
103
104     return i;
105 }
106
107 void LBS_II::initialize()
108 {
109     // get parameters
110     rg = par("random_generator");
111     serverCount = par("server_count");
112     // create arrays
113     server = new Server *[serverCount];
114     rate = new double[serverCount];
115     balancing = new double[serverCount];
116 }
117
118 void LBS_II::finish()
119 {
120     delete [] server;
121     delete [] rate;
122     delete [] balancing;
123 }
124
125