Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make sure we kill peers properly, and plug some memleaks
[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
18 XBT_LOG_NEW_DEFAULT_CATEGORY(saturate,"Messages specific to this example");
19
20 /* **********************************************************************
21  * Sensor code
22  * **********************************************************************/
23
24 /* Global private data */
25 typedef struct {
26   gras_socket_t sock;
27   int done;
28 } sensor_data_t;
29
30 /* Function prototypes */
31 int sensor (int argc,char *argv[]);
32
33 static int sensor_cb_kill(gras_msg_cb_ctx_t ctx,
34                           void             *payload_data) {
35   
36   sensor_data_t *g=gras_userdata_get();
37   g->done = 1;
38   INFO0("Killed");
39   return 1;
40 }
41 int sensor (int argc,char *argv[]) {
42   sensor_data_t *g;
43
44   gras_init(&argc,argv);
45   amok_bw_init();
46
47   g=gras_userdata_new(sensor_data_t);  
48   g->sock = gras_socket_server(4000);
49   g->done = 0;
50   gras_msgtype_declare("kill",NULL);
51   gras_cb_register(gras_msgtype_by_name("kill"),&sensor_cb_kill);
52
53   while (!g->done) {
54     gras_msg_handle(60.0);
55   }
56
57   gras_socket_close(g->sock);
58   free(g);
59   gras_exit();
60   return 0;
61 }
62
63 /* **********************************************************************
64  * Maestro code
65  * **********************************************************************/
66
67 /* Function prototypes */
68 int maestro (int argc,char *argv[]);
69
70 /* XP setups */
71 const int buf_size = 0;
72 const int exp_size = 1024;// * 1024;
73 const int msg_size = 1024;
74 const int sat_size = 1024 * 1024 * 10 * 10;
75
76 static double XP(const char *bw1, const char *bw2,
77                  const char *sat1, const char *sat2) {
78
79   double sec, bw, sec_sat,bw_sat;
80
81   /* Test BW without saturation */
82   amok_bw_request(bw1,4000,bw2,4000,
83                   buf_size,exp_size,msg_size,&sec,&bw);
84   INFO4("BW(%s,%s) => %f sec, achieving %f Mb/s",
85        bw1, bw2, sec, (bw/1024.0/1024.0));
86
87
88   /* Test BW with saturation */  
89   amok_bw_saturate_start(sat1,4000,sat2,4000, sat_size,60);
90   gras_os_sleep(1.0); /* let it start */
91
92   amok_bw_request(bw1,4000,bw2,4000,
93                   buf_size,exp_size,msg_size,&sec_sat,&bw_sat);
94   INFO6("BW(%s,%s//%s,%s) => %f sec, achieving %f Mb/s",
95        bw1,bw2,sat1,sat2,sec,bw/1024.0/1024.0);
96   
97   amok_bw_saturate_stop(sat1,4000,NULL,NULL);
98
99   if (bw_sat/bw < 0.7) {
100     INFO0("THERE IS SOME INTERFERENCE !!!");
101   } 
102   if (bw/bw_sat < 0.7) {
103     INFO0("THERE IS SOME INTERFERENCE (and I'm an idiot) !!!");
104   } 
105   return bw_sat/bw;
106 }
107
108 static void kill_buddy(char *name,int port){
109   gras_socket_t sock=gras_socket_client(name,port);
110   gras_msg_send(sock,gras_msgtype_by_name("kill"),NULL);
111   gras_socket_close(sock);
112 }
113 static void kill_buddy_dynar(void *b) {
114   xbt_host_t buddy=*(xbt_host_t*)b;
115   kill_buddy(buddy->name,buddy->port);
116 }
117
118 static void free_host(void *d){
119   xbt_host_t h=*(xbt_host_t*)d;
120   free(h->name);
121   free(h);
122 }
123
124 static void simple_saturation(int argc, char*argv[]) {
125   xbt_ex_t e;
126
127   kill_buddy(argv[5],atoi(argv[6]));
128   kill_buddy(argv[7],atoi(argv[8]));
129
130   amok_bw_saturate_start(argv[1],atoi(argv[2]),argv[3],atoi(argv[4]),
131                          sat_size*5,5);
132   gras_os_sleep(3);
133   TRY {
134     amok_bw_saturate_stop(argv[1],atoi(argv[2]),NULL,NULL);
135   } CATCH(e) {
136     xbt_ex_free(&e);
137   }
138
139  
140   kill_buddy(argv[1],atoi(argv[2]));
141   kill_buddy(argv[3],atoi(argv[4]));
142 }
143
144 static void full_fledged_saturation(int argc, char*argv[]) {
145   xbt_ex_t e;
146
147   /* timers */
148   double begin_simulated; 
149   int begin;
150
151   /* where are the sensors */
152   xbt_dynar_t hosts = xbt_dynar_new(sizeof(xbt_host_t),&free_host);
153   int nb_hosts;
154
155   /* results */
156   double *bw;
157   double *bw_sat;
158
159   /* iterators */
160   int i,j,k,l;
161   xbt_host_t h1,h2,h3,h4;
162
163   /* Get the sensor location from argc/argv */
164   for (i=1; i<argc-1; i+=2){
165     xbt_host_t host=xbt_new(s_xbt_host_t,1);
166     host->name=strdup(argv[i]);
167     host->port=atoi(argv[i+1]);
168     INFO2("New sensor: %s:%d",host->name,host->port);
169     xbt_dynar_push(hosts,&host);
170   }
171   nb_hosts = xbt_dynar_length(hosts);
172
173   /* Do the test without saturation */
174   begin=time(NULL);
175   begin_simulated=gras_os_time();
176
177   bw=amok_bw_matrix(hosts,buf_size,exp_size,msg_size);
178
179   INFO2("Did all BW tests in %ld sec (%.2f simulated sec)",
180           time(NULL)-begin,gras_os_time()-begin_simulated);
181
182   /* Do the test with saturation */
183   bw_sat=xbt_new(double,nb_hosts*nb_hosts);
184   xbt_dynar_foreach(hosts,i,h1) {
185     xbt_dynar_foreach(hosts,j,h2) {
186       if (i==j) continue;
187         
188       TRY {
189         amok_bw_saturate_start(h1->name,h1->port,
190                                h2->name,h2->port,
191                                sat_size,120);
192       } CATCH(e) {
193         RETHROW0("Cannot ask hosts to saturate the link: %s");
194       }
195       gras_os_sleep(1.0);
196
197       begin=time(NULL);
198       begin_simulated=gras_os_time();
199       xbt_dynar_foreach(hosts,k,h3) {
200         if (i==k || j==k) continue;
201
202         xbt_dynar_foreach(hosts,l,h4) {
203           double ratio;
204           if (i==l || j==l || k==l) continue;
205
206           INFO4("TEST %s %s // %s %s",
207                 h1->name,h2->name,h3->name,h4->name);
208           amok_bw_request(h3->name,h3->port, h4->name,h4->port,
209                           buf_size,exp_size,msg_size,
210                           NULL,&(bw_sat[k*nb_hosts + l])); 
211           
212           ratio=bw_sat[k*nb_hosts + l] / bw[k*nb_hosts + l];
213           INFO8("SATURATED BW XP(%s %s // %s %s) => %f (%f vs %f)%s",
214                 h1->name,h2->name,h3->name,h4->name,
215                 ratio,
216                 bw[k*nb_hosts + l] , bw_sat[k*nb_hosts + l],
217
218                 ratio < 0.7 ? " THERE IS SOME INTERFERENCE !!!": "");
219         }
220       }
221
222       amok_bw_saturate_stop(h1->name,h1->port, NULL,NULL);
223
224       INFO2("Did an iteration on saturation pair in %ld sec (%.2f simulated sec)",
225               time(NULL)-begin, gras_os_time()-begin_simulated);
226     }
227   }
228
229   free(bw_sat);
230   free(bw);
231   xbt_dynar_map(hosts,kill_buddy_dynar);
232   xbt_dynar_free(&hosts);
233 }
234
235
236 int maestro(int argc,char *argv[]) {
237
238   gras_init(&argc,argv);
239   amok_bw_init();
240
241
242   //  simple_saturation(argc,argv);
243   full_fledged_saturation(argc, argv);  
244
245   gras_exit();
246   return 0;
247
248 }