Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
355c2ab575cb5a5c651c25df9a37ae8d1e8c7009
[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 #include "amok/peermanagement.h"
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(saturate,"Messages specific to this example");
20
21 /* **********************************************************************
22  * Sensor code
23  * **********************************************************************/
24
25 /* Function prototypes */
26 int sensor (int argc,char *argv[]);
27
28 int sensor (int argc,char *argv[]) {
29   gras_socket_t mysock;
30   gras_socket_t master;
31
32   gras_init(&argc, argv);
33   amok_bw_init();
34   amok_pm_init();
35  
36   mysock = gras_socket_server_range(3000,9999,0,0);
37   INFO1("Sensor starting (on port %d)",gras_os_myport());
38   gras_os_sleep(2); /* let the master get ready */
39   master = gras_socket_client_from_string(argv[1]);
40                                               
41   amok_pm_group_join(master,"saturate");
42   amok_pm_mainloop(600);
43
44   gras_socket_close(mysock);
45   gras_socket_close(master);
46   gras_exit();
47   return 0;
48 }
49
50 /* **********************************************************************
51  * Maestro code
52  * **********************************************************************/
53
54 /* Function prototypes */
55 int maestro (int argc,char *argv[]);
56
57 /* XP setups */
58 const int buf_size = 0;
59 const int msg_size = 50 * 1024;
60 const int msg_amount = 2;
61 const int sat_size = 1024 * 1024 * 10;
62 const double min_duration = 1;
63
64 static double XP(const char *bw1, const char *bw2,
65                  const char *sat1, const char *sat2) {
66
67   double sec, bw, sec_sat,bw_sat;
68
69   gras_os_sleep(5.0); /* wait for the sensors to show up */
70   /* Test BW without saturation */
71   amok_bw_request(bw1,4000,bw2,4000,
72                   buf_size,msg_size,msg_amount,min_duration,&sec,&bw);
73   INFO4("BW(%s,%s) => %f sec, achieving %f Mb/s",
74        bw1, bw2, sec, (bw/1024.0/1024.0));
75
76
77   /* Test BW with saturation */  
78   amok_bw_saturate_start(sat1,4000,sat2,4000, sat_size,60);
79   gras_os_sleep(1.0); /* let it start */
80
81   amok_bw_request(bw1,4000,bw2,4000,
82                   buf_size,msg_size,msg_amount,min_duration,&sec_sat,&bw_sat);
83   INFO6("BW(%s,%s//%s,%s) => %f sec, achieving %f Mb/s",
84        bw1,bw2,sat1,sat2,sec,bw/1024.0/1024.0);
85   
86   amok_bw_saturate_stop(sat1,4000,NULL,NULL);
87
88   if (bw_sat/bw < 0.7) {
89     INFO0("THERE IS SOME INTERFERENCE !!!");
90   } 
91   if (bw/bw_sat < 0.7) {
92     INFO0("THERE IS SOME INTERFERENCE (and I'm an idiot) !!!");
93   } 
94   return bw_sat/bw;
95 }
96
97 static void kill_buddy(char *name,int port){
98   gras_socket_t sock=gras_socket_client(name,port);
99   gras_msg_send(sock,gras_msgtype_by_name("kill"),NULL);
100   gras_socket_close(sock);
101 }
102 static void kill_buddy_dynar(void *b) {
103   xbt_peer_t buddy=*(xbt_peer_t*)b;
104   kill_buddy(buddy->name,buddy->port);
105 }
106
107 static void free_peer(void *d){
108   xbt_peer_t h=*(xbt_peer_t*)d;
109   free(h->name);
110   free(h);
111 }
112
113 static void simple_saturation(int argc, char*argv[]) {
114   xbt_ex_t e;
115
116   /* where are the sensors */
117   xbt_dynar_t peers;
118   xbt_peer_t h1,h2;
119   /* results */
120   double duration,bw;
121
122   /* Init the group */
123   peers=amok_pm_group_new("saturate");
124   /* wait for dudes */
125   gras_msg_handleall(5);
126
127   /* get 2 friends */
128   xbt_dynar_get_cpy(peers,0,&h1);
129   xbt_dynar_get_cpy(peers,1,&h2);
130    
131   /* Start saturation */
132   amok_bw_saturate_start(h1->name,h1->port,
133                          h2->name,h2->port,
134                          0, /* Be a nice boy, compute msg_size yourself */
135                          30  /* 5 sec timeout */);  
136
137   /* Stop it after a while */
138   gras_os_sleep(1);
139   TRY {
140     amok_bw_saturate_stop(h1->name,h1->port, &duration,&bw);
141   } CATCH(e) {
142     xbt_ex_free(e);
143   }
144   INFO2("Saturation took %.2fsec, achieving %fb/s",duration,bw);
145    
146   /* Game is over, friends */
147   amok_pm_group_shutdown ("saturate");
148 }
149 /********************************************************************************************/
150 static void full_fledged_saturation(int argc, char*argv[]) {
151   xbt_ex_t e;
152   double time1=5.0,bw1=5.0; // 0.5 for test
153   /* timers */
154   double begin_simulated; 
155   int begin;
156
157   /* where are the sensors */
158   xbt_dynar_t peers;
159   int nb_peers;
160
161   /* results */
162   double *bw;
163   double *bw_sat;
164
165   /* iterators */
166   int i,j,k,l;
167   xbt_peer_t h1,h2,h3,h4;
168
169   /* Init the group */
170   peers=amok_pm_group_new("saturate");
171   /* wait 4 dudes */
172   gras_msg_handle(60);
173   gras_msg_handle(60);
174   gras_msg_handle(60);
175   gras_msg_handle(60);
176   nb_peers = xbt_dynar_length(peers);
177
178   INFO0("Let's go for the bw_matrix");
179
180   /* Do the test without saturation */
181   begin=time(NULL);
182   begin_simulated=gras_os_time();
183
184   bw=amok_bw_matrix(peers,buf_size,msg_size,msg_amount,min_duration);
185
186   INFO2("Did all BW tests in %ld sec (%.2f simulated(?) sec)",
187           time(NULL)-begin,gras_os_time()-begin_simulated);
188
189   /* Do the test with saturation */
190   bw_sat=xbt_new(double,nb_peers*nb_peers);
191   xbt_dynar_foreach(peers,i,h1) {
192     xbt_dynar_foreach(peers,j,h2) {
193       if (i==j) continue;
194
195       TRY {
196         amok_bw_saturate_start(h1->name,h1->port,
197                                h2->name,h2->port,
198                                0, /* Be nice, compute msg_size yourself */
199                                0  /* no timeout */);  
200       } CATCH(e) {
201         RETHROW0("Cannot ask peers to saturate the link: %s");
202       }
203       gras_os_sleep(5);
204
205       begin=time(NULL);
206       begin_simulated=gras_os_time();
207       xbt_dynar_foreach(peers,k,h3) {
208         if (i==k || j==k) continue;
209
210         xbt_dynar_foreach(peers,l,h4) {
211           double ratio;
212           if (i==l || j==l || k==l) continue;
213
214           VERB4("TEST %s %s // %s %s",
215                 h1->name,h2->name,h3->name,h4->name);
216           amok_bw_request(h3->name,h3->port, h4->name,h4->port,
217                           buf_size,msg_size,msg_amount,min_duration,
218                           NULL,&(bw_sat[k*nb_peers + l]));
219
220           ratio=bw_sat[k*nb_peers + l] / bw[k*nb_peers + l];
221           INFO8("SATURATED BW XP(%s %s // %s %s) => %f (%f vs %f)%s",
222                 h1->name,h2->name,h3->name,h4->name,
223                 ratio,
224                 bw[k*nb_peers + l] , bw_sat[k*nb_peers + l],
225                 ratio < 0.7 ? " THERE IS SOME INTERFERENCE !!!": "");
226         }
227       }
228       amok_bw_saturate_stop(h1->name,h1->port,&time1,&bw1);
229
230       INFO2("Did an iteration on saturation pair in %ld sec (%.2f simulated sec)",
231               time(NULL)-begin, gras_os_time()-begin_simulated);
232         INFO2("the duration of the experiment >>>>> %.3f sec (%.3f bandwidth)",time1,bw1);
233     }
234   }
235   free(bw_sat);
236   free(bw);
237   /* Game is over, friends */
238   amok_pm_group_shutdown ("saturate");
239 }
240
241
242 int maestro(int argc,char *argv[]) {
243
244   gras_init(&argc,argv);
245   amok_bw_init();
246   amok_pm_init();
247
248   gras_socket_server(atoi(argv[1]));
249
250   simple_saturation(argc,argv);
251   //full_fledged_saturation(argc, argv);  
252
253   gras_exit();
254   return 0;
255
256 }