Logo AND Algorithmique Numérique Distribuée

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