Logo AND Algorithmique Numérique Distribuée

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