Logo AND Algorithmique Numérique Distribuée

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