Logo AND Algorithmique Numérique Distribuée

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