Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8e452502bfa6ceb4e3f5eb1763be35f037f51b01
[simgrid.git] / examples / amok / saturate / saturate.c
1 /* $Id$ */
2
3 /* saturate - link saturation demo of AMOK features                         */
4
5 /* Copyright (c) 2003-6 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 <stdio.h>
11 #include <stdlib.h>
12 #include <signal.h>
13 #include <time.h>
14
15 #include "gras.h"
16 #include "amok/bandwidth.h"
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(saturate,"Messages specific to this example");
19
20 /* **********************************************************************
21  * Sensor code
22  * **********************************************************************/
23
24 /* Global private data */
25 typedef struct {
26   gras_socket_t sock;
27   int done;
28 } sensor_data_t;
29
30 /* Function prototypes */
31 int sensor (int argc,char *argv[]);
32
33 static int sensor_cb_kill(gras_msg_cb_ctx_t ctx,
34                           void             *payload_data) {
35
36   sensor_data_t *g=gras_userdata_get();
37   g->done = 1;
38   INFO0("Killed");
39   return 1;
40 }
41 int sensor (int argc,char *argv[]) {
42   sensor_data_t *g;
43   xbt_ex_t e;
44
45   gras_init(&argc,argv);
46   amok_bw_init();
47
48   g=gras_userdata_new(sensor_data_t);  
49   g->sock = gras_socket_server(atoi(argv[1]));
50
51   g->done = 0;
52   gras_msgtype_declare("kill",NULL);
53   gras_cb_register(gras_msgtype_by_name("kill"),&sensor_cb_kill);
54
55   while (!g->done) {
56     TRY {
57       gras_msg_handle(20.0);
58     } CATCH(e) {
59       if (e.category != timeout_error)
60         RETHROW;
61       xbt_ex_free(e);
62     }
63   }
64
65   gras_socket_close(g->sock);
66   free(g);
67   gras_exit();
68   return 0;
69 }
70
71 /* **********************************************************************
72  * Maestro code
73  * **********************************************************************/
74
75 /* Function prototypes */
76 int maestro (int argc,char *argv[]);
77
78 /* XP setups */
79 const int buf_size = 0;
80 const int exp_size = 100 * 1024;
81 const int msg_size = 50 * 1024;
82 const int sat_size = 1024 * 1024 * 10;
83 const double min_duration = 1;
84
85 static double XP(const char *bw1, const char *bw2,
86                  const char *sat1, const char *sat2) {
87
88   double sec, bw, sec_sat,bw_sat;
89
90   gras_os_sleep(5.0); /* wait for the sensors to show up */
91   /* Test BW without saturation */
92   amok_bw_request(bw1,4000,bw2,4000,
93                   buf_size,exp_size,msg_size,min_duration,&sec,&bw);
94   INFO4("BW(%s,%s) => %f sec, achieving %f Mb/s",
95        bw1, bw2, sec, (bw/1024.0/1024.0));
96
97
98   /* Test BW with saturation */  
99   amok_bw_saturate_start(sat1,4000,sat2,4000, sat_size,60);
100   gras_os_sleep(1.0); /* let it start */
101
102   amok_bw_request(bw1,4000,bw2,4000,
103                   buf_size,exp_size,msg_size,min_duration,&sec_sat,&bw_sat);
104   INFO6("BW(%s,%s//%s,%s) => %f sec, achieving %f Mb/s",
105        bw1,bw2,sat1,sat2,sec,bw/1024.0/1024.0);
106   
107   amok_bw_saturate_stop(sat1,4000,NULL,NULL);
108
109   if (bw_sat/bw < 0.7) {
110     INFO0("THERE IS SOME INTERFERENCE !!!");
111   } 
112   if (bw/bw_sat < 0.7) {
113     INFO0("THERE IS SOME INTERFERENCE (and I'm an idiot) !!!");
114   } 
115   return bw_sat/bw;
116 }
117
118 static void kill_buddy(char *name,int port){
119   gras_socket_t sock=gras_socket_client(name,port);
120   gras_msg_send(sock,gras_msgtype_by_name("kill"),NULL);
121   gras_socket_close(sock);
122 }
123 static void kill_buddy_dynar(void *b) {
124   xbt_host_t buddy=*(xbt_host_t*)b;
125   kill_buddy(buddy->name,buddy->port);
126 }
127
128 static void free_host(void *d){
129   xbt_host_t h=*(xbt_host_t*)d;
130   free(h->name);
131   free(h);
132 }
133
134 static void simple_saturation(int argc, char*argv[]) {
135   xbt_ex_t e;
136
137   kill_buddy(argv[5],atoi(argv[6]));
138   kill_buddy(argv[7],atoi(argv[8]));
139
140   amok_bw_saturate_start(argv[1],atoi(argv[2]),argv[3],atoi(argv[4]),
141                          sat_size,5);
142   gras_os_sleep(3);
143   TRY {
144     amok_bw_saturate_stop(argv[1],atoi(argv[2]),NULL,NULL);
145   } CATCH(e) {
146     xbt_ex_free(e);
147   }
148
149  
150   kill_buddy(argv[1],atoi(argv[2]));
151   kill_buddy(argv[3],atoi(argv[4]));
152 }
153 /********************************************************************************************/
154 static void env_hosttohost_bw(int argc, char*argv[]) {
155
156   /* where are the sensors */
157   xbt_dynar_t hosts = xbt_dynar_new(sizeof(xbt_host_t),&free_host);
158   int nb_hosts;
159
160   /* results */
161   double sec, bw;
162
163   /* iterators */
164   int i;
165   xbt_host_t h1;
166
167   gras_socket_t peer; /* socket to sensor */
168
169   /* wait to ensure that all server sockets are there before starting the experiment */ 
170   gras_os_sleep(0.5);
171
172   /* Get the sensor location from argc/argv */
173   for (i=1; i<argc-1; i+=2){
174     xbt_host_t host=xbt_new(s_xbt_host_t,1);
175     host->name=strdup(argv[i]);
176     host->port=atoi(argv[i+1]);
177     INFO2("New sensor: %s:%d",host->name,host->port);
178     xbt_dynar_push(hosts,&host);
179   }
180   nb_hosts = xbt_dynar_length(hosts);
181
182   INFO0(">>> start Test1: ENV end to end mesurements");
183
184   xbt_dynar_foreach(hosts,i,h1) {
185         peer = gras_socket_client(h1->name,h1->port);
186         amok_bw_test(peer,buf_size,exp_size,msg_size,min_duration,&sec,&bw);
187         INFO6("Bandwidth between me and %s:%d (%d bytes in msgs of %d bytes) took %f sec, achieving %.3f kb/s",
188         h1->name,h1->port,
189         exp_size,msg_size,
190         sec,((double)bw)/1024.0);
191   }
192
193   xbt_dynar_map(hosts,kill_buddy_dynar);
194   xbt_dynar_free(&hosts);
195
196 }
197 /********************************************************************************************/
198 static void env_Pairwisehost_bw(int argc, char*argv[]) {
199   xbt_ex_t e;
200
201   /* where are the sensors */
202   xbt_dynar_t hosts = xbt_dynar_new(sizeof(xbt_host_t),&free_host);
203   int nb_hosts;
204
205   /* getting the name of maestro for the saturation and the concurrent bandwidth measurements  */
206   char* host_test=argv[0];
207
208   /* results */
209   double sec, bw;
210
211   /* iterators */
212   int i,j;
213   xbt_host_t h1,h2;
214
215   /* socket to sensor */
216   gras_socket_t peer;
217
218   /* wait to ensure that all server sockets are there before starting the experiment */ 
219   gras_os_sleep(0.5);
220
221   INFO1(">>>>>< le maestro est: %s ",argv[0]);
222   /* Get the sensor location from argc/argv */
223   for (i=1; i<argc-1; i+=2){
224     xbt_host_t host=xbt_new(s_xbt_host_t,1);
225     host->name=strdup(argv[i]);
226     host->port=atoi(argv[i+1]);
227     INFO2("New sensor: %s:%d",host->name,host->port);
228     xbt_dynar_push(hosts,&host);
229   }
230   nb_hosts = xbt_dynar_length(hosts);
231
232   INFO0(">>> start Test2: ENV pairwise host bandwidth mesurements");
233  xbt_dynar_foreach(hosts,i,h1) {
234
235       TRY {
236         amok_bw_saturate_start(h1->name,h1->port,
237                                host_test,h1->port,//"Ginette"
238                                msg_size,120); // sturation of the link with msg_size to compute a concurent bandwidth MA //MB
239       } CATCH(e) {
240         RETHROW0("Cannot ask hosts to saturate the link: %s");
241       }
242      // gras_os_sleep(1.0);
243
244         xbt_dynar_foreach(hosts,j,h2) {
245         if (i==j) continue;
246
247         peer = gras_socket_client(h2->name,h2->port);
248         amok_bw_test(peer,buf_size,exp_size,msg_size,min_duration,&sec,&bw);
249         INFO6("Bandwidth between me and %s // measurement between me and %s (%d bytes in msgs of %d bytes) took %f sec, achieving %.3f kb/s",
250         h2->name,h1->name,
251         exp_size,msg_size,
252         sec,((double)bw)/1024.0);
253
254   }
255         amok_bw_saturate_stop(h1->name,h1->port,NULL,NULL);
256 }
257   xbt_dynar_map(hosts,kill_buddy_dynar);
258   xbt_dynar_free(&hosts);
259
260 }
261 /********************************************************************************************/
262 static void full_fledged_saturation(int argc, char*argv[]) {
263   xbt_ex_t e;
264 //unsigned int time1=5,bw1=5;
265   double time1=5.0,bw1=5.0; // 0.5 for test
266   /* timers */
267   double begin_simulated; 
268   int begin;
269
270   /* where are the sensors */
271   xbt_dynar_t hosts = xbt_dynar_new(sizeof(xbt_host_t),&free_host);
272   int nb_hosts;
273
274   /* results */
275   double *bw;
276   double *bw_sat;
277
278   /* iterators */
279   int i,j,k,l;
280   xbt_host_t h1,h2,h3,h4;
281
282   /* Get the sensor location from argc/argv */
283   for (i=1; i<argc; i++){
284     xbt_host_t host=xbt_host_from_string(argv[i]);
285     INFO2("New sensor: %s:%d",host->name,host->port);
286     xbt_dynar_push(hosts,&host);
287   }
288   nb_hosts = xbt_dynar_length(hosts);
289
290   gras_os_sleep(2); /* wait for my pals to get ready */
291   INFO0("Let's go for the bw_matrix");
292
293   /* Do the test without saturation */
294   begin=time(NULL);
295   begin_simulated=gras_os_time();
296
297   bw=amok_bw_matrix(hosts,buf_size,exp_size,msg_size,min_duration);
298
299   INFO2("Did all BW tests in %ld sec (%.2f simulated sec)",
300           time(NULL)-begin,gras_os_time()-begin_simulated);
301
302   /* Do the test with saturation */
303   bw_sat=xbt_new(double,nb_hosts*nb_hosts);
304   xbt_dynar_foreach(hosts,i,h1) {
305     xbt_dynar_foreach(hosts,j,h2) {
306       if (i==j) continue;
307
308       TRY {
309         amok_bw_saturate_start(h1->name,h1->port,
310                                h2->name,h2->port,
311                                sat_size, 0/* no timeout */);  
312       } CATCH(e) {
313         RETHROW0("Cannot ask hosts to saturate the link: %s");
314       }
315       gras_os_sleep(1.0);
316
317       begin=time(NULL);
318       begin_simulated=gras_os_time();
319       xbt_dynar_foreach(hosts,k,h3) {
320         if (i==k || j==k) continue;
321
322         xbt_dynar_foreach(hosts,l,h4) {
323           double ratio;
324           if (i==l || j==l || k==l) continue;
325
326           VERB4("TEST %s %s // %s %s",
327                 h1->name,h2->name,h3->name,h4->name);
328           amok_bw_request(h3->name,h3->port, h4->name,h4->port,
329                           buf_size,exp_size,msg_size,min_duration,
330                           NULL,&(bw_sat[k*nb_hosts + l]));
331
332           ratio=bw_sat[k*nb_hosts + l] / bw[k*nb_hosts + l];
333           INFO8("SATURATED BW XP(%s %s // %s %s) => %f (%f vs %f)%s",
334                 h1->name,h2->name,h3->name,h4->name,
335                 ratio,
336                 bw[k*nb_hosts + l] , bw_sat[k*nb_hosts + l],
337                 ratio < 0.7 ? " THERE IS SOME INTERFERENCE !!!": "");
338         }
339       }
340       amok_bw_saturate_stop(h1->name,h1->port,&time1,&bw1);// NULL,NULL);
341
342       INFO2("Did an iteration on saturation pair in %ld sec (%.2f simulated sec)",
343               time(NULL)-begin, gras_os_time()-begin_simulated);
344         INFO2("the duration of the experiment >>>>> %.3f sec (%.3f bandwidth)",time1,bw1);
345     }
346   }
347   free(bw_sat);
348   free(bw);
349   xbt_dynar_map(hosts,kill_buddy_dynar);
350   xbt_dynar_free(&hosts);
351 }
352
353
354 int maestro(int argc,char *argv[]) {
355
356   gras_init(&argc,argv);
357   amok_bw_init();
358
359   gras_socket_server(3333); /* only so that messages from the transport layer in gras identify us */
360
361   //env_Pairwisehost_bw(argc,argv);
362   //env_hosttohost_bw(argc,argv);
363
364   //  simple_saturation(argc,argv);
365   full_fledged_saturation(argc, argv);  
366
367   gras_exit();
368   return 0;
369
370 }