Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add function sg_platf_new_* for *route and bypass*route.
[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_linkctn_cb_list = NULL;   // of sg_platf_linkctn_cb_t
19 xbt_dynar_t sg_platf_router_cb_list = NULL; // of sg_platf_router_cb_t
20 xbt_dynar_t sg_platf_peer_cb_list = NULL; // of sg_platf_peer_cb_t
21 xbt_dynar_t sg_platf_cluster_cb_list = NULL; // of sg_platf_cluster_cb_t
22 xbt_dynar_t sg_platf_cabinet_cb_list = NULL; // of sg_platf_cluster_cb_t
23 xbt_dynar_t sg_platf_AS_begin_cb_list = NULL; //of sg_platf_AS_begin_cb_t
24 xbt_dynar_t sg_platf_AS_end_cb_list = NULL; //of void_f_void_t
25 xbt_dynar_t sg_platf_postparse_cb_list = NULL; // of void_f_void_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_storage_cb_list = NULL; // of sg_platf_storage_cb_t
33 xbt_dynar_t sg_platf_storage_type_cb_list = NULL; // of sg_platf_storage_cb_t
34 xbt_dynar_t sg_platf_mstorage_cb_list = NULL; // of sg_platf_storage_cb_t
35 xbt_dynar_t sg_platf_mount_cb_list = NULL; // of sg_platf_storage_cb_t
36
37 static int surf_parse_models_setup_already_called;
38
39 /* one RngStream for the platform, to respect some statistic rules */
40 static RngStream sg_platf_rng_stream = NULL;
41
42 /** Module management function: creates all internal data structures */
43 void sg_platf_init(void) {
44
45   //FIXME : Ugly, but useful...
46   if(sg_platf_host_cb_list)
47     return; //Already initialized, so do nothing...
48
49   sg_platf_host_cb_list = xbt_dynar_new(sizeof(sg_platf_host_cb_t), NULL);
50   sg_platf_host_link_cb_list = xbt_dynar_new(sizeof(sg_platf_host_link_cb_t), NULL);
51   sg_platf_router_cb_list = xbt_dynar_new(sizeof(sg_platf_router_cb_t), NULL);
52   sg_platf_link_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t), NULL);
53   sg_platf_linkctn_cb_list = xbt_dynar_new(sizeof(sg_platf_linkctn_cb_t), NULL);
54   sg_platf_peer_cb_list = xbt_dynar_new(sizeof(sg_platf_peer_cb_t), NULL);
55   sg_platf_cluster_cb_list = xbt_dynar_new(sizeof(sg_platf_cluster_cb_t), NULL);
56   sg_platf_cabinet_cb_list = xbt_dynar_new(sizeof(sg_platf_cabinet_cb_t), NULL);
57   sg_platf_postparse_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t),NULL);
58   sg_platf_AS_begin_cb_list = xbt_dynar_new(sizeof(sg_platf_AS_begin_cb_t),NULL);
59   sg_platf_AS_end_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
60
61   sg_platf_route_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
62   sg_platf_ASroute_cb_list = xbt_dynar_new(sizeof(sg_platf_ASroute_cb_t), NULL);
63   sg_platf_bypassRoute_cb_list = xbt_dynar_new(sizeof(sg_platf_bypassRoute_cb_t), NULL);
64   sg_platf_bypassASroute_cb_list = xbt_dynar_new(sizeof(sg_platf_bypassASroute_cb_t), NULL);
65
66   sg_platf_storage_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
67   sg_platf_storage_type_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
68   sg_platf_mstorage_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
69   sg_platf_mount_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
70 }
71 /** Module management function: frees all internal data structures */
72 void sg_platf_exit(void) {
73   xbt_dynar_free(&sg_platf_host_cb_list);
74   xbt_dynar_free(&sg_platf_host_link_cb_list);
75   xbt_dynar_free(&sg_platf_router_cb_list);
76   xbt_dynar_free(&sg_platf_link_cb_list);
77   xbt_dynar_free(&sg_platf_linkctn_cb_list);
78   xbt_dynar_free(&sg_platf_postparse_cb_list);
79   xbt_dynar_free(&sg_platf_peer_cb_list);
80   xbt_dynar_free(&sg_platf_cluster_cb_list);
81   xbt_dynar_free(&sg_platf_cabinet_cb_list);
82   xbt_dynar_free(&sg_platf_AS_begin_cb_list);
83   xbt_dynar_free(&sg_platf_AS_end_cb_list);
84
85   xbt_dynar_free(&sg_platf_route_cb_list);
86   xbt_dynar_free(&sg_platf_ASroute_cb_list);
87   xbt_dynar_free(&sg_platf_bypassRoute_cb_list);
88   xbt_dynar_free(&sg_platf_bypassASroute_cb_list);
89
90   xbt_dynar_free(&sg_platf_storage_cb_list);
91   xbt_dynar_free(&sg_platf_storage_type_cb_list);
92   xbt_dynar_free(&sg_platf_mstorage_cb_list);
93   xbt_dynar_free(&sg_platf_mount_cb_list);
94
95   /* make sure that we will reinit the models while loading the platf once reinited */
96   surf_parse_models_setup_already_called = 0;
97 }
98
99 void sg_platf_new_host(sg_platf_host_cbarg_t h){
100   unsigned int iterator;
101   sg_platf_host_cb_t fun;
102   xbt_dynar_foreach(sg_platf_host_cb_list, iterator, fun) {
103     fun(h);
104   }
105 }
106 void sg_platf_new_host_link(sg_platf_host_link_cbarg_t h){
107   unsigned int iterator;
108   sg_platf_host_link_cb_t fun;
109   xbt_dynar_foreach(sg_platf_host_link_cb_list, iterator, fun) {
110     fun(h);
111   }
112 }
113 void sg_platf_new_router(sg_platf_router_cbarg_t router) {
114   unsigned int iterator;
115   sg_platf_router_cb_t fun;
116   xbt_dynar_foreach(sg_platf_router_cb_list, iterator, fun) {
117     fun(router);
118   }
119 }
120 void sg_platf_new_link(sg_platf_link_cbarg_t link){
121   unsigned int iterator;
122   sg_platf_link_cb_t fun;
123   xbt_dynar_foreach(sg_platf_link_cb_list, iterator, fun) {
124     fun(link);
125   }
126 }
127
128 void sg_platf_new_linkctn(sg_platf_linkctn_cbarg_t linkctn){
129   unsigned int iterator;
130   sg_platf_linkctn_cb_t fun;
131   xbt_dynar_foreach(sg_platf_linkctn_cb_list, iterator, fun) {
132     fun(linkctn);
133   }
134 }
135 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer){
136   unsigned int iterator;
137   sg_platf_peer_cb_t fun;
138   xbt_dynar_foreach(sg_platf_peer_cb_list, iterator, fun) {
139     fun(peer);
140   }
141 }
142 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster){
143   unsigned int iterator;
144   sg_platf_cluster_cb_t fun;
145   xbt_dynar_foreach(sg_platf_cluster_cb_list, iterator, fun) {
146     fun(cluster);
147   }
148 }
149 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet){
150   unsigned int iterator;
151   sg_platf_cabinet_cb_t fun;
152   xbt_dynar_foreach(sg_platf_cabinet_cb_list, iterator, fun) {
153     fun(cabinet);
154   }
155 }
156 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage){
157   unsigned int iterator;
158   sg_platf_storage_cb_t fun;
159   xbt_dynar_foreach(sg_platf_storage_cb_list, iterator, fun) {
160     fun(storage);
161   }
162 }
163 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
164   unsigned int iterator;
165   sg_platf_storage_type_cb_t fun;
166   xbt_dynar_foreach(sg_platf_storage_type_cb_list, iterator, fun) {
167     fun(storage_type);
168   }
169 }
170 void sg_platf_new_mstorage(sg_platf_mstorage_cbarg_t mstorage){
171   unsigned int iterator;
172   sg_platf_mstorage_cb_t fun;
173   xbt_dynar_foreach(sg_platf_mstorage_cb_list, iterator, fun) {
174     fun(mstorage);
175   }
176 }
177 void sg_platf_new_mount(sg_platf_mount_cbarg_t mount){
178   unsigned int iterator;
179   sg_platf_mount_cb_t fun;
180   xbt_dynar_foreach(sg_platf_mount_cb_list, iterator, fun) {
181     fun(mount);
182   }
183 }
184 void sg_platf_new_route(sg_platf_route_cbarg_t route) {
185   unsigned int iterator;
186   sg_platf_route_cb_t fun;
187   xbt_dynar_foreach(sg_platf_route_cb_list, iterator, fun) {
188     fun(route);
189   }
190 }void sg_platf_new_ASroute(sg_platf_ASroute_cbarg_t ASroute) {
191   unsigned int iterator;
192   sg_platf_ASroute_cb_t fun;
193   xbt_dynar_foreach(sg_platf_ASroute_cb_list, iterator, fun) {
194     fun(ASroute);
195   }
196 }
197 void sg_platf_new_bypassRoute(sg_platf_bypassRoute_cbarg_t bypassRoute) {
198   unsigned int iterator;
199   sg_platf_bypassRoute_cb_t fun;
200   xbt_dynar_foreach(sg_platf_bypassRoute_cb_list, iterator, fun) {
201     fun(bypassRoute);
202   }
203 }void sg_platf_new_bypassASroute(sg_platf_bypassASroute_cbarg_t bypassASroute) {
204   unsigned int iterator;
205   sg_platf_bypassASroute_cb_t fun;
206   xbt_dynar_foreach(sg_platf_bypassASroute_cb_list, iterator, fun) {
207     fun(bypassASroute);
208   }
209 }
210 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
211
212 void sg_platf_end() {
213   unsigned int iterator;
214   void_f_void_t fun;
215   xbt_dynar_foreach(sg_platf_postparse_cb_list, iterator, fun) {
216     fun();
217   }
218 }
219
220 static int surf_parse_models_setup_already_called = 0;
221
222 void sg_platf_new_AS_begin(const char *id, int routing) {
223   unsigned int iterator;
224   sg_platf_AS_begin_cb_t fun;
225
226   if (!surf_parse_models_setup_already_called && !xbt_dynar_is_empty(sg_platf_AS_begin_cb_list)) {
227     /* Initialize the surf models. That must be done after we got all config, and before we need the models.
228      * That is, after the last <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
229      *
230      * I'm not sure for <trace> and <trace_connect>, there may be a bug here
231      * (FIXME: check it out by creating a file beginning with one of these tags)
232      * but cluster and peer create ASes internally, so putting the code in there is ok.
233      *
234      * We are also guarding against xbt_dynar_length(sg_platf_AS_begin_cb_list) because we don't
235      * want to initialize the models if we are parsing the file to get the deployment. That could happen if
236      * the same file would be used for platf and deploy: it'd contain AS tags even during the deploy parsing.
237      * Removing that guard would result of the models to get re-inited when parsing for deploy.
238      */
239     surf_parse_models_setup_already_called = 1;
240     surf_config_models_setup();
241   }
242
243   xbt_dynar_foreach(sg_platf_AS_begin_cb_list, iterator, fun) {
244     fun(id, routing);
245   }
246 }
247
248 void sg_platf_new_AS_end() {
249   unsigned int iterator;
250   void_f_void_t fun;
251   xbt_dynar_foreach(sg_platf_AS_end_cb_list, iterator, fun) {
252     fun();
253   }
254 }
255
256
257 void sg_platf_host_add_cb(sg_platf_host_cb_t fct) {
258   xbt_dynar_push(sg_platf_host_cb_list, &fct);
259 }
260 void sg_platf_host_link_add_cb(sg_platf_host_link_cb_t fct) {
261   xbt_dynar_push(sg_platf_host_link_cb_list, &fct);
262 }
263 void sg_platf_link_add_cb(sg_platf_link_cb_t fct) {
264   xbt_dynar_push(sg_platf_link_cb_list, &fct);
265 }
266 void sg_platf_linkctn_add_cb(sg_platf_linkctn_cb_t fct) {
267   xbt_dynar_push(sg_platf_linkctn_cb_list, &fct);
268 }
269 void sg_platf_router_add_cb(sg_platf_router_cb_t fct) {
270   xbt_dynar_push(sg_platf_router_cb_list, &fct);
271 }
272 void sg_platf_peer_add_cb(sg_platf_peer_cb_t fct) {
273   xbt_dynar_push(sg_platf_peer_cb_list, &fct);
274 }
275 void sg_platf_cluster_add_cb(sg_platf_cluster_cb_t fct) {
276   xbt_dynar_push(sg_platf_cluster_cb_list, &fct);
277 }
278 void sg_platf_cabinet_add_cb(sg_platf_cabinet_cb_t fct) {
279   xbt_dynar_push(sg_platf_cabinet_cb_list, &fct);
280 }
281 void sg_platf_postparse_add_cb(void_f_void_t fct) {
282   xbt_dynar_push(sg_platf_postparse_cb_list, &fct);
283 }
284 void sg_platf_AS_begin_add_cb(sg_platf_AS_begin_cb_t fct) {
285   xbt_dynar_push(sg_platf_AS_begin_cb_list, &fct);
286 }
287 void sg_platf_AS_end_add_cb(void_f_void_t fct) {
288   xbt_dynar_push(sg_platf_AS_end_cb_list, &fct);
289 }
290 void sg_platf_storage_add_cb(sg_platf_storage_cb_t fct) {
291   xbt_dynar_push(sg_platf_storage_cb_list, &fct);
292 }
293 void sg_platf_storage_type_add_cb(sg_platf_storage_type_cb_t fct) {
294   xbt_dynar_push(sg_platf_storage_type_cb_list, &fct);
295 }
296 void sg_platf_mstorage_add_cb(sg_platf_mstorage_cb_t fct) {
297   xbt_dynar_push(sg_platf_mstorage_cb_list, &fct);
298 }
299 void sg_platf_mount_add_cb(sg_platf_mount_cb_t fct) {
300   xbt_dynar_push(sg_platf_mount_cb_list, &fct);
301 }
302 void sg_platf_route_add_cb(sg_platf_route_cb_t fct) {
303   xbt_dynar_push(sg_platf_route_cb_list, &fct);
304 }
305 void sg_platf_ASroute_add_cb(sg_platf_ASroute_cb_t fct) {
306   xbt_dynar_push(sg_platf_ASroute_cb_list, &fct);
307 }
308 void sg_platf_bypassRoute_add_cb(sg_platf_bypassRoute_cb_t fct) {
309   xbt_dynar_push(sg_platf_bypassRoute_cb_list, &fct);
310 }
311 void sg_platf_bypassASroute_add_cb(sg_platf_bypassASroute_cb_t fct) {
312   xbt_dynar_push(sg_platf_bypassASroute_cb_list, &fct);
313 }
314
315 void sg_platf_rng_stream_init(unsigned long seed[6]) {
316   RngStream_SetPackageSeed(seed);
317   sg_platf_rng_stream = RngStream_CreateStream(NULL);
318 }
319
320 RngStream sg_platf_rng_stream_get(const char* id) {
321   RngStream stream = NULL;
322   unsigned int id_hash;
323
324   stream = RngStream_CopyStream(sg_platf_rng_stream);
325   id_hash = xbt_str_hash(id);
326   RngStream_AdvanceState(stream, 0, (long)id_hash);
327
328   return stream;
329 }