Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
some more work on the bandwidth test module (ie, root of ALNeM) modernization. Still...
[simgrid.git] / src / amok / Bandwidth / saturate.c
1 /* $Id$ */
2
3 /* amok_saturate - Link saturating facilities (for ALNeM's BW testing)      */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2003, 2004 the OURAGAN project.                            */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include "amok/Bandwidth/bandwidth_private.h"
12
13 GRAS_LOG_EXTERNAL_CATEGORY(bw);
14 GRAS_LOG_DEFAULT_CATEGORY(bw);
15
16 #if 0
17 /* ***************************************************************************
18  * Link saturation
19  * ***************************************************************************/
20
21 gras_error_t grasbw_saturate_start(const char* from_name,unsigned int from_port,
22                                    const char* to_name,unsigned int to_port,
23                                    unsigned int msgSize, unsigned int timeout) {
24   gras_sock_t *sock;
25   gras_error_t errcode;
26   /* The request */
27   SatExp_t *request;
28   msgHost_t *target;
29   /* answer */
30   gras_msg_t *answer;
31
32   if((errcode=gras_sock_client_open(from_name,from_port,&sock))) {
33     fprintf(stderr,"%s:%d:saturate_start(): Error %s encountered while contacting peer\n",
34             __FILE__,__LINE__,gras_error_name(errcode));
35     return errcode;
36   }
37   if (!(request=(SatExp_t *)malloc(sizeof(SatExp_t))) ||
38       !(target=(msgHost_t*)malloc(sizeof(msgHost_t)))) {
39     fprintf(stderr,"%s:%d:saturate_start(): Malloc error\n",__FILE__,__LINE__);
40     gras_sock_close(sock);
41     return malloc_error;    
42   }
43
44   request->timeout=timeout;
45   request->msgSize=msgSize;
46
47   strcpy(target->host,to_name);
48   target->port=to_port;
49
50   if ((errcode=gras_msg_new_and_send(sock,GRASMSG_SAT_START, 2, 
51                               target,1,
52                               request,1))) {
53     fprintf(stderr,"%s:%d:saturate_start(): Error %s encountered while sending the request.\n",
54             __FILE__,__LINE__,gras_error_name(errcode));
55     gras_sock_close(sock);
56     return errcode;
57   }
58   if ((errcode=gras_msg_wait(120,GRASMSG_SAT_STARTED,&answer))) {
59     fprintf(stderr,"%s:%d:saturate_start(): Error %s encountered while waiting for the ACK.\n",
60             __FILE__,__LINE__,gras_error_name(errcode));
61     gras_sock_close(sock);
62     return errcode;
63   }
64
65   if((errcode=gras_msg_ctn(answer,0,0,msgError_t).errcode)) {
66     fprintf(stderr,"%s:%d:saturate_start(): Peer reported error %s (%s).\n",
67             __FILE__,__LINE__,gras_error_name(errcode),gras_msg_ctn(answer,0,0,msgError_t).errmsg);
68     gras_msg_free(answer);
69     gras_sock_close(sock);
70     return errcode;
71   }
72
73   gras_msg_free(answer);
74   gras_sock_close(sock);
75   return no_error;
76 }
77
78 int grasbw_cbSatStart(gras_msg_t *msg) {
79   gras_rawsock_t *raw;
80   gras_sock_t *sock;
81   gras_error_t errcode;
82   double start; /* time to timeout */
83
84   /* specification of the test to run */
85   char* to_name=gras_msg_ctn(msg,0,0,msgHost_t).host;
86   unsigned int to_port=gras_msg_ctn(msg,0,0,msgHost_t).port;
87
88   unsigned int msgSize=gras_msg_ctn(msg,1,0,SatExp_t).msgSize;
89   unsigned int timeout=gras_msg_ctn(msg,1,0,SatExp_t).timeout;
90   unsigned int raw_port;
91
92   /* The request */
93   SatExp_t *request;
94   /* answer */
95   gras_msg_t *answer;
96
97   /*
98   fprintf(stderr,"grasbw_cbSatStart(sd=%p)\n",msg->sock);
99   fprintf(stderr,"(server=%d,raw=%d,fromPID=%d,toPID=%d,toHost=%p,toPort=%d,toChan=%d)\n",
100           msg->sock->server_sock,msg->sock->raw_sock,msg->sock->from_PID,
101           msg->sock->to_PID,msg->sock->to_host,msg->sock->to_port,msg->sock->to_chan);
102   */
103
104   /* Negociate the saturation with the peer */
105   if((errcode=gras_sock_client_open(to_name,to_port,&sock))) {
106     fprintf(stderr,"cbSatStart(): Error %s encountered while contacting peer\n",
107             gras_error_name(errcode));
108     grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
109                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
110                      errcode,"Cannot contact peer.\n");
111     return 1;
112   }
113   if (!(request=(SatExp_t *)malloc(sizeof(SatExp_t)))) {
114     fprintf(stderr,"cbSatStart(): Malloc error\n");
115     gras_sock_close(sock);
116     grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
117                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
118                      malloc_error,"Cannot build request.\n");
119     return 1;    
120   }
121
122   request->timeout=gras_msg_ctn(msg,1,0,SatExp_t).timeout;
123   request->msgSize=gras_msg_ctn(msg,1,0,SatExp_t).msgSize;
124
125   if ((errcode=gras_msg_new_and_send(sock,GRASMSG_SAT_BEGIN, 1, 
126                               request,1))) {
127     fprintf(stderr,"cbSatStart(): Error %s encountered while sending the request.\n",
128             gras_error_name(errcode));
129     grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
130                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
131                      errcode,"Cannot send request.\n");
132     gras_sock_close(sock);
133     return 1;
134   }
135
136   if ((errcode=gras_msg_wait(120,GRASMSG_SAT_BEGUN,&answer))) {
137     fprintf(stderr,"cbSatStart(): Error %s encountered while waiting for the ACK.\n",
138             gras_error_name(errcode));
139     gras_sock_close(sock);
140
141     grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
142                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
143                      errcode,
144                      "Cannot receive the ACK.\n");
145     return 1;
146   }
147
148   if((errcode=gras_msg_ctn(answer,0,0,msgError_t).errcode)) {
149     fprintf(stderr,"cbSatStart(): Peer reported error %s (%s).\n",
150             gras_error_name(errcode),gras_msg_ctn(answer,0,0,msgError_t).errmsg);
151
152     grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
153                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
154                      errcode,
155                      "Peer repported '%s'.\n",gras_msg_ctn(answer,0,0,msgError_t).errmsg);
156     gras_msg_free(answer);
157     gras_sock_close(sock);
158     return 1;
159   }
160
161   raw_port=gras_msg_ctn(answer,1,0,SatExp_t).port;
162
163   if ((errcode=gras_rawsock_client_open(to_name,raw_port,msgSize,&raw))) {
164     fprintf(stderr,"cbSatStart(): Error %s while opening raw socket to %s:%d.\n",
165             gras_error_name(errcode),to_name,gras_msg_ctn(answer,1,0,SatExp_t).port);
166
167     grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
168                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
169                      errcode,"Cannot open raw socket.\n");
170     gras_sock_close(sock);
171     return 1;
172   }
173
174   /* send a train of data before repporting that XP is started */
175   if ((errcode=gras_rawsock_send(raw,msgSize,msgSize))) {
176     fprintf(stderr,"cbSatStart: Failure %s during raw send\n",gras_error_name(errcode));
177     grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
178                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
179                      errcode,"Cannot raw send.\n");
180     gras_sock_close(sock);
181     gras_rawsock_close(raw);
182     return 1;
183   }
184   
185   grasRepportError(msg->sock,GRASMSG_SAT_STARTED,1,
186                    "cbSatStart: Severe error: Cannot send error status to requester!!\n",
187                    no_error,"Saturation started");
188   gras_msg_free(answer);
189   gras_msg_free(msg);
190   
191   /* Do the saturation until we get a SAT_STOP message or until we timeout the whole XP*/
192   start=gras_time();
193   while (gras_msg_wait(0,GRASMSG_SAT_STOP,&msg)==timeout_error && 
194          gras_time()-start < timeout) {
195     if ((errcode=gras_rawsock_send(raw,msgSize,msgSize))) {
196       fprintf(stderr,"cbSatStart: Failure %s during raw send\n",gras_error_name(errcode));
197       /* our error message do not interess anyone. SAT_STOP will do nothing. */
198       gras_sock_close(sock);
199       gras_rawsock_close(raw);
200       return 1;
201     } 
202   }
203   if (gras_time()-start > timeout) {
204     fprintf(stderr,"The saturation experiment did timeout. Stop it NOW\n");
205     gras_sock_close(sock);
206     gras_rawsock_close(raw);
207     return 1;
208   }
209
210   /* Handle the SAT_STOP which broke the previous while */
211   
212   if ((errcode=gras_msg_new_and_send(sock, GRASMSG_SAT_END,0))) {
213     fprintf(stderr,"cbSatStart(): Cannot tell peer to stop saturation\n");
214
215     grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
216                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
217                      errcode,"Sending SAT_END to peer failed.\n");
218     gras_sock_close(sock);
219     gras_rawsock_close(raw);
220     return 1;
221   }
222   
223   if ((errcode=gras_msg_wait(60,GRASMSG_SAT_ENDED,&answer))) {
224     fprintf(stderr,"cbSatStart(): Peer didn't ACK the end\n");
225
226     grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
227                      "cbSatStart: Severe error: Cannot send error status to requester!!\n",
228                      errcode,"Receiving SAT_ENDED from peer failed.\n");
229     gras_sock_close(sock);
230     gras_rawsock_close(raw);
231     return 1;
232   }
233   grasRepportError(msg->sock,GRASMSG_SAT_STOPPED,1,
234                    "cbSatStart: Severe error: Cannot send error status to requester!!\n",
235                    no_error,"");
236
237   gras_sock_close(sock);
238   gras_rawsock_close(raw);
239   gras_msg_free(answer);
240   gras_msg_free(msg);
241
242   return 1;  
243 }
244
245 int grasbw_cbSatBegin(gras_msg_t *msg) {
246   gras_rawsock_t *raw;
247   gras_error_t errcode;
248   double start; /* timer */
249   /* request */
250   unsigned int msgSize=gras_msg_ctn(msg,0,0,SatExp_t).msgSize;
251   unsigned int timeout=gras_msg_ctn(msg,0,0,SatExp_t).timeout;
252   /* answer */
253   SatExp_t *request;
254   msgError_t *error;
255
256   if (!(request=(SatExp_t*)malloc(sizeof(SatExp_t))) ||
257       !(error=(msgError_t *)malloc(sizeof(msgError_t)))) {
258     fprintf(stderr,"cbSatBegin(): Malloc error\n");
259     grasRepportError(msg->sock,GRASMSG_SAT_BEGUN,2,
260                      "cbSatBegin: Severe error: Cannot send error status to requester!!\n",
261                      malloc_error,"Malloc error");
262     return 1;
263   }
264
265   if ((errcode=gras_rawsock_server_open(6666,8000,msgSize,&raw))) { 
266     fprintf(stderr,"cbSatBegin(): Error %s encountered while opening a raw socket\n",
267             gras_error_name(errcode));
268     grasRepportError(msg->sock,GRASMSG_SAT_BEGUN,2,
269                      "cbSatBegin: Severe error: Cannot send error status to requester!!\n",
270                      errcode,"Cannot open raw socket");
271     return 1;
272   }
273   request->port=gras_rawsock_get_peer_port(raw);
274   request->msgSize=msgSize;
275   error->errcode=no_error;
276   error->errmsg[0]='\0';
277   if ((errcode=gras_msg_new_and_send(msg->sock,GRASMSG_SAT_BEGUN,2,
278                               error,1,
279                               request,1))) {
280     fprintf(stderr,"cbSatBegin(): Error %s encountered while send ACK to peer\n",
281             gras_error_name(errcode));
282     return 1;
283   }
284   gras_msg_free(msg);
285
286   start=gras_time();
287   while (gras_msg_wait(0,GRASMSG_SAT_END,&msg)==timeout_error &&
288          gras_time() - start < timeout) {
289     errcode=gras_rawsock_recv(raw,msgSize,msgSize,1);
290     if (errcode != timeout_error && errcode != no_error) {
291       fprintf(stderr,"cbSatBegin: Failure %s during raw receive\n",gras_error_name(errcode));
292       /* our error message do not interess anyone. SAT_END will do nothing. */
293       /* (if timeout'ed, it may be because the sender stopped emission. so survive it) */
294       return 1;
295     } 
296   }
297   if (gras_time()-start > timeout) {
298     fprintf(stderr,"The saturation experiment did timeout. Stop it NOW.\n");
299     gras_rawsock_close(raw);
300     return 1;
301   }
302
303   grasRepportError(msg->sock,GRASMSG_SAT_ENDED,1,
304                    "cbSatBegin: Cannot send SAT_ENDED.\n",
305                    no_error,"");
306   gras_rawsock_close(raw);
307   gras_msg_free(msg);
308   return 1;
309 }
310
311 gras_error_t grasbw_saturate_stop(const char* from_name,unsigned int from_port,
312                                  const char* to_name,unsigned int to_port) {
313   gras_error_t errcode;
314   gras_sock_t *sock;
315   gras_msg_t *answer;
316
317   if((errcode=gras_sock_client_open(from_name,from_port,&sock))) {
318     fprintf(stderr,"saturate_stop(): Error %s encountered while contacting peer\n",
319             gras_error_name(errcode));
320     return errcode;
321   }
322
323   if ((errcode=gras_msg_new_and_send(sock,GRASMSG_SAT_STOP,0))) {
324     fprintf(stderr,"saturate_stop(): Error %s encountered while sending request\n",
325             gras_error_name(errcode));
326     gras_sock_close(sock);
327     return errcode;
328   }
329
330   if ((errcode=gras_msg_wait(120,GRASMSG_SAT_STOPPED,&answer))) {
331     fprintf(stderr,"saturate_stop(): Error %s encountered while receiving ACK\n",
332             gras_error_name(errcode));
333     gras_sock_close(sock);
334     return errcode;
335   }
336
337   if((errcode=gras_msg_ctn(answer,0,0,msgError_t).errcode)) {
338     fprintf(stderr,"saturate_stop(): Peer reported error %s (%s).\n",
339             gras_error_name(errcode),gras_msg_ctn(answer,0,0,msgError_t).errmsg);
340     gras_msg_free(answer);
341     gras_sock_close(sock);
342     return errcode;
343   }
344
345   gras_msg_free(answer);
346   gras_sock_close(sock);
347
348   return no_error;
349 }
350 #endif