Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
* Better split between bandwidth and saturation sub-modules
[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(bw);
14 XBT_LOG_DEFAULT_CATEGORY(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 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,"host",gras_datadesc_by_name("s_xbt_host_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, NULL);
34   //  gras_msgtype_declare_rpc("amok_bw_sat end",   NULL,             NULL);
35   gras_msgtype_declare_rpc("amok_bw_sat stop",  NULL,             NULL);
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  * @from_name: Name of the host we are asking to do a experiment with (to_name:to_port)
59  * @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  * @to_name: Name of the host with which we should conduct the experiment
63  * @to_port: port on which the peer process is listening for message
64  * @msg_size: Size of each message sent.
65  * @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, unsigned int 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   amok_bw_saturate_begin(request->host.name,request->host.port,
96                          request->msg_size, request->duration,
97                          NULL,NULL);
98   free(request->host.name);
99   free(request);
100   return 1;
101 }
102
103 /**
104  * @brief Start saturating between the current process and the designated peer
105  *
106  * Note that the only way to break this function before the end of the timeout
107  * is to have a remote host calling amok_bw_saturate_stop to this process.
108  */
109 void amok_bw_saturate_begin(const char* to_name,unsigned int to_port,
110                             unsigned int msg_size, unsigned int duration,
111                             /*out*/ double *elapsed_res, double *bw_res) {
112  
113   xbt_ex_t e;
114
115   gras_socket_t peer_cmd = gras_socket_client(to_name, to_port);
116   gras_socket_t measMaster=NULL,meas=NULL;
117   int port;
118
119   s_gras_msg_t msg_got;
120
121   unsigned int packet_sent=0;
122   double start,elapsed=-1; /* timer */
123   double bw;
124
125   volatile int saturate_further; /* boolean in the main loop */ 
126
127   /* Negociate the saturation with the peer */
128   sat_request_t request = xbt_new(s_sat_request_t,1);
129
130   DEBUG2("Begin to saturate to %s:%d",to_name,to_port);
131   memset(&msg_got,0,sizeof(msg_got));
132
133   for (port = 6000; port <= 10000 && measMaster == NULL; port++) {
134     TRY {
135       measMaster = gras_socket_server_ext(port,
136                                           0 /*bufsize: auto*/,
137                                           1 /*meas: true*/);
138     } CATCH(e) {
139       measMaster = NULL;
140       if (port < 10000)
141         xbt_ex_free(e);
142       else
143         RETHROW0("Error encountered while opening a measurement server socket: %s");
144     }
145   }
146
147   request->msg_size = msg_size;
148   request->duration = duration;
149   request->host.name = NULL;
150   request->host.port = port;
151
152   gras_msg_cb_ctx_t ctx = gras_msg_rpc_async_call(peer_cmd, 60, 
153                                   gras_msgtype_by_name("amok_bw_sat begin"),
154                                                   &request);
155   gras_socket_close(peer_cmd);
156   INFO1("Async called %d",port);
157
158   TRY {
159     meas = gras_socket_meas_accept(measMaster);
160     DEBUG0("saturation handshake answered");
161   } CATCH(e) {
162     gras_socket_close(measMaster);
163     RETHROW0("Error during saturation handshake: %s");
164   }  
165   INFO0("Accepted");
166   gras_msg_rpc_async_wait(ctx,&request);
167
168   /* Start experiment */
169   start=gras_os_time();
170
171   do {
172     /* do send it */
173     gras_socket_meas_send(meas,120,msg_size,msg_size);
174     packet_sent++;
175
176     /* Check whether someone asked us to stop saturation */
177     saturate_further = 0;
178     TRY {
179       gras_msg_wait_ext(0/*no wait*/,gras_msgtype_by_name("amok_bw_sat stop"),
180                         NULL /* accept any sender */,
181                         NULL, NULL, /* No specific filter */
182                         &msg_got);
183     } CATCH(e) {
184       if (e.category == timeout_error) {
185         saturate_further=1;
186         memset(&msg_got,0,sizeof(msg_got)); /* may be overprotectiv here */
187       }
188       xbt_ex_free(e);
189     }
190
191     /* Check whether the experiment has to be terminated by now */
192     elapsed=gras_os_time()-start;
193
194   } while (saturate_further && elapsed < duration);
195
196   bw = ((double)(packet_sent*msg_size)) / elapsed;
197
198   if (elapsed_res)
199     *elapsed_res = elapsed;
200   if (bw_res)
201     *bw_res = bw;
202
203   if (elapsed >= duration) {
204     INFO2("Saturation experiment terminated. Took %f sec (achieving %f kb/s)",
205           elapsed, bw/1024.0);
206   }
207
208   /* If someone stopped us, inform him about the achieved bandwidth */
209   if (msg_got.expe) {
210     bw_res_t answer = xbt_new(s_bw_res_t,1);
211     s_gras_msg_cb_ctx_t ctx;
212
213     answer->timestamp=gras_os_time();
214     answer->sec=elapsed;
215     answer->bw=bw;
216
217     ctx.expeditor = msg_got.expe;
218     ctx.ID = msg_got.ID;
219     ctx.msgtype = msg_got.type;
220
221     gras_msg_rpcreturn(60,&ctx,&answer);
222     free(answer);
223   }
224   
225   gras_socket_close(measMaster);
226   gras_socket_close(meas);
227 }
228
229 /* Sender will saturate link to us */
230 static int amok_bw_cb_sat_begin(gras_msg_cb_ctx_t ctx, void *payload){
231   gras_socket_t from=gras_msg_cb_ctx_from(ctx);
232   sat_request_t request=*(sat_request_t*)payload;
233   volatile int saturate_further = 1;
234   xbt_ex_t e;
235
236   gras_os_sleep(5); /* Wait for the accept */
237   gras_socket_t sock=gras_socket_client_ext( gras_socket_peer_name(from),
238                                              request->host.port,
239                                              0 /*bufsize: auto*/,
240                                              1 /*meas: true*/);
241   gras_msg_rpcreturn(60, ctx, NULL);
242
243   while (saturate_further) {
244     TRY {
245       gras_socket_meas_send(sock,120,request->msg_size,request->msg_size);
246     } CATCH(e) {
247       saturate_further = 0;
248       xbt_ex_free(e);
249     }
250   }
251   gras_socket_close(sock);
252   free(request);
253   return 1;
254 }
255
256 /**
257  * @brief Ask 'from_name:from_port' to stop any saturation experiments
258  * @from_name: Name of the host we are asking to do a experiment with (to_name:to_port)
259  * @from_port: port on which the process we are asking for an experiment is listening
260  * @time: the duration of the experiment
261  * @bw: the achieved bandwidth
262  *
263  */
264 void amok_bw_saturate_stop(const char* from_name,unsigned int from_port,
265                            /*out*/ unsigned int *time, unsigned int *bw) {
266
267   gras_socket_t sock = gras_socket_client(from_name,from_port);
268   gras_msg_rpccall(sock,60,gras_msgtype_by_name("amok_bw_sat stop"),NULL,NULL);
269   gras_socket_close(sock);
270 }
271
272
273 #if 0
274 int grasbw_cbSatStart(gras_msg_t *msg) {
275   gras_rawsock_t *raw;
276   gras_sock_t *sock;
277   xbt_error_t errcode;
278   double start; /* time to timeout */
279
280   /* specification of the test to run */
281   char* to_name=gras_msg_ctn(msg,0,0,msgHost_t).host;
282   unsigned int to_port=gras_msg_ctn(msg,0,0,msgHost_t).port;
283
284   unsigned int msgSize=gras_msg_ctn(msg,1,0,SatExp_t).msgSize;
285   unsigned int timeout=gras_msg_ctn(msg,1,0,SatExp_t).timeout;
286   unsigned int raw_port;
287
288   /* The request */
289   SatExp_t *request;
290   /* answer */
291   gras_msg_t *answer;
292
293
294   /* Negociate the saturation with the peer */
295   if((errcode=gras_sock_client_open(to_name,to_port,&sock))) {
296     fprintf(stderr,"cbSatStart(): Error %s encountered while contacting peer\n",
297             xbt_error_name(errcode));
298     grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
299                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
300                      errcode,"Cannot contact peer.\n");
301     return 1;
302   }
303   if (!(request=(SatExp_t *)malloc(sizeof(SatExp_t)))) {
304     fprintf(stderr,"cbSatStart(): Malloc error\n");
305     gras_sock_close(sock);
306     grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
307                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
308                      malloc_error,"Cannot build request.\n");
309     return 1;    
310   }
311
312   request->timeout=gras_msg_ctn(msg,1,0,SatExp_t).timeout;
313   request->msgSize=gras_msg_ctn(msg,1,0,SatExp_t).msgSize;
314
315   if ((errcode=gras_msg_new_and_send(sock,GRASMSG_SAT_BEGIN, 1, 
316                               request,1))) {
317     fprintf(stderr,"cbSatStart(): Error %s encountered while sending the request.\n",
318             xbt_error_name(errcode));
319     grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
320                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
321                      errcode,"Cannot send request.\n");
322     gras_sock_close(sock);
323     return 1;
324   }
325
326   if ((errcode=gras_msg_wait(120,GRASMSG_SAT_BEGUN,&answer))) {
327     fprintf(stderr,"cbSatStart(): Error %s encountered while waiting for the ACK.\n",
328             xbt_error_name(errcode));
329     gras_sock_close(sock);
330
331     grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
332                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
333                      errcode,
334                      "Cannot receive the ACK.\n");
335     return 1;
336   }
337
338   if((errcode=gras_msg_ctn(answer,0,0,msgError_t).errcode)) {
339     fprintf(stderr,"cbSatStart(): Peer reported error %s (%s).\n",
340             xbt_error_name(errcode),gras_msg_ctn(answer,0,0,msgError_t).errmsg);
341
342     grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
343                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
344                      errcode,
345                      "Peer repported '%s'.\n",gras_msg_ctn(answer,0,0,msgError_t).errmsg);
346     gras_msg_free(answer);
347     gras_sock_close(sock);
348     return 1;
349   }
350
351   raw_port=gras_msg_ctn(answer,1,0,SatExp_t).port;
352
353   if ((errcode=gras_rawsock_client_open(to_name,raw_port,msgSize,&raw))) {
354     fprintf(stderr,"cbSatStart(): Error %s while opening raw socket to %s:%d.\n",
355             xbt_error_name(errcode),to_name,gras_msg_ctn(answer,1,0,SatExp_t).port);
356
357     grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
358                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
359                      errcode,"Cannot open raw socket.\n");
360     gras_sock_close(sock);
361     return 1;
362   }
363
364   /* send a train of data before repporting that XP is started */
365   if ((errcode=gras_rawsock_send(raw,msgSize,msgSize))) {
366     fprintf(stderr,"cbSatStart: Failure %s during raw send\n",xbt_error_name(errcode));
367     grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
368                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
369                      errcode,"Cannot raw send.\n");
370     gras_sock_close(sock);
371     gras_rawsock_close(raw);
372     return 1;
373   }
374   
375   grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
376                    "cbSatStart: Severe error: Cannot send error status to requester!!\n",
377                    no_error,"Saturation started");
378   gras_msg_free(answer);
379   gras_msg_free(msg);
380   
381   /* Do the saturation until we get a SAT_STOP message or until we timeout the whole XP*/
382   start=gras_time();
383   while (gras_msg_wait(0,GRASMSG_SAT_STOP,&msg)==timeout_error && 
384          gras_time()-start < timeout) {
385     if ((errcode=gras_rawsock_send(raw,msgSize,msgSize))) {
386       fprintf(stderr,"cbSatStart: Failure %s during raw send\n",xbt_error_name(errcode));
387       /* our error message do not interess anyone. SAT_STOP will do nothing. */
388       gras_sock_close(sock);
389       gras_rawsock_close(raw);
390       return 1;
391     } 
392   }
393   if (gras_time()-start > timeout) {
394     fprintf(stderr,"The saturation experiment did timeout. Stop it NOW\n");
395     gras_sock_close(sock);
396     gras_rawsock_close(raw);
397     return 1;
398   }
399
400   /* Handle the SAT_STOP which broke the previous while */
401   
402   if ((errcode=gras_msg_new_and_send(sock, GRASMSG_SAT_END,0))) {
403     fprintf(stderr,"cbSatStart(): Cannot tell peer to stop saturation\n");
404
405     grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
406                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
407                      errcode,"Sending SAT_END to peer failed.\n");
408     gras_sock_close(sock);
409     gras_rawsock_close(raw);
410     return 1;
411   }
412   
413   if ((errcode=gras_msg_wait(60,GRASMSG_SAT_ENDED,&answer))) {
414     fprintf(stderr,"cbSatStart(): Peer didn't ACK the end\n");
415
416     grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
417                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
418                      errcode,"Receiving SAT_ENDED from peer failed.\n");
419     gras_sock_close(sock);
420     gras_rawsock_close(raw);
421     return 1;
422   }
423   grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
424                    "cbSatStart: Severe error: Cannot send error status to requester!!\n",
425                    no_error,"");
426
427   gras_sock_close(sock);
428   gras_rawsock_close(raw);
429   gras_msg_free(answer);
430   gras_msg_free(msg);
431
432   return 1;  
433 }
434
435 int grasbw_cbSatBegin(gras_msg_t *msg) {
436   gras_rawsock_t *raw;
437   xbt_error_t errcode;
438   double start; /* timer */
439   /* request */
440   unsigned int msgSize=gras_msg_ctn(msg,0,0,SatExp_t).msgSize;
441   unsigned int timeout=gras_msg_ctn(msg,0,0,SatExp_t).timeout;
442   /* answer */
443   SatExp_t *request;
444   msgError_t *error;
445
446   if (!(request=(SatExp_t*)malloc(sizeof(SatExp_t))) ||
447       !(error=(msgError_t *)malloc(sizeof(msgError_t)))) {
448     fprintf(stderr,"cbSatBegin(): Malloc error\n");
449     grasRepportError(msg->sock,GRASMSG_SAT_BEGUN,2,
450                      "cbSatBegin: Severe error: Cannot send error status to requester!!\n",
451                      malloc_error,"Malloc error");
452     return 1;
453   }
454
455   if ((errcode=gras_rawsock_server_open(6666,8000,msgSize,&raw))) { 
456     fprintf(stderr,"cbSatBegin(): Error %s encountered while opening a raw socket\n",
457             xbt_error_name(errcode));
458     grasRepportError(msg->sock,GRASMSG_SAT_BEGUN,2,
459                      "cbSatBegin: Severe error: Cannot send error status to requester!!\n",
460                      errcode,"Cannot open raw socket");
461     return 1;
462   }
463   request->port=gras_rawsock_get_peer_port(raw);
464   request->msgSize=msgSize;
465   error->errcode=no_error;
466   error->errmsg[0]='\0';
467   if ((errcode=gras_msg_new_and_send(msg->sock,GRASMSG_SAT_BEGUN,2,
468                               error,1,
469                               request,1))) {
470     fprintf(stderr,"cbSatBegin(): Error %s encountered while send ACK to peer\n",
471             xbt_error_name(errcode));
472     return 1;
473   }
474   gras_msg_free(msg);
475
476   start=gras_time();
477   while (gras_msg_wait(0,GRASMSG_SAT_END,&msg)==timeout_error &&
478          gras_time() - start < timeout) {
479     errcode=gras_rawsock_recv(raw,msgSize,msgSize,1);
480     if (errcode != timeout_error && errcode != no_error) {
481       fprintf(stderr,"cbSatBegin: Failure %s during raw receive\n",xbt_error_name(errcode));
482       /* our error message do not interess anyone. SAT_END will do nothing. */
483       /* (if timeout'ed, it may be because the sender stopped emission. so survive it) */
484       return 1;
485     } 
486   }
487   if (gras_time()-start > timeout) {
488     fprintf(stderr,"The saturation experiment did timeout. Stop it NOW.\n");
489     gras_rawsock_close(raw);
490     return 1;
491   }
492
493   grasRepportError(msg->sock,GRASMSG_SAT_ENDED,1,
494                    "cbSatBegin: Cannot send SAT_ENDED.\n",
495                    no_error,"");
496   gras_rawsock_close(raw);
497   gras_msg_free(msg);
498   return 1;
499 }
500
501 xbt_error_t grasbw_saturate_stop(const char* from_name,unsigned int from_port,
502                                  const char* to_name,unsigned int to_port) {
503   xbt_error_t errcode;
504   gras_sock_t *sock;
505   gras_msg_t *answer;
506
507   if((errcode=gras_sock_client_open(from_name,from_port,&sock))) {
508     fprintf(stderr,"saturate_stop(): Error %s encountered while contacting peer\n",
509             xbt_error_name(errcode));
510     return errcode;
511   }
512
513   if ((errcode=gras_msg_new_and_send(sock,GRASMSG_SAT_STOP,0))) {
514     fprintf(stderr,"saturate_stop(): Error %s encountered while sending request\n",
515             xbt_error_name(errcode));
516     gras_sock_close(sock);
517     return errcode;
518   }
519
520   if ((errcode=gras_msg_wait(120,GRASMSG_SAT_STOPPED,&answer))) {
521     fprintf(stderr,"saturate_stop(): Error %s encountered while receiving ACK\n",
522             xbt_error_name(errcode));
523     gras_sock_close(sock);
524     return errcode;
525   }
526
527   if((errcode=gras_msg_ctn(answer,0,0,msgError_t).errcode)) {
528     fprintf(stderr,"saturate_stop(): Peer reported error %s (%s).\n",
529             xbt_error_name(errcode),gras_msg_ctn(answer,0,0,msgError_t).errmsg);
530     gras_msg_free(answer);
531     gras_sock_close(sock);
532     return errcode;
533   }
534
535   gras_msg_free(answer);
536   gras_sock_close(sock);
537
538   return no_error;
539 }
540 #endif