3 /* amok_saturate - Link saturating facilities (for ALNeM's BW testing) */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
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. */
10 #include "amok/Bandwidth/bandwidth_private.h"
11 #include "gras/Msg/msg_private.h" /* FIXME: This mucks with contextes to answer RPC directly */
13 XBT_LOG_EXTERNAL_CATEGORY(bw);
14 XBT_LOG_DEFAULT_CATEGORY(bw);
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);
20 void amok_bw_sat_init(void) {
21 gras_datadesc_type_t sat_request_desc;
22 /* Build the saturation datatype descriptions */
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);
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);
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);
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);
51 /* ***************************************************************************
53 * ***************************************************************************/
56 * @brief Ask 'from_name:from_port' to stop saturating going to to_name:to_name.
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
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.
67 * Ask the process 'from_name:from_port' to start to saturate the link between itself
68 * and to_name:to_name.
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) {
75 sat_request_t request = xbt_new(s_sat_request_t,1);
77 sock = gras_socket_client(from_name,from_port);
79 request->host.name = (char*)to_name;
80 request->host.port = to_port;
82 request->duration=duration;
83 request->msg_size=msg_size;
86 gras_msg_rpccall(sock,60,gras_msgtype_by_name("amok_bw_sat start"),&request, NULL);
89 gras_socket_close(sock);
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,
98 free(request->host.name);
104 * @brief Start saturating between the current process and the designated peer
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.
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) {
115 gras_socket_t peer_cmd = gras_socket_client(to_name, to_port);
116 gras_socket_t measMaster=NULL,meas=NULL;
119 s_gras_msg_t msg_got;
121 unsigned int packet_sent=0;
122 double start,elapsed=-1; /* timer */
125 volatile int saturate_further; /* boolean in the main loop */
127 /* Negociate the saturation with the peer */
128 sat_request_t request = xbt_new(s_sat_request_t,1);
130 DEBUG2("Begin to saturate to %s:%d",to_name,to_port);
131 memset(&msg_got,0,sizeof(msg_got));
133 for (port = 6000; port <= 10000 && measMaster == NULL; port++) {
135 measMaster = gras_socket_server_ext(port,
143 RETHROW0("Error encountered while opening a measurement server socket: %s");
147 request->msg_size = msg_size;
148 request->duration = duration;
149 request->host.name = NULL;
150 request->host.port = port;
152 gras_msg_cb_ctx_t ctx = gras_msg_rpc_async_call(peer_cmd, 60,
153 gras_msgtype_by_name("amok_bw_sat begin"),
155 gras_socket_close(peer_cmd);
156 INFO1("Async called %d",port);
159 meas = gras_socket_meas_accept(measMaster);
160 DEBUG0("saturation handshake answered");
162 gras_socket_close(measMaster);
163 RETHROW0("Error during saturation handshake: %s");
166 gras_msg_rpc_async_wait(ctx,&request);
168 /* Start experiment */
169 start=gras_os_time();
173 gras_socket_meas_send(meas,120,msg_size,msg_size);
176 /* Check whether someone asked us to stop saturation */
177 saturate_further = 0;
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 */
184 if (e.category == timeout_error) {
186 memset(&msg_got,0,sizeof(msg_got)); /* may be overprotectiv here */
191 /* Check whether the experiment has to be terminated by now */
192 elapsed=gras_os_time()-start;
194 } while (saturate_further && elapsed < duration);
196 bw = ((double)(packet_sent*msg_size)) / elapsed;
199 *elapsed_res = elapsed;
203 if (elapsed >= duration) {
204 INFO2("Saturation experiment terminated. Took %f sec (achieving %f kb/s)",
208 /* If someone stopped us, inform him about the achieved bandwidth */
210 bw_res_t answer = xbt_new(s_bw_res_t,1);
211 s_gras_msg_cb_ctx_t ctx;
213 answer->timestamp=gras_os_time();
217 ctx.expeditor = msg_got.expe;
219 ctx.msgtype = msg_got.type;
221 gras_msg_rpcreturn(60,&ctx,&answer);
225 gras_socket_close(measMaster);
226 gras_socket_close(meas);
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;
236 gras_os_sleep(5); /* Wait for the accept */
237 gras_socket_t sock=gras_socket_client_ext( gras_socket_peer_name(from),
241 gras_msg_rpcreturn(60, ctx, NULL);
243 while (saturate_further) {
245 gras_socket_meas_send(sock,120,request->msg_size,request->msg_size);
247 saturate_further = 0;
251 gras_socket_close(sock);
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
264 void amok_bw_saturate_stop(const char* from_name,unsigned int from_port,
265 /*out*/ unsigned int *time, unsigned int *bw) {
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);
274 int grasbw_cbSatStart(gras_msg_t *msg) {
278 double start; /* time to timeout */
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;
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;
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");
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");
312 request->timeout=gras_msg_ctn(msg,1,0,SatExp_t).timeout;
313 request->msgSize=gras_msg_ctn(msg,1,0,SatExp_t).msgSize;
315 if ((errcode=gras_msg_new_and_send(sock,GRASMSG_SAT_BEGIN, 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);
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);
331 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
332 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
334 "Cannot receive the ACK.\n");
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);
342 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
343 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
345 "Peer repported '%s'.\n",gras_msg_ctn(answer,0,0,msgError_t).errmsg);
346 gras_msg_free(answer);
347 gras_sock_close(sock);
351 raw_port=gras_msg_ctn(answer,1,0,SatExp_t).port;
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);
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);
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);
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);
381 /* Do the saturation until we get a SAT_STOP message or until we timeout the whole XP*/
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);
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);
400 /* Handle the SAT_STOP which broke the previous while */
402 if ((errcode=gras_msg_new_and_send(sock, GRASMSG_SAT_END,0))) {
403 fprintf(stderr,"cbSatStart(): Cannot tell peer to stop saturation\n");
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);
413 if ((errcode=gras_msg_wait(60,GRASMSG_SAT_ENDED,&answer))) {
414 fprintf(stderr,"cbSatStart(): Peer didn't ACK the end\n");
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);
423 grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
424 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
427 gras_sock_close(sock);
428 gras_rawsock_close(raw);
429 gras_msg_free(answer);
435 int grasbw_cbSatBegin(gras_msg_t *msg) {
438 double start; /* timer */
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;
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");
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");
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,
470 fprintf(stderr,"cbSatBegin(): Error %s encountered while send ACK to peer\n",
471 xbt_error_name(errcode));
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) */
487 if (gras_time()-start > timeout) {
488 fprintf(stderr,"The saturation experiment did timeout. Stop it NOW.\n");
489 gras_rawsock_close(raw);
493 grasRepportError(msg->sock,GRASMSG_SAT_ENDED,1,
494 "cbSatBegin: Cannot send SAT_ENDED.\n",
496 gras_rawsock_close(raw);
501 xbt_error_t grasbw_saturate_stop(const char* from_name,unsigned int from_port,
502 const char* to_name,unsigned int to_port) {
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));
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);
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);
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);
535 gras_msg_free(answer);
536 gras_sock_close(sock);