Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
13d2be176170d71fd00b6eec564d0c855dde2a09
[simgrid.git] / examples / amok / alnem / alnem.c
1 /* ALNeM itself                                                             */
2
3 /* Copyright (c) 2003 Martin Quinson. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <signal.h>
12 #include <time.h>
13
14 #include <gras.h>
15
16 #include <tbx_graph.h> /* alvin's graph toolbox (+ reconstruction algorithm) */
17
18 /* **********************************************************************
19  * Sensor code
20  * **********************************************************************/
21
22 /* Global private data */
23 typedef struct {
24   gras_sock_t *sock;
25 } sensor_data_t;
26
27 /* Function prototypes */
28 int sensor (int argc,char *argv[]);
29
30 int sensor (int argc,char *argv[]) {
31   xbt_error_t errcode;
32   sensor_data_t *g=gras_userdata_new(sensor_data_t);  
33
34   if ((errcode=gras_sock_server_open(4000,4000,&(g->sock)))) { 
35     fprintf(stderr,"Sensor: Error %s encountered while opening the server socket\n",xbt_error_name(errcode));
36     return 1;
37   }
38
39   if (grasbw_register_messages()) {
40     gras_sock_close(g->sock);
41     return 1;
42   }
43
44   while (1) {
45     if ((errcode=gras_msg_handle(3600.0)) && errcode != timeout_error) {
46       fprintf(stderr,"Sensor: Error '%s' while handling message\n",
47               xbt_error_name(errcode));
48     }
49   }
50
51   gras_sleep(5,0);
52   return gras_sock_close(g->sock);
53 }
54
55 /* **********************************************************************
56  * Maestro code
57  * **********************************************************************/
58
59 /* Global private data */
60 typedef struct {
61   gras_sock_t *sock;
62 } maestro_data_t;
63
64 /* Function prototypes */
65 int maestro (int argc,char *argv[]);
66
67 #define MAXHOSTS 100
68
69 #define INTERF(graph,table,a,u,b,v) INTERFERENCE(table,\
70                                                 TBX_Graph_nodeSearch(graph,a),\
71                                                 TBX_Graph_nodeSearch(graph,u),\
72                                                 TBX_Graph_nodeSearch(graph,b),\
73                                                 TBX_Graph_nodeSearch(graph,v))
74
75 int maestro(int argc,char *argv[]) {
76   int bufSize=32 * 1024;
77   int expSize= 1024 * 1024;
78   int msgSize=expSize;
79   int satSize=msgSize * 100;
80   double dummy,beginSim;
81   xbt_error_t errcode;
82   maestro_data_t *g=gras_userdata_new(maestro_data_t);
83
84   double bw[MAXHOSTS][MAXHOSTS];
85   double bw_sat[MAXHOSTS][MAXHOSTS];
86
87   int a,b,c,d,begin;
88
89   TBX_Graph_t graph = TBX_Graph_newGraph("Essai",0,NULL); /* a dummy graph containing all hosts */
90   TBX_FIFO_t host_fifo = TBX_FIFO_newFIFO();
91   TBX_InterfTable_t interf = NULL; /* the measured interferences */
92   TBX_Graph_t builded_graph = NULL; /* the graph builded from the interferences */
93
94   /* basics setups */
95   if (argc>MAXHOSTS) {
96     fprintf(stderr,"You gave more than %d sensors for this experiment. Increase the MAX HOSTS constant in alnem code to be bigger than this number.\n",argc);
97     return 1;
98   }
99
100   if ((errcode=gras_sock_server_open(4000,5000,&(g->sock)))) { 
101     fprintf(stderr,"MAESTRO: Error %s encountered while opening the server socket\n",xbt_error_name(errcode));
102     return 1;
103   }
104
105   if (grasbw_register_messages()) {
106     gras_sock_close(g->sock);
107     return 1;
108   }
109
110   for (a=1; a<argc; a++) {
111     TBX_FIFO_insert(host_fifo,TBX_Graph_newNode(graph,argv[a], NULL));
112   }
113   TBX_Graph_fixNodeNumbering(graph);
114   interf=TBX_Graph_newInterfTable(host_fifo);
115   TBX_Graph_graphInterfTableInit(graph,interf);
116
117   /* measure the bandwidths */
118   begin=time(NULL);
119   beginSim=gras_time();
120   for (a=1; a<argc; a++) {
121     for (b=1; b<argc; b++) {
122       int test=0;
123        
124       if (a==b) continue;
125       fprintf(stderr,"BW XP(%s %s)=",argv[a],argv[b]); 
126       while ((errcode=grasbw_request(argv[a],4000,argv[b],4000,bufSize,expSize,msgSize,
127                                   &dummy,&(bw[a][b]))) && (errcode == timeout_error)) {
128          test++;
129          if (test==10) {
130             fprintf(stderr,"MAESTRO: 10 Timeouts; giving up\n");
131             return 1;
132          }
133       }
134       if (errcode) {
135         fprintf(stderr,"MAESTRO: Error %s encountered while doing the test\n",xbt_error_name(errcode));
136         return 1;
137       }
138       fprintf(stderr,"%f Mb/s in %f sec\n",bw[a][b],dummy);
139     }
140   }
141   fprintf(stderr,"Did all BW tests in %ld sec (%.2f simulated sec)\n",
142           time(NULL)-begin,gras_time()-beginSim);
143       
144   /* saturation tests */
145   for (a=1; a<argc; a++) {
146     for (b=1; b<argc; b++) {
147       for (c=1 ;c<argc; c++) {
148         INTERF(graph,interf,argv[a],argv[c],argv[b],argv[c])= 1;
149       }
150     }
151   }
152           
153   for (a=1; a<argc; a++) {
154     for (b=1; b<argc; b++) {
155       if (a==b) continue;
156         
157       if ((errcode=grasbw_saturate_start(argv[a],4000,argv[b],4000,satSize,360000000))) {
158         fprintf(stderr,"MAESTRO: Error %s encountered while starting saturation\n",
159                 xbt_error_name(errcode));
160         return -1;
161       }
162       gras_sleep(1,0);
163
164       begin=time(NULL);
165       beginSim=gras_time();
166       for (c=1 ;c<argc; c++) {
167         if (a==c || b==c) continue;
168
169         for (d=1 ;d<argc; d++) {
170           if (a==d || b==d || c==d) continue;
171           
172           if ((errcode=grasbw_request(argv[c],4000,argv[d],4000,bufSize,expSize,msgSize,
173                                       &dummy,&(bw_sat[c][d])))) {
174             fprintf(stderr,"MAESTRO: Error %s encountered in test\n",xbt_error_name(errcode));
175             return 1;
176           }
177           fprintf(stderr, "MAESTRO[%.2f sec]: SATURATED BW XP(%s %s // %s %s) => %f (%f vs %f)%s\n",
178                   gras_time(),
179                   argv[c],argv[d],argv[a],argv[b],
180                   bw_sat[c][d]/bw[c][d],bw[c][d],bw_sat[c][d],
181
182                   (bw_sat[c][d]/bw[c][d] < 0.75) ? " THERE IS SOME INTERFERENCE !!!":"");
183           INTERF(graph,interf,argv[c],argv[d],argv[a],argv[b])= 
184             (bw_sat[c][d]/bw[c][d] < 0.75) ? 1 : 0;
185         }
186       }
187
188       if ((errcode=grasbw_saturate_stop(argv[a],4000,argv[b],4000))) {
189         fprintf(stderr,"MAESTRO: Error %s encountered while stopping saturation\n",
190                 xbt_error_name(errcode));
191         return -1;
192       }
193       fprintf(stderr,"Did an iteration on saturation pair in %ld sec (%.2f simulated sec)\n",
194               time(NULL)-begin, gras_time()-beginSim);
195     }
196   }
197
198   /* reconstruct the graph */
199   TBX_Graph_interferenceTableDump(interf);
200   TBX_Graph_interferenceTableSave(interf,"interference.dat");
201   begin=time(NULL);
202   fprintf(stderr, "MAESTRO: Reconstruct the graph... ");
203   builded_graph = TBX_Graph_exploreInterference(interf);
204   TBX_Graph_exportToGraphViz(builded_graph, "toto.dot");
205   fprintf(stderr, "done (took %d sec)",(int)time(NULL));
206
207   /* end */
208   TBX_Graph_freeGraph(graph,NULL,NULL,NULL);
209   TBX_Graph_freeGraph(builded_graph,NULL,NULL,NULL);
210   TBX_Graph_freeInterfTable(interf);
211
212   gras_sleep(5,0);
213   exit(0); /* FIXME: There is a bug in MSG preventing me from terminating this server properly */
214   return gras_sock_close(g->sock);
215 }