1 /* Copyright (c) 2006-2014. 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"
13 #include "surf/surf_routing.h"
15 #include "cpu_interface.hpp"
16 #include "host_interface.hpp"
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
19 xbt_dynar_t sg_platf_host_cb_list = NULL; // of sg_platf_host_cb_t
20 xbt_dynar_t sg_platf_host_link_cb_list = NULL; // of sg_platf_host_link_cb_t
21 xbt_dynar_t sg_platf_link_cb_list = NULL; // of sg_platf_link_cb_t
22 xbt_dynar_t sg_platf_router_cb_list = NULL; // of sg_platf_router_cb_t
23 xbt_dynar_t sg_platf_peer_cb_list = NULL; // of sg_platf_peer_cb_t
24 xbt_dynar_t sg_platf_cluster_cb_list = NULL; // of sg_platf_cluster_cb_t
25 xbt_dynar_t sg_platf_cabinet_cb_list = NULL; // of sg_platf_cluster_cb_t
26 xbt_dynar_t sg_platf_AS_begin_cb_list = NULL; //of sg_platf_AS_begin_cb_t
27 xbt_dynar_t sg_platf_AS_end_cb_list = NULL; //of void_f_void_t
28 xbt_dynar_t sg_platf_postparse_cb_list = NULL; // of void_f_void_t
29 xbt_dynar_t sg_platf_prop_cb_list = NULL; // of sg_platf_prop_cb_t
31 xbt_dynar_t sg_platf_route_cb_list = NULL; // of sg_platf_route_cb_t
32 xbt_dynar_t sg_platf_ASroute_cb_list = NULL; // of sg_platf_ASroute_cb_t
33 xbt_dynar_t sg_platf_bypassRoute_cb_list = NULL; // of sg_platf_bypassRoute_cb_t
34 xbt_dynar_t sg_platf_bypassASroute_cb_list = NULL; // of sg_platf_bypassASroute_cb_t
36 xbt_dynar_t sg_platf_trace_cb_list = NULL;
37 xbt_dynar_t sg_platf_trace_connect_cb_list = NULL;
39 xbt_dynar_t sg_platf_storage_cb_list = NULL; // of sg_platf_storage_cb_t
40 xbt_dynar_t sg_platf_storage_type_cb_list = NULL; // of sg_platf_storage_cb_t
41 xbt_dynar_t sg_platf_mstorage_cb_list = NULL; // of sg_platf_storage_cb_t
42 xbt_dynar_t sg_platf_mount_cb_list = NULL; // of sg_platf_storage_cb_t
44 xbt_dynar_t sg_platf_process_cb_list = NULL;
46 /* ***************************************** */
47 /* TUTORIAL: New TAG */
49 xbt_dynar_t sg_platf_gpu_cb_list = NULL;
50 /* ***************************************** */
53 static int surf_parse_models_setup_already_called = 0;
55 /* one RngStream for the platform, to respect some statistic rules */
56 static RngStream sg_platf_rng_stream = NULL;
58 /** Module management function: creates all internal data structures */
59 void sg_platf_init(void) {
61 //FIXME : Ugly, but useful...
62 if(sg_platf_host_cb_list)
63 return; //Already initialized, so do nothing...
65 sg_platf_host_cb_list = xbt_dynar_new(sizeof(sg_platf_host_cb_t), NULL);
66 sg_platf_host_link_cb_list = xbt_dynar_new(sizeof(sg_platf_host_link_cb_t), NULL);
67 sg_platf_router_cb_list = xbt_dynar_new(sizeof(sg_platf_router_cb_t), NULL);
68 sg_platf_link_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t), NULL);
69 sg_platf_peer_cb_list = xbt_dynar_new(sizeof(sg_platf_peer_cb_t), NULL);
70 sg_platf_cluster_cb_list = xbt_dynar_new(sizeof(sg_platf_cluster_cb_t), NULL);
71 sg_platf_cabinet_cb_list = xbt_dynar_new(sizeof(sg_platf_cabinet_cb_t), NULL);
72 sg_platf_postparse_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t),NULL);
73 sg_platf_AS_begin_cb_list = xbt_dynar_new(sizeof(sg_platf_AS_cb_t),NULL);
74 sg_platf_AS_end_cb_list = xbt_dynar_new(sizeof(sg_platf_AS_cb_t),NULL);
75 sg_platf_prop_cb_list = xbt_dynar_new(sizeof(sg_platf_prop_cb_t),NULL);
77 sg_platf_route_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
78 sg_platf_ASroute_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
79 sg_platf_bypassRoute_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
80 sg_platf_bypassASroute_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
82 sg_platf_trace_cb_list = xbt_dynar_new(sizeof(sg_platf_trace_cb_t), NULL);
83 sg_platf_trace_connect_cb_list = xbt_dynar_new(sizeof(sg_platf_trace_connect_cb_t), NULL);
85 sg_platf_storage_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
86 sg_platf_storage_type_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
87 sg_platf_mstorage_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
88 sg_platf_mount_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
90 sg_platf_process_cb_list = xbt_dynar_new(sizeof(sg_platf_process_cb_t), NULL);
92 /* ***************************************** */
93 /* TUTORIAL: New TAG */
95 sg_platf_gpu_cb_list = xbt_dynar_new(sizeof(sg_platf_gpu_cb_t), NULL);
96 /* ***************************************** */
98 /** Module management function: frees all internal data structures */
99 void sg_platf_exit(void) {
100 xbt_dynar_free(&sg_platf_host_cb_list);
101 xbt_dynar_free(&sg_platf_host_link_cb_list);
102 xbt_dynar_free(&sg_platf_router_cb_list);
103 xbt_dynar_free(&sg_platf_link_cb_list);
104 xbt_dynar_free(&sg_platf_postparse_cb_list);
105 xbt_dynar_free(&sg_platf_peer_cb_list);
106 xbt_dynar_free(&sg_platf_cluster_cb_list);
107 xbt_dynar_free(&sg_platf_cabinet_cb_list);
108 xbt_dynar_free(&sg_platf_AS_begin_cb_list);
109 xbt_dynar_free(&sg_platf_AS_end_cb_list);
110 xbt_dynar_free(&sg_platf_prop_cb_list);
112 xbt_dynar_free(&sg_platf_trace_cb_list);
113 xbt_dynar_free(&sg_platf_trace_connect_cb_list);
115 xbt_dynar_free(&sg_platf_route_cb_list);
116 xbt_dynar_free(&sg_platf_ASroute_cb_list);
117 xbt_dynar_free(&sg_platf_bypassRoute_cb_list);
118 xbt_dynar_free(&sg_platf_bypassASroute_cb_list);
120 xbt_dynar_free(&sg_platf_storage_cb_list);
121 xbt_dynar_free(&sg_platf_storage_type_cb_list);
122 xbt_dynar_free(&sg_platf_mstorage_cb_list);
123 xbt_dynar_free(&sg_platf_mount_cb_list);
125 xbt_dynar_free(&sg_platf_process_cb_list);
127 /* ***************************************** */
128 /* TUTORIAL: New TAG */
130 xbt_dynar_free(&sg_platf_gpu_cb_list);
132 /* ***************************************** */
134 /* make sure that we will reinit the models while loading the platf once reinited */
135 surf_parse_models_setup_already_called = 0;
138 void sg_platf_new_host(sg_platf_host_cbarg_t h){
139 routing_parse_init(h);
143 unsigned int iterator;
144 sg_platf_host_cb_t fun;
145 xbt_dynar_foreach(sg_platf_host_cb_list, iterator, fun) {
149 void sg_platf_new_host_link(sg_platf_host_link_cbarg_t h){
150 unsigned int iterator;
151 sg_platf_host_link_cb_t fun;
152 xbt_dynar_foreach(sg_platf_host_link_cb_list, iterator, fun) {
156 void sg_platf_new_router(sg_platf_router_cbarg_t router) {
157 unsigned int iterator;
158 sg_platf_router_cb_t fun;
159 xbt_dynar_foreach(sg_platf_router_cb_list, iterator, fun) {
163 void sg_platf_new_link(sg_platf_link_cbarg_t link){
164 unsigned int iterator;
165 sg_platf_link_cb_t fun;
166 xbt_dynar_foreach(sg_platf_link_cb_list, iterator, fun) {
171 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer){
172 unsigned int iterator;
173 sg_platf_peer_cb_t fun;
174 xbt_dynar_foreach(sg_platf_peer_cb_list, iterator, fun) {
178 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster){
179 unsigned int iterator;
180 sg_platf_cluster_cb_t fun;
181 xbt_dynar_foreach(sg_platf_cluster_cb_list, iterator, fun) {
185 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet){
186 unsigned int iterator;
187 sg_platf_cabinet_cb_t fun;
188 xbt_dynar_foreach(sg_platf_cabinet_cb_list, iterator, fun) {
192 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage){
193 unsigned int iterator;
194 sg_platf_storage_cb_t fun;
195 xbt_dynar_foreach(sg_platf_storage_cb_list, iterator, fun) {
199 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
200 unsigned int iterator;
201 sg_platf_storage_type_cb_t fun;
202 xbt_dynar_foreach(sg_platf_storage_type_cb_list, iterator, fun) {
206 void sg_platf_new_mstorage(sg_platf_mstorage_cbarg_t mstorage){
207 unsigned int iterator;
208 sg_platf_mstorage_cb_t fun;
209 xbt_dynar_foreach(sg_platf_mstorage_cb_list, iterator, fun) {
213 void sg_platf_new_mount(sg_platf_mount_cbarg_t mount){
214 unsigned int iterator;
215 sg_platf_mount_cb_t fun;
216 xbt_dynar_foreach(sg_platf_mount_cb_list, iterator, fun) {
220 void sg_platf_new_route(sg_platf_route_cbarg_t route) {
221 unsigned int iterator;
222 sg_platf_route_cb_t fun;
223 xbt_dynar_foreach(sg_platf_route_cb_list, iterator, fun) {
227 void sg_platf_new_ASroute(sg_platf_route_cbarg_t ASroute) {
228 unsigned int iterator;
229 sg_platf_route_cb_t fun;
230 xbt_dynar_foreach(sg_platf_ASroute_cb_list, iterator, fun) {
234 void sg_platf_new_bypassRoute(sg_platf_route_cbarg_t bypassRoute) {
235 unsigned int iterator;
236 sg_platf_route_cb_t fun;
237 xbt_dynar_foreach(sg_platf_bypassRoute_cb_list, iterator, fun) {
241 void sg_platf_new_bypassASroute(sg_platf_route_cbarg_t bypassASroute) {
242 unsigned int iterator;
243 sg_platf_route_cb_t fun;
244 xbt_dynar_foreach(sg_platf_bypassASroute_cb_list, iterator, fun) {
248 void sg_platf_new_prop(sg_platf_prop_cbarg_t prop) {
249 unsigned int iterator;
250 sg_platf_prop_cb_t fun;
251 xbt_dynar_foreach(sg_platf_prop_cb_list, iterator, fun) {
255 void sg_platf_new_trace(sg_platf_trace_cbarg_t trace) {
256 unsigned int iterator;
257 sg_platf_trace_cb_t fun;
258 xbt_dynar_foreach(sg_platf_trace_cb_list, iterator, fun) {
262 void sg_platf_trace_connect(sg_platf_trace_connect_cbarg_t trace_connect) {
263 unsigned int iterator;
264 sg_platf_trace_connect_cb_t fun;
265 xbt_dynar_foreach(sg_platf_trace_connect_cb_list, iterator, fun) {
269 void sg_platf_new_process(sg_platf_process_cbarg_t process){
270 unsigned int iterator;
271 sg_platf_process_cb_t fun;
272 xbt_dynar_foreach(sg_platf_process_cb_list, iterator, fun) {
277 void sg_platf_route_begin (sg_platf_route_cbarg_t route){
278 route->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
280 void sg_platf_ASroute_begin (sg_platf_route_cbarg_t ASroute){
281 ASroute->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
284 void sg_platf_route_end (sg_platf_route_cbarg_t route){
285 sg_platf_new_route(route);
287 void sg_platf_ASroute_end (sg_platf_route_cbarg_t ASroute){
288 sg_platf_new_ASroute(ASroute);
291 void sg_platf_route_add_link (const char* link_id, sg_platf_route_cbarg_t route){
292 char *link_name = xbt_strdup(link_id);
293 xbt_dynar_push(route->link_list, &link_name);
295 void sg_platf_ASroute_add_link (const char* link_id, sg_platf_route_cbarg_t ASroute){
296 char *link_name = xbt_strdup(link_id);
297 xbt_dynar_push(ASroute->link_list, &link_name);
300 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
302 void sg_platf_end() {
303 unsigned int iterator;
305 xbt_dynar_foreach(sg_platf_postparse_cb_list, iterator, fun) {
310 void sg_platf_new_AS_begin(sg_platf_AS_cbarg_t AS) {
311 unsigned int iterator;
312 sg_platf_AS_cb_t fun;
314 if (!surf_parse_models_setup_already_called && !xbt_dynar_is_empty(sg_platf_AS_begin_cb_list)) {
315 /* Initialize the surf models. That must be done after we got all config, and before we need the models.
316 * That is, after the last <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
318 * I'm not sure for <trace> and <trace_connect>, there may be a bug here
319 * (FIXME: check it out by creating a file beginning with one of these tags)
320 * but cluster and peer create ASes internally, so putting the code in there is ok.
322 * We are also guarding against xbt_dynar_length(sg_platf_AS_begin_cb_list) because we don't
323 * want to initialize the models if we are parsing the file to get the deployment. That could happen if
324 * the same file would be used for platf and deploy: it'd contain AS tags even during the deploy parsing.
325 * Removing that guard would result of the models to get re-inited when parsing for deploy.
327 surf_parse_models_setup_already_called = 1;
328 surf_config_models_setup();
331 xbt_dynar_foreach(sg_platf_AS_begin_cb_list, iterator, fun) {
336 void sg_platf_new_AS_end() {
337 unsigned int iterator;
339 xbt_dynar_foreach(sg_platf_AS_end_cb_list, iterator, fun) {
344 /* ***************************************** */
345 /* TUTORIAL: New TAG */
347 void sg_platf_new_gpu(sg_platf_gpu_cbarg_t gpu) {
348 unsigned int iterator;
350 xbt_dynar_foreach(sg_platf_gpu_cb_list, iterator, fun) {
355 void sg_platf_gpu_add_cb(sg_platf_gpu_cb_t fct) {
356 xbt_dynar_push(sg_platf_gpu_cb_list, &fct);
359 /* ***************************************** */
362 void sg_platf_host_add_cb(sg_platf_host_cb_t fct) {
363 xbt_dynar_push(sg_platf_host_cb_list, &fct);
365 void sg_platf_host_link_add_cb(sg_platf_host_link_cb_t fct) {
366 xbt_dynar_push(sg_platf_host_link_cb_list, &fct);
368 void sg_platf_link_add_cb(sg_platf_link_cb_t fct) {
369 xbt_dynar_push(sg_platf_link_cb_list, &fct);
371 void sg_platf_router_add_cb(sg_platf_router_cb_t fct) {
372 xbt_dynar_push(sg_platf_router_cb_list, &fct);
374 void sg_platf_peer_add_cb(sg_platf_peer_cb_t fct) {
375 xbt_dynar_push(sg_platf_peer_cb_list, &fct);
377 void sg_platf_cluster_add_cb(sg_platf_cluster_cb_t fct) {
378 xbt_dynar_push(sg_platf_cluster_cb_list, &fct);
380 void sg_platf_cabinet_add_cb(sg_platf_cabinet_cb_t fct) {
381 xbt_dynar_push(sg_platf_cabinet_cb_list, &fct);
383 void sg_platf_postparse_add_cb(void_f_void_t fct) {
384 xbt_dynar_push(sg_platf_postparse_cb_list, &fct);
386 void sg_platf_AS_begin_add_cb(sg_platf_AS_cb_t fct) {
387 xbt_dynar_push(sg_platf_AS_begin_cb_list, &fct);
389 void sg_platf_AS_end_add_cb(sg_platf_AS_cb_t fct) {
390 xbt_dynar_push(sg_platf_AS_end_cb_list, &fct);
392 void sg_platf_storage_add_cb(sg_platf_storage_cb_t fct) {
393 xbt_dynar_push(sg_platf_storage_cb_list, &fct);
395 void sg_platf_storage_type_add_cb(sg_platf_storage_type_cb_t fct) {
396 xbt_dynar_push(sg_platf_storage_type_cb_list, &fct);
398 void sg_platf_mstorage_add_cb(sg_platf_mstorage_cb_t fct) {
399 xbt_dynar_push(sg_platf_mstorage_cb_list, &fct);
401 void sg_platf_mount_add_cb(sg_platf_mount_cb_t fct) {
402 xbt_dynar_push(sg_platf_mount_cb_list, &fct);
404 void sg_platf_route_add_cb(sg_platf_route_cb_t fct) {
405 xbt_dynar_push(sg_platf_route_cb_list, &fct);
407 void sg_platf_ASroute_add_cb(sg_platf_route_cb_t fct) {
408 xbt_dynar_push(sg_platf_ASroute_cb_list, &fct);
410 void sg_platf_bypassRoute_add_cb(sg_platf_route_cb_t fct) {
411 xbt_dynar_push(sg_platf_bypassRoute_cb_list, &fct);
413 void sg_platf_bypassASroute_add_cb(sg_platf_route_cb_t fct) {
414 xbt_dynar_push(sg_platf_bypassASroute_cb_list, &fct);
416 void sg_platf_prop_add_cb(sg_platf_prop_cb_t fct) {
417 xbt_dynar_push(sg_platf_prop_cb_list, &fct);
419 void sg_platf_trace_add_cb(sg_platf_trace_cb_t fct) {
420 xbt_dynar_push(sg_platf_trace_cb_list, &fct);
422 void sg_platf_trace_connect_add_cb(sg_platf_trace_connect_cb_t fct) {
423 xbt_dynar_push(sg_platf_trace_connect_cb_list, &fct);
425 void sg_platf_rng_stream_init(unsigned long seed[6]) {
426 RngStream_SetPackageSeed(seed);
427 sg_platf_rng_stream = RngStream_CreateStream(NULL);
429 void sg_platf_process_add_cb(sg_platf_process_cb_t fct) {
430 xbt_dynar_push(sg_platf_process_cb_list, &fct);
433 RngStream sg_platf_rng_stream_get(const char* id) {
434 RngStream stream = NULL;
435 unsigned int id_hash;
437 stream = RngStream_CopyStream(sg_platf_rng_stream);
438 id_hash = xbt_str_hash(id);
439 RngStream_AdvanceState(stream, 0, (long)id_hash);