Logo AND Algorithmique Numérique Distribuée

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