Logo AND Algorithmique Numérique Distribuée

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