Logo AND Algorithmique Numérique Distribuée

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