Logo AND Algorithmique Numérique Distribuée

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