Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reduce the number of loops to do to not kill the old machines out there
[simgrid.git] / examples / saturate / saturate.c
1 /* $Id$ */
2
3 /* saturate - link saturation demo of GRAS features                         */
4
5 /* Copyright (c) 2003 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
17 XBT_LOG_NEW_DEFAULT_CATEGORY(saturate,"Messages specific to this example");
18
19 /* **********************************************************************
20  * Sensor code
21  * **********************************************************************/
22
23 /* Global private data */
24 typedef struct {
25   gras_socket_t *sock;
26 } sensor_data_t;
27
28 /* Function prototypes */
29 int sensor (int argc,char *argv[]);
30
31 int sensor (int argc,char *argv[]) {
32   xbt_error_t errcode;
33   sensor_data_t *g=gras_userdata_new(sensor_data_t);  
34
35   if ((errcode=gras_socket_server(4000,&(g->sock)))) {
36     CRITICAL1("Sensor: Error %s encountered while opening the server socket",xbt_error_name(errcode));
37     return 1;
38   }
39
40   if (grasbw_register_messages()) {
41     gras_socket_close(g->sock);
42     return 1;
43   }
44
45   while (1) {
46     if ((errcode=gras_msg_handle(60.0)) && errcode != timeout_error) { 
47        CRITICAL1("Sensor: Error '%s' while handling message",
48               xbt_error_name(errcode));
49     }
50   }
51
52   gras_os_sleep(5,0);
53   gras_socket_close(g->sock);
54    
55   return 0;
56 }
57
58 /* **********************************************************************
59  * Maestro code
60  * **********************************************************************/
61
62 /* Global private data */
63 typedef struct {
64   gras_socket_t *sock;
65 } maestro_data_t;
66
67 /* Function prototypes */
68 int maestro (int argc,char *argv[]);
69 double XP(const char *bw1, const char *bw2, const char *sat1, const char *sat2);
70
71 double XP(const char *bw1, const char *bw2, const char *sat1, const char *sat2) {
72   xbt_error_t errcode;
73   int bufSize=32 * 1024;
74   int expSize=64 * 1024;
75   int msgSize=64 * 1024;
76   int satSize=msgSize * 10;
77   double sec, bw, sec_sat,bw_sat;
78
79   if ((errcode=grasbw_request(bw1,4000,bw2,4000,bufSize,expSize,msgSize,&sec,&bw))) {
80     fprintf(stderr,"MAESTRO: Error %s encountered while doing the test\n",xbt_error_name(errcode));
81     return -1;
82   }
83    
84   fprintf(stderr,"MAESTRO: BW(%s,%s) => %f sec, achieving %f Mb/s\n",bw1,bw2,sec,bw);
85
86   if ((errcode=grasbw_saturate_start(sat1,4000,sat2,4000,satSize,60))) {
87     fprintf(stderr,"MAESTRO: Error %s encountered while starting saturation\n",
88             xbt_error_name(errcode));
89     return -1;
90   }
91   gras_os_sleep(1,0);
92   if ((errcode=grasbw_request(bw1,4000,bw2,4000,bufSize,expSize,msgSize,&sec_sat,&bw_sat))) {
93     fprintf(stderr,"MAESTRO: Error %s encountered while doing the test\n",xbt_error_name(errcode));
94     return -1;
95   }
96    
97   fprintf(stderr,"MAESTRO: BW(%s,%s//%s,%s) => %f sec, achieving %f Mb/s\n",
98          bw1,bw2,sat1,sat2,sec_sat,bw_sat);
99
100   if ((errcode=grasbw_saturate_stop(sat1,4000,sat2,4000))) {
101     fprintf(stderr,"MAESTRO: Error %s encountered while stopping saturation\n",
102             xbt_error_name(errcode));
103     return -1;
104   }
105
106   if (bw_sat/bw < 0.7) {
107     fprintf(stderr,"MAESTRO: THERE IS SOME INTERFERENCE !!!\n");
108   } 
109   if (bw/bw_sat < 0.7) {
110     fprintf(stderr,"MAESTRO: THERE IS SOME INTERFERENCE (and Im a cretin) !!!\n");
111   } 
112   return bw_sat/bw;
113
114 }
115
116 //#define MAXHOSTS 33
117 #define MAXHOSTS 4
118
119 int maestro(int argc,char *argv[]) {
120   int bufSize=32 * 1024;
121   int expSize= 1024 * 1024;
122   int msgSize=expSize;
123   int satSize=msgSize * 100;
124   double dummy,beginSim;
125   xbt_error_t errcode;
126   maestro_data_t *g=gras_userdata_new(maestro_data_t);
127   //  const char *hosts[MAXHOSTS] = { "61", "62", "63", "69", "70", "77", "81", "83", "85", "87", "88", "95", "98", "107", "109", "111", "112", "121", "124", "125", "131", "145", "150", "156", "157", "162", "165", "168", "169", "170", "175", "177", "178" };
128   const char *hosts[MAXHOSTS] = { "A", "B", "C", "D" };
129
130   double bw[MAXHOSTS][MAXHOSTS];
131   double bw_sat[MAXHOSTS][MAXHOSTS];
132
133   int a,b,c,d,begin;
134
135   if ((errcode=gras_socket_server(4000,&(g->sock)))) { 
136     fprintf(stderr,"MAESTRO: Error %s encountered while opening the server socket\n",xbt_error_name(errcode));
137     return 1;
138   }
139
140   if (grasbw_register_messages()) {
141     gras_socket_close(g->sock);
142     return 1;
143   }
144
145   begin=time(NULL);
146   beginSim=gras_os_time();
147   for (a=0; a<MAXHOSTS; a++) {
148     for (b=0; b<MAXHOSTS; b++) {
149       if (a==b) continue;
150       fprintf(stderr,"BW XP(%s %s)=",hosts[a],hosts[b]); 
151       if ((errcode=grasbw_request(hosts[a],4000,hosts[b],4000,bufSize,expSize,msgSize,
152                                   &dummy,&(bw[a][b])))) {
153         fprintf(stderr,"MAESTRO: Error %s encountered while doing the test\n",xbt_error_name(errcode));
154         return 1;
155       }
156       fprintf(stderr,"%f Mb/s in %f sec\n",bw[a][b],dummy);
157     }
158   }
159   fprintf(stderr,"Did all BW tests in %ld sec (%.2f simulated sec)\n",
160           time(NULL)-begin,gras_os_time()-beginSim);
161       
162   for (a=0; a<MAXHOSTS; a++) {
163     for (b=0; b<MAXHOSTS; b++) {
164       if (a==b) continue;
165         
166       if ((errcode=grasbw_saturate_start(hosts[a],4000,hosts[b],4000,satSize,360000000))) {
167         fprintf(stderr,"MAESTRO: Error %s encountered while starting saturation\n",
168                 xbt_error_name(errcode));
169         return -1;
170       }
171       gras_os_sleep(1,0);
172
173       begin=time(NULL);
174       beginSim=gras_os_time();
175       for (c=0 ;c<MAXHOSTS; c++) {
176         if (a==c) continue;
177         if (b==c) continue;
178
179         for (d=0 ;d<MAXHOSTS; d++) {
180           if (a==d) continue;
181           if (b==d) continue;
182           if (c==d) continue;
183           
184           if ((errcode=grasbw_request(hosts[c],4000,hosts[d],4000,bufSize,expSize,msgSize,
185                                       &dummy,&(bw_sat[c][d])))) {
186             fprintf(stderr,"MAESTRO: Error %s encountered in test\n",xbt_error_name(errcode));
187             return 1;
188           }
189           fprintf(stderr, "MAESTRO[%.2f sec]: SATURATED BW XP(%s %s // %s %s) => %f (%f vs %f)%s\n",
190                   gras_os_time(),
191                   hosts[c],hosts[d],hosts[a],hosts[b],
192                   bw_sat[c][d]/bw[c][d],bw[c][d],bw_sat[c][d],
193
194                   (bw_sat[c][d]/bw[c][d] < 0.7) ? " THERE IS SOME INTERFERENCE !!!":
195                   ((bw[c][d]/bw_sat[c][d] < 0.7) ? " THERE IS SOME INTERFERENCE (and Im a cretin) !!!":
196                    ""));
197         }
198       }
199
200       if ((errcode=grasbw_saturate_stop(hosts[a],4000,hosts[b],4000))) {
201         fprintf(stderr,"MAESTRO: Error %s encountered while stopping saturation\n",
202                 xbt_error_name(errcode));
203         return -1;
204       }
205       fprintf(stderr,"Did an iteration on saturation pair in %ld sec (%.2f simulated sec)\n",
206               time(NULL)-begin, gras_os_time()-beginSim);
207     }
208   }
209
210   gras_os_sleep(5,0);
211   exit(0);
212 #if 0
213   return 0;
214   /* start saturation */
215   fprintf(stderr,"MAESTRO: Start saturation with size %d\n",msgSize);
216   if ((errcode=grasbw_saturate_start(argv[5],atoi(argv[6]),argv[7],atoi(argv[8]),msgSize*10,60))) {
217     fprintf(stderr,"MAESTRO: Error %s encountered while starting saturation\n",
218             xbt_error_name(errcode));
219     return 1;
220   }
221   fprintf(stderr,"MAESTRO: Saturation started\n");
222   gras_os_sleep(5,0);
223
224   /* test with saturation */
225   if ((errcode=grasbw_request(argv[1],atoi(argv[2]),argv[3],atoi(argv[4]),
226                               bufSize,expSize,msgSize,&sec,&bw))) {
227     fprintf(stderr,"MAESTRO: Error %s encountered while doing the test\n",xbt_error_name(errcode));
228     return 1;
229   }
230    
231   fprintf(stderr,"MAESTRO: Experience3 (%d ko in msgs of %d ko with saturation) took %f sec, achieving %f Mb/s\n",
232           expSize/1024,msgSize/1024,
233           sec,bw);
234
235   /* stop saturation */
236   if ((errcode=grasbw_saturate_stop(argv[5],atoi(argv[6]),argv[7],atoi(argv[8])))) {
237     fprintf(stderr,"MAESTRO: Error %s encountered while stopping saturation\n",
238             xbt_error_name(errcode));
239     return 1;
240   }
241
242   /* test without saturation */
243   if ((errcode=grasbw_request(argv[1],atoi(argv[2]),argv[3],atoi(argv[4]),
244                               bufSize,expSize,msgSize,&sec,&bw))) {
245     fprintf(stderr,"MAESTRO: Error %s encountered while doing the test\n",xbt_error_name(errcode));
246     return 1;
247   }
248    
249   fprintf(stderr,"MAESTRO: Experience4 (%d ko in msgs of %d ko, without saturation) took %f sec, achieving %f Mb/s\n",
250           expSize/1024,msgSize/1024,
251           sec,bw);
252
253   gras_os_sleep(5,0);
254 #endif
255   gras_socket_close(g->sock);
256    
257   return 0;
258 }