Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make two tests in the same file: a simple saturation test, and a full featured one
[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,char *port){
109   gras_socket_t sock=gras_socket_client(name,atoi(port));
110   gras_msg_send(sock,gras_msgtype_by_name("kill"),NULL);
111   gras_socket_close(sock);
112 }
113
114 static void free_host(void *d){
115   xbt_host_t h=*(xbt_host_t*)d;
116   free(h->name);
117   free(h);
118 }
119
120 static void simple_saturation(int argc, char*argv[]) {
121   xbt_ex_t e;
122
123   kill_buddy(argv[5],argv[6]);
124   kill_buddy(argv[7],argv[8]);
125
126   amok_bw_saturate_start(argv[1],atoi(argv[2]),argv[3],atoi(argv[4]),
127                          sat_size*5,5);
128   gras_os_sleep(3);
129   TRY {
130     amok_bw_saturate_stop(argv[1],atoi(argv[2]),NULL,NULL);
131   } CATCH(e) {
132     xbt_ex_free(e);
133   }
134
135  
136   kill_buddy(argv[1],argv[2]);
137   kill_buddy(argv[3],argv[4]);
138 }
139
140 static void full_fledged_saturation(int argc, char*argv[]) {
141   xbt_ex_t e;
142
143   /* timers */
144   double begin_simulated; 
145   int begin;
146
147   /* where are the sensors */
148   xbt_dynar_t hosts = xbt_dynar_new(sizeof(xbt_host_t),&free_host);
149   int nb_hosts;
150
151   /* results */
152   double *bw;
153   double *bw_sat;
154
155   /* iterators */
156   int i,j,k,l;
157   xbt_host_t h1,h2,h3,h4;
158
159   /* Get the sensor location from argc/argv */
160   for (i=1; i<argc-1; i+=2){
161     xbt_host_t host=xbt_new(s_xbt_host_t,1);
162     host->name=strdup(argv[i]);
163     host->port=atoi(argv[i+1]);
164     INFO2("New sensor: %s:%d",host->name,host->port);
165     xbt_dynar_push(hosts,&host);
166   }
167   nb_hosts = xbt_dynar_length(hosts);
168
169   /* Do the test without saturation */
170   begin=time(NULL);
171   begin_simulated=gras_os_time();
172
173   bw=amok_bw_matrix(hosts,buf_size,exp_size,msg_size);
174
175   INFO2("Did all BW tests in %ld sec (%.2f simulated sec)",
176           time(NULL)-begin,gras_os_time()-begin_simulated);
177
178   /* Do the test with saturation */
179   bw_sat=xbt_new(double,nb_hosts*nb_hosts);
180   xbt_dynar_foreach(hosts,i,h1) {
181     xbt_dynar_foreach(hosts,j,h2) {
182       if (i==j) continue;
183         
184       TRY {
185         amok_bw_saturate_start(h1->name,h1->port,
186                                h2->name,h2->port,
187                                sat_size,120);
188       } CATCH(e) {
189         RETHROW0("Cannot ask hosts to saturate the link: %s");
190       }
191       gras_os_sleep(1.0);
192
193       begin=time(NULL);
194       begin_simulated=gras_os_time();
195       xbt_dynar_foreach(hosts,k,h3) {
196         if (i==k || j==k) continue;
197
198         xbt_dynar_foreach(hosts,l,h4) {
199           double ratio;
200           if (i==l || j==l || k==l) continue;
201
202           INFO4("TEST %s %s // %s %s",
203                 h1->name,h2->name,h3->name,h4->name);
204           amok_bw_request(h3->name,h3->port, h4->name,h4->port,
205                           buf_size,exp_size,msg_size,
206                           NULL,&(bw_sat[k*nb_hosts + l])); 
207           
208           ratio=bw_sat[k*nb_hosts + l] / bw[k*nb_hosts + l];
209           INFO8("SATURATED BW XP(%s %s // %s %s) => %f (%f vs %f)%s",
210                 h1->name,h2->name,h3->name,h4->name,
211                 ratio,
212                 bw[k*nb_hosts + l] , bw_sat[k*nb_hosts + l],
213
214                 ratio < 0.7 ? " THERE IS SOME INTERFERENCE !!!": "");
215         }
216       }
217
218       amok_bw_saturate_stop(h1->name,h1->port, NULL,NULL);
219
220       INFO2("Did an iteration on saturation pair in %ld sec (%.2f simulated sec)",
221               time(NULL)-begin, gras_os_time()-begin_simulated);
222     }
223   }
224   xbt_dynar_free(&hosts);
225 }
226
227 int maestro(int argc,char *argv[]) {
228
229   gras_init(&argc,argv);
230   amok_bw_init();
231
232
233   //  simple_saturation(argc,argv);
234   full_fledged_saturation(argc, argv);  
235
236   gras_exit();
237   return 0;
238
239 }