1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011. The SimGrid Team.
2 * All rights reserved. */
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. */
11 #include "xbt/RngStream.h"
12 #include "simgrid/platf_interface.h"
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
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
31 static int surf_parse_models_setup_already_called;
33 /* one RngStream for the platform, to respect some statistic rules */
34 static RngStream sg_platf_rng_stream = NULL;
36 /** Module management function: creates all internal data structures */
37 void sg_platf_init(void) {
38 sg_platf_host_cb_list = xbt_dynar_new(sizeof(sg_platf_host_cb_t), NULL);
39 sg_platf_host_link_cb_list = xbt_dynar_new(sizeof(sg_platf_host_link_cb_t), NULL);
40 sg_platf_router_cb_list = xbt_dynar_new(sizeof(sg_platf_host_cb_t), NULL);
41 sg_platf_link_cb_list = xbt_dynar_new(sizeof(sg_platf_host_cb_t), NULL);
42 sg_platf_peer_cb_list = xbt_dynar_new(sizeof(sg_platf_peer_cb_t), NULL);
43 sg_platf_cluster_cb_list = xbt_dynar_new(sizeof(sg_platf_cluster_cb_t), NULL);
44 sg_platf_cabinet_cb_list = xbt_dynar_new(sizeof(sg_platf_cabinet_cb_t), NULL);
45 sg_platf_postparse_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t),NULL);
46 sg_platf_AS_begin_cb_list = xbt_dynar_new(sizeof(sg_platf_AS_begin_cb_t),NULL);
47 sg_platf_AS_end_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
49 sg_platf_storage_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
50 sg_platf_storage_type_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
51 sg_platf_mstorage_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
52 sg_platf_mount_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
54 /** Module management function: frees all internal data structures */
55 void sg_platf_exit(void) {
56 xbt_dynar_free(&sg_platf_host_cb_list);
57 xbt_dynar_free(&sg_platf_host_link_cb_list);
58 xbt_dynar_free(&sg_platf_router_cb_list);
59 xbt_dynar_free(&sg_platf_link_cb_list);
60 xbt_dynar_free(&sg_platf_postparse_cb_list);
61 xbt_dynar_free(&sg_platf_peer_cb_list);
62 xbt_dynar_free(&sg_platf_cluster_cb_list);
63 xbt_dynar_free(&sg_platf_cabinet_cb_list);
64 xbt_dynar_free(&sg_platf_AS_begin_cb_list);
65 xbt_dynar_free(&sg_platf_AS_end_cb_list);
67 xbt_dynar_free(&sg_platf_storage_cb_list);
68 xbt_dynar_free(&sg_platf_storage_type_cb_list);
69 xbt_dynar_free(&sg_platf_mstorage_cb_list);
70 xbt_dynar_free(&sg_platf_mount_cb_list);
72 /* make sure that we will reinit the models while loading the platf once reinited */
73 surf_parse_models_setup_already_called = 0;
76 void sg_platf_new_host(sg_platf_host_cbarg_t h){
77 unsigned int iterator;
78 sg_platf_host_cb_t fun;
79 xbt_dynar_foreach(sg_platf_host_cb_list, iterator, fun) {
83 void sg_platf_new_host_link(sg_platf_host_link_cbarg_t h){
84 unsigned int iterator;
85 sg_platf_host_link_cb_t fun;
86 xbt_dynar_foreach(sg_platf_host_link_cb_list, iterator, fun) {
90 void sg_platf_new_router(sg_platf_router_cbarg_t router) {
91 unsigned int iterator;
92 sg_platf_router_cb_t fun;
93 xbt_dynar_foreach(sg_platf_router_cb_list, iterator, fun) {
97 void sg_platf_new_link(sg_platf_link_cbarg_t link){
98 unsigned int iterator;
99 sg_platf_link_cb_t fun;
100 xbt_dynar_foreach(sg_platf_link_cb_list, iterator, fun) {
104 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer){
105 unsigned int iterator;
106 sg_platf_peer_cb_t fun;
107 xbt_dynar_foreach(sg_platf_peer_cb_list, iterator, fun) {
111 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster){
112 unsigned int iterator;
113 sg_platf_cluster_cb_t fun;
114 xbt_dynar_foreach(sg_platf_cluster_cb_list, iterator, fun) {
118 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet){
119 unsigned int iterator;
120 sg_platf_cabinet_cb_t fun;
121 xbt_dynar_foreach(sg_platf_cabinet_cb_list, iterator, fun) {
125 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage){
126 unsigned int iterator;
127 sg_platf_storage_cb_t fun;
128 xbt_dynar_foreach(sg_platf_storage_cb_list, iterator, fun) {
132 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
133 unsigned int iterator;
134 sg_platf_storage_type_cb_t fun;
135 xbt_dynar_foreach(sg_platf_storage_type_cb_list, iterator, fun) {
139 void sg_platf_new_mstorage(sg_platf_mstorage_cbarg_t mstorage){
140 unsigned int iterator;
141 sg_platf_mstorage_cb_t fun;
142 xbt_dynar_foreach(sg_platf_mstorage_cb_list, iterator, fun) {
146 void sg_platf_new_mount(sg_platf_mount_cbarg_t mount){
147 unsigned int iterator;
148 sg_platf_mount_cb_t fun;
149 xbt_dynar_foreach(sg_platf_mount_cb_list, iterator, fun) {
154 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
156 void sg_platf_end() {
157 unsigned int iterator;
159 xbt_dynar_foreach(sg_platf_postparse_cb_list, iterator, fun) {
164 static int surf_parse_models_setup_already_called = 0;
166 void sg_platf_new_AS_begin(const char *id, int routing) {
167 unsigned int iterator;
168 sg_platf_AS_begin_cb_t fun;
170 if (!surf_parse_models_setup_already_called && !xbt_dynar_is_empty(sg_platf_AS_begin_cb_list)) {
171 /* Initialize the surf models. That must be done after we got all config, and before we need the models.
172 * That is, after the last <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
174 * I'm not sure for <trace> and <trace_connect>, there may be a bug here
175 * (FIXME: check it out by creating a file beginning with one of these tags)
176 * but cluster and peer create ASes internally, so putting the code in there is ok.
178 * We are also guarding against xbt_dynar_length(sg_platf_AS_begin_cb_list) because we don't
179 * want to initialize the models if we are parsing the file to get the deployment. That could happen if
180 * the same file would be used for platf and deploy: it'd contain AS tags even during the deploy parsing.
181 * Removing that guard would result of the models to get re-inited when parsing for deploy.
183 surf_parse_models_setup_already_called = 1;
184 surf_config_models_setup();
187 xbt_dynar_foreach(sg_platf_AS_begin_cb_list, iterator, fun) {
192 void sg_platf_new_AS_end() {
193 unsigned int iterator;
195 xbt_dynar_foreach(sg_platf_AS_end_cb_list, iterator, fun) {
201 void sg_platf_host_add_cb(sg_platf_host_cb_t fct) {
202 xbt_dynar_push(sg_platf_host_cb_list, &fct);
204 void sg_platf_host_link_add_cb(sg_platf_host_link_cb_t fct) {
205 xbt_dynar_push(sg_platf_host_link_cb_list, &fct);
207 void sg_platf_link_add_cb(sg_platf_link_cb_t fct) {
208 xbt_dynar_push(sg_platf_link_cb_list, &fct);
210 void sg_platf_router_add_cb(sg_platf_router_cb_t fct) {
211 xbt_dynar_push(sg_platf_router_cb_list, &fct);
213 void sg_platf_peer_add_cb(sg_platf_peer_cb_t fct) {
214 xbt_dynar_push(sg_platf_peer_cb_list, &fct);
216 void sg_platf_cluster_add_cb(sg_platf_cluster_cb_t fct) {
217 xbt_dynar_push(sg_platf_cluster_cb_list, &fct);
219 void sg_platf_cabinet_add_cb(sg_platf_cabinet_cb_t fct) {
220 xbt_dynar_push(sg_platf_cabinet_cb_list, &fct);
222 void sg_platf_postparse_add_cb(void_f_void_t fct) {
223 xbt_dynar_push(sg_platf_postparse_cb_list, &fct);
225 void sg_platf_AS_begin_add_cb(sg_platf_AS_begin_cb_t fct) {
226 xbt_dynar_push(sg_platf_AS_begin_cb_list, &fct);
228 void sg_platf_AS_end_add_cb(void_f_void_t fct) {
229 xbt_dynar_push(sg_platf_AS_end_cb_list, &fct);
231 void sg_platf_storage_add_cb(sg_platf_storage_cb_t fct) {
232 xbt_dynar_push(sg_platf_storage_cb_list, &fct);
234 void sg_platf_storage_type_add_cb(sg_platf_storage_type_cb_t fct) {
235 xbt_dynar_push(sg_platf_storage_type_cb_list, &fct);
237 void sg_platf_mstorage_add_cb(sg_platf_mstorage_cb_t fct) {
238 xbt_dynar_push(sg_platf_mstorage_cb_list, &fct);
240 void sg_platf_mount_add_cb(sg_platf_mount_cb_t fct) {
241 xbt_dynar_push(sg_platf_mount_cb_list, &fct);
245 void sg_platf_rng_stream_init(unsigned long seed[6]) {
246 RngStream_SetPackageSeed(seed);
247 sg_platf_rng_stream = RngStream_CreateStream(NULL);
250 RngStream sg_platf_rng_stream_get(const char* id) {
251 RngStream stream = NULL;
252 unsigned int id_hash;
254 stream = RngStream_CopyStream(sg_platf_rng_stream);
255 id_hash = xbt_str_hash(id);
256 RngStream_AdvanceState(stream, 0, (long)id_hash);