Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
First wave of GRAS API breaking: gras_cb_register wants a message name (char*) as...
[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,gras_msgtype_by_name("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 1;
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, 
170                                 gras_msgtype_by_name("amok_bw_sat begin"),
171                                                   &request);
172   free(request);
173   gras_msg_rpc_async_wait(ctx,&request);
174   meas=gras_socket_client_ext( to_name, request->peer.port,
175                                0 /*bufsize: auto*/,
176                                1 /*meas: true*/);
177
178   free(request);
179
180   gras_socket_close(peer_cmd);
181   INFO4("Saturation(%s:%d->%s:%d) started",gras_os_myname(),gras_os_myport(),to_name,to_port);
182
183   /* Start experiment */
184   start=gras_os_time();
185
186   do {
187     /* do send it */
188     gras_socket_meas_send(meas, 120, msg_size, 1);
189     packet_sent++;
190
191     /* Check whether someone asked us to stop saturation */
192     saturate_further = 0;
193     TRY {
194       gras_msg_wait_ext(0/*no wait*/,gras_msgtype_by_name("amok_bw_sat stop"),
195                         NULL /* accept any sender */,
196                         NULL, NULL, /* No specific filter */
197                         &msg_got);
198     } CATCH(e) {
199       if (e.category == timeout_error) {
200         saturate_further=1;
201         memset(&msg_got,0,sizeof(msg_got)); /* may be overprotectiv here */
202       }
203       xbt_ex_free(e);
204     }
205
206     /* Check whether the experiment has to be terminated by now */
207     elapsed=gras_os_time()-start;
208     DEBUG3("elapsed %f duration %f (msg_size=%d)",elapsed, duration,msg_size);
209
210   } while (saturate_further && (duration==0 || elapsed < duration));
211
212   bw = ((double)(packet_sent*msg_size)) / elapsed;
213
214   if (elapsed_res)
215     *elapsed_res = elapsed;
216   if (bw_res)
217     *bw_res = bw;
218
219   /* If someone stopped us, inform him about the achieved bandwidth */
220   if (msg_got.expe) {
221     bw_res_t answer = xbt_new(s_bw_res_t,1);
222     s_gras_msg_cb_ctx_t ctx;
223
224     INFO6("Saturation from %s:%d to %s:%d stopped by %s:%d",
225           gras_os_myname(),gras_os_myport(),to_name,to_port, gras_socket_peer_name(msg_got.expe),gras_socket_peer_port(msg_got.expe));
226     answer->timestamp=gras_os_time();
227     answer->sec=elapsed;
228     answer->bw=bw;
229
230     ctx.expeditor = msg_got.expe;
231     ctx.ID = msg_got.ID;
232     ctx.msgtype = msg_got.type;
233
234     gras_msg_rpcreturn(60,&ctx,&answer);
235     free(answer);
236   } else {
237     INFO6("Saturation from %s:%d to %s:%d elapsed after %f sec (achieving %f kb/s)",
238           gras_os_myname(),gras_os_myport(),to_name,to_port,elapsed,bw/1024.0);
239   }
240   
241   gras_socket_close(meas);
242 }
243
244 /* Sender will saturate link to us */
245 static int amok_bw_cb_sat_begin(gras_msg_cb_ctx_t ctx, void *payload){
246   sat_request_t request=*(sat_request_t*)payload;
247   sat_request_t answer=xbt_new0(s_sat_request_t,1);
248   volatile int saturate_further = 1;
249   xbt_ex_t e;
250   gras_socket_t measMaster=NULL,meas=NULL;
251   gras_socket_t from=gras_msg_cb_ctx_from(ctx);
252
253   int port=6000;
254   while (port <= 10000 && measMaster == NULL) {
255     TRY {
256       measMaster = gras_socket_server_ext(port,
257                                           0 /*bufsize: auto*/,
258                                           1 /*meas: true*/);
259     } CATCH(e) {
260       measMaster = NULL;
261       if (port < 10000)
262         xbt_ex_free(e);
263       else
264         RETHROW0("Error encountered while opening a measurement server socket: %s");
265     }
266     if (measMaster == NULL) 
267       port++; /* prepare for a new loop */
268   }
269   answer->peer.port=port;
270
271   gras_msg_rpcreturn(60, ctx, &answer);
272   free(answer);
273
274   gras_os_sleep(5); /* Wait for the accept */
275
276   TRY {
277     meas = gras_socket_meas_accept(measMaster);
278     DEBUG0("saturation handshake answered");
279   } CATCH(e) {
280     gras_socket_close(measMaster);
281     RETHROW0("Error during saturation handshake: %s");
282   }  
283
284   while (saturate_further) {
285     TRY {
286       gras_socket_meas_recv(meas, 5, request->msg_size, 1);
287     } CATCH(e) {
288       saturate_further = 0;
289       xbt_ex_free(e);
290     }
291   }
292   INFO4("Saturation comming from %s:%d stopped on %s:%d",
293         gras_socket_peer_name(from),gras_socket_peer_port(from),
294         gras_os_myname(),gras_os_myport());
295
296   gras_socket_close(meas);
297   if (gras_if_RL()) /* On SG, accepted=master */
298     gras_socket_close(measMaster); 
299   free(request);
300   return 1;
301 }
302
303 /**
304  * @brief Ask 'from_name:from_port' to stop any saturation experiments
305  * @param from_name: Name of the peer we are asking to do a experiment with (to_name:to_port)
306  * @param from_port: port on which the process we are asking for an experiment is listening
307  * @param time: the duration of the experiment
308  * @param bw: the achieved bandwidth
309  *
310  */
311 void amok_bw_saturate_stop(const char* from_name,unsigned int from_port,
312                            /*out*/ double *time, double *bw) {
313   xbt_ex_t e;
314    
315   gras_socket_t sock = gras_socket_client(from_name,from_port);
316   bw_res_t answer;
317   VERB2("Ask %s:%d to stop the saturation",from_name,from_port);
318   TRY { 
319      gras_msg_rpccall(sock,60,gras_msgtype_by_name("amok_bw_sat stop"),NULL,&answer);
320   } CATCH(e) {
321      RETHROW2("Cannot ask %s:%d to stop saturation: %s",from_name, from_port);
322   }
323   gras_socket_close(sock);
324   if (time) *time=answer->sec;
325   if (bw)   *bw  =answer->bw;
326   free(answer);
327 }