Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c504ab036b27597f5a54da66600b155e2e51f931
[simgrid.git] / src / amok / Bandwidth / saturate.c
1 /* $Id$ */
2
3 /* amok_saturate - Link saturating facilities (for ALNeM's BW testing)      */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "amok/Bandwidth/bandwidth_private.h"
11 #include "gras/Msg/msg_private.h" /* FIXME: This mucks with contextes to answer RPC directly */
12 #include "xbt/ex.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(amok_bw_sat,amok_bw,"Everything concerning the SATuration part of the amok_bw module");
15
16 static int amok_bw_cb_sat_start(gras_msg_cb_ctx_t ctx, void *payload);
17 static int amok_bw_cb_sat_begin(gras_msg_cb_ctx_t ctx, void *payload);
18
19
20 void amok_bw_sat_init(void) {
21   gras_datadesc_type_t bw_res_desc=gras_datadesc_by_name("bw_res_t");
22   gras_datadesc_type_t sat_request_desc;
23   /* Build the saturation datatype descriptions */ 
24   
25   sat_request_desc = gras_datadesc_struct("s_sat_request_desc_t");
26   gras_datadesc_struct_append(sat_request_desc,"peer",gras_datadesc_by_name("s_xbt_peer_t"));
27   gras_datadesc_struct_append(sat_request_desc,"msg_size",gras_datadesc_by_name("unsigned int"));
28   gras_datadesc_struct_append(sat_request_desc,"duration",gras_datadesc_by_name("unsigned int"));
29   gras_datadesc_struct_close(sat_request_desc);
30   sat_request_desc = gras_datadesc_ref("sat_request_t",sat_request_desc);
31   
32   /* Register the saturation messages */
33   gras_msgtype_declare_rpc("amok_bw_sat start", sat_request_desc, NULL);
34   gras_msgtype_declare_rpc("amok_bw_sat begin", sat_request_desc, sat_request_desc);
35   gras_msgtype_declare_rpc("amok_bw_sat stop",  NULL,             bw_res_desc);
36
37 }
38 void amok_bw_sat_join(void) {
39   gras_cb_register(gras_msgtype_by_name("amok_bw_sat start"),
40                    &amok_bw_cb_sat_start);
41   gras_cb_register(gras_msgtype_by_name("amok_bw_sat begin"),
42                    &amok_bw_cb_sat_begin);
43 }
44 void amok_bw_sat_leave(void) {
45   gras_cb_unregister(gras_msgtype_by_name("amok_bw_sat start"),
46                      &amok_bw_cb_sat_start);
47   gras_cb_unregister(gras_msgtype_by_name("amok_bw_sat begin"),
48                      &amok_bw_cb_sat_begin);
49 }
50
51 /* ***************************************************************************
52  * Link saturation
53  * ***************************************************************************/
54
55 /**
56  * @brief Ask 'from_name:from_port' to stop saturating going to to_name:to_name.
57  *
58  * @param from_name: Name of the peer we are asking to do a experiment with (to_name:to_port)
59  * @param from_port: port on which the process we are asking for an experiment is listening
60  * (for message, do not give a raw socket here. The needed raw socket will be negociated 
61  * between the peers)
62  * @param to_name: Name of the peer with which we should conduct the experiment
63  * @param to_port: port on which the peer process is listening for message
64  * @param msg_size: Size of each message sent.
65  * @param duration: How long in maximum should be the saturation.
66  *
67  * Ask the process 'from_name:from_port' to start to saturate the link between itself
68  * and to_name:to_name.
69  */
70 void amok_bw_saturate_start(const char* from_name,unsigned int from_port,
71                             const char* to_name,unsigned int to_port,
72                             unsigned int msg_size, double duration) {
73   gras_socket_t sock;
74   sat_request_t request = xbt_new(s_sat_request_t,1);
75
76   VERB4("Start from_name %s:%d -> to_name %s:%d",
77         from_name, from_port, to_name, to_port);
78   sock = gras_socket_client(from_name,from_port);
79
80   request->peer.name = (char*)to_name;
81   request->peer.port = to_port;
82   
83   request->duration=duration;
84   request->msg_size=msg_size;
85
86   gras_msg_rpccall(sock,60,gras_msgtype_by_name("amok_bw_sat start"),&request, NULL);
87
88   free(request);
89   gras_socket_close(sock);
90 }
91
92 /* Asked to begin a saturation */
93 static int amok_bw_cb_sat_start(gras_msg_cb_ctx_t ctx, void *payload){
94   sat_request_t request = *(sat_request_t*)payload;
95   gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
96  
97   VERB4("Asked by %s:%d to start a saturation to %s:%d",
98         gras_socket_peer_name(expeditor),gras_socket_peer_port(expeditor),
99         request->peer.name,request->peer.port);
100         
101   gras_msg_rpcreturn(60,ctx, NULL);
102
103   amok_bw_saturate_begin(request->peer.name,request->peer.port,
104
105                          request->msg_size, request->duration,
106                          NULL,NULL);
107  
108   free(request->peer.name);
109
110   free(request);
111   return 1;
112 }
113
114 /**
115  * @brief Start saturating between the current process and the designated peer
116  *
117  * Note that the only way to break this function before the end of the timeout
118  * is to have a remote peer calling amok_bw_saturate_stop to this process.
119  *
120  * If duration=0, the experiment will never timeout (you then have to manually
121  * stop it).
122  * 
123  * If msg_size=0, the size will be automatically computed to make sure that
124  * each of the messages occupy the connexion one second
125  */
126 void amok_bw_saturate_begin(const char* to_name,unsigned int to_port,
127                             unsigned int msg_size, double duration,
128                             /*out*/ double *elapsed_res, double *bw_res) {
129  
130   xbt_ex_t e;
131
132   gras_socket_t peer_cmd = gras_socket_client(to_name, to_port);
133   gras_msg_cb_ctx_t ctx;
134
135   gras_socket_t meas;
136
137   s_gras_msg_t msg_got;
138
139   unsigned int packet_sent=0;
140   double start,elapsed=-1; /* timer */
141   double bw;
142
143   volatile int saturate_further; /* boolean in the main loop */ 
144
145   /* Negociate the saturation with the peer */
146   sat_request_t request = xbt_new(s_sat_request_t,1);
147
148   DEBUG2("Begin to saturate to %s:%d",to_name,to_port);
149   memset(&msg_got,0,sizeof(msg_got));
150
151   request->msg_size = msg_size;
152   request->duration = duration;
153   request->peer.name = NULL;
154   request->peer.port = 0;
155
156
157   /* Size autodetection on need */
158   if (!msg_size) {
159     double bw;
160     double sec;
161     amok_bw_test(peer_cmd,
162                  0,512*1024, 512*1024, /* 512k as first guess */
163                  1, /* at least one sec */
164                  &sec, &bw);
165     msg_size = request->msg_size = (int)bw;
166     DEBUG1("Saturate with packets of %d bytes",request->msg_size);
167   }
168    
169   /* Launch the saturation */
170
171   ctx = gras_msg_rpc_async_call(peer_cmd, 60, 
172                                 gras_msgtype_by_name("amok_bw_sat begin"),
173                                                   &request);
174   free(request);
175   gras_msg_rpc_async_wait(ctx,&request);
176   meas=gras_socket_client_ext( to_name, request->peer.port,
177                                0 /*bufsize: auto*/,
178                                1 /*meas: true*/);
179
180   free(request);
181
182   gras_socket_close(peer_cmd);
183   INFO4("Saturation(%s:%d->%s:%d) started",gras_os_myname(),gras_os_myport(),to_name,to_port);
184
185   /* Start experiment */
186   start=gras_os_time();
187
188   do {
189     /* do send it */
190     gras_socket_meas_send(meas, 120, msg_size, 1);
191     packet_sent++;
192
193     /* Check whether someone asked us to stop saturation */
194     saturate_further = 0;
195     TRY {
196       gras_msg_wait_ext(0/*no wait*/,gras_msgtype_by_name("amok_bw_sat stop"),
197                         NULL /* accept any sender */,
198                         NULL, NULL, /* No specific filter */
199                         &msg_got);
200     } CATCH(e) {
201       if (e.category == timeout_error) {
202         saturate_further=1;
203         memset(&msg_got,0,sizeof(msg_got)); /* may be overprotectiv here */
204       }
205       xbt_ex_free(e);
206     }
207
208     /* Check whether the experiment has to be terminated by now */
209     elapsed=gras_os_time()-start;
210     DEBUG3("elapsed %f duration %f (msg_size=%d)",elapsed, duration,msg_size);
211
212   } while (saturate_further && (duration==0 || elapsed < duration));
213
214   bw = ((double)(packet_sent*msg_size)) / elapsed;
215
216   if (elapsed_res)
217     *elapsed_res = elapsed;
218   if (bw_res)
219     *bw_res = bw;
220
221   /* If someone stopped us, inform him about the achieved bandwidth */
222   if (msg_got.expe) {
223     bw_res_t answer = xbt_new(s_bw_res_t,1);
224     s_gras_msg_cb_ctx_t ctx;
225
226     INFO6("Saturation from %s:%d to %s:%d stopped by %s:%d",
227           gras_os_myname(),gras_os_myport(),to_name,to_port, gras_socket_peer_name(msg_got.expe),gras_socket_peer_port(msg_got.expe));
228     answer->timestamp=gras_os_time();
229     answer->sec=elapsed;
230     answer->bw=bw;
231
232     ctx.expeditor = msg_got.expe;
233     ctx.ID = msg_got.ID;
234     ctx.msgtype = msg_got.type;
235
236     gras_msg_rpcreturn(60,&ctx,&answer);
237     free(answer);
238   } else {
239     INFO6("Saturation from %s:%d to %s:%d elapsed after %f sec (achieving %f kb/s)",
240           gras_os_myname(),gras_os_myport(),to_name,to_port,elapsed,bw/1024.0);
241   }
242   
243   gras_socket_close(meas);
244 }
245
246 /* Sender will saturate link to us */
247 static int amok_bw_cb_sat_begin(gras_msg_cb_ctx_t ctx, void *payload){
248   sat_request_t request=*(sat_request_t*)payload;
249   sat_request_t answer=xbt_new0(s_sat_request_t,1);
250   volatile int saturate_further = 1;
251   xbt_ex_t e;
252   gras_socket_t measMaster=NULL,meas=NULL;
253   gras_socket_t from=gras_msg_cb_ctx_from(ctx);
254
255   int port=6000;
256   while (port <= 10000 && measMaster == NULL) {
257     TRY {
258       measMaster = gras_socket_server_ext(port,
259                                           0 /*bufsize: auto*/,
260                                           1 /*meas: true*/);
261     } CATCH(e) {
262       measMaster = NULL;
263       if (port < 10000)
264         xbt_ex_free(e);
265       else
266         RETHROW0("Error encountered while opening a measurement server socket: %s");
267     }
268     if (measMaster == NULL) 
269       port++; /* prepare for a new loop */
270   }
271   answer->peer.port=port;
272
273   gras_msg_rpcreturn(60, ctx, &answer);
274   free(answer);
275
276   gras_os_sleep(5); /* Wait for the accept */
277
278   TRY {
279     meas = gras_socket_meas_accept(measMaster);
280     DEBUG0("saturation handshake answered");
281   } CATCH(e) {
282     gras_socket_close(measMaster);
283     RETHROW0("Error during saturation handshake: %s");
284   }  
285
286   while (saturate_further) {
287     TRY {
288       gras_socket_meas_recv(meas, 5, request->msg_size, 1);
289     } CATCH(e) {
290       saturate_further = 0;
291       xbt_ex_free(e);
292     }
293   }
294   INFO4("Saturation comming from %s:%d stopped on %s:%d",
295         gras_socket_peer_name(from),gras_socket_peer_port(from),
296         gras_os_myname(),gras_os_myport());
297
298   gras_socket_close(meas);
299   if (gras_if_RL()) /* On SG, accepted=master */
300     gras_socket_close(measMaster); 
301   free(request);
302   return 1;
303 }
304
305 /**
306  * @brief Ask 'from_name:from_port' to stop any saturation experiments
307  * @param from_name: Name of the peer we are asking to do a experiment with (to_name:to_port)
308  * @param from_port: port on which the process we are asking for an experiment is listening
309  * @param time: the duration of the experiment
310  * @param bw: the achieved bandwidth
311  *
312  */
313 void amok_bw_saturate_stop(const char* from_name,unsigned int from_port,
314                            /*out*/ double *time, double *bw) {
315   xbt_ex_t e;
316    
317   gras_socket_t sock = gras_socket_client(from_name,from_port);
318   bw_res_t answer;
319   VERB2("Ask %s:%d to stop the saturation",from_name,from_port);
320   TRY { 
321      gras_msg_rpccall(sock,60,gras_msgtype_by_name("amok_bw_sat stop"),NULL,&answer);
322   } CATCH(e) {
323      RETHROW2("Cannot ask %s:%d to stop saturation: %s",from_name, from_port);
324   }
325   gras_socket_close(sock);
326   if (time) *time=answer->sec;
327   if (bw)   *bw  =answer->bw;
328   free(answer);
329 }