Logo AND Algorithmique Numérique Distribuée

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