Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[surf] Remove sg_platf_ASRoute_cb
[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 #include "surf/surf.h"
15
16 #include "src/simix/smx_private.h"
17
18 #include "cpu_interface.hpp"
19 #include "host_interface.hpp"
20
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
22 xbt_dynar_t sg_platf_link_cb_list = NULL;   // of sg_platf_link_cb_t
23 xbt_dynar_t sg_platf_cluster_cb_list = NULL; // of sg_platf_cluster_cb_t
24 xbt_dynar_t sg_platf_postparse_cb_list = NULL; // of void_f_void_t
25
26 /* ***************************************** */
27 /* TUTORIAL: New TAG                         */
28
29 xbt_dynar_t sg_platf_gpu_cb_list = NULL;
30 /* ***************************************** */
31
32
33 static int surf_parse_models_setup_already_called = 0;
34
35 /* one RngStream for the platform, to respect some statistic rules */
36 static RngStream sg_platf_rng_stream = NULL;
37
38 /** Module management function: creates all internal data structures */
39 void sg_platf_init(void) {
40
41   //FIXME : Ugly, but useful...
42   if (sg_platf_gpu_cb_list)
43     return; //Already initialized, so do nothing...
44
45   sg_platf_link_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t), NULL);
46   sg_platf_cluster_cb_list = xbt_dynar_new(sizeof(sg_platf_cluster_cb_t), NULL);
47   sg_platf_postparse_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t),NULL);
48
49   /* ***************************************** */
50   /* TUTORIAL: New TAG                         */
51
52   sg_platf_gpu_cb_list = xbt_dynar_new(sizeof(sg_platf_gpu_cb_t), NULL);
53   /* ***************************************** */
54 }
55 /** Module management function: frees all internal data structures */
56 void sg_platf_exit(void) {
57   xbt_dynar_free(&sg_platf_link_cb_list);
58   xbt_dynar_free(&sg_platf_postparse_cb_list);
59   xbt_dynar_free(&sg_platf_cluster_cb_list);
60
61   /* ***************************************** */
62   /* TUTORIAL: New TAG                         */
63
64   xbt_dynar_free(&sg_platf_gpu_cb_list);
65
66   /* ***************************************** */
67
68   /* make sure that we will reinit the models while loading the platf once reinited */
69   surf_parse_models_setup_already_called = 0;
70 }
71
72 void sg_platf_new_host(sg_platf_host_cbarg_t host)
73 {
74   xbt_assert(! sg_host_by_name(host->id),
75                      "Refusing to create a second host named '%s'.", host->id);
76
77   RoutingEdge *net = NULL;
78   As* current_routing = routing_get_current();
79   if (current_routing)
80     net = routing_add_host(current_routing, host);
81
82   Cpu *cpu = surf_cpu_model_pm->createCpu(
83         host->id,
84         host->power_peak,
85         host->pstate,
86         host->power_scale,
87         host->power_trace,
88         host->core_amount,
89         host->initial_state,
90         host->state_trace,
91         host->properties);
92   surf_host_model->createHost(host->id, net, cpu);
93
94   if (TRACE_is_enabled() && TRACE_needs_platform())
95     sg_instr_new_host(host);
96 }
97
98 /**
99  * \brief Add a "router" to the network element list
100  */
101 void sg_platf_new_router(sg_platf_router_cbarg_t router)
102 {
103   As* current_routing = routing_get_current();
104
105   if (current_routing->p_hierarchy == SURF_ROUTING_NULL)
106     current_routing->p_hierarchy = SURF_ROUTING_BASE;
107   xbt_assert(!xbt_lib_get_or_null(as_router_lib, router->id, ROUTING_ASR_LEVEL),
108              "Reading a router, processing unit \"%s\" already exists",
109              router->id);
110
111   RoutingEdge *info = new RoutingEdgeImpl(xbt_strdup(router->id),
112                                             -1,
113                                             SURF_NETWORK_ELEMENT_ROUTER,
114                                             current_routing);
115   info->setId(current_routing->parsePU(info));
116   xbt_lib_set(as_router_lib, router->id, ROUTING_ASR_LEVEL, (void *) info);
117   XBT_DEBUG("Having set name '%s' id '%d'", router->id, info->getId());
118   routingEdgeCreatedCallbacks(info);
119
120   if (router->coord && strcmp(router->coord, "")) {
121     unsigned int cursor;
122     char*str;
123
124     if (!COORD_ASR_LEVEL)
125       xbt_die ("To use host coordinates, please add --cfg=network/coordinates:yes to your command line");
126     /* Pre-parse the host coordinates */
127     xbt_dynar_t ctn_str = xbt_str_split_str(router->coord, " ");
128     xbt_dynar_t ctn = xbt_dynar_new(sizeof(double),NULL);
129     xbt_dynar_foreach(ctn_str,cursor, str) {
130       double val = atof(str);
131       xbt_dynar_push(ctn,&val);
132     }
133     xbt_dynar_shrink(ctn, 0);
134     xbt_dynar_free(&ctn_str);
135     xbt_lib_set(as_router_lib, router->id, COORD_ASR_LEVEL, (void *) ctn);
136     XBT_DEBUG("Having set router coordinates for '%s'",router->id);
137   }
138
139   if (TRACE_is_enabled())
140     sg_instr_new_router(router);
141 }
142
143 void sg_platf_new_link(sg_platf_link_cbarg_t link){
144   unsigned int iterator;
145   sg_platf_link_cb_t fun;
146   xbt_dynar_foreach(sg_platf_link_cb_list, iterator, fun) {
147     fun(link);
148   }
149 }
150
151 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster){
152   unsigned int iterator;
153   sg_platf_cluster_cb_t fun;
154   xbt_dynar_foreach(sg_platf_cluster_cb_list, iterator, fun) {
155     fun(cluster);
156   }
157 }
158
159 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage)
160 {
161   xbt_assert(!xbt_lib_get_or_null(storage_lib, storage->id,ROUTING_STORAGE_LEVEL),
162                "Reading a storage, processing unit \"%s\" already exists", storage->id);
163
164   // Verification of an existing type_id
165 #ifndef NDEBUG
166   void* storage_type = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
167 #endif
168   xbt_assert(storage_type,"Reading a storage, type id \"%s\" does not exists", storage->type_id);
169
170   XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
171       storage->id,
172       storage->type_id,
173       storage->content);
174
175   xbt_lib_set(storage_lib,
176       storage->id,
177       ROUTING_STORAGE_LEVEL,
178       (void *) xbt_strdup(storage->type_id));
179
180   void* stype = xbt_lib_get_or_null(storage_type_lib,
181                                     storage->type_id,
182                                     ROUTING_STORAGE_TYPE_LEVEL);
183   if(!stype) xbt_die("No storage type '%s'",storage->type_id);
184
185   // if storage content is not specified use the content of storage_type if exist
186   if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
187     storage->content = ((storage_type_t) stype)->content;
188     storage->content_type = ((storage_type_t) stype)->content_type;
189     XBT_DEBUG("For disk '%s' content is empty, inherit the content (of type %s) from storage type '%s' ",
190         storage->id,((storage_type_t) stype)->content_type,
191         ((storage_type_t) stype)->type_id);
192   }
193
194   XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s' "
195       "\n\t\tmodel '%s' \n\t\tcontent '%s'\n\t\tcontent_type '%s' "
196       "\n\t\tproperties '%p''\n",
197       storage->id,
198       ((storage_type_t) stype)->model,
199       ((storage_type_t) stype)->type_id,
200       storage->content,
201       storage->content_type,
202       storage->properties);
203
204   surf_storage_model->createStorage(storage->id,
205                                      ((storage_type_t) stype)->type_id,
206                                      storage->content,
207                                      storage->content_type,
208                                      storage->properties,
209                                      storage->attach);
210 }
211 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
212
213   xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
214                "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
215
216   storage_type_t stype = xbt_new0(s_storage_type_t, 1);
217   stype->model = xbt_strdup(storage_type->model);
218   stype->properties = storage_type->properties;
219   stype->content = xbt_strdup(storage_type->content);
220   stype->content_type = xbt_strdup(storage_type->content_type);
221   stype->type_id = xbt_strdup(storage_type->id);
222   stype->size = storage_type->size;
223   stype->model_properties = storage_type->model_properties;
224
225   XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
226       "content '%s', and content_type '%s'",
227       stype->type_id,
228       stype->model,
229       storage_type->content,
230       storage_type->content_type);
231
232   xbt_lib_set(storage_type_lib,
233       stype->type_id,
234       ROUTING_STORAGE_TYPE_LEVEL,
235       (void *) stype);
236 }
237 void sg_platf_new_mstorage(sg_platf_mstorage_cbarg_t mstorage)
238 {
239   THROW_UNIMPLEMENTED;
240 //  mount_t mnt = xbt_new0(s_mount_t, 1);
241 //  mnt->id = xbt_strdup(mstorage->type_id);
242 //  mnt->name = xbt_strdup(mstorage->name);
243 //
244 //  if(!mount_list){
245 //    XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
246 //    mount_list = xbt_dynar_new(sizeof(char *), NULL);
247 //  }
248 //  xbt_dynar_push(mount_list,(void *) mnt);
249 //  free(mnt->id);
250 //  free(mnt->name);
251 //  xbt_free(mnt);
252 //  XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
253 }
254
255 static void mount_free(void *p)
256 {
257   mount_t mnt = (mount_t) p;
258   xbt_free(mnt->name);
259 }
260
261 void sg_platf_new_mount(sg_platf_mount_cbarg_t mount){
262   // Verification of an existing storage
263 #ifndef NDEBUG
264   void* storage = xbt_lib_get_or_null(storage_lib, mount->storageId, ROUTING_STORAGE_LEVEL);
265 #endif
266   xbt_assert(storage,"Disk id \"%s\" does not exists", mount->storageId);
267
268   XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
269
270   s_mount_t mnt;
271   mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
272   mnt.name = xbt_strdup(mount->name);
273
274   if(!mount_list){
275     XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
276     mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
277   }
278   xbt_dynar_push(mount_list, &mnt);
279 }
280
281 void sg_platf_new_route(sg_platf_route_cbarg_t route)
282 {
283   routing_get_current()->parseRoute(route);
284 }
285
286 void sg_platf_new_ASroute(sg_platf_route_cbarg_t ASroute)
287 {
288   routing_get_current()->parseASroute(ASroute);
289 }
290
291 void sg_platf_new_bypassRoute(sg_platf_route_cbarg_t bypassRoute)
292 {
293   routing_get_current()->parseBypassroute(bypassRoute);
294 }
295
296 void sg_platf_new_bypassASroute(sg_platf_route_cbarg_t bypassASroute)
297 {
298   routing_get_current()->parseBypassroute(bypassASroute);
299 }
300
301 void sg_platf_new_process(sg_platf_process_cbarg_t process)
302 {
303   if (!simix_global)
304     xbt_die("Cannot create process without SIMIX.");
305
306   sg_host_t host = sg_host_by_name(process->host);
307   if (!host)
308     THROWF(arg_error, 0, "Host '%s' unknown", process->host);
309   xbt_main_func_t parse_code = SIMIX_get_registered_function(process->function);
310   xbt_assert(parse_code, "Function '%s' unknown", process->function);
311
312   double start_time = process->start_time;
313   double kill_time  = process->kill_time;
314   int auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1;
315
316   smx_process_arg_t arg = NULL;
317   smx_process_t process_created = NULL;
318
319   arg = xbt_new0(s_smx_process_arg_t, 1);
320   arg->code = parse_code;
321   arg->data = NULL;
322   arg->hostname = sg_host_get_name(host);
323   arg->argc = process->argc;
324   arg->argv = xbt_new(char *,process->argc);
325   int i;
326   for (i=0; i<process->argc; i++)
327     arg->argv[i] = xbt_strdup(process->argv[i]);
328   arg->name = xbt_strdup(arg->argv[0]);
329   arg->kill_time = kill_time;
330   arg->properties = current_property_set;
331   if (!sg_host_simix(host)->boot_processes) {
332     sg_host_simix(host)->boot_processes = xbt_dynar_new(sizeof(smx_process_arg_t), _SIMIX_host_free_process_arg);
333   }
334   xbt_dynar_push_as(sg_host_simix(host)->boot_processes,smx_process_arg_t,arg);
335
336   if (start_time > SIMIX_get_clock()) {
337     arg = xbt_new0(s_smx_process_arg_t, 1);
338     arg->name = (char*)(process->argv)[0];
339     arg->code = parse_code;
340     arg->data = NULL;
341     arg->hostname = sg_host_get_name(host);
342     arg->argc = process->argc;
343     arg->argv = (char**)(process->argv);
344     arg->kill_time = kill_time;
345     arg->properties = current_property_set;
346
347     XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
348            arg->hostname, start_time);
349     SIMIX_timer_set(start_time, [](void* arg) {
350       SIMIX_process_create_from_wrapper((smx_process_arg_t) arg);
351     }, arg);
352   } else {                      // start_time <= SIMIX_get_clock()
353     XBT_DEBUG("Starting Process %s(%s) right now", process->argv[0], sg_host_get_name(host));
354
355     if (simix_global->create_process_function)
356       process_created = simix_global->create_process_function(
357                                             (char*)(process->argv)[0],
358                                             parse_code,
359                                             NULL,
360                                             sg_host_get_name(host),
361                                             kill_time,
362                                             process->argc,
363                                             (char**)(process->argv),
364                                             current_property_set,
365                                             auto_restart, NULL);
366     else
367       process_created = simcall_process_create((char*)(process->argv)[0], parse_code, NULL, sg_host_get_name(host), kill_time, process->argc,
368           (char**)process->argv, current_property_set,auto_restart);
369
370     /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
371     if (!process_created) {
372       return;
373     }
374   }
375   current_property_set = NULL;
376 }
377
378 void sg_platf_route_begin (sg_platf_route_cbarg_t route){
379   route->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
380 }
381 void sg_platf_ASroute_begin (sg_platf_route_cbarg_t ASroute){
382   ASroute->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
383 }
384
385 void sg_platf_route_end (sg_platf_route_cbarg_t route){
386   sg_platf_new_route(route);
387 }
388 void sg_platf_ASroute_end (sg_platf_route_cbarg_t ASroute){
389   sg_platf_new_ASroute(ASroute);
390 }
391
392 void sg_platf_route_add_link (const char* link_id, sg_platf_route_cbarg_t route){
393   char *link_name = xbt_strdup(link_id);
394   xbt_dynar_push(route->link_list, &link_name);
395 }
396 void sg_platf_ASroute_add_link (const char* link_id, sg_platf_route_cbarg_t ASroute){
397   char *link_name = xbt_strdup(link_id);
398   xbt_dynar_push(ASroute->link_list, &link_name);
399 }
400
401 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
402
403 void sg_platf_end() {
404   unsigned int iterator;
405   void_f_void_t fun;
406   xbt_dynar_foreach(sg_platf_postparse_cb_list, iterator, fun) {
407     fun();
408   }
409 }
410
411 void sg_platf_new_AS_begin(sg_platf_AS_cbarg_t AS)
412 {
413   if (!surf_parse_models_setup_already_called) {
414     /* Initialize the surf models. That must be done after we got all config, and before we need the models.
415      * That is, after the last <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
416      *
417      * I'm not sure for <trace> and <trace_connect>, there may be a bug here
418      * (FIXME: check it out by creating a file beginning with one of these tags)
419      * but cluster and peer create ASes internally, so putting the code in there is ok.
420      *
421      * TODO, There used to be a guard protecting here against
422      * xbt_dynar_length(sg_platf_AS_begin_cb_list) because we don't want to
423      * initialize the models if we are parsing the file to get the deployment.
424      * That could happen if the same file would be used for platf and deploy:
425      * it'd contain AS tags even during the deploy parsing. Removing that guard
426      * would result of the models to get re-inited when parsing for deploy.
427      * Currently using the same file for platform and deployment is broken
428      * however. This guard will have to ba adapted in order to make this feature
429      * work again.
430      */
431     surf_parse_models_setup_already_called = 1;
432     surf_config_models_setup();
433   }
434
435   routing_AS_begin(AS);
436   if (TRACE_is_enabled())
437     sg_instr_AS_begin(AS);
438 }
439
440 void sg_platf_new_AS_end()
441 {
442   routing_AS_end();
443   if (TRACE_is_enabled())
444     sg_instr_AS_end();
445 }
446
447 /* ***************************************** */
448 /* TUTORIAL: New TAG                         */
449
450 void sg_platf_new_gpu(sg_platf_gpu_cbarg_t gpu) {
451   unsigned int iterator;
452   void_f_void_t fun;
453   xbt_dynar_foreach(sg_platf_gpu_cb_list, iterator, fun) {
454     fun();
455   }
456 }
457
458 void sg_platf_gpu_add_cb(sg_platf_gpu_cb_t fct) {
459   xbt_dynar_push(sg_platf_gpu_cb_list, &fct);
460 }
461
462 /* ***************************************** */
463
464 void sg_platf_link_add_cb(sg_platf_link_cb_t fct) {
465   xbt_dynar_push(sg_platf_link_cb_list, &fct);
466 }
467 void sg_platf_cluster_add_cb(sg_platf_cluster_cb_t fct) {
468   xbt_dynar_push(sg_platf_cluster_cb_list, &fct);
469 }
470 void sg_platf_postparse_add_cb(void_f_void_t fct) {
471   xbt_dynar_push(sg_platf_postparse_cb_list, &fct);
472 }
473
474 void sg_platf_rng_stream_init(unsigned long seed[6]) {
475   RngStream_SetPackageSeed(seed);
476   sg_platf_rng_stream = RngStream_CreateStream(NULL);
477 }
478
479 RngStream sg_platf_rng_stream_get(const char* id) {
480   RngStream stream = NULL;
481   unsigned int id_hash;
482
483   stream = RngStream_CopyStream(sg_platf_rng_stream);
484   id_hash = xbt_str_hash(id);
485   RngStream_AdvanceState(stream, 0, (long)id_hash);
486
487   return stream;
488 }