Logo AND Algorithmique Numérique Distribuée

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