Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Plug a memleak
[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_EXTERNAL_CATEGORY(amok_bw);
14 XBT_LOG_DEFAULT_CATEGORY(amok_bw);
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,"host",gras_datadesc_by_name("s_xbt_host_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 host 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 host 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
75   sat_request_t request = xbt_new(s_sat_request_t,1);
76
77   sock = gras_socket_client(from_name,from_port);
78
79   request->host.name = (char*)to_name;
80   request->host.port = to_port;
81   
82   request->duration=duration;
83   request->msg_size=msg_size;
84
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->host.name,request->host.port);
100         
101   gras_msg_rpcreturn(60,ctx, NULL);
102   amok_bw_saturate_begin(request->host.name,request->host.port,
103                          request->msg_size, request->duration,
104                          NULL,NULL);
105   free(request->host.name);
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 host 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 void amok_bw_saturate_begin(const char* to_name,unsigned int to_port,
120                             unsigned int msg_size, double duration,
121                             /*out*/ double *elapsed_res, double *bw_res) {
122  
123   xbt_ex_t e;
124
125   gras_socket_t peer_cmd = gras_socket_client(to_name, to_port);
126   gras_msg_cb_ctx_t ctx;
127
128   gras_socket_t meas;
129
130   s_gras_msg_t msg_got;
131
132   unsigned int packet_sent=0;
133   double start,elapsed=-1; /* timer */
134   double bw;
135
136   volatile int saturate_further; /* boolean in the main loop */ 
137
138   /* Negociate the saturation with the peer */
139   sat_request_t request = xbt_new(s_sat_request_t,1);
140
141   DEBUG2("Begin to saturate to %s:%d",to_name,to_port);
142   memset(&msg_got,0,sizeof(msg_got));
143
144   request->msg_size = msg_size;
145   request->duration = duration;
146   request->host.name = NULL;
147   request->host.port = 0;
148
149   ctx = gras_msg_rpc_async_call(peer_cmd, 60, 
150                                 gras_msgtype_by_name("amok_bw_sat begin"),
151                                                   &request);
152   free(request);
153   gras_msg_rpc_async_wait(ctx,&request);
154   meas=gras_socket_client_ext( to_name, request->host.port,
155                                0 /*bufsize: auto*/,
156                                1 /*meas: true*/);
157   free(request);
158
159   gras_socket_close(peer_cmd);
160   INFO2("Saturation from %s to %s started",gras_os_myname(),to_name);
161
162   /* Start experiment */
163   start=gras_os_time();
164
165   do {
166     /* do send it */
167     gras_socket_meas_send(meas,120,msg_size,msg_size);
168     packet_sent++;
169
170     /* Check whether someone asked us to stop saturation */
171     saturate_further = 0;
172     TRY {
173       gras_msg_wait_ext(0/*no wait*/,gras_msgtype_by_name("amok_bw_sat stop"),
174                         NULL /* accept any sender */,
175                         NULL, NULL, /* No specific filter */
176                         &msg_got);
177     } CATCH(e) {
178       if (e.category == timeout_error) {
179         saturate_further=1;
180         memset(&msg_got,0,sizeof(msg_got)); /* may be overprotectiv here */
181       }
182       xbt_ex_free(e);
183     }
184
185     /* Check whether the experiment has to be terminated by now */
186     elapsed=gras_os_time()-start;
187     VERB2("elapsed %f duration %f",elapsed, duration);
188
189   } while (saturate_further && (duration==0 || elapsed < duration));
190
191   bw = ((double)(packet_sent*msg_size)) / elapsed;
192
193   if (elapsed_res)
194     *elapsed_res = elapsed;
195   if (bw_res)
196     *bw_res = bw;
197
198   /* If someone stopped us, inform him about the achieved bandwidth */
199   if (msg_got.expe) {
200     bw_res_t answer = xbt_new(s_bw_res_t,1);
201     s_gras_msg_cb_ctx_t ctx;
202
203     INFO3("Saturation from %s to %s stopped by %s",
204           gras_os_myname(),to_name, gras_socket_peer_name(msg_got.expe));
205     answer->timestamp=gras_os_time();
206     answer->sec=elapsed;
207     answer->bw=bw;
208
209     ctx.expeditor = msg_got.expe;
210     ctx.ID = msg_got.ID;
211     ctx.msgtype = msg_got.type;
212
213     gras_msg_rpcreturn(60,&ctx,&answer);
214     free(answer);
215   } else {
216     INFO4("Saturation from %s to %s elapsed after %f sec (achieving %f kb/s)",
217           gras_os_myname(),to_name,elapsed,bw/1024.0);
218   }
219   
220   gras_socket_close(meas);
221 }
222
223 /* Sender will saturate link to us */
224 static int amok_bw_cb_sat_begin(gras_msg_cb_ctx_t ctx, void *payload){
225   sat_request_t request=*(sat_request_t*)payload;
226   sat_request_t answer=xbt_new0(s_sat_request_t,1);
227   volatile int saturate_further = 1;
228   xbt_ex_t e;
229   gras_socket_t measMaster=NULL,meas=NULL;
230   gras_socket_t from=gras_msg_cb_ctx_from(ctx);
231
232   int port=6000;
233   while (port <= 10000 && measMaster == NULL) {
234     TRY {
235       measMaster = gras_socket_server_ext(port,
236                                           0 /*bufsize: auto*/,
237                                           1 /*meas: true*/);
238     } CATCH(e) {
239       measMaster = NULL;
240       if (port < 10000)
241         xbt_ex_free(e);
242       else
243         RETHROW0("Error encountered while opening a measurement server socket: %s");
244     }
245     if (measMaster == NULL) 
246       port++; /* prepare for a new loop */
247   }
248   answer->host.port=port;
249
250   gras_msg_rpcreturn(60, ctx, &answer);
251   free(answer);
252
253   gras_os_sleep(5); /* Wait for the accept */
254
255   TRY {
256     meas = gras_socket_meas_accept(measMaster);
257     DEBUG0("saturation handshake answered");
258   } CATCH(e) {
259     gras_socket_close(measMaster);
260     RETHROW0("Error during saturation handshake: %s");
261   }  
262
263   while (saturate_further) {
264     TRY {
265       gras_socket_meas_recv(meas,120,request->msg_size,request->msg_size);
266     } CATCH(e) {
267       saturate_further = 0;
268       xbt_ex_free(e);
269     }
270   }
271   INFO3("Saturation comming from %s:%d stopped on %s",
272         gras_socket_peer_name(from),gras_socket_peer_port(from),
273         gras_os_myname());
274
275   gras_socket_close(meas);
276   if (gras_if_RL()) /* On SG, accepted=master */
277     gras_socket_close(measMaster); 
278   free(request);
279   return 1;
280 }
281
282 /**
283  * @brief Ask 'from_name:from_port' to stop any saturation experiments
284  * @param from_name: Name of the host we are asking to do a experiment with (to_name:to_port)
285  * @param from_port: port on which the process we are asking for an experiment is listening
286  * @param time: the duration of the experiment
287  * @param bw: the achieved bandwidth
288  *
289  */
290 void amok_bw_saturate_stop(const char* from_name,unsigned int from_port,
291                            /*out*/ double *time, double *bw) {
292
293   gras_socket_t sock = gras_socket_client(from_name,from_port);
294   bw_res_t answer;
295   VERB2("Ask %s:%d to stop the saturation",
296         from_name,from_port);
297   gras_msg_rpccall(sock,60,gras_msgtype_by_name("amok_bw_sat stop"),NULL,&answer);
298   gras_socket_close(sock);
299   if (time) *time=answer->sec;
300   if (bw)   *bw  =answer->bw;
301   free(answer);
302 }