Logo AND Algorithmique Numérique Distribuée

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