Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[surf] Remove some layers of indirections in sg_platf_new_host()
[simgrid.git] / src / surf / sg_platf.cpp
1 /* Copyright (c) 2006-2014. 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 #include "surf/surf_routing.h"
14
15 #include "cpu_interface.hpp"
16 #include "host_interface.hpp"
17
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
30
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
35
36 xbt_dynar_t sg_platf_trace_cb_list = NULL;
37 xbt_dynar_t sg_platf_trace_connect_cb_list = NULL;
38
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
43
44 xbt_dynar_t sg_platf_process_cb_list = NULL;
45
46 /* ***************************************** */
47 /* TUTORIAL: New TAG                         */
48
49 xbt_dynar_t sg_platf_gpu_cb_list = NULL;
50 /* ***************************************** */
51
52
53 static int surf_parse_models_setup_already_called = 0;
54
55 /* one RngStream for the platform, to respect some statistic rules */
56 static RngStream sg_platf_rng_stream = NULL;
57
58 /** Module management function: creates all internal data structures */
59 void sg_platf_init(void) {
60
61   //FIXME : Ugly, but useful...
62   if(sg_platf_host_cb_list)
63     return; //Already initialized, so do nothing...
64
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);
76
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);
81
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);
84
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);
89
90   sg_platf_process_cb_list = xbt_dynar_new(sizeof(sg_platf_process_cb_t), NULL);
91
92   /* ***************************************** */
93   /* TUTORIAL: New TAG                         */
94
95   sg_platf_gpu_cb_list = xbt_dynar_new(sizeof(sg_platf_gpu_cb_t), NULL);
96   /* ***************************************** */
97 }
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);
111
112   xbt_dynar_free(&sg_platf_trace_cb_list);
113   xbt_dynar_free(&sg_platf_trace_connect_cb_list);
114
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);
119
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);
124
125   xbt_dynar_free(&sg_platf_process_cb_list);
126
127   /* ***************************************** */
128   /* TUTORIAL: New TAG                         */
129
130   xbt_dynar_free(&sg_platf_gpu_cb_list);
131
132   /* ***************************************** */
133
134   /* make sure that we will reinit the models while loading the platf once reinited */
135   surf_parse_models_setup_already_called = 0;
136 }
137
138 void sg_platf_new_host(sg_platf_host_cbarg_t host)
139 {
140   routing_parse_init(host);
141   surf_cpu_model_pm->createCpu(
142         host->id,
143         host->power_peak,
144         host->pstate,
145         host->power_scale,
146         host->power_trace,
147         host->core_amount,
148         host->initial_state,
149         host->state_trace,
150         host->properties);
151   surf_host_model->createHost(host->id);
152
153   unsigned int iterator;
154   sg_platf_host_cb_t fun;
155   xbt_dynar_foreach(sg_platf_host_cb_list, iterator, fun) {
156     fun(host);
157   }
158 }
159 void sg_platf_new_host_link(sg_platf_host_link_cbarg_t h){
160   unsigned int iterator;
161   sg_platf_host_link_cb_t fun;
162   xbt_dynar_foreach(sg_platf_host_link_cb_list, iterator, fun) {
163     fun(h);
164   }
165 }
166 void sg_platf_new_router(sg_platf_router_cbarg_t router) {
167   unsigned int iterator;
168   sg_platf_router_cb_t fun;
169   xbt_dynar_foreach(sg_platf_router_cb_list, iterator, fun) {
170     fun(router);
171   }
172 }
173 void sg_platf_new_link(sg_platf_link_cbarg_t link){
174   unsigned int iterator;
175   sg_platf_link_cb_t fun;
176   xbt_dynar_foreach(sg_platf_link_cb_list, iterator, fun) {
177     fun(link);
178   }
179 }
180
181 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer){
182   unsigned int iterator;
183   sg_platf_peer_cb_t fun;
184   xbt_dynar_foreach(sg_platf_peer_cb_list, iterator, fun) {
185     fun(peer);
186   }
187 }
188 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster){
189   unsigned int iterator;
190   sg_platf_cluster_cb_t fun;
191   xbt_dynar_foreach(sg_platf_cluster_cb_list, iterator, fun) {
192     fun(cluster);
193   }
194 }
195 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet){
196   unsigned int iterator;
197   sg_platf_cabinet_cb_t fun;
198   xbt_dynar_foreach(sg_platf_cabinet_cb_list, iterator, fun) {
199     fun(cabinet);
200   }
201 }
202 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage){
203   unsigned int iterator;
204   sg_platf_storage_cb_t fun;
205   xbt_dynar_foreach(sg_platf_storage_cb_list, iterator, fun) {
206     fun(storage);
207   }
208 }
209 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
210   unsigned int iterator;
211   sg_platf_storage_type_cb_t fun;
212   xbt_dynar_foreach(sg_platf_storage_type_cb_list, iterator, fun) {
213     fun(storage_type);
214   }
215 }
216 void sg_platf_new_mstorage(sg_platf_mstorage_cbarg_t mstorage){
217   unsigned int iterator;
218   sg_platf_mstorage_cb_t fun;
219   xbt_dynar_foreach(sg_platf_mstorage_cb_list, iterator, fun) {
220     fun(mstorage);
221   }
222 }
223 void sg_platf_new_mount(sg_platf_mount_cbarg_t mount){
224   unsigned int iterator;
225   sg_platf_mount_cb_t fun;
226   xbt_dynar_foreach(sg_platf_mount_cb_list, iterator, fun) {
227     fun(mount);
228   }
229 }
230 void sg_platf_new_route(sg_platf_route_cbarg_t route) {
231   unsigned int iterator;
232   sg_platf_route_cb_t fun;
233   xbt_dynar_foreach(sg_platf_route_cb_list, iterator, fun) {
234     fun(route);
235   }
236 }
237 void sg_platf_new_ASroute(sg_platf_route_cbarg_t ASroute) {
238   unsigned int iterator;
239   sg_platf_route_cb_t fun;
240   xbt_dynar_foreach(sg_platf_ASroute_cb_list, iterator, fun) {
241     fun(ASroute);
242   }
243 }
244 void sg_platf_new_bypassRoute(sg_platf_route_cbarg_t bypassRoute) {
245   unsigned int iterator;
246   sg_platf_route_cb_t fun;
247   xbt_dynar_foreach(sg_platf_bypassRoute_cb_list, iterator, fun) {
248     fun(bypassRoute);
249   }
250 }
251 void sg_platf_new_bypassASroute(sg_platf_route_cbarg_t bypassASroute) {
252   unsigned int iterator;
253   sg_platf_route_cb_t fun;
254   xbt_dynar_foreach(sg_platf_bypassASroute_cb_list, iterator, fun) {
255     fun(bypassASroute);
256   }
257 }
258 void sg_platf_new_prop(sg_platf_prop_cbarg_t prop) {
259   unsigned int iterator;
260   sg_platf_prop_cb_t fun;
261   xbt_dynar_foreach(sg_platf_prop_cb_list, iterator, fun) {
262     fun(prop);
263   }
264 }
265 void sg_platf_new_trace(sg_platf_trace_cbarg_t trace) {
266   unsigned int iterator;
267   sg_platf_trace_cb_t fun;
268   xbt_dynar_foreach(sg_platf_trace_cb_list, iterator, fun) {
269     fun(trace);
270   }
271 }
272 void sg_platf_trace_connect(sg_platf_trace_connect_cbarg_t trace_connect) {
273   unsigned int iterator;
274   sg_platf_trace_connect_cb_t fun;
275   xbt_dynar_foreach(sg_platf_trace_connect_cb_list, iterator, fun) {
276     fun(trace_connect);
277   }
278 }
279 void sg_platf_new_process(sg_platf_process_cbarg_t process){
280   unsigned int iterator;
281   sg_platf_process_cb_t fun;
282   xbt_dynar_foreach(sg_platf_process_cb_list, iterator, fun) {
283     fun(process);
284   }
285 }
286
287 void sg_platf_route_begin (sg_platf_route_cbarg_t route){
288   route->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
289 }
290 void sg_platf_ASroute_begin (sg_platf_route_cbarg_t ASroute){
291   ASroute->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
292 }
293
294 void sg_platf_route_end (sg_platf_route_cbarg_t route){
295   sg_platf_new_route(route);
296 }
297 void sg_platf_ASroute_end (sg_platf_route_cbarg_t ASroute){
298   sg_platf_new_ASroute(ASroute);
299 }
300
301 void sg_platf_route_add_link (const char* link_id, sg_platf_route_cbarg_t route){
302   char *link_name = xbt_strdup(link_id);
303   xbt_dynar_push(route->link_list, &link_name);
304 }
305 void sg_platf_ASroute_add_link (const char* link_id, sg_platf_route_cbarg_t ASroute){
306   char *link_name = xbt_strdup(link_id);
307   xbt_dynar_push(ASroute->link_list, &link_name);
308 }
309
310 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
311
312 void sg_platf_end() {
313   unsigned int iterator;
314   void_f_void_t fun;
315   xbt_dynar_foreach(sg_platf_postparse_cb_list, iterator, fun) {
316     fun();
317   }
318 }
319
320 void sg_platf_new_AS_begin(sg_platf_AS_cbarg_t AS) {
321   unsigned int iterator;
322   sg_platf_AS_cb_t fun;
323
324   if (!surf_parse_models_setup_already_called && !xbt_dynar_is_empty(sg_platf_AS_begin_cb_list)) {
325     /* Initialize the surf models. That must be done after we got all config, and before we need the models.
326      * That is, after the last <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
327      *
328      * I'm not sure for <trace> and <trace_connect>, there may be a bug here
329      * (FIXME: check it out by creating a file beginning with one of these tags)
330      * but cluster and peer create ASes internally, so putting the code in there is ok.
331      *
332      * We are also guarding against xbt_dynar_length(sg_platf_AS_begin_cb_list) because we don't
333      * want to initialize the models if we are parsing the file to get the deployment. That could happen if
334      * the same file would be used for platf and deploy: it'd contain AS tags even during the deploy parsing.
335      * Removing that guard would result of the models to get re-inited when parsing for deploy.
336      */
337     surf_parse_models_setup_already_called = 1;
338     surf_config_models_setup();
339   }
340
341   xbt_dynar_foreach(sg_platf_AS_begin_cb_list, iterator, fun) {
342     fun(AS);
343   }
344 }
345
346 void sg_platf_new_AS_end() {
347   unsigned int iterator;
348   void_f_void_t fun;
349   xbt_dynar_foreach(sg_platf_AS_end_cb_list, iterator, fun) {
350     fun();
351   }
352 }
353
354 /* ***************************************** */
355 /* TUTORIAL: New TAG                         */
356
357 void sg_platf_new_gpu(sg_platf_gpu_cbarg_t gpu) {
358   unsigned int iterator;
359   void_f_void_t fun;
360   xbt_dynar_foreach(sg_platf_gpu_cb_list, iterator, fun) {
361     fun();
362   }
363 }
364
365 void sg_platf_gpu_add_cb(sg_platf_gpu_cb_t fct) {
366   xbt_dynar_push(sg_platf_gpu_cb_list, &fct);
367 }
368
369 /* ***************************************** */
370
371
372 void sg_platf_host_add_cb(sg_platf_host_cb_t fct) {
373   xbt_dynar_push(sg_platf_host_cb_list, &fct);
374 }
375 void sg_platf_host_link_add_cb(sg_platf_host_link_cb_t fct) {
376   xbt_dynar_push(sg_platf_host_link_cb_list, &fct);
377 }
378 void sg_platf_link_add_cb(sg_platf_link_cb_t fct) {
379   xbt_dynar_push(sg_platf_link_cb_list, &fct);
380 }
381 void sg_platf_router_add_cb(sg_platf_router_cb_t fct) {
382   xbt_dynar_push(sg_platf_router_cb_list, &fct);
383 }
384 void sg_platf_peer_add_cb(sg_platf_peer_cb_t fct) {
385   xbt_dynar_push(sg_platf_peer_cb_list, &fct);
386 }
387 void sg_platf_cluster_add_cb(sg_platf_cluster_cb_t fct) {
388   xbt_dynar_push(sg_platf_cluster_cb_list, &fct);
389 }
390 void sg_platf_cabinet_add_cb(sg_platf_cabinet_cb_t fct) {
391   xbt_dynar_push(sg_platf_cabinet_cb_list, &fct);
392 }
393 void sg_platf_postparse_add_cb(void_f_void_t fct) {
394   xbt_dynar_push(sg_platf_postparse_cb_list, &fct);
395 }
396 void sg_platf_AS_begin_add_cb(sg_platf_AS_cb_t fct) {
397   xbt_dynar_push(sg_platf_AS_begin_cb_list, &fct);
398 }
399 void sg_platf_AS_end_add_cb(sg_platf_AS_cb_t fct) {
400   xbt_dynar_push(sg_platf_AS_end_cb_list, &fct);
401 }
402 void sg_platf_storage_add_cb(sg_platf_storage_cb_t fct) {
403   xbt_dynar_push(sg_platf_storage_cb_list, &fct);
404 }
405 void sg_platf_storage_type_add_cb(sg_platf_storage_type_cb_t fct) {
406   xbt_dynar_push(sg_platf_storage_type_cb_list, &fct);
407 }
408 void sg_platf_mstorage_add_cb(sg_platf_mstorage_cb_t fct) {
409   xbt_dynar_push(sg_platf_mstorage_cb_list, &fct);
410 }
411 void sg_platf_mount_add_cb(sg_platf_mount_cb_t fct) {
412   xbt_dynar_push(sg_platf_mount_cb_list, &fct);
413 }
414 void sg_platf_route_add_cb(sg_platf_route_cb_t fct) {
415   xbt_dynar_push(sg_platf_route_cb_list, &fct);
416 }
417 void sg_platf_ASroute_add_cb(sg_platf_route_cb_t fct) {
418   xbt_dynar_push(sg_platf_ASroute_cb_list, &fct);
419 }
420 void sg_platf_bypassRoute_add_cb(sg_platf_route_cb_t fct) {
421   xbt_dynar_push(sg_platf_bypassRoute_cb_list, &fct);
422 }
423 void sg_platf_bypassASroute_add_cb(sg_platf_route_cb_t fct) {
424   xbt_dynar_push(sg_platf_bypassASroute_cb_list, &fct);
425 }
426 void sg_platf_prop_add_cb(sg_platf_prop_cb_t fct) {
427   xbt_dynar_push(sg_platf_prop_cb_list, &fct);
428 }
429 void sg_platf_trace_add_cb(sg_platf_trace_cb_t fct) {
430   xbt_dynar_push(sg_platf_trace_cb_list, &fct);
431 }
432 void sg_platf_trace_connect_add_cb(sg_platf_trace_connect_cb_t fct) {
433   xbt_dynar_push(sg_platf_trace_connect_cb_list, &fct);
434 }
435 void sg_platf_rng_stream_init(unsigned long seed[6]) {
436   RngStream_SetPackageSeed(seed);
437   sg_platf_rng_stream = RngStream_CreateStream(NULL);
438 }
439 void sg_platf_process_add_cb(sg_platf_process_cb_t fct) {
440   xbt_dynar_push(sg_platf_process_cb_list, &fct);
441 }
442
443 RngStream sg_platf_rng_stream_get(const char* id) {
444   RngStream stream = NULL;
445   unsigned int id_hash;
446
447   stream = RngStream_CopyStream(sg_platf_rng_stream);
448   id_hash = xbt_str_hash(id);
449   RngStream_AdvanceState(stream, 0, (long)id_hash);
450
451   return stream;
452 }