Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill old $Id$ command dating from CVS
[simgrid.git] / examples / amok / saturate / saturate.c
1 /* saturate - link saturation demo of AMOK features                         */
2
3 /* Copyright (c) 2003-6 Martin Quinson. All rights reserved.                */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <signal.h>
11 #include <time.h>
12
13 #include "xbt/peer.h"
14 #include "gras.h"
15 #include "amok/bandwidth.h"
16 #include "amok/peermanagement.h"
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(saturate, "Messages specific to this example");
19
20 /* **********************************************************************
21  * Sensor code
22  * **********************************************************************/
23
24 /* Function prototypes */
25 int sensor(int argc, char *argv[]);
26
27 int sensor(int argc, char *argv[])
28 {
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
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,
84                   &bw_sat);
85   INFO6("BW(%s,%s//%s,%s) => %f sec, achieving %f Mb/s", bw1, bw2, sat1, sat2,
86         sec, bw / 1024.0 / 1024.0);
87
88   amok_bw_saturate_stop(sat1, 4000, NULL, NULL);
89
90   if (bw_sat / bw < 0.7) {
91     INFO0("THERE IS SOME INTERFERENCE !!!");
92   }
93   if (bw / bw_sat < 0.7) {
94     INFO0("THERE IS SOME INTERFERENCE (and I'm an idiot) !!!");
95   }
96   return bw_sat / bw;
97 }
98
99 static void kill_buddy(char *name, int port)
100 {
101   gras_socket_t sock = gras_socket_client(name, port);
102   gras_msg_send(sock, "kill", NULL);
103   gras_socket_close(sock);
104 }
105
106 static void kill_buddy_dynar(void *b)
107 {
108   xbt_peer_t buddy = *(xbt_peer_t *) b;
109   kill_buddy(buddy->name, buddy->port);
110 }
111
112 static void free_peer(void *d)
113 {
114   xbt_peer_t h = *(xbt_peer_t *) d;
115   free(h->name);
116   free(h);
117 }
118
119 static void simple_saturation(int argc, char *argv[])
120 {
121   xbt_ex_t e;
122
123   /* where are the sensors */
124   xbt_dynar_t peers;
125   xbt_peer_t h1, h2;
126   /* results */
127   double duration, bw;
128
129   /* Init the group */
130   peers = amok_pm_group_new("saturate");
131   /* wait for dudes */
132   gras_msg_handleall(5);
133
134   /* Stop all sensors but two of them */
135   while (xbt_dynar_length(peers) > 2) {
136     xbt_dynar_pop(peers, &h1);
137     amok_pm_kill_hp(h1->name, h1->port);
138     xbt_peer_free(h1);
139   }
140
141   /* get 2 friends */
142   xbt_dynar_get_cpy(peers, 0, &h1);
143   xbt_dynar_get_cpy(peers, 1, &h2);
144
145   /* Start saturation */
146   INFO4("Start saturation between %s:%d and %s:%d",
147         h1->name, h1->port, h2->name, h2->port);
148
149   amok_bw_saturate_start(h1->name, h1->port, h2->name, h2->port, 0,     /* Be a nice boy, compute msg_size yourself */
150                          30 /* 5 sec timeout */ );
151
152   /* Stop it after a while */
153   INFO0("Have a rest");
154   gras_os_sleep(1);
155   TRY {
156     INFO0("Stop the saturation");
157     amok_bw_saturate_stop(h1->name, h1->port, &duration, &bw);
158   }
159   CATCH(e) {
160     INFO0("Ooops, stoping the saturation raised an exception");
161     xbt_ex_free(e);
162   }
163   INFO2("Saturation took %.2fsec, achieving %fb/s", duration, bw);
164
165   /* Game is over, friends */
166   amok_pm_group_shutdown("saturate");
167 }
168
169 /********************************************************************************************/
170 static void full_fledged_saturation(int argc, char *argv[])
171 {
172   xbt_ex_t e;
173   double time1 = 5.0, bw1 = 5.0;        // 0.5 for test
174   /* timers */
175   double begin_simulated;
176   int begin;
177
178   /* where are the sensors */
179   xbt_dynar_t peers;
180   int nb_peers;
181
182   /* results */
183   double *bw;
184   double *bw_sat;
185
186   /* iterators */
187   unsigned int i, j, k, l;
188   xbt_peer_t h1, h2, h3, h4;
189
190   /* Init the group */
191   peers = amok_pm_group_new("saturate");
192   /* wait 4 dudes */
193   gras_msg_handle(60);
194   gras_msg_handle(60);
195   gras_msg_handle(60);
196   gras_msg_handle(60);
197   nb_peers = xbt_dynar_length(peers);
198
199   INFO0("Let's go for the bw_matrix");
200
201   /* Do the test without saturation */
202   begin = time(NULL);
203   begin_simulated = gras_os_time();
204
205   bw = amok_bw_matrix(peers, buf_size, msg_size, msg_amount, min_duration);
206
207   INFO2("Did all BW tests in %ld sec (%.2f simulated(?) sec)",
208         (long int) (time(NULL) - begin), gras_os_time() - begin_simulated);
209
210   /* Do the test with saturation */
211   bw_sat = xbt_new(double, nb_peers * nb_peers);
212   xbt_dynar_foreach(peers, i, h1) {
213     xbt_dynar_foreach(peers, j, h2) {
214       if (i == j)
215         continue;
216
217       TRY {
218         amok_bw_saturate_start(h1->name, h1->port, h2->name, h2->port, 0,       /* Be nice, compute msg_size yourself */
219                                0 /* no timeout */ );
220       }
221       CATCH(e) {
222         RETHROW0("Cannot ask peers to saturate the link: %s");
223       }
224       gras_os_sleep(5);
225
226       begin = time(NULL);
227       begin_simulated = gras_os_time();
228       xbt_dynar_foreach(peers, k, h3) {
229         if (i == k || j == k)
230           continue;
231
232         xbt_dynar_foreach(peers, l, h4) {
233           double ratio;
234           if (i == l || j == l || k == l)
235             continue;
236
237           VERB4("TEST %s %s // %s %s",
238                 h1->name, h2->name, h3->name, h4->name);
239           amok_bw_request(h3->name, h3->port, h4->name, h4->port,
240                           buf_size, msg_size, msg_amount, min_duration,
241                           NULL, &(bw_sat[k * nb_peers + l]));
242
243           ratio = bw_sat[k * nb_peers + l] / bw[k * nb_peers + l];
244           INFO8("SATURATED BW XP(%s %s // %s %s) => %f (%f vs %f)%s",
245                 h1->name, h2->name, h3->name, h4->name,
246                 ratio,
247                 bw[k * nb_peers + l], bw_sat[k * nb_peers + l],
248                 ratio < 0.7 ? " THERE IS SOME INTERFERENCE !!!" : "");
249         }
250       }
251       amok_bw_saturate_stop(h1->name, h1->port, &time1, &bw1);
252
253       INFO2
254         ("Did an iteration on saturation pair in %ld sec (%.2f simulated sec)",
255          (long int) (time(NULL) - begin), gras_os_time() - begin_simulated);
256       INFO2("the duration of the experiment >>>>> %.3f sec (%.3f bandwidth)",
257             time1, bw1);
258     }
259   }
260   free(bw_sat);
261   free(bw);
262   /* Game is over, friends */
263   amok_pm_group_shutdown("saturate");
264 }
265
266
267 int maestro(int argc, char *argv[])
268 {
269
270   gras_init(&argc, argv);
271   amok_bw_init();
272   amok_pm_init();
273
274   gras_socket_server(atoi(argv[1]));
275
276   simple_saturation(argc, argv);
277   //full_fledged_saturation(argc, argv);  
278
279   gras_exit();
280   return 0;
281
282 }