Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove link_list from surf_routing.c and add it into the structure of *route and...
[simgrid.git] / src / surf / sg_platf.c
1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/misc.h"
8 #include "xbt/log.h"
9 #include "xbt/str.h"
10 #include "xbt/dict.h"
11 #include "xbt/RngStream.h"
12 #include "simgrid/platf_interface.h"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
15 xbt_dynar_t sg_platf_host_cb_list = NULL;   // of sg_platf_host_cb_t
16 xbt_dynar_t sg_platf_host_link_cb_list = NULL;   // of sg_platf_host_link_cb_t
17 xbt_dynar_t sg_platf_link_cb_list = NULL;   // of sg_platf_link_cb_t
18 xbt_dynar_t sg_platf_router_cb_list = NULL; // of sg_platf_router_cb_t
19 xbt_dynar_t sg_platf_peer_cb_list = NULL; // of sg_platf_peer_cb_t
20 xbt_dynar_t sg_platf_cluster_cb_list = NULL; // of sg_platf_cluster_cb_t
21 xbt_dynar_t sg_platf_cabinet_cb_list = NULL; // of sg_platf_cluster_cb_t
22 xbt_dynar_t sg_platf_AS_begin_cb_list = NULL; //of sg_platf_AS_begin_cb_t
23 xbt_dynar_t sg_platf_AS_end_cb_list = NULL; //of void_f_void_t
24 xbt_dynar_t sg_platf_postparse_cb_list = NULL; // of void_f_void_t
25 xbt_dynar_t sg_platf_prop_cb_list = NULL; // of sg_platf_prop_cb_t
26
27 xbt_dynar_t sg_platf_route_cb_list = NULL; // of sg_platf_route_cb_t
28 xbt_dynar_t sg_platf_ASroute_cb_list = NULL; // of sg_platf_ASroute_cb_t
29 xbt_dynar_t sg_platf_bypassRoute_cb_list = NULL; // of sg_platf_bypassRoute_cb_t
30 xbt_dynar_t sg_platf_bypassASroute_cb_list = NULL; // of sg_platf_bypassASroute_cb_t
31
32 xbt_dynar_t sg_platf_trace_cb_list = NULL;
33 xbt_dynar_t sg_platf_trace_connect_cb_list = NULL;
34
35 xbt_dynar_t sg_platf_storage_cb_list = NULL; // of sg_platf_storage_cb_t
36 xbt_dynar_t sg_platf_storage_type_cb_list = NULL; // of sg_platf_storage_cb_t
37 xbt_dynar_t sg_platf_mstorage_cb_list = NULL; // of sg_platf_storage_cb_t
38 xbt_dynar_t sg_platf_mount_cb_list = NULL; // of sg_platf_storage_cb_t
39
40 xbt_dynar_t sg_platf_process_cb_list = NULL;
41
42 static int surf_parse_models_setup_already_called;
43
44 /* one RngStream for the platform, to respect some statistic rules */
45 static RngStream sg_platf_rng_stream = NULL;
46
47 /** Module management function: creates all internal data structures */
48 void sg_platf_init(void) {
49
50   //FIXME : Ugly, but useful...
51   if(sg_platf_host_cb_list)
52     return; //Already initialized, so do nothing...
53
54   sg_platf_host_cb_list = xbt_dynar_new(sizeof(sg_platf_host_cb_t), NULL);
55   sg_platf_host_link_cb_list = xbt_dynar_new(sizeof(sg_platf_host_link_cb_t), NULL);
56   sg_platf_router_cb_list = xbt_dynar_new(sizeof(sg_platf_router_cb_t), NULL);
57   sg_platf_link_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t), NULL);
58   sg_platf_peer_cb_list = xbt_dynar_new(sizeof(sg_platf_peer_cb_t), NULL);
59   sg_platf_cluster_cb_list = xbt_dynar_new(sizeof(sg_platf_cluster_cb_t), NULL);
60   sg_platf_cabinet_cb_list = xbt_dynar_new(sizeof(sg_platf_cabinet_cb_t), NULL);
61   sg_platf_postparse_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t),NULL);
62   sg_platf_AS_begin_cb_list = xbt_dynar_new(sizeof(sg_platf_AS_begin_cb_t),NULL);
63   sg_platf_AS_end_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
64   sg_platf_prop_cb_list = xbt_dynar_new(sizeof(sg_platf_prop_cb_t),NULL);
65
66   sg_platf_route_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
67   sg_platf_ASroute_cb_list = xbt_dynar_new(sizeof(sg_platf_ASroute_cb_t), NULL);
68   sg_platf_bypassRoute_cb_list = xbt_dynar_new(sizeof(sg_platf_bypassRoute_cb_t), NULL);
69   sg_platf_bypassASroute_cb_list = xbt_dynar_new(sizeof(sg_platf_bypassASroute_cb_t), NULL);
70
71   sg_platf_trace_cb_list = xbt_dynar_new(sizeof(sg_platf_trace_cb_t), NULL);
72   sg_platf_trace_connect_cb_list = xbt_dynar_new(sizeof(sg_platf_trace_connect_cb_t), NULL);
73
74   sg_platf_storage_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
75   sg_platf_storage_type_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
76   sg_platf_mstorage_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
77   sg_platf_mount_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
78
79   sg_platf_process_cb_list = xbt_dynar_new(sizeof(sg_platf_process_cb_t), NULL);
80 }
81 /** Module management function: frees all internal data structures */
82 void sg_platf_exit(void) {
83   xbt_dynar_free(&sg_platf_host_cb_list);
84   xbt_dynar_free(&sg_platf_host_link_cb_list);
85   xbt_dynar_free(&sg_platf_router_cb_list);
86   xbt_dynar_free(&sg_platf_link_cb_list);
87   xbt_dynar_free(&sg_platf_postparse_cb_list);
88   xbt_dynar_free(&sg_platf_peer_cb_list);
89   xbt_dynar_free(&sg_platf_cluster_cb_list);
90   xbt_dynar_free(&sg_platf_cabinet_cb_list);
91   xbt_dynar_free(&sg_platf_AS_begin_cb_list);
92   xbt_dynar_free(&sg_platf_AS_end_cb_list);
93   xbt_dynar_free(&sg_platf_prop_cb_list);
94
95   xbt_dynar_free(&sg_platf_trace_cb_list);
96   xbt_dynar_free(&sg_platf_trace_connect_cb_list);
97
98   xbt_dynar_free(&sg_platf_route_cb_list);
99   xbt_dynar_free(&sg_platf_ASroute_cb_list);
100   xbt_dynar_free(&sg_platf_bypassRoute_cb_list);
101   xbt_dynar_free(&sg_platf_bypassASroute_cb_list);
102
103   xbt_dynar_free(&sg_platf_storage_cb_list);
104   xbt_dynar_free(&sg_platf_storage_type_cb_list);
105   xbt_dynar_free(&sg_platf_mstorage_cb_list);
106   xbt_dynar_free(&sg_platf_mount_cb_list);
107
108   xbt_dynar_free(&sg_platf_process_cb_list);
109
110   /* make sure that we will reinit the models while loading the platf once reinited */
111   surf_parse_models_setup_already_called = 0;
112 }
113
114 void sg_platf_new_host(sg_platf_host_cbarg_t h){
115   unsigned int iterator;
116   sg_platf_host_cb_t fun;
117   xbt_dynar_foreach(sg_platf_host_cb_list, iterator, fun) {
118     fun(h);
119   }
120 }
121 void sg_platf_new_host_link(sg_platf_host_link_cbarg_t h){
122   unsigned int iterator;
123   sg_platf_host_link_cb_t fun;
124   xbt_dynar_foreach(sg_platf_host_link_cb_list, iterator, fun) {
125     fun(h);
126   }
127 }
128 void sg_platf_new_router(sg_platf_router_cbarg_t router) {
129   unsigned int iterator;
130   sg_platf_router_cb_t fun;
131   xbt_dynar_foreach(sg_platf_router_cb_list, iterator, fun) {
132     fun(router);
133   }
134 }
135 void sg_platf_new_link(sg_platf_link_cbarg_t link){
136   unsigned int iterator;
137   sg_platf_link_cb_t fun;
138   xbt_dynar_foreach(sg_platf_link_cb_list, iterator, fun) {
139     fun(link);
140   }
141 }
142
143 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer){
144   unsigned int iterator;
145   sg_platf_peer_cb_t fun;
146   xbt_dynar_foreach(sg_platf_peer_cb_list, iterator, fun) {
147     fun(peer);
148   }
149 }
150 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster){
151   unsigned int iterator;
152   sg_platf_cluster_cb_t fun;
153   xbt_dynar_foreach(sg_platf_cluster_cb_list, iterator, fun) {
154     fun(cluster);
155   }
156 }
157 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet){
158   unsigned int iterator;
159   sg_platf_cabinet_cb_t fun;
160   xbt_dynar_foreach(sg_platf_cabinet_cb_list, iterator, fun) {
161     fun(cabinet);
162   }
163 }
164 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage){
165   unsigned int iterator;
166   sg_platf_storage_cb_t fun;
167   xbt_dynar_foreach(sg_platf_storage_cb_list, iterator, fun) {
168     fun(storage);
169   }
170 }
171 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
172   unsigned int iterator;
173   sg_platf_storage_type_cb_t fun;
174   xbt_dynar_foreach(sg_platf_storage_type_cb_list, iterator, fun) {
175     fun(storage_type);
176   }
177 }
178 void sg_platf_new_mstorage(sg_platf_mstorage_cbarg_t mstorage){
179   unsigned int iterator;
180   sg_platf_mstorage_cb_t fun;
181   xbt_dynar_foreach(sg_platf_mstorage_cb_list, iterator, fun) {
182     fun(mstorage);
183   }
184 }
185 void sg_platf_new_mount(sg_platf_mount_cbarg_t mount){
186   unsigned int iterator;
187   sg_platf_mount_cb_t fun;
188   xbt_dynar_foreach(sg_platf_mount_cb_list, iterator, fun) {
189     fun(mount);
190   }
191 }
192 void sg_platf_new_route(sg_platf_route_cbarg_t route) {
193   unsigned int iterator;
194   sg_platf_route_cb_t fun;
195   xbt_dynar_foreach(sg_platf_route_cb_list, iterator, fun) {
196     fun(route);
197   }
198 }void sg_platf_new_ASroute(sg_platf_ASroute_cbarg_t ASroute) {
199   unsigned int iterator;
200   sg_platf_ASroute_cb_t fun;
201   xbt_dynar_foreach(sg_platf_ASroute_cb_list, iterator, fun) {
202     fun(ASroute);
203   }
204 }
205 void sg_platf_new_bypassRoute(sg_platf_bypassRoute_cbarg_t bypassRoute) {
206   unsigned int iterator;
207   sg_platf_bypassRoute_cb_t fun;
208   xbt_dynar_foreach(sg_platf_bypassRoute_cb_list, iterator, fun) {
209     fun(bypassRoute);
210   }
211 }void sg_platf_new_bypassASroute(sg_platf_bypassASroute_cbarg_t bypassASroute) {
212   unsigned int iterator;
213   sg_platf_bypassASroute_cb_t fun;
214   xbt_dynar_foreach(sg_platf_bypassASroute_cb_list, iterator, fun) {
215     fun(bypassASroute);
216   }
217 }
218 void sg_platf_new_prop(sg_platf_prop_cbarg_t prop) {
219   unsigned int iterator;
220   sg_platf_prop_cb_t fun;
221   xbt_dynar_foreach(sg_platf_prop_cb_list, iterator, fun) {
222     fun(prop);
223   }
224 }
225 void sg_platf_new_trace(sg_platf_trace_cbarg_t trace) {
226   unsigned int iterator;
227   sg_platf_trace_cb_t fun;
228   xbt_dynar_foreach(sg_platf_trace_cb_list, iterator, fun) {
229     fun(trace);
230   }
231 }
232 void sg_platf_new_trace_connect(sg_platf_trace_connect_cbarg_t trace_connect) {
233   unsigned int iterator;
234   sg_platf_trace_connect_cb_t fun;
235   xbt_dynar_foreach(sg_platf_trace_connect_cb_list, iterator, fun) {
236     fun(trace_connect);
237   }
238 }
239 void sg_platf_new_process(sg_platf_process_cbarg_t process){
240   unsigned int iterator;
241   sg_platf_process_cb_t fun;
242   xbt_dynar_foreach(sg_platf_process_cb_list, iterator, fun) {
243     fun(process);
244   }
245 }
246
247 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
248
249 void sg_platf_end() {
250   unsigned int iterator;
251   void_f_void_t fun;
252   xbt_dynar_foreach(sg_platf_postparse_cb_list, iterator, fun) {
253     fun();
254   }
255 }
256
257 static int surf_parse_models_setup_already_called = 0;
258
259 void sg_platf_new_AS_begin(const char *id, int routing) {
260   unsigned int iterator;
261   sg_platf_AS_begin_cb_t fun;
262
263   if (!surf_parse_models_setup_already_called && !xbt_dynar_is_empty(sg_platf_AS_begin_cb_list)) {
264     /* Initialize the surf models. That must be done after we got all config, and before we need the models.
265      * That is, after the last <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
266      *
267      * I'm not sure for <trace> and <trace_connect>, there may be a bug here
268      * (FIXME: check it out by creating a file beginning with one of these tags)
269      * but cluster and peer create ASes internally, so putting the code in there is ok.
270      *
271      * We are also guarding against xbt_dynar_length(sg_platf_AS_begin_cb_list) because we don't
272      * want to initialize the models if we are parsing the file to get the deployment. That could happen if
273      * the same file would be used for platf and deploy: it'd contain AS tags even during the deploy parsing.
274      * Removing that guard would result of the models to get re-inited when parsing for deploy.
275      */
276     surf_parse_models_setup_already_called = 1;
277     surf_config_models_setup();
278   }
279
280   xbt_dynar_foreach(sg_platf_AS_begin_cb_list, iterator, fun) {
281     fun(id, routing);
282   }
283 }
284
285 void sg_platf_new_AS_end() {
286   unsigned int iterator;
287   void_f_void_t fun;
288   xbt_dynar_foreach(sg_platf_AS_end_cb_list, iterator, fun) {
289     fun();
290   }
291 }
292
293
294 void sg_platf_host_add_cb(sg_platf_host_cb_t fct) {
295   xbt_dynar_push(sg_platf_host_cb_list, &fct);
296 }
297 void sg_platf_host_link_add_cb(sg_platf_host_link_cb_t fct) {
298   xbt_dynar_push(sg_platf_host_link_cb_list, &fct);
299 }
300 void sg_platf_link_add_cb(sg_platf_link_cb_t fct) {
301   xbt_dynar_push(sg_platf_link_cb_list, &fct);
302 }
303 void sg_platf_router_add_cb(sg_platf_router_cb_t fct) {
304   xbt_dynar_push(sg_platf_router_cb_list, &fct);
305 }
306 void sg_platf_peer_add_cb(sg_platf_peer_cb_t fct) {
307   xbt_dynar_push(sg_platf_peer_cb_list, &fct);
308 }
309 void sg_platf_cluster_add_cb(sg_platf_cluster_cb_t fct) {
310   xbt_dynar_push(sg_platf_cluster_cb_list, &fct);
311 }
312 void sg_platf_cabinet_add_cb(sg_platf_cabinet_cb_t fct) {
313   xbt_dynar_push(sg_platf_cabinet_cb_list, &fct);
314 }
315 void sg_platf_postparse_add_cb(void_f_void_t fct) {
316   xbt_dynar_push(sg_platf_postparse_cb_list, &fct);
317 }
318 void sg_platf_AS_begin_add_cb(sg_platf_AS_begin_cb_t fct) {
319   xbt_dynar_push(sg_platf_AS_begin_cb_list, &fct);
320 }
321 void sg_platf_AS_end_add_cb(void_f_void_t fct) {
322   xbt_dynar_push(sg_platf_AS_end_cb_list, &fct);
323 }
324 void sg_platf_storage_add_cb(sg_platf_storage_cb_t fct) {
325   xbt_dynar_push(sg_platf_storage_cb_list, &fct);
326 }
327 void sg_platf_storage_type_add_cb(sg_platf_storage_type_cb_t fct) {
328   xbt_dynar_push(sg_platf_storage_type_cb_list, &fct);
329 }
330 void sg_platf_mstorage_add_cb(sg_platf_mstorage_cb_t fct) {
331   xbt_dynar_push(sg_platf_mstorage_cb_list, &fct);
332 }
333 void sg_platf_mount_add_cb(sg_platf_mount_cb_t fct) {
334   xbt_dynar_push(sg_platf_mount_cb_list, &fct);
335 }
336 void sg_platf_route_add_cb(sg_platf_route_cb_t fct) {
337   xbt_dynar_push(sg_platf_route_cb_list, &fct);
338 }
339 void sg_platf_ASroute_add_cb(sg_platf_ASroute_cb_t fct) {
340   xbt_dynar_push(sg_platf_ASroute_cb_list, &fct);
341 }
342 void sg_platf_bypassRoute_add_cb(sg_platf_bypassRoute_cb_t fct) {
343   xbt_dynar_push(sg_platf_bypassRoute_cb_list, &fct);
344 }
345 void sg_platf_bypassASroute_add_cb(sg_platf_bypassASroute_cb_t fct) {
346   xbt_dynar_push(sg_platf_bypassASroute_cb_list, &fct);
347 }
348 void sg_platf_prop_add_cb(sg_platf_prop_cb_t fct) {
349   xbt_dynar_push(sg_platf_prop_cb_list, &fct);
350 }
351 void sg_platf_trace_add_cb(sg_platf_trace_cb_t fct) {
352   xbt_dynar_push(sg_platf_trace_cb_list, &fct);
353 }
354 void sg_platf_trace_connect_add_cb(sg_platf_trace_connect_cb_t fct) {
355   xbt_dynar_push(sg_platf_trace_connect_cb_list, &fct);
356 }
357 void sg_platf_rng_stream_init(unsigned long seed[6]) {
358   RngStream_SetPackageSeed(seed);
359   sg_platf_rng_stream = RngStream_CreateStream(NULL);
360 }
361 void sg_platf_process_add_cb(sg_platf_process_cb_t fct) {
362   xbt_dynar_push(sg_platf_process_cb_list, &fct);
363 }
364
365 RngStream sg_platf_rng_stream_get(const char* id) {
366   RngStream stream = NULL;
367   unsigned int id_hash;
368
369   stream = RngStream_CopyStream(sg_platf_rng_stream);
370   id_hash = xbt_str_hash(id);
371   RngStream_AdvanceState(stream, 0, (long)id_hash);
372
373   return stream;
374 }