Logo AND Algorithmique Numérique Distribuée

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