Logo AND Algorithmique Numérique Distribuée

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