Logo AND Algorithmique Numérique Distribuée

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