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, sat_request_desc);
34 gras_msgtype_declare_rpc("amok_bw_sat stop", NULL, NULL);
37 void amok_bw_sat_join(void) {
38 gras_cb_register(gras_msgtype_by_name("amok_bw_sat start"),
39 &amok_bw_cb_sat_start);
40 gras_cb_register(gras_msgtype_by_name("amok_bw_sat begin"),
41 &amok_bw_cb_sat_begin);
43 void amok_bw_sat_leave(void) {
44 gras_cb_unregister(gras_msgtype_by_name("amok_bw_sat start"),
45 &amok_bw_cb_sat_start);
46 gras_cb_unregister(gras_msgtype_by_name("amok_bw_sat begin"),
47 &amok_bw_cb_sat_begin);
50 /* ***************************************************************************
52 * ***************************************************************************/
55 * @brief Ask 'from_name:from_port' to stop saturating going to to_name:to_name.
57 * @param from_name: Name of the host we are asking to do a experiment with (to_name:to_port)
58 * @param from_port: port on which the process we are asking for an experiment is listening
59 * (for message, do not give a raw socket here. The needed raw socket will be negociated
61 * @param to_name: Name of the host with which we should conduct the experiment
62 * @param to_port: port on which the peer process is listening for message
63 * @param msg_size: Size of each message sent.
64 * @param duration: How long in maximum should be the saturation.
66 * Ask the process 'from_name:from_port' to start to saturate the link between itself
67 * and to_name:to_name.
69 void amok_bw_saturate_start(const char* from_name,unsigned int from_port,
70 const char* to_name,unsigned int to_port,
71 unsigned int msg_size, double duration) {
74 sat_request_t request = xbt_new(s_sat_request_t,1);
76 sock = gras_socket_client(from_name,from_port);
78 request->host.name = (char*)to_name;
79 request->host.port = to_port;
81 request->duration=duration;
82 request->msg_size=msg_size;
85 gras_msg_rpccall(sock,60,gras_msgtype_by_name("amok_bw_sat start"),&request, NULL);
88 gras_socket_close(sock);
91 /* Asked to begin a saturation */
92 static int amok_bw_cb_sat_start(gras_msg_cb_ctx_t ctx, void *payload){
93 sat_request_t request = *(sat_request_t*)payload;
94 gras_msg_rpcreturn(60,ctx, NULL);
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, double duration,
111 /*out*/ double *elapsed_res, double *bw_res) {
115 gras_socket_t peer_cmd = gras_socket_client(to_name, to_port);
116 gras_msg_cb_ctx_t ctx;
120 s_gras_msg_t msg_got;
122 unsigned int packet_sent=0;
123 double start,elapsed=-1; /* timer */
126 volatile int saturate_further; /* boolean in the main loop */
128 /* Negociate the saturation with the peer */
129 sat_request_t request = xbt_new(s_sat_request_t,1);
131 DEBUG2("Begin to saturate to %s:%d",to_name,to_port);
132 memset(&msg_got,0,sizeof(msg_got));
134 request->msg_size = msg_size;
135 request->duration = duration;
136 request->host.name = NULL;
137 request->host.port = 0;
139 ctx = gras_msg_rpc_async_call(peer_cmd, 60,
140 gras_msgtype_by_name("amok_bw_sat begin"),
143 gras_msg_rpc_async_wait(ctx,&request);
144 meas=gras_socket_client_ext( to_name, request->host.port,
149 gras_socket_close(peer_cmd);
150 INFO2("Saturation from %s to %s started",gras_os_myname(),to_name);
152 /* Start experiment */
153 start=gras_os_time();
157 gras_socket_meas_send(meas,120,msg_size,msg_size);
160 /* Check whether someone asked us to stop saturation */
161 saturate_further = 0;
163 gras_msg_wait_ext(0/*no wait*/,gras_msgtype_by_name("amok_bw_sat stop"),
164 NULL /* accept any sender */,
165 NULL, NULL, /* No specific filter */
168 if (e.category == timeout_error) {
170 memset(&msg_got,0,sizeof(msg_got)); /* may be overprotectiv here */
175 /* Check whether the experiment has to be terminated by now */
176 elapsed=gras_os_time()-start;
177 VERB2("elapsed %f duration %f",elapsed, duration);
179 } while (saturate_further && elapsed < duration);
181 INFO2("Saturation from %s to %s stopped",gras_os_myname(),to_name);
182 bw = ((double)(packet_sent*msg_size)) / elapsed;
185 *elapsed_res = elapsed;
189 if (elapsed >= duration) {
190 INFO2("Saturation experiment terminated. Took %f sec (achieving %f kb/s)",
194 /* If someone stopped us, inform him about the achieved bandwidth */
196 bw_res_t answer = xbt_new(s_bw_res_t,1);
197 s_gras_msg_cb_ctx_t ctx;
199 answer->timestamp=gras_os_time();
203 ctx.expeditor = msg_got.expe;
205 ctx.msgtype = msg_got.type;
207 gras_msg_rpcreturn(60,&ctx,&answer);
211 gras_socket_close(meas);
214 /* Sender will saturate link to us */
215 static int amok_bw_cb_sat_begin(gras_msg_cb_ctx_t ctx, void *payload){
216 sat_request_t request=*(sat_request_t*)payload;
217 sat_request_t answer=xbt_new0(s_sat_request_t,1);
218 volatile int saturate_further = 1;
220 gras_socket_t measMaster=NULL,meas=NULL;
223 while (port <= 10000 && measMaster == NULL) {
225 measMaster = gras_socket_server_ext(port,
233 RETHROW0("Error encountered while opening a measurement server socket: %s");
235 if (measMaster == NULL)
236 port++; /* prepare for a new loop */
238 answer->host.port=port;
240 gras_msg_rpcreturn(60, ctx, &answer);
243 gras_os_sleep(5); /* Wait for the accept */
246 meas = gras_socket_meas_accept(measMaster);
247 DEBUG0("saturation handshake answered");
249 gras_socket_close(measMaster);
250 RETHROW0("Error during saturation handshake: %s");
253 while (saturate_further) {
255 gras_socket_meas_recv(meas,120,request->msg_size,request->msg_size);
257 saturate_further = 0;
261 INFO1("Saturation stopped on %s",gras_os_myname());
262 gras_socket_close(meas);
263 if (gras_if_RL()) /* On SG, accepted=master */
264 gras_socket_close(measMaster);
270 * @brief Ask 'from_name:from_port' to stop any saturation experiments
271 * @param from_name: Name of the host we are asking to do a experiment with (to_name:to_port)
272 * @param from_port: port on which the process we are asking for an experiment is listening
273 * @param time: the duration of the experiment
274 * @param bw: the achieved bandwidth
277 void amok_bw_saturate_stop(const char* from_name,unsigned int from_port,
278 /*out*/ unsigned int *time, unsigned int *bw) {
280 gras_socket_t sock = gras_socket_client(from_name,from_port);
281 gras_msg_rpccall(sock,60,gras_msgtype_by_name("amok_bw_sat stop"),NULL,NULL);
282 gras_socket_close(sock);
287 int grasbw_cbSatStart(gras_msg_t *msg) {
291 double start; /* time to timeout */
293 /* specification of the test to run */
294 char* to_name=gras_msg_ctn(msg,0,0,msgHost_t).host;
295 unsigned int to_port=gras_msg_ctn(msg,0,0,msgHost_t).port;
297 unsigned int msgSize=gras_msg_ctn(msg,1,0,SatExp_t).msgSize;
298 unsigned int timeout=gras_msg_ctn(msg,1,0,SatExp_t).timeout;
299 unsigned int raw_port;
307 /* Negociate the saturation with the peer */
308 if((errcode=gras_sock_client_open(to_name,to_port,&sock))) {
309 fprintf(stderr,"cbSatStart(): Error %s encountered while contacting peer\n",
310 xbt_error_name(errcode));
311 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
312 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
313 errcode,"Cannot contact peer.\n");
316 if (!(request=(SatExp_t *)malloc(sizeof(SatExp_t)))) {
317 fprintf(stderr,"cbSatStart(): Malloc error\n");
318 gras_sock_close(sock);
319 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
320 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
321 malloc_error,"Cannot build request.\n");
325 request->timeout=gras_msg_ctn(msg,1,0,SatExp_t).timeout;
326 request->msgSize=gras_msg_ctn(msg,1,0,SatExp_t).msgSize;
328 if ((errcode=gras_msg_new_and_send(sock,GRASMSG_SAT_BEGIN, 1,
330 fprintf(stderr,"cbSatStart(): Error %s encountered while sending the request.\n",
331 xbt_error_name(errcode));
332 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
333 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
334 errcode,"Cannot send request.\n");
335 gras_sock_close(sock);
339 if ((errcode=gras_msg_wait(120,GRASMSG_SAT_BEGUN,&answer))) {
340 fprintf(stderr,"cbSatStart(): Error %s encountered while waiting for the ACK.\n",
341 xbt_error_name(errcode));
342 gras_sock_close(sock);
344 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
345 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
347 "Cannot receive the ACK.\n");
351 if((errcode=gras_msg_ctn(answer,0,0,msgError_t).errcode)) {
352 fprintf(stderr,"cbSatStart(): Peer reported error %s (%s).\n",
353 xbt_error_name(errcode),gras_msg_ctn(answer,0,0,msgError_t).errmsg);
355 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
356 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
358 "Peer repported '%s'.\n",gras_msg_ctn(answer,0,0,msgError_t).errmsg);
359 gras_msg_free(answer);
360 gras_sock_close(sock);
364 raw_port=gras_msg_ctn(answer,1,0,SatExp_t).port;
366 if ((errcode=gras_rawsock_client_open(to_name,raw_port,msgSize,&raw))) {
367 fprintf(stderr,"cbSatStart(): Error %s while opening raw socket to %s:%d.\n",
368 xbt_error_name(errcode),to_name,gras_msg_ctn(answer,1,0,SatExp_t).port);
370 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
371 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
372 errcode,"Cannot open raw socket.\n");
373 gras_sock_close(sock);
377 /* send a train of data before repporting that XP is started */
378 if ((errcode=gras_rawsock_send(raw,msgSize,msgSize))) {
379 fprintf(stderr,"cbSatStart: Failure %s during raw send\n",xbt_error_name(errcode));
380 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
381 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
382 errcode,"Cannot raw send.\n");
383 gras_sock_close(sock);
384 gras_rawsock_close(raw);
388 grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
389 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
390 no_error,"Saturation started");
391 gras_msg_free(answer);
394 /* Do the saturation until we get a SAT_STOP message or until we timeout the whole XP*/
396 while (gras_msg_wait(0,GRASMSG_SAT_STOP,&msg)==timeout_error &&
397 gras_time()-start < timeout) {
398 if ((errcode=gras_rawsock_send(raw,msgSize,msgSize))) {
399 fprintf(stderr,"cbSatStart: Failure %s during raw send\n",xbt_error_name(errcode));
400 /* our error message do not interess anyone. SAT_STOP will do nothing. */
401 gras_sock_close(sock);
402 gras_rawsock_close(raw);
406 if (gras_time()-start > timeout) {
407 fprintf(stderr,"The saturation experiment did timeout. Stop it NOW\n");
408 gras_sock_close(sock);
409 gras_rawsock_close(raw);
413 /* Handle the SAT_STOP which broke the previous while */
415 if ((errcode=gras_msg_new_and_send(sock, GRASMSG_SAT_END,0))) {
416 fprintf(stderr,"cbSatStart(): Cannot tell peer to stop saturation\n");
418 grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
419 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
420 errcode,"Sending SAT_END to peer failed.\n");
421 gras_sock_close(sock);
422 gras_rawsock_close(raw);
426 if ((errcode=gras_msg_wait(60,GRASMSG_SAT_ENDED,&answer))) {
427 fprintf(stderr,"cbSatStart(): Peer didn't ACK the end\n");
429 grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
430 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
431 errcode,"Receiving SAT_ENDED from peer failed.\n");
432 gras_sock_close(sock);
433 gras_rawsock_close(raw);
436 grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
437 "cbSatStart: Severe error: Cannot send error status to requester!!\n",
440 gras_sock_close(sock);
441 gras_rawsock_close(raw);
442 gras_msg_free(answer);
448 int grasbw_cbSatBegin(gras_msg_t *msg) {
451 double start; /* timer */
453 unsigned int msgSize=gras_msg_ctn(msg,0,0,SatExp_t).msgSize;
454 unsigned int timeout=gras_msg_ctn(msg,0,0,SatExp_t).timeout;
459 if (!(request=(SatExp_t*)malloc(sizeof(SatExp_t))) ||
460 !(error=(msgError_t *)malloc(sizeof(msgError_t)))) {
461 fprintf(stderr,"cbSatBegin(): Malloc error\n");
462 grasRepportError(msg->sock,GRASMSG_SAT_BEGUN,2,
463 "cbSatBegin: Severe error: Cannot send error status to requester!!\n",
464 malloc_error,"Malloc error");
468 if ((errcode=gras_rawsock_server_open(6666,8000,msgSize,&raw))) {
469 fprintf(stderr,"cbSatBegin(): Error %s encountered while opening a raw socket\n",
470 xbt_error_name(errcode));
471 grasRepportError(msg->sock,GRASMSG_SAT_BEGUN,2,
472 "cbSatBegin: Severe error: Cannot send error status to requester!!\n",
473 errcode,"Cannot open raw socket");
476 request->port=gras_rawsock_get_peer_port(raw);
477 request->msgSize=msgSize;
478 error->errcode=no_error;
479 error->errmsg[0]='\0';
480 if ((errcode=gras_msg_new_and_send(msg->sock,GRASMSG_SAT_BEGUN,2,
483 fprintf(stderr,"cbSatBegin(): Error %s encountered while send ACK to peer\n",
484 xbt_error_name(errcode));
490 while (gras_msg_wait(0,GRASMSG_SAT_END,&msg)==timeout_error &&
491 gras_time() - start < timeout) {
492 errcode=gras_rawsock_recv(raw,msgSize,msgSize,1);
493 if (errcode != timeout_error && errcode != no_error) {
494 fprintf(stderr,"cbSatBegin: Failure %s during raw receive\n",xbt_error_name(errcode));
495 /* our error message do not interess anyone. SAT_END will do nothing. */
496 /* (if timeout'ed, it may be because the sender stopped emission. so survive it) */
500 if (gras_time()-start > timeout) {
501 fprintf(stderr,"The saturation experiment did timeout. Stop it NOW.\n");
502 gras_rawsock_close(raw);
506 grasRepportError(msg->sock,GRASMSG_SAT_ENDED,1,
507 "cbSatBegin: Cannot send SAT_ENDED.\n",
509 gras_rawsock_close(raw);
514 xbt_error_t grasbw_saturate_stop(const char* from_name,unsigned int from_port,
515 const char* to_name,unsigned int to_port) {
520 if((errcode=gras_sock_client_open(from_name,from_port,&sock))) {
521 fprintf(stderr,"saturate_stop(): Error %s encountered while contacting peer\n",
522 xbt_error_name(errcode));
526 if ((errcode=gras_msg_new_and_send(sock,GRASMSG_SAT_STOP,0))) {
527 fprintf(stderr,"saturate_stop(): Error %s encountered while sending request\n",
528 xbt_error_name(errcode));
529 gras_sock_close(sock);
533 if ((errcode=gras_msg_wait(120,GRASMSG_SAT_STOPPED,&answer))) {
534 fprintf(stderr,"saturate_stop(): Error %s encountered while receiving ACK\n",
535 xbt_error_name(errcode));
536 gras_sock_close(sock);
540 if((errcode=gras_msg_ctn(answer,0,0,msgError_t).errcode)) {
541 fprintf(stderr,"saturate_stop(): Peer reported error %s (%s).\n",
542 xbt_error_name(errcode),gras_msg_ctn(answer,0,0,msgError_t).errmsg);
543 gras_msg_free(answer);
544 gras_sock_close(sock);
548 gras_msg_free(answer);
549 gras_sock_close(sock);