Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3a8a2f1006dcc9c31f1595202cc2b90acc0b9241
[simgrid.git] / src / surf / gtnets / gtnets_simulator.cc
1 /*      $Id$     */
2 /* Copyright (c) 2007 Kayo Fujiwara. All rights reserved.                  */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "gtnets_simulator.h"
8 #include "gtnets_topology.h"
9 #include <map>
10 #include <vector>
11 #ifdef DEBUG0
12         #undef DEBUG0
13 #endif
14 #include "xbt/log.h"
15 #include "xbt/asserts.h"
16 #include "RngStream.h"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets_simulator, surf_network_gtnets,
19                                 "Logging specific to the SURF network GTNetS simulator");
20
21
22 using namespace std;
23
24 static vector<void*> meta_flows;
25 static int* meta_nflow;
26 static int meta_flg = 0;
27
28
29 void static tcp_sent_callback(void* action, double completion_time);
30
31 // Constructor.
32 // TODO: check the default values.
33 GTSim::GTSim(){
34   int wsize = 20000;
35   is_topology_ = 0;
36   nflow_ = 0;
37   jitter_ = 0;
38   jitter_seed_ = 10;
39
40   // EXTRACTED FROM GTNETS SOURCE CODE COMMENTS
41   //  REDQueue::REDQueue(
42   //     DCount_t in_w_q, Count_t in_min_th, Count_t in_max_th,
43   //     Count_t in_limit, DCount_t in_max_p, Count_t in_mean_pktsize) : iface(nil)
44   // Set default values.
45   //Doc:Desc This constructor the critical RED parameters and builds a
46   //Doc:Desc correspoding RED queue
47   //Doc:Arg1 weight of the queue
48   //Doc:Arg2 minimum threshold
49   //Doc:Arg3 maximum threshold
50   //Doc:Arg4 Limit/max size for the queue
51   //Doc:Arg5 maximum value for mark/drop probability
52   //Doc:Arg6 Average packet size
53
54   //Default Parameters
55   //REDQueue *default_red_queue_ = new REDQueue(0.002, 2500, 7500, 30000, 0.10, 500);
56   //Same as above
57   //REDQueue *default_red_queue_ = new REDQueue();
58
59   //See for details of how those values are calucated below
60   //[1] Sally Floyd and Van Jacobson, "Random Early Detection Gateways with Congestion Avoidance",
61   //    IEEE/ACM Transactions on Networking, vol. 1, n. 4, august 1993.
62   //
63   //[2] Kostas Pentikousis, "Active Queue Management", ACM Crossroads, vol. 7, n. 5,
64   //    mid-summer 2001
65   //
66   //[3] Stefann De Cnodder, Omar Ecoumi, Kenny Paulwels, "RED behavior with different packet sizes",
67   //    5th IEEE Symposium on Computers and Communication, (ISCC 2000)
68   //
69   //[4] http://www.opalsoft.net/qos/DS-26.htm
70   //
71   //short explanation:
72   // q_weight = fixed to 0.002 in most literature
73   // min_bytes = max / 3 = 16,666,666
74   // max_bytes = mean_bw * max_tolerable_latency, set to 1e8 * 0.5 = 50,000,000
75   // limit_bytes = 8 * max = 400,000,000
76   // prob = follow most literature 0.02
77   // avgpkt = fixed to the same TCP segment size, 1000 Bytes
78   //
79   // burst = (2*(min+max))/(3*avgpkt) ***DON'T USED BY GTNetS***
80   REDQueue *default_red_queue_ = new REDQueue(0.002, 16666666, 50000000, 400000000, 0.02, 1000);
81
82   Queue::Default(*default_red_queue_);
83   delete default_red_queue_;
84
85   TCP::DefaultAdvWin(wsize);
86   TCP::DefaultSegSize(1000);
87   TCP::DefaultTxBuffer(128000);
88   TCP::DefaultRxBuffer(128000);
89
90   sim_ = new Simulator();
91   sim_->verbose=false;
92   topo_ = new GTNETS_Topology();
93
94   // Manual routing
95   rm_ = new RoutingManual();
96   Routing::SetRouting(rm_);
97 }
98
99 GTSim::~GTSim(){
100
101   map<int, Linkp2p*>::iterator it;
102   for (it = gtnets_links_.begin(); it != gtnets_links_.end(); it++){
103     delete it->second;
104   }
105   while (!gtnets_links_.empty())
106     gtnets_links_.erase(gtnets_links_.begin());
107
108   map<int, Uniform*>::iterator it2;
109   for (it2 = uniform_jitter_generator_.begin(); it2 != uniform_jitter_generator_.end(); it2++){
110     delete it2->second;
111   }
112   while (!uniform_jitter_generator_.empty())
113     uniform_jitter_generator_.erase(uniform_jitter_generator_.begin());
114
115   map<int, Node*>::iterator it3;
116   for (it3 = gtnets_nodes_.begin(); it3 != gtnets_nodes_.end(); it3++){
117     delete it3->second;
118   }
119   while (!gtnets_nodes_.empty())
120     gtnets_nodes_.erase(gtnets_nodes_.begin());
121
122   map<int, TCPServer*>::iterator it4;
123   for (it4 = gtnets_servers_.begin(); it4 != gtnets_servers_.end(); it4++){
124     delete it4->second;
125   }
126   while (!gtnets_servers_.empty())
127     gtnets_servers_.erase(gtnets_servers_.begin());
128
129   map<int, TCPSend*>::iterator it5;
130   for (it5 = gtnets_clients_.begin(); it5 != gtnets_clients_.end(); it5++){
131     delete it5->second;
132   }
133   while (!gtnets_clients_.empty())
134     gtnets_clients_.erase(gtnets_clients_.begin());
135
136   is_topology_ = 0;
137   delete sim_;
138   delete topo_;
139   delete rm_;
140   sim_ = 0;
141   topo_ = 0;
142   rm_ = 0;
143 }
144
145 int GTSim::add_router(int id){
146   xbt_assert1(!(topo_->add_router(id) < 0), "can't add router %d. already exists", id);
147 }
148
149 //bandwidth: in bytes.
150 //latency: in seconds.
151 int GTSim::add_link(int id, double bandwidth, double latency){
152   double bw = bandwidth * 8; //Bandwidth in bits (used in GTNETS).
153   xbt_assert1(!(topo_->add_link(id) < 0),"Can't add link %d. already exists", id);
154   DEBUG3("Creating a new P2P, linkid %d, bandwidth %gl, latency %gl", id, bandwidth, latency);
155   gtnets_links_[id] = new Linkp2p(bw, latency);
156   if(jitter_ > 0){
157         DEBUG2("Using jitter %f, and seed %u", jitter_, jitter_seed_);
158         double min = -1*jitter_*latency;
159         double max = jitter_*latency;
160         uniform_jitter_generator_[id] = new Uniform(min,max);
161         gtnets_links_[id]->Jitter((const Random &) *(uniform_jitter_generator_[id]));
162   }
163   return 0;
164 }
165
166 // if gtnets_nodes_ includes id, return true, otherwise return false.
167 bool GTSim::node_include(int id){
168   if (gtnets_nodes_.find(id) != gtnets_nodes_.end()) return true;
169   else return false;
170 }
171
172 // if gtnets_link_ includes id, return true, otherwise return false.
173 bool GTSim::link_include(int id){
174   if (gtnets_links_.find(id) != gtnets_links_.end()) return true;
175   else return false;
176 }
177
178 int GTSim::add_onehop_route(int src, int dst, int link){
179   xbt_assert3(!(topo_->add_onehop_route(src, dst, link) < 0), "Cannot add a route, src: %d, dst: %d, link: %d", src, dst, link);
180   return 0;
181 }
182
183 // Generate the gtnets nodes according to topo_.
184 void GTSim::add_nodes(){
185   static unsigned int address = IPAddr("192.168.0.1");
186   IPAddr helper = IPAddr();
187   vector<GTNETS_Node*> nodes = topo_->nodes();
188   vector<GTNETS_Node*>::iterator it;
189   int id;
190   for (it = nodes.begin(); it != nodes.end(); it++){
191     id = (*it)->id();
192     gtnets_nodes_[id] = new Node();
193     gtnets_nodes_[id]->SetIPAddr(address++);
194     DEBUG2("In GTSim, add_node: %d, with IPAddr %s", id, helper.ToDotted(address-1));
195
196   }
197 }
198
199 void GTSim::node_connect(){
200
201   map<int, GTNETS_Link*> links = topo_->links();
202   map<int, GTNETS_Link*>::iterator it;
203   int linkid, srcid, dstid;
204   for (it = links.begin(); it != links.end(); it++){
205     linkid = it->second->id();
206     //if link is used in a route, connect the two nodes.
207     if (it->second->src_node() && it->second->dst_node()){
208
209       srcid = it->second->src_node()->id();
210       dstid = it->second->dst_node()->id();
211
212       gtnets_nodes_[srcid]->
213         AddDuplexLink(gtnets_nodes_[dstid], *(gtnets_links_[linkid]));
214     DEBUG3("Setting DuplexLink, src %d, dst %d, linkid %d", srcid, dstid, linkid);
215     }
216   }
217 }
218
219 // Create nodes and routes from the temporary topology, GTNETS_Topolgy.
220 void GTSim::create_gtnets_topology(){
221   add_nodes();
222   node_connect();
223 }
224
225 void GTSim::print_topology(){
226   topo_->print_topology();
227 }
228
229 // Add a route that includes more than one hop. All one hop
230 // routes must have been added. When this function is called
231 // for the first time, all gtnets nodes are generated.
232 int GTSim::add_route(int src, int dst, int* links, int nlink){
233   if (is_topology_ == 0){
234     create_gtnets_topology();
235     is_topology_ = 1;
236   }  
237
238   IPAddr_t mymask = IPAddr("255.255.255.255");
239
240   int src_node = topo_->nodeid_from_hostid(src);
241   int dst_node = topo_->nodeid_from_hostid(dst);
242
243   xbt_assert1(!(gtnets_nodes_.find(src_node) == gtnets_nodes_.end()), "Node %d not found", src_node);
244   xbt_assert1(!(gtnets_nodes_.find(dst_node) == gtnets_nodes_.end()), "Node %d not found", dst_node);
245
246   Node* tmpsrc = gtnets_nodes_[src_node];
247   Node* tmpdst = gtnets_nodes_[dst_node];
248
249   int next_node, cur_node;
250   
251   cur_node = src_node;
252   for (int i = 0; i < nlink; i++){
253         xbt_assert1(!(gtnets_nodes_.find(cur_node) == gtnets_nodes_.end()), "Node %d not found", cur_node);
254     next_node = topo_->peer_node_id(links[i], cur_node);
255     xbt_assert0(!(next_node < 0), "Peer node not found");
256     xbt_assert1(!(gtnets_nodes_.find(next_node) == gtnets_nodes_.end()), "Node %d not found", next_node);
257     
258     //add route
259     Node* tmpcur = gtnets_nodes_[cur_node];
260     Node* tmpnext = gtnets_nodes_[next_node];
261
262     tmpcur->AddRoute(tmpdst->GetIPAddr(),
263                      mymask,
264                      tmpcur->GetIfByNode(tmpnext),
265                      tmpnext->GetIPAddr());
266
267     tmpnext->AddRoute(tmpsrc->GetIPAddr(),
268                       mymask,
269                       tmpnext->GetIfByNode(tmpcur),
270                       tmpcur->GetIPAddr());
271     
272     cur_node = next_node;
273   }
274
275   xbt_assert2(!(cur_node != dst_node), "Route inconsistency, last: %d, dst: %d",cur_node, dst_node);
276
277   return 0;
278 }
279
280
281
282 int GTSim::create_flow(int src, int dst, long datasize, void* metadata){
283   //if no route with more than one links, topology has not been generated.
284   //generate it here.
285   if (is_topology_ == 0){
286     create_gtnets_topology();
287     is_topology_ = 1;
288   }
289
290   int src_node = topo_->nodeid_from_hostid(src);
291   xbt_assert1(!(src_node < 0), "Src %d not found", src_node);
292
293   int dst_node = topo_->nodeid_from_hostid(dst);
294   xbt_assert1(!(dst_node < 0), "Dst %d not found", dst_node);
295
296   gtnets_servers_[nflow_] = (TCPServer*) gtnets_nodes_[dst_node]->
297        AddApplication(TCPServer(TCPReno()));
298   gtnets_servers_[nflow_]->BindAndListen(1000+nflow_);
299
300   gtnets_clients_[nflow_] = (TCPSend*)gtnets_nodes_[src_node]->
301     AddApplication(TCPSend(metadata, gtnets_nodes_[dst_node]->GetIPAddr(), 
302                            1000+nflow_, Constant(datasize), TCPReno()));
303   gtnets_clients_[nflow_]->SetSendCallBack(tcp_sent_callback);
304   gtnets_clients_[nflow_]->Start(0);
305
306   gtnets_action_to_flow_[metadata] = nflow_;
307   nflow_++;
308
309   return 0;
310 }
311
312 Time_t GTSim::get_time_to_next_flow_completion(){
313   int status;
314   Time_t t1;
315   int pfds[2];
316   int soon_pid=-1;
317   meta_flg=0;
318
319   //remain needs to be updated in the future
320   Count_t remain;
321   
322   pipe(pfds);
323   
324   t1 = 0;
325
326   if ( (soon_pid=fork()) != 0){
327     read(pfds[0], &t1, sizeof(Time_t));
328     waitpid(soon_pid, &status, 0);      
329   }else{
330     Time_t t;
331     t = sim_->RunUntilNextCompletion();
332     write(pfds[1], (const void*)&t, sizeof(Time_t));
333     exit(0);
334   }
335
336   return t1;
337 }
338
339 double GTSim::gtnets_get_flow_rx(void *metadata){
340   int flow_id = gtnets_action_to_flow_[metadata];
341   return gtnets_servers_[flow_id]->GetTotRx(); 
342 }
343
344
345 int GTSim::run_until_next_flow_completion(void ***metadata, int *number_of_flows){
346
347   meta_flows.clear();
348   meta_nflow = number_of_flows;
349   meta_flg = 1;
350
351   Time_t t1 = sim_->RunUntilNextCompletion();
352
353   *metadata = (meta_flows.empty() ? NULL : &meta_flows[0]);
354   return 0;
355 }
356
357 int GTSim::run(double delta){
358   meta_flg=0;
359   sim_->Run(delta);
360   return 0;
361 }
362
363 void GTSim::set_jitter(double d){
364   xbt_assert1(((0 <= d)&&(d <= 1)), "The jitter value must be within interval [0.0;1.0), got %f", d);
365   jitter_ = d;
366 }
367
368 void GTSim::set_jitter_seed(int s){
369   jitter_seed_ = s;
370
371   if(jitter_seed_ > 0.0){
372     INFO1("Setting the jitter_seed with %d", jitter_seed_ );
373     Random::GlobalSeed(jitter_seed_  , jitter_seed_  , jitter_seed_  ,jitter_seed_  ,jitter_seed_  ,jitter_seed_);
374   }
375 }
376
377 void static tcp_sent_callback(void* action, double completion_time){
378   // Schedule the flow complete event.
379   SimulatorEvent* e =
380     new SimulatorEvent(SimulatorEvent::FLOW_COMPLETE);
381   Simulator::instance->Schedule(e, 0, Simulator::instance);
382
383   if (meta_flg){
384     meta_flows.push_back(action);
385     (*meta_nflow)++;
386   }
387 }
388
389