Logo AND Algorithmique Numérique Distribuée

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