Logo AND Algorithmique Numérique Distribuée

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