Logo AND Algorithmique Numérique Distribuée

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