Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
The ENV code which where in the saturation example with no good reason
[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/hostmanagement.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_hm_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(0.5); /* let the master get ready */
39   master = gras_socket_client_from_string(argv[1]);
40                                               
41   amok_hm_group_join(master,"saturate");
42   amok_hm_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 exp_size = 100 * 1024;
60 const int msg_size = 50 * 1024;
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,exp_size,msg_size,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,exp_size,msg_size,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_host_t buddy=*(xbt_host_t*)b;
104   kill_buddy(buddy->name,buddy->port);
105 }
106
107 static void free_host(void *d){
108   xbt_host_t h=*(xbt_host_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   kill_buddy(argv[5],atoi(argv[6]));
117   kill_buddy(argv[7],atoi(argv[8]));
118
119   amok_bw_saturate_start(argv[1],atoi(argv[2]),argv[3],atoi(argv[4]),
120                          sat_size,5);
121   gras_os_sleep(3);
122   TRY {
123     amok_bw_saturate_stop(argv[1],atoi(argv[2]),NULL,NULL);
124   } CATCH(e) {
125     xbt_ex_free(e);
126   }
127
128  
129   kill_buddy(argv[1],atoi(argv[2]));
130   kill_buddy(argv[3],atoi(argv[4]));
131 }
132 /********************************************************************************************/
133 static void full_fledged_saturation(int argc, char*argv[]) {
134   xbt_ex_t e;
135 //unsigned int time1=5,bw1=5;
136   double time1=5.0,bw1=5.0; // 0.5 for test
137   /* timers */
138   double begin_simulated; 
139   int begin;
140
141   /* where are the sensors */
142   xbt_dynar_t hosts;
143   int nb_hosts;
144
145   /* results */
146   double *bw;
147   double *bw_sat;
148
149   /* iterators */
150   int i,j,k,l;
151   xbt_host_t h1,h2,h3,h4;
152
153   /* Init the group */
154   hosts=amok_hm_group_new("saturate");
155   INFO0("Wait for peers for 5 sec");
156   gras_msg_handleall(5); /* friends, we're ready. Come and play */
157   nb_hosts = xbt_dynar_length(hosts);
158
159   INFO0("Let's go for the bw_matrix");
160
161   /* Do the test without saturation */
162   begin=time(NULL);
163   begin_simulated=gras_os_time();
164
165   bw=amok_bw_matrix(hosts,buf_size,exp_size,msg_size,min_duration);
166
167   INFO2("Did all BW tests in %ld sec (%.2f simulated(?) sec)",
168           time(NULL)-begin,gras_os_time()-begin_simulated);
169
170   /* Do the test with saturation */
171   bw_sat=xbt_new(double,nb_hosts*nb_hosts);
172   xbt_dynar_foreach(hosts,i,h1) {
173     xbt_dynar_foreach(hosts,j,h2) {
174       if (i==j) continue;
175
176       TRY {
177         amok_bw_saturate_start(h1->name,h1->port,
178                                h2->name,h2->port,
179                                sat_size, 0/* no timeout */);  
180       } CATCH(e) {
181         RETHROW0("Cannot ask hosts to saturate the link: %s");
182       }
183       gras_os_sleep(1.0);
184
185       begin=time(NULL);
186       begin_simulated=gras_os_time();
187       xbt_dynar_foreach(hosts,k,h3) {
188         if (i==k || j==k) continue;
189
190         xbt_dynar_foreach(hosts,l,h4) {
191           double ratio;
192           if (i==l || j==l || k==l) continue;
193
194           VERB4("TEST %s %s // %s %s",
195                 h1->name,h2->name,h3->name,h4->name);
196           amok_bw_request(h3->name,h3->port, h4->name,h4->port,
197                           buf_size,exp_size,msg_size,min_duration,
198                           NULL,&(bw_sat[k*nb_hosts + l]));
199
200           ratio=bw_sat[k*nb_hosts + l] / bw[k*nb_hosts + l];
201           INFO8("SATURATED BW XP(%s %s // %s %s) => %f (%f vs %f)%s",
202                 h1->name,h2->name,h3->name,h4->name,
203                 ratio,
204                 bw[k*nb_hosts + l] , bw_sat[k*nb_hosts + l],
205                 ratio < 0.7 ? " THERE IS SOME INTERFERENCE !!!": "");
206         }
207       }
208       amok_bw_saturate_stop(h1->name,h1->port,&time1,&bw1);// NULL,NULL);
209
210       INFO2("Did an iteration on saturation pair in %ld sec (%.2f simulated sec)",
211               time(NULL)-begin, gras_os_time()-begin_simulated);
212         INFO2("the duration of the experiment >>>>> %.3f sec (%.3f bandwidth)",time1,bw1);
213     }
214   }
215   free(bw_sat);
216   free(bw);
217   /* Game is over, friends */
218   amok_hm_group_shutdown ("saturate");
219 }
220
221
222 int maestro(int argc,char *argv[]) {
223
224   gras_init(&argc,argv);
225   amok_bw_init();
226   amok_hm_init();
227
228   gras_socket_server(atoi(argv[1]));
229
230   //  simple_saturation(argc,argv);
231   full_fledged_saturation(argc, argv);  
232
233   gras_exit();
234   return 0;
235
236 }