Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Added a jitter functionallity to GTNets model, latency is increased by a random numbe...
[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
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets_simulator, surf_network_gtnets,
18                                 "Logging specific to the SURF network GTNetS simulator");
19
20
21 using namespace std;
22
23 static vector<void*> meta_flows;
24 static int* meta_nflow;
25 static int meta_flg = 0;
26
27
28 void static tcp_sent_callback(void* action, double completion_time);
29
30 // Constructor.
31 // TODO: check the default values.
32 GTSim::GTSim(){
33   int wsize = 20000;
34   is_topology_ = 0;
35   nflow_ = 0;
36   sim_ = new Simulator();
37   topo_ = new GTNETS_Topology();
38   uniform_jitter_ = NULL;
39
40   sim_->verbose=false;
41   // Set default values.
42   TCP::DefaultAdvWin(wsize);
43   TCP::DefaultSegSize(1000);
44   TCP::DefaultTxBuffer(128000);
45   TCP::DefaultRxBuffer(128000);
46
47   // Manual routing
48   rm_ = new RoutingManual();
49   Routing::SetRouting(rm_);
50 }
51
52 GTSim::~GTSim(){
53
54   map<int, Linkp2p*>::iterator it;
55   for (it = gtnets_links_.begin(); it != gtnets_links_.end(); it++){
56     delete it->second;
57   }
58   while (!gtnets_links_.empty())
59     gtnets_links_.erase(gtnets_links_.begin());
60
61   map<int, Node*>::iterator it3;
62   for (it3 = gtnets_nodes_.begin(); it3 != gtnets_nodes_.end(); it3++){
63     delete it3->second;
64   }
65   while (!gtnets_nodes_.empty())
66     gtnets_nodes_.erase(gtnets_nodes_.begin());
67
68   map<int, TCPServer*>::iterator it4;
69   for (it4 = gtnets_servers_.begin(); it4 != gtnets_servers_.end(); it4++){
70     delete it4->second;
71   }
72   while (!gtnets_servers_.empty())
73     gtnets_servers_.erase(gtnets_servers_.begin());
74
75   map<int, TCPSend*>::iterator it5;
76   for (it5 = gtnets_clients_.begin(); it5 != gtnets_clients_.end(); it5++){
77     delete it5->second;
78   }
79   while (!gtnets_clients_.empty())
80     gtnets_clients_.erase(gtnets_clients_.begin());
81
82   is_topology_ = 0;
83   delete sim_;
84   delete topo_;
85   delete rm_;
86   sim_ = 0;
87   topo_ = 0;
88   rm_ = 0;
89 }
90
91 int GTSim::add_router(int id){
92   xbt_assert1(!(topo_->add_router(id) < 0), "can't add router %d. already exists", id);
93 }
94
95 //bandwidth: in bytes.
96 //latency: in seconds.
97 int GTSim::add_link(int id, double bandwidth, double latency){
98   double bw = bandwidth * 8; //Bandwidth in bits (used in GTNETS).
99   xbt_assert1(!(topo_->add_link(id) < 0),"Can't add link %d. already exists", id);
100   DEBUG3("Creating a new P2P, linkid %d, bandwidth %gl, latency %gl", id, bandwidth, latency);
101   gtnets_links_[id] = new Linkp2p(bw, latency);
102   if(jitter_ > 0){
103         DEBUG1("Using jitter %f", jitter_);
104         double min = 0.0;
105         double max = jitter_*latency;
106         if(uniform_jitter_ == NULL){
107                 uniform_jitter_ = new Uniform(min,max);
108         }
109         gtnets_links_[id]->Jitter((const Random &) *uniform_jitter_);
110   }
111   return 0;
112 }
113
114 // if gtnets_nodes_ includes id, return true, otherwise return false.
115 bool GTSim::node_include(int id){
116   if (gtnets_nodes_.find(id) != gtnets_nodes_.end()) return true;
117   else return false;
118 }
119
120 // if gtnets_link_ includes id, return true, otherwise return false.
121 bool GTSim::link_include(int id){
122   if (gtnets_links_.find(id) != gtnets_links_.end()) return true;
123   else return false;
124 }
125
126 int GTSim::add_onehop_route(int src, int dst, int link){
127   xbt_assert3(!(topo_->add_onehop_route(src, dst, link) < 0), "Cannot add a route, src: %d, dst: %d, link: %d", src, dst, link);
128   return 0;
129 }
130
131 // Generate the gtnets nodes according to topo_.
132 void GTSim::add_nodes(){
133   static unsigned int address = IPAddr("192.168.0.1");
134   IPAddr helper = IPAddr();
135   vector<GTNETS_Node*> nodes = topo_->nodes();
136   vector<GTNETS_Node*>::iterator it;
137   int id;
138   for (it = nodes.begin(); it != nodes.end(); it++){
139     id = (*it)->id();
140     gtnets_nodes_[id] = new Node();
141     gtnets_nodes_[id]->SetIPAddr(address++);
142     DEBUG2("In GTSim, add_node: %d, with IPAddr %s", id, helper.ToDotted(address-1));
143
144   }
145 }
146
147 void GTSim::node_connect(){
148
149   map<int, GTNETS_Link*> links = topo_->links();
150   map<int, GTNETS_Link*>::iterator it;
151   int linkid, srcid, dstid;
152   for (it = links.begin(); it != links.end(); it++){
153     linkid = it->second->id();
154     //if link is used in a route, connect the two nodes.
155     if (it->second->src_node() && it->second->dst_node()){
156
157       srcid = it->second->src_node()->id();
158       dstid = it->second->dst_node()->id();
159
160       gtnets_nodes_[srcid]->
161         AddDuplexLink(gtnets_nodes_[dstid], *(gtnets_links_[linkid]));
162     DEBUG3("Setting DuplexLink, src %d, dst %d, linkid %d", srcid, dstid, linkid);
163     }
164   }
165 }
166
167 // Create nodes and routes from the temporary topology, GTNETS_Topolgy.
168 void GTSim::create_gtnets_topology(){
169   add_nodes();
170   node_connect();
171 }
172
173 void GTSim::print_topology(){
174   topo_->print_topology();
175 }
176
177 // Add a route that includes more than one hop. All one hop
178 // routes must have been added. When this function is called
179 // for the first time, all gtnets nodes are generated.
180 int GTSim::add_route(int src, int dst, int* links, int nlink){
181   if (is_topology_ == 0){
182     create_gtnets_topology();
183     is_topology_ = 1;
184   }  
185
186   IPAddr_t mymask = IPAddr("255.255.255.255");
187
188   int src_node = topo_->nodeid_from_hostid(src);
189   int dst_node = topo_->nodeid_from_hostid(dst);
190
191   xbt_assert1(!(gtnets_nodes_.find(src_node) == gtnets_nodes_.end()), "Node %d not found", src_node);
192   xbt_assert1(!(gtnets_nodes_.find(dst_node) == gtnets_nodes_.end()), "Node %d not found", dst_node);
193
194   Node* tmpsrc = gtnets_nodes_[src_node];
195   Node* tmpdst = gtnets_nodes_[dst_node];
196
197   int next_node, cur_node;
198   
199   cur_node = src_node;
200   for (int i = 0; i < nlink; i++){
201         xbt_assert1(!(gtnets_nodes_.find(cur_node) == gtnets_nodes_.end()), "Node %d not found", cur_node);
202     next_node = topo_->peer_node_id(links[i], cur_node);
203     xbt_assert0(!(next_node < 0), "Peer node not found");
204     xbt_assert1(!(gtnets_nodes_.find(next_node) == gtnets_nodes_.end()), "Node %d not found", next_node);
205     
206     //add route
207     Node* tmpcur = gtnets_nodes_[cur_node];
208     Node* tmpnext = gtnets_nodes_[next_node];
209
210     tmpcur->AddRoute(tmpdst->GetIPAddr(),
211                      mymask,
212                      tmpcur->GetIfByNode(tmpnext),
213                      tmpnext->GetIPAddr());
214
215     tmpnext->AddRoute(tmpsrc->GetIPAddr(),
216                       mymask,
217                       tmpnext->GetIfByNode(tmpcur),
218                       tmpcur->GetIPAddr());
219     
220     cur_node = next_node;
221   }
222
223   xbt_assert2(!(cur_node != dst_node), "Route inconsistency, last: %d, dst: %d",cur_node, dst_node);
224
225   return 0;
226 }
227
228
229
230 int GTSim::create_flow(int src, int dst, long datasize, void* metadata){
231   //if no route with more than one links, topology has not been generated.
232   //generate it here.
233   if (is_topology_ == 0){
234     create_gtnets_topology();
235     is_topology_ = 1;
236   }
237
238   int src_node = topo_->nodeid_from_hostid(src);
239   xbt_assert1(!(src_node < 0), "Src %d not found", src_node);
240
241   int dst_node = topo_->nodeid_from_hostid(dst);
242   xbt_assert1(!(dst_node < 0), "Dst %d not found", dst_node);
243
244   gtnets_servers_[nflow_] = (TCPServer*) gtnets_nodes_[dst_node]->
245        AddApplication(TCPServer(TCPReno()));
246   gtnets_servers_[nflow_]->BindAndListen(1000+nflow_);
247
248   gtnets_clients_[nflow_] = (TCPSend*)gtnets_nodes_[src_node]->
249     AddApplication(TCPSend(metadata, gtnets_nodes_[dst_node]->GetIPAddr(), 
250                            1000+nflow_, Constant(datasize), TCPReno()));
251   gtnets_clients_[nflow_]->SetSendCallBack(tcp_sent_callback);
252   gtnets_clients_[nflow_]->Start(0);
253
254   gtnets_action_to_flow_[metadata] = nflow_;
255   nflow_++;
256
257   return 0;
258 }
259
260 Time_t GTSim::get_time_to_next_flow_completion(){
261   int status;
262   Time_t t1;
263   int pfds[2];
264   int soon_pid=-1;
265   meta_flg=0;
266
267   //remain needs to be updated in the future
268   Count_t remain;
269   
270   pipe(pfds);
271   
272   t1 = 0;
273
274   if ( (soon_pid=fork()) != 0){
275     read(pfds[0], &t1, sizeof(Time_t));
276     waitpid(soon_pid, &status, 0);      
277   }else{
278     Time_t t;
279     t = sim_->RunUntilNextCompletion();
280     write(pfds[1], (const void*)&t, sizeof(Time_t));
281     exit(0);
282   }
283
284   return t1;
285 }
286
287 double GTSim::gtnets_get_flow_rx(void *metadata){
288   int flow_id = gtnets_action_to_flow_[metadata];
289   return gtnets_servers_[flow_id]->GetTotRx(); 
290 }
291
292 int GTSim::run_until_next_flow_completion(void ***metadata, int *number_of_flows){
293
294   meta_flows.clear();
295   meta_nflow = number_of_flows;
296   meta_flg = 1;
297
298   Time_t t1 = sim_->RunUntilNextCompletion();
299
300   *metadata = (meta_flows.empty() ? NULL : &meta_flows[0]);
301   return 0;
302 }
303
304 int GTSim::run(double delta){
305   meta_flg=0;
306   sim_->Run(delta);
307   return 0;
308 }
309
310 void GTSim::set_jitter(double d){
311   jitter_ = d;
312 }
313
314 void static tcp_sent_callback(void* action, double completion_time){
315   // Schedule the flow complete event.
316   SimulatorEvent* e =
317     new SimulatorEvent(SimulatorEvent::FLOW_COMPLETE);
318   Simulator::instance->Schedule(e, 0, Simulator::instance);
319
320   if (meta_flg){
321     meta_flows.push_back(action);
322     (*meta_nflow)++;
323   }
324 }
325
326